blob: 24c0b7805fe190259d3838faa7c47f0b85642e7f [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
Chris Mason75eff682008-12-15 15:54:40 -05001517 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001518 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xie963d6782013-01-29 10:10:51 +00001519 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1520 root->fs_info->delalloc_batch);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001521 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001522 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1523 &root->fs_info->delalloc_inodes);
1524 }
Chris Mason75eff682008-12-15 15:54:40 -05001525 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001526 }
Chris Mason291d6732008-01-29 15:55:23 -05001527}
1528
Chris Masond352ac62008-09-29 15:18:18 -04001529/*
1530 * extent_io.c clear_bit_hook, see set_bit_hook for why
1531 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001532static void btrfs_clear_bit_hook(struct inode *inode,
1533 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001534{
Chris Mason75eff682008-12-15 15:54:40 -05001535 /*
1536 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001537 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001538 * bit, which is only set or cleared with irqs on
1539 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001540 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001541 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001542 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001543 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001544
Josef Bacik9e0baf62011-07-15 15:16:44 +00001545 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001546 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001547 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1548 spin_lock(&BTRFS_I(inode)->lock);
1549 BTRFS_I(inode)->outstanding_extents--;
1550 spin_unlock(&BTRFS_I(inode)->lock);
1551 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552
1553 if (*bits & EXTENT_DO_ACCOUNTING)
1554 btrfs_delalloc_release_metadata(inode, len);
1555
Josef Bacik0cb59c92010-07-02 12:14:14 -04001556 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1557 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001558 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001559
Chris Mason75eff682008-12-15 15:54:40 -05001560 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie963d6782013-01-29 10:10:51 +00001561 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1562 root->fs_info->delalloc_batch);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001563 BTRFS_I(inode)->delalloc_bytes -= len;
1564
Josef Bacik0cb59c92010-07-02 12:14:14 -04001565 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001566 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1567 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1568 }
Chris Mason75eff682008-12-15 15:54:40 -05001569 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001570 }
Chris Mason291d6732008-01-29 15:55:23 -05001571}
1572
Chris Masond352ac62008-09-29 15:18:18 -04001573/*
1574 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1575 * we don't create bios that span stripes or chunks
1576 */
Chris Mason239b14b2008-03-24 15:02:07 -04001577int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001578 size_t size, struct bio *bio,
1579 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001580{
1581 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001582 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001583 u64 length = 0;
1584 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001585 int ret;
1586
Chris Mason771ed682008-11-06 22:02:51 -05001587 if (bio_flags & EXTENT_BIO_COMPRESSED)
1588 return 0;
1589
Chris Masonf2d8d742008-04-21 10:03:05 -04001590 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001591 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001592 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001593 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001594 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001595 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001596 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001597 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001598 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001599}
1600
Chris Masond352ac62008-09-29 15:18:18 -04001601/*
1602 * in order to insert checksums into the metadata in large chunks,
1603 * we wait until bio submission time. All the pages in the bio are
1604 * checksummed and sums are attached onto the ordered extent record.
1605 *
1606 * At IO completion time the cums attached on the ordered extent record
1607 * are inserted into the btree
1608 */
Chris Masond3977122009-01-05 21:25:51 -05001609static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1610 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001611 unsigned long bio_flags,
1612 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001613{
Chris Mason065631f2008-02-20 12:07:25 -05001614 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001615 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001616
Chris Masond20f7042008-12-08 16:58:54 -05001617 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001618 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001619 return 0;
1620}
Chris Masone0156402008-04-16 11:15:20 -04001621
Chris Mason4a69a412008-11-06 22:03:00 -05001622/*
1623 * in order to insert checksums into the metadata in large chunks,
1624 * we wait until bio submission time. All the pages in the bio are
1625 * checksummed and sums are attached onto the ordered extent record.
1626 *
1627 * At IO completion time the cums attached on the ordered extent record
1628 * are inserted into the btree
1629 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001630static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001631 int mirror_num, unsigned long bio_flags,
1632 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001633{
1634 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001635 int ret;
1636
1637 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1638 if (ret)
1639 bio_endio(bio, ret);
1640 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001641}
1642
Chris Masond352ac62008-09-29 15:18:18 -04001643/*
Chris Masoncad321a2008-12-17 14:51:42 -05001644 * extent_io.c submission hook. This does the right thing for csum calculation
1645 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001646 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001647static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001648 int mirror_num, unsigned long bio_flags,
1649 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001650{
1651 struct btrfs_root *root = BTRFS_I(inode)->root;
1652 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001653 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001654 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001655 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001656
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001657 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001658
Liu Bo83eea1f2012-07-10 05:28:39 -06001659 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001660 metadata = 2;
1661
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001662 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001663 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1664 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001665 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001666
Chris Masond20f7042008-12-08 16:58:54 -05001667 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001668 ret = btrfs_submit_compressed_read(inode, bio,
1669 mirror_num,
1670 bio_flags);
1671 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001672 } else if (!skip_sum) {
1673 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1674 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001675 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001676 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001677 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001678 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001679 /* csum items have already been cloned */
1680 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1681 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001682 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001683 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001684 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001685 bio_flags, bio_offset,
1686 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001687 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001688 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001689 } else if (!skip_sum) {
1690 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1691 if (ret)
1692 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001693 }
1694
Chris Mason0b86a832008-03-24 15:01:56 -04001695mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001696 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1697
1698out:
1699 if (ret < 0)
1700 bio_endio(bio, ret);
1701 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001702}
Chris Mason6885f302008-02-20 16:11:05 -05001703
Chris Masond352ac62008-09-29 15:18:18 -04001704/*
1705 * given a list of ordered sums record them in the inode. This happens
1706 * at IO completion time based on sums calculated at bio submission time.
1707 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001708static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001709 struct inode *inode, u64 file_offset,
1710 struct list_head *list)
1711{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001712 struct btrfs_ordered_sum *sum;
1713
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001714 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001715 btrfs_csum_file_blocks(trans,
1716 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717 }
1718 return 0;
1719}
1720
Josef Bacik2ac55d42010-02-03 19:33:23 +00001721int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1722 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001723{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001724 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001725 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001726 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001727}
1728
Chris Masond352ac62008-09-29 15:18:18 -04001729/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001730struct btrfs_writepage_fixup {
1731 struct page *page;
1732 struct btrfs_work work;
1733};
1734
Christoph Hellwigb2950862008-12-02 09:54:17 -05001735static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001736{
1737 struct btrfs_writepage_fixup *fixup;
1738 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001740 struct page *page;
1741 struct inode *inode;
1742 u64 page_start;
1743 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001744 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001745
1746 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1747 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001748again:
Chris Mason247e7432008-07-17 12:53:51 -04001749 lock_page(page);
1750 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1751 ClearPageChecked(page);
1752 goto out_page;
1753 }
1754
1755 inode = page->mapping->host;
1756 page_start = page_offset(page);
1757 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1758
Josef Bacik2ac55d42010-02-03 19:33:23 +00001759 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001760 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001761
1762 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001763 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001764 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001765
1766 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1767 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001768 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1769 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001770 unlock_page(page);
1771 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001772 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001773 goto again;
1774 }
Chris Mason247e7432008-07-17 12:53:51 -04001775
Jeff Mahoney87826df2012-02-15 16:23:57 +01001776 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1777 if (ret) {
1778 mapping_set_error(page->mapping, ret);
1779 end_extent_writepage(page, ret, page_start, page_end);
1780 ClearPageChecked(page);
1781 goto out;
1782 }
1783
Josef Bacik2ac55d42010-02-03 19:33:23 +00001784 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001785 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001786 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001787out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001788 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1789 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001790out_page:
1791 unlock_page(page);
1792 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001793 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001794}
1795
1796/*
1797 * There are a few paths in the higher layers of the kernel that directly
1798 * set the page dirty bit without asking the filesystem if it is a
1799 * good idea. This causes problems because we want to make sure COW
1800 * properly happens and the data=ordered rules are followed.
1801 *
Chris Masonc8b97812008-10-29 14:49:59 -04001802 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001803 * hasn't been properly setup for IO. We kick off an async process
1804 * to fix it up. The async helper will wait for ordered extents, set
1805 * the delalloc bit and make it safe to write the page.
1806 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001807static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001808{
1809 struct inode *inode = page->mapping->host;
1810 struct btrfs_writepage_fixup *fixup;
1811 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001812
Chris Mason8b62b722009-09-02 16:53:46 -04001813 /* this page is properly in the ordered list */
1814 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001815 return 0;
1816
1817 if (PageChecked(page))
1818 return -EAGAIN;
1819
1820 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1821 if (!fixup)
1822 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001823
Chris Mason247e7432008-07-17 12:53:51 -04001824 SetPageChecked(page);
1825 page_cache_get(page);
1826 fixup->work.func = btrfs_writepage_fixup_worker;
1827 fixup->page = page;
1828 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001829 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001830}
1831
Yan Zhengd899e052008-10-30 14:25:28 -04001832static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1833 struct inode *inode, u64 file_pos,
1834 u64 disk_bytenr, u64 disk_num_bytes,
1835 u64 num_bytes, u64 ram_bytes,
1836 u8 compression, u8 encryption,
1837 u16 other_encoding, int extent_type)
1838{
1839 struct btrfs_root *root = BTRFS_I(inode)->root;
1840 struct btrfs_file_extent_item *fi;
1841 struct btrfs_path *path;
1842 struct extent_buffer *leaf;
1843 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001844 int ret;
1845
1846 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001847 if (!path)
1848 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001849
Chris Masonb9473432009-03-13 11:00:37 -04001850 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001851
1852 /*
1853 * we may be replacing one extent in the tree with another.
1854 * The new extent is pinned in the extent map, and we don't want
1855 * to drop it from the cache until it is completely in the btree.
1856 *
1857 * So, tell btrfs_drop_extents to leave this extent in the cache.
1858 * the caller is expected to unpin it and allow it to be merged
1859 * with the others.
1860 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001861 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001862 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001863 if (ret)
1864 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001865
Li Zefan33345d012011-04-20 10:31:50 +08001866 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001867 ins.offset = file_pos;
1868 ins.type = BTRFS_EXTENT_DATA_KEY;
1869 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001870 if (ret)
1871 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001872 leaf = path->nodes[0];
1873 fi = btrfs_item_ptr(leaf, path->slots[0],
1874 struct btrfs_file_extent_item);
1875 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1876 btrfs_set_file_extent_type(leaf, fi, extent_type);
1877 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1878 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1879 btrfs_set_file_extent_offset(leaf, fi, 0);
1880 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1881 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1882 btrfs_set_file_extent_compression(leaf, fi, compression);
1883 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1884 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001885
Yan Zhengd899e052008-10-30 14:25:28 -04001886 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001887 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001888
1889 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001890
1891 ins.objectid = disk_bytenr;
1892 ins.offset = disk_num_bytes;
1893 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 ret = btrfs_alloc_reserved_file_extent(trans, root,
1895 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001896 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001897out:
Yan Zhengd899e052008-10-30 14:25:28 -04001898 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001899
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001900 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001901}
1902
Chris Mason5d13a982009-03-13 11:41:46 -04001903/*
1904 * helper function for btrfs_finish_ordered_io, this
1905 * just reads in some of the csum leaves to prime them into ram
1906 * before we start the transaction. It limits the amount of btree
1907 * reads required while inside the transaction.
1908 */
Chris Masond352ac62008-09-29 15:18:18 -04001909/* as ordered data IO finishes, this gets called so we can finish
1910 * an ordered extent if the range of bytes in the file it covers are
1911 * fully written.
1912 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001913static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001914{
Josef Bacik5fd02042012-05-02 14:00:54 -04001915 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001916 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001917 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001918 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001919 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001920 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001921 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001922 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001923
Liu Bo83eea1f2012-07-10 05:28:39 -06001924 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001925
Josef Bacik5fd02042012-05-02 14:00:54 -04001926 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1927 ret = -EIO;
1928 goto out;
1929 }
1930
Yan, Zhengc21677542009-11-12 09:34:21 +00001931 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001932 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001933 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1934 if (nolock)
1935 trans = btrfs_join_transaction_nolock(root);
1936 else
1937 trans = btrfs_join_transaction(root);
1938 if (IS_ERR(trans)) {
1939 ret = PTR_ERR(trans);
1940 trans = NULL;
1941 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00001942 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001943 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1944 ret = btrfs_update_inode_fallback(trans, root, inode);
1945 if (ret) /* -ENOMEM or corruption */
1946 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001947 goto out;
1948 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001949
Josef Bacik2ac55d42010-02-03 19:33:23 +00001950 lock_extent_bits(io_tree, ordered_extent->file_offset,
1951 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001952 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001953
Josef Bacik0cb59c92010-07-02 12:14:14 -04001954 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001955 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001956 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001957 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001958 if (IS_ERR(trans)) {
1959 ret = PTR_ERR(trans);
1960 trans = NULL;
1961 goto out_unlock;
1962 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001963 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001964
Chris Masonc8b97812008-10-29 14:49:59 -04001965 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001966 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001967 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001968 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001969 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001970 ordered_extent->file_offset,
1971 ordered_extent->file_offset +
1972 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001973 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001974 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001975 ret = insert_reserved_file_extent(trans, inode,
1976 ordered_extent->file_offset,
1977 ordered_extent->start,
1978 ordered_extent->disk_len,
1979 ordered_extent->len,
1980 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001981 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001982 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001983 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001984 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1985 ordered_extent->file_offset, ordered_extent->len,
1986 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001987 if (ret < 0) {
1988 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001989 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001990 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001991
Chris Masone6dcd2d2008-07-17 12:53:50 -04001992 add_pending_csums(trans, inode, ordered_extent->file_offset,
1993 &ordered_extent->list);
1994
Josef Bacik6c760c02012-11-09 10:53:21 -05001995 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1996 ret = btrfs_update_inode_fallback(trans, root, inode);
1997 if (ret) { /* -ENOMEM or corruption */
1998 btrfs_abort_transaction(trans, root, ret);
1999 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002000 }
2001 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002002out_unlock:
2003 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2004 ordered_extent->file_offset +
2005 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002006out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002007 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002008 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002009 if (trans)
2010 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002011
Josef Bacik5fd02042012-05-02 14:00:54 -04002012 if (ret)
2013 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2014 ordered_extent->file_offset +
2015 ordered_extent->len - 1, NULL, GFP_NOFS);
2016
2017 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002018 * This needs to be done to make sure anybody waiting knows we are done
2019 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002020 */
2021 btrfs_remove_ordered_extent(inode, ordered_extent);
2022
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 /* once for us */
2024 btrfs_put_ordered_extent(ordered_extent);
2025 /* once for the tree */
2026 btrfs_put_ordered_extent(ordered_extent);
2027
Josef Bacik5fd02042012-05-02 14:00:54 -04002028 return ret;
2029}
2030
2031static void finish_ordered_fn(struct btrfs_work *work)
2032{
2033 struct btrfs_ordered_extent *ordered_extent;
2034 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2035 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002036}
2037
Christoph Hellwigb2950862008-12-02 09:54:17 -05002038static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002039 struct extent_state *state, int uptodate)
2040{
Josef Bacik5fd02042012-05-02 14:00:54 -04002041 struct inode *inode = page->mapping->host;
2042 struct btrfs_root *root = BTRFS_I(inode)->root;
2043 struct btrfs_ordered_extent *ordered_extent = NULL;
2044 struct btrfs_workers *workers;
2045
liubo1abe9b82011-03-24 11:18:59 +00002046 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2047
Chris Mason8b62b722009-09-02 16:53:46 -04002048 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002049 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2050 end - start + 1, uptodate))
2051 return 0;
2052
2053 ordered_extent->work.func = finish_ordered_fn;
2054 ordered_extent->work.flags = 0;
2055
Liu Bo83eea1f2012-07-10 05:28:39 -06002056 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002057 workers = &root->fs_info->endio_freespace_worker;
2058 else
2059 workers = &root->fs_info->endio_write_workers;
2060 btrfs_queue_worker(workers, &ordered_extent->work);
2061
2062 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002063}
2064
Chris Masond352ac62008-09-29 15:18:18 -04002065/*
Chris Masond352ac62008-09-29 15:18:18 -04002066 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002067 * if there's a match, we allow the bio to finish. If not, the code in
2068 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002069 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002070static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002071 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002072{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002073 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002074 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002075 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002076 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002077 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002078 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002079 struct btrfs_root *root = BTRFS_I(inode)->root;
2080 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002081
Chris Masond20f7042008-12-08 16:58:54 -05002082 if (PageChecked(page)) {
2083 ClearPageChecked(page);
2084 goto good;
2085 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002086
2087 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002088 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002089
Yan Zheng17d217f2008-12-12 10:03:38 -05002090 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002091 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002092 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2093 GFP_NOFS);
2094 return 0;
2095 }
2096
Yanc2e639f2008-02-04 08:57:25 -05002097 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002098 private = state->private;
2099 ret = 0;
2100 } else {
2101 ret = get_state_private(io_tree, start, &private);
2102 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002103 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002104 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002105 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002106
Chris Masonff79f812007-10-15 16:22:25 -04002107 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2108 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002109 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002110 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002111
Cong Wang7ac687d2011-11-25 23:14:28 +08002112 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002113good:
Chris Mason07157aa2007-08-30 08:50:51 -04002114 return 0;
2115
2116zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002117 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002118 "private %llu\n",
2119 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002120 (unsigned long long)start, csum,
2121 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002122 memset(kaddr + offset, 1, end - start + 1);
2123 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002124 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002125 if (private == 0)
2126 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002127 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002128}
Chris Masonb888db22007-08-27 16:49:44 -04002129
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002130struct delayed_iput {
2131 struct list_head list;
2132 struct inode *inode;
2133};
2134
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002135/* JDM: If this is fs-wide, why can't we add a pointer to
2136 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002137void btrfs_add_delayed_iput(struct inode *inode)
2138{
2139 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2140 struct delayed_iput *delayed;
2141
2142 if (atomic_add_unless(&inode->i_count, -1, 1))
2143 return;
2144
2145 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2146 delayed->inode = inode;
2147
2148 spin_lock(&fs_info->delayed_iput_lock);
2149 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2150 spin_unlock(&fs_info->delayed_iput_lock);
2151}
2152
2153void btrfs_run_delayed_iputs(struct btrfs_root *root)
2154{
2155 LIST_HEAD(list);
2156 struct btrfs_fs_info *fs_info = root->fs_info;
2157 struct delayed_iput *delayed;
2158 int empty;
2159
2160 spin_lock(&fs_info->delayed_iput_lock);
2161 empty = list_empty(&fs_info->delayed_iputs);
2162 spin_unlock(&fs_info->delayed_iput_lock);
2163 if (empty)
2164 return;
2165
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002166 spin_lock(&fs_info->delayed_iput_lock);
2167 list_splice_init(&fs_info->delayed_iputs, &list);
2168 spin_unlock(&fs_info->delayed_iput_lock);
2169
2170 while (!list_empty(&list)) {
2171 delayed = list_entry(list.next, struct delayed_iput, list);
2172 list_del(&delayed->list);
2173 iput(delayed->inode);
2174 kfree(delayed);
2175 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002176}
2177
Yan, Zhengd68fc572010-05-16 10:49:58 -04002178enum btrfs_orphan_cleanup_state {
2179 ORPHAN_CLEANUP_STARTED = 1,
2180 ORPHAN_CLEANUP_DONE = 2,
2181};
2182
2183/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002184 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002185 * files in the subvolume, it removes orphan item and frees block_rsv
2186 * structure.
2187 */
2188void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2189 struct btrfs_root *root)
2190{
Josef Bacik90290e12011-12-02 15:44:12 -05002191 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002192 int ret;
2193
Josef Bacik8a35d952012-05-23 14:26:42 -04002194 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002195 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2196 return;
2197
Josef Bacik90290e12011-12-02 15:44:12 -05002198 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002199 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002200 spin_unlock(&root->orphan_lock);
2201 return;
2202 }
2203
2204 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2205 spin_unlock(&root->orphan_lock);
2206 return;
2207 }
2208
2209 block_rsv = root->orphan_block_rsv;
2210 root->orphan_block_rsv = NULL;
2211 spin_unlock(&root->orphan_lock);
2212
Yan, Zhengd68fc572010-05-16 10:49:58 -04002213 if (root->orphan_item_inserted &&
2214 btrfs_root_refs(&root->root_item) > 0) {
2215 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2216 root->root_key.objectid);
2217 BUG_ON(ret);
2218 root->orphan_item_inserted = 0;
2219 }
2220
Josef Bacik90290e12011-12-02 15:44:12 -05002221 if (block_rsv) {
2222 WARN_ON(block_rsv->size > 0);
2223 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002224 }
2225}
2226
2227/*
Josef Bacik7b128762008-07-24 12:17:14 -04002228 * This creates an orphan entry for the given inode in case something goes
2229 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002230 *
2231 * NOTE: caller of this function should reserve 5 units of metadata for
2232 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002233 */
2234int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2235{
2236 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 struct btrfs_block_rsv *block_rsv = NULL;
2238 int reserve = 0;
2239 int insert = 0;
2240 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002241
Yan, Zhengd68fc572010-05-16 10:49:58 -04002242 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002243 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002244 if (!block_rsv)
2245 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002246 }
2247
Yan, Zhengd68fc572010-05-16 10:49:58 -04002248 spin_lock(&root->orphan_lock);
2249 if (!root->orphan_block_rsv) {
2250 root->orphan_block_rsv = block_rsv;
2251 } else if (block_rsv) {
2252 btrfs_free_block_rsv(root, block_rsv);
2253 block_rsv = NULL;
2254 }
Josef Bacik7b128762008-07-24 12:17:14 -04002255
Josef Bacik8a35d952012-05-23 14:26:42 -04002256 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2257 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002258#if 0
2259 /*
2260 * For proper ENOSPC handling, we should do orphan
2261 * cleanup when mounting. But this introduces backward
2262 * compatibility issue.
2263 */
2264 if (!xchg(&root->orphan_item_inserted, 1))
2265 insert = 2;
2266 else
2267 insert = 1;
2268#endif
2269 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002270 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002271 }
Josef Bacik7b128762008-07-24 12:17:14 -04002272
Josef Bacik72ac3c02012-05-23 14:13:11 -04002273 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2274 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002275 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002276 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002277
Yan, Zhengd68fc572010-05-16 10:49:58 -04002278 /* grab metadata reservation from transaction handle */
2279 if (reserve) {
2280 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002281 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002282 }
2283
2284 /* insert an orphan item to track this unlinked/truncated file */
2285 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002286 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002287 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002288 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2289 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002290 btrfs_abort_transaction(trans, root, ret);
2291 return ret;
2292 }
2293 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002294 }
2295
2296 /* insert an orphan item to track subvolume contains orphan files */
2297 if (insert >= 2) {
2298 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2299 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002300 if (ret && ret != -EEXIST) {
2301 btrfs_abort_transaction(trans, root, ret);
2302 return ret;
2303 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002304 }
2305 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002306}
2307
2308/*
2309 * We have done the truncate/delete so we can go ahead and remove the orphan
2310 * item for this particular inode.
2311 */
2312int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2313{
2314 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002315 int delete_item = 0;
2316 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002317 int ret = 0;
2318
Yan, Zhengd68fc572010-05-16 10:49:58 -04002319 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002320 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2321 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002322 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002323
Josef Bacik72ac3c02012-05-23 14:13:11 -04002324 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2325 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002326 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002327 spin_unlock(&root->orphan_lock);
2328
2329 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002330 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002331 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002332 }
2333
Josef Bacik8a35d952012-05-23 14:26:42 -04002334 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002335 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002336 atomic_dec(&root->orphan_inodes);
2337 }
Josef Bacik7b128762008-07-24 12:17:14 -04002338
Yan, Zhengd68fc572010-05-16 10:49:58 -04002339 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002340}
2341
2342/*
2343 * this cleans up any orphans that may be left on the list from the last use
2344 * of this root.
2345 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002346int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002347{
2348 struct btrfs_path *path;
2349 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002350 struct btrfs_key key, found_key;
2351 struct btrfs_trans_handle *trans;
2352 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002353 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002354 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2355
Yan, Zhengd68fc572010-05-16 10:49:58 -04002356 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002357 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002358
2359 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002360 if (!path) {
2361 ret = -ENOMEM;
2362 goto out;
2363 }
Josef Bacik7b128762008-07-24 12:17:14 -04002364 path->reada = -1;
2365
2366 key.objectid = BTRFS_ORPHAN_OBJECTID;
2367 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2368 key.offset = (u64)-1;
2369
Josef Bacik7b128762008-07-24 12:17:14 -04002370 while (1) {
2371 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002372 if (ret < 0)
2373 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002374
2375 /*
2376 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002377 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002378 * find the key and see if we have stuff that matches
2379 */
2380 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002381 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002382 if (path->slots[0] == 0)
2383 break;
2384 path->slots[0]--;
2385 }
2386
2387 /* pull out the item */
2388 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002389 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2390
2391 /* make sure the item matches what we want */
2392 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2393 break;
2394 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2395 break;
2396
2397 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002398 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002399
2400 /*
2401 * this is where we are basically btrfs_lookup, without the
2402 * crossing root thing. we store the inode number in the
2403 * offset of the orphan item.
2404 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002405
2406 if (found_key.offset == last_objectid) {
2407 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2408 "stopping orphan cleanup\n");
2409 ret = -EINVAL;
2410 goto out;
2411 }
2412
2413 last_objectid = found_key.offset;
2414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415 found_key.objectid = found_key.offset;
2416 found_key.type = BTRFS_INODE_ITEM_KEY;
2417 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002418 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002419 ret = PTR_RET(inode);
2420 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002421 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002422
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002423 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2424 struct btrfs_root *dead_root;
2425 struct btrfs_fs_info *fs_info = root->fs_info;
2426 int is_dead_root = 0;
2427
2428 /*
2429 * this is an orphan in the tree root. Currently these
2430 * could come from 2 sources:
2431 * a) a snapshot deletion in progress
2432 * b) a free space cache inode
2433 * We need to distinguish those two, as the snapshot
2434 * orphan must not get deleted.
2435 * find_dead_roots already ran before us, so if this
2436 * is a snapshot deletion, we should find the root
2437 * in the dead_roots list
2438 */
2439 spin_lock(&fs_info->trans_lock);
2440 list_for_each_entry(dead_root, &fs_info->dead_roots,
2441 root_list) {
2442 if (dead_root->root_key.objectid ==
2443 found_key.objectid) {
2444 is_dead_root = 1;
2445 break;
2446 }
2447 }
2448 spin_unlock(&fs_info->trans_lock);
2449 if (is_dead_root) {
2450 /* prevent this orphan from being found again */
2451 key.offset = found_key.objectid - 1;
2452 continue;
2453 }
2454 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002455 /*
2456 * Inode is already gone but the orphan item is still there,
2457 * kill the orphan item.
2458 */
2459 if (ret == -ESTALE) {
2460 trans = btrfs_start_transaction(root, 1);
2461 if (IS_ERR(trans)) {
2462 ret = PTR_ERR(trans);
2463 goto out;
2464 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002465 printk(KERN_ERR "auto deleting %Lu\n",
2466 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002467 ret = btrfs_del_orphan_item(trans, root,
2468 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002469 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002470 btrfs_end_transaction(trans, root);
2471 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002472 }
Josef Bacik7b128762008-07-24 12:17:14 -04002473
Josef Bacik7b128762008-07-24 12:17:14 -04002474 /*
2475 * add this inode to the orphan list so btrfs_orphan_del does
2476 * the proper thing when we hit it
2477 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002478 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2479 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002480
Josef Bacik7b128762008-07-24 12:17:14 -04002481 /* if we have links, this was a truncate, lets do that */
2482 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002483 if (!S_ISREG(inode->i_mode)) {
2484 WARN_ON(1);
2485 iput(inode);
2486 continue;
2487 }
Josef Bacik7b128762008-07-24 12:17:14 -04002488 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002489
2490 /* 1 for the orphan item deletion. */
2491 trans = btrfs_start_transaction(root, 1);
2492 if (IS_ERR(trans)) {
2493 ret = PTR_ERR(trans);
2494 goto out;
2495 }
2496 ret = btrfs_orphan_add(trans, inode);
2497 btrfs_end_transaction(trans, root);
2498 if (ret)
2499 goto out;
2500
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002501 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002502 } else {
2503 nr_unlink++;
2504 }
2505
2506 /* this will do delete_inode and everything for us */
2507 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002508 if (ret)
2509 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002510 }
Miao Xie3254c872011-11-10 20:45:05 -05002511 /* release the path since we're done with it */
2512 btrfs_release_path(path);
2513
Yan, Zhengd68fc572010-05-16 10:49:58 -04002514 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2515
2516 if (root->orphan_block_rsv)
2517 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2518 (u64)-1);
2519
2520 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002521 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002522 if (!IS_ERR(trans))
2523 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002524 }
Josef Bacik7b128762008-07-24 12:17:14 -04002525
2526 if (nr_unlink)
2527 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2528 if (nr_truncate)
2529 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002530
2531out:
2532 if (ret)
2533 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2534 btrfs_free_path(path);
2535 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002536}
2537
Chris Masond352ac62008-09-29 15:18:18 -04002538/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002539 * very simple check to peek ahead in the leaf looking for xattrs. If we
2540 * don't find any xattrs, we know there can't be any acls.
2541 *
2542 * slot is the slot the inode is in, objectid is the objectid of the inode
2543 */
2544static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2545 int slot, u64 objectid)
2546{
2547 u32 nritems = btrfs_header_nritems(leaf);
2548 struct btrfs_key found_key;
2549 int scanned = 0;
2550
2551 slot++;
2552 while (slot < nritems) {
2553 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2554
2555 /* we found a different objectid, there must not be acls */
2556 if (found_key.objectid != objectid)
2557 return 0;
2558
2559 /* we found an xattr, assume we've got an acl */
2560 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2561 return 1;
2562
2563 /*
2564 * we found a key greater than an xattr key, there can't
2565 * be any acls later on
2566 */
2567 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2568 return 0;
2569
2570 slot++;
2571 scanned++;
2572
2573 /*
2574 * it goes inode, inode backrefs, xattrs, extents,
2575 * so if there are a ton of hard links to an inode there can
2576 * be a lot of backrefs. Don't waste time searching too hard,
2577 * this is just an optimization
2578 */
2579 if (scanned >= 8)
2580 break;
2581 }
2582 /* we hit the end of the leaf before we found an xattr or
2583 * something larger than an xattr. We have to assume the inode
2584 * has acls
2585 */
2586 return 1;
2587}
2588
2589/*
Chris Masond352ac62008-09-29 15:18:18 -04002590 * read an inode from the btree into the in-memory inode
2591 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002592static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002593{
2594 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002595 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002596 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002597 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002598 struct btrfs_root *root = BTRFS_I(inode)->root;
2599 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002600 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002601 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002602 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002603 bool filled = false;
2604
2605 ret = btrfs_fill_inode(inode, &rdev);
2606 if (!ret)
2607 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002608
2609 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002610 if (!path)
2611 goto make_bad;
2612
Josef Bacikd90c7322011-05-17 09:50:54 -04002613 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002614 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002615
Chris Mason39279cc2007-06-12 06:35:45 -04002616 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002617 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002618 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002619
Chris Mason5f39d392007-10-15 16:14:19 -04002620 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002621
2622 if (filled)
2623 goto cache_acl;
2624
Chris Mason5f39d392007-10-15 16:14:19 -04002625 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2626 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002627 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002628 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002629 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2630 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002631 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002632
2633 tspec = btrfs_inode_atime(inode_item);
2634 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2635 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2636
2637 tspec = btrfs_inode_mtime(inode_item);
2638 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2639 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2640
2641 tspec = btrfs_inode_ctime(inode_item);
2642 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2643 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2644
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002645 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002646 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002647 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2648
2649 /*
2650 * If we were modified in the current generation and evicted from memory
2651 * and then re-read we need to do a full sync since we don't have any
2652 * idea about which extents were modified before we were evicted from
2653 * cache.
2654 */
2655 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2656 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2657 &BTRFS_I(inode)->runtime_flags);
2658
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002659 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002660 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002661 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002662 rdev = btrfs_inode_rdev(leaf, inode_item);
2663
Josef Bacikaec74772008-07-24 12:12:38 -04002664 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002665 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002666cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002667 /*
2668 * try to precache a NULL acl entry for files that don't have
2669 * any xattrs or acls
2670 */
Li Zefan33345d012011-04-20 10:31:50 +08002671 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2672 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002673 if (!maybe_acls)
2674 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002675
Chris Mason39279cc2007-06-12 06:35:45 -04002676 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002677
Chris Mason39279cc2007-06-12 06:35:45 -04002678 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002679 case S_IFREG:
2680 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002681 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002682 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002683 inode->i_fop = &btrfs_file_operations;
2684 inode->i_op = &btrfs_file_inode_operations;
2685 break;
2686 case S_IFDIR:
2687 inode->i_fop = &btrfs_dir_file_operations;
2688 if (root == root->fs_info->tree_root)
2689 inode->i_op = &btrfs_dir_ro_inode_operations;
2690 else
2691 inode->i_op = &btrfs_dir_inode_operations;
2692 break;
2693 case S_IFLNK:
2694 inode->i_op = &btrfs_symlink_inode_operations;
2695 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002696 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002697 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002698 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002699 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002700 init_special_inode(inode, inode->i_mode, rdev);
2701 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002702 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002703
2704 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002705 return;
2706
2707make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002708 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002709 make_bad_inode(inode);
2710}
2711
Chris Masond352ac62008-09-29 15:18:18 -04002712/*
2713 * given a leaf and an inode, copy the inode fields into the leaf
2714 */
Chris Masone02119d2008-09-05 16:13:11 -04002715static void fill_inode_item(struct btrfs_trans_handle *trans,
2716 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002717 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002718 struct inode *inode)
2719{
Liu Bo51fab692012-12-27 09:01:21 +00002720 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002721
Liu Bo51fab692012-12-27 09:01:21 +00002722 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04002723
Liu Bo51fab692012-12-27 09:01:21 +00002724 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2725 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2726 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
2727 &token);
2728 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2729 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002730
Liu Bo51fab692012-12-27 09:01:21 +00002731 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2732 inode->i_atime.tv_sec, &token);
2733 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2734 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002735
Liu Bo51fab692012-12-27 09:01:21 +00002736 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2737 inode->i_mtime.tv_sec, &token);
2738 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
2739 inode->i_mtime.tv_nsec, &token);
2740
2741 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
2742 inode->i_ctime.tv_sec, &token);
2743 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
2744 inode->i_ctime.tv_nsec, &token);
2745
2746 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
2747 &token);
2748 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
2749 &token);
2750 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
2751 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
2752 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
2753 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
2754 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04002755}
2756
Chris Masond352ac62008-09-29 15:18:18 -04002757/*
2758 * copy everything in the in-memory inode into the btree.
2759 */
Chris Mason21151332011-11-10 20:39:08 -05002760static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002761 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002762{
2763 struct btrfs_inode_item *inode_item;
2764 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002765 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002766 int ret;
2767
2768 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002769 if (!path)
2770 return -ENOMEM;
2771
Chris Masonb9473432009-03-13 11:00:37 -04002772 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002773 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2774 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002775 if (ret) {
2776 if (ret > 0)
2777 ret = -ENOENT;
2778 goto failed;
2779 }
2780
Chris Masonb4ce94d2009-02-04 09:25:08 -05002781 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002782 leaf = path->nodes[0];
2783 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002784 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002785
Chris Masone02119d2008-09-05 16:13:11 -04002786 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002787 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002788 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002789 ret = 0;
2790failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002791 btrfs_free_path(path);
2792 return ret;
2793}
2794
Chris Masond352ac62008-09-29 15:18:18 -04002795/*
Chris Mason21151332011-11-10 20:39:08 -05002796 * copy everything in the in-memory inode into the btree.
2797 */
2798noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2799 struct btrfs_root *root, struct inode *inode)
2800{
2801 int ret;
2802
2803 /*
2804 * If the inode is a free space inode, we can deadlock during commit
2805 * if we put it into the delayed code.
2806 *
2807 * The data relocation inode should also be directly updated
2808 * without delay
2809 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002810 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002811 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002812 btrfs_update_root_times(trans, root);
2813
Chris Mason21151332011-11-10 20:39:08 -05002814 ret = btrfs_delayed_update_inode(trans, root, inode);
2815 if (!ret)
2816 btrfs_set_inode_last_trans(trans, inode);
2817 return ret;
2818 }
2819
2820 return btrfs_update_inode_item(trans, root, inode);
2821}
2822
Josef Bacikbe6aef62012-10-22 15:43:12 -04002823noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2824 struct btrfs_root *root,
2825 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002826{
2827 int ret;
2828
2829 ret = btrfs_update_inode(trans, root, inode);
2830 if (ret == -ENOSPC)
2831 return btrfs_update_inode_item(trans, root, inode);
2832 return ret;
2833}
2834
2835/*
Chris Masond352ac62008-09-29 15:18:18 -04002836 * unlink helper that gets used here in inode.c and in the tree logging
2837 * recovery code. It remove a link in a directory with a given name, and
2838 * also drops the back refs in the inode to the directory
2839 */
Al Viro92986792011-03-04 17:14:37 +00002840static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2841 struct btrfs_root *root,
2842 struct inode *dir, struct inode *inode,
2843 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002844{
2845 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002846 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002847 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002848 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002849 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002850 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002851 u64 ino = btrfs_ino(inode);
2852 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002853
2854 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002855 if (!path) {
2856 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002857 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002858 }
2859
Chris Masonb9473432009-03-13 11:00:37 -04002860 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002861 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002862 name, name_len, -1);
2863 if (IS_ERR(di)) {
2864 ret = PTR_ERR(di);
2865 goto err;
2866 }
2867 if (!di) {
2868 ret = -ENOENT;
2869 goto err;
2870 }
Chris Mason5f39d392007-10-15 16:14:19 -04002871 leaf = path->nodes[0];
2872 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002873 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002874 if (ret)
2875 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002876 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002877
Li Zefan33345d012011-04-20 10:31:50 +08002878 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2879 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002880 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002881 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002882 "inode %llu parent %llu\n", name_len, name,
2883 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002884 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002885 goto err;
2886 }
2887
Miao Xie16cdcec2011-04-22 18:12:22 +08002888 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002889 if (ret) {
2890 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002891 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002892 }
Chris Mason39279cc2007-06-12 06:35:45 -04002893
Chris Masone02119d2008-09-05 16:13:11 -04002894 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002895 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896 if (ret != 0 && ret != -ENOENT) {
2897 btrfs_abort_transaction(trans, root, ret);
2898 goto err;
2899 }
Chris Masone02119d2008-09-05 16:13:11 -04002900
2901 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2902 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002903 if (ret == -ENOENT)
2904 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002905err:
2906 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002907 if (ret)
2908 goto out;
2909
2910 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002911 inode_inc_iversion(inode);
2912 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002913 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002914 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002915out:
Chris Mason39279cc2007-06-12 06:35:45 -04002916 return ret;
2917}
2918
Al Viro92986792011-03-04 17:14:37 +00002919int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2920 struct btrfs_root *root,
2921 struct inode *dir, struct inode *inode,
2922 const char *name, int name_len)
2923{
2924 int ret;
2925 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2926 if (!ret) {
2927 btrfs_drop_nlink(inode);
2928 ret = btrfs_update_inode(trans, root, inode);
2929 }
2930 return ret;
2931}
2932
2933
Yan, Zhenga22285a2010-05-16 10:48:46 -04002934/* helper to check if there is any shared block in the path */
2935static int check_path_shared(struct btrfs_root *root,
2936 struct btrfs_path *path)
2937{
2938 struct extent_buffer *eb;
2939 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002940 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002941
2942 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002943 int ret;
2944
Yan, Zhenga22285a2010-05-16 10:48:46 -04002945 if (!path->nodes[level])
2946 break;
2947 eb = path->nodes[level];
2948 if (!btrfs_block_can_be_shared(root, eb))
2949 continue;
2950 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2951 &refs, NULL);
2952 if (refs > 1)
2953 return 1;
2954 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002955 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002956}
2957
2958/*
2959 * helper to start transaction for unlink and rmdir.
2960 *
2961 * unlink and rmdir are special in btrfs, they do not always free space.
2962 * so in enospc case, we should make sure they will free space before
2963 * allowing them to use the global metadata reservation.
2964 */
2965static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2966 struct dentry *dentry)
2967{
2968 struct btrfs_trans_handle *trans;
2969 struct btrfs_root *root = BTRFS_I(dir)->root;
2970 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002971 struct btrfs_dir_item *di;
2972 struct inode *inode = dentry->d_inode;
2973 u64 index;
2974 int check_link = 1;
2975 int err = -ENOSPC;
2976 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002977 u64 ino = btrfs_ino(inode);
2978 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002979
Josef Bacike70bea52011-10-11 14:18:24 -04002980 /*
2981 * 1 for the possible orphan item
2982 * 1 for the dir item
2983 * 1 for the dir index
2984 * 1 for the inode ref
2985 * 1 for the inode ref in the tree log
2986 * 2 for the dir entries in the log
2987 * 1 for the inode
2988 */
2989 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002990 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2991 return trans;
2992
Li Zefan33345d012011-04-20 10:31:50 +08002993 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002994 return ERR_PTR(-ENOSPC);
2995
2996 /* check if there is someone else holds reference */
2997 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2998 return ERR_PTR(-ENOSPC);
2999
3000 if (atomic_read(&inode->i_count) > 2)
3001 return ERR_PTR(-ENOSPC);
3002
3003 if (xchg(&root->fs_info->enospc_unlink, 1))
3004 return ERR_PTR(-ENOSPC);
3005
3006 path = btrfs_alloc_path();
3007 if (!path) {
3008 root->fs_info->enospc_unlink = 0;
3009 return ERR_PTR(-ENOMEM);
3010 }
3011
Josef Bacik3880a1b2011-10-14 14:46:51 -04003012 /* 1 for the orphan item */
3013 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003014 if (IS_ERR(trans)) {
3015 btrfs_free_path(path);
3016 root->fs_info->enospc_unlink = 0;
3017 return trans;
3018 }
3019
3020 path->skip_locking = 1;
3021 path->search_commit_root = 1;
3022
3023 ret = btrfs_lookup_inode(trans, root, path,
3024 &BTRFS_I(dir)->location, 0);
3025 if (ret < 0) {
3026 err = ret;
3027 goto out;
3028 }
3029 if (ret == 0) {
3030 if (check_path_shared(root, path))
3031 goto out;
3032 } else {
3033 check_link = 0;
3034 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003035 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003036
3037 ret = btrfs_lookup_inode(trans, root, path,
3038 &BTRFS_I(inode)->location, 0);
3039 if (ret < 0) {
3040 err = ret;
3041 goto out;
3042 }
3043 if (ret == 0) {
3044 if (check_path_shared(root, path))
3045 goto out;
3046 } else {
3047 check_link = 0;
3048 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003049 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003050
3051 if (ret == 0 && S_ISREG(inode->i_mode)) {
3052 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003053 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003054 if (ret < 0) {
3055 err = ret;
3056 goto out;
3057 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003059 if (check_path_shared(root, path))
3060 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003061 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003062 }
3063
3064 if (!check_link) {
3065 err = 0;
3066 goto out;
3067 }
3068
Li Zefan33345d012011-04-20 10:31:50 +08003069 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003070 dentry->d_name.name, dentry->d_name.len, 0);
3071 if (IS_ERR(di)) {
3072 err = PTR_ERR(di);
3073 goto out;
3074 }
3075 if (di) {
3076 if (check_path_shared(root, path))
3077 goto out;
3078 } else {
3079 err = 0;
3080 goto out;
3081 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003082 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003083
Mark Fashehf1863732012-08-08 11:32:27 -07003084 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3085 dentry->d_name.len, ino, dir_ino, 0,
3086 &index);
3087 if (ret) {
3088 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003089 goto out;
3090 }
Mark Fashehf1863732012-08-08 11:32:27 -07003091
Yan, Zhenga22285a2010-05-16 10:48:46 -04003092 if (check_path_shared(root, path))
3093 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003094
David Sterbab3b4aa72011-04-21 01:20:15 +02003095 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096
Miao Xie16cdcec2011-04-22 18:12:22 +08003097 /*
3098 * This is a commit root search, if we can lookup inode item and other
3099 * relative items in the commit root, it means the transaction of
3100 * dir/file creation has been committed, and the dir index item that we
3101 * delay to insert has also been inserted into the commit root. So
3102 * we needn't worry about the delayed insertion of the dir index item
3103 * here.
3104 */
Li Zefan33345d012011-04-20 10:31:50 +08003105 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003106 dentry->d_name.name, dentry->d_name.len, 0);
3107 if (IS_ERR(di)) {
3108 err = PTR_ERR(di);
3109 goto out;
3110 }
3111 BUG_ON(ret == -ENOENT);
3112 if (check_path_shared(root, path))
3113 goto out;
3114
3115 err = 0;
3116out:
3117 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003118 /* Migrate the orphan reservation over */
3119 if (!err)
3120 err = btrfs_block_rsv_migrate(trans->block_rsv,
3121 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003122 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003123
Yan, Zhenga22285a2010-05-16 10:48:46 -04003124 if (err) {
3125 btrfs_end_transaction(trans, root);
3126 root->fs_info->enospc_unlink = 0;
3127 return ERR_PTR(err);
3128 }
3129
3130 trans->block_rsv = &root->fs_info->global_block_rsv;
3131 return trans;
3132}
3133
3134static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3135 struct btrfs_root *root)
3136{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003137 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003138 btrfs_block_rsv_release(root, trans->block_rsv,
3139 trans->bytes_reserved);
3140 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003141 BUG_ON(!root->fs_info->enospc_unlink);
3142 root->fs_info->enospc_unlink = 0;
3143 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003144 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003145}
3146
Chris Mason39279cc2007-06-12 06:35:45 -04003147static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3148{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003149 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003150 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003151 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003152 int ret;
3153
Yan, Zhenga22285a2010-05-16 10:48:46 -04003154 trans = __unlink_start_trans(dir, dentry);
3155 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003156 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003157
Chris Mason12fcfd22009-03-24 10:24:20 -04003158 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3159
Chris Masone02119d2008-09-05 16:13:11 -04003160 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3161 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003162 if (ret)
3163 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003164
Yan, Zhenga22285a2010-05-16 10:48:46 -04003165 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003166 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003167 if (ret)
3168 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003169 }
Josef Bacik7b128762008-07-24 12:17:14 -04003170
Tsutomu Itohb5324022011-07-19 07:27:20 +00003171out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003172 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003173 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003174 return ret;
3175}
3176
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003177int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3178 struct btrfs_root *root,
3179 struct inode *dir, u64 objectid,
3180 const char *name, int name_len)
3181{
3182 struct btrfs_path *path;
3183 struct extent_buffer *leaf;
3184 struct btrfs_dir_item *di;
3185 struct btrfs_key key;
3186 u64 index;
3187 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003188 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003189
3190 path = btrfs_alloc_path();
3191 if (!path)
3192 return -ENOMEM;
3193
Li Zefan33345d012011-04-20 10:31:50 +08003194 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003195 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003196 if (IS_ERR_OR_NULL(di)) {
3197 if (!di)
3198 ret = -ENOENT;
3199 else
3200 ret = PTR_ERR(di);
3201 goto out;
3202 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003203
3204 leaf = path->nodes[0];
3205 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3206 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3207 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003208 if (ret) {
3209 btrfs_abort_transaction(trans, root, ret);
3210 goto out;
3211 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003212 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003213
3214 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3215 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003216 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003217 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003218 if (ret != -ENOENT) {
3219 btrfs_abort_transaction(trans, root, ret);
3220 goto out;
3221 }
Li Zefan33345d012011-04-20 10:31:50 +08003222 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003223 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003224 if (IS_ERR_OR_NULL(di)) {
3225 if (!di)
3226 ret = -ENOENT;
3227 else
3228 ret = PTR_ERR(di);
3229 btrfs_abort_transaction(trans, root, ret);
3230 goto out;
3231 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003232
3233 leaf = path->nodes[0];
3234 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003235 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003236 index = key.offset;
3237 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003238 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003239
Miao Xie16cdcec2011-04-22 18:12:22 +08003240 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003241 if (ret) {
3242 btrfs_abort_transaction(trans, root, ret);
3243 goto out;
3244 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003245
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003246 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003247 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003248 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003249 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003250 if (ret)
3251 btrfs_abort_transaction(trans, root, ret);
3252out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003253 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003254 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003255}
3256
Chris Mason39279cc2007-06-12 06:35:45 -04003257static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3258{
3259 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003260 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003261 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003262 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003263
David Sterbab3ae2442012-09-13 16:04:34 -06003264 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003265 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003266 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3267 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003268
Yan, Zhenga22285a2010-05-16 10:48:46 -04003269 trans = __unlink_start_trans(dir, dentry);
3270 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003271 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003272
Li Zefan33345d012011-04-20 10:31:50 +08003273 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003274 err = btrfs_unlink_subvol(trans, root, dir,
3275 BTRFS_I(inode)->location.objectid,
3276 dentry->d_name.name,
3277 dentry->d_name.len);
3278 goto out;
3279 }
3280
Josef Bacik7b128762008-07-24 12:17:14 -04003281 err = btrfs_orphan_add(trans, inode);
3282 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003283 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003284
Chris Mason39279cc2007-06-12 06:35:45 -04003285 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003286 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3287 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003288 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003289 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003290out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003291 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003292 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003293
Chris Mason39279cc2007-06-12 06:35:45 -04003294 return err;
3295}
3296
Chris Mason323ac952008-10-01 19:05:46 -04003297/*
Chris Mason39279cc2007-06-12 06:35:45 -04003298 * this can truncate away extent items, csum items and directory items.
3299 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003300 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003301 *
3302 * csum items that cross the new i_size are truncated to the new size
3303 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003304 *
3305 * min_type is the minimum key type to truncate down to. If set to 0, this
3306 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003307 */
Yan, Zheng80825102009-11-12 09:35:36 +00003308int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3309 struct btrfs_root *root,
3310 struct inode *inode,
3311 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003312{
Chris Mason39279cc2007-06-12 06:35:45 -04003313 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003314 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003315 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003316 struct btrfs_key key;
3317 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003318 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003319 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003321 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003322 u64 mask = root->sectorsize - 1;
3323 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003324 int found_extent;
3325 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003326 int pending_del_nr = 0;
3327 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003328 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003329 int ret;
3330 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003331 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003332
3333 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003334
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003335 path = btrfs_alloc_path();
3336 if (!path)
3337 return -ENOMEM;
3338 path->reada = -1;
3339
Josef Bacik5dc562c2012-08-17 13:14:17 -04003340 /*
3341 * We want to drop from the next block forward in case this new size is
3342 * not block aligned since we will be keeping the last block of the
3343 * extent just the way it is.
3344 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003345 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003346 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003347
Miao Xie16cdcec2011-04-22 18:12:22 +08003348 /*
3349 * This function is also used to drop the items in the log tree before
3350 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3351 * it is used to drop the loged items. So we shouldn't kill the delayed
3352 * items.
3353 */
3354 if (min_type == 0 && root == BTRFS_I(inode)->root)
3355 btrfs_kill_delayed_inode_items(inode);
3356
Li Zefan33345d012011-04-20 10:31:50 +08003357 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003358 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003359 key.type = (u8)-1;
3360
Chris Mason85e21ba2008-01-29 15:11:36 -05003361search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003362 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003363 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003364 if (ret < 0) {
3365 err = ret;
3366 goto out;
3367 }
Chris Masond3977122009-01-05 21:25:51 -05003368
Chris Mason85e21ba2008-01-29 15:11:36 -05003369 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003370 /* there are no items in the tree for us to truncate, we're
3371 * done
3372 */
Yan, Zheng80825102009-11-12 09:35:36 +00003373 if (path->slots[0] == 0)
3374 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003375 path->slots[0]--;
3376 }
3377
Chris Masond3977122009-01-05 21:25:51 -05003378 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003379 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003380 leaf = path->nodes[0];
3381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3382 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003383
Li Zefan33345d012011-04-20 10:31:50 +08003384 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003385 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003386
Chris Mason85e21ba2008-01-29 15:11:36 -05003387 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003388 break;
3389
Chris Mason5f39d392007-10-15 16:14:19 -04003390 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003391 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003392 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003393 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003394 extent_type = btrfs_file_extent_type(leaf, fi);
3395 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003396 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003397 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003398 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003399 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003400 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003401 }
Yan008630c2007-11-07 13:31:09 -05003402 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003403 }
Yan, Zheng80825102009-11-12 09:35:36 +00003404 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003405 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003406 } else {
3407 if (item_end < new_size)
3408 break;
3409 if (found_key.offset >= new_size)
3410 del_item = 1;
3411 else
3412 del_item = 0;
3413 }
Chris Mason39279cc2007-06-12 06:35:45 -04003414 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003415 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003416 if (found_type != BTRFS_EXTENT_DATA_KEY)
3417 goto delete;
3418
3419 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003420 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003421 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003422 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003423 u64 orig_num_bytes =
3424 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003425 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003426 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05003427 extent_num_bytes = extent_num_bytes &
3428 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003429 btrfs_set_file_extent_num_bytes(leaf, fi,
3430 extent_num_bytes);
3431 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003432 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003433 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003434 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003435 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003436 } else {
Chris Masondb945352007-10-15 16:15:53 -04003437 extent_num_bytes =
3438 btrfs_file_extent_disk_num_bytes(leaf,
3439 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003440 extent_offset = found_key.offset -
3441 btrfs_file_extent_offset(leaf, fi);
3442
Chris Mason39279cc2007-06-12 06:35:45 -04003443 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003444 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003445 if (extent_start != 0) {
3446 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003447 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003448 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003449 }
3450 }
Chris Mason90692182008-02-08 13:49:28 -05003451 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003452 /*
3453 * we can't truncate inline items that have had
3454 * special encodings
3455 */
3456 if (!del_item &&
3457 btrfs_file_extent_compression(leaf, fi) == 0 &&
3458 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3459 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003460 u32 size = new_size - found_key.offset;
3461
3462 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003463 inode_sub_bytes(inode, item_end + 1 -
3464 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003465 }
3466 size =
3467 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003468 btrfs_truncate_item(trans, root, path,
3469 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003470 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003471 inode_sub_bytes(inode, item_end + 1 -
3472 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003473 }
Chris Mason39279cc2007-06-12 06:35:45 -04003474 }
Chris Mason179e29e2007-11-01 11:28:41 -04003475delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003476 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003477 if (!pending_del_nr) {
3478 /* no pending yet, add ourselves */
3479 pending_del_slot = path->slots[0];
3480 pending_del_nr = 1;
3481 } else if (pending_del_nr &&
3482 path->slots[0] + 1 == pending_del_slot) {
3483 /* hop on the pending chunk */
3484 pending_del_nr++;
3485 pending_del_slot = path->slots[0];
3486 } else {
Chris Masond3977122009-01-05 21:25:51 -05003487 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003488 }
Chris Mason39279cc2007-06-12 06:35:45 -04003489 } else {
3490 break;
3491 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003492 if (found_extent && (root->ref_cows ||
3493 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003494 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003495 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003496 extent_num_bytes, 0,
3497 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003498 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003499 BUG_ON(ret);
3500 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003501
Yan, Zheng80825102009-11-12 09:35:36 +00003502 if (found_type == BTRFS_INODE_ITEM_KEY)
3503 break;
3504
3505 if (path->slots[0] == 0 ||
3506 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003507 if (pending_del_nr) {
3508 ret = btrfs_del_items(trans, root, path,
3509 pending_del_slot,
3510 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003511 if (ret) {
3512 btrfs_abort_transaction(trans,
3513 root, ret);
3514 goto error;
3515 }
Yan, Zheng80825102009-11-12 09:35:36 +00003516 pending_del_nr = 0;
3517 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003518 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003519 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003520 } else {
3521 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003522 }
Chris Mason39279cc2007-06-12 06:35:45 -04003523 }
Yan, Zheng80825102009-11-12 09:35:36 +00003524out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003525 if (pending_del_nr) {
3526 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3527 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003528 if (ret)
3529 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003530 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003531error:
Chris Mason39279cc2007-06-12 06:35:45 -04003532 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003533 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003534}
3535
Chris Masona52d9a82007-08-27 16:49:44 -04003536/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003537 * btrfs_truncate_page - read, zero a chunk and write a page
3538 * @inode - inode that we're zeroing
3539 * @from - the offset to start zeroing
3540 * @len - the length to zero, 0 to zero the entire range respective to the
3541 * offset
3542 * @front - zero up to the offset instead of from the offset on
3543 *
3544 * This will find the page for the "from" offset and cow the page and zero the
3545 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003546 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003547int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3548 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003549{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003550 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003551 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3553 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003554 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003555 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003556 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003557 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3558 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3559 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003560 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003561 int ret = 0;
3562 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003563 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003564
Josef Bacik2aaa6652012-08-29 14:27:18 -04003565 if ((offset & (blocksize - 1)) == 0 &&
3566 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003567 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003568 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003569 if (ret)
3570 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003571
Chris Mason211c17f2008-05-15 09:13:45 -04003572again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003573 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003574 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003575 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003576 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003577 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003578 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003579
3580 page_start = page_offset(page);
3581 page_end = page_start + PAGE_CACHE_SIZE - 1;
3582
Chris Masona52d9a82007-08-27 16:49:44 -04003583 if (!PageUptodate(page)) {
3584 ret = btrfs_readpage(NULL, page);
3585 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003586 if (page->mapping != mapping) {
3587 unlock_page(page);
3588 page_cache_release(page);
3589 goto again;
3590 }
Chris Masona52d9a82007-08-27 16:49:44 -04003591 if (!PageUptodate(page)) {
3592 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003593 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003594 }
3595 }
Chris Mason211c17f2008-05-15 09:13:45 -04003596 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003597
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003598 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003599 set_page_extent_mapped(page);
3600
3601 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3602 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003603 unlock_extent_cached(io_tree, page_start, page_end,
3604 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003605 unlock_page(page);
3606 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003607 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003608 btrfs_put_ordered_extent(ordered);
3609 goto again;
3610 }
3611
Josef Bacik2ac55d42010-02-03 19:33:23 +00003612 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003613 EXTENT_DIRTY | EXTENT_DELALLOC |
3614 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003615 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003616
Josef Bacik2ac55d42010-02-03 19:33:23 +00003617 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3618 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003619 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003620 unlock_extent_cached(io_tree, page_start, page_end,
3621 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003622 goto out_unlock;
3623 }
3624
Chris Masone6dcd2d2008-07-17 12:53:50 -04003625 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003626 if (!len)
3627 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003628 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003629 if (front)
3630 memset(kaddr, 0, offset);
3631 else
3632 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003633 flush_dcache_page(page);
3634 kunmap(page);
3635 }
Chris Mason247e7432008-07-17 12:53:51 -04003636 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003637 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003638 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3639 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003640
Chris Mason89642222008-07-24 09:41:53 -04003641out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003642 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003643 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003644 unlock_page(page);
3645 page_cache_release(page);
3646out:
3647 return ret;
3648}
3649
Josef Bacik695a0d02011-03-04 15:46:53 -05003650/*
3651 * This function puts in dummy file extents for the area we're creating a hole
3652 * for. So if we are truncating this file to a larger size we need to insert
3653 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3654 * the range between oldsize and size
3655 */
Josef Bacika41ad392011-01-31 15:30:16 -05003656int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003657{
3658 struct btrfs_trans_handle *trans;
3659 struct btrfs_root *root = BTRFS_I(inode)->root;
3660 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003661 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003662 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003663 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003664 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003665 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003666 u64 block_end = (size + mask) & ~mask;
3667 u64 last_byte;
3668 u64 cur_offset;
3669 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003670 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003671
3672 if (size <= hole_start)
3673 return 0;
3674
Yan Zheng9036c102008-10-30 14:19:41 -04003675 while (1) {
3676 struct btrfs_ordered_extent *ordered;
3677 btrfs_wait_ordered_range(inode, hole_start,
3678 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003679 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003680 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003681 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3682 if (!ordered)
3683 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003684 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3685 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003686 btrfs_put_ordered_extent(ordered);
3687 }
3688
Yan Zheng9036c102008-10-30 14:19:41 -04003689 cur_offset = hole_start;
3690 while (1) {
3691 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3692 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003693 if (IS_ERR(em)) {
3694 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003695 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003696 break;
3697 }
Yan Zheng9036c102008-10-30 14:19:41 -04003698 last_byte = min(extent_map_end(em), block_end);
3699 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003700 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003701 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003702 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003703
Miao Xie36423202011-12-14 20:12:02 -05003704 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003705 if (IS_ERR(trans)) {
3706 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003707 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003708 }
Yan, Zheng80825102009-11-12 09:35:36 +00003709
Josef Bacik5dc562c2012-08-17 13:14:17 -04003710 err = btrfs_drop_extents(trans, root, inode,
3711 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003712 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003713 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003714 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003715 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003716 break;
Miao Xie5b397372011-09-11 10:52:24 -04003717 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003718
Yan Zheng9036c102008-10-30 14:19:41 -04003719 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003720 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003721 0, hole_size, 0, hole_size,
3722 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003723 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003724 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003725 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003726 break;
Miao Xie5b397372011-09-11 10:52:24 -04003727 }
Yan, Zheng80825102009-11-12 09:35:36 +00003728
Josef Bacik5dc562c2012-08-17 13:14:17 -04003729 btrfs_drop_extent_cache(inode, cur_offset,
3730 cur_offset + hole_size - 1, 0);
3731 hole_em = alloc_extent_map();
3732 if (!hole_em) {
3733 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3734 &BTRFS_I(inode)->runtime_flags);
3735 goto next;
3736 }
3737 hole_em->start = cur_offset;
3738 hole_em->len = hole_size;
3739 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003740
Josef Bacik5dc562c2012-08-17 13:14:17 -04003741 hole_em->block_start = EXTENT_MAP_HOLE;
3742 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003743 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003744 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3745 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3746 hole_em->generation = trans->transid;
3747
3748 while (1) {
3749 write_lock(&em_tree->lock);
3750 err = add_extent_mapping(em_tree, hole_em);
3751 if (!err)
3752 list_move(&hole_em->list,
3753 &em_tree->modified_extents);
3754 write_unlock(&em_tree->lock);
3755 if (err != -EEXIST)
3756 break;
3757 btrfs_drop_extent_cache(inode, cur_offset,
3758 cur_offset +
3759 hole_size - 1, 0);
3760 }
3761 free_extent_map(hole_em);
3762next:
Miao Xie36423202011-12-14 20:12:02 -05003763 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003764 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003765 }
3766 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003767 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003768 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003769 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003770 break;
3771 }
3772
Yan, Zhenga22285a2010-05-16 10:48:46 -04003773 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003774 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3775 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003776 return err;
3777}
3778
Eric Sandeen3972f262013-01-12 02:57:22 +00003779static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00003780{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003781 struct btrfs_root *root = BTRFS_I(inode)->root;
3782 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003783 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00003784 loff_t newsize = attr->ia_size;
3785 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00003786 int ret;
3787
Josef Bacika41ad392011-01-31 15:30:16 -05003788 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003789 return 0;
3790
Eric Sandeen3972f262013-01-12 02:57:22 +00003791 /*
3792 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3793 * special case where we need to update the times despite not having
3794 * these flags set. For all other operations the VFS set these flags
3795 * explicitly if it wants a timestamp update.
3796 */
3797 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3798 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3799
Josef Bacika41ad392011-01-31 15:30:16 -05003800 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003801 truncate_pagecache(inode, oldsize, newsize);
3802 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003803 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003804 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003805
Miao Xief4a2f4c2011-12-14 20:12:01 -05003806 trans = btrfs_start_transaction(root, 1);
3807 if (IS_ERR(trans))
3808 return PTR_ERR(trans);
3809
3810 i_size_write(inode, newsize);
3811 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3812 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003813 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003814 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003815
Josef Bacika41ad392011-01-31 15:30:16 -05003816 /*
3817 * We're truncating a file that used to have good data down to
3818 * zero. Make sure it gets into the ordered flush list so that
3819 * any new writes get down to disk quickly.
3820 */
3821 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003822 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3823 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003824
Josef Bacikf3fe8202013-01-07 17:03:21 -05003825 /*
3826 * 1 for the orphan item we're going to add
3827 * 1 for the orphan item deletion.
3828 */
3829 trans = btrfs_start_transaction(root, 2);
3830 if (IS_ERR(trans))
3831 return PTR_ERR(trans);
3832
3833 /*
3834 * We need to do this in case we fail at _any_ point during the
3835 * actual truncate. Once we do the truncate_setsize we could
3836 * invalidate pages which forces any outstanding ordered io to
3837 * be instantly completed which will give us extents that need
3838 * to be truncated. If we fail to get an orphan inode down we
3839 * could have left over extents that were never meant to live,
3840 * so we need to garuntee from this point on that everything
3841 * will be consistent.
3842 */
3843 ret = btrfs_orphan_add(trans, inode);
3844 btrfs_end_transaction(trans, root);
3845 if (ret)
3846 return ret;
3847
Josef Bacika41ad392011-01-31 15:30:16 -05003848 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3849 truncate_setsize(inode, newsize);
3850 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003851 if (ret && inode->i_nlink)
3852 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003853 }
3854
Josef Bacika41ad392011-01-31 15:30:16 -05003855 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003856}
3857
Chris Mason39279cc2007-06-12 06:35:45 -04003858static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3859{
3860 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003861 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003862 int err;
3863
Li Zefanb83cc962010-12-20 16:04:08 +08003864 if (btrfs_root_readonly(root))
3865 return -EROFS;
3866
Chris Mason39279cc2007-06-12 06:35:45 -04003867 err = inode_change_ok(inode, attr);
3868 if (err)
3869 return err;
3870
Chris Mason5a3f23d2009-03-31 13:27:11 -04003871 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00003872 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00003873 if (err)
3874 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 }
Yan Zheng9036c102008-10-30 14:19:41 -04003876
Christoph Hellwig10257742010-06-04 11:30:02 +02003877 if (attr->ia_valid) {
3878 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003879 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003880 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003881
Josef Bacik22c44fe2011-11-30 10:45:38 -05003882 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003883 err = btrfs_acl_chmod(inode);
3884 }
3885
Chris Mason39279cc2007-06-12 06:35:45 -04003886 return err;
3887}
Chris Mason61295eb2008-01-14 16:24:38 -05003888
Al Virobd555972010-06-07 11:35:40 -04003889void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003890{
3891 struct btrfs_trans_handle *trans;
3892 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003893 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003894 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003895 int ret;
3896
liubo1abe9b82011-03-24 11:18:59 +00003897 trace_btrfs_inode_evict(inode);
3898
Chris Mason39279cc2007-06-12 06:35:45 -04003899 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003900 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003901 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003902 goto no_delete;
3903
Chris Mason39279cc2007-06-12 06:35:45 -04003904 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003905 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003906 goto no_delete;
3907 }
Al Virobd555972010-06-07 11:35:40 -04003908 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003909 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003910
Yan, Zhengc71bf092009-11-12 09:34:40 +00003911 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003912 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003913 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003914 goto no_delete;
3915 }
3916
Yan, Zheng76dda932009-09-21 16:00:26 -04003917 if (inode->i_nlink > 0) {
3918 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3919 goto no_delete;
3920 }
3921
Miao Xie0e8c36a2012-12-19 06:59:51 +00003922 ret = btrfs_commit_inode_delayed_inode(inode);
3923 if (ret) {
3924 btrfs_orphan_del(NULL, inode);
3925 goto no_delete;
3926 }
3927
Miao Xie66d8f3d2012-09-06 04:02:28 -06003928 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003929 if (!rsv) {
3930 btrfs_orphan_del(NULL, inode);
3931 goto no_delete;
3932 }
Josef Bacik4a338542011-08-29 11:01:31 -04003933 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003934 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003935 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003936
Chris Masondbe674a2008-07-17 12:54:05 -04003937 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003938
Josef Bacik4289a662011-08-05 13:22:24 -04003939 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003940 * This is a bit simpler than btrfs_truncate since we've already
3941 * reserved our space for our orphan item in the unlink, so we just
3942 * need to reserve some slack space in case we add bytes and update
3943 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003944 */
Yan, Zheng80825102009-11-12 09:35:36 +00003945 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003946 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3947 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003948
Josef Bacik726c35f2011-09-26 15:46:06 -04003949 /*
3950 * Try and steal from the global reserve since we will
3951 * likely not use this space anyway, we want to try as
3952 * hard as possible to get this to work.
3953 */
3954 if (ret)
3955 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3956
Yan, Zhengd68fc572010-05-16 10:49:58 -04003957 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003958 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003959 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003960 btrfs_orphan_del(NULL, inode);
3961 btrfs_free_block_rsv(root, rsv);
3962 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003963 }
3964
Miao Xie0e8c36a2012-12-19 06:59:51 +00003965 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04003966 if (IS_ERR(trans)) {
3967 btrfs_orphan_del(NULL, inode);
3968 btrfs_free_block_rsv(root, rsv);
3969 goto no_delete;
3970 }
3971
3972 trans->block_rsv = rsv;
3973
Yan, Zhengd68fc572010-05-16 10:49:58 -04003974 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003975 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003976 break;
3977
Miao Xie8407aa42012-09-07 01:43:32 -06003978 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003979 btrfs_end_transaction(trans, root);
3980 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00003981 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04003982 }
3983
Josef Bacik4289a662011-08-05 13:22:24 -04003984 btrfs_free_block_rsv(root, rsv);
3985
Yan, Zheng80825102009-11-12 09:35:36 +00003986 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003987 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003988 ret = btrfs_orphan_del(trans, inode);
3989 BUG_ON(ret);
3990 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003991
Josef Bacik4289a662011-08-05 13:22:24 -04003992 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003993 if (!(root == root->fs_info->tree_root ||
3994 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003995 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003996
Chris Mason54aa1f42007-06-22 14:16:25 -04003997 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003998 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003999no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004000 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004001 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004002}
4003
4004/*
4005 * this returns the key found in the dir entry in the location pointer.
4006 * If no dir entries were found, location->objectid is 0.
4007 */
4008static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4009 struct btrfs_key *location)
4010{
4011 const char *name = dentry->d_name.name;
4012 int namelen = dentry->d_name.len;
4013 struct btrfs_dir_item *di;
4014 struct btrfs_path *path;
4015 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004016 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004017
4018 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004019 if (!path)
4020 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004021
Li Zefan33345d012011-04-20 10:31:50 +08004022 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004023 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004024 if (IS_ERR(di))
4025 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004026
David Sterbac7040052011-04-19 18:00:01 +02004027 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004028 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004029
Chris Mason5f39d392007-10-15 16:14:19 -04004030 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004031out:
Chris Mason39279cc2007-06-12 06:35:45 -04004032 btrfs_free_path(path);
4033 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004034out_err:
4035 location->objectid = 0;
4036 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004037}
4038
4039/*
4040 * when we hit a tree root in a directory, the btrfs part of the inode
4041 * needs to be changed to reflect the root directory of the tree root. This
4042 * is kind of like crossing a mount point.
4043 */
4044static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004045 struct inode *dir,
4046 struct dentry *dentry,
4047 struct btrfs_key *location,
4048 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004049{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004050 struct btrfs_path *path;
4051 struct btrfs_root *new_root;
4052 struct btrfs_root_ref *ref;
4053 struct extent_buffer *leaf;
4054 int ret;
4055 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004056
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004057 path = btrfs_alloc_path();
4058 if (!path) {
4059 err = -ENOMEM;
4060 goto out;
4061 }
Chris Mason39279cc2007-06-12 06:35:45 -04004062
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004063 err = -ENOENT;
4064 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4065 BTRFS_I(dir)->root->root_key.objectid,
4066 location->objectid);
4067 if (ret) {
4068 if (ret < 0)
4069 err = ret;
4070 goto out;
4071 }
Chris Mason39279cc2007-06-12 06:35:45 -04004072
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004073 leaf = path->nodes[0];
4074 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004075 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4077 goto out;
4078
4079 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4080 (unsigned long)(ref + 1),
4081 dentry->d_name.len);
4082 if (ret)
4083 goto out;
4084
David Sterbab3b4aa72011-04-21 01:20:15 +02004085 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004086
4087 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4088 if (IS_ERR(new_root)) {
4089 err = PTR_ERR(new_root);
4090 goto out;
4091 }
4092
4093 if (btrfs_root_refs(&new_root->root_item) == 0) {
4094 err = -ENOENT;
4095 goto out;
4096 }
4097
4098 *sub_root = new_root;
4099 location->objectid = btrfs_root_dirid(&new_root->root_item);
4100 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004101 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102 err = 0;
4103out:
4104 btrfs_free_path(path);
4105 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004106}
4107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004108static void inode_tree_add(struct inode *inode)
4109{
4110 struct btrfs_root *root = BTRFS_I(inode)->root;
4111 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004112 struct rb_node **p;
4113 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004114 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004115again:
4116 p = &root->inode_tree.rb_node;
4117 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004118
Al Viro1d3382c2010-10-23 15:19:20 -04004119 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004120 return;
4121
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004122 spin_lock(&root->inode_lock);
4123 while (*p) {
4124 parent = *p;
4125 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4126
Li Zefan33345d012011-04-20 10:31:50 +08004127 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004128 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004129 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004130 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004131 else {
4132 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004133 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004134 rb_erase(parent, &root->inode_tree);
4135 RB_CLEAR_NODE(parent);
4136 spin_unlock(&root->inode_lock);
4137 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004138 }
4139 }
4140 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4141 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4142 spin_unlock(&root->inode_lock);
4143}
4144
4145static void inode_tree_del(struct inode *inode)
4146{
4147 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004148 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004149
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004150 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004151 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004152 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004153 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004154 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004155 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004156 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004157
Josef Bacik0af3d002010-06-21 14:48:16 -04004158 /*
4159 * Free space cache has inodes in the tree root, but the tree root has a
4160 * root_refs of 0, so this could end up dropping the tree root as a
4161 * snapshot, so we need the extra !root->fs_info->tree_root check to
4162 * make sure we don't drop it.
4163 */
4164 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4165 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004166 synchronize_srcu(&root->fs_info->subvol_srcu);
4167 spin_lock(&root->inode_lock);
4168 empty = RB_EMPTY_ROOT(&root->inode_tree);
4169 spin_unlock(&root->inode_lock);
4170 if (empty)
4171 btrfs_add_dead_root(root);
4172 }
4173}
4174
Jeff Mahoney143bede2012-03-01 14:56:26 +01004175void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004176{
4177 struct rb_node *node;
4178 struct rb_node *prev;
4179 struct btrfs_inode *entry;
4180 struct inode *inode;
4181 u64 objectid = 0;
4182
4183 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4184
4185 spin_lock(&root->inode_lock);
4186again:
4187 node = root->inode_tree.rb_node;
4188 prev = NULL;
4189 while (node) {
4190 prev = node;
4191 entry = rb_entry(node, struct btrfs_inode, rb_node);
4192
Li Zefan33345d012011-04-20 10:31:50 +08004193 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004194 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004195 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004196 node = node->rb_right;
4197 else
4198 break;
4199 }
4200 if (!node) {
4201 while (prev) {
4202 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004203 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004204 node = prev;
4205 break;
4206 }
4207 prev = rb_next(prev);
4208 }
4209 }
4210 while (node) {
4211 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004212 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004213 inode = igrab(&entry->vfs_inode);
4214 if (inode) {
4215 spin_unlock(&root->inode_lock);
4216 if (atomic_read(&inode->i_count) > 1)
4217 d_prune_aliases(inode);
4218 /*
Al Viro45321ac2010-06-07 13:43:19 -04004219 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004220 * the inode cache when its usage count
4221 * hits zero.
4222 */
4223 iput(inode);
4224 cond_resched();
4225 spin_lock(&root->inode_lock);
4226 goto again;
4227 }
4228
4229 if (cond_resched_lock(&root->inode_lock))
4230 goto again;
4231
4232 node = rb_next(node);
4233 }
4234 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004235}
4236
Chris Masone02119d2008-09-05 16:13:11 -04004237static int btrfs_init_locked_inode(struct inode *inode, void *p)
4238{
4239 struct btrfs_iget_args *args = p;
4240 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004241 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004242 return 0;
4243}
4244
4245static int btrfs_find_actor(struct inode *inode, void *opaque)
4246{
4247 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004248 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004249 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004250}
4251
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004252static struct inode *btrfs_iget_locked(struct super_block *s,
4253 u64 objectid,
4254 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004255{
4256 struct inode *inode;
4257 struct btrfs_iget_args args;
4258 args.ino = objectid;
4259 args.root = root;
4260
4261 inode = iget5_locked(s, objectid, btrfs_find_actor,
4262 btrfs_init_locked_inode,
4263 (void *)&args);
4264 return inode;
4265}
4266
Balaji Rao1a54ef82008-07-21 02:01:04 +05304267/* Get an inode object given its location and corresponding root.
4268 * Returns in *is_new if the inode was read from disk
4269 */
4270struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004271 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304272{
4273 struct inode *inode;
4274
4275 inode = btrfs_iget_locked(s, location->objectid, root);
4276 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004277 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304278
4279 if (inode->i_state & I_NEW) {
4280 BTRFS_I(inode)->root = root;
4281 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4282 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004283 if (!is_bad_inode(inode)) {
4284 inode_tree_add(inode);
4285 unlock_new_inode(inode);
4286 if (new)
4287 *new = 1;
4288 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004289 unlock_new_inode(inode);
4290 iput(inode);
4291 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004292 }
4293 }
4294
Balaji Rao1a54ef82008-07-21 02:01:04 +05304295 return inode;
4296}
4297
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004298static struct inode *new_simple_dir(struct super_block *s,
4299 struct btrfs_key *key,
4300 struct btrfs_root *root)
4301{
4302 struct inode *inode = new_inode(s);
4303
4304 if (!inode)
4305 return ERR_PTR(-ENOMEM);
4306
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004307 BTRFS_I(inode)->root = root;
4308 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004309 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004310
4311 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004312 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313 inode->i_fop = &simple_dir_operations;
4314 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4315 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4316
4317 return inode;
4318}
4319
Chris Mason3de45862008-11-17 21:02:50 -05004320struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004321{
Chris Masond3977122009-01-05 21:25:51 -05004322 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004323 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004324 struct btrfs_root *sub_root = root;
4325 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004326 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004327 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004328
4329 if (dentry->d_name.len > BTRFS_NAME_LEN)
4330 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004331
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004332 if (unlikely(d_need_lookup(dentry))) {
4333 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4334 kfree(dentry->d_fsdata);
4335 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004336 /* This thing is hashed, drop it for now */
4337 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004338 } else {
4339 ret = btrfs_inode_by_name(dir, dentry, &location);
4340 }
Chris Mason5f39d392007-10-15 16:14:19 -04004341
Chris Mason39279cc2007-06-12 06:35:45 -04004342 if (ret < 0)
4343 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004344
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004345 if (location.objectid == 0)
4346 return NULL;
4347
4348 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004349 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004350 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004351 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004352
4353 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4354
Yan, Zheng76dda932009-09-21 16:00:26 -04004355 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004356 ret = fixup_tree_root_location(root, dir, dentry,
4357 &location, &sub_root);
4358 if (ret < 0) {
4359 if (ret != -ENOENT)
4360 inode = ERR_PTR(ret);
4361 else
4362 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4363 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004364 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004366 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4367
Julia Lawall34d19ba2011-01-24 19:55:19 +00004368 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004369 down_read(&root->fs_info->cleanup_work_sem);
4370 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004371 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004372 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004373 if (ret)
4374 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004375 }
4376
Chris Mason3de45862008-11-17 21:02:50 -05004377 return inode;
4378}
4379
Nick Pigginfe15ce42011-01-07 17:49:23 +11004380static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004381{
4382 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004383 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004384
Li Zefan848cce02012-02-21 17:04:28 +08004385 if (!inode && !IS_ROOT(dentry))
4386 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004387
Li Zefan848cce02012-02-21 17:04:28 +08004388 if (inode) {
4389 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004390 if (btrfs_root_refs(&root->root_item) == 0)
4391 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004392
4393 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4394 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004395 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004396 return 0;
4397}
4398
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004399static void btrfs_dentry_release(struct dentry *dentry)
4400{
4401 if (dentry->d_fsdata)
4402 kfree(dentry->d_fsdata);
4403}
4404
Chris Mason3de45862008-11-17 21:02:50 -05004405static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004406 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004407{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004408 struct dentry *ret;
4409
4410 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4411 if (unlikely(d_need_lookup(dentry))) {
4412 spin_lock(&dentry->d_lock);
4413 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4414 spin_unlock(&dentry->d_lock);
4415 }
4416 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004417}
4418
Miao Xie16cdcec2011-04-22 18:12:22 +08004419unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004420 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4421};
4422
David Woodhousecbdf5a22008-08-06 19:42:33 +01004423static int btrfs_real_readdir(struct file *filp, void *dirent,
4424 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004425{
Chris Mason6da6aba2007-12-18 16:15:09 -05004426 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 struct btrfs_root *root = BTRFS_I(inode)->root;
4428 struct btrfs_item *item;
4429 struct btrfs_dir_item *di;
4430 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004431 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004432 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004433 struct list_head ins_list;
4434 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004435 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004436 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004437 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004438 unsigned char d_type;
4439 int over = 0;
4440 u32 di_cur;
4441 u32 di_total;
4442 u32 di_len;
4443 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004444 char tmp_name[32];
4445 char *name_ptr;
4446 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004447 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004448
4449 /* FIXME, use a real flag for deciding about the key type */
4450 if (root->fs_info->tree_root == root)
4451 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004452
Chris Mason39544012007-12-12 14:38:19 -05004453 /* special case for "." */
4454 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004455 over = filldir(dirent, ".", 1,
4456 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004457 if (over)
4458 return 0;
4459 filp->f_pos = 1;
4460 }
Chris Mason39544012007-12-12 14:38:19 -05004461 /* special case for .., just use the back ref */
4462 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004463 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004464 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004465 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004466 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004467 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004468 filp->f_pos = 2;
4469 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004470 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004471 if (!path)
4472 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004473
Josef Bacik026fd312011-05-13 10:32:11 -04004474 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004475
Miao Xie16cdcec2011-04-22 18:12:22 +08004476 if (key_type == BTRFS_DIR_INDEX_KEY) {
4477 INIT_LIST_HEAD(&ins_list);
4478 INIT_LIST_HEAD(&del_list);
4479 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4480 }
4481
Chris Mason39279cc2007-06-12 06:35:45 -04004482 btrfs_set_key_type(&key, key_type);
4483 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004484 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004485
Chris Mason39279cc2007-06-12 06:35:45 -04004486 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4487 if (ret < 0)
4488 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004489
4490 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004491 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004492 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004493 if (slot >= btrfs_header_nritems(leaf)) {
4494 ret = btrfs_next_leaf(root, path);
4495 if (ret < 0)
4496 goto err;
4497 else if (ret > 0)
4498 break;
4499 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004500 }
Chris Mason3de45862008-11-17 21:02:50 -05004501
Chris Mason5f39d392007-10-15 16:14:19 -04004502 item = btrfs_item_nr(leaf, slot);
4503 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4504
4505 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004506 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004507 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004508 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004509 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004510 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004511 if (key_type == BTRFS_DIR_INDEX_KEY &&
4512 btrfs_should_delete_dir_index(&del_list,
4513 found_key.offset))
4514 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004515
4516 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004517 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004518
Chris Mason39279cc2007-06-12 06:35:45 -04004519 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4520 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004521 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004522
4523 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004524 struct btrfs_key location;
4525
Josef Bacik22a94d42011-03-16 16:47:17 -04004526 if (verify_dir_item(root, leaf, di))
4527 break;
4528
Chris Mason5f39d392007-10-15 16:14:19 -04004529 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004530 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004531 name_ptr = tmp_name;
4532 } else {
4533 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004534 if (!name_ptr) {
4535 ret = -ENOMEM;
4536 goto err;
4537 }
Chris Mason5f39d392007-10-15 16:14:19 -04004538 }
4539 read_extent_buffer(leaf, name_ptr,
4540 (unsigned long)(di + 1), name_len);
4541
4542 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4543 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004544
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004545
Chris Mason3de45862008-11-17 21:02:50 -05004546 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004547 * skip it.
4548 *
4549 * In contrast to old kernels, we insert the snapshot's
4550 * dir item and dir index after it has been created, so
4551 * we won't find a reference to our own snapshot. We
4552 * still keep the following code for backward
4553 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004554 */
4555 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4556 location.objectid == root->root_key.objectid) {
4557 over = 0;
4558 goto skip;
4559 }
Chris Mason5f39d392007-10-15 16:14:19 -04004560 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004561 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004562 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004563
Chris Mason3de45862008-11-17 21:02:50 -05004564skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004565 if (name_ptr != tmp_name)
4566 kfree(name_ptr);
4567
Chris Mason39279cc2007-06-12 06:35:45 -04004568 if (over)
4569 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004570 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004571 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004572 di_cur += di_len;
4573 di = (struct btrfs_dir_item *)((char *)di + di_len);
4574 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004575next:
4576 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004577 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004578
Miao Xie16cdcec2011-04-22 18:12:22 +08004579 if (key_type == BTRFS_DIR_INDEX_KEY) {
4580 if (is_curr)
4581 filp->f_pos++;
4582 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4583 &ins_list);
4584 if (ret)
4585 goto nopos;
4586 }
4587
David Woodhouse49593bf2008-08-17 17:08:36 +01004588 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004589 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004590 /*
4591 * 32-bit glibc will use getdents64, but then strtol -
4592 * so the last number we can serve is this.
4593 */
4594 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004595 else
4596 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004597nopos:
4598 ret = 0;
4599err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004600 if (key_type == BTRFS_DIR_INDEX_KEY)
4601 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004602 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004603 return ret;
4604}
4605
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004606int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004607{
4608 struct btrfs_root *root = BTRFS_I(inode)->root;
4609 struct btrfs_trans_handle *trans;
4610 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004611 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004612
Josef Bacik72ac3c02012-05-23 14:13:11 -04004613 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004614 return 0;
4615
Liu Bo83eea1f2012-07-10 05:28:39 -06004616 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004617 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004618
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004619 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004620 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004621 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004622 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004623 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004624 if (IS_ERR(trans))
4625 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004626 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 }
4628 return ret;
4629}
4630
4631/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004632 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004633 * inode changes. But, it is most likely to find the inode in cache.
4634 * FIXME, needs more benchmarking...there are no reasons other than performance
4635 * to keep or drop this code.
4636 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004637int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004638{
4639 struct btrfs_root *root = BTRFS_I(inode)->root;
4640 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004641 int ret;
4642
Josef Bacik72ac3c02012-05-23 14:13:11 -04004643 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004644 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004645
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004646 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004647 if (IS_ERR(trans))
4648 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004649
4650 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004651 if (ret && ret == -ENOSPC) {
4652 /* whoops, lets try again with the full transaction */
4653 btrfs_end_transaction(trans, root);
4654 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004655 if (IS_ERR(trans))
4656 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004657
Chris Mason94b60442010-05-26 11:02:00 -04004658 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004659 }
Chris Mason39279cc2007-06-12 06:35:45 -04004660 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004661 if (BTRFS_I(inode)->delayed_node)
4662 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004663
4664 return ret;
4665}
4666
4667/*
4668 * This is a copy of file_update_time. We need this so we can return error on
4669 * ENOSPC for updating the inode in the case of file write and mmap writes.
4670 */
Josef Bacike41f9412012-03-26 09:46:47 -04004671static int btrfs_update_time(struct inode *inode, struct timespec *now,
4672 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004673{
Alexander Block2bc5565282012-06-15 09:49:33 +02004674 struct btrfs_root *root = BTRFS_I(inode)->root;
4675
4676 if (btrfs_root_readonly(root))
4677 return -EROFS;
4678
Josef Bacike41f9412012-03-26 09:46:47 -04004679 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004680 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004681 if (flags & S_CTIME)
4682 inode->i_ctime = *now;
4683 if (flags & S_MTIME)
4684 inode->i_mtime = *now;
4685 if (flags & S_ATIME)
4686 inode->i_atime = *now;
4687 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004688}
4689
Chris Masond352ac62008-09-29 15:18:18 -04004690/*
4691 * find the highest existing sequence number in a directory
4692 * and then set the in-memory index_cnt variable to reflect
4693 * free sequence numbers
4694 */
Josef Bacikaec74772008-07-24 12:12:38 -04004695static int btrfs_set_inode_index_count(struct inode *inode)
4696{
4697 struct btrfs_root *root = BTRFS_I(inode)->root;
4698 struct btrfs_key key, found_key;
4699 struct btrfs_path *path;
4700 struct extent_buffer *leaf;
4701 int ret;
4702
Li Zefan33345d012011-04-20 10:31:50 +08004703 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004704 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4705 key.offset = (u64)-1;
4706
4707 path = btrfs_alloc_path();
4708 if (!path)
4709 return -ENOMEM;
4710
4711 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4712 if (ret < 0)
4713 goto out;
4714 /* FIXME: we should be able to handle this */
4715 if (ret == 0)
4716 goto out;
4717 ret = 0;
4718
4719 /*
4720 * MAGIC NUMBER EXPLANATION:
4721 * since we search a directory based on f_pos we have to start at 2
4722 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4723 * else has to start at 2
4724 */
4725 if (path->slots[0] == 0) {
4726 BTRFS_I(inode)->index_cnt = 2;
4727 goto out;
4728 }
4729
4730 path->slots[0]--;
4731
4732 leaf = path->nodes[0];
4733 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4734
Li Zefan33345d012011-04-20 10:31:50 +08004735 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004736 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4737 BTRFS_I(inode)->index_cnt = 2;
4738 goto out;
4739 }
4740
4741 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4742out:
4743 btrfs_free_path(path);
4744 return ret;
4745}
4746
Chris Masond352ac62008-09-29 15:18:18 -04004747/*
4748 * helper to find a free sequence number in a given directory. This current
4749 * code is very simple, later versions will do smarter things in the btree
4750 */
Chris Mason3de45862008-11-17 21:02:50 -05004751int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004752{
4753 int ret = 0;
4754
4755 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004756 ret = btrfs_inode_delayed_dir_index_count(dir);
4757 if (ret) {
4758 ret = btrfs_set_inode_index_count(dir);
4759 if (ret)
4760 return ret;
4761 }
Josef Bacikaec74772008-07-24 12:12:38 -04004762 }
4763
Chris Mason00e4e6b2008-08-05 11:18:09 -04004764 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004765 BTRFS_I(dir)->index_cnt++;
4766
4767 return ret;
4768}
4769
Chris Mason39279cc2007-06-12 06:35:45 -04004770static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4771 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004772 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004773 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004774 u64 ref_objectid, u64 objectid,
4775 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004776{
4777 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004778 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004779 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004780 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004781 struct btrfs_inode_ref *ref;
4782 struct btrfs_key key[2];
4783 u32 sizes[2];
4784 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004785 int ret;
4786 int owner;
4787
Chris Mason5f39d392007-10-15 16:14:19 -04004788 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004789 if (!path)
4790 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004791
Chris Mason39279cc2007-06-12 06:35:45 -04004792 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004793 if (!inode) {
4794 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004795 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004796 }
Chris Mason39279cc2007-06-12 06:35:45 -04004797
Li Zefan581bb052011-04-20 10:06:11 +08004798 /*
4799 * we have to initialize this early, so we can reclaim the inode
4800 * number if we fail afterwards in this function.
4801 */
4802 inode->i_ino = objectid;
4803
Josef Bacikaec74772008-07-24 12:12:38 -04004804 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004805 trace_btrfs_inode_request(dir);
4806
Chris Mason3de45862008-11-17 21:02:50 -05004807 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004808 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004809 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004810 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004811 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004812 }
Josef Bacikaec74772008-07-24 12:12:38 -04004813 }
4814 /*
4815 * index_cnt is ignored for everything but a dir,
4816 * btrfs_get_inode_index_count has an explanation for the magic
4817 * number
4818 */
4819 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004820 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004821 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004822 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004823
Josef Bacik5dc562c2012-08-17 13:14:17 -04004824 /*
4825 * We could have gotten an inode number from somebody who was fsynced
4826 * and then removed in this same transaction, so let's just set full
4827 * sync since it will be a full sync anyway and this will blow away the
4828 * old info in the log.
4829 */
4830 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4831
Al Viro569254b2011-07-24 17:08:40 -04004832 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004833 owner = 0;
4834 else
4835 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004836
4837 key[0].objectid = objectid;
4838 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4839 key[0].offset = 0;
4840
Mark Fashehf1863732012-08-08 11:32:27 -07004841 /*
4842 * Start new inodes with an inode_ref. This is slightly more
4843 * efficient for small numbers of hard links since they will
4844 * be packed into one item. Extended refs will kick in if we
4845 * add more hard links than can fit in the ref item.
4846 */
Chris Mason9c583092008-01-29 15:15:18 -05004847 key[1].objectid = objectid;
4848 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4849 key[1].offset = ref_objectid;
4850
4851 sizes[0] = sizeof(struct btrfs_inode_item);
4852 sizes[1] = name_len + sizeof(*ref);
4853
Chris Masonb9473432009-03-13 11:00:37 -04004854 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004855 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4856 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004857 goto fail;
4858
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004859 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004860 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004861 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004862 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4863 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004864 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4865 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004866 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004867
4868 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4869 struct btrfs_inode_ref);
4870 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004871 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004872 ptr = (unsigned long)(ref + 1);
4873 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4874
Chris Mason5f39d392007-10-15 16:14:19 -04004875 btrfs_mark_buffer_dirty(path->nodes[0]);
4876 btrfs_free_path(path);
4877
Chris Mason39279cc2007-06-12 06:35:45 -04004878 location = &BTRFS_I(inode)->location;
4879 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004880 location->offset = 0;
4881 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4882
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004883 btrfs_inherit_iflags(inode, dir);
4884
Al Viro569254b2011-07-24 17:08:40 -04004885 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004886 if (btrfs_test_opt(root, NODATASUM))
4887 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004888 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004889 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4890 }
4891
Chris Mason39279cc2007-06-12 06:35:45 -04004892 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004893 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004894
4895 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004896 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004897
Alexander Block8ea05e32012-07-25 17:35:53 +02004898 btrfs_update_root_times(trans, root);
4899
Chris Mason39279cc2007-06-12 06:35:45 -04004900 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004901fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004902 if (dir)
4903 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004904 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004905 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004906 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004907}
4908
4909static inline u8 btrfs_inode_type(struct inode *inode)
4910{
4911 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4912}
4913
Chris Masond352ac62008-09-29 15:18:18 -04004914/*
4915 * utility function to add 'inode' into 'parent_inode' with
4916 * a give name and a given sequence number.
4917 * if 'add_backref' is true, also insert a backref from the
4918 * inode to the parent directory.
4919 */
Chris Masone02119d2008-09-05 16:13:11 -04004920int btrfs_add_link(struct btrfs_trans_handle *trans,
4921 struct inode *parent_inode, struct inode *inode,
4922 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004923{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004924 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004925 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004926 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004927 u64 ino = btrfs_ino(inode);
4928 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004929
Li Zefan33345d012011-04-20 10:31:50 +08004930 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004931 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4932 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004933 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004934 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4935 key.offset = 0;
4936 }
Chris Mason39279cc2007-06-12 06:35:45 -04004937
Li Zefan33345d012011-04-20 10:31:50 +08004938 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004939 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4940 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004941 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004942 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004943 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4944 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004945 }
4946
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004947 /* Nothing to clean up yet */
4948 if (ret)
4949 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004950
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004951 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4952 parent_inode, &key,
4953 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004954 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004955 goto fail_dir_item;
4956 else if (ret) {
4957 btrfs_abort_transaction(trans, root, ret);
4958 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004959 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004960
4961 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4962 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004963 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004964 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4965 ret = btrfs_update_inode(trans, root, parent_inode);
4966 if (ret)
4967 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004968 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004969
4970fail_dir_item:
4971 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4972 u64 local_index;
4973 int err;
4974 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4975 key.objectid, root->root_key.objectid,
4976 parent_ino, &local_index, name, name_len);
4977
4978 } else if (add_backref) {
4979 u64 local_index;
4980 int err;
4981
4982 err = btrfs_del_inode_ref(trans, root, name, name_len,
4983 ino, parent_ino, &local_index);
4984 }
4985 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004986}
4987
4988static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004989 struct inode *dir, struct dentry *dentry,
4990 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004991{
Josef Bacika1b075d2010-11-19 20:36:11 +00004992 int err = btrfs_add_link(trans, dir, inode,
4993 dentry->d_name.name, dentry->d_name.len,
4994 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004995 if (err > 0)
4996 err = -EEXIST;
4997 return err;
4998}
4999
Josef Bacik618e21d2007-07-11 10:18:17 -04005000static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005001 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005002{
5003 struct btrfs_trans_handle *trans;
5004 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005005 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005006 int err;
5007 int drop_inode = 0;
5008 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005009 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005010
5011 if (!new_valid_dev(rdev))
5012 return -EINVAL;
5013
Josef Bacik9ed74f22009-09-11 16:12:44 -04005014 /*
5015 * 2 for inode item and ref
5016 * 2 for dir items
5017 * 1 for xattr if selinux is on
5018 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005019 trans = btrfs_start_transaction(root, 5);
5020 if (IS_ERR(trans))
5021 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005022
Li Zefan581bb052011-04-20 10:06:11 +08005023 err = btrfs_find_free_ino(root, &objectid);
5024 if (err)
5025 goto out_unlock;
5026
Josef Bacikaec74772008-07-24 12:12:38 -04005027 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005028 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005029 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005030 if (IS_ERR(inode)) {
5031 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005032 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005033 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005034
Eric Paris2a7dba32011-02-01 11:05:39 -05005035 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005036 if (err) {
5037 drop_inode = 1;
5038 goto out_unlock;
5039 }
5040
Casey Schauflerad19db72011-12-15 10:09:07 -05005041 /*
5042 * If the active LSM wants to access the inode during
5043 * d_instantiate it needs these. Smack checks to see
5044 * if the filesystem supports xattrs by looking at the
5045 * ops vector.
5046 */
5047
5048 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005049 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005050 if (err)
5051 drop_inode = 1;
5052 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005053 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005054 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005055 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005056 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005057out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005058 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005059 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005060 if (drop_inode) {
5061 inode_dec_link_count(inode);
5062 iput(inode);
5063 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005064 return err;
5065}
5066
Chris Mason39279cc2007-06-12 06:35:45 -04005067static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005068 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005069{
5070 struct btrfs_trans_handle *trans;
5071 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005072 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005073 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005074 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005075 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005076 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005077
Josef Bacik9ed74f22009-09-11 16:12:44 -04005078 /*
5079 * 2 for inode item and ref
5080 * 2 for dir items
5081 * 1 for xattr if selinux is on
5082 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005083 trans = btrfs_start_transaction(root, 5);
5084 if (IS_ERR(trans))
5085 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005086
Li Zefan581bb052011-04-20 10:06:11 +08005087 err = btrfs_find_free_ino(root, &objectid);
5088 if (err)
5089 goto out_unlock;
5090
Josef Bacikaec74772008-07-24 12:12:38 -04005091 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005092 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005093 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005094 if (IS_ERR(inode)) {
5095 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005096 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005097 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005098 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005099
Eric Paris2a7dba32011-02-01 11:05:39 -05005100 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005101 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005102 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005103
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005104 err = btrfs_update_inode(trans, root, inode);
5105 if (err)
5106 goto out_unlock;
5107
Casey Schauflerad19db72011-12-15 10:09:07 -05005108 /*
5109 * If the active LSM wants to access the inode during
5110 * d_instantiate it needs these. Smack checks to see
5111 * if the filesystem supports xattrs by looking at the
5112 * ops vector.
5113 */
5114 inode->i_fop = &btrfs_file_operations;
5115 inode->i_op = &btrfs_file_inode_operations;
5116
Josef Bacika1b075d2010-11-19 20:36:11 +00005117 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005118 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005119 goto out_unlock;
5120
5121 inode->i_mapping->a_ops = &btrfs_aops;
5122 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5124 d_instantiate(dentry, inode);
5125
Chris Mason39279cc2007-06-12 06:35:45 -04005126out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005127 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005128 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005129 inode_dec_link_count(inode);
5130 iput(inode);
5131 }
Liu Bob53d3f52012-11-14 14:34:34 +00005132 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005133 return err;
5134}
5135
5136static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5137 struct dentry *dentry)
5138{
5139 struct btrfs_trans_handle *trans;
5140 struct btrfs_root *root = BTRFS_I(dir)->root;
5141 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005142 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005143 int err;
5144 int drop_inode = 0;
5145
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005146 /* do not allow sys_link's with other subvols of the same device */
5147 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005148 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005149
Mark Fashehf1863732012-08-08 11:32:27 -07005150 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005151 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005152
Chris Mason3de45862008-11-17 21:02:50 -05005153 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005154 if (err)
5155 goto fail;
5156
Yan, Zhenga22285a2010-05-16 10:48:46 -04005157 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005158 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005159 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005160 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005161 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005162 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005163 if (IS_ERR(trans)) {
5164 err = PTR_ERR(trans);
5165 goto fail;
5166 }
Chris Mason5f39d392007-10-15 16:14:19 -04005167
Miao Xie31534952011-04-13 13:19:21 +08005168 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005169 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005170 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005171 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005172 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005173
Josef Bacika1b075d2010-11-19 20:36:11 +00005174 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005175
Yan, Zhenga5719522009-09-24 09:17:31 -04005176 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005177 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005178 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005179 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005180 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005181 if (err)
5182 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005183 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005184 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005185 }
Chris Mason39279cc2007-06-12 06:35:45 -04005186
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005187 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005188fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005189 if (drop_inode) {
5190 inode_dec_link_count(inode);
5191 iput(inode);
5192 }
Liu Bob53d3f52012-11-14 14:34:34 +00005193 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005194 return err;
5195}
5196
Al Viro18bb1db2011-07-26 01:41:39 -04005197static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005198{
Chris Masonb9d86662008-05-02 16:13:49 -04005199 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005200 struct btrfs_trans_handle *trans;
5201 struct btrfs_root *root = BTRFS_I(dir)->root;
5202 int err = 0;
5203 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005204 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005205 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005206
Josef Bacik9ed74f22009-09-11 16:12:44 -04005207 /*
5208 * 2 items for inode and ref
5209 * 2 items for dir items
5210 * 1 for xattr if selinux is on
5211 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005212 trans = btrfs_start_transaction(root, 5);
5213 if (IS_ERR(trans))
5214 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005215
Li Zefan581bb052011-04-20 10:06:11 +08005216 err = btrfs_find_free_ino(root, &objectid);
5217 if (err)
5218 goto out_fail;
5219
Josef Bacikaec74772008-07-24 12:12:38 -04005220 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005221 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005222 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005223 if (IS_ERR(inode)) {
5224 err = PTR_ERR(inode);
5225 goto out_fail;
5226 }
Chris Mason5f39d392007-10-15 16:14:19 -04005227
Chris Mason39279cc2007-06-12 06:35:45 -04005228 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005229
Eric Paris2a7dba32011-02-01 11:05:39 -05005230 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005231 if (err)
5232 goto out_fail;
5233
Chris Mason39279cc2007-06-12 06:35:45 -04005234 inode->i_op = &btrfs_dir_inode_operations;
5235 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005236
Chris Masondbe674a2008-07-17 12:54:05 -04005237 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005238 err = btrfs_update_inode(trans, root, inode);
5239 if (err)
5240 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005241
Josef Bacika1b075d2010-11-19 20:36:11 +00005242 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5243 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005244 if (err)
5245 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005246
Chris Mason39279cc2007-06-12 06:35:45 -04005247 d_instantiate(dentry, inode);
5248 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005249
5250out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005251 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005252 if (drop_on_err)
5253 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005254 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005255 return err;
5256}
5257
Chris Masond352ac62008-09-29 15:18:18 -04005258/* helper for btfs_get_extent. Given an existing extent in the tree,
5259 * and an extent that you want to insert, deal with overlap and insert
5260 * the new extent into the tree.
5261 */
Chris Mason3b951512008-04-17 11:29:12 -04005262static int merge_extent_mapping(struct extent_map_tree *em_tree,
5263 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005264 struct extent_map *em,
5265 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005266{
5267 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005268
Chris Masone6dcd2d2008-07-17 12:53:50 -04005269 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5270 start_diff = map_start - em->start;
5271 em->start = map_start;
5272 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005273 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5274 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005275 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005276 em->block_len -= start_diff;
5277 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005278 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005279}
5280
Chris Masonc8b97812008-10-29 14:49:59 -04005281static noinline int uncompress_inline(struct btrfs_path *path,
5282 struct inode *inode, struct page *page,
5283 size_t pg_offset, u64 extent_offset,
5284 struct btrfs_file_extent_item *item)
5285{
5286 int ret;
5287 struct extent_buffer *leaf = path->nodes[0];
5288 char *tmp;
5289 size_t max_size;
5290 unsigned long inline_size;
5291 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005292 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005293
5294 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005295 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005296 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5297 inline_size = btrfs_file_extent_inline_item_len(leaf,
5298 btrfs_item_nr(leaf, path->slots[0]));
5299 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005300 if (!tmp)
5301 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005302 ptr = btrfs_file_extent_inline_start(item);
5303
5304 read_extent_buffer(leaf, tmp, ptr, inline_size);
5305
Chris Mason5b050f02008-11-11 09:34:41 -05005306 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005307 ret = btrfs_decompress(compress_type, tmp, page,
5308 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005309 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005310 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005311 unsigned long copy_size = min_t(u64,
5312 PAGE_CACHE_SIZE - pg_offset,
5313 max_size - extent_offset);
5314 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005315 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005316 }
5317 kfree(tmp);
5318 return 0;
5319}
5320
Chris Masond352ac62008-09-29 15:18:18 -04005321/*
5322 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005323 * the ugly parts come from merging extents from the disk with the in-ram
5324 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005325 * where the in-ram extents might be locked pending data=ordered completion.
5326 *
5327 * This also copies inline extents directly into the page.
5328 */
Chris Masond3977122009-01-05 21:25:51 -05005329
Chris Masona52d9a82007-08-27 16:49:44 -04005330struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005331 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005332 int create)
5333{
5334 int ret;
5335 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005336 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005337 u64 extent_start = 0;
5338 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005339 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005340 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005341 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005342 struct btrfs_root *root = BTRFS_I(inode)->root;
5343 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005344 struct extent_buffer *leaf;
5345 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005346 struct extent_map *em = NULL;
5347 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005348 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005349 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005350 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005351
Chris Masona52d9a82007-08-27 16:49:44 -04005352again:
Chris Mason890871b2009-09-02 16:24:52 -04005353 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005354 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005355 if (em)
5356 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005357 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005358
Chris Masona52d9a82007-08-27 16:49:44 -04005359 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005360 if (em->start > start || em->start + em->len <= start)
5361 free_extent_map(em);
5362 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005363 free_extent_map(em);
5364 else
5365 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005366 }
David Sterba172ddd62011-04-21 00:48:27 +02005367 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005368 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005369 err = -ENOMEM;
5370 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005371 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005372 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005373 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005374 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005375 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005376 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005377
5378 if (!path) {
5379 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005380 if (!path) {
5381 err = -ENOMEM;
5382 goto out;
5383 }
5384 /*
5385 * Chances are we'll be called again, so go ahead and do
5386 * readahead
5387 */
5388 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005389 }
5390
Chris Mason179e29e2007-11-01 11:28:41 -04005391 ret = btrfs_lookup_file_extent(trans, root, path,
5392 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005393 if (ret < 0) {
5394 err = ret;
5395 goto out;
5396 }
5397
5398 if (ret != 0) {
5399 if (path->slots[0] == 0)
5400 goto not_found;
5401 path->slots[0]--;
5402 }
5403
Chris Mason5f39d392007-10-15 16:14:19 -04005404 leaf = path->nodes[0];
5405 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005406 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005407 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005408 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5409 found_type = btrfs_key_type(&found_key);
5410 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005411 found_type != BTRFS_EXTENT_DATA_KEY) {
5412 goto not_found;
5413 }
5414
Chris Mason5f39d392007-10-15 16:14:19 -04005415 found_type = btrfs_file_extent_type(leaf, item);
5416 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005417 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005418 if (found_type == BTRFS_FILE_EXTENT_REG ||
5419 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005420 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005421 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005422 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5423 size_t size;
5424 size = btrfs_file_extent_inline_len(leaf, item);
5425 extent_end = (extent_start + size + root->sectorsize - 1) &
5426 ~((u64)root->sectorsize - 1);
5427 }
5428
5429 if (start >= extent_end) {
5430 path->slots[0]++;
5431 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5432 ret = btrfs_next_leaf(root, path);
5433 if (ret < 0) {
5434 err = ret;
5435 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005436 }
Yan Zheng9036c102008-10-30 14:19:41 -04005437 if (ret > 0)
5438 goto not_found;
5439 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005440 }
Yan Zheng9036c102008-10-30 14:19:41 -04005441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5442 if (found_key.objectid != objectid ||
5443 found_key.type != BTRFS_EXTENT_DATA_KEY)
5444 goto not_found;
5445 if (start + len <= found_key.offset)
5446 goto not_found;
5447 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005448 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005449 em->len = found_key.offset - start;
5450 goto not_found_em;
5451 }
5452
Yan Zhengd899e052008-10-30 14:25:28 -04005453 if (found_type == BTRFS_FILE_EXTENT_REG ||
5454 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005455 em->start = extent_start;
5456 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005457 em->orig_start = extent_start -
5458 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005459 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5460 item);
Chris Masondb945352007-10-15 16:15:53 -04005461 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5462 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005463 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005464 goto insert;
5465 }
Li Zefan261507a02010-12-17 14:21:50 +08005466 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005467 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005468 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005469 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005470 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005471 } else {
5472 bytenr += btrfs_file_extent_offset(leaf, item);
5473 em->block_start = bytenr;
5474 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005475 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5476 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005477 }
Chris Masona52d9a82007-08-27 16:49:44 -04005478 goto insert;
5479 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005480 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005481 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005482 size_t size;
5483 size_t extent_offset;
5484 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005485
Chris Masona52d9a82007-08-27 16:49:44 -04005486 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005487 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005488 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005489 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005490 goto out;
5491 }
5492
Yan Zheng9036c102008-10-30 14:19:41 -04005493 size = btrfs_file_extent_inline_len(leaf, item);
5494 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005495 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005496 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005497 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005498 em->len = (copy_size + root->sectorsize - 1) &
5499 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005500 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005501 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005502 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005503 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005504 em->compress_type = compress_type;
5505 }
Yan689f9342007-10-29 11:41:07 -04005506 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005507 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005508 if (btrfs_file_extent_compression(leaf, item) !=
5509 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005510 ret = uncompress_inline(path, inode, page,
5511 pg_offset,
5512 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005513 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005514 } else {
5515 map = kmap(page);
5516 read_extent_buffer(leaf, map + pg_offset, ptr,
5517 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005518 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5519 memset(map + pg_offset + copy_size, 0,
5520 PAGE_CACHE_SIZE - pg_offset -
5521 copy_size);
5522 }
Chris Masonc8b97812008-10-29 14:49:59 -04005523 kunmap(page);
5524 }
Chris Mason179e29e2007-11-01 11:28:41 -04005525 flush_dcache_page(page);
5526 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005527 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005528 if (!trans) {
5529 kunmap(page);
5530 free_extent_map(em);
5531 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005532
David Sterbab3b4aa72011-04-21 01:20:15 +02005533 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005534 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005535
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005536 if (IS_ERR(trans))
5537 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005538 goto again;
5539 }
Chris Masonc8b97812008-10-29 14:49:59 -04005540 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005541 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005542 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005543 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005544 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005545 }
Chris Masond1310b22008-01-24 16:13:08 -05005546 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005547 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005548 goto insert;
5549 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005550 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005551 }
5552not_found:
5553 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005554 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005555 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005556not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005557 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005558 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005559insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005560 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005561 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005562 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5563 "[%llu %llu]\n", (unsigned long long)em->start,
5564 (unsigned long long)em->len,
5565 (unsigned long long)start,
5566 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005567 err = -EIO;
5568 goto out;
5569 }
Chris Masond1310b22008-01-24 16:13:08 -05005570
5571 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005572 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005573 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005574 /* it is possible that someone inserted the extent into the tree
5575 * while we had the lock dropped. It is also possible that
5576 * an overlapping map exists in the tree
5577 */
Chris Masona52d9a82007-08-27 16:49:44 -04005578 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005579 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005580
5581 ret = 0;
5582
Chris Mason3b951512008-04-17 11:29:12 -04005583 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005584 if (existing && (existing->start > start ||
5585 existing->start + existing->len <= start)) {
5586 free_extent_map(existing);
5587 existing = NULL;
5588 }
Chris Mason3b951512008-04-17 11:29:12 -04005589 if (!existing) {
5590 existing = lookup_extent_mapping(em_tree, em->start,
5591 em->len);
5592 if (existing) {
5593 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005594 em, start,
5595 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005596 free_extent_map(existing);
5597 if (err) {
5598 free_extent_map(em);
5599 em = NULL;
5600 }
5601 } else {
5602 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005603 free_extent_map(em);
5604 em = NULL;
5605 }
5606 } else {
5607 free_extent_map(em);
5608 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005609 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005610 }
Chris Masona52d9a82007-08-27 16:49:44 -04005611 }
Chris Mason890871b2009-09-02 16:24:52 -04005612 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005613out:
liubo1abe9b82011-03-24 11:18:59 +00005614
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005615 if (em)
5616 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005617
Chris Masonf4219502008-07-22 11:18:09 -04005618 if (path)
5619 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005620 if (trans) {
5621 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005622 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005623 err = ret;
5624 }
Chris Masona52d9a82007-08-27 16:49:44 -04005625 if (err) {
5626 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005627 return ERR_PTR(err);
5628 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005629 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005630 return em;
5631}
5632
Chris Masonec29ed52011-02-23 16:23:20 -05005633struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5634 size_t pg_offset, u64 start, u64 len,
5635 int create)
5636{
5637 struct extent_map *em;
5638 struct extent_map *hole_em = NULL;
5639 u64 range_start = start;
5640 u64 end;
5641 u64 found;
5642 u64 found_end;
5643 int err = 0;
5644
5645 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5646 if (IS_ERR(em))
5647 return em;
5648 if (em) {
5649 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005650 * if our em maps to
5651 * - a hole or
5652 * - a pre-alloc extent,
5653 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005654 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005655 if (em->block_start != EXTENT_MAP_HOLE &&
5656 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005657 return em;
5658 else
5659 hole_em = em;
5660 }
5661
5662 /* check to see if we've wrapped (len == -1 or similar) */
5663 end = start + len;
5664 if (end < start)
5665 end = (u64)-1;
5666 else
5667 end -= 1;
5668
5669 em = NULL;
5670
5671 /* ok, we didn't find anything, lets look for delalloc */
5672 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5673 end, len, EXTENT_DELALLOC, 1);
5674 found_end = range_start + found;
5675 if (found_end < range_start)
5676 found_end = (u64)-1;
5677
5678 /*
5679 * we didn't find anything useful, return
5680 * the original results from get_extent()
5681 */
5682 if (range_start > end || found_end <= start) {
5683 em = hole_em;
5684 hole_em = NULL;
5685 goto out;
5686 }
5687
5688 /* adjust the range_start to make sure it doesn't
5689 * go backwards from the start they passed in
5690 */
5691 range_start = max(start,range_start);
5692 found = found_end - range_start;
5693
5694 if (found > 0) {
5695 u64 hole_start = start;
5696 u64 hole_len = len;
5697
David Sterba172ddd62011-04-21 00:48:27 +02005698 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005699 if (!em) {
5700 err = -ENOMEM;
5701 goto out;
5702 }
5703 /*
5704 * when btrfs_get_extent can't find anything it
5705 * returns one huge hole
5706 *
5707 * make sure what it found really fits our range, and
5708 * adjust to make sure it is based on the start from
5709 * the caller
5710 */
5711 if (hole_em) {
5712 u64 calc_end = extent_map_end(hole_em);
5713
5714 if (calc_end <= start || (hole_em->start > end)) {
5715 free_extent_map(hole_em);
5716 hole_em = NULL;
5717 } else {
5718 hole_start = max(hole_em->start, start);
5719 hole_len = calc_end - hole_start;
5720 }
5721 }
5722 em->bdev = NULL;
5723 if (hole_em && range_start > hole_start) {
5724 /* our hole starts before our delalloc, so we
5725 * have to return just the parts of the hole
5726 * that go until the delalloc starts
5727 */
5728 em->len = min(hole_len,
5729 range_start - hole_start);
5730 em->start = hole_start;
5731 em->orig_start = hole_start;
5732 /*
5733 * don't adjust block start at all,
5734 * it is fixed at EXTENT_MAP_HOLE
5735 */
5736 em->block_start = hole_em->block_start;
5737 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005738 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5739 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005740 } else {
5741 em->start = range_start;
5742 em->len = found;
5743 em->orig_start = range_start;
5744 em->block_start = EXTENT_MAP_DELALLOC;
5745 em->block_len = found;
5746 }
5747 } else if (hole_em) {
5748 return hole_em;
5749 }
5750out:
5751
5752 free_extent_map(hole_em);
5753 if (err) {
5754 free_extent_map(em);
5755 return ERR_PTR(err);
5756 }
5757 return em;
5758}
5759
Josef Bacik4b46fce2010-05-23 11:00:55 -04005760static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5761 u64 start, u64 len)
5762{
5763 struct btrfs_root *root = BTRFS_I(inode)->root;
5764 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005765 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005766 struct btrfs_key ins;
5767 u64 alloc_hint;
5768 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005769
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005770 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005771 if (IS_ERR(trans))
5772 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005773
5774 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5775
5776 alloc_hint = get_extent_allocation_hint(inode, start, len);
5777 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005778 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005779 if (ret) {
5780 em = ERR_PTR(ret);
5781 goto out;
5782 }
5783
Josef Bacik70c8a912012-10-11 16:54:30 -04005784 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5785 ins.offset, ins.offset, 0);
5786 if (IS_ERR(em))
5787 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005788
5789 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5790 ins.offset, ins.offset, 0);
5791 if (ret) {
5792 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5793 em = ERR_PTR(ret);
5794 }
5795out:
5796 btrfs_end_transaction(trans, root);
5797 return em;
5798}
5799
Chris Mason46bfbb52010-05-26 11:04:10 -04005800/*
5801 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5802 * block must be cow'd
5803 */
5804static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5805 struct inode *inode, u64 offset, u64 len)
5806{
5807 struct btrfs_path *path;
5808 int ret;
5809 struct extent_buffer *leaf;
5810 struct btrfs_root *root = BTRFS_I(inode)->root;
5811 struct btrfs_file_extent_item *fi;
5812 struct btrfs_key key;
5813 u64 disk_bytenr;
5814 u64 backref_offset;
5815 u64 extent_end;
5816 u64 num_bytes;
5817 int slot;
5818 int found_type;
5819
5820 path = btrfs_alloc_path();
5821 if (!path)
5822 return -ENOMEM;
5823
Li Zefan33345d012011-04-20 10:31:50 +08005824 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005825 offset, 0);
5826 if (ret < 0)
5827 goto out;
5828
5829 slot = path->slots[0];
5830 if (ret == 1) {
5831 if (slot == 0) {
5832 /* can't find the item, must cow */
5833 ret = 0;
5834 goto out;
5835 }
5836 slot--;
5837 }
5838 ret = 0;
5839 leaf = path->nodes[0];
5840 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005841 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005842 key.type != BTRFS_EXTENT_DATA_KEY) {
5843 /* not our file or wrong item type, must cow */
5844 goto out;
5845 }
5846
5847 if (key.offset > offset) {
5848 /* Wrong offset, must cow */
5849 goto out;
5850 }
5851
5852 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5853 found_type = btrfs_file_extent_type(leaf, fi);
5854 if (found_type != BTRFS_FILE_EXTENT_REG &&
5855 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5856 /* not a regular extent, must cow */
5857 goto out;
5858 }
5859 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5860 backref_offset = btrfs_file_extent_offset(leaf, fi);
5861
5862 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5863 if (extent_end < offset + len) {
5864 /* extent doesn't include our full range, must cow */
5865 goto out;
5866 }
5867
5868 if (btrfs_extent_readonly(root, disk_bytenr))
5869 goto out;
5870
5871 /*
5872 * look for other files referencing this extent, if we
5873 * find any we must cow
5874 */
Li Zefan33345d012011-04-20 10:31:50 +08005875 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005876 key.offset - backref_offset, disk_bytenr))
5877 goto out;
5878
5879 /*
5880 * adjust disk_bytenr and num_bytes to cover just the bytes
5881 * in this extent we are about to write. If there
5882 * are any csums in that range we have to cow in order
5883 * to keep the csums correct
5884 */
5885 disk_bytenr += backref_offset;
5886 disk_bytenr += offset - key.offset;
5887 num_bytes = min(offset + len, extent_end) - offset;
5888 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5889 goto out;
5890 /*
5891 * all of the above have passed, it is safe to overwrite this extent
5892 * without cow
5893 */
5894 ret = 1;
5895out:
5896 btrfs_free_path(path);
5897 return ret;
5898}
5899
Josef Bacikeb838e72012-07-31 16:28:48 -04005900static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5901 struct extent_state **cached_state, int writing)
5902{
5903 struct btrfs_ordered_extent *ordered;
5904 int ret = 0;
5905
5906 while (1) {
5907 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5908 0, cached_state);
5909 /*
5910 * We're concerned with the entire range that we're going to be
5911 * doing DIO to, so we need to make sure theres no ordered
5912 * extents in this range.
5913 */
5914 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5915 lockend - lockstart + 1);
5916
5917 /*
5918 * We need to make sure there are no buffered pages in this
5919 * range either, we could have raced between the invalidate in
5920 * generic_file_direct_write and locking the extent. The
5921 * invalidate needs to happen so that reads after a write do not
5922 * get stale data.
5923 */
5924 if (!ordered && (!writing ||
5925 !test_range_bit(&BTRFS_I(inode)->io_tree,
5926 lockstart, lockend, EXTENT_UPTODATE, 0,
5927 *cached_state)))
5928 break;
5929
5930 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5931 cached_state, GFP_NOFS);
5932
5933 if (ordered) {
5934 btrfs_start_ordered_extent(inode, ordered, 1);
5935 btrfs_put_ordered_extent(ordered);
5936 } else {
5937 /* Screw you mmap */
5938 ret = filemap_write_and_wait_range(inode->i_mapping,
5939 lockstart,
5940 lockend);
5941 if (ret)
5942 break;
5943
5944 /*
5945 * If we found a page that couldn't be invalidated just
5946 * fall back to buffered.
5947 */
5948 ret = invalidate_inode_pages2_range(inode->i_mapping,
5949 lockstart >> PAGE_CACHE_SHIFT,
5950 lockend >> PAGE_CACHE_SHIFT);
5951 if (ret)
5952 break;
5953 }
5954
5955 cond_resched();
5956 }
5957
5958 return ret;
5959}
5960
Josef Bacik69ffb542012-09-11 15:40:07 -04005961static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5962 u64 len, u64 orig_start,
5963 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005964 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005965{
5966 struct extent_map_tree *em_tree;
5967 struct extent_map *em;
5968 struct btrfs_root *root = BTRFS_I(inode)->root;
5969 int ret;
5970
5971 em_tree = &BTRFS_I(inode)->extent_tree;
5972 em = alloc_extent_map();
5973 if (!em)
5974 return ERR_PTR(-ENOMEM);
5975
5976 em->start = start;
5977 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04005978 em->mod_start = start;
5979 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04005980 em->len = len;
5981 em->block_len = block_len;
5982 em->block_start = block_start;
5983 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05005984 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005985 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04005986 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5987 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05005988 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04005989
5990 do {
5991 btrfs_drop_extent_cache(inode, em->start,
5992 em->start + em->len - 1, 0);
5993 write_lock(&em_tree->lock);
5994 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04005995 if (!ret)
5996 list_move(&em->list,
5997 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04005998 write_unlock(&em_tree->lock);
5999 } while (ret == -EEXIST);
6000
6001 if (ret) {
6002 free_extent_map(em);
6003 return ERR_PTR(ret);
6004 }
6005
6006 return em;
6007}
6008
6009
Josef Bacik4b46fce2010-05-23 11:00:55 -04006010static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6011 struct buffer_head *bh_result, int create)
6012{
6013 struct extent_map *em;
6014 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006015 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006016 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006017 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006018 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006019 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006020 int unlock_bits = EXTENT_LOCKED;
6021 int ret;
6022
Josef Bacikeb838e72012-07-31 16:28:48 -04006023 if (create) {
6024 ret = btrfs_delalloc_reserve_space(inode, len);
6025 if (ret)
6026 return ret;
6027 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04006028 } else {
6029 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006030 }
6031
Josef Bacikc3298612012-08-03 16:49:19 -04006032 lockstart = start;
6033 lockend = start + len - 1;
6034
Josef Bacikeb838e72012-07-31 16:28:48 -04006035 /*
6036 * If this errors out it's because we couldn't invalidate pagecache for
6037 * this range and we need to fallback to buffered.
6038 */
6039 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6040 return -ENOTBLK;
6041
6042 if (create) {
6043 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6044 lockend, EXTENT_DELALLOC, NULL,
6045 &cached_state, GFP_NOFS);
6046 if (ret)
6047 goto unlock_err;
6048 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006049
6050 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006051 if (IS_ERR(em)) {
6052 ret = PTR_ERR(em);
6053 goto unlock_err;
6054 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006055
6056 /*
6057 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6058 * io. INLINE is special, and we could probably kludge it in here, but
6059 * it's still buffered so for safety lets just fall back to the generic
6060 * buffered path.
6061 *
6062 * For COMPRESSED we _have_ to read the entire extent in so we can
6063 * decompress it, so there will be buffering required no matter what we
6064 * do, so go ahead and fallback to buffered.
6065 *
6066 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6067 * to buffered IO. Don't blame me, this is the price we pay for using
6068 * the generic code.
6069 */
6070 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6071 em->block_start == EXTENT_MAP_INLINE) {
6072 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006073 ret = -ENOTBLK;
6074 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006075 }
6076
6077 /* Just a good old fashioned hole, return */
6078 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6079 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6080 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006081 ret = 0;
6082 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006083 }
6084
6085 /*
6086 * We don't allocate a new extent in the following cases
6087 *
6088 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6089 * existing extent.
6090 * 2) The extent is marked as PREALLOC. We're good to go here and can
6091 * just use the extent.
6092 *
6093 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006094 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006095 len = min(len, em->len - (start - em->start));
6096 lockstart = start + len;
6097 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006098 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006099
6100 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6101 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6102 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006103 int type;
6104 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006105 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006106
6107 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6108 type = BTRFS_ORDERED_PREALLOC;
6109 else
6110 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006111 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006112 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006113
6114 /*
6115 * we're not going to log anything, but we do need
6116 * to make sure the current transaction stays open
6117 * while we look for nocow cross refs
6118 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006119 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006120 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006121 goto must_cow;
6122
6123 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006124 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006125 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006126
6127 if (type == BTRFS_ORDERED_PREALLOC) {
6128 free_extent_map(em);
6129 em = create_pinned_em(inode, start, len,
6130 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006131 block_start, len,
6132 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006133 if (IS_ERR(em)) {
6134 btrfs_end_transaction(trans, root);
6135 goto unlock_err;
6136 }
6137 }
6138
Chris Mason46bfbb52010-05-26 11:04:10 -04006139 ret = btrfs_add_ordered_extent_dio(inode, start,
6140 block_start, len, len, type);
6141 btrfs_end_transaction(trans, root);
6142 if (ret) {
6143 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006144 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006145 }
6146 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006147 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006148 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006149 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006150must_cow:
6151 /*
6152 * this will cow the extent, reset the len in case we changed
6153 * it above
6154 */
6155 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006156 free_extent_map(em);
6157 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006158 if (IS_ERR(em)) {
6159 ret = PTR_ERR(em);
6160 goto unlock_err;
6161 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006162 len = min(len, em->len - (start - em->start));
6163unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006164 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6165 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006166 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006167 bh_result->b_bdev = em->bdev;
6168 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006169 if (create) {
6170 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6171 set_buffer_new(bh_result);
6172
6173 /*
6174 * Need to update the i_size under the extent lock so buffered
6175 * readers will get the updated i_size when we unlock.
6176 */
6177 if (start + len > i_size_read(inode))
6178 i_size_write(inode, start + len);
6179 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006180
Josef Bacikeb838e72012-07-31 16:28:48 -04006181 /*
6182 * In the case of write we need to clear and unlock the entire range,
6183 * in the case of read we need to unlock only the end area that we
6184 * aren't using if there is any left over space.
6185 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006186 if (lockstart < lockend) {
6187 if (create && len < lockend - lockstart) {
6188 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006189 lockstart + len - 1,
6190 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006191 &cached_state, GFP_NOFS);
6192 /*
6193 * Beside unlock, we also need to cleanup reserved space
6194 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6195 */
6196 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6197 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006198 unlock_bits | EXTENT_DO_ACCOUNTING |
6199 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006200 } else {
6201 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6202 lockend, unlock_bits, 1, 0,
6203 &cached_state, GFP_NOFS);
6204 }
6205 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006206 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006207 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006208
Josef Bacik4b46fce2010-05-23 11:00:55 -04006209 free_extent_map(em);
6210
6211 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006212
6213unlock_err:
6214 if (create)
6215 unlock_bits |= EXTENT_DO_ACCOUNTING;
6216
6217 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6218 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6219 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006220}
6221
6222struct btrfs_dio_private {
6223 struct inode *inode;
6224 u64 logical_offset;
6225 u64 disk_bytenr;
6226 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006227 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006228
6229 /* number of bios pending for this dio */
6230 atomic_t pending_bios;
6231
6232 /* IO errors */
6233 int errors;
6234
6235 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236};
6237
6238static void btrfs_endio_direct_read(struct bio *bio, int err)
6239{
Miao Xiee65e1532010-11-22 03:04:43 +00006240 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006241 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6242 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006243 struct inode *inode = dip->inode;
6244 struct btrfs_root *root = BTRFS_I(inode)->root;
6245 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006246
6247 start = dip->logical_offset;
6248 do {
6249 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6250 struct page *page = bvec->bv_page;
6251 char *kaddr;
6252 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006253 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006254 unsigned long flags;
6255
Josef Bacikc3298612012-08-03 16:49:19 -04006256 if (get_state_private(&BTRFS_I(inode)->io_tree,
6257 start, &private))
6258 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006259 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006260 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006261 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6262 csum, bvec->bv_len);
6263 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006264 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006265 local_irq_restore(flags);
6266
6267 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006268 if (csum != private) {
6269failed:
Li Zefan33345d012011-04-20 10:31:50 +08006270 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006271 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006272 (unsigned long long)btrfs_ino(inode),
6273 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006274 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006275 err = -EIO;
6276 }
6277 }
6278
6279 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006280 bvec++;
6281 } while (bvec <= bvec_end);
6282
6283 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006284 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006285 bio->bi_private = dip->private;
6286
Josef Bacik4b46fce2010-05-23 11:00:55 -04006287 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006288
6289 /* If we had a csum failure make sure to clear the uptodate flag */
6290 if (err)
6291 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006292 dio_end_io(bio, err);
6293}
6294
6295static void btrfs_endio_direct_write(struct bio *bio, int err)
6296{
6297 struct btrfs_dio_private *dip = bio->bi_private;
6298 struct inode *inode = dip->inode;
6299 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006300 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006301 u64 ordered_offset = dip->logical_offset;
6302 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006303 int ret;
6304
6305 if (err)
6306 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006307again:
6308 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6309 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006310 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006311 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006312 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006313
Josef Bacik5fd02042012-05-02 14:00:54 -04006314 ordered->work.func = finish_ordered_fn;
6315 ordered->work.flags = 0;
6316 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6317 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006318out_test:
6319 /*
6320 * our bio might span multiple ordered extents. If we haven't
6321 * completed the accounting for the whole dio, go back and try again
6322 */
6323 if (ordered_offset < dip->logical_offset + dip->bytes) {
6324 ordered_bytes = dip->logical_offset + dip->bytes -
6325 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006326 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006327 goto again;
6328 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006329out_done:
6330 bio->bi_private = dip->private;
6331
Josef Bacik4b46fce2010-05-23 11:00:55 -04006332 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006333
6334 /* If we had an error make sure to clear the uptodate flag */
6335 if (err)
6336 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006337 dio_end_io(bio, err);
6338}
6339
Chris Masoneaf25d92010-05-25 09:48:28 -04006340static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6341 struct bio *bio, int mirror_num,
6342 unsigned long bio_flags, u64 offset)
6343{
6344 int ret;
6345 struct btrfs_root *root = BTRFS_I(inode)->root;
6346 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006347 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006348 return 0;
6349}
6350
Miao Xiee65e1532010-11-22 03:04:43 +00006351static void btrfs_end_dio_bio(struct bio *bio, int err)
6352{
6353 struct btrfs_dio_private *dip = bio->bi_private;
6354
6355 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006356 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006357 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006358 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006359 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006360 dip->errors = 1;
6361
6362 /*
6363 * before atomic variable goto zero, we must make sure
6364 * dip->errors is perceived to be set.
6365 */
6366 smp_mb__before_atomic_dec();
6367 }
6368
6369 /* if there are more bios still pending for this dio, just exit */
6370 if (!atomic_dec_and_test(&dip->pending_bios))
6371 goto out;
6372
6373 if (dip->errors)
6374 bio_io_error(dip->orig_bio);
6375 else {
6376 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6377 bio_endio(dip->orig_bio, 0);
6378 }
6379out:
6380 bio_put(bio);
6381}
6382
6383static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6384 u64 first_sector, gfp_t gfp_flags)
6385{
6386 int nr_vecs = bio_get_nr_vecs(bdev);
6387 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6388}
6389
6390static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6391 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006392 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006393{
6394 int write = rw & REQ_WRITE;
6395 struct btrfs_root *root = BTRFS_I(inode)->root;
6396 int ret;
6397
Josef Bacikb812ce22012-11-16 13:56:32 -05006398 if (async_submit)
6399 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6400
Miao Xiee65e1532010-11-22 03:04:43 +00006401 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006402
6403 if (!write) {
6404 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6405 if (ret)
6406 goto err;
6407 }
Miao Xiee65e1532010-11-22 03:04:43 +00006408
Josef Bacik1ae39932011-04-06 14:41:34 -04006409 if (skip_sum)
6410 goto map;
6411
6412 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006413 ret = btrfs_wq_submit_bio(root->fs_info,
6414 inode, rw, bio, 0, 0,
6415 file_offset,
6416 __btrfs_submit_bio_start_direct_io,
6417 __btrfs_submit_bio_done);
6418 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006419 } else if (write) {
6420 /*
6421 * If we aren't doing async submit, calculate the csum of the
6422 * bio now.
6423 */
6424 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6425 if (ret)
6426 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006427 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006428 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006429 if (ret)
6430 goto err;
6431 }
Miao Xiee65e1532010-11-22 03:04:43 +00006432
Josef Bacik1ae39932011-04-06 14:41:34 -04006433map:
6434 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006435err:
6436 bio_put(bio);
6437 return ret;
6438}
6439
6440static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6441 int skip_sum)
6442{
6443 struct inode *inode = dip->inode;
6444 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006445 struct bio *bio;
6446 struct bio *orig_bio = dip->orig_bio;
6447 struct bio_vec *bvec = orig_bio->bi_io_vec;
6448 u64 start_sector = orig_bio->bi_sector;
6449 u64 file_offset = dip->logical_offset;
6450 u64 submit_len = 0;
6451 u64 map_length;
6452 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006453 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006454 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006455
Miao Xiee65e1532010-11-22 03:04:43 +00006456 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006457 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006458 &map_length, NULL, 0);
6459 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006460 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006461 return -EIO;
6462 }
6463
Josef Bacik02f57c72011-04-06 14:25:44 -04006464 if (map_length >= orig_bio->bi_size) {
6465 bio = orig_bio;
6466 goto submit;
6467 }
6468
Josef Bacik1ae39932011-04-06 14:41:34 -04006469 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006470 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6471 if (!bio)
6472 return -ENOMEM;
6473 bio->bi_private = dip;
6474 bio->bi_end_io = btrfs_end_dio_bio;
6475 atomic_inc(&dip->pending_bios);
6476
Miao Xiee65e1532010-11-22 03:04:43 +00006477 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6478 if (unlikely(map_length < submit_len + bvec->bv_len ||
6479 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6480 bvec->bv_offset) < bvec->bv_len)) {
6481 /*
6482 * inc the count before we submit the bio so
6483 * we know the end IO handler won't happen before
6484 * we inc the count. Otherwise, the dip might get freed
6485 * before we're done setting it up
6486 */
6487 atomic_inc(&dip->pending_bios);
6488 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6489 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006490 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006491 if (ret) {
6492 bio_put(bio);
6493 atomic_dec(&dip->pending_bios);
6494 goto out_err;
6495 }
6496
Miao Xiee65e1532010-11-22 03:04:43 +00006497 start_sector += submit_len >> 9;
6498 file_offset += submit_len;
6499
6500 submit_len = 0;
6501 nr_pages = 0;
6502
6503 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6504 start_sector, GFP_NOFS);
6505 if (!bio)
6506 goto out_err;
6507 bio->bi_private = dip;
6508 bio->bi_end_io = btrfs_end_dio_bio;
6509
6510 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006511 ret = btrfs_map_block(root->fs_info, READ,
6512 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006513 &map_length, NULL, 0);
6514 if (ret) {
6515 bio_put(bio);
6516 goto out_err;
6517 }
6518 } else {
6519 submit_len += bvec->bv_len;
6520 nr_pages ++;
6521 bvec++;
6522 }
6523 }
6524
Josef Bacik02f57c72011-04-06 14:25:44 -04006525submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006526 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006527 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006528 if (!ret)
6529 return 0;
6530
6531 bio_put(bio);
6532out_err:
6533 dip->errors = 1;
6534 /*
6535 * before atomic variable goto zero, we must
6536 * make sure dip->errors is perceived to be set.
6537 */
6538 smp_mb__before_atomic_dec();
6539 if (atomic_dec_and_test(&dip->pending_bios))
6540 bio_io_error(dip->orig_bio);
6541
6542 /* bio_end_io() will handle error, so we needn't return it */
6543 return 0;
6544}
6545
Josef Bacik4b46fce2010-05-23 11:00:55 -04006546static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6547 loff_t file_offset)
6548{
6549 struct btrfs_root *root = BTRFS_I(inode)->root;
6550 struct btrfs_dio_private *dip;
6551 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006552 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006553 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006554 int ret = 0;
6555
6556 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6557
6558 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6559 if (!dip) {
6560 ret = -ENOMEM;
6561 goto free_ordered;
6562 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006563
6564 dip->private = bio->bi_private;
6565 dip->inode = inode;
6566 dip->logical_offset = file_offset;
6567
Josef Bacik4b46fce2010-05-23 11:00:55 -04006568 dip->bytes = 0;
6569 do {
6570 dip->bytes += bvec->bv_len;
6571 bvec++;
6572 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6573
Chris Mason46bfbb52010-05-26 11:04:10 -04006574 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006575 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006576 dip->errors = 0;
6577 dip->orig_bio = bio;
6578 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006579
6580 if (write)
6581 bio->bi_end_io = btrfs_endio_direct_write;
6582 else
6583 bio->bi_end_io = btrfs_endio_direct_read;
6584
Miao Xiee65e1532010-11-22 03:04:43 +00006585 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6586 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006587 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006588free_ordered:
6589 /*
6590 * If this is a write, we need to clean up the reserved space and kill
6591 * the ordered extent.
6592 */
6593 if (write) {
6594 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006595 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006596 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6597 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6598 btrfs_free_reserved_extent(root, ordered->start,
6599 ordered->disk_len);
6600 btrfs_put_ordered_extent(ordered);
6601 btrfs_put_ordered_extent(ordered);
6602 }
6603 bio_endio(bio, ret);
6604}
6605
Chris Mason5a5f79b2010-05-26 21:33:37 -04006606static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6607 const struct iovec *iov, loff_t offset,
6608 unsigned long nr_segs)
6609{
6610 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006611 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006612 size_t size;
6613 unsigned long addr;
6614 unsigned blocksize_mask = root->sectorsize - 1;
6615 ssize_t retval = -EINVAL;
6616 loff_t end = offset;
6617
6618 if (offset & blocksize_mask)
6619 goto out;
6620
6621 /* Check the memory alignment. Blocks cannot straddle pages */
6622 for (seg = 0; seg < nr_segs; seg++) {
6623 addr = (unsigned long)iov[seg].iov_base;
6624 size = iov[seg].iov_len;
6625 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006626 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006627 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006628
6629 /* If this is a write we don't need to check anymore */
6630 if (rw & WRITE)
6631 continue;
6632
6633 /*
6634 * Check to make sure we don't have duplicate iov_base's in this
6635 * iovec, if so return EINVAL, otherwise we'll get csum errors
6636 * when reading back.
6637 */
6638 for (i = seg + 1; i < nr_segs; i++) {
6639 if (iov[seg].iov_base == iov[i].iov_base)
6640 goto out;
6641 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006642 }
6643 retval = 0;
6644out:
6645 return retval;
6646}
Josef Bacikeb838e72012-07-31 16:28:48 -04006647
Chris Mason16432982008-04-10 10:23:21 -04006648static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6649 const struct iovec *iov, loff_t offset,
6650 unsigned long nr_segs)
6651{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006652 struct file *file = iocb->ki_filp;
6653 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006654
Chris Mason5a5f79b2010-05-26 21:33:37 -04006655 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006656 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006657 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006658
Josef Bacikeb838e72012-07-31 16:28:48 -04006659 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006660 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6661 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6662 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006663}
6664
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006665#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6666
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006667static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6668 __u64 start, __u64 len)
6669{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006670 int ret;
6671
6672 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6673 if (ret)
6674 return ret;
6675
Chris Masonec29ed52011-02-23 16:23:20 -05006676 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006677}
6678
Chris Mason9ebefb182007-06-15 13:50:00 -04006679int btrfs_readpage(struct file *file, struct page *page)
6680{
Chris Masond1310b22008-01-24 16:13:08 -05006681 struct extent_io_tree *tree;
6682 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006683 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006684}
Chris Mason1832a6d2007-12-21 16:27:21 -05006685
Chris Mason39279cc2007-06-12 06:35:45 -04006686static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6687{
Chris Masond1310b22008-01-24 16:13:08 -05006688 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006689
6690
6691 if (current->flags & PF_MEMALLOC) {
6692 redirty_page_for_writepage(wbc, page);
6693 unlock_page(page);
6694 return 0;
6695 }
Chris Masond1310b22008-01-24 16:13:08 -05006696 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006697 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6698}
Chris Mason39279cc2007-06-12 06:35:45 -04006699
Chris Masonf4219502008-07-22 11:18:09 -04006700int btrfs_writepages(struct address_space *mapping,
6701 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006702{
Chris Masond1310b22008-01-24 16:13:08 -05006703 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006704
Chris Masond1310b22008-01-24 16:13:08 -05006705 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006706 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6707}
6708
Chris Mason3ab2fb52007-11-08 10:59:22 -05006709static int
6710btrfs_readpages(struct file *file, struct address_space *mapping,
6711 struct list_head *pages, unsigned nr_pages)
6712{
Chris Masond1310b22008-01-24 16:13:08 -05006713 struct extent_io_tree *tree;
6714 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006715 return extent_readpages(tree, mapping, pages, nr_pages,
6716 btrfs_get_extent);
6717}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006718static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006719{
Chris Masond1310b22008-01-24 16:13:08 -05006720 struct extent_io_tree *tree;
6721 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006722 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006723
Chris Masond1310b22008-01-24 16:13:08 -05006724 tree = &BTRFS_I(page->mapping->host)->io_tree;
6725 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006726 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006727 if (ret == 1) {
6728 ClearPagePrivate(page);
6729 set_page_private(page, 0);
6730 page_cache_release(page);
6731 }
6732 return ret;
6733}
Chris Mason39279cc2007-06-12 06:35:45 -04006734
Chris Masone6dcd2d2008-07-17 12:53:50 -04006735static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6736{
Chris Mason98509cf2008-09-11 15:51:43 -04006737 if (PageWriteback(page) || PageDirty(page))
6738 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006739 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006740}
6741
Chris Masona52d9a82007-08-27 16:49:44 -04006742static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6743{
Josef Bacik5fd02042012-05-02 14:00:54 -04006744 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006745 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006746 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006747 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006748 u64 page_start = page_offset(page);
6749 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006750
Chris Mason8b62b722009-09-02 16:53:46 -04006751 /*
6752 * we have the page locked, so new writeback can't start,
6753 * and the dirty bit won't be cleared while we are here.
6754 *
6755 * Wait for IO on this page so that we can safely clear
6756 * the PagePrivate2 bit and do ordered accounting
6757 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006758 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006759
Josef Bacik5fd02042012-05-02 14:00:54 -04006760 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006761 if (offset) {
6762 btrfs_releasepage(page, GFP_NOFS);
6763 return;
6764 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006765 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00006766 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04006767 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006768 /*
6769 * IO on this page will never be started, so we need
6770 * to account for any ordered extents now
6771 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006772 clear_extent_bit(tree, page_start, page_end,
6773 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006774 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6775 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006776 /*
6777 * whoever cleared the private bit is responsible
6778 * for the finish_ordered_io
6779 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006780 if (TestClearPagePrivate2(page) &&
6781 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6782 PAGE_CACHE_SIZE, 1)) {
6783 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006784 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006785 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006786 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006787 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006788 }
6789 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006790 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006791 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6792 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006793 __btrfs_releasepage(page, GFP_NOFS);
6794
Chris Mason4a096752008-07-21 10:29:44 -04006795 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006796 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006797 ClearPagePrivate(page);
6798 set_page_private(page, 0);
6799 page_cache_release(page);
6800 }
Chris Mason39279cc2007-06-12 06:35:45 -04006801}
6802
Chris Mason9ebefb182007-06-15 13:50:00 -04006803/*
6804 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6805 * called from a page fault handler when a page is first dirtied. Hence we must
6806 * be careful to check for EOF conditions here. We set the page up correctly
6807 * for a written page which means we get ENOSPC checking when writing into
6808 * holes and correct delalloc and unwritten extent mapping on filesystems that
6809 * support these features.
6810 *
6811 * We are not allowed to take the i_mutex here so we have to play games to
6812 * protect against truncate races as the page could now be beyond EOF. Because
6813 * vmtruncate() writes the inode size before removing pages, once we have the
6814 * page lock we can determine safely if the page is beyond EOF. If it is not
6815 * beyond EOF, then the page is guaranteed safe against truncation until we
6816 * unlock the page.
6817 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006818int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006819{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006820 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006821 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006822 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006823 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6824 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006825 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006826 char *kaddr;
6827 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006828 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006829 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006830 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006831 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006832 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006833
Jan Karab2b5ef52012-06-12 16:20:45 +02006834 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006835 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006836 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006837 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006838 reserved = 1;
6839 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006840 if (ret) {
6841 if (ret == -ENOMEM)
6842 ret = VM_FAULT_OOM;
6843 else /* -ENOSPC, -EIO, etc */
6844 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006845 if (reserved)
6846 goto out;
6847 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006848 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006849
Nick Piggin56a76f82009-03-31 15:23:23 -07006850 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006851again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006852 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006853 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006854 page_start = page_offset(page);
6855 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006856
Chris Mason9ebefb182007-06-15 13:50:00 -04006857 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006858 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006859 /* page got truncated out from underneath us */
6860 goto out_unlock;
6861 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006862 wait_on_page_writeback(page);
6863
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006864 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006865 set_page_extent_mapped(page);
6866
Chris Masoneb84ae02008-07-17 13:53:27 -04006867 /*
6868 * we can't set the delalloc bits if there are pending ordered
6869 * extents. Drop our locks and wait for them to finish
6870 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006871 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6872 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006873 unlock_extent_cached(io_tree, page_start, page_end,
6874 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006875 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006876 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006877 btrfs_put_ordered_extent(ordered);
6878 goto again;
6879 }
6880
Josef Bacikfbf19082009-10-01 17:10:23 -04006881 /*
6882 * XXX - page_mkwrite gets called every time the page is dirtied, even
6883 * if it was already dirty, so for space accounting reasons we need to
6884 * clear any delalloc bits for the range we are fixing to save. There
6885 * is probably a better way to do this, but for now keep consistent with
6886 * prepare_pages in the normal write path.
6887 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006888 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006889 EXTENT_DIRTY | EXTENT_DELALLOC |
6890 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006891 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006892
Josef Bacik2ac55d42010-02-03 19:33:23 +00006893 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6894 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006895 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006896 unlock_extent_cached(io_tree, page_start, page_end,
6897 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006898 ret = VM_FAULT_SIGBUS;
6899 goto out_unlock;
6900 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006901 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006902
6903 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006904 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006905 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006906 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006907 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006908
Chris Masone6dcd2d2008-07-17 12:53:50 -04006909 if (zero_start != PAGE_CACHE_SIZE) {
6910 kaddr = kmap(page);
6911 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6912 flush_dcache_page(page);
6913 kunmap(page);
6914 }
Chris Mason247e7432008-07-17 12:53:51 -04006915 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006916 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006917 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006918
Chris Mason257c62e2009-10-13 13:21:08 -04006919 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6920 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006921 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006922
Josef Bacik2ac55d42010-02-03 19:33:23 +00006923 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006924
6925out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006926 if (!ret) {
6927 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006928 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006929 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006930 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006931out:
Josef Bacikec39e182012-01-12 19:10:12 -05006932 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006933out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006934 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006935 return ret;
6936}
6937
Josef Bacika41ad392011-01-31 15:30:16 -05006938static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006939{
6940 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006941 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006942 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006943 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006944 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006945 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006946 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006947
Josef Bacik2aaa6652012-08-29 14:27:18 -04006948 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006949 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006950 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006951
Chris Mason4a096752008-07-21 10:29:44 -04006952 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006953 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006954
Josef Bacikfcb80c22011-05-03 10:40:22 -04006955 /*
6956 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6957 * 3 things going on here
6958 *
6959 * 1) We need to reserve space for our orphan item and the space to
6960 * delete our orphan item. Lord knows we don't want to have a dangling
6961 * orphan item because we didn't reserve space to remove it.
6962 *
6963 * 2) We need to reserve space to update our inode.
6964 *
6965 * 3) We need to have something to cache all the space that is going to
6966 * be free'd up by the truncate operation, but also have some slack
6967 * space reserved in case it uses space during the truncate (thank you
6968 * very much snapshotting).
6969 *
6970 * And we need these to all be seperate. The fact is we can use alot of
6971 * space doing the truncate, and we have no earthly idea how much space
6972 * we will use, so we need the truncate reservation to be seperate so it
6973 * doesn't end up using space reserved for updating the inode or
6974 * removing the orphan item. We also need to be able to stop the
6975 * transaction and start a new one, which means we need to be able to
6976 * update the inode several times, and we have no idea of knowing how
6977 * many times that will be, so we can't just reserve 1 item for the
6978 * entirety of the opration, so that has to be done seperately as well.
6979 * Then there is the orphan item, which does indeed need to be held on
6980 * to for the whole operation, and we need nobody to touch this reserved
6981 * space except the orphan code.
6982 *
6983 * So that leaves us with
6984 *
6985 * 1) root->orphan_block_rsv - for the orphan deletion.
6986 * 2) rsv - for the truncate reservation, which we will steal from the
6987 * transaction reservation.
6988 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6989 * updating the inode.
6990 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006991 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006992 if (!rsv)
6993 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006994 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006995 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006996
Josef Bacik907cbce2011-08-08 13:46:15 -04006997 /*
Josef Bacik07127182011-08-19 10:29:59 -04006998 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006999 * 1 for updating the inode.
7000 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007001 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007002 if (IS_ERR(trans)) {
7003 err = PTR_ERR(trans);
7004 goto out;
7005 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007006
Josef Bacik907cbce2011-08-08 13:46:15 -04007007 /* Migrate the slack space for the truncate to our reserve */
7008 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7009 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007010 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007011
Chris Mason5a3f23d2009-03-31 13:27:11 -04007012 /*
7013 * setattr is responsible for setting the ordered_data_close flag,
7014 * but that is only tested during the last file release. That
7015 * could happen well after the next commit, leaving a great big
7016 * window where new writes may get lost if someone chooses to write
7017 * to this file after truncating to zero
7018 *
7019 * The inode doesn't have any dirty data here, and so if we commit
7020 * this is a noop. If someone immediately starts writing to the inode
7021 * it is very likely we'll catch some of their writes in this
7022 * transaction, and the commit will find this file on the ordered
7023 * data list with good things to send down.
7024 *
7025 * This is a best effort solution, there is still a window where
7026 * using truncate to replace the contents of the file will
7027 * end up with a zero length file after a crash.
7028 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007029 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7030 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007031 btrfs_add_ordered_operation(trans, root, inode);
7032
Josef Bacik5dc562c2012-08-17 13:14:17 -04007033 /*
7034 * So if we truncate and then write and fsync we normally would just
7035 * write the extents that changed, which is a problem if we need to
7036 * first truncate that entire inode. So set this flag so we write out
7037 * all of the extents in the inode to the sync log so we're completely
7038 * safe.
7039 */
7040 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007041 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007042
Yan, Zheng80825102009-11-12 09:35:36 +00007043 while (1) {
7044 ret = btrfs_truncate_inode_items(trans, root, inode,
7045 inode->i_size,
7046 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007047 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007048 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007049 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007050 }
Chris Mason39279cc2007-06-12 06:35:45 -04007051
Josef Bacikfcb80c22011-05-03 10:40:22 -04007052 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007053 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007054 if (ret) {
7055 err = ret;
7056 break;
7057 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007058
Yan, Zheng80825102009-11-12 09:35:36 +00007059 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007060 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007061
7062 trans = btrfs_start_transaction(root, 2);
7063 if (IS_ERR(trans)) {
7064 ret = err = PTR_ERR(trans);
7065 trans = NULL;
7066 break;
7067 }
7068
7069 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7070 rsv, min_size);
7071 BUG_ON(ret); /* shouldn't happen */
7072 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007073 }
7074
7075 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007076 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007077 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007078 if (ret)
7079 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007080 }
7081
Chris Mason917c16b2011-11-08 14:49:59 -05007082 if (trans) {
7083 trans->block_rsv = &root->fs_info->trans_block_rsv;
7084 ret = btrfs_update_inode(trans, root, inode);
7085 if (ret && !err)
7086 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007087
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007088 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007089 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007090 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007091
7092out:
7093 btrfs_free_block_rsv(root, rsv);
7094
Josef Bacik3893e332011-01-31 16:03:11 -05007095 if (ret && !err)
7096 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007097
Josef Bacik3893e332011-01-31 16:03:11 -05007098 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007099}
7100
Sven Wegener3b963622008-06-09 21:57:42 -04007101/*
Chris Masond352ac62008-09-29 15:18:18 -04007102 * create a new subvolume directory/inode (helper for the ioctl).
7103 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007104int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007105 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007106{
Chris Mason39279cc2007-06-12 06:35:45 -04007107 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007108 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007109 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007110
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007111 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7112 new_dirid, new_dirid,
7113 S_IFDIR | (~current_umask() & S_IRWXUGO),
7114 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007115 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007116 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007117 inode->i_op = &btrfs_dir_inode_operations;
7118 inode->i_fop = &btrfs_dir_file_operations;
7119
Miklos Szeredibfe86842011-10-28 14:13:29 +02007120 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007121 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007122
Yan, Zheng76dda932009-09-21 16:00:26 -04007123 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007124
Yan, Zheng76dda932009-09-21 16:00:26 -04007125 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007126 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007127}
7128
Chris Mason39279cc2007-06-12 06:35:45 -04007129struct inode *btrfs_alloc_inode(struct super_block *sb)
7130{
7131 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007132 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007133
7134 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7135 if (!ei)
7136 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007137
7138 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007139 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007140 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007141 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007142 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007143 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007144 ei->disk_i_size = 0;
7145 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007146 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007147 ei->index_cnt = (u64)-1;
7148 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007149 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007150
Josef Bacik9e0baf62011-07-15 15:16:44 +00007151 spin_lock_init(&ei->lock);
7152 ei->outstanding_extents = 0;
7153 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007154
Josef Bacik72ac3c02012-05-23 14:13:11 -04007155 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007156 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007157
Miao Xie16cdcec2011-04-22 18:12:22 +08007158 ei->delayed_node = NULL;
7159
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007160 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007161 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007162 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7163 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007164 ei->io_tree.track_uptodate = 1;
7165 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007166 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007167 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007168 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007169 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007170 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007171 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007172 RB_CLEAR_NODE(&ei->rb_node);
7173
7174 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007175}
7176
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007177static void btrfs_i_callback(struct rcu_head *head)
7178{
7179 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007180 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7181}
7182
Chris Mason39279cc2007-06-12 06:35:45 -04007183void btrfs_destroy_inode(struct inode *inode)
7184{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007185 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007186 struct btrfs_root *root = BTRFS_I(inode)->root;
7187
Al Virob3d9b7a2012-06-09 13:51:19 -04007188 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007189 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007190 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7191 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007192 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7193 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007194
Chris Mason5a3f23d2009-03-31 13:27:11 -04007195 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007196 * This can happen where we create an inode, but somebody else also
7197 * created the same inode and we need to destroy the one we already
7198 * created.
7199 */
7200 if (!root)
7201 goto free;
7202
7203 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007204 * Make sure we're properly removed from the ordered operation
7205 * lists.
7206 */
7207 smp_mb();
7208 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7209 spin_lock(&root->fs_info->ordered_extent_lock);
7210 list_del_init(&BTRFS_I(inode)->ordered_operations);
7211 spin_unlock(&root->fs_info->ordered_extent_lock);
7212 }
7213
Josef Bacik8a35d952012-05-23 14:26:42 -04007214 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7215 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007216 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7217 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007218 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007219 }
Josef Bacik7b128762008-07-24 12:17:14 -04007220
Chris Masond3977122009-01-05 21:25:51 -05007221 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007222 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7223 if (!ordered)
7224 break;
7225 else {
Chris Masond3977122009-01-05 21:25:51 -05007226 printk(KERN_ERR "btrfs found ordered "
7227 "extent %llu %llu on inode cleanup\n",
7228 (unsigned long long)ordered->file_offset,
7229 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007230 btrfs_remove_ordered_extent(inode, ordered);
7231 btrfs_put_ordered_extent(ordered);
7232 btrfs_put_ordered_extent(ordered);
7233 }
7234 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007235 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007236 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007237free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007238 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007239 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007240}
7241
Al Viro45321ac2010-06-07 13:43:19 -04007242int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007243{
7244 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007245
Josef Bacik0af3d002010-06-21 14:48:16 -04007246 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007247 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007248 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007249 else
Al Viro45321ac2010-06-07 13:43:19 -04007250 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007251}
7252
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007253static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007254{
7255 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7256
7257 inode_init_once(&ei->vfs_inode);
7258}
7259
7260void btrfs_destroy_cachep(void)
7261{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007262 /*
7263 * Make sure all delayed rcu free inodes are flushed before we
7264 * destroy cache.
7265 */
7266 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007267 if (btrfs_inode_cachep)
7268 kmem_cache_destroy(btrfs_inode_cachep);
7269 if (btrfs_trans_handle_cachep)
7270 kmem_cache_destroy(btrfs_trans_handle_cachep);
7271 if (btrfs_transaction_cachep)
7272 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007273 if (btrfs_path_cachep)
7274 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007275 if (btrfs_free_space_cachep)
7276 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007277 if (btrfs_delalloc_work_cachep)
7278 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007279}
7280
7281int btrfs_init_cachep(void)
7282{
David Sterba837e1972012-09-07 03:00:48 -06007283 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007284 sizeof(struct btrfs_inode), 0,
7285 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007286 if (!btrfs_inode_cachep)
7287 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007288
David Sterba837e1972012-09-07 03:00:48 -06007289 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007290 sizeof(struct btrfs_trans_handle), 0,
7291 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007292 if (!btrfs_trans_handle_cachep)
7293 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007294
David Sterba837e1972012-09-07 03:00:48 -06007295 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007296 sizeof(struct btrfs_transaction), 0,
7297 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007298 if (!btrfs_transaction_cachep)
7299 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007300
David Sterba837e1972012-09-07 03:00:48 -06007301 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007302 sizeof(struct btrfs_path), 0,
7303 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007304 if (!btrfs_path_cachep)
7305 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007306
David Sterba837e1972012-09-07 03:00:48 -06007307 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007308 sizeof(struct btrfs_free_space), 0,
7309 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7310 if (!btrfs_free_space_cachep)
7311 goto fail;
7312
Miao Xie8ccf6f192012-10-25 09:28:04 +00007313 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7314 sizeof(struct btrfs_delalloc_work), 0,
7315 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7316 NULL);
7317 if (!btrfs_delalloc_work_cachep)
7318 goto fail;
7319
Chris Mason39279cc2007-06-12 06:35:45 -04007320 return 0;
7321fail:
7322 btrfs_destroy_cachep();
7323 return -ENOMEM;
7324}
7325
7326static int btrfs_getattr(struct vfsmount *mnt,
7327 struct dentry *dentry, struct kstat *stat)
7328{
7329 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007330 u32 blocksize = inode->i_sb->s_blocksize;
7331
Chris Mason39279cc2007-06-12 06:35:45 -04007332 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007333 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007334 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007335 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7336 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007337 return 0;
7338}
7339
Liu Bo75e7cb72011-03-22 10:12:20 +00007340/*
7341 * If a file is moved, it will inherit the cow and compression flags of the new
7342 * directory.
7343 */
7344static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7345{
7346 struct btrfs_inode *b_dir = BTRFS_I(dir);
7347 struct btrfs_inode *b_inode = BTRFS_I(inode);
7348
7349 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7350 b_inode->flags |= BTRFS_INODE_NODATACOW;
7351 else
7352 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7353
Liu Bobc178232012-06-14 02:23:18 -06007354 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007355 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007356 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7357 } else {
7358 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7359 BTRFS_INODE_NOCOMPRESS);
7360 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007361}
7362
Chris Masond3977122009-01-05 21:25:51 -05007363static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7364 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007365{
7366 struct btrfs_trans_handle *trans;
7367 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007368 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007369 struct inode *new_inode = new_dentry->d_inode;
7370 struct inode *old_inode = old_dentry->d_inode;
7371 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007372 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007373 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007374 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007375 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007376
Li Zefan33345d012011-04-20 10:31:50 +08007377 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007378 return -EPERM;
7379
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007380 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007381 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007382 return -EXDEV;
7383
Li Zefan33345d012011-04-20 10:31:50 +08007384 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7385 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007386 return -ENOTEMPTY;
7387
Chris Mason39279cc2007-06-12 06:35:45 -04007388 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007389 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007390 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007391
7392
7393 /* check for collisions, even if the name isn't there */
7394 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7395 new_dentry->d_name.name,
7396 new_dentry->d_name.len);
7397
7398 if (ret) {
7399 if (ret == -EEXIST) {
7400 /* we shouldn't get
7401 * eexist without a new_inode */
7402 if (!new_inode) {
7403 WARN_ON(1);
7404 return ret;
7405 }
7406 } else {
7407 /* maybe -EOVERFLOW */
7408 return ret;
7409 }
7410 }
7411 ret = 0;
7412
Chris Mason5a3f23d2009-03-31 13:27:11 -04007413 /*
7414 * we're using rename to replace one file with another.
7415 * and the replacement file is large. Start IO on it now so
7416 * we don't add too much work to the end of the transaction
7417 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007418 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007419 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7420 filemap_flush(old_inode->i_mapping);
7421
Yan, Zheng76dda932009-09-21 16:00:26 -04007422 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007423 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007424 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007425 /*
7426 * We want to reserve the absolute worst case amount of items. So if
7427 * both inodes are subvols and we need to unlink them then that would
7428 * require 4 item modifications, but if they are both normal inodes it
7429 * would require 5 item modifications, so we'll assume their normal
7430 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7431 * should cover the worst case number of items we'll modify.
7432 */
7433 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007434 if (IS_ERR(trans)) {
7435 ret = PTR_ERR(trans);
7436 goto out_notrans;
7437 }
Chris Mason5f39d392007-10-15 16:14:19 -04007438
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007439 if (dest != root)
7440 btrfs_record_root_in_trans(trans, dest);
7441
Yan, Zhenga5719522009-09-24 09:17:31 -04007442 ret = btrfs_set_inode_index(new_dir, &index);
7443 if (ret)
7444 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007445
Li Zefan33345d012011-04-20 10:31:50 +08007446 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007447 /* force full log commit if subvolume involved. */
7448 root->fs_info->last_trans_log_full_commit = trans->transid;
7449 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007450 ret = btrfs_insert_inode_ref(trans, dest,
7451 new_dentry->d_name.name,
7452 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007453 old_ino,
7454 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007455 if (ret)
7456 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007457 /*
7458 * this is an ugly little race, but the rename is required
7459 * to make sure that if we crash, the inode is either at the
7460 * old name or the new one. pinning the log transaction lets
7461 * us make sure we don't allow a log commit to come in after
7462 * we unlink the name but before we add the new name back in.
7463 */
7464 btrfs_pin_log_trans(root);
7465 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007466 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007467 * make sure the inode gets flushed if it is replacing
7468 * something.
7469 */
Li Zefan33345d012011-04-20 10:31:50 +08007470 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007471 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007472
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007473 inode_inc_iversion(old_dir);
7474 inode_inc_iversion(new_dir);
7475 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007476 old_dir->i_ctime = old_dir->i_mtime = ctime;
7477 new_dir->i_ctime = new_dir->i_mtime = ctime;
7478 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007479
Chris Mason12fcfd22009-03-24 10:24:20 -04007480 if (old_dentry->d_parent != new_dentry->d_parent)
7481 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7482
Li Zefan33345d012011-04-20 10:31:50 +08007483 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007484 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7485 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7486 old_dentry->d_name.name,
7487 old_dentry->d_name.len);
7488 } else {
Al Viro92986792011-03-04 17:14:37 +00007489 ret = __btrfs_unlink_inode(trans, root, old_dir,
7490 old_dentry->d_inode,
7491 old_dentry->d_name.name,
7492 old_dentry->d_name.len);
7493 if (!ret)
7494 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007495 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007496 if (ret) {
7497 btrfs_abort_transaction(trans, root, ret);
7498 goto out_fail;
7499 }
Chris Mason39279cc2007-06-12 06:35:45 -04007500
7501 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007502 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007503 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007504 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007505 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7506 root_objectid = BTRFS_I(new_inode)->location.objectid;
7507 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7508 root_objectid,
7509 new_dentry->d_name.name,
7510 new_dentry->d_name.len);
7511 BUG_ON(new_inode->i_nlink == 0);
7512 } else {
7513 ret = btrfs_unlink_inode(trans, dest, new_dir,
7514 new_dentry->d_inode,
7515 new_dentry->d_name.name,
7516 new_dentry->d_name.len);
7517 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007518 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007519 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007520 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007521 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007522 if (ret) {
7523 btrfs_abort_transaction(trans, root, ret);
7524 goto out_fail;
7525 }
Chris Mason39279cc2007-06-12 06:35:45 -04007526 }
Josef Bacikaec74772008-07-24 12:12:38 -04007527
Liu Bo75e7cb72011-03-22 10:12:20 +00007528 fixup_inode_flags(new_dir, old_inode);
7529
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007530 ret = btrfs_add_link(trans, new_dir, old_inode,
7531 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007532 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007533 if (ret) {
7534 btrfs_abort_transaction(trans, root, ret);
7535 goto out_fail;
7536 }
Chris Mason39279cc2007-06-12 06:35:45 -04007537
Li Zefan33345d012011-04-20 10:31:50 +08007538 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007539 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007540 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007541 btrfs_end_log_trans(root);
7542 }
Chris Mason39279cc2007-06-12 06:35:45 -04007543out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007544 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007545out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007546 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007547 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007548
Chris Mason39279cc2007-06-12 06:35:45 -04007549 return ret;
7550}
7551
Miao Xie8ccf6f192012-10-25 09:28:04 +00007552static void btrfs_run_delalloc_work(struct btrfs_work *work)
7553{
7554 struct btrfs_delalloc_work *delalloc_work;
7555
7556 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7557 work);
7558 if (delalloc_work->wait)
7559 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7560 else
7561 filemap_flush(delalloc_work->inode->i_mapping);
7562
7563 if (delalloc_work->delay_iput)
7564 btrfs_add_delayed_iput(delalloc_work->inode);
7565 else
7566 iput(delalloc_work->inode);
7567 complete(&delalloc_work->completion);
7568}
7569
7570struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7571 int wait, int delay_iput)
7572{
7573 struct btrfs_delalloc_work *work;
7574
7575 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7576 if (!work)
7577 return NULL;
7578
7579 init_completion(&work->completion);
7580 INIT_LIST_HEAD(&work->list);
7581 work->inode = inode;
7582 work->wait = wait;
7583 work->delay_iput = delay_iput;
7584 work->work.func = btrfs_run_delalloc_work;
7585
7586 return work;
7587}
7588
7589void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7590{
7591 wait_for_completion(&work->completion);
7592 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7593}
7594
Chris Masond352ac62008-09-29 15:18:18 -04007595/*
7596 * some fairly slow code that needs optimization. This walks the list
7597 * of all the inodes with pending delalloc and forces them to disk.
7598 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007599int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007600{
Chris Masonea8c2812008-08-04 23:17:27 -04007601 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007602 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007603 struct btrfs_delalloc_work *work, *next;
7604 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00007605 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007606 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007607
Yan Zhengc146afa2008-11-12 14:34:12 -05007608 if (root->fs_info->sb->s_flags & MS_RDONLY)
7609 return -EROFS;
7610
Miao Xie8ccf6f192012-10-25 09:28:04 +00007611 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007612 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00007613
Chris Mason75eff682008-12-15 15:54:40 -05007614 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007615 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7616 while (!list_empty(&splice)) {
7617 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04007618 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007619
7620 list_del_init(&binode->delalloc_inodes);
7621
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007622 inode = igrab(&binode->vfs_inode);
7623 if (!inode)
Miao Xie1eafa6c2013-01-22 10:49:00 +00007624 continue;
7625
7626 list_add_tail(&binode->delalloc_inodes,
7627 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007628 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007629
7630 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7631 if (unlikely(!work)) {
7632 ret = -ENOMEM;
7633 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007634 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007635 list_add_tail(&work->list, &works);
7636 btrfs_queue_worker(&root->fs_info->flush_workers,
7637 &work->work);
7638
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007639 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007640 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007641 }
Chris Mason75eff682008-12-15 15:54:40 -05007642 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007643
Miao Xie1eafa6c2013-01-22 10:49:00 +00007644 list_for_each_entry_safe(work, next, &works, list) {
7645 list_del_init(&work->list);
7646 btrfs_wait_and_free_delalloc_work(work);
7647 }
7648
Chris Mason8c8bee12008-09-29 11:19:10 -04007649 /* the filemap_flush will queue IO into the worker threads, but
7650 * we have to make sure the IO is actually started and that
7651 * ordered extents get created before we return
7652 */
7653 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007654 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007655 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007656 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007657 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7658 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007659 }
7660 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007661 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007662out:
7663 list_for_each_entry_safe(work, next, &works, list) {
7664 list_del_init(&work->list);
7665 btrfs_wait_and_free_delalloc_work(work);
7666 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007667
7668 if (!list_empty_careful(&splice)) {
7669 spin_lock(&root->fs_info->delalloc_lock);
7670 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7671 spin_unlock(&root->fs_info->delalloc_lock);
7672 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00007673 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007674}
7675
Chris Mason39279cc2007-06-12 06:35:45 -04007676static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7677 const char *symname)
7678{
7679 struct btrfs_trans_handle *trans;
7680 struct btrfs_root *root = BTRFS_I(dir)->root;
7681 struct btrfs_path *path;
7682 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007683 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007684 int err;
7685 int drop_inode = 0;
7686 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007687 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007688 int name_len;
7689 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007690 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007691 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007692 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007693
7694 name_len = strlen(symname) + 1;
7695 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7696 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007697
Josef Bacik9ed74f22009-09-11 16:12:44 -04007698 /*
7699 * 2 items for inode item and ref
7700 * 2 items for dir items
7701 * 1 item for xattr if selinux is on
7702 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007703 trans = btrfs_start_transaction(root, 5);
7704 if (IS_ERR(trans))
7705 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007706
Li Zefan581bb052011-04-20 10:06:11 +08007707 err = btrfs_find_free_ino(root, &objectid);
7708 if (err)
7709 goto out_unlock;
7710
Josef Bacikaec74772008-07-24 12:12:38 -04007711 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007712 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007713 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007714 if (IS_ERR(inode)) {
7715 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007716 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007717 }
Chris Mason39279cc2007-06-12 06:35:45 -04007718
Eric Paris2a7dba32011-02-01 11:05:39 -05007719 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007720 if (err) {
7721 drop_inode = 1;
7722 goto out_unlock;
7723 }
7724
Casey Schauflerad19db72011-12-15 10:09:07 -05007725 /*
7726 * If the active LSM wants to access the inode during
7727 * d_instantiate it needs these. Smack checks to see
7728 * if the filesystem supports xattrs by looking at the
7729 * ops vector.
7730 */
7731 inode->i_fop = &btrfs_file_operations;
7732 inode->i_op = &btrfs_file_inode_operations;
7733
Josef Bacika1b075d2010-11-19 20:36:11 +00007734 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007735 if (err)
7736 drop_inode = 1;
7737 else {
7738 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007739 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007740 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007741 }
Chris Mason39279cc2007-06-12 06:35:45 -04007742 if (drop_inode)
7743 goto out_unlock;
7744
7745 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007746 if (!path) {
7747 err = -ENOMEM;
7748 drop_inode = 1;
7749 goto out_unlock;
7750 }
Li Zefan33345d012011-04-20 10:31:50 +08007751 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007752 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007753 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7754 datasize = btrfs_file_extent_calc_inline_size(name_len);
7755 err = btrfs_insert_empty_item(trans, root, path, &key,
7756 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007757 if (err) {
7758 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007759 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007760 goto out_unlock;
7761 }
Chris Mason5f39d392007-10-15 16:14:19 -04007762 leaf = path->nodes[0];
7763 ei = btrfs_item_ptr(leaf, path->slots[0],
7764 struct btrfs_file_extent_item);
7765 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7766 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007767 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007768 btrfs_set_file_extent_encryption(leaf, ei, 0);
7769 btrfs_set_file_extent_compression(leaf, ei, 0);
7770 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7771 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7772
Chris Mason39279cc2007-06-12 06:35:45 -04007773 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007774 write_extent_buffer(leaf, symname, ptr, name_len);
7775 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007776 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007777
Chris Mason39279cc2007-06-12 06:35:45 -04007778 inode->i_op = &btrfs_symlink_inode_operations;
7779 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007780 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007781 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007782 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007783 err = btrfs_update_inode(trans, root, inode);
7784 if (err)
7785 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007786
7787out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007788 if (!err)
7789 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007790 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007791 if (drop_inode) {
7792 inode_dec_link_count(inode);
7793 iput(inode);
7794 }
Liu Bob53d3f52012-11-14 14:34:34 +00007795 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007796 return err;
7797}
Chris Mason16432982008-04-10 10:23:21 -04007798
Josef Bacik0af3d002010-06-21 14:48:16 -04007799static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7800 u64 start, u64 num_bytes, u64 min_size,
7801 loff_t actual_len, u64 *alloc_hint,
7802 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007803{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007804 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7805 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007806 struct btrfs_root *root = BTRFS_I(inode)->root;
7807 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007808 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007809 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007810 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007811 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007812
Josef Bacik0af3d002010-06-21 14:48:16 -04007813 if (trans)
7814 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007815 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007816 if (own_trans) {
7817 trans = btrfs_start_transaction(root, 3);
7818 if (IS_ERR(trans)) {
7819 ret = PTR_ERR(trans);
7820 break;
7821 }
Yan Zhengd899e052008-10-30 14:25:28 -04007822 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007823
Yan, Zhengefa56462010-05-16 10:49:59 -04007824 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007825 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007826 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007827 if (own_trans)
7828 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007829 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007830 }
7831
Yan Zhengd899e052008-10-30 14:25:28 -04007832 ret = insert_reserved_file_extent(trans, inode,
7833 cur_offset, ins.objectid,
7834 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007835 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007836 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007837 if (ret) {
7838 btrfs_abort_transaction(trans, root, ret);
7839 if (own_trans)
7840 btrfs_end_transaction(trans, root);
7841 break;
7842 }
Chris Masona1ed8352009-09-11 12:27:37 -04007843 btrfs_drop_extent_cache(inode, cur_offset,
7844 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007845
Josef Bacik5dc562c2012-08-17 13:14:17 -04007846 em = alloc_extent_map();
7847 if (!em) {
7848 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7849 &BTRFS_I(inode)->runtime_flags);
7850 goto next;
7851 }
7852
7853 em->start = cur_offset;
7854 em->orig_start = cur_offset;
7855 em->len = ins.offset;
7856 em->block_start = ins.objectid;
7857 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007858 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007859 em->bdev = root->fs_info->fs_devices->latest_bdev;
7860 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7861 em->generation = trans->transid;
7862
7863 while (1) {
7864 write_lock(&em_tree->lock);
7865 ret = add_extent_mapping(em_tree, em);
7866 if (!ret)
7867 list_move(&em->list,
7868 &em_tree->modified_extents);
7869 write_unlock(&em_tree->lock);
7870 if (ret != -EEXIST)
7871 break;
7872 btrfs_drop_extent_cache(inode, cur_offset,
7873 cur_offset + ins.offset - 1,
7874 0);
7875 }
7876 free_extent_map(em);
7877next:
Yan Zhengd899e052008-10-30 14:25:28 -04007878 num_bytes -= ins.offset;
7879 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007880 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007881
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007882 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007883 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007884 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007885 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007886 (actual_len > inode->i_size) &&
7887 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007888 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007889 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007890 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007891 i_size = cur_offset;
7892 i_size_write(inode, i_size);
7893 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007894 }
7895
Yan Zhengd899e052008-10-30 14:25:28 -04007896 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007897
7898 if (ret) {
7899 btrfs_abort_transaction(trans, root, ret);
7900 if (own_trans)
7901 btrfs_end_transaction(trans, root);
7902 break;
7903 }
Yan Zhengd899e052008-10-30 14:25:28 -04007904
Josef Bacik0af3d002010-06-21 14:48:16 -04007905 if (own_trans)
7906 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007907 }
Yan Zhengd899e052008-10-30 14:25:28 -04007908 return ret;
7909}
7910
Josef Bacik0af3d002010-06-21 14:48:16 -04007911int btrfs_prealloc_file_range(struct inode *inode, int mode,
7912 u64 start, u64 num_bytes, u64 min_size,
7913 loff_t actual_len, u64 *alloc_hint)
7914{
7915 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7916 min_size, actual_len, alloc_hint,
7917 NULL);
7918}
7919
7920int btrfs_prealloc_file_range_trans(struct inode *inode,
7921 struct btrfs_trans_handle *trans, int mode,
7922 u64 start, u64 num_bytes, u64 min_size,
7923 loff_t actual_len, u64 *alloc_hint)
7924{
7925 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7926 min_size, actual_len, alloc_hint, trans);
7927}
7928
Chris Masone6dcd2d2008-07-17 12:53:50 -04007929static int btrfs_set_page_dirty(struct page *page)
7930{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007931 return __set_page_dirty_nobuffers(page);
7932}
7933
Al Viro10556cb2011-06-20 19:28:19 -04007934static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007935{
Li Zefanb83cc962010-12-20 16:04:08 +08007936 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007937 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007938
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007939 if (mask & MAY_WRITE &&
7940 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7941 if (btrfs_root_readonly(root))
7942 return -EROFS;
7943 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7944 return -EACCES;
7945 }
Al Viro2830ba72011-06-20 19:16:29 -04007946 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007947}
Chris Mason39279cc2007-06-12 06:35:45 -04007948
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007949static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007950 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007951 .lookup = btrfs_lookup,
7952 .create = btrfs_create,
7953 .unlink = btrfs_unlink,
7954 .link = btrfs_link,
7955 .mkdir = btrfs_mkdir,
7956 .rmdir = btrfs_rmdir,
7957 .rename = btrfs_rename,
7958 .symlink = btrfs_symlink,
7959 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007960 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007961 .setxattr = btrfs_setxattr,
7962 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007963 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007964 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007965 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007966 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007967};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007968static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007969 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007970 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007971 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007972};
Yan, Zheng76dda932009-09-21 16:00:26 -04007973
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007974static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007975 .llseek = generic_file_llseek,
7976 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007977 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007978 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007979#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007980 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007981#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007982 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007983 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007984};
7985
Chris Masond1310b22008-01-24 16:13:08 -05007986static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007987 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007988 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007989 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007990 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007991 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007992 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007993 .set_bit_hook = btrfs_set_bit_hook,
7994 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007995 .merge_extent_hook = btrfs_merge_extent_hook,
7996 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007997};
7998
Chris Mason35054392009-01-21 13:11:13 -05007999/*
8000 * btrfs doesn't support the bmap operation because swapfiles
8001 * use bmap to make a mapping of extents in the file. They assume
8002 * these extents won't change over the life of the file and they
8003 * use the bmap result to do IO directly to the drive.
8004 *
8005 * the btrfs bmap call would return logical addresses that aren't
8006 * suitable for IO and they also will change frequently as COW
8007 * operations happen. So, swapfile + btrfs == corruption.
8008 *
8009 * For now we're avoiding this by dropping bmap.
8010 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008011static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008012 .readpage = btrfs_readpage,
8013 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008014 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008015 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008016 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008017 .invalidatepage = btrfs_invalidatepage,
8018 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008019 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008020 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008021};
8022
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008023static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008024 .readpage = btrfs_readpage,
8025 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008026 .invalidatepage = btrfs_invalidatepage,
8027 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008028};
8029
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008030static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008031 .getattr = btrfs_getattr,
8032 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008033 .setxattr = btrfs_setxattr,
8034 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008035 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008036 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008037 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008038 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008039 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008040 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008041};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008042static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008043 .getattr = btrfs_getattr,
8044 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008045 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008046 .setxattr = btrfs_setxattr,
8047 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008048 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008049 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008050 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008051 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008052};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008053static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008054 .readlink = generic_readlink,
8055 .follow_link = page_follow_link_light,
8056 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008057 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008058 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008059 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008060 .setxattr = btrfs_setxattr,
8061 .getxattr = btrfs_getxattr,
8062 .listxattr = btrfs_listxattr,
8063 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008064 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008065 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008066};
Yan, Zheng76dda932009-09-21 16:00:26 -04008067
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008068const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008069 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008070 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008071};