blob: 17f3064b4a3ebf7b65188be58b66713670f00ca9 [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 Masonf03d9302009-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 Masonf03d9302009-02-04 09:31:06 -0500560cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500561 /*
562 * No compression, but we still need to write the pages in
563 * the file we've been given so far. redirty the locked
564 * page if it corresponds to our extent and set things up
565 * for the async work queue to run cow_file_range to do
566 * the normal delalloc dance
567 */
568 if (page_offset(locked_page) >= start &&
569 page_offset(locked_page) <= end) {
570 __set_page_dirty_nobuffers(locked_page);
571 /* unlocked later on in the async handlers */
572 }
Chris Mason4adaa612013-03-26 13:07:00 -0400573 if (redirty)
574 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800575 add_async_extent(async_cow, start, end - start + 1,
576 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500577 *num_added += 1;
578 }
579
580out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100581 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500582
583free_pages_out:
584 for (i = 0; i < nr_pages_ret; i++) {
585 WARN_ON(pages[i]->mapping);
586 page_cache_release(pages[i]);
587 }
Chris Masond3977122009-01-05 21:25:51 -0500588 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500589
590 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100591
592cleanup_and_out:
593 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
594 start, end, NULL,
595 EXTENT_CLEAR_UNLOCK_PAGE |
596 EXTENT_CLEAR_DIRTY |
597 EXTENT_CLEAR_DELALLOC |
598 EXTENT_SET_WRITEBACK |
599 EXTENT_END_WRITEBACK);
600 if (!trans || IS_ERR(trans))
601 btrfs_error(root->fs_info, ret, "Failed to join transaction");
602 else
603 btrfs_abort_transaction(trans, root, ret);
604 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500605}
606
607/*
608 * phase two of compressed writeback. This is the ordered portion
609 * of the code, which only gets called in the order the work was
610 * queued. We walk all the async extents created by compress_file_range
611 * and send them down to the disk.
612 */
613static noinline int submit_compressed_extents(struct inode *inode,
614 struct async_cow *async_cow)
615{
616 struct async_extent *async_extent;
617 u64 alloc_hint = 0;
618 struct btrfs_trans_handle *trans;
619 struct btrfs_key ins;
620 struct extent_map *em;
621 struct btrfs_root *root = BTRFS_I(inode)->root;
622 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
623 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500624 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500625
626 if (list_empty(&async_cow->extents))
627 return 0;
628
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500629again:
Chris Masond3977122009-01-05 21:25:51 -0500630 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500631 async_extent = list_entry(async_cow->extents.next,
632 struct async_extent, list);
633 list_del(&async_extent->list);
634
635 io_tree = &BTRFS_I(inode)->io_tree;
636
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500637retry:
Chris Mason771ed682008-11-06 22:02:51 -0500638 /* did the compression code fall back to uncompressed IO? */
639 if (!async_extent->pages) {
640 int page_started = 0;
641 unsigned long nr_written = 0;
642
643 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000644 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100645 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500646
647 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500648 ret = cow_file_range(inode, async_cow->locked_page,
649 async_extent->start,
650 async_extent->start +
651 async_extent->ram_size - 1,
652 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500653
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100654 /* JDM XXX */
655
Chris Mason771ed682008-11-06 22:02:51 -0500656 /*
657 * if page_started, cow_file_range inserted an
658 * inline extent and took care of all the unlocking
659 * and IO for us. Otherwise, we need to submit
660 * all those pages down to the drive.
661 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500662 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500663 extent_write_locked_range(io_tree,
664 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500665 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500666 async_extent->ram_size - 1,
667 btrfs_get_extent,
668 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500669 else if (ret)
670 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500671 kfree(async_extent);
672 cond_resched();
673 continue;
674 }
675
676 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100677 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500678
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400679 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100680 if (IS_ERR(trans)) {
681 ret = PTR_ERR(trans);
682 } else {
683 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
684 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500685 async_extent->compressed_size,
686 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500687 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600688 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100689 btrfs_abort_transaction(trans, root, ret);
690 btrfs_end_transaction(trans, root);
691 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000692
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500693 if (ret) {
694 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500695
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500696 for (i = 0; i < async_extent->nr_pages; i++) {
697 WARN_ON(async_extent->pages[i]->mapping);
698 page_cache_release(async_extent->pages[i]);
699 }
700 kfree(async_extent->pages);
701 async_extent->nr_pages = 0;
702 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500703
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100704 if (ret == -ENOSPC)
705 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500706 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500707 }
708
Yan, Zhengc2167752009-11-12 09:34:21 +0000709 /*
710 * here we're doing allocation and writeback of the
711 * compressed pages
712 */
713 btrfs_drop_extent_cache(inode, async_extent->start,
714 async_extent->start +
715 async_extent->ram_size - 1, 0);
716
David Sterba172ddd62011-04-21 00:48:27 +0200717 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000718 if (!em) {
719 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500720 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000721 }
Chris Mason771ed682008-11-06 22:02:51 -0500722 em->start = async_extent->start;
723 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500724 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400725 em->mod_start = em->start;
726 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500727
728 em->block_start = ins.objectid;
729 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500730 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400731 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500732 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800733 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500734 set_bit(EXTENT_FLAG_PINNED, &em->flags);
735 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400736 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500737
Chris Masond3977122009-01-05 21:25:51 -0500738 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400739 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400740 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400741 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500742 if (ret != -EEXIST) {
743 free_extent_map(em);
744 break;
745 }
746 btrfs_drop_extent_cache(inode, async_extent->start,
747 async_extent->start +
748 async_extent->ram_size - 1, 0);
749 }
750
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500751 if (ret)
752 goto out_free_reserve;
753
Li Zefan261507a02010-12-17 14:21:50 +0800754 ret = btrfs_add_ordered_extent_compress(inode,
755 async_extent->start,
756 ins.objectid,
757 async_extent->ram_size,
758 ins.offset,
759 BTRFS_ORDERED_COMPRESSED,
760 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500761 if (ret)
762 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500763
Chris Mason771ed682008-11-06 22:02:51 -0500764 /*
765 * clear dirty, set writeback and unlock the pages.
766 */
767 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400768 &BTRFS_I(inode)->io_tree,
769 async_extent->start,
770 async_extent->start +
771 async_extent->ram_size - 1,
772 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
773 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400774 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400775 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500776
777 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500778 async_extent->start,
779 async_extent->ram_size,
780 ins.objectid,
781 ins.offset, async_extent->pages,
782 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500783 alloc_hint = ins.objectid + ins.offset;
784 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 if (ret)
786 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500787 cond_resched();
788 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100789 ret = 0;
790out:
791 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500792out_free_reserve:
793 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100794out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
796 async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1,
799 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
800 EXTENT_CLEAR_UNLOCK |
801 EXTENT_CLEAR_DELALLOC |
802 EXTENT_CLEAR_DIRTY |
803 EXTENT_SET_WRITEBACK |
804 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100805 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500806 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500807}
808
Josef Bacik4b46fce2010-05-23 11:00:55 -0400809static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
810 u64 num_bytes)
811{
812 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
813 struct extent_map *em;
814 u64 alloc_hint = 0;
815
816 read_lock(&em_tree->lock);
817 em = search_extent_mapping(em_tree, start, num_bytes);
818 if (em) {
819 /*
820 * if block start isn't an actual block number then find the
821 * first block in this inode and use that as a hint. If that
822 * block is also bogus then just don't worry about it.
823 */
824 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
825 free_extent_map(em);
826 em = search_extent_mapping(em_tree, 0, 0);
827 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
828 alloc_hint = em->block_start;
829 if (em)
830 free_extent_map(em);
831 } else {
832 alloc_hint = em->block_start;
833 free_extent_map(em);
834 }
835 }
836 read_unlock(&em_tree->lock);
837
838 return alloc_hint;
839}
840
Chris Mason771ed682008-11-06 22:02:51 -0500841/*
842 * when extent_io.c finds a delayed allocation range in the file,
843 * the call backs end up in this code. The basic idea is to
844 * allocate extents on disk for the range, and create ordered data structs
845 * in ram to track those extents.
846 *
847 * locked_page is the page that writepage had locked already. We use
848 * it to make sure we don't do extra locks or unlocks.
849 *
850 * *page_started is set to one if we unlock locked_page and do everything
851 * required to start IO on it. It may be clean and already done with
852 * IO when we return.
853 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000854static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
855 struct inode *inode,
856 struct btrfs_root *root,
857 struct page *locked_page,
858 u64 start, u64 end, int *page_started,
859 unsigned long *nr_written,
860 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500861{
Chris Mason771ed682008-11-06 22:02:51 -0500862 u64 alloc_hint = 0;
863 u64 num_bytes;
864 unsigned long ram_size;
865 u64 disk_num_bytes;
866 u64 cur_alloc_size;
867 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500868 struct btrfs_key ins;
869 struct extent_map *em;
870 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
871 int ret = 0;
872
Liu Bo83eea1f2012-07-10 05:28:39 -0600873 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500874
Qu Wenruofda28322013-02-26 08:10:22 +0000875 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500876 num_bytes = max(blocksize, num_bytes);
877 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500878
Chris Mason4cb53002011-05-24 15:35:30 -0400879 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400880 if (num_bytes < 64 * 1024 &&
881 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400882 btrfs_add_inode_defrag(trans, inode);
883
Chris Mason771ed682008-11-06 22:02:51 -0500884 if (start == 0) {
885 /* lets try to make an inline extent */
886 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000887 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500888 if (ret == 0) {
889 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400890 &BTRFS_I(inode)->io_tree,
891 start, end, NULL,
892 EXTENT_CLEAR_UNLOCK_PAGE |
893 EXTENT_CLEAR_UNLOCK |
894 EXTENT_CLEAR_DELALLOC |
895 EXTENT_CLEAR_DIRTY |
896 EXTENT_SET_WRITEBACK |
897 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000898
Chris Mason771ed682008-11-06 22:02:51 -0500899 *nr_written = *nr_written +
900 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
901 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500902 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 } else if (ret < 0) {
904 btrfs_abort_transaction(trans, root, ret);
905 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500906 }
907 }
Chris Masonc8b97812008-10-29 14:49:59 -0400908
909 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200910 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400911
Josef Bacik4b46fce2010-05-23 11:00:55 -0400912 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400913 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400914
Chris Masond3977122009-01-05 21:25:51 -0500915 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400916 unsigned long op;
917
Josef Bacik287a0ab2010-03-19 18:07:23 +0000918 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500920 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500921 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100922 if (ret < 0) {
923 btrfs_abort_transaction(trans, root, ret);
924 goto out_unlock;
925 }
Chris Masond3977122009-01-05 21:25:51 -0500926
David Sterba172ddd62011-04-21 00:48:27 +0200927 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000928 if (!em) {
929 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000930 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000931 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500933 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500934 ram_size = ins.offset;
935 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400936 em->mod_start = em->start;
937 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400938
Chris Masone6dcd2d2008-07-17 12:53:50 -0400939 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400940 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500941 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400942 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400943 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400944 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400945 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400946
Chris Masond3977122009-01-05 21:25:51 -0500947 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400948 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400949 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400950 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400951 if (ret != -EEXIST) {
952 free_extent_map(em);
953 break;
954 }
955 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400956 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400957 }
Liu Boace68ba2013-04-22 10:53:47 +0000958 if (ret)
959 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960
Chris Mason98d20f62008-04-14 09:46:10 -0400961 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400962 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500963 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000964 if (ret)
965 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400966
Yan Zheng17d217f2008-12-12 10:03:38 -0500967 if (root->root_key.objectid ==
968 BTRFS_DATA_RELOC_TREE_OBJECTID) {
969 ret = btrfs_reloc_clone_csums(inode, start,
970 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971 if (ret) {
972 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000973 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100974 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500975 }
976
Chris Masond3977122009-01-05 21:25:51 -0500977 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400978 break;
Chris Masond3977122009-01-05 21:25:51 -0500979
Chris Masonc8b97812008-10-29 14:49:59 -0400980 /* we're not doing compressed IO, don't unlock the first
981 * page (which the caller expects to stay locked), don't
982 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400983 *
984 * Do set the Private2 bit so we know this page was properly
985 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400986 */
Chris Masona791e352009-10-08 11:27:10 -0400987 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
988 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
989 EXTENT_SET_PRIVATE2;
990
Chris Masonc8b97812008-10-29 14:49:59 -0400991 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
992 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400993 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400994 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500995 num_bytes -= cur_alloc_size;
996 alloc_hint = ins.objectid + ins.offset;
997 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400998 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001000 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001001
Liu Boace68ba2013-04-22 10:53:47 +00001002out_reserve:
1003 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004out_unlock:
1005 extent_clear_unlock_delalloc(inode,
1006 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001007 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 EXTENT_CLEAR_UNLOCK_PAGE |
1009 EXTENT_CLEAR_UNLOCK |
1010 EXTENT_CLEAR_DELALLOC |
1011 EXTENT_CLEAR_DIRTY |
1012 EXTENT_SET_WRITEBACK |
1013 EXTENT_END_WRITEBACK);
1014
1015 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001016}
Chris Masonc8b97812008-10-29 14:49:59 -04001017
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001018static noinline int cow_file_range(struct inode *inode,
1019 struct page *locked_page,
1020 u64 start, u64 end, int *page_started,
1021 unsigned long *nr_written,
1022 int unlock)
1023{
1024 struct btrfs_trans_handle *trans;
1025 struct btrfs_root *root = BTRFS_I(inode)->root;
1026 int ret;
1027
1028 trans = btrfs_join_transaction(root);
1029 if (IS_ERR(trans)) {
1030 extent_clear_unlock_delalloc(inode,
1031 &BTRFS_I(inode)->io_tree,
1032 start, end, locked_page,
1033 EXTENT_CLEAR_UNLOCK_PAGE |
1034 EXTENT_CLEAR_UNLOCK |
1035 EXTENT_CLEAR_DELALLOC |
1036 EXTENT_CLEAR_DIRTY |
1037 EXTENT_SET_WRITEBACK |
1038 EXTENT_END_WRITEBACK);
1039 return PTR_ERR(trans);
1040 }
1041 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1042
1043 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1044 page_started, nr_written, unlock);
1045
1046 btrfs_end_transaction(trans, root);
1047
1048 return ret;
1049}
1050
Chris Mason771ed682008-11-06 22:02:51 -05001051/*
1052 * work queue call back to started compression on a file and pages
1053 */
1054static noinline void async_cow_start(struct btrfs_work *work)
1055{
1056 struct async_cow *async_cow;
1057 int num_added = 0;
1058 async_cow = container_of(work, struct async_cow, work);
1059
1060 compress_file_range(async_cow->inode, async_cow->locked_page,
1061 async_cow->start, async_cow->end, async_cow,
1062 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001063 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001064 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001065 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001066 }
Chris Mason771ed682008-11-06 22:02:51 -05001067}
1068
1069/*
1070 * work queue call back to submit previously compressed pages
1071 */
1072static noinline void async_cow_submit(struct btrfs_work *work)
1073{
1074 struct async_cow *async_cow;
1075 struct btrfs_root *root;
1076 unsigned long nr_pages;
1077
1078 async_cow = container_of(work, struct async_cow, work);
1079
1080 root = async_cow->root;
1081 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1082 PAGE_CACHE_SHIFT;
1083
Josef Bacik66657b32012-08-01 15:36:24 -04001084 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001085 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001086 waitqueue_active(&root->fs_info->async_submit_wait))
1087 wake_up(&root->fs_info->async_submit_wait);
1088
Chris Masond3977122009-01-05 21:25:51 -05001089 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001090 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001091}
Chris Masonc8b97812008-10-29 14:49:59 -04001092
Chris Mason771ed682008-11-06 22:02:51 -05001093static noinline void async_cow_free(struct btrfs_work *work)
1094{
1095 struct async_cow *async_cow;
1096 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001097 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001098 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001099 kfree(async_cow);
1100}
1101
1102static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1103 u64 start, u64 end, int *page_started,
1104 unsigned long *nr_written)
1105{
1106 struct async_cow *async_cow;
1107 struct btrfs_root *root = BTRFS_I(inode)->root;
1108 unsigned long nr_pages;
1109 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001110 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001111
Chris Masona3429ab2009-10-08 12:30:20 -04001112 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1113 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001114 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001115 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001116 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001118 async_cow->root = root;
1119 async_cow->locked_page = locked_page;
1120 async_cow->start = start;
1121
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001122 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001123 cur_end = end;
1124 else
1125 cur_end = min(end, start + 512 * 1024 - 1);
1126
1127 async_cow->end = cur_end;
1128 INIT_LIST_HEAD(&async_cow->extents);
1129
1130 async_cow->work.func = async_cow_start;
1131 async_cow->work.ordered_func = async_cow_submit;
1132 async_cow->work.ordered_free = async_cow_free;
1133 async_cow->work.flags = 0;
1134
Chris Mason771ed682008-11-06 22:02:51 -05001135 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1136 PAGE_CACHE_SHIFT;
1137 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1138
1139 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1140 &async_cow->work);
1141
1142 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1143 wait_event(root->fs_info->async_submit_wait,
1144 (atomic_read(&root->fs_info->async_delalloc_pages) <
1145 limit));
1146 }
1147
Chris Masond3977122009-01-05 21:25:51 -05001148 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001149 atomic_read(&root->fs_info->async_delalloc_pages)) {
1150 wait_event(root->fs_info->async_submit_wait,
1151 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1152 0));
1153 }
1154
1155 *nr_written += nr_pages;
1156 start = cur_end + 1;
1157 }
1158 *page_started = 1;
1159 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001160}
1161
Chris Masond3977122009-01-05 21:25:51 -05001162static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001163 u64 bytenr, u64 num_bytes)
1164{
1165 int ret;
1166 struct btrfs_ordered_sum *sums;
1167 LIST_HEAD(list);
1168
Yan Zheng07d400a2009-01-06 11:42:00 -05001169 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001170 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001171 if (ret == 0 && list_empty(&list))
1172 return 0;
1173
1174 while (!list_empty(&list)) {
1175 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1176 list_del(&sums->list);
1177 kfree(sums);
1178 }
1179 return 1;
1180}
1181
Chris Masond352ac62008-09-29 15:18:18 -04001182/*
1183 * when nowcow writeback call back. This checks for snapshots or COW copies
1184 * of the extents that exist in the file, and COWs the file as required.
1185 *
1186 * If no cow copies or snapshots exist, we write directly to the existing
1187 * blocks on disk
1188 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001189static noinline int run_delalloc_nocow(struct inode *inode,
1190 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001191 u64 start, u64 end, int *page_started, int force,
1192 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001193{
Chris Masonbe20aa92007-12-17 20:14:01 -05001194 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001195 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001196 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001197 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 u64 cow_start;
1201 u64 cur_offset;
1202 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 u64 disk_bytenr;
1205 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001206 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001207 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001209 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001210 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 int nocow;
1212 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001213 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001214 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001215
1216 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001217 if (!path) {
1218 extent_clear_unlock_delalloc(inode,
1219 &BTRFS_I(inode)->io_tree,
1220 start, end, locked_page,
1221 EXTENT_CLEAR_UNLOCK_PAGE |
1222 EXTENT_CLEAR_UNLOCK |
1223 EXTENT_CLEAR_DELALLOC |
1224 EXTENT_CLEAR_DIRTY |
1225 EXTENT_SET_WRITEBACK |
1226 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001227 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001228 }
Li Zefan82d59022011-04-20 10:33:24 +08001229
Liu Bo83eea1f2012-07-10 05:28:39 -06001230 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001231
1232 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001233 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001234 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001235 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001236
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001237 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001238 extent_clear_unlock_delalloc(inode,
1239 &BTRFS_I(inode)->io_tree,
1240 start, end, locked_page,
1241 EXTENT_CLEAR_UNLOCK_PAGE |
1242 EXTENT_CLEAR_UNLOCK |
1243 EXTENT_CLEAR_DELALLOC |
1244 EXTENT_CLEAR_DIRTY |
1245 EXTENT_SET_WRITEBACK |
1246 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001247 btrfs_free_path(path);
1248 return PTR_ERR(trans);
1249 }
1250
Josef Bacik74b21072011-04-13 12:02:53 -04001251 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001252
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 cow_start = (u64)-1;
1254 cur_offset = start;
1255 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001256 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001258 if (ret < 0) {
1259 btrfs_abort_transaction(trans, root, ret);
1260 goto error;
1261 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1263 leaf = path->nodes[0];
1264 btrfs_item_key_to_cpu(leaf, &found_key,
1265 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001266 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 found_key.type == BTRFS_EXTENT_DATA_KEY)
1268 path->slots[0]--;
1269 }
1270 check_prev = 0;
1271next_slot:
1272 leaf = path->nodes[0];
1273 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1274 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 if (ret < 0) {
1276 btrfs_abort_transaction(trans, root, ret);
1277 goto error;
1278 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 if (ret > 0)
1280 break;
1281 leaf = path->nodes[0];
1282 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001283
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 nocow = 0;
1285 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001286 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Li Zefan33345d012011-04-20 10:31:50 +08001289 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1291 found_key.offset > end)
1292 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001293
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 if (found_key.offset > cur_offset) {
1295 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001296 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 goto out_check;
1298 }
Chris Masonc31f8832008-01-08 15:46:31 -05001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 fi = btrfs_item_ptr(leaf, path->slots[0],
1301 struct btrfs_file_extent_item);
1302 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001303
Josef Bacikcc95bef2013-04-04 14:31:27 -04001304 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001305 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1306 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001307 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001308 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 extent_end = found_key.offset +
1310 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001311 disk_num_bytes =
1312 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 if (extent_end <= start) {
1314 path->slots[0]++;
1315 goto next_slot;
1316 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001317 if (disk_bytenr == 0)
1318 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 if (btrfs_file_extent_compression(leaf, fi) ||
1320 btrfs_file_extent_encryption(leaf, fi) ||
1321 btrfs_file_extent_other_encoding(leaf, fi))
1322 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001323 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1324 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001325 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001327 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 found_key.offset -
1329 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001330 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001332 disk_bytenr += cur_offset - found_key.offset;
1333 num_bytes = min(end + 1, extent_end) - cur_offset;
1334 /*
1335 * force cow if csum exists in the range.
1336 * this ensure that csum for a given extent are
1337 * either valid or do not exist.
1338 */
1339 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1340 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 nocow = 1;
1342 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1343 extent_end = found_key.offset +
1344 btrfs_file_extent_inline_len(leaf, fi);
1345 extent_end = ALIGN(extent_end, root->sectorsize);
1346 } else {
1347 BUG_ON(1);
1348 }
1349out_check:
1350 if (extent_end <= start) {
1351 path->slots[0]++;
1352 goto next_slot;
1353 }
1354 if (!nocow) {
1355 if (cow_start == (u64)-1)
1356 cow_start = cur_offset;
1357 cur_offset = extent_end;
1358 if (cur_offset > end)
1359 break;
1360 path->slots[0]++;
1361 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001362 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001363
David Sterbab3b4aa72011-04-21 01:20:15 +02001364 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001366 ret = __cow_file_range(trans, inode, root, locked_page,
1367 cow_start, found_key.offset - 1,
1368 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369 if (ret) {
1370 btrfs_abort_transaction(trans, root, ret);
1371 goto error;
1372 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001374 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001375
Yan Zhengd899e052008-10-30 14:25:28 -04001376 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1377 struct extent_map *em;
1378 struct extent_map_tree *em_tree;
1379 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001380 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001381 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001382 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001383 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001384 em->len = num_bytes;
1385 em->block_len = num_bytes;
1386 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001387 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001388 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001390 em->mod_start = em->start;
1391 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001392 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001393 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001394 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001395 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001396 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001397 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001398 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001399 if (ret != -EEXIST) {
1400 free_extent_map(em);
1401 break;
1402 }
1403 btrfs_drop_extent_cache(inode, em->start,
1404 em->start + em->len - 1, 0);
1405 }
1406 type = BTRFS_ORDERED_PREALLOC;
1407 } else {
1408 type = BTRFS_ORDERED_NOCOW;
1409 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001410
1411 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001412 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001413 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001414
Yan, Zhengefa56462010-05-16 10:49:59 -04001415 if (root->root_key.objectid ==
1416 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1417 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1418 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001419 if (ret) {
1420 btrfs_abort_transaction(trans, root, ret);
1421 goto error;
1422 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001423 }
1424
Yan Zhengd899e052008-10-30 14:25:28 -04001425 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001426 cur_offset, cur_offset + num_bytes - 1,
1427 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1428 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1429 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001430 cur_offset = extent_end;
1431 if (cur_offset > end)
1432 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001433 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001434 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435
Josef Bacik17ca04a2012-05-31 15:58:55 -04001436 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001438 cur_offset = end;
1439 }
1440
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001442 ret = __cow_file_range(trans, inode, root, locked_page,
1443 cow_start, end,
1444 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 if (ret) {
1446 btrfs_abort_transaction(trans, root, ret);
1447 goto error;
1448 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 }
1450
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001451error:
Miao Xiea698d0752012-09-20 01:51:59 -06001452 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001453 if (!ret)
1454 ret = err;
1455
Josef Bacik17ca04a2012-05-31 15:58:55 -04001456 if (ret && cur_offset < end)
1457 extent_clear_unlock_delalloc(inode,
1458 &BTRFS_I(inode)->io_tree,
1459 cur_offset, end, locked_page,
1460 EXTENT_CLEAR_UNLOCK_PAGE |
1461 EXTENT_CLEAR_UNLOCK |
1462 EXTENT_CLEAR_DELALLOC |
1463 EXTENT_CLEAR_DIRTY |
1464 EXTENT_SET_WRITEBACK |
1465 EXTENT_END_WRITEBACK);
1466
Yan Zheng7ea394f2008-08-05 13:05:02 -04001467 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001468 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001469}
1470
Chris Masond352ac62008-09-29 15:18:18 -04001471/*
1472 * extent_io.c call back to do delayed allocation processing
1473 */
Chris Masonc8b97812008-10-29 14:49:59 -04001474static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001475 u64 start, u64 end, int *page_started,
1476 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001477{
Chris Masonbe20aa92007-12-17 20:14:01 -05001478 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001479 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001480
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001481 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001482 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001483 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001484 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001485 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001486 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001487 } else if (!btrfs_test_opt(root, COMPRESS) &&
1488 !(BTRFS_I(inode)->force_compress) &&
1489 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001490 ret = cow_file_range(inode, locked_page, start, end,
1491 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001492 } else {
1493 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1494 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001495 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001496 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001497 }
Chris Masonb888db22007-08-27 16:49:44 -04001498 return ret;
1499}
1500
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001501static void btrfs_split_extent_hook(struct inode *inode,
1502 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001504 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001505 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001506 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507
Josef Bacik9e0baf62011-07-15 15:16:44 +00001508 spin_lock(&BTRFS_I(inode)->lock);
1509 BTRFS_I(inode)->outstanding_extents++;
1510 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511}
1512
1513/*
1514 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1515 * extents so we can keep track of new extents that are just merged onto old
1516 * extents, such as when we are doing sequential writes, so we can properly
1517 * account for the metadata space we'll need.
1518 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001519static void btrfs_merge_extent_hook(struct inode *inode,
1520 struct extent_state *new,
1521 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001523 /* not delalloc, ignore it */
1524 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001525 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526
Josef Bacik9e0baf62011-07-15 15:16:44 +00001527 spin_lock(&BTRFS_I(inode)->lock);
1528 BTRFS_I(inode)->outstanding_extents--;
1529 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001530}
1531
Chris Masond352ac62008-09-29 15:18:18 -04001532/*
1533 * extent_io.c set_bit_hook, used to track delayed allocation
1534 * bytes in this file, and to maintain the list of inodes that
1535 * have pending delalloc work to be done.
1536 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001537static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001538 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001539{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540
Chris Mason75eff682008-12-15 15:54:40 -05001541 /*
1542 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001543 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001544 * bit, which is only set or cleared with irqs on
1545 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001546 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001547 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001548 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001549 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001550
Josef Bacik9e0baf62011-07-15 15:16:44 +00001551 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001553 } else {
1554 spin_lock(&BTRFS_I(inode)->lock);
1555 BTRFS_I(inode)->outstanding_extents++;
1556 spin_unlock(&BTRFS_I(inode)->lock);
1557 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001558
Miao Xie963d6782013-01-29 10:10:51 +00001559 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1560 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001561 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001562 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001563 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1564 &BTRFS_I(inode)->runtime_flags)) {
1565 spin_lock(&root->fs_info->delalloc_lock);
1566 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1567 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1568 &root->fs_info->delalloc_inodes);
1569 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1570 &BTRFS_I(inode)->runtime_flags);
1571 }
1572 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001573 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001574 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001575 }
Chris Mason291d6732008-01-29 15:55:23 -05001576}
1577
Chris Masond352ac62008-09-29 15:18:18 -04001578/*
1579 * extent_io.c clear_bit_hook, see set_bit_hook for why
1580 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001581static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001582 struct extent_state *state,
1583 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001584{
Chris Mason75eff682008-12-15 15:54:40 -05001585 /*
1586 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001587 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001588 * bit, which is only set or cleared with irqs on
1589 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001590 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001591 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001592 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001593 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001594
Josef Bacik9e0baf62011-07-15 15:16:44 +00001595 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001596 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001597 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1598 spin_lock(&BTRFS_I(inode)->lock);
1599 BTRFS_I(inode)->outstanding_extents--;
1600 spin_unlock(&BTRFS_I(inode)->lock);
1601 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602
1603 if (*bits & EXTENT_DO_ACCOUNTING)
1604 btrfs_delalloc_release_metadata(inode, len);
1605
Josef Bacik0cb59c92010-07-02 12:14:14 -04001606 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1607 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001608 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609
Miao Xie963d6782013-01-29 10:10:51 +00001610 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1611 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001612 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001613 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001614 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001615 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1616 &BTRFS_I(inode)->runtime_flags)) {
1617 spin_lock(&root->fs_info->delalloc_lock);
1618 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1619 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1620 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1621 &BTRFS_I(inode)->runtime_flags);
1622 }
1623 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001624 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001625 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001626 }
Chris Mason291d6732008-01-29 15:55:23 -05001627}
1628
Chris Masond352ac62008-09-29 15:18:18 -04001629/*
1630 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1631 * we don't create bios that span stripes or chunks
1632 */
David Woodhouse64a16702009-07-15 23:29:37 +01001633int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001634 size_t size, struct bio *bio,
1635 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001636{
1637 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001638 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001639 u64 length = 0;
1640 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001641 int ret;
1642
Chris Mason771ed682008-11-06 22:02:51 -05001643 if (bio_flags & EXTENT_BIO_COMPRESSED)
1644 return 0;
1645
Chris Masonf2d8d742008-04-21 10:03:05 -04001646 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001647 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001648 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001649 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001650 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001651 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001652 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001653 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001654 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001655}
1656
Chris Masond352ac62008-09-29 15:18:18 -04001657/*
1658 * in order to insert checksums into the metadata in large chunks,
1659 * we wait until bio submission time. All the pages in the bio are
1660 * checksummed and sums are attached onto the ordered extent record.
1661 *
1662 * At IO completion time the cums attached on the ordered extent record
1663 * are inserted into the btree
1664 */
Chris Masond3977122009-01-05 21:25:51 -05001665static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1666 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001667 unsigned long bio_flags,
1668 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001669{
Chris Mason065631f2008-02-20 12:07:25 -05001670 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001671 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001672
Chris Masond20f7042008-12-08 16:58:54 -05001673 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001674 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001675 return 0;
1676}
Chris Masone0156402008-04-16 11:15:20 -04001677
Chris Mason4a69a412008-11-06 22:03:00 -05001678/*
1679 * in order to insert checksums into the metadata in large chunks,
1680 * we wait until bio submission time. All the pages in the bio are
1681 * checksummed and sums are attached onto the ordered extent record.
1682 *
1683 * At IO completion time the cums attached on the ordered extent record
1684 * are inserted into the btree
1685 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001686static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001687 int mirror_num, unsigned long bio_flags,
1688 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001689{
1690 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001691 int ret;
1692
1693 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1694 if (ret)
1695 bio_endio(bio, ret);
1696 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001697}
1698
Chris Masond352ac62008-09-29 15:18:18 -04001699/*
Chris Masoncad321a2008-12-17 14:51:42 -05001700 * extent_io.c submission hook. This does the right thing for csum calculation
1701 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001702 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001703static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001704 int mirror_num, unsigned long bio_flags,
1705 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001706{
1707 struct btrfs_root *root = BTRFS_I(inode)->root;
1708 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001709 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001710 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001711 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001712
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001713 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001714
Liu Bo83eea1f2012-07-10 05:28:39 -06001715 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001716 metadata = 2;
1717
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001718 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001719 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1720 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001721 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001722
Chris Masond20f7042008-12-08 16:58:54 -05001723 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001724 ret = btrfs_submit_compressed_read(inode, bio,
1725 mirror_num,
1726 bio_flags);
1727 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001728 } else if (!skip_sum) {
1729 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1730 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001731 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001732 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001733 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001734 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001735 /* csum items have already been cloned */
1736 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1737 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001738 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001739 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001740 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001741 bio_flags, bio_offset,
1742 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001743 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001744 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001745 } else if (!skip_sum) {
1746 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1747 if (ret)
1748 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001749 }
1750
Chris Mason0b86a832008-03-24 15:01:56 -04001751mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001752 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1753
1754out:
1755 if (ret < 0)
1756 bio_endio(bio, ret);
1757 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001758}
Chris Mason6885f302008-02-20 16:11:05 -05001759
Chris Masond352ac62008-09-29 15:18:18 -04001760/*
1761 * given a list of ordered sums record them in the inode. This happens
1762 * at IO completion time based on sums calculated at bio submission time.
1763 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001764static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001765 struct inode *inode, u64 file_offset,
1766 struct list_head *list)
1767{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001768 struct btrfs_ordered_sum *sum;
1769
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001770 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001771 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001772 btrfs_csum_file_blocks(trans,
1773 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001774 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001775 }
1776 return 0;
1777}
1778
Josef Bacik2ac55d42010-02-03 19:33:23 +00001779int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1780 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001781{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001782 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001783 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001784 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001785}
1786
Chris Masond352ac62008-09-29 15:18:18 -04001787/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001788struct btrfs_writepage_fixup {
1789 struct page *page;
1790 struct btrfs_work work;
1791};
1792
Christoph Hellwigb2950862008-12-02 09:54:17 -05001793static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001794{
1795 struct btrfs_writepage_fixup *fixup;
1796 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001797 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001798 struct page *page;
1799 struct inode *inode;
1800 u64 page_start;
1801 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001802 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001803
1804 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1805 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001806again:
Chris Mason247e7432008-07-17 12:53:51 -04001807 lock_page(page);
1808 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1809 ClearPageChecked(page);
1810 goto out_page;
1811 }
1812
1813 inode = page->mapping->host;
1814 page_start = page_offset(page);
1815 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1816
Josef Bacik2ac55d42010-02-03 19:33:23 +00001817 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001818 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001819
1820 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001821 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001822 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001823
1824 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1825 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001826 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1827 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001828 unlock_page(page);
1829 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001830 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001831 goto again;
1832 }
Chris Mason247e7432008-07-17 12:53:51 -04001833
Jeff Mahoney87826df2012-02-15 16:23:57 +01001834 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1835 if (ret) {
1836 mapping_set_error(page->mapping, ret);
1837 end_extent_writepage(page, ret, page_start, page_end);
1838 ClearPageChecked(page);
1839 goto out;
1840 }
1841
Josef Bacik2ac55d42010-02-03 19:33:23 +00001842 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001843 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001844 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001845out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001846 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1847 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001848out_page:
1849 unlock_page(page);
1850 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001851 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001852}
1853
1854/*
1855 * There are a few paths in the higher layers of the kernel that directly
1856 * set the page dirty bit without asking the filesystem if it is a
1857 * good idea. This causes problems because we want to make sure COW
1858 * properly happens and the data=ordered rules are followed.
1859 *
Chris Masonc8b97812008-10-29 14:49:59 -04001860 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001861 * hasn't been properly setup for IO. We kick off an async process
1862 * to fix it up. The async helper will wait for ordered extents, set
1863 * the delalloc bit and make it safe to write the page.
1864 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001865static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001866{
1867 struct inode *inode = page->mapping->host;
1868 struct btrfs_writepage_fixup *fixup;
1869 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001870
Chris Mason8b62b722009-09-02 16:53:46 -04001871 /* this page is properly in the ordered list */
1872 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001873 return 0;
1874
1875 if (PageChecked(page))
1876 return -EAGAIN;
1877
1878 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1879 if (!fixup)
1880 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001881
Chris Mason247e7432008-07-17 12:53:51 -04001882 SetPageChecked(page);
1883 page_cache_get(page);
1884 fixup->work.func = btrfs_writepage_fixup_worker;
1885 fixup->page = page;
1886 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001887 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001888}
1889
Yan Zhengd899e052008-10-30 14:25:28 -04001890static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1891 struct inode *inode, u64 file_pos,
1892 u64 disk_bytenr, u64 disk_num_bytes,
1893 u64 num_bytes, u64 ram_bytes,
1894 u8 compression, u8 encryption,
1895 u16 other_encoding, int extent_type)
1896{
1897 struct btrfs_root *root = BTRFS_I(inode)->root;
1898 struct btrfs_file_extent_item *fi;
1899 struct btrfs_path *path;
1900 struct extent_buffer *leaf;
1901 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001902 int ret;
1903
1904 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001905 if (!path)
1906 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001907
Chris Masonb9473432009-03-13 11:00:37 -04001908 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001909
1910 /*
1911 * we may be replacing one extent in the tree with another.
1912 * The new extent is pinned in the extent map, and we don't want
1913 * to drop it from the cache until it is completely in the btree.
1914 *
1915 * So, tell btrfs_drop_extents to leave this extent in the cache.
1916 * the caller is expected to unpin it and allow it to be merged
1917 * with the others.
1918 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001919 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001920 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001921 if (ret)
1922 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001923
Li Zefan33345d012011-04-20 10:31:50 +08001924 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001925 ins.offset = file_pos;
1926 ins.type = BTRFS_EXTENT_DATA_KEY;
1927 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 if (ret)
1929 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001930 leaf = path->nodes[0];
1931 fi = btrfs_item_ptr(leaf, path->slots[0],
1932 struct btrfs_file_extent_item);
1933 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1934 btrfs_set_file_extent_type(leaf, fi, extent_type);
1935 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1936 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1937 btrfs_set_file_extent_offset(leaf, fi, 0);
1938 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1939 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1940 btrfs_set_file_extent_compression(leaf, fi, compression);
1941 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1942 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001943
Yan Zhengd899e052008-10-30 14:25:28 -04001944 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001945 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001946
1947 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001948
1949 ins.objectid = disk_bytenr;
1950 ins.offset = disk_num_bytes;
1951 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 ret = btrfs_alloc_reserved_file_extent(trans, root,
1953 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001954 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001955out:
Yan Zhengd899e052008-10-30 14:25:28 -04001956 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001957
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001958 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001959}
1960
Liu Bo38c227d2013-01-29 03:18:40 +00001961/* snapshot-aware defrag */
1962struct sa_defrag_extent_backref {
1963 struct rb_node node;
1964 struct old_sa_defrag_extent *old;
1965 u64 root_id;
1966 u64 inum;
1967 u64 file_pos;
1968 u64 extent_offset;
1969 u64 num_bytes;
1970 u64 generation;
1971};
1972
1973struct old_sa_defrag_extent {
1974 struct list_head list;
1975 struct new_sa_defrag_extent *new;
1976
1977 u64 extent_offset;
1978 u64 bytenr;
1979 u64 offset;
1980 u64 len;
1981 int count;
1982};
1983
1984struct new_sa_defrag_extent {
1985 struct rb_root root;
1986 struct list_head head;
1987 struct btrfs_path *path;
1988 struct inode *inode;
1989 u64 file_pos;
1990 u64 len;
1991 u64 bytenr;
1992 u64 disk_len;
1993 u8 compress_type;
1994};
1995
1996static int backref_comp(struct sa_defrag_extent_backref *b1,
1997 struct sa_defrag_extent_backref *b2)
1998{
1999 if (b1->root_id < b2->root_id)
2000 return -1;
2001 else if (b1->root_id > b2->root_id)
2002 return 1;
2003
2004 if (b1->inum < b2->inum)
2005 return -1;
2006 else if (b1->inum > b2->inum)
2007 return 1;
2008
2009 if (b1->file_pos < b2->file_pos)
2010 return -1;
2011 else if (b1->file_pos > b2->file_pos)
2012 return 1;
2013
2014 /*
2015 * [------------------------------] ===> (a range of space)
2016 * |<--->| |<---->| =============> (fs/file tree A)
2017 * |<---------------------------->| ===> (fs/file tree B)
2018 *
2019 * A range of space can refer to two file extents in one tree while
2020 * refer to only one file extent in another tree.
2021 *
2022 * So we may process a disk offset more than one time(two extents in A)
2023 * and locate at the same extent(one extent in B), then insert two same
2024 * backrefs(both refer to the extent in B).
2025 */
2026 return 0;
2027}
2028
2029static void backref_insert(struct rb_root *root,
2030 struct sa_defrag_extent_backref *backref)
2031{
2032 struct rb_node **p = &root->rb_node;
2033 struct rb_node *parent = NULL;
2034 struct sa_defrag_extent_backref *entry;
2035 int ret;
2036
2037 while (*p) {
2038 parent = *p;
2039 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2040
2041 ret = backref_comp(backref, entry);
2042 if (ret < 0)
2043 p = &(*p)->rb_left;
2044 else
2045 p = &(*p)->rb_right;
2046 }
2047
2048 rb_link_node(&backref->node, parent, p);
2049 rb_insert_color(&backref->node, root);
2050}
2051
2052/*
2053 * Note the backref might has changed, and in this case we just return 0.
2054 */
2055static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2056 void *ctx)
2057{
2058 struct btrfs_file_extent_item *extent;
2059 struct btrfs_fs_info *fs_info;
2060 struct old_sa_defrag_extent *old = ctx;
2061 struct new_sa_defrag_extent *new = old->new;
2062 struct btrfs_path *path = new->path;
2063 struct btrfs_key key;
2064 struct btrfs_root *root;
2065 struct sa_defrag_extent_backref *backref;
2066 struct extent_buffer *leaf;
2067 struct inode *inode = new->inode;
2068 int slot;
2069 int ret;
2070 u64 extent_offset;
2071 u64 num_bytes;
2072
2073 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2074 inum == btrfs_ino(inode))
2075 return 0;
2076
2077 key.objectid = root_id;
2078 key.type = BTRFS_ROOT_ITEM_KEY;
2079 key.offset = (u64)-1;
2080
2081 fs_info = BTRFS_I(inode)->root->fs_info;
2082 root = btrfs_read_fs_root_no_name(fs_info, &key);
2083 if (IS_ERR(root)) {
2084 if (PTR_ERR(root) == -ENOENT)
2085 return 0;
2086 WARN_ON(1);
2087 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2088 inum, offset, root_id);
2089 return PTR_ERR(root);
2090 }
2091
2092 key.objectid = inum;
2093 key.type = BTRFS_EXTENT_DATA_KEY;
2094 if (offset > (u64)-1 << 32)
2095 key.offset = 0;
2096 else
2097 key.offset = offset;
2098
2099 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2100 if (ret < 0) {
2101 WARN_ON(1);
2102 return ret;
2103 }
2104
2105 while (1) {
2106 cond_resched();
2107
2108 leaf = path->nodes[0];
2109 slot = path->slots[0];
2110
2111 if (slot >= btrfs_header_nritems(leaf)) {
2112 ret = btrfs_next_leaf(root, path);
2113 if (ret < 0) {
2114 goto out;
2115 } else if (ret > 0) {
2116 ret = 0;
2117 goto out;
2118 }
2119 continue;
2120 }
2121
2122 path->slots[0]++;
2123
2124 btrfs_item_key_to_cpu(leaf, &key, slot);
2125
2126 if (key.objectid > inum)
2127 goto out;
2128
2129 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2130 continue;
2131
2132 extent = btrfs_item_ptr(leaf, slot,
2133 struct btrfs_file_extent_item);
2134
2135 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2136 continue;
2137
2138 extent_offset = btrfs_file_extent_offset(leaf, extent);
2139 if (key.offset - extent_offset != offset)
2140 continue;
2141
2142 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2143 if (extent_offset >= old->extent_offset + old->offset +
2144 old->len || extent_offset + num_bytes <=
2145 old->extent_offset + old->offset)
2146 continue;
2147
2148 break;
2149 }
2150
2151 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2152 if (!backref) {
2153 ret = -ENOENT;
2154 goto out;
2155 }
2156
2157 backref->root_id = root_id;
2158 backref->inum = inum;
2159 backref->file_pos = offset + extent_offset;
2160 backref->num_bytes = num_bytes;
2161 backref->extent_offset = extent_offset;
2162 backref->generation = btrfs_file_extent_generation(leaf, extent);
2163 backref->old = old;
2164 backref_insert(&new->root, backref);
2165 old->count++;
2166out:
2167 btrfs_release_path(path);
2168 WARN_ON(ret);
2169 return ret;
2170}
2171
2172static noinline bool record_extent_backrefs(struct btrfs_path *path,
2173 struct new_sa_defrag_extent *new)
2174{
2175 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2176 struct old_sa_defrag_extent *old, *tmp;
2177 int ret;
2178
2179 new->path = path;
2180
2181 list_for_each_entry_safe(old, tmp, &new->head, list) {
2182 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2183 path, record_one_backref,
2184 old);
2185 BUG_ON(ret < 0 && ret != -ENOENT);
2186
2187 /* no backref to be processed for this extent */
2188 if (!old->count) {
2189 list_del(&old->list);
2190 kfree(old);
2191 }
2192 }
2193
2194 if (list_empty(&new->head))
2195 return false;
2196
2197 return true;
2198}
2199
2200static int relink_is_mergable(struct extent_buffer *leaf,
2201 struct btrfs_file_extent_item *fi,
2202 u64 disk_bytenr)
2203{
2204 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2205 return 0;
2206
2207 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2208 return 0;
2209
2210 if (btrfs_file_extent_compression(leaf, fi) ||
2211 btrfs_file_extent_encryption(leaf, fi) ||
2212 btrfs_file_extent_other_encoding(leaf, fi))
2213 return 0;
2214
2215 return 1;
2216}
2217
2218/*
2219 * Note the backref might has changed, and in this case we just return 0.
2220 */
2221static noinline int relink_extent_backref(struct btrfs_path *path,
2222 struct sa_defrag_extent_backref *prev,
2223 struct sa_defrag_extent_backref *backref)
2224{
2225 struct btrfs_file_extent_item *extent;
2226 struct btrfs_file_extent_item *item;
2227 struct btrfs_ordered_extent *ordered;
2228 struct btrfs_trans_handle *trans;
2229 struct btrfs_fs_info *fs_info;
2230 struct btrfs_root *root;
2231 struct btrfs_key key;
2232 struct extent_buffer *leaf;
2233 struct old_sa_defrag_extent *old = backref->old;
2234 struct new_sa_defrag_extent *new = old->new;
2235 struct inode *src_inode = new->inode;
2236 struct inode *inode;
2237 struct extent_state *cached = NULL;
2238 int ret = 0;
2239 u64 start;
2240 u64 len;
2241 u64 lock_start;
2242 u64 lock_end;
2243 bool merge = false;
2244 int index;
2245
2246 if (prev && prev->root_id == backref->root_id &&
2247 prev->inum == backref->inum &&
2248 prev->file_pos + prev->num_bytes == backref->file_pos)
2249 merge = true;
2250
2251 /* step 1: get root */
2252 key.objectid = backref->root_id;
2253 key.type = BTRFS_ROOT_ITEM_KEY;
2254 key.offset = (u64)-1;
2255
2256 fs_info = BTRFS_I(src_inode)->root->fs_info;
2257 index = srcu_read_lock(&fs_info->subvol_srcu);
2258
2259 root = btrfs_read_fs_root_no_name(fs_info, &key);
2260 if (IS_ERR(root)) {
2261 srcu_read_unlock(&fs_info->subvol_srcu, index);
2262 if (PTR_ERR(root) == -ENOENT)
2263 return 0;
2264 return PTR_ERR(root);
2265 }
2266 if (btrfs_root_refs(&root->root_item) == 0) {
2267 srcu_read_unlock(&fs_info->subvol_srcu, index);
2268 /* parse ENOENT to 0 */
2269 return 0;
2270 }
2271
2272 /* step 2: get inode */
2273 key.objectid = backref->inum;
2274 key.type = BTRFS_INODE_ITEM_KEY;
2275 key.offset = 0;
2276
2277 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2278 if (IS_ERR(inode)) {
2279 srcu_read_unlock(&fs_info->subvol_srcu, index);
2280 return 0;
2281 }
2282
2283 srcu_read_unlock(&fs_info->subvol_srcu, index);
2284
2285 /* step 3: relink backref */
2286 lock_start = backref->file_pos;
2287 lock_end = backref->file_pos + backref->num_bytes - 1;
2288 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2289 0, &cached);
2290
2291 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2292 if (ordered) {
2293 btrfs_put_ordered_extent(ordered);
2294 goto out_unlock;
2295 }
2296
2297 trans = btrfs_join_transaction(root);
2298 if (IS_ERR(trans)) {
2299 ret = PTR_ERR(trans);
2300 goto out_unlock;
2301 }
2302
2303 key.objectid = backref->inum;
2304 key.type = BTRFS_EXTENT_DATA_KEY;
2305 key.offset = backref->file_pos;
2306
2307 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2308 if (ret < 0) {
2309 goto out_free_path;
2310 } else if (ret > 0) {
2311 ret = 0;
2312 goto out_free_path;
2313 }
2314
2315 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2316 struct btrfs_file_extent_item);
2317
2318 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2319 backref->generation)
2320 goto out_free_path;
2321
2322 btrfs_release_path(path);
2323
2324 start = backref->file_pos;
2325 if (backref->extent_offset < old->extent_offset + old->offset)
2326 start += old->extent_offset + old->offset -
2327 backref->extent_offset;
2328
2329 len = min(backref->extent_offset + backref->num_bytes,
2330 old->extent_offset + old->offset + old->len);
2331 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2332
2333 ret = btrfs_drop_extents(trans, root, inode, start,
2334 start + len, 1);
2335 if (ret)
2336 goto out_free_path;
2337again:
2338 key.objectid = btrfs_ino(inode);
2339 key.type = BTRFS_EXTENT_DATA_KEY;
2340 key.offset = start;
2341
Liu Boa09a0a72013-03-11 09:20:58 +00002342 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002343 if (merge) {
2344 struct btrfs_file_extent_item *fi;
2345 u64 extent_len;
2346 struct btrfs_key found_key;
2347
2348 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2349 if (ret < 0)
2350 goto out_free_path;
2351
2352 path->slots[0]--;
2353 leaf = path->nodes[0];
2354 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2355
2356 fi = btrfs_item_ptr(leaf, path->slots[0],
2357 struct btrfs_file_extent_item);
2358 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2359
2360 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2361 extent_len + found_key.offset == start) {
2362 btrfs_set_file_extent_num_bytes(leaf, fi,
2363 extent_len + len);
2364 btrfs_mark_buffer_dirty(leaf);
2365 inode_add_bytes(inode, len);
2366
2367 ret = 1;
2368 goto out_free_path;
2369 } else {
2370 merge = false;
2371 btrfs_release_path(path);
2372 goto again;
2373 }
2374 }
2375
2376 ret = btrfs_insert_empty_item(trans, root, path, &key,
2377 sizeof(*extent));
2378 if (ret) {
2379 btrfs_abort_transaction(trans, root, ret);
2380 goto out_free_path;
2381 }
2382
2383 leaf = path->nodes[0];
2384 item = btrfs_item_ptr(leaf, path->slots[0],
2385 struct btrfs_file_extent_item);
2386 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2387 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2388 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2389 btrfs_set_file_extent_num_bytes(leaf, item, len);
2390 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2391 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2392 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2393 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2394 btrfs_set_file_extent_encryption(leaf, item, 0);
2395 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2396
2397 btrfs_mark_buffer_dirty(leaf);
2398 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002399 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002400
2401 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2402 new->disk_len, 0,
2403 backref->root_id, backref->inum,
2404 new->file_pos, 0); /* start - extent_offset */
2405 if (ret) {
2406 btrfs_abort_transaction(trans, root, ret);
2407 goto out_free_path;
2408 }
2409
2410 ret = 1;
2411out_free_path:
2412 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002413 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002414 btrfs_end_transaction(trans, root);
2415out_unlock:
2416 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2417 &cached, GFP_NOFS);
2418 iput(inode);
2419 return ret;
2420}
2421
2422static void relink_file_extents(struct new_sa_defrag_extent *new)
2423{
2424 struct btrfs_path *path;
2425 struct old_sa_defrag_extent *old, *tmp;
2426 struct sa_defrag_extent_backref *backref;
2427 struct sa_defrag_extent_backref *prev = NULL;
2428 struct inode *inode;
2429 struct btrfs_root *root;
2430 struct rb_node *node;
2431 int ret;
2432
2433 inode = new->inode;
2434 root = BTRFS_I(inode)->root;
2435
2436 path = btrfs_alloc_path();
2437 if (!path)
2438 return;
2439
2440 if (!record_extent_backrefs(path, new)) {
2441 btrfs_free_path(path);
2442 goto out;
2443 }
2444 btrfs_release_path(path);
2445
2446 while (1) {
2447 node = rb_first(&new->root);
2448 if (!node)
2449 break;
2450 rb_erase(node, &new->root);
2451
2452 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2453
2454 ret = relink_extent_backref(path, prev, backref);
2455 WARN_ON(ret < 0);
2456
2457 kfree(prev);
2458
2459 if (ret == 1)
2460 prev = backref;
2461 else
2462 prev = NULL;
2463 cond_resched();
2464 }
2465 kfree(prev);
2466
2467 btrfs_free_path(path);
2468
2469 list_for_each_entry_safe(old, tmp, &new->head, list) {
2470 list_del(&old->list);
2471 kfree(old);
2472 }
2473out:
2474 atomic_dec(&root->fs_info->defrag_running);
2475 wake_up(&root->fs_info->transaction_wait);
2476
2477 kfree(new);
2478}
2479
2480static struct new_sa_defrag_extent *
2481record_old_file_extents(struct inode *inode,
2482 struct btrfs_ordered_extent *ordered)
2483{
2484 struct btrfs_root *root = BTRFS_I(inode)->root;
2485 struct btrfs_path *path;
2486 struct btrfs_key key;
2487 struct old_sa_defrag_extent *old, *tmp;
2488 struct new_sa_defrag_extent *new;
2489 int ret;
2490
2491 new = kmalloc(sizeof(*new), GFP_NOFS);
2492 if (!new)
2493 return NULL;
2494
2495 new->inode = inode;
2496 new->file_pos = ordered->file_offset;
2497 new->len = ordered->len;
2498 new->bytenr = ordered->start;
2499 new->disk_len = ordered->disk_len;
2500 new->compress_type = ordered->compress_type;
2501 new->root = RB_ROOT;
2502 INIT_LIST_HEAD(&new->head);
2503
2504 path = btrfs_alloc_path();
2505 if (!path)
2506 goto out_kfree;
2507
2508 key.objectid = btrfs_ino(inode);
2509 key.type = BTRFS_EXTENT_DATA_KEY;
2510 key.offset = new->file_pos;
2511
2512 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2513 if (ret < 0)
2514 goto out_free_path;
2515 if (ret > 0 && path->slots[0] > 0)
2516 path->slots[0]--;
2517
2518 /* find out all the old extents for the file range */
2519 while (1) {
2520 struct btrfs_file_extent_item *extent;
2521 struct extent_buffer *l;
2522 int slot;
2523 u64 num_bytes;
2524 u64 offset;
2525 u64 end;
2526 u64 disk_bytenr;
2527 u64 extent_offset;
2528
2529 l = path->nodes[0];
2530 slot = path->slots[0];
2531
2532 if (slot >= btrfs_header_nritems(l)) {
2533 ret = btrfs_next_leaf(root, path);
2534 if (ret < 0)
2535 goto out_free_list;
2536 else if (ret > 0)
2537 break;
2538 continue;
2539 }
2540
2541 btrfs_item_key_to_cpu(l, &key, slot);
2542
2543 if (key.objectid != btrfs_ino(inode))
2544 break;
2545 if (key.type != BTRFS_EXTENT_DATA_KEY)
2546 break;
2547 if (key.offset >= new->file_pos + new->len)
2548 break;
2549
2550 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2551
2552 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2553 if (key.offset + num_bytes < new->file_pos)
2554 goto next;
2555
2556 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2557 if (!disk_bytenr)
2558 goto next;
2559
2560 extent_offset = btrfs_file_extent_offset(l, extent);
2561
2562 old = kmalloc(sizeof(*old), GFP_NOFS);
2563 if (!old)
2564 goto out_free_list;
2565
2566 offset = max(new->file_pos, key.offset);
2567 end = min(new->file_pos + new->len, key.offset + num_bytes);
2568
2569 old->bytenr = disk_bytenr;
2570 old->extent_offset = extent_offset;
2571 old->offset = offset - key.offset;
2572 old->len = end - offset;
2573 old->new = new;
2574 old->count = 0;
2575 list_add_tail(&old->list, &new->head);
2576next:
2577 path->slots[0]++;
2578 cond_resched();
2579 }
2580
2581 btrfs_free_path(path);
2582 atomic_inc(&root->fs_info->defrag_running);
2583
2584 return new;
2585
2586out_free_list:
2587 list_for_each_entry_safe(old, tmp, &new->head, list) {
2588 list_del(&old->list);
2589 kfree(old);
2590 }
2591out_free_path:
2592 btrfs_free_path(path);
2593out_kfree:
2594 kfree(new);
2595 return NULL;
2596}
2597
Chris Mason5d13a982009-03-13 11:41:46 -04002598/*
2599 * helper function for btrfs_finish_ordered_io, this
2600 * just reads in some of the csum leaves to prime them into ram
2601 * before we start the transaction. It limits the amount of btree
2602 * reads required while inside the transaction.
2603 */
Chris Masond352ac62008-09-29 15:18:18 -04002604/* as ordered data IO finishes, this gets called so we can finish
2605 * an ordered extent if the range of bytes in the file it covers are
2606 * fully written.
2607 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002608static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002609{
Josef Bacik5fd02042012-05-02 14:00:54 -04002610 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002612 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002613 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002614 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002615 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002616 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002617 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002618 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619
Liu Bo83eea1f2012-07-10 05:28:39 -06002620 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002621
Josef Bacik5fd02042012-05-02 14:00:54 -04002622 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2623 ret = -EIO;
2624 goto out;
2625 }
2626
Yan, Zhengc2167752009-11-12 09:34:21 +00002627 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002628 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002629 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2630 if (nolock)
2631 trans = btrfs_join_transaction_nolock(root);
2632 else
2633 trans = btrfs_join_transaction(root);
2634 if (IS_ERR(trans)) {
2635 ret = PTR_ERR(trans);
2636 trans = NULL;
2637 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002638 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002639 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2640 ret = btrfs_update_inode_fallback(trans, root, inode);
2641 if (ret) /* -ENOMEM or corruption */
2642 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002643 goto out;
2644 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Josef Bacik2ac55d42010-02-03 19:33:23 +00002646 lock_extent_bits(io_tree, ordered_extent->file_offset,
2647 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002648 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002649
Liu Bo38c227d2013-01-29 03:18:40 +00002650 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2651 ordered_extent->file_offset + ordered_extent->len - 1,
2652 EXTENT_DEFRAG, 1, cached_state);
2653 if (ret) {
2654 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2655 if (last_snapshot >= BTRFS_I(inode)->generation)
2656 /* the inode is shared */
2657 new = record_old_file_extents(inode, ordered_extent);
2658
2659 clear_extent_bit(io_tree, ordered_extent->file_offset,
2660 ordered_extent->file_offset + ordered_extent->len - 1,
2661 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2662 }
2663
Josef Bacik0cb59c92010-07-02 12:14:14 -04002664 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002665 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002666 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002667 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002668 if (IS_ERR(trans)) {
2669 ret = PTR_ERR(trans);
2670 trans = NULL;
2671 goto out_unlock;
2672 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002673 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002674
Chris Masonc8b97812008-10-29 14:49:59 -04002675 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002676 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002677 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002678 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002679 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002680 ordered_extent->file_offset,
2681 ordered_extent->file_offset +
2682 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002683 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002684 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002685 ret = insert_reserved_file_extent(trans, inode,
2686 ordered_extent->file_offset,
2687 ordered_extent->start,
2688 ordered_extent->disk_len,
2689 ordered_extent->len,
2690 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002691 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002692 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002693 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002694 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2695 ordered_extent->file_offset, ordered_extent->len,
2696 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002697 if (ret < 0) {
2698 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002699 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002700 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002701
Chris Masone6dcd2d2008-07-17 12:53:50 -04002702 add_pending_csums(trans, inode, ordered_extent->file_offset,
2703 &ordered_extent->list);
2704
Josef Bacik6c760c02012-11-09 10:53:21 -05002705 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2706 ret = btrfs_update_inode_fallback(trans, root, inode);
2707 if (ret) { /* -ENOMEM or corruption */
2708 btrfs_abort_transaction(trans, root, ret);
2709 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002710 }
2711 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002712out_unlock:
2713 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2714 ordered_extent->file_offset +
2715 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002716out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002717 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002718 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002719 if (trans)
2720 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002721
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002722 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002723 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2724 ordered_extent->file_offset +
2725 ordered_extent->len - 1, NULL, GFP_NOFS);
2726
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002727 /*
2728 * If the ordered extent had an IOERR or something else went
2729 * wrong we need to return the space for this ordered extent
2730 * back to the allocator.
2731 */
2732 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2733 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2734 btrfs_free_reserved_extent(root, ordered_extent->start,
2735 ordered_extent->disk_len);
2736 }
2737
2738
Josef Bacik5fd02042012-05-02 14:00:54 -04002739 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002740 * This needs to be done to make sure anybody waiting knows we are done
2741 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002742 */
2743 btrfs_remove_ordered_extent(inode, ordered_extent);
2744
Liu Bo38c227d2013-01-29 03:18:40 +00002745 /* for snapshot-aware defrag */
2746 if (new)
2747 relink_file_extents(new);
2748
Chris Masone6dcd2d2008-07-17 12:53:50 -04002749 /* once for us */
2750 btrfs_put_ordered_extent(ordered_extent);
2751 /* once for the tree */
2752 btrfs_put_ordered_extent(ordered_extent);
2753
Josef Bacik5fd02042012-05-02 14:00:54 -04002754 return ret;
2755}
2756
2757static void finish_ordered_fn(struct btrfs_work *work)
2758{
2759 struct btrfs_ordered_extent *ordered_extent;
2760 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2761 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002762}
2763
Christoph Hellwigb2950862008-12-02 09:54:17 -05002764static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002765 struct extent_state *state, int uptodate)
2766{
Josef Bacik5fd02042012-05-02 14:00:54 -04002767 struct inode *inode = page->mapping->host;
2768 struct btrfs_root *root = BTRFS_I(inode)->root;
2769 struct btrfs_ordered_extent *ordered_extent = NULL;
2770 struct btrfs_workers *workers;
2771
liubo1abe9b82011-03-24 11:18:59 +00002772 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2773
Chris Mason8b62b722009-09-02 16:53:46 -04002774 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2776 end - start + 1, uptodate))
2777 return 0;
2778
2779 ordered_extent->work.func = finish_ordered_fn;
2780 ordered_extent->work.flags = 0;
2781
Liu Bo83eea1f2012-07-10 05:28:39 -06002782 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002783 workers = &root->fs_info->endio_freespace_worker;
2784 else
2785 workers = &root->fs_info->endio_write_workers;
2786 btrfs_queue_worker(workers, &ordered_extent->work);
2787
2788 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002789}
2790
Chris Masond352ac62008-09-29 15:18:18 -04002791/*
Chris Masond352ac62008-09-29 15:18:18 -04002792 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002793 * if there's a match, we allow the bio to finish. If not, the code in
2794 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002795 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002796static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002797 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002798{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002799 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002800 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002801 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002802 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002803 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002804 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002805 struct btrfs_root *root = BTRFS_I(inode)->root;
2806 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002807 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2808 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002809
Chris Masond20f7042008-12-08 16:58:54 -05002810 if (PageChecked(page)) {
2811 ClearPageChecked(page);
2812 goto good;
2813 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002814
2815 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002816 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002817
Yan Zheng17d217f2008-12-12 10:03:38 -05002818 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002819 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002820 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2821 GFP_NOFS);
2822 return 0;
2823 }
2824
Yanc2e639f2008-02-04 08:57:25 -05002825 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002826 private = state->private;
2827 ret = 0;
2828 } else {
2829 ret = get_state_private(io_tree, start, &private);
2830 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002831 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002832 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002833 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002834
Liu Bob0496682013-03-14 14:57:45 +00002835 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002836 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002837 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002838 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002839
Cong Wang7ac687d2011-11-25 23:14:28 +08002840 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002841good:
Chris Mason07157aa2007-08-30 08:50:51 -04002842 return 0;
2843
2844zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002845 if (__ratelimit(&_rs))
2846 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2847 (unsigned long long)btrfs_ino(page->mapping->host),
2848 (unsigned long long)start, csum,
2849 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002850 memset(kaddr + offset, 1, end - start + 1);
2851 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002852 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002853 if (private == 0)
2854 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002855 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002856}
Chris Masonb888db22007-08-27 16:49:44 -04002857
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002858struct delayed_iput {
2859 struct list_head list;
2860 struct inode *inode;
2861};
2862
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002863/* JDM: If this is fs-wide, why can't we add a pointer to
2864 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002865void btrfs_add_delayed_iput(struct inode *inode)
2866{
2867 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2868 struct delayed_iput *delayed;
2869
2870 if (atomic_add_unless(&inode->i_count, -1, 1))
2871 return;
2872
2873 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2874 delayed->inode = inode;
2875
2876 spin_lock(&fs_info->delayed_iput_lock);
2877 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2878 spin_unlock(&fs_info->delayed_iput_lock);
2879}
2880
2881void btrfs_run_delayed_iputs(struct btrfs_root *root)
2882{
2883 LIST_HEAD(list);
2884 struct btrfs_fs_info *fs_info = root->fs_info;
2885 struct delayed_iput *delayed;
2886 int empty;
2887
2888 spin_lock(&fs_info->delayed_iput_lock);
2889 empty = list_empty(&fs_info->delayed_iputs);
2890 spin_unlock(&fs_info->delayed_iput_lock);
2891 if (empty)
2892 return;
2893
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002894 spin_lock(&fs_info->delayed_iput_lock);
2895 list_splice_init(&fs_info->delayed_iputs, &list);
2896 spin_unlock(&fs_info->delayed_iput_lock);
2897
2898 while (!list_empty(&list)) {
2899 delayed = list_entry(list.next, struct delayed_iput, list);
2900 list_del(&delayed->list);
2901 iput(delayed->inode);
2902 kfree(delayed);
2903 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002904}
2905
Yan, Zhengd68fc572010-05-16 10:49:58 -04002906/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002907 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002908 * files in the subvolume, it removes orphan item and frees block_rsv
2909 * structure.
2910 */
2911void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2912 struct btrfs_root *root)
2913{
Josef Bacik90290e12011-12-02 15:44:12 -05002914 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002915 int ret;
2916
Josef Bacik8a35d952012-05-23 14:26:42 -04002917 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002918 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2919 return;
2920
Josef Bacik90290e12011-12-02 15:44:12 -05002921 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002922 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002923 spin_unlock(&root->orphan_lock);
2924 return;
2925 }
2926
2927 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2928 spin_unlock(&root->orphan_lock);
2929 return;
2930 }
2931
2932 block_rsv = root->orphan_block_rsv;
2933 root->orphan_block_rsv = NULL;
2934 spin_unlock(&root->orphan_lock);
2935
Yan, Zhengd68fc572010-05-16 10:49:58 -04002936 if (root->orphan_item_inserted &&
2937 btrfs_root_refs(&root->root_item) > 0) {
2938 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2939 root->root_key.objectid);
2940 BUG_ON(ret);
2941 root->orphan_item_inserted = 0;
2942 }
2943
Josef Bacik90290e12011-12-02 15:44:12 -05002944 if (block_rsv) {
2945 WARN_ON(block_rsv->size > 0);
2946 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002947 }
2948}
2949
2950/*
Josef Bacik7b128762008-07-24 12:17:14 -04002951 * This creates an orphan entry for the given inode in case something goes
2952 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002953 *
2954 * NOTE: caller of this function should reserve 5 units of metadata for
2955 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002956 */
2957int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2958{
2959 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 struct btrfs_block_rsv *block_rsv = NULL;
2961 int reserve = 0;
2962 int insert = 0;
2963 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002964
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002966 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002967 if (!block_rsv)
2968 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002969 }
2970
Yan, Zhengd68fc572010-05-16 10:49:58 -04002971 spin_lock(&root->orphan_lock);
2972 if (!root->orphan_block_rsv) {
2973 root->orphan_block_rsv = block_rsv;
2974 } else if (block_rsv) {
2975 btrfs_free_block_rsv(root, block_rsv);
2976 block_rsv = NULL;
2977 }
Josef Bacik7b128762008-07-24 12:17:14 -04002978
Josef Bacik8a35d952012-05-23 14:26:42 -04002979 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2980 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002981#if 0
2982 /*
2983 * For proper ENOSPC handling, we should do orphan
2984 * cleanup when mounting. But this introduces backward
2985 * compatibility issue.
2986 */
2987 if (!xchg(&root->orphan_item_inserted, 1))
2988 insert = 2;
2989 else
2990 insert = 1;
2991#endif
2992 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002993 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 }
Josef Bacik7b128762008-07-24 12:17:14 -04002995
Josef Bacik72ac3c02012-05-23 14:13:11 -04002996 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2997 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002998 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003000
Yan, Zhengd68fc572010-05-16 10:49:58 -04003001 /* grab metadata reservation from transaction handle */
3002 if (reserve) {
3003 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003004 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003005 }
3006
3007 /* insert an orphan item to track this unlinked/truncated file */
3008 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003009 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003010 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003011 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3012 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003013 btrfs_abort_transaction(trans, root, ret);
3014 return ret;
3015 }
3016 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 }
3018
3019 /* insert an orphan item to track subvolume contains orphan files */
3020 if (insert >= 2) {
3021 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3022 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003023 if (ret && ret != -EEXIST) {
3024 btrfs_abort_transaction(trans, root, ret);
3025 return ret;
3026 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 }
3028 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003029}
3030
3031/*
3032 * We have done the truncate/delete so we can go ahead and remove the orphan
3033 * item for this particular inode.
3034 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003035static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3036 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003037{
3038 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003039 int delete_item = 0;
3040 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003041 int ret = 0;
3042
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003044 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3045 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003047
Josef Bacik72ac3c02012-05-23 14:13:11 -04003048 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3049 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 spin_unlock(&root->orphan_lock);
3052
3053 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003054 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003055 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003056 }
3057
Josef Bacik8a35d952012-05-23 14:26:42 -04003058 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003060 atomic_dec(&root->orphan_inodes);
3061 }
Josef Bacik7b128762008-07-24 12:17:14 -04003062
Yan, Zhengd68fc572010-05-16 10:49:58 -04003063 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003064}
3065
3066/*
3067 * this cleans up any orphans that may be left on the list from the last use
3068 * of this root.
3069 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003070int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003071{
3072 struct btrfs_path *path;
3073 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003074 struct btrfs_key key, found_key;
3075 struct btrfs_trans_handle *trans;
3076 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003077 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003078 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3079
Yan, Zhengd68fc572010-05-16 10:49:58 -04003080 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003081 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003082
3083 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003084 if (!path) {
3085 ret = -ENOMEM;
3086 goto out;
3087 }
Josef Bacik7b128762008-07-24 12:17:14 -04003088 path->reada = -1;
3089
3090 key.objectid = BTRFS_ORPHAN_OBJECTID;
3091 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3092 key.offset = (u64)-1;
3093
Josef Bacik7b128762008-07-24 12:17:14 -04003094 while (1) {
3095 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003096 if (ret < 0)
3097 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003098
3099 /*
3100 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003101 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003102 * find the key and see if we have stuff that matches
3103 */
3104 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003105 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003106 if (path->slots[0] == 0)
3107 break;
3108 path->slots[0]--;
3109 }
3110
3111 /* pull out the item */
3112 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003113 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3114
3115 /* make sure the item matches what we want */
3116 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3117 break;
3118 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3119 break;
3120
3121 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003122 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003123
3124 /*
3125 * this is where we are basically btrfs_lookup, without the
3126 * crossing root thing. we store the inode number in the
3127 * offset of the orphan item.
3128 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003129
3130 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003131 btrfs_err(root->fs_info,
3132 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003133 ret = -EINVAL;
3134 goto out;
3135 }
3136
3137 last_objectid = found_key.offset;
3138
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003139 found_key.objectid = found_key.offset;
3140 found_key.type = BTRFS_INODE_ITEM_KEY;
3141 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003142 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003143 ret = PTR_RET(inode);
3144 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003145 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003146
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003147 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3148 struct btrfs_root *dead_root;
3149 struct btrfs_fs_info *fs_info = root->fs_info;
3150 int is_dead_root = 0;
3151
3152 /*
3153 * this is an orphan in the tree root. Currently these
3154 * could come from 2 sources:
3155 * a) a snapshot deletion in progress
3156 * b) a free space cache inode
3157 * We need to distinguish those two, as the snapshot
3158 * orphan must not get deleted.
3159 * find_dead_roots already ran before us, so if this
3160 * is a snapshot deletion, we should find the root
3161 * in the dead_roots list
3162 */
3163 spin_lock(&fs_info->trans_lock);
3164 list_for_each_entry(dead_root, &fs_info->dead_roots,
3165 root_list) {
3166 if (dead_root->root_key.objectid ==
3167 found_key.objectid) {
3168 is_dead_root = 1;
3169 break;
3170 }
3171 }
3172 spin_unlock(&fs_info->trans_lock);
3173 if (is_dead_root) {
3174 /* prevent this orphan from being found again */
3175 key.offset = found_key.objectid - 1;
3176 continue;
3177 }
3178 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003179 /*
3180 * Inode is already gone but the orphan item is still there,
3181 * kill the orphan item.
3182 */
3183 if (ret == -ESTALE) {
3184 trans = btrfs_start_transaction(root, 1);
3185 if (IS_ERR(trans)) {
3186 ret = PTR_ERR(trans);
3187 goto out;
3188 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003189 btrfs_debug(root->fs_info, "auto deleting %Lu",
3190 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003191 ret = btrfs_del_orphan_item(trans, root,
3192 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003193 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003194 btrfs_end_transaction(trans, root);
3195 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003196 }
Josef Bacik7b128762008-07-24 12:17:14 -04003197
Josef Bacik7b128762008-07-24 12:17:14 -04003198 /*
3199 * add this inode to the orphan list so btrfs_orphan_del does
3200 * the proper thing when we hit it
3201 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003202 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3203 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003204 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003205
Josef Bacik7b128762008-07-24 12:17:14 -04003206 /* if we have links, this was a truncate, lets do that */
3207 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003208 if (!S_ISREG(inode->i_mode)) {
3209 WARN_ON(1);
3210 iput(inode);
3211 continue;
3212 }
Josef Bacik7b128762008-07-24 12:17:14 -04003213 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003214
3215 /* 1 for the orphan item deletion. */
3216 trans = btrfs_start_transaction(root, 1);
3217 if (IS_ERR(trans)) {
3218 ret = PTR_ERR(trans);
3219 goto out;
3220 }
3221 ret = btrfs_orphan_add(trans, inode);
3222 btrfs_end_transaction(trans, root);
3223 if (ret)
3224 goto out;
3225
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003226 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003227 if (ret)
3228 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003229 } else {
3230 nr_unlink++;
3231 }
3232
3233 /* this will do delete_inode and everything for us */
3234 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003235 if (ret)
3236 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003237 }
Miao Xie3254c872011-11-10 20:45:05 -05003238 /* release the path since we're done with it */
3239 btrfs_release_path(path);
3240
Yan, Zhengd68fc572010-05-16 10:49:58 -04003241 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3242
3243 if (root->orphan_block_rsv)
3244 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3245 (u64)-1);
3246
3247 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003248 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003249 if (!IS_ERR(trans))
3250 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 }
Josef Bacik7b128762008-07-24 12:17:14 -04003252
3253 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003254 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003255 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003256 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003257
3258out:
3259 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003260 btrfs_crit(root->fs_info,
3261 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003262 btrfs_free_path(path);
3263 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003264}
3265
Chris Masond352ac62008-09-29 15:18:18 -04003266/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003267 * very simple check to peek ahead in the leaf looking for xattrs. If we
3268 * don't find any xattrs, we know there can't be any acls.
3269 *
3270 * slot is the slot the inode is in, objectid is the objectid of the inode
3271 */
3272static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3273 int slot, u64 objectid)
3274{
3275 u32 nritems = btrfs_header_nritems(leaf);
3276 struct btrfs_key found_key;
3277 int scanned = 0;
3278
3279 slot++;
3280 while (slot < nritems) {
3281 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3282
3283 /* we found a different objectid, there must not be acls */
3284 if (found_key.objectid != objectid)
3285 return 0;
3286
3287 /* we found an xattr, assume we've got an acl */
3288 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3289 return 1;
3290
3291 /*
3292 * we found a key greater than an xattr key, there can't
3293 * be any acls later on
3294 */
3295 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3296 return 0;
3297
3298 slot++;
3299 scanned++;
3300
3301 /*
3302 * it goes inode, inode backrefs, xattrs, extents,
3303 * so if there are a ton of hard links to an inode there can
3304 * be a lot of backrefs. Don't waste time searching too hard,
3305 * this is just an optimization
3306 */
3307 if (scanned >= 8)
3308 break;
3309 }
3310 /* we hit the end of the leaf before we found an xattr or
3311 * something larger than an xattr. We have to assume the inode
3312 * has acls
3313 */
3314 return 1;
3315}
3316
3317/*
Chris Masond352ac62008-09-29 15:18:18 -04003318 * read an inode from the btree into the in-memory inode
3319 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003321{
3322 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003323 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003324 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003325 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003326 struct btrfs_root *root = BTRFS_I(inode)->root;
3327 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003328 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003329 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003330 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003331 bool filled = false;
3332
3333 ret = btrfs_fill_inode(inode, &rdev);
3334 if (!ret)
3335 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003336
3337 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003338 if (!path)
3339 goto make_bad;
3340
Josef Bacikd90c7322011-05-17 09:50:54 -04003341 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003342 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003343
Chris Mason39279cc2007-06-12 06:35:45 -04003344 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003345 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003346 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003347
Chris Mason5f39d392007-10-15 16:14:19 -04003348 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003349
3350 if (filled)
3351 goto cache_acl;
3352
Chris Mason5f39d392007-10-15 16:14:19 -04003353 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3354 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003355 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003356 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003357 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3358 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003359 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003360
3361 tspec = btrfs_inode_atime(inode_item);
3362 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3363 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3364
3365 tspec = btrfs_inode_mtime(inode_item);
3366 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3367 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3368
3369 tspec = btrfs_inode_ctime(inode_item);
3370 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3371 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3372
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003373 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003374 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003375 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3376
3377 /*
3378 * If we were modified in the current generation and evicted from memory
3379 * and then re-read we need to do a full sync since we don't have any
3380 * idea about which extents were modified before we were evicted from
3381 * cache.
3382 */
3383 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3384 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3385 &BTRFS_I(inode)->runtime_flags);
3386
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003387 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003388 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003389 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003390 rdev = btrfs_inode_rdev(leaf, inode_item);
3391
Josef Bacikaec74772008-07-24 12:12:38 -04003392 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003393 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003394cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003395 /*
3396 * try to precache a NULL acl entry for files that don't have
3397 * any xattrs or acls
3398 */
Li Zefan33345d012011-04-20 10:31:50 +08003399 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3400 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003401 if (!maybe_acls)
3402 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003403
Chris Mason39279cc2007-06-12 06:35:45 -04003404 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003405
Chris Mason39279cc2007-06-12 06:35:45 -04003406 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003407 case S_IFREG:
3408 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003409 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003410 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003411 inode->i_fop = &btrfs_file_operations;
3412 inode->i_op = &btrfs_file_inode_operations;
3413 break;
3414 case S_IFDIR:
3415 inode->i_fop = &btrfs_dir_file_operations;
3416 if (root == root->fs_info->tree_root)
3417 inode->i_op = &btrfs_dir_ro_inode_operations;
3418 else
3419 inode->i_op = &btrfs_dir_inode_operations;
3420 break;
3421 case S_IFLNK:
3422 inode->i_op = &btrfs_symlink_inode_operations;
3423 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003424 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003425 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003426 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003427 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003428 init_special_inode(inode, inode->i_mode, rdev);
3429 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003430 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003431
3432 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003433 return;
3434
3435make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003436 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003437 make_bad_inode(inode);
3438}
3439
Chris Masond352ac62008-09-29 15:18:18 -04003440/*
3441 * given a leaf and an inode, copy the inode fields into the leaf
3442 */
Chris Masone02119d2008-09-05 16:13:11 -04003443static void fill_inode_item(struct btrfs_trans_handle *trans,
3444 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003445 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003446 struct inode *inode)
3447{
Liu Bo51fab692012-12-27 09:01:21 +00003448 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003449
Liu Bo51fab692012-12-27 09:01:21 +00003450 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003451
Liu Bo51fab692012-12-27 09:01:21 +00003452 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3453 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3454 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3455 &token);
3456 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3457 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003458
Liu Bo51fab692012-12-27 09:01:21 +00003459 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3460 inode->i_atime.tv_sec, &token);
3461 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3462 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003463
Liu Bo51fab692012-12-27 09:01:21 +00003464 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3465 inode->i_mtime.tv_sec, &token);
3466 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3467 inode->i_mtime.tv_nsec, &token);
3468
3469 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3470 inode->i_ctime.tv_sec, &token);
3471 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3472 inode->i_ctime.tv_nsec, &token);
3473
3474 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3475 &token);
3476 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3477 &token);
3478 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3479 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3480 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3481 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3482 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003483}
3484
Chris Masond352ac62008-09-29 15:18:18 -04003485/*
3486 * copy everything in the in-memory inode into the btree.
3487 */
Chris Mason21151332011-11-10 20:39:08 -05003488static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003489 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003490{
3491 struct btrfs_inode_item *inode_item;
3492 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003493 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003494 int ret;
3495
3496 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003497 if (!path)
3498 return -ENOMEM;
3499
Chris Masonb9473432009-03-13 11:00:37 -04003500 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003501 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3502 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003503 if (ret) {
3504 if (ret > 0)
3505 ret = -ENOENT;
3506 goto failed;
3507 }
3508
Chris Masonb4ce94d2009-02-04 09:25:08 -05003509 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003510 leaf = path->nodes[0];
3511 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003512 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003513
Chris Masone02119d2008-09-05 16:13:11 -04003514 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003515 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003516 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003517 ret = 0;
3518failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003519 btrfs_free_path(path);
3520 return ret;
3521}
3522
Chris Masond352ac62008-09-29 15:18:18 -04003523/*
Chris Mason21151332011-11-10 20:39:08 -05003524 * copy everything in the in-memory inode into the btree.
3525 */
3526noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3527 struct btrfs_root *root, struct inode *inode)
3528{
3529 int ret;
3530
3531 /*
3532 * If the inode is a free space inode, we can deadlock during commit
3533 * if we put it into the delayed code.
3534 *
3535 * The data relocation inode should also be directly updated
3536 * without delay
3537 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003538 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003539 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003540 btrfs_update_root_times(trans, root);
3541
Chris Mason21151332011-11-10 20:39:08 -05003542 ret = btrfs_delayed_update_inode(trans, root, inode);
3543 if (!ret)
3544 btrfs_set_inode_last_trans(trans, inode);
3545 return ret;
3546 }
3547
3548 return btrfs_update_inode_item(trans, root, inode);
3549}
3550
Josef Bacikbe6aef62012-10-22 15:43:12 -04003551noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3552 struct btrfs_root *root,
3553 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003554{
3555 int ret;
3556
3557 ret = btrfs_update_inode(trans, root, inode);
3558 if (ret == -ENOSPC)
3559 return btrfs_update_inode_item(trans, root, inode);
3560 return ret;
3561}
3562
3563/*
Chris Masond352ac62008-09-29 15:18:18 -04003564 * unlink helper that gets used here in inode.c and in the tree logging
3565 * recovery code. It remove a link in a directory with a given name, and
3566 * also drops the back refs in the inode to the directory
3567 */
Al Viro92986792011-03-04 17:14:37 +00003568static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3569 struct btrfs_root *root,
3570 struct inode *dir, struct inode *inode,
3571 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003572{
3573 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003574 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003575 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003576 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003577 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003578 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003579 u64 ino = btrfs_ino(inode);
3580 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003581
3582 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003583 if (!path) {
3584 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003585 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003586 }
3587
Chris Masonb9473432009-03-13 11:00:37 -04003588 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003589 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003590 name, name_len, -1);
3591 if (IS_ERR(di)) {
3592 ret = PTR_ERR(di);
3593 goto err;
3594 }
3595 if (!di) {
3596 ret = -ENOENT;
3597 goto err;
3598 }
Chris Mason5f39d392007-10-15 16:14:19 -04003599 leaf = path->nodes[0];
3600 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003601 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003602 if (ret)
3603 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003604 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003605
Li Zefan33345d012011-04-20 10:31:50 +08003606 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3607 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003608 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003609 btrfs_info(root->fs_info,
3610 "failed to delete reference to %.*s, inode %llu parent %llu",
3611 name_len, name,
3612 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003613 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003614 goto err;
3615 }
3616
Miao Xie16cdcec2011-04-22 18:12:22 +08003617 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003618 if (ret) {
3619 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003620 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003621 }
Chris Mason39279cc2007-06-12 06:35:45 -04003622
Chris Masone02119d2008-09-05 16:13:11 -04003623 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003624 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003625 if (ret != 0 && ret != -ENOENT) {
3626 btrfs_abort_transaction(trans, root, ret);
3627 goto err;
3628 }
Chris Masone02119d2008-09-05 16:13:11 -04003629
3630 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3631 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003632 if (ret == -ENOENT)
3633 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003634 else if (ret)
3635 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003636err:
3637 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003638 if (ret)
3639 goto out;
3640
3641 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003642 inode_inc_iversion(inode);
3643 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003644 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003645 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003646out:
Chris Mason39279cc2007-06-12 06:35:45 -04003647 return ret;
3648}
3649
Al Viro92986792011-03-04 17:14:37 +00003650int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3651 struct btrfs_root *root,
3652 struct inode *dir, struct inode *inode,
3653 const char *name, int name_len)
3654{
3655 int ret;
3656 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3657 if (!ret) {
3658 btrfs_drop_nlink(inode);
3659 ret = btrfs_update_inode(trans, root, inode);
3660 }
3661 return ret;
3662}
3663
3664
Yan, Zhenga22285a2010-05-16 10:48:46 -04003665/* helper to check if there is any shared block in the path */
3666static int check_path_shared(struct btrfs_root *root,
3667 struct btrfs_path *path)
3668{
3669 struct extent_buffer *eb;
3670 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003671 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003672
3673 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003674 int ret;
3675
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676 if (!path->nodes[level])
3677 break;
3678 eb = path->nodes[level];
3679 if (!btrfs_block_can_be_shared(root, eb))
3680 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003681 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682 &refs, NULL);
3683 if (refs > 1)
3684 return 1;
3685 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003686 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687}
3688
3689/*
3690 * helper to start transaction for unlink and rmdir.
3691 *
3692 * unlink and rmdir are special in btrfs, they do not always free space.
3693 * so in enospc case, we should make sure they will free space before
3694 * allowing them to use the global metadata reservation.
3695 */
3696static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3697 struct dentry *dentry)
3698{
3699 struct btrfs_trans_handle *trans;
3700 struct btrfs_root *root = BTRFS_I(dir)->root;
3701 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 struct btrfs_dir_item *di;
3703 struct inode *inode = dentry->d_inode;
3704 u64 index;
3705 int check_link = 1;
3706 int err = -ENOSPC;
3707 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003708 u64 ino = btrfs_ino(inode);
3709 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710
Josef Bacike70bea52011-10-11 14:18:24 -04003711 /*
3712 * 1 for the possible orphan item
3713 * 1 for the dir item
3714 * 1 for the dir index
3715 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003716 * 1 for the inode
3717 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003718 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3720 return trans;
3721
Li Zefan33345d012011-04-20 10:31:50 +08003722 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003723 return ERR_PTR(-ENOSPC);
3724
3725 /* check if there is someone else holds reference */
3726 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3727 return ERR_PTR(-ENOSPC);
3728
3729 if (atomic_read(&inode->i_count) > 2)
3730 return ERR_PTR(-ENOSPC);
3731
3732 if (xchg(&root->fs_info->enospc_unlink, 1))
3733 return ERR_PTR(-ENOSPC);
3734
3735 path = btrfs_alloc_path();
3736 if (!path) {
3737 root->fs_info->enospc_unlink = 0;
3738 return ERR_PTR(-ENOMEM);
3739 }
3740
Josef Bacik3880a1b2011-10-14 14:46:51 -04003741 /* 1 for the orphan item */
3742 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003743 if (IS_ERR(trans)) {
3744 btrfs_free_path(path);
3745 root->fs_info->enospc_unlink = 0;
3746 return trans;
3747 }
3748
3749 path->skip_locking = 1;
3750 path->search_commit_root = 1;
3751
3752 ret = btrfs_lookup_inode(trans, root, path,
3753 &BTRFS_I(dir)->location, 0);
3754 if (ret < 0) {
3755 err = ret;
3756 goto out;
3757 }
3758 if (ret == 0) {
3759 if (check_path_shared(root, path))
3760 goto out;
3761 } else {
3762 check_link = 0;
3763 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003764 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003765
3766 ret = btrfs_lookup_inode(trans, root, path,
3767 &BTRFS_I(inode)->location, 0);
3768 if (ret < 0) {
3769 err = ret;
3770 goto out;
3771 }
3772 if (ret == 0) {
3773 if (check_path_shared(root, path))
3774 goto out;
3775 } else {
3776 check_link = 0;
3777 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003778 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003779
3780 if (ret == 0 && S_ISREG(inode->i_mode)) {
3781 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003782 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003783 if (ret < 0) {
3784 err = ret;
3785 goto out;
3786 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003787 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003788 if (check_path_shared(root, path))
3789 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003790 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003791 }
3792
3793 if (!check_link) {
3794 err = 0;
3795 goto out;
3796 }
3797
Li Zefan33345d012011-04-20 10:31:50 +08003798 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003799 dentry->d_name.name, dentry->d_name.len, 0);
3800 if (IS_ERR(di)) {
3801 err = PTR_ERR(di);
3802 goto out;
3803 }
3804 if (di) {
3805 if (check_path_shared(root, path))
3806 goto out;
3807 } else {
3808 err = 0;
3809 goto out;
3810 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003811 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003812
Mark Fashehf1863732012-08-08 11:32:27 -07003813 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3814 dentry->d_name.len, ino, dir_ino, 0,
3815 &index);
3816 if (ret) {
3817 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003818 goto out;
3819 }
Mark Fashehf1863732012-08-08 11:32:27 -07003820
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821 if (check_path_shared(root, path))
3822 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003823
David Sterbab3b4aa72011-04-21 01:20:15 +02003824 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003825
Miao Xie16cdcec2011-04-22 18:12:22 +08003826 /*
3827 * This is a commit root search, if we can lookup inode item and other
3828 * relative items in the commit root, it means the transaction of
3829 * dir/file creation has been committed, and the dir index item that we
3830 * delay to insert has also been inserted into the commit root. So
3831 * we needn't worry about the delayed insertion of the dir index item
3832 * here.
3833 */
Li Zefan33345d012011-04-20 10:31:50 +08003834 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003835 dentry->d_name.name, dentry->d_name.len, 0);
3836 if (IS_ERR(di)) {
3837 err = PTR_ERR(di);
3838 goto out;
3839 }
3840 BUG_ON(ret == -ENOENT);
3841 if (check_path_shared(root, path))
3842 goto out;
3843
3844 err = 0;
3845out:
3846 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003847 /* Migrate the orphan reservation over */
3848 if (!err)
3849 err = btrfs_block_rsv_migrate(trans->block_rsv,
3850 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003851 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003852
Yan, Zhenga22285a2010-05-16 10:48:46 -04003853 if (err) {
3854 btrfs_end_transaction(trans, root);
3855 root->fs_info->enospc_unlink = 0;
3856 return ERR_PTR(err);
3857 }
3858
3859 trans->block_rsv = &root->fs_info->global_block_rsv;
3860 return trans;
3861}
3862
3863static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3864 struct btrfs_root *root)
3865{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003866 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003867 btrfs_block_rsv_release(root, trans->block_rsv,
3868 trans->bytes_reserved);
3869 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003870 BUG_ON(!root->fs_info->enospc_unlink);
3871 root->fs_info->enospc_unlink = 0;
3872 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003873 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003874}
3875
Chris Mason39279cc2007-06-12 06:35:45 -04003876static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3877{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003878 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003879 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003880 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003881 int ret;
3882
Yan, Zhenga22285a2010-05-16 10:48:46 -04003883 trans = __unlink_start_trans(dir, dentry);
3884 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003885 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003886
Chris Mason12fcfd22009-03-24 10:24:20 -04003887 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3888
Chris Masone02119d2008-09-05 16:13:11 -04003889 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3890 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003891 if (ret)
3892 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003893
Yan, Zhenga22285a2010-05-16 10:48:46 -04003894 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003895 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003896 if (ret)
3897 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003898 }
Josef Bacik7b128762008-07-24 12:17:14 -04003899
Tsutomu Itohb5324022011-07-19 07:27:20 +00003900out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003901 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003902 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003903 return ret;
3904}
3905
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003906int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3907 struct btrfs_root *root,
3908 struct inode *dir, u64 objectid,
3909 const char *name, int name_len)
3910{
3911 struct btrfs_path *path;
3912 struct extent_buffer *leaf;
3913 struct btrfs_dir_item *di;
3914 struct btrfs_key key;
3915 u64 index;
3916 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003917 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003918
3919 path = btrfs_alloc_path();
3920 if (!path)
3921 return -ENOMEM;
3922
Li Zefan33345d012011-04-20 10:31:50 +08003923 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003924 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003925 if (IS_ERR_OR_NULL(di)) {
3926 if (!di)
3927 ret = -ENOENT;
3928 else
3929 ret = PTR_ERR(di);
3930 goto out;
3931 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003932
3933 leaf = path->nodes[0];
3934 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3935 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3936 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003937 if (ret) {
3938 btrfs_abort_transaction(trans, root, ret);
3939 goto out;
3940 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003941 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942
3943 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3944 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003945 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003946 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003947 if (ret != -ENOENT) {
3948 btrfs_abort_transaction(trans, root, ret);
3949 goto out;
3950 }
Li Zefan33345d012011-04-20 10:31:50 +08003951 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003952 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003953 if (IS_ERR_OR_NULL(di)) {
3954 if (!di)
3955 ret = -ENOENT;
3956 else
3957 ret = PTR_ERR(di);
3958 btrfs_abort_transaction(trans, root, ret);
3959 goto out;
3960 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003961
3962 leaf = path->nodes[0];
3963 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003964 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003965 index = key.offset;
3966 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003967 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968
Miao Xie16cdcec2011-04-22 18:12:22 +08003969 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003970 if (ret) {
3971 btrfs_abort_transaction(trans, root, ret);
3972 goto out;
3973 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003974
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003975 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003976 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003977 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003978 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003979 if (ret)
3980 btrfs_abort_transaction(trans, root, ret);
3981out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003982 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003983 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003984}
3985
Chris Mason39279cc2007-06-12 06:35:45 -04003986static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3987{
3988 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003989 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003991 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003992
David Sterbab3ae2442012-09-13 16:04:34 -06003993 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003994 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003995 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3996 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003997
Yan, Zhenga22285a2010-05-16 10:48:46 -04003998 trans = __unlink_start_trans(dir, dentry);
3999 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004000 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004001
Li Zefan33345d012011-04-20 10:31:50 +08004002 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004003 err = btrfs_unlink_subvol(trans, root, dir,
4004 BTRFS_I(inode)->location.objectid,
4005 dentry->d_name.name,
4006 dentry->d_name.len);
4007 goto out;
4008 }
4009
Josef Bacik7b128762008-07-24 12:17:14 -04004010 err = btrfs_orphan_add(trans, inode);
4011 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004012 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004013
Chris Mason39279cc2007-06-12 06:35:45 -04004014 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004015 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4016 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004017 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004018 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004019out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004021 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004022
Chris Mason39279cc2007-06-12 06:35:45 -04004023 return err;
4024}
4025
Chris Mason323ac952008-10-01 19:05:46 -04004026/*
Chris Mason39279cc2007-06-12 06:35:45 -04004027 * this can truncate away extent items, csum items and directory items.
4028 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004029 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004030 *
4031 * csum items that cross the new i_size are truncated to the new size
4032 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004033 *
4034 * min_type is the minimum key type to truncate down to. If set to 0, this
4035 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004036 */
Yan, Zheng80825102009-11-12 09:35:36 +00004037int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4038 struct btrfs_root *root,
4039 struct inode *inode,
4040 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004041{
Chris Mason39279cc2007-06-12 06:35:45 -04004042 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004043 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004044 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004045 struct btrfs_key key;
4046 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004047 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004048 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004049 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004050 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004051 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004052 int found_extent;
4053 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004054 int pending_del_nr = 0;
4055 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004056 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004057 int ret;
4058 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004059 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004060
4061 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004062
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004063 path = btrfs_alloc_path();
4064 if (!path)
4065 return -ENOMEM;
4066 path->reada = -1;
4067
Josef Bacik5dc562c2012-08-17 13:14:17 -04004068 /*
4069 * We want to drop from the next block forward in case this new size is
4070 * not block aligned since we will be keeping the last block of the
4071 * extent just the way it is.
4072 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004073 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004074 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4075 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004076
Miao Xie16cdcec2011-04-22 18:12:22 +08004077 /*
4078 * This function is also used to drop the items in the log tree before
4079 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4080 * it is used to drop the loged items. So we shouldn't kill the delayed
4081 * items.
4082 */
4083 if (min_type == 0 && root == BTRFS_I(inode)->root)
4084 btrfs_kill_delayed_inode_items(inode);
4085
Li Zefan33345d012011-04-20 10:31:50 +08004086 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004087 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004088 key.type = (u8)-1;
4089
Chris Mason85e21ba2008-01-29 15:11:36 -05004090search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004091 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004092 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004093 if (ret < 0) {
4094 err = ret;
4095 goto out;
4096 }
Chris Masond3977122009-01-05 21:25:51 -05004097
Chris Mason85e21ba2008-01-29 15:11:36 -05004098 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004099 /* there are no items in the tree for us to truncate, we're
4100 * done
4101 */
Yan, Zheng80825102009-11-12 09:35:36 +00004102 if (path->slots[0] == 0)
4103 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004104 path->slots[0]--;
4105 }
4106
Chris Masond3977122009-01-05 21:25:51 -05004107 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004108 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004109 leaf = path->nodes[0];
4110 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4111 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004112
Li Zefan33345d012011-04-20 10:31:50 +08004113 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004114 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004115
Chris Mason85e21ba2008-01-29 15:11:36 -05004116 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004117 break;
4118
Chris Mason5f39d392007-10-15 16:14:19 -04004119 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004120 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004121 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004122 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004123 extent_type = btrfs_file_extent_type(leaf, fi);
4124 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004125 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004126 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004127 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004128 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004129 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004130 }
Yan008630c2007-11-07 13:31:09 -05004131 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004132 }
Yan, Zheng80825102009-11-12 09:35:36 +00004133 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004134 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004135 } else {
4136 if (item_end < new_size)
4137 break;
4138 if (found_key.offset >= new_size)
4139 del_item = 1;
4140 else
4141 del_item = 0;
4142 }
Chris Mason39279cc2007-06-12 06:35:45 -04004143 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004144 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004145 if (found_type != BTRFS_EXTENT_DATA_KEY)
4146 goto delete;
4147
4148 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004149 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004150 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004151 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004152 u64 orig_num_bytes =
4153 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004154 extent_num_bytes = ALIGN(new_size -
4155 found_key.offset,
4156 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004157 btrfs_set_file_extent_num_bytes(leaf, fi,
4158 extent_num_bytes);
4159 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004160 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004161 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004162 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004163 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004164 } else {
Chris Masondb945352007-10-15 16:15:53 -04004165 extent_num_bytes =
4166 btrfs_file_extent_disk_num_bytes(leaf,
4167 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004168 extent_offset = found_key.offset -
4169 btrfs_file_extent_offset(leaf, fi);
4170
Chris Mason39279cc2007-06-12 06:35:45 -04004171 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004172 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004173 if (extent_start != 0) {
4174 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004175 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004176 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004177 }
4178 }
Chris Mason90692182008-02-08 13:49:28 -05004179 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004180 /*
4181 * we can't truncate inline items that have had
4182 * special encodings
4183 */
4184 if (!del_item &&
4185 btrfs_file_extent_compression(leaf, fi) == 0 &&
4186 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4187 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004188 u32 size = new_size - found_key.offset;
4189
4190 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004191 inode_sub_bytes(inode, item_end + 1 -
4192 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004193 }
4194 size =
4195 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004196 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004197 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004198 inode_sub_bytes(inode, item_end + 1 -
4199 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004200 }
Chris Mason39279cc2007-06-12 06:35:45 -04004201 }
Chris Mason179e29e2007-11-01 11:28:41 -04004202delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004203 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004204 if (!pending_del_nr) {
4205 /* no pending yet, add ourselves */
4206 pending_del_slot = path->slots[0];
4207 pending_del_nr = 1;
4208 } else if (pending_del_nr &&
4209 path->slots[0] + 1 == pending_del_slot) {
4210 /* hop on the pending chunk */
4211 pending_del_nr++;
4212 pending_del_slot = path->slots[0];
4213 } else {
Chris Masond3977122009-01-05 21:25:51 -05004214 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004215 }
Chris Mason39279cc2007-06-12 06:35:45 -04004216 } else {
4217 break;
4218 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004219 if (found_extent && (root->ref_cows ||
4220 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004221 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004222 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004223 extent_num_bytes, 0,
4224 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004225 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004226 BUG_ON(ret);
4227 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004228
Yan, Zheng80825102009-11-12 09:35:36 +00004229 if (found_type == BTRFS_INODE_ITEM_KEY)
4230 break;
4231
4232 if (path->slots[0] == 0 ||
4233 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004234 if (pending_del_nr) {
4235 ret = btrfs_del_items(trans, root, path,
4236 pending_del_slot,
4237 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004238 if (ret) {
4239 btrfs_abort_transaction(trans,
4240 root, ret);
4241 goto error;
4242 }
Yan, Zheng80825102009-11-12 09:35:36 +00004243 pending_del_nr = 0;
4244 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004245 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004246 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004247 } else {
4248 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004249 }
Chris Mason39279cc2007-06-12 06:35:45 -04004250 }
Yan, Zheng80825102009-11-12 09:35:36 +00004251out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004252 if (pending_del_nr) {
4253 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4254 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 if (ret)
4256 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004257 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004258error:
Chris Mason39279cc2007-06-12 06:35:45 -04004259 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004260 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004261}
4262
Chris Masona52d9a82007-08-27 16:49:44 -04004263/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004264 * btrfs_truncate_page - read, zero a chunk and write a page
4265 * @inode - inode that we're zeroing
4266 * @from - the offset to start zeroing
4267 * @len - the length to zero, 0 to zero the entire range respective to the
4268 * offset
4269 * @front - zero up to the offset instead of from the offset on
4270 *
4271 * This will find the page for the "from" offset and cow the page and zero the
4272 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004273 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004274int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4275 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004276{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004277 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004278 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004279 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4280 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004281 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004282 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004283 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004284 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4285 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4286 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004287 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004288 int ret = 0;
4289 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004290 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004291
Josef Bacik2aaa6652012-08-29 14:27:18 -04004292 if ((offset & (blocksize - 1)) == 0 &&
4293 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004294 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004295 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004296 if (ret)
4297 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004298
Chris Mason211c17f2008-05-15 09:13:45 -04004299again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004300 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004301 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004302 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004303 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004304 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004305 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004306
4307 page_start = page_offset(page);
4308 page_end = page_start + PAGE_CACHE_SIZE - 1;
4309
Chris Masona52d9a82007-08-27 16:49:44 -04004310 if (!PageUptodate(page)) {
4311 ret = btrfs_readpage(NULL, page);
4312 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004313 if (page->mapping != mapping) {
4314 unlock_page(page);
4315 page_cache_release(page);
4316 goto again;
4317 }
Chris Masona52d9a82007-08-27 16:49:44 -04004318 if (!PageUptodate(page)) {
4319 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004320 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004321 }
4322 }
Chris Mason211c17f2008-05-15 09:13:45 -04004323 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004324
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004325 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 set_page_extent_mapped(page);
4327
4328 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4329 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004330 unlock_extent_cached(io_tree, page_start, page_end,
4331 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004332 unlock_page(page);
4333 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004334 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004335 btrfs_put_ordered_extent(ordered);
4336 goto again;
4337 }
4338
Josef Bacik2ac55d42010-02-03 19:33:23 +00004339 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004340 EXTENT_DIRTY | EXTENT_DELALLOC |
4341 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004342 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004343
Josef Bacik2ac55d42010-02-03 19:33:23 +00004344 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4345 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004346 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004347 unlock_extent_cached(io_tree, page_start, page_end,
4348 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004349 goto out_unlock;
4350 }
4351
Chris Masone6dcd2d2008-07-17 12:53:50 -04004352 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004353 if (!len)
4354 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004355 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004356 if (front)
4357 memset(kaddr, 0, offset);
4358 else
4359 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004360 flush_dcache_page(page);
4361 kunmap(page);
4362 }
Chris Mason247e7432008-07-17 12:53:51 -04004363 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004364 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004365 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4366 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004367
Chris Mason89642222008-07-24 09:41:53 -04004368out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004369 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004370 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004371 unlock_page(page);
4372 page_cache_release(page);
4373out:
4374 return ret;
4375}
4376
Josef Bacik695a0d02011-03-04 15:46:53 -05004377/*
4378 * This function puts in dummy file extents for the area we're creating a hole
4379 * for. So if we are truncating this file to a larger size we need to insert
4380 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4381 * the range between oldsize and size
4382 */
Josef Bacika41ad392011-01-31 15:30:16 -05004383int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004384{
4385 struct btrfs_trans_handle *trans;
4386 struct btrfs_root *root = BTRFS_I(inode)->root;
4387 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004388 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004389 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004390 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004391 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4392 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004393 u64 last_byte;
4394 u64 cur_offset;
4395 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004396 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004397
4398 if (size <= hole_start)
4399 return 0;
4400
Yan Zheng9036c102008-10-30 14:19:41 -04004401 while (1) {
4402 struct btrfs_ordered_extent *ordered;
4403 btrfs_wait_ordered_range(inode, hole_start,
4404 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004405 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004406 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004407 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4408 if (!ordered)
4409 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004410 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4411 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004412 btrfs_put_ordered_extent(ordered);
4413 }
4414
Yan Zheng9036c102008-10-30 14:19:41 -04004415 cur_offset = hole_start;
4416 while (1) {
4417 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4418 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004419 if (IS_ERR(em)) {
4420 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004421 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004422 break;
4423 }
Yan Zheng9036c102008-10-30 14:19:41 -04004424 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004425 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004426 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004427 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004428 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004429
Miao Xie36423202011-12-14 20:12:02 -05004430 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004431 if (IS_ERR(trans)) {
4432 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004433 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004434 }
Yan, Zheng80825102009-11-12 09:35:36 +00004435
Josef Bacik5dc562c2012-08-17 13:14:17 -04004436 err = btrfs_drop_extents(trans, root, inode,
4437 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004438 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004439 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004440 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004441 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004442 break;
Miao Xie5b397372011-09-11 10:52:24 -04004443 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004444
Yan Zheng9036c102008-10-30 14:19:41 -04004445 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004446 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004447 0, hole_size, 0, hole_size,
4448 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004449 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004450 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004451 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004452 break;
Miao Xie5b397372011-09-11 10:52:24 -04004453 }
Yan, Zheng80825102009-11-12 09:35:36 +00004454
Josef Bacik5dc562c2012-08-17 13:14:17 -04004455 btrfs_drop_extent_cache(inode, cur_offset,
4456 cur_offset + hole_size - 1, 0);
4457 hole_em = alloc_extent_map();
4458 if (!hole_em) {
4459 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4460 &BTRFS_I(inode)->runtime_flags);
4461 goto next;
4462 }
4463 hole_em->start = cur_offset;
4464 hole_em->len = hole_size;
4465 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004466
Josef Bacik5dc562c2012-08-17 13:14:17 -04004467 hole_em->block_start = EXTENT_MAP_HOLE;
4468 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004469 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004470 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004471 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4472 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4473 hole_em->generation = trans->transid;
4474
4475 while (1) {
4476 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004477 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004478 write_unlock(&em_tree->lock);
4479 if (err != -EEXIST)
4480 break;
4481 btrfs_drop_extent_cache(inode, cur_offset,
4482 cur_offset +
4483 hole_size - 1, 0);
4484 }
4485 free_extent_map(hole_em);
4486next:
Miao Xie36423202011-12-14 20:12:02 -05004487 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004488 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004489 }
4490 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004491 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004492 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004493 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004494 break;
4495 }
4496
Yan, Zhenga22285a2010-05-16 10:48:46 -04004497 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004498 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4499 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004500 return err;
4501}
4502
Eric Sandeen3972f262013-01-12 02:57:22 +00004503static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004504{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004505 struct btrfs_root *root = BTRFS_I(inode)->root;
4506 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004507 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004508 loff_t newsize = attr->ia_size;
4509 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004510 int ret;
4511
Josef Bacika41ad392011-01-31 15:30:16 -05004512 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004513 return 0;
4514
Eric Sandeen3972f262013-01-12 02:57:22 +00004515 /*
4516 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4517 * special case where we need to update the times despite not having
4518 * these flags set. For all other operations the VFS set these flags
4519 * explicitly if it wants a timestamp update.
4520 */
4521 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4522 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4523
Josef Bacika41ad392011-01-31 15:30:16 -05004524 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004525 truncate_pagecache(inode, oldsize, newsize);
4526 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004527 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004528 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004529
Miao Xief4a2f4c2011-12-14 20:12:01 -05004530 trans = btrfs_start_transaction(root, 1);
4531 if (IS_ERR(trans))
4532 return PTR_ERR(trans);
4533
4534 i_size_write(inode, newsize);
4535 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4536 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004537 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004538 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004539
Josef Bacika41ad392011-01-31 15:30:16 -05004540 /*
4541 * We're truncating a file that used to have good data down to
4542 * zero. Make sure it gets into the ordered flush list so that
4543 * any new writes get down to disk quickly.
4544 */
4545 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004546 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4547 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004548
Josef Bacikf3fe8202013-01-07 17:03:21 -05004549 /*
4550 * 1 for the orphan item we're going to add
4551 * 1 for the orphan item deletion.
4552 */
4553 trans = btrfs_start_transaction(root, 2);
4554 if (IS_ERR(trans))
4555 return PTR_ERR(trans);
4556
4557 /*
4558 * We need to do this in case we fail at _any_ point during the
4559 * actual truncate. Once we do the truncate_setsize we could
4560 * invalidate pages which forces any outstanding ordered io to
4561 * be instantly completed which will give us extents that need
4562 * to be truncated. If we fail to get an orphan inode down we
4563 * could have left over extents that were never meant to live,
4564 * so we need to garuntee from this point on that everything
4565 * will be consistent.
4566 */
4567 ret = btrfs_orphan_add(trans, inode);
4568 btrfs_end_transaction(trans, root);
4569 if (ret)
4570 return ret;
4571
Josef Bacika41ad392011-01-31 15:30:16 -05004572 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4573 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004574
4575 /* Disable nonlocked read DIO to avoid the end less truncate */
4576 btrfs_inode_block_unlocked_dio(inode);
4577 inode_dio_wait(inode);
4578 btrfs_inode_resume_unlocked_dio(inode);
4579
Josef Bacika41ad392011-01-31 15:30:16 -05004580 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004581 if (ret && inode->i_nlink)
4582 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004583 }
4584
Josef Bacika41ad392011-01-31 15:30:16 -05004585 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004586}
4587
Chris Mason39279cc2007-06-12 06:35:45 -04004588static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4589{
4590 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004591 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004592 int err;
4593
Li Zefanb83cc962010-12-20 16:04:08 +08004594 if (btrfs_root_readonly(root))
4595 return -EROFS;
4596
Chris Mason39279cc2007-06-12 06:35:45 -04004597 err = inode_change_ok(inode, attr);
4598 if (err)
4599 return err;
4600
Chris Mason5a3f23d2009-03-31 13:27:11 -04004601 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004602 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004603 if (err)
4604 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004605 }
Yan Zheng9036c102008-10-30 14:19:41 -04004606
Christoph Hellwig10257742010-06-04 11:30:02 +02004607 if (attr->ia_valid) {
4608 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004609 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004610 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004611
Josef Bacik22c44fe2011-11-30 10:45:38 -05004612 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004613 err = btrfs_acl_chmod(inode);
4614 }
4615
Chris Mason39279cc2007-06-12 06:35:45 -04004616 return err;
4617}
Chris Mason61295eb2008-01-14 16:24:38 -05004618
Al Virobd555972010-06-07 11:35:40 -04004619void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004620{
4621 struct btrfs_trans_handle *trans;
4622 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004623 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004624 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004625 int ret;
4626
liubo1abe9b82011-03-24 11:18:59 +00004627 trace_btrfs_inode_evict(inode);
4628
Chris Mason39279cc2007-06-12 06:35:45 -04004629 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004630 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004631 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004632 goto no_delete;
4633
Chris Mason39279cc2007-06-12 06:35:45 -04004634 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004635 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004636 goto no_delete;
4637 }
Al Virobd555972010-06-07 11:35:40 -04004638 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004639 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004640
Yan, Zhengc71bf092009-11-12 09:34:40 +00004641 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004642 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004643 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004644 goto no_delete;
4645 }
4646
Yan, Zheng76dda932009-09-21 16:00:26 -04004647 if (inode->i_nlink > 0) {
4648 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4649 goto no_delete;
4650 }
4651
Miao Xie0e8c36a2012-12-19 06:59:51 +00004652 ret = btrfs_commit_inode_delayed_inode(inode);
4653 if (ret) {
4654 btrfs_orphan_del(NULL, inode);
4655 goto no_delete;
4656 }
4657
Miao Xie66d8f3d2012-09-06 04:02:28 -06004658 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004659 if (!rsv) {
4660 btrfs_orphan_del(NULL, inode);
4661 goto no_delete;
4662 }
Josef Bacik4a338542011-08-29 11:01:31 -04004663 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004664 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004665 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004666
Chris Masondbe674a2008-07-17 12:54:05 -04004667 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004668
Josef Bacik4289a662011-08-05 13:22:24 -04004669 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004670 * This is a bit simpler than btrfs_truncate since we've already
4671 * reserved our space for our orphan item in the unlink, so we just
4672 * need to reserve some slack space in case we add bytes and update
4673 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004674 */
Yan, Zheng80825102009-11-12 09:35:36 +00004675 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004676 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4677 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004678
Josef Bacik726c35f2011-09-26 15:46:06 -04004679 /*
4680 * Try and steal from the global reserve since we will
4681 * likely not use this space anyway, we want to try as
4682 * hard as possible to get this to work.
4683 */
4684 if (ret)
4685 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4686
Yan, Zhengd68fc572010-05-16 10:49:58 -04004687 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004688 btrfs_warn(root->fs_info,
4689 "Could not get space for a delete, will truncate on mount %d",
4690 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004691 btrfs_orphan_del(NULL, inode);
4692 btrfs_free_block_rsv(root, rsv);
4693 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004694 }
4695
Miao Xie0e8c36a2012-12-19 06:59:51 +00004696 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004697 if (IS_ERR(trans)) {
4698 btrfs_orphan_del(NULL, inode);
4699 btrfs_free_block_rsv(root, rsv);
4700 goto no_delete;
4701 }
4702
4703 trans->block_rsv = rsv;
4704
Yan, Zhengd68fc572010-05-16 10:49:58 -04004705 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004706 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004707 break;
4708
Miao Xie8407aa42012-09-07 01:43:32 -06004709 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004710 btrfs_end_transaction(trans, root);
4711 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004712 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004713 }
4714
Josef Bacik4289a662011-08-05 13:22:24 -04004715 btrfs_free_block_rsv(root, rsv);
4716
Yan, Zheng80825102009-11-12 09:35:36 +00004717 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004718 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004719 ret = btrfs_orphan_del(trans, inode);
4720 BUG_ON(ret);
4721 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004722
Josef Bacik4289a662011-08-05 13:22:24 -04004723 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004724 if (!(root == root->fs_info->tree_root ||
4725 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004726 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004727
Chris Mason54aa1f42007-06-22 14:16:25 -04004728 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004729 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004730no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004731 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004732 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004733 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004734}
4735
4736/*
4737 * this returns the key found in the dir entry in the location pointer.
4738 * If no dir entries were found, location->objectid is 0.
4739 */
4740static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4741 struct btrfs_key *location)
4742{
4743 const char *name = dentry->d_name.name;
4744 int namelen = dentry->d_name.len;
4745 struct btrfs_dir_item *di;
4746 struct btrfs_path *path;
4747 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004748 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004749
4750 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004751 if (!path)
4752 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004753
Li Zefan33345d012011-04-20 10:31:50 +08004754 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004755 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004756 if (IS_ERR(di))
4757 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004758
David Sterbac7040052011-04-19 18:00:01 +02004759 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004760 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004761
Chris Mason5f39d392007-10-15 16:14:19 -04004762 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004763out:
Chris Mason39279cc2007-06-12 06:35:45 -04004764 btrfs_free_path(path);
4765 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004766out_err:
4767 location->objectid = 0;
4768 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004769}
4770
4771/*
4772 * when we hit a tree root in a directory, the btrfs part of the inode
4773 * needs to be changed to reflect the root directory of the tree root. This
4774 * is kind of like crossing a mount point.
4775 */
4776static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004777 struct inode *dir,
4778 struct dentry *dentry,
4779 struct btrfs_key *location,
4780 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004781{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004782 struct btrfs_path *path;
4783 struct btrfs_root *new_root;
4784 struct btrfs_root_ref *ref;
4785 struct extent_buffer *leaf;
4786 int ret;
4787 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004789 path = btrfs_alloc_path();
4790 if (!path) {
4791 err = -ENOMEM;
4792 goto out;
4793 }
Chris Mason39279cc2007-06-12 06:35:45 -04004794
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004795 err = -ENOENT;
4796 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4797 BTRFS_I(dir)->root->root_key.objectid,
4798 location->objectid);
4799 if (ret) {
4800 if (ret < 0)
4801 err = ret;
4802 goto out;
4803 }
Chris Mason39279cc2007-06-12 06:35:45 -04004804
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004805 leaf = path->nodes[0];
4806 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004807 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004808 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4809 goto out;
4810
4811 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4812 (unsigned long)(ref + 1),
4813 dentry->d_name.len);
4814 if (ret)
4815 goto out;
4816
David Sterbab3b4aa72011-04-21 01:20:15 +02004817 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004818
4819 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4820 if (IS_ERR(new_root)) {
4821 err = PTR_ERR(new_root);
4822 goto out;
4823 }
4824
4825 if (btrfs_root_refs(&new_root->root_item) == 0) {
4826 err = -ENOENT;
4827 goto out;
4828 }
4829
4830 *sub_root = new_root;
4831 location->objectid = btrfs_root_dirid(&new_root->root_item);
4832 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004833 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004834 err = 0;
4835out:
4836 btrfs_free_path(path);
4837 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004838}
4839
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004840static void inode_tree_add(struct inode *inode)
4841{
4842 struct btrfs_root *root = BTRFS_I(inode)->root;
4843 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004844 struct rb_node **p;
4845 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004846 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004847
Al Viro1d3382cb2010-10-23 15:19:20 -04004848 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004849 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004850again:
4851 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004852 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004853 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004854 while (*p) {
4855 parent = *p;
4856 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4857
Li Zefan33345d012011-04-20 10:31:50 +08004858 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004859 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004860 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004861 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004862 else {
4863 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004864 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004865 rb_erase(parent, &root->inode_tree);
4866 RB_CLEAR_NODE(parent);
4867 spin_unlock(&root->inode_lock);
4868 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004869 }
4870 }
4871 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4872 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4873 spin_unlock(&root->inode_lock);
4874}
4875
4876static void inode_tree_del(struct inode *inode)
4877{
4878 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004879 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004880
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004881 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004882 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004883 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004884 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004885 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004886 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004887 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004888
Josef Bacik0af3d002010-06-21 14:48:16 -04004889 /*
4890 * Free space cache has inodes in the tree root, but the tree root has a
4891 * root_refs of 0, so this could end up dropping the tree root as a
4892 * snapshot, so we need the extra !root->fs_info->tree_root check to
4893 * make sure we don't drop it.
4894 */
4895 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4896 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004897 synchronize_srcu(&root->fs_info->subvol_srcu);
4898 spin_lock(&root->inode_lock);
4899 empty = RB_EMPTY_ROOT(&root->inode_tree);
4900 spin_unlock(&root->inode_lock);
4901 if (empty)
4902 btrfs_add_dead_root(root);
4903 }
4904}
4905
Jeff Mahoney143bede2012-03-01 14:56:26 +01004906void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004907{
4908 struct rb_node *node;
4909 struct rb_node *prev;
4910 struct btrfs_inode *entry;
4911 struct inode *inode;
4912 u64 objectid = 0;
4913
4914 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4915
4916 spin_lock(&root->inode_lock);
4917again:
4918 node = root->inode_tree.rb_node;
4919 prev = NULL;
4920 while (node) {
4921 prev = node;
4922 entry = rb_entry(node, struct btrfs_inode, rb_node);
4923
Li Zefan33345d012011-04-20 10:31:50 +08004924 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004925 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004926 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004927 node = node->rb_right;
4928 else
4929 break;
4930 }
4931 if (!node) {
4932 while (prev) {
4933 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004934 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004935 node = prev;
4936 break;
4937 }
4938 prev = rb_next(prev);
4939 }
4940 }
4941 while (node) {
4942 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004943 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004944 inode = igrab(&entry->vfs_inode);
4945 if (inode) {
4946 spin_unlock(&root->inode_lock);
4947 if (atomic_read(&inode->i_count) > 1)
4948 d_prune_aliases(inode);
4949 /*
Al Viro45321ac2010-06-07 13:43:19 -04004950 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004951 * the inode cache when its usage count
4952 * hits zero.
4953 */
4954 iput(inode);
4955 cond_resched();
4956 spin_lock(&root->inode_lock);
4957 goto again;
4958 }
4959
4960 if (cond_resched_lock(&root->inode_lock))
4961 goto again;
4962
4963 node = rb_next(node);
4964 }
4965 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004966}
4967
Chris Masone02119d2008-09-05 16:13:11 -04004968static int btrfs_init_locked_inode(struct inode *inode, void *p)
4969{
4970 struct btrfs_iget_args *args = p;
4971 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004972 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004973 return 0;
4974}
4975
4976static int btrfs_find_actor(struct inode *inode, void *opaque)
4977{
4978 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004979 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004980 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004981}
4982
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004983static struct inode *btrfs_iget_locked(struct super_block *s,
4984 u64 objectid,
4985 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004986{
4987 struct inode *inode;
4988 struct btrfs_iget_args args;
4989 args.ino = objectid;
4990 args.root = root;
4991
4992 inode = iget5_locked(s, objectid, btrfs_find_actor,
4993 btrfs_init_locked_inode,
4994 (void *)&args);
4995 return inode;
4996}
4997
Balaji Rao1a54ef82008-07-21 02:01:04 +05304998/* Get an inode object given its location and corresponding root.
4999 * Returns in *is_new if the inode was read from disk
5000 */
5001struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005002 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305003{
5004 struct inode *inode;
5005
5006 inode = btrfs_iget_locked(s, location->objectid, root);
5007 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005008 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305009
5010 if (inode->i_state & I_NEW) {
5011 BTRFS_I(inode)->root = root;
5012 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5013 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005014 if (!is_bad_inode(inode)) {
5015 inode_tree_add(inode);
5016 unlock_new_inode(inode);
5017 if (new)
5018 *new = 1;
5019 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005020 unlock_new_inode(inode);
5021 iput(inode);
5022 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005023 }
5024 }
5025
Balaji Rao1a54ef82008-07-21 02:01:04 +05305026 return inode;
5027}
5028
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005029static struct inode *new_simple_dir(struct super_block *s,
5030 struct btrfs_key *key,
5031 struct btrfs_root *root)
5032{
5033 struct inode *inode = new_inode(s);
5034
5035 if (!inode)
5036 return ERR_PTR(-ENOMEM);
5037
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005038 BTRFS_I(inode)->root = root;
5039 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005040 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005041
5042 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005043 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005044 inode->i_fop = &simple_dir_operations;
5045 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5046 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5047
5048 return inode;
5049}
5050
Chris Mason3de45862008-11-17 21:02:50 -05005051struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005052{
Chris Masond3977122009-01-05 21:25:51 -05005053 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005054 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005055 struct btrfs_root *sub_root = root;
5056 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005057 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005058 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005059
5060 if (dentry->d_name.len > BTRFS_NAME_LEN)
5061 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005062
Jeff Layton39e3c952012-11-28 11:30:53 -05005063 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005064 if (ret < 0)
5065 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005066
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005067 if (location.objectid == 0)
5068 return NULL;
5069
5070 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005071 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005072 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005073 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005074
5075 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5076
Yan, Zheng76dda932009-09-21 16:00:26 -04005077 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005078 ret = fixup_tree_root_location(root, dir, dentry,
5079 &location, &sub_root);
5080 if (ret < 0) {
5081 if (ret != -ENOENT)
5082 inode = ERR_PTR(ret);
5083 else
5084 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5085 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005086 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005087 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005088 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5089
Julia Lawall34d19ba2011-01-24 19:55:19 +00005090 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005091 down_read(&root->fs_info->cleanup_work_sem);
5092 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005093 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005094 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005095 if (ret)
5096 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005097 }
5098
Chris Mason3de45862008-11-17 21:02:50 -05005099 return inode;
5100}
5101
Nick Pigginfe15ce42011-01-07 17:49:23 +11005102static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005103{
5104 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005105 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005106
Li Zefan848cce02012-02-21 17:04:28 +08005107 if (!inode && !IS_ROOT(dentry))
5108 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005109
Li Zefan848cce02012-02-21 17:04:28 +08005110 if (inode) {
5111 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005112 if (btrfs_root_refs(&root->root_item) == 0)
5113 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005114
5115 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5116 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005117 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005118 return 0;
5119}
5120
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005121static void btrfs_dentry_release(struct dentry *dentry)
5122{
5123 if (dentry->d_fsdata)
5124 kfree(dentry->d_fsdata);
5125}
5126
Chris Mason3de45862008-11-17 21:02:50 -05005127static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005128 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005129{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005130 struct dentry *ret;
5131
5132 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005133 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005134}
5135
Miao Xie16cdcec2011-04-22 18:12:22 +08005136unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005137 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5138};
5139
David Woodhousecbdf5a22008-08-06 19:42:33 +01005140static int btrfs_real_readdir(struct file *filp, void *dirent,
5141 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005142{
Al Viro496ad9a2013-01-23 17:07:38 -05005143 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005144 struct btrfs_root *root = BTRFS_I(inode)->root;
5145 struct btrfs_item *item;
5146 struct btrfs_dir_item *di;
5147 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005148 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005149 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005150 struct list_head ins_list;
5151 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005152 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005153 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005154 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005155 unsigned char d_type;
5156 int over = 0;
5157 u32 di_cur;
5158 u32 di_total;
5159 u32 di_len;
5160 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005161 char tmp_name[32];
5162 char *name_ptr;
5163 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005164 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005165
5166 /* FIXME, use a real flag for deciding about the key type */
5167 if (root->fs_info->tree_root == root)
5168 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005169
Chris Mason39544012007-12-12 14:38:19 -05005170 /* special case for "." */
5171 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005172 over = filldir(dirent, ".", 1,
5173 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005174 if (over)
5175 return 0;
5176 filp->f_pos = 1;
5177 }
Chris Mason39544012007-12-12 14:38:19 -05005178 /* special case for .., just use the back ref */
5179 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005180 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005181 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005182 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005183 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005184 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005185 filp->f_pos = 2;
5186 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005187 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005188 if (!path)
5189 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005190
Josef Bacik026fd312011-05-13 10:32:11 -04005191 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005192
Miao Xie16cdcec2011-04-22 18:12:22 +08005193 if (key_type == BTRFS_DIR_INDEX_KEY) {
5194 INIT_LIST_HEAD(&ins_list);
5195 INIT_LIST_HEAD(&del_list);
5196 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5197 }
5198
Chris Mason39279cc2007-06-12 06:35:45 -04005199 btrfs_set_key_type(&key, key_type);
5200 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005201 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005202
Chris Mason39279cc2007-06-12 06:35:45 -04005203 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5204 if (ret < 0)
5205 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005206
5207 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005208 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005209 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005210 if (slot >= btrfs_header_nritems(leaf)) {
5211 ret = btrfs_next_leaf(root, path);
5212 if (ret < 0)
5213 goto err;
5214 else if (ret > 0)
5215 break;
5216 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005217 }
Chris Mason3de45862008-11-17 21:02:50 -05005218
Chris Mason5f39d392007-10-15 16:14:19 -04005219 item = btrfs_item_nr(leaf, slot);
5220 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5221
5222 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005223 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005224 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005225 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005226 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005227 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005228 if (key_type == BTRFS_DIR_INDEX_KEY &&
5229 btrfs_should_delete_dir_index(&del_list,
5230 found_key.offset))
5231 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005232
5233 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005234 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005235
Chris Mason39279cc2007-06-12 06:35:45 -04005236 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5237 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005238 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005239
5240 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005241 struct btrfs_key location;
5242
Josef Bacik22a94d42011-03-16 16:47:17 -04005243 if (verify_dir_item(root, leaf, di))
5244 break;
5245
Chris Mason5f39d392007-10-15 16:14:19 -04005246 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005247 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005248 name_ptr = tmp_name;
5249 } else {
5250 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005251 if (!name_ptr) {
5252 ret = -ENOMEM;
5253 goto err;
5254 }
Chris Mason5f39d392007-10-15 16:14:19 -04005255 }
5256 read_extent_buffer(leaf, name_ptr,
5257 (unsigned long)(di + 1), name_len);
5258
5259 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5260 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005261
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005262
Chris Mason3de45862008-11-17 21:02:50 -05005263 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005264 * skip it.
5265 *
5266 * In contrast to old kernels, we insert the snapshot's
5267 * dir item and dir index after it has been created, so
5268 * we won't find a reference to our own snapshot. We
5269 * still keep the following code for backward
5270 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005271 */
5272 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5273 location.objectid == root->root_key.objectid) {
5274 over = 0;
5275 goto skip;
5276 }
Chris Mason5f39d392007-10-15 16:14:19 -04005277 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005278 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005279 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005280
Chris Mason3de45862008-11-17 21:02:50 -05005281skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005282 if (name_ptr != tmp_name)
5283 kfree(name_ptr);
5284
Chris Mason39279cc2007-06-12 06:35:45 -04005285 if (over)
5286 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005287 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005288 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005289 di_cur += di_len;
5290 di = (struct btrfs_dir_item *)((char *)di + di_len);
5291 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005292next:
5293 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005294 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005295
Miao Xie16cdcec2011-04-22 18:12:22 +08005296 if (key_type == BTRFS_DIR_INDEX_KEY) {
5297 if (is_curr)
5298 filp->f_pos++;
5299 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5300 &ins_list);
5301 if (ret)
5302 goto nopos;
5303 }
5304
David Woodhouse49593bf2008-08-17 17:08:36 +01005305 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005306 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005307 /*
5308 * 32-bit glibc will use getdents64, but then strtol -
5309 * so the last number we can serve is this.
5310 */
5311 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005312 else
5313 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005314nopos:
5315 ret = 0;
5316err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005317 if (key_type == BTRFS_DIR_INDEX_KEY)
5318 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005319 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005320 return ret;
5321}
5322
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005323int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005324{
5325 struct btrfs_root *root = BTRFS_I(inode)->root;
5326 struct btrfs_trans_handle *trans;
5327 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005328 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005329
Josef Bacik72ac3c02012-05-23 14:13:11 -04005330 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005331 return 0;
5332
Liu Bo83eea1f2012-07-10 05:28:39 -06005333 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005334 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005335
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005336 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005337 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005338 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005339 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005340 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005341 if (IS_ERR(trans))
5342 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005343 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005344 }
5345 return ret;
5346}
5347
5348/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005349 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005350 * inode changes. But, it is most likely to find the inode in cache.
5351 * FIXME, needs more benchmarking...there are no reasons other than performance
5352 * to keep or drop this code.
5353 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005354static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005355{
5356 struct btrfs_root *root = BTRFS_I(inode)->root;
5357 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005358 int ret;
5359
Josef Bacik72ac3c02012-05-23 14:13:11 -04005360 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005361 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005362
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005363 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005364 if (IS_ERR(trans))
5365 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005366
5367 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005368 if (ret && ret == -ENOSPC) {
5369 /* whoops, lets try again with the full transaction */
5370 btrfs_end_transaction(trans, root);
5371 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005372 if (IS_ERR(trans))
5373 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005374
Chris Mason94b60442010-05-26 11:02:00 -04005375 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005376 }
Chris Mason39279cc2007-06-12 06:35:45 -04005377 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005378 if (BTRFS_I(inode)->delayed_node)
5379 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005380
5381 return ret;
5382}
5383
5384/*
5385 * This is a copy of file_update_time. We need this so we can return error on
5386 * ENOSPC for updating the inode in the case of file write and mmap writes.
5387 */
Josef Bacike41f9412012-03-26 09:46:47 -04005388static int btrfs_update_time(struct inode *inode, struct timespec *now,
5389 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005390{
Alexander Block2bc5565282012-06-15 09:49:33 +02005391 struct btrfs_root *root = BTRFS_I(inode)->root;
5392
5393 if (btrfs_root_readonly(root))
5394 return -EROFS;
5395
Josef Bacike41f9412012-03-26 09:46:47 -04005396 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005397 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005398 if (flags & S_CTIME)
5399 inode->i_ctime = *now;
5400 if (flags & S_MTIME)
5401 inode->i_mtime = *now;
5402 if (flags & S_ATIME)
5403 inode->i_atime = *now;
5404 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005405}
5406
Chris Masond352ac62008-09-29 15:18:18 -04005407/*
5408 * find the highest existing sequence number in a directory
5409 * and then set the in-memory index_cnt variable to reflect
5410 * free sequence numbers
5411 */
Josef Bacikaec74772008-07-24 12:12:38 -04005412static int btrfs_set_inode_index_count(struct inode *inode)
5413{
5414 struct btrfs_root *root = BTRFS_I(inode)->root;
5415 struct btrfs_key key, found_key;
5416 struct btrfs_path *path;
5417 struct extent_buffer *leaf;
5418 int ret;
5419
Li Zefan33345d012011-04-20 10:31:50 +08005420 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005421 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5422 key.offset = (u64)-1;
5423
5424 path = btrfs_alloc_path();
5425 if (!path)
5426 return -ENOMEM;
5427
5428 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5429 if (ret < 0)
5430 goto out;
5431 /* FIXME: we should be able to handle this */
5432 if (ret == 0)
5433 goto out;
5434 ret = 0;
5435
5436 /*
5437 * MAGIC NUMBER EXPLANATION:
5438 * since we search a directory based on f_pos we have to start at 2
5439 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5440 * else has to start at 2
5441 */
5442 if (path->slots[0] == 0) {
5443 BTRFS_I(inode)->index_cnt = 2;
5444 goto out;
5445 }
5446
5447 path->slots[0]--;
5448
5449 leaf = path->nodes[0];
5450 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5451
Li Zefan33345d012011-04-20 10:31:50 +08005452 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005453 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5454 BTRFS_I(inode)->index_cnt = 2;
5455 goto out;
5456 }
5457
5458 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5459out:
5460 btrfs_free_path(path);
5461 return ret;
5462}
5463
Chris Masond352ac62008-09-29 15:18:18 -04005464/*
5465 * helper to find a free sequence number in a given directory. This current
5466 * code is very simple, later versions will do smarter things in the btree
5467 */
Chris Mason3de45862008-11-17 21:02:50 -05005468int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005469{
5470 int ret = 0;
5471
5472 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005473 ret = btrfs_inode_delayed_dir_index_count(dir);
5474 if (ret) {
5475 ret = btrfs_set_inode_index_count(dir);
5476 if (ret)
5477 return ret;
5478 }
Josef Bacikaec74772008-07-24 12:12:38 -04005479 }
5480
Chris Mason00e4e6b2008-08-05 11:18:09 -04005481 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005482 BTRFS_I(dir)->index_cnt++;
5483
5484 return ret;
5485}
5486
Chris Mason39279cc2007-06-12 06:35:45 -04005487static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5488 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005489 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005490 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005491 u64 ref_objectid, u64 objectid,
5492 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005493{
5494 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005495 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005496 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005497 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005498 struct btrfs_inode_ref *ref;
5499 struct btrfs_key key[2];
5500 u32 sizes[2];
5501 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005502 int ret;
5503 int owner;
5504
Chris Mason5f39d392007-10-15 16:14:19 -04005505 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005506 if (!path)
5507 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005508
Chris Mason39279cc2007-06-12 06:35:45 -04005509 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005510 if (!inode) {
5511 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005512 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005513 }
Chris Mason39279cc2007-06-12 06:35:45 -04005514
Li Zefan581bb052011-04-20 10:06:11 +08005515 /*
5516 * we have to initialize this early, so we can reclaim the inode
5517 * number if we fail afterwards in this function.
5518 */
5519 inode->i_ino = objectid;
5520
Josef Bacikaec74772008-07-24 12:12:38 -04005521 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005522 trace_btrfs_inode_request(dir);
5523
Chris Mason3de45862008-11-17 21:02:50 -05005524 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005525 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005526 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005527 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005528 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005529 }
Josef Bacikaec74772008-07-24 12:12:38 -04005530 }
5531 /*
5532 * index_cnt is ignored for everything but a dir,
5533 * btrfs_get_inode_index_count has an explanation for the magic
5534 * number
5535 */
5536 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005537 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005538 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005539 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005540
Josef Bacik5dc562c2012-08-17 13:14:17 -04005541 /*
5542 * We could have gotten an inode number from somebody who was fsynced
5543 * and then removed in this same transaction, so let's just set full
5544 * sync since it will be a full sync anyway and this will blow away the
5545 * old info in the log.
5546 */
5547 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5548
Al Viro569254b2011-07-24 17:08:40 -04005549 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005550 owner = 0;
5551 else
5552 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005553
5554 key[0].objectid = objectid;
5555 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5556 key[0].offset = 0;
5557
Mark Fashehf1863732012-08-08 11:32:27 -07005558 /*
5559 * Start new inodes with an inode_ref. This is slightly more
5560 * efficient for small numbers of hard links since they will
5561 * be packed into one item. Extended refs will kick in if we
5562 * add more hard links than can fit in the ref item.
5563 */
Chris Mason9c583092008-01-29 15:15:18 -05005564 key[1].objectid = objectid;
5565 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5566 key[1].offset = ref_objectid;
5567
5568 sizes[0] = sizeof(struct btrfs_inode_item);
5569 sizes[1] = name_len + sizeof(*ref);
5570
Chris Masonb9473432009-03-13 11:00:37 -04005571 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005572 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5573 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005574 goto fail;
5575
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005576 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005577 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005578 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005579 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5580 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005581 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5582 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005583 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005584
5585 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5586 struct btrfs_inode_ref);
5587 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005588 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005589 ptr = (unsigned long)(ref + 1);
5590 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5591
Chris Mason5f39d392007-10-15 16:14:19 -04005592 btrfs_mark_buffer_dirty(path->nodes[0]);
5593 btrfs_free_path(path);
5594
Chris Mason39279cc2007-06-12 06:35:45 -04005595 location = &BTRFS_I(inode)->location;
5596 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005597 location->offset = 0;
5598 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5599
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005600 btrfs_inherit_iflags(inode, dir);
5601
Al Viro569254b2011-07-24 17:08:40 -04005602 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005603 if (btrfs_test_opt(root, NODATASUM))
5604 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005605 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005606 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5607 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005608 }
5609
Chris Mason39279cc2007-06-12 06:35:45 -04005610 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005611 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005612
5613 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005614 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005615
Alexander Block8ea05e32012-07-25 17:35:53 +02005616 btrfs_update_root_times(trans, root);
5617
Chris Mason39279cc2007-06-12 06:35:45 -04005618 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005619fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005620 if (dir)
5621 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005622 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005623 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005624 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005625}
5626
5627static inline u8 btrfs_inode_type(struct inode *inode)
5628{
5629 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5630}
5631
Chris Masond352ac62008-09-29 15:18:18 -04005632/*
5633 * utility function to add 'inode' into 'parent_inode' with
5634 * a give name and a given sequence number.
5635 * if 'add_backref' is true, also insert a backref from the
5636 * inode to the parent directory.
5637 */
Chris Masone02119d2008-09-05 16:13:11 -04005638int btrfs_add_link(struct btrfs_trans_handle *trans,
5639 struct inode *parent_inode, struct inode *inode,
5640 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005641{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005642 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005643 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005644 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005645 u64 ino = btrfs_ino(inode);
5646 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005647
Li Zefan33345d012011-04-20 10:31:50 +08005648 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005649 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5650 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005651 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5653 key.offset = 0;
5654 }
Chris Mason39279cc2007-06-12 06:35:45 -04005655
Li Zefan33345d012011-04-20 10:31:50 +08005656 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5658 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005659 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005660 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005661 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5662 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005663 }
5664
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005665 /* Nothing to clean up yet */
5666 if (ret)
5667 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005668
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005669 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5670 parent_inode, &key,
5671 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005672 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005673 goto fail_dir_item;
5674 else if (ret) {
5675 btrfs_abort_transaction(trans, root, ret);
5676 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005677 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005678
5679 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5680 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005681 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005682 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5683 ret = btrfs_update_inode(trans, root, parent_inode);
5684 if (ret)
5685 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005686 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005687
5688fail_dir_item:
5689 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5690 u64 local_index;
5691 int err;
5692 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5693 key.objectid, root->root_key.objectid,
5694 parent_ino, &local_index, name, name_len);
5695
5696 } else if (add_backref) {
5697 u64 local_index;
5698 int err;
5699
5700 err = btrfs_del_inode_ref(trans, root, name, name_len,
5701 ino, parent_ino, &local_index);
5702 }
5703 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005704}
5705
5706static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005707 struct inode *dir, struct dentry *dentry,
5708 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005709{
Josef Bacika1b075d2010-11-19 20:36:11 +00005710 int err = btrfs_add_link(trans, dir, inode,
5711 dentry->d_name.name, dentry->d_name.len,
5712 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005713 if (err > 0)
5714 err = -EEXIST;
5715 return err;
5716}
5717
Josef Bacik618e21d2007-07-11 10:18:17 -04005718static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005719 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005720{
5721 struct btrfs_trans_handle *trans;
5722 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005723 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005724 int err;
5725 int drop_inode = 0;
5726 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005727 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005728
5729 if (!new_valid_dev(rdev))
5730 return -EINVAL;
5731
Josef Bacik9ed74f22009-09-11 16:12:44 -04005732 /*
5733 * 2 for inode item and ref
5734 * 2 for dir items
5735 * 1 for xattr if selinux is on
5736 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005737 trans = btrfs_start_transaction(root, 5);
5738 if (IS_ERR(trans))
5739 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005740
Li Zefan581bb052011-04-20 10:06:11 +08005741 err = btrfs_find_free_ino(root, &objectid);
5742 if (err)
5743 goto out_unlock;
5744
Josef Bacikaec74772008-07-24 12:12:38 -04005745 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005746 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005747 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005748 if (IS_ERR(inode)) {
5749 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005750 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005751 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005752
Eric Paris2a7dba32011-02-01 11:05:39 -05005753 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005754 if (err) {
5755 drop_inode = 1;
5756 goto out_unlock;
5757 }
5758
Casey Schauflerad19db72011-12-15 10:09:07 -05005759 /*
5760 * If the active LSM wants to access the inode during
5761 * d_instantiate it needs these. Smack checks to see
5762 * if the filesystem supports xattrs by looking at the
5763 * ops vector.
5764 */
5765
5766 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005767 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005768 if (err)
5769 drop_inode = 1;
5770 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005771 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005772 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005773 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005774 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005775out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005776 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005777 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005778 if (drop_inode) {
5779 inode_dec_link_count(inode);
5780 iput(inode);
5781 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005782 return err;
5783}
5784
Chris Mason39279cc2007-06-12 06:35:45 -04005785static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005786 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005787{
5788 struct btrfs_trans_handle *trans;
5789 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005790 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005791 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005792 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005793 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005794 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005795
Josef Bacik9ed74f22009-09-11 16:12:44 -04005796 /*
5797 * 2 for inode item and ref
5798 * 2 for dir items
5799 * 1 for xattr if selinux is on
5800 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005801 trans = btrfs_start_transaction(root, 5);
5802 if (IS_ERR(trans))
5803 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005804
Li Zefan581bb052011-04-20 10:06:11 +08005805 err = btrfs_find_free_ino(root, &objectid);
5806 if (err)
5807 goto out_unlock;
5808
Josef Bacikaec74772008-07-24 12:12:38 -04005809 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005810 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005811 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005812 if (IS_ERR(inode)) {
5813 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005814 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005815 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005816 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005817
Eric Paris2a7dba32011-02-01 11:05:39 -05005818 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005819 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005820 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005821
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005822 err = btrfs_update_inode(trans, root, inode);
5823 if (err)
5824 goto out_unlock;
5825
Casey Schauflerad19db72011-12-15 10:09:07 -05005826 /*
5827 * If the active LSM wants to access the inode during
5828 * d_instantiate it needs these. Smack checks to see
5829 * if the filesystem supports xattrs by looking at the
5830 * ops vector.
5831 */
5832 inode->i_fop = &btrfs_file_operations;
5833 inode->i_op = &btrfs_file_inode_operations;
5834
Josef Bacika1b075d2010-11-19 20:36:11 +00005835 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005836 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005837 goto out_unlock;
5838
5839 inode->i_mapping->a_ops = &btrfs_aops;
5840 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5841 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5842 d_instantiate(dentry, inode);
5843
Chris Mason39279cc2007-06-12 06:35:45 -04005844out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005845 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005846 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005847 inode_dec_link_count(inode);
5848 iput(inode);
5849 }
Liu Bob53d3f52012-11-14 14:34:34 +00005850 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005851 return err;
5852}
5853
5854static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5855 struct dentry *dentry)
5856{
5857 struct btrfs_trans_handle *trans;
5858 struct btrfs_root *root = BTRFS_I(dir)->root;
5859 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005860 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005861 int err;
5862 int drop_inode = 0;
5863
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005864 /* do not allow sys_link's with other subvols of the same device */
5865 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005866 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005867
Mark Fashehf1863732012-08-08 11:32:27 -07005868 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005869 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005870
Chris Mason3de45862008-11-17 21:02:50 -05005871 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005872 if (err)
5873 goto fail;
5874
Yan, Zhenga22285a2010-05-16 10:48:46 -04005875 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005876 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005877 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005878 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005879 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005880 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005881 if (IS_ERR(trans)) {
5882 err = PTR_ERR(trans);
5883 goto fail;
5884 }
Chris Mason5f39d392007-10-15 16:14:19 -04005885
Miao Xie31534952011-04-13 13:19:21 +08005886 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005887 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005888 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005889 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005890 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005891
Josef Bacika1b075d2010-11-19 20:36:11 +00005892 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005893
Yan, Zhenga5719522009-09-24 09:17:31 -04005894 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005895 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005896 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005897 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005898 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005899 if (err)
5900 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005901 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005902 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005903 }
Chris Mason39279cc2007-06-12 06:35:45 -04005904
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005905 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005906fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005907 if (drop_inode) {
5908 inode_dec_link_count(inode);
5909 iput(inode);
5910 }
Liu Bob53d3f52012-11-14 14:34:34 +00005911 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005912 return err;
5913}
5914
Al Viro18bb1db2011-07-26 01:41:39 -04005915static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005916{
Chris Masonb9d86662008-05-02 16:13:49 -04005917 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005918 struct btrfs_trans_handle *trans;
5919 struct btrfs_root *root = BTRFS_I(dir)->root;
5920 int err = 0;
5921 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005922 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005923 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005924
Josef Bacik9ed74f22009-09-11 16:12:44 -04005925 /*
5926 * 2 items for inode and ref
5927 * 2 items for dir items
5928 * 1 for xattr if selinux is on
5929 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005930 trans = btrfs_start_transaction(root, 5);
5931 if (IS_ERR(trans))
5932 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005933
Li Zefan581bb052011-04-20 10:06:11 +08005934 err = btrfs_find_free_ino(root, &objectid);
5935 if (err)
5936 goto out_fail;
5937
Josef Bacikaec74772008-07-24 12:12:38 -04005938 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005939 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005940 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005941 if (IS_ERR(inode)) {
5942 err = PTR_ERR(inode);
5943 goto out_fail;
5944 }
Chris Mason5f39d392007-10-15 16:14:19 -04005945
Chris Mason39279cc2007-06-12 06:35:45 -04005946 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005947
Eric Paris2a7dba32011-02-01 11:05:39 -05005948 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005949 if (err)
5950 goto out_fail;
5951
Chris Mason39279cc2007-06-12 06:35:45 -04005952 inode->i_op = &btrfs_dir_inode_operations;
5953 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005954
Chris Masondbe674a2008-07-17 12:54:05 -04005955 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005956 err = btrfs_update_inode(trans, root, inode);
5957 if (err)
5958 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005959
Josef Bacika1b075d2010-11-19 20:36:11 +00005960 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5961 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005962 if (err)
5963 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005964
Chris Mason39279cc2007-06-12 06:35:45 -04005965 d_instantiate(dentry, inode);
5966 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005967
5968out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005969 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005970 if (drop_on_err)
5971 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005972 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005973 return err;
5974}
5975
Chris Masond352ac62008-09-29 15:18:18 -04005976/* helper for btfs_get_extent. Given an existing extent in the tree,
5977 * and an extent that you want to insert, deal with overlap and insert
5978 * the new extent into the tree.
5979 */
Chris Mason3b951512008-04-17 11:29:12 -04005980static int merge_extent_mapping(struct extent_map_tree *em_tree,
5981 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005982 struct extent_map *em,
5983 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005984{
5985 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005986
Chris Masone6dcd2d2008-07-17 12:53:50 -04005987 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5988 start_diff = map_start - em->start;
5989 em->start = map_start;
5990 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005991 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5992 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005993 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005994 em->block_len -= start_diff;
5995 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005996 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005997}
5998
Chris Masonc8b97812008-10-29 14:49:59 -04005999static noinline int uncompress_inline(struct btrfs_path *path,
6000 struct inode *inode, struct page *page,
6001 size_t pg_offset, u64 extent_offset,
6002 struct btrfs_file_extent_item *item)
6003{
6004 int ret;
6005 struct extent_buffer *leaf = path->nodes[0];
6006 char *tmp;
6007 size_t max_size;
6008 unsigned long inline_size;
6009 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006010 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006011
6012 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006013 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006014 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6015 inline_size = btrfs_file_extent_inline_item_len(leaf,
6016 btrfs_item_nr(leaf, path->slots[0]));
6017 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006018 if (!tmp)
6019 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006020 ptr = btrfs_file_extent_inline_start(item);
6021
6022 read_extent_buffer(leaf, tmp, ptr, inline_size);
6023
Chris Mason5b050f02008-11-11 09:34:41 -05006024 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006025 ret = btrfs_decompress(compress_type, tmp, page,
6026 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006027 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006028 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006029 unsigned long copy_size = min_t(u64,
6030 PAGE_CACHE_SIZE - pg_offset,
6031 max_size - extent_offset);
6032 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006033 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006034 }
6035 kfree(tmp);
6036 return 0;
6037}
6038
Chris Masond352ac62008-09-29 15:18:18 -04006039/*
6040 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006041 * the ugly parts come from merging extents from the disk with the in-ram
6042 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006043 * where the in-ram extents might be locked pending data=ordered completion.
6044 *
6045 * This also copies inline extents directly into the page.
6046 */
Chris Masond3977122009-01-05 21:25:51 -05006047
Chris Masona52d9a82007-08-27 16:49:44 -04006048struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006049 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006050 int create)
6051{
6052 int ret;
6053 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006054 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006055 u64 extent_start = 0;
6056 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006057 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006058 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006059 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006060 struct btrfs_root *root = BTRFS_I(inode)->root;
6061 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006062 struct extent_buffer *leaf;
6063 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006064 struct extent_map *em = NULL;
6065 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006066 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006067 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006068 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006069
Chris Masona52d9a82007-08-27 16:49:44 -04006070again:
Chris Mason890871b2009-09-02 16:24:52 -04006071 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006072 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006073 if (em)
6074 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006075 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006076
Chris Masona52d9a82007-08-27 16:49:44 -04006077 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006078 if (em->start > start || em->start + em->len <= start)
6079 free_extent_map(em);
6080 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006081 free_extent_map(em);
6082 else
6083 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006084 }
David Sterba172ddd62011-04-21 00:48:27 +02006085 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006086 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006087 err = -ENOMEM;
6088 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006089 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006090 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006091 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006092 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006093 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006094 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006095
6096 if (!path) {
6097 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006098 if (!path) {
6099 err = -ENOMEM;
6100 goto out;
6101 }
6102 /*
6103 * Chances are we'll be called again, so go ahead and do
6104 * readahead
6105 */
6106 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006107 }
6108
Chris Mason179e29e2007-11-01 11:28:41 -04006109 ret = btrfs_lookup_file_extent(trans, root, path,
6110 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006111 if (ret < 0) {
6112 err = ret;
6113 goto out;
6114 }
6115
6116 if (ret != 0) {
6117 if (path->slots[0] == 0)
6118 goto not_found;
6119 path->slots[0]--;
6120 }
6121
Chris Mason5f39d392007-10-15 16:14:19 -04006122 leaf = path->nodes[0];
6123 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006124 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006125 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006126 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6127 found_type = btrfs_key_type(&found_key);
6128 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006129 found_type != BTRFS_EXTENT_DATA_KEY) {
6130 goto not_found;
6131 }
6132
Chris Mason5f39d392007-10-15 16:14:19 -04006133 found_type = btrfs_file_extent_type(leaf, item);
6134 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006135 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006136 if (found_type == BTRFS_FILE_EXTENT_REG ||
6137 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006138 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006139 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006140 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6141 size_t size;
6142 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006143 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006144 }
6145
6146 if (start >= extent_end) {
6147 path->slots[0]++;
6148 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6149 ret = btrfs_next_leaf(root, path);
6150 if (ret < 0) {
6151 err = ret;
6152 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006153 }
Yan Zheng9036c102008-10-30 14:19:41 -04006154 if (ret > 0)
6155 goto not_found;
6156 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006157 }
Yan Zheng9036c102008-10-30 14:19:41 -04006158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6159 if (found_key.objectid != objectid ||
6160 found_key.type != BTRFS_EXTENT_DATA_KEY)
6161 goto not_found;
6162 if (start + len <= found_key.offset)
6163 goto not_found;
6164 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006165 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006166 em->len = found_key.offset - start;
6167 goto not_found_em;
6168 }
6169
Josef Bacikcc95bef2013-04-04 14:31:27 -04006170 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006171 if (found_type == BTRFS_FILE_EXTENT_REG ||
6172 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006173 em->start = extent_start;
6174 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006175 em->orig_start = extent_start -
6176 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006177 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6178 item);
Chris Masondb945352007-10-15 16:15:53 -04006179 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6180 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006181 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006182 goto insert;
6183 }
Li Zefan261507a02010-12-17 14:21:50 +08006184 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006185 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006186 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006187 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006188 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006189 } else {
6190 bytenr += btrfs_file_extent_offset(leaf, item);
6191 em->block_start = bytenr;
6192 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006193 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6194 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006195 }
Chris Masona52d9a82007-08-27 16:49:44 -04006196 goto insert;
6197 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006198 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006199 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006200 size_t size;
6201 size_t extent_offset;
6202 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006203
Chris Masona52d9a82007-08-27 16:49:44 -04006204 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006205 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006206 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006207 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006208 goto out;
6209 }
6210
Yan Zheng9036c102008-10-30 14:19:41 -04006211 size = btrfs_file_extent_inline_len(leaf, item);
6212 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006213 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006214 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006215 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006216 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006217 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006218 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006219 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006220 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006221 em->compress_type = compress_type;
6222 }
Yan689f9342007-10-29 11:41:07 -04006223 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006224 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006225 if (btrfs_file_extent_compression(leaf, item) !=
6226 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006227 ret = uncompress_inline(path, inode, page,
6228 pg_offset,
6229 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006230 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006231 } else {
6232 map = kmap(page);
6233 read_extent_buffer(leaf, map + pg_offset, ptr,
6234 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006235 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6236 memset(map + pg_offset + copy_size, 0,
6237 PAGE_CACHE_SIZE - pg_offset -
6238 copy_size);
6239 }
Chris Masonc8b97812008-10-29 14:49:59 -04006240 kunmap(page);
6241 }
Chris Mason179e29e2007-11-01 11:28:41 -04006242 flush_dcache_page(page);
6243 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006244 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006245 if (!trans) {
6246 kunmap(page);
6247 free_extent_map(em);
6248 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006249
David Sterbab3b4aa72011-04-21 01:20:15 +02006250 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006251 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006252
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006253 if (IS_ERR(trans))
6254 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006255 goto again;
6256 }
Chris Masonc8b97812008-10-29 14:49:59 -04006257 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006258 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006259 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006260 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006261 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006262 }
Chris Masond1310b22008-01-24 16:13:08 -05006263 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006264 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006265 goto insert;
6266 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006267 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006268 }
6269not_found:
6270 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006271 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006272 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006273not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006274 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006275 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006276insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006277 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006278 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006279 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6280 (unsigned long long)em->start,
6281 (unsigned long long)em->len,
6282 (unsigned long long)start,
6283 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006284 err = -EIO;
6285 goto out;
6286 }
Chris Masond1310b22008-01-24 16:13:08 -05006287
6288 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006289 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006290 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006291 /* it is possible that someone inserted the extent into the tree
6292 * while we had the lock dropped. It is also possible that
6293 * an overlapping map exists in the tree
6294 */
Chris Masona52d9a82007-08-27 16:49:44 -04006295 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006296 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006297
6298 ret = 0;
6299
Chris Mason3b951512008-04-17 11:29:12 -04006300 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006301 if (existing && (existing->start > start ||
6302 existing->start + existing->len <= start)) {
6303 free_extent_map(existing);
6304 existing = NULL;
6305 }
Chris Mason3b951512008-04-17 11:29:12 -04006306 if (!existing) {
6307 existing = lookup_extent_mapping(em_tree, em->start,
6308 em->len);
6309 if (existing) {
6310 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006311 em, start,
6312 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006313 free_extent_map(existing);
6314 if (err) {
6315 free_extent_map(em);
6316 em = NULL;
6317 }
6318 } else {
6319 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006320 free_extent_map(em);
6321 em = NULL;
6322 }
6323 } else {
6324 free_extent_map(em);
6325 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006326 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006327 }
Chris Masona52d9a82007-08-27 16:49:44 -04006328 }
Chris Mason890871b2009-09-02 16:24:52 -04006329 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006330out:
liubo1abe9b82011-03-24 11:18:59 +00006331
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006332 if (em)
6333 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006334
Chris Masonf4219502008-07-22 11:18:09 -04006335 if (path)
6336 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006337 if (trans) {
6338 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006339 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006340 err = ret;
6341 }
Chris Masona52d9a82007-08-27 16:49:44 -04006342 if (err) {
6343 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006344 return ERR_PTR(err);
6345 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006346 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006347 return em;
6348}
6349
Chris Masonec29ed52011-02-23 16:23:20 -05006350struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6351 size_t pg_offset, u64 start, u64 len,
6352 int create)
6353{
6354 struct extent_map *em;
6355 struct extent_map *hole_em = NULL;
6356 u64 range_start = start;
6357 u64 end;
6358 u64 found;
6359 u64 found_end;
6360 int err = 0;
6361
6362 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6363 if (IS_ERR(em))
6364 return em;
6365 if (em) {
6366 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006367 * if our em maps to
6368 * - a hole or
6369 * - a pre-alloc extent,
6370 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006371 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006372 if (em->block_start != EXTENT_MAP_HOLE &&
6373 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006374 return em;
6375 else
6376 hole_em = em;
6377 }
6378
6379 /* check to see if we've wrapped (len == -1 or similar) */
6380 end = start + len;
6381 if (end < start)
6382 end = (u64)-1;
6383 else
6384 end -= 1;
6385
6386 em = NULL;
6387
6388 /* ok, we didn't find anything, lets look for delalloc */
6389 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6390 end, len, EXTENT_DELALLOC, 1);
6391 found_end = range_start + found;
6392 if (found_end < range_start)
6393 found_end = (u64)-1;
6394
6395 /*
6396 * we didn't find anything useful, return
6397 * the original results from get_extent()
6398 */
6399 if (range_start > end || found_end <= start) {
6400 em = hole_em;
6401 hole_em = NULL;
6402 goto out;
6403 }
6404
6405 /* adjust the range_start to make sure it doesn't
6406 * go backwards from the start they passed in
6407 */
6408 range_start = max(start,range_start);
6409 found = found_end - range_start;
6410
6411 if (found > 0) {
6412 u64 hole_start = start;
6413 u64 hole_len = len;
6414
David Sterba172ddd62011-04-21 00:48:27 +02006415 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006416 if (!em) {
6417 err = -ENOMEM;
6418 goto out;
6419 }
6420 /*
6421 * when btrfs_get_extent can't find anything it
6422 * returns one huge hole
6423 *
6424 * make sure what it found really fits our range, and
6425 * adjust to make sure it is based on the start from
6426 * the caller
6427 */
6428 if (hole_em) {
6429 u64 calc_end = extent_map_end(hole_em);
6430
6431 if (calc_end <= start || (hole_em->start > end)) {
6432 free_extent_map(hole_em);
6433 hole_em = NULL;
6434 } else {
6435 hole_start = max(hole_em->start, start);
6436 hole_len = calc_end - hole_start;
6437 }
6438 }
6439 em->bdev = NULL;
6440 if (hole_em && range_start > hole_start) {
6441 /* our hole starts before our delalloc, so we
6442 * have to return just the parts of the hole
6443 * that go until the delalloc starts
6444 */
6445 em->len = min(hole_len,
6446 range_start - hole_start);
6447 em->start = hole_start;
6448 em->orig_start = hole_start;
6449 /*
6450 * don't adjust block start at all,
6451 * it is fixed at EXTENT_MAP_HOLE
6452 */
6453 em->block_start = hole_em->block_start;
6454 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006455 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6456 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006457 } else {
6458 em->start = range_start;
6459 em->len = found;
6460 em->orig_start = range_start;
6461 em->block_start = EXTENT_MAP_DELALLOC;
6462 em->block_len = found;
6463 }
6464 } else if (hole_em) {
6465 return hole_em;
6466 }
6467out:
6468
6469 free_extent_map(hole_em);
6470 if (err) {
6471 free_extent_map(em);
6472 return ERR_PTR(err);
6473 }
6474 return em;
6475}
6476
Josef Bacik4b46fce2010-05-23 11:00:55 -04006477static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6478 u64 start, u64 len)
6479{
6480 struct btrfs_root *root = BTRFS_I(inode)->root;
6481 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006482 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006483 struct btrfs_key ins;
6484 u64 alloc_hint;
6485 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006486
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006487 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006488 if (IS_ERR(trans))
6489 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006490
6491 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6492
6493 alloc_hint = get_extent_allocation_hint(inode, start, len);
6494 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006495 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006496 if (ret) {
6497 em = ERR_PTR(ret);
6498 goto out;
6499 }
6500
Josef Bacik70c8a912012-10-11 16:54:30 -04006501 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006502 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006503 if (IS_ERR(em))
6504 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006505
6506 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6507 ins.offset, ins.offset, 0);
6508 if (ret) {
6509 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6510 em = ERR_PTR(ret);
6511 }
6512out:
6513 btrfs_end_transaction(trans, root);
6514 return em;
6515}
6516
Chris Mason46bfbb52010-05-26 11:04:10 -04006517/*
6518 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6519 * block must be cow'd
6520 */
6521static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006522 struct inode *inode, u64 offset, u64 *len,
6523 u64 *orig_start, u64 *orig_block_len,
6524 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006525{
6526 struct btrfs_path *path;
6527 int ret;
6528 struct extent_buffer *leaf;
6529 struct btrfs_root *root = BTRFS_I(inode)->root;
6530 struct btrfs_file_extent_item *fi;
6531 struct btrfs_key key;
6532 u64 disk_bytenr;
6533 u64 backref_offset;
6534 u64 extent_end;
6535 u64 num_bytes;
6536 int slot;
6537 int found_type;
6538
6539 path = btrfs_alloc_path();
6540 if (!path)
6541 return -ENOMEM;
6542
Li Zefan33345d012011-04-20 10:31:50 +08006543 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006544 offset, 0);
6545 if (ret < 0)
6546 goto out;
6547
6548 slot = path->slots[0];
6549 if (ret == 1) {
6550 if (slot == 0) {
6551 /* can't find the item, must cow */
6552 ret = 0;
6553 goto out;
6554 }
6555 slot--;
6556 }
6557 ret = 0;
6558 leaf = path->nodes[0];
6559 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006560 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006561 key.type != BTRFS_EXTENT_DATA_KEY) {
6562 /* not our file or wrong item type, must cow */
6563 goto out;
6564 }
6565
6566 if (key.offset > offset) {
6567 /* Wrong offset, must cow */
6568 goto out;
6569 }
6570
6571 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6572 found_type = btrfs_file_extent_type(leaf, fi);
6573 if (found_type != BTRFS_FILE_EXTENT_REG &&
6574 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6575 /* not a regular extent, must cow */
6576 goto out;
6577 }
6578 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6579 backref_offset = btrfs_file_extent_offset(leaf, fi);
6580
Josef Bacikeb384b52013-04-24 16:32:55 -04006581 *orig_start = key.offset - backref_offset;
6582 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6583 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6584
Chris Mason46bfbb52010-05-26 11:04:10 -04006585 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006586 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006587 /* extent doesn't include our full range, must cow */
6588 goto out;
6589 }
6590
6591 if (btrfs_extent_readonly(root, disk_bytenr))
6592 goto out;
6593
6594 /*
6595 * look for other files referencing this extent, if we
6596 * find any we must cow
6597 */
Li Zefan33345d012011-04-20 10:31:50 +08006598 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006599 key.offset - backref_offset, disk_bytenr))
6600 goto out;
6601
6602 /*
6603 * adjust disk_bytenr and num_bytes to cover just the bytes
6604 * in this extent we are about to write. If there
6605 * are any csums in that range we have to cow in order
6606 * to keep the csums correct
6607 */
6608 disk_bytenr += backref_offset;
6609 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006610 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006611 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6612 goto out;
6613 /*
6614 * all of the above have passed, it is safe to overwrite this extent
6615 * without cow
6616 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006617 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006618 ret = 1;
6619out:
6620 btrfs_free_path(path);
6621 return ret;
6622}
6623
Josef Bacikeb838e72012-07-31 16:28:48 -04006624static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6625 struct extent_state **cached_state, int writing)
6626{
6627 struct btrfs_ordered_extent *ordered;
6628 int ret = 0;
6629
6630 while (1) {
6631 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6632 0, cached_state);
6633 /*
6634 * We're concerned with the entire range that we're going to be
6635 * doing DIO to, so we need to make sure theres no ordered
6636 * extents in this range.
6637 */
6638 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6639 lockend - lockstart + 1);
6640
6641 /*
6642 * We need to make sure there are no buffered pages in this
6643 * range either, we could have raced between the invalidate in
6644 * generic_file_direct_write and locking the extent. The
6645 * invalidate needs to happen so that reads after a write do not
6646 * get stale data.
6647 */
6648 if (!ordered && (!writing ||
6649 !test_range_bit(&BTRFS_I(inode)->io_tree,
6650 lockstart, lockend, EXTENT_UPTODATE, 0,
6651 *cached_state)))
6652 break;
6653
6654 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6655 cached_state, GFP_NOFS);
6656
6657 if (ordered) {
6658 btrfs_start_ordered_extent(inode, ordered, 1);
6659 btrfs_put_ordered_extent(ordered);
6660 } else {
6661 /* Screw you mmap */
6662 ret = filemap_write_and_wait_range(inode->i_mapping,
6663 lockstart,
6664 lockend);
6665 if (ret)
6666 break;
6667
6668 /*
6669 * If we found a page that couldn't be invalidated just
6670 * fall back to buffered.
6671 */
6672 ret = invalidate_inode_pages2_range(inode->i_mapping,
6673 lockstart >> PAGE_CACHE_SHIFT,
6674 lockend >> PAGE_CACHE_SHIFT);
6675 if (ret)
6676 break;
6677 }
6678
6679 cond_resched();
6680 }
6681
6682 return ret;
6683}
6684
Josef Bacik69ffb542012-09-11 15:40:07 -04006685static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6686 u64 len, u64 orig_start,
6687 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006688 u64 orig_block_len, u64 ram_bytes,
6689 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006690{
6691 struct extent_map_tree *em_tree;
6692 struct extent_map *em;
6693 struct btrfs_root *root = BTRFS_I(inode)->root;
6694 int ret;
6695
6696 em_tree = &BTRFS_I(inode)->extent_tree;
6697 em = alloc_extent_map();
6698 if (!em)
6699 return ERR_PTR(-ENOMEM);
6700
6701 em->start = start;
6702 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006703 em->mod_start = start;
6704 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006705 em->len = len;
6706 em->block_len = block_len;
6707 em->block_start = block_start;
6708 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006709 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006710 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006711 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006712 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6713 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006714 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006715
6716 do {
6717 btrfs_drop_extent_cache(inode, em->start,
6718 em->start + em->len - 1, 0);
6719 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006720 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006721 write_unlock(&em_tree->lock);
6722 } while (ret == -EEXIST);
6723
6724 if (ret) {
6725 free_extent_map(em);
6726 return ERR_PTR(ret);
6727 }
6728
6729 return em;
6730}
6731
6732
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6734 struct buffer_head *bh_result, int create)
6735{
6736 struct extent_map *em;
6737 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006738 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006739 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006740 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006741 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006742 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006743 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006744 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006745
Miao Xie172a5042013-02-21 02:48:22 -07006746 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006747 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006748 else
Josef Bacikc3298612012-08-03 16:49:19 -04006749 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006750
Josef Bacikc3298612012-08-03 16:49:19 -04006751 lockstart = start;
6752 lockend = start + len - 1;
6753
Josef Bacikeb838e72012-07-31 16:28:48 -04006754 /*
6755 * If this errors out it's because we couldn't invalidate pagecache for
6756 * this range and we need to fallback to buffered.
6757 */
6758 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6759 return -ENOTBLK;
6760
Josef Bacik4b46fce2010-05-23 11:00:55 -04006761 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006762 if (IS_ERR(em)) {
6763 ret = PTR_ERR(em);
6764 goto unlock_err;
6765 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006766
6767 /*
6768 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6769 * io. INLINE is special, and we could probably kludge it in here, but
6770 * it's still buffered so for safety lets just fall back to the generic
6771 * buffered path.
6772 *
6773 * For COMPRESSED we _have_ to read the entire extent in so we can
6774 * decompress it, so there will be buffering required no matter what we
6775 * do, so go ahead and fallback to buffered.
6776 *
6777 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6778 * to buffered IO. Don't blame me, this is the price we pay for using
6779 * the generic code.
6780 */
6781 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6782 em->block_start == EXTENT_MAP_INLINE) {
6783 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006784 ret = -ENOTBLK;
6785 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006786 }
6787
6788 /* Just a good old fashioned hole, return */
6789 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6790 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6791 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006792 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006793 }
6794
6795 /*
6796 * We don't allocate a new extent in the following cases
6797 *
6798 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6799 * existing extent.
6800 * 2) The extent is marked as PREALLOC. We're good to go here and can
6801 * just use the extent.
6802 *
6803 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006804 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006805 len = min(len, em->len - (start - em->start));
6806 lockstart = start + len;
6807 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006808 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006809
6810 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6811 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6812 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006813 int type;
6814 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006815 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006816
6817 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6818 type = BTRFS_ORDERED_PREALLOC;
6819 else
6820 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006821 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006823
6824 /*
6825 * we're not going to log anything, but we do need
6826 * to make sure the current transaction stays open
6827 * while we look for nocow cross refs
6828 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006829 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006830 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006831 goto must_cow;
6832
Josef Bacikeb384b52013-04-24 16:32:55 -04006833 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6834 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006835 if (type == BTRFS_ORDERED_PREALLOC) {
6836 free_extent_map(em);
6837 em = create_pinned_em(inode, start, len,
6838 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006839 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006840 orig_block_len,
6841 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006842 if (IS_ERR(em)) {
6843 btrfs_end_transaction(trans, root);
6844 goto unlock_err;
6845 }
6846 }
6847
Chris Mason46bfbb52010-05-26 11:04:10 -04006848 ret = btrfs_add_ordered_extent_dio(inode, start,
6849 block_start, len, len, type);
6850 btrfs_end_transaction(trans, root);
6851 if (ret) {
6852 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006853 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006854 }
6855 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006857 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006858 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006859must_cow:
6860 /*
6861 * this will cow the extent, reset the len in case we changed
6862 * it above
6863 */
6864 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006865 free_extent_map(em);
6866 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006867 if (IS_ERR(em)) {
6868 ret = PTR_ERR(em);
6869 goto unlock_err;
6870 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006871 len = min(len, em->len - (start - em->start));
6872unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6874 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006875 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006876 bh_result->b_bdev = em->bdev;
6877 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006878 if (create) {
6879 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6880 set_buffer_new(bh_result);
6881
6882 /*
6883 * Need to update the i_size under the extent lock so buffered
6884 * readers will get the updated i_size when we unlock.
6885 */
6886 if (start + len > i_size_read(inode))
6887 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006888
Miao Xie172a5042013-02-21 02:48:22 -07006889 spin_lock(&BTRFS_I(inode)->lock);
6890 BTRFS_I(inode)->outstanding_extents++;
6891 spin_unlock(&BTRFS_I(inode)->lock);
6892
Miao Xie09348562013-02-07 10:12:07 +00006893 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6894 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6895 &cached_state, GFP_NOFS);
6896 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006897 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006898
Josef Bacikeb838e72012-07-31 16:28:48 -04006899 /*
6900 * In the case of write we need to clear and unlock the entire range,
6901 * in the case of read we need to unlock only the end area that we
6902 * aren't using if there is any left over space.
6903 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006904 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006905 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6906 lockend, unlock_bits, 1, 0,
6907 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006908 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006909 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006910 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006911
Josef Bacik4b46fce2010-05-23 11:00:55 -04006912 free_extent_map(em);
6913
6914 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006915
6916unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006917 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6918 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6919 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006920}
6921
6922struct btrfs_dio_private {
6923 struct inode *inode;
6924 u64 logical_offset;
6925 u64 disk_bytenr;
6926 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006927 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006928
6929 /* number of bios pending for this dio */
6930 atomic_t pending_bios;
6931
6932 /* IO errors */
6933 int errors;
6934
Chris Mason9be33952013-05-17 18:30:14 -04006935 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006936 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006937
6938 /* dio_bio came from fs/direct-io.c */
6939 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940};
6941
6942static void btrfs_endio_direct_read(struct bio *bio, int err)
6943{
Miao Xiee65e1532010-11-22 03:04:43 +00006944 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6946 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006947 struct inode *inode = dip->inode;
6948 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006949 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006950 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006951
6952 start = dip->logical_offset;
6953 do {
6954 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6955 struct page *page = bvec->bv_page;
6956 char *kaddr;
6957 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006958 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006959 unsigned long flags;
6960
Josef Bacikc3298612012-08-03 16:49:19 -04006961 if (get_state_private(&BTRFS_I(inode)->io_tree,
6962 start, &private))
6963 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006964 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006965 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006966 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006967 csum, bvec->bv_len);
6968 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006969 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006970 local_irq_restore(flags);
6971
6972 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006973 if (csum != private) {
6974failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006975 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6976 (unsigned long long)btrfs_ino(inode),
6977 (unsigned long long)start,
6978 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006979 err = -EIO;
6980 }
6981 }
6982
6983 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006984 bvec++;
6985 } while (bvec <= bvec_end);
6986
6987 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006988 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006989 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006990
Josef Bacik4b46fce2010-05-23 11:00:55 -04006991 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006992
6993 /* If we had a csum failure make sure to clear the uptodate flag */
6994 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006995 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6996 dio_end_io(dio_bio, err);
6997 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006998}
6999
7000static void btrfs_endio_direct_write(struct bio *bio, int err)
7001{
7002 struct btrfs_dio_private *dip = bio->bi_private;
7003 struct inode *inode = dip->inode;
7004 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007005 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007006 u64 ordered_offset = dip->logical_offset;
7007 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007008 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007009 int ret;
7010
7011 if (err)
7012 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007013again:
7014 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7015 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007016 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007017 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007018 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007019
Josef Bacik5fd02042012-05-02 14:00:54 -04007020 ordered->work.func = finish_ordered_fn;
7021 ordered->work.flags = 0;
7022 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7023 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007024out_test:
7025 /*
7026 * our bio might span multiple ordered extents. If we haven't
7027 * completed the accounting for the whole dio, go back and try again
7028 */
7029 if (ordered_offset < dip->logical_offset + dip->bytes) {
7030 ordered_bytes = dip->logical_offset + dip->bytes -
7031 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007032 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007033 goto again;
7034 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007035out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007036 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007037
Josef Bacik4b46fce2010-05-23 11:00:55 -04007038 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007039
7040 /* If we had an error make sure to clear the uptodate flag */
7041 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007042 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7043 dio_end_io(dio_bio, err);
7044 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007045}
7046
Chris Masoneaf25d92010-05-25 09:48:28 -04007047static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7048 struct bio *bio, int mirror_num,
7049 unsigned long bio_flags, u64 offset)
7050{
7051 int ret;
7052 struct btrfs_root *root = BTRFS_I(inode)->root;
7053 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007054 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007055 return 0;
7056}
7057
Miao Xiee65e1532010-11-22 03:04:43 +00007058static void btrfs_end_dio_bio(struct bio *bio, int err)
7059{
7060 struct btrfs_dio_private *dip = bio->bi_private;
7061
7062 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007063 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007064 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007065 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007066 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007067 dip->errors = 1;
7068
7069 /*
7070 * before atomic variable goto zero, we must make sure
7071 * dip->errors is perceived to be set.
7072 */
7073 smp_mb__before_atomic_dec();
7074 }
7075
7076 /* if there are more bios still pending for this dio, just exit */
7077 if (!atomic_dec_and_test(&dip->pending_bios))
7078 goto out;
7079
Chris Mason9be33952013-05-17 18:30:14 -04007080 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007081 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007082 } else {
7083 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007084 bio_endio(dip->orig_bio, 0);
7085 }
7086out:
7087 bio_put(bio);
7088}
7089
7090static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7091 u64 first_sector, gfp_t gfp_flags)
7092{
7093 int nr_vecs = bio_get_nr_vecs(bdev);
7094 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7095}
7096
7097static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7098 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007099 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007100{
7101 int write = rw & REQ_WRITE;
7102 struct btrfs_root *root = BTRFS_I(inode)->root;
7103 int ret;
7104
Josef Bacikb812ce22012-11-16 13:56:32 -05007105 if (async_submit)
7106 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7107
Miao Xiee65e1532010-11-22 03:04:43 +00007108 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007109
7110 if (!write) {
7111 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7112 if (ret)
7113 goto err;
7114 }
Miao Xiee65e1532010-11-22 03:04:43 +00007115
Josef Bacik1ae39932011-04-06 14:41:34 -04007116 if (skip_sum)
7117 goto map;
7118
7119 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007120 ret = btrfs_wq_submit_bio(root->fs_info,
7121 inode, rw, bio, 0, 0,
7122 file_offset,
7123 __btrfs_submit_bio_start_direct_io,
7124 __btrfs_submit_bio_done);
7125 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007126 } else if (write) {
7127 /*
7128 * If we aren't doing async submit, calculate the csum of the
7129 * bio now.
7130 */
7131 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7132 if (ret)
7133 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007134 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007135 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007136 if (ret)
7137 goto err;
7138 }
Miao Xiee65e1532010-11-22 03:04:43 +00007139
Josef Bacik1ae39932011-04-06 14:41:34 -04007140map:
7141 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007142err:
7143 bio_put(bio);
7144 return ret;
7145}
7146
7147static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7148 int skip_sum)
7149{
7150 struct inode *inode = dip->inode;
7151 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007152 struct bio *bio;
7153 struct bio *orig_bio = dip->orig_bio;
7154 struct bio_vec *bvec = orig_bio->bi_io_vec;
7155 u64 start_sector = orig_bio->bi_sector;
7156 u64 file_offset = dip->logical_offset;
7157 u64 submit_len = 0;
7158 u64 map_length;
7159 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007160 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007161 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007162
Miao Xiee65e1532010-11-22 03:04:43 +00007163 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007164 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007165 &map_length, NULL, 0);
7166 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007167 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007168 return -EIO;
7169 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007170 if (map_length >= orig_bio->bi_size) {
7171 bio = orig_bio;
7172 goto submit;
7173 }
7174
David Woodhouse53b381b2013-01-29 18:40:14 -05007175 /* async crcs make it difficult to collect full stripe writes. */
7176 if (btrfs_get_alloc_profile(root, 1) &
7177 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7178 async_submit = 0;
7179 else
7180 async_submit = 1;
7181
Josef Bacik02f57c72011-04-06 14:25:44 -04007182 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7183 if (!bio)
7184 return -ENOMEM;
7185 bio->bi_private = dip;
7186 bio->bi_end_io = btrfs_end_dio_bio;
7187 atomic_inc(&dip->pending_bios);
7188
Miao Xiee65e1532010-11-22 03:04:43 +00007189 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7190 if (unlikely(map_length < submit_len + bvec->bv_len ||
7191 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7192 bvec->bv_offset) < bvec->bv_len)) {
7193 /*
7194 * inc the count before we submit the bio so
7195 * we know the end IO handler won't happen before
7196 * we inc the count. Otherwise, the dip might get freed
7197 * before we're done setting it up
7198 */
7199 atomic_inc(&dip->pending_bios);
7200 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7201 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007202 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007203 if (ret) {
7204 bio_put(bio);
7205 atomic_dec(&dip->pending_bios);
7206 goto out_err;
7207 }
7208
Miao Xiee65e1532010-11-22 03:04:43 +00007209 start_sector += submit_len >> 9;
7210 file_offset += submit_len;
7211
7212 submit_len = 0;
7213 nr_pages = 0;
7214
7215 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7216 start_sector, GFP_NOFS);
7217 if (!bio)
7218 goto out_err;
7219 bio->bi_private = dip;
7220 bio->bi_end_io = btrfs_end_dio_bio;
7221
7222 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007223 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007224 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007225 &map_length, NULL, 0);
7226 if (ret) {
7227 bio_put(bio);
7228 goto out_err;
7229 }
7230 } else {
7231 submit_len += bvec->bv_len;
7232 nr_pages ++;
7233 bvec++;
7234 }
7235 }
7236
Josef Bacik02f57c72011-04-06 14:25:44 -04007237submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007238 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007239 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007240 if (!ret)
7241 return 0;
7242
7243 bio_put(bio);
7244out_err:
7245 dip->errors = 1;
7246 /*
7247 * before atomic variable goto zero, we must
7248 * make sure dip->errors is perceived to be set.
7249 */
7250 smp_mb__before_atomic_dec();
7251 if (atomic_dec_and_test(&dip->pending_bios))
7252 bio_io_error(dip->orig_bio);
7253
7254 /* bio_end_io() will handle error, so we needn't return it */
7255 return 0;
7256}
7257
Chris Mason9be33952013-05-17 18:30:14 -04007258static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7259 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007260{
7261 struct btrfs_root *root = BTRFS_I(inode)->root;
7262 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007263 struct bio_vec *bvec = dio_bio->bi_io_vec;
7264 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007265 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007266 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007267 int ret = 0;
7268
7269 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7270
Chris Mason9be33952013-05-17 18:30:14 -04007271 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7272
7273 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274 ret = -ENOMEM;
7275 goto free_ordered;
7276 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277
Chris Mason9be33952013-05-17 18:30:14 -04007278 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7279 if (!dip) {
7280 ret = -ENOMEM;
7281 goto free_io_bio;
7282 }
7283
7284 dip->private = dio_bio->bi_private;
7285 io_bio->bi_private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007286 dip->inode = inode;
7287 dip->logical_offset = file_offset;
7288
Josef Bacik4b46fce2010-05-23 11:00:55 -04007289 dip->bytes = 0;
7290 do {
7291 dip->bytes += bvec->bv_len;
7292 bvec++;
Chris Mason9be33952013-05-17 18:30:14 -04007293 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294
Chris Mason9be33952013-05-17 18:30:14 -04007295 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7296 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007297 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007298 dip->orig_bio = io_bio;
7299 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007300 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007301
7302 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007303 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007304 else
Chris Mason9be33952013-05-17 18:30:14 -04007305 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007306
Miao Xiee65e1532010-11-22 03:04:43 +00007307 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7308 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007309 return;
Chris Mason9be33952013-05-17 18:30:14 -04007310
7311free_io_bio:
7312 bio_put(io_bio);
7313
Josef Bacik4b46fce2010-05-23 11:00:55 -04007314free_ordered:
7315 /*
7316 * If this is a write, we need to clean up the reserved space and kill
7317 * the ordered extent.
7318 */
7319 if (write) {
7320 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007321 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7323 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7324 btrfs_free_reserved_extent(root, ordered->start,
7325 ordered->disk_len);
7326 btrfs_put_ordered_extent(ordered);
7327 btrfs_put_ordered_extent(ordered);
7328 }
Chris Mason9be33952013-05-17 18:30:14 -04007329 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007330}
7331
Chris Mason5a5f79b2010-05-26 21:33:37 -04007332static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7333 const struct iovec *iov, loff_t offset,
7334 unsigned long nr_segs)
7335{
7336 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007337 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007338 size_t size;
7339 unsigned long addr;
7340 unsigned blocksize_mask = root->sectorsize - 1;
7341 ssize_t retval = -EINVAL;
7342 loff_t end = offset;
7343
7344 if (offset & blocksize_mask)
7345 goto out;
7346
7347 /* Check the memory alignment. Blocks cannot straddle pages */
7348 for (seg = 0; seg < nr_segs; seg++) {
7349 addr = (unsigned long)iov[seg].iov_base;
7350 size = iov[seg].iov_len;
7351 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007352 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007353 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007354
7355 /* If this is a write we don't need to check anymore */
7356 if (rw & WRITE)
7357 continue;
7358
7359 /*
7360 * Check to make sure we don't have duplicate iov_base's in this
7361 * iovec, if so return EINVAL, otherwise we'll get csum errors
7362 * when reading back.
7363 */
7364 for (i = seg + 1; i < nr_segs; i++) {
7365 if (iov[seg].iov_base == iov[i].iov_base)
7366 goto out;
7367 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007368 }
7369 retval = 0;
7370out:
7371 return retval;
7372}
Josef Bacikeb838e72012-07-31 16:28:48 -04007373
Chris Mason16432982008-04-10 10:23:21 -04007374static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7375 const struct iovec *iov, loff_t offset,
7376 unsigned long nr_segs)
7377{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007378 struct file *file = iocb->ki_filp;
7379 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007380 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007381 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007382 bool wakeup = true;
7383 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007384 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007385
Chris Mason5a5f79b2010-05-26 21:33:37 -04007386 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007387 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007388 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007389
Miao Xie38851cc2013-02-08 07:04:11 +00007390 atomic_inc(&inode->i_dio_count);
7391 smp_mb__after_atomic_inc();
7392
Miao Xie09348562013-02-07 10:12:07 +00007393 if (rw & WRITE) {
7394 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007395 /*
7396 * If the write DIO is beyond the EOF, we need update
7397 * the isize, but it is protected by i_mutex. So we can
7398 * not unlock the i_mutex at this case.
7399 */
7400 if (offset + count <= inode->i_size) {
7401 mutex_unlock(&inode->i_mutex);
7402 relock = true;
7403 }
Miao Xie09348562013-02-07 10:12:07 +00007404 ret = btrfs_delalloc_reserve_space(inode, count);
7405 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007406 goto out;
7407 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7408 &BTRFS_I(inode)->runtime_flags))) {
7409 inode_dio_done(inode);
7410 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7411 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007412 }
7413
7414 ret = __blockdev_direct_IO(rw, iocb, inode,
7415 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7416 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007417 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007418 if (rw & WRITE) {
7419 if (ret < 0 && ret != -EIOCBQUEUED)
7420 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007421 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007422 btrfs_delalloc_release_space(inode,
7423 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007424 else
7425 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007426 }
Miao Xie38851cc2013-02-08 07:04:11 +00007427out:
Miao Xie2e60a512013-02-08 07:01:08 +00007428 if (wakeup)
7429 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007430 if (relock)
7431 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007432
7433 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007434}
7435
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007436#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7437
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007438static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7439 __u64 start, __u64 len)
7440{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007441 int ret;
7442
7443 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7444 if (ret)
7445 return ret;
7446
Chris Masonec29ed52011-02-23 16:23:20 -05007447 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007448}
7449
Chris Mason9ebefb182007-06-15 13:50:00 -04007450int btrfs_readpage(struct file *file, struct page *page)
7451{
Chris Masond1310b22008-01-24 16:13:08 -05007452 struct extent_io_tree *tree;
7453 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007454 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007455}
Chris Mason1832a6d2007-12-21 16:27:21 -05007456
Chris Mason39279cc2007-06-12 06:35:45 -04007457static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7458{
Chris Masond1310b22008-01-24 16:13:08 -05007459 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007460
7461
7462 if (current->flags & PF_MEMALLOC) {
7463 redirty_page_for_writepage(wbc, page);
7464 unlock_page(page);
7465 return 0;
7466 }
Chris Masond1310b22008-01-24 16:13:08 -05007467 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007468 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7469}
Chris Mason39279cc2007-06-12 06:35:45 -04007470
Eric Sandeen48a3b632013-04-25 20:41:01 +00007471static int btrfs_writepages(struct address_space *mapping,
7472 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007473{
Chris Masond1310b22008-01-24 16:13:08 -05007474 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007475
Chris Masond1310b22008-01-24 16:13:08 -05007476 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007477 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7478}
7479
Chris Mason3ab2fb52007-11-08 10:59:22 -05007480static int
7481btrfs_readpages(struct file *file, struct address_space *mapping,
7482 struct list_head *pages, unsigned nr_pages)
7483{
Chris Masond1310b22008-01-24 16:13:08 -05007484 struct extent_io_tree *tree;
7485 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007486 return extent_readpages(tree, mapping, pages, nr_pages,
7487 btrfs_get_extent);
7488}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007489static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007490{
Chris Masond1310b22008-01-24 16:13:08 -05007491 struct extent_io_tree *tree;
7492 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007493 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007494
Chris Masond1310b22008-01-24 16:13:08 -05007495 tree = &BTRFS_I(page->mapping->host)->io_tree;
7496 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007497 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007498 if (ret == 1) {
7499 ClearPagePrivate(page);
7500 set_page_private(page, 0);
7501 page_cache_release(page);
7502 }
7503 return ret;
7504}
Chris Mason39279cc2007-06-12 06:35:45 -04007505
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7507{
Chris Mason98509cf2008-09-11 15:51:43 -04007508 if (PageWriteback(page) || PageDirty(page))
7509 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007510 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007511}
7512
Chris Masona52d9a82007-08-27 16:49:44 -04007513static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7514{
Josef Bacik5fd02042012-05-02 14:00:54 -04007515 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007516 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007517 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007518 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007519 u64 page_start = page_offset(page);
7520 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007521
Chris Mason8b62b722009-09-02 16:53:46 -04007522 /*
7523 * we have the page locked, so new writeback can't start,
7524 * and the dirty bit won't be cleared while we are here.
7525 *
7526 * Wait for IO on this page so that we can safely clear
7527 * the PagePrivate2 bit and do ordered accounting
7528 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007529 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007530
Josef Bacik5fd02042012-05-02 14:00:54 -04007531 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007532 if (offset) {
7533 btrfs_releasepage(page, GFP_NOFS);
7534 return;
7535 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007536 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007537 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007538 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007539 /*
7540 * IO on this page will never be started, so we need
7541 * to account for any ordered extents now
7542 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007543 clear_extent_bit(tree, page_start, page_end,
7544 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007545 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7546 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007547 /*
7548 * whoever cleared the private bit is responsible
7549 * for the finish_ordered_io
7550 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007551 if (TestClearPagePrivate2(page) &&
7552 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7553 PAGE_CACHE_SIZE, 1)) {
7554 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007555 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007556 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007557 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007558 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007559 }
7560 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007561 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007562 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7563 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007564 __btrfs_releasepage(page, GFP_NOFS);
7565
Chris Mason4a096752008-07-21 10:29:44 -04007566 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007567 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007568 ClearPagePrivate(page);
7569 set_page_private(page, 0);
7570 page_cache_release(page);
7571 }
Chris Mason39279cc2007-06-12 06:35:45 -04007572}
7573
Chris Mason9ebefb182007-06-15 13:50:00 -04007574/*
7575 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7576 * called from a page fault handler when a page is first dirtied. Hence we must
7577 * be careful to check for EOF conditions here. We set the page up correctly
7578 * for a written page which means we get ENOSPC checking when writing into
7579 * holes and correct delalloc and unwritten extent mapping on filesystems that
7580 * support these features.
7581 *
7582 * We are not allowed to take the i_mutex here so we have to play games to
7583 * protect against truncate races as the page could now be beyond EOF. Because
7584 * vmtruncate() writes the inode size before removing pages, once we have the
7585 * page lock we can determine safely if the page is beyond EOF. If it is not
7586 * beyond EOF, then the page is guaranteed safe against truncation until we
7587 * unlock the page.
7588 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007589int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007590{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007591 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007592 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007593 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007594 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7595 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007596 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007597 char *kaddr;
7598 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007599 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007600 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007601 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007602 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007603 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007604
Jan Karab2b5ef52012-06-12 16:20:45 +02007605 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007606 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007607 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007608 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007609 reserved = 1;
7610 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007611 if (ret) {
7612 if (ret == -ENOMEM)
7613 ret = VM_FAULT_OOM;
7614 else /* -ENOSPC, -EIO, etc */
7615 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007616 if (reserved)
7617 goto out;
7618 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007619 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007620
Nick Piggin56a76f82009-03-31 15:23:23 -07007621 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007622again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007623 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007624 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007625 page_start = page_offset(page);
7626 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007627
Chris Mason9ebefb182007-06-15 13:50:00 -04007628 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007629 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007630 /* page got truncated out from underneath us */
7631 goto out_unlock;
7632 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007633 wait_on_page_writeback(page);
7634
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007635 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007636 set_page_extent_mapped(page);
7637
Chris Masoneb84ae02008-07-17 13:53:27 -04007638 /*
7639 * we can't set the delalloc bits if there are pending ordered
7640 * extents. Drop our locks and wait for them to finish
7641 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007642 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7643 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007644 unlock_extent_cached(io_tree, page_start, page_end,
7645 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007646 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007647 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007648 btrfs_put_ordered_extent(ordered);
7649 goto again;
7650 }
7651
Josef Bacikfbf19082009-10-01 17:10:23 -04007652 /*
7653 * XXX - page_mkwrite gets called every time the page is dirtied, even
7654 * if it was already dirty, so for space accounting reasons we need to
7655 * clear any delalloc bits for the range we are fixing to save. There
7656 * is probably a better way to do this, but for now keep consistent with
7657 * prepare_pages in the normal write path.
7658 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007659 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007660 EXTENT_DIRTY | EXTENT_DELALLOC |
7661 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007662 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007663
Josef Bacik2ac55d42010-02-03 19:33:23 +00007664 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7665 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007666 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007667 unlock_extent_cached(io_tree, page_start, page_end,
7668 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007669 ret = VM_FAULT_SIGBUS;
7670 goto out_unlock;
7671 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007672 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007673
7674 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007675 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007676 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007677 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007678 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007679
Chris Masone6dcd2d2008-07-17 12:53:50 -04007680 if (zero_start != PAGE_CACHE_SIZE) {
7681 kaddr = kmap(page);
7682 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7683 flush_dcache_page(page);
7684 kunmap(page);
7685 }
Chris Mason247e7432008-07-17 12:53:51 -04007686 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007687 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007688 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007689
Chris Mason257c62e2009-10-13 13:21:08 -04007690 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7691 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007692 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007693
Josef Bacik2ac55d42010-02-03 19:33:23 +00007694 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007695
7696out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007697 if (!ret) {
7698 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007699 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007700 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007701 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007702out:
Josef Bacikec39e182012-01-12 19:10:12 -05007703 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007704out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007705 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007706 return ret;
7707}
7708
Josef Bacika41ad392011-01-31 15:30:16 -05007709static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007710{
7711 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007712 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007713 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007714 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007715 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007716 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007717 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007718
Josef Bacik2aaa6652012-08-29 14:27:18 -04007719 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007720 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007721 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007722
Chris Mason4a096752008-07-21 10:29:44 -04007723 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007724 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007725
Josef Bacikfcb80c22011-05-03 10:40:22 -04007726 /*
7727 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7728 * 3 things going on here
7729 *
7730 * 1) We need to reserve space for our orphan item and the space to
7731 * delete our orphan item. Lord knows we don't want to have a dangling
7732 * orphan item because we didn't reserve space to remove it.
7733 *
7734 * 2) We need to reserve space to update our inode.
7735 *
7736 * 3) We need to have something to cache all the space that is going to
7737 * be free'd up by the truncate operation, but also have some slack
7738 * space reserved in case it uses space during the truncate (thank you
7739 * very much snapshotting).
7740 *
7741 * And we need these to all be seperate. The fact is we can use alot of
7742 * space doing the truncate, and we have no earthly idea how much space
7743 * we will use, so we need the truncate reservation to be seperate so it
7744 * doesn't end up using space reserved for updating the inode or
7745 * removing the orphan item. We also need to be able to stop the
7746 * transaction and start a new one, which means we need to be able to
7747 * update the inode several times, and we have no idea of knowing how
7748 * many times that will be, so we can't just reserve 1 item for the
7749 * entirety of the opration, so that has to be done seperately as well.
7750 * Then there is the orphan item, which does indeed need to be held on
7751 * to for the whole operation, and we need nobody to touch this reserved
7752 * space except the orphan code.
7753 *
7754 * So that leaves us with
7755 *
7756 * 1) root->orphan_block_rsv - for the orphan deletion.
7757 * 2) rsv - for the truncate reservation, which we will steal from the
7758 * transaction reservation.
7759 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7760 * updating the inode.
7761 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007762 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007763 if (!rsv)
7764 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007765 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007766 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007767
Josef Bacik907cbce2011-08-08 13:46:15 -04007768 /*
Josef Bacik07127182011-08-19 10:29:59 -04007769 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007770 * 1 for updating the inode.
7771 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007772 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007773 if (IS_ERR(trans)) {
7774 err = PTR_ERR(trans);
7775 goto out;
7776 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007777
Josef Bacik907cbce2011-08-08 13:46:15 -04007778 /* Migrate the slack space for the truncate to our reserve */
7779 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7780 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007781 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007782
Chris Mason5a3f23d2009-03-31 13:27:11 -04007783 /*
7784 * setattr is responsible for setting the ordered_data_close flag,
7785 * but that is only tested during the last file release. That
7786 * could happen well after the next commit, leaving a great big
7787 * window where new writes may get lost if someone chooses to write
7788 * to this file after truncating to zero
7789 *
7790 * The inode doesn't have any dirty data here, and so if we commit
7791 * this is a noop. If someone immediately starts writing to the inode
7792 * it is very likely we'll catch some of their writes in this
7793 * transaction, and the commit will find this file on the ordered
7794 * data list with good things to send down.
7795 *
7796 * This is a best effort solution, there is still a window where
7797 * using truncate to replace the contents of the file will
7798 * end up with a zero length file after a crash.
7799 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007800 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7801 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007802 btrfs_add_ordered_operation(trans, root, inode);
7803
Josef Bacik5dc562c2012-08-17 13:14:17 -04007804 /*
7805 * So if we truncate and then write and fsync we normally would just
7806 * write the extents that changed, which is a problem if we need to
7807 * first truncate that entire inode. So set this flag so we write out
7808 * all of the extents in the inode to the sync log so we're completely
7809 * safe.
7810 */
7811 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007812 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007813
Yan, Zheng80825102009-11-12 09:35:36 +00007814 while (1) {
7815 ret = btrfs_truncate_inode_items(trans, root, inode,
7816 inode->i_size,
7817 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007818 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007819 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007820 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007821 }
Chris Mason39279cc2007-06-12 06:35:45 -04007822
Josef Bacikfcb80c22011-05-03 10:40:22 -04007823 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007824 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007825 if (ret) {
7826 err = ret;
7827 break;
7828 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007829
Yan, Zheng80825102009-11-12 09:35:36 +00007830 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007831 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007832
7833 trans = btrfs_start_transaction(root, 2);
7834 if (IS_ERR(trans)) {
7835 ret = err = PTR_ERR(trans);
7836 trans = NULL;
7837 break;
7838 }
7839
7840 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7841 rsv, min_size);
7842 BUG_ON(ret); /* shouldn't happen */
7843 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007844 }
7845
7846 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007847 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007848 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007849 if (ret)
7850 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007851 }
7852
Chris Mason917c16b2011-11-08 14:49:59 -05007853 if (trans) {
7854 trans->block_rsv = &root->fs_info->trans_block_rsv;
7855 ret = btrfs_update_inode(trans, root, inode);
7856 if (ret && !err)
7857 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007858
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007859 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007860 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007861 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007862
7863out:
7864 btrfs_free_block_rsv(root, rsv);
7865
Josef Bacik3893e332011-01-31 16:03:11 -05007866 if (ret && !err)
7867 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007868
Josef Bacik3893e332011-01-31 16:03:11 -05007869 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007870}
7871
Sven Wegener3b963622008-06-09 21:57:42 -04007872/*
Chris Masond352ac62008-09-29 15:18:18 -04007873 * create a new subvolume directory/inode (helper for the ioctl).
7874 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007875int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007876 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007877{
Chris Mason39279cc2007-06-12 06:35:45 -04007878 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007879 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007880 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007881
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007882 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7883 new_dirid, new_dirid,
7884 S_IFDIR | (~current_umask() & S_IRWXUGO),
7885 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007886 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007887 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007888 inode->i_op = &btrfs_dir_inode_operations;
7889 inode->i_fop = &btrfs_dir_file_operations;
7890
Miklos Szeredibfe86842011-10-28 14:13:29 +02007891 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007892 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007893
Yan, Zheng76dda932009-09-21 16:00:26 -04007894 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007895
Yan, Zheng76dda932009-09-21 16:00:26 -04007896 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007897 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007898}
7899
Chris Mason39279cc2007-06-12 06:35:45 -04007900struct inode *btrfs_alloc_inode(struct super_block *sb)
7901{
7902 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007903 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007904
7905 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7906 if (!ei)
7907 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007908
7909 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007910 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007911 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007912 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007913 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007914 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007915 ei->disk_i_size = 0;
7916 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007917 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007918 ei->index_cnt = (u64)-1;
7919 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007920 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007921
Josef Bacik9e0baf62011-07-15 15:16:44 +00007922 spin_lock_init(&ei->lock);
7923 ei->outstanding_extents = 0;
7924 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007925
Josef Bacik72ac3c02012-05-23 14:13:11 -04007926 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007927 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007928
Miao Xie16cdcec2011-04-22 18:12:22 +08007929 ei->delayed_node = NULL;
7930
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007931 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007932 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007933 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7934 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007935 ei->io_tree.track_uptodate = 1;
7936 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007937 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007938 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007939 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007940 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007941 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007942 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007943 RB_CLEAR_NODE(&ei->rb_node);
7944
7945 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007946}
7947
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007948static void btrfs_i_callback(struct rcu_head *head)
7949{
7950 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007951 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7952}
7953
Chris Mason39279cc2007-06-12 06:35:45 -04007954void btrfs_destroy_inode(struct inode *inode)
7955{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007956 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007957 struct btrfs_root *root = BTRFS_I(inode)->root;
7958
Al Virob3d9b7a2012-06-09 13:51:19 -04007959 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007960 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007961 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7962 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007963 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7964 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007965
Chris Mason5a3f23d2009-03-31 13:27:11 -04007966 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007967 * This can happen where we create an inode, but somebody else also
7968 * created the same inode and we need to destroy the one we already
7969 * created.
7970 */
7971 if (!root)
7972 goto free;
7973
7974 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007975 * Make sure we're properly removed from the ordered operation
7976 * lists.
7977 */
7978 smp_mb();
7979 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7980 spin_lock(&root->fs_info->ordered_extent_lock);
7981 list_del_init(&BTRFS_I(inode)->ordered_operations);
7982 spin_unlock(&root->fs_info->ordered_extent_lock);
7983 }
7984
Josef Bacik8a35d952012-05-23 14:26:42 -04007985 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7986 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007987 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7988 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007989 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007990 }
Josef Bacik7b128762008-07-24 12:17:14 -04007991
Chris Masond3977122009-01-05 21:25:51 -05007992 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007993 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7994 if (!ordered)
7995 break;
7996 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007997 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7998 (unsigned long long)ordered->file_offset,
7999 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008000 btrfs_remove_ordered_extent(inode, ordered);
8001 btrfs_put_ordered_extent(ordered);
8002 btrfs_put_ordered_extent(ordered);
8003 }
8004 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008005 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008006 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008007free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008008 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008009}
8010
Al Viro45321ac2010-06-07 13:43:19 -04008011int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008012{
8013 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008014
Naohiro Aota6379ef92013-06-06 09:56:34 +00008015 if (root == NULL)
8016 return 1;
8017
Liu Bofa6ac872013-02-20 14:10:23 +00008018 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04008019 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00008020 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04008021 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008022 else
Al Viro45321ac2010-06-07 13:43:19 -04008023 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008024}
8025
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008026static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008027{
8028 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8029
8030 inode_init_once(&ei->vfs_inode);
8031}
8032
8033void btrfs_destroy_cachep(void)
8034{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008035 /*
8036 * Make sure all delayed rcu free inodes are flushed before we
8037 * destroy cache.
8038 */
8039 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008040 if (btrfs_inode_cachep)
8041 kmem_cache_destroy(btrfs_inode_cachep);
8042 if (btrfs_trans_handle_cachep)
8043 kmem_cache_destroy(btrfs_trans_handle_cachep);
8044 if (btrfs_transaction_cachep)
8045 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008046 if (btrfs_path_cachep)
8047 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008048 if (btrfs_free_space_cachep)
8049 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008050 if (btrfs_delalloc_work_cachep)
8051 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008052}
8053
8054int btrfs_init_cachep(void)
8055{
David Sterba837e1972012-09-07 03:00:48 -06008056 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008057 sizeof(struct btrfs_inode), 0,
8058 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008059 if (!btrfs_inode_cachep)
8060 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008061
David Sterba837e1972012-09-07 03:00:48 -06008062 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008063 sizeof(struct btrfs_trans_handle), 0,
8064 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008065 if (!btrfs_trans_handle_cachep)
8066 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008067
David Sterba837e1972012-09-07 03:00:48 -06008068 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008069 sizeof(struct btrfs_transaction), 0,
8070 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008071 if (!btrfs_transaction_cachep)
8072 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008073
David Sterba837e1972012-09-07 03:00:48 -06008074 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008075 sizeof(struct btrfs_path), 0,
8076 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008077 if (!btrfs_path_cachep)
8078 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008079
David Sterba837e1972012-09-07 03:00:48 -06008080 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008081 sizeof(struct btrfs_free_space), 0,
8082 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8083 if (!btrfs_free_space_cachep)
8084 goto fail;
8085
Miao Xie8ccf6f192012-10-25 09:28:04 +00008086 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8087 sizeof(struct btrfs_delalloc_work), 0,
8088 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8089 NULL);
8090 if (!btrfs_delalloc_work_cachep)
8091 goto fail;
8092
Chris Mason39279cc2007-06-12 06:35:45 -04008093 return 0;
8094fail:
8095 btrfs_destroy_cachep();
8096 return -ENOMEM;
8097}
8098
8099static int btrfs_getattr(struct vfsmount *mnt,
8100 struct dentry *dentry, struct kstat *stat)
8101{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008102 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008103 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008104 u32 blocksize = inode->i_sb->s_blocksize;
8105
Chris Mason39279cc2007-06-12 06:35:45 -04008106 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008107 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008108 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008109
8110 spin_lock(&BTRFS_I(inode)->lock);
8111 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8112 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008113 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008114 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008115 return 0;
8116}
8117
Chris Masond3977122009-01-05 21:25:51 -05008118static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8119 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008120{
8121 struct btrfs_trans_handle *trans;
8122 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008123 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008124 struct inode *new_inode = new_dentry->d_inode;
8125 struct inode *old_inode = old_dentry->d_inode;
8126 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008127 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008128 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008129 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008130 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008131
Li Zefan33345d012011-04-20 10:31:50 +08008132 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008133 return -EPERM;
8134
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008135 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008136 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008137 return -EXDEV;
8138
Li Zefan33345d012011-04-20 10:31:50 +08008139 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8140 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008141 return -ENOTEMPTY;
8142
Chris Mason39279cc2007-06-12 06:35:45 -04008143 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008144 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008145 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008146
8147
8148 /* check for collisions, even if the name isn't there */
8149 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8150 new_dentry->d_name.name,
8151 new_dentry->d_name.len);
8152
8153 if (ret) {
8154 if (ret == -EEXIST) {
8155 /* we shouldn't get
8156 * eexist without a new_inode */
8157 if (!new_inode) {
8158 WARN_ON(1);
8159 return ret;
8160 }
8161 } else {
8162 /* maybe -EOVERFLOW */
8163 return ret;
8164 }
8165 }
8166 ret = 0;
8167
Chris Mason5a3f23d2009-03-31 13:27:11 -04008168 /*
8169 * we're using rename to replace one file with another.
8170 * and the replacement file is large. Start IO on it now so
8171 * we don't add too much work to the end of the transaction
8172 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008173 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008174 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8175 filemap_flush(old_inode->i_mapping);
8176
Yan, Zheng76dda932009-09-21 16:00:26 -04008177 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008178 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008179 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008180 /*
8181 * We want to reserve the absolute worst case amount of items. So if
8182 * both inodes are subvols and we need to unlink them then that would
8183 * require 4 item modifications, but if they are both normal inodes it
8184 * would require 5 item modifications, so we'll assume their normal
8185 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8186 * should cover the worst case number of items we'll modify.
8187 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008188 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008189 if (IS_ERR(trans)) {
8190 ret = PTR_ERR(trans);
8191 goto out_notrans;
8192 }
Chris Mason5f39d392007-10-15 16:14:19 -04008193
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008194 if (dest != root)
8195 btrfs_record_root_in_trans(trans, dest);
8196
Yan, Zhenga5719522009-09-24 09:17:31 -04008197 ret = btrfs_set_inode_index(new_dir, &index);
8198 if (ret)
8199 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008200
Li Zefan33345d012011-04-20 10:31:50 +08008201 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008202 /* force full log commit if subvolume involved. */
8203 root->fs_info->last_trans_log_full_commit = trans->transid;
8204 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008205 ret = btrfs_insert_inode_ref(trans, dest,
8206 new_dentry->d_name.name,
8207 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008208 old_ino,
8209 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008210 if (ret)
8211 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008212 /*
8213 * this is an ugly little race, but the rename is required
8214 * to make sure that if we crash, the inode is either at the
8215 * old name or the new one. pinning the log transaction lets
8216 * us make sure we don't allow a log commit to come in after
8217 * we unlink the name but before we add the new name back in.
8218 */
8219 btrfs_pin_log_trans(root);
8220 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008221 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008222 * make sure the inode gets flushed if it is replacing
8223 * something.
8224 */
Li Zefan33345d012011-04-20 10:31:50 +08008225 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008226 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008227
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008228 inode_inc_iversion(old_dir);
8229 inode_inc_iversion(new_dir);
8230 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008231 old_dir->i_ctime = old_dir->i_mtime = ctime;
8232 new_dir->i_ctime = new_dir->i_mtime = ctime;
8233 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008234
Chris Mason12fcfd22009-03-24 10:24:20 -04008235 if (old_dentry->d_parent != new_dentry->d_parent)
8236 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8237
Li Zefan33345d012011-04-20 10:31:50 +08008238 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008239 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8240 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8241 old_dentry->d_name.name,
8242 old_dentry->d_name.len);
8243 } else {
Al Viro92986792011-03-04 17:14:37 +00008244 ret = __btrfs_unlink_inode(trans, root, old_dir,
8245 old_dentry->d_inode,
8246 old_dentry->d_name.name,
8247 old_dentry->d_name.len);
8248 if (!ret)
8249 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008250 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008251 if (ret) {
8252 btrfs_abort_transaction(trans, root, ret);
8253 goto out_fail;
8254 }
Chris Mason39279cc2007-06-12 06:35:45 -04008255
8256 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008257 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008258 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008259 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008260 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8261 root_objectid = BTRFS_I(new_inode)->location.objectid;
8262 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8263 root_objectid,
8264 new_dentry->d_name.name,
8265 new_dentry->d_name.len);
8266 BUG_ON(new_inode->i_nlink == 0);
8267 } else {
8268 ret = btrfs_unlink_inode(trans, dest, new_dir,
8269 new_dentry->d_inode,
8270 new_dentry->d_name.name,
8271 new_dentry->d_name.len);
8272 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008273 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008274 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008275 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008276 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008277 if (ret) {
8278 btrfs_abort_transaction(trans, root, ret);
8279 goto out_fail;
8280 }
Chris Mason39279cc2007-06-12 06:35:45 -04008281 }
Josef Bacikaec74772008-07-24 12:12:38 -04008282
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008283 ret = btrfs_add_link(trans, new_dir, old_inode,
8284 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008285 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008286 if (ret) {
8287 btrfs_abort_transaction(trans, root, ret);
8288 goto out_fail;
8289 }
Chris Mason39279cc2007-06-12 06:35:45 -04008290
Li Zefan33345d012011-04-20 10:31:50 +08008291 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008292 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008293 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008294 btrfs_end_log_trans(root);
8295 }
Chris Mason39279cc2007-06-12 06:35:45 -04008296out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008297 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008298out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008299 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008300 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008301
Chris Mason39279cc2007-06-12 06:35:45 -04008302 return ret;
8303}
8304
Miao Xie8ccf6f192012-10-25 09:28:04 +00008305static void btrfs_run_delalloc_work(struct btrfs_work *work)
8306{
8307 struct btrfs_delalloc_work *delalloc_work;
8308
8309 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8310 work);
8311 if (delalloc_work->wait)
8312 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8313 else
8314 filemap_flush(delalloc_work->inode->i_mapping);
8315
8316 if (delalloc_work->delay_iput)
8317 btrfs_add_delayed_iput(delalloc_work->inode);
8318 else
8319 iput(delalloc_work->inode);
8320 complete(&delalloc_work->completion);
8321}
8322
8323struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8324 int wait, int delay_iput)
8325{
8326 struct btrfs_delalloc_work *work;
8327
8328 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8329 if (!work)
8330 return NULL;
8331
8332 init_completion(&work->completion);
8333 INIT_LIST_HEAD(&work->list);
8334 work->inode = inode;
8335 work->wait = wait;
8336 work->delay_iput = delay_iput;
8337 work->work.func = btrfs_run_delalloc_work;
8338
8339 return work;
8340}
8341
8342void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8343{
8344 wait_for_completion(&work->completion);
8345 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8346}
8347
Chris Masond352ac62008-09-29 15:18:18 -04008348/*
8349 * some fairly slow code that needs optimization. This walks the list
8350 * of all the inodes with pending delalloc and forces them to disk.
8351 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008352int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008353{
Chris Masonea8c2812008-08-04 23:17:27 -04008354 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008355 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008356 struct btrfs_delalloc_work *work, *next;
8357 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008358 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008359 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008360
Yan Zhengc146afa2008-11-12 14:34:12 -05008361 if (root->fs_info->sb->s_flags & MS_RDONLY)
8362 return -EROFS;
8363
Miao Xie8ccf6f192012-10-25 09:28:04 +00008364 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008365 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008366
Chris Mason75eff682008-12-15 15:54:40 -05008367 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008368 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8369 while (!list_empty(&splice)) {
8370 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008371 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008372
8373 list_del_init(&binode->delalloc_inodes);
8374
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008375 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008376 if (!inode) {
8377 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8378 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008379 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008380 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008381
8382 list_add_tail(&binode->delalloc_inodes,
8383 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008384 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008385
8386 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8387 if (unlikely(!work)) {
8388 ret = -ENOMEM;
8389 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008390 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008391 list_add_tail(&work->list, &works);
8392 btrfs_queue_worker(&root->fs_info->flush_workers,
8393 &work->work);
8394
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008395 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008396 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008397 }
Chris Mason75eff682008-12-15 15:54:40 -05008398 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008399
Miao Xie1eafa6c2013-01-22 10:49:00 +00008400 list_for_each_entry_safe(work, next, &works, list) {
8401 list_del_init(&work->list);
8402 btrfs_wait_and_free_delalloc_work(work);
8403 }
8404
Chris Mason8c8bee12008-09-29 11:19:10 -04008405 /* the filemap_flush will queue IO into the worker threads, but
8406 * we have to make sure the IO is actually started and that
8407 * ordered extents get created before we return
8408 */
8409 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008410 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008411 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008412 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008413 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8414 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008415 }
8416 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008417 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008418out:
8419 list_for_each_entry_safe(work, next, &works, list) {
8420 list_del_init(&work->list);
8421 btrfs_wait_and_free_delalloc_work(work);
8422 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008423
8424 if (!list_empty_careful(&splice)) {
8425 spin_lock(&root->fs_info->delalloc_lock);
8426 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8427 spin_unlock(&root->fs_info->delalloc_lock);
8428 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008429 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008430}
8431
Chris Mason39279cc2007-06-12 06:35:45 -04008432static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8433 const char *symname)
8434{
8435 struct btrfs_trans_handle *trans;
8436 struct btrfs_root *root = BTRFS_I(dir)->root;
8437 struct btrfs_path *path;
8438 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008439 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008440 int err;
8441 int drop_inode = 0;
8442 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008443 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008444 int name_len;
8445 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008446 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008447 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008448 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008449
8450 name_len = strlen(symname) + 1;
8451 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8452 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008453
Josef Bacik9ed74f22009-09-11 16:12:44 -04008454 /*
8455 * 2 items for inode item and ref
8456 * 2 items for dir items
8457 * 1 item for xattr if selinux is on
8458 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008459 trans = btrfs_start_transaction(root, 5);
8460 if (IS_ERR(trans))
8461 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008462
Li Zefan581bb052011-04-20 10:06:11 +08008463 err = btrfs_find_free_ino(root, &objectid);
8464 if (err)
8465 goto out_unlock;
8466
Josef Bacikaec74772008-07-24 12:12:38 -04008467 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008468 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008469 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008470 if (IS_ERR(inode)) {
8471 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008472 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008473 }
Chris Mason39279cc2007-06-12 06:35:45 -04008474
Eric Paris2a7dba32011-02-01 11:05:39 -05008475 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008476 if (err) {
8477 drop_inode = 1;
8478 goto out_unlock;
8479 }
8480
Casey Schauflerad19db72011-12-15 10:09:07 -05008481 /*
8482 * If the active LSM wants to access the inode during
8483 * d_instantiate it needs these. Smack checks to see
8484 * if the filesystem supports xattrs by looking at the
8485 * ops vector.
8486 */
8487 inode->i_fop = &btrfs_file_operations;
8488 inode->i_op = &btrfs_file_inode_operations;
8489
Josef Bacika1b075d2010-11-19 20:36:11 +00008490 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008491 if (err)
8492 drop_inode = 1;
8493 else {
8494 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008495 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008496 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008497 }
Chris Mason39279cc2007-06-12 06:35:45 -04008498 if (drop_inode)
8499 goto out_unlock;
8500
8501 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008502 if (!path) {
8503 err = -ENOMEM;
8504 drop_inode = 1;
8505 goto out_unlock;
8506 }
Li Zefan33345d012011-04-20 10:31:50 +08008507 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008508 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008509 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8510 datasize = btrfs_file_extent_calc_inline_size(name_len);
8511 err = btrfs_insert_empty_item(trans, root, path, &key,
8512 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008513 if (err) {
8514 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008515 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008516 goto out_unlock;
8517 }
Chris Mason5f39d392007-10-15 16:14:19 -04008518 leaf = path->nodes[0];
8519 ei = btrfs_item_ptr(leaf, path->slots[0],
8520 struct btrfs_file_extent_item);
8521 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8522 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008523 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008524 btrfs_set_file_extent_encryption(leaf, ei, 0);
8525 btrfs_set_file_extent_compression(leaf, ei, 0);
8526 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8527 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8528
Chris Mason39279cc2007-06-12 06:35:45 -04008529 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008530 write_extent_buffer(leaf, symname, ptr, name_len);
8531 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008532 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008533
Chris Mason39279cc2007-06-12 06:35:45 -04008534 inode->i_op = &btrfs_symlink_inode_operations;
8535 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008536 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008537 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008538 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008539 err = btrfs_update_inode(trans, root, inode);
8540 if (err)
8541 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008542
8543out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008544 if (!err)
8545 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008546 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008547 if (drop_inode) {
8548 inode_dec_link_count(inode);
8549 iput(inode);
8550 }
Liu Bob53d3f52012-11-14 14:34:34 +00008551 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008552 return err;
8553}
Chris Mason16432982008-04-10 10:23:21 -04008554
Josef Bacik0af3d002010-06-21 14:48:16 -04008555static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8556 u64 start, u64 num_bytes, u64 min_size,
8557 loff_t actual_len, u64 *alloc_hint,
8558 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008559{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008560 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8561 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008562 struct btrfs_root *root = BTRFS_I(inode)->root;
8563 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008564 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008565 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008566 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008567 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008568 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008569
Josef Bacik0af3d002010-06-21 14:48:16 -04008570 if (trans)
8571 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008572 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008573 if (own_trans) {
8574 trans = btrfs_start_transaction(root, 3);
8575 if (IS_ERR(trans)) {
8576 ret = PTR_ERR(trans);
8577 break;
8578 }
Yan Zhengd899e052008-10-30 14:25:28 -04008579 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008580
Chris Mason154ea282013-03-05 11:11:26 -05008581 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8582 cur_bytes = max(cur_bytes, min_size);
8583 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008584 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008585 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008586 if (own_trans)
8587 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008588 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008589 }
8590
Yan Zhengd899e052008-10-30 14:25:28 -04008591 ret = insert_reserved_file_extent(trans, inode,
8592 cur_offset, ins.objectid,
8593 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008594 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008595 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008596 if (ret) {
8597 btrfs_abort_transaction(trans, root, ret);
8598 if (own_trans)
8599 btrfs_end_transaction(trans, root);
8600 break;
8601 }
Chris Masona1ed8352009-09-11 12:27:37 -04008602 btrfs_drop_extent_cache(inode, cur_offset,
8603 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008604
Josef Bacik5dc562c2012-08-17 13:14:17 -04008605 em = alloc_extent_map();
8606 if (!em) {
8607 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8608 &BTRFS_I(inode)->runtime_flags);
8609 goto next;
8610 }
8611
8612 em->start = cur_offset;
8613 em->orig_start = cur_offset;
8614 em->len = ins.offset;
8615 em->block_start = ins.objectid;
8616 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008617 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008618 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008619 em->bdev = root->fs_info->fs_devices->latest_bdev;
8620 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8621 em->generation = trans->transid;
8622
8623 while (1) {
8624 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008625 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008626 write_unlock(&em_tree->lock);
8627 if (ret != -EEXIST)
8628 break;
8629 btrfs_drop_extent_cache(inode, cur_offset,
8630 cur_offset + ins.offset - 1,
8631 0);
8632 }
8633 free_extent_map(em);
8634next:
Yan Zhengd899e052008-10-30 14:25:28 -04008635 num_bytes -= ins.offset;
8636 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008637 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008638
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008639 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008640 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008641 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008642 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008643 (actual_len > inode->i_size) &&
8644 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008645 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008646 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008647 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008648 i_size = cur_offset;
8649 i_size_write(inode, i_size);
8650 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008651 }
8652
Yan Zhengd899e052008-10-30 14:25:28 -04008653 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008654
8655 if (ret) {
8656 btrfs_abort_transaction(trans, root, ret);
8657 if (own_trans)
8658 btrfs_end_transaction(trans, root);
8659 break;
8660 }
Yan Zhengd899e052008-10-30 14:25:28 -04008661
Josef Bacik0af3d002010-06-21 14:48:16 -04008662 if (own_trans)
8663 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008664 }
Yan Zhengd899e052008-10-30 14:25:28 -04008665 return ret;
8666}
8667
Josef Bacik0af3d002010-06-21 14:48:16 -04008668int btrfs_prealloc_file_range(struct inode *inode, int mode,
8669 u64 start, u64 num_bytes, u64 min_size,
8670 loff_t actual_len, u64 *alloc_hint)
8671{
8672 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8673 min_size, actual_len, alloc_hint,
8674 NULL);
8675}
8676
8677int btrfs_prealloc_file_range_trans(struct inode *inode,
8678 struct btrfs_trans_handle *trans, int mode,
8679 u64 start, u64 num_bytes, u64 min_size,
8680 loff_t actual_len, u64 *alloc_hint)
8681{
8682 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8683 min_size, actual_len, alloc_hint, trans);
8684}
8685
Chris Masone6dcd2d2008-07-17 12:53:50 -04008686static int btrfs_set_page_dirty(struct page *page)
8687{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008688 return __set_page_dirty_nobuffers(page);
8689}
8690
Al Viro10556cb2011-06-20 19:28:19 -04008691static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008692{
Li Zefanb83cc962010-12-20 16:04:08 +08008693 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008694 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008695
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008696 if (mask & MAY_WRITE &&
8697 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8698 if (btrfs_root_readonly(root))
8699 return -EROFS;
8700 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8701 return -EACCES;
8702 }
Al Viro2830ba72011-06-20 19:16:29 -04008703 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008704}
Chris Mason39279cc2007-06-12 06:35:45 -04008705
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008706static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008707 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008708 .lookup = btrfs_lookup,
8709 .create = btrfs_create,
8710 .unlink = btrfs_unlink,
8711 .link = btrfs_link,
8712 .mkdir = btrfs_mkdir,
8713 .rmdir = btrfs_rmdir,
8714 .rename = btrfs_rename,
8715 .symlink = btrfs_symlink,
8716 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008717 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008718 .setxattr = btrfs_setxattr,
8719 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008720 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008721 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008722 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008723 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008724};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008725static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008726 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008727 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008728 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008729};
Yan, Zheng76dda932009-09-21 16:00:26 -04008730
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008731static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008732 .llseek = generic_file_llseek,
8733 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008734 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008735 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008736#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008737 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008738#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008739 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008740 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008741};
8742
Chris Masond1310b22008-01-24 16:13:08 -05008743static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008744 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008745 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008746 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008747 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008748 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008749 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008750 .set_bit_hook = btrfs_set_bit_hook,
8751 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008752 .merge_extent_hook = btrfs_merge_extent_hook,
8753 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008754};
8755
Chris Mason35054392009-01-21 13:11:13 -05008756/*
8757 * btrfs doesn't support the bmap operation because swapfiles
8758 * use bmap to make a mapping of extents in the file. They assume
8759 * these extents won't change over the life of the file and they
8760 * use the bmap result to do IO directly to the drive.
8761 *
8762 * the btrfs bmap call would return logical addresses that aren't
8763 * suitable for IO and they also will change frequently as COW
8764 * operations happen. So, swapfile + btrfs == corruption.
8765 *
8766 * For now we're avoiding this by dropping bmap.
8767 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008768static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008769 .readpage = btrfs_readpage,
8770 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008771 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008772 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008773 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008774 .invalidatepage = btrfs_invalidatepage,
8775 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008776 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008777 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008778};
8779
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008780static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008781 .readpage = btrfs_readpage,
8782 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008783 .invalidatepage = btrfs_invalidatepage,
8784 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008785};
8786
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008787static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008788 .getattr = btrfs_getattr,
8789 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008790 .setxattr = btrfs_setxattr,
8791 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008792 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008793 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008794 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008795 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008796 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008797 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008798};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008799static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008800 .getattr = btrfs_getattr,
8801 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008802 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008803 .setxattr = btrfs_setxattr,
8804 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008805 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008806 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008807 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008808 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008809};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008810static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008811 .readlink = generic_readlink,
8812 .follow_link = page_follow_link_light,
8813 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008814 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008815 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008816 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008817 .setxattr = btrfs_setxattr,
8818 .getxattr = btrfs_getxattr,
8819 .listxattr = btrfs_listxattr,
8820 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008821 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008822 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008823};
Yan, Zheng76dda932009-09-21 16:00:26 -04008824
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008825const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008826 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008827 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008828};