blob: 2b48d2d51d50b81862eca4e725d76e43049e1153 [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, Zhengc21677542009-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, Zhengc21677542009-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 Masonb888db22007-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 Masonb888db22007-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 Masonf03d9302009-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 Bacik7a7eaa402011-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, Zhengc21677542009-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, Zhengc21677542009-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 Masonf03d9302009-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
Chris Mason771ed682008-11-06 22:02:51 -0500611
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);
651 kfree(async_extent);
652 cond_resched();
653 continue;
654 }
655
656 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100657 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500658
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400659 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100660 if (IS_ERR(trans)) {
661 ret = PTR_ERR(trans);
662 } else {
663 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
664 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->compressed_size,
666 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500667 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600668 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100669 btrfs_abort_transaction(trans, root, ret);
670 btrfs_end_transaction(trans, root);
671 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000672
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500673 if (ret) {
674 int i;
675 for (i = 0; i < async_extent->nr_pages; i++) {
676 WARN_ON(async_extent->pages[i]->mapping);
677 page_cache_release(async_extent->pages[i]);
678 }
679 kfree(async_extent->pages);
680 async_extent->nr_pages = 0;
681 async_extent->pages = NULL;
682 unlock_extent(io_tree, async_extent->start,
683 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100684 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100685 if (ret == -ENOSPC)
686 goto retry;
687 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688 }
689
Yan, Zhengc21677542009-11-12 09:34:21 +0000690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
David Sterba172ddd62011-04-21 00:48:27 +0200698 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500700 em->start = async_extent->start;
701 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500702 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400703 em->mod_start = em->start;
704 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500705
706 em->block_start = ins.objectid;
707 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500708 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500709 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800710 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500711 set_bit(EXTENT_FLAG_PINNED, &em->flags);
712 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400713 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500714
Chris Masond3977122009-01-05 21:25:51 -0500715 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400716 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500717 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400718 if (!ret)
719 list_move(&em->list,
720 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400721 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500722 if (ret != -EEXIST) {
723 free_extent_map(em);
724 break;
725 }
726 btrfs_drop_extent_cache(inode, async_extent->start,
727 async_extent->start +
728 async_extent->ram_size - 1, 0);
729 }
730
Li Zefan261507a02010-12-17 14:21:50 +0800731 ret = btrfs_add_ordered_extent_compress(inode,
732 async_extent->start,
733 ins.objectid,
734 async_extent->ram_size,
735 ins.offset,
736 BTRFS_ORDERED_COMPRESSED,
737 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100738 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500739
Chris Mason771ed682008-11-06 22:02:51 -0500740 /*
741 * clear dirty, set writeback and unlock the pages.
742 */
743 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400744 &BTRFS_I(inode)->io_tree,
745 async_extent->start,
746 async_extent->start +
747 async_extent->ram_size - 1,
748 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
749 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400750 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400751 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500752
753 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500754 async_extent->start,
755 async_extent->ram_size,
756 ins.objectid,
757 ins.offset, async_extent->pages,
758 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500759
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100760 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500761 alloc_hint = ins.objectid + ins.offset;
762 kfree(async_extent);
763 cond_resched();
764 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100765 ret = 0;
766out:
767 return ret;
768out_free:
769 kfree(async_extent);
770 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500771}
772
Josef Bacik4b46fce2010-05-23 11:00:55 -0400773static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
774 u64 num_bytes)
775{
776 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
777 struct extent_map *em;
778 u64 alloc_hint = 0;
779
780 read_lock(&em_tree->lock);
781 em = search_extent_mapping(em_tree, start, num_bytes);
782 if (em) {
783 /*
784 * if block start isn't an actual block number then find the
785 * first block in this inode and use that as a hint. If that
786 * block is also bogus then just don't worry about it.
787 */
788 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
789 free_extent_map(em);
790 em = search_extent_mapping(em_tree, 0, 0);
791 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
792 alloc_hint = em->block_start;
793 if (em)
794 free_extent_map(em);
795 } else {
796 alloc_hint = em->block_start;
797 free_extent_map(em);
798 }
799 }
800 read_unlock(&em_tree->lock);
801
802 return alloc_hint;
803}
804
Chris Mason771ed682008-11-06 22:02:51 -0500805/*
806 * when extent_io.c finds a delayed allocation range in the file,
807 * the call backs end up in this code. The basic idea is to
808 * allocate extents on disk for the range, and create ordered data structs
809 * in ram to track those extents.
810 *
811 * locked_page is the page that writepage had locked already. We use
812 * it to make sure we don't do extra locks or unlocks.
813 *
814 * *page_started is set to one if we unlock locked_page and do everything
815 * required to start IO on it. It may be clean and already done with
816 * IO when we return.
817 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000818static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
819 struct inode *inode,
820 struct btrfs_root *root,
821 struct page *locked_page,
822 u64 start, u64 end, int *page_started,
823 unsigned long *nr_written,
824 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500825{
Chris Mason771ed682008-11-06 22:02:51 -0500826 u64 alloc_hint = 0;
827 u64 num_bytes;
828 unsigned long ram_size;
829 u64 disk_num_bytes;
830 u64 cur_alloc_size;
831 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500832 struct btrfs_key ins;
833 struct extent_map *em;
834 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
835 int ret = 0;
836
Liu Bo83eea1f2012-07-10 05:28:39 -0600837 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500838
Chris Mason771ed682008-11-06 22:02:51 -0500839 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
840 num_bytes = max(blocksize, num_bytes);
841 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500842
Chris Mason4cb53002011-05-24 15:35:30 -0400843 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400844 if (num_bytes < 64 * 1024 &&
845 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400846 btrfs_add_inode_defrag(trans, inode);
847
Chris Mason771ed682008-11-06 22:02:51 -0500848 if (start == 0) {
849 /* lets try to make an inline extent */
850 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000851 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500852 if (ret == 0) {
853 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400854 &BTRFS_I(inode)->io_tree,
855 start, end, NULL,
856 EXTENT_CLEAR_UNLOCK_PAGE |
857 EXTENT_CLEAR_UNLOCK |
858 EXTENT_CLEAR_DELALLOC |
859 EXTENT_CLEAR_DIRTY |
860 EXTENT_SET_WRITEBACK |
861 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000862
Chris Mason771ed682008-11-06 22:02:51 -0500863 *nr_written = *nr_written +
864 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
865 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500866 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100867 } else if (ret < 0) {
868 btrfs_abort_transaction(trans, root, ret);
869 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500870 }
871 }
Chris Masonc8b97812008-10-29 14:49:59 -0400872
873 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200874 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400875
Josef Bacik4b46fce2010-05-23 11:00:55 -0400876 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400877 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400878
Chris Masond3977122009-01-05 21:25:51 -0500879 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400880 unsigned long op;
881
Josef Bacik287a0ab2010-03-19 18:07:23 +0000882 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400883 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500884 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500885 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 if (ret < 0) {
887 btrfs_abort_transaction(trans, root, ret);
888 goto out_unlock;
889 }
Chris Masond3977122009-01-05 21:25:51 -0500890
David Sterba172ddd62011-04-21 00:48:27 +0200891 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400893 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500894 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500895 ram_size = ins.offset;
896 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400897 em->mod_start = em->start;
898 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400899
Chris Masone6dcd2d2008-07-17 12:53:50 -0400900 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400901 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500902 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400904 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400905 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Chris Masond3977122009-01-05 21:25:51 -0500907 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400908 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400909 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400910 if (!ret)
911 list_move(&em->list,
912 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400913 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400914 if (ret != -EEXIST) {
915 free_extent_map(em);
916 break;
917 }
918 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400919 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 }
921
Chris Mason98d20f62008-04-14 09:46:10 -0400922 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400923 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500924 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400926
Yan Zheng17d217f2008-12-12 10:03:38 -0500927 if (root->root_key.objectid ==
928 BTRFS_DATA_RELOC_TREE_OBJECTID) {
929 ret = btrfs_reloc_clone_csums(inode, start,
930 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100931 if (ret) {
932 btrfs_abort_transaction(trans, root, ret);
933 goto out_unlock;
934 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500935 }
936
Chris Masond3977122009-01-05 21:25:51 -0500937 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400938 break;
Chris Masond3977122009-01-05 21:25:51 -0500939
Chris Masonc8b97812008-10-29 14:49:59 -0400940 /* we're not doing compressed IO, don't unlock the first
941 * page (which the caller expects to stay locked), don't
942 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400943 *
944 * Do set the Private2 bit so we know this page was properly
945 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400946 */
Chris Masona791e352009-10-08 11:27:10 -0400947 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
948 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
949 EXTENT_SET_PRIVATE2;
950
Chris Masonc8b97812008-10-29 14:49:59 -0400951 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
952 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400953 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400954 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500955 num_bytes -= cur_alloc_size;
956 alloc_hint = ins.objectid + ins.offset;
957 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400958 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500960 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000961
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100962out_unlock:
963 extent_clear_unlock_delalloc(inode,
964 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400965 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 EXTENT_CLEAR_UNLOCK_PAGE |
967 EXTENT_CLEAR_UNLOCK |
968 EXTENT_CLEAR_DELALLOC |
969 EXTENT_CLEAR_DIRTY |
970 EXTENT_SET_WRITEBACK |
971 EXTENT_END_WRITEBACK);
972
973 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500974}
Chris Masonc8b97812008-10-29 14:49:59 -0400975
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000976static noinline int cow_file_range(struct inode *inode,
977 struct page *locked_page,
978 u64 start, u64 end, int *page_started,
979 unsigned long *nr_written,
980 int unlock)
981{
982 struct btrfs_trans_handle *trans;
983 struct btrfs_root *root = BTRFS_I(inode)->root;
984 int ret;
985
986 trans = btrfs_join_transaction(root);
987 if (IS_ERR(trans)) {
988 extent_clear_unlock_delalloc(inode,
989 &BTRFS_I(inode)->io_tree,
990 start, end, locked_page,
991 EXTENT_CLEAR_UNLOCK_PAGE |
992 EXTENT_CLEAR_UNLOCK |
993 EXTENT_CLEAR_DELALLOC |
994 EXTENT_CLEAR_DIRTY |
995 EXTENT_SET_WRITEBACK |
996 EXTENT_END_WRITEBACK);
997 return PTR_ERR(trans);
998 }
999 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1000
1001 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1002 page_started, nr_written, unlock);
1003
1004 btrfs_end_transaction(trans, root);
1005
1006 return ret;
1007}
1008
Chris Mason771ed682008-11-06 22:02:51 -05001009/*
1010 * work queue call back to started compression on a file and pages
1011 */
1012static noinline void async_cow_start(struct btrfs_work *work)
1013{
1014 struct async_cow *async_cow;
1015 int num_added = 0;
1016 async_cow = container_of(work, struct async_cow, work);
1017
1018 compress_file_range(async_cow->inode, async_cow->locked_page,
1019 async_cow->start, async_cow->end, async_cow,
1020 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001021 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001022 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001023 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001024 }
Chris Mason771ed682008-11-06 22:02:51 -05001025}
1026
1027/*
1028 * work queue call back to submit previously compressed pages
1029 */
1030static noinline void async_cow_submit(struct btrfs_work *work)
1031{
1032 struct async_cow *async_cow;
1033 struct btrfs_root *root;
1034 unsigned long nr_pages;
1035
1036 async_cow = container_of(work, struct async_cow, work);
1037
1038 root = async_cow->root;
1039 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1040 PAGE_CACHE_SHIFT;
1041
Josef Bacik66657b32012-08-01 15:36:24 -04001042 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001043 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001044 waitqueue_active(&root->fs_info->async_submit_wait))
1045 wake_up(&root->fs_info->async_submit_wait);
1046
Chris Masond3977122009-01-05 21:25:51 -05001047 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001048 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001049}
Chris Masonc8b97812008-10-29 14:49:59 -04001050
Chris Mason771ed682008-11-06 22:02:51 -05001051static noinline void async_cow_free(struct btrfs_work *work)
1052{
1053 struct async_cow *async_cow;
1054 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001055 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001056 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001057 kfree(async_cow);
1058}
1059
1060static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1061 u64 start, u64 end, int *page_started,
1062 unsigned long *nr_written)
1063{
1064 struct async_cow *async_cow;
1065 struct btrfs_root *root = BTRFS_I(inode)->root;
1066 unsigned long nr_pages;
1067 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001068 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001069
Chris Masona3429ab2009-10-08 12:30:20 -04001070 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1071 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001072 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001074 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001075 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001076 async_cow->root = root;
1077 async_cow->locked_page = locked_page;
1078 async_cow->start = start;
1079
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001080 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001081 cur_end = end;
1082 else
1083 cur_end = min(end, start + 512 * 1024 - 1);
1084
1085 async_cow->end = cur_end;
1086 INIT_LIST_HEAD(&async_cow->extents);
1087
1088 async_cow->work.func = async_cow_start;
1089 async_cow->work.ordered_func = async_cow_submit;
1090 async_cow->work.ordered_free = async_cow_free;
1091 async_cow->work.flags = 0;
1092
Chris Mason771ed682008-11-06 22:02:51 -05001093 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1094 PAGE_CACHE_SHIFT;
1095 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1096
1097 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1098 &async_cow->work);
1099
1100 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1101 wait_event(root->fs_info->async_submit_wait,
1102 (atomic_read(&root->fs_info->async_delalloc_pages) <
1103 limit));
1104 }
1105
Chris Masond3977122009-01-05 21:25:51 -05001106 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001107 atomic_read(&root->fs_info->async_delalloc_pages)) {
1108 wait_event(root->fs_info->async_submit_wait,
1109 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1110 0));
1111 }
1112
1113 *nr_written += nr_pages;
1114 start = cur_end + 1;
1115 }
1116 *page_started = 1;
1117 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001118}
1119
Chris Masond3977122009-01-05 21:25:51 -05001120static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001121 u64 bytenr, u64 num_bytes)
1122{
1123 int ret;
1124 struct btrfs_ordered_sum *sums;
1125 LIST_HEAD(list);
1126
Yan Zheng07d400a2009-01-06 11:42:00 -05001127 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001128 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001129 if (ret == 0 && list_empty(&list))
1130 return 0;
1131
1132 while (!list_empty(&list)) {
1133 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1134 list_del(&sums->list);
1135 kfree(sums);
1136 }
1137 return 1;
1138}
1139
Chris Masond352ac62008-09-29 15:18:18 -04001140/*
1141 * when nowcow writeback call back. This checks for snapshots or COW copies
1142 * of the extents that exist in the file, and COWs the file as required.
1143 *
1144 * If no cow copies or snapshots exist, we write directly to the existing
1145 * blocks on disk
1146 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001147static noinline int run_delalloc_nocow(struct inode *inode,
1148 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001149 u64 start, u64 end, int *page_started, int force,
1150 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001151{
Chris Masonbe20aa92007-12-17 20:14:01 -05001152 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001153 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001154 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001155 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001156 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001157 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 u64 cow_start;
1159 u64 cur_offset;
1160 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001161 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 u64 disk_bytenr;
1163 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001164 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001166 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001167 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001168 int nocow;
1169 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001170 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001171 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001172
1173 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001174 if (!path) {
1175 extent_clear_unlock_delalloc(inode,
1176 &BTRFS_I(inode)->io_tree,
1177 start, end, locked_page,
1178 EXTENT_CLEAR_UNLOCK_PAGE |
1179 EXTENT_CLEAR_UNLOCK |
1180 EXTENT_CLEAR_DELALLOC |
1181 EXTENT_CLEAR_DIRTY |
1182 EXTENT_SET_WRITEBACK |
1183 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001184 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001185 }
Li Zefan82d59022011-04-20 10:33:24 +08001186
Liu Bo83eea1f2012-07-10 05:28:39 -06001187 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001188
1189 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001190 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001191 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001192 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001195 extent_clear_unlock_delalloc(inode,
1196 &BTRFS_I(inode)->io_tree,
1197 start, end, locked_page,
1198 EXTENT_CLEAR_UNLOCK_PAGE |
1199 EXTENT_CLEAR_UNLOCK |
1200 EXTENT_CLEAR_DELALLOC |
1201 EXTENT_CLEAR_DIRTY |
1202 EXTENT_SET_WRITEBACK |
1203 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001204 btrfs_free_path(path);
1205 return PTR_ERR(trans);
1206 }
1207
Josef Bacik74b21072011-04-13 12:02:53 -04001208 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001209
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 cow_start = (u64)-1;
1211 cur_offset = start;
1212 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001213 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001215 if (ret < 0) {
1216 btrfs_abort_transaction(trans, root, ret);
1217 goto error;
1218 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1220 leaf = path->nodes[0];
1221 btrfs_item_key_to_cpu(leaf, &found_key,
1222 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001223 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 found_key.type == BTRFS_EXTENT_DATA_KEY)
1225 path->slots[0]--;
1226 }
1227 check_prev = 0;
1228next_slot:
1229 leaf = path->nodes[0];
1230 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1231 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001232 if (ret < 0) {
1233 btrfs_abort_transaction(trans, root, ret);
1234 goto error;
1235 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 if (ret > 0)
1237 break;
1238 leaf = path->nodes[0];
1239 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001240
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 nocow = 0;
1242 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001243 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001245
Li Zefan33345d012011-04-20 10:31:50 +08001246 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1248 found_key.offset > end)
1249 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001250
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 if (found_key.offset > cur_offset) {
1252 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001253 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 goto out_check;
1255 }
Chris Masonc31f8832008-01-08 15:46:31 -05001256
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 fi = btrfs_item_ptr(leaf, path->slots[0],
1258 struct btrfs_file_extent_item);
1259 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
Yan Zhengd899e052008-10-30 14:25:28 -04001261 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1262 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 extent_end = found_key.offset +
1266 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001267 disk_num_bytes =
1268 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (extent_end <= start) {
1270 path->slots[0]++;
1271 goto next_slot;
1272 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001273 if (disk_bytenr == 0)
1274 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 if (btrfs_file_extent_compression(leaf, fi) ||
1276 btrfs_file_extent_encryption(leaf, fi) ||
1277 btrfs_file_extent_other_encoding(leaf, fi))
1278 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001279 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1280 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001281 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001283 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 found_key.offset -
1285 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001286 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001287 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001288 disk_bytenr += cur_offset - found_key.offset;
1289 num_bytes = min(end + 1, extent_end) - cur_offset;
1290 /*
1291 * force cow if csum exists in the range.
1292 * this ensure that csum for a given extent are
1293 * either valid or do not exist.
1294 */
1295 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1296 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 nocow = 1;
1298 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1299 extent_end = found_key.offset +
1300 btrfs_file_extent_inline_len(leaf, fi);
1301 extent_end = ALIGN(extent_end, root->sectorsize);
1302 } else {
1303 BUG_ON(1);
1304 }
1305out_check:
1306 if (extent_end <= start) {
1307 path->slots[0]++;
1308 goto next_slot;
1309 }
1310 if (!nocow) {
1311 if (cow_start == (u64)-1)
1312 cow_start = cur_offset;
1313 cur_offset = extent_end;
1314 if (cur_offset > end)
1315 break;
1316 path->slots[0]++;
1317 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001318 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001319
David Sterbab3b4aa72011-04-21 01:20:15 +02001320 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001322 ret = __cow_file_range(trans, inode, root, locked_page,
1323 cow_start, found_key.offset - 1,
1324 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001325 if (ret) {
1326 btrfs_abort_transaction(trans, root, ret);
1327 goto error;
1328 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001330 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001331
Yan Zhengd899e052008-10-30 14:25:28 -04001332 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1333 struct extent_map *em;
1334 struct extent_map_tree *em_tree;
1335 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001336 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001337 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001338 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001339 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001340 em->len = num_bytes;
1341 em->block_len = num_bytes;
1342 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001343 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001344 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001345 em->mod_start = em->start;
1346 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001347 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001348 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001349 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001350 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001351 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001352 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001353 if (!ret)
1354 list_move(&em->list,
1355 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001356 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (ret != -EEXIST) {
1358 free_extent_map(em);
1359 break;
1360 }
1361 btrfs_drop_extent_cache(inode, em->start,
1362 em->start + em->len - 1, 0);
1363 }
1364 type = BTRFS_ORDERED_PREALLOC;
1365 } else {
1366 type = BTRFS_ORDERED_NOCOW;
1367 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001368
1369 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001370 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001371 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001372
Yan, Zhengefa56462010-05-16 10:49:59 -04001373 if (root->root_key.objectid ==
1374 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1375 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1376 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001377 if (ret) {
1378 btrfs_abort_transaction(trans, root, ret);
1379 goto error;
1380 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001381 }
1382
Yan Zhengd899e052008-10-30 14:25:28 -04001383 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001384 cur_offset, cur_offset + num_bytes - 1,
1385 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1386 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1387 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 cur_offset = extent_end;
1389 if (cur_offset > end)
1390 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001391 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001392 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001393
Josef Bacik17ca04a2012-05-31 15:58:55 -04001394 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001396 cur_offset = end;
1397 }
1398
Yan Zheng80ff3852008-10-30 14:20:02 -04001399 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001400 ret = __cow_file_range(trans, inode, root, locked_page,
1401 cow_start, end,
1402 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 if (ret) {
1404 btrfs_abort_transaction(trans, root, ret);
1405 goto error;
1406 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 }
1408
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409error:
Miao Xiea698d0752012-09-20 01:51:59 -06001410 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001411 if (!ret)
1412 ret = err;
1413
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 if (ret && cur_offset < end)
1415 extent_clear_unlock_delalloc(inode,
1416 &BTRFS_I(inode)->io_tree,
1417 cur_offset, end, locked_page,
1418 EXTENT_CLEAR_UNLOCK_PAGE |
1419 EXTENT_CLEAR_UNLOCK |
1420 EXTENT_CLEAR_DELALLOC |
1421 EXTENT_CLEAR_DIRTY |
1422 EXTENT_SET_WRITEBACK |
1423 EXTENT_END_WRITEBACK);
1424
Yan Zheng7ea394f2008-08-05 13:05:02 -04001425 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001426 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001427}
1428
Chris Masond352ac62008-09-29 15:18:18 -04001429/*
1430 * extent_io.c call back to do delayed allocation processing
1431 */
Chris Masonc8b97812008-10-29 14:49:59 -04001432static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001433 u64 start, u64 end, int *page_started,
1434 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001435{
Chris Masonbe20aa92007-12-17 20:14:01 -05001436 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001437 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001438
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001439 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001440 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001441 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001442 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001443 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001444 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001445 } else if (!btrfs_test_opt(root, COMPRESS) &&
1446 !(BTRFS_I(inode)->force_compress) &&
1447 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001448 ret = cow_file_range(inode, locked_page, start, end,
1449 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001450 } else {
1451 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1452 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001453 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001454 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001455 }
Chris Masonb888db22007-08-27 16:49:44 -04001456 return ret;
1457}
1458
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001459static void btrfs_split_extent_hook(struct inode *inode,
1460 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001461{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001462 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001463 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001464 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001465
Josef Bacik9e0baf62011-07-15 15:16:44 +00001466 spin_lock(&BTRFS_I(inode)->lock);
1467 BTRFS_I(inode)->outstanding_extents++;
1468 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001469}
1470
1471/*
1472 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1473 * extents so we can keep track of new extents that are just merged onto old
1474 * extents, such as when we are doing sequential writes, so we can properly
1475 * account for the metadata space we'll need.
1476 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477static void btrfs_merge_extent_hook(struct inode *inode,
1478 struct extent_state *new,
1479 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481 /* not delalloc, ignore it */
1482 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001483 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001484
Josef Bacik9e0baf62011-07-15 15:16:44 +00001485 spin_lock(&BTRFS_I(inode)->lock);
1486 BTRFS_I(inode)->outstanding_extents--;
1487 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001488}
1489
Chris Masond352ac62008-09-29 15:18:18 -04001490/*
1491 * extent_io.c set_bit_hook, used to track delayed allocation
1492 * bytes in this file, and to maintain the list of inodes that
1493 * have pending delalloc work to be done.
1494 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001495static void btrfs_set_bit_hook(struct inode *inode,
1496 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001497{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498
Chris Mason75eff682008-12-15 15:54:40 -05001499 /*
1500 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001501 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001502 * bit, which is only set or cleared with irqs on
1503 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001504 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001505 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001506 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001507 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001508
Josef Bacik9e0baf62011-07-15 15:16:44 +00001509 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001510 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001511 } else {
1512 spin_lock(&BTRFS_I(inode)->lock);
1513 BTRFS_I(inode)->outstanding_extents++;
1514 spin_unlock(&BTRFS_I(inode)->lock);
1515 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001516
Miao Xie963d6782013-01-29 10:10:51 +00001517 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1518 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001519 spin_lock(&BTRFS_I(inode)->lock);
1520 BTRFS_I(inode)->delalloc_bytes += len;
1521 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1522 &BTRFS_I(inode)->runtime_flags)) {
1523 spin_lock(&root->fs_info->delalloc_lock);
1524 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1525 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1526 &root->fs_info->delalloc_inodes);
1527 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1528 &BTRFS_I(inode)->runtime_flags);
1529 }
1530 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001531 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001532 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001533 }
Chris Mason291d6732008-01-29 15:55:23 -05001534}
1535
Chris Masond352ac62008-09-29 15:18:18 -04001536/*
1537 * extent_io.c clear_bit_hook, see set_bit_hook for why
1538 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001539static void btrfs_clear_bit_hook(struct inode *inode,
1540 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001541{
Chris Mason75eff682008-12-15 15:54:40 -05001542 /*
1543 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001544 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001545 * bit, which is only set or cleared with irqs on
1546 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001547 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001548 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001549 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001550 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001551
Josef Bacik9e0baf62011-07-15 15:16:44 +00001552 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001554 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1555 spin_lock(&BTRFS_I(inode)->lock);
1556 BTRFS_I(inode)->outstanding_extents--;
1557 spin_unlock(&BTRFS_I(inode)->lock);
1558 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001559
1560 if (*bits & EXTENT_DO_ACCOUNTING)
1561 btrfs_delalloc_release_metadata(inode, len);
1562
Josef Bacik0cb59c92010-07-02 12:14:14 -04001563 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1564 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001566
Miao Xie963d6782013-01-29 10:10:51 +00001567 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1568 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001569 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001570 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001571 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a52013-01-29 10:11:59 +00001572 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1573 &BTRFS_I(inode)->runtime_flags)) {
1574 spin_lock(&root->fs_info->delalloc_lock);
1575 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1576 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1577 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1578 &BTRFS_I(inode)->runtime_flags);
1579 }
1580 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001581 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001582 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001583 }
Chris Mason291d6732008-01-29 15:55:23 -05001584}
1585
Chris Masond352ac62008-09-29 15:18:18 -04001586/*
1587 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1588 * we don't create bios that span stripes or chunks
1589 */
Chris Mason239b14b2008-03-24 15:02:07 -04001590int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001591 size_t size, struct bio *bio,
1592 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001593{
1594 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001595 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001596 u64 length = 0;
1597 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001598 int ret;
1599
Chris Mason771ed682008-11-06 22:02:51 -05001600 if (bio_flags & EXTENT_BIO_COMPRESSED)
1601 return 0;
1602
Chris Masonf2d8d742008-04-21 10:03:05 -04001603 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001604 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001605 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001606 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001607 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001608 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001609 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001610 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001611 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001612}
1613
Chris Masond352ac62008-09-29 15:18:18 -04001614/*
1615 * in order to insert checksums into the metadata in large chunks,
1616 * we wait until bio submission time. All the pages in the bio are
1617 * checksummed and sums are attached onto the ordered extent record.
1618 *
1619 * At IO completion time the cums attached on the ordered extent record
1620 * are inserted into the btree
1621 */
Chris Masond3977122009-01-05 21:25:51 -05001622static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1623 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001624 unsigned long bio_flags,
1625 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001626{
Chris Mason065631f2008-02-20 12:07:25 -05001627 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001628 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001629
Chris Masond20f7042008-12-08 16:58:54 -05001630 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001631 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001632 return 0;
1633}
Chris Masone0156402008-04-16 11:15:20 -04001634
Chris Mason4a69a412008-11-06 22:03:00 -05001635/*
1636 * in order to insert checksums into the metadata in large chunks,
1637 * we wait until bio submission time. All the pages in the bio are
1638 * checksummed and sums are attached onto the ordered extent record.
1639 *
1640 * At IO completion time the cums attached on the ordered extent record
1641 * are inserted into the btree
1642 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001643static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001644 int mirror_num, unsigned long bio_flags,
1645 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001646{
1647 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001648 int ret;
1649
1650 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1651 if (ret)
1652 bio_endio(bio, ret);
1653 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001654}
1655
Chris Masond352ac62008-09-29 15:18:18 -04001656/*
Chris Masoncad321a2008-12-17 14:51:42 -05001657 * extent_io.c submission hook. This does the right thing for csum calculation
1658 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001659 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001660static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001661 int mirror_num, unsigned long bio_flags,
1662 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001663{
1664 struct btrfs_root *root = BTRFS_I(inode)->root;
1665 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001666 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001667 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001668 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001669
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001670 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001671
Liu Bo83eea1f2012-07-10 05:28:39 -06001672 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001673 metadata = 2;
1674
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001675 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001676 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1677 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001678 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001679
Chris Masond20f7042008-12-08 16:58:54 -05001680 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001681 ret = btrfs_submit_compressed_read(inode, bio,
1682 mirror_num,
1683 bio_flags);
1684 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001685 } else if (!skip_sum) {
1686 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1687 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001688 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001689 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001690 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001691 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001692 /* csum items have already been cloned */
1693 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1694 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001695 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001696 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001697 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001698 bio_flags, bio_offset,
1699 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001700 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001701 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001702 } else if (!skip_sum) {
1703 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1704 if (ret)
1705 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001706 }
1707
Chris Mason0b86a832008-03-24 15:01:56 -04001708mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001709 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1710
1711out:
1712 if (ret < 0)
1713 bio_endio(bio, ret);
1714 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001715}
Chris Mason6885f302008-02-20 16:11:05 -05001716
Chris Masond352ac62008-09-29 15:18:18 -04001717/*
1718 * given a list of ordered sums record them in the inode. This happens
1719 * at IO completion time based on sums calculated at bio submission time.
1720 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001721static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001722 struct inode *inode, u64 file_offset,
1723 struct list_head *list)
1724{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725 struct btrfs_ordered_sum *sum;
1726
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001727 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001728 btrfs_csum_file_blocks(trans,
1729 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001730 }
1731 return 0;
1732}
1733
Josef Bacik2ac55d42010-02-03 19:33:23 +00001734int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1735 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001736{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001737 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001738 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001740}
1741
Chris Masond352ac62008-09-29 15:18:18 -04001742/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001743struct btrfs_writepage_fixup {
1744 struct page *page;
1745 struct btrfs_work work;
1746};
1747
Christoph Hellwigb2950862008-12-02 09:54:17 -05001748static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001749{
1750 struct btrfs_writepage_fixup *fixup;
1751 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001753 struct page *page;
1754 struct inode *inode;
1755 u64 page_start;
1756 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001757 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001758
1759 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1760 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001761again:
Chris Mason247e7432008-07-17 12:53:51 -04001762 lock_page(page);
1763 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1764 ClearPageChecked(page);
1765 goto out_page;
1766 }
1767
1768 inode = page->mapping->host;
1769 page_start = page_offset(page);
1770 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1771
Josef Bacik2ac55d42010-02-03 19:33:23 +00001772 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001773 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001774
1775 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001776 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001777 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001778
1779 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1780 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001781 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1782 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001783 unlock_page(page);
1784 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001785 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001786 goto again;
1787 }
Chris Mason247e7432008-07-17 12:53:51 -04001788
Jeff Mahoney87826df2012-02-15 16:23:57 +01001789 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1790 if (ret) {
1791 mapping_set_error(page->mapping, ret);
1792 end_extent_writepage(page, ret, page_start, page_end);
1793 ClearPageChecked(page);
1794 goto out;
1795 }
1796
Josef Bacik2ac55d42010-02-03 19:33:23 +00001797 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001798 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001799 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001800out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001801 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1802 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001803out_page:
1804 unlock_page(page);
1805 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001806 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001807}
1808
1809/*
1810 * There are a few paths in the higher layers of the kernel that directly
1811 * set the page dirty bit without asking the filesystem if it is a
1812 * good idea. This causes problems because we want to make sure COW
1813 * properly happens and the data=ordered rules are followed.
1814 *
Chris Masonc8b97812008-10-29 14:49:59 -04001815 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001816 * hasn't been properly setup for IO. We kick off an async process
1817 * to fix it up. The async helper will wait for ordered extents, set
1818 * the delalloc bit and make it safe to write the page.
1819 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001820static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001821{
1822 struct inode *inode = page->mapping->host;
1823 struct btrfs_writepage_fixup *fixup;
1824 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001825
Chris Mason8b62b722009-09-02 16:53:46 -04001826 /* this page is properly in the ordered list */
1827 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001828 return 0;
1829
1830 if (PageChecked(page))
1831 return -EAGAIN;
1832
1833 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1834 if (!fixup)
1835 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001836
Chris Mason247e7432008-07-17 12:53:51 -04001837 SetPageChecked(page);
1838 page_cache_get(page);
1839 fixup->work.func = btrfs_writepage_fixup_worker;
1840 fixup->page = page;
1841 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001842 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001843}
1844
Yan Zhengd899e052008-10-30 14:25:28 -04001845static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1846 struct inode *inode, u64 file_pos,
1847 u64 disk_bytenr, u64 disk_num_bytes,
1848 u64 num_bytes, u64 ram_bytes,
1849 u8 compression, u8 encryption,
1850 u16 other_encoding, int extent_type)
1851{
1852 struct btrfs_root *root = BTRFS_I(inode)->root;
1853 struct btrfs_file_extent_item *fi;
1854 struct btrfs_path *path;
1855 struct extent_buffer *leaf;
1856 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001857 int ret;
1858
1859 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001860 if (!path)
1861 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001862
Chris Masonb9473432009-03-13 11:00:37 -04001863 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001864
1865 /*
1866 * we may be replacing one extent in the tree with another.
1867 * The new extent is pinned in the extent map, and we don't want
1868 * to drop it from the cache until it is completely in the btree.
1869 *
1870 * So, tell btrfs_drop_extents to leave this extent in the cache.
1871 * the caller is expected to unpin it and allow it to be merged
1872 * with the others.
1873 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001874 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001875 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001876 if (ret)
1877 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001878
Li Zefan33345d012011-04-20 10:31:50 +08001879 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001880 ins.offset = file_pos;
1881 ins.type = BTRFS_EXTENT_DATA_KEY;
1882 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001883 if (ret)
1884 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001885 leaf = path->nodes[0];
1886 fi = btrfs_item_ptr(leaf, path->slots[0],
1887 struct btrfs_file_extent_item);
1888 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1889 btrfs_set_file_extent_type(leaf, fi, extent_type);
1890 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1891 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1892 btrfs_set_file_extent_offset(leaf, fi, 0);
1893 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1894 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1895 btrfs_set_file_extent_compression(leaf, fi, compression);
1896 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1897 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001898
Yan Zhengd899e052008-10-30 14:25:28 -04001899 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001900 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001901
1902 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001903
1904 ins.objectid = disk_bytenr;
1905 ins.offset = disk_num_bytes;
1906 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001907 ret = btrfs_alloc_reserved_file_extent(trans, root,
1908 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001909 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001910out:
Yan Zhengd899e052008-10-30 14:25:28 -04001911 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001912
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001913 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001914}
1915
Chris Mason5d13a982009-03-13 11:41:46 -04001916/*
1917 * helper function for btrfs_finish_ordered_io, this
1918 * just reads in some of the csum leaves to prime them into ram
1919 * before we start the transaction. It limits the amount of btree
1920 * reads required while inside the transaction.
1921 */
Chris Masond352ac62008-09-29 15:18:18 -04001922/* as ordered data IO finishes, this gets called so we can finish
1923 * an ordered extent if the range of bytes in the file it covers are
1924 * fully written.
1925 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001926static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001927{
Josef Bacik5fd02042012-05-02 14:00:54 -04001928 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001929 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001930 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001931 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001932 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001933 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001934 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001935 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001936
Liu Bo83eea1f2012-07-10 05:28:39 -06001937 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001938
Josef Bacik5fd02042012-05-02 14:00:54 -04001939 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1940 ret = -EIO;
1941 goto out;
1942 }
1943
Yan, Zhengc21677542009-11-12 09:34:21 +00001944 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001945 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001946 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1947 if (nolock)
1948 trans = btrfs_join_transaction_nolock(root);
1949 else
1950 trans = btrfs_join_transaction(root);
1951 if (IS_ERR(trans)) {
1952 ret = PTR_ERR(trans);
1953 trans = NULL;
1954 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00001955 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001956 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1957 ret = btrfs_update_inode_fallback(trans, root, inode);
1958 if (ret) /* -ENOMEM or corruption */
1959 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001960 goto out;
1961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001962
Josef Bacik2ac55d42010-02-03 19:33:23 +00001963 lock_extent_bits(io_tree, ordered_extent->file_offset,
1964 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001965 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001966
Josef Bacik0cb59c92010-07-02 12:14:14 -04001967 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001968 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001969 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001970 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001971 if (IS_ERR(trans)) {
1972 ret = PTR_ERR(trans);
1973 trans = NULL;
1974 goto out_unlock;
1975 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001976 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001977
Chris Masonc8b97812008-10-29 14:49:59 -04001978 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001979 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001980 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001981 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001982 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001983 ordered_extent->file_offset,
1984 ordered_extent->file_offset +
1985 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001986 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001987 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001988 ret = insert_reserved_file_extent(trans, inode,
1989 ordered_extent->file_offset,
1990 ordered_extent->start,
1991 ordered_extent->disk_len,
1992 ordered_extent->len,
1993 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001994 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001995 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001996 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001997 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1998 ordered_extent->file_offset, ordered_extent->len,
1999 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002000 if (ret < 0) {
2001 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002002 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002003 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002004
Chris Masone6dcd2d2008-07-17 12:53:50 -04002005 add_pending_csums(trans, inode, ordered_extent->file_offset,
2006 &ordered_extent->list);
2007
Josef Bacik6c760c02012-11-09 10:53:21 -05002008 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2009 ret = btrfs_update_inode_fallback(trans, root, inode);
2010 if (ret) { /* -ENOMEM or corruption */
2011 btrfs_abort_transaction(trans, root, ret);
2012 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002013 }
2014 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002015out_unlock:
2016 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2017 ordered_extent->file_offset +
2018 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002019out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002020 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002021 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002022 if (trans)
2023 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002024
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002025 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002026 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2027 ordered_extent->file_offset +
2028 ordered_extent->len - 1, NULL, GFP_NOFS);
2029
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002030 /*
2031 * If the ordered extent had an IOERR or something else went
2032 * wrong we need to return the space for this ordered extent
2033 * back to the allocator.
2034 */
2035 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2036 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2037 btrfs_free_reserved_extent(root, ordered_extent->start,
2038 ordered_extent->disk_len);
2039 }
2040
2041
Josef Bacik5fd02042012-05-02 14:00:54 -04002042 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002043 * This needs to be done to make sure anybody waiting knows we are done
2044 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002045 */
2046 btrfs_remove_ordered_extent(inode, ordered_extent);
2047
Chris Masone6dcd2d2008-07-17 12:53:50 -04002048 /* once for us */
2049 btrfs_put_ordered_extent(ordered_extent);
2050 /* once for the tree */
2051 btrfs_put_ordered_extent(ordered_extent);
2052
Josef Bacik5fd02042012-05-02 14:00:54 -04002053 return ret;
2054}
2055
2056static void finish_ordered_fn(struct btrfs_work *work)
2057{
2058 struct btrfs_ordered_extent *ordered_extent;
2059 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2060 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002061}
2062
Christoph Hellwigb2950862008-12-02 09:54:17 -05002063static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002064 struct extent_state *state, int uptodate)
2065{
Josef Bacik5fd02042012-05-02 14:00:54 -04002066 struct inode *inode = page->mapping->host;
2067 struct btrfs_root *root = BTRFS_I(inode)->root;
2068 struct btrfs_ordered_extent *ordered_extent = NULL;
2069 struct btrfs_workers *workers;
2070
liubo1abe9b82011-03-24 11:18:59 +00002071 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2072
Chris Mason8b62b722009-09-02 16:53:46 -04002073 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002074 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2075 end - start + 1, uptodate))
2076 return 0;
2077
2078 ordered_extent->work.func = finish_ordered_fn;
2079 ordered_extent->work.flags = 0;
2080
Liu Bo83eea1f2012-07-10 05:28:39 -06002081 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002082 workers = &root->fs_info->endio_freespace_worker;
2083 else
2084 workers = &root->fs_info->endio_write_workers;
2085 btrfs_queue_worker(workers, &ordered_extent->work);
2086
2087 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002088}
2089
Chris Masond352ac62008-09-29 15:18:18 -04002090/*
Chris Masond352ac62008-09-29 15:18:18 -04002091 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002092 * if there's a match, we allow the bio to finish. If not, the code in
2093 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002094 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002095static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002096 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002097{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002098 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002099 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002100 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002101 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002102 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002103 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002106
Chris Masond20f7042008-12-08 16:58:54 -05002107 if (PageChecked(page)) {
2108 ClearPageChecked(page);
2109 goto good;
2110 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002111
2112 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002113 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002114
Yan Zheng17d217f2008-12-12 10:03:38 -05002115 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002116 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002117 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2118 GFP_NOFS);
2119 return 0;
2120 }
2121
Yanc2e639f2008-02-04 08:57:25 -05002122 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002123 private = state->private;
2124 ret = 0;
2125 } else {
2126 ret = get_state_private(io_tree, start, &private);
2127 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002128 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002129 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002130 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002131
Chris Masonff79f812007-10-15 16:22:25 -04002132 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2133 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002134 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002135 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002136
Cong Wang7ac687d2011-11-25 23:14:28 +08002137 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002138good:
Chris Mason07157aa2007-08-30 08:50:51 -04002139 return 0;
2140
2141zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002142 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002143 "private %llu\n",
2144 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002145 (unsigned long long)start, csum,
2146 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002147 memset(kaddr + offset, 1, end - start + 1);
2148 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002149 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002150 if (private == 0)
2151 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002152 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002153}
Chris Masonb888db22007-08-27 16:49:44 -04002154
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002155struct delayed_iput {
2156 struct list_head list;
2157 struct inode *inode;
2158};
2159
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002160/* JDM: If this is fs-wide, why can't we add a pointer to
2161 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002162void btrfs_add_delayed_iput(struct inode *inode)
2163{
2164 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2165 struct delayed_iput *delayed;
2166
2167 if (atomic_add_unless(&inode->i_count, -1, 1))
2168 return;
2169
2170 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2171 delayed->inode = inode;
2172
2173 spin_lock(&fs_info->delayed_iput_lock);
2174 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2175 spin_unlock(&fs_info->delayed_iput_lock);
2176}
2177
2178void btrfs_run_delayed_iputs(struct btrfs_root *root)
2179{
2180 LIST_HEAD(list);
2181 struct btrfs_fs_info *fs_info = root->fs_info;
2182 struct delayed_iput *delayed;
2183 int empty;
2184
2185 spin_lock(&fs_info->delayed_iput_lock);
2186 empty = list_empty(&fs_info->delayed_iputs);
2187 spin_unlock(&fs_info->delayed_iput_lock);
2188 if (empty)
2189 return;
2190
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002191 spin_lock(&fs_info->delayed_iput_lock);
2192 list_splice_init(&fs_info->delayed_iputs, &list);
2193 spin_unlock(&fs_info->delayed_iput_lock);
2194
2195 while (!list_empty(&list)) {
2196 delayed = list_entry(list.next, struct delayed_iput, list);
2197 list_del(&delayed->list);
2198 iput(delayed->inode);
2199 kfree(delayed);
2200 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002201}
2202
Yan, Zhengd68fc572010-05-16 10:49:58 -04002203enum btrfs_orphan_cleanup_state {
2204 ORPHAN_CLEANUP_STARTED = 1,
2205 ORPHAN_CLEANUP_DONE = 2,
2206};
2207
2208/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002209 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002210 * files in the subvolume, it removes orphan item and frees block_rsv
2211 * structure.
2212 */
2213void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2214 struct btrfs_root *root)
2215{
Josef Bacik90290e12011-12-02 15:44:12 -05002216 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002217 int ret;
2218
Josef Bacik8a35d952012-05-23 14:26:42 -04002219 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002220 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2221 return;
2222
Josef Bacik90290e12011-12-02 15:44:12 -05002223 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002224 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002225 spin_unlock(&root->orphan_lock);
2226 return;
2227 }
2228
2229 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2230 spin_unlock(&root->orphan_lock);
2231 return;
2232 }
2233
2234 block_rsv = root->orphan_block_rsv;
2235 root->orphan_block_rsv = NULL;
2236 spin_unlock(&root->orphan_lock);
2237
Yan, Zhengd68fc572010-05-16 10:49:58 -04002238 if (root->orphan_item_inserted &&
2239 btrfs_root_refs(&root->root_item) > 0) {
2240 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2241 root->root_key.objectid);
2242 BUG_ON(ret);
2243 root->orphan_item_inserted = 0;
2244 }
2245
Josef Bacik90290e12011-12-02 15:44:12 -05002246 if (block_rsv) {
2247 WARN_ON(block_rsv->size > 0);
2248 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002249 }
2250}
2251
2252/*
Josef Bacik7b128762008-07-24 12:17:14 -04002253 * This creates an orphan entry for the given inode in case something goes
2254 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002255 *
2256 * NOTE: caller of this function should reserve 5 units of metadata for
2257 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002258 */
2259int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2260{
2261 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002262 struct btrfs_block_rsv *block_rsv = NULL;
2263 int reserve = 0;
2264 int insert = 0;
2265 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002266
Yan, Zhengd68fc572010-05-16 10:49:58 -04002267 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002268 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002269 if (!block_rsv)
2270 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002271 }
2272
Yan, Zhengd68fc572010-05-16 10:49:58 -04002273 spin_lock(&root->orphan_lock);
2274 if (!root->orphan_block_rsv) {
2275 root->orphan_block_rsv = block_rsv;
2276 } else if (block_rsv) {
2277 btrfs_free_block_rsv(root, block_rsv);
2278 block_rsv = NULL;
2279 }
Josef Bacik7b128762008-07-24 12:17:14 -04002280
Josef Bacik8a35d952012-05-23 14:26:42 -04002281 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2282 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002283#if 0
2284 /*
2285 * For proper ENOSPC handling, we should do orphan
2286 * cleanup when mounting. But this introduces backward
2287 * compatibility issue.
2288 */
2289 if (!xchg(&root->orphan_item_inserted, 1))
2290 insert = 2;
2291 else
2292 insert = 1;
2293#endif
2294 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002295 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002296 }
Josef Bacik7b128762008-07-24 12:17:14 -04002297
Josef Bacik72ac3c02012-05-23 14:13:11 -04002298 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2299 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002300 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002301 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002302
Yan, Zhengd68fc572010-05-16 10:49:58 -04002303 /* grab metadata reservation from transaction handle */
2304 if (reserve) {
2305 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002306 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002307 }
2308
2309 /* insert an orphan item to track this unlinked/truncated file */
2310 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002311 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002312 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002313 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2314 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002315 btrfs_abort_transaction(trans, root, ret);
2316 return ret;
2317 }
2318 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002319 }
2320
2321 /* insert an orphan item to track subvolume contains orphan files */
2322 if (insert >= 2) {
2323 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2324 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002325 if (ret && ret != -EEXIST) {
2326 btrfs_abort_transaction(trans, root, ret);
2327 return ret;
2328 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002329 }
2330 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002331}
2332
2333/*
2334 * We have done the truncate/delete so we can go ahead and remove the orphan
2335 * item for this particular inode.
2336 */
2337int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2338{
2339 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002340 int delete_item = 0;
2341 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002342 int ret = 0;
2343
Yan, Zhengd68fc572010-05-16 10:49:58 -04002344 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002345 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2346 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002347 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002348
Josef Bacik72ac3c02012-05-23 14:13:11 -04002349 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2350 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002351 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002352 spin_unlock(&root->orphan_lock);
2353
2354 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002355 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002356 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002357 }
2358
Josef Bacik8a35d952012-05-23 14:26:42 -04002359 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002360 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002361 atomic_dec(&root->orphan_inodes);
2362 }
Josef Bacik7b128762008-07-24 12:17:14 -04002363
Yan, Zhengd68fc572010-05-16 10:49:58 -04002364 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002365}
2366
2367/*
2368 * this cleans up any orphans that may be left on the list from the last use
2369 * of this root.
2370 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002371int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002372{
2373 struct btrfs_path *path;
2374 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002375 struct btrfs_key key, found_key;
2376 struct btrfs_trans_handle *trans;
2377 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002378 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002379 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2380
Yan, Zhengd68fc572010-05-16 10:49:58 -04002381 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002382 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002383
2384 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002385 if (!path) {
2386 ret = -ENOMEM;
2387 goto out;
2388 }
Josef Bacik7b128762008-07-24 12:17:14 -04002389 path->reada = -1;
2390
2391 key.objectid = BTRFS_ORPHAN_OBJECTID;
2392 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2393 key.offset = (u64)-1;
2394
Josef Bacik7b128762008-07-24 12:17:14 -04002395 while (1) {
2396 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002397 if (ret < 0)
2398 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002399
2400 /*
2401 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002402 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002403 * find the key and see if we have stuff that matches
2404 */
2405 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002406 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002407 if (path->slots[0] == 0)
2408 break;
2409 path->slots[0]--;
2410 }
2411
2412 /* pull out the item */
2413 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002414 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2415
2416 /* make sure the item matches what we want */
2417 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2418 break;
2419 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2420 break;
2421
2422 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002423 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002424
2425 /*
2426 * this is where we are basically btrfs_lookup, without the
2427 * crossing root thing. we store the inode number in the
2428 * offset of the orphan item.
2429 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002430
2431 if (found_key.offset == last_objectid) {
2432 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2433 "stopping orphan cleanup\n");
2434 ret = -EINVAL;
2435 goto out;
2436 }
2437
2438 last_objectid = found_key.offset;
2439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002440 found_key.objectid = found_key.offset;
2441 found_key.type = BTRFS_INODE_ITEM_KEY;
2442 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002443 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002444 ret = PTR_RET(inode);
2445 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002446 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002447
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002448 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2449 struct btrfs_root *dead_root;
2450 struct btrfs_fs_info *fs_info = root->fs_info;
2451 int is_dead_root = 0;
2452
2453 /*
2454 * this is an orphan in the tree root. Currently these
2455 * could come from 2 sources:
2456 * a) a snapshot deletion in progress
2457 * b) a free space cache inode
2458 * We need to distinguish those two, as the snapshot
2459 * orphan must not get deleted.
2460 * find_dead_roots already ran before us, so if this
2461 * is a snapshot deletion, we should find the root
2462 * in the dead_roots list
2463 */
2464 spin_lock(&fs_info->trans_lock);
2465 list_for_each_entry(dead_root, &fs_info->dead_roots,
2466 root_list) {
2467 if (dead_root->root_key.objectid ==
2468 found_key.objectid) {
2469 is_dead_root = 1;
2470 break;
2471 }
2472 }
2473 spin_unlock(&fs_info->trans_lock);
2474 if (is_dead_root) {
2475 /* prevent this orphan from being found again */
2476 key.offset = found_key.objectid - 1;
2477 continue;
2478 }
2479 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002480 /*
2481 * Inode is already gone but the orphan item is still there,
2482 * kill the orphan item.
2483 */
2484 if (ret == -ESTALE) {
2485 trans = btrfs_start_transaction(root, 1);
2486 if (IS_ERR(trans)) {
2487 ret = PTR_ERR(trans);
2488 goto out;
2489 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002490 printk(KERN_ERR "auto deleting %Lu\n",
2491 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002492 ret = btrfs_del_orphan_item(trans, root,
2493 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002494 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002495 btrfs_end_transaction(trans, root);
2496 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002497 }
Josef Bacik7b128762008-07-24 12:17:14 -04002498
Josef Bacik7b128762008-07-24 12:17:14 -04002499 /*
2500 * add this inode to the orphan list so btrfs_orphan_del does
2501 * the proper thing when we hit it
2502 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002503 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2504 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002505
Josef Bacik7b128762008-07-24 12:17:14 -04002506 /* if we have links, this was a truncate, lets do that */
2507 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002508 if (!S_ISREG(inode->i_mode)) {
2509 WARN_ON(1);
2510 iput(inode);
2511 continue;
2512 }
Josef Bacik7b128762008-07-24 12:17:14 -04002513 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002514
2515 /* 1 for the orphan item deletion. */
2516 trans = btrfs_start_transaction(root, 1);
2517 if (IS_ERR(trans)) {
2518 ret = PTR_ERR(trans);
2519 goto out;
2520 }
2521 ret = btrfs_orphan_add(trans, inode);
2522 btrfs_end_transaction(trans, root);
2523 if (ret)
2524 goto out;
2525
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002526 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002527 } else {
2528 nr_unlink++;
2529 }
2530
2531 /* this will do delete_inode and everything for us */
2532 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002533 if (ret)
2534 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002535 }
Miao Xie3254c872011-11-10 20:45:05 -05002536 /* release the path since we're done with it */
2537 btrfs_release_path(path);
2538
Yan, Zhengd68fc572010-05-16 10:49:58 -04002539 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2540
2541 if (root->orphan_block_rsv)
2542 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2543 (u64)-1);
2544
2545 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002546 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002547 if (!IS_ERR(trans))
2548 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002549 }
Josef Bacik7b128762008-07-24 12:17:14 -04002550
2551 if (nr_unlink)
2552 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2553 if (nr_truncate)
2554 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002555
2556out:
2557 if (ret)
2558 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2559 btrfs_free_path(path);
2560 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002561}
2562
Chris Masond352ac62008-09-29 15:18:18 -04002563/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002564 * very simple check to peek ahead in the leaf looking for xattrs. If we
2565 * don't find any xattrs, we know there can't be any acls.
2566 *
2567 * slot is the slot the inode is in, objectid is the objectid of the inode
2568 */
2569static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2570 int slot, u64 objectid)
2571{
2572 u32 nritems = btrfs_header_nritems(leaf);
2573 struct btrfs_key found_key;
2574 int scanned = 0;
2575
2576 slot++;
2577 while (slot < nritems) {
2578 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2579
2580 /* we found a different objectid, there must not be acls */
2581 if (found_key.objectid != objectid)
2582 return 0;
2583
2584 /* we found an xattr, assume we've got an acl */
2585 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2586 return 1;
2587
2588 /*
2589 * we found a key greater than an xattr key, there can't
2590 * be any acls later on
2591 */
2592 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2593 return 0;
2594
2595 slot++;
2596 scanned++;
2597
2598 /*
2599 * it goes inode, inode backrefs, xattrs, extents,
2600 * so if there are a ton of hard links to an inode there can
2601 * be a lot of backrefs. Don't waste time searching too hard,
2602 * this is just an optimization
2603 */
2604 if (scanned >= 8)
2605 break;
2606 }
2607 /* we hit the end of the leaf before we found an xattr or
2608 * something larger than an xattr. We have to assume the inode
2609 * has acls
2610 */
2611 return 1;
2612}
2613
2614/*
Chris Masond352ac62008-09-29 15:18:18 -04002615 * read an inode from the btree into the in-memory inode
2616 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002617static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002618{
2619 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002620 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002621 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002622 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002623 struct btrfs_root *root = BTRFS_I(inode)->root;
2624 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002625 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002626 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002627 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002628 bool filled = false;
2629
2630 ret = btrfs_fill_inode(inode, &rdev);
2631 if (!ret)
2632 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002633
2634 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002635 if (!path)
2636 goto make_bad;
2637
Josef Bacikd90c7322011-05-17 09:50:54 -04002638 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002639 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002640
Chris Mason39279cc2007-06-12 06:35:45 -04002641 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002642 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002643 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002644
Chris Mason5f39d392007-10-15 16:14:19 -04002645 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002646
2647 if (filled)
2648 goto cache_acl;
2649
Chris Mason5f39d392007-10-15 16:14:19 -04002650 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2651 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002652 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002653 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002654 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2655 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002656 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002657
2658 tspec = btrfs_inode_atime(inode_item);
2659 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2660 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2661
2662 tspec = btrfs_inode_mtime(inode_item);
2663 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2664 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2665
2666 tspec = btrfs_inode_ctime(inode_item);
2667 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2668 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2669
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002670 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002671 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002672 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2673
2674 /*
2675 * If we were modified in the current generation and evicted from memory
2676 * and then re-read we need to do a full sync since we don't have any
2677 * idea about which extents were modified before we were evicted from
2678 * cache.
2679 */
2680 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2681 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2682 &BTRFS_I(inode)->runtime_flags);
2683
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002684 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002685 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002686 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002687 rdev = btrfs_inode_rdev(leaf, inode_item);
2688
Josef Bacikaec74772008-07-24 12:12:38 -04002689 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002690 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002691cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002692 /*
2693 * try to precache a NULL acl entry for files that don't have
2694 * any xattrs or acls
2695 */
Li Zefan33345d012011-04-20 10:31:50 +08002696 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2697 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002698 if (!maybe_acls)
2699 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002700
Chris Mason39279cc2007-06-12 06:35:45 -04002701 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002702
Chris Mason39279cc2007-06-12 06:35:45 -04002703 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002704 case S_IFREG:
2705 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002706 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002707 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002708 inode->i_fop = &btrfs_file_operations;
2709 inode->i_op = &btrfs_file_inode_operations;
2710 break;
2711 case S_IFDIR:
2712 inode->i_fop = &btrfs_dir_file_operations;
2713 if (root == root->fs_info->tree_root)
2714 inode->i_op = &btrfs_dir_ro_inode_operations;
2715 else
2716 inode->i_op = &btrfs_dir_inode_operations;
2717 break;
2718 case S_IFLNK:
2719 inode->i_op = &btrfs_symlink_inode_operations;
2720 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002721 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002722 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002723 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002724 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002725 init_special_inode(inode, inode->i_mode, rdev);
2726 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002727 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002728
2729 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002730 return;
2731
2732make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002733 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002734 make_bad_inode(inode);
2735}
2736
Chris Masond352ac62008-09-29 15:18:18 -04002737/*
2738 * given a leaf and an inode, copy the inode fields into the leaf
2739 */
Chris Masone02119d2008-09-05 16:13:11 -04002740static void fill_inode_item(struct btrfs_trans_handle *trans,
2741 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002742 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002743 struct inode *inode)
2744{
Liu Bo51fab692012-12-27 09:01:21 +00002745 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002746
Liu Bo51fab692012-12-27 09:01:21 +00002747 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04002748
Liu Bo51fab692012-12-27 09:01:21 +00002749 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2750 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2751 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
2752 &token);
2753 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2754 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002755
Liu Bo51fab692012-12-27 09:01:21 +00002756 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2757 inode->i_atime.tv_sec, &token);
2758 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2759 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002760
Liu Bo51fab692012-12-27 09:01:21 +00002761 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2762 inode->i_mtime.tv_sec, &token);
2763 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
2764 inode->i_mtime.tv_nsec, &token);
2765
2766 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
2767 inode->i_ctime.tv_sec, &token);
2768 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
2769 inode->i_ctime.tv_nsec, &token);
2770
2771 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
2772 &token);
2773 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
2774 &token);
2775 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
2776 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
2777 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
2778 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
2779 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04002780}
2781
Chris Masond352ac62008-09-29 15:18:18 -04002782/*
2783 * copy everything in the in-memory inode into the btree.
2784 */
Chris Mason21151332011-11-10 20:39:08 -05002785static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002786 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002787{
2788 struct btrfs_inode_item *inode_item;
2789 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002790 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002791 int ret;
2792
2793 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002794 if (!path)
2795 return -ENOMEM;
2796
Chris Masonb9473432009-03-13 11:00:37 -04002797 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002798 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2799 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002800 if (ret) {
2801 if (ret > 0)
2802 ret = -ENOENT;
2803 goto failed;
2804 }
2805
Chris Masonb4ce94d2009-02-04 09:25:08 -05002806 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002807 leaf = path->nodes[0];
2808 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002809 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002810
Chris Masone02119d2008-09-05 16:13:11 -04002811 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002812 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002813 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002814 ret = 0;
2815failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002816 btrfs_free_path(path);
2817 return ret;
2818}
2819
Chris Masond352ac62008-09-29 15:18:18 -04002820/*
Chris Mason21151332011-11-10 20:39:08 -05002821 * copy everything in the in-memory inode into the btree.
2822 */
2823noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2824 struct btrfs_root *root, struct inode *inode)
2825{
2826 int ret;
2827
2828 /*
2829 * If the inode is a free space inode, we can deadlock during commit
2830 * if we put it into the delayed code.
2831 *
2832 * The data relocation inode should also be directly updated
2833 * without delay
2834 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002835 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002836 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002837 btrfs_update_root_times(trans, root);
2838
Chris Mason21151332011-11-10 20:39:08 -05002839 ret = btrfs_delayed_update_inode(trans, root, inode);
2840 if (!ret)
2841 btrfs_set_inode_last_trans(trans, inode);
2842 return ret;
2843 }
2844
2845 return btrfs_update_inode_item(trans, root, inode);
2846}
2847
Josef Bacikbe6aef62012-10-22 15:43:12 -04002848noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2849 struct btrfs_root *root,
2850 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002851{
2852 int ret;
2853
2854 ret = btrfs_update_inode(trans, root, inode);
2855 if (ret == -ENOSPC)
2856 return btrfs_update_inode_item(trans, root, inode);
2857 return ret;
2858}
2859
2860/*
Chris Masond352ac62008-09-29 15:18:18 -04002861 * unlink helper that gets used here in inode.c and in the tree logging
2862 * recovery code. It remove a link in a directory with a given name, and
2863 * also drops the back refs in the inode to the directory
2864 */
Al Viro92986792011-03-04 17:14:37 +00002865static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2866 struct btrfs_root *root,
2867 struct inode *dir, struct inode *inode,
2868 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002869{
2870 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002871 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002872 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002873 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002874 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002875 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002876 u64 ino = btrfs_ino(inode);
2877 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002878
2879 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002880 if (!path) {
2881 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002882 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002883 }
2884
Chris Masonb9473432009-03-13 11:00:37 -04002885 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002886 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002887 name, name_len, -1);
2888 if (IS_ERR(di)) {
2889 ret = PTR_ERR(di);
2890 goto err;
2891 }
2892 if (!di) {
2893 ret = -ENOENT;
2894 goto err;
2895 }
Chris Mason5f39d392007-10-15 16:14:19 -04002896 leaf = path->nodes[0];
2897 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002898 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002899 if (ret)
2900 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002901 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002902
Li Zefan33345d012011-04-20 10:31:50 +08002903 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2904 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002905 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002906 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002907 "inode %llu parent %llu\n", name_len, name,
2908 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002909 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002910 goto err;
2911 }
2912
Miao Xie16cdcec2011-04-22 18:12:22 +08002913 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002914 if (ret) {
2915 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002916 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002917 }
Chris Mason39279cc2007-06-12 06:35:45 -04002918
Chris Masone02119d2008-09-05 16:13:11 -04002919 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002920 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002921 if (ret != 0 && ret != -ENOENT) {
2922 btrfs_abort_transaction(trans, root, ret);
2923 goto err;
2924 }
Chris Masone02119d2008-09-05 16:13:11 -04002925
2926 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2927 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002928 if (ret == -ENOENT)
2929 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002930err:
2931 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002932 if (ret)
2933 goto out;
2934
2935 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002936 inode_inc_iversion(inode);
2937 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002938 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002939 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002940out:
Chris Mason39279cc2007-06-12 06:35:45 -04002941 return ret;
2942}
2943
Al Viro92986792011-03-04 17:14:37 +00002944int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2945 struct btrfs_root *root,
2946 struct inode *dir, struct inode *inode,
2947 const char *name, int name_len)
2948{
2949 int ret;
2950 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2951 if (!ret) {
2952 btrfs_drop_nlink(inode);
2953 ret = btrfs_update_inode(trans, root, inode);
2954 }
2955 return ret;
2956}
2957
2958
Yan, Zhenga22285a2010-05-16 10:48:46 -04002959/* helper to check if there is any shared block in the path */
2960static int check_path_shared(struct btrfs_root *root,
2961 struct btrfs_path *path)
2962{
2963 struct extent_buffer *eb;
2964 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002965 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002966
2967 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002968 int ret;
2969
Yan, Zhenga22285a2010-05-16 10:48:46 -04002970 if (!path->nodes[level])
2971 break;
2972 eb = path->nodes[level];
2973 if (!btrfs_block_can_be_shared(root, eb))
2974 continue;
2975 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2976 &refs, NULL);
2977 if (refs > 1)
2978 return 1;
2979 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002980 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002981}
2982
2983/*
2984 * helper to start transaction for unlink and rmdir.
2985 *
2986 * unlink and rmdir are special in btrfs, they do not always free space.
2987 * so in enospc case, we should make sure they will free space before
2988 * allowing them to use the global metadata reservation.
2989 */
2990static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2991 struct dentry *dentry)
2992{
2993 struct btrfs_trans_handle *trans;
2994 struct btrfs_root *root = BTRFS_I(dir)->root;
2995 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002996 struct btrfs_dir_item *di;
2997 struct inode *inode = dentry->d_inode;
2998 u64 index;
2999 int check_link = 1;
3000 int err = -ENOSPC;
3001 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003002 u64 ino = btrfs_ino(inode);
3003 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003004
Josef Bacike70bea52011-10-11 14:18:24 -04003005 /*
3006 * 1 for the possible orphan item
3007 * 1 for the dir item
3008 * 1 for the dir index
3009 * 1 for the inode ref
3010 * 1 for the inode ref in the tree log
3011 * 2 for the dir entries in the log
3012 * 1 for the inode
3013 */
3014 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003015 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3016 return trans;
3017
Li Zefan33345d012011-04-20 10:31:50 +08003018 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003019 return ERR_PTR(-ENOSPC);
3020
3021 /* check if there is someone else holds reference */
3022 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3023 return ERR_PTR(-ENOSPC);
3024
3025 if (atomic_read(&inode->i_count) > 2)
3026 return ERR_PTR(-ENOSPC);
3027
3028 if (xchg(&root->fs_info->enospc_unlink, 1))
3029 return ERR_PTR(-ENOSPC);
3030
3031 path = btrfs_alloc_path();
3032 if (!path) {
3033 root->fs_info->enospc_unlink = 0;
3034 return ERR_PTR(-ENOMEM);
3035 }
3036
Josef Bacik3880a1b2011-10-14 14:46:51 -04003037 /* 1 for the orphan item */
3038 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003039 if (IS_ERR(trans)) {
3040 btrfs_free_path(path);
3041 root->fs_info->enospc_unlink = 0;
3042 return trans;
3043 }
3044
3045 path->skip_locking = 1;
3046 path->search_commit_root = 1;
3047
3048 ret = btrfs_lookup_inode(trans, root, path,
3049 &BTRFS_I(dir)->location, 0);
3050 if (ret < 0) {
3051 err = ret;
3052 goto out;
3053 }
3054 if (ret == 0) {
3055 if (check_path_shared(root, path))
3056 goto out;
3057 } else {
3058 check_link = 0;
3059 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003060 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003061
3062 ret = btrfs_lookup_inode(trans, root, path,
3063 &BTRFS_I(inode)->location, 0);
3064 if (ret < 0) {
3065 err = ret;
3066 goto out;
3067 }
3068 if (ret == 0) {
3069 if (check_path_shared(root, path))
3070 goto out;
3071 } else {
3072 check_link = 0;
3073 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003074 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003075
3076 if (ret == 0 && S_ISREG(inode->i_mode)) {
3077 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003078 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003079 if (ret < 0) {
3080 err = ret;
3081 goto out;
3082 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003083 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003084 if (check_path_shared(root, path))
3085 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003086 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003087 }
3088
3089 if (!check_link) {
3090 err = 0;
3091 goto out;
3092 }
3093
Li Zefan33345d012011-04-20 10:31:50 +08003094 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003095 dentry->d_name.name, dentry->d_name.len, 0);
3096 if (IS_ERR(di)) {
3097 err = PTR_ERR(di);
3098 goto out;
3099 }
3100 if (di) {
3101 if (check_path_shared(root, path))
3102 goto out;
3103 } else {
3104 err = 0;
3105 goto out;
3106 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003107 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003108
Mark Fashehf1863732012-08-08 11:32:27 -07003109 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3110 dentry->d_name.len, ino, dir_ino, 0,
3111 &index);
3112 if (ret) {
3113 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003114 goto out;
3115 }
Mark Fashehf1863732012-08-08 11:32:27 -07003116
Yan, Zhenga22285a2010-05-16 10:48:46 -04003117 if (check_path_shared(root, path))
3118 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003119
David Sterbab3b4aa72011-04-21 01:20:15 +02003120 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003121
Miao Xie16cdcec2011-04-22 18:12:22 +08003122 /*
3123 * This is a commit root search, if we can lookup inode item and other
3124 * relative items in the commit root, it means the transaction of
3125 * dir/file creation has been committed, and the dir index item that we
3126 * delay to insert has also been inserted into the commit root. So
3127 * we needn't worry about the delayed insertion of the dir index item
3128 * here.
3129 */
Li Zefan33345d012011-04-20 10:31:50 +08003130 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003131 dentry->d_name.name, dentry->d_name.len, 0);
3132 if (IS_ERR(di)) {
3133 err = PTR_ERR(di);
3134 goto out;
3135 }
3136 BUG_ON(ret == -ENOENT);
3137 if (check_path_shared(root, path))
3138 goto out;
3139
3140 err = 0;
3141out:
3142 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003143 /* Migrate the orphan reservation over */
3144 if (!err)
3145 err = btrfs_block_rsv_migrate(trans->block_rsv,
3146 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003147 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003148
Yan, Zhenga22285a2010-05-16 10:48:46 -04003149 if (err) {
3150 btrfs_end_transaction(trans, root);
3151 root->fs_info->enospc_unlink = 0;
3152 return ERR_PTR(err);
3153 }
3154
3155 trans->block_rsv = &root->fs_info->global_block_rsv;
3156 return trans;
3157}
3158
3159static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3160 struct btrfs_root *root)
3161{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003162 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003163 btrfs_block_rsv_release(root, trans->block_rsv,
3164 trans->bytes_reserved);
3165 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003166 BUG_ON(!root->fs_info->enospc_unlink);
3167 root->fs_info->enospc_unlink = 0;
3168 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003169 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003170}
3171
Chris Mason39279cc2007-06-12 06:35:45 -04003172static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3173{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003174 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003175 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003176 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003177 int ret;
3178
Yan, Zhenga22285a2010-05-16 10:48:46 -04003179 trans = __unlink_start_trans(dir, dentry);
3180 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003181 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003182
Chris Mason12fcfd22009-03-24 10:24:20 -04003183 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3184
Chris Masone02119d2008-09-05 16:13:11 -04003185 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3186 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003187 if (ret)
3188 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003189
Yan, Zhenga22285a2010-05-16 10:48:46 -04003190 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003191 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003192 if (ret)
3193 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003194 }
Josef Bacik7b128762008-07-24 12:17:14 -04003195
Tsutomu Itohb5324022011-07-19 07:27:20 +00003196out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003197 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003198 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003199 return ret;
3200}
3201
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003202int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3203 struct btrfs_root *root,
3204 struct inode *dir, u64 objectid,
3205 const char *name, int name_len)
3206{
3207 struct btrfs_path *path;
3208 struct extent_buffer *leaf;
3209 struct btrfs_dir_item *di;
3210 struct btrfs_key key;
3211 u64 index;
3212 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003213 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003214
3215 path = btrfs_alloc_path();
3216 if (!path)
3217 return -ENOMEM;
3218
Li Zefan33345d012011-04-20 10:31:50 +08003219 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003220 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003221 if (IS_ERR_OR_NULL(di)) {
3222 if (!di)
3223 ret = -ENOENT;
3224 else
3225 ret = PTR_ERR(di);
3226 goto out;
3227 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003228
3229 leaf = path->nodes[0];
3230 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3231 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3232 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003233 if (ret) {
3234 btrfs_abort_transaction(trans, root, ret);
3235 goto out;
3236 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003237 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003238
3239 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3240 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003241 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003242 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003243 if (ret != -ENOENT) {
3244 btrfs_abort_transaction(trans, root, ret);
3245 goto out;
3246 }
Li Zefan33345d012011-04-20 10:31:50 +08003247 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003248 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003249 if (IS_ERR_OR_NULL(di)) {
3250 if (!di)
3251 ret = -ENOENT;
3252 else
3253 ret = PTR_ERR(di);
3254 btrfs_abort_transaction(trans, root, ret);
3255 goto out;
3256 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003257
3258 leaf = path->nodes[0];
3259 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003260 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003261 index = key.offset;
3262 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003263 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003264
Miao Xie16cdcec2011-04-22 18:12:22 +08003265 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003266 if (ret) {
3267 btrfs_abort_transaction(trans, root, ret);
3268 goto out;
3269 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003270
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003271 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003272 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003273 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003274 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003275 if (ret)
3276 btrfs_abort_transaction(trans, root, ret);
3277out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003278 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003279 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003280}
3281
Chris Mason39279cc2007-06-12 06:35:45 -04003282static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3283{
3284 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003285 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003286 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003287 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003288
David Sterbab3ae2442012-09-13 16:04:34 -06003289 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003290 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003291 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3292 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003293
Yan, Zhenga22285a2010-05-16 10:48:46 -04003294 trans = __unlink_start_trans(dir, dentry);
3295 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003296 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003297
Li Zefan33345d012011-04-20 10:31:50 +08003298 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003299 err = btrfs_unlink_subvol(trans, root, dir,
3300 BTRFS_I(inode)->location.objectid,
3301 dentry->d_name.name,
3302 dentry->d_name.len);
3303 goto out;
3304 }
3305
Josef Bacik7b128762008-07-24 12:17:14 -04003306 err = btrfs_orphan_add(trans, inode);
3307 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003308 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003309
Chris Mason39279cc2007-06-12 06:35:45 -04003310 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003311 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3312 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003313 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003314 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003315out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003316 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003317 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003318
Chris Mason39279cc2007-06-12 06:35:45 -04003319 return err;
3320}
3321
Chris Mason323ac952008-10-01 19:05:46 -04003322/*
Chris Mason39279cc2007-06-12 06:35:45 -04003323 * this can truncate away extent items, csum items and directory items.
3324 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003325 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003326 *
3327 * csum items that cross the new i_size are truncated to the new size
3328 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003329 *
3330 * min_type is the minimum key type to truncate down to. If set to 0, this
3331 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003332 */
Yan, Zheng80825102009-11-12 09:35:36 +00003333int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3334 struct btrfs_root *root,
3335 struct inode *inode,
3336 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003337{
Chris Mason39279cc2007-06-12 06:35:45 -04003338 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003339 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003340 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003341 struct btrfs_key key;
3342 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003343 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003344 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003345 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003346 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003347 u64 mask = root->sectorsize - 1;
3348 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003349 int found_extent;
3350 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003351 int pending_del_nr = 0;
3352 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003353 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003354 int ret;
3355 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003356 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003357
3358 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003359
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003360 path = btrfs_alloc_path();
3361 if (!path)
3362 return -ENOMEM;
3363 path->reada = -1;
3364
Josef Bacik5dc562c2012-08-17 13:14:17 -04003365 /*
3366 * We want to drop from the next block forward in case this new size is
3367 * not block aligned since we will be keeping the last block of the
3368 * extent just the way it is.
3369 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003370 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003371 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003372
Miao Xie16cdcec2011-04-22 18:12:22 +08003373 /*
3374 * This function is also used to drop the items in the log tree before
3375 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3376 * it is used to drop the loged items. So we shouldn't kill the delayed
3377 * items.
3378 */
3379 if (min_type == 0 && root == BTRFS_I(inode)->root)
3380 btrfs_kill_delayed_inode_items(inode);
3381
Li Zefan33345d012011-04-20 10:31:50 +08003382 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003383 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003384 key.type = (u8)-1;
3385
Chris Mason85e21ba2008-01-29 15:11:36 -05003386search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003387 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003388 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003389 if (ret < 0) {
3390 err = ret;
3391 goto out;
3392 }
Chris Masond3977122009-01-05 21:25:51 -05003393
Chris Mason85e21ba2008-01-29 15:11:36 -05003394 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003395 /* there are no items in the tree for us to truncate, we're
3396 * done
3397 */
Yan, Zheng80825102009-11-12 09:35:36 +00003398 if (path->slots[0] == 0)
3399 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003400 path->slots[0]--;
3401 }
3402
Chris Masond3977122009-01-05 21:25:51 -05003403 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003404 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003405 leaf = path->nodes[0];
3406 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3407 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003408
Li Zefan33345d012011-04-20 10:31:50 +08003409 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003410 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003411
Chris Mason85e21ba2008-01-29 15:11:36 -05003412 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003413 break;
3414
Chris Mason5f39d392007-10-15 16:14:19 -04003415 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003416 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003417 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003418 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003419 extent_type = btrfs_file_extent_type(leaf, fi);
3420 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003421 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003422 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003423 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003424 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003425 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003426 }
Yan008630c2007-11-07 13:31:09 -05003427 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003428 }
Yan, Zheng80825102009-11-12 09:35:36 +00003429 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003430 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003431 } else {
3432 if (item_end < new_size)
3433 break;
3434 if (found_key.offset >= new_size)
3435 del_item = 1;
3436 else
3437 del_item = 0;
3438 }
Chris Mason39279cc2007-06-12 06:35:45 -04003439 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003440 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003441 if (found_type != BTRFS_EXTENT_DATA_KEY)
3442 goto delete;
3443
3444 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003445 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003446 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003447 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003448 u64 orig_num_bytes =
3449 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003450 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003451 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05003452 extent_num_bytes = extent_num_bytes &
3453 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003454 btrfs_set_file_extent_num_bytes(leaf, fi,
3455 extent_num_bytes);
3456 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003457 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003458 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003459 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003460 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003461 } else {
Chris Masondb945352007-10-15 16:15:53 -04003462 extent_num_bytes =
3463 btrfs_file_extent_disk_num_bytes(leaf,
3464 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003465 extent_offset = found_key.offset -
3466 btrfs_file_extent_offset(leaf, fi);
3467
Chris Mason39279cc2007-06-12 06:35:45 -04003468 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003469 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003470 if (extent_start != 0) {
3471 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003472 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003473 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003474 }
3475 }
Chris Mason90692182008-02-08 13:49:28 -05003476 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003477 /*
3478 * we can't truncate inline items that have had
3479 * special encodings
3480 */
3481 if (!del_item &&
3482 btrfs_file_extent_compression(leaf, fi) == 0 &&
3483 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3484 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003485 u32 size = new_size - found_key.offset;
3486
3487 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003488 inode_sub_bytes(inode, item_end + 1 -
3489 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003490 }
3491 size =
3492 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003493 btrfs_truncate_item(trans, root, path,
3494 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003495 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003496 inode_sub_bytes(inode, item_end + 1 -
3497 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003498 }
Chris Mason39279cc2007-06-12 06:35:45 -04003499 }
Chris Mason179e29e2007-11-01 11:28:41 -04003500delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003501 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003502 if (!pending_del_nr) {
3503 /* no pending yet, add ourselves */
3504 pending_del_slot = path->slots[0];
3505 pending_del_nr = 1;
3506 } else if (pending_del_nr &&
3507 path->slots[0] + 1 == pending_del_slot) {
3508 /* hop on the pending chunk */
3509 pending_del_nr++;
3510 pending_del_slot = path->slots[0];
3511 } else {
Chris Masond3977122009-01-05 21:25:51 -05003512 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003513 }
Chris Mason39279cc2007-06-12 06:35:45 -04003514 } else {
3515 break;
3516 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003517 if (found_extent && (root->ref_cows ||
3518 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003519 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003520 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003521 extent_num_bytes, 0,
3522 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003523 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003524 BUG_ON(ret);
3525 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003526
Yan, Zheng80825102009-11-12 09:35:36 +00003527 if (found_type == BTRFS_INODE_ITEM_KEY)
3528 break;
3529
3530 if (path->slots[0] == 0 ||
3531 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003532 if (pending_del_nr) {
3533 ret = btrfs_del_items(trans, root, path,
3534 pending_del_slot,
3535 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003536 if (ret) {
3537 btrfs_abort_transaction(trans,
3538 root, ret);
3539 goto error;
3540 }
Yan, Zheng80825102009-11-12 09:35:36 +00003541 pending_del_nr = 0;
3542 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003543 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003544 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003545 } else {
3546 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003547 }
Chris Mason39279cc2007-06-12 06:35:45 -04003548 }
Yan, Zheng80825102009-11-12 09:35:36 +00003549out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003550 if (pending_del_nr) {
3551 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3552 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003553 if (ret)
3554 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003555 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003556error:
Chris Mason39279cc2007-06-12 06:35:45 -04003557 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003558 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003559}
3560
Chris Masona52d9a82007-08-27 16:49:44 -04003561/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003562 * btrfs_truncate_page - read, zero a chunk and write a page
3563 * @inode - inode that we're zeroing
3564 * @from - the offset to start zeroing
3565 * @len - the length to zero, 0 to zero the entire range respective to the
3566 * offset
3567 * @front - zero up to the offset instead of from the offset on
3568 *
3569 * This will find the page for the "from" offset and cow the page and zero the
3570 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003571 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003572int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3573 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003574{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003575 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003576 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003577 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3578 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003579 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003580 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003581 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003582 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3583 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3584 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003585 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003586 int ret = 0;
3587 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003588 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003589
Josef Bacik2aaa6652012-08-29 14:27:18 -04003590 if ((offset & (blocksize - 1)) == 0 &&
3591 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003592 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003593 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003594 if (ret)
3595 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003596
Chris Mason211c17f2008-05-15 09:13:45 -04003597again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003598 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003599 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003600 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003601 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003602 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003603 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003604
3605 page_start = page_offset(page);
3606 page_end = page_start + PAGE_CACHE_SIZE - 1;
3607
Chris Masona52d9a82007-08-27 16:49:44 -04003608 if (!PageUptodate(page)) {
3609 ret = btrfs_readpage(NULL, page);
3610 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003611 if (page->mapping != mapping) {
3612 unlock_page(page);
3613 page_cache_release(page);
3614 goto again;
3615 }
Chris Masona52d9a82007-08-27 16:49:44 -04003616 if (!PageUptodate(page)) {
3617 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003618 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003619 }
3620 }
Chris Mason211c17f2008-05-15 09:13:45 -04003621 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003622
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003623 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003624 set_page_extent_mapped(page);
3625
3626 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3627 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003628 unlock_extent_cached(io_tree, page_start, page_end,
3629 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003630 unlock_page(page);
3631 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003632 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003633 btrfs_put_ordered_extent(ordered);
3634 goto again;
3635 }
3636
Josef Bacik2ac55d42010-02-03 19:33:23 +00003637 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003638 EXTENT_DIRTY | EXTENT_DELALLOC |
3639 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003640 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003641
Josef Bacik2ac55d42010-02-03 19:33:23 +00003642 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3643 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003644 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003645 unlock_extent_cached(io_tree, page_start, page_end,
3646 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003647 goto out_unlock;
3648 }
3649
Chris Masone6dcd2d2008-07-17 12:53:50 -04003650 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003651 if (!len)
3652 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003653 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003654 if (front)
3655 memset(kaddr, 0, offset);
3656 else
3657 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003658 flush_dcache_page(page);
3659 kunmap(page);
3660 }
Chris Mason247e7432008-07-17 12:53:51 -04003661 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003662 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003663 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3664 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003665
Chris Mason89642222008-07-24 09:41:53 -04003666out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003667 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003668 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003669 unlock_page(page);
3670 page_cache_release(page);
3671out:
3672 return ret;
3673}
3674
Josef Bacik695a0d02011-03-04 15:46:53 -05003675/*
3676 * This function puts in dummy file extents for the area we're creating a hole
3677 * for. So if we are truncating this file to a larger size we need to insert
3678 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3679 * the range between oldsize and size
3680 */
Josef Bacika41ad392011-01-31 15:30:16 -05003681int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003682{
3683 struct btrfs_trans_handle *trans;
3684 struct btrfs_root *root = BTRFS_I(inode)->root;
3685 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003686 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003687 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003688 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003689 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003690 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003691 u64 block_end = (size + mask) & ~mask;
3692 u64 last_byte;
3693 u64 cur_offset;
3694 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003695 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003696
3697 if (size <= hole_start)
3698 return 0;
3699
Yan Zheng9036c102008-10-30 14:19:41 -04003700 while (1) {
3701 struct btrfs_ordered_extent *ordered;
3702 btrfs_wait_ordered_range(inode, hole_start,
3703 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003704 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003705 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003706 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3707 if (!ordered)
3708 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003709 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3710 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003711 btrfs_put_ordered_extent(ordered);
3712 }
3713
Yan Zheng9036c102008-10-30 14:19:41 -04003714 cur_offset = hole_start;
3715 while (1) {
3716 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3717 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003718 if (IS_ERR(em)) {
3719 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003720 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003721 break;
3722 }
Yan Zheng9036c102008-10-30 14:19:41 -04003723 last_byte = min(extent_map_end(em), block_end);
3724 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003725 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003726 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003727 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003728
Miao Xie36423202011-12-14 20:12:02 -05003729 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003730 if (IS_ERR(trans)) {
3731 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003732 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003733 }
Yan, Zheng80825102009-11-12 09:35:36 +00003734
Josef Bacik5dc562c2012-08-17 13:14:17 -04003735 err = btrfs_drop_extents(trans, root, inode,
3736 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003737 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003738 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003739 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003740 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003741 break;
Miao Xie5b397372011-09-11 10:52:24 -04003742 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003743
Yan Zheng9036c102008-10-30 14:19:41 -04003744 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003745 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003746 0, hole_size, 0, hole_size,
3747 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003748 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003749 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003750 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003751 break;
Miao Xie5b397372011-09-11 10:52:24 -04003752 }
Yan, Zheng80825102009-11-12 09:35:36 +00003753
Josef Bacik5dc562c2012-08-17 13:14:17 -04003754 btrfs_drop_extent_cache(inode, cur_offset,
3755 cur_offset + hole_size - 1, 0);
3756 hole_em = alloc_extent_map();
3757 if (!hole_em) {
3758 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3759 &BTRFS_I(inode)->runtime_flags);
3760 goto next;
3761 }
3762 hole_em->start = cur_offset;
3763 hole_em->len = hole_size;
3764 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003765
Josef Bacik5dc562c2012-08-17 13:14:17 -04003766 hole_em->block_start = EXTENT_MAP_HOLE;
3767 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003768 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003769 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3770 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3771 hole_em->generation = trans->transid;
3772
3773 while (1) {
3774 write_lock(&em_tree->lock);
3775 err = add_extent_mapping(em_tree, hole_em);
3776 if (!err)
3777 list_move(&hole_em->list,
3778 &em_tree->modified_extents);
3779 write_unlock(&em_tree->lock);
3780 if (err != -EEXIST)
3781 break;
3782 btrfs_drop_extent_cache(inode, cur_offset,
3783 cur_offset +
3784 hole_size - 1, 0);
3785 }
3786 free_extent_map(hole_em);
3787next:
Miao Xie36423202011-12-14 20:12:02 -05003788 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003789 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003790 }
3791 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003792 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003793 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003794 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003795 break;
3796 }
3797
Yan, Zhenga22285a2010-05-16 10:48:46 -04003798 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003799 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3800 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003801 return err;
3802}
3803
Eric Sandeen3972f262013-01-12 02:57:22 +00003804static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00003805{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003806 struct btrfs_root *root = BTRFS_I(inode)->root;
3807 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003808 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00003809 loff_t newsize = attr->ia_size;
3810 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00003811 int ret;
3812
Josef Bacika41ad392011-01-31 15:30:16 -05003813 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003814 return 0;
3815
Eric Sandeen3972f262013-01-12 02:57:22 +00003816 /*
3817 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3818 * special case where we need to update the times despite not having
3819 * these flags set. For all other operations the VFS set these flags
3820 * explicitly if it wants a timestamp update.
3821 */
3822 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3823 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3824
Josef Bacika41ad392011-01-31 15:30:16 -05003825 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003826 truncate_pagecache(inode, oldsize, newsize);
3827 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003828 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003829 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003830
Miao Xief4a2f4c2011-12-14 20:12:01 -05003831 trans = btrfs_start_transaction(root, 1);
3832 if (IS_ERR(trans))
3833 return PTR_ERR(trans);
3834
3835 i_size_write(inode, newsize);
3836 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3837 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003838 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003839 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003840
Josef Bacika41ad392011-01-31 15:30:16 -05003841 /*
3842 * We're truncating a file that used to have good data down to
3843 * zero. Make sure it gets into the ordered flush list so that
3844 * any new writes get down to disk quickly.
3845 */
3846 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003847 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3848 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003849
Josef Bacikf3fe8202013-01-07 17:03:21 -05003850 /*
3851 * 1 for the orphan item we're going to add
3852 * 1 for the orphan item deletion.
3853 */
3854 trans = btrfs_start_transaction(root, 2);
3855 if (IS_ERR(trans))
3856 return PTR_ERR(trans);
3857
3858 /*
3859 * We need to do this in case we fail at _any_ point during the
3860 * actual truncate. Once we do the truncate_setsize we could
3861 * invalidate pages which forces any outstanding ordered io to
3862 * be instantly completed which will give us extents that need
3863 * to be truncated. If we fail to get an orphan inode down we
3864 * could have left over extents that were never meant to live,
3865 * so we need to garuntee from this point on that everything
3866 * will be consistent.
3867 */
3868 ret = btrfs_orphan_add(trans, inode);
3869 btrfs_end_transaction(trans, root);
3870 if (ret)
3871 return ret;
3872
Josef Bacika41ad392011-01-31 15:30:16 -05003873 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3874 truncate_setsize(inode, newsize);
3875 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003876 if (ret && inode->i_nlink)
3877 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003878 }
3879
Josef Bacika41ad392011-01-31 15:30:16 -05003880 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003881}
3882
Chris Mason39279cc2007-06-12 06:35:45 -04003883static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3884{
3885 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003886 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003887 int err;
3888
Li Zefanb83cc962010-12-20 16:04:08 +08003889 if (btrfs_root_readonly(root))
3890 return -EROFS;
3891
Chris Mason39279cc2007-06-12 06:35:45 -04003892 err = inode_change_ok(inode, attr);
3893 if (err)
3894 return err;
3895
Chris Mason5a3f23d2009-03-31 13:27:11 -04003896 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00003897 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00003898 if (err)
3899 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003900 }
Yan Zheng9036c102008-10-30 14:19:41 -04003901
Christoph Hellwig10257742010-06-04 11:30:02 +02003902 if (attr->ia_valid) {
3903 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003904 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003905 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003906
Josef Bacik22c44fe2011-11-30 10:45:38 -05003907 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003908 err = btrfs_acl_chmod(inode);
3909 }
3910
Chris Mason39279cc2007-06-12 06:35:45 -04003911 return err;
3912}
Chris Mason61295eb2008-01-14 16:24:38 -05003913
Al Virobd555972010-06-07 11:35:40 -04003914void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003915{
3916 struct btrfs_trans_handle *trans;
3917 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003918 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003919 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003920 int ret;
3921
liubo1abe9b82011-03-24 11:18:59 +00003922 trace_btrfs_inode_evict(inode);
3923
Chris Mason39279cc2007-06-12 06:35:45 -04003924 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003925 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003926 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003927 goto no_delete;
3928
Chris Mason39279cc2007-06-12 06:35:45 -04003929 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003930 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003931 goto no_delete;
3932 }
Al Virobd555972010-06-07 11:35:40 -04003933 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003934 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003935
Yan, Zhengc71bf092009-11-12 09:34:40 +00003936 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003937 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003938 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003939 goto no_delete;
3940 }
3941
Yan, Zheng76dda932009-09-21 16:00:26 -04003942 if (inode->i_nlink > 0) {
3943 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3944 goto no_delete;
3945 }
3946
Miao Xie0e8c36a2012-12-19 06:59:51 +00003947 ret = btrfs_commit_inode_delayed_inode(inode);
3948 if (ret) {
3949 btrfs_orphan_del(NULL, inode);
3950 goto no_delete;
3951 }
3952
Miao Xie66d8f3d2012-09-06 04:02:28 -06003953 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003954 if (!rsv) {
3955 btrfs_orphan_del(NULL, inode);
3956 goto no_delete;
3957 }
Josef Bacik4a338542011-08-29 11:01:31 -04003958 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003959 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003960 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003961
Chris Masondbe674a2008-07-17 12:54:05 -04003962 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003963
Josef Bacik4289a662011-08-05 13:22:24 -04003964 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003965 * This is a bit simpler than btrfs_truncate since we've already
3966 * reserved our space for our orphan item in the unlink, so we just
3967 * need to reserve some slack space in case we add bytes and update
3968 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003969 */
Yan, Zheng80825102009-11-12 09:35:36 +00003970 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003971 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3972 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003973
Josef Bacik726c35f2011-09-26 15:46:06 -04003974 /*
3975 * Try and steal from the global reserve since we will
3976 * likely not use this space anyway, we want to try as
3977 * hard as possible to get this to work.
3978 */
3979 if (ret)
3980 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3981
Yan, Zhengd68fc572010-05-16 10:49:58 -04003982 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003983 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003984 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003985 btrfs_orphan_del(NULL, inode);
3986 btrfs_free_block_rsv(root, rsv);
3987 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003988 }
3989
Miao Xie0e8c36a2012-12-19 06:59:51 +00003990 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04003991 if (IS_ERR(trans)) {
3992 btrfs_orphan_del(NULL, inode);
3993 btrfs_free_block_rsv(root, rsv);
3994 goto no_delete;
3995 }
3996
3997 trans->block_rsv = rsv;
3998
Yan, Zhengd68fc572010-05-16 10:49:58 -04003999 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004000 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004001 break;
4002
Miao Xie8407aa42012-09-07 01:43:32 -06004003 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004004 btrfs_end_transaction(trans, root);
4005 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004006 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004007 }
4008
Josef Bacik4289a662011-08-05 13:22:24 -04004009 btrfs_free_block_rsv(root, rsv);
4010
Yan, Zheng80825102009-11-12 09:35:36 +00004011 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004012 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004013 ret = btrfs_orphan_del(trans, inode);
4014 BUG_ON(ret);
4015 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004016
Josef Bacik4289a662011-08-05 13:22:24 -04004017 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004018 if (!(root == root->fs_info->tree_root ||
4019 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004020 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004021
Chris Mason54aa1f42007-06-22 14:16:25 -04004022 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004023 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004024no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004025 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004026 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004027}
4028
4029/*
4030 * this returns the key found in the dir entry in the location pointer.
4031 * If no dir entries were found, location->objectid is 0.
4032 */
4033static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4034 struct btrfs_key *location)
4035{
4036 const char *name = dentry->d_name.name;
4037 int namelen = dentry->d_name.len;
4038 struct btrfs_dir_item *di;
4039 struct btrfs_path *path;
4040 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004041 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004042
4043 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004044 if (!path)
4045 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004046
Li Zefan33345d012011-04-20 10:31:50 +08004047 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004048 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004049 if (IS_ERR(di))
4050 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004051
David Sterbac7040052011-04-19 18:00:01 +02004052 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004053 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004054
Chris Mason5f39d392007-10-15 16:14:19 -04004055 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004056out:
Chris Mason39279cc2007-06-12 06:35:45 -04004057 btrfs_free_path(path);
4058 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004059out_err:
4060 location->objectid = 0;
4061 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004062}
4063
4064/*
4065 * when we hit a tree root in a directory, the btrfs part of the inode
4066 * needs to be changed to reflect the root directory of the tree root. This
4067 * is kind of like crossing a mount point.
4068 */
4069static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004070 struct inode *dir,
4071 struct dentry *dentry,
4072 struct btrfs_key *location,
4073 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004074{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004075 struct btrfs_path *path;
4076 struct btrfs_root *new_root;
4077 struct btrfs_root_ref *ref;
4078 struct extent_buffer *leaf;
4079 int ret;
4080 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004081
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082 path = btrfs_alloc_path();
4083 if (!path) {
4084 err = -ENOMEM;
4085 goto out;
4086 }
Chris Mason39279cc2007-06-12 06:35:45 -04004087
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088 err = -ENOENT;
4089 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4090 BTRFS_I(dir)->root->root_key.objectid,
4091 location->objectid);
4092 if (ret) {
4093 if (ret < 0)
4094 err = ret;
4095 goto out;
4096 }
Chris Mason39279cc2007-06-12 06:35:45 -04004097
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004098 leaf = path->nodes[0];
4099 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004100 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4102 goto out;
4103
4104 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4105 (unsigned long)(ref + 1),
4106 dentry->d_name.len);
4107 if (ret)
4108 goto out;
4109
David Sterbab3b4aa72011-04-21 01:20:15 +02004110 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111
4112 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4113 if (IS_ERR(new_root)) {
4114 err = PTR_ERR(new_root);
4115 goto out;
4116 }
4117
4118 if (btrfs_root_refs(&new_root->root_item) == 0) {
4119 err = -ENOENT;
4120 goto out;
4121 }
4122
4123 *sub_root = new_root;
4124 location->objectid = btrfs_root_dirid(&new_root->root_item);
4125 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004126 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127 err = 0;
4128out:
4129 btrfs_free_path(path);
4130 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004131}
4132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004133static void inode_tree_add(struct inode *inode)
4134{
4135 struct btrfs_root *root = BTRFS_I(inode)->root;
4136 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004137 struct rb_node **p;
4138 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004139 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004140again:
4141 p = &root->inode_tree.rb_node;
4142 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004143
Al Viro1d3382c2010-10-23 15:19:20 -04004144 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004145 return;
4146
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004147 spin_lock(&root->inode_lock);
4148 while (*p) {
4149 parent = *p;
4150 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4151
Li Zefan33345d012011-04-20 10:31:50 +08004152 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004153 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004154 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004155 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004156 else {
4157 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004158 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004159 rb_erase(parent, &root->inode_tree);
4160 RB_CLEAR_NODE(parent);
4161 spin_unlock(&root->inode_lock);
4162 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004163 }
4164 }
4165 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4166 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4167 spin_unlock(&root->inode_lock);
4168}
4169
4170static void inode_tree_del(struct inode *inode)
4171{
4172 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004173 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004174
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004175 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004176 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004177 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004178 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004179 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004180 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004181 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004182
Josef Bacik0af3d002010-06-21 14:48:16 -04004183 /*
4184 * Free space cache has inodes in the tree root, but the tree root has a
4185 * root_refs of 0, so this could end up dropping the tree root as a
4186 * snapshot, so we need the extra !root->fs_info->tree_root check to
4187 * make sure we don't drop it.
4188 */
4189 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4190 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004191 synchronize_srcu(&root->fs_info->subvol_srcu);
4192 spin_lock(&root->inode_lock);
4193 empty = RB_EMPTY_ROOT(&root->inode_tree);
4194 spin_unlock(&root->inode_lock);
4195 if (empty)
4196 btrfs_add_dead_root(root);
4197 }
4198}
4199
Jeff Mahoney143bede2012-03-01 14:56:26 +01004200void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004201{
4202 struct rb_node *node;
4203 struct rb_node *prev;
4204 struct btrfs_inode *entry;
4205 struct inode *inode;
4206 u64 objectid = 0;
4207
4208 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4209
4210 spin_lock(&root->inode_lock);
4211again:
4212 node = root->inode_tree.rb_node;
4213 prev = NULL;
4214 while (node) {
4215 prev = node;
4216 entry = rb_entry(node, struct btrfs_inode, rb_node);
4217
Li Zefan33345d012011-04-20 10:31:50 +08004218 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004219 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004220 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004221 node = node->rb_right;
4222 else
4223 break;
4224 }
4225 if (!node) {
4226 while (prev) {
4227 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004228 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004229 node = prev;
4230 break;
4231 }
4232 prev = rb_next(prev);
4233 }
4234 }
4235 while (node) {
4236 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004237 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004238 inode = igrab(&entry->vfs_inode);
4239 if (inode) {
4240 spin_unlock(&root->inode_lock);
4241 if (atomic_read(&inode->i_count) > 1)
4242 d_prune_aliases(inode);
4243 /*
Al Viro45321ac2010-06-07 13:43:19 -04004244 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004245 * the inode cache when its usage count
4246 * hits zero.
4247 */
4248 iput(inode);
4249 cond_resched();
4250 spin_lock(&root->inode_lock);
4251 goto again;
4252 }
4253
4254 if (cond_resched_lock(&root->inode_lock))
4255 goto again;
4256
4257 node = rb_next(node);
4258 }
4259 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004260}
4261
Chris Masone02119d2008-09-05 16:13:11 -04004262static int btrfs_init_locked_inode(struct inode *inode, void *p)
4263{
4264 struct btrfs_iget_args *args = p;
4265 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004266 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004267 return 0;
4268}
4269
4270static int btrfs_find_actor(struct inode *inode, void *opaque)
4271{
4272 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004273 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004274 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004275}
4276
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004277static struct inode *btrfs_iget_locked(struct super_block *s,
4278 u64 objectid,
4279 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004280{
4281 struct inode *inode;
4282 struct btrfs_iget_args args;
4283 args.ino = objectid;
4284 args.root = root;
4285
4286 inode = iget5_locked(s, objectid, btrfs_find_actor,
4287 btrfs_init_locked_inode,
4288 (void *)&args);
4289 return inode;
4290}
4291
Balaji Rao1a54ef82008-07-21 02:01:04 +05304292/* Get an inode object given its location and corresponding root.
4293 * Returns in *is_new if the inode was read from disk
4294 */
4295struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004296 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304297{
4298 struct inode *inode;
4299
4300 inode = btrfs_iget_locked(s, location->objectid, root);
4301 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004302 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304303
4304 if (inode->i_state & I_NEW) {
4305 BTRFS_I(inode)->root = root;
4306 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4307 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004308 if (!is_bad_inode(inode)) {
4309 inode_tree_add(inode);
4310 unlock_new_inode(inode);
4311 if (new)
4312 *new = 1;
4313 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004314 unlock_new_inode(inode);
4315 iput(inode);
4316 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004317 }
4318 }
4319
Balaji Rao1a54ef82008-07-21 02:01:04 +05304320 return inode;
4321}
4322
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004323static struct inode *new_simple_dir(struct super_block *s,
4324 struct btrfs_key *key,
4325 struct btrfs_root *root)
4326{
4327 struct inode *inode = new_inode(s);
4328
4329 if (!inode)
4330 return ERR_PTR(-ENOMEM);
4331
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004332 BTRFS_I(inode)->root = root;
4333 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004334 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004335
4336 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004337 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004338 inode->i_fop = &simple_dir_operations;
4339 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4340 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4341
4342 return inode;
4343}
4344
Chris Mason3de45862008-11-17 21:02:50 -05004345struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004346{
Chris Masond3977122009-01-05 21:25:51 -05004347 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004348 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004349 struct btrfs_root *sub_root = root;
4350 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004351 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004352 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004353
4354 if (dentry->d_name.len > BTRFS_NAME_LEN)
4355 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004356
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004357 if (unlikely(d_need_lookup(dentry))) {
4358 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4359 kfree(dentry->d_fsdata);
4360 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004361 /* This thing is hashed, drop it for now */
4362 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004363 } else {
4364 ret = btrfs_inode_by_name(dir, dentry, &location);
4365 }
Chris Mason5f39d392007-10-15 16:14:19 -04004366
Chris Mason39279cc2007-06-12 06:35:45 -04004367 if (ret < 0)
4368 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004369
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004370 if (location.objectid == 0)
4371 return NULL;
4372
4373 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004374 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004375 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004376 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004377
4378 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4379
Yan, Zheng76dda932009-09-21 16:00:26 -04004380 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004381 ret = fixup_tree_root_location(root, dir, dentry,
4382 &location, &sub_root);
4383 if (ret < 0) {
4384 if (ret != -ENOENT)
4385 inode = ERR_PTR(ret);
4386 else
4387 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4388 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004389 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004390 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004391 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4392
Julia Lawall34d19ba2011-01-24 19:55:19 +00004393 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004394 down_read(&root->fs_info->cleanup_work_sem);
4395 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004396 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004397 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004398 if (ret)
4399 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004400 }
4401
Chris Mason3de45862008-11-17 21:02:50 -05004402 return inode;
4403}
4404
Nick Pigginfe15ce42011-01-07 17:49:23 +11004405static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004406{
4407 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004408 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004409
Li Zefan848cce02012-02-21 17:04:28 +08004410 if (!inode && !IS_ROOT(dentry))
4411 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004412
Li Zefan848cce02012-02-21 17:04:28 +08004413 if (inode) {
4414 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004415 if (btrfs_root_refs(&root->root_item) == 0)
4416 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004417
4418 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4419 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004420 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004421 return 0;
4422}
4423
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004424static void btrfs_dentry_release(struct dentry *dentry)
4425{
4426 if (dentry->d_fsdata)
4427 kfree(dentry->d_fsdata);
4428}
4429
Chris Mason3de45862008-11-17 21:02:50 -05004430static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004431 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004432{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004433 struct dentry *ret;
4434
4435 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4436 if (unlikely(d_need_lookup(dentry))) {
4437 spin_lock(&dentry->d_lock);
4438 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4439 spin_unlock(&dentry->d_lock);
4440 }
4441 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004442}
4443
Miao Xie16cdcec2011-04-22 18:12:22 +08004444unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004445 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4446};
4447
David Woodhousecbdf5a22008-08-06 19:42:33 +01004448static int btrfs_real_readdir(struct file *filp, void *dirent,
4449 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004450{
Chris Mason6da6aba2007-12-18 16:15:09 -05004451 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004452 struct btrfs_root *root = BTRFS_I(inode)->root;
4453 struct btrfs_item *item;
4454 struct btrfs_dir_item *di;
4455 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004456 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004457 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004458 struct list_head ins_list;
4459 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004460 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004461 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004462 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004463 unsigned char d_type;
4464 int over = 0;
4465 u32 di_cur;
4466 u32 di_total;
4467 u32 di_len;
4468 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004469 char tmp_name[32];
4470 char *name_ptr;
4471 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004472 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004473
4474 /* FIXME, use a real flag for deciding about the key type */
4475 if (root->fs_info->tree_root == root)
4476 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004477
Chris Mason39544012007-12-12 14:38:19 -05004478 /* special case for "." */
4479 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004480 over = filldir(dirent, ".", 1,
4481 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004482 if (over)
4483 return 0;
4484 filp->f_pos = 1;
4485 }
Chris Mason39544012007-12-12 14:38:19 -05004486 /* special case for .., just use the back ref */
4487 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004488 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004489 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004490 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004491 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004492 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004493 filp->f_pos = 2;
4494 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004495 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004496 if (!path)
4497 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004498
Josef Bacik026fd312011-05-13 10:32:11 -04004499 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004500
Miao Xie16cdcec2011-04-22 18:12:22 +08004501 if (key_type == BTRFS_DIR_INDEX_KEY) {
4502 INIT_LIST_HEAD(&ins_list);
4503 INIT_LIST_HEAD(&del_list);
4504 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4505 }
4506
Chris Mason39279cc2007-06-12 06:35:45 -04004507 btrfs_set_key_type(&key, key_type);
4508 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004509 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004510
Chris Mason39279cc2007-06-12 06:35:45 -04004511 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4512 if (ret < 0)
4513 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004514
4515 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004516 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004517 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004518 if (slot >= btrfs_header_nritems(leaf)) {
4519 ret = btrfs_next_leaf(root, path);
4520 if (ret < 0)
4521 goto err;
4522 else if (ret > 0)
4523 break;
4524 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004525 }
Chris Mason3de45862008-11-17 21:02:50 -05004526
Chris Mason5f39d392007-10-15 16:14:19 -04004527 item = btrfs_item_nr(leaf, slot);
4528 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4529
4530 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004531 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004532 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004533 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004534 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004535 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004536 if (key_type == BTRFS_DIR_INDEX_KEY &&
4537 btrfs_should_delete_dir_index(&del_list,
4538 found_key.offset))
4539 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004540
4541 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004542 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004543
Chris Mason39279cc2007-06-12 06:35:45 -04004544 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4545 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004546 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004547
4548 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004549 struct btrfs_key location;
4550
Josef Bacik22a94d42011-03-16 16:47:17 -04004551 if (verify_dir_item(root, leaf, di))
4552 break;
4553
Chris Mason5f39d392007-10-15 16:14:19 -04004554 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004555 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004556 name_ptr = tmp_name;
4557 } else {
4558 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004559 if (!name_ptr) {
4560 ret = -ENOMEM;
4561 goto err;
4562 }
Chris Mason5f39d392007-10-15 16:14:19 -04004563 }
4564 read_extent_buffer(leaf, name_ptr,
4565 (unsigned long)(di + 1), name_len);
4566
4567 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4568 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004569
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004570
Chris Mason3de45862008-11-17 21:02:50 -05004571 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004572 * skip it.
4573 *
4574 * In contrast to old kernels, we insert the snapshot's
4575 * dir item and dir index after it has been created, so
4576 * we won't find a reference to our own snapshot. We
4577 * still keep the following code for backward
4578 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004579 */
4580 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4581 location.objectid == root->root_key.objectid) {
4582 over = 0;
4583 goto skip;
4584 }
Chris Mason5f39d392007-10-15 16:14:19 -04004585 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004586 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004587 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004588
Chris Mason3de45862008-11-17 21:02:50 -05004589skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004590 if (name_ptr != tmp_name)
4591 kfree(name_ptr);
4592
Chris Mason39279cc2007-06-12 06:35:45 -04004593 if (over)
4594 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004595 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004596 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004597 di_cur += di_len;
4598 di = (struct btrfs_dir_item *)((char *)di + di_len);
4599 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004600next:
4601 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004602 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004603
Miao Xie16cdcec2011-04-22 18:12:22 +08004604 if (key_type == BTRFS_DIR_INDEX_KEY) {
4605 if (is_curr)
4606 filp->f_pos++;
4607 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4608 &ins_list);
4609 if (ret)
4610 goto nopos;
4611 }
4612
David Woodhouse49593bf2008-08-17 17:08:36 +01004613 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004614 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004615 /*
4616 * 32-bit glibc will use getdents64, but then strtol -
4617 * so the last number we can serve is this.
4618 */
4619 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004620 else
4621 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004622nopos:
4623 ret = 0;
4624err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004625 if (key_type == BTRFS_DIR_INDEX_KEY)
4626 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004628 return ret;
4629}
4630
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004631int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004632{
4633 struct btrfs_root *root = BTRFS_I(inode)->root;
4634 struct btrfs_trans_handle *trans;
4635 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004636 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004637
Josef Bacik72ac3c02012-05-23 14:13:11 -04004638 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004639 return 0;
4640
Liu Bo83eea1f2012-07-10 05:28:39 -06004641 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004642 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004643
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004644 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004645 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004646 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004647 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004648 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004649 if (IS_ERR(trans))
4650 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004651 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004652 }
4653 return ret;
4654}
4655
4656/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004657 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004658 * inode changes. But, it is most likely to find the inode in cache.
4659 * FIXME, needs more benchmarking...there are no reasons other than performance
4660 * to keep or drop this code.
4661 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004662int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004663{
4664 struct btrfs_root *root = BTRFS_I(inode)->root;
4665 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004666 int ret;
4667
Josef Bacik72ac3c02012-05-23 14:13:11 -04004668 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004669 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004670
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004671 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004672 if (IS_ERR(trans))
4673 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004674
4675 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004676 if (ret && ret == -ENOSPC) {
4677 /* whoops, lets try again with the full transaction */
4678 btrfs_end_transaction(trans, root);
4679 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004680 if (IS_ERR(trans))
4681 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004682
Chris Mason94b60442010-05-26 11:02:00 -04004683 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004684 }
Chris Mason39279cc2007-06-12 06:35:45 -04004685 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004686 if (BTRFS_I(inode)->delayed_node)
4687 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004688
4689 return ret;
4690}
4691
4692/*
4693 * This is a copy of file_update_time. We need this so we can return error on
4694 * ENOSPC for updating the inode in the case of file write and mmap writes.
4695 */
Josef Bacike41f9412012-03-26 09:46:47 -04004696static int btrfs_update_time(struct inode *inode, struct timespec *now,
4697 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004698{
Alexander Block2bc5565282012-06-15 09:49:33 +02004699 struct btrfs_root *root = BTRFS_I(inode)->root;
4700
4701 if (btrfs_root_readonly(root))
4702 return -EROFS;
4703
Josef Bacike41f9412012-03-26 09:46:47 -04004704 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004705 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004706 if (flags & S_CTIME)
4707 inode->i_ctime = *now;
4708 if (flags & S_MTIME)
4709 inode->i_mtime = *now;
4710 if (flags & S_ATIME)
4711 inode->i_atime = *now;
4712 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004713}
4714
Chris Masond352ac62008-09-29 15:18:18 -04004715/*
4716 * find the highest existing sequence number in a directory
4717 * and then set the in-memory index_cnt variable to reflect
4718 * free sequence numbers
4719 */
Josef Bacikaec74772008-07-24 12:12:38 -04004720static int btrfs_set_inode_index_count(struct inode *inode)
4721{
4722 struct btrfs_root *root = BTRFS_I(inode)->root;
4723 struct btrfs_key key, found_key;
4724 struct btrfs_path *path;
4725 struct extent_buffer *leaf;
4726 int ret;
4727
Li Zefan33345d012011-04-20 10:31:50 +08004728 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004729 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4730 key.offset = (u64)-1;
4731
4732 path = btrfs_alloc_path();
4733 if (!path)
4734 return -ENOMEM;
4735
4736 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4737 if (ret < 0)
4738 goto out;
4739 /* FIXME: we should be able to handle this */
4740 if (ret == 0)
4741 goto out;
4742 ret = 0;
4743
4744 /*
4745 * MAGIC NUMBER EXPLANATION:
4746 * since we search a directory based on f_pos we have to start at 2
4747 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4748 * else has to start at 2
4749 */
4750 if (path->slots[0] == 0) {
4751 BTRFS_I(inode)->index_cnt = 2;
4752 goto out;
4753 }
4754
4755 path->slots[0]--;
4756
4757 leaf = path->nodes[0];
4758 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4759
Li Zefan33345d012011-04-20 10:31:50 +08004760 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004761 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4762 BTRFS_I(inode)->index_cnt = 2;
4763 goto out;
4764 }
4765
4766 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4767out:
4768 btrfs_free_path(path);
4769 return ret;
4770}
4771
Chris Masond352ac62008-09-29 15:18:18 -04004772/*
4773 * helper to find a free sequence number in a given directory. This current
4774 * code is very simple, later versions will do smarter things in the btree
4775 */
Chris Mason3de45862008-11-17 21:02:50 -05004776int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004777{
4778 int ret = 0;
4779
4780 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004781 ret = btrfs_inode_delayed_dir_index_count(dir);
4782 if (ret) {
4783 ret = btrfs_set_inode_index_count(dir);
4784 if (ret)
4785 return ret;
4786 }
Josef Bacikaec74772008-07-24 12:12:38 -04004787 }
4788
Chris Mason00e4e6b2008-08-05 11:18:09 -04004789 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004790 BTRFS_I(dir)->index_cnt++;
4791
4792 return ret;
4793}
4794
Chris Mason39279cc2007-06-12 06:35:45 -04004795static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4796 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004797 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004798 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004799 u64 ref_objectid, u64 objectid,
4800 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004801{
4802 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004803 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004804 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004805 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004806 struct btrfs_inode_ref *ref;
4807 struct btrfs_key key[2];
4808 u32 sizes[2];
4809 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004810 int ret;
4811 int owner;
4812
Chris Mason5f39d392007-10-15 16:14:19 -04004813 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004814 if (!path)
4815 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004816
Chris Mason39279cc2007-06-12 06:35:45 -04004817 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004818 if (!inode) {
4819 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004820 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004821 }
Chris Mason39279cc2007-06-12 06:35:45 -04004822
Li Zefan581bb052011-04-20 10:06:11 +08004823 /*
4824 * we have to initialize this early, so we can reclaim the inode
4825 * number if we fail afterwards in this function.
4826 */
4827 inode->i_ino = objectid;
4828
Josef Bacikaec74772008-07-24 12:12:38 -04004829 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004830 trace_btrfs_inode_request(dir);
4831
Chris Mason3de45862008-11-17 21:02:50 -05004832 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004833 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004834 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004835 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004836 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004837 }
Josef Bacikaec74772008-07-24 12:12:38 -04004838 }
4839 /*
4840 * index_cnt is ignored for everything but a dir,
4841 * btrfs_get_inode_index_count has an explanation for the magic
4842 * number
4843 */
4844 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004845 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004846 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004847 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004848
Josef Bacik5dc562c2012-08-17 13:14:17 -04004849 /*
4850 * We could have gotten an inode number from somebody who was fsynced
4851 * and then removed in this same transaction, so let's just set full
4852 * sync since it will be a full sync anyway and this will blow away the
4853 * old info in the log.
4854 */
4855 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4856
Al Viro569254b2011-07-24 17:08:40 -04004857 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004858 owner = 0;
4859 else
4860 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004861
4862 key[0].objectid = objectid;
4863 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4864 key[0].offset = 0;
4865
Mark Fashehf1863732012-08-08 11:32:27 -07004866 /*
4867 * Start new inodes with an inode_ref. This is slightly more
4868 * efficient for small numbers of hard links since they will
4869 * be packed into one item. Extended refs will kick in if we
4870 * add more hard links than can fit in the ref item.
4871 */
Chris Mason9c583092008-01-29 15:15:18 -05004872 key[1].objectid = objectid;
4873 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4874 key[1].offset = ref_objectid;
4875
4876 sizes[0] = sizeof(struct btrfs_inode_item);
4877 sizes[1] = name_len + sizeof(*ref);
4878
Chris Masonb9473432009-03-13 11:00:37 -04004879 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004880 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4881 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004882 goto fail;
4883
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004884 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004885 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004886 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004887 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4888 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004889 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4890 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004891 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004892
4893 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4894 struct btrfs_inode_ref);
4895 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004896 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004897 ptr = (unsigned long)(ref + 1);
4898 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4899
Chris Mason5f39d392007-10-15 16:14:19 -04004900 btrfs_mark_buffer_dirty(path->nodes[0]);
4901 btrfs_free_path(path);
4902
Chris Mason39279cc2007-06-12 06:35:45 -04004903 location = &BTRFS_I(inode)->location;
4904 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004905 location->offset = 0;
4906 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4907
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004908 btrfs_inherit_iflags(inode, dir);
4909
Al Viro569254b2011-07-24 17:08:40 -04004910 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004911 if (btrfs_test_opt(root, NODATASUM))
4912 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004913 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004914 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4915 }
4916
Chris Mason39279cc2007-06-12 06:35:45 -04004917 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004918 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004919
4920 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004921 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004922
Alexander Block8ea05e32012-07-25 17:35:53 +02004923 btrfs_update_root_times(trans, root);
4924
Chris Mason39279cc2007-06-12 06:35:45 -04004925 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004926fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004927 if (dir)
4928 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004929 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004930 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004931 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004932}
4933
4934static inline u8 btrfs_inode_type(struct inode *inode)
4935{
4936 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4937}
4938
Chris Masond352ac62008-09-29 15:18:18 -04004939/*
4940 * utility function to add 'inode' into 'parent_inode' with
4941 * a give name and a given sequence number.
4942 * if 'add_backref' is true, also insert a backref from the
4943 * inode to the parent directory.
4944 */
Chris Masone02119d2008-09-05 16:13:11 -04004945int btrfs_add_link(struct btrfs_trans_handle *trans,
4946 struct inode *parent_inode, struct inode *inode,
4947 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004948{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004949 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004950 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004951 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004952 u64 ino = btrfs_ino(inode);
4953 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004954
Li Zefan33345d012011-04-20 10:31:50 +08004955 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004956 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4957 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004958 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004959 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4960 key.offset = 0;
4961 }
Chris Mason39279cc2007-06-12 06:35:45 -04004962
Li Zefan33345d012011-04-20 10:31:50 +08004963 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004964 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4965 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004966 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004967 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004968 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4969 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004970 }
4971
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004972 /* Nothing to clean up yet */
4973 if (ret)
4974 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004975
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004976 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4977 parent_inode, &key,
4978 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004979 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004980 goto fail_dir_item;
4981 else if (ret) {
4982 btrfs_abort_transaction(trans, root, ret);
4983 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004984 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004985
4986 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4987 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004988 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004989 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4990 ret = btrfs_update_inode(trans, root, parent_inode);
4991 if (ret)
4992 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004993 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004994
4995fail_dir_item:
4996 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4997 u64 local_index;
4998 int err;
4999 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5000 key.objectid, root->root_key.objectid,
5001 parent_ino, &local_index, name, name_len);
5002
5003 } else if (add_backref) {
5004 u64 local_index;
5005 int err;
5006
5007 err = btrfs_del_inode_ref(trans, root, name, name_len,
5008 ino, parent_ino, &local_index);
5009 }
5010 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005011}
5012
5013static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005014 struct inode *dir, struct dentry *dentry,
5015 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005016{
Josef Bacika1b075d2010-11-19 20:36:11 +00005017 int err = btrfs_add_link(trans, dir, inode,
5018 dentry->d_name.name, dentry->d_name.len,
5019 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005020 if (err > 0)
5021 err = -EEXIST;
5022 return err;
5023}
5024
Josef Bacik618e21d2007-07-11 10:18:17 -04005025static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005026 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005027{
5028 struct btrfs_trans_handle *trans;
5029 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005030 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005031 int err;
5032 int drop_inode = 0;
5033 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005034 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005035
5036 if (!new_valid_dev(rdev))
5037 return -EINVAL;
5038
Josef Bacik9ed74f22009-09-11 16:12:44 -04005039 /*
5040 * 2 for inode item and ref
5041 * 2 for dir items
5042 * 1 for xattr if selinux is on
5043 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005044 trans = btrfs_start_transaction(root, 5);
5045 if (IS_ERR(trans))
5046 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005047
Li Zefan581bb052011-04-20 10:06:11 +08005048 err = btrfs_find_free_ino(root, &objectid);
5049 if (err)
5050 goto out_unlock;
5051
Josef Bacikaec74772008-07-24 12:12:38 -04005052 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005053 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005054 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005055 if (IS_ERR(inode)) {
5056 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005057 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005058 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005059
Eric Paris2a7dba32011-02-01 11:05:39 -05005060 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005061 if (err) {
5062 drop_inode = 1;
5063 goto out_unlock;
5064 }
5065
Casey Schauflerad19db72011-12-15 10:09:07 -05005066 /*
5067 * If the active LSM wants to access the inode during
5068 * d_instantiate it needs these. Smack checks to see
5069 * if the filesystem supports xattrs by looking at the
5070 * ops vector.
5071 */
5072
5073 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005074 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005075 if (err)
5076 drop_inode = 1;
5077 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005078 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005079 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005080 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005081 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005082out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005083 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005084 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005085 if (drop_inode) {
5086 inode_dec_link_count(inode);
5087 iput(inode);
5088 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005089 return err;
5090}
5091
Chris Mason39279cc2007-06-12 06:35:45 -04005092static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005093 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005094{
5095 struct btrfs_trans_handle *trans;
5096 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005097 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005098 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005099 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005100 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005101 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005102
Josef Bacik9ed74f22009-09-11 16:12:44 -04005103 /*
5104 * 2 for inode item and ref
5105 * 2 for dir items
5106 * 1 for xattr if selinux is on
5107 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005108 trans = btrfs_start_transaction(root, 5);
5109 if (IS_ERR(trans))
5110 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005111
Li Zefan581bb052011-04-20 10:06:11 +08005112 err = btrfs_find_free_ino(root, &objectid);
5113 if (err)
5114 goto out_unlock;
5115
Josef Bacikaec74772008-07-24 12:12:38 -04005116 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005117 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005118 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005119 if (IS_ERR(inode)) {
5120 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005121 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005122 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005123 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005124
Eric Paris2a7dba32011-02-01 11:05:39 -05005125 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005126 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005127 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005128
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005129 err = btrfs_update_inode(trans, root, inode);
5130 if (err)
5131 goto out_unlock;
5132
Casey Schauflerad19db72011-12-15 10:09:07 -05005133 /*
5134 * If the active LSM wants to access the inode during
5135 * d_instantiate it needs these. Smack checks to see
5136 * if the filesystem supports xattrs by looking at the
5137 * ops vector.
5138 */
5139 inode->i_fop = &btrfs_file_operations;
5140 inode->i_op = &btrfs_file_inode_operations;
5141
Josef Bacika1b075d2010-11-19 20:36:11 +00005142 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005143 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005144 goto out_unlock;
5145
5146 inode->i_mapping->a_ops = &btrfs_aops;
5147 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5148 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5149 d_instantiate(dentry, inode);
5150
Chris Mason39279cc2007-06-12 06:35:45 -04005151out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005152 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005153 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005154 inode_dec_link_count(inode);
5155 iput(inode);
5156 }
Liu Bob53d3f52012-11-14 14:34:34 +00005157 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005158 return err;
5159}
5160
5161static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5162 struct dentry *dentry)
5163{
5164 struct btrfs_trans_handle *trans;
5165 struct btrfs_root *root = BTRFS_I(dir)->root;
5166 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005167 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005168 int err;
5169 int drop_inode = 0;
5170
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005171 /* do not allow sys_link's with other subvols of the same device */
5172 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005173 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005174
Mark Fashehf1863732012-08-08 11:32:27 -07005175 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005176 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005177
Chris Mason3de45862008-11-17 21:02:50 -05005178 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005179 if (err)
5180 goto fail;
5181
Yan, Zhenga22285a2010-05-16 10:48:46 -04005182 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005183 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005184 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005185 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005186 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005187 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005188 if (IS_ERR(trans)) {
5189 err = PTR_ERR(trans);
5190 goto fail;
5191 }
Chris Mason5f39d392007-10-15 16:14:19 -04005192
Miao Xie31534952011-04-13 13:19:21 +08005193 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005194 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005195 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005196 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005197 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005198
Josef Bacika1b075d2010-11-19 20:36:11 +00005199 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005200
Yan, Zhenga5719522009-09-24 09:17:31 -04005201 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005202 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005203 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005204 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005205 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005206 if (err)
5207 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005208 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005209 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005210 }
Chris Mason39279cc2007-06-12 06:35:45 -04005211
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005212 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005213fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005214 if (drop_inode) {
5215 inode_dec_link_count(inode);
5216 iput(inode);
5217 }
Liu Bob53d3f52012-11-14 14:34:34 +00005218 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005219 return err;
5220}
5221
Al Viro18bb1db2011-07-26 01:41:39 -04005222static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005223{
Chris Masonb9d86662008-05-02 16:13:49 -04005224 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005225 struct btrfs_trans_handle *trans;
5226 struct btrfs_root *root = BTRFS_I(dir)->root;
5227 int err = 0;
5228 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005229 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005230 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005231
Josef Bacik9ed74f22009-09-11 16:12:44 -04005232 /*
5233 * 2 items for inode and ref
5234 * 2 items for dir items
5235 * 1 for xattr if selinux is on
5236 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005237 trans = btrfs_start_transaction(root, 5);
5238 if (IS_ERR(trans))
5239 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005240
Li Zefan581bb052011-04-20 10:06:11 +08005241 err = btrfs_find_free_ino(root, &objectid);
5242 if (err)
5243 goto out_fail;
5244
Josef Bacikaec74772008-07-24 12:12:38 -04005245 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005246 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005247 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005248 if (IS_ERR(inode)) {
5249 err = PTR_ERR(inode);
5250 goto out_fail;
5251 }
Chris Mason5f39d392007-10-15 16:14:19 -04005252
Chris Mason39279cc2007-06-12 06:35:45 -04005253 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005254
Eric Paris2a7dba32011-02-01 11:05:39 -05005255 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005256 if (err)
5257 goto out_fail;
5258
Chris Mason39279cc2007-06-12 06:35:45 -04005259 inode->i_op = &btrfs_dir_inode_operations;
5260 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005261
Chris Masondbe674a2008-07-17 12:54:05 -04005262 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005263 err = btrfs_update_inode(trans, root, inode);
5264 if (err)
5265 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005266
Josef Bacika1b075d2010-11-19 20:36:11 +00005267 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5268 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005269 if (err)
5270 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005271
Chris Mason39279cc2007-06-12 06:35:45 -04005272 d_instantiate(dentry, inode);
5273 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005274
5275out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005276 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005277 if (drop_on_err)
5278 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005279 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005280 return err;
5281}
5282
Chris Masond352ac62008-09-29 15:18:18 -04005283/* helper for btfs_get_extent. Given an existing extent in the tree,
5284 * and an extent that you want to insert, deal with overlap and insert
5285 * the new extent into the tree.
5286 */
Chris Mason3b951512008-04-17 11:29:12 -04005287static int merge_extent_mapping(struct extent_map_tree *em_tree,
5288 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005289 struct extent_map *em,
5290 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005291{
5292 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005293
Chris Masone6dcd2d2008-07-17 12:53:50 -04005294 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5295 start_diff = map_start - em->start;
5296 em->start = map_start;
5297 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005298 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5299 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005300 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005301 em->block_len -= start_diff;
5302 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005303 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005304}
5305
Chris Masonc8b97812008-10-29 14:49:59 -04005306static noinline int uncompress_inline(struct btrfs_path *path,
5307 struct inode *inode, struct page *page,
5308 size_t pg_offset, u64 extent_offset,
5309 struct btrfs_file_extent_item *item)
5310{
5311 int ret;
5312 struct extent_buffer *leaf = path->nodes[0];
5313 char *tmp;
5314 size_t max_size;
5315 unsigned long inline_size;
5316 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005317 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005318
5319 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005320 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005321 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5322 inline_size = btrfs_file_extent_inline_item_len(leaf,
5323 btrfs_item_nr(leaf, path->slots[0]));
5324 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005325 if (!tmp)
5326 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005327 ptr = btrfs_file_extent_inline_start(item);
5328
5329 read_extent_buffer(leaf, tmp, ptr, inline_size);
5330
Chris Mason5b050f02008-11-11 09:34:41 -05005331 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005332 ret = btrfs_decompress(compress_type, tmp, page,
5333 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005334 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005335 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005336 unsigned long copy_size = min_t(u64,
5337 PAGE_CACHE_SIZE - pg_offset,
5338 max_size - extent_offset);
5339 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005340 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005341 }
5342 kfree(tmp);
5343 return 0;
5344}
5345
Chris Masond352ac62008-09-29 15:18:18 -04005346/*
5347 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005348 * the ugly parts come from merging extents from the disk with the in-ram
5349 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005350 * where the in-ram extents might be locked pending data=ordered completion.
5351 *
5352 * This also copies inline extents directly into the page.
5353 */
Chris Masond3977122009-01-05 21:25:51 -05005354
Chris Masona52d9a82007-08-27 16:49:44 -04005355struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005356 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005357 int create)
5358{
5359 int ret;
5360 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005361 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005362 u64 extent_start = 0;
5363 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005364 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005365 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005366 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005367 struct btrfs_root *root = BTRFS_I(inode)->root;
5368 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005369 struct extent_buffer *leaf;
5370 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005371 struct extent_map *em = NULL;
5372 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005373 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005374 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005375 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005376
Chris Masona52d9a82007-08-27 16:49:44 -04005377again:
Chris Mason890871b2009-09-02 16:24:52 -04005378 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005379 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005380 if (em)
5381 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005382 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005383
Chris Masona52d9a82007-08-27 16:49:44 -04005384 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005385 if (em->start > start || em->start + em->len <= start)
5386 free_extent_map(em);
5387 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005388 free_extent_map(em);
5389 else
5390 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005391 }
David Sterba172ddd62011-04-21 00:48:27 +02005392 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005393 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005394 err = -ENOMEM;
5395 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005396 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005397 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005398 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005399 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005400 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005401 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005402
5403 if (!path) {
5404 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005405 if (!path) {
5406 err = -ENOMEM;
5407 goto out;
5408 }
5409 /*
5410 * Chances are we'll be called again, so go ahead and do
5411 * readahead
5412 */
5413 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005414 }
5415
Chris Mason179e29e2007-11-01 11:28:41 -04005416 ret = btrfs_lookup_file_extent(trans, root, path,
5417 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005418 if (ret < 0) {
5419 err = ret;
5420 goto out;
5421 }
5422
5423 if (ret != 0) {
5424 if (path->slots[0] == 0)
5425 goto not_found;
5426 path->slots[0]--;
5427 }
5428
Chris Mason5f39d392007-10-15 16:14:19 -04005429 leaf = path->nodes[0];
5430 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005431 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005432 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005433 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5434 found_type = btrfs_key_type(&found_key);
5435 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005436 found_type != BTRFS_EXTENT_DATA_KEY) {
5437 goto not_found;
5438 }
5439
Chris Mason5f39d392007-10-15 16:14:19 -04005440 found_type = btrfs_file_extent_type(leaf, item);
5441 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005442 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005443 if (found_type == BTRFS_FILE_EXTENT_REG ||
5444 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005445 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005446 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005447 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5448 size_t size;
5449 size = btrfs_file_extent_inline_len(leaf, item);
5450 extent_end = (extent_start + size + root->sectorsize - 1) &
5451 ~((u64)root->sectorsize - 1);
5452 }
5453
5454 if (start >= extent_end) {
5455 path->slots[0]++;
5456 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5457 ret = btrfs_next_leaf(root, path);
5458 if (ret < 0) {
5459 err = ret;
5460 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005461 }
Yan Zheng9036c102008-10-30 14:19:41 -04005462 if (ret > 0)
5463 goto not_found;
5464 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005465 }
Yan Zheng9036c102008-10-30 14:19:41 -04005466 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5467 if (found_key.objectid != objectid ||
5468 found_key.type != BTRFS_EXTENT_DATA_KEY)
5469 goto not_found;
5470 if (start + len <= found_key.offset)
5471 goto not_found;
5472 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005473 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005474 em->len = found_key.offset - start;
5475 goto not_found_em;
5476 }
5477
Yan Zhengd899e052008-10-30 14:25:28 -04005478 if (found_type == BTRFS_FILE_EXTENT_REG ||
5479 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005480 em->start = extent_start;
5481 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005482 em->orig_start = extent_start -
5483 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005484 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5485 item);
Chris Masondb945352007-10-15 16:15:53 -04005486 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5487 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005488 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005489 goto insert;
5490 }
Li Zefan261507a02010-12-17 14:21:50 +08005491 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005492 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005493 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005494 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005495 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005496 } else {
5497 bytenr += btrfs_file_extent_offset(leaf, item);
5498 em->block_start = bytenr;
5499 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005500 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5501 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005502 }
Chris Masona52d9a82007-08-27 16:49:44 -04005503 goto insert;
5504 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005505 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005506 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005507 size_t size;
5508 size_t extent_offset;
5509 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005510
Chris Masona52d9a82007-08-27 16:49:44 -04005511 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005512 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005513 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005514 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005515 goto out;
5516 }
5517
Yan Zheng9036c102008-10-30 14:19:41 -04005518 size = btrfs_file_extent_inline_len(leaf, item);
5519 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005520 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005521 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005522 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005523 em->len = (copy_size + root->sectorsize - 1) &
5524 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005525 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005526 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005527 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005528 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005529 em->compress_type = compress_type;
5530 }
Yan689f9342007-10-29 11:41:07 -04005531 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005532 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005533 if (btrfs_file_extent_compression(leaf, item) !=
5534 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005535 ret = uncompress_inline(path, inode, page,
5536 pg_offset,
5537 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005538 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005539 } else {
5540 map = kmap(page);
5541 read_extent_buffer(leaf, map + pg_offset, ptr,
5542 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005543 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5544 memset(map + pg_offset + copy_size, 0,
5545 PAGE_CACHE_SIZE - pg_offset -
5546 copy_size);
5547 }
Chris Masonc8b97812008-10-29 14:49:59 -04005548 kunmap(page);
5549 }
Chris Mason179e29e2007-11-01 11:28:41 -04005550 flush_dcache_page(page);
5551 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005552 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005553 if (!trans) {
5554 kunmap(page);
5555 free_extent_map(em);
5556 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005557
David Sterbab3b4aa72011-04-21 01:20:15 +02005558 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005559 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005560
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005561 if (IS_ERR(trans))
5562 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005563 goto again;
5564 }
Chris Masonc8b97812008-10-29 14:49:59 -04005565 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005566 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005567 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005568 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005569 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005570 }
Chris Masond1310b22008-01-24 16:13:08 -05005571 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005572 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005573 goto insert;
5574 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005575 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005576 }
5577not_found:
5578 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005579 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005580 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005581not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005582 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005583 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005584insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005585 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005586 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005587 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5588 "[%llu %llu]\n", (unsigned long long)em->start,
5589 (unsigned long long)em->len,
5590 (unsigned long long)start,
5591 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005592 err = -EIO;
5593 goto out;
5594 }
Chris Masond1310b22008-01-24 16:13:08 -05005595
5596 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005597 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005598 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005599 /* it is possible that someone inserted the extent into the tree
5600 * while we had the lock dropped. It is also possible that
5601 * an overlapping map exists in the tree
5602 */
Chris Masona52d9a82007-08-27 16:49:44 -04005603 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005604 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005605
5606 ret = 0;
5607
Chris Mason3b951512008-04-17 11:29:12 -04005608 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005609 if (existing && (existing->start > start ||
5610 existing->start + existing->len <= start)) {
5611 free_extent_map(existing);
5612 existing = NULL;
5613 }
Chris Mason3b951512008-04-17 11:29:12 -04005614 if (!existing) {
5615 existing = lookup_extent_mapping(em_tree, em->start,
5616 em->len);
5617 if (existing) {
5618 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005619 em, start,
5620 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005621 free_extent_map(existing);
5622 if (err) {
5623 free_extent_map(em);
5624 em = NULL;
5625 }
5626 } else {
5627 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005628 free_extent_map(em);
5629 em = NULL;
5630 }
5631 } else {
5632 free_extent_map(em);
5633 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005634 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005635 }
Chris Masona52d9a82007-08-27 16:49:44 -04005636 }
Chris Mason890871b2009-09-02 16:24:52 -04005637 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005638out:
liubo1abe9b82011-03-24 11:18:59 +00005639
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005640 if (em)
5641 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005642
Chris Masonf4219502008-07-22 11:18:09 -04005643 if (path)
5644 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005645 if (trans) {
5646 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005647 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005648 err = ret;
5649 }
Chris Masona52d9a82007-08-27 16:49:44 -04005650 if (err) {
5651 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005652 return ERR_PTR(err);
5653 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005654 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005655 return em;
5656}
5657
Chris Masonec29ed52011-02-23 16:23:20 -05005658struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5659 size_t pg_offset, u64 start, u64 len,
5660 int create)
5661{
5662 struct extent_map *em;
5663 struct extent_map *hole_em = NULL;
5664 u64 range_start = start;
5665 u64 end;
5666 u64 found;
5667 u64 found_end;
5668 int err = 0;
5669
5670 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5671 if (IS_ERR(em))
5672 return em;
5673 if (em) {
5674 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005675 * if our em maps to
5676 * - a hole or
5677 * - a pre-alloc extent,
5678 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005679 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005680 if (em->block_start != EXTENT_MAP_HOLE &&
5681 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005682 return em;
5683 else
5684 hole_em = em;
5685 }
5686
5687 /* check to see if we've wrapped (len == -1 or similar) */
5688 end = start + len;
5689 if (end < start)
5690 end = (u64)-1;
5691 else
5692 end -= 1;
5693
5694 em = NULL;
5695
5696 /* ok, we didn't find anything, lets look for delalloc */
5697 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5698 end, len, EXTENT_DELALLOC, 1);
5699 found_end = range_start + found;
5700 if (found_end < range_start)
5701 found_end = (u64)-1;
5702
5703 /*
5704 * we didn't find anything useful, return
5705 * the original results from get_extent()
5706 */
5707 if (range_start > end || found_end <= start) {
5708 em = hole_em;
5709 hole_em = NULL;
5710 goto out;
5711 }
5712
5713 /* adjust the range_start to make sure it doesn't
5714 * go backwards from the start they passed in
5715 */
5716 range_start = max(start,range_start);
5717 found = found_end - range_start;
5718
5719 if (found > 0) {
5720 u64 hole_start = start;
5721 u64 hole_len = len;
5722
David Sterba172ddd62011-04-21 00:48:27 +02005723 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005724 if (!em) {
5725 err = -ENOMEM;
5726 goto out;
5727 }
5728 /*
5729 * when btrfs_get_extent can't find anything it
5730 * returns one huge hole
5731 *
5732 * make sure what it found really fits our range, and
5733 * adjust to make sure it is based on the start from
5734 * the caller
5735 */
5736 if (hole_em) {
5737 u64 calc_end = extent_map_end(hole_em);
5738
5739 if (calc_end <= start || (hole_em->start > end)) {
5740 free_extent_map(hole_em);
5741 hole_em = NULL;
5742 } else {
5743 hole_start = max(hole_em->start, start);
5744 hole_len = calc_end - hole_start;
5745 }
5746 }
5747 em->bdev = NULL;
5748 if (hole_em && range_start > hole_start) {
5749 /* our hole starts before our delalloc, so we
5750 * have to return just the parts of the hole
5751 * that go until the delalloc starts
5752 */
5753 em->len = min(hole_len,
5754 range_start - hole_start);
5755 em->start = hole_start;
5756 em->orig_start = hole_start;
5757 /*
5758 * don't adjust block start at all,
5759 * it is fixed at EXTENT_MAP_HOLE
5760 */
5761 em->block_start = hole_em->block_start;
5762 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005763 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5764 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005765 } else {
5766 em->start = range_start;
5767 em->len = found;
5768 em->orig_start = range_start;
5769 em->block_start = EXTENT_MAP_DELALLOC;
5770 em->block_len = found;
5771 }
5772 } else if (hole_em) {
5773 return hole_em;
5774 }
5775out:
5776
5777 free_extent_map(hole_em);
5778 if (err) {
5779 free_extent_map(em);
5780 return ERR_PTR(err);
5781 }
5782 return em;
5783}
5784
Josef Bacik4b46fce2010-05-23 11:00:55 -04005785static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5786 u64 start, u64 len)
5787{
5788 struct btrfs_root *root = BTRFS_I(inode)->root;
5789 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005790 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005791 struct btrfs_key ins;
5792 u64 alloc_hint;
5793 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005794
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005795 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005796 if (IS_ERR(trans))
5797 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005798
5799 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5800
5801 alloc_hint = get_extent_allocation_hint(inode, start, len);
5802 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005803 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005804 if (ret) {
5805 em = ERR_PTR(ret);
5806 goto out;
5807 }
5808
Josef Bacik70c8a912012-10-11 16:54:30 -04005809 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5810 ins.offset, ins.offset, 0);
5811 if (IS_ERR(em))
5812 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005813
5814 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5815 ins.offset, ins.offset, 0);
5816 if (ret) {
5817 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5818 em = ERR_PTR(ret);
5819 }
5820out:
5821 btrfs_end_transaction(trans, root);
5822 return em;
5823}
5824
Chris Mason46bfbb52010-05-26 11:04:10 -04005825/*
5826 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5827 * block must be cow'd
5828 */
5829static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5830 struct inode *inode, u64 offset, u64 len)
5831{
5832 struct btrfs_path *path;
5833 int ret;
5834 struct extent_buffer *leaf;
5835 struct btrfs_root *root = BTRFS_I(inode)->root;
5836 struct btrfs_file_extent_item *fi;
5837 struct btrfs_key key;
5838 u64 disk_bytenr;
5839 u64 backref_offset;
5840 u64 extent_end;
5841 u64 num_bytes;
5842 int slot;
5843 int found_type;
5844
5845 path = btrfs_alloc_path();
5846 if (!path)
5847 return -ENOMEM;
5848
Li Zefan33345d012011-04-20 10:31:50 +08005849 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005850 offset, 0);
5851 if (ret < 0)
5852 goto out;
5853
5854 slot = path->slots[0];
5855 if (ret == 1) {
5856 if (slot == 0) {
5857 /* can't find the item, must cow */
5858 ret = 0;
5859 goto out;
5860 }
5861 slot--;
5862 }
5863 ret = 0;
5864 leaf = path->nodes[0];
5865 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005866 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005867 key.type != BTRFS_EXTENT_DATA_KEY) {
5868 /* not our file or wrong item type, must cow */
5869 goto out;
5870 }
5871
5872 if (key.offset > offset) {
5873 /* Wrong offset, must cow */
5874 goto out;
5875 }
5876
5877 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5878 found_type = btrfs_file_extent_type(leaf, fi);
5879 if (found_type != BTRFS_FILE_EXTENT_REG &&
5880 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5881 /* not a regular extent, must cow */
5882 goto out;
5883 }
5884 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5885 backref_offset = btrfs_file_extent_offset(leaf, fi);
5886
5887 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5888 if (extent_end < offset + len) {
5889 /* extent doesn't include our full range, must cow */
5890 goto out;
5891 }
5892
5893 if (btrfs_extent_readonly(root, disk_bytenr))
5894 goto out;
5895
5896 /*
5897 * look for other files referencing this extent, if we
5898 * find any we must cow
5899 */
Li Zefan33345d012011-04-20 10:31:50 +08005900 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005901 key.offset - backref_offset, disk_bytenr))
5902 goto out;
5903
5904 /*
5905 * adjust disk_bytenr and num_bytes to cover just the bytes
5906 * in this extent we are about to write. If there
5907 * are any csums in that range we have to cow in order
5908 * to keep the csums correct
5909 */
5910 disk_bytenr += backref_offset;
5911 disk_bytenr += offset - key.offset;
5912 num_bytes = min(offset + len, extent_end) - offset;
5913 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5914 goto out;
5915 /*
5916 * all of the above have passed, it is safe to overwrite this extent
5917 * without cow
5918 */
5919 ret = 1;
5920out:
5921 btrfs_free_path(path);
5922 return ret;
5923}
5924
Josef Bacikeb838e72012-07-31 16:28:48 -04005925static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5926 struct extent_state **cached_state, int writing)
5927{
5928 struct btrfs_ordered_extent *ordered;
5929 int ret = 0;
5930
5931 while (1) {
5932 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5933 0, cached_state);
5934 /*
5935 * We're concerned with the entire range that we're going to be
5936 * doing DIO to, so we need to make sure theres no ordered
5937 * extents in this range.
5938 */
5939 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5940 lockend - lockstart + 1);
5941
5942 /*
5943 * We need to make sure there are no buffered pages in this
5944 * range either, we could have raced between the invalidate in
5945 * generic_file_direct_write and locking the extent. The
5946 * invalidate needs to happen so that reads after a write do not
5947 * get stale data.
5948 */
5949 if (!ordered && (!writing ||
5950 !test_range_bit(&BTRFS_I(inode)->io_tree,
5951 lockstart, lockend, EXTENT_UPTODATE, 0,
5952 *cached_state)))
5953 break;
5954
5955 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5956 cached_state, GFP_NOFS);
5957
5958 if (ordered) {
5959 btrfs_start_ordered_extent(inode, ordered, 1);
5960 btrfs_put_ordered_extent(ordered);
5961 } else {
5962 /* Screw you mmap */
5963 ret = filemap_write_and_wait_range(inode->i_mapping,
5964 lockstart,
5965 lockend);
5966 if (ret)
5967 break;
5968
5969 /*
5970 * If we found a page that couldn't be invalidated just
5971 * fall back to buffered.
5972 */
5973 ret = invalidate_inode_pages2_range(inode->i_mapping,
5974 lockstart >> PAGE_CACHE_SHIFT,
5975 lockend >> PAGE_CACHE_SHIFT);
5976 if (ret)
5977 break;
5978 }
5979
5980 cond_resched();
5981 }
5982
5983 return ret;
5984}
5985
Josef Bacik69ffb542012-09-11 15:40:07 -04005986static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5987 u64 len, u64 orig_start,
5988 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005989 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005990{
5991 struct extent_map_tree *em_tree;
5992 struct extent_map *em;
5993 struct btrfs_root *root = BTRFS_I(inode)->root;
5994 int ret;
5995
5996 em_tree = &BTRFS_I(inode)->extent_tree;
5997 em = alloc_extent_map();
5998 if (!em)
5999 return ERR_PTR(-ENOMEM);
6000
6001 em->start = start;
6002 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006003 em->mod_start = start;
6004 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006005 em->len = len;
6006 em->block_len = block_len;
6007 em->block_start = block_start;
6008 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006009 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006010 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006011 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6012 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006013 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006014
6015 do {
6016 btrfs_drop_extent_cache(inode, em->start,
6017 em->start + em->len - 1, 0);
6018 write_lock(&em_tree->lock);
6019 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006020 if (!ret)
6021 list_move(&em->list,
6022 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006023 write_unlock(&em_tree->lock);
6024 } while (ret == -EEXIST);
6025
6026 if (ret) {
6027 free_extent_map(em);
6028 return ERR_PTR(ret);
6029 }
6030
6031 return em;
6032}
6033
6034
Josef Bacik4b46fce2010-05-23 11:00:55 -04006035static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6036 struct buffer_head *bh_result, int create)
6037{
6038 struct extent_map *em;
6039 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006040 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006041 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006042 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006043 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006044 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006045 int unlock_bits = EXTENT_LOCKED;
6046 int ret;
6047
Josef Bacikeb838e72012-07-31 16:28:48 -04006048 if (create) {
6049 ret = btrfs_delalloc_reserve_space(inode, len);
6050 if (ret)
6051 return ret;
6052 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04006053 } else {
6054 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006055 }
6056
Josef Bacikc3298612012-08-03 16:49:19 -04006057 lockstart = start;
6058 lockend = start + len - 1;
6059
Josef Bacikeb838e72012-07-31 16:28:48 -04006060 /*
6061 * If this errors out it's because we couldn't invalidate pagecache for
6062 * this range and we need to fallback to buffered.
6063 */
6064 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6065 return -ENOTBLK;
6066
6067 if (create) {
6068 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6069 lockend, EXTENT_DELALLOC, NULL,
6070 &cached_state, GFP_NOFS);
6071 if (ret)
6072 goto unlock_err;
6073 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006074
6075 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006076 if (IS_ERR(em)) {
6077 ret = PTR_ERR(em);
6078 goto unlock_err;
6079 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006080
6081 /*
6082 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6083 * io. INLINE is special, and we could probably kludge it in here, but
6084 * it's still buffered so for safety lets just fall back to the generic
6085 * buffered path.
6086 *
6087 * For COMPRESSED we _have_ to read the entire extent in so we can
6088 * decompress it, so there will be buffering required no matter what we
6089 * do, so go ahead and fallback to buffered.
6090 *
6091 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6092 * to buffered IO. Don't blame me, this is the price we pay for using
6093 * the generic code.
6094 */
6095 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6096 em->block_start == EXTENT_MAP_INLINE) {
6097 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006098 ret = -ENOTBLK;
6099 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006100 }
6101
6102 /* Just a good old fashioned hole, return */
6103 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6104 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6105 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006106 ret = 0;
6107 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006108 }
6109
6110 /*
6111 * We don't allocate a new extent in the following cases
6112 *
6113 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6114 * existing extent.
6115 * 2) The extent is marked as PREALLOC. We're good to go here and can
6116 * just use the extent.
6117 *
6118 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006119 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006120 len = min(len, em->len - (start - em->start));
6121 lockstart = start + len;
6122 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006123 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006124
6125 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6126 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6127 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006128 int type;
6129 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006130 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006131
6132 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6133 type = BTRFS_ORDERED_PREALLOC;
6134 else
6135 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006136 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006137 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006138
6139 /*
6140 * we're not going to log anything, but we do need
6141 * to make sure the current transaction stays open
6142 * while we look for nocow cross refs
6143 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006144 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006145 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006146 goto must_cow;
6147
6148 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006149 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006150 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006151
6152 if (type == BTRFS_ORDERED_PREALLOC) {
6153 free_extent_map(em);
6154 em = create_pinned_em(inode, start, len,
6155 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006156 block_start, len,
6157 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006158 if (IS_ERR(em)) {
6159 btrfs_end_transaction(trans, root);
6160 goto unlock_err;
6161 }
6162 }
6163
Chris Mason46bfbb52010-05-26 11:04:10 -04006164 ret = btrfs_add_ordered_extent_dio(inode, start,
6165 block_start, len, len, type);
6166 btrfs_end_transaction(trans, root);
6167 if (ret) {
6168 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006169 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006170 }
6171 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006172 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006173 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006174 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006175must_cow:
6176 /*
6177 * this will cow the extent, reset the len in case we changed
6178 * it above
6179 */
6180 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006181 free_extent_map(em);
6182 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006183 if (IS_ERR(em)) {
6184 ret = PTR_ERR(em);
6185 goto unlock_err;
6186 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006187 len = min(len, em->len - (start - em->start));
6188unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006189 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6190 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006191 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006192 bh_result->b_bdev = em->bdev;
6193 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006194 if (create) {
6195 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6196 set_buffer_new(bh_result);
6197
6198 /*
6199 * Need to update the i_size under the extent lock so buffered
6200 * readers will get the updated i_size when we unlock.
6201 */
6202 if (start + len > i_size_read(inode))
6203 i_size_write(inode, start + len);
6204 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006205
Josef Bacikeb838e72012-07-31 16:28:48 -04006206 /*
6207 * In the case of write we need to clear and unlock the entire range,
6208 * in the case of read we need to unlock only the end area that we
6209 * aren't using if there is any left over space.
6210 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006211 if (lockstart < lockend) {
6212 if (create && len < lockend - lockstart) {
6213 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006214 lockstart + len - 1,
6215 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006216 &cached_state, GFP_NOFS);
6217 /*
6218 * Beside unlock, we also need to cleanup reserved space
6219 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6220 */
6221 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6222 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006223 unlock_bits | EXTENT_DO_ACCOUNTING |
6224 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006225 } else {
6226 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6227 lockend, unlock_bits, 1, 0,
6228 &cached_state, GFP_NOFS);
6229 }
6230 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006231 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006232 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006233
Josef Bacik4b46fce2010-05-23 11:00:55 -04006234 free_extent_map(em);
6235
6236 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006237
6238unlock_err:
6239 if (create)
6240 unlock_bits |= EXTENT_DO_ACCOUNTING;
6241
6242 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6243 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6244 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006245}
6246
6247struct btrfs_dio_private {
6248 struct inode *inode;
6249 u64 logical_offset;
6250 u64 disk_bytenr;
6251 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006252 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006253
6254 /* number of bios pending for this dio */
6255 atomic_t pending_bios;
6256
6257 /* IO errors */
6258 int errors;
6259
6260 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006261};
6262
6263static void btrfs_endio_direct_read(struct bio *bio, int err)
6264{
Miao Xiee65e1532010-11-22 03:04:43 +00006265 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006266 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6267 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006268 struct inode *inode = dip->inode;
6269 struct btrfs_root *root = BTRFS_I(inode)->root;
6270 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006271
6272 start = dip->logical_offset;
6273 do {
6274 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6275 struct page *page = bvec->bv_page;
6276 char *kaddr;
6277 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006278 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006279 unsigned long flags;
6280
Josef Bacikc3298612012-08-03 16:49:19 -04006281 if (get_state_private(&BTRFS_I(inode)->io_tree,
6282 start, &private))
6283 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006284 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006285 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006286 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6287 csum, bvec->bv_len);
6288 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006289 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006290 local_irq_restore(flags);
6291
6292 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006293 if (csum != private) {
6294failed:
Li Zefan33345d012011-04-20 10:31:50 +08006295 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006296 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006297 (unsigned long long)btrfs_ino(inode),
6298 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006299 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006300 err = -EIO;
6301 }
6302 }
6303
6304 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006305 bvec++;
6306 } while (bvec <= bvec_end);
6307
6308 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006309 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006310 bio->bi_private = dip->private;
6311
Josef Bacik4b46fce2010-05-23 11:00:55 -04006312 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006313
6314 /* If we had a csum failure make sure to clear the uptodate flag */
6315 if (err)
6316 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006317 dio_end_io(bio, err);
6318}
6319
6320static void btrfs_endio_direct_write(struct bio *bio, int err)
6321{
6322 struct btrfs_dio_private *dip = bio->bi_private;
6323 struct inode *inode = dip->inode;
6324 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006325 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006326 u64 ordered_offset = dip->logical_offset;
6327 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006328 int ret;
6329
6330 if (err)
6331 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006332again:
6333 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6334 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006335 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006336 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006337 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006338
Josef Bacik5fd02042012-05-02 14:00:54 -04006339 ordered->work.func = finish_ordered_fn;
6340 ordered->work.flags = 0;
6341 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6342 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006343out_test:
6344 /*
6345 * our bio might span multiple ordered extents. If we haven't
6346 * completed the accounting for the whole dio, go back and try again
6347 */
6348 if (ordered_offset < dip->logical_offset + dip->bytes) {
6349 ordered_bytes = dip->logical_offset + dip->bytes -
6350 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006351 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006352 goto again;
6353 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006354out_done:
6355 bio->bi_private = dip->private;
6356
Josef Bacik4b46fce2010-05-23 11:00:55 -04006357 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006358
6359 /* If we had an error make sure to clear the uptodate flag */
6360 if (err)
6361 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006362 dio_end_io(bio, err);
6363}
6364
Chris Masoneaf25d92010-05-25 09:48:28 -04006365static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6366 struct bio *bio, int mirror_num,
6367 unsigned long bio_flags, u64 offset)
6368{
6369 int ret;
6370 struct btrfs_root *root = BTRFS_I(inode)->root;
6371 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006372 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006373 return 0;
6374}
6375
Miao Xiee65e1532010-11-22 03:04:43 +00006376static void btrfs_end_dio_bio(struct bio *bio, int err)
6377{
6378 struct btrfs_dio_private *dip = bio->bi_private;
6379
6380 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006381 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006382 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006383 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006384 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006385 dip->errors = 1;
6386
6387 /*
6388 * before atomic variable goto zero, we must make sure
6389 * dip->errors is perceived to be set.
6390 */
6391 smp_mb__before_atomic_dec();
6392 }
6393
6394 /* if there are more bios still pending for this dio, just exit */
6395 if (!atomic_dec_and_test(&dip->pending_bios))
6396 goto out;
6397
6398 if (dip->errors)
6399 bio_io_error(dip->orig_bio);
6400 else {
6401 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6402 bio_endio(dip->orig_bio, 0);
6403 }
6404out:
6405 bio_put(bio);
6406}
6407
6408static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6409 u64 first_sector, gfp_t gfp_flags)
6410{
6411 int nr_vecs = bio_get_nr_vecs(bdev);
6412 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6413}
6414
6415static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6416 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006417 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006418{
6419 int write = rw & REQ_WRITE;
6420 struct btrfs_root *root = BTRFS_I(inode)->root;
6421 int ret;
6422
Josef Bacikb812ce22012-11-16 13:56:32 -05006423 if (async_submit)
6424 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6425
Miao Xiee65e1532010-11-22 03:04:43 +00006426 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006427
6428 if (!write) {
6429 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6430 if (ret)
6431 goto err;
6432 }
Miao Xiee65e1532010-11-22 03:04:43 +00006433
Josef Bacik1ae39932011-04-06 14:41:34 -04006434 if (skip_sum)
6435 goto map;
6436
6437 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006438 ret = btrfs_wq_submit_bio(root->fs_info,
6439 inode, rw, bio, 0, 0,
6440 file_offset,
6441 __btrfs_submit_bio_start_direct_io,
6442 __btrfs_submit_bio_done);
6443 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006444 } else if (write) {
6445 /*
6446 * If we aren't doing async submit, calculate the csum of the
6447 * bio now.
6448 */
6449 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6450 if (ret)
6451 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006452 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006453 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006454 if (ret)
6455 goto err;
6456 }
Miao Xiee65e1532010-11-22 03:04:43 +00006457
Josef Bacik1ae39932011-04-06 14:41:34 -04006458map:
6459 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006460err:
6461 bio_put(bio);
6462 return ret;
6463}
6464
6465static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6466 int skip_sum)
6467{
6468 struct inode *inode = dip->inode;
6469 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006470 struct bio *bio;
6471 struct bio *orig_bio = dip->orig_bio;
6472 struct bio_vec *bvec = orig_bio->bi_io_vec;
6473 u64 start_sector = orig_bio->bi_sector;
6474 u64 file_offset = dip->logical_offset;
6475 u64 submit_len = 0;
6476 u64 map_length;
6477 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006478 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006479 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006480
Miao Xiee65e1532010-11-22 03:04:43 +00006481 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006482 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006483 &map_length, NULL, 0);
6484 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006485 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006486 return -EIO;
6487 }
6488
Josef Bacik02f57c72011-04-06 14:25:44 -04006489 if (map_length >= orig_bio->bi_size) {
6490 bio = orig_bio;
6491 goto submit;
6492 }
6493
Josef Bacik1ae39932011-04-06 14:41:34 -04006494 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006495 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6496 if (!bio)
6497 return -ENOMEM;
6498 bio->bi_private = dip;
6499 bio->bi_end_io = btrfs_end_dio_bio;
6500 atomic_inc(&dip->pending_bios);
6501
Miao Xiee65e1532010-11-22 03:04:43 +00006502 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6503 if (unlikely(map_length < submit_len + bvec->bv_len ||
6504 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6505 bvec->bv_offset) < bvec->bv_len)) {
6506 /*
6507 * inc the count before we submit the bio so
6508 * we know the end IO handler won't happen before
6509 * we inc the count. Otherwise, the dip might get freed
6510 * before we're done setting it up
6511 */
6512 atomic_inc(&dip->pending_bios);
6513 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6514 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006515 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006516 if (ret) {
6517 bio_put(bio);
6518 atomic_dec(&dip->pending_bios);
6519 goto out_err;
6520 }
6521
Miao Xiee65e1532010-11-22 03:04:43 +00006522 start_sector += submit_len >> 9;
6523 file_offset += submit_len;
6524
6525 submit_len = 0;
6526 nr_pages = 0;
6527
6528 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6529 start_sector, GFP_NOFS);
6530 if (!bio)
6531 goto out_err;
6532 bio->bi_private = dip;
6533 bio->bi_end_io = btrfs_end_dio_bio;
6534
6535 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006536 ret = btrfs_map_block(root->fs_info, READ,
6537 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006538 &map_length, NULL, 0);
6539 if (ret) {
6540 bio_put(bio);
6541 goto out_err;
6542 }
6543 } else {
6544 submit_len += bvec->bv_len;
6545 nr_pages ++;
6546 bvec++;
6547 }
6548 }
6549
Josef Bacik02f57c72011-04-06 14:25:44 -04006550submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006551 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006552 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006553 if (!ret)
6554 return 0;
6555
6556 bio_put(bio);
6557out_err:
6558 dip->errors = 1;
6559 /*
6560 * before atomic variable goto zero, we must
6561 * make sure dip->errors is perceived to be set.
6562 */
6563 smp_mb__before_atomic_dec();
6564 if (atomic_dec_and_test(&dip->pending_bios))
6565 bio_io_error(dip->orig_bio);
6566
6567 /* bio_end_io() will handle error, so we needn't return it */
6568 return 0;
6569}
6570
Josef Bacik4b46fce2010-05-23 11:00:55 -04006571static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6572 loff_t file_offset)
6573{
6574 struct btrfs_root *root = BTRFS_I(inode)->root;
6575 struct btrfs_dio_private *dip;
6576 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006577 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006578 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006579 int ret = 0;
6580
6581 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6582
6583 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6584 if (!dip) {
6585 ret = -ENOMEM;
6586 goto free_ordered;
6587 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006588
6589 dip->private = bio->bi_private;
6590 dip->inode = inode;
6591 dip->logical_offset = file_offset;
6592
Josef Bacik4b46fce2010-05-23 11:00:55 -04006593 dip->bytes = 0;
6594 do {
6595 dip->bytes += bvec->bv_len;
6596 bvec++;
6597 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6598
Chris Mason46bfbb52010-05-26 11:04:10 -04006599 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006600 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006601 dip->errors = 0;
6602 dip->orig_bio = bio;
6603 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006604
6605 if (write)
6606 bio->bi_end_io = btrfs_endio_direct_write;
6607 else
6608 bio->bi_end_io = btrfs_endio_direct_read;
6609
Miao Xiee65e1532010-11-22 03:04:43 +00006610 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6611 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006612 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006613free_ordered:
6614 /*
6615 * If this is a write, we need to clean up the reserved space and kill
6616 * the ordered extent.
6617 */
6618 if (write) {
6619 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006620 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006621 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6622 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6623 btrfs_free_reserved_extent(root, ordered->start,
6624 ordered->disk_len);
6625 btrfs_put_ordered_extent(ordered);
6626 btrfs_put_ordered_extent(ordered);
6627 }
6628 bio_endio(bio, ret);
6629}
6630
Chris Mason5a5f79b2010-05-26 21:33:37 -04006631static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6632 const struct iovec *iov, loff_t offset,
6633 unsigned long nr_segs)
6634{
6635 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006636 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006637 size_t size;
6638 unsigned long addr;
6639 unsigned blocksize_mask = root->sectorsize - 1;
6640 ssize_t retval = -EINVAL;
6641 loff_t end = offset;
6642
6643 if (offset & blocksize_mask)
6644 goto out;
6645
6646 /* Check the memory alignment. Blocks cannot straddle pages */
6647 for (seg = 0; seg < nr_segs; seg++) {
6648 addr = (unsigned long)iov[seg].iov_base;
6649 size = iov[seg].iov_len;
6650 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006651 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006652 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006653
6654 /* If this is a write we don't need to check anymore */
6655 if (rw & WRITE)
6656 continue;
6657
6658 /*
6659 * Check to make sure we don't have duplicate iov_base's in this
6660 * iovec, if so return EINVAL, otherwise we'll get csum errors
6661 * when reading back.
6662 */
6663 for (i = seg + 1; i < nr_segs; i++) {
6664 if (iov[seg].iov_base == iov[i].iov_base)
6665 goto out;
6666 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006667 }
6668 retval = 0;
6669out:
6670 return retval;
6671}
Josef Bacikeb838e72012-07-31 16:28:48 -04006672
Chris Mason16432982008-04-10 10:23:21 -04006673static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6674 const struct iovec *iov, loff_t offset,
6675 unsigned long nr_segs)
6676{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006677 struct file *file = iocb->ki_filp;
6678 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006679
Chris Mason5a5f79b2010-05-26 21:33:37 -04006680 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006681 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006682 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006683
Josef Bacikeb838e72012-07-31 16:28:48 -04006684 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006685 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6686 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6687 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006688}
6689
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006690#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6691
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006692static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6693 __u64 start, __u64 len)
6694{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006695 int ret;
6696
6697 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6698 if (ret)
6699 return ret;
6700
Chris Masonec29ed52011-02-23 16:23:20 -05006701 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006702}
6703
Chris Mason9ebefb182007-06-15 13:50:00 -04006704int btrfs_readpage(struct file *file, struct page *page)
6705{
Chris Masond1310b22008-01-24 16:13:08 -05006706 struct extent_io_tree *tree;
6707 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006708 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006709}
Chris Mason1832a6d2007-12-21 16:27:21 -05006710
Chris Mason39279cc2007-06-12 06:35:45 -04006711static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6712{
Chris Masond1310b22008-01-24 16:13:08 -05006713 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006714
6715
6716 if (current->flags & PF_MEMALLOC) {
6717 redirty_page_for_writepage(wbc, page);
6718 unlock_page(page);
6719 return 0;
6720 }
Chris Masond1310b22008-01-24 16:13:08 -05006721 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006722 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6723}
Chris Mason39279cc2007-06-12 06:35:45 -04006724
Chris Masonf4219502008-07-22 11:18:09 -04006725int btrfs_writepages(struct address_space *mapping,
6726 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006727{
Chris Masond1310b22008-01-24 16:13:08 -05006728 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006729
Chris Masond1310b22008-01-24 16:13:08 -05006730 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006731 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6732}
6733
Chris Mason3ab2fb52007-11-08 10:59:22 -05006734static int
6735btrfs_readpages(struct file *file, struct address_space *mapping,
6736 struct list_head *pages, unsigned nr_pages)
6737{
Chris Masond1310b22008-01-24 16:13:08 -05006738 struct extent_io_tree *tree;
6739 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006740 return extent_readpages(tree, mapping, pages, nr_pages,
6741 btrfs_get_extent);
6742}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006743static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006744{
Chris Masond1310b22008-01-24 16:13:08 -05006745 struct extent_io_tree *tree;
6746 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006747 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006748
Chris Masond1310b22008-01-24 16:13:08 -05006749 tree = &BTRFS_I(page->mapping->host)->io_tree;
6750 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006751 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006752 if (ret == 1) {
6753 ClearPagePrivate(page);
6754 set_page_private(page, 0);
6755 page_cache_release(page);
6756 }
6757 return ret;
6758}
Chris Mason39279cc2007-06-12 06:35:45 -04006759
Chris Masone6dcd2d2008-07-17 12:53:50 -04006760static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6761{
Chris Mason98509cf2008-09-11 15:51:43 -04006762 if (PageWriteback(page) || PageDirty(page))
6763 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006764 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006765}
6766
Chris Masona52d9a82007-08-27 16:49:44 -04006767static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6768{
Josef Bacik5fd02042012-05-02 14:00:54 -04006769 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006770 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006771 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006772 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006773 u64 page_start = page_offset(page);
6774 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006775
Chris Mason8b62b722009-09-02 16:53:46 -04006776 /*
6777 * we have the page locked, so new writeback can't start,
6778 * and the dirty bit won't be cleared while we are here.
6779 *
6780 * Wait for IO on this page so that we can safely clear
6781 * the PagePrivate2 bit and do ordered accounting
6782 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006783 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006784
Josef Bacik5fd02042012-05-02 14:00:54 -04006785 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006786 if (offset) {
6787 btrfs_releasepage(page, GFP_NOFS);
6788 return;
6789 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006790 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00006791 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04006792 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006793 /*
6794 * IO on this page will never be started, so we need
6795 * to account for any ordered extents now
6796 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006797 clear_extent_bit(tree, page_start, page_end,
6798 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006799 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6800 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006801 /*
6802 * whoever cleared the private bit is responsible
6803 * for the finish_ordered_io
6804 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006805 if (TestClearPagePrivate2(page) &&
6806 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6807 PAGE_CACHE_SIZE, 1)) {
6808 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006809 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006810 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006811 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006812 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006813 }
6814 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006815 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006816 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6817 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006818 __btrfs_releasepage(page, GFP_NOFS);
6819
Chris Mason4a096752008-07-21 10:29:44 -04006820 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006821 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006822 ClearPagePrivate(page);
6823 set_page_private(page, 0);
6824 page_cache_release(page);
6825 }
Chris Mason39279cc2007-06-12 06:35:45 -04006826}
6827
Chris Mason9ebefb182007-06-15 13:50:00 -04006828/*
6829 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6830 * called from a page fault handler when a page is first dirtied. Hence we must
6831 * be careful to check for EOF conditions here. We set the page up correctly
6832 * for a written page which means we get ENOSPC checking when writing into
6833 * holes and correct delalloc and unwritten extent mapping on filesystems that
6834 * support these features.
6835 *
6836 * We are not allowed to take the i_mutex here so we have to play games to
6837 * protect against truncate races as the page could now be beyond EOF. Because
6838 * vmtruncate() writes the inode size before removing pages, once we have the
6839 * page lock we can determine safely if the page is beyond EOF. If it is not
6840 * beyond EOF, then the page is guaranteed safe against truncation until we
6841 * unlock the page.
6842 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006843int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006844{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006845 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006846 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006847 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006848 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6849 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006850 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006851 char *kaddr;
6852 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006853 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006854 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006855 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006856 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006857 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006858
Jan Karab2b5ef52012-06-12 16:20:45 +02006859 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006860 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006861 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006862 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006863 reserved = 1;
6864 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006865 if (ret) {
6866 if (ret == -ENOMEM)
6867 ret = VM_FAULT_OOM;
6868 else /* -ENOSPC, -EIO, etc */
6869 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006870 if (reserved)
6871 goto out;
6872 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006873 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006874
Nick Piggin56a76f82009-03-31 15:23:23 -07006875 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006876again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006877 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006878 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006879 page_start = page_offset(page);
6880 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006881
Chris Mason9ebefb182007-06-15 13:50:00 -04006882 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006883 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006884 /* page got truncated out from underneath us */
6885 goto out_unlock;
6886 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006887 wait_on_page_writeback(page);
6888
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006889 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006890 set_page_extent_mapped(page);
6891
Chris Masoneb84ae02008-07-17 13:53:27 -04006892 /*
6893 * we can't set the delalloc bits if there are pending ordered
6894 * extents. Drop our locks and wait for them to finish
6895 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006896 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6897 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006898 unlock_extent_cached(io_tree, page_start, page_end,
6899 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006900 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006901 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006902 btrfs_put_ordered_extent(ordered);
6903 goto again;
6904 }
6905
Josef Bacikfbf19082009-10-01 17:10:23 -04006906 /*
6907 * XXX - page_mkwrite gets called every time the page is dirtied, even
6908 * if it was already dirty, so for space accounting reasons we need to
6909 * clear any delalloc bits for the range we are fixing to save. There
6910 * is probably a better way to do this, but for now keep consistent with
6911 * prepare_pages in the normal write path.
6912 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006913 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006914 EXTENT_DIRTY | EXTENT_DELALLOC |
6915 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006916 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006917
Josef Bacik2ac55d42010-02-03 19:33:23 +00006918 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6919 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006920 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006921 unlock_extent_cached(io_tree, page_start, page_end,
6922 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006923 ret = VM_FAULT_SIGBUS;
6924 goto out_unlock;
6925 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006926 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006927
6928 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006929 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006930 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006931 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006932 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006933
Chris Masone6dcd2d2008-07-17 12:53:50 -04006934 if (zero_start != PAGE_CACHE_SIZE) {
6935 kaddr = kmap(page);
6936 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6937 flush_dcache_page(page);
6938 kunmap(page);
6939 }
Chris Mason247e7432008-07-17 12:53:51 -04006940 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006941 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006942 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006943
Chris Mason257c62e2009-10-13 13:21:08 -04006944 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6945 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006946 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006947
Josef Bacik2ac55d42010-02-03 19:33:23 +00006948 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006949
6950out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006951 if (!ret) {
6952 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006953 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006954 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006955 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006956out:
Josef Bacikec39e182012-01-12 19:10:12 -05006957 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006958out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006959 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006960 return ret;
6961}
6962
Josef Bacika41ad392011-01-31 15:30:16 -05006963static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006964{
6965 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006966 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006967 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006968 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006969 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006970 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006971 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006972
Josef Bacik2aaa6652012-08-29 14:27:18 -04006973 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006974 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006975 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006976
Chris Mason4a096752008-07-21 10:29:44 -04006977 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006978 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006979
Josef Bacikfcb80c22011-05-03 10:40:22 -04006980 /*
6981 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6982 * 3 things going on here
6983 *
6984 * 1) We need to reserve space for our orphan item and the space to
6985 * delete our orphan item. Lord knows we don't want to have a dangling
6986 * orphan item because we didn't reserve space to remove it.
6987 *
6988 * 2) We need to reserve space to update our inode.
6989 *
6990 * 3) We need to have something to cache all the space that is going to
6991 * be free'd up by the truncate operation, but also have some slack
6992 * space reserved in case it uses space during the truncate (thank you
6993 * very much snapshotting).
6994 *
6995 * And we need these to all be seperate. The fact is we can use alot of
6996 * space doing the truncate, and we have no earthly idea how much space
6997 * we will use, so we need the truncate reservation to be seperate so it
6998 * doesn't end up using space reserved for updating the inode or
6999 * removing the orphan item. We also need to be able to stop the
7000 * transaction and start a new one, which means we need to be able to
7001 * update the inode several times, and we have no idea of knowing how
7002 * many times that will be, so we can't just reserve 1 item for the
7003 * entirety of the opration, so that has to be done seperately as well.
7004 * Then there is the orphan item, which does indeed need to be held on
7005 * to for the whole operation, and we need nobody to touch this reserved
7006 * space except the orphan code.
7007 *
7008 * So that leaves us with
7009 *
7010 * 1) root->orphan_block_rsv - for the orphan deletion.
7011 * 2) rsv - for the truncate reservation, which we will steal from the
7012 * transaction reservation.
7013 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7014 * updating the inode.
7015 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007016 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007017 if (!rsv)
7018 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007019 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007020 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007021
Josef Bacik907cbce2011-08-08 13:46:15 -04007022 /*
Josef Bacik07127182011-08-19 10:29:59 -04007023 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007024 * 1 for updating the inode.
7025 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007026 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007027 if (IS_ERR(trans)) {
7028 err = PTR_ERR(trans);
7029 goto out;
7030 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007031
Josef Bacik907cbce2011-08-08 13:46:15 -04007032 /* Migrate the slack space for the truncate to our reserve */
7033 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7034 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007035 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007036
Chris Mason5a3f23d2009-03-31 13:27:11 -04007037 /*
7038 * setattr is responsible for setting the ordered_data_close flag,
7039 * but that is only tested during the last file release. That
7040 * could happen well after the next commit, leaving a great big
7041 * window where new writes may get lost if someone chooses to write
7042 * to this file after truncating to zero
7043 *
7044 * The inode doesn't have any dirty data here, and so if we commit
7045 * this is a noop. If someone immediately starts writing to the inode
7046 * it is very likely we'll catch some of their writes in this
7047 * transaction, and the commit will find this file on the ordered
7048 * data list with good things to send down.
7049 *
7050 * This is a best effort solution, there is still a window where
7051 * using truncate to replace the contents of the file will
7052 * end up with a zero length file after a crash.
7053 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007054 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7055 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007056 btrfs_add_ordered_operation(trans, root, inode);
7057
Josef Bacik5dc562c2012-08-17 13:14:17 -04007058 /*
7059 * So if we truncate and then write and fsync we normally would just
7060 * write the extents that changed, which is a problem if we need to
7061 * first truncate that entire inode. So set this flag so we write out
7062 * all of the extents in the inode to the sync log so we're completely
7063 * safe.
7064 */
7065 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007066 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007067
Yan, Zheng80825102009-11-12 09:35:36 +00007068 while (1) {
7069 ret = btrfs_truncate_inode_items(trans, root, inode,
7070 inode->i_size,
7071 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007072 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007073 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007074 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007075 }
Chris Mason39279cc2007-06-12 06:35:45 -04007076
Josef Bacikfcb80c22011-05-03 10:40:22 -04007077 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007078 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007079 if (ret) {
7080 err = ret;
7081 break;
7082 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007083
Yan, Zheng80825102009-11-12 09:35:36 +00007084 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007085 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007086
7087 trans = btrfs_start_transaction(root, 2);
7088 if (IS_ERR(trans)) {
7089 ret = err = PTR_ERR(trans);
7090 trans = NULL;
7091 break;
7092 }
7093
7094 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7095 rsv, min_size);
7096 BUG_ON(ret); /* shouldn't happen */
7097 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007098 }
7099
7100 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007101 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007102 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007103 if (ret)
7104 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007105 }
7106
Chris Mason917c16b2011-11-08 14:49:59 -05007107 if (trans) {
7108 trans->block_rsv = &root->fs_info->trans_block_rsv;
7109 ret = btrfs_update_inode(trans, root, inode);
7110 if (ret && !err)
7111 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007112
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007113 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007114 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007115 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007116
7117out:
7118 btrfs_free_block_rsv(root, rsv);
7119
Josef Bacik3893e332011-01-31 16:03:11 -05007120 if (ret && !err)
7121 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007122
Josef Bacik3893e332011-01-31 16:03:11 -05007123 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007124}
7125
Sven Wegener3b963622008-06-09 21:57:42 -04007126/*
Chris Masond352ac62008-09-29 15:18:18 -04007127 * create a new subvolume directory/inode (helper for the ioctl).
7128 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007129int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007130 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007131{
Chris Mason39279cc2007-06-12 06:35:45 -04007132 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007133 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007134 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007135
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007136 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7137 new_dirid, new_dirid,
7138 S_IFDIR | (~current_umask() & S_IRWXUGO),
7139 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007140 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007141 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007142 inode->i_op = &btrfs_dir_inode_operations;
7143 inode->i_fop = &btrfs_dir_file_operations;
7144
Miklos Szeredibfe86842011-10-28 14:13:29 +02007145 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007146 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007147
Yan, Zheng76dda932009-09-21 16:00:26 -04007148 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007149
Yan, Zheng76dda932009-09-21 16:00:26 -04007150 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007151 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007152}
7153
Chris Mason39279cc2007-06-12 06:35:45 -04007154struct inode *btrfs_alloc_inode(struct super_block *sb)
7155{
7156 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007157 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007158
7159 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7160 if (!ei)
7161 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007162
7163 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007164 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007165 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007166 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007167 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007168 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007169 ei->disk_i_size = 0;
7170 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007171 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007172 ei->index_cnt = (u64)-1;
7173 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007174 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007175
Josef Bacik9e0baf62011-07-15 15:16:44 +00007176 spin_lock_init(&ei->lock);
7177 ei->outstanding_extents = 0;
7178 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007179
Josef Bacik72ac3c02012-05-23 14:13:11 -04007180 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007181 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007182
Miao Xie16cdcec2011-04-22 18:12:22 +08007183 ei->delayed_node = NULL;
7184
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007185 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007186 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007187 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7188 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007189 ei->io_tree.track_uptodate = 1;
7190 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007191 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007192 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007193 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007194 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007195 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007196 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007197 RB_CLEAR_NODE(&ei->rb_node);
7198
7199 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007200}
7201
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007202static void btrfs_i_callback(struct rcu_head *head)
7203{
7204 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007205 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7206}
7207
Chris Mason39279cc2007-06-12 06:35:45 -04007208void btrfs_destroy_inode(struct inode *inode)
7209{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007210 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007211 struct btrfs_root *root = BTRFS_I(inode)->root;
7212
Al Virob3d9b7a2012-06-09 13:51:19 -04007213 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007214 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007215 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7216 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007217 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7218 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007219
Chris Mason5a3f23d2009-03-31 13:27:11 -04007220 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007221 * This can happen where we create an inode, but somebody else also
7222 * created the same inode and we need to destroy the one we already
7223 * created.
7224 */
7225 if (!root)
7226 goto free;
7227
7228 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007229 * Make sure we're properly removed from the ordered operation
7230 * lists.
7231 */
7232 smp_mb();
7233 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7234 spin_lock(&root->fs_info->ordered_extent_lock);
7235 list_del_init(&BTRFS_I(inode)->ordered_operations);
7236 spin_unlock(&root->fs_info->ordered_extent_lock);
7237 }
7238
Josef Bacik8a35d952012-05-23 14:26:42 -04007239 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7240 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007241 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7242 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007243 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007244 }
Josef Bacik7b128762008-07-24 12:17:14 -04007245
Chris Masond3977122009-01-05 21:25:51 -05007246 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007247 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7248 if (!ordered)
7249 break;
7250 else {
Chris Masond3977122009-01-05 21:25:51 -05007251 printk(KERN_ERR "btrfs found ordered "
7252 "extent %llu %llu on inode cleanup\n",
7253 (unsigned long long)ordered->file_offset,
7254 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007255 btrfs_remove_ordered_extent(inode, ordered);
7256 btrfs_put_ordered_extent(ordered);
7257 btrfs_put_ordered_extent(ordered);
7258 }
7259 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007260 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007261 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007262free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007263 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007264 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007265}
7266
Al Viro45321ac2010-06-07 13:43:19 -04007267int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007268{
7269 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007270
Josef Bacik0af3d002010-06-21 14:48:16 -04007271 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007272 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007273 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007274 else
Al Viro45321ac2010-06-07 13:43:19 -04007275 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007276}
7277
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007278static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007279{
7280 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7281
7282 inode_init_once(&ei->vfs_inode);
7283}
7284
7285void btrfs_destroy_cachep(void)
7286{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007287 /*
7288 * Make sure all delayed rcu free inodes are flushed before we
7289 * destroy cache.
7290 */
7291 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007292 if (btrfs_inode_cachep)
7293 kmem_cache_destroy(btrfs_inode_cachep);
7294 if (btrfs_trans_handle_cachep)
7295 kmem_cache_destroy(btrfs_trans_handle_cachep);
7296 if (btrfs_transaction_cachep)
7297 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007298 if (btrfs_path_cachep)
7299 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007300 if (btrfs_free_space_cachep)
7301 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007302 if (btrfs_delalloc_work_cachep)
7303 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007304}
7305
7306int btrfs_init_cachep(void)
7307{
David Sterba837e1972012-09-07 03:00:48 -06007308 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007309 sizeof(struct btrfs_inode), 0,
7310 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007311 if (!btrfs_inode_cachep)
7312 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007313
David Sterba837e1972012-09-07 03:00:48 -06007314 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007315 sizeof(struct btrfs_trans_handle), 0,
7316 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007317 if (!btrfs_trans_handle_cachep)
7318 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007319
David Sterba837e1972012-09-07 03:00:48 -06007320 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007321 sizeof(struct btrfs_transaction), 0,
7322 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007323 if (!btrfs_transaction_cachep)
7324 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007325
David Sterba837e1972012-09-07 03:00:48 -06007326 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007327 sizeof(struct btrfs_path), 0,
7328 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007329 if (!btrfs_path_cachep)
7330 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007331
David Sterba837e1972012-09-07 03:00:48 -06007332 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007333 sizeof(struct btrfs_free_space), 0,
7334 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7335 if (!btrfs_free_space_cachep)
7336 goto fail;
7337
Miao Xie8ccf6f192012-10-25 09:28:04 +00007338 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7339 sizeof(struct btrfs_delalloc_work), 0,
7340 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7341 NULL);
7342 if (!btrfs_delalloc_work_cachep)
7343 goto fail;
7344
Chris Mason39279cc2007-06-12 06:35:45 -04007345 return 0;
7346fail:
7347 btrfs_destroy_cachep();
7348 return -ENOMEM;
7349}
7350
7351static int btrfs_getattr(struct vfsmount *mnt,
7352 struct dentry *dentry, struct kstat *stat)
7353{
Miao Xiedf0af1a52013-01-29 10:11:59 +00007354 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007355 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007356 u32 blocksize = inode->i_sb->s_blocksize;
7357
Chris Mason39279cc2007-06-12 06:35:45 -04007358 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007359 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007360 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00007361
7362 spin_lock(&BTRFS_I(inode)->lock);
7363 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7364 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007365 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00007366 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007367 return 0;
7368}
7369
Liu Bo75e7cb72011-03-22 10:12:20 +00007370/*
7371 * If a file is moved, it will inherit the cow and compression flags of the new
7372 * directory.
7373 */
7374static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7375{
7376 struct btrfs_inode *b_dir = BTRFS_I(dir);
7377 struct btrfs_inode *b_inode = BTRFS_I(inode);
7378
7379 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7380 b_inode->flags |= BTRFS_INODE_NODATACOW;
7381 else
7382 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7383
Liu Bobc178232012-06-14 02:23:18 -06007384 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007385 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007386 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7387 } else {
7388 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7389 BTRFS_INODE_NOCOMPRESS);
7390 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007391}
7392
Chris Masond3977122009-01-05 21:25:51 -05007393static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7394 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007395{
7396 struct btrfs_trans_handle *trans;
7397 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007398 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007399 struct inode *new_inode = new_dentry->d_inode;
7400 struct inode *old_inode = old_dentry->d_inode;
7401 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007402 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007403 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007404 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007405 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007406
Li Zefan33345d012011-04-20 10:31:50 +08007407 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007408 return -EPERM;
7409
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007410 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007411 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007412 return -EXDEV;
7413
Li Zefan33345d012011-04-20 10:31:50 +08007414 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7415 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007416 return -ENOTEMPTY;
7417
Chris Mason39279cc2007-06-12 06:35:45 -04007418 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007419 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007420 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007421
7422
7423 /* check for collisions, even if the name isn't there */
7424 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7425 new_dentry->d_name.name,
7426 new_dentry->d_name.len);
7427
7428 if (ret) {
7429 if (ret == -EEXIST) {
7430 /* we shouldn't get
7431 * eexist without a new_inode */
7432 if (!new_inode) {
7433 WARN_ON(1);
7434 return ret;
7435 }
7436 } else {
7437 /* maybe -EOVERFLOW */
7438 return ret;
7439 }
7440 }
7441 ret = 0;
7442
Chris Mason5a3f23d2009-03-31 13:27:11 -04007443 /*
7444 * we're using rename to replace one file with another.
7445 * and the replacement file is large. Start IO on it now so
7446 * we don't add too much work to the end of the transaction
7447 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007448 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007449 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7450 filemap_flush(old_inode->i_mapping);
7451
Yan, Zheng76dda932009-09-21 16:00:26 -04007452 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007453 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007454 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007455 /*
7456 * We want to reserve the absolute worst case amount of items. So if
7457 * both inodes are subvols and we need to unlink them then that would
7458 * require 4 item modifications, but if they are both normal inodes it
7459 * would require 5 item modifications, so we'll assume their normal
7460 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7461 * should cover the worst case number of items we'll modify.
7462 */
7463 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007464 if (IS_ERR(trans)) {
7465 ret = PTR_ERR(trans);
7466 goto out_notrans;
7467 }
Chris Mason5f39d392007-10-15 16:14:19 -04007468
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007469 if (dest != root)
7470 btrfs_record_root_in_trans(trans, dest);
7471
Yan, Zhenga5719522009-09-24 09:17:31 -04007472 ret = btrfs_set_inode_index(new_dir, &index);
7473 if (ret)
7474 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007475
Li Zefan33345d012011-04-20 10:31:50 +08007476 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007477 /* force full log commit if subvolume involved. */
7478 root->fs_info->last_trans_log_full_commit = trans->transid;
7479 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007480 ret = btrfs_insert_inode_ref(trans, dest,
7481 new_dentry->d_name.name,
7482 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007483 old_ino,
7484 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007485 if (ret)
7486 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007487 /*
7488 * this is an ugly little race, but the rename is required
7489 * to make sure that if we crash, the inode is either at the
7490 * old name or the new one. pinning the log transaction lets
7491 * us make sure we don't allow a log commit to come in after
7492 * we unlink the name but before we add the new name back in.
7493 */
7494 btrfs_pin_log_trans(root);
7495 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007496 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007497 * make sure the inode gets flushed if it is replacing
7498 * something.
7499 */
Li Zefan33345d012011-04-20 10:31:50 +08007500 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007501 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007502
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007503 inode_inc_iversion(old_dir);
7504 inode_inc_iversion(new_dir);
7505 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007506 old_dir->i_ctime = old_dir->i_mtime = ctime;
7507 new_dir->i_ctime = new_dir->i_mtime = ctime;
7508 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007509
Chris Mason12fcfd22009-03-24 10:24:20 -04007510 if (old_dentry->d_parent != new_dentry->d_parent)
7511 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7512
Li Zefan33345d012011-04-20 10:31:50 +08007513 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007514 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7515 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7516 old_dentry->d_name.name,
7517 old_dentry->d_name.len);
7518 } else {
Al Viro92986792011-03-04 17:14:37 +00007519 ret = __btrfs_unlink_inode(trans, root, old_dir,
7520 old_dentry->d_inode,
7521 old_dentry->d_name.name,
7522 old_dentry->d_name.len);
7523 if (!ret)
7524 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007525 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007526 if (ret) {
7527 btrfs_abort_transaction(trans, root, ret);
7528 goto out_fail;
7529 }
Chris Mason39279cc2007-06-12 06:35:45 -04007530
7531 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007532 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007533 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007534 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007535 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7536 root_objectid = BTRFS_I(new_inode)->location.objectid;
7537 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7538 root_objectid,
7539 new_dentry->d_name.name,
7540 new_dentry->d_name.len);
7541 BUG_ON(new_inode->i_nlink == 0);
7542 } else {
7543 ret = btrfs_unlink_inode(trans, dest, new_dir,
7544 new_dentry->d_inode,
7545 new_dentry->d_name.name,
7546 new_dentry->d_name.len);
7547 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007548 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007549 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007550 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007551 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007552 if (ret) {
7553 btrfs_abort_transaction(trans, root, ret);
7554 goto out_fail;
7555 }
Chris Mason39279cc2007-06-12 06:35:45 -04007556 }
Josef Bacikaec74772008-07-24 12:12:38 -04007557
Liu Bo75e7cb72011-03-22 10:12:20 +00007558 fixup_inode_flags(new_dir, old_inode);
7559
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007560 ret = btrfs_add_link(trans, new_dir, old_inode,
7561 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007562 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007563 if (ret) {
7564 btrfs_abort_transaction(trans, root, ret);
7565 goto out_fail;
7566 }
Chris Mason39279cc2007-06-12 06:35:45 -04007567
Li Zefan33345d012011-04-20 10:31:50 +08007568 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007569 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007570 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007571 btrfs_end_log_trans(root);
7572 }
Chris Mason39279cc2007-06-12 06:35:45 -04007573out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007574 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007575out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007576 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007577 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007578
Chris Mason39279cc2007-06-12 06:35:45 -04007579 return ret;
7580}
7581
Miao Xie8ccf6f192012-10-25 09:28:04 +00007582static void btrfs_run_delalloc_work(struct btrfs_work *work)
7583{
7584 struct btrfs_delalloc_work *delalloc_work;
7585
7586 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7587 work);
7588 if (delalloc_work->wait)
7589 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7590 else
7591 filemap_flush(delalloc_work->inode->i_mapping);
7592
7593 if (delalloc_work->delay_iput)
7594 btrfs_add_delayed_iput(delalloc_work->inode);
7595 else
7596 iput(delalloc_work->inode);
7597 complete(&delalloc_work->completion);
7598}
7599
7600struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7601 int wait, int delay_iput)
7602{
7603 struct btrfs_delalloc_work *work;
7604
7605 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7606 if (!work)
7607 return NULL;
7608
7609 init_completion(&work->completion);
7610 INIT_LIST_HEAD(&work->list);
7611 work->inode = inode;
7612 work->wait = wait;
7613 work->delay_iput = delay_iput;
7614 work->work.func = btrfs_run_delalloc_work;
7615
7616 return work;
7617}
7618
7619void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7620{
7621 wait_for_completion(&work->completion);
7622 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7623}
7624
Chris Masond352ac62008-09-29 15:18:18 -04007625/*
7626 * some fairly slow code that needs optimization. This walks the list
7627 * of all the inodes with pending delalloc and forces them to disk.
7628 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007629int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007630{
Chris Masonea8c2812008-08-04 23:17:27 -04007631 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007632 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007633 struct btrfs_delalloc_work *work, *next;
7634 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00007635 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007636 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007637
Yan Zhengc146afa2008-11-12 14:34:12 -05007638 if (root->fs_info->sb->s_flags & MS_RDONLY)
7639 return -EROFS;
7640
Miao Xie8ccf6f192012-10-25 09:28:04 +00007641 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007642 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00007643
Chris Mason75eff682008-12-15 15:54:40 -05007644 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007645 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7646 while (!list_empty(&splice)) {
7647 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04007648 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007649
7650 list_del_init(&binode->delalloc_inodes);
7651
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007652 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00007653 if (!inode) {
7654 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
7655 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007656 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00007657 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007658
7659 list_add_tail(&binode->delalloc_inodes,
7660 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007661 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007662
7663 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7664 if (unlikely(!work)) {
7665 ret = -ENOMEM;
7666 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007667 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007668 list_add_tail(&work->list, &works);
7669 btrfs_queue_worker(&root->fs_info->flush_workers,
7670 &work->work);
7671
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007672 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007673 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007674 }
Chris Mason75eff682008-12-15 15:54:40 -05007675 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007676
Miao Xie1eafa6c2013-01-22 10:49:00 +00007677 list_for_each_entry_safe(work, next, &works, list) {
7678 list_del_init(&work->list);
7679 btrfs_wait_and_free_delalloc_work(work);
7680 }
7681
Chris Mason8c8bee12008-09-29 11:19:10 -04007682 /* the filemap_flush will queue IO into the worker threads, but
7683 * we have to make sure the IO is actually started and that
7684 * ordered extents get created before we return
7685 */
7686 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007687 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007688 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007689 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007690 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7691 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007692 }
7693 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007694 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007695out:
7696 list_for_each_entry_safe(work, next, &works, list) {
7697 list_del_init(&work->list);
7698 btrfs_wait_and_free_delalloc_work(work);
7699 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007700
7701 if (!list_empty_careful(&splice)) {
7702 spin_lock(&root->fs_info->delalloc_lock);
7703 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7704 spin_unlock(&root->fs_info->delalloc_lock);
7705 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00007706 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007707}
7708
Chris Mason39279cc2007-06-12 06:35:45 -04007709static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7710 const char *symname)
7711{
7712 struct btrfs_trans_handle *trans;
7713 struct btrfs_root *root = BTRFS_I(dir)->root;
7714 struct btrfs_path *path;
7715 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007716 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007717 int err;
7718 int drop_inode = 0;
7719 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007720 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007721 int name_len;
7722 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007723 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007724 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007725 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007726
7727 name_len = strlen(symname) + 1;
7728 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7729 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007730
Josef Bacik9ed74f22009-09-11 16:12:44 -04007731 /*
7732 * 2 items for inode item and ref
7733 * 2 items for dir items
7734 * 1 item for xattr if selinux is on
7735 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007736 trans = btrfs_start_transaction(root, 5);
7737 if (IS_ERR(trans))
7738 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007739
Li Zefan581bb052011-04-20 10:06:11 +08007740 err = btrfs_find_free_ino(root, &objectid);
7741 if (err)
7742 goto out_unlock;
7743
Josef Bacikaec74772008-07-24 12:12:38 -04007744 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007745 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007746 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007747 if (IS_ERR(inode)) {
7748 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007749 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007750 }
Chris Mason39279cc2007-06-12 06:35:45 -04007751
Eric Paris2a7dba32011-02-01 11:05:39 -05007752 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007753 if (err) {
7754 drop_inode = 1;
7755 goto out_unlock;
7756 }
7757
Casey Schauflerad19db72011-12-15 10:09:07 -05007758 /*
7759 * If the active LSM wants to access the inode during
7760 * d_instantiate it needs these. Smack checks to see
7761 * if the filesystem supports xattrs by looking at the
7762 * ops vector.
7763 */
7764 inode->i_fop = &btrfs_file_operations;
7765 inode->i_op = &btrfs_file_inode_operations;
7766
Josef Bacika1b075d2010-11-19 20:36:11 +00007767 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007768 if (err)
7769 drop_inode = 1;
7770 else {
7771 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007772 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007773 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007774 }
Chris Mason39279cc2007-06-12 06:35:45 -04007775 if (drop_inode)
7776 goto out_unlock;
7777
7778 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007779 if (!path) {
7780 err = -ENOMEM;
7781 drop_inode = 1;
7782 goto out_unlock;
7783 }
Li Zefan33345d012011-04-20 10:31:50 +08007784 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007785 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007786 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7787 datasize = btrfs_file_extent_calc_inline_size(name_len);
7788 err = btrfs_insert_empty_item(trans, root, path, &key,
7789 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007790 if (err) {
7791 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007792 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007793 goto out_unlock;
7794 }
Chris Mason5f39d392007-10-15 16:14:19 -04007795 leaf = path->nodes[0];
7796 ei = btrfs_item_ptr(leaf, path->slots[0],
7797 struct btrfs_file_extent_item);
7798 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7799 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007800 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007801 btrfs_set_file_extent_encryption(leaf, ei, 0);
7802 btrfs_set_file_extent_compression(leaf, ei, 0);
7803 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7804 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7805
Chris Mason39279cc2007-06-12 06:35:45 -04007806 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007807 write_extent_buffer(leaf, symname, ptr, name_len);
7808 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007809 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007810
Chris Mason39279cc2007-06-12 06:35:45 -04007811 inode->i_op = &btrfs_symlink_inode_operations;
7812 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007813 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007814 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007815 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007816 err = btrfs_update_inode(trans, root, inode);
7817 if (err)
7818 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007819
7820out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007821 if (!err)
7822 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007823 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007824 if (drop_inode) {
7825 inode_dec_link_count(inode);
7826 iput(inode);
7827 }
Liu Bob53d3f52012-11-14 14:34:34 +00007828 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007829 return err;
7830}
Chris Mason16432982008-04-10 10:23:21 -04007831
Josef Bacik0af3d002010-06-21 14:48:16 -04007832static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7833 u64 start, u64 num_bytes, u64 min_size,
7834 loff_t actual_len, u64 *alloc_hint,
7835 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007836{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007837 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7838 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007839 struct btrfs_root *root = BTRFS_I(inode)->root;
7840 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007841 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007842 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007843 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007844 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007845
Josef Bacik0af3d002010-06-21 14:48:16 -04007846 if (trans)
7847 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007848 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007849 if (own_trans) {
7850 trans = btrfs_start_transaction(root, 3);
7851 if (IS_ERR(trans)) {
7852 ret = PTR_ERR(trans);
7853 break;
7854 }
Yan Zhengd899e052008-10-30 14:25:28 -04007855 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007856
Yan, Zhengefa56462010-05-16 10:49:59 -04007857 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007858 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007859 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007860 if (own_trans)
7861 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007862 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007863 }
7864
Yan Zhengd899e052008-10-30 14:25:28 -04007865 ret = insert_reserved_file_extent(trans, inode,
7866 cur_offset, ins.objectid,
7867 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007868 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007869 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007870 if (ret) {
7871 btrfs_abort_transaction(trans, root, ret);
7872 if (own_trans)
7873 btrfs_end_transaction(trans, root);
7874 break;
7875 }
Chris Masona1ed8352009-09-11 12:27:37 -04007876 btrfs_drop_extent_cache(inode, cur_offset,
7877 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007878
Josef Bacik5dc562c2012-08-17 13:14:17 -04007879 em = alloc_extent_map();
7880 if (!em) {
7881 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7882 &BTRFS_I(inode)->runtime_flags);
7883 goto next;
7884 }
7885
7886 em->start = cur_offset;
7887 em->orig_start = cur_offset;
7888 em->len = ins.offset;
7889 em->block_start = ins.objectid;
7890 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007891 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007892 em->bdev = root->fs_info->fs_devices->latest_bdev;
7893 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7894 em->generation = trans->transid;
7895
7896 while (1) {
7897 write_lock(&em_tree->lock);
7898 ret = add_extent_mapping(em_tree, em);
7899 if (!ret)
7900 list_move(&em->list,
7901 &em_tree->modified_extents);
7902 write_unlock(&em_tree->lock);
7903 if (ret != -EEXIST)
7904 break;
7905 btrfs_drop_extent_cache(inode, cur_offset,
7906 cur_offset + ins.offset - 1,
7907 0);
7908 }
7909 free_extent_map(em);
7910next:
Yan Zhengd899e052008-10-30 14:25:28 -04007911 num_bytes -= ins.offset;
7912 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007913 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007914
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007915 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007916 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007917 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007918 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007919 (actual_len > inode->i_size) &&
7920 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007921 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007922 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007923 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007924 i_size = cur_offset;
7925 i_size_write(inode, i_size);
7926 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007927 }
7928
Yan Zhengd899e052008-10-30 14:25:28 -04007929 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007930
7931 if (ret) {
7932 btrfs_abort_transaction(trans, root, ret);
7933 if (own_trans)
7934 btrfs_end_transaction(trans, root);
7935 break;
7936 }
Yan Zhengd899e052008-10-30 14:25:28 -04007937
Josef Bacik0af3d002010-06-21 14:48:16 -04007938 if (own_trans)
7939 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007940 }
Yan Zhengd899e052008-10-30 14:25:28 -04007941 return ret;
7942}
7943
Josef Bacik0af3d002010-06-21 14:48:16 -04007944int btrfs_prealloc_file_range(struct inode *inode, int mode,
7945 u64 start, u64 num_bytes, u64 min_size,
7946 loff_t actual_len, u64 *alloc_hint)
7947{
7948 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7949 min_size, actual_len, alloc_hint,
7950 NULL);
7951}
7952
7953int btrfs_prealloc_file_range_trans(struct inode *inode,
7954 struct btrfs_trans_handle *trans, int mode,
7955 u64 start, u64 num_bytes, u64 min_size,
7956 loff_t actual_len, u64 *alloc_hint)
7957{
7958 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7959 min_size, actual_len, alloc_hint, trans);
7960}
7961
Chris Masone6dcd2d2008-07-17 12:53:50 -04007962static int btrfs_set_page_dirty(struct page *page)
7963{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007964 return __set_page_dirty_nobuffers(page);
7965}
7966
Al Viro10556cb2011-06-20 19:28:19 -04007967static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007968{
Li Zefanb83cc962010-12-20 16:04:08 +08007969 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007970 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007971
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007972 if (mask & MAY_WRITE &&
7973 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7974 if (btrfs_root_readonly(root))
7975 return -EROFS;
7976 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7977 return -EACCES;
7978 }
Al Viro2830ba72011-06-20 19:16:29 -04007979 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007980}
Chris Mason39279cc2007-06-12 06:35:45 -04007981
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007982static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007983 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007984 .lookup = btrfs_lookup,
7985 .create = btrfs_create,
7986 .unlink = btrfs_unlink,
7987 .link = btrfs_link,
7988 .mkdir = btrfs_mkdir,
7989 .rmdir = btrfs_rmdir,
7990 .rename = btrfs_rename,
7991 .symlink = btrfs_symlink,
7992 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007993 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007994 .setxattr = btrfs_setxattr,
7995 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007996 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007997 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007998 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007999 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008000};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008001static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008002 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008003 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008004 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008005};
Yan, Zheng76dda932009-09-21 16:00:26 -04008006
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008007static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008008 .llseek = generic_file_llseek,
8009 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008010 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008011 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008012#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008013 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008014#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008015 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008016 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008017};
8018
Chris Masond1310b22008-01-24 16:13:08 -05008019static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008020 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008021 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008022 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008023 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008024 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008025 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008026 .set_bit_hook = btrfs_set_bit_hook,
8027 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008028 .merge_extent_hook = btrfs_merge_extent_hook,
8029 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008030};
8031
Chris Mason35054392009-01-21 13:11:13 -05008032/*
8033 * btrfs doesn't support the bmap operation because swapfiles
8034 * use bmap to make a mapping of extents in the file. They assume
8035 * these extents won't change over the life of the file and they
8036 * use the bmap result to do IO directly to the drive.
8037 *
8038 * the btrfs bmap call would return logical addresses that aren't
8039 * suitable for IO and they also will change frequently as COW
8040 * operations happen. So, swapfile + btrfs == corruption.
8041 *
8042 * For now we're avoiding this by dropping bmap.
8043 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008044static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008045 .readpage = btrfs_readpage,
8046 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008047 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008048 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008049 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008050 .invalidatepage = btrfs_invalidatepage,
8051 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008052 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008053 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008054};
8055
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008056static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008057 .readpage = btrfs_readpage,
8058 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008059 .invalidatepage = btrfs_invalidatepage,
8060 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008061};
8062
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008063static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008064 .getattr = btrfs_getattr,
8065 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008066 .setxattr = btrfs_setxattr,
8067 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008068 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008069 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008070 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008071 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008072 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008073 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008074};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008075static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008076 .getattr = btrfs_getattr,
8077 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008078 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008079 .setxattr = btrfs_setxattr,
8080 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008081 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008082 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008083 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008084 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008085};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008086static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008087 .readlink = generic_readlink,
8088 .follow_link = page_follow_link_light,
8089 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008090 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008091 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008092 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008093 .setxattr = btrfs_setxattr,
8094 .getxattr = btrfs_getxattr,
8095 .listxattr = btrfs_listxattr,
8096 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008097 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008098 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008099};
Yan, Zheng76dda932009-09-21 16:00:26 -04008100
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008101const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008102 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008103 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008104};