blob: 348b7bb920ef655cb89d61826c0f64a255f46538 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044#include "ctree.h"
45#include "disk-io.h"
46#include "transaction.h"
47#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000074static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_trans_handle_cachep;
76struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80#define S_SHIFT 12
81static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89};
90
Eric Sandeen3972f262013-01-12 02:57:22 +000091static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050092static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040093static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050094static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
96 u64 start, u64 end, int *page_started,
97 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -040098static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
99 u64 len, u64 orig_start,
100 u64 block_start, u64 block_len,
101 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400102
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500104 struct inode *inode, struct inode *dir,
105 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500106{
107 int err;
108
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000109 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500110 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500112 return err;
113}
114
Chris Masond352ac62008-09-29 15:18:18 -0400115/*
Chris Masonc8b97812008-10-29 14:49:59 -0400116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
119 */
Chris Masond3977122009-01-05 21:25:51 -0500120static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct btrfs_root *root, struct inode *inode,
122 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000123 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400124 struct page **compressed_pages)
125{
126 struct btrfs_key key;
127 struct btrfs_path *path;
128 struct extent_buffer *leaf;
129 struct page *page = NULL;
130 char *kaddr;
131 unsigned long ptr;
132 struct btrfs_file_extent_item *ei;
133 int err = 0;
134 int ret;
135 size_t cur_size = size;
136 size_t datasize;
137 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Li Zefanfe3f5662011-03-28 08:30:38 +0000139 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400140 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400141
Chris Masond3977122009-01-05 21:25:51 -0500142 path = btrfs_alloc_path();
143 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400144 return -ENOMEM;
145
Chris Masonb9473432009-03-13 11:00:37 -0400146 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Li Zefan33345d012011-04-20 10:31:50 +0800148 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400149 key.offset = start;
150 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 datasize = btrfs_file_extent_calc_inline_size(cur_size);
152
153 inode_add_bytes(inode, size);
154 ret = btrfs_insert_empty_item(trans, root, path, &key,
155 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 if (ret) {
157 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 goto fail;
159 }
160 leaf = path->nodes[0];
161 ei = btrfs_item_ptr(leaf, path->slots[0],
162 struct btrfs_file_extent_item);
163 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
164 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
165 btrfs_set_file_extent_encryption(leaf, ei, 0);
166 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
167 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
168 ptr = btrfs_file_extent_inline_start(ei);
169
Li Zefan261507a02010-12-17 14:21:50 +0800170 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page *cpage;
172 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500173 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400174 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500175 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400176 PAGE_CACHE_SIZE);
177
Cong Wang7ac687d2011-11-25 23:14:28 +0800178 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400179 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800180 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400181
182 i++;
183 ptr += cur_size;
184 compressed_size -= cur_size;
185 }
186 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800187 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400188 } else {
189 page = find_get_page(inode->i_mapping,
190 start >> PAGE_CACHE_SHIFT);
191 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 offset = start & (PAGE_CACHE_SIZE - 1);
194 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 page_cache_release(page);
197 }
198 btrfs_mark_buffer_dirty(leaf);
199 btrfs_free_path(path);
200
Yan, Zhengc2167752009-11-12 09:34:21 +0000201 /*
202 * we're an inline extent, so nobody can
203 * extend the file past i_size without locking
204 * a page we already have locked.
205 *
206 * We must do any isize and inode updates
207 * before we unlock the pages. Otherwise we
208 * could end up racing with unlink.
209 */
Chris Masonc8b97812008-10-29 14:49:59 -0400210 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100211 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000212
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100213 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400214fail:
215 btrfs_free_path(path);
216 return err;
217}
218
219
220/*
221 * conditionally insert an inline extent into the file. This
222 * does the checks required to make sure the data is small enough
223 * to fit as an inline extent.
224 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400225static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct btrfs_root *root,
227 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000228 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400229 struct page **compressed_pages)
230{
231 u64 isize = i_size_read(inode);
232 u64 actual_end = min(end + 1, isize);
233 u64 inline_len = actual_end - start;
234 u64 aligned_end = (end + root->sectorsize - 1) &
235 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 u64 data_len = inline_len;
237 int ret;
238
239 if (compressed_size)
240 data_len = compressed_size;
241
242 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400243 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400244 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
245 (!compressed_size &&
246 (actual_end & (root->sectorsize - 1)) == 0) ||
247 end + 1 < isize ||
248 data_len > root->fs_info->max_inline) {
249 return 1;
250 }
251
Josef Bacik26714852012-08-29 12:24:27 -0400252 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100253 if (ret)
254 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400255
256 if (isize > actual_end)
257 inline_len = min_t(u64, isize, actual_end);
258 ret = insert_inline_extent(trans, root, inode, start,
259 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000260 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400261 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100262 btrfs_abort_transaction(trans, root, ret);
263 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400264 } else if (ret == -ENOSPC) {
265 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400267
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400268 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400269 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400270 return 0;
271}
272
Chris Mason771ed682008-11-06 22:02:51 -0500273struct async_extent {
274 u64 start;
275 u64 ram_size;
276 u64 compressed_size;
277 struct page **pages;
278 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800279 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500280 struct list_head list;
281};
282
283struct async_cow {
284 struct inode *inode;
285 struct btrfs_root *root;
286 struct page *locked_page;
287 u64 start;
288 u64 end;
289 struct list_head extents;
290 struct btrfs_work work;
291};
292
293static noinline int add_async_extent(struct async_cow *cow,
294 u64 start, u64 ram_size,
295 u64 compressed_size,
296 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800297 unsigned long nr_pages,
298 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500299{
300 struct async_extent *async_extent;
301
302 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100303 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500304 async_extent->start = start;
305 async_extent->ram_size = ram_size;
306 async_extent->compressed_size = compressed_size;
307 async_extent->pages = pages;
308 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800309 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500310 list_add_tail(&async_extent->list, &cow->extents);
311 return 0;
312}
313
Chris Masonc8b97812008-10-29 14:49:59 -0400314/*
Chris Mason771ed682008-11-06 22:02:51 -0500315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400318 *
Chris Mason771ed682008-11-06 22:02:51 -0500319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400324 *
Chris Mason771ed682008-11-06 22:02:51 -0500325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300328 * are written in the same order that the flusher thread sent them
329 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400330 */
Chris Mason771ed682008-11-06 22:02:51 -0500331static noinline int compress_file_range(struct inode *inode,
332 struct page *locked_page,
333 u64 start, u64 end,
334 struct async_cow *async_cow,
335 int *num_added)
Chris 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 Masonf03d9301f2009-02-04 09:31:06 -0500366 /*
367 * we don't want to send crud past the end of i_size through
368 * compression, that's just a waste of CPU time. So, if the
369 * end of the file is before the start of our current
370 * requested range of bytes, we bail out to the uncompressed
371 * cleanup code that can deal with all of this.
372 *
373 * It isn't really the fastest way to fix things, but this is a
374 * very uncommon corner.
375 */
376 if (actual_end <= start)
377 goto cleanup_and_bail_uncompressed;
378
Chris Masonc8b97812008-10-29 14:49:59 -0400379 total_compressed = actual_end - start;
380
381 /* we want to make sure that amount of ram required to uncompress
382 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500383 * of a compressed extent to 128k. This is a crucial number
384 * because it also controls how easily we can spread reads across
385 * cpus for decompression.
386 *
387 * We also want to make sure the amount of IO required to do
388 * a random read is reasonably small, so we limit the size of
389 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400390 */
391 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400392 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500393 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400394 total_in = 0;
395 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400396
Chris Mason771ed682008-11-06 22:02:51 -0500397 /*
398 * we do compression for mount -o compress and when the
399 * inode has not been flagged as nocompress. This flag can
400 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200402 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500403 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000404 (BTRFS_I(inode)->force_compress) ||
405 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400406 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400407 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800408 if (!pages) {
409 /* just bail out to the uncompressed code */
410 goto cont;
411 }
Chris Mason179e29e2007-11-01 11:28:41 -0400412
Li Zefan261507a02010-12-17 14:21:50 +0800413 if (BTRFS_I(inode)->force_compress)
414 compress_type = BTRFS_I(inode)->force_compress;
415
416 ret = btrfs_compress_pages(compress_type,
417 inode->i_mapping, start,
418 total_compressed, pages,
419 nr_pages, &nr_pages_ret,
420 &total_in,
421 &total_compressed,
422 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400423
424 if (!ret) {
425 unsigned long offset = total_compressed &
426 (PAGE_CACHE_SIZE - 1);
427 struct page *page = pages[nr_pages_ret - 1];
428 char *kaddr;
429
430 /* zero the tail end of the last page, we might be
431 * sending it down to disk
432 */
433 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800434 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400435 memset(kaddr + offset, 0,
436 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800437 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400438 }
439 will_compress = 1;
440 }
441 }
Li Zefan560f7d72011-09-08 10:22:01 +0800442cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400443 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400444 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100445 if (IS_ERR(trans)) {
446 ret = PTR_ERR(trans);
447 trans = NULL;
448 goto cleanup_and_out;
449 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400450 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500451
Chris Masonc8b97812008-10-29 14:49:59 -0400452 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500453 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400454 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500455 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400456 */
457 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000458 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400459 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500460 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400461 ret = cow_file_range_inline(trans, root, inode,
462 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000463 total_compressed,
464 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400465 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100466 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500467 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100468 * inline extent creation worked or returned error,
469 * we don't need to create any more async work items.
470 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500471 */
Chris Masonc8b97812008-10-29 14:49:59 -0400472 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400473 &BTRFS_I(inode)->io_tree,
474 start, end, NULL,
475 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400476 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400477 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000478
479 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 goto free_pages_out;
481 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000482 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400483 }
484
485 if (will_compress) {
486 /*
487 * we aren't doing an inline extent round the compressed size
488 * up to a block size boundary so the allocator does sane
489 * things
490 */
491 total_compressed = (total_compressed + blocksize - 1) &
492 ~(blocksize - 1);
493
494 /*
495 * one last check to make sure the compression is really a
496 * win, compare the page count read with the blocks on disk
497 */
498 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
499 ~(PAGE_CACHE_SIZE - 1);
500 if (total_compressed >= total_in) {
501 will_compress = 0;
502 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 num_bytes = total_in;
504 }
505 }
506 if (!will_compress && pages) {
507 /*
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
510 */
511 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400512 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 page_cache_release(pages[i]);
514 }
515 kfree(pages);
516 pages = NULL;
517 total_compressed = 0;
518 nr_pages_ret = 0;
519
520 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500521 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
522 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500523 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500524 }
Chris Masonc8b97812008-10-29 14:49:59 -0400525 }
Chris Mason771ed682008-11-06 22:02:51 -0500526 if (will_compress) {
527 *num_added += 1;
528
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
532 */
533 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800534 total_compressed, pages, nr_pages_ret,
535 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500536
Yan, Zheng24ae6362010-12-06 07:02:36 +0000537 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500538 start += num_bytes;
539 pages = NULL;
540 cond_resched();
541 goto again;
542 }
543 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500544cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500545 /*
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
551 */
552 if (page_offset(locked_page) >= start &&
553 page_offset(locked_page) <= end) {
554 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */
556 }
Li Zefan261507a02010-12-17 14:21:50 +0800557 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500559 *num_added += 1;
560 }
561
562out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100563 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500564
565free_pages_out:
566 for (i = 0; i < nr_pages_ret; i++) {
567 WARN_ON(pages[i]->mapping);
568 page_cache_release(pages[i]);
569 }
Chris Masond3977122009-01-05 21:25:51 -0500570 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500571
572 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573
574cleanup_and_out:
575 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
576 start, end, NULL,
577 EXTENT_CLEAR_UNLOCK_PAGE |
578 EXTENT_CLEAR_DIRTY |
579 EXTENT_CLEAR_DELALLOC |
580 EXTENT_SET_WRITEBACK |
581 EXTENT_END_WRITEBACK);
582 if (!trans || IS_ERR(trans))
583 btrfs_error(root->fs_info, ret, "Failed to join transaction");
584 else
585 btrfs_abort_transaction(trans, root, ret);
586 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500587}
588
589/*
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
594 */
595static noinline int submit_compressed_extents(struct inode *inode,
596 struct async_cow *async_cow)
597{
598 struct async_extent *async_extent;
599 u64 alloc_hint = 0;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_key ins;
602 struct extent_map *em;
603 struct btrfs_root *root = BTRFS_I(inode)->root;
604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
605 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500606 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 if (list_empty(&async_cow->extents))
609 return 0;
610
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500611again:
Chris Masond3977122009-01-05 21:25:51 -0500612 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500613 async_extent = list_entry(async_cow->extents.next,
614 struct async_extent, list);
615 list_del(&async_extent->list);
616
617 io_tree = &BTRFS_I(inode)->io_tree;
618
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500619retry:
Chris Mason771ed682008-11-06 22:02:51 -0500620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent->pages) {
622 int page_started = 0;
623 unsigned long nr_written = 0;
624
625 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100627 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500628
629 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 ret = cow_file_range(inode, async_cow->locked_page,
631 async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1,
634 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500635
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 /* JDM XXX */
637
Chris Mason771ed682008-11-06 22:02:51 -0500638 /*
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
643 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500644 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500645 extent_write_locked_range(io_tree,
646 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500648 async_extent->ram_size - 1,
649 btrfs_get_extent,
650 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500651 else if (ret)
652 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500653 kfree(async_extent);
654 cond_resched();
655 continue;
656 }
657
658 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100659 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500660
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400661 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100662 if (IS_ERR(trans)) {
663 ret = PTR_ERR(trans);
664 } else {
665 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
666 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500667 async_extent->compressed_size,
668 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500669 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600670 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 btrfs_abort_transaction(trans, root, ret);
672 btrfs_end_transaction(trans, root);
673 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000674
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500675 if (ret) {
676 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500677
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500678 for (i = 0; i < async_extent->nr_pages; i++) {
679 WARN_ON(async_extent->pages[i]->mapping);
680 page_cache_release(async_extent->pages[i]);
681 }
682 kfree(async_extent->pages);
683 async_extent->nr_pages = 0;
684 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500685
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 if (ret == -ENOSPC)
687 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500688 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 }
690
Yan, Zhengc2167752009-11-12 09:34:21 +0000691 /*
692 * here we're doing allocation and writeback of the
693 * compressed pages
694 */
695 btrfs_drop_extent_cache(inode, async_extent->start,
696 async_extent->start +
697 async_extent->ram_size - 1, 0);
698
David Sterba172ddd62011-04-21 00:48:27 +0200699 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500700 if (!em)
701 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500702 em->start = async_extent->start;
703 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500704 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400705 em->mod_start = em->start;
706 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500707
708 em->block_start = ins.objectid;
709 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500710 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500711 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800712 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500713 set_bit(EXTENT_FLAG_PINNED, &em->flags);
714 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400715 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500716
Chris Masond3977122009-01-05 21:25:51 -0500717 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400718 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500719 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400720 if (!ret)
721 list_move(&em->list,
722 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400723 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500724 if (ret != -EEXIST) {
725 free_extent_map(em);
726 break;
727 }
728 btrfs_drop_extent_cache(inode, async_extent->start,
729 async_extent->start +
730 async_extent->ram_size - 1, 0);
731 }
732
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500733 if (ret)
734 goto out_free_reserve;
735
Li Zefan261507a02010-12-17 14:21:50 +0800736 ret = btrfs_add_ordered_extent_compress(inode,
737 async_extent->start,
738 ins.objectid,
739 async_extent->ram_size,
740 ins.offset,
741 BTRFS_ORDERED_COMPRESSED,
742 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500743 if (ret)
744 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500745
Chris Mason771ed682008-11-06 22:02:51 -0500746 /*
747 * clear dirty, set writeback and unlock the pages.
748 */
749 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400750 &BTRFS_I(inode)->io_tree,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
754 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
755 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400756 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400757 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500758
759 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500760 async_extent->start,
761 async_extent->ram_size,
762 ins.objectid,
763 ins.offset, async_extent->pages,
764 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500765 alloc_hint = ins.objectid + ins.offset;
766 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 if (ret)
768 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500769 cond_resched();
770 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100771 ret = 0;
772out:
773 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774out_free_reserve:
775 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100776out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
778 async_extent->start,
779 async_extent->start +
780 async_extent->ram_size - 1,
781 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
782 EXTENT_CLEAR_UNLOCK |
783 EXTENT_CLEAR_DELALLOC |
784 EXTENT_CLEAR_DIRTY |
785 EXTENT_SET_WRITEBACK |
786 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100787 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500789}
790
Josef Bacik4b46fce2010-05-23 11:00:55 -0400791static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
792 u64 num_bytes)
793{
794 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
795 struct extent_map *em;
796 u64 alloc_hint = 0;
797
798 read_lock(&em_tree->lock);
799 em = search_extent_mapping(em_tree, start, num_bytes);
800 if (em) {
801 /*
802 * if block start isn't an actual block number then find the
803 * first block in this inode and use that as a hint. If that
804 * block is also bogus then just don't worry about it.
805 */
806 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
807 free_extent_map(em);
808 em = search_extent_mapping(em_tree, 0, 0);
809 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
810 alloc_hint = em->block_start;
811 if (em)
812 free_extent_map(em);
813 } else {
814 alloc_hint = em->block_start;
815 free_extent_map(em);
816 }
817 }
818 read_unlock(&em_tree->lock);
819
820 return alloc_hint;
821}
822
Chris Mason771ed682008-11-06 22:02:51 -0500823/*
824 * when extent_io.c finds a delayed allocation range in the file,
825 * the call backs end up in this code. The basic idea is to
826 * allocate extents on disk for the range, and create ordered data structs
827 * in ram to track those extents.
828 *
829 * locked_page is the page that writepage had locked already. We use
830 * it to make sure we don't do extra locks or unlocks.
831 *
832 * *page_started is set to one if we unlock locked_page and do everything
833 * required to start IO on it. It may be clean and already done with
834 * IO when we return.
835 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000836static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
837 struct inode *inode,
838 struct btrfs_root *root,
839 struct page *locked_page,
840 u64 start, u64 end, int *page_started,
841 unsigned long *nr_written,
842 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500843{
Chris Mason771ed682008-11-06 22:02:51 -0500844 u64 alloc_hint = 0;
845 u64 num_bytes;
846 unsigned long ram_size;
847 u64 disk_num_bytes;
848 u64 cur_alloc_size;
849 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500850 struct btrfs_key ins;
851 struct extent_map *em;
852 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
853 int ret = 0;
854
Liu Bo83eea1f2012-07-10 05:28:39 -0600855 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500856
Chris Mason771ed682008-11-06 22:02:51 -0500857 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
858 num_bytes = max(blocksize, num_bytes);
859 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500860
Chris Mason4cb53002011-05-24 15:35:30 -0400861 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400862 if (num_bytes < 64 * 1024 &&
863 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400864 btrfs_add_inode_defrag(trans, inode);
865
Chris Mason771ed682008-11-06 22:02:51 -0500866 if (start == 0) {
867 /* lets try to make an inline extent */
868 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000869 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500870 if (ret == 0) {
871 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400872 &BTRFS_I(inode)->io_tree,
873 start, end, NULL,
874 EXTENT_CLEAR_UNLOCK_PAGE |
875 EXTENT_CLEAR_UNLOCK |
876 EXTENT_CLEAR_DELALLOC |
877 EXTENT_CLEAR_DIRTY |
878 EXTENT_SET_WRITEBACK |
879 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000880
Chris Mason771ed682008-11-06 22:02:51 -0500881 *nr_written = *nr_written +
882 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
883 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500884 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 } else if (ret < 0) {
886 btrfs_abort_transaction(trans, root, ret);
887 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500888 }
889 }
Chris Masonc8b97812008-10-29 14:49:59 -0400890
891 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200892 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400893
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400895 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400896
Chris Masond3977122009-01-05 21:25:51 -0500897 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400898 unsigned long op;
899
Josef Bacik287a0ab2010-03-19 18:07:23 +0000900 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500902 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500903 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100904 if (ret < 0) {
905 btrfs_abort_transaction(trans, root, ret);
906 goto out_unlock;
907 }
Chris Masond3977122009-01-05 21:25:51 -0500908
David Sterba172ddd62011-04-21 00:48:27 +0200909 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100910 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500912 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500913 ram_size = ins.offset;
914 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400915 em->mod_start = em->start;
916 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400917
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400919 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500920 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400921 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400922 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400923 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400924
Chris Masond3977122009-01-05 21:25:51 -0500925 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400926 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400928 if (!ret)
929 list_move(&em->list,
930 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400931 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400932 if (ret != -EEXIST) {
933 free_extent_map(em);
934 break;
935 }
936 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400937 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 }
939
Chris Mason98d20f62008-04-14 09:46:10 -0400940 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400941 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500942 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100943 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400944
Yan Zheng17d217f2008-12-12 10:03:38 -0500945 if (root->root_key.objectid ==
946 BTRFS_DATA_RELOC_TREE_OBJECTID) {
947 ret = btrfs_reloc_clone_csums(inode, start,
948 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100949 if (ret) {
950 btrfs_abort_transaction(trans, root, ret);
951 goto out_unlock;
952 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500953 }
954
Chris Masond3977122009-01-05 21:25:51 -0500955 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400956 break;
Chris Masond3977122009-01-05 21:25:51 -0500957
Chris Masonc8b97812008-10-29 14:49:59 -0400958 /* we're not doing compressed IO, don't unlock the first
959 * page (which the caller expects to stay locked), don't
960 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400961 *
962 * Do set the Private2 bit so we know this page was properly
963 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400964 */
Chris Masona791e352009-10-08 11:27:10 -0400965 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
966 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
967 EXTENT_SET_PRIVATE2;
968
Chris Masonc8b97812008-10-29 14:49:59 -0400969 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
970 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400971 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400972 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500973 num_bytes -= cur_alloc_size;
974 alloc_hint = ins.objectid + ins.offset;
975 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400976 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500978 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000979
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100980out_unlock:
981 extent_clear_unlock_delalloc(inode,
982 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400983 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100984 EXTENT_CLEAR_UNLOCK_PAGE |
985 EXTENT_CLEAR_UNLOCK |
986 EXTENT_CLEAR_DELALLOC |
987 EXTENT_CLEAR_DIRTY |
988 EXTENT_SET_WRITEBACK |
989 EXTENT_END_WRITEBACK);
990
991 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500992}
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000994static noinline int cow_file_range(struct inode *inode,
995 struct page *locked_page,
996 u64 start, u64 end, int *page_started,
997 unsigned long *nr_written,
998 int unlock)
999{
1000 struct btrfs_trans_handle *trans;
1001 struct btrfs_root *root = BTRFS_I(inode)->root;
1002 int ret;
1003
1004 trans = btrfs_join_transaction(root);
1005 if (IS_ERR(trans)) {
1006 extent_clear_unlock_delalloc(inode,
1007 &BTRFS_I(inode)->io_tree,
1008 start, end, locked_page,
1009 EXTENT_CLEAR_UNLOCK_PAGE |
1010 EXTENT_CLEAR_UNLOCK |
1011 EXTENT_CLEAR_DELALLOC |
1012 EXTENT_CLEAR_DIRTY |
1013 EXTENT_SET_WRITEBACK |
1014 EXTENT_END_WRITEBACK);
1015 return PTR_ERR(trans);
1016 }
1017 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1018
1019 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1020 page_started, nr_written, unlock);
1021
1022 btrfs_end_transaction(trans, root);
1023
1024 return ret;
1025}
1026
Chris Mason771ed682008-11-06 22:02:51 -05001027/*
1028 * work queue call back to started compression on a file and pages
1029 */
1030static noinline void async_cow_start(struct btrfs_work *work)
1031{
1032 struct async_cow *async_cow;
1033 int num_added = 0;
1034 async_cow = container_of(work, struct async_cow, work);
1035
1036 compress_file_range(async_cow->inode, async_cow->locked_page,
1037 async_cow->start, async_cow->end, async_cow,
1038 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001039 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001040 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001041 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001042 }
Chris Mason771ed682008-11-06 22:02:51 -05001043}
1044
1045/*
1046 * work queue call back to submit previously compressed pages
1047 */
1048static noinline void async_cow_submit(struct btrfs_work *work)
1049{
1050 struct async_cow *async_cow;
1051 struct btrfs_root *root;
1052 unsigned long nr_pages;
1053
1054 async_cow = container_of(work, struct async_cow, work);
1055
1056 root = async_cow->root;
1057 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1058 PAGE_CACHE_SHIFT;
1059
Josef Bacik66657b32012-08-01 15:36:24 -04001060 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001061 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001062 waitqueue_active(&root->fs_info->async_submit_wait))
1063 wake_up(&root->fs_info->async_submit_wait);
1064
Chris Masond3977122009-01-05 21:25:51 -05001065 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001066 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001067}
Chris Masonc8b97812008-10-29 14:49:59 -04001068
Chris Mason771ed682008-11-06 22:02:51 -05001069static noinline void async_cow_free(struct btrfs_work *work)
1070{
1071 struct async_cow *async_cow;
1072 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001073 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001074 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001075 kfree(async_cow);
1076}
1077
1078static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1079 u64 start, u64 end, int *page_started,
1080 unsigned long *nr_written)
1081{
1082 struct async_cow *async_cow;
1083 struct btrfs_root *root = BTRFS_I(inode)->root;
1084 unsigned long nr_pages;
1085 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001086 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001087
Chris Masona3429ab2009-10-08 12:30:20 -04001088 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1089 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001090 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001091 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001093 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001094 async_cow->root = root;
1095 async_cow->locked_page = locked_page;
1096 async_cow->start = start;
1097
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001098 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001099 cur_end = end;
1100 else
1101 cur_end = min(end, start + 512 * 1024 - 1);
1102
1103 async_cow->end = cur_end;
1104 INIT_LIST_HEAD(&async_cow->extents);
1105
1106 async_cow->work.func = async_cow_start;
1107 async_cow->work.ordered_func = async_cow_submit;
1108 async_cow->work.ordered_free = async_cow_free;
1109 async_cow->work.flags = 0;
1110
Chris Mason771ed682008-11-06 22:02:51 -05001111 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1112 PAGE_CACHE_SHIFT;
1113 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1114
1115 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1116 &async_cow->work);
1117
1118 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1119 wait_event(root->fs_info->async_submit_wait,
1120 (atomic_read(&root->fs_info->async_delalloc_pages) <
1121 limit));
1122 }
1123
Chris Masond3977122009-01-05 21:25:51 -05001124 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001125 atomic_read(&root->fs_info->async_delalloc_pages)) {
1126 wait_event(root->fs_info->async_submit_wait,
1127 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1128 0));
1129 }
1130
1131 *nr_written += nr_pages;
1132 start = cur_end + 1;
1133 }
1134 *page_started = 1;
1135 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001136}
1137
Chris Masond3977122009-01-05 21:25:51 -05001138static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001139 u64 bytenr, u64 num_bytes)
1140{
1141 int ret;
1142 struct btrfs_ordered_sum *sums;
1143 LIST_HEAD(list);
1144
Yan Zheng07d400a2009-01-06 11:42:00 -05001145 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001146 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001147 if (ret == 0 && list_empty(&list))
1148 return 0;
1149
1150 while (!list_empty(&list)) {
1151 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1152 list_del(&sums->list);
1153 kfree(sums);
1154 }
1155 return 1;
1156}
1157
Chris Masond352ac62008-09-29 15:18:18 -04001158/*
1159 * when nowcow writeback call back. This checks for snapshots or COW copies
1160 * of the extents that exist in the file, and COWs the file as required.
1161 *
1162 * If no cow copies or snapshots exist, we write directly to the existing
1163 * blocks on disk
1164 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001165static noinline int run_delalloc_nocow(struct inode *inode,
1166 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001167 u64 start, u64 end, int *page_started, int force,
1168 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001169{
Chris Masonbe20aa92007-12-17 20:14:01 -05001170 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001171 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001172 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001173 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001174 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001175 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 u64 cow_start;
1177 u64 cur_offset;
1178 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 u64 disk_bytenr;
1181 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001182 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001184 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001185 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 int nocow;
1187 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001188 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001189 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001190
1191 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001192 if (!path) {
1193 extent_clear_unlock_delalloc(inode,
1194 &BTRFS_I(inode)->io_tree,
1195 start, end, locked_page,
1196 EXTENT_CLEAR_UNLOCK_PAGE |
1197 EXTENT_CLEAR_UNLOCK |
1198 EXTENT_CLEAR_DELALLOC |
1199 EXTENT_CLEAR_DIRTY |
1200 EXTENT_SET_WRITEBACK |
1201 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001202 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001203 }
Li Zefan82d59022011-04-20 10:33:24 +08001204
Liu Bo83eea1f2012-07-10 05:28:39 -06001205 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001206
1207 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001208 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001209 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001210 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001211
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001213 extent_clear_unlock_delalloc(inode,
1214 &BTRFS_I(inode)->io_tree,
1215 start, end, locked_page,
1216 EXTENT_CLEAR_UNLOCK_PAGE |
1217 EXTENT_CLEAR_UNLOCK |
1218 EXTENT_CLEAR_DELALLOC |
1219 EXTENT_CLEAR_DIRTY |
1220 EXTENT_SET_WRITEBACK |
1221 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001222 btrfs_free_path(path);
1223 return PTR_ERR(trans);
1224 }
1225
Josef Bacik74b21072011-04-13 12:02:53 -04001226 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001227
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 cow_start = (u64)-1;
1229 cur_offset = start;
1230 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001231 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 if (ret < 0) {
1234 btrfs_abort_transaction(trans, root, ret);
1235 goto error;
1236 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1238 leaf = path->nodes[0];
1239 btrfs_item_key_to_cpu(leaf, &found_key,
1240 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001241 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 found_key.type == BTRFS_EXTENT_DATA_KEY)
1243 path->slots[0]--;
1244 }
1245 check_prev = 0;
1246next_slot:
1247 leaf = path->nodes[0];
1248 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1249 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 if (ret < 0) {
1251 btrfs_abort_transaction(trans, root, ret);
1252 goto error;
1253 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 if (ret > 0)
1255 break;
1256 leaf = path->nodes[0];
1257 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001258
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 nocow = 0;
1260 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001261 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001263
Li Zefan33345d012011-04-20 10:31:50 +08001264 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1266 found_key.offset > end)
1267 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (found_key.offset > cur_offset) {
1270 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001271 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 goto out_check;
1273 }
Chris Masonc31f8832008-01-08 15:46:31 -05001274
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 fi = btrfs_item_ptr(leaf, path->slots[0],
1276 struct btrfs_file_extent_item);
1277 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Yan Zhengd899e052008-10-30 14:25:28 -04001279 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1280 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 extent_end = found_key.offset +
1284 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001285 disk_num_bytes =
1286 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (extent_end <= start) {
1288 path->slots[0]++;
1289 goto next_slot;
1290 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 if (disk_bytenr == 0)
1292 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 if (btrfs_file_extent_compression(leaf, fi) ||
1294 btrfs_file_extent_encryption(leaf, fi) ||
1295 btrfs_file_extent_other_encoding(leaf, fi))
1296 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001297 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1298 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001299 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001301 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001302 found_key.offset -
1303 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001304 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001306 disk_bytenr += cur_offset - found_key.offset;
1307 num_bytes = min(end + 1, extent_end) - cur_offset;
1308 /*
1309 * force cow if csum exists in the range.
1310 * this ensure that csum for a given extent are
1311 * either valid or do not exist.
1312 */
1313 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1314 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 nocow = 1;
1316 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1317 extent_end = found_key.offset +
1318 btrfs_file_extent_inline_len(leaf, fi);
1319 extent_end = ALIGN(extent_end, root->sectorsize);
1320 } else {
1321 BUG_ON(1);
1322 }
1323out_check:
1324 if (extent_end <= start) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
1328 if (!nocow) {
1329 if (cow_start == (u64)-1)
1330 cow_start = cur_offset;
1331 cur_offset = extent_end;
1332 if (cur_offset > end)
1333 break;
1334 path->slots[0]++;
1335 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001336 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
David Sterbab3b4aa72011-04-21 01:20:15 +02001338 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001340 ret = __cow_file_range(trans, inode, root, locked_page,
1341 cow_start, found_key.offset - 1,
1342 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001343 if (ret) {
1344 btrfs_abort_transaction(trans, root, ret);
1345 goto error;
1346 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001348 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001349
Yan Zhengd899e052008-10-30 14:25:28 -04001350 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1351 struct extent_map *em;
1352 struct extent_map_tree *em_tree;
1353 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001354 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001355 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001356 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001357 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001358 em->len = num_bytes;
1359 em->block_len = num_bytes;
1360 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001361 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001362 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001363 em->mod_start = em->start;
1364 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001365 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001366 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001367 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001368 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001369 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001370 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001371 if (!ret)
1372 list_move(&em->list,
1373 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001374 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (ret != -EEXIST) {
1376 free_extent_map(em);
1377 break;
1378 }
1379 btrfs_drop_extent_cache(inode, em->start,
1380 em->start + em->len - 1, 0);
1381 }
1382 type = BTRFS_ORDERED_PREALLOC;
1383 } else {
1384 type = BTRFS_ORDERED_NOCOW;
1385 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001386
1387 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001388 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001389 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001390
Yan, Zhengefa56462010-05-16 10:49:59 -04001391 if (root->root_key.objectid ==
1392 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1393 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1394 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 if (ret) {
1396 btrfs_abort_transaction(trans, root, ret);
1397 goto error;
1398 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001399 }
1400
Yan Zhengd899e052008-10-30 14:25:28 -04001401 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001402 cur_offset, cur_offset + num_bytes - 1,
1403 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1404 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1405 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001406 cur_offset = extent_end;
1407 if (cur_offset > end)
1408 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001409 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001410 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001411
Josef Bacik17ca04a2012-05-31 15:58:55 -04001412 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001413 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 cur_offset = end;
1415 }
1416
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001418 ret = __cow_file_range(trans, inode, root, locked_page,
1419 cow_start, end,
1420 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001421 if (ret) {
1422 btrfs_abort_transaction(trans, root, ret);
1423 goto error;
1424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 }
1426
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427error:
Miao Xiea698d0752012-09-20 01:51:59 -06001428 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 if (!ret)
1430 ret = err;
1431
Josef Bacik17ca04a2012-05-31 15:58:55 -04001432 if (ret && cur_offset < end)
1433 extent_clear_unlock_delalloc(inode,
1434 &BTRFS_I(inode)->io_tree,
1435 cur_offset, end, locked_page,
1436 EXTENT_CLEAR_UNLOCK_PAGE |
1437 EXTENT_CLEAR_UNLOCK |
1438 EXTENT_CLEAR_DELALLOC |
1439 EXTENT_CLEAR_DIRTY |
1440 EXTENT_SET_WRITEBACK |
1441 EXTENT_END_WRITEBACK);
1442
Yan Zheng7ea394f2008-08-05 13:05:02 -04001443 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001445}
1446
Chris Masond352ac62008-09-29 15:18:18 -04001447/*
1448 * extent_io.c call back to do delayed allocation processing
1449 */
Chris Masonc8b97812008-10-29 14:49:59 -04001450static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001451 u64 start, u64 end, int *page_started,
1452 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001453{
Chris Masonbe20aa92007-12-17 20:14:01 -05001454 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001455 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001456
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001457 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001458 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001459 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001460 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001461 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001462 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001463 } else if (!btrfs_test_opt(root, COMPRESS) &&
1464 !(BTRFS_I(inode)->force_compress) &&
1465 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001466 ret = cow_file_range(inode, locked_page, start, end,
1467 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001468 } else {
1469 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1470 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001471 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001472 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001473 }
Chris Masonb888db22007-08-27 16:49:44 -04001474 return ret;
1475}
1476
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477static void btrfs_split_extent_hook(struct inode *inode,
1478 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001480 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001482 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001483
Josef Bacik9e0baf62011-07-15 15:16:44 +00001484 spin_lock(&BTRFS_I(inode)->lock);
1485 BTRFS_I(inode)->outstanding_extents++;
1486 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001487}
1488
1489/*
1490 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1491 * extents so we can keep track of new extents that are just merged onto old
1492 * extents, such as when we are doing sequential writes, so we can properly
1493 * account for the metadata space we'll need.
1494 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001495static void btrfs_merge_extent_hook(struct inode *inode,
1496 struct extent_state *new,
1497 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499 /* not delalloc, ignore it */
1500 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001501 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502
Josef Bacik9e0baf62011-07-15 15:16:44 +00001503 spin_lock(&BTRFS_I(inode)->lock);
1504 BTRFS_I(inode)->outstanding_extents--;
1505 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506}
1507
Chris Masond352ac62008-09-29 15:18:18 -04001508/*
1509 * extent_io.c set_bit_hook, used to track delayed allocation
1510 * bytes in this file, and to maintain the list of inodes that
1511 * have pending delalloc work to be done.
1512 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001513static void btrfs_set_bit_hook(struct inode *inode,
1514 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001515{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516
Chris Mason75eff682008-12-15 15:54:40 -05001517 /*
1518 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001519 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001520 * bit, which is only set or cleared with irqs on
1521 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001523 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001524 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001525 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526
Josef Bacik9e0baf62011-07-15 15:16:44 +00001527 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001528 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001529 } else {
1530 spin_lock(&BTRFS_I(inode)->lock);
1531 BTRFS_I(inode)->outstanding_extents++;
1532 spin_unlock(&BTRFS_I(inode)->lock);
1533 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001534
Miao Xie963d6782013-01-29 10:10:51 +00001535 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1536 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001537 spin_lock(&BTRFS_I(inode)->lock);
1538 BTRFS_I(inode)->delalloc_bytes += len;
1539 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1540 &BTRFS_I(inode)->runtime_flags)) {
1541 spin_lock(&root->fs_info->delalloc_lock);
1542 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1543 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1544 &root->fs_info->delalloc_inodes);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1546 &BTRFS_I(inode)->runtime_flags);
1547 }
1548 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001549 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001550 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001551 }
Chris Mason291d6732008-01-29 15:55:23 -05001552}
1553
Chris Masond352ac62008-09-29 15:18:18 -04001554/*
1555 * extent_io.c clear_bit_hook, see set_bit_hook for why
1556 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001557static void btrfs_clear_bit_hook(struct inode *inode,
1558 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001559{
Chris Mason75eff682008-12-15 15:54:40 -05001560 /*
1561 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001562 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001563 * bit, which is only set or cleared with irqs on
1564 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001566 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001568 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001569
Josef Bacik9e0baf62011-07-15 15:16:44 +00001570 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001571 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1573 spin_lock(&BTRFS_I(inode)->lock);
1574 BTRFS_I(inode)->outstanding_extents--;
1575 spin_unlock(&BTRFS_I(inode)->lock);
1576 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001577
1578 if (*bits & EXTENT_DO_ACCOUNTING)
1579 btrfs_delalloc_release_metadata(inode, len);
1580
Josef Bacik0cb59c92010-07-02 12:14:14 -04001581 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1582 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001584
Miao Xie963d6782013-01-29 10:10:51 +00001585 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1586 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001587 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001588 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001589 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001590 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1591 &BTRFS_I(inode)->runtime_flags)) {
1592 spin_lock(&root->fs_info->delalloc_lock);
1593 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1594 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1595 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1596 &BTRFS_I(inode)->runtime_flags);
1597 }
1598 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001599 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001600 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001601 }
Chris Mason291d6732008-01-29 15:55:23 -05001602}
1603
Chris Masond352ac62008-09-29 15:18:18 -04001604/*
1605 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1606 * we don't create bios that span stripes or chunks
1607 */
Chris Mason239b14b2008-03-24 15:02:07 -04001608int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001609 size_t size, struct bio *bio,
1610 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001611{
1612 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001613 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001614 u64 length = 0;
1615 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001616 int ret;
1617
Chris Mason771ed682008-11-06 22:02:51 -05001618 if (bio_flags & EXTENT_BIO_COMPRESSED)
1619 return 0;
1620
Chris Masonf2d8d742008-04-21 10:03:05 -04001621 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001622 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001623 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001624 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001625 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001626 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001627 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001628 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001629 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001630}
1631
Chris Masond352ac62008-09-29 15:18:18 -04001632/*
1633 * in order to insert checksums into the metadata in large chunks,
1634 * we wait until bio submission time. All the pages in the bio are
1635 * checksummed and sums are attached onto the ordered extent record.
1636 *
1637 * At IO completion time the cums attached on the ordered extent record
1638 * are inserted into the btree
1639 */
Chris Masond3977122009-01-05 21:25:51 -05001640static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1641 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001642 unsigned long bio_flags,
1643 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001644{
Chris Mason065631f2008-02-20 12:07:25 -05001645 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001646 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001647
Chris Masond20f7042008-12-08 16:58:54 -05001648 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001649 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001650 return 0;
1651}
Chris Masone0156402008-04-16 11:15:20 -04001652
Chris Mason4a69a412008-11-06 22:03:00 -05001653/*
1654 * in order to insert checksums into the metadata in large chunks,
1655 * we wait until bio submission time. All the pages in the bio are
1656 * checksummed and sums are attached onto the ordered extent record.
1657 *
1658 * At IO completion time the cums attached on the ordered extent record
1659 * are inserted into the btree
1660 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001661static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001662 int mirror_num, unsigned long bio_flags,
1663 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001664{
1665 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001666 int ret;
1667
1668 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1669 if (ret)
1670 bio_endio(bio, ret);
1671 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001672}
1673
Chris Masond352ac62008-09-29 15:18:18 -04001674/*
Chris Masoncad321a2008-12-17 14:51:42 -05001675 * extent_io.c submission hook. This does the right thing for csum calculation
1676 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001677 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001678static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001679 int mirror_num, unsigned long bio_flags,
1680 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001681{
1682 struct btrfs_root *root = BTRFS_I(inode)->root;
1683 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001684 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001685 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001686 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001687
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001688 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001689
Liu Bo83eea1f2012-07-10 05:28:39 -06001690 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001691 metadata = 2;
1692
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001693 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001694 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1695 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001696 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001697
Chris Masond20f7042008-12-08 16:58:54 -05001698 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001699 ret = btrfs_submit_compressed_read(inode, bio,
1700 mirror_num,
1701 bio_flags);
1702 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001703 } else if (!skip_sum) {
1704 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1705 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001706 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001707 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001708 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001709 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001710 /* csum items have already been cloned */
1711 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1712 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001713 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001714 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001715 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001716 bio_flags, bio_offset,
1717 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001718 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001719 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001720 } else if (!skip_sum) {
1721 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1722 if (ret)
1723 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001724 }
1725
Chris Mason0b86a832008-03-24 15:01:56 -04001726mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001727 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1728
1729out:
1730 if (ret < 0)
1731 bio_endio(bio, ret);
1732 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001733}
Chris Mason6885f302008-02-20 16:11:05 -05001734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * given a list of ordered sums record them in the inode. This happens
1737 * at IO completion time based on sums calculated at bio submission time.
1738 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001739static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001740 struct inode *inode, u64 file_offset,
1741 struct list_head *list)
1742{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001743 struct btrfs_ordered_sum *sum;
1744
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001745 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001746 btrfs_csum_file_blocks(trans,
1747 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001748 }
1749 return 0;
1750}
1751
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1753 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001754{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001755 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001756 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001757 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001758}
1759
Chris Masond352ac62008-09-29 15:18:18 -04001760/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001761struct btrfs_writepage_fixup {
1762 struct page *page;
1763 struct btrfs_work work;
1764};
1765
Christoph Hellwigb2950862008-12-02 09:54:17 -05001766static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001767{
1768 struct btrfs_writepage_fixup *fixup;
1769 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001770 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001771 struct page *page;
1772 struct inode *inode;
1773 u64 page_start;
1774 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001775 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001776
1777 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1778 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001779again:
Chris Mason247e7432008-07-17 12:53:51 -04001780 lock_page(page);
1781 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1782 ClearPageChecked(page);
1783 goto out_page;
1784 }
1785
1786 inode = page->mapping->host;
1787 page_start = page_offset(page);
1788 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1789
Josef Bacik2ac55d42010-02-03 19:33:23 +00001790 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001791 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001792
1793 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001794 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001795 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001796
1797 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1798 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001799 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1800 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001801 unlock_page(page);
1802 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001803 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001804 goto again;
1805 }
Chris Mason247e7432008-07-17 12:53:51 -04001806
Jeff Mahoney87826df2012-02-15 16:23:57 +01001807 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1808 if (ret) {
1809 mapping_set_error(page->mapping, ret);
1810 end_extent_writepage(page, ret, page_start, page_end);
1811 ClearPageChecked(page);
1812 goto out;
1813 }
1814
Josef Bacik2ac55d42010-02-03 19:33:23 +00001815 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001816 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001817 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001818out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001819 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1820 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001821out_page:
1822 unlock_page(page);
1823 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001824 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001825}
1826
1827/*
1828 * There are a few paths in the higher layers of the kernel that directly
1829 * set the page dirty bit without asking the filesystem if it is a
1830 * good idea. This causes problems because we want to make sure COW
1831 * properly happens and the data=ordered rules are followed.
1832 *
Chris Masonc8b97812008-10-29 14:49:59 -04001833 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001834 * hasn't been properly setup for IO. We kick off an async process
1835 * to fix it up. The async helper will wait for ordered extents, set
1836 * the delalloc bit and make it safe to write the page.
1837 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001838static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001839{
1840 struct inode *inode = page->mapping->host;
1841 struct btrfs_writepage_fixup *fixup;
1842 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001843
Chris Mason8b62b722009-09-02 16:53:46 -04001844 /* this page is properly in the ordered list */
1845 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001846 return 0;
1847
1848 if (PageChecked(page))
1849 return -EAGAIN;
1850
1851 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1852 if (!fixup)
1853 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001854
Chris Mason247e7432008-07-17 12:53:51 -04001855 SetPageChecked(page);
1856 page_cache_get(page);
1857 fixup->work.func = btrfs_writepage_fixup_worker;
1858 fixup->page = page;
1859 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001860 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001861}
1862
Yan Zhengd899e052008-10-30 14:25:28 -04001863static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1864 struct inode *inode, u64 file_pos,
1865 u64 disk_bytenr, u64 disk_num_bytes,
1866 u64 num_bytes, u64 ram_bytes,
1867 u8 compression, u8 encryption,
1868 u16 other_encoding, int extent_type)
1869{
1870 struct btrfs_root *root = BTRFS_I(inode)->root;
1871 struct btrfs_file_extent_item *fi;
1872 struct btrfs_path *path;
1873 struct extent_buffer *leaf;
1874 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001875 int ret;
1876
1877 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001878 if (!path)
1879 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001880
Chris Masonb9473432009-03-13 11:00:37 -04001881 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001882
1883 /*
1884 * we may be replacing one extent in the tree with another.
1885 * The new extent is pinned in the extent map, and we don't want
1886 * to drop it from the cache until it is completely in the btree.
1887 *
1888 * So, tell btrfs_drop_extents to leave this extent in the cache.
1889 * the caller is expected to unpin it and allow it to be merged
1890 * with the others.
1891 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001892 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001893 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 if (ret)
1895 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001896
Li Zefan33345d012011-04-20 10:31:50 +08001897 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001898 ins.offset = file_pos;
1899 ins.type = BTRFS_EXTENT_DATA_KEY;
1900 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 if (ret)
1902 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001903 leaf = path->nodes[0];
1904 fi = btrfs_item_ptr(leaf, path->slots[0],
1905 struct btrfs_file_extent_item);
1906 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1907 btrfs_set_file_extent_type(leaf, fi, extent_type);
1908 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1909 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1910 btrfs_set_file_extent_offset(leaf, fi, 0);
1911 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1912 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1913 btrfs_set_file_extent_compression(leaf, fi, compression);
1914 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1915 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001916
Yan Zhengd899e052008-10-30 14:25:28 -04001917 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001918 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001919
1920 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001921
1922 ins.objectid = disk_bytenr;
1923 ins.offset = disk_num_bytes;
1924 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001925 ret = btrfs_alloc_reserved_file_extent(trans, root,
1926 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001927 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928out:
Yan Zhengd899e052008-10-30 14:25:28 -04001929 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001930
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001932}
1933
Chris Mason5d13a982009-03-13 11:41:46 -04001934/*
1935 * helper function for btrfs_finish_ordered_io, this
1936 * just reads in some of the csum leaves to prime them into ram
1937 * before we start the transaction. It limits the amount of btree
1938 * reads required while inside the transaction.
1939 */
Chris Masond352ac62008-09-29 15:18:18 -04001940/* as ordered data IO finishes, this gets called so we can finish
1941 * an ordered extent if the range of bytes in the file it covers are
1942 * fully written.
1943 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001944static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001945{
Josef Bacik5fd02042012-05-02 14:00:54 -04001946 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001947 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001948 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001949 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001950 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001951 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001952 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001953 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001954
Liu Bo83eea1f2012-07-10 05:28:39 -06001955 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001956
Josef Bacik5fd02042012-05-02 14:00:54 -04001957 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1958 ret = -EIO;
1959 goto out;
1960 }
1961
Yan, Zhengc2167752009-11-12 09:34:21 +00001962 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001963 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001964 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1965 if (nolock)
1966 trans = btrfs_join_transaction_nolock(root);
1967 else
1968 trans = btrfs_join_transaction(root);
1969 if (IS_ERR(trans)) {
1970 ret = PTR_ERR(trans);
1971 trans = NULL;
1972 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00001973 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001974 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1975 ret = btrfs_update_inode_fallback(trans, root, inode);
1976 if (ret) /* -ENOMEM or corruption */
1977 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001978 goto out;
1979 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001980
Josef Bacik2ac55d42010-02-03 19:33:23 +00001981 lock_extent_bits(io_tree, ordered_extent->file_offset,
1982 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001983 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001984
Josef Bacik0cb59c92010-07-02 12:14:14 -04001985 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001986 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001987 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001988 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001989 if (IS_ERR(trans)) {
1990 ret = PTR_ERR(trans);
1991 trans = NULL;
1992 goto out_unlock;
1993 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001994 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001995
Chris Masonc8b97812008-10-29 14:49:59 -04001996 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001997 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001998 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001999 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002000 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002001 ordered_extent->file_offset,
2002 ordered_extent->file_offset +
2003 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002004 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002005 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002006 ret = insert_reserved_file_extent(trans, inode,
2007 ordered_extent->file_offset,
2008 ordered_extent->start,
2009 ordered_extent->disk_len,
2010 ordered_extent->len,
2011 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002012 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002013 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002014 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002015 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2016 ordered_extent->file_offset, ordered_extent->len,
2017 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002018 if (ret < 0) {
2019 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002020 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002021 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002022
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 add_pending_csums(trans, inode, ordered_extent->file_offset,
2024 &ordered_extent->list);
2025
Josef Bacik6c760c02012-11-09 10:53:21 -05002026 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2027 ret = btrfs_update_inode_fallback(trans, root, inode);
2028 if (ret) { /* -ENOMEM or corruption */
2029 btrfs_abort_transaction(trans, root, ret);
2030 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002031 }
2032 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002033out_unlock:
2034 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2035 ordered_extent->file_offset +
2036 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002037out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002038 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002039 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002040 if (trans)
2041 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002042
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002043 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002044 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2045 ordered_extent->file_offset +
2046 ordered_extent->len - 1, NULL, GFP_NOFS);
2047
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002048 /*
2049 * If the ordered extent had an IOERR or something else went
2050 * wrong we need to return the space for this ordered extent
2051 * back to the allocator.
2052 */
2053 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2054 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2055 btrfs_free_reserved_extent(root, ordered_extent->start,
2056 ordered_extent->disk_len);
2057 }
2058
2059
Josef Bacik5fd02042012-05-02 14:00:54 -04002060 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002061 * This needs to be done to make sure anybody waiting knows we are done
2062 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002063 */
2064 btrfs_remove_ordered_extent(inode, ordered_extent);
2065
Chris Masone6dcd2d2008-07-17 12:53:50 -04002066 /* once for us */
2067 btrfs_put_ordered_extent(ordered_extent);
2068 /* once for the tree */
2069 btrfs_put_ordered_extent(ordered_extent);
2070
Josef Bacik5fd02042012-05-02 14:00:54 -04002071 return ret;
2072}
2073
2074static void finish_ordered_fn(struct btrfs_work *work)
2075{
2076 struct btrfs_ordered_extent *ordered_extent;
2077 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2078 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002079}
2080
Christoph Hellwigb2950862008-12-02 09:54:17 -05002081static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002082 struct extent_state *state, int uptodate)
2083{
Josef Bacik5fd02042012-05-02 14:00:54 -04002084 struct inode *inode = page->mapping->host;
2085 struct btrfs_root *root = BTRFS_I(inode)->root;
2086 struct btrfs_ordered_extent *ordered_extent = NULL;
2087 struct btrfs_workers *workers;
2088
liubo1abe9b82011-03-24 11:18:59 +00002089 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2090
Chris Mason8b62b722009-09-02 16:53:46 -04002091 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002092 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2093 end - start + 1, uptodate))
2094 return 0;
2095
2096 ordered_extent->work.func = finish_ordered_fn;
2097 ordered_extent->work.flags = 0;
2098
Liu Bo83eea1f2012-07-10 05:28:39 -06002099 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002100 workers = &root->fs_info->endio_freespace_worker;
2101 else
2102 workers = &root->fs_info->endio_write_workers;
2103 btrfs_queue_worker(workers, &ordered_extent->work);
2104
2105 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002106}
2107
Chris Masond352ac62008-09-29 15:18:18 -04002108/*
Chris Masond352ac62008-09-29 15:18:18 -04002109 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002110 * if there's a match, we allow the bio to finish. If not, the code in
2111 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002112 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002113static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002114 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002115{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002116 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002117 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002118 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002119 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002120 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002121 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002122 struct btrfs_root *root = BTRFS_I(inode)->root;
2123 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002124
Chris Masond20f7042008-12-08 16:58:54 -05002125 if (PageChecked(page)) {
2126 ClearPageChecked(page);
2127 goto good;
2128 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002129
2130 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002131 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002132
Yan Zheng17d217f2008-12-12 10:03:38 -05002133 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002134 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002135 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2136 GFP_NOFS);
2137 return 0;
2138 }
2139
Yanc2e639f2008-02-04 08:57:25 -05002140 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002141 private = state->private;
2142 ret = 0;
2143 } else {
2144 ret = get_state_private(io_tree, start, &private);
2145 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002146 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002147 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002148 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002149
Chris Masonff79f812007-10-15 16:22:25 -04002150 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2151 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002152 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002153 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002154
Cong Wang7ac687d2011-11-25 23:14:28 +08002155 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002156good:
Chris Mason07157aa2007-08-30 08:50:51 -04002157 return 0;
2158
2159zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002160 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002161 "private %llu\n",
2162 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002163 (unsigned long long)start, csum,
2164 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002165 memset(kaddr + offset, 1, end - start + 1);
2166 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002167 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002168 if (private == 0)
2169 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002170 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002171}
Chris Masonb888db22007-08-27 16:49:44 -04002172
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002173struct delayed_iput {
2174 struct list_head list;
2175 struct inode *inode;
2176};
2177
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002178/* JDM: If this is fs-wide, why can't we add a pointer to
2179 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002180void btrfs_add_delayed_iput(struct inode *inode)
2181{
2182 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2183 struct delayed_iput *delayed;
2184
2185 if (atomic_add_unless(&inode->i_count, -1, 1))
2186 return;
2187
2188 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2189 delayed->inode = inode;
2190
2191 spin_lock(&fs_info->delayed_iput_lock);
2192 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2193 spin_unlock(&fs_info->delayed_iput_lock);
2194}
2195
2196void btrfs_run_delayed_iputs(struct btrfs_root *root)
2197{
2198 LIST_HEAD(list);
2199 struct btrfs_fs_info *fs_info = root->fs_info;
2200 struct delayed_iput *delayed;
2201 int empty;
2202
2203 spin_lock(&fs_info->delayed_iput_lock);
2204 empty = list_empty(&fs_info->delayed_iputs);
2205 spin_unlock(&fs_info->delayed_iput_lock);
2206 if (empty)
2207 return;
2208
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002209 spin_lock(&fs_info->delayed_iput_lock);
2210 list_splice_init(&fs_info->delayed_iputs, &list);
2211 spin_unlock(&fs_info->delayed_iput_lock);
2212
2213 while (!list_empty(&list)) {
2214 delayed = list_entry(list.next, struct delayed_iput, list);
2215 list_del(&delayed->list);
2216 iput(delayed->inode);
2217 kfree(delayed);
2218 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002219}
2220
Yan, Zhengd68fc572010-05-16 10:49:58 -04002221enum btrfs_orphan_cleanup_state {
2222 ORPHAN_CLEANUP_STARTED = 1,
2223 ORPHAN_CLEANUP_DONE = 2,
2224};
2225
2226/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002227 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002228 * files in the subvolume, it removes orphan item and frees block_rsv
2229 * structure.
2230 */
2231void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2232 struct btrfs_root *root)
2233{
Josef Bacik90290e12011-12-02 15:44:12 -05002234 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002235 int ret;
2236
Josef Bacik8a35d952012-05-23 14:26:42 -04002237 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002238 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2239 return;
2240
Josef Bacik90290e12011-12-02 15:44:12 -05002241 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002242 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002243 spin_unlock(&root->orphan_lock);
2244 return;
2245 }
2246
2247 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2248 spin_unlock(&root->orphan_lock);
2249 return;
2250 }
2251
2252 block_rsv = root->orphan_block_rsv;
2253 root->orphan_block_rsv = NULL;
2254 spin_unlock(&root->orphan_lock);
2255
Yan, Zhengd68fc572010-05-16 10:49:58 -04002256 if (root->orphan_item_inserted &&
2257 btrfs_root_refs(&root->root_item) > 0) {
2258 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2259 root->root_key.objectid);
2260 BUG_ON(ret);
2261 root->orphan_item_inserted = 0;
2262 }
2263
Josef Bacik90290e12011-12-02 15:44:12 -05002264 if (block_rsv) {
2265 WARN_ON(block_rsv->size > 0);
2266 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002267 }
2268}
2269
2270/*
Josef Bacik7b128762008-07-24 12:17:14 -04002271 * This creates an orphan entry for the given inode in case something goes
2272 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002273 *
2274 * NOTE: caller of this function should reserve 5 units of metadata for
2275 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002276 */
2277int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2278{
2279 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002280 struct btrfs_block_rsv *block_rsv = NULL;
2281 int reserve = 0;
2282 int insert = 0;
2283 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002284
Yan, Zhengd68fc572010-05-16 10:49:58 -04002285 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002286 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002287 if (!block_rsv)
2288 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002289 }
2290
Yan, Zhengd68fc572010-05-16 10:49:58 -04002291 spin_lock(&root->orphan_lock);
2292 if (!root->orphan_block_rsv) {
2293 root->orphan_block_rsv = block_rsv;
2294 } else if (block_rsv) {
2295 btrfs_free_block_rsv(root, block_rsv);
2296 block_rsv = NULL;
2297 }
Josef Bacik7b128762008-07-24 12:17:14 -04002298
Josef Bacik8a35d952012-05-23 14:26:42 -04002299 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2300 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002301#if 0
2302 /*
2303 * For proper ENOSPC handling, we should do orphan
2304 * cleanup when mounting. But this introduces backward
2305 * compatibility issue.
2306 */
2307 if (!xchg(&root->orphan_item_inserted, 1))
2308 insert = 2;
2309 else
2310 insert = 1;
2311#endif
2312 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002313 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002314 }
Josef Bacik7b128762008-07-24 12:17:14 -04002315
Josef Bacik72ac3c02012-05-23 14:13:11 -04002316 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2317 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002318 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002319 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002320
Yan, Zhengd68fc572010-05-16 10:49:58 -04002321 /* grab metadata reservation from transaction handle */
2322 if (reserve) {
2323 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002324 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002325 }
2326
2327 /* insert an orphan item to track this unlinked/truncated file */
2328 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002329 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002330 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002331 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2332 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002333 btrfs_abort_transaction(trans, root, ret);
2334 return ret;
2335 }
2336 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002337 }
2338
2339 /* insert an orphan item to track subvolume contains orphan files */
2340 if (insert >= 2) {
2341 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2342 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002343 if (ret && ret != -EEXIST) {
2344 btrfs_abort_transaction(trans, root, ret);
2345 return ret;
2346 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002347 }
2348 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002349}
2350
2351/*
2352 * We have done the truncate/delete so we can go ahead and remove the orphan
2353 * item for this particular inode.
2354 */
2355int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2356{
2357 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002358 int delete_item = 0;
2359 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002360 int ret = 0;
2361
Yan, Zhengd68fc572010-05-16 10:49:58 -04002362 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002363 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2364 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002365 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002366
Josef Bacik72ac3c02012-05-23 14:13:11 -04002367 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2368 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002369 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002370 spin_unlock(&root->orphan_lock);
2371
2372 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002373 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002374 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002375 }
2376
Josef Bacik8a35d952012-05-23 14:26:42 -04002377 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002378 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002379 atomic_dec(&root->orphan_inodes);
2380 }
Josef Bacik7b128762008-07-24 12:17:14 -04002381
Yan, Zhengd68fc572010-05-16 10:49:58 -04002382 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002383}
2384
2385/*
2386 * this cleans up any orphans that may be left on the list from the last use
2387 * of this root.
2388 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002389int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002390{
2391 struct btrfs_path *path;
2392 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002393 struct btrfs_key key, found_key;
2394 struct btrfs_trans_handle *trans;
2395 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002396 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002397 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2398
Yan, Zhengd68fc572010-05-16 10:49:58 -04002399 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002400 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002401
2402 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002403 if (!path) {
2404 ret = -ENOMEM;
2405 goto out;
2406 }
Josef Bacik7b128762008-07-24 12:17:14 -04002407 path->reada = -1;
2408
2409 key.objectid = BTRFS_ORPHAN_OBJECTID;
2410 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2411 key.offset = (u64)-1;
2412
Josef Bacik7b128762008-07-24 12:17:14 -04002413 while (1) {
2414 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002415 if (ret < 0)
2416 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002417
2418 /*
2419 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002420 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002421 * find the key and see if we have stuff that matches
2422 */
2423 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002424 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002425 if (path->slots[0] == 0)
2426 break;
2427 path->slots[0]--;
2428 }
2429
2430 /* pull out the item */
2431 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002432 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2433
2434 /* make sure the item matches what we want */
2435 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2436 break;
2437 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2438 break;
2439
2440 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002441 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002442
2443 /*
2444 * this is where we are basically btrfs_lookup, without the
2445 * crossing root thing. we store the inode number in the
2446 * offset of the orphan item.
2447 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002448
2449 if (found_key.offset == last_objectid) {
2450 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2451 "stopping orphan cleanup\n");
2452 ret = -EINVAL;
2453 goto out;
2454 }
2455
2456 last_objectid = found_key.offset;
2457
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002458 found_key.objectid = found_key.offset;
2459 found_key.type = BTRFS_INODE_ITEM_KEY;
2460 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002461 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002462 ret = PTR_RET(inode);
2463 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002464 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002465
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002466 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2467 struct btrfs_root *dead_root;
2468 struct btrfs_fs_info *fs_info = root->fs_info;
2469 int is_dead_root = 0;
2470
2471 /*
2472 * this is an orphan in the tree root. Currently these
2473 * could come from 2 sources:
2474 * a) a snapshot deletion in progress
2475 * b) a free space cache inode
2476 * We need to distinguish those two, as the snapshot
2477 * orphan must not get deleted.
2478 * find_dead_roots already ran before us, so if this
2479 * is a snapshot deletion, we should find the root
2480 * in the dead_roots list
2481 */
2482 spin_lock(&fs_info->trans_lock);
2483 list_for_each_entry(dead_root, &fs_info->dead_roots,
2484 root_list) {
2485 if (dead_root->root_key.objectid ==
2486 found_key.objectid) {
2487 is_dead_root = 1;
2488 break;
2489 }
2490 }
2491 spin_unlock(&fs_info->trans_lock);
2492 if (is_dead_root) {
2493 /* prevent this orphan from being found again */
2494 key.offset = found_key.objectid - 1;
2495 continue;
2496 }
2497 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002498 /*
2499 * Inode is already gone but the orphan item is still there,
2500 * kill the orphan item.
2501 */
2502 if (ret == -ESTALE) {
2503 trans = btrfs_start_transaction(root, 1);
2504 if (IS_ERR(trans)) {
2505 ret = PTR_ERR(trans);
2506 goto out;
2507 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002508 printk(KERN_ERR "auto deleting %Lu\n",
2509 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002510 ret = btrfs_del_orphan_item(trans, root,
2511 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002512 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002513 btrfs_end_transaction(trans, root);
2514 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002515 }
Josef Bacik7b128762008-07-24 12:17:14 -04002516
Josef Bacik7b128762008-07-24 12:17:14 -04002517 /*
2518 * add this inode to the orphan list so btrfs_orphan_del does
2519 * the proper thing when we hit it
2520 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002521 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2522 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05002523 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04002524
Josef Bacik7b128762008-07-24 12:17:14 -04002525 /* if we have links, this was a truncate, lets do that */
2526 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002527 if (!S_ISREG(inode->i_mode)) {
2528 WARN_ON(1);
2529 iput(inode);
2530 continue;
2531 }
Josef Bacik7b128762008-07-24 12:17:14 -04002532 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002533
2534 /* 1 for the orphan item deletion. */
2535 trans = btrfs_start_transaction(root, 1);
2536 if (IS_ERR(trans)) {
2537 ret = PTR_ERR(trans);
2538 goto out;
2539 }
2540 ret = btrfs_orphan_add(trans, inode);
2541 btrfs_end_transaction(trans, root);
2542 if (ret)
2543 goto out;
2544
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002545 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002546 } else {
2547 nr_unlink++;
2548 }
2549
2550 /* this will do delete_inode and everything for us */
2551 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002552 if (ret)
2553 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002554 }
Miao Xie3254c872011-11-10 20:45:05 -05002555 /* release the path since we're done with it */
2556 btrfs_release_path(path);
2557
Yan, Zhengd68fc572010-05-16 10:49:58 -04002558 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2559
2560 if (root->orphan_block_rsv)
2561 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2562 (u64)-1);
2563
2564 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002565 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002566 if (!IS_ERR(trans))
2567 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002568 }
Josef Bacik7b128762008-07-24 12:17:14 -04002569
2570 if (nr_unlink)
2571 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2572 if (nr_truncate)
2573 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002574
2575out:
2576 if (ret)
2577 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2578 btrfs_free_path(path);
2579 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002580}
2581
Chris Masond352ac62008-09-29 15:18:18 -04002582/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002583 * very simple check to peek ahead in the leaf looking for xattrs. If we
2584 * don't find any xattrs, we know there can't be any acls.
2585 *
2586 * slot is the slot the inode is in, objectid is the objectid of the inode
2587 */
2588static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2589 int slot, u64 objectid)
2590{
2591 u32 nritems = btrfs_header_nritems(leaf);
2592 struct btrfs_key found_key;
2593 int scanned = 0;
2594
2595 slot++;
2596 while (slot < nritems) {
2597 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2598
2599 /* we found a different objectid, there must not be acls */
2600 if (found_key.objectid != objectid)
2601 return 0;
2602
2603 /* we found an xattr, assume we've got an acl */
2604 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2605 return 1;
2606
2607 /*
2608 * we found a key greater than an xattr key, there can't
2609 * be any acls later on
2610 */
2611 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2612 return 0;
2613
2614 slot++;
2615 scanned++;
2616
2617 /*
2618 * it goes inode, inode backrefs, xattrs, extents,
2619 * so if there are a ton of hard links to an inode there can
2620 * be a lot of backrefs. Don't waste time searching too hard,
2621 * this is just an optimization
2622 */
2623 if (scanned >= 8)
2624 break;
2625 }
2626 /* we hit the end of the leaf before we found an xattr or
2627 * something larger than an xattr. We have to assume the inode
2628 * has acls
2629 */
2630 return 1;
2631}
2632
2633/*
Chris Masond352ac62008-09-29 15:18:18 -04002634 * read an inode from the btree into the in-memory inode
2635 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002636static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002637{
2638 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002639 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002640 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002641 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002642 struct btrfs_root *root = BTRFS_I(inode)->root;
2643 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002644 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002645 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002646 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002647 bool filled = false;
2648
2649 ret = btrfs_fill_inode(inode, &rdev);
2650 if (!ret)
2651 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002652
2653 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002654 if (!path)
2655 goto make_bad;
2656
Josef Bacikd90c7322011-05-17 09:50:54 -04002657 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002658 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002659
Chris Mason39279cc2007-06-12 06:35:45 -04002660 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002661 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002662 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002663
Chris Mason5f39d392007-10-15 16:14:19 -04002664 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002665
2666 if (filled)
2667 goto cache_acl;
2668
Chris Mason5f39d392007-10-15 16:14:19 -04002669 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2670 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002672 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002673 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2674 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002675 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002676
2677 tspec = btrfs_inode_atime(inode_item);
2678 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2679 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2680
2681 tspec = btrfs_inode_mtime(inode_item);
2682 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2683 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2684
2685 tspec = btrfs_inode_ctime(inode_item);
2686 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2687 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2688
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002689 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002690 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002691 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2692
2693 /*
2694 * If we were modified in the current generation and evicted from memory
2695 * and then re-read we need to do a full sync since we don't have any
2696 * idea about which extents were modified before we were evicted from
2697 * cache.
2698 */
2699 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2700 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2701 &BTRFS_I(inode)->runtime_flags);
2702
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002703 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002704 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002705 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002706 rdev = btrfs_inode_rdev(leaf, inode_item);
2707
Josef Bacikaec74772008-07-24 12:12:38 -04002708 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002709 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002710cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002711 /*
2712 * try to precache a NULL acl entry for files that don't have
2713 * any xattrs or acls
2714 */
Li Zefan33345d012011-04-20 10:31:50 +08002715 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2716 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002717 if (!maybe_acls)
2718 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002719
Chris Mason39279cc2007-06-12 06:35:45 -04002720 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002721
Chris Mason39279cc2007-06-12 06:35:45 -04002722 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002723 case S_IFREG:
2724 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002725 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002726 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002727 inode->i_fop = &btrfs_file_operations;
2728 inode->i_op = &btrfs_file_inode_operations;
2729 break;
2730 case S_IFDIR:
2731 inode->i_fop = &btrfs_dir_file_operations;
2732 if (root == root->fs_info->tree_root)
2733 inode->i_op = &btrfs_dir_ro_inode_operations;
2734 else
2735 inode->i_op = &btrfs_dir_inode_operations;
2736 break;
2737 case S_IFLNK:
2738 inode->i_op = &btrfs_symlink_inode_operations;
2739 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002740 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002741 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002742 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002743 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002744 init_special_inode(inode, inode->i_mode, rdev);
2745 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002746 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002747
2748 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002749 return;
2750
2751make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002752 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002753 make_bad_inode(inode);
2754}
2755
Chris Masond352ac62008-09-29 15:18:18 -04002756/*
2757 * given a leaf and an inode, copy the inode fields into the leaf
2758 */
Chris Masone02119d2008-09-05 16:13:11 -04002759static void fill_inode_item(struct btrfs_trans_handle *trans,
2760 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002761 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002762 struct inode *inode)
2763{
Liu Bo51fab692012-12-27 09:01:21 +00002764 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002765
Liu Bo51fab692012-12-27 09:01:21 +00002766 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04002767
Liu Bo51fab692012-12-27 09:01:21 +00002768 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2769 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2770 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
2771 &token);
2772 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2773 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002774
Liu Bo51fab692012-12-27 09:01:21 +00002775 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2776 inode->i_atime.tv_sec, &token);
2777 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2778 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002779
Liu Bo51fab692012-12-27 09:01:21 +00002780 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2781 inode->i_mtime.tv_sec, &token);
2782 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
2783 inode->i_mtime.tv_nsec, &token);
2784
2785 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
2786 inode->i_ctime.tv_sec, &token);
2787 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
2788 inode->i_ctime.tv_nsec, &token);
2789
2790 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
2791 &token);
2792 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
2793 &token);
2794 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
2795 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
2796 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
2797 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
2798 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04002799}
2800
Chris Masond352ac62008-09-29 15:18:18 -04002801/*
2802 * copy everything in the in-memory inode into the btree.
2803 */
Chris Mason21151332011-11-10 20:39:08 -05002804static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002805 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002806{
2807 struct btrfs_inode_item *inode_item;
2808 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002809 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002810 int ret;
2811
2812 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002813 if (!path)
2814 return -ENOMEM;
2815
Chris Masonb9473432009-03-13 11:00:37 -04002816 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002817 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2818 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002819 if (ret) {
2820 if (ret > 0)
2821 ret = -ENOENT;
2822 goto failed;
2823 }
2824
Chris Masonb4ce94d2009-02-04 09:25:08 -05002825 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002826 leaf = path->nodes[0];
2827 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002828 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002829
Chris Masone02119d2008-09-05 16:13:11 -04002830 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002831 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002832 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002833 ret = 0;
2834failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002835 btrfs_free_path(path);
2836 return ret;
2837}
2838
Chris Masond352ac62008-09-29 15:18:18 -04002839/*
Chris Mason21151332011-11-10 20:39:08 -05002840 * copy everything in the in-memory inode into the btree.
2841 */
2842noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2843 struct btrfs_root *root, struct inode *inode)
2844{
2845 int ret;
2846
2847 /*
2848 * If the inode is a free space inode, we can deadlock during commit
2849 * if we put it into the delayed code.
2850 *
2851 * The data relocation inode should also be directly updated
2852 * without delay
2853 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002854 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002855 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002856 btrfs_update_root_times(trans, root);
2857
Chris Mason21151332011-11-10 20:39:08 -05002858 ret = btrfs_delayed_update_inode(trans, root, inode);
2859 if (!ret)
2860 btrfs_set_inode_last_trans(trans, inode);
2861 return ret;
2862 }
2863
2864 return btrfs_update_inode_item(trans, root, inode);
2865}
2866
Josef Bacikbe6aef62012-10-22 15:43:12 -04002867noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2868 struct btrfs_root *root,
2869 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002870{
2871 int ret;
2872
2873 ret = btrfs_update_inode(trans, root, inode);
2874 if (ret == -ENOSPC)
2875 return btrfs_update_inode_item(trans, root, inode);
2876 return ret;
2877}
2878
2879/*
Chris Masond352ac62008-09-29 15:18:18 -04002880 * unlink helper that gets used here in inode.c and in the tree logging
2881 * recovery code. It remove a link in a directory with a given name, and
2882 * also drops the back refs in the inode to the directory
2883 */
Al Viro92986792011-03-04 17:14:37 +00002884static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2885 struct btrfs_root *root,
2886 struct inode *dir, struct inode *inode,
2887 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002888{
2889 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002890 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002891 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002892 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002893 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002894 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002895 u64 ino = btrfs_ino(inode);
2896 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002897
2898 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002899 if (!path) {
2900 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002901 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002902 }
2903
Chris Masonb9473432009-03-13 11:00:37 -04002904 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002905 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002906 name, name_len, -1);
2907 if (IS_ERR(di)) {
2908 ret = PTR_ERR(di);
2909 goto err;
2910 }
2911 if (!di) {
2912 ret = -ENOENT;
2913 goto err;
2914 }
Chris Mason5f39d392007-10-15 16:14:19 -04002915 leaf = path->nodes[0];
2916 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002917 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002918 if (ret)
2919 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002920 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002921
Li Zefan33345d012011-04-20 10:31:50 +08002922 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2923 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002924 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002925 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002926 "inode %llu parent %llu\n", name_len, name,
2927 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002928 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002929 goto err;
2930 }
2931
Miao Xie16cdcec2011-04-22 18:12:22 +08002932 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002933 if (ret) {
2934 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002935 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002936 }
Chris Mason39279cc2007-06-12 06:35:45 -04002937
Chris Masone02119d2008-09-05 16:13:11 -04002938 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002939 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002940 if (ret != 0 && ret != -ENOENT) {
2941 btrfs_abort_transaction(trans, root, ret);
2942 goto err;
2943 }
Chris Masone02119d2008-09-05 16:13:11 -04002944
2945 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2946 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002947 if (ret == -ENOENT)
2948 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002949err:
2950 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002951 if (ret)
2952 goto out;
2953
2954 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002955 inode_inc_iversion(inode);
2956 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002957 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002958 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002959out:
Chris Mason39279cc2007-06-12 06:35:45 -04002960 return ret;
2961}
2962
Al Viro92986792011-03-04 17:14:37 +00002963int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2964 struct btrfs_root *root,
2965 struct inode *dir, struct inode *inode,
2966 const char *name, int name_len)
2967{
2968 int ret;
2969 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2970 if (!ret) {
2971 btrfs_drop_nlink(inode);
2972 ret = btrfs_update_inode(trans, root, inode);
2973 }
2974 return ret;
2975}
2976
2977
Yan, Zhenga22285a2010-05-16 10:48:46 -04002978/* helper to check if there is any shared block in the path */
2979static int check_path_shared(struct btrfs_root *root,
2980 struct btrfs_path *path)
2981{
2982 struct extent_buffer *eb;
2983 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002984 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002985
2986 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002987 int ret;
2988
Yan, Zhenga22285a2010-05-16 10:48:46 -04002989 if (!path->nodes[level])
2990 break;
2991 eb = path->nodes[level];
2992 if (!btrfs_block_can_be_shared(root, eb))
2993 continue;
2994 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2995 &refs, NULL);
2996 if (refs > 1)
2997 return 1;
2998 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002999 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003000}
3001
3002/*
3003 * helper to start transaction for unlink and rmdir.
3004 *
3005 * unlink and rmdir are special in btrfs, they do not always free space.
3006 * so in enospc case, we should make sure they will free space before
3007 * allowing them to use the global metadata reservation.
3008 */
3009static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3010 struct dentry *dentry)
3011{
3012 struct btrfs_trans_handle *trans;
3013 struct btrfs_root *root = BTRFS_I(dir)->root;
3014 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003015 struct btrfs_dir_item *di;
3016 struct inode *inode = dentry->d_inode;
3017 u64 index;
3018 int check_link = 1;
3019 int err = -ENOSPC;
3020 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003021 u64 ino = btrfs_ino(inode);
3022 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003023
Josef Bacike70bea52011-10-11 14:18:24 -04003024 /*
3025 * 1 for the possible orphan item
3026 * 1 for the dir item
3027 * 1 for the dir index
3028 * 1 for the inode ref
3029 * 1 for the inode ref in the tree log
3030 * 2 for the dir entries in the log
3031 * 1 for the inode
3032 */
3033 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003034 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3035 return trans;
3036
Li Zefan33345d012011-04-20 10:31:50 +08003037 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003038 return ERR_PTR(-ENOSPC);
3039
3040 /* check if there is someone else holds reference */
3041 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3042 return ERR_PTR(-ENOSPC);
3043
3044 if (atomic_read(&inode->i_count) > 2)
3045 return ERR_PTR(-ENOSPC);
3046
3047 if (xchg(&root->fs_info->enospc_unlink, 1))
3048 return ERR_PTR(-ENOSPC);
3049
3050 path = btrfs_alloc_path();
3051 if (!path) {
3052 root->fs_info->enospc_unlink = 0;
3053 return ERR_PTR(-ENOMEM);
3054 }
3055
Josef Bacik3880a1b2011-10-14 14:46:51 -04003056 /* 1 for the orphan item */
3057 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003058 if (IS_ERR(trans)) {
3059 btrfs_free_path(path);
3060 root->fs_info->enospc_unlink = 0;
3061 return trans;
3062 }
3063
3064 path->skip_locking = 1;
3065 path->search_commit_root = 1;
3066
3067 ret = btrfs_lookup_inode(trans, root, path,
3068 &BTRFS_I(dir)->location, 0);
3069 if (ret < 0) {
3070 err = ret;
3071 goto out;
3072 }
3073 if (ret == 0) {
3074 if (check_path_shared(root, path))
3075 goto out;
3076 } else {
3077 check_link = 0;
3078 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003079 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003080
3081 ret = btrfs_lookup_inode(trans, root, path,
3082 &BTRFS_I(inode)->location, 0);
3083 if (ret < 0) {
3084 err = ret;
3085 goto out;
3086 }
3087 if (ret == 0) {
3088 if (check_path_shared(root, path))
3089 goto out;
3090 } else {
3091 check_link = 0;
3092 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003093 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003094
3095 if (ret == 0 && S_ISREG(inode->i_mode)) {
3096 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003097 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003098 if (ret < 0) {
3099 err = ret;
3100 goto out;
3101 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003102 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003103 if (check_path_shared(root, path))
3104 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003105 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003106 }
3107
3108 if (!check_link) {
3109 err = 0;
3110 goto out;
3111 }
3112
Li Zefan33345d012011-04-20 10:31:50 +08003113 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003114 dentry->d_name.name, dentry->d_name.len, 0);
3115 if (IS_ERR(di)) {
3116 err = PTR_ERR(di);
3117 goto out;
3118 }
3119 if (di) {
3120 if (check_path_shared(root, path))
3121 goto out;
3122 } else {
3123 err = 0;
3124 goto out;
3125 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003126 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003127
Mark Fashehf1863732012-08-08 11:32:27 -07003128 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3129 dentry->d_name.len, ino, dir_ino, 0,
3130 &index);
3131 if (ret) {
3132 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003133 goto out;
3134 }
Mark Fashehf1863732012-08-08 11:32:27 -07003135
Yan, Zhenga22285a2010-05-16 10:48:46 -04003136 if (check_path_shared(root, path))
3137 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003138
David Sterbab3b4aa72011-04-21 01:20:15 +02003139 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003140
Miao Xie16cdcec2011-04-22 18:12:22 +08003141 /*
3142 * This is a commit root search, if we can lookup inode item and other
3143 * relative items in the commit root, it means the transaction of
3144 * dir/file creation has been committed, and the dir index item that we
3145 * delay to insert has also been inserted into the commit root. So
3146 * we needn't worry about the delayed insertion of the dir index item
3147 * here.
3148 */
Li Zefan33345d012011-04-20 10:31:50 +08003149 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003150 dentry->d_name.name, dentry->d_name.len, 0);
3151 if (IS_ERR(di)) {
3152 err = PTR_ERR(di);
3153 goto out;
3154 }
3155 BUG_ON(ret == -ENOENT);
3156 if (check_path_shared(root, path))
3157 goto out;
3158
3159 err = 0;
3160out:
3161 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003162 /* Migrate the orphan reservation over */
3163 if (!err)
3164 err = btrfs_block_rsv_migrate(trans->block_rsv,
3165 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003166 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003167
Yan, Zhenga22285a2010-05-16 10:48:46 -04003168 if (err) {
3169 btrfs_end_transaction(trans, root);
3170 root->fs_info->enospc_unlink = 0;
3171 return ERR_PTR(err);
3172 }
3173
3174 trans->block_rsv = &root->fs_info->global_block_rsv;
3175 return trans;
3176}
3177
3178static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3179 struct btrfs_root *root)
3180{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003181 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003182 btrfs_block_rsv_release(root, trans->block_rsv,
3183 trans->bytes_reserved);
3184 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003185 BUG_ON(!root->fs_info->enospc_unlink);
3186 root->fs_info->enospc_unlink = 0;
3187 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003188 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003189}
3190
Chris Mason39279cc2007-06-12 06:35:45 -04003191static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3192{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003193 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003194 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003195 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003196 int ret;
3197
Yan, Zhenga22285a2010-05-16 10:48:46 -04003198 trans = __unlink_start_trans(dir, dentry);
3199 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003200 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003201
Chris Mason12fcfd22009-03-24 10:24:20 -04003202 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3203
Chris Masone02119d2008-09-05 16:13:11 -04003204 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3205 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003206 if (ret)
3207 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003208
Yan, Zhenga22285a2010-05-16 10:48:46 -04003209 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003210 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003211 if (ret)
3212 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003213 }
Josef Bacik7b128762008-07-24 12:17:14 -04003214
Tsutomu Itohb5324022011-07-19 07:27:20 +00003215out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003216 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003217 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003218 return ret;
3219}
3220
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003221int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3222 struct btrfs_root *root,
3223 struct inode *dir, u64 objectid,
3224 const char *name, int name_len)
3225{
3226 struct btrfs_path *path;
3227 struct extent_buffer *leaf;
3228 struct btrfs_dir_item *di;
3229 struct btrfs_key key;
3230 u64 index;
3231 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003232 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003233
3234 path = btrfs_alloc_path();
3235 if (!path)
3236 return -ENOMEM;
3237
Li Zefan33345d012011-04-20 10:31:50 +08003238 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003239 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003240 if (IS_ERR_OR_NULL(di)) {
3241 if (!di)
3242 ret = -ENOENT;
3243 else
3244 ret = PTR_ERR(di);
3245 goto out;
3246 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003247
3248 leaf = path->nodes[0];
3249 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3250 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3251 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003252 if (ret) {
3253 btrfs_abort_transaction(trans, root, ret);
3254 goto out;
3255 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003256 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003257
3258 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3259 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003260 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003261 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003262 if (ret != -ENOENT) {
3263 btrfs_abort_transaction(trans, root, ret);
3264 goto out;
3265 }
Li Zefan33345d012011-04-20 10:31:50 +08003266 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003267 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003268 if (IS_ERR_OR_NULL(di)) {
3269 if (!di)
3270 ret = -ENOENT;
3271 else
3272 ret = PTR_ERR(di);
3273 btrfs_abort_transaction(trans, root, ret);
3274 goto out;
3275 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003276
3277 leaf = path->nodes[0];
3278 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003279 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003280 index = key.offset;
3281 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003282 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003283
Miao Xie16cdcec2011-04-22 18:12:22 +08003284 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003285 if (ret) {
3286 btrfs_abort_transaction(trans, root, ret);
3287 goto out;
3288 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003289
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003290 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003291 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003292 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003293 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003294 if (ret)
3295 btrfs_abort_transaction(trans, root, ret);
3296out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003297 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003298 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003299}
3300
Chris Mason39279cc2007-06-12 06:35:45 -04003301static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3302{
3303 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003304 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003305 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003306 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003307
David Sterbab3ae2442012-09-13 16:04:34 -06003308 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003309 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003310 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3311 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003312
Yan, Zhenga22285a2010-05-16 10:48:46 -04003313 trans = __unlink_start_trans(dir, dentry);
3314 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003315 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003316
Li Zefan33345d012011-04-20 10:31:50 +08003317 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003318 err = btrfs_unlink_subvol(trans, root, dir,
3319 BTRFS_I(inode)->location.objectid,
3320 dentry->d_name.name,
3321 dentry->d_name.len);
3322 goto out;
3323 }
3324
Josef Bacik7b128762008-07-24 12:17:14 -04003325 err = btrfs_orphan_add(trans, inode);
3326 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003327 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003328
Chris Mason39279cc2007-06-12 06:35:45 -04003329 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003330 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3331 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003332 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003333 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003334out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003335 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003336 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003337
Chris Mason39279cc2007-06-12 06:35:45 -04003338 return err;
3339}
3340
Chris Mason323ac952008-10-01 19:05:46 -04003341/*
Chris Mason39279cc2007-06-12 06:35:45 -04003342 * this can truncate away extent items, csum items and directory items.
3343 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003344 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003345 *
3346 * csum items that cross the new i_size are truncated to the new size
3347 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003348 *
3349 * min_type is the minimum key type to truncate down to. If set to 0, this
3350 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003351 */
Yan, Zheng80825102009-11-12 09:35:36 +00003352int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3353 struct btrfs_root *root,
3354 struct inode *inode,
3355 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003356{
Chris Mason39279cc2007-06-12 06:35:45 -04003357 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003358 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003359 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003360 struct btrfs_key key;
3361 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003362 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003363 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003364 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003365 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003366 u64 mask = root->sectorsize - 1;
3367 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003368 int found_extent;
3369 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003370 int pending_del_nr = 0;
3371 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003372 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003373 int ret;
3374 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003375 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003376
3377 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003378
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003379 path = btrfs_alloc_path();
3380 if (!path)
3381 return -ENOMEM;
3382 path->reada = -1;
3383
Josef Bacik5dc562c2012-08-17 13:14:17 -04003384 /*
3385 * We want to drop from the next block forward in case this new size is
3386 * not block aligned since we will be keeping the last block of the
3387 * extent just the way it is.
3388 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003389 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003390 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003391
Miao Xie16cdcec2011-04-22 18:12:22 +08003392 /*
3393 * This function is also used to drop the items in the log tree before
3394 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3395 * it is used to drop the loged items. So we shouldn't kill the delayed
3396 * items.
3397 */
3398 if (min_type == 0 && root == BTRFS_I(inode)->root)
3399 btrfs_kill_delayed_inode_items(inode);
3400
Li Zefan33345d012011-04-20 10:31:50 +08003401 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003402 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003403 key.type = (u8)-1;
3404
Chris Mason85e21ba2008-01-29 15:11:36 -05003405search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003406 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003407 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003408 if (ret < 0) {
3409 err = ret;
3410 goto out;
3411 }
Chris Masond3977122009-01-05 21:25:51 -05003412
Chris Mason85e21ba2008-01-29 15:11:36 -05003413 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003414 /* there are no items in the tree for us to truncate, we're
3415 * done
3416 */
Yan, Zheng80825102009-11-12 09:35:36 +00003417 if (path->slots[0] == 0)
3418 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003419 path->slots[0]--;
3420 }
3421
Chris Masond3977122009-01-05 21:25:51 -05003422 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003423 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003424 leaf = path->nodes[0];
3425 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3426 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003427
Li Zefan33345d012011-04-20 10:31:50 +08003428 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003429 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003430
Chris Mason85e21ba2008-01-29 15:11:36 -05003431 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003432 break;
3433
Chris Mason5f39d392007-10-15 16:14:19 -04003434 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003435 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003436 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003437 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003438 extent_type = btrfs_file_extent_type(leaf, fi);
3439 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003440 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003441 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003442 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003443 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003444 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003445 }
Yan008630c2007-11-07 13:31:09 -05003446 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003447 }
Yan, Zheng80825102009-11-12 09:35:36 +00003448 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003449 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003450 } else {
3451 if (item_end < new_size)
3452 break;
3453 if (found_key.offset >= new_size)
3454 del_item = 1;
3455 else
3456 del_item = 0;
3457 }
Chris Mason39279cc2007-06-12 06:35:45 -04003458 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003459 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003460 if (found_type != BTRFS_EXTENT_DATA_KEY)
3461 goto delete;
3462
3463 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003464 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003465 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003466 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003467 u64 orig_num_bytes =
3468 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003469 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003470 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003471 extent_num_bytes = extent_num_bytes &
3472 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003473 btrfs_set_file_extent_num_bytes(leaf, fi,
3474 extent_num_bytes);
3475 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003476 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003477 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003478 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003479 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003480 } else {
Chris Masondb945352007-10-15 16:15:53 -04003481 extent_num_bytes =
3482 btrfs_file_extent_disk_num_bytes(leaf,
3483 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003484 extent_offset = found_key.offset -
3485 btrfs_file_extent_offset(leaf, fi);
3486
Chris Mason39279cc2007-06-12 06:35:45 -04003487 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003488 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003489 if (extent_start != 0) {
3490 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003491 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003492 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003493 }
3494 }
Chris Mason90692182008-02-08 13:49:28 -05003495 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003496 /*
3497 * we can't truncate inline items that have had
3498 * special encodings
3499 */
3500 if (!del_item &&
3501 btrfs_file_extent_compression(leaf, fi) == 0 &&
3502 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3503 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003504 u32 size = new_size - found_key.offset;
3505
3506 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003507 inode_sub_bytes(inode, item_end + 1 -
3508 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003509 }
3510 size =
3511 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003512 btrfs_truncate_item(trans, root, path,
3513 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003514 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003515 inode_sub_bytes(inode, item_end + 1 -
3516 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003517 }
Chris Mason39279cc2007-06-12 06:35:45 -04003518 }
Chris Mason179e29e2007-11-01 11:28:41 -04003519delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003520 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003521 if (!pending_del_nr) {
3522 /* no pending yet, add ourselves */
3523 pending_del_slot = path->slots[0];
3524 pending_del_nr = 1;
3525 } else if (pending_del_nr &&
3526 path->slots[0] + 1 == pending_del_slot) {
3527 /* hop on the pending chunk */
3528 pending_del_nr++;
3529 pending_del_slot = path->slots[0];
3530 } else {
Chris Masond3977122009-01-05 21:25:51 -05003531 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003532 }
Chris Mason39279cc2007-06-12 06:35:45 -04003533 } else {
3534 break;
3535 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003536 if (found_extent && (root->ref_cows ||
3537 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003538 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003539 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003540 extent_num_bytes, 0,
3541 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003542 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003543 BUG_ON(ret);
3544 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003545
Yan, Zheng80825102009-11-12 09:35:36 +00003546 if (found_type == BTRFS_INODE_ITEM_KEY)
3547 break;
3548
3549 if (path->slots[0] == 0 ||
3550 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003551 if (pending_del_nr) {
3552 ret = btrfs_del_items(trans, root, path,
3553 pending_del_slot,
3554 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003555 if (ret) {
3556 btrfs_abort_transaction(trans,
3557 root, ret);
3558 goto error;
3559 }
Yan, Zheng80825102009-11-12 09:35:36 +00003560 pending_del_nr = 0;
3561 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003562 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003563 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003564 } else {
3565 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003566 }
Chris Mason39279cc2007-06-12 06:35:45 -04003567 }
Yan, Zheng80825102009-11-12 09:35:36 +00003568out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003569 if (pending_del_nr) {
3570 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3571 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003572 if (ret)
3573 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003574 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003575error:
Chris Mason39279cc2007-06-12 06:35:45 -04003576 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003577 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003578}
3579
Chris Masona52d9a82007-08-27 16:49:44 -04003580/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003581 * btrfs_truncate_page - read, zero a chunk and write a page
3582 * @inode - inode that we're zeroing
3583 * @from - the offset to start zeroing
3584 * @len - the length to zero, 0 to zero the entire range respective to the
3585 * offset
3586 * @front - zero up to the offset instead of from the offset on
3587 *
3588 * This will find the page for the "from" offset and cow the page and zero the
3589 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003590 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003591int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3592 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003593{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003594 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003595 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003596 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3597 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003598 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003599 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003600 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003601 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3602 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3603 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003604 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003605 int ret = 0;
3606 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003607 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003608
Josef Bacik2aaa6652012-08-29 14:27:18 -04003609 if ((offset & (blocksize - 1)) == 0 &&
3610 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003611 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003612 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003613 if (ret)
3614 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003615
Chris Mason211c17f2008-05-15 09:13:45 -04003616again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003617 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003618 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003619 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003620 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003621 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003622 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003623
3624 page_start = page_offset(page);
3625 page_end = page_start + PAGE_CACHE_SIZE - 1;
3626
Chris Masona52d9a82007-08-27 16:49:44 -04003627 if (!PageUptodate(page)) {
3628 ret = btrfs_readpage(NULL, page);
3629 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003630 if (page->mapping != mapping) {
3631 unlock_page(page);
3632 page_cache_release(page);
3633 goto again;
3634 }
Chris Masona52d9a82007-08-27 16:49:44 -04003635 if (!PageUptodate(page)) {
3636 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003637 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003638 }
3639 }
Chris Mason211c17f2008-05-15 09:13:45 -04003640 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003641
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003642 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003643 set_page_extent_mapped(page);
3644
3645 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3646 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003647 unlock_extent_cached(io_tree, page_start, page_end,
3648 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003649 unlock_page(page);
3650 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003651 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003652 btrfs_put_ordered_extent(ordered);
3653 goto again;
3654 }
3655
Josef Bacik2ac55d42010-02-03 19:33:23 +00003656 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003657 EXTENT_DIRTY | EXTENT_DELALLOC |
3658 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003659 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003660
Josef Bacik2ac55d42010-02-03 19:33:23 +00003661 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3662 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003663 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003664 unlock_extent_cached(io_tree, page_start, page_end,
3665 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003666 goto out_unlock;
3667 }
3668
Chris Masone6dcd2d2008-07-17 12:53:50 -04003669 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003670 if (!len)
3671 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003672 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003673 if (front)
3674 memset(kaddr, 0, offset);
3675 else
3676 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003677 flush_dcache_page(page);
3678 kunmap(page);
3679 }
Chris Mason247e7432008-07-17 12:53:51 -04003680 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003681 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003682 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3683 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003684
Chris Mason89642222008-07-24 09:41:53 -04003685out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003686 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003687 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003688 unlock_page(page);
3689 page_cache_release(page);
3690out:
3691 return ret;
3692}
3693
Josef Bacik695a0d02011-03-04 15:46:53 -05003694/*
3695 * This function puts in dummy file extents for the area we're creating a hole
3696 * for. So if we are truncating this file to a larger size we need to insert
3697 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3698 * the range between oldsize and size
3699 */
Josef Bacika41ad392011-01-31 15:30:16 -05003700int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003701{
3702 struct btrfs_trans_handle *trans;
3703 struct btrfs_root *root = BTRFS_I(inode)->root;
3704 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003705 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003706 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003707 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003708 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003709 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003710 u64 block_end = (size + mask) & ~mask;
3711 u64 last_byte;
3712 u64 cur_offset;
3713 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003714 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003715
3716 if (size <= hole_start)
3717 return 0;
3718
Yan Zheng9036c102008-10-30 14:19:41 -04003719 while (1) {
3720 struct btrfs_ordered_extent *ordered;
3721 btrfs_wait_ordered_range(inode, hole_start,
3722 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003723 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003724 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003725 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3726 if (!ordered)
3727 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003728 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3729 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003730 btrfs_put_ordered_extent(ordered);
3731 }
3732
Yan Zheng9036c102008-10-30 14:19:41 -04003733 cur_offset = hole_start;
3734 while (1) {
3735 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3736 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 if (IS_ERR(em)) {
3738 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003739 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003740 break;
3741 }
Yan Zheng9036c102008-10-30 14:19:41 -04003742 last_byte = min(extent_map_end(em), block_end);
3743 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003744 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003745 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003746 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003747
Miao Xie36423202011-12-14 20:12:02 -05003748 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003749 if (IS_ERR(trans)) {
3750 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003751 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 }
Yan, Zheng80825102009-11-12 09:35:36 +00003753
Josef Bacik5dc562c2012-08-17 13:14:17 -04003754 err = btrfs_drop_extents(trans, root, inode,
3755 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003756 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003757 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003758 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003759 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003760 break;
Miao Xie5b397372011-09-11 10:52:24 -04003761 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003762
Yan Zheng9036c102008-10-30 14:19:41 -04003763 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003764 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003765 0, hole_size, 0, hole_size,
3766 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003767 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003769 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003770 break;
Miao Xie5b397372011-09-11 10:52:24 -04003771 }
Yan, Zheng80825102009-11-12 09:35:36 +00003772
Josef Bacik5dc562c2012-08-17 13:14:17 -04003773 btrfs_drop_extent_cache(inode, cur_offset,
3774 cur_offset + hole_size - 1, 0);
3775 hole_em = alloc_extent_map();
3776 if (!hole_em) {
3777 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3778 &BTRFS_I(inode)->runtime_flags);
3779 goto next;
3780 }
3781 hole_em->start = cur_offset;
3782 hole_em->len = hole_size;
3783 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003784
Josef Bacik5dc562c2012-08-17 13:14:17 -04003785 hole_em->block_start = EXTENT_MAP_HOLE;
3786 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003787 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003788 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3789 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3790 hole_em->generation = trans->transid;
3791
3792 while (1) {
3793 write_lock(&em_tree->lock);
3794 err = add_extent_mapping(em_tree, hole_em);
3795 if (!err)
3796 list_move(&hole_em->list,
3797 &em_tree->modified_extents);
3798 write_unlock(&em_tree->lock);
3799 if (err != -EEXIST)
3800 break;
3801 btrfs_drop_extent_cache(inode, cur_offset,
3802 cur_offset +
3803 hole_size - 1, 0);
3804 }
3805 free_extent_map(hole_em);
3806next:
Miao Xie36423202011-12-14 20:12:02 -05003807 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003808 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003809 }
3810 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003811 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003812 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003813 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003814 break;
3815 }
3816
Yan, Zhenga22285a2010-05-16 10:48:46 -04003817 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003818 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3819 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003820 return err;
3821}
3822
Eric Sandeen3972f262013-01-12 02:57:22 +00003823static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00003824{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003825 struct btrfs_root *root = BTRFS_I(inode)->root;
3826 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003827 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00003828 loff_t newsize = attr->ia_size;
3829 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00003830 int ret;
3831
Josef Bacika41ad392011-01-31 15:30:16 -05003832 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003833 return 0;
3834
Eric Sandeen3972f262013-01-12 02:57:22 +00003835 /*
3836 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3837 * special case where we need to update the times despite not having
3838 * these flags set. For all other operations the VFS set these flags
3839 * explicitly if it wants a timestamp update.
3840 */
3841 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3842 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3843
Josef Bacika41ad392011-01-31 15:30:16 -05003844 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003845 truncate_pagecache(inode, oldsize, newsize);
3846 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003847 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003848 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003849
Miao Xief4a2f4c2011-12-14 20:12:01 -05003850 trans = btrfs_start_transaction(root, 1);
3851 if (IS_ERR(trans))
3852 return PTR_ERR(trans);
3853
3854 i_size_write(inode, newsize);
3855 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3856 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003857 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003858 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003859
Josef Bacika41ad392011-01-31 15:30:16 -05003860 /*
3861 * We're truncating a file that used to have good data down to
3862 * zero. Make sure it gets into the ordered flush list so that
3863 * any new writes get down to disk quickly.
3864 */
3865 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003866 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3867 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003868
Josef Bacikf3fe8202013-01-07 17:03:21 -05003869 /*
3870 * 1 for the orphan item we're going to add
3871 * 1 for the orphan item deletion.
3872 */
3873 trans = btrfs_start_transaction(root, 2);
3874 if (IS_ERR(trans))
3875 return PTR_ERR(trans);
3876
3877 /*
3878 * We need to do this in case we fail at _any_ point during the
3879 * actual truncate. Once we do the truncate_setsize we could
3880 * invalidate pages which forces any outstanding ordered io to
3881 * be instantly completed which will give us extents that need
3882 * to be truncated. If we fail to get an orphan inode down we
3883 * could have left over extents that were never meant to live,
3884 * so we need to garuntee from this point on that everything
3885 * will be consistent.
3886 */
3887 ret = btrfs_orphan_add(trans, inode);
3888 btrfs_end_transaction(trans, root);
3889 if (ret)
3890 return ret;
3891
Josef Bacika41ad392011-01-31 15:30:16 -05003892 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3893 truncate_setsize(inode, newsize);
3894 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003895 if (ret && inode->i_nlink)
3896 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003897 }
3898
Josef Bacika41ad392011-01-31 15:30:16 -05003899 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003900}
3901
Chris Mason39279cc2007-06-12 06:35:45 -04003902static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3903{
3904 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003905 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 int err;
3907
Li Zefanb83cc962010-12-20 16:04:08 +08003908 if (btrfs_root_readonly(root))
3909 return -EROFS;
3910
Chris Mason39279cc2007-06-12 06:35:45 -04003911 err = inode_change_ok(inode, attr);
3912 if (err)
3913 return err;
3914
Chris Mason5a3f23d2009-03-31 13:27:11 -04003915 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00003916 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00003917 if (err)
3918 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 }
Yan Zheng9036c102008-10-30 14:19:41 -04003920
Christoph Hellwig10257742010-06-04 11:30:02 +02003921 if (attr->ia_valid) {
3922 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003923 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003924 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003925
Josef Bacik22c44fe2011-11-30 10:45:38 -05003926 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003927 err = btrfs_acl_chmod(inode);
3928 }
3929
Chris Mason39279cc2007-06-12 06:35:45 -04003930 return err;
3931}
Chris Mason61295eb2008-01-14 16:24:38 -05003932
Al Virobd555972010-06-07 11:35:40 -04003933void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003934{
3935 struct btrfs_trans_handle *trans;
3936 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003937 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003938 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003939 int ret;
3940
liubo1abe9b82011-03-24 11:18:59 +00003941 trace_btrfs_inode_evict(inode);
3942
Chris Mason39279cc2007-06-12 06:35:45 -04003943 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003944 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003945 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003946 goto no_delete;
3947
Chris Mason39279cc2007-06-12 06:35:45 -04003948 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003949 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003950 goto no_delete;
3951 }
Al Virobd555972010-06-07 11:35:40 -04003952 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003953 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003954
Yan, Zhengc71bf092009-11-12 09:34:40 +00003955 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003956 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003957 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003958 goto no_delete;
3959 }
3960
Yan, Zheng76dda932009-09-21 16:00:26 -04003961 if (inode->i_nlink > 0) {
3962 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3963 goto no_delete;
3964 }
3965
Miao Xie0e8c36a2012-12-19 06:59:51 +00003966 ret = btrfs_commit_inode_delayed_inode(inode);
3967 if (ret) {
3968 btrfs_orphan_del(NULL, inode);
3969 goto no_delete;
3970 }
3971
Miao Xie66d8f3d2012-09-06 04:02:28 -06003972 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003973 if (!rsv) {
3974 btrfs_orphan_del(NULL, inode);
3975 goto no_delete;
3976 }
Josef Bacik4a338542011-08-29 11:01:31 -04003977 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003978 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003979 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003980
Chris Masondbe674a2008-07-17 12:54:05 -04003981 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003982
Josef Bacik4289a662011-08-05 13:22:24 -04003983 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003984 * This is a bit simpler than btrfs_truncate since we've already
3985 * reserved our space for our orphan item in the unlink, so we just
3986 * need to reserve some slack space in case we add bytes and update
3987 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003988 */
Yan, Zheng80825102009-11-12 09:35:36 +00003989 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003990 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3991 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003992
Josef Bacik726c35f2011-09-26 15:46:06 -04003993 /*
3994 * Try and steal from the global reserve since we will
3995 * likely not use this space anyway, we want to try as
3996 * hard as possible to get this to work.
3997 */
3998 if (ret)
3999 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4000
Yan, Zhengd68fc572010-05-16 10:49:58 -04004001 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004002 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004003 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004004 btrfs_orphan_del(NULL, inode);
4005 btrfs_free_block_rsv(root, rsv);
4006 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004007 }
4008
Miao Xie0e8c36a2012-12-19 06:59:51 +00004009 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004010 if (IS_ERR(trans)) {
4011 btrfs_orphan_del(NULL, inode);
4012 btrfs_free_block_rsv(root, rsv);
4013 goto no_delete;
4014 }
4015
4016 trans->block_rsv = rsv;
4017
Yan, Zhengd68fc572010-05-16 10:49:58 -04004018 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004019 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004020 break;
4021
Miao Xie8407aa42012-09-07 01:43:32 -06004022 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004023 btrfs_end_transaction(trans, root);
4024 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004025 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004026 }
4027
Josef Bacik4289a662011-08-05 13:22:24 -04004028 btrfs_free_block_rsv(root, rsv);
4029
Yan, Zheng80825102009-11-12 09:35:36 +00004030 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004031 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004032 ret = btrfs_orphan_del(trans, inode);
4033 BUG_ON(ret);
4034 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004035
Josef Bacik4289a662011-08-05 13:22:24 -04004036 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004037 if (!(root == root->fs_info->tree_root ||
4038 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004039 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004040
Chris Mason54aa1f42007-06-22 14:16:25 -04004041 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004042 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004043no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004044 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004045 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004046}
4047
4048/*
4049 * this returns the key found in the dir entry in the location pointer.
4050 * If no dir entries were found, location->objectid is 0.
4051 */
4052static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4053 struct btrfs_key *location)
4054{
4055 const char *name = dentry->d_name.name;
4056 int namelen = dentry->d_name.len;
4057 struct btrfs_dir_item *di;
4058 struct btrfs_path *path;
4059 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004060 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004061
4062 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004063 if (!path)
4064 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004065
Li Zefan33345d012011-04-20 10:31:50 +08004066 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004067 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004068 if (IS_ERR(di))
4069 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004070
David Sterbac7040052011-04-19 18:00:01 +02004071 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004072 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004073
Chris Mason5f39d392007-10-15 16:14:19 -04004074 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004075out:
Chris Mason39279cc2007-06-12 06:35:45 -04004076 btrfs_free_path(path);
4077 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004078out_err:
4079 location->objectid = 0;
4080 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004081}
4082
4083/*
4084 * when we hit a tree root in a directory, the btrfs part of the inode
4085 * needs to be changed to reflect the root directory of the tree root. This
4086 * is kind of like crossing a mount point.
4087 */
4088static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004089 struct inode *dir,
4090 struct dentry *dentry,
4091 struct btrfs_key *location,
4092 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004093{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094 struct btrfs_path *path;
4095 struct btrfs_root *new_root;
4096 struct btrfs_root_ref *ref;
4097 struct extent_buffer *leaf;
4098 int ret;
4099 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004100
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101 path = btrfs_alloc_path();
4102 if (!path) {
4103 err = -ENOMEM;
4104 goto out;
4105 }
Chris Mason39279cc2007-06-12 06:35:45 -04004106
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004107 err = -ENOENT;
4108 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4109 BTRFS_I(dir)->root->root_key.objectid,
4110 location->objectid);
4111 if (ret) {
4112 if (ret < 0)
4113 err = ret;
4114 goto out;
4115 }
Chris Mason39279cc2007-06-12 06:35:45 -04004116
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004117 leaf = path->nodes[0];
4118 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004119 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004120 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4121 goto out;
4122
4123 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4124 (unsigned long)(ref + 1),
4125 dentry->d_name.len);
4126 if (ret)
4127 goto out;
4128
David Sterbab3b4aa72011-04-21 01:20:15 +02004129 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004130
4131 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4132 if (IS_ERR(new_root)) {
4133 err = PTR_ERR(new_root);
4134 goto out;
4135 }
4136
4137 if (btrfs_root_refs(&new_root->root_item) == 0) {
4138 err = -ENOENT;
4139 goto out;
4140 }
4141
4142 *sub_root = new_root;
4143 location->objectid = btrfs_root_dirid(&new_root->root_item);
4144 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004145 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004146 err = 0;
4147out:
4148 btrfs_free_path(path);
4149 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004150}
4151
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004152static void inode_tree_add(struct inode *inode)
4153{
4154 struct btrfs_root *root = BTRFS_I(inode)->root;
4155 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004156 struct rb_node **p;
4157 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004158 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004159again:
4160 p = &root->inode_tree.rb_node;
4161 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162
Al Viro1d3382cb2010-10-23 15:19:20 -04004163 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004164 return;
4165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166 spin_lock(&root->inode_lock);
4167 while (*p) {
4168 parent = *p;
4169 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4170
Li Zefan33345d012011-04-20 10:31:50 +08004171 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004172 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004173 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004174 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004175 else {
4176 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004177 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004178 rb_erase(parent, &root->inode_tree);
4179 RB_CLEAR_NODE(parent);
4180 spin_unlock(&root->inode_lock);
4181 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004182 }
4183 }
4184 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4185 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4186 spin_unlock(&root->inode_lock);
4187}
4188
4189static void inode_tree_del(struct inode *inode)
4190{
4191 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004192 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004193
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004194 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004195 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004196 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004197 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004198 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004199 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004200 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004201
Josef Bacik0af3d002010-06-21 14:48:16 -04004202 /*
4203 * Free space cache has inodes in the tree root, but the tree root has a
4204 * root_refs of 0, so this could end up dropping the tree root as a
4205 * snapshot, so we need the extra !root->fs_info->tree_root check to
4206 * make sure we don't drop it.
4207 */
4208 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4209 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004210 synchronize_srcu(&root->fs_info->subvol_srcu);
4211 spin_lock(&root->inode_lock);
4212 empty = RB_EMPTY_ROOT(&root->inode_tree);
4213 spin_unlock(&root->inode_lock);
4214 if (empty)
4215 btrfs_add_dead_root(root);
4216 }
4217}
4218
Jeff Mahoney143bede2012-03-01 14:56:26 +01004219void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004220{
4221 struct rb_node *node;
4222 struct rb_node *prev;
4223 struct btrfs_inode *entry;
4224 struct inode *inode;
4225 u64 objectid = 0;
4226
4227 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4228
4229 spin_lock(&root->inode_lock);
4230again:
4231 node = root->inode_tree.rb_node;
4232 prev = NULL;
4233 while (node) {
4234 prev = node;
4235 entry = rb_entry(node, struct btrfs_inode, rb_node);
4236
Li Zefan33345d012011-04-20 10:31:50 +08004237 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004238 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004239 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004240 node = node->rb_right;
4241 else
4242 break;
4243 }
4244 if (!node) {
4245 while (prev) {
4246 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004247 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004248 node = prev;
4249 break;
4250 }
4251 prev = rb_next(prev);
4252 }
4253 }
4254 while (node) {
4255 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004256 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004257 inode = igrab(&entry->vfs_inode);
4258 if (inode) {
4259 spin_unlock(&root->inode_lock);
4260 if (atomic_read(&inode->i_count) > 1)
4261 d_prune_aliases(inode);
4262 /*
Al Viro45321ac2010-06-07 13:43:19 -04004263 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004264 * the inode cache when its usage count
4265 * hits zero.
4266 */
4267 iput(inode);
4268 cond_resched();
4269 spin_lock(&root->inode_lock);
4270 goto again;
4271 }
4272
4273 if (cond_resched_lock(&root->inode_lock))
4274 goto again;
4275
4276 node = rb_next(node);
4277 }
4278 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004279}
4280
Chris Masone02119d2008-09-05 16:13:11 -04004281static int btrfs_init_locked_inode(struct inode *inode, void *p)
4282{
4283 struct btrfs_iget_args *args = p;
4284 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004285 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004286 return 0;
4287}
4288
4289static int btrfs_find_actor(struct inode *inode, void *opaque)
4290{
4291 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004292 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004293 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004294}
4295
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004296static struct inode *btrfs_iget_locked(struct super_block *s,
4297 u64 objectid,
4298 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004299{
4300 struct inode *inode;
4301 struct btrfs_iget_args args;
4302 args.ino = objectid;
4303 args.root = root;
4304
4305 inode = iget5_locked(s, objectid, btrfs_find_actor,
4306 btrfs_init_locked_inode,
4307 (void *)&args);
4308 return inode;
4309}
4310
Balaji Rao1a54ef82008-07-21 02:01:04 +05304311/* Get an inode object given its location and corresponding root.
4312 * Returns in *is_new if the inode was read from disk
4313 */
4314struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004315 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304316{
4317 struct inode *inode;
4318
4319 inode = btrfs_iget_locked(s, location->objectid, root);
4320 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004321 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304322
4323 if (inode->i_state & I_NEW) {
4324 BTRFS_I(inode)->root = root;
4325 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4326 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004327 if (!is_bad_inode(inode)) {
4328 inode_tree_add(inode);
4329 unlock_new_inode(inode);
4330 if (new)
4331 *new = 1;
4332 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004333 unlock_new_inode(inode);
4334 iput(inode);
4335 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004336 }
4337 }
4338
Balaji Rao1a54ef82008-07-21 02:01:04 +05304339 return inode;
4340}
4341
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004342static struct inode *new_simple_dir(struct super_block *s,
4343 struct btrfs_key *key,
4344 struct btrfs_root *root)
4345{
4346 struct inode *inode = new_inode(s);
4347
4348 if (!inode)
4349 return ERR_PTR(-ENOMEM);
4350
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004351 BTRFS_I(inode)->root = root;
4352 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004353 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004354
4355 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004356 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004357 inode->i_fop = &simple_dir_operations;
4358 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4359 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4360
4361 return inode;
4362}
4363
Chris Mason3de45862008-11-17 21:02:50 -05004364struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004365{
Chris Masond3977122009-01-05 21:25:51 -05004366 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004367 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004368 struct btrfs_root *sub_root = root;
4369 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004370 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004371 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004372
4373 if (dentry->d_name.len > BTRFS_NAME_LEN)
4374 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004375
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004376 if (unlikely(d_need_lookup(dentry))) {
4377 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4378 kfree(dentry->d_fsdata);
4379 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004380 /* This thing is hashed, drop it for now */
4381 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004382 } else {
4383 ret = btrfs_inode_by_name(dir, dentry, &location);
4384 }
Chris Mason5f39d392007-10-15 16:14:19 -04004385
Chris Mason39279cc2007-06-12 06:35:45 -04004386 if (ret < 0)
4387 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004388
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004389 if (location.objectid == 0)
4390 return NULL;
4391
4392 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004393 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004394 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004395 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004396
4397 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4398
Yan, Zheng76dda932009-09-21 16:00:26 -04004399 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004400 ret = fixup_tree_root_location(root, dir, dentry,
4401 &location, &sub_root);
4402 if (ret < 0) {
4403 if (ret != -ENOENT)
4404 inode = ERR_PTR(ret);
4405 else
4406 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4407 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004408 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004409 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004410 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4411
Julia Lawall34d19ba2011-01-24 19:55:19 +00004412 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004413 down_read(&root->fs_info->cleanup_work_sem);
4414 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004415 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004416 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004417 if (ret)
4418 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004419 }
4420
Chris Mason3de45862008-11-17 21:02:50 -05004421 return inode;
4422}
4423
Nick Pigginfe15ce42011-01-07 17:49:23 +11004424static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004425{
4426 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004427 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004428
Li Zefan848cce02012-02-21 17:04:28 +08004429 if (!inode && !IS_ROOT(dentry))
4430 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004431
Li Zefan848cce02012-02-21 17:04:28 +08004432 if (inode) {
4433 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004434 if (btrfs_root_refs(&root->root_item) == 0)
4435 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004436
4437 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4438 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004439 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004440 return 0;
4441}
4442
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004443static void btrfs_dentry_release(struct dentry *dentry)
4444{
4445 if (dentry->d_fsdata)
4446 kfree(dentry->d_fsdata);
4447}
4448
Chris Mason3de45862008-11-17 21:02:50 -05004449static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004450 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004451{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004452 struct dentry *ret;
4453
4454 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4455 if (unlikely(d_need_lookup(dentry))) {
4456 spin_lock(&dentry->d_lock);
4457 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4458 spin_unlock(&dentry->d_lock);
4459 }
4460 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004461}
4462
Miao Xie16cdcec2011-04-22 18:12:22 +08004463unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004464 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4465};
4466
David Woodhousecbdf5a22008-08-06 19:42:33 +01004467static int btrfs_real_readdir(struct file *filp, void *dirent,
4468 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004469{
Chris Mason6da6aba2007-12-18 16:15:09 -05004470 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004471 struct btrfs_root *root = BTRFS_I(inode)->root;
4472 struct btrfs_item *item;
4473 struct btrfs_dir_item *di;
4474 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004475 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004476 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004477 struct list_head ins_list;
4478 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004480 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004481 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004482 unsigned char d_type;
4483 int over = 0;
4484 u32 di_cur;
4485 u32 di_total;
4486 u32 di_len;
4487 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004488 char tmp_name[32];
4489 char *name_ptr;
4490 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004491 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004492
4493 /* FIXME, use a real flag for deciding about the key type */
4494 if (root->fs_info->tree_root == root)
4495 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004496
Chris Mason39544012007-12-12 14:38:19 -05004497 /* special case for "." */
4498 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004499 over = filldir(dirent, ".", 1,
4500 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004501 if (over)
4502 return 0;
4503 filp->f_pos = 1;
4504 }
Chris Mason39544012007-12-12 14:38:19 -05004505 /* special case for .., just use the back ref */
4506 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004507 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004508 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004509 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004510 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004511 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004512 filp->f_pos = 2;
4513 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004514 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004515 if (!path)
4516 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004517
Josef Bacik026fd312011-05-13 10:32:11 -04004518 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004519
Miao Xie16cdcec2011-04-22 18:12:22 +08004520 if (key_type == BTRFS_DIR_INDEX_KEY) {
4521 INIT_LIST_HEAD(&ins_list);
4522 INIT_LIST_HEAD(&del_list);
4523 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4524 }
4525
Chris Mason39279cc2007-06-12 06:35:45 -04004526 btrfs_set_key_type(&key, key_type);
4527 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004528 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004529
Chris Mason39279cc2007-06-12 06:35:45 -04004530 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4531 if (ret < 0)
4532 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004533
4534 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004535 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004536 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004537 if (slot >= btrfs_header_nritems(leaf)) {
4538 ret = btrfs_next_leaf(root, path);
4539 if (ret < 0)
4540 goto err;
4541 else if (ret > 0)
4542 break;
4543 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004544 }
Chris Mason3de45862008-11-17 21:02:50 -05004545
Chris Mason5f39d392007-10-15 16:14:19 -04004546 item = btrfs_item_nr(leaf, slot);
4547 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4548
4549 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004550 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004551 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004552 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004553 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004554 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004555 if (key_type == BTRFS_DIR_INDEX_KEY &&
4556 btrfs_should_delete_dir_index(&del_list,
4557 found_key.offset))
4558 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004559
4560 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004561 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004562
Chris Mason39279cc2007-06-12 06:35:45 -04004563 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4564 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004565 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004566
4567 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004568 struct btrfs_key location;
4569
Josef Bacik22a94d42011-03-16 16:47:17 -04004570 if (verify_dir_item(root, leaf, di))
4571 break;
4572
Chris Mason5f39d392007-10-15 16:14:19 -04004573 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004574 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004575 name_ptr = tmp_name;
4576 } else {
4577 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004578 if (!name_ptr) {
4579 ret = -ENOMEM;
4580 goto err;
4581 }
Chris Mason5f39d392007-10-15 16:14:19 -04004582 }
4583 read_extent_buffer(leaf, name_ptr,
4584 (unsigned long)(di + 1), name_len);
4585
4586 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4587 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004588
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004589
Chris Mason3de45862008-11-17 21:02:50 -05004590 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004591 * skip it.
4592 *
4593 * In contrast to old kernels, we insert the snapshot's
4594 * dir item and dir index after it has been created, so
4595 * we won't find a reference to our own snapshot. We
4596 * still keep the following code for backward
4597 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004598 */
4599 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4600 location.objectid == root->root_key.objectid) {
4601 over = 0;
4602 goto skip;
4603 }
Chris Mason5f39d392007-10-15 16:14:19 -04004604 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004605 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004606 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004607
Chris Mason3de45862008-11-17 21:02:50 -05004608skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004609 if (name_ptr != tmp_name)
4610 kfree(name_ptr);
4611
Chris Mason39279cc2007-06-12 06:35:45 -04004612 if (over)
4613 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004614 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004615 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004616 di_cur += di_len;
4617 di = (struct btrfs_dir_item *)((char *)di + di_len);
4618 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004619next:
4620 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004621 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004622
Miao Xie16cdcec2011-04-22 18:12:22 +08004623 if (key_type == BTRFS_DIR_INDEX_KEY) {
4624 if (is_curr)
4625 filp->f_pos++;
4626 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4627 &ins_list);
4628 if (ret)
4629 goto nopos;
4630 }
4631
David Woodhouse49593bf2008-08-17 17:08:36 +01004632 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004633 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004634 /*
4635 * 32-bit glibc will use getdents64, but then strtol -
4636 * so the last number we can serve is this.
4637 */
4638 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004639 else
4640 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004641nopos:
4642 ret = 0;
4643err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004644 if (key_type == BTRFS_DIR_INDEX_KEY)
4645 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004646 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004647 return ret;
4648}
4649
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004650int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004651{
4652 struct btrfs_root *root = BTRFS_I(inode)->root;
4653 struct btrfs_trans_handle *trans;
4654 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004655 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004656
Josef Bacik72ac3c02012-05-23 14:13:11 -04004657 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004658 return 0;
4659
Liu Bo83eea1f2012-07-10 05:28:39 -06004660 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004661 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004662
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004663 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004664 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004665 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004666 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004667 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004668 if (IS_ERR(trans))
4669 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004670 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004671 }
4672 return ret;
4673}
4674
4675/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004676 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004677 * inode changes. But, it is most likely to find the inode in cache.
4678 * FIXME, needs more benchmarking...there are no reasons other than performance
4679 * to keep or drop this code.
4680 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004681int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004682{
4683 struct btrfs_root *root = BTRFS_I(inode)->root;
4684 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004685 int ret;
4686
Josef Bacik72ac3c02012-05-23 14:13:11 -04004687 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004688 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004689
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004690 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004691 if (IS_ERR(trans))
4692 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004693
4694 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004695 if (ret && ret == -ENOSPC) {
4696 /* whoops, lets try again with the full transaction */
4697 btrfs_end_transaction(trans, root);
4698 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004699 if (IS_ERR(trans))
4700 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004701
Chris Mason94b60442010-05-26 11:02:00 -04004702 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004703 }
Chris Mason39279cc2007-06-12 06:35:45 -04004704 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004705 if (BTRFS_I(inode)->delayed_node)
4706 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004707
4708 return ret;
4709}
4710
4711/*
4712 * This is a copy of file_update_time. We need this so we can return error on
4713 * ENOSPC for updating the inode in the case of file write and mmap writes.
4714 */
Josef Bacike41f9412012-03-26 09:46:47 -04004715static int btrfs_update_time(struct inode *inode, struct timespec *now,
4716 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004717{
Alexander Block2bc5565282012-06-15 09:49:33 +02004718 struct btrfs_root *root = BTRFS_I(inode)->root;
4719
4720 if (btrfs_root_readonly(root))
4721 return -EROFS;
4722
Josef Bacike41f9412012-03-26 09:46:47 -04004723 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004724 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004725 if (flags & S_CTIME)
4726 inode->i_ctime = *now;
4727 if (flags & S_MTIME)
4728 inode->i_mtime = *now;
4729 if (flags & S_ATIME)
4730 inode->i_atime = *now;
4731 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004732}
4733
Chris Masond352ac62008-09-29 15:18:18 -04004734/*
4735 * find the highest existing sequence number in a directory
4736 * and then set the in-memory index_cnt variable to reflect
4737 * free sequence numbers
4738 */
Josef Bacikaec74772008-07-24 12:12:38 -04004739static int btrfs_set_inode_index_count(struct inode *inode)
4740{
4741 struct btrfs_root *root = BTRFS_I(inode)->root;
4742 struct btrfs_key key, found_key;
4743 struct btrfs_path *path;
4744 struct extent_buffer *leaf;
4745 int ret;
4746
Li Zefan33345d012011-04-20 10:31:50 +08004747 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004748 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4749 key.offset = (u64)-1;
4750
4751 path = btrfs_alloc_path();
4752 if (!path)
4753 return -ENOMEM;
4754
4755 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4756 if (ret < 0)
4757 goto out;
4758 /* FIXME: we should be able to handle this */
4759 if (ret == 0)
4760 goto out;
4761 ret = 0;
4762
4763 /*
4764 * MAGIC NUMBER EXPLANATION:
4765 * since we search a directory based on f_pos we have to start at 2
4766 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4767 * else has to start at 2
4768 */
4769 if (path->slots[0] == 0) {
4770 BTRFS_I(inode)->index_cnt = 2;
4771 goto out;
4772 }
4773
4774 path->slots[0]--;
4775
4776 leaf = path->nodes[0];
4777 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4778
Li Zefan33345d012011-04-20 10:31:50 +08004779 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004780 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4781 BTRFS_I(inode)->index_cnt = 2;
4782 goto out;
4783 }
4784
4785 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4786out:
4787 btrfs_free_path(path);
4788 return ret;
4789}
4790
Chris Masond352ac62008-09-29 15:18:18 -04004791/*
4792 * helper to find a free sequence number in a given directory. This current
4793 * code is very simple, later versions will do smarter things in the btree
4794 */
Chris Mason3de45862008-11-17 21:02:50 -05004795int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004796{
4797 int ret = 0;
4798
4799 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004800 ret = btrfs_inode_delayed_dir_index_count(dir);
4801 if (ret) {
4802 ret = btrfs_set_inode_index_count(dir);
4803 if (ret)
4804 return ret;
4805 }
Josef Bacikaec74772008-07-24 12:12:38 -04004806 }
4807
Chris Mason00e4e6b2008-08-05 11:18:09 -04004808 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004809 BTRFS_I(dir)->index_cnt++;
4810
4811 return ret;
4812}
4813
Chris Mason39279cc2007-06-12 06:35:45 -04004814static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4815 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004816 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004817 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004818 u64 ref_objectid, u64 objectid,
4819 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004820{
4821 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004822 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004823 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004824 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004825 struct btrfs_inode_ref *ref;
4826 struct btrfs_key key[2];
4827 u32 sizes[2];
4828 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004829 int ret;
4830 int owner;
4831
Chris Mason5f39d392007-10-15 16:14:19 -04004832 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004833 if (!path)
4834 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004835
Chris Mason39279cc2007-06-12 06:35:45 -04004836 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004837 if (!inode) {
4838 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004839 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004840 }
Chris Mason39279cc2007-06-12 06:35:45 -04004841
Li Zefan581bb052011-04-20 10:06:11 +08004842 /*
4843 * we have to initialize this early, so we can reclaim the inode
4844 * number if we fail afterwards in this function.
4845 */
4846 inode->i_ino = objectid;
4847
Josef Bacikaec74772008-07-24 12:12:38 -04004848 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004849 trace_btrfs_inode_request(dir);
4850
Chris Mason3de45862008-11-17 21:02:50 -05004851 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004852 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004853 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004854 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004855 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004856 }
Josef Bacikaec74772008-07-24 12:12:38 -04004857 }
4858 /*
4859 * index_cnt is ignored for everything but a dir,
4860 * btrfs_get_inode_index_count has an explanation for the magic
4861 * number
4862 */
4863 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004864 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004865 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004866 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004867
Josef Bacik5dc562c2012-08-17 13:14:17 -04004868 /*
4869 * We could have gotten an inode number from somebody who was fsynced
4870 * and then removed in this same transaction, so let's just set full
4871 * sync since it will be a full sync anyway and this will blow away the
4872 * old info in the log.
4873 */
4874 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4875
Al Viro569254b2011-07-24 17:08:40 -04004876 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004877 owner = 0;
4878 else
4879 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004880
4881 key[0].objectid = objectid;
4882 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4883 key[0].offset = 0;
4884
Mark Fashehf1863732012-08-08 11:32:27 -07004885 /*
4886 * Start new inodes with an inode_ref. This is slightly more
4887 * efficient for small numbers of hard links since they will
4888 * be packed into one item. Extended refs will kick in if we
4889 * add more hard links than can fit in the ref item.
4890 */
Chris Mason9c583092008-01-29 15:15:18 -05004891 key[1].objectid = objectid;
4892 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4893 key[1].offset = ref_objectid;
4894
4895 sizes[0] = sizeof(struct btrfs_inode_item);
4896 sizes[1] = name_len + sizeof(*ref);
4897
Chris Masonb9473432009-03-13 11:00:37 -04004898 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004899 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4900 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004901 goto fail;
4902
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004903 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004904 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004905 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004906 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4907 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004908 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4909 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004910 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004911
4912 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4913 struct btrfs_inode_ref);
4914 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004915 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004916 ptr = (unsigned long)(ref + 1);
4917 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4918
Chris Mason5f39d392007-10-15 16:14:19 -04004919 btrfs_mark_buffer_dirty(path->nodes[0]);
4920 btrfs_free_path(path);
4921
Chris Mason39279cc2007-06-12 06:35:45 -04004922 location = &BTRFS_I(inode)->location;
4923 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004924 location->offset = 0;
4925 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4926
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004927 btrfs_inherit_iflags(inode, dir);
4928
Al Viro569254b2011-07-24 17:08:40 -04004929 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004930 if (btrfs_test_opt(root, NODATASUM))
4931 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004932 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004933 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4934 }
4935
Chris Mason39279cc2007-06-12 06:35:45 -04004936 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004937 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004938
4939 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004940 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004941
Alexander Block8ea05e32012-07-25 17:35:53 +02004942 btrfs_update_root_times(trans, root);
4943
Chris Mason39279cc2007-06-12 06:35:45 -04004944 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004945fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004946 if (dir)
4947 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004948 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004949 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004950 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004951}
4952
4953static inline u8 btrfs_inode_type(struct inode *inode)
4954{
4955 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4956}
4957
Chris Masond352ac62008-09-29 15:18:18 -04004958/*
4959 * utility function to add 'inode' into 'parent_inode' with
4960 * a give name and a given sequence number.
4961 * if 'add_backref' is true, also insert a backref from the
4962 * inode to the parent directory.
4963 */
Chris Masone02119d2008-09-05 16:13:11 -04004964int btrfs_add_link(struct btrfs_trans_handle *trans,
4965 struct inode *parent_inode, struct inode *inode,
4966 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004967{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004968 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004969 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004970 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004971 u64 ino = btrfs_ino(inode);
4972 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004973
Li Zefan33345d012011-04-20 10:31:50 +08004974 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004975 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4976 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004977 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004978 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4979 key.offset = 0;
4980 }
Chris Mason39279cc2007-06-12 06:35:45 -04004981
Li Zefan33345d012011-04-20 10:31:50 +08004982 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004983 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4984 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004985 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004986 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004987 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4988 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004989 }
4990
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004991 /* Nothing to clean up yet */
4992 if (ret)
4993 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004994
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004995 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4996 parent_inode, &key,
4997 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004998 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004999 goto fail_dir_item;
5000 else if (ret) {
5001 btrfs_abort_transaction(trans, root, ret);
5002 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005003 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005004
5005 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5006 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005007 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005008 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5009 ret = btrfs_update_inode(trans, root, parent_inode);
5010 if (ret)
5011 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005012 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005013
5014fail_dir_item:
5015 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5016 u64 local_index;
5017 int err;
5018 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5019 key.objectid, root->root_key.objectid,
5020 parent_ino, &local_index, name, name_len);
5021
5022 } else if (add_backref) {
5023 u64 local_index;
5024 int err;
5025
5026 err = btrfs_del_inode_ref(trans, root, name, name_len,
5027 ino, parent_ino, &local_index);
5028 }
5029 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005030}
5031
5032static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005033 struct inode *dir, struct dentry *dentry,
5034 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005035{
Josef Bacika1b075d2010-11-19 20:36:11 +00005036 int err = btrfs_add_link(trans, dir, inode,
5037 dentry->d_name.name, dentry->d_name.len,
5038 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005039 if (err > 0)
5040 err = -EEXIST;
5041 return err;
5042}
5043
Josef Bacik618e21d2007-07-11 10:18:17 -04005044static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005045 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005046{
5047 struct btrfs_trans_handle *trans;
5048 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005049 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005050 int err;
5051 int drop_inode = 0;
5052 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005053 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005054
5055 if (!new_valid_dev(rdev))
5056 return -EINVAL;
5057
Josef Bacik9ed74f22009-09-11 16:12:44 -04005058 /*
5059 * 2 for inode item and ref
5060 * 2 for dir items
5061 * 1 for xattr if selinux is on
5062 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005063 trans = btrfs_start_transaction(root, 5);
5064 if (IS_ERR(trans))
5065 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005066
Li Zefan581bb052011-04-20 10:06:11 +08005067 err = btrfs_find_free_ino(root, &objectid);
5068 if (err)
5069 goto out_unlock;
5070
Josef Bacikaec74772008-07-24 12:12:38 -04005071 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005072 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005073 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005074 if (IS_ERR(inode)) {
5075 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005076 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005077 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005078
Eric Paris2a7dba32011-02-01 11:05:39 -05005079 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005080 if (err) {
5081 drop_inode = 1;
5082 goto out_unlock;
5083 }
5084
Casey Schauflerad19db72011-12-15 10:09:07 -05005085 /*
5086 * If the active LSM wants to access the inode during
5087 * d_instantiate it needs these. Smack checks to see
5088 * if the filesystem supports xattrs by looking at the
5089 * ops vector.
5090 */
5091
5092 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005093 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005094 if (err)
5095 drop_inode = 1;
5096 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005097 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005098 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005099 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005100 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005101out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005102 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005103 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005104 if (drop_inode) {
5105 inode_dec_link_count(inode);
5106 iput(inode);
5107 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005108 return err;
5109}
5110
Chris Mason39279cc2007-06-12 06:35:45 -04005111static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005112 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005113{
5114 struct btrfs_trans_handle *trans;
5115 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005116 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005117 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005118 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005119 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005120 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005121
Josef Bacik9ed74f22009-09-11 16:12:44 -04005122 /*
5123 * 2 for inode item and ref
5124 * 2 for dir items
5125 * 1 for xattr if selinux is on
5126 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005127 trans = btrfs_start_transaction(root, 5);
5128 if (IS_ERR(trans))
5129 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005130
Li Zefan581bb052011-04-20 10:06:11 +08005131 err = btrfs_find_free_ino(root, &objectid);
5132 if (err)
5133 goto out_unlock;
5134
Josef Bacikaec74772008-07-24 12:12:38 -04005135 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005136 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005137 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005138 if (IS_ERR(inode)) {
5139 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005141 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005142 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005143
Eric Paris2a7dba32011-02-01 11:05:39 -05005144 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005145 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005146 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005147
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005148 err = btrfs_update_inode(trans, root, inode);
5149 if (err)
5150 goto out_unlock;
5151
Casey Schauflerad19db72011-12-15 10:09:07 -05005152 /*
5153 * If the active LSM wants to access the inode during
5154 * d_instantiate it needs these. Smack checks to see
5155 * if the filesystem supports xattrs by looking at the
5156 * ops vector.
5157 */
5158 inode->i_fop = &btrfs_file_operations;
5159 inode->i_op = &btrfs_file_inode_operations;
5160
Josef Bacika1b075d2010-11-19 20:36:11 +00005161 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005162 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005163 goto out_unlock;
5164
5165 inode->i_mapping->a_ops = &btrfs_aops;
5166 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5167 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5168 d_instantiate(dentry, inode);
5169
Chris Mason39279cc2007-06-12 06:35:45 -04005170out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005171 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005172 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005173 inode_dec_link_count(inode);
5174 iput(inode);
5175 }
Liu Bob53d3f52012-11-14 14:34:34 +00005176 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005177 return err;
5178}
5179
5180static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5181 struct dentry *dentry)
5182{
5183 struct btrfs_trans_handle *trans;
5184 struct btrfs_root *root = BTRFS_I(dir)->root;
5185 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005186 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005187 int err;
5188 int drop_inode = 0;
5189
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005190 /* do not allow sys_link's with other subvols of the same device */
5191 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005192 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005193
Mark Fashehf1863732012-08-08 11:32:27 -07005194 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005195 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005196
Chris Mason3de45862008-11-17 21:02:50 -05005197 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005198 if (err)
5199 goto fail;
5200
Yan, Zhenga22285a2010-05-16 10:48:46 -04005201 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005202 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005203 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005204 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005205 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005206 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005207 if (IS_ERR(trans)) {
5208 err = PTR_ERR(trans);
5209 goto fail;
5210 }
Chris Mason5f39d392007-10-15 16:14:19 -04005211
Miao Xie31534952011-04-13 13:19:21 +08005212 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005213 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005214 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005215 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005216 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005217
Josef Bacika1b075d2010-11-19 20:36:11 +00005218 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005219
Yan, Zhenga5719522009-09-24 09:17:31 -04005220 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005221 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005222 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005223 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005224 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005225 if (err)
5226 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005227 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005228 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005229 }
Chris Mason39279cc2007-06-12 06:35:45 -04005230
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005231 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005232fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005233 if (drop_inode) {
5234 inode_dec_link_count(inode);
5235 iput(inode);
5236 }
Liu Bob53d3f52012-11-14 14:34:34 +00005237 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005238 return err;
5239}
5240
Al Viro18bb1db2011-07-26 01:41:39 -04005241static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005242{
Chris Masonb9d86662008-05-02 16:13:49 -04005243 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005244 struct btrfs_trans_handle *trans;
5245 struct btrfs_root *root = BTRFS_I(dir)->root;
5246 int err = 0;
5247 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005248 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005249 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005250
Josef Bacik9ed74f22009-09-11 16:12:44 -04005251 /*
5252 * 2 items for inode and ref
5253 * 2 items for dir items
5254 * 1 for xattr if selinux is on
5255 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005256 trans = btrfs_start_transaction(root, 5);
5257 if (IS_ERR(trans))
5258 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005259
Li Zefan581bb052011-04-20 10:06:11 +08005260 err = btrfs_find_free_ino(root, &objectid);
5261 if (err)
5262 goto out_fail;
5263
Josef Bacikaec74772008-07-24 12:12:38 -04005264 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005265 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005266 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005267 if (IS_ERR(inode)) {
5268 err = PTR_ERR(inode);
5269 goto out_fail;
5270 }
Chris Mason5f39d392007-10-15 16:14:19 -04005271
Chris Mason39279cc2007-06-12 06:35:45 -04005272 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005273
Eric Paris2a7dba32011-02-01 11:05:39 -05005274 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005275 if (err)
5276 goto out_fail;
5277
Chris Mason39279cc2007-06-12 06:35:45 -04005278 inode->i_op = &btrfs_dir_inode_operations;
5279 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005280
Chris Masondbe674a2008-07-17 12:54:05 -04005281 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005282 err = btrfs_update_inode(trans, root, inode);
5283 if (err)
5284 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005285
Josef Bacika1b075d2010-11-19 20:36:11 +00005286 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5287 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005288 if (err)
5289 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005290
Chris Mason39279cc2007-06-12 06:35:45 -04005291 d_instantiate(dentry, inode);
5292 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005293
5294out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005295 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005296 if (drop_on_err)
5297 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005298 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005299 return err;
5300}
5301
Chris Masond352ac62008-09-29 15:18:18 -04005302/* helper for btfs_get_extent. Given an existing extent in the tree,
5303 * and an extent that you want to insert, deal with overlap and insert
5304 * the new extent into the tree.
5305 */
Chris Mason3b951512008-04-17 11:29:12 -04005306static int merge_extent_mapping(struct extent_map_tree *em_tree,
5307 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005308 struct extent_map *em,
5309 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005310{
5311 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005312
Chris Masone6dcd2d2008-07-17 12:53:50 -04005313 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5314 start_diff = map_start - em->start;
5315 em->start = map_start;
5316 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005317 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5318 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005319 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005320 em->block_len -= start_diff;
5321 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005322 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005323}
5324
Chris Masonc8b97812008-10-29 14:49:59 -04005325static noinline int uncompress_inline(struct btrfs_path *path,
5326 struct inode *inode, struct page *page,
5327 size_t pg_offset, u64 extent_offset,
5328 struct btrfs_file_extent_item *item)
5329{
5330 int ret;
5331 struct extent_buffer *leaf = path->nodes[0];
5332 char *tmp;
5333 size_t max_size;
5334 unsigned long inline_size;
5335 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005336 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005337
5338 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005339 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005340 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5341 inline_size = btrfs_file_extent_inline_item_len(leaf,
5342 btrfs_item_nr(leaf, path->slots[0]));
5343 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005344 if (!tmp)
5345 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005346 ptr = btrfs_file_extent_inline_start(item);
5347
5348 read_extent_buffer(leaf, tmp, ptr, inline_size);
5349
Chris Mason5b050f02008-11-11 09:34:41 -05005350 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005351 ret = btrfs_decompress(compress_type, tmp, page,
5352 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005353 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005354 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005355 unsigned long copy_size = min_t(u64,
5356 PAGE_CACHE_SIZE - pg_offset,
5357 max_size - extent_offset);
5358 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005359 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005360 }
5361 kfree(tmp);
5362 return 0;
5363}
5364
Chris Masond352ac62008-09-29 15:18:18 -04005365/*
5366 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005367 * the ugly parts come from merging extents from the disk with the in-ram
5368 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005369 * where the in-ram extents might be locked pending data=ordered completion.
5370 *
5371 * This also copies inline extents directly into the page.
5372 */
Chris Masond3977122009-01-05 21:25:51 -05005373
Chris Masona52d9a82007-08-27 16:49:44 -04005374struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005375 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005376 int create)
5377{
5378 int ret;
5379 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005380 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005381 u64 extent_start = 0;
5382 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005383 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005384 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005385 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005386 struct btrfs_root *root = BTRFS_I(inode)->root;
5387 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005388 struct extent_buffer *leaf;
5389 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005390 struct extent_map *em = NULL;
5391 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005392 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005393 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005394 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005395
Chris Masona52d9a82007-08-27 16:49:44 -04005396again:
Chris Mason890871b2009-09-02 16:24:52 -04005397 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005398 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005399 if (em)
5400 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005401 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005402
Chris Masona52d9a82007-08-27 16:49:44 -04005403 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005404 if (em->start > start || em->start + em->len <= start)
5405 free_extent_map(em);
5406 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005407 free_extent_map(em);
5408 else
5409 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005410 }
David Sterba172ddd62011-04-21 00:48:27 +02005411 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005412 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005413 err = -ENOMEM;
5414 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005415 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005416 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005417 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005418 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005419 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005420 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005421
5422 if (!path) {
5423 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005424 if (!path) {
5425 err = -ENOMEM;
5426 goto out;
5427 }
5428 /*
5429 * Chances are we'll be called again, so go ahead and do
5430 * readahead
5431 */
5432 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005433 }
5434
Chris Mason179e29e2007-11-01 11:28:41 -04005435 ret = btrfs_lookup_file_extent(trans, root, path,
5436 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005437 if (ret < 0) {
5438 err = ret;
5439 goto out;
5440 }
5441
5442 if (ret != 0) {
5443 if (path->slots[0] == 0)
5444 goto not_found;
5445 path->slots[0]--;
5446 }
5447
Chris Mason5f39d392007-10-15 16:14:19 -04005448 leaf = path->nodes[0];
5449 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005450 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005451 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005452 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5453 found_type = btrfs_key_type(&found_key);
5454 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005455 found_type != BTRFS_EXTENT_DATA_KEY) {
5456 goto not_found;
5457 }
5458
Chris Mason5f39d392007-10-15 16:14:19 -04005459 found_type = btrfs_file_extent_type(leaf, item);
5460 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005461 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005462 if (found_type == BTRFS_FILE_EXTENT_REG ||
5463 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005464 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005465 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005466 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5467 size_t size;
5468 size = btrfs_file_extent_inline_len(leaf, item);
5469 extent_end = (extent_start + size + root->sectorsize - 1) &
5470 ~((u64)root->sectorsize - 1);
5471 }
5472
5473 if (start >= extent_end) {
5474 path->slots[0]++;
5475 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5476 ret = btrfs_next_leaf(root, path);
5477 if (ret < 0) {
5478 err = ret;
5479 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005480 }
Yan Zheng9036c102008-10-30 14:19:41 -04005481 if (ret > 0)
5482 goto not_found;
5483 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005484 }
Yan Zheng9036c102008-10-30 14:19:41 -04005485 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5486 if (found_key.objectid != objectid ||
5487 found_key.type != BTRFS_EXTENT_DATA_KEY)
5488 goto not_found;
5489 if (start + len <= found_key.offset)
5490 goto not_found;
5491 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005492 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005493 em->len = found_key.offset - start;
5494 goto not_found_em;
5495 }
5496
Yan Zhengd899e052008-10-30 14:25:28 -04005497 if (found_type == BTRFS_FILE_EXTENT_REG ||
5498 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005499 em->start = extent_start;
5500 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005501 em->orig_start = extent_start -
5502 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005503 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5504 item);
Chris Masondb945352007-10-15 16:15:53 -04005505 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5506 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005507 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005508 goto insert;
5509 }
Li Zefan261507a02010-12-17 14:21:50 +08005510 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005511 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005512 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005513 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005514 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005515 } else {
5516 bytenr += btrfs_file_extent_offset(leaf, item);
5517 em->block_start = bytenr;
5518 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005519 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5520 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005521 }
Chris Masona52d9a82007-08-27 16:49:44 -04005522 goto insert;
5523 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005524 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005525 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005526 size_t size;
5527 size_t extent_offset;
5528 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005529
Chris Masona52d9a82007-08-27 16:49:44 -04005530 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005531 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005532 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005533 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005534 goto out;
5535 }
5536
Yan Zheng9036c102008-10-30 14:19:41 -04005537 size = btrfs_file_extent_inline_len(leaf, item);
5538 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005539 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005540 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005541 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005542 em->len = (copy_size + root->sectorsize - 1) &
5543 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005544 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005545 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005546 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005547 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005548 em->compress_type = compress_type;
5549 }
Yan689f9342007-10-29 11:41:07 -04005550 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005551 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005552 if (btrfs_file_extent_compression(leaf, item) !=
5553 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005554 ret = uncompress_inline(path, inode, page,
5555 pg_offset,
5556 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005557 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005558 } else {
5559 map = kmap(page);
5560 read_extent_buffer(leaf, map + pg_offset, ptr,
5561 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005562 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5563 memset(map + pg_offset + copy_size, 0,
5564 PAGE_CACHE_SIZE - pg_offset -
5565 copy_size);
5566 }
Chris Masonc8b97812008-10-29 14:49:59 -04005567 kunmap(page);
5568 }
Chris Mason179e29e2007-11-01 11:28:41 -04005569 flush_dcache_page(page);
5570 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005571 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005572 if (!trans) {
5573 kunmap(page);
5574 free_extent_map(em);
5575 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005576
David Sterbab3b4aa72011-04-21 01:20:15 +02005577 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005578 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005579
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005580 if (IS_ERR(trans))
5581 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005582 goto again;
5583 }
Chris Masonc8b97812008-10-29 14:49:59 -04005584 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005585 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005586 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005587 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005588 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005589 }
Chris Masond1310b22008-01-24 16:13:08 -05005590 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005591 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005592 goto insert;
5593 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005594 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005595 }
5596not_found:
5597 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005598 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005599 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005600not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005601 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005602 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005603insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005604 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005605 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005606 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5607 "[%llu %llu]\n", (unsigned long long)em->start,
5608 (unsigned long long)em->len,
5609 (unsigned long long)start,
5610 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005611 err = -EIO;
5612 goto out;
5613 }
Chris Masond1310b22008-01-24 16:13:08 -05005614
5615 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005616 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005617 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005618 /* it is possible that someone inserted the extent into the tree
5619 * while we had the lock dropped. It is also possible that
5620 * an overlapping map exists in the tree
5621 */
Chris Masona52d9a82007-08-27 16:49:44 -04005622 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005623 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005624
5625 ret = 0;
5626
Chris Mason3b951512008-04-17 11:29:12 -04005627 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005628 if (existing && (existing->start > start ||
5629 existing->start + existing->len <= start)) {
5630 free_extent_map(existing);
5631 existing = NULL;
5632 }
Chris Mason3b951512008-04-17 11:29:12 -04005633 if (!existing) {
5634 existing = lookup_extent_mapping(em_tree, em->start,
5635 em->len);
5636 if (existing) {
5637 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005638 em, start,
5639 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005640 free_extent_map(existing);
5641 if (err) {
5642 free_extent_map(em);
5643 em = NULL;
5644 }
5645 } else {
5646 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005647 free_extent_map(em);
5648 em = NULL;
5649 }
5650 } else {
5651 free_extent_map(em);
5652 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005653 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005654 }
Chris Masona52d9a82007-08-27 16:49:44 -04005655 }
Chris Mason890871b2009-09-02 16:24:52 -04005656 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005657out:
liubo1abe9b82011-03-24 11:18:59 +00005658
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005659 if (em)
5660 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005661
Chris Masonf4219502008-07-22 11:18:09 -04005662 if (path)
5663 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005664 if (trans) {
5665 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005666 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005667 err = ret;
5668 }
Chris Masona52d9a82007-08-27 16:49:44 -04005669 if (err) {
5670 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005671 return ERR_PTR(err);
5672 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005673 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005674 return em;
5675}
5676
Chris Masonec29ed52011-02-23 16:23:20 -05005677struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5678 size_t pg_offset, u64 start, u64 len,
5679 int create)
5680{
5681 struct extent_map *em;
5682 struct extent_map *hole_em = NULL;
5683 u64 range_start = start;
5684 u64 end;
5685 u64 found;
5686 u64 found_end;
5687 int err = 0;
5688
5689 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5690 if (IS_ERR(em))
5691 return em;
5692 if (em) {
5693 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005694 * if our em maps to
5695 * - a hole or
5696 * - a pre-alloc extent,
5697 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005698 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005699 if (em->block_start != EXTENT_MAP_HOLE &&
5700 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005701 return em;
5702 else
5703 hole_em = em;
5704 }
5705
5706 /* check to see if we've wrapped (len == -1 or similar) */
5707 end = start + len;
5708 if (end < start)
5709 end = (u64)-1;
5710 else
5711 end -= 1;
5712
5713 em = NULL;
5714
5715 /* ok, we didn't find anything, lets look for delalloc */
5716 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5717 end, len, EXTENT_DELALLOC, 1);
5718 found_end = range_start + found;
5719 if (found_end < range_start)
5720 found_end = (u64)-1;
5721
5722 /*
5723 * we didn't find anything useful, return
5724 * the original results from get_extent()
5725 */
5726 if (range_start > end || found_end <= start) {
5727 em = hole_em;
5728 hole_em = NULL;
5729 goto out;
5730 }
5731
5732 /* adjust the range_start to make sure it doesn't
5733 * go backwards from the start they passed in
5734 */
5735 range_start = max(start,range_start);
5736 found = found_end - range_start;
5737
5738 if (found > 0) {
5739 u64 hole_start = start;
5740 u64 hole_len = len;
5741
David Sterba172ddd62011-04-21 00:48:27 +02005742 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005743 if (!em) {
5744 err = -ENOMEM;
5745 goto out;
5746 }
5747 /*
5748 * when btrfs_get_extent can't find anything it
5749 * returns one huge hole
5750 *
5751 * make sure what it found really fits our range, and
5752 * adjust to make sure it is based on the start from
5753 * the caller
5754 */
5755 if (hole_em) {
5756 u64 calc_end = extent_map_end(hole_em);
5757
5758 if (calc_end <= start || (hole_em->start > end)) {
5759 free_extent_map(hole_em);
5760 hole_em = NULL;
5761 } else {
5762 hole_start = max(hole_em->start, start);
5763 hole_len = calc_end - hole_start;
5764 }
5765 }
5766 em->bdev = NULL;
5767 if (hole_em && range_start > hole_start) {
5768 /* our hole starts before our delalloc, so we
5769 * have to return just the parts of the hole
5770 * that go until the delalloc starts
5771 */
5772 em->len = min(hole_len,
5773 range_start - hole_start);
5774 em->start = hole_start;
5775 em->orig_start = hole_start;
5776 /*
5777 * don't adjust block start at all,
5778 * it is fixed at EXTENT_MAP_HOLE
5779 */
5780 em->block_start = hole_em->block_start;
5781 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005782 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5783 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005784 } else {
5785 em->start = range_start;
5786 em->len = found;
5787 em->orig_start = range_start;
5788 em->block_start = EXTENT_MAP_DELALLOC;
5789 em->block_len = found;
5790 }
5791 } else if (hole_em) {
5792 return hole_em;
5793 }
5794out:
5795
5796 free_extent_map(hole_em);
5797 if (err) {
5798 free_extent_map(em);
5799 return ERR_PTR(err);
5800 }
5801 return em;
5802}
5803
Josef Bacik4b46fce2010-05-23 11:00:55 -04005804static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5805 u64 start, u64 len)
5806{
5807 struct btrfs_root *root = BTRFS_I(inode)->root;
5808 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005809 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005810 struct btrfs_key ins;
5811 u64 alloc_hint;
5812 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005813
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005814 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005815 if (IS_ERR(trans))
5816 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005817
5818 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5819
5820 alloc_hint = get_extent_allocation_hint(inode, start, len);
5821 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005822 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005823 if (ret) {
5824 em = ERR_PTR(ret);
5825 goto out;
5826 }
5827
Josef Bacik70c8a912012-10-11 16:54:30 -04005828 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5829 ins.offset, ins.offset, 0);
5830 if (IS_ERR(em))
5831 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005832
5833 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5834 ins.offset, ins.offset, 0);
5835 if (ret) {
5836 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5837 em = ERR_PTR(ret);
5838 }
5839out:
5840 btrfs_end_transaction(trans, root);
5841 return em;
5842}
5843
Chris Mason46bfbb52010-05-26 11:04:10 -04005844/*
5845 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5846 * block must be cow'd
5847 */
5848static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5849 struct inode *inode, u64 offset, u64 len)
5850{
5851 struct btrfs_path *path;
5852 int ret;
5853 struct extent_buffer *leaf;
5854 struct btrfs_root *root = BTRFS_I(inode)->root;
5855 struct btrfs_file_extent_item *fi;
5856 struct btrfs_key key;
5857 u64 disk_bytenr;
5858 u64 backref_offset;
5859 u64 extent_end;
5860 u64 num_bytes;
5861 int slot;
5862 int found_type;
5863
5864 path = btrfs_alloc_path();
5865 if (!path)
5866 return -ENOMEM;
5867
Li Zefan33345d012011-04-20 10:31:50 +08005868 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005869 offset, 0);
5870 if (ret < 0)
5871 goto out;
5872
5873 slot = path->slots[0];
5874 if (ret == 1) {
5875 if (slot == 0) {
5876 /* can't find the item, must cow */
5877 ret = 0;
5878 goto out;
5879 }
5880 slot--;
5881 }
5882 ret = 0;
5883 leaf = path->nodes[0];
5884 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005885 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005886 key.type != BTRFS_EXTENT_DATA_KEY) {
5887 /* not our file or wrong item type, must cow */
5888 goto out;
5889 }
5890
5891 if (key.offset > offset) {
5892 /* Wrong offset, must cow */
5893 goto out;
5894 }
5895
5896 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5897 found_type = btrfs_file_extent_type(leaf, fi);
5898 if (found_type != BTRFS_FILE_EXTENT_REG &&
5899 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5900 /* not a regular extent, must cow */
5901 goto out;
5902 }
5903 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5904 backref_offset = btrfs_file_extent_offset(leaf, fi);
5905
5906 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5907 if (extent_end < offset + len) {
5908 /* extent doesn't include our full range, must cow */
5909 goto out;
5910 }
5911
5912 if (btrfs_extent_readonly(root, disk_bytenr))
5913 goto out;
5914
5915 /*
5916 * look for other files referencing this extent, if we
5917 * find any we must cow
5918 */
Li Zefan33345d012011-04-20 10:31:50 +08005919 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005920 key.offset - backref_offset, disk_bytenr))
5921 goto out;
5922
5923 /*
5924 * adjust disk_bytenr and num_bytes to cover just the bytes
5925 * in this extent we are about to write. If there
5926 * are any csums in that range we have to cow in order
5927 * to keep the csums correct
5928 */
5929 disk_bytenr += backref_offset;
5930 disk_bytenr += offset - key.offset;
5931 num_bytes = min(offset + len, extent_end) - offset;
5932 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5933 goto out;
5934 /*
5935 * all of the above have passed, it is safe to overwrite this extent
5936 * without cow
5937 */
5938 ret = 1;
5939out:
5940 btrfs_free_path(path);
5941 return ret;
5942}
5943
Josef Bacikeb838e72012-07-31 16:28:48 -04005944static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5945 struct extent_state **cached_state, int writing)
5946{
5947 struct btrfs_ordered_extent *ordered;
5948 int ret = 0;
5949
5950 while (1) {
5951 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5952 0, cached_state);
5953 /*
5954 * We're concerned with the entire range that we're going to be
5955 * doing DIO to, so we need to make sure theres no ordered
5956 * extents in this range.
5957 */
5958 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5959 lockend - lockstart + 1);
5960
5961 /*
5962 * We need to make sure there are no buffered pages in this
5963 * range either, we could have raced between the invalidate in
5964 * generic_file_direct_write and locking the extent. The
5965 * invalidate needs to happen so that reads after a write do not
5966 * get stale data.
5967 */
5968 if (!ordered && (!writing ||
5969 !test_range_bit(&BTRFS_I(inode)->io_tree,
5970 lockstart, lockend, EXTENT_UPTODATE, 0,
5971 *cached_state)))
5972 break;
5973
5974 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5975 cached_state, GFP_NOFS);
5976
5977 if (ordered) {
5978 btrfs_start_ordered_extent(inode, ordered, 1);
5979 btrfs_put_ordered_extent(ordered);
5980 } else {
5981 /* Screw you mmap */
5982 ret = filemap_write_and_wait_range(inode->i_mapping,
5983 lockstart,
5984 lockend);
5985 if (ret)
5986 break;
5987
5988 /*
5989 * If we found a page that couldn't be invalidated just
5990 * fall back to buffered.
5991 */
5992 ret = invalidate_inode_pages2_range(inode->i_mapping,
5993 lockstart >> PAGE_CACHE_SHIFT,
5994 lockend >> PAGE_CACHE_SHIFT);
5995 if (ret)
5996 break;
5997 }
5998
5999 cond_resched();
6000 }
6001
6002 return ret;
6003}
6004
Josef Bacik69ffb542012-09-11 15:40:07 -04006005static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6006 u64 len, u64 orig_start,
6007 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05006008 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006009{
6010 struct extent_map_tree *em_tree;
6011 struct extent_map *em;
6012 struct btrfs_root *root = BTRFS_I(inode)->root;
6013 int ret;
6014
6015 em_tree = &BTRFS_I(inode)->extent_tree;
6016 em = alloc_extent_map();
6017 if (!em)
6018 return ERR_PTR(-ENOMEM);
6019
6020 em->start = start;
6021 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006022 em->mod_start = start;
6023 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006024 em->len = len;
6025 em->block_len = block_len;
6026 em->block_start = block_start;
6027 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006028 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006029 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006030 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6031 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006032 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006033
6034 do {
6035 btrfs_drop_extent_cache(inode, em->start,
6036 em->start + em->len - 1, 0);
6037 write_lock(&em_tree->lock);
6038 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006039 if (!ret)
6040 list_move(&em->list,
6041 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006042 write_unlock(&em_tree->lock);
6043 } while (ret == -EEXIST);
6044
6045 if (ret) {
6046 free_extent_map(em);
6047 return ERR_PTR(ret);
6048 }
6049
6050 return em;
6051}
6052
6053
Josef Bacik4b46fce2010-05-23 11:00:55 -04006054static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6055 struct buffer_head *bh_result, int create)
6056{
6057 struct extent_map *em;
6058 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006059 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006060 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006061 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006062 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006063 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006064 int unlock_bits = EXTENT_LOCKED;
6065 int ret;
6066
Josef Bacikeb838e72012-07-31 16:28:48 -04006067 if (create) {
6068 ret = btrfs_delalloc_reserve_space(inode, len);
6069 if (ret)
6070 return ret;
6071 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04006072 } else {
6073 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006074 }
6075
Josef Bacikc3298612012-08-03 16:49:19 -04006076 lockstart = start;
6077 lockend = start + len - 1;
6078
Josef Bacikeb838e72012-07-31 16:28:48 -04006079 /*
6080 * If this errors out it's because we couldn't invalidate pagecache for
6081 * this range and we need to fallback to buffered.
6082 */
6083 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6084 return -ENOTBLK;
6085
6086 if (create) {
6087 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6088 lockend, EXTENT_DELALLOC, NULL,
6089 &cached_state, GFP_NOFS);
6090 if (ret)
6091 goto unlock_err;
6092 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006093
6094 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006095 if (IS_ERR(em)) {
6096 ret = PTR_ERR(em);
6097 goto unlock_err;
6098 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006099
6100 /*
6101 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6102 * io. INLINE is special, and we could probably kludge it in here, but
6103 * it's still buffered so for safety lets just fall back to the generic
6104 * buffered path.
6105 *
6106 * For COMPRESSED we _have_ to read the entire extent in so we can
6107 * decompress it, so there will be buffering required no matter what we
6108 * do, so go ahead and fallback to buffered.
6109 *
6110 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6111 * to buffered IO. Don't blame me, this is the price we pay for using
6112 * the generic code.
6113 */
6114 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6115 em->block_start == EXTENT_MAP_INLINE) {
6116 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006117 ret = -ENOTBLK;
6118 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006119 }
6120
6121 /* Just a good old fashioned hole, return */
6122 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6123 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6124 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006125 ret = 0;
6126 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006127 }
6128
6129 /*
6130 * We don't allocate a new extent in the following cases
6131 *
6132 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6133 * existing extent.
6134 * 2) The extent is marked as PREALLOC. We're good to go here and can
6135 * just use the extent.
6136 *
6137 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006138 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006139 len = min(len, em->len - (start - em->start));
6140 lockstart = start + len;
6141 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006142 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006143
6144 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6145 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6146 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006147 int type;
6148 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006149 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006150
6151 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6152 type = BTRFS_ORDERED_PREALLOC;
6153 else
6154 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006155 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006156 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006157
6158 /*
6159 * we're not going to log anything, but we do need
6160 * to make sure the current transaction stays open
6161 * while we look for nocow cross refs
6162 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006163 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006164 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006165 goto must_cow;
6166
6167 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006168 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006169 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006170
6171 if (type == BTRFS_ORDERED_PREALLOC) {
6172 free_extent_map(em);
6173 em = create_pinned_em(inode, start, len,
6174 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006175 block_start, len,
6176 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006177 if (IS_ERR(em)) {
6178 btrfs_end_transaction(trans, root);
6179 goto unlock_err;
6180 }
6181 }
6182
Chris Mason46bfbb52010-05-26 11:04:10 -04006183 ret = btrfs_add_ordered_extent_dio(inode, start,
6184 block_start, len, len, type);
6185 btrfs_end_transaction(trans, root);
6186 if (ret) {
6187 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006188 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006189 }
6190 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006191 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006192 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006193 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006194must_cow:
6195 /*
6196 * this will cow the extent, reset the len in case we changed
6197 * it above
6198 */
6199 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006200 free_extent_map(em);
6201 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006202 if (IS_ERR(em)) {
6203 ret = PTR_ERR(em);
6204 goto unlock_err;
6205 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006206 len = min(len, em->len - (start - em->start));
6207unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006208 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6209 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006210 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006211 bh_result->b_bdev = em->bdev;
6212 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006213 if (create) {
6214 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6215 set_buffer_new(bh_result);
6216
6217 /*
6218 * Need to update the i_size under the extent lock so buffered
6219 * readers will get the updated i_size when we unlock.
6220 */
6221 if (start + len > i_size_read(inode))
6222 i_size_write(inode, start + len);
6223 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006224
Josef Bacikeb838e72012-07-31 16:28:48 -04006225 /*
6226 * In the case of write we need to clear and unlock the entire range,
6227 * in the case of read we need to unlock only the end area that we
6228 * aren't using if there is any left over space.
6229 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006230 if (lockstart < lockend) {
6231 if (create && len < lockend - lockstart) {
6232 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006233 lockstart + len - 1,
6234 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006235 &cached_state, GFP_NOFS);
6236 /*
6237 * Beside unlock, we also need to cleanup reserved space
6238 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6239 */
6240 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6241 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006242 unlock_bits | EXTENT_DO_ACCOUNTING |
6243 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006244 } else {
6245 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6246 lockend, unlock_bits, 1, 0,
6247 &cached_state, GFP_NOFS);
6248 }
6249 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006250 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006251 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006252
Josef Bacik4b46fce2010-05-23 11:00:55 -04006253 free_extent_map(em);
6254
6255 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006256
6257unlock_err:
6258 if (create)
6259 unlock_bits |= EXTENT_DO_ACCOUNTING;
6260
6261 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6262 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6263 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006264}
6265
6266struct btrfs_dio_private {
6267 struct inode *inode;
6268 u64 logical_offset;
6269 u64 disk_bytenr;
6270 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006271 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006272
6273 /* number of bios pending for this dio */
6274 atomic_t pending_bios;
6275
6276 /* IO errors */
6277 int errors;
6278
6279 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006280};
6281
6282static void btrfs_endio_direct_read(struct bio *bio, int err)
6283{
Miao Xiee65e1532010-11-22 03:04:43 +00006284 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006285 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6286 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006287 struct inode *inode = dip->inode;
6288 struct btrfs_root *root = BTRFS_I(inode)->root;
6289 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006290
6291 start = dip->logical_offset;
6292 do {
6293 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6294 struct page *page = bvec->bv_page;
6295 char *kaddr;
6296 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006297 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006298 unsigned long flags;
6299
Josef Bacikc3298612012-08-03 16:49:19 -04006300 if (get_state_private(&BTRFS_I(inode)->io_tree,
6301 start, &private))
6302 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006303 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006304 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006305 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6306 csum, bvec->bv_len);
6307 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006308 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006309 local_irq_restore(flags);
6310
6311 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006312 if (csum != private) {
6313failed:
Li Zefan33345d012011-04-20 10:31:50 +08006314 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006315 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006316 (unsigned long long)btrfs_ino(inode),
6317 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006318 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006319 err = -EIO;
6320 }
6321 }
6322
6323 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006324 bvec++;
6325 } while (bvec <= bvec_end);
6326
6327 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006328 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006329 bio->bi_private = dip->private;
6330
Josef Bacik4b46fce2010-05-23 11:00:55 -04006331 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006332
6333 /* If we had a csum failure make sure to clear the uptodate flag */
6334 if (err)
6335 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006336 dio_end_io(bio, err);
6337}
6338
6339static void btrfs_endio_direct_write(struct bio *bio, int err)
6340{
6341 struct btrfs_dio_private *dip = bio->bi_private;
6342 struct inode *inode = dip->inode;
6343 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006344 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006345 u64 ordered_offset = dip->logical_offset;
6346 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006347 int ret;
6348
6349 if (err)
6350 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006351again:
6352 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6353 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006354 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006355 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006356 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006357
Josef Bacik5fd02042012-05-02 14:00:54 -04006358 ordered->work.func = finish_ordered_fn;
6359 ordered->work.flags = 0;
6360 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6361 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006362out_test:
6363 /*
6364 * our bio might span multiple ordered extents. If we haven't
6365 * completed the accounting for the whole dio, go back and try again
6366 */
6367 if (ordered_offset < dip->logical_offset + dip->bytes) {
6368 ordered_bytes = dip->logical_offset + dip->bytes -
6369 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006370 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006371 goto again;
6372 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006373out_done:
6374 bio->bi_private = dip->private;
6375
Josef Bacik4b46fce2010-05-23 11:00:55 -04006376 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006377
6378 /* If we had an error make sure to clear the uptodate flag */
6379 if (err)
6380 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006381 dio_end_io(bio, err);
6382}
6383
Chris Masoneaf25d92010-05-25 09:48:28 -04006384static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6385 struct bio *bio, int mirror_num,
6386 unsigned long bio_flags, u64 offset)
6387{
6388 int ret;
6389 struct btrfs_root *root = BTRFS_I(inode)->root;
6390 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006391 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006392 return 0;
6393}
6394
Miao Xiee65e1532010-11-22 03:04:43 +00006395static void btrfs_end_dio_bio(struct bio *bio, int err)
6396{
6397 struct btrfs_dio_private *dip = bio->bi_private;
6398
6399 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006400 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006401 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006402 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006403 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006404 dip->errors = 1;
6405
6406 /*
6407 * before atomic variable goto zero, we must make sure
6408 * dip->errors is perceived to be set.
6409 */
6410 smp_mb__before_atomic_dec();
6411 }
6412
6413 /* if there are more bios still pending for this dio, just exit */
6414 if (!atomic_dec_and_test(&dip->pending_bios))
6415 goto out;
6416
6417 if (dip->errors)
6418 bio_io_error(dip->orig_bio);
6419 else {
6420 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6421 bio_endio(dip->orig_bio, 0);
6422 }
6423out:
6424 bio_put(bio);
6425}
6426
6427static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6428 u64 first_sector, gfp_t gfp_flags)
6429{
6430 int nr_vecs = bio_get_nr_vecs(bdev);
6431 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6432}
6433
6434static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6435 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006436 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006437{
6438 int write = rw & REQ_WRITE;
6439 struct btrfs_root *root = BTRFS_I(inode)->root;
6440 int ret;
6441
Josef Bacikb812ce22012-11-16 13:56:32 -05006442 if (async_submit)
6443 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6444
Miao Xiee65e1532010-11-22 03:04:43 +00006445 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006446
6447 if (!write) {
6448 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6449 if (ret)
6450 goto err;
6451 }
Miao Xiee65e1532010-11-22 03:04:43 +00006452
Josef Bacik1ae39932011-04-06 14:41:34 -04006453 if (skip_sum)
6454 goto map;
6455
6456 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006457 ret = btrfs_wq_submit_bio(root->fs_info,
6458 inode, rw, bio, 0, 0,
6459 file_offset,
6460 __btrfs_submit_bio_start_direct_io,
6461 __btrfs_submit_bio_done);
6462 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006463 } else if (write) {
6464 /*
6465 * If we aren't doing async submit, calculate the csum of the
6466 * bio now.
6467 */
6468 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6469 if (ret)
6470 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006471 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006472 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006473 if (ret)
6474 goto err;
6475 }
Miao Xiee65e1532010-11-22 03:04:43 +00006476
Josef Bacik1ae39932011-04-06 14:41:34 -04006477map:
6478 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006479err:
6480 bio_put(bio);
6481 return ret;
6482}
6483
6484static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6485 int skip_sum)
6486{
6487 struct inode *inode = dip->inode;
6488 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006489 struct bio *bio;
6490 struct bio *orig_bio = dip->orig_bio;
6491 struct bio_vec *bvec = orig_bio->bi_io_vec;
6492 u64 start_sector = orig_bio->bi_sector;
6493 u64 file_offset = dip->logical_offset;
6494 u64 submit_len = 0;
6495 u64 map_length;
6496 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006497 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006498 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006499
Miao Xiee65e1532010-11-22 03:04:43 +00006500 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006501 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006502 &map_length, NULL, 0);
6503 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006504 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006505 return -EIO;
6506 }
6507
Josef Bacik02f57c72011-04-06 14:25:44 -04006508 if (map_length >= orig_bio->bi_size) {
6509 bio = orig_bio;
6510 goto submit;
6511 }
6512
Josef Bacik1ae39932011-04-06 14:41:34 -04006513 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006514 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6515 if (!bio)
6516 return -ENOMEM;
6517 bio->bi_private = dip;
6518 bio->bi_end_io = btrfs_end_dio_bio;
6519 atomic_inc(&dip->pending_bios);
6520
Miao Xiee65e1532010-11-22 03:04:43 +00006521 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6522 if (unlikely(map_length < submit_len + bvec->bv_len ||
6523 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6524 bvec->bv_offset) < bvec->bv_len)) {
6525 /*
6526 * inc the count before we submit the bio so
6527 * we know the end IO handler won't happen before
6528 * we inc the count. Otherwise, the dip might get freed
6529 * before we're done setting it up
6530 */
6531 atomic_inc(&dip->pending_bios);
6532 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6533 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006534 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006535 if (ret) {
6536 bio_put(bio);
6537 atomic_dec(&dip->pending_bios);
6538 goto out_err;
6539 }
6540
Miao Xiee65e1532010-11-22 03:04:43 +00006541 start_sector += submit_len >> 9;
6542 file_offset += submit_len;
6543
6544 submit_len = 0;
6545 nr_pages = 0;
6546
6547 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6548 start_sector, GFP_NOFS);
6549 if (!bio)
6550 goto out_err;
6551 bio->bi_private = dip;
6552 bio->bi_end_io = btrfs_end_dio_bio;
6553
6554 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006555 ret = btrfs_map_block(root->fs_info, READ,
6556 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006557 &map_length, NULL, 0);
6558 if (ret) {
6559 bio_put(bio);
6560 goto out_err;
6561 }
6562 } else {
6563 submit_len += bvec->bv_len;
6564 nr_pages ++;
6565 bvec++;
6566 }
6567 }
6568
Josef Bacik02f57c72011-04-06 14:25:44 -04006569submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006570 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006571 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006572 if (!ret)
6573 return 0;
6574
6575 bio_put(bio);
6576out_err:
6577 dip->errors = 1;
6578 /*
6579 * before atomic variable goto zero, we must
6580 * make sure dip->errors is perceived to be set.
6581 */
6582 smp_mb__before_atomic_dec();
6583 if (atomic_dec_and_test(&dip->pending_bios))
6584 bio_io_error(dip->orig_bio);
6585
6586 /* bio_end_io() will handle error, so we needn't return it */
6587 return 0;
6588}
6589
Josef Bacik4b46fce2010-05-23 11:00:55 -04006590static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6591 loff_t file_offset)
6592{
6593 struct btrfs_root *root = BTRFS_I(inode)->root;
6594 struct btrfs_dio_private *dip;
6595 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006596 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006597 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006598 int ret = 0;
6599
6600 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6601
6602 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6603 if (!dip) {
6604 ret = -ENOMEM;
6605 goto free_ordered;
6606 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006607
6608 dip->private = bio->bi_private;
6609 dip->inode = inode;
6610 dip->logical_offset = file_offset;
6611
Josef Bacik4b46fce2010-05-23 11:00:55 -04006612 dip->bytes = 0;
6613 do {
6614 dip->bytes += bvec->bv_len;
6615 bvec++;
6616 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6617
Chris Mason46bfbb52010-05-26 11:04:10 -04006618 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006619 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006620 dip->errors = 0;
6621 dip->orig_bio = bio;
6622 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006623
6624 if (write)
6625 bio->bi_end_io = btrfs_endio_direct_write;
6626 else
6627 bio->bi_end_io = btrfs_endio_direct_read;
6628
Miao Xiee65e1532010-11-22 03:04:43 +00006629 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6630 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006631 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006632free_ordered:
6633 /*
6634 * If this is a write, we need to clean up the reserved space and kill
6635 * the ordered extent.
6636 */
6637 if (write) {
6638 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006639 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006640 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6641 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6642 btrfs_free_reserved_extent(root, ordered->start,
6643 ordered->disk_len);
6644 btrfs_put_ordered_extent(ordered);
6645 btrfs_put_ordered_extent(ordered);
6646 }
6647 bio_endio(bio, ret);
6648}
6649
Chris Mason5a5f79b2010-05-26 21:33:37 -04006650static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6651 const struct iovec *iov, loff_t offset,
6652 unsigned long nr_segs)
6653{
6654 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006655 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006656 size_t size;
6657 unsigned long addr;
6658 unsigned blocksize_mask = root->sectorsize - 1;
6659 ssize_t retval = -EINVAL;
6660 loff_t end = offset;
6661
6662 if (offset & blocksize_mask)
6663 goto out;
6664
6665 /* Check the memory alignment. Blocks cannot straddle pages */
6666 for (seg = 0; seg < nr_segs; seg++) {
6667 addr = (unsigned long)iov[seg].iov_base;
6668 size = iov[seg].iov_len;
6669 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006670 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006671 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006672
6673 /* If this is a write we don't need to check anymore */
6674 if (rw & WRITE)
6675 continue;
6676
6677 /*
6678 * Check to make sure we don't have duplicate iov_base's in this
6679 * iovec, if so return EINVAL, otherwise we'll get csum errors
6680 * when reading back.
6681 */
6682 for (i = seg + 1; i < nr_segs; i++) {
6683 if (iov[seg].iov_base == iov[i].iov_base)
6684 goto out;
6685 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006686 }
6687 retval = 0;
6688out:
6689 return retval;
6690}
Josef Bacikeb838e72012-07-31 16:28:48 -04006691
Chris Mason16432982008-04-10 10:23:21 -04006692static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6693 const struct iovec *iov, loff_t offset,
6694 unsigned long nr_segs)
6695{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006696 struct file *file = iocb->ki_filp;
6697 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006698
Chris Mason5a5f79b2010-05-26 21:33:37 -04006699 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006700 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006701 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006702
Josef Bacikeb838e72012-07-31 16:28:48 -04006703 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006704 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6705 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6706 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006707}
6708
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006709#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6710
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006711static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6712 __u64 start, __u64 len)
6713{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006714 int ret;
6715
6716 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6717 if (ret)
6718 return ret;
6719
Chris Masonec29ed52011-02-23 16:23:20 -05006720 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006721}
6722
Chris Mason9ebefb182007-06-15 13:50:00 -04006723int btrfs_readpage(struct file *file, struct page *page)
6724{
Chris Masond1310b22008-01-24 16:13:08 -05006725 struct extent_io_tree *tree;
6726 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006727 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006728}
Chris Mason1832a6d2007-12-21 16:27:21 -05006729
Chris Mason39279cc2007-06-12 06:35:45 -04006730static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6731{
Chris Masond1310b22008-01-24 16:13:08 -05006732 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006733
6734
6735 if (current->flags & PF_MEMALLOC) {
6736 redirty_page_for_writepage(wbc, page);
6737 unlock_page(page);
6738 return 0;
6739 }
Chris Masond1310b22008-01-24 16:13:08 -05006740 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006741 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6742}
Chris Mason39279cc2007-06-12 06:35:45 -04006743
Chris Masonf4219502008-07-22 11:18:09 -04006744int btrfs_writepages(struct address_space *mapping,
6745 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006746{
Chris Masond1310b22008-01-24 16:13:08 -05006747 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006748
Chris Masond1310b22008-01-24 16:13:08 -05006749 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006750 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6751}
6752
Chris Mason3ab2fb52007-11-08 10:59:22 -05006753static int
6754btrfs_readpages(struct file *file, struct address_space *mapping,
6755 struct list_head *pages, unsigned nr_pages)
6756{
Chris Masond1310b22008-01-24 16:13:08 -05006757 struct extent_io_tree *tree;
6758 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006759 return extent_readpages(tree, mapping, pages, nr_pages,
6760 btrfs_get_extent);
6761}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006762static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006763{
Chris Masond1310b22008-01-24 16:13:08 -05006764 struct extent_io_tree *tree;
6765 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006766 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006767
Chris Masond1310b22008-01-24 16:13:08 -05006768 tree = &BTRFS_I(page->mapping->host)->io_tree;
6769 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006770 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006771 if (ret == 1) {
6772 ClearPagePrivate(page);
6773 set_page_private(page, 0);
6774 page_cache_release(page);
6775 }
6776 return ret;
6777}
Chris Mason39279cc2007-06-12 06:35:45 -04006778
Chris Masone6dcd2d2008-07-17 12:53:50 -04006779static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6780{
Chris Mason98509cf2008-09-11 15:51:43 -04006781 if (PageWriteback(page) || PageDirty(page))
6782 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006783 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006784}
6785
Chris Masona52d9a82007-08-27 16:49:44 -04006786static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6787{
Josef Bacik5fd02042012-05-02 14:00:54 -04006788 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006789 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006790 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006791 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006792 u64 page_start = page_offset(page);
6793 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006794
Chris Mason8b62b722009-09-02 16:53:46 -04006795 /*
6796 * we have the page locked, so new writeback can't start,
6797 * and the dirty bit won't be cleared while we are here.
6798 *
6799 * Wait for IO on this page so that we can safely clear
6800 * the PagePrivate2 bit and do ordered accounting
6801 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006802 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006803
Josef Bacik5fd02042012-05-02 14:00:54 -04006804 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006805 if (offset) {
6806 btrfs_releasepage(page, GFP_NOFS);
6807 return;
6808 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006809 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00006810 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04006811 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006812 /*
6813 * IO on this page will never be started, so we need
6814 * to account for any ordered extents now
6815 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006816 clear_extent_bit(tree, page_start, page_end,
6817 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006818 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6819 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006820 /*
6821 * whoever cleared the private bit is responsible
6822 * for the finish_ordered_io
6823 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006824 if (TestClearPagePrivate2(page) &&
6825 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6826 PAGE_CACHE_SIZE, 1)) {
6827 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006828 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006829 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006830 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006831 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006832 }
6833 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006834 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006835 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6836 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006837 __btrfs_releasepage(page, GFP_NOFS);
6838
Chris Mason4a096752008-07-21 10:29:44 -04006839 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006840 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006841 ClearPagePrivate(page);
6842 set_page_private(page, 0);
6843 page_cache_release(page);
6844 }
Chris Mason39279cc2007-06-12 06:35:45 -04006845}
6846
Chris Mason9ebefb182007-06-15 13:50:00 -04006847/*
6848 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6849 * called from a page fault handler when a page is first dirtied. Hence we must
6850 * be careful to check for EOF conditions here. We set the page up correctly
6851 * for a written page which means we get ENOSPC checking when writing into
6852 * holes and correct delalloc and unwritten extent mapping on filesystems that
6853 * support these features.
6854 *
6855 * We are not allowed to take the i_mutex here so we have to play games to
6856 * protect against truncate races as the page could now be beyond EOF. Because
6857 * vmtruncate() writes the inode size before removing pages, once we have the
6858 * page lock we can determine safely if the page is beyond EOF. If it is not
6859 * beyond EOF, then the page is guaranteed safe against truncation until we
6860 * unlock the page.
6861 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006862int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006863{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006864 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006865 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006866 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006867 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6868 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006869 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006870 char *kaddr;
6871 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006872 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006873 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006874 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006875 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006876 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006877
Jan Karab2b5ef52012-06-12 16:20:45 +02006878 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006879 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006880 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006881 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006882 reserved = 1;
6883 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006884 if (ret) {
6885 if (ret == -ENOMEM)
6886 ret = VM_FAULT_OOM;
6887 else /* -ENOSPC, -EIO, etc */
6888 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006889 if (reserved)
6890 goto out;
6891 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006892 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006893
Nick Piggin56a76f82009-03-31 15:23:23 -07006894 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006895again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006896 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006897 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006898 page_start = page_offset(page);
6899 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006900
Chris Mason9ebefb182007-06-15 13:50:00 -04006901 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006902 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006903 /* page got truncated out from underneath us */
6904 goto out_unlock;
6905 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006906 wait_on_page_writeback(page);
6907
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006908 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006909 set_page_extent_mapped(page);
6910
Chris Masoneb84ae02008-07-17 13:53:27 -04006911 /*
6912 * we can't set the delalloc bits if there are pending ordered
6913 * extents. Drop our locks and wait for them to finish
6914 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006915 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6916 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006917 unlock_extent_cached(io_tree, page_start, page_end,
6918 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006919 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006920 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006921 btrfs_put_ordered_extent(ordered);
6922 goto again;
6923 }
6924
Josef Bacikfbf19082009-10-01 17:10:23 -04006925 /*
6926 * XXX - page_mkwrite gets called every time the page is dirtied, even
6927 * if it was already dirty, so for space accounting reasons we need to
6928 * clear any delalloc bits for the range we are fixing to save. There
6929 * is probably a better way to do this, but for now keep consistent with
6930 * prepare_pages in the normal write path.
6931 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006932 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006933 EXTENT_DIRTY | EXTENT_DELALLOC |
6934 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006935 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006936
Josef Bacik2ac55d42010-02-03 19:33:23 +00006937 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6938 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006939 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006940 unlock_extent_cached(io_tree, page_start, page_end,
6941 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006942 ret = VM_FAULT_SIGBUS;
6943 goto out_unlock;
6944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006945 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006946
6947 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006948 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006949 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006950 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006951 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006952
Chris Masone6dcd2d2008-07-17 12:53:50 -04006953 if (zero_start != PAGE_CACHE_SIZE) {
6954 kaddr = kmap(page);
6955 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6956 flush_dcache_page(page);
6957 kunmap(page);
6958 }
Chris Mason247e7432008-07-17 12:53:51 -04006959 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006960 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006961 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006962
Chris Mason257c62e2009-10-13 13:21:08 -04006963 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6964 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006965 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006966
Josef Bacik2ac55d42010-02-03 19:33:23 +00006967 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006968
6969out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006970 if (!ret) {
6971 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006972 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006973 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006974 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006975out:
Josef Bacikec39e182012-01-12 19:10:12 -05006976 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006977out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006978 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006979 return ret;
6980}
6981
Josef Bacika41ad392011-01-31 15:30:16 -05006982static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006983{
6984 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006985 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006986 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006987 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006988 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006989 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006990 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006991
Josef Bacik2aaa6652012-08-29 14:27:18 -04006992 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006993 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006994 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006995
Chris Mason4a096752008-07-21 10:29:44 -04006996 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006997 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006998
Josef Bacikfcb80c22011-05-03 10:40:22 -04006999 /*
7000 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7001 * 3 things going on here
7002 *
7003 * 1) We need to reserve space for our orphan item and the space to
7004 * delete our orphan item. Lord knows we don't want to have a dangling
7005 * orphan item because we didn't reserve space to remove it.
7006 *
7007 * 2) We need to reserve space to update our inode.
7008 *
7009 * 3) We need to have something to cache all the space that is going to
7010 * be free'd up by the truncate operation, but also have some slack
7011 * space reserved in case it uses space during the truncate (thank you
7012 * very much snapshotting).
7013 *
7014 * And we need these to all be seperate. The fact is we can use alot of
7015 * space doing the truncate, and we have no earthly idea how much space
7016 * we will use, so we need the truncate reservation to be seperate so it
7017 * doesn't end up using space reserved for updating the inode or
7018 * removing the orphan item. We also need to be able to stop the
7019 * transaction and start a new one, which means we need to be able to
7020 * update the inode several times, and we have no idea of knowing how
7021 * many times that will be, so we can't just reserve 1 item for the
7022 * entirety of the opration, so that has to be done seperately as well.
7023 * Then there is the orphan item, which does indeed need to be held on
7024 * to for the whole operation, and we need nobody to touch this reserved
7025 * space except the orphan code.
7026 *
7027 * So that leaves us with
7028 *
7029 * 1) root->orphan_block_rsv - for the orphan deletion.
7030 * 2) rsv - for the truncate reservation, which we will steal from the
7031 * transaction reservation.
7032 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7033 * updating the inode.
7034 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007035 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007036 if (!rsv)
7037 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007038 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007039 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007040
Josef Bacik907cbce2011-08-08 13:46:15 -04007041 /*
Josef Bacik07127182011-08-19 10:29:59 -04007042 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007043 * 1 for updating the inode.
7044 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007045 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007046 if (IS_ERR(trans)) {
7047 err = PTR_ERR(trans);
7048 goto out;
7049 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007050
Josef Bacik907cbce2011-08-08 13:46:15 -04007051 /* Migrate the slack space for the truncate to our reserve */
7052 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7053 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007054 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007055
Chris Mason5a3f23d2009-03-31 13:27:11 -04007056 /*
7057 * setattr is responsible for setting the ordered_data_close flag,
7058 * but that is only tested during the last file release. That
7059 * could happen well after the next commit, leaving a great big
7060 * window where new writes may get lost if someone chooses to write
7061 * to this file after truncating to zero
7062 *
7063 * The inode doesn't have any dirty data here, and so if we commit
7064 * this is a noop. If someone immediately starts writing to the inode
7065 * it is very likely we'll catch some of their writes in this
7066 * transaction, and the commit will find this file on the ordered
7067 * data list with good things to send down.
7068 *
7069 * This is a best effort solution, there is still a window where
7070 * using truncate to replace the contents of the file will
7071 * end up with a zero length file after a crash.
7072 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007073 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7074 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007075 btrfs_add_ordered_operation(trans, root, inode);
7076
Josef Bacik5dc562c2012-08-17 13:14:17 -04007077 /*
7078 * So if we truncate and then write and fsync we normally would just
7079 * write the extents that changed, which is a problem if we need to
7080 * first truncate that entire inode. So set this flag so we write out
7081 * all of the extents in the inode to the sync log so we're completely
7082 * safe.
7083 */
7084 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007085 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007086
Yan, Zheng80825102009-11-12 09:35:36 +00007087 while (1) {
7088 ret = btrfs_truncate_inode_items(trans, root, inode,
7089 inode->i_size,
7090 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007091 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007092 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007093 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007094 }
Chris Mason39279cc2007-06-12 06:35:45 -04007095
Josef Bacikfcb80c22011-05-03 10:40:22 -04007096 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007097 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007098 if (ret) {
7099 err = ret;
7100 break;
7101 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007102
Yan, Zheng80825102009-11-12 09:35:36 +00007103 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007104 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007105
7106 trans = btrfs_start_transaction(root, 2);
7107 if (IS_ERR(trans)) {
7108 ret = err = PTR_ERR(trans);
7109 trans = NULL;
7110 break;
7111 }
7112
7113 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7114 rsv, min_size);
7115 BUG_ON(ret); /* shouldn't happen */
7116 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007117 }
7118
7119 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007120 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007121 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007122 if (ret)
7123 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007124 }
7125
Chris Mason917c16b2011-11-08 14:49:59 -05007126 if (trans) {
7127 trans->block_rsv = &root->fs_info->trans_block_rsv;
7128 ret = btrfs_update_inode(trans, root, inode);
7129 if (ret && !err)
7130 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007131
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007132 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007133 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007134 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007135
7136out:
7137 btrfs_free_block_rsv(root, rsv);
7138
Josef Bacik3893e332011-01-31 16:03:11 -05007139 if (ret && !err)
7140 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007141
Josef Bacik3893e332011-01-31 16:03:11 -05007142 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007143}
7144
Sven Wegener3b963622008-06-09 21:57:42 -04007145/*
Chris Masond352ac62008-09-29 15:18:18 -04007146 * create a new subvolume directory/inode (helper for the ioctl).
7147 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007148int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007149 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007150{
Chris Mason39279cc2007-06-12 06:35:45 -04007151 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007152 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007153 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007154
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007155 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7156 new_dirid, new_dirid,
7157 S_IFDIR | (~current_umask() & S_IRWXUGO),
7158 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007159 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007160 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007161 inode->i_op = &btrfs_dir_inode_operations;
7162 inode->i_fop = &btrfs_dir_file_operations;
7163
Miklos Szeredibfe86842011-10-28 14:13:29 +02007164 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007165 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007166
Yan, Zheng76dda932009-09-21 16:00:26 -04007167 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007168
Yan, Zheng76dda932009-09-21 16:00:26 -04007169 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007170 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007171}
7172
Chris Mason39279cc2007-06-12 06:35:45 -04007173struct inode *btrfs_alloc_inode(struct super_block *sb)
7174{
7175 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007176 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007177
7178 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7179 if (!ei)
7180 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007181
7182 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007183 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007184 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007185 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007186 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007187 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007188 ei->disk_i_size = 0;
7189 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007190 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007191 ei->index_cnt = (u64)-1;
7192 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007193 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007194
Josef Bacik9e0baf62011-07-15 15:16:44 +00007195 spin_lock_init(&ei->lock);
7196 ei->outstanding_extents = 0;
7197 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007198
Josef Bacik72ac3c02012-05-23 14:13:11 -04007199 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007200 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007201
Miao Xie16cdcec2011-04-22 18:12:22 +08007202 ei->delayed_node = NULL;
7203
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007204 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007205 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007206 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7207 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007208 ei->io_tree.track_uptodate = 1;
7209 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007210 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007211 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007212 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007213 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007214 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007215 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007216 RB_CLEAR_NODE(&ei->rb_node);
7217
7218 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007219}
7220
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007221static void btrfs_i_callback(struct rcu_head *head)
7222{
7223 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007224 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7225}
7226
Chris Mason39279cc2007-06-12 06:35:45 -04007227void btrfs_destroy_inode(struct inode *inode)
7228{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007229 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007230 struct btrfs_root *root = BTRFS_I(inode)->root;
7231
Al Virob3d9b7a2012-06-09 13:51:19 -04007232 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007233 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007234 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7235 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007236 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7237 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007238
Chris Mason5a3f23d2009-03-31 13:27:11 -04007239 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007240 * This can happen where we create an inode, but somebody else also
7241 * created the same inode and we need to destroy the one we already
7242 * created.
7243 */
7244 if (!root)
7245 goto free;
7246
7247 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007248 * Make sure we're properly removed from the ordered operation
7249 * lists.
7250 */
7251 smp_mb();
7252 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7253 spin_lock(&root->fs_info->ordered_extent_lock);
7254 list_del_init(&BTRFS_I(inode)->ordered_operations);
7255 spin_unlock(&root->fs_info->ordered_extent_lock);
7256 }
7257
Josef Bacik8a35d952012-05-23 14:26:42 -04007258 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7259 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007260 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7261 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007262 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007263 }
Josef Bacik7b128762008-07-24 12:17:14 -04007264
Chris Masond3977122009-01-05 21:25:51 -05007265 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007266 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7267 if (!ordered)
7268 break;
7269 else {
Chris Masond3977122009-01-05 21:25:51 -05007270 printk(KERN_ERR "btrfs found ordered "
7271 "extent %llu %llu on inode cleanup\n",
7272 (unsigned long long)ordered->file_offset,
7273 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007274 btrfs_remove_ordered_extent(inode, ordered);
7275 btrfs_put_ordered_extent(ordered);
7276 btrfs_put_ordered_extent(ordered);
7277 }
7278 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007279 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007280 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007281free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007282 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007283 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007284}
7285
Al Viro45321ac2010-06-07 13:43:19 -04007286int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007287{
7288 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007289
Josef Bacik0af3d002010-06-21 14:48:16 -04007290 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007291 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007292 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007293 else
Al Viro45321ac2010-06-07 13:43:19 -04007294 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007295}
7296
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007297static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007298{
7299 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7300
7301 inode_init_once(&ei->vfs_inode);
7302}
7303
7304void btrfs_destroy_cachep(void)
7305{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007306 /*
7307 * Make sure all delayed rcu free inodes are flushed before we
7308 * destroy cache.
7309 */
7310 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007311 if (btrfs_inode_cachep)
7312 kmem_cache_destroy(btrfs_inode_cachep);
7313 if (btrfs_trans_handle_cachep)
7314 kmem_cache_destroy(btrfs_trans_handle_cachep);
7315 if (btrfs_transaction_cachep)
7316 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007317 if (btrfs_path_cachep)
7318 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007319 if (btrfs_free_space_cachep)
7320 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007321 if (btrfs_delalloc_work_cachep)
7322 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007323}
7324
7325int btrfs_init_cachep(void)
7326{
David Sterba837e1972012-09-07 03:00:48 -06007327 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007328 sizeof(struct btrfs_inode), 0,
7329 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007330 if (!btrfs_inode_cachep)
7331 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007332
David Sterba837e1972012-09-07 03:00:48 -06007333 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007334 sizeof(struct btrfs_trans_handle), 0,
7335 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007336 if (!btrfs_trans_handle_cachep)
7337 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007338
David Sterba837e1972012-09-07 03:00:48 -06007339 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007340 sizeof(struct btrfs_transaction), 0,
7341 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007342 if (!btrfs_transaction_cachep)
7343 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007344
David Sterba837e1972012-09-07 03:00:48 -06007345 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007346 sizeof(struct btrfs_path), 0,
7347 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007348 if (!btrfs_path_cachep)
7349 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007350
David Sterba837e1972012-09-07 03:00:48 -06007351 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007352 sizeof(struct btrfs_free_space), 0,
7353 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7354 if (!btrfs_free_space_cachep)
7355 goto fail;
7356
Miao Xie8ccf6f192012-10-25 09:28:04 +00007357 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7358 sizeof(struct btrfs_delalloc_work), 0,
7359 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7360 NULL);
7361 if (!btrfs_delalloc_work_cachep)
7362 goto fail;
7363
Chris Mason39279cc2007-06-12 06:35:45 -04007364 return 0;
7365fail:
7366 btrfs_destroy_cachep();
7367 return -ENOMEM;
7368}
7369
7370static int btrfs_getattr(struct vfsmount *mnt,
7371 struct dentry *dentry, struct kstat *stat)
7372{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007373 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007374 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007375 u32 blocksize = inode->i_sb->s_blocksize;
7376
Chris Mason39279cc2007-06-12 06:35:45 -04007377 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007378 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007379 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007380
7381 spin_lock(&BTRFS_I(inode)->lock);
7382 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7383 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007384 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007385 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007386 return 0;
7387}
7388
Liu Bo75e7cb72011-03-22 10:12:20 +00007389/*
7390 * If a file is moved, it will inherit the cow and compression flags of the new
7391 * directory.
7392 */
7393static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7394{
7395 struct btrfs_inode *b_dir = BTRFS_I(dir);
7396 struct btrfs_inode *b_inode = BTRFS_I(inode);
7397
7398 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7399 b_inode->flags |= BTRFS_INODE_NODATACOW;
7400 else
7401 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7402
Liu Bobc178232012-06-14 02:23:18 -06007403 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007404 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007405 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7406 } else {
7407 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7408 BTRFS_INODE_NOCOMPRESS);
7409 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007410}
7411
Chris Masond3977122009-01-05 21:25:51 -05007412static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7413 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007414{
7415 struct btrfs_trans_handle *trans;
7416 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007417 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007418 struct inode *new_inode = new_dentry->d_inode;
7419 struct inode *old_inode = old_dentry->d_inode;
7420 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007421 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007422 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007423 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007424 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007425
Li Zefan33345d012011-04-20 10:31:50 +08007426 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007427 return -EPERM;
7428
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007429 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007430 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007431 return -EXDEV;
7432
Li Zefan33345d012011-04-20 10:31:50 +08007433 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7434 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007435 return -ENOTEMPTY;
7436
Chris Mason39279cc2007-06-12 06:35:45 -04007437 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007438 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007439 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007440
7441
7442 /* check for collisions, even if the name isn't there */
7443 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7444 new_dentry->d_name.name,
7445 new_dentry->d_name.len);
7446
7447 if (ret) {
7448 if (ret == -EEXIST) {
7449 /* we shouldn't get
7450 * eexist without a new_inode */
7451 if (!new_inode) {
7452 WARN_ON(1);
7453 return ret;
7454 }
7455 } else {
7456 /* maybe -EOVERFLOW */
7457 return ret;
7458 }
7459 }
7460 ret = 0;
7461
Chris Mason5a3f23d2009-03-31 13:27:11 -04007462 /*
7463 * we're using rename to replace one file with another.
7464 * and the replacement file is large. Start IO on it now so
7465 * we don't add too much work to the end of the transaction
7466 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007467 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007468 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7469 filemap_flush(old_inode->i_mapping);
7470
Yan, Zheng76dda932009-09-21 16:00:26 -04007471 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007472 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007473 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007474 /*
7475 * We want to reserve the absolute worst case amount of items. So if
7476 * both inodes are subvols and we need to unlink them then that would
7477 * require 4 item modifications, but if they are both normal inodes it
7478 * would require 5 item modifications, so we'll assume their normal
7479 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7480 * should cover the worst case number of items we'll modify.
7481 */
7482 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007483 if (IS_ERR(trans)) {
7484 ret = PTR_ERR(trans);
7485 goto out_notrans;
7486 }
Chris Mason5f39d392007-10-15 16:14:19 -04007487
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007488 if (dest != root)
7489 btrfs_record_root_in_trans(trans, dest);
7490
Yan, Zhenga5719522009-09-24 09:17:31 -04007491 ret = btrfs_set_inode_index(new_dir, &index);
7492 if (ret)
7493 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007494
Li Zefan33345d012011-04-20 10:31:50 +08007495 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007496 /* force full log commit if subvolume involved. */
7497 root->fs_info->last_trans_log_full_commit = trans->transid;
7498 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007499 ret = btrfs_insert_inode_ref(trans, dest,
7500 new_dentry->d_name.name,
7501 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007502 old_ino,
7503 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007504 if (ret)
7505 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007506 /*
7507 * this is an ugly little race, but the rename is required
7508 * to make sure that if we crash, the inode is either at the
7509 * old name or the new one. pinning the log transaction lets
7510 * us make sure we don't allow a log commit to come in after
7511 * we unlink the name but before we add the new name back in.
7512 */
7513 btrfs_pin_log_trans(root);
7514 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007515 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007516 * make sure the inode gets flushed if it is replacing
7517 * something.
7518 */
Li Zefan33345d012011-04-20 10:31:50 +08007519 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007520 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007521
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007522 inode_inc_iversion(old_dir);
7523 inode_inc_iversion(new_dir);
7524 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007525 old_dir->i_ctime = old_dir->i_mtime = ctime;
7526 new_dir->i_ctime = new_dir->i_mtime = ctime;
7527 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007528
Chris Mason12fcfd22009-03-24 10:24:20 -04007529 if (old_dentry->d_parent != new_dentry->d_parent)
7530 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7531
Li Zefan33345d012011-04-20 10:31:50 +08007532 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007533 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7534 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7535 old_dentry->d_name.name,
7536 old_dentry->d_name.len);
7537 } else {
Al Viro92986792011-03-04 17:14:37 +00007538 ret = __btrfs_unlink_inode(trans, root, old_dir,
7539 old_dentry->d_inode,
7540 old_dentry->d_name.name,
7541 old_dentry->d_name.len);
7542 if (!ret)
7543 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007544 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007545 if (ret) {
7546 btrfs_abort_transaction(trans, root, ret);
7547 goto out_fail;
7548 }
Chris Mason39279cc2007-06-12 06:35:45 -04007549
7550 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007551 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007552 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007553 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007554 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7555 root_objectid = BTRFS_I(new_inode)->location.objectid;
7556 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7557 root_objectid,
7558 new_dentry->d_name.name,
7559 new_dentry->d_name.len);
7560 BUG_ON(new_inode->i_nlink == 0);
7561 } else {
7562 ret = btrfs_unlink_inode(trans, dest, new_dir,
7563 new_dentry->d_inode,
7564 new_dentry->d_name.name,
7565 new_dentry->d_name.len);
7566 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007567 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007568 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007569 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007570 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007571 if (ret) {
7572 btrfs_abort_transaction(trans, root, ret);
7573 goto out_fail;
7574 }
Chris Mason39279cc2007-06-12 06:35:45 -04007575 }
Josef Bacikaec74772008-07-24 12:12:38 -04007576
Liu Bo75e7cb72011-03-22 10:12:20 +00007577 fixup_inode_flags(new_dir, old_inode);
7578
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007579 ret = btrfs_add_link(trans, new_dir, old_inode,
7580 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007581 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007582 if (ret) {
7583 btrfs_abort_transaction(trans, root, ret);
7584 goto out_fail;
7585 }
Chris Mason39279cc2007-06-12 06:35:45 -04007586
Li Zefan33345d012011-04-20 10:31:50 +08007587 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007588 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007589 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007590 btrfs_end_log_trans(root);
7591 }
Chris Mason39279cc2007-06-12 06:35:45 -04007592out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007593 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007594out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007595 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007596 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007597
Chris Mason39279cc2007-06-12 06:35:45 -04007598 return ret;
7599}
7600
Miao Xie8ccf6f192012-10-25 09:28:04 +00007601static void btrfs_run_delalloc_work(struct btrfs_work *work)
7602{
7603 struct btrfs_delalloc_work *delalloc_work;
7604
7605 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7606 work);
7607 if (delalloc_work->wait)
7608 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7609 else
7610 filemap_flush(delalloc_work->inode->i_mapping);
7611
7612 if (delalloc_work->delay_iput)
7613 btrfs_add_delayed_iput(delalloc_work->inode);
7614 else
7615 iput(delalloc_work->inode);
7616 complete(&delalloc_work->completion);
7617}
7618
7619struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7620 int wait, int delay_iput)
7621{
7622 struct btrfs_delalloc_work *work;
7623
7624 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7625 if (!work)
7626 return NULL;
7627
7628 init_completion(&work->completion);
7629 INIT_LIST_HEAD(&work->list);
7630 work->inode = inode;
7631 work->wait = wait;
7632 work->delay_iput = delay_iput;
7633 work->work.func = btrfs_run_delalloc_work;
7634
7635 return work;
7636}
7637
7638void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7639{
7640 wait_for_completion(&work->completion);
7641 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7642}
7643
Chris Masond352ac62008-09-29 15:18:18 -04007644/*
7645 * some fairly slow code that needs optimization. This walks the list
7646 * of all the inodes with pending delalloc and forces them to disk.
7647 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007648int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007649{
Chris Masonea8c2812008-08-04 23:17:27 -04007650 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007651 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007652 struct btrfs_delalloc_work *work, *next;
7653 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00007654 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007655 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007656
Yan Zhengc146afa2008-11-12 14:34:12 -05007657 if (root->fs_info->sb->s_flags & MS_RDONLY)
7658 return -EROFS;
7659
Miao Xie8ccf6f192012-10-25 09:28:04 +00007660 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007661 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00007662
Chris Mason75eff682008-12-15 15:54:40 -05007663 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007664 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7665 while (!list_empty(&splice)) {
7666 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04007667 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007668
7669 list_del_init(&binode->delalloc_inodes);
7670
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007671 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00007672 if (!inode) {
7673 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
7674 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007675 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007676 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007677
7678 list_add_tail(&binode->delalloc_inodes,
7679 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007680 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007681
7682 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7683 if (unlikely(!work)) {
7684 ret = -ENOMEM;
7685 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007686 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007687 list_add_tail(&work->list, &works);
7688 btrfs_queue_worker(&root->fs_info->flush_workers,
7689 &work->work);
7690
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007691 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007692 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007693 }
Chris Mason75eff682008-12-15 15:54:40 -05007694 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007695
Miao Xie1eafa6c2013-01-22 10:49:00 +00007696 list_for_each_entry_safe(work, next, &works, list) {
7697 list_del_init(&work->list);
7698 btrfs_wait_and_free_delalloc_work(work);
7699 }
7700
Chris Mason8c8bee12008-09-29 11:19:10 -04007701 /* the filemap_flush will queue IO into the worker threads, but
7702 * we have to make sure the IO is actually started and that
7703 * ordered extents get created before we return
7704 */
7705 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007706 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007707 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007708 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007709 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7710 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007711 }
7712 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007713 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007714out:
7715 list_for_each_entry_safe(work, next, &works, list) {
7716 list_del_init(&work->list);
7717 btrfs_wait_and_free_delalloc_work(work);
7718 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007719
7720 if (!list_empty_careful(&splice)) {
7721 spin_lock(&root->fs_info->delalloc_lock);
7722 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7723 spin_unlock(&root->fs_info->delalloc_lock);
7724 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00007725 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007726}
7727
Chris Mason39279cc2007-06-12 06:35:45 -04007728static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7729 const char *symname)
7730{
7731 struct btrfs_trans_handle *trans;
7732 struct btrfs_root *root = BTRFS_I(dir)->root;
7733 struct btrfs_path *path;
7734 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007735 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007736 int err;
7737 int drop_inode = 0;
7738 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007739 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007740 int name_len;
7741 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007742 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007743 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007744 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007745
7746 name_len = strlen(symname) + 1;
7747 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7748 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007749
Josef Bacik9ed74f22009-09-11 16:12:44 -04007750 /*
7751 * 2 items for inode item and ref
7752 * 2 items for dir items
7753 * 1 item for xattr if selinux is on
7754 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007755 trans = btrfs_start_transaction(root, 5);
7756 if (IS_ERR(trans))
7757 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007758
Li Zefan581bb052011-04-20 10:06:11 +08007759 err = btrfs_find_free_ino(root, &objectid);
7760 if (err)
7761 goto out_unlock;
7762
Josef Bacikaec74772008-07-24 12:12:38 -04007763 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007764 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007765 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007766 if (IS_ERR(inode)) {
7767 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007768 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007769 }
Chris Mason39279cc2007-06-12 06:35:45 -04007770
Eric Paris2a7dba32011-02-01 11:05:39 -05007771 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007772 if (err) {
7773 drop_inode = 1;
7774 goto out_unlock;
7775 }
7776
Casey Schauflerad19db72011-12-15 10:09:07 -05007777 /*
7778 * If the active LSM wants to access the inode during
7779 * d_instantiate it needs these. Smack checks to see
7780 * if the filesystem supports xattrs by looking at the
7781 * ops vector.
7782 */
7783 inode->i_fop = &btrfs_file_operations;
7784 inode->i_op = &btrfs_file_inode_operations;
7785
Josef Bacika1b075d2010-11-19 20:36:11 +00007786 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007787 if (err)
7788 drop_inode = 1;
7789 else {
7790 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007791 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007792 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007793 }
Chris Mason39279cc2007-06-12 06:35:45 -04007794 if (drop_inode)
7795 goto out_unlock;
7796
7797 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007798 if (!path) {
7799 err = -ENOMEM;
7800 drop_inode = 1;
7801 goto out_unlock;
7802 }
Li Zefan33345d012011-04-20 10:31:50 +08007803 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007804 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007805 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7806 datasize = btrfs_file_extent_calc_inline_size(name_len);
7807 err = btrfs_insert_empty_item(trans, root, path, &key,
7808 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007809 if (err) {
7810 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007811 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007812 goto out_unlock;
7813 }
Chris Mason5f39d392007-10-15 16:14:19 -04007814 leaf = path->nodes[0];
7815 ei = btrfs_item_ptr(leaf, path->slots[0],
7816 struct btrfs_file_extent_item);
7817 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7818 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007819 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007820 btrfs_set_file_extent_encryption(leaf, ei, 0);
7821 btrfs_set_file_extent_compression(leaf, ei, 0);
7822 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7823 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7824
Chris Mason39279cc2007-06-12 06:35:45 -04007825 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007826 write_extent_buffer(leaf, symname, ptr, name_len);
7827 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007828 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007829
Chris Mason39279cc2007-06-12 06:35:45 -04007830 inode->i_op = &btrfs_symlink_inode_operations;
7831 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007832 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007833 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007834 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007835 err = btrfs_update_inode(trans, root, inode);
7836 if (err)
7837 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007838
7839out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007840 if (!err)
7841 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007842 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007843 if (drop_inode) {
7844 inode_dec_link_count(inode);
7845 iput(inode);
7846 }
Liu Bob53d3f52012-11-14 14:34:34 +00007847 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007848 return err;
7849}
Chris Mason16432982008-04-10 10:23:21 -04007850
Josef Bacik0af3d002010-06-21 14:48:16 -04007851static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7852 u64 start, u64 num_bytes, u64 min_size,
7853 loff_t actual_len, u64 *alloc_hint,
7854 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007855{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007856 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7857 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007858 struct btrfs_root *root = BTRFS_I(inode)->root;
7859 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007860 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007861 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007862 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007863 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007864
Josef Bacik0af3d002010-06-21 14:48:16 -04007865 if (trans)
7866 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007867 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007868 if (own_trans) {
7869 trans = btrfs_start_transaction(root, 3);
7870 if (IS_ERR(trans)) {
7871 ret = PTR_ERR(trans);
7872 break;
7873 }
Yan Zhengd899e052008-10-30 14:25:28 -04007874 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007875
Yan, Zhengefa56462010-05-16 10:49:59 -04007876 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007877 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007878 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007879 if (own_trans)
7880 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007881 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007882 }
7883
Yan Zhengd899e052008-10-30 14:25:28 -04007884 ret = insert_reserved_file_extent(trans, inode,
7885 cur_offset, ins.objectid,
7886 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007887 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007888 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007889 if (ret) {
7890 btrfs_abort_transaction(trans, root, ret);
7891 if (own_trans)
7892 btrfs_end_transaction(trans, root);
7893 break;
7894 }
Chris Masona1ed8352009-09-11 12:27:37 -04007895 btrfs_drop_extent_cache(inode, cur_offset,
7896 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007897
Josef Bacik5dc562c2012-08-17 13:14:17 -04007898 em = alloc_extent_map();
7899 if (!em) {
7900 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7901 &BTRFS_I(inode)->runtime_flags);
7902 goto next;
7903 }
7904
7905 em->start = cur_offset;
7906 em->orig_start = cur_offset;
7907 em->len = ins.offset;
7908 em->block_start = ins.objectid;
7909 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007910 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007911 em->bdev = root->fs_info->fs_devices->latest_bdev;
7912 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7913 em->generation = trans->transid;
7914
7915 while (1) {
7916 write_lock(&em_tree->lock);
7917 ret = add_extent_mapping(em_tree, em);
7918 if (!ret)
7919 list_move(&em->list,
7920 &em_tree->modified_extents);
7921 write_unlock(&em_tree->lock);
7922 if (ret != -EEXIST)
7923 break;
7924 btrfs_drop_extent_cache(inode, cur_offset,
7925 cur_offset + ins.offset - 1,
7926 0);
7927 }
7928 free_extent_map(em);
7929next:
Yan Zhengd899e052008-10-30 14:25:28 -04007930 num_bytes -= ins.offset;
7931 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007932 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007933
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007934 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007935 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007936 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007937 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007938 (actual_len > inode->i_size) &&
7939 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007940 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007941 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007942 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007943 i_size = cur_offset;
7944 i_size_write(inode, i_size);
7945 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007946 }
7947
Yan Zhengd899e052008-10-30 14:25:28 -04007948 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007949
7950 if (ret) {
7951 btrfs_abort_transaction(trans, root, ret);
7952 if (own_trans)
7953 btrfs_end_transaction(trans, root);
7954 break;
7955 }
Yan Zhengd899e052008-10-30 14:25:28 -04007956
Josef Bacik0af3d002010-06-21 14:48:16 -04007957 if (own_trans)
7958 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007959 }
Yan Zhengd899e052008-10-30 14:25:28 -04007960 return ret;
7961}
7962
Josef Bacik0af3d002010-06-21 14:48:16 -04007963int btrfs_prealloc_file_range(struct inode *inode, int mode,
7964 u64 start, u64 num_bytes, u64 min_size,
7965 loff_t actual_len, u64 *alloc_hint)
7966{
7967 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7968 min_size, actual_len, alloc_hint,
7969 NULL);
7970}
7971
7972int btrfs_prealloc_file_range_trans(struct inode *inode,
7973 struct btrfs_trans_handle *trans, int mode,
7974 u64 start, u64 num_bytes, u64 min_size,
7975 loff_t actual_len, u64 *alloc_hint)
7976{
7977 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7978 min_size, actual_len, alloc_hint, trans);
7979}
7980
Chris Masone6dcd2d2008-07-17 12:53:50 -04007981static int btrfs_set_page_dirty(struct page *page)
7982{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007983 return __set_page_dirty_nobuffers(page);
7984}
7985
Al Viro10556cb22011-06-20 19:28:19 -04007986static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007987{
Li Zefanb83cc962010-12-20 16:04:08 +08007988 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007989 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007990
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007991 if (mask & MAY_WRITE &&
7992 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7993 if (btrfs_root_readonly(root))
7994 return -EROFS;
7995 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7996 return -EACCES;
7997 }
Al Viro2830ba72011-06-20 19:16:29 -04007998 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007999}
Chris Mason39279cc2007-06-12 06:35:45 -04008000
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008001static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008002 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008003 .lookup = btrfs_lookup,
8004 .create = btrfs_create,
8005 .unlink = btrfs_unlink,
8006 .link = btrfs_link,
8007 .mkdir = btrfs_mkdir,
8008 .rmdir = btrfs_rmdir,
8009 .rename = btrfs_rename,
8010 .symlink = btrfs_symlink,
8011 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008012 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008013 .setxattr = btrfs_setxattr,
8014 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008015 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008016 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008017 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008018 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008019};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008020static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008021 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008022 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008023 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008024};
Yan, Zheng76dda932009-09-21 16:00:26 -04008025
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008026static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008027 .llseek = generic_file_llseek,
8028 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008029 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008030 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008031#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008032 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008033#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008034 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008035 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008036};
8037
Chris Masond1310b22008-01-24 16:13:08 -05008038static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008039 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008040 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008041 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008042 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008043 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008044 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008045 .set_bit_hook = btrfs_set_bit_hook,
8046 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008047 .merge_extent_hook = btrfs_merge_extent_hook,
8048 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008049};
8050
Chris Mason35054392009-01-21 13:11:13 -05008051/*
8052 * btrfs doesn't support the bmap operation because swapfiles
8053 * use bmap to make a mapping of extents in the file. They assume
8054 * these extents won't change over the life of the file and they
8055 * use the bmap result to do IO directly to the drive.
8056 *
8057 * the btrfs bmap call would return logical addresses that aren't
8058 * suitable for IO and they also will change frequently as COW
8059 * operations happen. So, swapfile + btrfs == corruption.
8060 *
8061 * For now we're avoiding this by dropping bmap.
8062 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008063static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008064 .readpage = btrfs_readpage,
8065 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008066 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008067 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008068 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008069 .invalidatepage = btrfs_invalidatepage,
8070 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008071 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008072 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008073};
8074
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008075static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008076 .readpage = btrfs_readpage,
8077 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008078 .invalidatepage = btrfs_invalidatepage,
8079 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008080};
8081
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008082static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008083 .getattr = btrfs_getattr,
8084 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008085 .setxattr = btrfs_setxattr,
8086 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008087 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008088 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008089 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008090 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008091 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008092 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008093};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008094static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008095 .getattr = btrfs_getattr,
8096 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008097 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008098 .setxattr = btrfs_setxattr,
8099 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008100 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008101 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008102 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008103 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008104};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008105static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008106 .readlink = generic_readlink,
8107 .follow_link = page_follow_link_light,
8108 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008109 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008110 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008111 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008112 .setxattr = btrfs_setxattr,
8113 .getxattr = btrfs_getxattr,
8114 .listxattr = btrfs_listxattr,
8115 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008116 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008117 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008118};
Yan, Zheng76dda932009-09-21 16:00:26 -04008119
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008120const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008121 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008122 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008123};