blob: fce61991213c9284191244b75427f1465c38fbc4 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044#include "ctree.h"
45#include "disk-io.h"
46#include "transaction.h"
47#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000074static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_trans_handle_cachep;
76struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80#define S_SHIFT 12
81static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89};
90
Eric Sandeen3972f262013-01-12 02:57:22 +000091static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050092static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040093static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050094static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
96 u64 start, u64 end, int *page_started,
97 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -040098static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
99 u64 len, u64 orig_start,
100 u64 block_start, u64 block_len,
101 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400102
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500104 struct inode *inode, struct inode *dir,
105 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500106{
107 int err;
108
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000109 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500110 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500112 return err;
113}
114
Chris Masond352ac62008-09-29 15:18:18 -0400115/*
Chris Masonc8b97812008-10-29 14:49:59 -0400116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
119 */
Chris Masond3977122009-01-05 21:25:51 -0500120static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct btrfs_root *root, struct inode *inode,
122 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000123 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400124 struct page **compressed_pages)
125{
126 struct btrfs_key key;
127 struct btrfs_path *path;
128 struct extent_buffer *leaf;
129 struct page *page = NULL;
130 char *kaddr;
131 unsigned long ptr;
132 struct btrfs_file_extent_item *ei;
133 int err = 0;
134 int ret;
135 size_t cur_size = size;
136 size_t datasize;
137 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Li Zefanfe3f5662011-03-28 08:30:38 +0000139 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400140 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400141
Chris Masond3977122009-01-05 21:25:51 -0500142 path = btrfs_alloc_path();
143 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400144 return -ENOMEM;
145
Chris Masonb9473432009-03-13 11:00:37 -0400146 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Li Zefan33345d012011-04-20 10:31:50 +0800148 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400149 key.offset = start;
150 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 datasize = btrfs_file_extent_calc_inline_size(cur_size);
152
153 inode_add_bytes(inode, size);
154 ret = btrfs_insert_empty_item(trans, root, path, &key,
155 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 if (ret) {
157 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 goto fail;
159 }
160 leaf = path->nodes[0];
161 ei = btrfs_item_ptr(leaf, path->slots[0],
162 struct btrfs_file_extent_item);
163 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
164 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
165 btrfs_set_file_extent_encryption(leaf, ei, 0);
166 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
167 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
168 ptr = btrfs_file_extent_inline_start(ei);
169
Li Zefan261507a02010-12-17 14:21:50 +0800170 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page *cpage;
172 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500173 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400174 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500175 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400176 PAGE_CACHE_SIZE);
177
Cong Wang7ac687d2011-11-25 23:14:28 +0800178 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400179 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800180 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400181
182 i++;
183 ptr += cur_size;
184 compressed_size -= cur_size;
185 }
186 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800187 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400188 } else {
189 page = find_get_page(inode->i_mapping,
190 start >> PAGE_CACHE_SHIFT);
191 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 offset = start & (PAGE_CACHE_SIZE - 1);
194 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 page_cache_release(page);
197 }
198 btrfs_mark_buffer_dirty(leaf);
199 btrfs_free_path(path);
200
Yan, Zhengc2167752009-11-12 09:34:21 +0000201 /*
202 * we're an inline extent, so nobody can
203 * extend the file past i_size without locking
204 * a page we already have locked.
205 *
206 * We must do any isize and inode updates
207 * before we unlock the pages. Otherwise we
208 * could end up racing with unlink.
209 */
Chris Masonc8b97812008-10-29 14:49:59 -0400210 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100211 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000212
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100213 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400214fail:
215 btrfs_free_path(path);
216 return err;
217}
218
219
220/*
221 * conditionally insert an inline extent into the file. This
222 * does the checks required to make sure the data is small enough
223 * to fit as an inline extent.
224 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400225static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct btrfs_root *root,
227 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000228 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400229 struct page **compressed_pages)
230{
231 u64 isize = i_size_read(inode);
232 u64 actual_end = min(end + 1, isize);
233 u64 inline_len = actual_end - start;
234 u64 aligned_end = (end + root->sectorsize - 1) &
235 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 u64 data_len = inline_len;
237 int ret;
238
239 if (compressed_size)
240 data_len = compressed_size;
241
242 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400243 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400244 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
245 (!compressed_size &&
246 (actual_end & (root->sectorsize - 1)) == 0) ||
247 end + 1 < isize ||
248 data_len > root->fs_info->max_inline) {
249 return 1;
250 }
251
Josef Bacik26714852012-08-29 12:24:27 -0400252 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100253 if (ret)
254 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400255
256 if (isize > actual_end)
257 inline_len = min_t(u64, isize, actual_end);
258 ret = insert_inline_extent(trans, root, inode, start,
259 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000260 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400261 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100262 btrfs_abort_transaction(trans, root, ret);
263 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400264 } else if (ret == -ENOSPC) {
265 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400267
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400268 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400269 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400270 return 0;
271}
272
Chris Mason771ed682008-11-06 22:02:51 -0500273struct async_extent {
274 u64 start;
275 u64 ram_size;
276 u64 compressed_size;
277 struct page **pages;
278 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800279 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500280 struct list_head list;
281};
282
283struct async_cow {
284 struct inode *inode;
285 struct btrfs_root *root;
286 struct page *locked_page;
287 u64 start;
288 u64 end;
289 struct list_head extents;
290 struct btrfs_work work;
291};
292
293static noinline int add_async_extent(struct async_cow *cow,
294 u64 start, u64 ram_size,
295 u64 compressed_size,
296 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800297 unsigned long nr_pages,
298 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500299{
300 struct async_extent *async_extent;
301
302 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100303 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500304 async_extent->start = start;
305 async_extent->ram_size = ram_size;
306 async_extent->compressed_size = compressed_size;
307 async_extent->pages = pages;
308 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800309 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500310 list_add_tail(&async_extent->list, &cow->extents);
311 return 0;
312}
313
Chris Masonc8b97812008-10-29 14:49:59 -0400314/*
Chris Mason771ed682008-11-06 22:02:51 -0500315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400318 *
Chris Mason771ed682008-11-06 22:02:51 -0500319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400324 *
Chris Mason771ed682008-11-06 22:02:51 -0500325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300328 * are written in the same order that the flusher thread sent them
329 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400330 */
Chris Mason771ed682008-11-06 22:02:51 -0500331static noinline int compress_file_range(struct inode *inode,
332 struct page *locked_page,
333 u64 start, u64 end,
334 struct async_cow *async_cow,
335 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400336{
337 struct btrfs_root *root = BTRFS_I(inode)->root;
338 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400339 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400340 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400341 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500342 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400343 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400344 struct page **pages = NULL;
345 unsigned long nr_pages;
346 unsigned long nr_pages_ret = 0;
347 unsigned long total_compressed = 0;
348 unsigned long total_in = 0;
349 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500350 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400351 int i;
352 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800353 int compress_type = root->fs_info->compress_type;
Chris Masonb888db2b2007-08-27 16:49:44 -0400354
Liu Bo4cb13e52012-03-29 09:57:45 -0400355 /* if this is a small write inside eof, kick off a defrag */
356 if ((end - start + 1) < 16 * 1024 &&
357 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400358 btrfs_add_inode_defrag(NULL, inode);
359
Chris Mason42dc7ba2008-12-15 11:44:56 -0500360 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400361again:
362 will_compress = 0;
363 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
364 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
365
Chris Masonf03d9301f2009-02-04 09:31:06 -0500366 /*
367 * we don't want to send crud past the end of i_size through
368 * compression, that's just a waste of CPU time. So, if the
369 * end of the file is before the start of our current
370 * requested range of bytes, we bail out to the uncompressed
371 * cleanup code that can deal with all of this.
372 *
373 * It isn't really the fastest way to fix things, but this is a
374 * very uncommon corner.
375 */
376 if (actual_end <= start)
377 goto cleanup_and_bail_uncompressed;
378
Chris Masonc8b97812008-10-29 14:49:59 -0400379 total_compressed = actual_end - start;
380
381 /* we want to make sure that amount of ram required to uncompress
382 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500383 * of a compressed extent to 128k. This is a crucial number
384 * because it also controls how easily we can spread reads across
385 * cpus for decompression.
386 *
387 * We also want to make sure the amount of IO required to do
388 * a random read is reasonably small, so we limit the size of
389 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400390 */
391 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400392 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500393 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400394 total_in = 0;
395 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400396
Chris Mason771ed682008-11-06 22:02:51 -0500397 /*
398 * we do compression for mount -o compress and when the
399 * inode has not been flagged as nocompress. This flag can
400 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200402 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500403 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000404 (BTRFS_I(inode)->force_compress) ||
405 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400406 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400407 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800408 if (!pages) {
409 /* just bail out to the uncompressed code */
410 goto cont;
411 }
Chris Mason179e29e2007-11-01 11:28:41 -0400412
Li Zefan261507a02010-12-17 14:21:50 +0800413 if (BTRFS_I(inode)->force_compress)
414 compress_type = BTRFS_I(inode)->force_compress;
415
416 ret = btrfs_compress_pages(compress_type,
417 inode->i_mapping, start,
418 total_compressed, pages,
419 nr_pages, &nr_pages_ret,
420 &total_in,
421 &total_compressed,
422 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400423
424 if (!ret) {
425 unsigned long offset = total_compressed &
426 (PAGE_CACHE_SIZE - 1);
427 struct page *page = pages[nr_pages_ret - 1];
428 char *kaddr;
429
430 /* zero the tail end of the last page, we might be
431 * sending it down to disk
432 */
433 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800434 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400435 memset(kaddr + offset, 0,
436 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800437 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400438 }
439 will_compress = 1;
440 }
441 }
Li Zefan560f7d72011-09-08 10:22:01 +0800442cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400443 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400444 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100445 if (IS_ERR(trans)) {
446 ret = PTR_ERR(trans);
447 trans = NULL;
448 goto cleanup_and_out;
449 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400450 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500451
Chris Masonc8b97812008-10-29 14:49:59 -0400452 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500453 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400454 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500455 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400456 */
457 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000458 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400459 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500460 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400461 ret = cow_file_range_inline(trans, root, inode,
462 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000463 total_compressed,
464 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400465 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100466 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500467 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100468 * inline extent creation worked or returned error,
469 * we don't need to create any more async work items.
470 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500471 */
Chris Masonc8b97812008-10-29 14:49:59 -0400472 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400473 &BTRFS_I(inode)->io_tree,
474 start, end, NULL,
475 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400476 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400477 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000478
479 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 goto free_pages_out;
481 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000482 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400483 }
484
485 if (will_compress) {
486 /*
487 * we aren't doing an inline extent round the compressed size
488 * up to a block size boundary so the allocator does sane
489 * things
490 */
491 total_compressed = (total_compressed + blocksize - 1) &
492 ~(blocksize - 1);
493
494 /*
495 * one last check to make sure the compression is really a
496 * win, compare the page count read with the blocks on disk
497 */
498 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
499 ~(PAGE_CACHE_SIZE - 1);
500 if (total_compressed >= total_in) {
501 will_compress = 0;
502 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 num_bytes = total_in;
504 }
505 }
506 if (!will_compress && pages) {
507 /*
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
510 */
511 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400512 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 page_cache_release(pages[i]);
514 }
515 kfree(pages);
516 pages = NULL;
517 total_compressed = 0;
518 nr_pages_ret = 0;
519
520 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500521 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
522 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500523 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500524 }
Chris Masonc8b97812008-10-29 14:49:59 -0400525 }
Chris Mason771ed682008-11-06 22:02:51 -0500526 if (will_compress) {
527 *num_added += 1;
528
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
532 */
533 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800534 total_compressed, pages, nr_pages_ret,
535 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500536
Yan, Zheng24ae6362010-12-06 07:02:36 +0000537 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500538 start += num_bytes;
539 pages = NULL;
540 cond_resched();
541 goto again;
542 }
543 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500544cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500545 /*
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
551 */
552 if (page_offset(locked_page) >= start &&
553 page_offset(locked_page) <= end) {
554 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */
556 }
Li Zefan261507a02010-12-17 14:21:50 +0800557 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500559 *num_added += 1;
560 }
561
562out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100563 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500564
565free_pages_out:
566 for (i = 0; i < nr_pages_ret; i++) {
567 WARN_ON(pages[i]->mapping);
568 page_cache_release(pages[i]);
569 }
Chris Masond3977122009-01-05 21:25:51 -0500570 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500571
572 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573
574cleanup_and_out:
575 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
576 start, end, NULL,
577 EXTENT_CLEAR_UNLOCK_PAGE |
578 EXTENT_CLEAR_DIRTY |
579 EXTENT_CLEAR_DELALLOC |
580 EXTENT_SET_WRITEBACK |
581 EXTENT_END_WRITEBACK);
582 if (!trans || IS_ERR(trans))
583 btrfs_error(root->fs_info, ret, "Failed to join transaction");
584 else
585 btrfs_abort_transaction(trans, root, ret);
586 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500587}
588
589/*
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
594 */
595static noinline int submit_compressed_extents(struct inode *inode,
596 struct async_cow *async_cow)
597{
598 struct async_extent *async_extent;
599 u64 alloc_hint = 0;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_key ins;
602 struct extent_map *em;
603 struct btrfs_root *root = BTRFS_I(inode)->root;
604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
605 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500606 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 if (list_empty(&async_cow->extents))
609 return 0;
610
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500611again:
Chris Masond3977122009-01-05 21:25:51 -0500612 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500613 async_extent = list_entry(async_cow->extents.next,
614 struct async_extent, list);
615 list_del(&async_extent->list);
616
617 io_tree = &BTRFS_I(inode)->io_tree;
618
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500619retry:
Chris Mason771ed682008-11-06 22:02:51 -0500620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent->pages) {
622 int page_started = 0;
623 unsigned long nr_written = 0;
624
625 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100627 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500628
629 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 ret = cow_file_range(inode, async_cow->locked_page,
631 async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1,
634 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500635
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 /* JDM XXX */
637
Chris Mason771ed682008-11-06 22:02:51 -0500638 /*
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
643 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500644 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500645 extent_write_locked_range(io_tree,
646 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500648 async_extent->ram_size - 1,
649 btrfs_get_extent,
650 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500651 else if (ret)
652 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500653 kfree(async_extent);
654 cond_resched();
655 continue;
656 }
657
658 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100659 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500660
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400661 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100662 if (IS_ERR(trans)) {
663 ret = PTR_ERR(trans);
664 } else {
665 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
666 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500667 async_extent->compressed_size,
668 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500669 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600670 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 btrfs_abort_transaction(trans, root, ret);
672 btrfs_end_transaction(trans, root);
673 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000674
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500675 if (ret) {
676 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500677
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500678 for (i = 0; i < async_extent->nr_pages; i++) {
679 WARN_ON(async_extent->pages[i]->mapping);
680 page_cache_release(async_extent->pages[i]);
681 }
682 kfree(async_extent->pages);
683 async_extent->nr_pages = 0;
684 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500685
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 if (ret == -ENOSPC)
687 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500688 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 }
690
Yan, Zhengc2167752009-11-12 09:34:21 +0000691 /*
692 * here we're doing allocation and writeback of the
693 * compressed pages
694 */
695 btrfs_drop_extent_cache(inode, async_extent->start,
696 async_extent->start +
697 async_extent->ram_size - 1, 0);
698
David Sterba172ddd62011-04-21 00:48:27 +0200699 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500700 if (!em)
701 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500702 em->start = async_extent->start;
703 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500704 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400705 em->mod_start = em->start;
706 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500707
708 em->block_start = ins.objectid;
709 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500710 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500711 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800712 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500713 set_bit(EXTENT_FLAG_PINNED, &em->flags);
714 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400715 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500716
Chris Masond3977122009-01-05 21:25:51 -0500717 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400718 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500719 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400720 if (!ret)
721 list_move(&em->list,
722 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400723 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500724 if (ret != -EEXIST) {
725 free_extent_map(em);
726 break;
727 }
728 btrfs_drop_extent_cache(inode, async_extent->start,
729 async_extent->start +
730 async_extent->ram_size - 1, 0);
731 }
732
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500733 if (ret)
734 goto out_free_reserve;
735
Li Zefan261507a02010-12-17 14:21:50 +0800736 ret = btrfs_add_ordered_extent_compress(inode,
737 async_extent->start,
738 ins.objectid,
739 async_extent->ram_size,
740 ins.offset,
741 BTRFS_ORDERED_COMPRESSED,
742 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500743 if (ret)
744 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500745
Chris Mason771ed682008-11-06 22:02:51 -0500746 /*
747 * clear dirty, set writeback and unlock the pages.
748 */
749 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400750 &BTRFS_I(inode)->io_tree,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
754 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
755 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400756 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400757 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500758
759 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500760 async_extent->start,
761 async_extent->ram_size,
762 ins.objectid,
763 ins.offset, async_extent->pages,
764 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500765 alloc_hint = ins.objectid + ins.offset;
766 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 if (ret)
768 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500769 cond_resched();
770 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771 ret = 0;
772out:
773 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774out_free_reserve:
775 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100776out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
778 async_extent->start,
779 async_extent->start +
780 async_extent->ram_size - 1,
781 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
782 EXTENT_CLEAR_UNLOCK |
783 EXTENT_CLEAR_DELALLOC |
784 EXTENT_CLEAR_DIRTY |
785 EXTENT_SET_WRITEBACK |
786 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100787 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500789}
790
Josef Bacik4b46fce2010-05-23 11:00:55 -0400791static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
792 u64 num_bytes)
793{
794 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
795 struct extent_map *em;
796 u64 alloc_hint = 0;
797
798 read_lock(&em_tree->lock);
799 em = search_extent_mapping(em_tree, start, num_bytes);
800 if (em) {
801 /*
802 * if block start isn't an actual block number then find the
803 * first block in this inode and use that as a hint. If that
804 * block is also bogus then just don't worry about it.
805 */
806 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
807 free_extent_map(em);
808 em = search_extent_mapping(em_tree, 0, 0);
809 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
810 alloc_hint = em->block_start;
811 if (em)
812 free_extent_map(em);
813 } else {
814 alloc_hint = em->block_start;
815 free_extent_map(em);
816 }
817 }
818 read_unlock(&em_tree->lock);
819
820 return alloc_hint;
821}
822
Chris Mason771ed682008-11-06 22:02:51 -0500823/*
824 * when extent_io.c finds a delayed allocation range in the file,
825 * the call backs end up in this code. The basic idea is to
826 * allocate extents on disk for the range, and create ordered data structs
827 * in ram to track those extents.
828 *
829 * locked_page is the page that writepage had locked already. We use
830 * it to make sure we don't do extra locks or unlocks.
831 *
832 * *page_started is set to one if we unlock locked_page and do everything
833 * required to start IO on it. It may be clean and already done with
834 * IO when we return.
835 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000836static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
837 struct inode *inode,
838 struct btrfs_root *root,
839 struct page *locked_page,
840 u64 start, u64 end, int *page_started,
841 unsigned long *nr_written,
842 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500843{
Chris Mason771ed682008-11-06 22:02:51 -0500844 u64 alloc_hint = 0;
845 u64 num_bytes;
846 unsigned long ram_size;
847 u64 disk_num_bytes;
848 u64 cur_alloc_size;
849 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500850 struct btrfs_key ins;
851 struct extent_map *em;
852 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
853 int ret = 0;
854
Liu Bo83eea1f2012-07-10 05:28:39 -0600855 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500856
Chris Mason771ed682008-11-06 22:02:51 -0500857 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
858 num_bytes = max(blocksize, num_bytes);
859 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500860
Chris Mason4cb53002011-05-24 15:35:30 -0400861 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400862 if (num_bytes < 64 * 1024 &&
863 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400864 btrfs_add_inode_defrag(trans, inode);
865
Chris Mason771ed682008-11-06 22:02:51 -0500866 if (start == 0) {
867 /* lets try to make an inline extent */
868 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000869 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500870 if (ret == 0) {
871 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400872 &BTRFS_I(inode)->io_tree,
873 start, end, NULL,
874 EXTENT_CLEAR_UNLOCK_PAGE |
875 EXTENT_CLEAR_UNLOCK |
876 EXTENT_CLEAR_DELALLOC |
877 EXTENT_CLEAR_DIRTY |
878 EXTENT_SET_WRITEBACK |
879 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000880
Chris Mason771ed682008-11-06 22:02:51 -0500881 *nr_written = *nr_written +
882 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
883 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500884 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 } else if (ret < 0) {
886 btrfs_abort_transaction(trans, root, ret);
887 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500888 }
889 }
Chris Masonc8b97812008-10-29 14:49:59 -0400890
891 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200892 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400893
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400895 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400896
Chris Masond3977122009-01-05 21:25:51 -0500897 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400898 unsigned long op;
899
Josef Bacik287a0ab2010-03-19 18:07:23 +0000900 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500902 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500903 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904 if (ret < 0) {
905 btrfs_abort_transaction(trans, root, ret);
906 goto out_unlock;
907 }
Chris Masond3977122009-01-05 21:25:51 -0500908
David Sterba172ddd62011-04-21 00:48:27 +0200909 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100910 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500912 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500913 ram_size = ins.offset;
914 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400915 em->mod_start = em->start;
916 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400917
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400919 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500920 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400921 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400922 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400923 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400924
Chris Masond3977122009-01-05 21:25:51 -0500925 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400926 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400928 if (!ret)
929 list_move(&em->list,
930 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400931 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 if (ret != -EEXIST) {
933 free_extent_map(em);
934 break;
935 }
936 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400937 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 }
939
Chris Mason98d20f62008-04-14 09:46:10 -0400940 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400941 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500942 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100943 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400944
Yan Zheng17d217f2008-12-12 10:03:38 -0500945 if (root->root_key.objectid ==
946 BTRFS_DATA_RELOC_TREE_OBJECTID) {
947 ret = btrfs_reloc_clone_csums(inode, start,
948 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100949 if (ret) {
950 btrfs_abort_transaction(trans, root, ret);
951 goto out_unlock;
952 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500953 }
954
Chris Masond3977122009-01-05 21:25:51 -0500955 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400956 break;
Chris Masond3977122009-01-05 21:25:51 -0500957
Chris Masonc8b97812008-10-29 14:49:59 -0400958 /* we're not doing compressed IO, don't unlock the first
959 * page (which the caller expects to stay locked), don't
960 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400961 *
962 * Do set the Private2 bit so we know this page was properly
963 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400964 */
Chris Masona791e352009-10-08 11:27:10 -0400965 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
966 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
967 EXTENT_SET_PRIVATE2;
968
Chris Masonc8b97812008-10-29 14:49:59 -0400969 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
970 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400971 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400972 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500973 num_bytes -= cur_alloc_size;
974 alloc_hint = ins.objectid + ins.offset;
975 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400976 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500978 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000979
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100980out_unlock:
981 extent_clear_unlock_delalloc(inode,
982 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400983 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 EXTENT_CLEAR_UNLOCK_PAGE |
985 EXTENT_CLEAR_UNLOCK |
986 EXTENT_CLEAR_DELALLOC |
987 EXTENT_CLEAR_DIRTY |
988 EXTENT_SET_WRITEBACK |
989 EXTENT_END_WRITEBACK);
990
991 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500992}
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000994static noinline int cow_file_range(struct inode *inode,
995 struct page *locked_page,
996 u64 start, u64 end, int *page_started,
997 unsigned long *nr_written,
998 int unlock)
999{
1000 struct btrfs_trans_handle *trans;
1001 struct btrfs_root *root = BTRFS_I(inode)->root;
1002 int ret;
1003
1004 trans = btrfs_join_transaction(root);
1005 if (IS_ERR(trans)) {
1006 extent_clear_unlock_delalloc(inode,
1007 &BTRFS_I(inode)->io_tree,
1008 start, end, locked_page,
1009 EXTENT_CLEAR_UNLOCK_PAGE |
1010 EXTENT_CLEAR_UNLOCK |
1011 EXTENT_CLEAR_DELALLOC |
1012 EXTENT_CLEAR_DIRTY |
1013 EXTENT_SET_WRITEBACK |
1014 EXTENT_END_WRITEBACK);
1015 return PTR_ERR(trans);
1016 }
1017 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1018
1019 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1020 page_started, nr_written, unlock);
1021
1022 btrfs_end_transaction(trans, root);
1023
1024 return ret;
1025}
1026
Chris Mason771ed682008-11-06 22:02:51 -05001027/*
1028 * work queue call back to started compression on a file and pages
1029 */
1030static noinline void async_cow_start(struct btrfs_work *work)
1031{
1032 struct async_cow *async_cow;
1033 int num_added = 0;
1034 async_cow = container_of(work, struct async_cow, work);
1035
1036 compress_file_range(async_cow->inode, async_cow->locked_page,
1037 async_cow->start, async_cow->end, async_cow,
1038 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001039 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001040 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001041 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001042 }
Chris Mason771ed682008-11-06 22:02:51 -05001043}
1044
1045/*
1046 * work queue call back to submit previously compressed pages
1047 */
1048static noinline void async_cow_submit(struct btrfs_work *work)
1049{
1050 struct async_cow *async_cow;
1051 struct btrfs_root *root;
1052 unsigned long nr_pages;
1053
1054 async_cow = container_of(work, struct async_cow, work);
1055
1056 root = async_cow->root;
1057 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1058 PAGE_CACHE_SHIFT;
1059
Josef Bacik66657b32012-08-01 15:36:24 -04001060 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001061 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001062 waitqueue_active(&root->fs_info->async_submit_wait))
1063 wake_up(&root->fs_info->async_submit_wait);
1064
Chris Masond3977122009-01-05 21:25:51 -05001065 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001066 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001067}
Chris Masonc8b97812008-10-29 14:49:59 -04001068
Chris Mason771ed682008-11-06 22:02:51 -05001069static noinline void async_cow_free(struct btrfs_work *work)
1070{
1071 struct async_cow *async_cow;
1072 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001073 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001074 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001075 kfree(async_cow);
1076}
1077
1078static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1079 u64 start, u64 end, int *page_started,
1080 unsigned long *nr_written)
1081{
1082 struct async_cow *async_cow;
1083 struct btrfs_root *root = BTRFS_I(inode)->root;
1084 unsigned long nr_pages;
1085 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001086 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001087
Chris Masona3429ab2009-10-08 12:30:20 -04001088 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1089 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001090 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001091 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001093 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001094 async_cow->root = root;
1095 async_cow->locked_page = locked_page;
1096 async_cow->start = start;
1097
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001098 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001099 cur_end = end;
1100 else
1101 cur_end = min(end, start + 512 * 1024 - 1);
1102
1103 async_cow->end = cur_end;
1104 INIT_LIST_HEAD(&async_cow->extents);
1105
1106 async_cow->work.func = async_cow_start;
1107 async_cow->work.ordered_func = async_cow_submit;
1108 async_cow->work.ordered_free = async_cow_free;
1109 async_cow->work.flags = 0;
1110
Chris Mason771ed682008-11-06 22:02:51 -05001111 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1112 PAGE_CACHE_SHIFT;
1113 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1114
1115 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1116 &async_cow->work);
1117
1118 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1119 wait_event(root->fs_info->async_submit_wait,
1120 (atomic_read(&root->fs_info->async_delalloc_pages) <
1121 limit));
1122 }
1123
Chris Masond3977122009-01-05 21:25:51 -05001124 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001125 atomic_read(&root->fs_info->async_delalloc_pages)) {
1126 wait_event(root->fs_info->async_submit_wait,
1127 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1128 0));
1129 }
1130
1131 *nr_written += nr_pages;
1132 start = cur_end + 1;
1133 }
1134 *page_started = 1;
1135 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001136}
1137
Chris Masond3977122009-01-05 21:25:51 -05001138static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001139 u64 bytenr, u64 num_bytes)
1140{
1141 int ret;
1142 struct btrfs_ordered_sum *sums;
1143 LIST_HEAD(list);
1144
Yan Zheng07d400a2009-01-06 11:42:00 -05001145 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001146 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001147 if (ret == 0 && list_empty(&list))
1148 return 0;
1149
1150 while (!list_empty(&list)) {
1151 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1152 list_del(&sums->list);
1153 kfree(sums);
1154 }
1155 return 1;
1156}
1157
Chris Masond352ac62008-09-29 15:18:18 -04001158/*
1159 * when nowcow writeback call back. This checks for snapshots or COW copies
1160 * of the extents that exist in the file, and COWs the file as required.
1161 *
1162 * If no cow copies or snapshots exist, we write directly to the existing
1163 * blocks on disk
1164 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001165static noinline int run_delalloc_nocow(struct inode *inode,
1166 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001167 u64 start, u64 end, int *page_started, int force,
1168 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001169{
Chris Masonbe20aa92007-12-17 20:14:01 -05001170 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001171 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001173 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001174 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001175 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 u64 cow_start;
1177 u64 cur_offset;
1178 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 u64 disk_bytenr;
1181 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001182 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001185 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 int nocow;
1187 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001188 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001189 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001190
1191 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001192 if (!path) {
1193 extent_clear_unlock_delalloc(inode,
1194 &BTRFS_I(inode)->io_tree,
1195 start, end, locked_page,
1196 EXTENT_CLEAR_UNLOCK_PAGE |
1197 EXTENT_CLEAR_UNLOCK |
1198 EXTENT_CLEAR_DELALLOC |
1199 EXTENT_CLEAR_DIRTY |
1200 EXTENT_SET_WRITEBACK |
1201 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001202 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001203 }
Li Zefan82d59022011-04-20 10:33:24 +08001204
Liu Bo83eea1f2012-07-10 05:28:39 -06001205 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001206
1207 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001208 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001209 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001210 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001211
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001213 extent_clear_unlock_delalloc(inode,
1214 &BTRFS_I(inode)->io_tree,
1215 start, end, locked_page,
1216 EXTENT_CLEAR_UNLOCK_PAGE |
1217 EXTENT_CLEAR_UNLOCK |
1218 EXTENT_CLEAR_DELALLOC |
1219 EXTENT_CLEAR_DIRTY |
1220 EXTENT_SET_WRITEBACK |
1221 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001222 btrfs_free_path(path);
1223 return PTR_ERR(trans);
1224 }
1225
Josef Bacik74b21072011-04-13 12:02:53 -04001226 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001227
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 cow_start = (u64)-1;
1229 cur_offset = start;
1230 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001231 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 if (ret < 0) {
1234 btrfs_abort_transaction(trans, root, ret);
1235 goto error;
1236 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1238 leaf = path->nodes[0];
1239 btrfs_item_key_to_cpu(leaf, &found_key,
1240 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001241 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 found_key.type == BTRFS_EXTENT_DATA_KEY)
1243 path->slots[0]--;
1244 }
1245 check_prev = 0;
1246next_slot:
1247 leaf = path->nodes[0];
1248 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1249 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 if (ret < 0) {
1251 btrfs_abort_transaction(trans, root, ret);
1252 goto error;
1253 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 if (ret > 0)
1255 break;
1256 leaf = path->nodes[0];
1257 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001258
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 nocow = 0;
1260 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001261 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001263
Li Zefan33345d012011-04-20 10:31:50 +08001264 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1266 found_key.offset > end)
1267 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (found_key.offset > cur_offset) {
1270 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001271 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 goto out_check;
1273 }
Chris Masonc31f8832008-01-08 15:46:31 -05001274
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 fi = btrfs_item_ptr(leaf, path->slots[0],
1276 struct btrfs_file_extent_item);
1277 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Yan Zhengd899e052008-10-30 14:25:28 -04001279 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1280 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 extent_end = found_key.offset +
1284 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001285 disk_num_bytes =
1286 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (extent_end <= start) {
1288 path->slots[0]++;
1289 goto next_slot;
1290 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 if (disk_bytenr == 0)
1292 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 if (btrfs_file_extent_compression(leaf, fi) ||
1294 btrfs_file_extent_encryption(leaf, fi) ||
1295 btrfs_file_extent_other_encoding(leaf, fi))
1296 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001297 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1298 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001299 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001301 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001302 found_key.offset -
1303 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001304 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001306 disk_bytenr += cur_offset - found_key.offset;
1307 num_bytes = min(end + 1, extent_end) - cur_offset;
1308 /*
1309 * force cow if csum exists in the range.
1310 * this ensure that csum for a given extent are
1311 * either valid or do not exist.
1312 */
1313 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1314 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 nocow = 1;
1316 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1317 extent_end = found_key.offset +
1318 btrfs_file_extent_inline_len(leaf, fi);
1319 extent_end = ALIGN(extent_end, root->sectorsize);
1320 } else {
1321 BUG_ON(1);
1322 }
1323out_check:
1324 if (extent_end <= start) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
1328 if (!nocow) {
1329 if (cow_start == (u64)-1)
1330 cow_start = cur_offset;
1331 cur_offset = extent_end;
1332 if (cur_offset > end)
1333 break;
1334 path->slots[0]++;
1335 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001336 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
David Sterbab3b4aa72011-04-21 01:20:15 +02001338 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001340 ret = __cow_file_range(trans, inode, root, locked_page,
1341 cow_start, found_key.offset - 1,
1342 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001343 if (ret) {
1344 btrfs_abort_transaction(trans, root, ret);
1345 goto error;
1346 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001348 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001349
Yan Zhengd899e052008-10-30 14:25:28 -04001350 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1351 struct extent_map *em;
1352 struct extent_map_tree *em_tree;
1353 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001354 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001356 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001357 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001358 em->len = num_bytes;
1359 em->block_len = num_bytes;
1360 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001361 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001362 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001363 em->mod_start = em->start;
1364 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001365 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001366 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001367 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001368 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001369 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001370 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001371 if (!ret)
1372 list_move(&em->list,
1373 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001374 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (ret != -EEXIST) {
1376 free_extent_map(em);
1377 break;
1378 }
1379 btrfs_drop_extent_cache(inode, em->start,
1380 em->start + em->len - 1, 0);
1381 }
1382 type = BTRFS_ORDERED_PREALLOC;
1383 } else {
1384 type = BTRFS_ORDERED_NOCOW;
1385 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001386
1387 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001388 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001389 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001390
Yan, Zhengefa56462010-05-16 10:49:59 -04001391 if (root->root_key.objectid ==
1392 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1393 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1394 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 if (ret) {
1396 btrfs_abort_transaction(trans, root, ret);
1397 goto error;
1398 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001399 }
1400
Yan Zhengd899e052008-10-30 14:25:28 -04001401 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001402 cur_offset, cur_offset + num_bytes - 1,
1403 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1404 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1405 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001406 cur_offset = extent_end;
1407 if (cur_offset > end)
1408 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001409 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001410 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001411
Josef Bacik17ca04a2012-05-31 15:58:55 -04001412 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 cur_offset = end;
1415 }
1416
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001418 ret = __cow_file_range(trans, inode, root, locked_page,
1419 cow_start, end,
1420 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001421 if (ret) {
1422 btrfs_abort_transaction(trans, root, ret);
1423 goto error;
1424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 }
1426
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427error:
Miao Xiea698d0752012-09-20 01:51:59 -06001428 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 if (!ret)
1430 ret = err;
1431
Josef Bacik17ca04a2012-05-31 15:58:55 -04001432 if (ret && cur_offset < end)
1433 extent_clear_unlock_delalloc(inode,
1434 &BTRFS_I(inode)->io_tree,
1435 cur_offset, end, locked_page,
1436 EXTENT_CLEAR_UNLOCK_PAGE |
1437 EXTENT_CLEAR_UNLOCK |
1438 EXTENT_CLEAR_DELALLOC |
1439 EXTENT_CLEAR_DIRTY |
1440 EXTENT_SET_WRITEBACK |
1441 EXTENT_END_WRITEBACK);
1442
Yan Zheng7ea394f2008-08-05 13:05:02 -04001443 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001445}
1446
Chris Masond352ac62008-09-29 15:18:18 -04001447/*
1448 * extent_io.c call back to do delayed allocation processing
1449 */
Chris Masonc8b97812008-10-29 14:49:59 -04001450static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001451 u64 start, u64 end, int *page_started,
1452 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001453{
Chris Masonbe20aa92007-12-17 20:14:01 -05001454 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001455 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001456
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001457 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001458 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001459 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001460 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001461 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001462 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001463 } else if (!btrfs_test_opt(root, COMPRESS) &&
1464 !(BTRFS_I(inode)->force_compress) &&
1465 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001466 ret = cow_file_range(inode, locked_page, start, end,
1467 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001468 } else {
1469 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1470 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001471 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001472 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001473 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001474 return ret;
1475}
1476
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477static void btrfs_split_extent_hook(struct inode *inode,
1478 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001480 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001482 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001483
Josef Bacik9e0baf62011-07-15 15:16:44 +00001484 spin_lock(&BTRFS_I(inode)->lock);
1485 BTRFS_I(inode)->outstanding_extents++;
1486 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001487}
1488
1489/*
1490 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1491 * extents so we can keep track of new extents that are just merged onto old
1492 * extents, such as when we are doing sequential writes, so we can properly
1493 * account for the metadata space we'll need.
1494 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001495static void btrfs_merge_extent_hook(struct inode *inode,
1496 struct extent_state *new,
1497 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499 /* not delalloc, ignore it */
1500 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001501 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502
Josef Bacik9e0baf62011-07-15 15:16:44 +00001503 spin_lock(&BTRFS_I(inode)->lock);
1504 BTRFS_I(inode)->outstanding_extents--;
1505 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506}
1507
Chris Masond352ac62008-09-29 15:18:18 -04001508/*
1509 * extent_io.c set_bit_hook, used to track delayed allocation
1510 * bytes in this file, and to maintain the list of inodes that
1511 * have pending delalloc work to be done.
1512 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001513static void btrfs_set_bit_hook(struct inode *inode,
1514 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001515{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516
Chris Mason75eff682008-12-15 15:54:40 -05001517 /*
1518 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001519 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001520 * bit, which is only set or cleared with irqs on
1521 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001523 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001524 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001525 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526
Josef Bacik9e0baf62011-07-15 15:16:44 +00001527 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001528 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001529 } else {
1530 spin_lock(&BTRFS_I(inode)->lock);
1531 BTRFS_I(inode)->outstanding_extents++;
1532 spin_unlock(&BTRFS_I(inode)->lock);
1533 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001534
Miao Xie963d6782013-01-29 10:10:51 +00001535 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1536 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001537 spin_lock(&BTRFS_I(inode)->lock);
1538 BTRFS_I(inode)->delalloc_bytes += len;
1539 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1540 &BTRFS_I(inode)->runtime_flags)) {
1541 spin_lock(&root->fs_info->delalloc_lock);
1542 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1543 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1544 &root->fs_info->delalloc_inodes);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1546 &BTRFS_I(inode)->runtime_flags);
1547 }
1548 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001549 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001550 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001551 }
Chris Mason291d6732008-01-29 15:55:23 -05001552}
1553
Chris Masond352ac62008-09-29 15:18:18 -04001554/*
1555 * extent_io.c clear_bit_hook, see set_bit_hook for why
1556 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001557static void btrfs_clear_bit_hook(struct inode *inode,
1558 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001559{
Chris Mason75eff682008-12-15 15:54:40 -05001560 /*
1561 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001562 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001563 * bit, which is only set or cleared with irqs on
1564 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001566 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001568 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001569
Josef Bacik9e0baf62011-07-15 15:16:44 +00001570 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001571 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1573 spin_lock(&BTRFS_I(inode)->lock);
1574 BTRFS_I(inode)->outstanding_extents--;
1575 spin_unlock(&BTRFS_I(inode)->lock);
1576 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001577
1578 if (*bits & EXTENT_DO_ACCOUNTING)
1579 btrfs_delalloc_release_metadata(inode, len);
1580
Josef Bacik0cb59c92010-07-02 12:14:14 -04001581 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1582 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001584
Miao Xie963d6782013-01-29 10:10:51 +00001585 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1586 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001587 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001588 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001589 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001590 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1591 &BTRFS_I(inode)->runtime_flags)) {
1592 spin_lock(&root->fs_info->delalloc_lock);
1593 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1594 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1595 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1596 &BTRFS_I(inode)->runtime_flags);
1597 }
1598 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001599 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001600 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001601 }
Chris Mason291d6732008-01-29 15:55:23 -05001602}
1603
Chris Masond352ac62008-09-29 15:18:18 -04001604/*
1605 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1606 * we don't create bios that span stripes or chunks
1607 */
Chris Mason239b14b2008-03-24 15:02:07 -04001608int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001609 size_t size, struct bio *bio,
1610 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001611{
1612 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001613 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001614 u64 length = 0;
1615 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001616 int ret;
1617
Chris Mason771ed682008-11-06 22:02:51 -05001618 if (bio_flags & EXTENT_BIO_COMPRESSED)
1619 return 0;
1620
Chris Masonf2d8d742008-04-21 10:03:05 -04001621 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001622 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001623 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001624 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001625 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001626 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001627 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001628 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001629 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001630}
1631
Chris Masond352ac62008-09-29 15:18:18 -04001632/*
1633 * in order to insert checksums into the metadata in large chunks,
1634 * we wait until bio submission time. All the pages in the bio are
1635 * checksummed and sums are attached onto the ordered extent record.
1636 *
1637 * At IO completion time the cums attached on the ordered extent record
1638 * are inserted into the btree
1639 */
Chris Masond3977122009-01-05 21:25:51 -05001640static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1641 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001642 unsigned long bio_flags,
1643 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001644{
Chris Mason065631f2008-02-20 12:07:25 -05001645 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001646 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001647
Chris Masond20f7042008-12-08 16:58:54 -05001648 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001649 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001650 return 0;
1651}
Chris Masone0156402008-04-16 11:15:20 -04001652
Chris Mason4a69a412008-11-06 22:03:00 -05001653/*
1654 * in order to insert checksums into the metadata in large chunks,
1655 * we wait until bio submission time. All the pages in the bio are
1656 * checksummed and sums are attached onto the ordered extent record.
1657 *
1658 * At IO completion time the cums attached on the ordered extent record
1659 * are inserted into the btree
1660 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001661static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001662 int mirror_num, unsigned long bio_flags,
1663 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001664{
1665 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001666 int ret;
1667
1668 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1669 if (ret)
1670 bio_endio(bio, ret);
1671 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001672}
1673
Chris Masond352ac62008-09-29 15:18:18 -04001674/*
Chris Masoncad321a2008-12-17 14:51:42 -05001675 * extent_io.c submission hook. This does the right thing for csum calculation
1676 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001677 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001678static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001679 int mirror_num, unsigned long bio_flags,
1680 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001681{
1682 struct btrfs_root *root = BTRFS_I(inode)->root;
1683 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001684 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001685 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001686 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001687
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001688 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001689
Liu Bo83eea1f2012-07-10 05:28:39 -06001690 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001691 metadata = 2;
1692
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001693 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001694 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1695 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001696 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001697
Chris Masond20f7042008-12-08 16:58:54 -05001698 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001699 ret = btrfs_submit_compressed_read(inode, bio,
1700 mirror_num,
1701 bio_flags);
1702 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001703 } else if (!skip_sum) {
1704 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1705 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001706 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001707 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001708 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001709 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001710 /* csum items have already been cloned */
1711 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1712 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001713 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001714 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001715 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001716 bio_flags, bio_offset,
1717 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001718 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001719 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001720 } else if (!skip_sum) {
1721 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1722 if (ret)
1723 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001724 }
1725
Chris Mason0b86a832008-03-24 15:01:56 -04001726mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001727 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1728
1729out:
1730 if (ret < 0)
1731 bio_endio(bio, ret);
1732 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001733}
Chris Mason6885f302008-02-20 16:11:05 -05001734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * given a list of ordered sums record them in the inode. This happens
1737 * at IO completion time based on sums calculated at bio submission time.
1738 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001739static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001740 struct inode *inode, u64 file_offset,
1741 struct list_head *list)
1742{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001743 struct btrfs_ordered_sum *sum;
1744
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001745 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001746 btrfs_csum_file_blocks(trans,
1747 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001748 }
1749 return 0;
1750}
1751
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1753 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001754{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001755 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001756 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001757 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001758}
1759
Chris Masond352ac62008-09-29 15:18:18 -04001760/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001761struct btrfs_writepage_fixup {
1762 struct page *page;
1763 struct btrfs_work work;
1764};
1765
Christoph Hellwigb2950862008-12-02 09:54:17 -05001766static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001767{
1768 struct btrfs_writepage_fixup *fixup;
1769 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001770 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001771 struct page *page;
1772 struct inode *inode;
1773 u64 page_start;
1774 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001775 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001776
1777 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1778 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001779again:
Chris Mason247e7432008-07-17 12:53:51 -04001780 lock_page(page);
1781 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1782 ClearPageChecked(page);
1783 goto out_page;
1784 }
1785
1786 inode = page->mapping->host;
1787 page_start = page_offset(page);
1788 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1789
Josef Bacik2ac55d42010-02-03 19:33:23 +00001790 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001791 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001792
1793 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001794 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001795 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001796
1797 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1798 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001799 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1800 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001801 unlock_page(page);
1802 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001803 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001804 goto again;
1805 }
Chris Mason247e7432008-07-17 12:53:51 -04001806
Jeff Mahoney87826df2012-02-15 16:23:57 +01001807 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1808 if (ret) {
1809 mapping_set_error(page->mapping, ret);
1810 end_extent_writepage(page, ret, page_start, page_end);
1811 ClearPageChecked(page);
1812 goto out;
1813 }
1814
Josef Bacik2ac55d42010-02-03 19:33:23 +00001815 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001816 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001817 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001818out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001819 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1820 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001821out_page:
1822 unlock_page(page);
1823 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001824 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001825}
1826
1827/*
1828 * There are a few paths in the higher layers of the kernel that directly
1829 * set the page dirty bit without asking the filesystem if it is a
1830 * good idea. This causes problems because we want to make sure COW
1831 * properly happens and the data=ordered rules are followed.
1832 *
Chris Masonc8b97812008-10-29 14:49:59 -04001833 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001834 * hasn't been properly setup for IO. We kick off an async process
1835 * to fix it up. The async helper will wait for ordered extents, set
1836 * the delalloc bit and make it safe to write the page.
1837 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001838static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001839{
1840 struct inode *inode = page->mapping->host;
1841 struct btrfs_writepage_fixup *fixup;
1842 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001843
Chris Mason8b62b722009-09-02 16:53:46 -04001844 /* this page is properly in the ordered list */
1845 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001846 return 0;
1847
1848 if (PageChecked(page))
1849 return -EAGAIN;
1850
1851 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1852 if (!fixup)
1853 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001854
Chris Mason247e7432008-07-17 12:53:51 -04001855 SetPageChecked(page);
1856 page_cache_get(page);
1857 fixup->work.func = btrfs_writepage_fixup_worker;
1858 fixup->page = page;
1859 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001860 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001861}
1862
Yan Zhengd899e052008-10-30 14:25:28 -04001863static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1864 struct inode *inode, u64 file_pos,
1865 u64 disk_bytenr, u64 disk_num_bytes,
1866 u64 num_bytes, u64 ram_bytes,
1867 u8 compression, u8 encryption,
1868 u16 other_encoding, int extent_type)
1869{
1870 struct btrfs_root *root = BTRFS_I(inode)->root;
1871 struct btrfs_file_extent_item *fi;
1872 struct btrfs_path *path;
1873 struct extent_buffer *leaf;
1874 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001875 int ret;
1876
1877 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001878 if (!path)
1879 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001880
Chris Masonb9473432009-03-13 11:00:37 -04001881 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001882
1883 /*
1884 * we may be replacing one extent in the tree with another.
1885 * The new extent is pinned in the extent map, and we don't want
1886 * to drop it from the cache until it is completely in the btree.
1887 *
1888 * So, tell btrfs_drop_extents to leave this extent in the cache.
1889 * the caller is expected to unpin it and allow it to be merged
1890 * with the others.
1891 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001892 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001893 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 if (ret)
1895 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001896
Li Zefan33345d012011-04-20 10:31:50 +08001897 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001898 ins.offset = file_pos;
1899 ins.type = BTRFS_EXTENT_DATA_KEY;
1900 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 if (ret)
1902 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001903 leaf = path->nodes[0];
1904 fi = btrfs_item_ptr(leaf, path->slots[0],
1905 struct btrfs_file_extent_item);
1906 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1907 btrfs_set_file_extent_type(leaf, fi, extent_type);
1908 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1909 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1910 btrfs_set_file_extent_offset(leaf, fi, 0);
1911 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1912 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1913 btrfs_set_file_extent_compression(leaf, fi, compression);
1914 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1915 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001916
Yan Zhengd899e052008-10-30 14:25:28 -04001917 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001918 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001919
1920 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001921
1922 ins.objectid = disk_bytenr;
1923 ins.offset = disk_num_bytes;
1924 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001925 ret = btrfs_alloc_reserved_file_extent(trans, root,
1926 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001927 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928out:
Yan Zhengd899e052008-10-30 14:25:28 -04001929 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001930
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001932}
1933
Chris Mason5d13a982009-03-13 11:41:46 -04001934/*
1935 * helper function for btrfs_finish_ordered_io, this
1936 * just reads in some of the csum leaves to prime them into ram
1937 * before we start the transaction. It limits the amount of btree
1938 * reads required while inside the transaction.
1939 */
Chris Masond352ac62008-09-29 15:18:18 -04001940/* as ordered data IO finishes, this gets called so we can finish
1941 * an ordered extent if the range of bytes in the file it covers are
1942 * fully written.
1943 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001944static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001945{
Josef Bacik5fd02042012-05-02 14:00:54 -04001946 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001947 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001948 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001949 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001950 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001951 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001952 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001953 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001954
Liu Bo83eea1f2012-07-10 05:28:39 -06001955 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001956
Josef Bacik5fd02042012-05-02 14:00:54 -04001957 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1958 ret = -EIO;
1959 goto out;
1960 }
1961
Yan, Zhengc2167752009-11-12 09:34:21 +00001962 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001963 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001964 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1965 if (nolock)
1966 trans = btrfs_join_transaction_nolock(root);
1967 else
1968 trans = btrfs_join_transaction(root);
1969 if (IS_ERR(trans)) {
1970 ret = PTR_ERR(trans);
1971 trans = NULL;
1972 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00001973 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001974 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1975 ret = btrfs_update_inode_fallback(trans, root, inode);
1976 if (ret) /* -ENOMEM or corruption */
1977 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001978 goto out;
1979 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001980
Josef Bacik2ac55d42010-02-03 19:33:23 +00001981 lock_extent_bits(io_tree, ordered_extent->file_offset,
1982 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001983 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001984
Josef Bacik0cb59c92010-07-02 12:14:14 -04001985 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001986 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001987 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001988 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001989 if (IS_ERR(trans)) {
1990 ret = PTR_ERR(trans);
1991 trans = NULL;
1992 goto out_unlock;
1993 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001994 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001995
Chris Masonc8b97812008-10-29 14:49:59 -04001996 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001997 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001998 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001999 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002000 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002001 ordered_extent->file_offset,
2002 ordered_extent->file_offset +
2003 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002004 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002005 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002006 ret = insert_reserved_file_extent(trans, inode,
2007 ordered_extent->file_offset,
2008 ordered_extent->start,
2009 ordered_extent->disk_len,
2010 ordered_extent->len,
2011 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002012 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002013 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002014 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002015 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2016 ordered_extent->file_offset, ordered_extent->len,
2017 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002018 if (ret < 0) {
2019 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002020 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002021 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002022
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 add_pending_csums(trans, inode, ordered_extent->file_offset,
2024 &ordered_extent->list);
2025
Josef Bacik6c760c02012-11-09 10:53:21 -05002026 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2027 ret = btrfs_update_inode_fallback(trans, root, inode);
2028 if (ret) { /* -ENOMEM or corruption */
2029 btrfs_abort_transaction(trans, root, ret);
2030 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002031 }
2032 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002033out_unlock:
2034 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2035 ordered_extent->file_offset +
2036 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002037out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002038 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002039 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002040 if (trans)
2041 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002042
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002043 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002044 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2045 ordered_extent->file_offset +
2046 ordered_extent->len - 1, NULL, GFP_NOFS);
2047
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002048 /*
2049 * If the ordered extent had an IOERR or something else went
2050 * wrong we need to return the space for this ordered extent
2051 * back to the allocator.
2052 */
2053 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2054 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2055 btrfs_free_reserved_extent(root, ordered_extent->start,
2056 ordered_extent->disk_len);
2057 }
2058
2059
Josef Bacik5fd02042012-05-02 14:00:54 -04002060 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002061 * This needs to be done to make sure anybody waiting knows we are done
2062 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002063 */
2064 btrfs_remove_ordered_extent(inode, ordered_extent);
2065
Chris Masone6dcd2d2008-07-17 12:53:50 -04002066 /* once for us */
2067 btrfs_put_ordered_extent(ordered_extent);
2068 /* once for the tree */
2069 btrfs_put_ordered_extent(ordered_extent);
2070
Josef Bacik5fd02042012-05-02 14:00:54 -04002071 return ret;
2072}
2073
2074static void finish_ordered_fn(struct btrfs_work *work)
2075{
2076 struct btrfs_ordered_extent *ordered_extent;
2077 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2078 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002079}
2080
Christoph Hellwigb2950862008-12-02 09:54:17 -05002081static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002082 struct extent_state *state, int uptodate)
2083{
Josef Bacik5fd02042012-05-02 14:00:54 -04002084 struct inode *inode = page->mapping->host;
2085 struct btrfs_root *root = BTRFS_I(inode)->root;
2086 struct btrfs_ordered_extent *ordered_extent = NULL;
2087 struct btrfs_workers *workers;
2088
liubo1abe9b82011-03-24 11:18:59 +00002089 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2090
Chris Mason8b62b722009-09-02 16:53:46 -04002091 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002092 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2093 end - start + 1, uptodate))
2094 return 0;
2095
2096 ordered_extent->work.func = finish_ordered_fn;
2097 ordered_extent->work.flags = 0;
2098
Liu Bo83eea1f2012-07-10 05:28:39 -06002099 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002100 workers = &root->fs_info->endio_freespace_worker;
2101 else
2102 workers = &root->fs_info->endio_write_workers;
2103 btrfs_queue_worker(workers, &ordered_extent->work);
2104
2105 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002106}
2107
Chris Masond352ac62008-09-29 15:18:18 -04002108/*
Chris Masond352ac62008-09-29 15:18:18 -04002109 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110 * if there's a match, we allow the bio to finish. If not, the code in
2111 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002112 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002113static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002114 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002115{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002116 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002117 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002118 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002119 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002120 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002121 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002122 struct btrfs_root *root = BTRFS_I(inode)->root;
2123 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002124
Chris Masond20f7042008-12-08 16:58:54 -05002125 if (PageChecked(page)) {
2126 ClearPageChecked(page);
2127 goto good;
2128 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002129
2130 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002131 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002132
Yan Zheng17d217f2008-12-12 10:03:38 -05002133 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002134 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002135 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2136 GFP_NOFS);
2137 return 0;
2138 }
2139
Yanc2e639f2008-02-04 08:57:25 -05002140 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002141 private = state->private;
2142 ret = 0;
2143 } else {
2144 ret = get_state_private(io_tree, start, &private);
2145 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002146 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002147 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002148 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002149
Chris Masonff79f812007-10-15 16:22:25 -04002150 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2151 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002152 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002153 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002154
Cong Wang7ac687d2011-11-25 23:14:28 +08002155 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002156good:
Chris Mason07157aa2007-08-30 08:50:51 -04002157 return 0;
2158
2159zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002160 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002161 "private %llu\n",
2162 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002163 (unsigned long long)start, csum,
2164 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002165 memset(kaddr + offset, 1, end - start + 1);
2166 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002167 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002168 if (private == 0)
2169 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002170 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002171}
Chris Masonb888db2b2007-08-27 16:49:44 -04002172
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002173struct delayed_iput {
2174 struct list_head list;
2175 struct inode *inode;
2176};
2177
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002178/* JDM: If this is fs-wide, why can't we add a pointer to
2179 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002180void btrfs_add_delayed_iput(struct inode *inode)
2181{
2182 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2183 struct delayed_iput *delayed;
2184
2185 if (atomic_add_unless(&inode->i_count, -1, 1))
2186 return;
2187
2188 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2189 delayed->inode = inode;
2190
2191 spin_lock(&fs_info->delayed_iput_lock);
2192 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2193 spin_unlock(&fs_info->delayed_iput_lock);
2194}
2195
2196void btrfs_run_delayed_iputs(struct btrfs_root *root)
2197{
2198 LIST_HEAD(list);
2199 struct btrfs_fs_info *fs_info = root->fs_info;
2200 struct delayed_iput *delayed;
2201 int empty;
2202
2203 spin_lock(&fs_info->delayed_iput_lock);
2204 empty = list_empty(&fs_info->delayed_iputs);
2205 spin_unlock(&fs_info->delayed_iput_lock);
2206 if (empty)
2207 return;
2208
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002209 spin_lock(&fs_info->delayed_iput_lock);
2210 list_splice_init(&fs_info->delayed_iputs, &list);
2211 spin_unlock(&fs_info->delayed_iput_lock);
2212
2213 while (!list_empty(&list)) {
2214 delayed = list_entry(list.next, struct delayed_iput, list);
2215 list_del(&delayed->list);
2216 iput(delayed->inode);
2217 kfree(delayed);
2218 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002219}
2220
Yan, Zhengd68fc572010-05-16 10:49:58 -04002221/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002222 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002223 * files in the subvolume, it removes orphan item and frees block_rsv
2224 * structure.
2225 */
2226void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2227 struct btrfs_root *root)
2228{
Josef Bacik90290e12011-12-02 15:44:12 -05002229 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002230 int ret;
2231
Josef Bacik8a35d952012-05-23 14:26:42 -04002232 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002233 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2234 return;
2235
Josef Bacik90290e12011-12-02 15:44:12 -05002236 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002237 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002238 spin_unlock(&root->orphan_lock);
2239 return;
2240 }
2241
2242 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2243 spin_unlock(&root->orphan_lock);
2244 return;
2245 }
2246
2247 block_rsv = root->orphan_block_rsv;
2248 root->orphan_block_rsv = NULL;
2249 spin_unlock(&root->orphan_lock);
2250
Yan, Zhengd68fc572010-05-16 10:49:58 -04002251 if (root->orphan_item_inserted &&
2252 btrfs_root_refs(&root->root_item) > 0) {
2253 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2254 root->root_key.objectid);
2255 BUG_ON(ret);
2256 root->orphan_item_inserted = 0;
2257 }
2258
Josef Bacik90290e12011-12-02 15:44:12 -05002259 if (block_rsv) {
2260 WARN_ON(block_rsv->size > 0);
2261 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002262 }
2263}
2264
2265/*
Josef Bacik7b128762008-07-24 12:17:14 -04002266 * This creates an orphan entry for the given inode in case something goes
2267 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002268 *
2269 * NOTE: caller of this function should reserve 5 units of metadata for
2270 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002271 */
2272int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2273{
2274 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002275 struct btrfs_block_rsv *block_rsv = NULL;
2276 int reserve = 0;
2277 int insert = 0;
2278 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002279
Yan, Zhengd68fc572010-05-16 10:49:58 -04002280 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002281 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002282 if (!block_rsv)
2283 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002284 }
2285
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 spin_lock(&root->orphan_lock);
2287 if (!root->orphan_block_rsv) {
2288 root->orphan_block_rsv = block_rsv;
2289 } else if (block_rsv) {
2290 btrfs_free_block_rsv(root, block_rsv);
2291 block_rsv = NULL;
2292 }
Josef Bacik7b128762008-07-24 12:17:14 -04002293
Josef Bacik8a35d952012-05-23 14:26:42 -04002294 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2295 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002296#if 0
2297 /*
2298 * For proper ENOSPC handling, we should do orphan
2299 * cleanup when mounting. But this introduces backward
2300 * compatibility issue.
2301 */
2302 if (!xchg(&root->orphan_item_inserted, 1))
2303 insert = 2;
2304 else
2305 insert = 1;
2306#endif
2307 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002308 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002309 }
Josef Bacik7b128762008-07-24 12:17:14 -04002310
Josef Bacik72ac3c02012-05-23 14:13:11 -04002311 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2312 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002313 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002314 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002315
Yan, Zhengd68fc572010-05-16 10:49:58 -04002316 /* grab metadata reservation from transaction handle */
2317 if (reserve) {
2318 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002319 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002320 }
2321
2322 /* insert an orphan item to track this unlinked/truncated file */
2323 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002324 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002325 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002326 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2327 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002328 btrfs_abort_transaction(trans, root, ret);
2329 return ret;
2330 }
2331 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002332 }
2333
2334 /* insert an orphan item to track subvolume contains orphan files */
2335 if (insert >= 2) {
2336 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2337 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002338 if (ret && ret != -EEXIST) {
2339 btrfs_abort_transaction(trans, root, ret);
2340 return ret;
2341 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002342 }
2343 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002344}
2345
2346/*
2347 * We have done the truncate/delete so we can go ahead and remove the orphan
2348 * item for this particular inode.
2349 */
2350int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2351{
2352 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002353 int delete_item = 0;
2354 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002355 int ret = 0;
2356
Yan, Zhengd68fc572010-05-16 10:49:58 -04002357 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002358 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2359 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002360 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002361
Josef Bacik72ac3c02012-05-23 14:13:11 -04002362 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2363 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002364 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002365 spin_unlock(&root->orphan_lock);
2366
2367 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002368 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002369 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002370 }
2371
Josef Bacik8a35d952012-05-23 14:26:42 -04002372 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002373 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002374 atomic_dec(&root->orphan_inodes);
2375 }
Josef Bacik7b128762008-07-24 12:17:14 -04002376
Yan, Zhengd68fc572010-05-16 10:49:58 -04002377 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002378}
2379
2380/*
2381 * this cleans up any orphans that may be left on the list from the last use
2382 * of this root.
2383 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002384int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002385{
2386 struct btrfs_path *path;
2387 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002388 struct btrfs_key key, found_key;
2389 struct btrfs_trans_handle *trans;
2390 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002391 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002392 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2393
Yan, Zhengd68fc572010-05-16 10:49:58 -04002394 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002395 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002396
2397 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002398 if (!path) {
2399 ret = -ENOMEM;
2400 goto out;
2401 }
Josef Bacik7b128762008-07-24 12:17:14 -04002402 path->reada = -1;
2403
2404 key.objectid = BTRFS_ORPHAN_OBJECTID;
2405 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2406 key.offset = (u64)-1;
2407
Josef Bacik7b128762008-07-24 12:17:14 -04002408 while (1) {
2409 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002410 if (ret < 0)
2411 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002412
2413 /*
2414 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002415 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002416 * find the key and see if we have stuff that matches
2417 */
2418 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002419 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002420 if (path->slots[0] == 0)
2421 break;
2422 path->slots[0]--;
2423 }
2424
2425 /* pull out the item */
2426 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002427 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2428
2429 /* make sure the item matches what we want */
2430 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2431 break;
2432 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2433 break;
2434
2435 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002436 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002437
2438 /*
2439 * this is where we are basically btrfs_lookup, without the
2440 * crossing root thing. we store the inode number in the
2441 * offset of the orphan item.
2442 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002443
2444 if (found_key.offset == last_objectid) {
2445 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2446 "stopping orphan cleanup\n");
2447 ret = -EINVAL;
2448 goto out;
2449 }
2450
2451 last_objectid = found_key.offset;
2452
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002453 found_key.objectid = found_key.offset;
2454 found_key.type = BTRFS_INODE_ITEM_KEY;
2455 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002456 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002457 ret = PTR_RET(inode);
2458 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002459 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002460
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002461 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2462 struct btrfs_root *dead_root;
2463 struct btrfs_fs_info *fs_info = root->fs_info;
2464 int is_dead_root = 0;
2465
2466 /*
2467 * this is an orphan in the tree root. Currently these
2468 * could come from 2 sources:
2469 * a) a snapshot deletion in progress
2470 * b) a free space cache inode
2471 * We need to distinguish those two, as the snapshot
2472 * orphan must not get deleted.
2473 * find_dead_roots already ran before us, so if this
2474 * is a snapshot deletion, we should find the root
2475 * in the dead_roots list
2476 */
2477 spin_lock(&fs_info->trans_lock);
2478 list_for_each_entry(dead_root, &fs_info->dead_roots,
2479 root_list) {
2480 if (dead_root->root_key.objectid ==
2481 found_key.objectid) {
2482 is_dead_root = 1;
2483 break;
2484 }
2485 }
2486 spin_unlock(&fs_info->trans_lock);
2487 if (is_dead_root) {
2488 /* prevent this orphan from being found again */
2489 key.offset = found_key.objectid - 1;
2490 continue;
2491 }
2492 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002493 /*
2494 * Inode is already gone but the orphan item is still there,
2495 * kill the orphan item.
2496 */
2497 if (ret == -ESTALE) {
2498 trans = btrfs_start_transaction(root, 1);
2499 if (IS_ERR(trans)) {
2500 ret = PTR_ERR(trans);
2501 goto out;
2502 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002503 printk(KERN_ERR "auto deleting %Lu\n",
2504 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002505 ret = btrfs_del_orphan_item(trans, root,
2506 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002507 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002508 btrfs_end_transaction(trans, root);
2509 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002510 }
Josef Bacik7b128762008-07-24 12:17:14 -04002511
Josef Bacik7b128762008-07-24 12:17:14 -04002512 /*
2513 * add this inode to the orphan list so btrfs_orphan_del does
2514 * the proper thing when we hit it
2515 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002516 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2517 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05002518 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04002519
Josef Bacik7b128762008-07-24 12:17:14 -04002520 /* if we have links, this was a truncate, lets do that */
2521 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002522 if (!S_ISREG(inode->i_mode)) {
2523 WARN_ON(1);
2524 iput(inode);
2525 continue;
2526 }
Josef Bacik7b128762008-07-24 12:17:14 -04002527 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002528
2529 /* 1 for the orphan item deletion. */
2530 trans = btrfs_start_transaction(root, 1);
2531 if (IS_ERR(trans)) {
2532 ret = PTR_ERR(trans);
2533 goto out;
2534 }
2535 ret = btrfs_orphan_add(trans, inode);
2536 btrfs_end_transaction(trans, root);
2537 if (ret)
2538 goto out;
2539
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002540 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05002541 if (ret)
2542 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002543 } else {
2544 nr_unlink++;
2545 }
2546
2547 /* this will do delete_inode and everything for us */
2548 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002549 if (ret)
2550 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002551 }
Miao Xie3254c872011-11-10 20:45:05 -05002552 /* release the path since we're done with it */
2553 btrfs_release_path(path);
2554
Yan, Zhengd68fc572010-05-16 10:49:58 -04002555 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2556
2557 if (root->orphan_block_rsv)
2558 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2559 (u64)-1);
2560
2561 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002562 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002563 if (!IS_ERR(trans))
2564 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002565 }
Josef Bacik7b128762008-07-24 12:17:14 -04002566
2567 if (nr_unlink)
2568 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2569 if (nr_truncate)
2570 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002571
2572out:
2573 if (ret)
2574 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2575 btrfs_free_path(path);
2576 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002577}
2578
Chris Masond352ac62008-09-29 15:18:18 -04002579/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002580 * very simple check to peek ahead in the leaf looking for xattrs. If we
2581 * don't find any xattrs, we know there can't be any acls.
2582 *
2583 * slot is the slot the inode is in, objectid is the objectid of the inode
2584 */
2585static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2586 int slot, u64 objectid)
2587{
2588 u32 nritems = btrfs_header_nritems(leaf);
2589 struct btrfs_key found_key;
2590 int scanned = 0;
2591
2592 slot++;
2593 while (slot < nritems) {
2594 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2595
2596 /* we found a different objectid, there must not be acls */
2597 if (found_key.objectid != objectid)
2598 return 0;
2599
2600 /* we found an xattr, assume we've got an acl */
2601 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2602 return 1;
2603
2604 /*
2605 * we found a key greater than an xattr key, there can't
2606 * be any acls later on
2607 */
2608 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2609 return 0;
2610
2611 slot++;
2612 scanned++;
2613
2614 /*
2615 * it goes inode, inode backrefs, xattrs, extents,
2616 * so if there are a ton of hard links to an inode there can
2617 * be a lot of backrefs. Don't waste time searching too hard,
2618 * this is just an optimization
2619 */
2620 if (scanned >= 8)
2621 break;
2622 }
2623 /* we hit the end of the leaf before we found an xattr or
2624 * something larger than an xattr. We have to assume the inode
2625 * has acls
2626 */
2627 return 1;
2628}
2629
2630/*
Chris Masond352ac62008-09-29 15:18:18 -04002631 * read an inode from the btree into the in-memory inode
2632 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002633static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002634{
2635 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002636 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002637 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002638 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002639 struct btrfs_root *root = BTRFS_I(inode)->root;
2640 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002641 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002642 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002643 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002644 bool filled = false;
2645
2646 ret = btrfs_fill_inode(inode, &rdev);
2647 if (!ret)
2648 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002649
2650 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002651 if (!path)
2652 goto make_bad;
2653
Josef Bacikd90c7322011-05-17 09:50:54 -04002654 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002655 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002656
Chris Mason39279cc2007-06-12 06:35:45 -04002657 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002658 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002659 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002660
Chris Mason5f39d392007-10-15 16:14:19 -04002661 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002662
2663 if (filled)
2664 goto cache_acl;
2665
Chris Mason5f39d392007-10-15 16:14:19 -04002666 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2667 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002668 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002669 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002670 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2671 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002672 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002673
2674 tspec = btrfs_inode_atime(inode_item);
2675 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2676 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2677
2678 tspec = btrfs_inode_mtime(inode_item);
2679 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2680 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2681
2682 tspec = btrfs_inode_ctime(inode_item);
2683 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2684 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2685
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002686 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002687 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002688 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2689
2690 /*
2691 * If we were modified in the current generation and evicted from memory
2692 * and then re-read we need to do a full sync since we don't have any
2693 * idea about which extents were modified before we were evicted from
2694 * cache.
2695 */
2696 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2697 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2698 &BTRFS_I(inode)->runtime_flags);
2699
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002700 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002701 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002702 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002703 rdev = btrfs_inode_rdev(leaf, inode_item);
2704
Josef Bacikaec74772008-07-24 12:12:38 -04002705 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002706 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002707cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002708 /*
2709 * try to precache a NULL acl entry for files that don't have
2710 * any xattrs or acls
2711 */
Li Zefan33345d012011-04-20 10:31:50 +08002712 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2713 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002714 if (!maybe_acls)
2715 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002716
Chris Mason39279cc2007-06-12 06:35:45 -04002717 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002718
Chris Mason39279cc2007-06-12 06:35:45 -04002719 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002720 case S_IFREG:
2721 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002722 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002723 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002724 inode->i_fop = &btrfs_file_operations;
2725 inode->i_op = &btrfs_file_inode_operations;
2726 break;
2727 case S_IFDIR:
2728 inode->i_fop = &btrfs_dir_file_operations;
2729 if (root == root->fs_info->tree_root)
2730 inode->i_op = &btrfs_dir_ro_inode_operations;
2731 else
2732 inode->i_op = &btrfs_dir_inode_operations;
2733 break;
2734 case S_IFLNK:
2735 inode->i_op = &btrfs_symlink_inode_operations;
2736 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002737 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002738 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002739 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002740 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002741 init_special_inode(inode, inode->i_mode, rdev);
2742 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002743 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002744
2745 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002746 return;
2747
2748make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002749 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002750 make_bad_inode(inode);
2751}
2752
Chris Masond352ac62008-09-29 15:18:18 -04002753/*
2754 * given a leaf and an inode, copy the inode fields into the leaf
2755 */
Chris Masone02119d2008-09-05 16:13:11 -04002756static void fill_inode_item(struct btrfs_trans_handle *trans,
2757 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002758 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002759 struct inode *inode)
2760{
Liu Bo51fab692012-12-27 09:01:21 +00002761 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002762
Liu Bo51fab692012-12-27 09:01:21 +00002763 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04002764
Liu Bo51fab692012-12-27 09:01:21 +00002765 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2766 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2767 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
2768 &token);
2769 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2770 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002771
Liu Bo51fab692012-12-27 09:01:21 +00002772 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2773 inode->i_atime.tv_sec, &token);
2774 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2775 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002776
Liu Bo51fab692012-12-27 09:01:21 +00002777 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2778 inode->i_mtime.tv_sec, &token);
2779 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
2780 inode->i_mtime.tv_nsec, &token);
2781
2782 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
2783 inode->i_ctime.tv_sec, &token);
2784 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
2785 inode->i_ctime.tv_nsec, &token);
2786
2787 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
2788 &token);
2789 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
2790 &token);
2791 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
2792 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
2793 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
2794 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
2795 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04002796}
2797
Chris Masond352ac62008-09-29 15:18:18 -04002798/*
2799 * copy everything in the in-memory inode into the btree.
2800 */
Chris Mason21151332011-11-10 20:39:08 -05002801static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002802 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002803{
2804 struct btrfs_inode_item *inode_item;
2805 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002806 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002807 int ret;
2808
2809 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002810 if (!path)
2811 return -ENOMEM;
2812
Chris Masonb9473432009-03-13 11:00:37 -04002813 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002814 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2815 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002816 if (ret) {
2817 if (ret > 0)
2818 ret = -ENOENT;
2819 goto failed;
2820 }
2821
Chris Masonb4ce94d2009-02-04 09:25:08 -05002822 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002823 leaf = path->nodes[0];
2824 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002825 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002826
Chris Masone02119d2008-09-05 16:13:11 -04002827 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002828 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002829 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002830 ret = 0;
2831failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002832 btrfs_free_path(path);
2833 return ret;
2834}
2835
Chris Masond352ac62008-09-29 15:18:18 -04002836/*
Chris Mason21151332011-11-10 20:39:08 -05002837 * copy everything in the in-memory inode into the btree.
2838 */
2839noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2840 struct btrfs_root *root, struct inode *inode)
2841{
2842 int ret;
2843
2844 /*
2845 * If the inode is a free space inode, we can deadlock during commit
2846 * if we put it into the delayed code.
2847 *
2848 * The data relocation inode should also be directly updated
2849 * without delay
2850 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002851 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002852 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002853 btrfs_update_root_times(trans, root);
2854
Chris Mason21151332011-11-10 20:39:08 -05002855 ret = btrfs_delayed_update_inode(trans, root, inode);
2856 if (!ret)
2857 btrfs_set_inode_last_trans(trans, inode);
2858 return ret;
2859 }
2860
2861 return btrfs_update_inode_item(trans, root, inode);
2862}
2863
Josef Bacikbe6aef62012-10-22 15:43:12 -04002864noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2865 struct btrfs_root *root,
2866 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002867{
2868 int ret;
2869
2870 ret = btrfs_update_inode(trans, root, inode);
2871 if (ret == -ENOSPC)
2872 return btrfs_update_inode_item(trans, root, inode);
2873 return ret;
2874}
2875
2876/*
Chris Masond352ac62008-09-29 15:18:18 -04002877 * unlink helper that gets used here in inode.c and in the tree logging
2878 * recovery code. It remove a link in a directory with a given name, and
2879 * also drops the back refs in the inode to the directory
2880 */
Al Viro92986792011-03-04 17:14:37 +00002881static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2882 struct btrfs_root *root,
2883 struct inode *dir, struct inode *inode,
2884 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002885{
2886 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002887 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002888 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002889 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002890 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002891 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002892 u64 ino = btrfs_ino(inode);
2893 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002894
2895 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002896 if (!path) {
2897 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002898 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002899 }
2900
Chris Masonb9473432009-03-13 11:00:37 -04002901 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002902 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002903 name, name_len, -1);
2904 if (IS_ERR(di)) {
2905 ret = PTR_ERR(di);
2906 goto err;
2907 }
2908 if (!di) {
2909 ret = -ENOENT;
2910 goto err;
2911 }
Chris Mason5f39d392007-10-15 16:14:19 -04002912 leaf = path->nodes[0];
2913 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002914 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002915 if (ret)
2916 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002917 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002918
Li Zefan33345d012011-04-20 10:31:50 +08002919 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2920 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002921 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002922 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002923 "inode %llu parent %llu\n", name_len, name,
2924 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002925 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002926 goto err;
2927 }
2928
Miao Xie16cdcec2011-04-22 18:12:22 +08002929 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002930 if (ret) {
2931 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002932 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002933 }
Chris Mason39279cc2007-06-12 06:35:45 -04002934
Chris Masone02119d2008-09-05 16:13:11 -04002935 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002936 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002937 if (ret != 0 && ret != -ENOENT) {
2938 btrfs_abort_transaction(trans, root, ret);
2939 goto err;
2940 }
Chris Masone02119d2008-09-05 16:13:11 -04002941
2942 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2943 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002944 if (ret == -ENOENT)
2945 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002946err:
2947 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002948 if (ret)
2949 goto out;
2950
2951 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002952 inode_inc_iversion(inode);
2953 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002954 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002955 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002956out:
Chris Mason39279cc2007-06-12 06:35:45 -04002957 return ret;
2958}
2959
Al Viro92986792011-03-04 17:14:37 +00002960int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2961 struct btrfs_root *root,
2962 struct inode *dir, struct inode *inode,
2963 const char *name, int name_len)
2964{
2965 int ret;
2966 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2967 if (!ret) {
2968 btrfs_drop_nlink(inode);
2969 ret = btrfs_update_inode(trans, root, inode);
2970 }
2971 return ret;
2972}
2973
2974
Yan, Zhenga22285a2010-05-16 10:48:46 -04002975/* helper to check if there is any shared block in the path */
2976static int check_path_shared(struct btrfs_root *root,
2977 struct btrfs_path *path)
2978{
2979 struct extent_buffer *eb;
2980 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002981 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002982
2983 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002984 int ret;
2985
Yan, Zhenga22285a2010-05-16 10:48:46 -04002986 if (!path->nodes[level])
2987 break;
2988 eb = path->nodes[level];
2989 if (!btrfs_block_can_be_shared(root, eb))
2990 continue;
2991 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2992 &refs, NULL);
2993 if (refs > 1)
2994 return 1;
2995 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002996 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002997}
2998
2999/*
3000 * helper to start transaction for unlink and rmdir.
3001 *
3002 * unlink and rmdir are special in btrfs, they do not always free space.
3003 * so in enospc case, we should make sure they will free space before
3004 * allowing them to use the global metadata reservation.
3005 */
3006static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3007 struct dentry *dentry)
3008{
3009 struct btrfs_trans_handle *trans;
3010 struct btrfs_root *root = BTRFS_I(dir)->root;
3011 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003012 struct btrfs_dir_item *di;
3013 struct inode *inode = dentry->d_inode;
3014 u64 index;
3015 int check_link = 1;
3016 int err = -ENOSPC;
3017 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003018 u64 ino = btrfs_ino(inode);
3019 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003020
Josef Bacike70bea52011-10-11 14:18:24 -04003021 /*
3022 * 1 for the possible orphan item
3023 * 1 for the dir item
3024 * 1 for the dir index
3025 * 1 for the inode ref
3026 * 1 for the inode ref in the tree log
3027 * 2 for the dir entries in the log
3028 * 1 for the inode
3029 */
3030 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003031 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3032 return trans;
3033
Li Zefan33345d012011-04-20 10:31:50 +08003034 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003035 return ERR_PTR(-ENOSPC);
3036
3037 /* check if there is someone else holds reference */
3038 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3039 return ERR_PTR(-ENOSPC);
3040
3041 if (atomic_read(&inode->i_count) > 2)
3042 return ERR_PTR(-ENOSPC);
3043
3044 if (xchg(&root->fs_info->enospc_unlink, 1))
3045 return ERR_PTR(-ENOSPC);
3046
3047 path = btrfs_alloc_path();
3048 if (!path) {
3049 root->fs_info->enospc_unlink = 0;
3050 return ERR_PTR(-ENOMEM);
3051 }
3052
Josef Bacik3880a1b2011-10-14 14:46:51 -04003053 /* 1 for the orphan item */
3054 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003055 if (IS_ERR(trans)) {
3056 btrfs_free_path(path);
3057 root->fs_info->enospc_unlink = 0;
3058 return trans;
3059 }
3060
3061 path->skip_locking = 1;
3062 path->search_commit_root = 1;
3063
3064 ret = btrfs_lookup_inode(trans, root, path,
3065 &BTRFS_I(dir)->location, 0);
3066 if (ret < 0) {
3067 err = ret;
3068 goto out;
3069 }
3070 if (ret == 0) {
3071 if (check_path_shared(root, path))
3072 goto out;
3073 } else {
3074 check_link = 0;
3075 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003076 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003077
3078 ret = btrfs_lookup_inode(trans, root, path,
3079 &BTRFS_I(inode)->location, 0);
3080 if (ret < 0) {
3081 err = ret;
3082 goto out;
3083 }
3084 if (ret == 0) {
3085 if (check_path_shared(root, path))
3086 goto out;
3087 } else {
3088 check_link = 0;
3089 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003090 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003091
3092 if (ret == 0 && S_ISREG(inode->i_mode)) {
3093 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003094 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003095 if (ret < 0) {
3096 err = ret;
3097 goto out;
3098 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003099 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003100 if (check_path_shared(root, path))
3101 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003102 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003103 }
3104
3105 if (!check_link) {
3106 err = 0;
3107 goto out;
3108 }
3109
Li Zefan33345d012011-04-20 10:31:50 +08003110 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003111 dentry->d_name.name, dentry->d_name.len, 0);
3112 if (IS_ERR(di)) {
3113 err = PTR_ERR(di);
3114 goto out;
3115 }
3116 if (di) {
3117 if (check_path_shared(root, path))
3118 goto out;
3119 } else {
3120 err = 0;
3121 goto out;
3122 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003123 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003124
Mark Fashehf1863732012-08-08 11:32:27 -07003125 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3126 dentry->d_name.len, ino, dir_ino, 0,
3127 &index);
3128 if (ret) {
3129 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003130 goto out;
3131 }
Mark Fashehf1863732012-08-08 11:32:27 -07003132
Yan, Zhenga22285a2010-05-16 10:48:46 -04003133 if (check_path_shared(root, path))
3134 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003135
David Sterbab3b4aa72011-04-21 01:20:15 +02003136 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003137
Miao Xie16cdcec2011-04-22 18:12:22 +08003138 /*
3139 * This is a commit root search, if we can lookup inode item and other
3140 * relative items in the commit root, it means the transaction of
3141 * dir/file creation has been committed, and the dir index item that we
3142 * delay to insert has also been inserted into the commit root. So
3143 * we needn't worry about the delayed insertion of the dir index item
3144 * here.
3145 */
Li Zefan33345d012011-04-20 10:31:50 +08003146 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003147 dentry->d_name.name, dentry->d_name.len, 0);
3148 if (IS_ERR(di)) {
3149 err = PTR_ERR(di);
3150 goto out;
3151 }
3152 BUG_ON(ret == -ENOENT);
3153 if (check_path_shared(root, path))
3154 goto out;
3155
3156 err = 0;
3157out:
3158 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003159 /* Migrate the orphan reservation over */
3160 if (!err)
3161 err = btrfs_block_rsv_migrate(trans->block_rsv,
3162 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003163 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003164
Yan, Zhenga22285a2010-05-16 10:48:46 -04003165 if (err) {
3166 btrfs_end_transaction(trans, root);
3167 root->fs_info->enospc_unlink = 0;
3168 return ERR_PTR(err);
3169 }
3170
3171 trans->block_rsv = &root->fs_info->global_block_rsv;
3172 return trans;
3173}
3174
3175static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3176 struct btrfs_root *root)
3177{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003178 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003179 btrfs_block_rsv_release(root, trans->block_rsv,
3180 trans->bytes_reserved);
3181 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003182 BUG_ON(!root->fs_info->enospc_unlink);
3183 root->fs_info->enospc_unlink = 0;
3184 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003185 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003186}
3187
Chris Mason39279cc2007-06-12 06:35:45 -04003188static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3189{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003190 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003191 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003192 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003193 int ret;
3194
Yan, Zhenga22285a2010-05-16 10:48:46 -04003195 trans = __unlink_start_trans(dir, dentry);
3196 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003197 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003198
Chris Mason12fcfd22009-03-24 10:24:20 -04003199 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3200
Chris Masone02119d2008-09-05 16:13:11 -04003201 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3202 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003203 if (ret)
3204 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003205
Yan, Zhenga22285a2010-05-16 10:48:46 -04003206 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003207 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003208 if (ret)
3209 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003210 }
Josef Bacik7b128762008-07-24 12:17:14 -04003211
Tsutomu Itohb5324022011-07-19 07:27:20 +00003212out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003213 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003214 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003215 return ret;
3216}
3217
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003218int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3219 struct btrfs_root *root,
3220 struct inode *dir, u64 objectid,
3221 const char *name, int name_len)
3222{
3223 struct btrfs_path *path;
3224 struct extent_buffer *leaf;
3225 struct btrfs_dir_item *di;
3226 struct btrfs_key key;
3227 u64 index;
3228 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003229 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003230
3231 path = btrfs_alloc_path();
3232 if (!path)
3233 return -ENOMEM;
3234
Li Zefan33345d012011-04-20 10:31:50 +08003235 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003236 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003237 if (IS_ERR_OR_NULL(di)) {
3238 if (!di)
3239 ret = -ENOENT;
3240 else
3241 ret = PTR_ERR(di);
3242 goto out;
3243 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003244
3245 leaf = path->nodes[0];
3246 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3247 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3248 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003249 if (ret) {
3250 btrfs_abort_transaction(trans, root, ret);
3251 goto out;
3252 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003253 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003254
3255 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3256 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003257 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003258 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003259 if (ret != -ENOENT) {
3260 btrfs_abort_transaction(trans, root, ret);
3261 goto out;
3262 }
Li Zefan33345d012011-04-20 10:31:50 +08003263 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003264 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003265 if (IS_ERR_OR_NULL(di)) {
3266 if (!di)
3267 ret = -ENOENT;
3268 else
3269 ret = PTR_ERR(di);
3270 btrfs_abort_transaction(trans, root, ret);
3271 goto out;
3272 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003273
3274 leaf = path->nodes[0];
3275 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003276 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003277 index = key.offset;
3278 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003279 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003280
Miao Xie16cdcec2011-04-22 18:12:22 +08003281 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003282 if (ret) {
3283 btrfs_abort_transaction(trans, root, ret);
3284 goto out;
3285 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003286
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003287 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003288 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003289 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003290 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003291 if (ret)
3292 btrfs_abort_transaction(trans, root, ret);
3293out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003294 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003295 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003296}
3297
Chris Mason39279cc2007-06-12 06:35:45 -04003298static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3299{
3300 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003301 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003302 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003303 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003304
David Sterbab3ae2442012-09-13 16:04:34 -06003305 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003306 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003307 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3308 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003309
Yan, Zhenga22285a2010-05-16 10:48:46 -04003310 trans = __unlink_start_trans(dir, dentry);
3311 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003312 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003313
Li Zefan33345d012011-04-20 10:31:50 +08003314 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003315 err = btrfs_unlink_subvol(trans, root, dir,
3316 BTRFS_I(inode)->location.objectid,
3317 dentry->d_name.name,
3318 dentry->d_name.len);
3319 goto out;
3320 }
3321
Josef Bacik7b128762008-07-24 12:17:14 -04003322 err = btrfs_orphan_add(trans, inode);
3323 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003324 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003325
Chris Mason39279cc2007-06-12 06:35:45 -04003326 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003327 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3328 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003329 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003330 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003331out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003332 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003333 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003334
Chris Mason39279cc2007-06-12 06:35:45 -04003335 return err;
3336}
3337
Chris Mason323ac952008-10-01 19:05:46 -04003338/*
Chris Mason39279cc2007-06-12 06:35:45 -04003339 * this can truncate away extent items, csum items and directory items.
3340 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003341 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003342 *
3343 * csum items that cross the new i_size are truncated to the new size
3344 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003345 *
3346 * min_type is the minimum key type to truncate down to. If set to 0, this
3347 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003348 */
Yan, Zheng80825102009-11-12 09:35:36 +00003349int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3350 struct btrfs_root *root,
3351 struct inode *inode,
3352 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003353{
Chris Mason39279cc2007-06-12 06:35:45 -04003354 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003355 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003356 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003357 struct btrfs_key key;
3358 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003359 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003360 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003361 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003362 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003363 u64 mask = root->sectorsize - 1;
3364 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003365 int found_extent;
3366 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003367 int pending_del_nr = 0;
3368 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003369 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003370 int ret;
3371 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003372 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003373
3374 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003375
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003376 path = btrfs_alloc_path();
3377 if (!path)
3378 return -ENOMEM;
3379 path->reada = -1;
3380
Josef Bacik5dc562c2012-08-17 13:14:17 -04003381 /*
3382 * We want to drop from the next block forward in case this new size is
3383 * not block aligned since we will be keeping the last block of the
3384 * extent just the way it is.
3385 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003386 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003387 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003388
Miao Xie16cdcec2011-04-22 18:12:22 +08003389 /*
3390 * This function is also used to drop the items in the log tree before
3391 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3392 * it is used to drop the loged items. So we shouldn't kill the delayed
3393 * items.
3394 */
3395 if (min_type == 0 && root == BTRFS_I(inode)->root)
3396 btrfs_kill_delayed_inode_items(inode);
3397
Li Zefan33345d012011-04-20 10:31:50 +08003398 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003399 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003400 key.type = (u8)-1;
3401
Chris Mason85e21ba2008-01-29 15:11:36 -05003402search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003403 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003404 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003405 if (ret < 0) {
3406 err = ret;
3407 goto out;
3408 }
Chris Masond3977122009-01-05 21:25:51 -05003409
Chris Mason85e21ba2008-01-29 15:11:36 -05003410 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003411 /* there are no items in the tree for us to truncate, we're
3412 * done
3413 */
Yan, Zheng80825102009-11-12 09:35:36 +00003414 if (path->slots[0] == 0)
3415 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003416 path->slots[0]--;
3417 }
3418
Chris Masond3977122009-01-05 21:25:51 -05003419 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003420 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003421 leaf = path->nodes[0];
3422 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3423 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003424
Li Zefan33345d012011-04-20 10:31:50 +08003425 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003426 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003427
Chris Mason85e21ba2008-01-29 15:11:36 -05003428 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003429 break;
3430
Chris Mason5f39d392007-10-15 16:14:19 -04003431 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003432 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003433 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003434 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003435 extent_type = btrfs_file_extent_type(leaf, fi);
3436 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003437 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003438 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003439 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003440 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003441 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003442 }
Yan008630c2007-11-07 13:31:09 -05003443 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003444 }
Yan, Zheng80825102009-11-12 09:35:36 +00003445 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003446 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003447 } else {
3448 if (item_end < new_size)
3449 break;
3450 if (found_key.offset >= new_size)
3451 del_item = 1;
3452 else
3453 del_item = 0;
3454 }
Chris Mason39279cc2007-06-12 06:35:45 -04003455 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003456 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003457 if (found_type != BTRFS_EXTENT_DATA_KEY)
3458 goto delete;
3459
3460 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003461 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003462 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003463 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003464 u64 orig_num_bytes =
3465 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003466 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003467 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003468 extent_num_bytes = extent_num_bytes &
3469 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003470 btrfs_set_file_extent_num_bytes(leaf, fi,
3471 extent_num_bytes);
3472 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003473 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003474 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003475 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003476 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003477 } else {
Chris Masondb945352007-10-15 16:15:53 -04003478 extent_num_bytes =
3479 btrfs_file_extent_disk_num_bytes(leaf,
3480 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003481 extent_offset = found_key.offset -
3482 btrfs_file_extent_offset(leaf, fi);
3483
Chris Mason39279cc2007-06-12 06:35:45 -04003484 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003485 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003486 if (extent_start != 0) {
3487 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003488 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003489 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003490 }
3491 }
Chris Mason90692182008-02-08 13:49:28 -05003492 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003493 /*
3494 * we can't truncate inline items that have had
3495 * special encodings
3496 */
3497 if (!del_item &&
3498 btrfs_file_extent_compression(leaf, fi) == 0 &&
3499 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3500 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003501 u32 size = new_size - found_key.offset;
3502
3503 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003504 inode_sub_bytes(inode, item_end + 1 -
3505 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003506 }
3507 size =
3508 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003509 btrfs_truncate_item(trans, root, path,
3510 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003511 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003512 inode_sub_bytes(inode, item_end + 1 -
3513 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003514 }
Chris Mason39279cc2007-06-12 06:35:45 -04003515 }
Chris Mason179e29e2007-11-01 11:28:41 -04003516delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003517 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003518 if (!pending_del_nr) {
3519 /* no pending yet, add ourselves */
3520 pending_del_slot = path->slots[0];
3521 pending_del_nr = 1;
3522 } else if (pending_del_nr &&
3523 path->slots[0] + 1 == pending_del_slot) {
3524 /* hop on the pending chunk */
3525 pending_del_nr++;
3526 pending_del_slot = path->slots[0];
3527 } else {
Chris Masond3977122009-01-05 21:25:51 -05003528 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003529 }
Chris Mason39279cc2007-06-12 06:35:45 -04003530 } else {
3531 break;
3532 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003533 if (found_extent && (root->ref_cows ||
3534 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003535 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003536 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003537 extent_num_bytes, 0,
3538 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003539 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003540 BUG_ON(ret);
3541 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003542
Yan, Zheng80825102009-11-12 09:35:36 +00003543 if (found_type == BTRFS_INODE_ITEM_KEY)
3544 break;
3545
3546 if (path->slots[0] == 0 ||
3547 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003548 if (pending_del_nr) {
3549 ret = btrfs_del_items(trans, root, path,
3550 pending_del_slot,
3551 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003552 if (ret) {
3553 btrfs_abort_transaction(trans,
3554 root, ret);
3555 goto error;
3556 }
Yan, Zheng80825102009-11-12 09:35:36 +00003557 pending_del_nr = 0;
3558 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003559 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003560 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003561 } else {
3562 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003563 }
Chris Mason39279cc2007-06-12 06:35:45 -04003564 }
Yan, Zheng80825102009-11-12 09:35:36 +00003565out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003566 if (pending_del_nr) {
3567 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3568 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003569 if (ret)
3570 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003571 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003572error:
Chris Mason39279cc2007-06-12 06:35:45 -04003573 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003574 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003575}
3576
Chris Masona52d9a82007-08-27 16:49:44 -04003577/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003578 * btrfs_truncate_page - read, zero a chunk and write a page
3579 * @inode - inode that we're zeroing
3580 * @from - the offset to start zeroing
3581 * @len - the length to zero, 0 to zero the entire range respective to the
3582 * offset
3583 * @front - zero up to the offset instead of from the offset on
3584 *
3585 * This will find the page for the "from" offset and cow the page and zero the
3586 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003587 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003588int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3589 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003590{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003591 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003592 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003593 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3594 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003595 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003596 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003597 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003598 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3599 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3600 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003601 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003602 int ret = 0;
3603 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003604 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003605
Josef Bacik2aaa6652012-08-29 14:27:18 -04003606 if ((offset & (blocksize - 1)) == 0 &&
3607 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003608 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003609 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003610 if (ret)
3611 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003612
Chris Mason211c17f2008-05-15 09:13:45 -04003613again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003614 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003615 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003616 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003617 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003618 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003619 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003620
3621 page_start = page_offset(page);
3622 page_end = page_start + PAGE_CACHE_SIZE - 1;
3623
Chris Masona52d9a82007-08-27 16:49:44 -04003624 if (!PageUptodate(page)) {
3625 ret = btrfs_readpage(NULL, page);
3626 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003627 if (page->mapping != mapping) {
3628 unlock_page(page);
3629 page_cache_release(page);
3630 goto again;
3631 }
Chris Masona52d9a82007-08-27 16:49:44 -04003632 if (!PageUptodate(page)) {
3633 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003634 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003635 }
3636 }
Chris Mason211c17f2008-05-15 09:13:45 -04003637 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003638
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003639 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003640 set_page_extent_mapped(page);
3641
3642 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3643 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003644 unlock_extent_cached(io_tree, page_start, page_end,
3645 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003646 unlock_page(page);
3647 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003648 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003649 btrfs_put_ordered_extent(ordered);
3650 goto again;
3651 }
3652
Josef Bacik2ac55d42010-02-03 19:33:23 +00003653 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003654 EXTENT_DIRTY | EXTENT_DELALLOC |
3655 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003656 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003657
Josef Bacik2ac55d42010-02-03 19:33:23 +00003658 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3659 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003660 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003661 unlock_extent_cached(io_tree, page_start, page_end,
3662 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003663 goto out_unlock;
3664 }
3665
Chris Masone6dcd2d2008-07-17 12:53:50 -04003666 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003667 if (!len)
3668 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003669 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003670 if (front)
3671 memset(kaddr, 0, offset);
3672 else
3673 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003674 flush_dcache_page(page);
3675 kunmap(page);
3676 }
Chris Mason247e7432008-07-17 12:53:51 -04003677 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003678 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003679 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3680 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003681
Chris Mason89642222008-07-24 09:41:53 -04003682out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003683 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003684 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003685 unlock_page(page);
3686 page_cache_release(page);
3687out:
3688 return ret;
3689}
3690
Josef Bacik695a0d02011-03-04 15:46:53 -05003691/*
3692 * This function puts in dummy file extents for the area we're creating a hole
3693 * for. So if we are truncating this file to a larger size we need to insert
3694 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3695 * the range between oldsize and size
3696 */
Josef Bacika41ad392011-01-31 15:30:16 -05003697int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003698{
3699 struct btrfs_trans_handle *trans;
3700 struct btrfs_root *root = BTRFS_I(inode)->root;
3701 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003703 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003704 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003705 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003706 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003707 u64 block_end = (size + mask) & ~mask;
3708 u64 last_byte;
3709 u64 cur_offset;
3710 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003711 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003712
3713 if (size <= hole_start)
3714 return 0;
3715
Yan Zheng9036c102008-10-30 14:19:41 -04003716 while (1) {
3717 struct btrfs_ordered_extent *ordered;
3718 btrfs_wait_ordered_range(inode, hole_start,
3719 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003720 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003721 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003722 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3723 if (!ordered)
3724 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003725 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3726 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003727 btrfs_put_ordered_extent(ordered);
3728 }
3729
Yan Zheng9036c102008-10-30 14:19:41 -04003730 cur_offset = hole_start;
3731 while (1) {
3732 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3733 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003734 if (IS_ERR(em)) {
3735 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003736 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 break;
3738 }
Yan Zheng9036c102008-10-30 14:19:41 -04003739 last_byte = min(extent_map_end(em), block_end);
3740 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003741 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003742 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003743 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003744
Miao Xie36423202011-12-14 20:12:02 -05003745 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003746 if (IS_ERR(trans)) {
3747 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003748 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003749 }
Yan, Zheng80825102009-11-12 09:35:36 +00003750
Josef Bacik5dc562c2012-08-17 13:14:17 -04003751 err = btrfs_drop_extents(trans, root, inode,
3752 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003753 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003754 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003755 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003756 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003757 break;
Miao Xie5b397372011-09-11 10:52:24 -04003758 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003759
Yan Zheng9036c102008-10-30 14:19:41 -04003760 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003761 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003762 0, hole_size, 0, hole_size,
3763 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003764 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003765 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003766 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003767 break;
Miao Xie5b397372011-09-11 10:52:24 -04003768 }
Yan, Zheng80825102009-11-12 09:35:36 +00003769
Josef Bacik5dc562c2012-08-17 13:14:17 -04003770 btrfs_drop_extent_cache(inode, cur_offset,
3771 cur_offset + hole_size - 1, 0);
3772 hole_em = alloc_extent_map();
3773 if (!hole_em) {
3774 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3775 &BTRFS_I(inode)->runtime_flags);
3776 goto next;
3777 }
3778 hole_em->start = cur_offset;
3779 hole_em->len = hole_size;
3780 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003781
Josef Bacik5dc562c2012-08-17 13:14:17 -04003782 hole_em->block_start = EXTENT_MAP_HOLE;
3783 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003784 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003785 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3786 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3787 hole_em->generation = trans->transid;
3788
3789 while (1) {
3790 write_lock(&em_tree->lock);
3791 err = add_extent_mapping(em_tree, hole_em);
3792 if (!err)
3793 list_move(&hole_em->list,
3794 &em_tree->modified_extents);
3795 write_unlock(&em_tree->lock);
3796 if (err != -EEXIST)
3797 break;
3798 btrfs_drop_extent_cache(inode, cur_offset,
3799 cur_offset +
3800 hole_size - 1, 0);
3801 }
3802 free_extent_map(hole_em);
3803next:
Miao Xie36423202011-12-14 20:12:02 -05003804 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003805 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003806 }
3807 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003808 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003809 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003810 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003811 break;
3812 }
3813
Yan, Zhenga22285a2010-05-16 10:48:46 -04003814 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003815 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3816 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003817 return err;
3818}
3819
Eric Sandeen3972f262013-01-12 02:57:22 +00003820static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00003821{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003822 struct btrfs_root *root = BTRFS_I(inode)->root;
3823 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003824 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00003825 loff_t newsize = attr->ia_size;
3826 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00003827 int ret;
3828
Josef Bacika41ad392011-01-31 15:30:16 -05003829 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003830 return 0;
3831
Eric Sandeen3972f262013-01-12 02:57:22 +00003832 /*
3833 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3834 * special case where we need to update the times despite not having
3835 * these flags set. For all other operations the VFS set these flags
3836 * explicitly if it wants a timestamp update.
3837 */
3838 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3839 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3840
Josef Bacika41ad392011-01-31 15:30:16 -05003841 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003842 truncate_pagecache(inode, oldsize, newsize);
3843 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003844 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003845 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003846
Miao Xief4a2f4c2011-12-14 20:12:01 -05003847 trans = btrfs_start_transaction(root, 1);
3848 if (IS_ERR(trans))
3849 return PTR_ERR(trans);
3850
3851 i_size_write(inode, newsize);
3852 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3853 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003854 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003855 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003856
Josef Bacika41ad392011-01-31 15:30:16 -05003857 /*
3858 * We're truncating a file that used to have good data down to
3859 * zero. Make sure it gets into the ordered flush list so that
3860 * any new writes get down to disk quickly.
3861 */
3862 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003863 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3864 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003865
Josef Bacikf3fe8202013-01-07 17:03:21 -05003866 /*
3867 * 1 for the orphan item we're going to add
3868 * 1 for the orphan item deletion.
3869 */
3870 trans = btrfs_start_transaction(root, 2);
3871 if (IS_ERR(trans))
3872 return PTR_ERR(trans);
3873
3874 /*
3875 * We need to do this in case we fail at _any_ point during the
3876 * actual truncate. Once we do the truncate_setsize we could
3877 * invalidate pages which forces any outstanding ordered io to
3878 * be instantly completed which will give us extents that need
3879 * to be truncated. If we fail to get an orphan inode down we
3880 * could have left over extents that were never meant to live,
3881 * so we need to garuntee from this point on that everything
3882 * will be consistent.
3883 */
3884 ret = btrfs_orphan_add(trans, inode);
3885 btrfs_end_transaction(trans, root);
3886 if (ret)
3887 return ret;
3888
Josef Bacika41ad392011-01-31 15:30:16 -05003889 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3890 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00003891
3892 /* Disable nonlocked read DIO to avoid the end less truncate */
3893 btrfs_inode_block_unlocked_dio(inode);
3894 inode_dio_wait(inode);
3895 btrfs_inode_resume_unlocked_dio(inode);
3896
Josef Bacika41ad392011-01-31 15:30:16 -05003897 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003898 if (ret && inode->i_nlink)
3899 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003900 }
3901
Josef Bacika41ad392011-01-31 15:30:16 -05003902 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003903}
3904
Chris Mason39279cc2007-06-12 06:35:45 -04003905static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3906{
3907 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003908 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 int err;
3910
Li Zefanb83cc962010-12-20 16:04:08 +08003911 if (btrfs_root_readonly(root))
3912 return -EROFS;
3913
Chris Mason39279cc2007-06-12 06:35:45 -04003914 err = inode_change_ok(inode, attr);
3915 if (err)
3916 return err;
3917
Chris Mason5a3f23d2009-03-31 13:27:11 -04003918 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00003919 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00003920 if (err)
3921 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003922 }
Yan Zheng9036c102008-10-30 14:19:41 -04003923
Christoph Hellwig10257742010-06-04 11:30:02 +02003924 if (attr->ia_valid) {
3925 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003926 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003927 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003928
Josef Bacik22c44fe2011-11-30 10:45:38 -05003929 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003930 err = btrfs_acl_chmod(inode);
3931 }
3932
Chris Mason39279cc2007-06-12 06:35:45 -04003933 return err;
3934}
Chris Mason61295eb2008-01-14 16:24:38 -05003935
Al Virobd555972010-06-07 11:35:40 -04003936void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003937{
3938 struct btrfs_trans_handle *trans;
3939 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003940 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003941 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003942 int ret;
3943
liubo1abe9b82011-03-24 11:18:59 +00003944 trace_btrfs_inode_evict(inode);
3945
Chris Mason39279cc2007-06-12 06:35:45 -04003946 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003947 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003948 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003949 goto no_delete;
3950
Chris Mason39279cc2007-06-12 06:35:45 -04003951 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003952 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003953 goto no_delete;
3954 }
Al Virobd555972010-06-07 11:35:40 -04003955 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003956 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003957
Yan, Zhengc71bf092009-11-12 09:34:40 +00003958 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003959 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003960 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003961 goto no_delete;
3962 }
3963
Yan, Zheng76dda932009-09-21 16:00:26 -04003964 if (inode->i_nlink > 0) {
3965 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3966 goto no_delete;
3967 }
3968
Miao Xie0e8c36a2012-12-19 06:59:51 +00003969 ret = btrfs_commit_inode_delayed_inode(inode);
3970 if (ret) {
3971 btrfs_orphan_del(NULL, inode);
3972 goto no_delete;
3973 }
3974
Miao Xie66d8f3d2012-09-06 04:02:28 -06003975 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003976 if (!rsv) {
3977 btrfs_orphan_del(NULL, inode);
3978 goto no_delete;
3979 }
Josef Bacik4a338542011-08-29 11:01:31 -04003980 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003981 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003982 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003983
Chris Masondbe674a2008-07-17 12:54:05 -04003984 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003985
Josef Bacik4289a662011-08-05 13:22:24 -04003986 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003987 * This is a bit simpler than btrfs_truncate since we've already
3988 * reserved our space for our orphan item in the unlink, so we just
3989 * need to reserve some slack space in case we add bytes and update
3990 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003991 */
Yan, Zheng80825102009-11-12 09:35:36 +00003992 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003993 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3994 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003995
Josef Bacik726c35f2011-09-26 15:46:06 -04003996 /*
3997 * Try and steal from the global reserve since we will
3998 * likely not use this space anyway, we want to try as
3999 * hard as possible to get this to work.
4000 */
4001 if (ret)
4002 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4003
Yan, Zhengd68fc572010-05-16 10:49:58 -04004004 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004005 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004006 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004007 btrfs_orphan_del(NULL, inode);
4008 btrfs_free_block_rsv(root, rsv);
4009 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004010 }
4011
Miao Xie0e8c36a2012-12-19 06:59:51 +00004012 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004013 if (IS_ERR(trans)) {
4014 btrfs_orphan_del(NULL, inode);
4015 btrfs_free_block_rsv(root, rsv);
4016 goto no_delete;
4017 }
4018
4019 trans->block_rsv = rsv;
4020
Yan, Zhengd68fc572010-05-16 10:49:58 -04004021 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004022 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004023 break;
4024
Miao Xie8407aa42012-09-07 01:43:32 -06004025 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004026 btrfs_end_transaction(trans, root);
4027 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004028 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004029 }
4030
Josef Bacik4289a662011-08-05 13:22:24 -04004031 btrfs_free_block_rsv(root, rsv);
4032
Yan, Zheng80825102009-11-12 09:35:36 +00004033 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004034 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004035 ret = btrfs_orphan_del(trans, inode);
4036 BUG_ON(ret);
4037 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004038
Josef Bacik4289a662011-08-05 13:22:24 -04004039 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004040 if (!(root == root->fs_info->tree_root ||
4041 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004042 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004043
Chris Mason54aa1f42007-06-22 14:16:25 -04004044 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004045 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004046no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004047 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004048 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004049}
4050
4051/*
4052 * this returns the key found in the dir entry in the location pointer.
4053 * If no dir entries were found, location->objectid is 0.
4054 */
4055static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4056 struct btrfs_key *location)
4057{
4058 const char *name = dentry->d_name.name;
4059 int namelen = dentry->d_name.len;
4060 struct btrfs_dir_item *di;
4061 struct btrfs_path *path;
4062 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004063 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004064
4065 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004066 if (!path)
4067 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004068
Li Zefan33345d012011-04-20 10:31:50 +08004069 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004070 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004071 if (IS_ERR(di))
4072 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004073
David Sterbac7040052011-04-19 18:00:01 +02004074 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004075 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004076
Chris Mason5f39d392007-10-15 16:14:19 -04004077 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004078out:
Chris Mason39279cc2007-06-12 06:35:45 -04004079 btrfs_free_path(path);
4080 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004081out_err:
4082 location->objectid = 0;
4083 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004084}
4085
4086/*
4087 * when we hit a tree root in a directory, the btrfs part of the inode
4088 * needs to be changed to reflect the root directory of the tree root. This
4089 * is kind of like crossing a mount point.
4090 */
4091static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 struct inode *dir,
4093 struct dentry *dentry,
4094 struct btrfs_key *location,
4095 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004096{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097 struct btrfs_path *path;
4098 struct btrfs_root *new_root;
4099 struct btrfs_root_ref *ref;
4100 struct extent_buffer *leaf;
4101 int ret;
4102 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004103
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004104 path = btrfs_alloc_path();
4105 if (!path) {
4106 err = -ENOMEM;
4107 goto out;
4108 }
Chris Mason39279cc2007-06-12 06:35:45 -04004109
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110 err = -ENOENT;
4111 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4112 BTRFS_I(dir)->root->root_key.objectid,
4113 location->objectid);
4114 if (ret) {
4115 if (ret < 0)
4116 err = ret;
4117 goto out;
4118 }
Chris Mason39279cc2007-06-12 06:35:45 -04004119
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004120 leaf = path->nodes[0];
4121 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004122 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004123 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4124 goto out;
4125
4126 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4127 (unsigned long)(ref + 1),
4128 dentry->d_name.len);
4129 if (ret)
4130 goto out;
4131
David Sterbab3b4aa72011-04-21 01:20:15 +02004132 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133
4134 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4135 if (IS_ERR(new_root)) {
4136 err = PTR_ERR(new_root);
4137 goto out;
4138 }
4139
4140 if (btrfs_root_refs(&new_root->root_item) == 0) {
4141 err = -ENOENT;
4142 goto out;
4143 }
4144
4145 *sub_root = new_root;
4146 location->objectid = btrfs_root_dirid(&new_root->root_item);
4147 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004148 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149 err = 0;
4150out:
4151 btrfs_free_path(path);
4152 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004153}
4154
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004155static void inode_tree_add(struct inode *inode)
4156{
4157 struct btrfs_root *root = BTRFS_I(inode)->root;
4158 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004159 struct rb_node **p;
4160 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004161 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004162again:
4163 p = &root->inode_tree.rb_node;
4164 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004165
Al Viro1d3382cb2010-10-23 15:19:20 -04004166 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004167 return;
4168
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004169 spin_lock(&root->inode_lock);
4170 while (*p) {
4171 parent = *p;
4172 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4173
Li Zefan33345d012011-04-20 10:31:50 +08004174 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004175 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004176 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004177 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004178 else {
4179 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004180 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004181 rb_erase(parent, &root->inode_tree);
4182 RB_CLEAR_NODE(parent);
4183 spin_unlock(&root->inode_lock);
4184 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004185 }
4186 }
4187 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4188 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4189 spin_unlock(&root->inode_lock);
4190}
4191
4192static void inode_tree_del(struct inode *inode)
4193{
4194 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004195 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004196
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004197 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004198 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004199 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004200 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004201 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004202 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004203 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004204
Josef Bacik0af3d002010-06-21 14:48:16 -04004205 /*
4206 * Free space cache has inodes in the tree root, but the tree root has a
4207 * root_refs of 0, so this could end up dropping the tree root as a
4208 * snapshot, so we need the extra !root->fs_info->tree_root check to
4209 * make sure we don't drop it.
4210 */
4211 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4212 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004213 synchronize_srcu(&root->fs_info->subvol_srcu);
4214 spin_lock(&root->inode_lock);
4215 empty = RB_EMPTY_ROOT(&root->inode_tree);
4216 spin_unlock(&root->inode_lock);
4217 if (empty)
4218 btrfs_add_dead_root(root);
4219 }
4220}
4221
Jeff Mahoney143bede2012-03-01 14:56:26 +01004222void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004223{
4224 struct rb_node *node;
4225 struct rb_node *prev;
4226 struct btrfs_inode *entry;
4227 struct inode *inode;
4228 u64 objectid = 0;
4229
4230 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4231
4232 spin_lock(&root->inode_lock);
4233again:
4234 node = root->inode_tree.rb_node;
4235 prev = NULL;
4236 while (node) {
4237 prev = node;
4238 entry = rb_entry(node, struct btrfs_inode, rb_node);
4239
Li Zefan33345d012011-04-20 10:31:50 +08004240 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004241 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004242 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004243 node = node->rb_right;
4244 else
4245 break;
4246 }
4247 if (!node) {
4248 while (prev) {
4249 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004250 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004251 node = prev;
4252 break;
4253 }
4254 prev = rb_next(prev);
4255 }
4256 }
4257 while (node) {
4258 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004259 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004260 inode = igrab(&entry->vfs_inode);
4261 if (inode) {
4262 spin_unlock(&root->inode_lock);
4263 if (atomic_read(&inode->i_count) > 1)
4264 d_prune_aliases(inode);
4265 /*
Al Viro45321ac2010-06-07 13:43:19 -04004266 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004267 * the inode cache when its usage count
4268 * hits zero.
4269 */
4270 iput(inode);
4271 cond_resched();
4272 spin_lock(&root->inode_lock);
4273 goto again;
4274 }
4275
4276 if (cond_resched_lock(&root->inode_lock))
4277 goto again;
4278
4279 node = rb_next(node);
4280 }
4281 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004282}
4283
Chris Masone02119d2008-09-05 16:13:11 -04004284static int btrfs_init_locked_inode(struct inode *inode, void *p)
4285{
4286 struct btrfs_iget_args *args = p;
4287 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004288 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004289 return 0;
4290}
4291
4292static int btrfs_find_actor(struct inode *inode, void *opaque)
4293{
4294 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004295 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004296 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004297}
4298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004299static struct inode *btrfs_iget_locked(struct super_block *s,
4300 u64 objectid,
4301 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004302{
4303 struct inode *inode;
4304 struct btrfs_iget_args args;
4305 args.ino = objectid;
4306 args.root = root;
4307
4308 inode = iget5_locked(s, objectid, btrfs_find_actor,
4309 btrfs_init_locked_inode,
4310 (void *)&args);
4311 return inode;
4312}
4313
Balaji Rao1a54ef82008-07-21 02:01:04 +05304314/* Get an inode object given its location and corresponding root.
4315 * Returns in *is_new if the inode was read from disk
4316 */
4317struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004318 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304319{
4320 struct inode *inode;
4321
4322 inode = btrfs_iget_locked(s, location->objectid, root);
4323 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004324 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304325
4326 if (inode->i_state & I_NEW) {
4327 BTRFS_I(inode)->root = root;
4328 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4329 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004330 if (!is_bad_inode(inode)) {
4331 inode_tree_add(inode);
4332 unlock_new_inode(inode);
4333 if (new)
4334 *new = 1;
4335 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004336 unlock_new_inode(inode);
4337 iput(inode);
4338 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004339 }
4340 }
4341
Balaji Rao1a54ef82008-07-21 02:01:04 +05304342 return inode;
4343}
4344
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004345static struct inode *new_simple_dir(struct super_block *s,
4346 struct btrfs_key *key,
4347 struct btrfs_root *root)
4348{
4349 struct inode *inode = new_inode(s);
4350
4351 if (!inode)
4352 return ERR_PTR(-ENOMEM);
4353
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004354 BTRFS_I(inode)->root = root;
4355 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004356 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004357
4358 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004359 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004360 inode->i_fop = &simple_dir_operations;
4361 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4362 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4363
4364 return inode;
4365}
4366
Chris Mason3de45862008-11-17 21:02:50 -05004367struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004368{
Chris Masond3977122009-01-05 21:25:51 -05004369 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004370 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004371 struct btrfs_root *sub_root = root;
4372 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004373 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004374 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004375
4376 if (dentry->d_name.len > BTRFS_NAME_LEN)
4377 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004378
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004379 if (unlikely(d_need_lookup(dentry))) {
4380 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4381 kfree(dentry->d_fsdata);
4382 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004383 /* This thing is hashed, drop it for now */
4384 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004385 } else {
4386 ret = btrfs_inode_by_name(dir, dentry, &location);
4387 }
Chris Mason5f39d392007-10-15 16:14:19 -04004388
Chris Mason39279cc2007-06-12 06:35:45 -04004389 if (ret < 0)
4390 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004391
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004392 if (location.objectid == 0)
4393 return NULL;
4394
4395 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004396 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004397 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004399
4400 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4401
Yan, Zheng76dda932009-09-21 16:00:26 -04004402 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004403 ret = fixup_tree_root_location(root, dir, dentry,
4404 &location, &sub_root);
4405 if (ret < 0) {
4406 if (ret != -ENOENT)
4407 inode = ERR_PTR(ret);
4408 else
4409 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4410 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004411 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004412 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004413 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4414
Julia Lawall34d19ba2011-01-24 19:55:19 +00004415 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004416 down_read(&root->fs_info->cleanup_work_sem);
4417 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004418 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004419 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004420 if (ret)
4421 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004422 }
4423
Chris Mason3de45862008-11-17 21:02:50 -05004424 return inode;
4425}
4426
Nick Pigginfe15ce42011-01-07 17:49:23 +11004427static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004428{
4429 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004430 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004431
Li Zefan848cce02012-02-21 17:04:28 +08004432 if (!inode && !IS_ROOT(dentry))
4433 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004434
Li Zefan848cce02012-02-21 17:04:28 +08004435 if (inode) {
4436 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004437 if (btrfs_root_refs(&root->root_item) == 0)
4438 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004439
4440 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4441 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004442 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004443 return 0;
4444}
4445
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004446static void btrfs_dentry_release(struct dentry *dentry)
4447{
4448 if (dentry->d_fsdata)
4449 kfree(dentry->d_fsdata);
4450}
4451
Chris Mason3de45862008-11-17 21:02:50 -05004452static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004453 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004454{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004455 struct dentry *ret;
4456
4457 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4458 if (unlikely(d_need_lookup(dentry))) {
4459 spin_lock(&dentry->d_lock);
4460 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4461 spin_unlock(&dentry->d_lock);
4462 }
4463 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004464}
4465
Miao Xie16cdcec2011-04-22 18:12:22 +08004466unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004467 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4468};
4469
David Woodhousecbdf5a22008-08-06 19:42:33 +01004470static int btrfs_real_readdir(struct file *filp, void *dirent,
4471 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004472{
Chris Mason6da6aba2007-12-18 16:15:09 -05004473 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004474 struct btrfs_root *root = BTRFS_I(inode)->root;
4475 struct btrfs_item *item;
4476 struct btrfs_dir_item *di;
4477 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004478 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004480 struct list_head ins_list;
4481 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004482 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004483 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004484 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004485 unsigned char d_type;
4486 int over = 0;
4487 u32 di_cur;
4488 u32 di_total;
4489 u32 di_len;
4490 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004491 char tmp_name[32];
4492 char *name_ptr;
4493 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004494 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004495
4496 /* FIXME, use a real flag for deciding about the key type */
4497 if (root->fs_info->tree_root == root)
4498 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004499
Chris Mason39544012007-12-12 14:38:19 -05004500 /* special case for "." */
4501 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004502 over = filldir(dirent, ".", 1,
4503 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004504 if (over)
4505 return 0;
4506 filp->f_pos = 1;
4507 }
Chris Mason39544012007-12-12 14:38:19 -05004508 /* special case for .., just use the back ref */
4509 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004510 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004511 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004512 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004513 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004514 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004515 filp->f_pos = 2;
4516 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004517 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004518 if (!path)
4519 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004520
Josef Bacik026fd312011-05-13 10:32:11 -04004521 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004522
Miao Xie16cdcec2011-04-22 18:12:22 +08004523 if (key_type == BTRFS_DIR_INDEX_KEY) {
4524 INIT_LIST_HEAD(&ins_list);
4525 INIT_LIST_HEAD(&del_list);
4526 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4527 }
4528
Chris Mason39279cc2007-06-12 06:35:45 -04004529 btrfs_set_key_type(&key, key_type);
4530 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004531 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004532
Chris Mason39279cc2007-06-12 06:35:45 -04004533 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4534 if (ret < 0)
4535 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004536
4537 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004538 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004539 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004540 if (slot >= btrfs_header_nritems(leaf)) {
4541 ret = btrfs_next_leaf(root, path);
4542 if (ret < 0)
4543 goto err;
4544 else if (ret > 0)
4545 break;
4546 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004547 }
Chris Mason3de45862008-11-17 21:02:50 -05004548
Chris Mason5f39d392007-10-15 16:14:19 -04004549 item = btrfs_item_nr(leaf, slot);
4550 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4551
4552 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004553 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004554 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004555 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004556 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004557 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004558 if (key_type == BTRFS_DIR_INDEX_KEY &&
4559 btrfs_should_delete_dir_index(&del_list,
4560 found_key.offset))
4561 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004562
4563 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004564 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004565
Chris Mason39279cc2007-06-12 06:35:45 -04004566 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4567 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004568 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004569
4570 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004571 struct btrfs_key location;
4572
Josef Bacik22a94d42011-03-16 16:47:17 -04004573 if (verify_dir_item(root, leaf, di))
4574 break;
4575
Chris Mason5f39d392007-10-15 16:14:19 -04004576 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004577 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004578 name_ptr = tmp_name;
4579 } else {
4580 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004581 if (!name_ptr) {
4582 ret = -ENOMEM;
4583 goto err;
4584 }
Chris Mason5f39d392007-10-15 16:14:19 -04004585 }
4586 read_extent_buffer(leaf, name_ptr,
4587 (unsigned long)(di + 1), name_len);
4588
4589 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4590 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004591
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004592
Chris Mason3de45862008-11-17 21:02:50 -05004593 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004594 * skip it.
4595 *
4596 * In contrast to old kernels, we insert the snapshot's
4597 * dir item and dir index after it has been created, so
4598 * we won't find a reference to our own snapshot. We
4599 * still keep the following code for backward
4600 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004601 */
4602 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4603 location.objectid == root->root_key.objectid) {
4604 over = 0;
4605 goto skip;
4606 }
Chris Mason5f39d392007-10-15 16:14:19 -04004607 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004608 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004609 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004610
Chris Mason3de45862008-11-17 21:02:50 -05004611skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004612 if (name_ptr != tmp_name)
4613 kfree(name_ptr);
4614
Chris Mason39279cc2007-06-12 06:35:45 -04004615 if (over)
4616 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004617 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004618 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004619 di_cur += di_len;
4620 di = (struct btrfs_dir_item *)((char *)di + di_len);
4621 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004622next:
4623 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004624 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004625
Miao Xie16cdcec2011-04-22 18:12:22 +08004626 if (key_type == BTRFS_DIR_INDEX_KEY) {
4627 if (is_curr)
4628 filp->f_pos++;
4629 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4630 &ins_list);
4631 if (ret)
4632 goto nopos;
4633 }
4634
David Woodhouse49593bf2008-08-17 17:08:36 +01004635 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004636 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004637 /*
4638 * 32-bit glibc will use getdents64, but then strtol -
4639 * so the last number we can serve is this.
4640 */
4641 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004642 else
4643 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004644nopos:
4645 ret = 0;
4646err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004647 if (key_type == BTRFS_DIR_INDEX_KEY)
4648 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004649 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004650 return ret;
4651}
4652
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004653int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004654{
4655 struct btrfs_root *root = BTRFS_I(inode)->root;
4656 struct btrfs_trans_handle *trans;
4657 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004658 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004659
Josef Bacik72ac3c02012-05-23 14:13:11 -04004660 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004661 return 0;
4662
Liu Bo83eea1f2012-07-10 05:28:39 -06004663 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004664 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004665
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004666 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004667 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004668 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004669 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004670 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004671 if (IS_ERR(trans))
4672 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004673 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004674 }
4675 return ret;
4676}
4677
4678/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004679 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004680 * inode changes. But, it is most likely to find the inode in cache.
4681 * FIXME, needs more benchmarking...there are no reasons other than performance
4682 * to keep or drop this code.
4683 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004684int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004685{
4686 struct btrfs_root *root = BTRFS_I(inode)->root;
4687 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004688 int ret;
4689
Josef Bacik72ac3c02012-05-23 14:13:11 -04004690 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004691 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004692
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004693 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004694 if (IS_ERR(trans))
4695 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004696
4697 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004698 if (ret && ret == -ENOSPC) {
4699 /* whoops, lets try again with the full transaction */
4700 btrfs_end_transaction(trans, root);
4701 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004702 if (IS_ERR(trans))
4703 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004704
Chris Mason94b60442010-05-26 11:02:00 -04004705 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004706 }
Chris Mason39279cc2007-06-12 06:35:45 -04004707 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004708 if (BTRFS_I(inode)->delayed_node)
4709 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004710
4711 return ret;
4712}
4713
4714/*
4715 * This is a copy of file_update_time. We need this so we can return error on
4716 * ENOSPC for updating the inode in the case of file write and mmap writes.
4717 */
Josef Bacike41f9412012-03-26 09:46:47 -04004718static int btrfs_update_time(struct inode *inode, struct timespec *now,
4719 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004720{
Alexander Block2bc5565282012-06-15 09:49:33 +02004721 struct btrfs_root *root = BTRFS_I(inode)->root;
4722
4723 if (btrfs_root_readonly(root))
4724 return -EROFS;
4725
Josef Bacike41f9412012-03-26 09:46:47 -04004726 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004727 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004728 if (flags & S_CTIME)
4729 inode->i_ctime = *now;
4730 if (flags & S_MTIME)
4731 inode->i_mtime = *now;
4732 if (flags & S_ATIME)
4733 inode->i_atime = *now;
4734 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004735}
4736
Chris Masond352ac62008-09-29 15:18:18 -04004737/*
4738 * find the highest existing sequence number in a directory
4739 * and then set the in-memory index_cnt variable to reflect
4740 * free sequence numbers
4741 */
Josef Bacikaec74772008-07-24 12:12:38 -04004742static int btrfs_set_inode_index_count(struct inode *inode)
4743{
4744 struct btrfs_root *root = BTRFS_I(inode)->root;
4745 struct btrfs_key key, found_key;
4746 struct btrfs_path *path;
4747 struct extent_buffer *leaf;
4748 int ret;
4749
Li Zefan33345d012011-04-20 10:31:50 +08004750 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004751 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4752 key.offset = (u64)-1;
4753
4754 path = btrfs_alloc_path();
4755 if (!path)
4756 return -ENOMEM;
4757
4758 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4759 if (ret < 0)
4760 goto out;
4761 /* FIXME: we should be able to handle this */
4762 if (ret == 0)
4763 goto out;
4764 ret = 0;
4765
4766 /*
4767 * MAGIC NUMBER EXPLANATION:
4768 * since we search a directory based on f_pos we have to start at 2
4769 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4770 * else has to start at 2
4771 */
4772 if (path->slots[0] == 0) {
4773 BTRFS_I(inode)->index_cnt = 2;
4774 goto out;
4775 }
4776
4777 path->slots[0]--;
4778
4779 leaf = path->nodes[0];
4780 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4781
Li Zefan33345d012011-04-20 10:31:50 +08004782 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004783 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4784 BTRFS_I(inode)->index_cnt = 2;
4785 goto out;
4786 }
4787
4788 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4789out:
4790 btrfs_free_path(path);
4791 return ret;
4792}
4793
Chris Masond352ac62008-09-29 15:18:18 -04004794/*
4795 * helper to find a free sequence number in a given directory. This current
4796 * code is very simple, later versions will do smarter things in the btree
4797 */
Chris Mason3de45862008-11-17 21:02:50 -05004798int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004799{
4800 int ret = 0;
4801
4802 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004803 ret = btrfs_inode_delayed_dir_index_count(dir);
4804 if (ret) {
4805 ret = btrfs_set_inode_index_count(dir);
4806 if (ret)
4807 return ret;
4808 }
Josef Bacikaec74772008-07-24 12:12:38 -04004809 }
4810
Chris Mason00e4e6b2008-08-05 11:18:09 -04004811 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004812 BTRFS_I(dir)->index_cnt++;
4813
4814 return ret;
4815}
4816
Chris Mason39279cc2007-06-12 06:35:45 -04004817static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4818 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004819 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004820 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004821 u64 ref_objectid, u64 objectid,
4822 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004823{
4824 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004825 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004826 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004827 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004828 struct btrfs_inode_ref *ref;
4829 struct btrfs_key key[2];
4830 u32 sizes[2];
4831 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004832 int ret;
4833 int owner;
4834
Chris Mason5f39d392007-10-15 16:14:19 -04004835 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004836 if (!path)
4837 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004838
Chris Mason39279cc2007-06-12 06:35:45 -04004839 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004840 if (!inode) {
4841 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004842 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004843 }
Chris Mason39279cc2007-06-12 06:35:45 -04004844
Li Zefan581bb052011-04-20 10:06:11 +08004845 /*
4846 * we have to initialize this early, so we can reclaim the inode
4847 * number if we fail afterwards in this function.
4848 */
4849 inode->i_ino = objectid;
4850
Josef Bacikaec74772008-07-24 12:12:38 -04004851 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004852 trace_btrfs_inode_request(dir);
4853
Chris Mason3de45862008-11-17 21:02:50 -05004854 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004855 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004856 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004857 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004858 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004859 }
Josef Bacikaec74772008-07-24 12:12:38 -04004860 }
4861 /*
4862 * index_cnt is ignored for everything but a dir,
4863 * btrfs_get_inode_index_count has an explanation for the magic
4864 * number
4865 */
4866 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004867 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004868 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004869 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04004870
Josef Bacik5dc562c2012-08-17 13:14:17 -04004871 /*
4872 * We could have gotten an inode number from somebody who was fsynced
4873 * and then removed in this same transaction, so let's just set full
4874 * sync since it will be a full sync anyway and this will blow away the
4875 * old info in the log.
4876 */
4877 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4878
Al Viro569254b2011-07-24 17:08:40 -04004879 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004880 owner = 0;
4881 else
4882 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004883
4884 key[0].objectid = objectid;
4885 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4886 key[0].offset = 0;
4887
Mark Fashehf1863732012-08-08 11:32:27 -07004888 /*
4889 * Start new inodes with an inode_ref. This is slightly more
4890 * efficient for small numbers of hard links since they will
4891 * be packed into one item. Extended refs will kick in if we
4892 * add more hard links than can fit in the ref item.
4893 */
Chris Mason9c583092008-01-29 15:15:18 -05004894 key[1].objectid = objectid;
4895 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4896 key[1].offset = ref_objectid;
4897
4898 sizes[0] = sizeof(struct btrfs_inode_item);
4899 sizes[1] = name_len + sizeof(*ref);
4900
Chris Masonb9473432009-03-13 11:00:37 -04004901 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004902 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4903 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004904 goto fail;
4905
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004906 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004907 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004908 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004909 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4910 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004911 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4912 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004913 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004914
4915 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4916 struct btrfs_inode_ref);
4917 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004918 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004919 ptr = (unsigned long)(ref + 1);
4920 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4921
Chris Mason5f39d392007-10-15 16:14:19 -04004922 btrfs_mark_buffer_dirty(path->nodes[0]);
4923 btrfs_free_path(path);
4924
Chris Mason39279cc2007-06-12 06:35:45 -04004925 location = &BTRFS_I(inode)->location;
4926 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004927 location->offset = 0;
4928 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4929
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004930 btrfs_inherit_iflags(inode, dir);
4931
Al Viro569254b2011-07-24 17:08:40 -04004932 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004933 if (btrfs_test_opt(root, NODATASUM))
4934 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004935 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004936 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4937 }
4938
Chris Mason39279cc2007-06-12 06:35:45 -04004939 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004940 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004941
4942 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004943 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004944
Alexander Block8ea05e32012-07-25 17:35:53 +02004945 btrfs_update_root_times(trans, root);
4946
Chris Mason39279cc2007-06-12 06:35:45 -04004947 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004948fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004949 if (dir)
4950 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004951 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004952 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004953 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004954}
4955
4956static inline u8 btrfs_inode_type(struct inode *inode)
4957{
4958 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4959}
4960
Chris Masond352ac62008-09-29 15:18:18 -04004961/*
4962 * utility function to add 'inode' into 'parent_inode' with
4963 * a give name and a given sequence number.
4964 * if 'add_backref' is true, also insert a backref from the
4965 * inode to the parent directory.
4966 */
Chris Masone02119d2008-09-05 16:13:11 -04004967int btrfs_add_link(struct btrfs_trans_handle *trans,
4968 struct inode *parent_inode, struct inode *inode,
4969 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004970{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004971 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004972 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004973 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004974 u64 ino = btrfs_ino(inode);
4975 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004976
Li Zefan33345d012011-04-20 10:31:50 +08004977 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004978 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4979 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004980 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004981 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4982 key.offset = 0;
4983 }
Chris Mason39279cc2007-06-12 06:35:45 -04004984
Li Zefan33345d012011-04-20 10:31:50 +08004985 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004986 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4987 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004988 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004989 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004990 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4991 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004992 }
4993
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004994 /* Nothing to clean up yet */
4995 if (ret)
4996 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004997
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004998 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4999 parent_inode, &key,
5000 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005001 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005002 goto fail_dir_item;
5003 else if (ret) {
5004 btrfs_abort_transaction(trans, root, ret);
5005 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005006 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005007
5008 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5009 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005010 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005011 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5012 ret = btrfs_update_inode(trans, root, parent_inode);
5013 if (ret)
5014 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005015 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005016
5017fail_dir_item:
5018 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5019 u64 local_index;
5020 int err;
5021 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5022 key.objectid, root->root_key.objectid,
5023 parent_ino, &local_index, name, name_len);
5024
5025 } else if (add_backref) {
5026 u64 local_index;
5027 int err;
5028
5029 err = btrfs_del_inode_ref(trans, root, name, name_len,
5030 ino, parent_ino, &local_index);
5031 }
5032 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005033}
5034
5035static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005036 struct inode *dir, struct dentry *dentry,
5037 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005038{
Josef Bacika1b075d2010-11-19 20:36:11 +00005039 int err = btrfs_add_link(trans, dir, inode,
5040 dentry->d_name.name, dentry->d_name.len,
5041 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005042 if (err > 0)
5043 err = -EEXIST;
5044 return err;
5045}
5046
Josef Bacik618e21d2007-07-11 10:18:17 -04005047static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005048 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005049{
5050 struct btrfs_trans_handle *trans;
5051 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005052 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005053 int err;
5054 int drop_inode = 0;
5055 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005056 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005057
5058 if (!new_valid_dev(rdev))
5059 return -EINVAL;
5060
Josef Bacik9ed74f22009-09-11 16:12:44 -04005061 /*
5062 * 2 for inode item and ref
5063 * 2 for dir items
5064 * 1 for xattr if selinux is on
5065 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005066 trans = btrfs_start_transaction(root, 5);
5067 if (IS_ERR(trans))
5068 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005069
Li Zefan581bb052011-04-20 10:06:11 +08005070 err = btrfs_find_free_ino(root, &objectid);
5071 if (err)
5072 goto out_unlock;
5073
Josef Bacikaec74772008-07-24 12:12:38 -04005074 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005075 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005076 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005077 if (IS_ERR(inode)) {
5078 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005079 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005080 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005081
Eric Paris2a7dba32011-02-01 11:05:39 -05005082 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005083 if (err) {
5084 drop_inode = 1;
5085 goto out_unlock;
5086 }
5087
Casey Schauflerad19db72011-12-15 10:09:07 -05005088 /*
5089 * If the active LSM wants to access the inode during
5090 * d_instantiate it needs these. Smack checks to see
5091 * if the filesystem supports xattrs by looking at the
5092 * ops vector.
5093 */
5094
5095 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005096 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005097 if (err)
5098 drop_inode = 1;
5099 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005100 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005101 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005102 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005103 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005104out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005105 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005106 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005107 if (drop_inode) {
5108 inode_dec_link_count(inode);
5109 iput(inode);
5110 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005111 return err;
5112}
5113
Chris Mason39279cc2007-06-12 06:35:45 -04005114static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005115 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005116{
5117 struct btrfs_trans_handle *trans;
5118 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005119 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005120 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005121 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005122 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005123 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005124
Josef Bacik9ed74f22009-09-11 16:12:44 -04005125 /*
5126 * 2 for inode item and ref
5127 * 2 for dir items
5128 * 1 for xattr if selinux is on
5129 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005130 trans = btrfs_start_transaction(root, 5);
5131 if (IS_ERR(trans))
5132 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005133
Li Zefan581bb052011-04-20 10:06:11 +08005134 err = btrfs_find_free_ino(root, &objectid);
5135 if (err)
5136 goto out_unlock;
5137
Josef Bacikaec74772008-07-24 12:12:38 -04005138 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005139 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005140 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005141 if (IS_ERR(inode)) {
5142 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005143 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005144 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005145 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005146
Eric Paris2a7dba32011-02-01 11:05:39 -05005147 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005148 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005149 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005150
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005151 err = btrfs_update_inode(trans, root, inode);
5152 if (err)
5153 goto out_unlock;
5154
Casey Schauflerad19db72011-12-15 10:09:07 -05005155 /*
5156 * If the active LSM wants to access the inode during
5157 * d_instantiate it needs these. Smack checks to see
5158 * if the filesystem supports xattrs by looking at the
5159 * ops vector.
5160 */
5161 inode->i_fop = &btrfs_file_operations;
5162 inode->i_op = &btrfs_file_inode_operations;
5163
Josef Bacika1b075d2010-11-19 20:36:11 +00005164 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005165 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005166 goto out_unlock;
5167
5168 inode->i_mapping->a_ops = &btrfs_aops;
5169 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5170 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5171 d_instantiate(dentry, inode);
5172
Chris Mason39279cc2007-06-12 06:35:45 -04005173out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005174 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005175 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005176 inode_dec_link_count(inode);
5177 iput(inode);
5178 }
Liu Bob53d3f52012-11-14 14:34:34 +00005179 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005180 return err;
5181}
5182
5183static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5184 struct dentry *dentry)
5185{
5186 struct btrfs_trans_handle *trans;
5187 struct btrfs_root *root = BTRFS_I(dir)->root;
5188 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005189 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005190 int err;
5191 int drop_inode = 0;
5192
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005193 /* do not allow sys_link's with other subvols of the same device */
5194 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005195 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005196
Mark Fashehf1863732012-08-08 11:32:27 -07005197 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005198 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005199
Chris Mason3de45862008-11-17 21:02:50 -05005200 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005201 if (err)
5202 goto fail;
5203
Yan, Zhenga22285a2010-05-16 10:48:46 -04005204 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005205 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005206 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005207 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005208 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005209 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005210 if (IS_ERR(trans)) {
5211 err = PTR_ERR(trans);
5212 goto fail;
5213 }
Chris Mason5f39d392007-10-15 16:14:19 -04005214
Miao Xie31534952011-04-13 13:19:21 +08005215 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005216 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005217 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005218 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005219 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005220
Josef Bacika1b075d2010-11-19 20:36:11 +00005221 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005222
Yan, Zhenga5719522009-09-24 09:17:31 -04005223 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005224 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005225 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005226 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005227 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005228 if (err)
5229 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005230 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005231 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005232 }
Chris Mason39279cc2007-06-12 06:35:45 -04005233
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005234 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005235fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005236 if (drop_inode) {
5237 inode_dec_link_count(inode);
5238 iput(inode);
5239 }
Liu Bob53d3f52012-11-14 14:34:34 +00005240 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005241 return err;
5242}
5243
Al Viro18bb1db2011-07-26 01:41:39 -04005244static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005245{
Chris Masonb9d86662008-05-02 16:13:49 -04005246 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005247 struct btrfs_trans_handle *trans;
5248 struct btrfs_root *root = BTRFS_I(dir)->root;
5249 int err = 0;
5250 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005251 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005252 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005253
Josef Bacik9ed74f22009-09-11 16:12:44 -04005254 /*
5255 * 2 items for inode and ref
5256 * 2 items for dir items
5257 * 1 for xattr if selinux is on
5258 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005259 trans = btrfs_start_transaction(root, 5);
5260 if (IS_ERR(trans))
5261 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005262
Li Zefan581bb052011-04-20 10:06:11 +08005263 err = btrfs_find_free_ino(root, &objectid);
5264 if (err)
5265 goto out_fail;
5266
Josef Bacikaec74772008-07-24 12:12:38 -04005267 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005268 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005269 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005270 if (IS_ERR(inode)) {
5271 err = PTR_ERR(inode);
5272 goto out_fail;
5273 }
Chris Mason5f39d392007-10-15 16:14:19 -04005274
Chris Mason39279cc2007-06-12 06:35:45 -04005275 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005276
Eric Paris2a7dba32011-02-01 11:05:39 -05005277 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005278 if (err)
5279 goto out_fail;
5280
Chris Mason39279cc2007-06-12 06:35:45 -04005281 inode->i_op = &btrfs_dir_inode_operations;
5282 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005283
Chris Masondbe674a2008-07-17 12:54:05 -04005284 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005285 err = btrfs_update_inode(trans, root, inode);
5286 if (err)
5287 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005288
Josef Bacika1b075d2010-11-19 20:36:11 +00005289 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5290 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005291 if (err)
5292 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005293
Chris Mason39279cc2007-06-12 06:35:45 -04005294 d_instantiate(dentry, inode);
5295 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005296
5297out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005298 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005299 if (drop_on_err)
5300 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005301 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005302 return err;
5303}
5304
Chris Masond352ac62008-09-29 15:18:18 -04005305/* helper for btfs_get_extent. Given an existing extent in the tree,
5306 * and an extent that you want to insert, deal with overlap and insert
5307 * the new extent into the tree.
5308 */
Chris Mason3b951512008-04-17 11:29:12 -04005309static int merge_extent_mapping(struct extent_map_tree *em_tree,
5310 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005311 struct extent_map *em,
5312 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005313{
5314 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005315
Chris Masone6dcd2d2008-07-17 12:53:50 -04005316 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5317 start_diff = map_start - em->start;
5318 em->start = map_start;
5319 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005320 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5321 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005322 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005323 em->block_len -= start_diff;
5324 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005325 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005326}
5327
Chris Masonc8b97812008-10-29 14:49:59 -04005328static noinline int uncompress_inline(struct btrfs_path *path,
5329 struct inode *inode, struct page *page,
5330 size_t pg_offset, u64 extent_offset,
5331 struct btrfs_file_extent_item *item)
5332{
5333 int ret;
5334 struct extent_buffer *leaf = path->nodes[0];
5335 char *tmp;
5336 size_t max_size;
5337 unsigned long inline_size;
5338 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005339 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005340
5341 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005342 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005343 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5344 inline_size = btrfs_file_extent_inline_item_len(leaf,
5345 btrfs_item_nr(leaf, path->slots[0]));
5346 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005347 if (!tmp)
5348 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005349 ptr = btrfs_file_extent_inline_start(item);
5350
5351 read_extent_buffer(leaf, tmp, ptr, inline_size);
5352
Chris Mason5b050f02008-11-11 09:34:41 -05005353 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005354 ret = btrfs_decompress(compress_type, tmp, page,
5355 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005356 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005357 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005358 unsigned long copy_size = min_t(u64,
5359 PAGE_CACHE_SIZE - pg_offset,
5360 max_size - extent_offset);
5361 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005362 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005363 }
5364 kfree(tmp);
5365 return 0;
5366}
5367
Chris Masond352ac62008-09-29 15:18:18 -04005368/*
5369 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005370 * the ugly parts come from merging extents from the disk with the in-ram
5371 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005372 * where the in-ram extents might be locked pending data=ordered completion.
5373 *
5374 * This also copies inline extents directly into the page.
5375 */
Chris Masond3977122009-01-05 21:25:51 -05005376
Chris Masona52d9a82007-08-27 16:49:44 -04005377struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005378 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005379 int create)
5380{
5381 int ret;
5382 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005383 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005384 u64 extent_start = 0;
5385 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005386 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005387 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005388 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005389 struct btrfs_root *root = BTRFS_I(inode)->root;
5390 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005391 struct extent_buffer *leaf;
5392 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005393 struct extent_map *em = NULL;
5394 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005395 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005396 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005397 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005398
Chris Masona52d9a82007-08-27 16:49:44 -04005399again:
Chris Mason890871b2009-09-02 16:24:52 -04005400 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005401 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005402 if (em)
5403 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005404 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005405
Chris Masona52d9a82007-08-27 16:49:44 -04005406 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005407 if (em->start > start || em->start + em->len <= start)
5408 free_extent_map(em);
5409 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005410 free_extent_map(em);
5411 else
5412 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005413 }
David Sterba172ddd62011-04-21 00:48:27 +02005414 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005415 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005416 err = -ENOMEM;
5417 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005418 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005419 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005420 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005421 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005422 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005423 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005424
5425 if (!path) {
5426 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005427 if (!path) {
5428 err = -ENOMEM;
5429 goto out;
5430 }
5431 /*
5432 * Chances are we'll be called again, so go ahead and do
5433 * readahead
5434 */
5435 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005436 }
5437
Chris Mason179e29e2007-11-01 11:28:41 -04005438 ret = btrfs_lookup_file_extent(trans, root, path,
5439 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005440 if (ret < 0) {
5441 err = ret;
5442 goto out;
5443 }
5444
5445 if (ret != 0) {
5446 if (path->slots[0] == 0)
5447 goto not_found;
5448 path->slots[0]--;
5449 }
5450
Chris Mason5f39d392007-10-15 16:14:19 -04005451 leaf = path->nodes[0];
5452 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005453 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005454 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005455 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5456 found_type = btrfs_key_type(&found_key);
5457 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005458 found_type != BTRFS_EXTENT_DATA_KEY) {
5459 goto not_found;
5460 }
5461
Chris Mason5f39d392007-10-15 16:14:19 -04005462 found_type = btrfs_file_extent_type(leaf, item);
5463 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005464 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005465 if (found_type == BTRFS_FILE_EXTENT_REG ||
5466 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005467 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005468 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005469 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5470 size_t size;
5471 size = btrfs_file_extent_inline_len(leaf, item);
5472 extent_end = (extent_start + size + root->sectorsize - 1) &
5473 ~((u64)root->sectorsize - 1);
5474 }
5475
5476 if (start >= extent_end) {
5477 path->slots[0]++;
5478 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5479 ret = btrfs_next_leaf(root, path);
5480 if (ret < 0) {
5481 err = ret;
5482 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005483 }
Yan Zheng9036c102008-10-30 14:19:41 -04005484 if (ret > 0)
5485 goto not_found;
5486 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005487 }
Yan Zheng9036c102008-10-30 14:19:41 -04005488 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5489 if (found_key.objectid != objectid ||
5490 found_key.type != BTRFS_EXTENT_DATA_KEY)
5491 goto not_found;
5492 if (start + len <= found_key.offset)
5493 goto not_found;
5494 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005495 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005496 em->len = found_key.offset - start;
5497 goto not_found_em;
5498 }
5499
Yan Zhengd899e052008-10-30 14:25:28 -04005500 if (found_type == BTRFS_FILE_EXTENT_REG ||
5501 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005502 em->start = extent_start;
5503 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005504 em->orig_start = extent_start -
5505 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005506 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5507 item);
Chris Masondb945352007-10-15 16:15:53 -04005508 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5509 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005510 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005511 goto insert;
5512 }
Li Zefan261507a02010-12-17 14:21:50 +08005513 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005514 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005515 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005516 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005517 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005518 } else {
5519 bytenr += btrfs_file_extent_offset(leaf, item);
5520 em->block_start = bytenr;
5521 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005522 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5523 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005524 }
Chris Masona52d9a82007-08-27 16:49:44 -04005525 goto insert;
5526 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005527 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005528 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005529 size_t size;
5530 size_t extent_offset;
5531 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005532
Chris Masona52d9a82007-08-27 16:49:44 -04005533 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005534 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005535 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005536 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005537 goto out;
5538 }
5539
Yan Zheng9036c102008-10-30 14:19:41 -04005540 size = btrfs_file_extent_inline_len(leaf, item);
5541 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005542 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005543 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005544 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005545 em->len = (copy_size + root->sectorsize - 1) &
5546 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005547 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005548 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005549 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005550 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005551 em->compress_type = compress_type;
5552 }
Yan689f9342007-10-29 11:41:07 -04005553 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005554 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005555 if (btrfs_file_extent_compression(leaf, item) !=
5556 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005557 ret = uncompress_inline(path, inode, page,
5558 pg_offset,
5559 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005560 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005561 } else {
5562 map = kmap(page);
5563 read_extent_buffer(leaf, map + pg_offset, ptr,
5564 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005565 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5566 memset(map + pg_offset + copy_size, 0,
5567 PAGE_CACHE_SIZE - pg_offset -
5568 copy_size);
5569 }
Chris Masonc8b97812008-10-29 14:49:59 -04005570 kunmap(page);
5571 }
Chris Mason179e29e2007-11-01 11:28:41 -04005572 flush_dcache_page(page);
5573 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005574 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005575 if (!trans) {
5576 kunmap(page);
5577 free_extent_map(em);
5578 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005579
David Sterbab3b4aa72011-04-21 01:20:15 +02005580 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005581 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005582
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005583 if (IS_ERR(trans))
5584 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005585 goto again;
5586 }
Chris Masonc8b97812008-10-29 14:49:59 -04005587 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005588 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005589 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005590 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005591 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005592 }
Chris Masond1310b22008-01-24 16:13:08 -05005593 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005594 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005595 goto insert;
5596 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005597 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005598 }
5599not_found:
5600 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005601 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005602 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005603not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005604 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005605 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005606insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005607 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005608 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005609 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5610 "[%llu %llu]\n", (unsigned long long)em->start,
5611 (unsigned long long)em->len,
5612 (unsigned long long)start,
5613 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005614 err = -EIO;
5615 goto out;
5616 }
Chris Masond1310b22008-01-24 16:13:08 -05005617
5618 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005619 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005620 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005621 /* it is possible that someone inserted the extent into the tree
5622 * while we had the lock dropped. It is also possible that
5623 * an overlapping map exists in the tree
5624 */
Chris Masona52d9a82007-08-27 16:49:44 -04005625 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005626 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005627
5628 ret = 0;
5629
Chris Mason3b951512008-04-17 11:29:12 -04005630 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005631 if (existing && (existing->start > start ||
5632 existing->start + existing->len <= start)) {
5633 free_extent_map(existing);
5634 existing = NULL;
5635 }
Chris Mason3b951512008-04-17 11:29:12 -04005636 if (!existing) {
5637 existing = lookup_extent_mapping(em_tree, em->start,
5638 em->len);
5639 if (existing) {
5640 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005641 em, start,
5642 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005643 free_extent_map(existing);
5644 if (err) {
5645 free_extent_map(em);
5646 em = NULL;
5647 }
5648 } else {
5649 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005650 free_extent_map(em);
5651 em = NULL;
5652 }
5653 } else {
5654 free_extent_map(em);
5655 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005656 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005657 }
Chris Masona52d9a82007-08-27 16:49:44 -04005658 }
Chris Mason890871b2009-09-02 16:24:52 -04005659 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005660out:
liubo1abe9b82011-03-24 11:18:59 +00005661
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005662 if (em)
5663 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005664
Chris Masonf4219502008-07-22 11:18:09 -04005665 if (path)
5666 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005667 if (trans) {
5668 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005669 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005670 err = ret;
5671 }
Chris Masona52d9a82007-08-27 16:49:44 -04005672 if (err) {
5673 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005674 return ERR_PTR(err);
5675 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005676 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005677 return em;
5678}
5679
Chris Masonec29ed52011-02-23 16:23:20 -05005680struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5681 size_t pg_offset, u64 start, u64 len,
5682 int create)
5683{
5684 struct extent_map *em;
5685 struct extent_map *hole_em = NULL;
5686 u64 range_start = start;
5687 u64 end;
5688 u64 found;
5689 u64 found_end;
5690 int err = 0;
5691
5692 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5693 if (IS_ERR(em))
5694 return em;
5695 if (em) {
5696 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005697 * if our em maps to
5698 * - a hole or
5699 * - a pre-alloc extent,
5700 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005701 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005702 if (em->block_start != EXTENT_MAP_HOLE &&
5703 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005704 return em;
5705 else
5706 hole_em = em;
5707 }
5708
5709 /* check to see if we've wrapped (len == -1 or similar) */
5710 end = start + len;
5711 if (end < start)
5712 end = (u64)-1;
5713 else
5714 end -= 1;
5715
5716 em = NULL;
5717
5718 /* ok, we didn't find anything, lets look for delalloc */
5719 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5720 end, len, EXTENT_DELALLOC, 1);
5721 found_end = range_start + found;
5722 if (found_end < range_start)
5723 found_end = (u64)-1;
5724
5725 /*
5726 * we didn't find anything useful, return
5727 * the original results from get_extent()
5728 */
5729 if (range_start > end || found_end <= start) {
5730 em = hole_em;
5731 hole_em = NULL;
5732 goto out;
5733 }
5734
5735 /* adjust the range_start to make sure it doesn't
5736 * go backwards from the start they passed in
5737 */
5738 range_start = max(start,range_start);
5739 found = found_end - range_start;
5740
5741 if (found > 0) {
5742 u64 hole_start = start;
5743 u64 hole_len = len;
5744
David Sterba172ddd62011-04-21 00:48:27 +02005745 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005746 if (!em) {
5747 err = -ENOMEM;
5748 goto out;
5749 }
5750 /*
5751 * when btrfs_get_extent can't find anything it
5752 * returns one huge hole
5753 *
5754 * make sure what it found really fits our range, and
5755 * adjust to make sure it is based on the start from
5756 * the caller
5757 */
5758 if (hole_em) {
5759 u64 calc_end = extent_map_end(hole_em);
5760
5761 if (calc_end <= start || (hole_em->start > end)) {
5762 free_extent_map(hole_em);
5763 hole_em = NULL;
5764 } else {
5765 hole_start = max(hole_em->start, start);
5766 hole_len = calc_end - hole_start;
5767 }
5768 }
5769 em->bdev = NULL;
5770 if (hole_em && range_start > hole_start) {
5771 /* our hole starts before our delalloc, so we
5772 * have to return just the parts of the hole
5773 * that go until the delalloc starts
5774 */
5775 em->len = min(hole_len,
5776 range_start - hole_start);
5777 em->start = hole_start;
5778 em->orig_start = hole_start;
5779 /*
5780 * don't adjust block start at all,
5781 * it is fixed at EXTENT_MAP_HOLE
5782 */
5783 em->block_start = hole_em->block_start;
5784 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005785 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5786 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005787 } else {
5788 em->start = range_start;
5789 em->len = found;
5790 em->orig_start = range_start;
5791 em->block_start = EXTENT_MAP_DELALLOC;
5792 em->block_len = found;
5793 }
5794 } else if (hole_em) {
5795 return hole_em;
5796 }
5797out:
5798
5799 free_extent_map(hole_em);
5800 if (err) {
5801 free_extent_map(em);
5802 return ERR_PTR(err);
5803 }
5804 return em;
5805}
5806
Josef Bacik4b46fce2010-05-23 11:00:55 -04005807static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5808 u64 start, u64 len)
5809{
5810 struct btrfs_root *root = BTRFS_I(inode)->root;
5811 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005812 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005813 struct btrfs_key ins;
5814 u64 alloc_hint;
5815 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005816
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005817 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005818 if (IS_ERR(trans))
5819 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005820
5821 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5822
5823 alloc_hint = get_extent_allocation_hint(inode, start, len);
5824 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005825 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005826 if (ret) {
5827 em = ERR_PTR(ret);
5828 goto out;
5829 }
5830
Josef Bacik70c8a912012-10-11 16:54:30 -04005831 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5832 ins.offset, ins.offset, 0);
5833 if (IS_ERR(em))
5834 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005835
5836 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5837 ins.offset, ins.offset, 0);
5838 if (ret) {
5839 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5840 em = ERR_PTR(ret);
5841 }
5842out:
5843 btrfs_end_transaction(trans, root);
5844 return em;
5845}
5846
Chris Mason46bfbb52010-05-26 11:04:10 -04005847/*
5848 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5849 * block must be cow'd
5850 */
5851static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5852 struct inode *inode, u64 offset, u64 len)
5853{
5854 struct btrfs_path *path;
5855 int ret;
5856 struct extent_buffer *leaf;
5857 struct btrfs_root *root = BTRFS_I(inode)->root;
5858 struct btrfs_file_extent_item *fi;
5859 struct btrfs_key key;
5860 u64 disk_bytenr;
5861 u64 backref_offset;
5862 u64 extent_end;
5863 u64 num_bytes;
5864 int slot;
5865 int found_type;
5866
5867 path = btrfs_alloc_path();
5868 if (!path)
5869 return -ENOMEM;
5870
Li Zefan33345d012011-04-20 10:31:50 +08005871 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005872 offset, 0);
5873 if (ret < 0)
5874 goto out;
5875
5876 slot = path->slots[0];
5877 if (ret == 1) {
5878 if (slot == 0) {
5879 /* can't find the item, must cow */
5880 ret = 0;
5881 goto out;
5882 }
5883 slot--;
5884 }
5885 ret = 0;
5886 leaf = path->nodes[0];
5887 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005888 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005889 key.type != BTRFS_EXTENT_DATA_KEY) {
5890 /* not our file or wrong item type, must cow */
5891 goto out;
5892 }
5893
5894 if (key.offset > offset) {
5895 /* Wrong offset, must cow */
5896 goto out;
5897 }
5898
5899 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5900 found_type = btrfs_file_extent_type(leaf, fi);
5901 if (found_type != BTRFS_FILE_EXTENT_REG &&
5902 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5903 /* not a regular extent, must cow */
5904 goto out;
5905 }
5906 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5907 backref_offset = btrfs_file_extent_offset(leaf, fi);
5908
5909 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5910 if (extent_end < offset + len) {
5911 /* extent doesn't include our full range, must cow */
5912 goto out;
5913 }
5914
5915 if (btrfs_extent_readonly(root, disk_bytenr))
5916 goto out;
5917
5918 /*
5919 * look for other files referencing this extent, if we
5920 * find any we must cow
5921 */
Li Zefan33345d012011-04-20 10:31:50 +08005922 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005923 key.offset - backref_offset, disk_bytenr))
5924 goto out;
5925
5926 /*
5927 * adjust disk_bytenr and num_bytes to cover just the bytes
5928 * in this extent we are about to write. If there
5929 * are any csums in that range we have to cow in order
5930 * to keep the csums correct
5931 */
5932 disk_bytenr += backref_offset;
5933 disk_bytenr += offset - key.offset;
5934 num_bytes = min(offset + len, extent_end) - offset;
5935 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5936 goto out;
5937 /*
5938 * all of the above have passed, it is safe to overwrite this extent
5939 * without cow
5940 */
5941 ret = 1;
5942out:
5943 btrfs_free_path(path);
5944 return ret;
5945}
5946
Josef Bacikeb838e72012-07-31 16:28:48 -04005947static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5948 struct extent_state **cached_state, int writing)
5949{
5950 struct btrfs_ordered_extent *ordered;
5951 int ret = 0;
5952
5953 while (1) {
5954 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5955 0, cached_state);
5956 /*
5957 * We're concerned with the entire range that we're going to be
5958 * doing DIO to, so we need to make sure theres no ordered
5959 * extents in this range.
5960 */
5961 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5962 lockend - lockstart + 1);
5963
5964 /*
5965 * We need to make sure there are no buffered pages in this
5966 * range either, we could have raced between the invalidate in
5967 * generic_file_direct_write and locking the extent. The
5968 * invalidate needs to happen so that reads after a write do not
5969 * get stale data.
5970 */
5971 if (!ordered && (!writing ||
5972 !test_range_bit(&BTRFS_I(inode)->io_tree,
5973 lockstart, lockend, EXTENT_UPTODATE, 0,
5974 *cached_state)))
5975 break;
5976
5977 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5978 cached_state, GFP_NOFS);
5979
5980 if (ordered) {
5981 btrfs_start_ordered_extent(inode, ordered, 1);
5982 btrfs_put_ordered_extent(ordered);
5983 } else {
5984 /* Screw you mmap */
5985 ret = filemap_write_and_wait_range(inode->i_mapping,
5986 lockstart,
5987 lockend);
5988 if (ret)
5989 break;
5990
5991 /*
5992 * If we found a page that couldn't be invalidated just
5993 * fall back to buffered.
5994 */
5995 ret = invalidate_inode_pages2_range(inode->i_mapping,
5996 lockstart >> PAGE_CACHE_SHIFT,
5997 lockend >> PAGE_CACHE_SHIFT);
5998 if (ret)
5999 break;
6000 }
6001
6002 cond_resched();
6003 }
6004
6005 return ret;
6006}
6007
Josef Bacik69ffb542012-09-11 15:40:07 -04006008static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6009 u64 len, u64 orig_start,
6010 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006011 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006012{
6013 struct extent_map_tree *em_tree;
6014 struct extent_map *em;
6015 struct btrfs_root *root = BTRFS_I(inode)->root;
6016 int ret;
6017
6018 em_tree = &BTRFS_I(inode)->extent_tree;
6019 em = alloc_extent_map();
6020 if (!em)
6021 return ERR_PTR(-ENOMEM);
6022
6023 em->start = start;
6024 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006025 em->mod_start = start;
6026 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006027 em->len = len;
6028 em->block_len = block_len;
6029 em->block_start = block_start;
6030 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006031 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006032 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006033 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6034 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006035 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006036
6037 do {
6038 btrfs_drop_extent_cache(inode, em->start,
6039 em->start + em->len - 1, 0);
6040 write_lock(&em_tree->lock);
6041 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006042 if (!ret)
6043 list_move(&em->list,
6044 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006045 write_unlock(&em_tree->lock);
6046 } while (ret == -EEXIST);
6047
6048 if (ret) {
6049 free_extent_map(em);
6050 return ERR_PTR(ret);
6051 }
6052
6053 return em;
6054}
6055
6056
Josef Bacik4b46fce2010-05-23 11:00:55 -04006057static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6058 struct buffer_head *bh_result, int create)
6059{
6060 struct extent_map *em;
6061 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006062 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006063 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006064 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006065 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006066 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006067 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006068 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006069
Josef Bacikeb838e72012-07-31 16:28:48 -04006070 if (create) {
Miao Xie09348562013-02-07 10:12:07 +00006071 spin_lock(&BTRFS_I(inode)->lock);
6072 BTRFS_I(inode)->outstanding_extents++;
6073 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikeb838e72012-07-31 16:28:48 -04006074 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie09348562013-02-07 10:12:07 +00006075 } else
Josef Bacikc3298612012-08-03 16:49:19 -04006076 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006077
Josef Bacikc3298612012-08-03 16:49:19 -04006078 lockstart = start;
6079 lockend = start + len - 1;
6080
Josef Bacikeb838e72012-07-31 16:28:48 -04006081 /*
6082 * If this errors out it's because we couldn't invalidate pagecache for
6083 * this range and we need to fallback to buffered.
6084 */
6085 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6086 return -ENOTBLK;
6087
Josef Bacik4b46fce2010-05-23 11:00:55 -04006088 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006089 if (IS_ERR(em)) {
6090 ret = PTR_ERR(em);
6091 goto unlock_err;
6092 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006093
6094 /*
6095 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6096 * io. INLINE is special, and we could probably kludge it in here, but
6097 * it's still buffered so for safety lets just fall back to the generic
6098 * buffered path.
6099 *
6100 * For COMPRESSED we _have_ to read the entire extent in so we can
6101 * decompress it, so there will be buffering required no matter what we
6102 * do, so go ahead and fallback to buffered.
6103 *
6104 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6105 * to buffered IO. Don't blame me, this is the price we pay for using
6106 * the generic code.
6107 */
6108 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6109 em->block_start == EXTENT_MAP_INLINE) {
6110 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006111 ret = -ENOTBLK;
6112 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006113 }
6114
6115 /* Just a good old fashioned hole, return */
6116 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6117 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6118 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006119 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006120 }
6121
6122 /*
6123 * We don't allocate a new extent in the following cases
6124 *
6125 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6126 * existing extent.
6127 * 2) The extent is marked as PREALLOC. We're good to go here and can
6128 * just use the extent.
6129 *
6130 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006131 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006132 len = min(len, em->len - (start - em->start));
6133 lockstart = start + len;
6134 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006135 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006136
6137 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6138 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6139 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006140 int type;
6141 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006142 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006143
6144 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6145 type = BTRFS_ORDERED_PREALLOC;
6146 else
6147 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006148 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006149 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006150
6151 /*
6152 * we're not going to log anything, but we do need
6153 * to make sure the current transaction stays open
6154 * while we look for nocow cross refs
6155 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006156 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006157 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006158 goto must_cow;
6159
6160 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006161 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006162 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006163
6164 if (type == BTRFS_ORDERED_PREALLOC) {
6165 free_extent_map(em);
6166 em = create_pinned_em(inode, start, len,
6167 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006168 block_start, len,
6169 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006170 if (IS_ERR(em)) {
6171 btrfs_end_transaction(trans, root);
6172 goto unlock_err;
6173 }
6174 }
6175
Chris Mason46bfbb52010-05-26 11:04:10 -04006176 ret = btrfs_add_ordered_extent_dio(inode, start,
6177 block_start, len, len, type);
6178 btrfs_end_transaction(trans, root);
6179 if (ret) {
6180 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006181 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006182 }
6183 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006184 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006185 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006186 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006187must_cow:
6188 /*
6189 * this will cow the extent, reset the len in case we changed
6190 * it above
6191 */
6192 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006193 free_extent_map(em);
6194 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006195 if (IS_ERR(em)) {
6196 ret = PTR_ERR(em);
6197 goto unlock_err;
6198 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006199 len = min(len, em->len - (start - em->start));
6200unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006201 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6202 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006203 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006204 bh_result->b_bdev = em->bdev;
6205 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006206 if (create) {
6207 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6208 set_buffer_new(bh_result);
6209
6210 /*
6211 * Need to update the i_size under the extent lock so buffered
6212 * readers will get the updated i_size when we unlock.
6213 */
6214 if (start + len > i_size_read(inode))
6215 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006216
6217 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6218 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6219 &cached_state, GFP_NOFS);
6220 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006221 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006222
Josef Bacikeb838e72012-07-31 16:28:48 -04006223 /*
6224 * In the case of write we need to clear and unlock the entire range,
6225 * in the case of read we need to unlock only the end area that we
6226 * aren't using if there is any left over space.
6227 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006228 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006229 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6230 lockend, unlock_bits, 1, 0,
6231 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006232 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006233 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006234 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006235
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236 free_extent_map(em);
6237
6238 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006239
6240unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006241 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6242 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6243 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006244}
6245
6246struct btrfs_dio_private {
6247 struct inode *inode;
6248 u64 logical_offset;
6249 u64 disk_bytenr;
6250 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006251 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006252
6253 /* number of bios pending for this dio */
6254 atomic_t pending_bios;
6255
6256 /* IO errors */
6257 int errors;
6258
6259 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006260};
6261
6262static void btrfs_endio_direct_read(struct bio *bio, int err)
6263{
Miao Xiee65e1532010-11-22 03:04:43 +00006264 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006265 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6266 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006267 struct inode *inode = dip->inode;
6268 struct btrfs_root *root = BTRFS_I(inode)->root;
6269 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006270
6271 start = dip->logical_offset;
6272 do {
6273 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6274 struct page *page = bvec->bv_page;
6275 char *kaddr;
6276 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006277 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006278 unsigned long flags;
6279
Josef Bacikc3298612012-08-03 16:49:19 -04006280 if (get_state_private(&BTRFS_I(inode)->io_tree,
6281 start, &private))
6282 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006283 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006284 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006285 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6286 csum, bvec->bv_len);
6287 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006288 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006289 local_irq_restore(flags);
6290
6291 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006292 if (csum != private) {
6293failed:
Li Zefan33345d012011-04-20 10:31:50 +08006294 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006295 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006296 (unsigned long long)btrfs_ino(inode),
6297 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006298 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006299 err = -EIO;
6300 }
6301 }
6302
6303 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006304 bvec++;
6305 } while (bvec <= bvec_end);
6306
6307 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006308 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006309 bio->bi_private = dip->private;
6310
Josef Bacik4b46fce2010-05-23 11:00:55 -04006311 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006312
6313 /* If we had a csum failure make sure to clear the uptodate flag */
6314 if (err)
6315 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006316 dio_end_io(bio, err);
6317}
6318
6319static void btrfs_endio_direct_write(struct bio *bio, int err)
6320{
6321 struct btrfs_dio_private *dip = bio->bi_private;
6322 struct inode *inode = dip->inode;
6323 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006324 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006325 u64 ordered_offset = dip->logical_offset;
6326 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006327 int ret;
6328
6329 if (err)
6330 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006331again:
6332 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6333 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006334 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006335 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006336 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006337
Josef Bacik5fd02042012-05-02 14:00:54 -04006338 ordered->work.func = finish_ordered_fn;
6339 ordered->work.flags = 0;
6340 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6341 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006342out_test:
6343 /*
6344 * our bio might span multiple ordered extents. If we haven't
6345 * completed the accounting for the whole dio, go back and try again
6346 */
6347 if (ordered_offset < dip->logical_offset + dip->bytes) {
6348 ordered_bytes = dip->logical_offset + dip->bytes -
6349 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006350 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006351 goto again;
6352 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006353out_done:
6354 bio->bi_private = dip->private;
6355
Josef Bacik4b46fce2010-05-23 11:00:55 -04006356 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006357
6358 /* If we had an error make sure to clear the uptodate flag */
6359 if (err)
6360 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006361 dio_end_io(bio, err);
6362}
6363
Chris Masoneaf25d92010-05-25 09:48:28 -04006364static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6365 struct bio *bio, int mirror_num,
6366 unsigned long bio_flags, u64 offset)
6367{
6368 int ret;
6369 struct btrfs_root *root = BTRFS_I(inode)->root;
6370 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006371 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006372 return 0;
6373}
6374
Miao Xiee65e1532010-11-22 03:04:43 +00006375static void btrfs_end_dio_bio(struct bio *bio, int err)
6376{
6377 struct btrfs_dio_private *dip = bio->bi_private;
6378
6379 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006380 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006381 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006382 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006383 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006384 dip->errors = 1;
6385
6386 /*
6387 * before atomic variable goto zero, we must make sure
6388 * dip->errors is perceived to be set.
6389 */
6390 smp_mb__before_atomic_dec();
6391 }
6392
6393 /* if there are more bios still pending for this dio, just exit */
6394 if (!atomic_dec_and_test(&dip->pending_bios))
6395 goto out;
6396
6397 if (dip->errors)
6398 bio_io_error(dip->orig_bio);
6399 else {
6400 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6401 bio_endio(dip->orig_bio, 0);
6402 }
6403out:
6404 bio_put(bio);
6405}
6406
6407static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6408 u64 first_sector, gfp_t gfp_flags)
6409{
6410 int nr_vecs = bio_get_nr_vecs(bdev);
6411 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6412}
6413
6414static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6415 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006416 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006417{
6418 int write = rw & REQ_WRITE;
6419 struct btrfs_root *root = BTRFS_I(inode)->root;
6420 int ret;
6421
Josef Bacikb812ce22012-11-16 13:56:32 -05006422 if (async_submit)
6423 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6424
Miao Xiee65e1532010-11-22 03:04:43 +00006425 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006426
6427 if (!write) {
6428 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6429 if (ret)
6430 goto err;
6431 }
Miao Xiee65e1532010-11-22 03:04:43 +00006432
Josef Bacik1ae39932011-04-06 14:41:34 -04006433 if (skip_sum)
6434 goto map;
6435
6436 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006437 ret = btrfs_wq_submit_bio(root->fs_info,
6438 inode, rw, bio, 0, 0,
6439 file_offset,
6440 __btrfs_submit_bio_start_direct_io,
6441 __btrfs_submit_bio_done);
6442 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006443 } else if (write) {
6444 /*
6445 * If we aren't doing async submit, calculate the csum of the
6446 * bio now.
6447 */
6448 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6449 if (ret)
6450 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006451 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006452 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006453 if (ret)
6454 goto err;
6455 }
Miao Xiee65e1532010-11-22 03:04:43 +00006456
Josef Bacik1ae39932011-04-06 14:41:34 -04006457map:
6458 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006459err:
6460 bio_put(bio);
6461 return ret;
6462}
6463
6464static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6465 int skip_sum)
6466{
6467 struct inode *inode = dip->inode;
6468 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006469 struct bio *bio;
6470 struct bio *orig_bio = dip->orig_bio;
6471 struct bio_vec *bvec = orig_bio->bi_io_vec;
6472 u64 start_sector = orig_bio->bi_sector;
6473 u64 file_offset = dip->logical_offset;
6474 u64 submit_len = 0;
6475 u64 map_length;
6476 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006477 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006478 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006479
Miao Xiee65e1532010-11-22 03:04:43 +00006480 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006481 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006482 &map_length, NULL, 0);
6483 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006484 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006485 return -EIO;
6486 }
6487
Josef Bacik02f57c72011-04-06 14:25:44 -04006488 if (map_length >= orig_bio->bi_size) {
6489 bio = orig_bio;
6490 goto submit;
6491 }
6492
Josef Bacik1ae39932011-04-06 14:41:34 -04006493 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006494 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6495 if (!bio)
6496 return -ENOMEM;
6497 bio->bi_private = dip;
6498 bio->bi_end_io = btrfs_end_dio_bio;
6499 atomic_inc(&dip->pending_bios);
6500
Miao Xiee65e1532010-11-22 03:04:43 +00006501 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6502 if (unlikely(map_length < submit_len + bvec->bv_len ||
6503 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6504 bvec->bv_offset) < bvec->bv_len)) {
6505 /*
6506 * inc the count before we submit the bio so
6507 * we know the end IO handler won't happen before
6508 * we inc the count. Otherwise, the dip might get freed
6509 * before we're done setting it up
6510 */
6511 atomic_inc(&dip->pending_bios);
6512 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6513 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006514 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006515 if (ret) {
6516 bio_put(bio);
6517 atomic_dec(&dip->pending_bios);
6518 goto out_err;
6519 }
6520
Miao Xiee65e1532010-11-22 03:04:43 +00006521 start_sector += submit_len >> 9;
6522 file_offset += submit_len;
6523
6524 submit_len = 0;
6525 nr_pages = 0;
6526
6527 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6528 start_sector, GFP_NOFS);
6529 if (!bio)
6530 goto out_err;
6531 bio->bi_private = dip;
6532 bio->bi_end_io = btrfs_end_dio_bio;
6533
6534 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006535 ret = btrfs_map_block(root->fs_info, READ,
6536 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006537 &map_length, NULL, 0);
6538 if (ret) {
6539 bio_put(bio);
6540 goto out_err;
6541 }
6542 } else {
6543 submit_len += bvec->bv_len;
6544 nr_pages ++;
6545 bvec++;
6546 }
6547 }
6548
Josef Bacik02f57c72011-04-06 14:25:44 -04006549submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006550 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006551 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006552 if (!ret)
6553 return 0;
6554
6555 bio_put(bio);
6556out_err:
6557 dip->errors = 1;
6558 /*
6559 * before atomic variable goto zero, we must
6560 * make sure dip->errors is perceived to be set.
6561 */
6562 smp_mb__before_atomic_dec();
6563 if (atomic_dec_and_test(&dip->pending_bios))
6564 bio_io_error(dip->orig_bio);
6565
6566 /* bio_end_io() will handle error, so we needn't return it */
6567 return 0;
6568}
6569
Josef Bacik4b46fce2010-05-23 11:00:55 -04006570static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6571 loff_t file_offset)
6572{
6573 struct btrfs_root *root = BTRFS_I(inode)->root;
6574 struct btrfs_dio_private *dip;
6575 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006576 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006577 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006578 int ret = 0;
6579
6580 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6581
6582 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6583 if (!dip) {
6584 ret = -ENOMEM;
6585 goto free_ordered;
6586 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006587
6588 dip->private = bio->bi_private;
6589 dip->inode = inode;
6590 dip->logical_offset = file_offset;
6591
Josef Bacik4b46fce2010-05-23 11:00:55 -04006592 dip->bytes = 0;
6593 do {
6594 dip->bytes += bvec->bv_len;
6595 bvec++;
6596 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6597
Chris Mason46bfbb52010-05-26 11:04:10 -04006598 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006599 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006600 dip->errors = 0;
6601 dip->orig_bio = bio;
6602 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006603
6604 if (write)
6605 bio->bi_end_io = btrfs_endio_direct_write;
6606 else
6607 bio->bi_end_io = btrfs_endio_direct_read;
6608
Miao Xiee65e1532010-11-22 03:04:43 +00006609 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6610 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006611 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006612free_ordered:
6613 /*
6614 * If this is a write, we need to clean up the reserved space and kill
6615 * the ordered extent.
6616 */
6617 if (write) {
6618 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006619 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006620 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6621 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6622 btrfs_free_reserved_extent(root, ordered->start,
6623 ordered->disk_len);
6624 btrfs_put_ordered_extent(ordered);
6625 btrfs_put_ordered_extent(ordered);
6626 }
6627 bio_endio(bio, ret);
6628}
6629
Chris Mason5a5f79b2010-05-26 21:33:37 -04006630static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6631 const struct iovec *iov, loff_t offset,
6632 unsigned long nr_segs)
6633{
6634 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006635 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006636 size_t size;
6637 unsigned long addr;
6638 unsigned blocksize_mask = root->sectorsize - 1;
6639 ssize_t retval = -EINVAL;
6640 loff_t end = offset;
6641
6642 if (offset & blocksize_mask)
6643 goto out;
6644
6645 /* Check the memory alignment. Blocks cannot straddle pages */
6646 for (seg = 0; seg < nr_segs; seg++) {
6647 addr = (unsigned long)iov[seg].iov_base;
6648 size = iov[seg].iov_len;
6649 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006650 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006651 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006652
6653 /* If this is a write we don't need to check anymore */
6654 if (rw & WRITE)
6655 continue;
6656
6657 /*
6658 * Check to make sure we don't have duplicate iov_base's in this
6659 * iovec, if so return EINVAL, otherwise we'll get csum errors
6660 * when reading back.
6661 */
6662 for (i = seg + 1; i < nr_segs; i++) {
6663 if (iov[seg].iov_base == iov[i].iov_base)
6664 goto out;
6665 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006666 }
6667 retval = 0;
6668out:
6669 return retval;
6670}
Josef Bacikeb838e72012-07-31 16:28:48 -04006671
Chris Mason16432982008-04-10 10:23:21 -04006672static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6673 const struct iovec *iov, loff_t offset,
6674 unsigned long nr_segs)
6675{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006676 struct file *file = iocb->ki_filp;
6677 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00006678 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00006679 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00006680 bool wakeup = true;
6681 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00006682 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006683
Chris Mason5a5f79b2010-05-26 21:33:37 -04006684 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006685 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006686 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006687
Miao Xie38851cc2013-02-08 07:04:11 +00006688 atomic_inc(&inode->i_dio_count);
6689 smp_mb__after_atomic_inc();
6690
Miao Xie09348562013-02-07 10:12:07 +00006691 if (rw & WRITE) {
6692 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00006693 /*
6694 * If the write DIO is beyond the EOF, we need update
6695 * the isize, but it is protected by i_mutex. So we can
6696 * not unlock the i_mutex at this case.
6697 */
6698 if (offset + count <= inode->i_size) {
6699 mutex_unlock(&inode->i_mutex);
6700 relock = true;
6701 }
Miao Xie09348562013-02-07 10:12:07 +00006702 ret = btrfs_delalloc_reserve_space(inode, count);
6703 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00006704 goto out;
6705 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
6706 &BTRFS_I(inode)->runtime_flags))) {
6707 inode_dio_done(inode);
6708 flags = DIO_LOCKING | DIO_SKIP_HOLES;
6709 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00006710 }
6711
6712 ret = __blockdev_direct_IO(rw, iocb, inode,
6713 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6714 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00006715 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00006716 if (rw & WRITE) {
6717 if (ret < 0 && ret != -EIOCBQUEUED)
6718 btrfs_delalloc_release_space(inode, count);
6719 else if (ret > 0 && (size_t)ret < count) {
6720 spin_lock(&BTRFS_I(inode)->lock);
6721 BTRFS_I(inode)->outstanding_extents++;
6722 spin_unlock(&BTRFS_I(inode)->lock);
6723 btrfs_delalloc_release_space(inode,
6724 count - (size_t)ret);
6725 }
6726 btrfs_delalloc_release_metadata(inode, 0);
6727 }
Miao Xie38851cc2013-02-08 07:04:11 +00006728out:
Miao Xie2e60a512013-02-08 07:01:08 +00006729 if (wakeup)
6730 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00006731 if (relock)
6732 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00006733
6734 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006735}
6736
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006737#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6738
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006739static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6740 __u64 start, __u64 len)
6741{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006742 int ret;
6743
6744 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6745 if (ret)
6746 return ret;
6747
Chris Masonec29ed52011-02-23 16:23:20 -05006748 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006749}
6750
Chris Mason9ebefb182007-06-15 13:50:00 -04006751int btrfs_readpage(struct file *file, struct page *page)
6752{
Chris Masond1310b22008-01-24 16:13:08 -05006753 struct extent_io_tree *tree;
6754 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006755 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006756}
Chris Mason1832a6d2007-12-21 16:27:21 -05006757
Chris Mason39279cc2007-06-12 06:35:45 -04006758static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6759{
Chris Masond1310b22008-01-24 16:13:08 -05006760 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04006761
6762
6763 if (current->flags & PF_MEMALLOC) {
6764 redirty_page_for_writepage(wbc, page);
6765 unlock_page(page);
6766 return 0;
6767 }
Chris Masond1310b22008-01-24 16:13:08 -05006768 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006769 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6770}
Chris Mason39279cc2007-06-12 06:35:45 -04006771
Chris Masonf4219502008-07-22 11:18:09 -04006772int btrfs_writepages(struct address_space *mapping,
6773 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006774{
Chris Masond1310b22008-01-24 16:13:08 -05006775 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006776
Chris Masond1310b22008-01-24 16:13:08 -05006777 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006778 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6779}
6780
Chris Mason3ab2fb52007-11-08 10:59:22 -05006781static int
6782btrfs_readpages(struct file *file, struct address_space *mapping,
6783 struct list_head *pages, unsigned nr_pages)
6784{
Chris Masond1310b22008-01-24 16:13:08 -05006785 struct extent_io_tree *tree;
6786 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006787 return extent_readpages(tree, mapping, pages, nr_pages,
6788 btrfs_get_extent);
6789}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006790static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006791{
Chris Masond1310b22008-01-24 16:13:08 -05006792 struct extent_io_tree *tree;
6793 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006794 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006795
Chris Masond1310b22008-01-24 16:13:08 -05006796 tree = &BTRFS_I(page->mapping->host)->io_tree;
6797 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006798 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006799 if (ret == 1) {
6800 ClearPagePrivate(page);
6801 set_page_private(page, 0);
6802 page_cache_release(page);
6803 }
6804 return ret;
6805}
Chris Mason39279cc2007-06-12 06:35:45 -04006806
Chris Masone6dcd2d2008-07-17 12:53:50 -04006807static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6808{
Chris Mason98509cf2008-09-11 15:51:43 -04006809 if (PageWriteback(page) || PageDirty(page))
6810 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006811 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006812}
6813
Chris Masona52d9a82007-08-27 16:49:44 -04006814static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6815{
Josef Bacik5fd02042012-05-02 14:00:54 -04006816 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006817 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006818 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006819 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006820 u64 page_start = page_offset(page);
6821 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006822
Chris Mason8b62b722009-09-02 16:53:46 -04006823 /*
6824 * we have the page locked, so new writeback can't start,
6825 * and the dirty bit won't be cleared while we are here.
6826 *
6827 * Wait for IO on this page so that we can safely clear
6828 * the PagePrivate2 bit and do ordered accounting
6829 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006830 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006831
Josef Bacik5fd02042012-05-02 14:00:54 -04006832 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006833 if (offset) {
6834 btrfs_releasepage(page, GFP_NOFS);
6835 return;
6836 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006837 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00006838 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04006839 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006840 /*
6841 * IO on this page will never be started, so we need
6842 * to account for any ordered extents now
6843 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006844 clear_extent_bit(tree, page_start, page_end,
6845 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006846 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6847 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006848 /*
6849 * whoever cleared the private bit is responsible
6850 * for the finish_ordered_io
6851 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006852 if (TestClearPagePrivate2(page) &&
6853 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6854 PAGE_CACHE_SIZE, 1)) {
6855 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006856 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006857 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006858 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006859 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006860 }
6861 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006862 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006863 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6864 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006865 __btrfs_releasepage(page, GFP_NOFS);
6866
Chris Mason4a096752008-07-21 10:29:44 -04006867 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006868 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006869 ClearPagePrivate(page);
6870 set_page_private(page, 0);
6871 page_cache_release(page);
6872 }
Chris Mason39279cc2007-06-12 06:35:45 -04006873}
6874
Chris Mason9ebefb182007-06-15 13:50:00 -04006875/*
6876 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6877 * called from a page fault handler when a page is first dirtied. Hence we must
6878 * be careful to check for EOF conditions here. We set the page up correctly
6879 * for a written page which means we get ENOSPC checking when writing into
6880 * holes and correct delalloc and unwritten extent mapping on filesystems that
6881 * support these features.
6882 *
6883 * We are not allowed to take the i_mutex here so we have to play games to
6884 * protect against truncate races as the page could now be beyond EOF. Because
6885 * vmtruncate() writes the inode size before removing pages, once we have the
6886 * page lock we can determine safely if the page is beyond EOF. If it is not
6887 * beyond EOF, then the page is guaranteed safe against truncation until we
6888 * unlock the page.
6889 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006890int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006891{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006892 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006893 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006894 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006895 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6896 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006897 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006898 char *kaddr;
6899 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006900 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006901 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006902 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006903 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006904 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006905
Jan Karab2b5ef52012-06-12 16:20:45 +02006906 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006907 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006908 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006909 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006910 reserved = 1;
6911 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006912 if (ret) {
6913 if (ret == -ENOMEM)
6914 ret = VM_FAULT_OOM;
6915 else /* -ENOSPC, -EIO, etc */
6916 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006917 if (reserved)
6918 goto out;
6919 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006920 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006921
Nick Piggin56a76f82009-03-31 15:23:23 -07006922 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006923again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006924 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006925 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006926 page_start = page_offset(page);
6927 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006928
Chris Mason9ebefb182007-06-15 13:50:00 -04006929 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006930 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006931 /* page got truncated out from underneath us */
6932 goto out_unlock;
6933 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006934 wait_on_page_writeback(page);
6935
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006936 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006937 set_page_extent_mapped(page);
6938
Chris Masoneb84ae02008-07-17 13:53:27 -04006939 /*
6940 * we can't set the delalloc bits if there are pending ordered
6941 * extents. Drop our locks and wait for them to finish
6942 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006943 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6944 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006945 unlock_extent_cached(io_tree, page_start, page_end,
6946 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006947 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006948 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006949 btrfs_put_ordered_extent(ordered);
6950 goto again;
6951 }
6952
Josef Bacikfbf19082009-10-01 17:10:23 -04006953 /*
6954 * XXX - page_mkwrite gets called every time the page is dirtied, even
6955 * if it was already dirty, so for space accounting reasons we need to
6956 * clear any delalloc bits for the range we are fixing to save. There
6957 * is probably a better way to do this, but for now keep consistent with
6958 * prepare_pages in the normal write path.
6959 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006960 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006961 EXTENT_DIRTY | EXTENT_DELALLOC |
6962 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006963 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006964
Josef Bacik2ac55d42010-02-03 19:33:23 +00006965 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6966 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006967 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006968 unlock_extent_cached(io_tree, page_start, page_end,
6969 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006970 ret = VM_FAULT_SIGBUS;
6971 goto out_unlock;
6972 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006973 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006974
6975 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006976 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006977 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006978 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006979 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006980
Chris Masone6dcd2d2008-07-17 12:53:50 -04006981 if (zero_start != PAGE_CACHE_SIZE) {
6982 kaddr = kmap(page);
6983 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6984 flush_dcache_page(page);
6985 kunmap(page);
6986 }
Chris Mason247e7432008-07-17 12:53:51 -04006987 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006988 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006989 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006990
Chris Mason257c62e2009-10-13 13:21:08 -04006991 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6992 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006993 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006994
Josef Bacik2ac55d42010-02-03 19:33:23 +00006995 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006996
6997out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006998 if (!ret) {
6999 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007000 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007001 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007002 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007003out:
Josef Bacikec39e182012-01-12 19:10:12 -05007004 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007005out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007006 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007007 return ret;
7008}
7009
Josef Bacika41ad392011-01-31 15:30:16 -05007010static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007011{
7012 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007013 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007014 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007015 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007016 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007017 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007018 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007019
Josef Bacik2aaa6652012-08-29 14:27:18 -04007020 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007021 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007022 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007023
Chris Mason4a096752008-07-21 10:29:44 -04007024 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007025 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007026
Josef Bacikfcb80c22011-05-03 10:40:22 -04007027 /*
7028 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7029 * 3 things going on here
7030 *
7031 * 1) We need to reserve space for our orphan item and the space to
7032 * delete our orphan item. Lord knows we don't want to have a dangling
7033 * orphan item because we didn't reserve space to remove it.
7034 *
7035 * 2) We need to reserve space to update our inode.
7036 *
7037 * 3) We need to have something to cache all the space that is going to
7038 * be free'd up by the truncate operation, but also have some slack
7039 * space reserved in case it uses space during the truncate (thank you
7040 * very much snapshotting).
7041 *
7042 * And we need these to all be seperate. The fact is we can use alot of
7043 * space doing the truncate, and we have no earthly idea how much space
7044 * we will use, so we need the truncate reservation to be seperate so it
7045 * doesn't end up using space reserved for updating the inode or
7046 * removing the orphan item. We also need to be able to stop the
7047 * transaction and start a new one, which means we need to be able to
7048 * update the inode several times, and we have no idea of knowing how
7049 * many times that will be, so we can't just reserve 1 item for the
7050 * entirety of the opration, so that has to be done seperately as well.
7051 * Then there is the orphan item, which does indeed need to be held on
7052 * to for the whole operation, and we need nobody to touch this reserved
7053 * space except the orphan code.
7054 *
7055 * So that leaves us with
7056 *
7057 * 1) root->orphan_block_rsv - for the orphan deletion.
7058 * 2) rsv - for the truncate reservation, which we will steal from the
7059 * transaction reservation.
7060 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7061 * updating the inode.
7062 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007063 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007064 if (!rsv)
7065 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007066 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007067 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007068
Josef Bacik907cbce2011-08-08 13:46:15 -04007069 /*
Josef Bacik07127182011-08-19 10:29:59 -04007070 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007071 * 1 for updating the inode.
7072 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007073 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007074 if (IS_ERR(trans)) {
7075 err = PTR_ERR(trans);
7076 goto out;
7077 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007078
Josef Bacik907cbce2011-08-08 13:46:15 -04007079 /* Migrate the slack space for the truncate to our reserve */
7080 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7081 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007082 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007083
Chris Mason5a3f23d2009-03-31 13:27:11 -04007084 /*
7085 * setattr is responsible for setting the ordered_data_close flag,
7086 * but that is only tested during the last file release. That
7087 * could happen well after the next commit, leaving a great big
7088 * window where new writes may get lost if someone chooses to write
7089 * to this file after truncating to zero
7090 *
7091 * The inode doesn't have any dirty data here, and so if we commit
7092 * this is a noop. If someone immediately starts writing to the inode
7093 * it is very likely we'll catch some of their writes in this
7094 * transaction, and the commit will find this file on the ordered
7095 * data list with good things to send down.
7096 *
7097 * This is a best effort solution, there is still a window where
7098 * using truncate to replace the contents of the file will
7099 * end up with a zero length file after a crash.
7100 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007101 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7102 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007103 btrfs_add_ordered_operation(trans, root, inode);
7104
Josef Bacik5dc562c2012-08-17 13:14:17 -04007105 /*
7106 * So if we truncate and then write and fsync we normally would just
7107 * write the extents that changed, which is a problem if we need to
7108 * first truncate that entire inode. So set this flag so we write out
7109 * all of the extents in the inode to the sync log so we're completely
7110 * safe.
7111 */
7112 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007113 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007114
Yan, Zheng80825102009-11-12 09:35:36 +00007115 while (1) {
7116 ret = btrfs_truncate_inode_items(trans, root, inode,
7117 inode->i_size,
7118 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007119 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007120 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007121 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007122 }
Chris Mason39279cc2007-06-12 06:35:45 -04007123
Josef Bacikfcb80c22011-05-03 10:40:22 -04007124 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007125 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007126 if (ret) {
7127 err = ret;
7128 break;
7129 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007130
Yan, Zheng80825102009-11-12 09:35:36 +00007131 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007132 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007133
7134 trans = btrfs_start_transaction(root, 2);
7135 if (IS_ERR(trans)) {
7136 ret = err = PTR_ERR(trans);
7137 trans = NULL;
7138 break;
7139 }
7140
7141 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7142 rsv, min_size);
7143 BUG_ON(ret); /* shouldn't happen */
7144 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007145 }
7146
7147 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007148 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007149 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007150 if (ret)
7151 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007152 }
7153
Chris Mason917c16b2011-11-08 14:49:59 -05007154 if (trans) {
7155 trans->block_rsv = &root->fs_info->trans_block_rsv;
7156 ret = btrfs_update_inode(trans, root, inode);
7157 if (ret && !err)
7158 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007159
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007160 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007161 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007162 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007163
7164out:
7165 btrfs_free_block_rsv(root, rsv);
7166
Josef Bacik3893e332011-01-31 16:03:11 -05007167 if (ret && !err)
7168 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007169
Josef Bacik3893e332011-01-31 16:03:11 -05007170 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007171}
7172
Sven Wegener3b963622008-06-09 21:57:42 -04007173/*
Chris Masond352ac62008-09-29 15:18:18 -04007174 * create a new subvolume directory/inode (helper for the ioctl).
7175 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007176int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007177 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007178{
Chris Mason39279cc2007-06-12 06:35:45 -04007179 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007180 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007181 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007182
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007183 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7184 new_dirid, new_dirid,
7185 S_IFDIR | (~current_umask() & S_IRWXUGO),
7186 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007187 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007188 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007189 inode->i_op = &btrfs_dir_inode_operations;
7190 inode->i_fop = &btrfs_dir_file_operations;
7191
Miklos Szeredibfe86842011-10-28 14:13:29 +02007192 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007193 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007194
Yan, Zheng76dda932009-09-21 16:00:26 -04007195 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007196
Yan, Zheng76dda932009-09-21 16:00:26 -04007197 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007198 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007199}
7200
Chris Mason39279cc2007-06-12 06:35:45 -04007201struct inode *btrfs_alloc_inode(struct super_block *sb)
7202{
7203 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007204 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007205
7206 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7207 if (!ei)
7208 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007209
7210 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007211 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007212 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007213 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007214 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007215 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007216 ei->disk_i_size = 0;
7217 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007218 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007219 ei->index_cnt = (u64)-1;
7220 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007221 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007222
Josef Bacik9e0baf62011-07-15 15:16:44 +00007223 spin_lock_init(&ei->lock);
7224 ei->outstanding_extents = 0;
7225 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007226
Josef Bacik72ac3c02012-05-23 14:13:11 -04007227 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007228 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007229
Miao Xie16cdcec2011-04-22 18:12:22 +08007230 ei->delayed_node = NULL;
7231
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007232 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007233 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007234 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7235 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007236 ei->io_tree.track_uptodate = 1;
7237 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007238 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007239 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007240 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007241 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007242 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007243 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007244 RB_CLEAR_NODE(&ei->rb_node);
7245
7246 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007247}
7248
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007249static void btrfs_i_callback(struct rcu_head *head)
7250{
7251 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007252 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7253}
7254
Chris Mason39279cc2007-06-12 06:35:45 -04007255void btrfs_destroy_inode(struct inode *inode)
7256{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007257 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007258 struct btrfs_root *root = BTRFS_I(inode)->root;
7259
Al Virob3d9b7a2012-06-09 13:51:19 -04007260 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007261 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007262 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7263 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007264 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7265 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007266
Chris Mason5a3f23d2009-03-31 13:27:11 -04007267 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007268 * This can happen where we create an inode, but somebody else also
7269 * created the same inode and we need to destroy the one we already
7270 * created.
7271 */
7272 if (!root)
7273 goto free;
7274
7275 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007276 * Make sure we're properly removed from the ordered operation
7277 * lists.
7278 */
7279 smp_mb();
7280 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7281 spin_lock(&root->fs_info->ordered_extent_lock);
7282 list_del_init(&BTRFS_I(inode)->ordered_operations);
7283 spin_unlock(&root->fs_info->ordered_extent_lock);
7284 }
7285
Josef Bacik8a35d952012-05-23 14:26:42 -04007286 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7287 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007288 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7289 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007290 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007291 }
Josef Bacik7b128762008-07-24 12:17:14 -04007292
Chris Masond3977122009-01-05 21:25:51 -05007293 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007294 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7295 if (!ordered)
7296 break;
7297 else {
Chris Masond3977122009-01-05 21:25:51 -05007298 printk(KERN_ERR "btrfs found ordered "
7299 "extent %llu %llu on inode cleanup\n",
7300 (unsigned long long)ordered->file_offset,
7301 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007302 btrfs_remove_ordered_extent(inode, ordered);
7303 btrfs_put_ordered_extent(ordered);
7304 btrfs_put_ordered_extent(ordered);
7305 }
7306 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007307 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007308 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007309free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007310 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007311 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007312}
7313
Al Viro45321ac2010-06-07 13:43:19 -04007314int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007315{
7316 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007317
Josef Bacik0af3d002010-06-21 14:48:16 -04007318 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007319 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007320 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007321 else
Al Viro45321ac2010-06-07 13:43:19 -04007322 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007323}
7324
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007325static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007326{
7327 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7328
7329 inode_init_once(&ei->vfs_inode);
7330}
7331
7332void btrfs_destroy_cachep(void)
7333{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007334 /*
7335 * Make sure all delayed rcu free inodes are flushed before we
7336 * destroy cache.
7337 */
7338 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007339 if (btrfs_inode_cachep)
7340 kmem_cache_destroy(btrfs_inode_cachep);
7341 if (btrfs_trans_handle_cachep)
7342 kmem_cache_destroy(btrfs_trans_handle_cachep);
7343 if (btrfs_transaction_cachep)
7344 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007345 if (btrfs_path_cachep)
7346 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007347 if (btrfs_free_space_cachep)
7348 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007349 if (btrfs_delalloc_work_cachep)
7350 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007351}
7352
7353int btrfs_init_cachep(void)
7354{
David Sterba837e1972012-09-07 03:00:48 -06007355 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007356 sizeof(struct btrfs_inode), 0,
7357 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007358 if (!btrfs_inode_cachep)
7359 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007360
David Sterba837e1972012-09-07 03:00:48 -06007361 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007362 sizeof(struct btrfs_trans_handle), 0,
7363 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007364 if (!btrfs_trans_handle_cachep)
7365 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007366
David Sterba837e1972012-09-07 03:00:48 -06007367 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007368 sizeof(struct btrfs_transaction), 0,
7369 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007370 if (!btrfs_transaction_cachep)
7371 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007372
David Sterba837e1972012-09-07 03:00:48 -06007373 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007374 sizeof(struct btrfs_path), 0,
7375 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007376 if (!btrfs_path_cachep)
7377 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007378
David Sterba837e1972012-09-07 03:00:48 -06007379 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007380 sizeof(struct btrfs_free_space), 0,
7381 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7382 if (!btrfs_free_space_cachep)
7383 goto fail;
7384
Miao Xie8ccf6f192012-10-25 09:28:04 +00007385 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7386 sizeof(struct btrfs_delalloc_work), 0,
7387 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7388 NULL);
7389 if (!btrfs_delalloc_work_cachep)
7390 goto fail;
7391
Chris Mason39279cc2007-06-12 06:35:45 -04007392 return 0;
7393fail:
7394 btrfs_destroy_cachep();
7395 return -ENOMEM;
7396}
7397
7398static int btrfs_getattr(struct vfsmount *mnt,
7399 struct dentry *dentry, struct kstat *stat)
7400{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007401 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007402 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007403 u32 blocksize = inode->i_sb->s_blocksize;
7404
Chris Mason39279cc2007-06-12 06:35:45 -04007405 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007406 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007407 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007408
7409 spin_lock(&BTRFS_I(inode)->lock);
7410 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7411 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007412 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007413 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007414 return 0;
7415}
7416
Liu Bo75e7cb72011-03-22 10:12:20 +00007417/*
7418 * If a file is moved, it will inherit the cow and compression flags of the new
7419 * directory.
7420 */
7421static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7422{
7423 struct btrfs_inode *b_dir = BTRFS_I(dir);
7424 struct btrfs_inode *b_inode = BTRFS_I(inode);
7425
7426 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7427 b_inode->flags |= BTRFS_INODE_NODATACOW;
7428 else
7429 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7430
Liu Bobc178232012-06-14 02:23:18 -06007431 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007432 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007433 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7434 } else {
7435 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7436 BTRFS_INODE_NOCOMPRESS);
7437 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007438}
7439
Chris Masond3977122009-01-05 21:25:51 -05007440static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7441 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007442{
7443 struct btrfs_trans_handle *trans;
7444 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007445 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007446 struct inode *new_inode = new_dentry->d_inode;
7447 struct inode *old_inode = old_dentry->d_inode;
7448 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007449 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007450 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007451 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007452 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007453
Li Zefan33345d012011-04-20 10:31:50 +08007454 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007455 return -EPERM;
7456
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007457 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007458 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007459 return -EXDEV;
7460
Li Zefan33345d012011-04-20 10:31:50 +08007461 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7462 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007463 return -ENOTEMPTY;
7464
Chris Mason39279cc2007-06-12 06:35:45 -04007465 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007466 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007467 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007468
7469
7470 /* check for collisions, even if the name isn't there */
7471 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7472 new_dentry->d_name.name,
7473 new_dentry->d_name.len);
7474
7475 if (ret) {
7476 if (ret == -EEXIST) {
7477 /* we shouldn't get
7478 * eexist without a new_inode */
7479 if (!new_inode) {
7480 WARN_ON(1);
7481 return ret;
7482 }
7483 } else {
7484 /* maybe -EOVERFLOW */
7485 return ret;
7486 }
7487 }
7488 ret = 0;
7489
Chris Mason5a3f23d2009-03-31 13:27:11 -04007490 /*
7491 * we're using rename to replace one file with another.
7492 * and the replacement file is large. Start IO on it now so
7493 * we don't add too much work to the end of the transaction
7494 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007495 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007496 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7497 filemap_flush(old_inode->i_mapping);
7498
Yan, Zheng76dda932009-09-21 16:00:26 -04007499 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007500 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007501 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007502 /*
7503 * We want to reserve the absolute worst case amount of items. So if
7504 * both inodes are subvols and we need to unlink them then that would
7505 * require 4 item modifications, but if they are both normal inodes it
7506 * would require 5 item modifications, so we'll assume their normal
7507 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7508 * should cover the worst case number of items we'll modify.
7509 */
7510 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007511 if (IS_ERR(trans)) {
7512 ret = PTR_ERR(trans);
7513 goto out_notrans;
7514 }
Chris Mason5f39d392007-10-15 16:14:19 -04007515
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007516 if (dest != root)
7517 btrfs_record_root_in_trans(trans, dest);
7518
Yan, Zhenga5719522009-09-24 09:17:31 -04007519 ret = btrfs_set_inode_index(new_dir, &index);
7520 if (ret)
7521 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007522
Li Zefan33345d012011-04-20 10:31:50 +08007523 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007524 /* force full log commit if subvolume involved. */
7525 root->fs_info->last_trans_log_full_commit = trans->transid;
7526 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007527 ret = btrfs_insert_inode_ref(trans, dest,
7528 new_dentry->d_name.name,
7529 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007530 old_ino,
7531 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007532 if (ret)
7533 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007534 /*
7535 * this is an ugly little race, but the rename is required
7536 * to make sure that if we crash, the inode is either at the
7537 * old name or the new one. pinning the log transaction lets
7538 * us make sure we don't allow a log commit to come in after
7539 * we unlink the name but before we add the new name back in.
7540 */
7541 btrfs_pin_log_trans(root);
7542 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007543 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007544 * make sure the inode gets flushed if it is replacing
7545 * something.
7546 */
Li Zefan33345d012011-04-20 10:31:50 +08007547 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007548 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007549
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007550 inode_inc_iversion(old_dir);
7551 inode_inc_iversion(new_dir);
7552 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007553 old_dir->i_ctime = old_dir->i_mtime = ctime;
7554 new_dir->i_ctime = new_dir->i_mtime = ctime;
7555 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007556
Chris Mason12fcfd22009-03-24 10:24:20 -04007557 if (old_dentry->d_parent != new_dentry->d_parent)
7558 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7559
Li Zefan33345d012011-04-20 10:31:50 +08007560 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007561 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7562 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7563 old_dentry->d_name.name,
7564 old_dentry->d_name.len);
7565 } else {
Al Viro92986792011-03-04 17:14:37 +00007566 ret = __btrfs_unlink_inode(trans, root, old_dir,
7567 old_dentry->d_inode,
7568 old_dentry->d_name.name,
7569 old_dentry->d_name.len);
7570 if (!ret)
7571 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007573 if (ret) {
7574 btrfs_abort_transaction(trans, root, ret);
7575 goto out_fail;
7576 }
Chris Mason39279cc2007-06-12 06:35:45 -04007577
7578 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007579 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007580 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007581 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007582 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7583 root_objectid = BTRFS_I(new_inode)->location.objectid;
7584 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7585 root_objectid,
7586 new_dentry->d_name.name,
7587 new_dentry->d_name.len);
7588 BUG_ON(new_inode->i_nlink == 0);
7589 } else {
7590 ret = btrfs_unlink_inode(trans, dest, new_dir,
7591 new_dentry->d_inode,
7592 new_dentry->d_name.name,
7593 new_dentry->d_name.len);
7594 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007595 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007596 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007597 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007598 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007599 if (ret) {
7600 btrfs_abort_transaction(trans, root, ret);
7601 goto out_fail;
7602 }
Chris Mason39279cc2007-06-12 06:35:45 -04007603 }
Josef Bacikaec74772008-07-24 12:12:38 -04007604
Liu Bo75e7cb72011-03-22 10:12:20 +00007605 fixup_inode_flags(new_dir, old_inode);
7606
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007607 ret = btrfs_add_link(trans, new_dir, old_inode,
7608 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007609 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007610 if (ret) {
7611 btrfs_abort_transaction(trans, root, ret);
7612 goto out_fail;
7613 }
Chris Mason39279cc2007-06-12 06:35:45 -04007614
Li Zefan33345d012011-04-20 10:31:50 +08007615 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007616 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007617 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007618 btrfs_end_log_trans(root);
7619 }
Chris Mason39279cc2007-06-12 06:35:45 -04007620out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007621 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007622out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007623 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007624 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007625
Chris Mason39279cc2007-06-12 06:35:45 -04007626 return ret;
7627}
7628
Miao Xie8ccf6f192012-10-25 09:28:04 +00007629static void btrfs_run_delalloc_work(struct btrfs_work *work)
7630{
7631 struct btrfs_delalloc_work *delalloc_work;
7632
7633 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7634 work);
7635 if (delalloc_work->wait)
7636 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7637 else
7638 filemap_flush(delalloc_work->inode->i_mapping);
7639
7640 if (delalloc_work->delay_iput)
7641 btrfs_add_delayed_iput(delalloc_work->inode);
7642 else
7643 iput(delalloc_work->inode);
7644 complete(&delalloc_work->completion);
7645}
7646
7647struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7648 int wait, int delay_iput)
7649{
7650 struct btrfs_delalloc_work *work;
7651
7652 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7653 if (!work)
7654 return NULL;
7655
7656 init_completion(&work->completion);
7657 INIT_LIST_HEAD(&work->list);
7658 work->inode = inode;
7659 work->wait = wait;
7660 work->delay_iput = delay_iput;
7661 work->work.func = btrfs_run_delalloc_work;
7662
7663 return work;
7664}
7665
7666void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7667{
7668 wait_for_completion(&work->completion);
7669 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7670}
7671
Chris Masond352ac62008-09-29 15:18:18 -04007672/*
7673 * some fairly slow code that needs optimization. This walks the list
7674 * of all the inodes with pending delalloc and forces them to disk.
7675 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007676int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007677{
Chris Masonea8c2812008-08-04 23:17:27 -04007678 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007679 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007680 struct btrfs_delalloc_work *work, *next;
7681 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00007682 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007683 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007684
Yan Zhengc146afa2008-11-12 14:34:12 -05007685 if (root->fs_info->sb->s_flags & MS_RDONLY)
7686 return -EROFS;
7687
Miao Xie8ccf6f192012-10-25 09:28:04 +00007688 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007689 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00007690
Chris Mason75eff682008-12-15 15:54:40 -05007691 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007692 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7693 while (!list_empty(&splice)) {
7694 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04007695 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007696
7697 list_del_init(&binode->delalloc_inodes);
7698
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007699 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00007700 if (!inode) {
7701 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
7702 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007703 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007704 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007705
7706 list_add_tail(&binode->delalloc_inodes,
7707 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007708 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007709
7710 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7711 if (unlikely(!work)) {
7712 ret = -ENOMEM;
7713 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007714 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007715 list_add_tail(&work->list, &works);
7716 btrfs_queue_worker(&root->fs_info->flush_workers,
7717 &work->work);
7718
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007719 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007720 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007721 }
Chris Mason75eff682008-12-15 15:54:40 -05007722 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007723
Miao Xie1eafa6c2013-01-22 10:49:00 +00007724 list_for_each_entry_safe(work, next, &works, list) {
7725 list_del_init(&work->list);
7726 btrfs_wait_and_free_delalloc_work(work);
7727 }
7728
Chris Mason8c8bee12008-09-29 11:19:10 -04007729 /* the filemap_flush will queue IO into the worker threads, but
7730 * we have to make sure the IO is actually started and that
7731 * ordered extents get created before we return
7732 */
7733 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007734 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007735 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007736 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007737 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7738 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007739 }
7740 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007741 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007742out:
7743 list_for_each_entry_safe(work, next, &works, list) {
7744 list_del_init(&work->list);
7745 btrfs_wait_and_free_delalloc_work(work);
7746 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007747
7748 if (!list_empty_careful(&splice)) {
7749 spin_lock(&root->fs_info->delalloc_lock);
7750 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7751 spin_unlock(&root->fs_info->delalloc_lock);
7752 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00007753 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007754}
7755
Chris Mason39279cc2007-06-12 06:35:45 -04007756static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7757 const char *symname)
7758{
7759 struct btrfs_trans_handle *trans;
7760 struct btrfs_root *root = BTRFS_I(dir)->root;
7761 struct btrfs_path *path;
7762 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007763 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007764 int err;
7765 int drop_inode = 0;
7766 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007767 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007768 int name_len;
7769 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007770 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007771 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007772 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007773
7774 name_len = strlen(symname) + 1;
7775 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7776 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007777
Josef Bacik9ed74f22009-09-11 16:12:44 -04007778 /*
7779 * 2 items for inode item and ref
7780 * 2 items for dir items
7781 * 1 item for xattr if selinux is on
7782 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007783 trans = btrfs_start_transaction(root, 5);
7784 if (IS_ERR(trans))
7785 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007786
Li Zefan581bb052011-04-20 10:06:11 +08007787 err = btrfs_find_free_ino(root, &objectid);
7788 if (err)
7789 goto out_unlock;
7790
Josef Bacikaec74772008-07-24 12:12:38 -04007791 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007792 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007793 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007794 if (IS_ERR(inode)) {
7795 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007796 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007797 }
Chris Mason39279cc2007-06-12 06:35:45 -04007798
Eric Paris2a7dba32011-02-01 11:05:39 -05007799 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007800 if (err) {
7801 drop_inode = 1;
7802 goto out_unlock;
7803 }
7804
Casey Schauflerad19db72011-12-15 10:09:07 -05007805 /*
7806 * If the active LSM wants to access the inode during
7807 * d_instantiate it needs these. Smack checks to see
7808 * if the filesystem supports xattrs by looking at the
7809 * ops vector.
7810 */
7811 inode->i_fop = &btrfs_file_operations;
7812 inode->i_op = &btrfs_file_inode_operations;
7813
Josef Bacika1b075d2010-11-19 20:36:11 +00007814 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007815 if (err)
7816 drop_inode = 1;
7817 else {
7818 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007819 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007820 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007821 }
Chris Mason39279cc2007-06-12 06:35:45 -04007822 if (drop_inode)
7823 goto out_unlock;
7824
7825 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007826 if (!path) {
7827 err = -ENOMEM;
7828 drop_inode = 1;
7829 goto out_unlock;
7830 }
Li Zefan33345d012011-04-20 10:31:50 +08007831 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007832 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007833 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7834 datasize = btrfs_file_extent_calc_inline_size(name_len);
7835 err = btrfs_insert_empty_item(trans, root, path, &key,
7836 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007837 if (err) {
7838 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007839 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007840 goto out_unlock;
7841 }
Chris Mason5f39d392007-10-15 16:14:19 -04007842 leaf = path->nodes[0];
7843 ei = btrfs_item_ptr(leaf, path->slots[0],
7844 struct btrfs_file_extent_item);
7845 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7846 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007847 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007848 btrfs_set_file_extent_encryption(leaf, ei, 0);
7849 btrfs_set_file_extent_compression(leaf, ei, 0);
7850 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7851 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7852
Chris Mason39279cc2007-06-12 06:35:45 -04007853 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007854 write_extent_buffer(leaf, symname, ptr, name_len);
7855 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007856 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007857
Chris Mason39279cc2007-06-12 06:35:45 -04007858 inode->i_op = &btrfs_symlink_inode_operations;
7859 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007860 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007861 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007862 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007863 err = btrfs_update_inode(trans, root, inode);
7864 if (err)
7865 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007866
7867out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007868 if (!err)
7869 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007870 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007871 if (drop_inode) {
7872 inode_dec_link_count(inode);
7873 iput(inode);
7874 }
Liu Bob53d3f52012-11-14 14:34:34 +00007875 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007876 return err;
7877}
Chris Mason16432982008-04-10 10:23:21 -04007878
Josef Bacik0af3d002010-06-21 14:48:16 -04007879static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7880 u64 start, u64 num_bytes, u64 min_size,
7881 loff_t actual_len, u64 *alloc_hint,
7882 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007883{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007884 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7885 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007886 struct btrfs_root *root = BTRFS_I(inode)->root;
7887 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007888 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007889 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007890 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007891 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007892
Josef Bacik0af3d002010-06-21 14:48:16 -04007893 if (trans)
7894 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007895 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007896 if (own_trans) {
7897 trans = btrfs_start_transaction(root, 3);
7898 if (IS_ERR(trans)) {
7899 ret = PTR_ERR(trans);
7900 break;
7901 }
Yan Zhengd899e052008-10-30 14:25:28 -04007902 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007903
Yan, Zhengefa56462010-05-16 10:49:59 -04007904 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007905 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007906 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007907 if (own_trans)
7908 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007909 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007910 }
7911
Yan Zhengd899e052008-10-30 14:25:28 -04007912 ret = insert_reserved_file_extent(trans, inode,
7913 cur_offset, ins.objectid,
7914 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007915 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007916 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007917 if (ret) {
7918 btrfs_abort_transaction(trans, root, ret);
7919 if (own_trans)
7920 btrfs_end_transaction(trans, root);
7921 break;
7922 }
Chris Masona1ed8352009-09-11 12:27:37 -04007923 btrfs_drop_extent_cache(inode, cur_offset,
7924 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007925
Josef Bacik5dc562c2012-08-17 13:14:17 -04007926 em = alloc_extent_map();
7927 if (!em) {
7928 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7929 &BTRFS_I(inode)->runtime_flags);
7930 goto next;
7931 }
7932
7933 em->start = cur_offset;
7934 em->orig_start = cur_offset;
7935 em->len = ins.offset;
7936 em->block_start = ins.objectid;
7937 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007938 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007939 em->bdev = root->fs_info->fs_devices->latest_bdev;
7940 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7941 em->generation = trans->transid;
7942
7943 while (1) {
7944 write_lock(&em_tree->lock);
7945 ret = add_extent_mapping(em_tree, em);
7946 if (!ret)
7947 list_move(&em->list,
7948 &em_tree->modified_extents);
7949 write_unlock(&em_tree->lock);
7950 if (ret != -EEXIST)
7951 break;
7952 btrfs_drop_extent_cache(inode, cur_offset,
7953 cur_offset + ins.offset - 1,
7954 0);
7955 }
7956 free_extent_map(em);
7957next:
Yan Zhengd899e052008-10-30 14:25:28 -04007958 num_bytes -= ins.offset;
7959 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007960 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007961
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007962 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007963 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007964 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007965 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007966 (actual_len > inode->i_size) &&
7967 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007968 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007969 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007970 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007971 i_size = cur_offset;
7972 i_size_write(inode, i_size);
7973 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007974 }
7975
Yan Zhengd899e052008-10-30 14:25:28 -04007976 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007977
7978 if (ret) {
7979 btrfs_abort_transaction(trans, root, ret);
7980 if (own_trans)
7981 btrfs_end_transaction(trans, root);
7982 break;
7983 }
Yan Zhengd899e052008-10-30 14:25:28 -04007984
Josef Bacik0af3d002010-06-21 14:48:16 -04007985 if (own_trans)
7986 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007987 }
Yan Zhengd899e052008-10-30 14:25:28 -04007988 return ret;
7989}
7990
Josef Bacik0af3d002010-06-21 14:48:16 -04007991int btrfs_prealloc_file_range(struct inode *inode, int mode,
7992 u64 start, u64 num_bytes, u64 min_size,
7993 loff_t actual_len, u64 *alloc_hint)
7994{
7995 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7996 min_size, actual_len, alloc_hint,
7997 NULL);
7998}
7999
8000int btrfs_prealloc_file_range_trans(struct inode *inode,
8001 struct btrfs_trans_handle *trans, int mode,
8002 u64 start, u64 num_bytes, u64 min_size,
8003 loff_t actual_len, u64 *alloc_hint)
8004{
8005 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8006 min_size, actual_len, alloc_hint, trans);
8007}
8008
Chris Masone6dcd2d2008-07-17 12:53:50 -04008009static int btrfs_set_page_dirty(struct page *page)
8010{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008011 return __set_page_dirty_nobuffers(page);
8012}
8013
Al Viro10556cb2011-06-20 19:28:19 -04008014static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008015{
Li Zefanb83cc962010-12-20 16:04:08 +08008016 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008017 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008018
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008019 if (mask & MAY_WRITE &&
8020 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8021 if (btrfs_root_readonly(root))
8022 return -EROFS;
8023 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8024 return -EACCES;
8025 }
Al Viro2830ba72011-06-20 19:16:29 -04008026 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008027}
Chris Mason39279cc2007-06-12 06:35:45 -04008028
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008029static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008030 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008031 .lookup = btrfs_lookup,
8032 .create = btrfs_create,
8033 .unlink = btrfs_unlink,
8034 .link = btrfs_link,
8035 .mkdir = btrfs_mkdir,
8036 .rmdir = btrfs_rmdir,
8037 .rename = btrfs_rename,
8038 .symlink = btrfs_symlink,
8039 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008040 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008041 .setxattr = btrfs_setxattr,
8042 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008043 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008044 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008045 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008046 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008047};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008048static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008049 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008050 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008051 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008052};
Yan, Zheng76dda932009-09-21 16:00:26 -04008053
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008054static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008055 .llseek = generic_file_llseek,
8056 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008057 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008058 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008059#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008060 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008061#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008062 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008063 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008064};
8065
Chris Masond1310b22008-01-24 16:13:08 -05008066static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008067 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008068 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008069 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008070 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008071 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008072 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008073 .set_bit_hook = btrfs_set_bit_hook,
8074 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008075 .merge_extent_hook = btrfs_merge_extent_hook,
8076 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008077};
8078
Chris Mason35054392009-01-21 13:11:13 -05008079/*
8080 * btrfs doesn't support the bmap operation because swapfiles
8081 * use bmap to make a mapping of extents in the file. They assume
8082 * these extents won't change over the life of the file and they
8083 * use the bmap result to do IO directly to the drive.
8084 *
8085 * the btrfs bmap call would return logical addresses that aren't
8086 * suitable for IO and they also will change frequently as COW
8087 * operations happen. So, swapfile + btrfs == corruption.
8088 *
8089 * For now we're avoiding this by dropping bmap.
8090 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008091static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008092 .readpage = btrfs_readpage,
8093 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008094 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008095 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008096 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008097 .invalidatepage = btrfs_invalidatepage,
8098 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008099 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008100 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008101};
8102
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008103static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008104 .readpage = btrfs_readpage,
8105 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008106 .invalidatepage = btrfs_invalidatepage,
8107 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008108};
8109
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008110static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008111 .getattr = btrfs_getattr,
8112 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008113 .setxattr = btrfs_setxattr,
8114 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008115 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008116 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008117 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008118 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008119 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008120 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008121};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008122static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008123 .getattr = btrfs_getattr,
8124 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008125 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008126 .setxattr = btrfs_setxattr,
8127 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008128 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008129 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008130 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008131 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008132};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008133static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008134 .readlink = generic_readlink,
8135 .follow_link = page_follow_link_light,
8136 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008137 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008138 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008139 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008140 .setxattr = btrfs_setxattr,
8141 .getxattr = btrfs_getxattr,
8142 .listxattr = btrfs_listxattr,
8143 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008144 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008145 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008146};
Yan, Zheng76dda932009-09-21 16:00:26 -04008147
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008148const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008149 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008150 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008151};