blob: a2df4690b000d852efdb680d6f75510f36d407dd [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>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040059
60struct btrfs_iget_args {
61 u64 ino;
62 struct btrfs_root *root;
63};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050073static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000076static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050080struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
82#define S_SHIFT 12
83static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
84 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
85 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
86 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
87 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
88 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
89 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
90 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
91};
92
Eric Sandeen3972f262013-01-12 02:57:22 +000093static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050094static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040095static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050096static noinline int cow_file_range(struct inode *inode,
97 struct page *locked_page,
98 u64 start, u64 end, int *page_started,
99 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400100static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
101 u64 len, u64 orig_start,
102 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400103 u64 orig_block_len, u64 ram_bytes,
104 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400105
Eric Sandeen48a3b632013-04-25 20:41:01 +0000106static int btrfs_dirty_inode(struct inode *inode);
107
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000108static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500109 struct inode *inode, struct inode *dir,
110 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500111{
112 int err;
113
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000114 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500115 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500116 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500117 return err;
118}
119
Chris Masond352ac62008-09-29 15:18:18 -0400120/*
Chris Masonc8b97812008-10-29 14:49:59 -0400121 * this does all the hard work for inserting an inline extent into
122 * the btree. The caller should have done a btrfs_drop_extents so that
123 * no overlapping inline items exist in the btree
124 */
Chris Masond3977122009-01-05 21:25:51 -0500125static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400126 struct btrfs_root *root, struct inode *inode,
127 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000128 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct page **compressed_pages)
130{
131 struct btrfs_key key;
132 struct btrfs_path *path;
133 struct extent_buffer *leaf;
134 struct page *page = NULL;
135 char *kaddr;
136 unsigned long ptr;
137 struct btrfs_file_extent_item *ei;
138 int err = 0;
139 int ret;
140 size_t cur_size = size;
141 size_t datasize;
142 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Li Zefanfe3f5662011-03-28 08:30:38 +0000144 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400145 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Chris Masond3977122009-01-05 21:25:51 -0500147 path = btrfs_alloc_path();
148 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400149 return -ENOMEM;
150
Chris Masonb9473432009-03-13 11:00:37 -0400151 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400152
Li Zefan33345d012011-04-20 10:31:50 +0800153 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400154 key.offset = start;
155 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 datasize = btrfs_file_extent_calc_inline_size(cur_size);
157
158 inode_add_bytes(inode, size);
159 ret = btrfs_insert_empty_item(trans, root, path, &key,
160 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400161 if (ret) {
162 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400163 goto fail;
164 }
165 leaf = path->nodes[0];
166 ei = btrfs_item_ptr(leaf, path->slots[0],
167 struct btrfs_file_extent_item);
168 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
169 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
170 btrfs_set_file_extent_encryption(leaf, ei, 0);
171 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
172 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
173 ptr = btrfs_file_extent_inline_start(ei);
174
Li Zefan261507a02010-12-17 14:21:50 +0800175 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400176 struct page *cpage;
177 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500178 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500180 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400181 PAGE_CACHE_SIZE);
182
Cong Wang7ac687d2011-11-25 23:14:28 +0800183 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400184 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400186
187 i++;
188 ptr += cur_size;
189 compressed_size -= cur_size;
190 }
191 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800192 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 } else {
194 page = find_get_page(inode->i_mapping,
195 start >> PAGE_CACHE_SHIFT);
196 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800197 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400198 offset = start & (PAGE_CACHE_SIZE - 1);
199 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 page_cache_release(page);
202 }
203 btrfs_mark_buffer_dirty(leaf);
204 btrfs_free_path(path);
205
Yan, Zhengc2167752009-11-12 09:34:21 +0000206 /*
207 * we're an inline extent, so nobody can
208 * extend the file past i_size without locking
209 * a page we already have locked.
210 *
211 * We must do any isize and inode updates
212 * before we unlock the pages. Otherwise we
213 * could end up racing with unlink.
214 */
Chris Masonc8b97812008-10-29 14:49:59 -0400215 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100216 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000217
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400219fail:
220 btrfs_free_path(path);
221 return err;
222}
223
224
225/*
226 * conditionally insert an inline extent into the file. This
227 * does the checks required to make sure the data is small enough
228 * to fit as an inline extent.
229 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400230static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400231 struct btrfs_root *root,
232 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000233 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400234 struct page **compressed_pages)
235{
236 u64 isize = i_size_read(inode);
237 u64 actual_end = min(end + 1, isize);
238 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000239 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 u64 data_len = inline_len;
241 int ret;
242
243 if (compressed_size)
244 data_len = compressed_size;
245
246 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400247 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400248 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
249 (!compressed_size &&
250 (actual_end & (root->sectorsize - 1)) == 0) ||
251 end + 1 < isize ||
252 data_len > root->fs_info->max_inline) {
253 return 1;
254 }
255
Josef Bacik26714852012-08-29 12:24:27 -0400256 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100257 if (ret)
258 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400259
260 if (isize > actual_end)
261 inline_len = min_t(u64, isize, actual_end);
262 ret = insert_inline_extent(trans, root, inode, start,
263 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000264 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 btrfs_abort_transaction(trans, root, ret);
267 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400268 } else if (ret == -ENOSPC) {
269 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100270 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400271
Josef Bacikbdc20e62013-02-28 13:23:38 -0500272 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400273 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400274 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 0;
276}
277
Chris Mason771ed682008-11-06 22:02:51 -0500278struct async_extent {
279 u64 start;
280 u64 ram_size;
281 u64 compressed_size;
282 struct page **pages;
283 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800284 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500285 struct list_head list;
286};
287
288struct async_cow {
289 struct inode *inode;
290 struct btrfs_root *root;
291 struct page *locked_page;
292 u64 start;
293 u64 end;
294 struct list_head extents;
295 struct btrfs_work work;
296};
297
298static noinline int add_async_extent(struct async_cow *cow,
299 u64 start, u64 ram_size,
300 u64 compressed_size,
301 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800302 unsigned long nr_pages,
303 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500304{
305 struct async_extent *async_extent;
306
307 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100308 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500309 async_extent->start = start;
310 async_extent->ram_size = ram_size;
311 async_extent->compressed_size = compressed_size;
312 async_extent->pages = pages;
313 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800314 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500315 list_add_tail(&async_extent->list, &cow->extents);
316 return 0;
317}
318
Chris Masonc8b97812008-10-29 14:49:59 -0400319/*
Chris Mason771ed682008-11-06 22:02:51 -0500320 * we create compressed extents in two phases. The first
321 * phase compresses a range of pages that have already been
322 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * This is done inside an ordered work queue, and the compression
325 * is spread across many cpus. The actual IO submission is step
326 * two, and the ordered work queue takes care of making sure that
327 * happens in the same order things were put onto the queue by
328 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400329 *
Chris Mason771ed682008-11-06 22:02:51 -0500330 * If this code finds it can't get good compression, it puts an
331 * entry onto the work queue to write the uncompressed bytes. This
332 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300333 * are written in the same order that the flusher thread sent them
334 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400335 */
Chris Mason771ed682008-11-06 22:02:51 -0500336static noinline int compress_file_range(struct inode *inode,
337 struct page *locked_page,
338 u64 start, u64 end,
339 struct async_cow *async_cow,
340 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400341{
342 struct btrfs_root *root = BTRFS_I(inode)->root;
343 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400344 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400345 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500347 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400348 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 struct page **pages = NULL;
350 unsigned long nr_pages;
351 unsigned long nr_pages_ret = 0;
352 unsigned long total_compressed = 0;
353 unsigned long total_in = 0;
354 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500355 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400356 int i;
357 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800358 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400359 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400360
Liu Bo4cb13e52012-03-29 09:57:45 -0400361 /* if this is a small write inside eof, kick off a defrag */
362 if ((end - start + 1) < 16 * 1024 &&
363 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400364 btrfs_add_inode_defrag(NULL, inode);
365
Chris Mason42dc7ba2008-12-15 11:44:56 -0500366 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400367again:
368 will_compress = 0;
369 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
370 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
371
Chris Masonf03d9301f2009-02-04 09:31:06 -0500372 /*
373 * we don't want to send crud past the end of i_size through
374 * compression, that's just a waste of CPU time. So, if the
375 * end of the file is before the start of our current
376 * requested range of bytes, we bail out to the uncompressed
377 * cleanup code that can deal with all of this.
378 *
379 * It isn't really the fastest way to fix things, but this is a
380 * very uncommon corner.
381 */
382 if (actual_end <= start)
383 goto cleanup_and_bail_uncompressed;
384
Chris Masonc8b97812008-10-29 14:49:59 -0400385 total_compressed = actual_end - start;
386
387 /* we want to make sure that amount of ram required to uncompress
388 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500389 * of a compressed extent to 128k. This is a crucial number
390 * because it also controls how easily we can spread reads across
391 * cpus for decompression.
392 *
393 * We also want to make sure the amount of IO required to do
394 * a random read is reasonably small, so we limit the size of
395 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400396 */
397 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000398 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500399 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400400 total_in = 0;
401 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400402
Chris Mason771ed682008-11-06 22:02:51 -0500403 /*
404 * we do compression for mount -o compress and when the
405 * inode has not been flagged as nocompress. This flag can
406 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400407 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200408 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500409 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000410 (BTRFS_I(inode)->force_compress) ||
411 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400412 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400413 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800414 if (!pages) {
415 /* just bail out to the uncompressed code */
416 goto cont;
417 }
Chris Mason179e29e2007-11-01 11:28:41 -0400418
Li Zefan261507a02010-12-17 14:21:50 +0800419 if (BTRFS_I(inode)->force_compress)
420 compress_type = BTRFS_I(inode)->force_compress;
421
Chris Mason4adaa612013-03-26 13:07:00 -0400422 /*
423 * we need to call clear_page_dirty_for_io on each
424 * page in the range. Otherwise applications with the file
425 * mmap'd can wander in and change the page contents while
426 * we are compressing them.
427 *
428 * If the compression fails for any reason, we set the pages
429 * dirty again later on.
430 */
431 extent_range_clear_dirty_for_io(inode, start, end);
432 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800433 ret = btrfs_compress_pages(compress_type,
434 inode->i_mapping, start,
435 total_compressed, pages,
436 nr_pages, &nr_pages_ret,
437 &total_in,
438 &total_compressed,
439 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400440
441 if (!ret) {
442 unsigned long offset = total_compressed &
443 (PAGE_CACHE_SIZE - 1);
444 struct page *page = pages[nr_pages_ret - 1];
445 char *kaddr;
446
447 /* zero the tail end of the last page, we might be
448 * sending it down to disk
449 */
450 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800451 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400452 memset(kaddr + offset, 0,
453 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800454 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400455 }
456 will_compress = 1;
457 }
458 }
Li Zefan560f7d72011-09-08 10:22:01 +0800459cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400460 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400461 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100462 if (IS_ERR(trans)) {
463 ret = PTR_ERR(trans);
464 trans = NULL;
465 goto cleanup_and_out;
466 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400467 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500468
Chris Masonc8b97812008-10-29 14:49:59 -0400469 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500470 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400471 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500472 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 */
474 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000475 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400476 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500477 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400478 ret = cow_file_range_inline(trans, root, inode,
479 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000480 total_compressed,
481 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100483 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500484 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100485 * inline extent creation worked or returned error,
486 * we don't need to create any more async work items.
487 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500488 */
Chris Masonc8b97812008-10-29 14:49:59 -0400489 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400490 &BTRFS_I(inode)->io_tree,
491 start, end, NULL,
492 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400493 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400494 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000495
496 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 goto free_pages_out;
498 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000499 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 }
501
502 if (will_compress) {
503 /*
504 * we aren't doing an inline extent round the compressed size
505 * up to a block size boundary so the allocator does sane
506 * things
507 */
Qu Wenruofda28322013-02-26 08:10:22 +0000508 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400509
510 /*
511 * one last check to make sure the compression is really a
512 * win, compare the page count read with the blocks on disk
513 */
Qu Wenruofda28322013-02-26 08:10:22 +0000514 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400515 if (total_compressed >= total_in) {
516 will_compress = 0;
517 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400518 num_bytes = total_in;
519 }
520 }
521 if (!will_compress && pages) {
522 /*
523 * the compression code ran but failed to make things smaller,
524 * free any pages it allocated and our page pointer array
525 */
526 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400527 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 page_cache_release(pages[i]);
529 }
530 kfree(pages);
531 pages = NULL;
532 total_compressed = 0;
533 nr_pages_ret = 0;
534
535 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500536 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
537 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500538 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500539 }
Chris Masonc8b97812008-10-29 14:49:59 -0400540 }
Chris Mason771ed682008-11-06 22:02:51 -0500541 if (will_compress) {
542 *num_added += 1;
543
544 /* the async work queues will take care of doing actual
545 * allocation on disk for these compressed pages,
546 * and will submit them to the elevator.
547 */
548 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800549 total_compressed, pages, nr_pages_ret,
550 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500551
Yan, Zheng24ae6362010-12-06 07:02:36 +0000552 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500553 start += num_bytes;
554 pages = NULL;
555 cond_resched();
556 goto again;
557 }
558 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500559cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500560 /*
561 * No compression, but we still need to write the pages in
562 * the file we've been given so far. redirty the locked
563 * page if it corresponds to our extent and set things up
564 * for the async work queue to run cow_file_range to do
565 * the normal delalloc dance
566 */
567 if (page_offset(locked_page) >= start &&
568 page_offset(locked_page) <= end) {
569 __set_page_dirty_nobuffers(locked_page);
570 /* unlocked later on in the async handlers */
571 }
Chris Mason4adaa612013-03-26 13:07:00 -0400572 if (redirty)
573 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800574 add_async_extent(async_cow, start, end - start + 1,
575 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500576 *num_added += 1;
577 }
578
579out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100580 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500581
582free_pages_out:
583 for (i = 0; i < nr_pages_ret; i++) {
584 WARN_ON(pages[i]->mapping);
585 page_cache_release(pages[i]);
586 }
Chris Masond3977122009-01-05 21:25:51 -0500587 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500588
589 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590
591cleanup_and_out:
592 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
593 start, end, NULL,
594 EXTENT_CLEAR_UNLOCK_PAGE |
595 EXTENT_CLEAR_DIRTY |
596 EXTENT_CLEAR_DELALLOC |
597 EXTENT_SET_WRITEBACK |
598 EXTENT_END_WRITEBACK);
599 if (!trans || IS_ERR(trans))
600 btrfs_error(root->fs_info, ret, "Failed to join transaction");
601 else
602 btrfs_abort_transaction(trans, root, ret);
603 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500604}
605
606/*
607 * phase two of compressed writeback. This is the ordered portion
608 * of the code, which only gets called in the order the work was
609 * queued. We walk all the async extents created by compress_file_range
610 * and send them down to the disk.
611 */
612static noinline int submit_compressed_extents(struct inode *inode,
613 struct async_cow *async_cow)
614{
615 struct async_extent *async_extent;
616 u64 alloc_hint = 0;
617 struct btrfs_trans_handle *trans;
618 struct btrfs_key ins;
619 struct extent_map *em;
620 struct btrfs_root *root = BTRFS_I(inode)->root;
621 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
622 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500623 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500624
625 if (list_empty(&async_cow->extents))
626 return 0;
627
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500628again:
Chris Masond3977122009-01-05 21:25:51 -0500629 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500630 async_extent = list_entry(async_cow->extents.next,
631 struct async_extent, list);
632 list_del(&async_extent->list);
633
634 io_tree = &BTRFS_I(inode)->io_tree;
635
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500636retry:
Chris Mason771ed682008-11-06 22:02:51 -0500637 /* did the compression code fall back to uncompressed IO? */
638 if (!async_extent->pages) {
639 int page_started = 0;
640 unsigned long nr_written = 0;
641
642 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000643 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100644 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500647 ret = cow_file_range(inode, async_cow->locked_page,
648 async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1,
651 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500652
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100653 /* JDM XXX */
654
Chris Mason771ed682008-11-06 22:02:51 -0500655 /*
656 * if page_started, cow_file_range inserted an
657 * inline extent and took care of all the unlocking
658 * and IO for us. Otherwise, we need to submit
659 * all those pages down to the drive.
660 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500661 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500662 extent_write_locked_range(io_tree,
663 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500664 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->ram_size - 1,
666 btrfs_get_extent,
667 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500668 else if (ret)
669 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500670 kfree(async_extent);
671 cond_resched();
672 continue;
673 }
674
675 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100676 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500677
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400678 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 if (IS_ERR(trans)) {
680 ret = PTR_ERR(trans);
681 } else {
682 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
683 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500684 async_extent->compressed_size,
685 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500686 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600687 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100688 btrfs_abort_transaction(trans, root, ret);
689 btrfs_end_transaction(trans, root);
690 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 if (ret) {
693 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 for (i = 0; i < async_extent->nr_pages; i++) {
696 WARN_ON(async_extent->pages[i]->mapping);
697 page_cache_release(async_extent->pages[i]);
698 }
699 kfree(async_extent->pages);
700 async_extent->nr_pages = 0;
701 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100703 if (ret == -ENOSPC)
704 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500705 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500706 }
707
Yan, Zhengc2167752009-11-12 09:34:21 +0000708 /*
709 * here we're doing allocation and writeback of the
710 * compressed pages
711 */
712 btrfs_drop_extent_cache(inode, async_extent->start,
713 async_extent->start +
714 async_extent->ram_size - 1, 0);
715
David Sterba172ddd62011-04-21 00:48:27 +0200716 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000717 if (!em) {
718 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000720 }
Chris Mason771ed682008-11-06 22:02:51 -0500721 em->start = async_extent->start;
722 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500723 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400724 em->mod_start = em->start;
725 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500726
727 em->block_start = ins.objectid;
728 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500729 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400730 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500731 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800732 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500733 set_bit(EXTENT_FLAG_PINNED, &em->flags);
734 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400735 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500736
Chris Masond3977122009-01-05 21:25:51 -0500737 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400738 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400739 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400740 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500741 if (ret != -EEXIST) {
742 free_extent_map(em);
743 break;
744 }
745 btrfs_drop_extent_cache(inode, async_extent->start,
746 async_extent->start +
747 async_extent->ram_size - 1, 0);
748 }
749
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500750 if (ret)
751 goto out_free_reserve;
752
Li Zefan261507a02010-12-17 14:21:50 +0800753 ret = btrfs_add_ordered_extent_compress(inode,
754 async_extent->start,
755 ins.objectid,
756 async_extent->ram_size,
757 ins.offset,
758 BTRFS_ORDERED_COMPRESSED,
759 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500760 if (ret)
761 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500762
Chris Mason771ed682008-11-06 22:02:51 -0500763 /*
764 * clear dirty, set writeback and unlock the pages.
765 */
766 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400767 &BTRFS_I(inode)->io_tree,
768 async_extent->start,
769 async_extent->start +
770 async_extent->ram_size - 1,
771 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
772 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400773 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400774 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500775
776 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500777 async_extent->start,
778 async_extent->ram_size,
779 ins.objectid,
780 ins.offset, async_extent->pages,
781 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500782 alloc_hint = ins.objectid + ins.offset;
783 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500784 if (ret)
785 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500786 cond_resched();
787 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100788 ret = 0;
789out:
790 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791out_free_reserve:
792 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100793out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
795 async_extent->start,
796 async_extent->start +
797 async_extent->ram_size - 1,
798 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
799 EXTENT_CLEAR_UNLOCK |
800 EXTENT_CLEAR_DELALLOC |
801 EXTENT_CLEAR_DIRTY |
802 EXTENT_SET_WRITEBACK |
803 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100804 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500806}
807
Josef Bacik4b46fce2010-05-23 11:00:55 -0400808static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
809 u64 num_bytes)
810{
811 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
812 struct extent_map *em;
813 u64 alloc_hint = 0;
814
815 read_lock(&em_tree->lock);
816 em = search_extent_mapping(em_tree, start, num_bytes);
817 if (em) {
818 /*
819 * if block start isn't an actual block number then find the
820 * first block in this inode and use that as a hint. If that
821 * block is also bogus then just don't worry about it.
822 */
823 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
824 free_extent_map(em);
825 em = search_extent_mapping(em_tree, 0, 0);
826 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
827 alloc_hint = em->block_start;
828 if (em)
829 free_extent_map(em);
830 } else {
831 alloc_hint = em->block_start;
832 free_extent_map(em);
833 }
834 }
835 read_unlock(&em_tree->lock);
836
837 return alloc_hint;
838}
839
Chris Mason771ed682008-11-06 22:02:51 -0500840/*
841 * when extent_io.c finds a delayed allocation range in the file,
842 * the call backs end up in this code. The basic idea is to
843 * allocate extents on disk for the range, and create ordered data structs
844 * in ram to track those extents.
845 *
846 * locked_page is the page that writepage had locked already. We use
847 * it to make sure we don't do extra locks or unlocks.
848 *
849 * *page_started is set to one if we unlock locked_page and do everything
850 * required to start IO on it. It may be clean and already done with
851 * IO when we return.
852 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000853static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
854 struct inode *inode,
855 struct btrfs_root *root,
856 struct page *locked_page,
857 u64 start, u64 end, int *page_started,
858 unsigned long *nr_written,
859 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500860{
Chris Mason771ed682008-11-06 22:02:51 -0500861 u64 alloc_hint = 0;
862 u64 num_bytes;
863 unsigned long ram_size;
864 u64 disk_num_bytes;
865 u64 cur_alloc_size;
866 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500867 struct btrfs_key ins;
868 struct extent_map *em;
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870 int ret = 0;
871
Liu Bo83eea1f2012-07-10 05:28:39 -0600872 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500873
Qu Wenruofda28322013-02-26 08:10:22 +0000874 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500875 num_bytes = max(blocksize, num_bytes);
876 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500877
Chris Mason4cb53002011-05-24 15:35:30 -0400878 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400879 if (num_bytes < 64 * 1024 &&
880 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400881 btrfs_add_inode_defrag(trans, inode);
882
Chris Mason771ed682008-11-06 22:02:51 -0500883 if (start == 0) {
884 /* lets try to make an inline extent */
885 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000886 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500887 if (ret == 0) {
888 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400889 &BTRFS_I(inode)->io_tree,
890 start, end, NULL,
891 EXTENT_CLEAR_UNLOCK_PAGE |
892 EXTENT_CLEAR_UNLOCK |
893 EXTENT_CLEAR_DELALLOC |
894 EXTENT_CLEAR_DIRTY |
895 EXTENT_SET_WRITEBACK |
896 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000897
Chris Mason771ed682008-11-06 22:02:51 -0500898 *nr_written = *nr_written +
899 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
900 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500901 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100902 } else if (ret < 0) {
903 btrfs_abort_transaction(trans, root, ret);
904 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500905 }
906 }
Chris Masonc8b97812008-10-29 14:49:59 -0400907
908 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200909 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400910
Josef Bacik4b46fce2010-05-23 11:00:55 -0400911 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400912 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400913
Chris Masond3977122009-01-05 21:25:51 -0500914 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400915 unsigned long op;
916
Josef Bacik287a0ab2010-03-19 18:07:23 +0000917 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500919 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500920 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 if (ret < 0) {
922 btrfs_abort_transaction(trans, root, ret);
923 goto out_unlock;
924 }
Chris Masond3977122009-01-05 21:25:51 -0500925
David Sterba172ddd62011-04-21 00:48:27 +0200926 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000927 if (!em) {
928 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000929 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000930 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400931 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500932 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500933 ram_size = ins.offset;
934 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400935 em->mod_start = em->start;
936 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400937
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400939 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500940 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400941 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400942 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400943 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400944 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400945
Chris Masond3977122009-01-05 21:25:51 -0500946 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400947 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400948 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400949 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400950 if (ret != -EEXIST) {
951 free_extent_map(em);
952 break;
953 }
954 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400955 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400956 }
Liu Boace68ba2013-04-22 10:53:47 +0000957 if (ret)
958 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400959
Chris Mason98d20f62008-04-14 09:46:10 -0400960 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400961 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500962 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000963 if (ret)
964 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400965
Yan Zheng17d217f2008-12-12 10:03:38 -0500966 if (root->root_key.objectid ==
967 BTRFS_DATA_RELOC_TREE_OBJECTID) {
968 ret = btrfs_reloc_clone_csums(inode, start,
969 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 if (ret) {
971 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000972 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100973 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500974 }
975
Chris Masond3977122009-01-05 21:25:51 -0500976 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400977 break;
Chris Masond3977122009-01-05 21:25:51 -0500978
Chris Masonc8b97812008-10-29 14:49:59 -0400979 /* we're not doing compressed IO, don't unlock the first
980 * page (which the caller expects to stay locked), don't
981 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400982 *
983 * Do set the Private2 bit so we know this page was properly
984 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400985 */
Chris Masona791e352009-10-08 11:27:10 -0400986 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
987 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
988 EXTENT_SET_PRIVATE2;
989
Chris Masonc8b97812008-10-29 14:49:59 -0400990 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
991 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400992 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400993 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500994 num_bytes -= cur_alloc_size;
995 alloc_hint = ins.objectid + ins.offset;
996 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400997 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100998out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500999 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001000
Liu Boace68ba2013-04-22 10:53:47 +00001001out_reserve:
1002 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003out_unlock:
1004 extent_clear_unlock_delalloc(inode,
1005 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001006 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 EXTENT_CLEAR_UNLOCK_PAGE |
1008 EXTENT_CLEAR_UNLOCK |
1009 EXTENT_CLEAR_DELALLOC |
1010 EXTENT_CLEAR_DIRTY |
1011 EXTENT_SET_WRITEBACK |
1012 EXTENT_END_WRITEBACK);
1013
1014 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001015}
Chris Masonc8b97812008-10-29 14:49:59 -04001016
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001017static noinline int cow_file_range(struct inode *inode,
1018 struct page *locked_page,
1019 u64 start, u64 end, int *page_started,
1020 unsigned long *nr_written,
1021 int unlock)
1022{
1023 struct btrfs_trans_handle *trans;
1024 struct btrfs_root *root = BTRFS_I(inode)->root;
1025 int ret;
1026
1027 trans = btrfs_join_transaction(root);
1028 if (IS_ERR(trans)) {
1029 extent_clear_unlock_delalloc(inode,
1030 &BTRFS_I(inode)->io_tree,
1031 start, end, locked_page,
1032 EXTENT_CLEAR_UNLOCK_PAGE |
1033 EXTENT_CLEAR_UNLOCK |
1034 EXTENT_CLEAR_DELALLOC |
1035 EXTENT_CLEAR_DIRTY |
1036 EXTENT_SET_WRITEBACK |
1037 EXTENT_END_WRITEBACK);
1038 return PTR_ERR(trans);
1039 }
1040 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1041
1042 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1043 page_started, nr_written, unlock);
1044
1045 btrfs_end_transaction(trans, root);
1046
1047 return ret;
1048}
1049
Chris Mason771ed682008-11-06 22:02:51 -05001050/*
1051 * work queue call back to started compression on a file and pages
1052 */
1053static noinline void async_cow_start(struct btrfs_work *work)
1054{
1055 struct async_cow *async_cow;
1056 int num_added = 0;
1057 async_cow = container_of(work, struct async_cow, work);
1058
1059 compress_file_range(async_cow->inode, async_cow->locked_page,
1060 async_cow->start, async_cow->end, async_cow,
1061 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001062 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001063 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001064 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001065 }
Chris Mason771ed682008-11-06 22:02:51 -05001066}
1067
1068/*
1069 * work queue call back to submit previously compressed pages
1070 */
1071static noinline void async_cow_submit(struct btrfs_work *work)
1072{
1073 struct async_cow *async_cow;
1074 struct btrfs_root *root;
1075 unsigned long nr_pages;
1076
1077 async_cow = container_of(work, struct async_cow, work);
1078
1079 root = async_cow->root;
1080 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1081 PAGE_CACHE_SHIFT;
1082
Josef Bacik66657b32012-08-01 15:36:24 -04001083 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001084 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001085 waitqueue_active(&root->fs_info->async_submit_wait))
1086 wake_up(&root->fs_info->async_submit_wait);
1087
Chris Masond3977122009-01-05 21:25:51 -05001088 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001089 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001090}
Chris Masonc8b97812008-10-29 14:49:59 -04001091
Chris Mason771ed682008-11-06 22:02:51 -05001092static noinline void async_cow_free(struct btrfs_work *work)
1093{
1094 struct async_cow *async_cow;
1095 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001096 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001097 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001098 kfree(async_cow);
1099}
1100
1101static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1102 u64 start, u64 end, int *page_started,
1103 unsigned long *nr_written)
1104{
1105 struct async_cow *async_cow;
1106 struct btrfs_root *root = BTRFS_I(inode)->root;
1107 unsigned long nr_pages;
1108 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001109 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001110
Chris Masona3429ab2009-10-08 12:30:20 -04001111 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1112 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001113 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001114 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001115 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001116 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001117 async_cow->root = root;
1118 async_cow->locked_page = locked_page;
1119 async_cow->start = start;
1120
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001121 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001122 cur_end = end;
1123 else
1124 cur_end = min(end, start + 512 * 1024 - 1);
1125
1126 async_cow->end = cur_end;
1127 INIT_LIST_HEAD(&async_cow->extents);
1128
1129 async_cow->work.func = async_cow_start;
1130 async_cow->work.ordered_func = async_cow_submit;
1131 async_cow->work.ordered_free = async_cow_free;
1132 async_cow->work.flags = 0;
1133
Chris Mason771ed682008-11-06 22:02:51 -05001134 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1135 PAGE_CACHE_SHIFT;
1136 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1137
1138 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1139 &async_cow->work);
1140
1141 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1142 wait_event(root->fs_info->async_submit_wait,
1143 (atomic_read(&root->fs_info->async_delalloc_pages) <
1144 limit));
1145 }
1146
Chris Masond3977122009-01-05 21:25:51 -05001147 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001148 atomic_read(&root->fs_info->async_delalloc_pages)) {
1149 wait_event(root->fs_info->async_submit_wait,
1150 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1151 0));
1152 }
1153
1154 *nr_written += nr_pages;
1155 start = cur_end + 1;
1156 }
1157 *page_started = 1;
1158 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001159}
1160
Chris Masond3977122009-01-05 21:25:51 -05001161static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001162 u64 bytenr, u64 num_bytes)
1163{
1164 int ret;
1165 struct btrfs_ordered_sum *sums;
1166 LIST_HEAD(list);
1167
Yan Zheng07d400a2009-01-06 11:42:00 -05001168 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001169 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001170 if (ret == 0 && list_empty(&list))
1171 return 0;
1172
1173 while (!list_empty(&list)) {
1174 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1175 list_del(&sums->list);
1176 kfree(sums);
1177 }
1178 return 1;
1179}
1180
Chris Masond352ac62008-09-29 15:18:18 -04001181/*
1182 * when nowcow writeback call back. This checks for snapshots or COW copies
1183 * of the extents that exist in the file, and COWs the file as required.
1184 *
1185 * If no cow copies or snapshots exist, we write directly to the existing
1186 * blocks on disk
1187 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001188static noinline int run_delalloc_nocow(struct inode *inode,
1189 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001190 u64 start, u64 end, int *page_started, int force,
1191 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001192{
Chris Masonbe20aa92007-12-17 20:14:01 -05001193 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001194 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001195 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001196 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001198 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001199 u64 cow_start;
1200 u64 cur_offset;
1201 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001202 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 u64 disk_bytenr;
1204 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001205 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001206 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001208 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001209 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 int nocow;
1211 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001212 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001213 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001214
1215 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001216 if (!path) {
1217 extent_clear_unlock_delalloc(inode,
1218 &BTRFS_I(inode)->io_tree,
1219 start, end, locked_page,
1220 EXTENT_CLEAR_UNLOCK_PAGE |
1221 EXTENT_CLEAR_UNLOCK |
1222 EXTENT_CLEAR_DELALLOC |
1223 EXTENT_CLEAR_DIRTY |
1224 EXTENT_SET_WRITEBACK |
1225 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001226 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001227 }
Li Zefan82d59022011-04-20 10:33:24 +08001228
Liu Bo83eea1f2012-07-10 05:28:39 -06001229 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001230
1231 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001232 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001233 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001234 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001235
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001236 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001237 extent_clear_unlock_delalloc(inode,
1238 &BTRFS_I(inode)->io_tree,
1239 start, end, locked_page,
1240 EXTENT_CLEAR_UNLOCK_PAGE |
1241 EXTENT_CLEAR_UNLOCK |
1242 EXTENT_CLEAR_DELALLOC |
1243 EXTENT_CLEAR_DIRTY |
1244 EXTENT_SET_WRITEBACK |
1245 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001246 btrfs_free_path(path);
1247 return PTR_ERR(trans);
1248 }
1249
Josef Bacik74b21072011-04-13 12:02:53 -04001250 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 cow_start = (u64)-1;
1253 cur_offset = start;
1254 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001255 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001257 if (ret < 0) {
1258 btrfs_abort_transaction(trans, root, ret);
1259 goto error;
1260 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1262 leaf = path->nodes[0];
1263 btrfs_item_key_to_cpu(leaf, &found_key,
1264 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001265 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 found_key.type == BTRFS_EXTENT_DATA_KEY)
1267 path->slots[0]--;
1268 }
1269 check_prev = 0;
1270next_slot:
1271 leaf = path->nodes[0];
1272 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1273 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001274 if (ret < 0) {
1275 btrfs_abort_transaction(trans, root, ret);
1276 goto error;
1277 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 if (ret > 0)
1279 break;
1280 leaf = path->nodes[0];
1281 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001282
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 nocow = 0;
1284 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001285 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001287
Li Zefan33345d012011-04-20 10:31:50 +08001288 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1290 found_key.offset > end)
1291 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 if (found_key.offset > cur_offset) {
1294 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001295 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 goto out_check;
1297 }
Chris Masonc31f8832008-01-08 15:46:31 -05001298
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 fi = btrfs_item_ptr(leaf, path->slots[0],
1300 struct btrfs_file_extent_item);
1301 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
Josef Bacikcc95bef2013-04-04 14:31:27 -04001303 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001304 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1305 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001307 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 extent_end = found_key.offset +
1309 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001310 disk_num_bytes =
1311 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 if (extent_end <= start) {
1313 path->slots[0]++;
1314 goto next_slot;
1315 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001316 if (disk_bytenr == 0)
1317 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 if (btrfs_file_extent_compression(leaf, fi) ||
1319 btrfs_file_extent_encryption(leaf, fi) ||
1320 btrfs_file_extent_other_encoding(leaf, fi))
1321 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001322 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1323 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001324 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001326 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001327 found_key.offset -
1328 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001331 disk_bytenr += cur_offset - found_key.offset;
1332 num_bytes = min(end + 1, extent_end) - cur_offset;
1333 /*
1334 * force cow if csum exists in the range.
1335 * this ensure that csum for a given extent are
1336 * either valid or do not exist.
1337 */
1338 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1339 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 nocow = 1;
1341 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1342 extent_end = found_key.offset +
1343 btrfs_file_extent_inline_len(leaf, fi);
1344 extent_end = ALIGN(extent_end, root->sectorsize);
1345 } else {
1346 BUG_ON(1);
1347 }
1348out_check:
1349 if (extent_end <= start) {
1350 path->slots[0]++;
1351 goto next_slot;
1352 }
1353 if (!nocow) {
1354 if (cow_start == (u64)-1)
1355 cow_start = cur_offset;
1356 cur_offset = extent_end;
1357 if (cur_offset > end)
1358 break;
1359 path->slots[0]++;
1360 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001361 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001362
David Sterbab3b4aa72011-04-21 01:20:15 +02001363 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001365 ret = __cow_file_range(trans, inode, root, locked_page,
1366 cow_start, found_key.offset - 1,
1367 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001368 if (ret) {
1369 btrfs_abort_transaction(trans, root, ret);
1370 goto error;
1371 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001373 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001374
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1376 struct extent_map *em;
1377 struct extent_map_tree *em_tree;
1378 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001379 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001380 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001381 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001382 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001383 em->len = num_bytes;
1384 em->block_len = num_bytes;
1385 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001386 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001387 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001389 em->mod_start = em->start;
1390 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001391 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001392 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001393 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001394 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001395 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001396 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001397 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001398 if (ret != -EEXIST) {
1399 free_extent_map(em);
1400 break;
1401 }
1402 btrfs_drop_extent_cache(inode, em->start,
1403 em->start + em->len - 1, 0);
1404 }
1405 type = BTRFS_ORDERED_PREALLOC;
1406 } else {
1407 type = BTRFS_ORDERED_NOCOW;
1408 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001409
1410 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001411 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001412 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001413
Yan, Zhengefa56462010-05-16 10:49:59 -04001414 if (root->root_key.objectid ==
1415 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1416 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1417 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001418 if (ret) {
1419 btrfs_abort_transaction(trans, root, ret);
1420 goto error;
1421 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001422 }
1423
Yan Zhengd899e052008-10-30 14:25:28 -04001424 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001425 cur_offset, cur_offset + num_bytes - 1,
1426 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1427 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1428 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 cur_offset = extent_end;
1430 if (cur_offset > end)
1431 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001432 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001433 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001434
Josef Bacik17ca04a2012-05-31 15:58:55 -04001435 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001437 cur_offset = end;
1438 }
1439
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001441 ret = __cow_file_range(trans, inode, root, locked_page,
1442 cow_start, end,
1443 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 if (ret) {
1445 btrfs_abort_transaction(trans, root, ret);
1446 goto error;
1447 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 }
1449
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001450error:
Miao Xiea698d0752012-09-20 01:51:59 -06001451 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001452 if (!ret)
1453 ret = err;
1454
Josef Bacik17ca04a2012-05-31 15:58:55 -04001455 if (ret && cur_offset < end)
1456 extent_clear_unlock_delalloc(inode,
1457 &BTRFS_I(inode)->io_tree,
1458 cur_offset, end, locked_page,
1459 EXTENT_CLEAR_UNLOCK_PAGE |
1460 EXTENT_CLEAR_UNLOCK |
1461 EXTENT_CLEAR_DELALLOC |
1462 EXTENT_CLEAR_DIRTY |
1463 EXTENT_SET_WRITEBACK |
1464 EXTENT_END_WRITEBACK);
1465
Yan Zheng7ea394f2008-08-05 13:05:02 -04001466 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001467 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001468}
1469
Chris Masond352ac62008-09-29 15:18:18 -04001470/*
1471 * extent_io.c call back to do delayed allocation processing
1472 */
Chris Masonc8b97812008-10-29 14:49:59 -04001473static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001474 u64 start, u64 end, int *page_started,
1475 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001476{
Chris Masonbe20aa92007-12-17 20:14:01 -05001477 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001478 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001479
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001480 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001481 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001482 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001483 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001484 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001485 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001486 } else if (!btrfs_test_opt(root, COMPRESS) &&
1487 !(BTRFS_I(inode)->force_compress) &&
1488 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001489 ret = cow_file_range(inode, locked_page, start, end,
1490 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001491 } else {
1492 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1493 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001494 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001495 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001496 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001497 return ret;
1498}
1499
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001500static void btrfs_split_extent_hook(struct inode *inode,
1501 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001503 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001504 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001505 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506
Josef Bacik9e0baf62011-07-15 15:16:44 +00001507 spin_lock(&BTRFS_I(inode)->lock);
1508 BTRFS_I(inode)->outstanding_extents++;
1509 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001510}
1511
1512/*
1513 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1514 * extents so we can keep track of new extents that are just merged onto old
1515 * extents, such as when we are doing sequential writes, so we can properly
1516 * account for the metadata space we'll need.
1517 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001518static void btrfs_merge_extent_hook(struct inode *inode,
1519 struct extent_state *new,
1520 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522 /* not delalloc, ignore it */
1523 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001524 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525
Josef Bacik9e0baf62011-07-15 15:16:44 +00001526 spin_lock(&BTRFS_I(inode)->lock);
1527 BTRFS_I(inode)->outstanding_extents--;
1528 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001529}
1530
Miao Xieeb73c1b2013-05-15 07:48:22 +00001531static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1532 struct inode *inode)
1533{
1534 spin_lock(&root->delalloc_lock);
1535 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1536 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1537 &root->delalloc_inodes);
1538 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1539 &BTRFS_I(inode)->runtime_flags);
1540 root->nr_delalloc_inodes++;
1541 if (root->nr_delalloc_inodes == 1) {
1542 spin_lock(&root->fs_info->delalloc_root_lock);
1543 BUG_ON(!list_empty(&root->delalloc_root));
1544 list_add_tail(&root->delalloc_root,
1545 &root->fs_info->delalloc_roots);
1546 spin_unlock(&root->fs_info->delalloc_root_lock);
1547 }
1548 }
1549 spin_unlock(&root->delalloc_lock);
1550}
1551
1552static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1553 struct inode *inode)
1554{
1555 spin_lock(&root->delalloc_lock);
1556 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1557 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1558 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1559 &BTRFS_I(inode)->runtime_flags);
1560 root->nr_delalloc_inodes--;
1561 if (!root->nr_delalloc_inodes) {
1562 spin_lock(&root->fs_info->delalloc_root_lock);
1563 BUG_ON(list_empty(&root->delalloc_root));
1564 list_del_init(&root->delalloc_root);
1565 spin_unlock(&root->fs_info->delalloc_root_lock);
1566 }
1567 }
1568 spin_unlock(&root->delalloc_lock);
1569}
1570
Chris Masond352ac62008-09-29 15:18:18 -04001571/*
1572 * extent_io.c set_bit_hook, used to track delayed allocation
1573 * bytes in this file, and to maintain the list of inodes that
1574 * have pending delalloc work to be done.
1575 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001576static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001577 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001578{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001579
Chris Mason75eff682008-12-15 15:54:40 -05001580 /*
1581 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001582 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001583 * bit, which is only set or cleared with irqs on
1584 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001585 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001586 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001587 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001588 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001589
Josef Bacik9e0baf62011-07-15 15:16:44 +00001590 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001592 } else {
1593 spin_lock(&BTRFS_I(inode)->lock);
1594 BTRFS_I(inode)->outstanding_extents++;
1595 spin_unlock(&BTRFS_I(inode)->lock);
1596 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001597
Miao Xie963d6782013-01-29 10:10:51 +00001598 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1599 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001600 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001601 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001602 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001603 &BTRFS_I(inode)->runtime_flags))
1604 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001605 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001606 }
Chris Mason291d6732008-01-29 15:55:23 -05001607}
1608
Chris Masond352ac62008-09-29 15:18:18 -04001609/*
1610 * extent_io.c clear_bit_hook, see set_bit_hook for why
1611 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001612static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001613 struct extent_state *state,
1614 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001615{
Chris Mason75eff682008-12-15 15:54:40 -05001616 /*
1617 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001618 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001619 * bit, which is only set or cleared with irqs on
1620 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001621 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001622 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001624 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001625
Josef Bacik9e0baf62011-07-15 15:16:44 +00001626 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001627 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001628 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1629 spin_lock(&BTRFS_I(inode)->lock);
1630 BTRFS_I(inode)->outstanding_extents--;
1631 spin_unlock(&BTRFS_I(inode)->lock);
1632 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001633
1634 if (*bits & EXTENT_DO_ACCOUNTING)
1635 btrfs_delalloc_release_metadata(inode, len);
1636
Josef Bacik0cb59c92010-07-02 12:14:14 -04001637 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1638 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001639 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001640
Miao Xie963d6782013-01-29 10:10:51 +00001641 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1642 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001643 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001644 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001645 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001646 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001647 &BTRFS_I(inode)->runtime_flags))
1648 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001649 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001650 }
Chris Mason291d6732008-01-29 15:55:23 -05001651}
1652
Chris Masond352ac62008-09-29 15:18:18 -04001653/*
1654 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1655 * we don't create bios that span stripes or chunks
1656 */
David Woodhouse64a16702009-07-15 23:29:37 +01001657int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001658 size_t size, struct bio *bio,
1659 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001660{
1661 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001662 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001663 u64 length = 0;
1664 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001665 int ret;
1666
Chris Mason771ed682008-11-06 22:02:51 -05001667 if (bio_flags & EXTENT_BIO_COMPRESSED)
1668 return 0;
1669
Chris Masonf2d8d742008-04-21 10:03:05 -04001670 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001671 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001672 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001673 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001674 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001675 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001676 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001677 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001678 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001679}
1680
Chris Masond352ac62008-09-29 15:18:18 -04001681/*
1682 * in order to insert checksums into the metadata in large chunks,
1683 * we wait until bio submission time. All the pages in the bio are
1684 * checksummed and sums are attached onto the ordered extent record.
1685 *
1686 * At IO completion time the cums attached on the ordered extent record
1687 * are inserted into the btree
1688 */
Chris Masond3977122009-01-05 21:25:51 -05001689static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1690 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001691 unsigned long bio_flags,
1692 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001693{
Chris Mason065631f2008-02-20 12:07:25 -05001694 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001695 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001696
Chris Masond20f7042008-12-08 16:58:54 -05001697 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001698 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001699 return 0;
1700}
Chris Masone0156402008-04-16 11:15:20 -04001701
Chris Mason4a69a412008-11-06 22:03:00 -05001702/*
1703 * in order to insert checksums into the metadata in large chunks,
1704 * we wait until bio submission time. All the pages in the bio are
1705 * checksummed and sums are attached onto the ordered extent record.
1706 *
1707 * At IO completion time the cums attached on the ordered extent record
1708 * are inserted into the btree
1709 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001710static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001711 int mirror_num, unsigned long bio_flags,
1712 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001713{
1714 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001715 int ret;
1716
1717 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1718 if (ret)
1719 bio_endio(bio, ret);
1720 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001721}
1722
Chris Masond352ac62008-09-29 15:18:18 -04001723/*
Chris Masoncad321a2008-12-17 14:51:42 -05001724 * extent_io.c submission hook. This does the right thing for csum calculation
1725 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001726 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001727static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001728 int mirror_num, unsigned long bio_flags,
1729 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001730{
1731 struct btrfs_root *root = BTRFS_I(inode)->root;
1732 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001733 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001734 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001735 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001736
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001737 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001738
Liu Bo83eea1f2012-07-10 05:28:39 -06001739 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001740 metadata = 2;
1741
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001742 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001743 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1744 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001745 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001746
Chris Masond20f7042008-12-08 16:58:54 -05001747 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001748 ret = btrfs_submit_compressed_read(inode, bio,
1749 mirror_num,
1750 bio_flags);
1751 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001752 } else if (!skip_sum) {
1753 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1754 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001755 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001756 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001757 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001758 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001759 /* csum items have already been cloned */
1760 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1761 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001762 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001763 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001764 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001765 bio_flags, bio_offset,
1766 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001767 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001768 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001769 } else if (!skip_sum) {
1770 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1771 if (ret)
1772 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001773 }
1774
Chris Mason0b86a832008-03-24 15:01:56 -04001775mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001776 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1777
1778out:
1779 if (ret < 0)
1780 bio_endio(bio, ret);
1781 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001782}
Chris Mason6885f302008-02-20 16:11:05 -05001783
Chris Masond352ac62008-09-29 15:18:18 -04001784/*
1785 * given a list of ordered sums record them in the inode. This happens
1786 * at IO completion time based on sums calculated at bio submission time.
1787 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001788static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001789 struct inode *inode, u64 file_offset,
1790 struct list_head *list)
1791{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001792 struct btrfs_ordered_sum *sum;
1793
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001794 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001795 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001796 btrfs_csum_file_blocks(trans,
1797 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001798 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001799 }
1800 return 0;
1801}
1802
Josef Bacik2ac55d42010-02-03 19:33:23 +00001803int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1804 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001805{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001806 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001807 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001808 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001809}
1810
Chris Masond352ac62008-09-29 15:18:18 -04001811/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001812struct btrfs_writepage_fixup {
1813 struct page *page;
1814 struct btrfs_work work;
1815};
1816
Christoph Hellwigb2950862008-12-02 09:54:17 -05001817static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001818{
1819 struct btrfs_writepage_fixup *fixup;
1820 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001821 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001822 struct page *page;
1823 struct inode *inode;
1824 u64 page_start;
1825 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001826 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001827
1828 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1829 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001830again:
Chris Mason247e7432008-07-17 12:53:51 -04001831 lock_page(page);
1832 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1833 ClearPageChecked(page);
1834 goto out_page;
1835 }
1836
1837 inode = page->mapping->host;
1838 page_start = page_offset(page);
1839 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1840
Josef Bacik2ac55d42010-02-03 19:33:23 +00001841 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001842 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001843
1844 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001845 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001846 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001847
1848 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1849 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001850 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1851 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001852 unlock_page(page);
1853 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001854 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001855 goto again;
1856 }
Chris Mason247e7432008-07-17 12:53:51 -04001857
Jeff Mahoney87826df2012-02-15 16:23:57 +01001858 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1859 if (ret) {
1860 mapping_set_error(page->mapping, ret);
1861 end_extent_writepage(page, ret, page_start, page_end);
1862 ClearPageChecked(page);
1863 goto out;
1864 }
1865
Josef Bacik2ac55d42010-02-03 19:33:23 +00001866 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001867 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001868 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001869out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001870 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1871 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001872out_page:
1873 unlock_page(page);
1874 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001875 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001876}
1877
1878/*
1879 * There are a few paths in the higher layers of the kernel that directly
1880 * set the page dirty bit without asking the filesystem if it is a
1881 * good idea. This causes problems because we want to make sure COW
1882 * properly happens and the data=ordered rules are followed.
1883 *
Chris Masonc8b97812008-10-29 14:49:59 -04001884 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001885 * hasn't been properly setup for IO. We kick off an async process
1886 * to fix it up. The async helper will wait for ordered extents, set
1887 * the delalloc bit and make it safe to write the page.
1888 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001889static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001890{
1891 struct inode *inode = page->mapping->host;
1892 struct btrfs_writepage_fixup *fixup;
1893 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001894
Chris Mason8b62b722009-09-02 16:53:46 -04001895 /* this page is properly in the ordered list */
1896 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001897 return 0;
1898
1899 if (PageChecked(page))
1900 return -EAGAIN;
1901
1902 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1903 if (!fixup)
1904 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001905
Chris Mason247e7432008-07-17 12:53:51 -04001906 SetPageChecked(page);
1907 page_cache_get(page);
1908 fixup->work.func = btrfs_writepage_fixup_worker;
1909 fixup->page = page;
1910 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001911 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001912}
1913
Yan Zhengd899e052008-10-30 14:25:28 -04001914static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1915 struct inode *inode, u64 file_pos,
1916 u64 disk_bytenr, u64 disk_num_bytes,
1917 u64 num_bytes, u64 ram_bytes,
1918 u8 compression, u8 encryption,
1919 u16 other_encoding, int extent_type)
1920{
1921 struct btrfs_root *root = BTRFS_I(inode)->root;
1922 struct btrfs_file_extent_item *fi;
1923 struct btrfs_path *path;
1924 struct extent_buffer *leaf;
1925 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001926 int ret;
1927
1928 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001929 if (!path)
1930 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001931
Chris Masonb9473432009-03-13 11:00:37 -04001932 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001933
1934 /*
1935 * we may be replacing one extent in the tree with another.
1936 * The new extent is pinned in the extent map, and we don't want
1937 * to drop it from the cache until it is completely in the btree.
1938 *
1939 * So, tell btrfs_drop_extents to leave this extent in the cache.
1940 * the caller is expected to unpin it and allow it to be merged
1941 * with the others.
1942 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001943 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001944 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001945 if (ret)
1946 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001947
Li Zefan33345d012011-04-20 10:31:50 +08001948 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001949 ins.offset = file_pos;
1950 ins.type = BTRFS_EXTENT_DATA_KEY;
1951 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952 if (ret)
1953 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001954 leaf = path->nodes[0];
1955 fi = btrfs_item_ptr(leaf, path->slots[0],
1956 struct btrfs_file_extent_item);
1957 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1958 btrfs_set_file_extent_type(leaf, fi, extent_type);
1959 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1960 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1961 btrfs_set_file_extent_offset(leaf, fi, 0);
1962 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1963 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1964 btrfs_set_file_extent_compression(leaf, fi, compression);
1965 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1966 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001967
Yan Zhengd899e052008-10-30 14:25:28 -04001968 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001969 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001970
1971 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001972
1973 ins.objectid = disk_bytenr;
1974 ins.offset = disk_num_bytes;
1975 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 ret = btrfs_alloc_reserved_file_extent(trans, root,
1977 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001978 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001979out:
Yan Zhengd899e052008-10-30 14:25:28 -04001980 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001981
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001982 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001983}
1984
Liu Bo38c227d2013-01-29 03:18:40 +00001985/* snapshot-aware defrag */
1986struct sa_defrag_extent_backref {
1987 struct rb_node node;
1988 struct old_sa_defrag_extent *old;
1989 u64 root_id;
1990 u64 inum;
1991 u64 file_pos;
1992 u64 extent_offset;
1993 u64 num_bytes;
1994 u64 generation;
1995};
1996
1997struct old_sa_defrag_extent {
1998 struct list_head list;
1999 struct new_sa_defrag_extent *new;
2000
2001 u64 extent_offset;
2002 u64 bytenr;
2003 u64 offset;
2004 u64 len;
2005 int count;
2006};
2007
2008struct new_sa_defrag_extent {
2009 struct rb_root root;
2010 struct list_head head;
2011 struct btrfs_path *path;
2012 struct inode *inode;
2013 u64 file_pos;
2014 u64 len;
2015 u64 bytenr;
2016 u64 disk_len;
2017 u8 compress_type;
2018};
2019
2020static int backref_comp(struct sa_defrag_extent_backref *b1,
2021 struct sa_defrag_extent_backref *b2)
2022{
2023 if (b1->root_id < b2->root_id)
2024 return -1;
2025 else if (b1->root_id > b2->root_id)
2026 return 1;
2027
2028 if (b1->inum < b2->inum)
2029 return -1;
2030 else if (b1->inum > b2->inum)
2031 return 1;
2032
2033 if (b1->file_pos < b2->file_pos)
2034 return -1;
2035 else if (b1->file_pos > b2->file_pos)
2036 return 1;
2037
2038 /*
2039 * [------------------------------] ===> (a range of space)
2040 * |<--->| |<---->| =============> (fs/file tree A)
2041 * |<---------------------------->| ===> (fs/file tree B)
2042 *
2043 * A range of space can refer to two file extents in one tree while
2044 * refer to only one file extent in another tree.
2045 *
2046 * So we may process a disk offset more than one time(two extents in A)
2047 * and locate at the same extent(one extent in B), then insert two same
2048 * backrefs(both refer to the extent in B).
2049 */
2050 return 0;
2051}
2052
2053static void backref_insert(struct rb_root *root,
2054 struct sa_defrag_extent_backref *backref)
2055{
2056 struct rb_node **p = &root->rb_node;
2057 struct rb_node *parent = NULL;
2058 struct sa_defrag_extent_backref *entry;
2059 int ret;
2060
2061 while (*p) {
2062 parent = *p;
2063 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2064
2065 ret = backref_comp(backref, entry);
2066 if (ret < 0)
2067 p = &(*p)->rb_left;
2068 else
2069 p = &(*p)->rb_right;
2070 }
2071
2072 rb_link_node(&backref->node, parent, p);
2073 rb_insert_color(&backref->node, root);
2074}
2075
2076/*
2077 * Note the backref might has changed, and in this case we just return 0.
2078 */
2079static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2080 void *ctx)
2081{
2082 struct btrfs_file_extent_item *extent;
2083 struct btrfs_fs_info *fs_info;
2084 struct old_sa_defrag_extent *old = ctx;
2085 struct new_sa_defrag_extent *new = old->new;
2086 struct btrfs_path *path = new->path;
2087 struct btrfs_key key;
2088 struct btrfs_root *root;
2089 struct sa_defrag_extent_backref *backref;
2090 struct extent_buffer *leaf;
2091 struct inode *inode = new->inode;
2092 int slot;
2093 int ret;
2094 u64 extent_offset;
2095 u64 num_bytes;
2096
2097 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2098 inum == btrfs_ino(inode))
2099 return 0;
2100
2101 key.objectid = root_id;
2102 key.type = BTRFS_ROOT_ITEM_KEY;
2103 key.offset = (u64)-1;
2104
2105 fs_info = BTRFS_I(inode)->root->fs_info;
2106 root = btrfs_read_fs_root_no_name(fs_info, &key);
2107 if (IS_ERR(root)) {
2108 if (PTR_ERR(root) == -ENOENT)
2109 return 0;
2110 WARN_ON(1);
2111 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2112 inum, offset, root_id);
2113 return PTR_ERR(root);
2114 }
2115
2116 key.objectid = inum;
2117 key.type = BTRFS_EXTENT_DATA_KEY;
2118 if (offset > (u64)-1 << 32)
2119 key.offset = 0;
2120 else
2121 key.offset = offset;
2122
2123 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2124 if (ret < 0) {
2125 WARN_ON(1);
2126 return ret;
2127 }
2128
2129 while (1) {
2130 cond_resched();
2131
2132 leaf = path->nodes[0];
2133 slot = path->slots[0];
2134
2135 if (slot >= btrfs_header_nritems(leaf)) {
2136 ret = btrfs_next_leaf(root, path);
2137 if (ret < 0) {
2138 goto out;
2139 } else if (ret > 0) {
2140 ret = 0;
2141 goto out;
2142 }
2143 continue;
2144 }
2145
2146 path->slots[0]++;
2147
2148 btrfs_item_key_to_cpu(leaf, &key, slot);
2149
2150 if (key.objectid > inum)
2151 goto out;
2152
2153 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2154 continue;
2155
2156 extent = btrfs_item_ptr(leaf, slot,
2157 struct btrfs_file_extent_item);
2158
2159 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2160 continue;
2161
2162 extent_offset = btrfs_file_extent_offset(leaf, extent);
2163 if (key.offset - extent_offset != offset)
2164 continue;
2165
2166 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2167 if (extent_offset >= old->extent_offset + old->offset +
2168 old->len || extent_offset + num_bytes <=
2169 old->extent_offset + old->offset)
2170 continue;
2171
2172 break;
2173 }
2174
2175 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2176 if (!backref) {
2177 ret = -ENOENT;
2178 goto out;
2179 }
2180
2181 backref->root_id = root_id;
2182 backref->inum = inum;
2183 backref->file_pos = offset + extent_offset;
2184 backref->num_bytes = num_bytes;
2185 backref->extent_offset = extent_offset;
2186 backref->generation = btrfs_file_extent_generation(leaf, extent);
2187 backref->old = old;
2188 backref_insert(&new->root, backref);
2189 old->count++;
2190out:
2191 btrfs_release_path(path);
2192 WARN_ON(ret);
2193 return ret;
2194}
2195
2196static noinline bool record_extent_backrefs(struct btrfs_path *path,
2197 struct new_sa_defrag_extent *new)
2198{
2199 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2200 struct old_sa_defrag_extent *old, *tmp;
2201 int ret;
2202
2203 new->path = path;
2204
2205 list_for_each_entry_safe(old, tmp, &new->head, list) {
2206 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2207 path, record_one_backref,
2208 old);
2209 BUG_ON(ret < 0 && ret != -ENOENT);
2210
2211 /* no backref to be processed for this extent */
2212 if (!old->count) {
2213 list_del(&old->list);
2214 kfree(old);
2215 }
2216 }
2217
2218 if (list_empty(&new->head))
2219 return false;
2220
2221 return true;
2222}
2223
2224static int relink_is_mergable(struct extent_buffer *leaf,
2225 struct btrfs_file_extent_item *fi,
2226 u64 disk_bytenr)
2227{
2228 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2229 return 0;
2230
2231 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2232 return 0;
2233
2234 if (btrfs_file_extent_compression(leaf, fi) ||
2235 btrfs_file_extent_encryption(leaf, fi) ||
2236 btrfs_file_extent_other_encoding(leaf, fi))
2237 return 0;
2238
2239 return 1;
2240}
2241
2242/*
2243 * Note the backref might has changed, and in this case we just return 0.
2244 */
2245static noinline int relink_extent_backref(struct btrfs_path *path,
2246 struct sa_defrag_extent_backref *prev,
2247 struct sa_defrag_extent_backref *backref)
2248{
2249 struct btrfs_file_extent_item *extent;
2250 struct btrfs_file_extent_item *item;
2251 struct btrfs_ordered_extent *ordered;
2252 struct btrfs_trans_handle *trans;
2253 struct btrfs_fs_info *fs_info;
2254 struct btrfs_root *root;
2255 struct btrfs_key key;
2256 struct extent_buffer *leaf;
2257 struct old_sa_defrag_extent *old = backref->old;
2258 struct new_sa_defrag_extent *new = old->new;
2259 struct inode *src_inode = new->inode;
2260 struct inode *inode;
2261 struct extent_state *cached = NULL;
2262 int ret = 0;
2263 u64 start;
2264 u64 len;
2265 u64 lock_start;
2266 u64 lock_end;
2267 bool merge = false;
2268 int index;
2269
2270 if (prev && prev->root_id == backref->root_id &&
2271 prev->inum == backref->inum &&
2272 prev->file_pos + prev->num_bytes == backref->file_pos)
2273 merge = true;
2274
2275 /* step 1: get root */
2276 key.objectid = backref->root_id;
2277 key.type = BTRFS_ROOT_ITEM_KEY;
2278 key.offset = (u64)-1;
2279
2280 fs_info = BTRFS_I(src_inode)->root->fs_info;
2281 index = srcu_read_lock(&fs_info->subvol_srcu);
2282
2283 root = btrfs_read_fs_root_no_name(fs_info, &key);
2284 if (IS_ERR(root)) {
2285 srcu_read_unlock(&fs_info->subvol_srcu, index);
2286 if (PTR_ERR(root) == -ENOENT)
2287 return 0;
2288 return PTR_ERR(root);
2289 }
Liu Bo38c227d2013-01-29 03:18:40 +00002290
2291 /* step 2: get inode */
2292 key.objectid = backref->inum;
2293 key.type = BTRFS_INODE_ITEM_KEY;
2294 key.offset = 0;
2295
2296 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2297 if (IS_ERR(inode)) {
2298 srcu_read_unlock(&fs_info->subvol_srcu, index);
2299 return 0;
2300 }
2301
2302 srcu_read_unlock(&fs_info->subvol_srcu, index);
2303
2304 /* step 3: relink backref */
2305 lock_start = backref->file_pos;
2306 lock_end = backref->file_pos + backref->num_bytes - 1;
2307 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2308 0, &cached);
2309
2310 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2311 if (ordered) {
2312 btrfs_put_ordered_extent(ordered);
2313 goto out_unlock;
2314 }
2315
2316 trans = btrfs_join_transaction(root);
2317 if (IS_ERR(trans)) {
2318 ret = PTR_ERR(trans);
2319 goto out_unlock;
2320 }
2321
2322 key.objectid = backref->inum;
2323 key.type = BTRFS_EXTENT_DATA_KEY;
2324 key.offset = backref->file_pos;
2325
2326 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2327 if (ret < 0) {
2328 goto out_free_path;
2329 } else if (ret > 0) {
2330 ret = 0;
2331 goto out_free_path;
2332 }
2333
2334 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2335 struct btrfs_file_extent_item);
2336
2337 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2338 backref->generation)
2339 goto out_free_path;
2340
2341 btrfs_release_path(path);
2342
2343 start = backref->file_pos;
2344 if (backref->extent_offset < old->extent_offset + old->offset)
2345 start += old->extent_offset + old->offset -
2346 backref->extent_offset;
2347
2348 len = min(backref->extent_offset + backref->num_bytes,
2349 old->extent_offset + old->offset + old->len);
2350 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2351
2352 ret = btrfs_drop_extents(trans, root, inode, start,
2353 start + len, 1);
2354 if (ret)
2355 goto out_free_path;
2356again:
2357 key.objectid = btrfs_ino(inode);
2358 key.type = BTRFS_EXTENT_DATA_KEY;
2359 key.offset = start;
2360
Liu Boa09a0a72013-03-11 09:20:58 +00002361 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002362 if (merge) {
2363 struct btrfs_file_extent_item *fi;
2364 u64 extent_len;
2365 struct btrfs_key found_key;
2366
2367 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2368 if (ret < 0)
2369 goto out_free_path;
2370
2371 path->slots[0]--;
2372 leaf = path->nodes[0];
2373 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2374
2375 fi = btrfs_item_ptr(leaf, path->slots[0],
2376 struct btrfs_file_extent_item);
2377 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2378
2379 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2380 extent_len + found_key.offset == start) {
2381 btrfs_set_file_extent_num_bytes(leaf, fi,
2382 extent_len + len);
2383 btrfs_mark_buffer_dirty(leaf);
2384 inode_add_bytes(inode, len);
2385
2386 ret = 1;
2387 goto out_free_path;
2388 } else {
2389 merge = false;
2390 btrfs_release_path(path);
2391 goto again;
2392 }
2393 }
2394
2395 ret = btrfs_insert_empty_item(trans, root, path, &key,
2396 sizeof(*extent));
2397 if (ret) {
2398 btrfs_abort_transaction(trans, root, ret);
2399 goto out_free_path;
2400 }
2401
2402 leaf = path->nodes[0];
2403 item = btrfs_item_ptr(leaf, path->slots[0],
2404 struct btrfs_file_extent_item);
2405 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2406 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2407 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2408 btrfs_set_file_extent_num_bytes(leaf, item, len);
2409 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2410 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2411 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2412 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2413 btrfs_set_file_extent_encryption(leaf, item, 0);
2414 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2415
2416 btrfs_mark_buffer_dirty(leaf);
2417 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002418 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002419
2420 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2421 new->disk_len, 0,
2422 backref->root_id, backref->inum,
2423 new->file_pos, 0); /* start - extent_offset */
2424 if (ret) {
2425 btrfs_abort_transaction(trans, root, ret);
2426 goto out_free_path;
2427 }
2428
2429 ret = 1;
2430out_free_path:
2431 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002432 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002433 btrfs_end_transaction(trans, root);
2434out_unlock:
2435 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2436 &cached, GFP_NOFS);
2437 iput(inode);
2438 return ret;
2439}
2440
2441static void relink_file_extents(struct new_sa_defrag_extent *new)
2442{
2443 struct btrfs_path *path;
2444 struct old_sa_defrag_extent *old, *tmp;
2445 struct sa_defrag_extent_backref *backref;
2446 struct sa_defrag_extent_backref *prev = NULL;
2447 struct inode *inode;
2448 struct btrfs_root *root;
2449 struct rb_node *node;
2450 int ret;
2451
2452 inode = new->inode;
2453 root = BTRFS_I(inode)->root;
2454
2455 path = btrfs_alloc_path();
2456 if (!path)
2457 return;
2458
2459 if (!record_extent_backrefs(path, new)) {
2460 btrfs_free_path(path);
2461 goto out;
2462 }
2463 btrfs_release_path(path);
2464
2465 while (1) {
2466 node = rb_first(&new->root);
2467 if (!node)
2468 break;
2469 rb_erase(node, &new->root);
2470
2471 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2472
2473 ret = relink_extent_backref(path, prev, backref);
2474 WARN_ON(ret < 0);
2475
2476 kfree(prev);
2477
2478 if (ret == 1)
2479 prev = backref;
2480 else
2481 prev = NULL;
2482 cond_resched();
2483 }
2484 kfree(prev);
2485
2486 btrfs_free_path(path);
2487
2488 list_for_each_entry_safe(old, tmp, &new->head, list) {
2489 list_del(&old->list);
2490 kfree(old);
2491 }
2492out:
2493 atomic_dec(&root->fs_info->defrag_running);
2494 wake_up(&root->fs_info->transaction_wait);
2495
2496 kfree(new);
2497}
2498
2499static struct new_sa_defrag_extent *
2500record_old_file_extents(struct inode *inode,
2501 struct btrfs_ordered_extent *ordered)
2502{
2503 struct btrfs_root *root = BTRFS_I(inode)->root;
2504 struct btrfs_path *path;
2505 struct btrfs_key key;
2506 struct old_sa_defrag_extent *old, *tmp;
2507 struct new_sa_defrag_extent *new;
2508 int ret;
2509
2510 new = kmalloc(sizeof(*new), GFP_NOFS);
2511 if (!new)
2512 return NULL;
2513
2514 new->inode = inode;
2515 new->file_pos = ordered->file_offset;
2516 new->len = ordered->len;
2517 new->bytenr = ordered->start;
2518 new->disk_len = ordered->disk_len;
2519 new->compress_type = ordered->compress_type;
2520 new->root = RB_ROOT;
2521 INIT_LIST_HEAD(&new->head);
2522
2523 path = btrfs_alloc_path();
2524 if (!path)
2525 goto out_kfree;
2526
2527 key.objectid = btrfs_ino(inode);
2528 key.type = BTRFS_EXTENT_DATA_KEY;
2529 key.offset = new->file_pos;
2530
2531 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2532 if (ret < 0)
2533 goto out_free_path;
2534 if (ret > 0 && path->slots[0] > 0)
2535 path->slots[0]--;
2536
2537 /* find out all the old extents for the file range */
2538 while (1) {
2539 struct btrfs_file_extent_item *extent;
2540 struct extent_buffer *l;
2541 int slot;
2542 u64 num_bytes;
2543 u64 offset;
2544 u64 end;
2545 u64 disk_bytenr;
2546 u64 extent_offset;
2547
2548 l = path->nodes[0];
2549 slot = path->slots[0];
2550
2551 if (slot >= btrfs_header_nritems(l)) {
2552 ret = btrfs_next_leaf(root, path);
2553 if (ret < 0)
2554 goto out_free_list;
2555 else if (ret > 0)
2556 break;
2557 continue;
2558 }
2559
2560 btrfs_item_key_to_cpu(l, &key, slot);
2561
2562 if (key.objectid != btrfs_ino(inode))
2563 break;
2564 if (key.type != BTRFS_EXTENT_DATA_KEY)
2565 break;
2566 if (key.offset >= new->file_pos + new->len)
2567 break;
2568
2569 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2570
2571 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2572 if (key.offset + num_bytes < new->file_pos)
2573 goto next;
2574
2575 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2576 if (!disk_bytenr)
2577 goto next;
2578
2579 extent_offset = btrfs_file_extent_offset(l, extent);
2580
2581 old = kmalloc(sizeof(*old), GFP_NOFS);
2582 if (!old)
2583 goto out_free_list;
2584
2585 offset = max(new->file_pos, key.offset);
2586 end = min(new->file_pos + new->len, key.offset + num_bytes);
2587
2588 old->bytenr = disk_bytenr;
2589 old->extent_offset = extent_offset;
2590 old->offset = offset - key.offset;
2591 old->len = end - offset;
2592 old->new = new;
2593 old->count = 0;
2594 list_add_tail(&old->list, &new->head);
2595next:
2596 path->slots[0]++;
2597 cond_resched();
2598 }
2599
2600 btrfs_free_path(path);
2601 atomic_inc(&root->fs_info->defrag_running);
2602
2603 return new;
2604
2605out_free_list:
2606 list_for_each_entry_safe(old, tmp, &new->head, list) {
2607 list_del(&old->list);
2608 kfree(old);
2609 }
2610out_free_path:
2611 btrfs_free_path(path);
2612out_kfree:
2613 kfree(new);
2614 return NULL;
2615}
2616
Chris Mason5d13a982009-03-13 11:41:46 -04002617/*
2618 * helper function for btrfs_finish_ordered_io, this
2619 * just reads in some of the csum leaves to prime them into ram
2620 * before we start the transaction. It limits the amount of btree
2621 * reads required while inside the transaction.
2622 */
Chris Masond352ac62008-09-29 15:18:18 -04002623/* as ordered data IO finishes, this gets called so we can finish
2624 * an ordered extent if the range of bytes in the file it covers are
2625 * fully written.
2626 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002627static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002628{
Josef Bacik5fd02042012-05-02 14:00:54 -04002629 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002630 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002631 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002632 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002633 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002634 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002635 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002636 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002637 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002638
Liu Bo83eea1f2012-07-10 05:28:39 -06002639 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002640
Josef Bacik5fd02042012-05-02 14:00:54 -04002641 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2642 ret = -EIO;
2643 goto out;
2644 }
2645
Yan, Zhengc2167752009-11-12 09:34:21 +00002646 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002647 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002648 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2649 if (nolock)
2650 trans = btrfs_join_transaction_nolock(root);
2651 else
2652 trans = btrfs_join_transaction(root);
2653 if (IS_ERR(trans)) {
2654 ret = PTR_ERR(trans);
2655 trans = NULL;
2656 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002657 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002658 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2659 ret = btrfs_update_inode_fallback(trans, root, inode);
2660 if (ret) /* -ENOMEM or corruption */
2661 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002662 goto out;
2663 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002664
Josef Bacik2ac55d42010-02-03 19:33:23 +00002665 lock_extent_bits(io_tree, ordered_extent->file_offset,
2666 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002667 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002668
Liu Bo38c227d2013-01-29 03:18:40 +00002669 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2670 ordered_extent->file_offset + ordered_extent->len - 1,
2671 EXTENT_DEFRAG, 1, cached_state);
2672 if (ret) {
2673 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2674 if (last_snapshot >= BTRFS_I(inode)->generation)
2675 /* the inode is shared */
2676 new = record_old_file_extents(inode, ordered_extent);
2677
2678 clear_extent_bit(io_tree, ordered_extent->file_offset,
2679 ordered_extent->file_offset + ordered_extent->len - 1,
2680 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2681 }
2682
Josef Bacik0cb59c92010-07-02 12:14:14 -04002683 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002684 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002685 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002686 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002687 if (IS_ERR(trans)) {
2688 ret = PTR_ERR(trans);
2689 trans = NULL;
2690 goto out_unlock;
2691 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002692 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002693
Chris Masonc8b97812008-10-29 14:49:59 -04002694 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002695 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002696 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002697 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002698 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002699 ordered_extent->file_offset,
2700 ordered_extent->file_offset +
2701 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002702 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002703 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002704 ret = insert_reserved_file_extent(trans, inode,
2705 ordered_extent->file_offset,
2706 ordered_extent->start,
2707 ordered_extent->disk_len,
2708 ordered_extent->len,
2709 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002710 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002711 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002712 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002713 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2714 ordered_extent->file_offset, ordered_extent->len,
2715 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002716 if (ret < 0) {
2717 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002718 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002719 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002720
Chris Masone6dcd2d2008-07-17 12:53:50 -04002721 add_pending_csums(trans, inode, ordered_extent->file_offset,
2722 &ordered_extent->list);
2723
Josef Bacik6c760c02012-11-09 10:53:21 -05002724 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2725 ret = btrfs_update_inode_fallback(trans, root, inode);
2726 if (ret) { /* -ENOMEM or corruption */
2727 btrfs_abort_transaction(trans, root, ret);
2728 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002729 }
2730 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002731out_unlock:
2732 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2733 ordered_extent->file_offset +
2734 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002735out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002736 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002737 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002738 if (trans)
2739 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002740
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002741 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002742 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2743 ordered_extent->file_offset +
2744 ordered_extent->len - 1, NULL, GFP_NOFS);
2745
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002746 /*
2747 * If the ordered extent had an IOERR or something else went
2748 * wrong we need to return the space for this ordered extent
2749 * back to the allocator.
2750 */
2751 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2752 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2753 btrfs_free_reserved_extent(root, ordered_extent->start,
2754 ordered_extent->disk_len);
2755 }
2756
2757
Josef Bacik5fd02042012-05-02 14:00:54 -04002758 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002759 * This needs to be done to make sure anybody waiting knows we are done
2760 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002761 */
2762 btrfs_remove_ordered_extent(inode, ordered_extent);
2763
Liu Bo38c227d2013-01-29 03:18:40 +00002764 /* for snapshot-aware defrag */
2765 if (new)
2766 relink_file_extents(new);
2767
Chris Masone6dcd2d2008-07-17 12:53:50 -04002768 /* once for us */
2769 btrfs_put_ordered_extent(ordered_extent);
2770 /* once for the tree */
2771 btrfs_put_ordered_extent(ordered_extent);
2772
Josef Bacik5fd02042012-05-02 14:00:54 -04002773 return ret;
2774}
2775
2776static void finish_ordered_fn(struct btrfs_work *work)
2777{
2778 struct btrfs_ordered_extent *ordered_extent;
2779 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2780 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002781}
2782
Christoph Hellwigb2950862008-12-02 09:54:17 -05002783static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002784 struct extent_state *state, int uptodate)
2785{
Josef Bacik5fd02042012-05-02 14:00:54 -04002786 struct inode *inode = page->mapping->host;
2787 struct btrfs_root *root = BTRFS_I(inode)->root;
2788 struct btrfs_ordered_extent *ordered_extent = NULL;
2789 struct btrfs_workers *workers;
2790
liubo1abe9b82011-03-24 11:18:59 +00002791 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2792
Chris Mason8b62b722009-09-02 16:53:46 -04002793 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002794 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2795 end - start + 1, uptodate))
2796 return 0;
2797
2798 ordered_extent->work.func = finish_ordered_fn;
2799 ordered_extent->work.flags = 0;
2800
Liu Bo83eea1f2012-07-10 05:28:39 -06002801 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002802 workers = &root->fs_info->endio_freespace_worker;
2803 else
2804 workers = &root->fs_info->endio_write_workers;
2805 btrfs_queue_worker(workers, &ordered_extent->work);
2806
2807 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002808}
2809
Chris Masond352ac62008-09-29 15:18:18 -04002810/*
Chris Masond352ac62008-09-29 15:18:18 -04002811 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002812 * if there's a match, we allow the bio to finish. If not, the code in
2813 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002814 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002815static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002816 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002817{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002818 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002819 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002820 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002821 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002822 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002823 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002824 struct btrfs_root *root = BTRFS_I(inode)->root;
2825 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002826 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2827 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002828
Chris Masond20f7042008-12-08 16:58:54 -05002829 if (PageChecked(page)) {
2830 ClearPageChecked(page);
2831 goto good;
2832 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002833
2834 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002835 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002836
Yan Zheng17d217f2008-12-12 10:03:38 -05002837 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002838 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002839 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2840 GFP_NOFS);
2841 return 0;
2842 }
2843
Yanc2e639f2008-02-04 08:57:25 -05002844 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002845 private = state->private;
2846 ret = 0;
2847 } else {
2848 ret = get_state_private(io_tree, start, &private);
2849 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002850 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002851 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002852 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002853
Liu Bob0496682013-03-14 14:57:45 +00002854 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002855 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002856 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002857 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002858
Cong Wang7ac687d2011-11-25 23:14:28 +08002859 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002860good:
Chris Mason07157aa2007-08-30 08:50:51 -04002861 return 0;
2862
2863zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002864 if (__ratelimit(&_rs))
2865 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2866 (unsigned long long)btrfs_ino(page->mapping->host),
2867 (unsigned long long)start, csum,
2868 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002869 memset(kaddr + offset, 1, end - start + 1);
2870 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002871 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002872 if (private == 0)
2873 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002874 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002875}
Chris Masonb888db2b2007-08-27 16:49:44 -04002876
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002877struct delayed_iput {
2878 struct list_head list;
2879 struct inode *inode;
2880};
2881
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002882/* JDM: If this is fs-wide, why can't we add a pointer to
2883 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002884void btrfs_add_delayed_iput(struct inode *inode)
2885{
2886 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2887 struct delayed_iput *delayed;
2888
2889 if (atomic_add_unless(&inode->i_count, -1, 1))
2890 return;
2891
2892 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2893 delayed->inode = inode;
2894
2895 spin_lock(&fs_info->delayed_iput_lock);
2896 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2897 spin_unlock(&fs_info->delayed_iput_lock);
2898}
2899
2900void btrfs_run_delayed_iputs(struct btrfs_root *root)
2901{
2902 LIST_HEAD(list);
2903 struct btrfs_fs_info *fs_info = root->fs_info;
2904 struct delayed_iput *delayed;
2905 int empty;
2906
2907 spin_lock(&fs_info->delayed_iput_lock);
2908 empty = list_empty(&fs_info->delayed_iputs);
2909 spin_unlock(&fs_info->delayed_iput_lock);
2910 if (empty)
2911 return;
2912
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002913 spin_lock(&fs_info->delayed_iput_lock);
2914 list_splice_init(&fs_info->delayed_iputs, &list);
2915 spin_unlock(&fs_info->delayed_iput_lock);
2916
2917 while (!list_empty(&list)) {
2918 delayed = list_entry(list.next, struct delayed_iput, list);
2919 list_del(&delayed->list);
2920 iput(delayed->inode);
2921 kfree(delayed);
2922 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002923}
2924
Yan, Zhengd68fc572010-05-16 10:49:58 -04002925/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002926 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002927 * files in the subvolume, it removes orphan item and frees block_rsv
2928 * structure.
2929 */
2930void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2931 struct btrfs_root *root)
2932{
Josef Bacik90290e12011-12-02 15:44:12 -05002933 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002934 int ret;
2935
Josef Bacik8a35d952012-05-23 14:26:42 -04002936 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002937 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2938 return;
2939
Josef Bacik90290e12011-12-02 15:44:12 -05002940 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002941 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002942 spin_unlock(&root->orphan_lock);
2943 return;
2944 }
2945
2946 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2947 spin_unlock(&root->orphan_lock);
2948 return;
2949 }
2950
2951 block_rsv = root->orphan_block_rsv;
2952 root->orphan_block_rsv = NULL;
2953 spin_unlock(&root->orphan_lock);
2954
Yan, Zhengd68fc572010-05-16 10:49:58 -04002955 if (root->orphan_item_inserted &&
2956 btrfs_root_refs(&root->root_item) > 0) {
2957 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2958 root->root_key.objectid);
2959 BUG_ON(ret);
2960 root->orphan_item_inserted = 0;
2961 }
2962
Josef Bacik90290e12011-12-02 15:44:12 -05002963 if (block_rsv) {
2964 WARN_ON(block_rsv->size > 0);
2965 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 }
2967}
2968
2969/*
Josef Bacik7b128762008-07-24 12:17:14 -04002970 * This creates an orphan entry for the given inode in case something goes
2971 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 *
2973 * NOTE: caller of this function should reserve 5 units of metadata for
2974 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002975 */
2976int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2977{
2978 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002979 struct btrfs_block_rsv *block_rsv = NULL;
2980 int reserve = 0;
2981 int insert = 0;
2982 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002983
Yan, Zhengd68fc572010-05-16 10:49:58 -04002984 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002985 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002986 if (!block_rsv)
2987 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002988 }
2989
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 spin_lock(&root->orphan_lock);
2991 if (!root->orphan_block_rsv) {
2992 root->orphan_block_rsv = block_rsv;
2993 } else if (block_rsv) {
2994 btrfs_free_block_rsv(root, block_rsv);
2995 block_rsv = NULL;
2996 }
Josef Bacik7b128762008-07-24 12:17:14 -04002997
Josef Bacik8a35d952012-05-23 14:26:42 -04002998 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2999 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003000#if 0
3001 /*
3002 * For proper ENOSPC handling, we should do orphan
3003 * cleanup when mounting. But this introduces backward
3004 * compatibility issue.
3005 */
3006 if (!xchg(&root->orphan_item_inserted, 1))
3007 insert = 2;
3008 else
3009 insert = 1;
3010#endif
3011 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003012 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003013 }
Josef Bacik7b128762008-07-24 12:17:14 -04003014
Josef Bacik72ac3c02012-05-23 14:13:11 -04003015 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3016 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003019
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 /* grab metadata reservation from transaction handle */
3021 if (reserve) {
3022 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003023 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 }
3025
3026 /* insert an orphan item to track this unlinked/truncated file */
3027 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003028 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003029 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003030 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3031 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003032 btrfs_abort_transaction(trans, root, ret);
3033 return ret;
3034 }
3035 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003036 }
3037
3038 /* insert an orphan item to track subvolume contains orphan files */
3039 if (insert >= 2) {
3040 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3041 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003042 if (ret && ret != -EEXIST) {
3043 btrfs_abort_transaction(trans, root, ret);
3044 return ret;
3045 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 }
3047 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003048}
3049
3050/*
3051 * We have done the truncate/delete so we can go ahead and remove the orphan
3052 * item for this particular inode.
3053 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003054static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3055 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003056{
3057 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003058 int delete_item = 0;
3059 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003060 int ret = 0;
3061
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003063 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3064 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003065 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003066
Josef Bacik72ac3c02012-05-23 14:13:11 -04003067 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3068 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003069 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003070 spin_unlock(&root->orphan_lock);
3071
3072 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003073 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003074 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003075 }
3076
Josef Bacik8a35d952012-05-23 14:26:42 -04003077 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003078 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003079 atomic_dec(&root->orphan_inodes);
3080 }
Josef Bacik7b128762008-07-24 12:17:14 -04003081
Yan, Zhengd68fc572010-05-16 10:49:58 -04003082 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003083}
3084
3085/*
3086 * this cleans up any orphans that may be left on the list from the last use
3087 * of this root.
3088 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003089int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003090{
3091 struct btrfs_path *path;
3092 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003093 struct btrfs_key key, found_key;
3094 struct btrfs_trans_handle *trans;
3095 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003096 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003097 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3098
Yan, Zhengd68fc572010-05-16 10:49:58 -04003099 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003100 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003101
3102 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003103 if (!path) {
3104 ret = -ENOMEM;
3105 goto out;
3106 }
Josef Bacik7b128762008-07-24 12:17:14 -04003107 path->reada = -1;
3108
3109 key.objectid = BTRFS_ORPHAN_OBJECTID;
3110 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3111 key.offset = (u64)-1;
3112
Josef Bacik7b128762008-07-24 12:17:14 -04003113 while (1) {
3114 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003115 if (ret < 0)
3116 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003117
3118 /*
3119 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003120 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003121 * find the key and see if we have stuff that matches
3122 */
3123 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003124 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003125 if (path->slots[0] == 0)
3126 break;
3127 path->slots[0]--;
3128 }
3129
3130 /* pull out the item */
3131 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003132 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3133
3134 /* make sure the item matches what we want */
3135 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3136 break;
3137 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3138 break;
3139
3140 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003141 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003142
3143 /*
3144 * this is where we are basically btrfs_lookup, without the
3145 * crossing root thing. we store the inode number in the
3146 * offset of the orphan item.
3147 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003148
3149 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003150 btrfs_err(root->fs_info,
3151 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003152 ret = -EINVAL;
3153 goto out;
3154 }
3155
3156 last_objectid = found_key.offset;
3157
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003158 found_key.objectid = found_key.offset;
3159 found_key.type = BTRFS_INODE_ITEM_KEY;
3160 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003161 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003162 ret = PTR_RET(inode);
3163 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003164 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003165
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003166 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3167 struct btrfs_root *dead_root;
3168 struct btrfs_fs_info *fs_info = root->fs_info;
3169 int is_dead_root = 0;
3170
3171 /*
3172 * this is an orphan in the tree root. Currently these
3173 * could come from 2 sources:
3174 * a) a snapshot deletion in progress
3175 * b) a free space cache inode
3176 * We need to distinguish those two, as the snapshot
3177 * orphan must not get deleted.
3178 * find_dead_roots already ran before us, so if this
3179 * is a snapshot deletion, we should find the root
3180 * in the dead_roots list
3181 */
3182 spin_lock(&fs_info->trans_lock);
3183 list_for_each_entry(dead_root, &fs_info->dead_roots,
3184 root_list) {
3185 if (dead_root->root_key.objectid ==
3186 found_key.objectid) {
3187 is_dead_root = 1;
3188 break;
3189 }
3190 }
3191 spin_unlock(&fs_info->trans_lock);
3192 if (is_dead_root) {
3193 /* prevent this orphan from being found again */
3194 key.offset = found_key.objectid - 1;
3195 continue;
3196 }
3197 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003198 /*
3199 * Inode is already gone but the orphan item is still there,
3200 * kill the orphan item.
3201 */
3202 if (ret == -ESTALE) {
3203 trans = btrfs_start_transaction(root, 1);
3204 if (IS_ERR(trans)) {
3205 ret = PTR_ERR(trans);
3206 goto out;
3207 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003208 btrfs_debug(root->fs_info, "auto deleting %Lu",
3209 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003210 ret = btrfs_del_orphan_item(trans, root,
3211 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003212 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003213 btrfs_end_transaction(trans, root);
3214 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003215 }
Josef Bacik7b128762008-07-24 12:17:14 -04003216
Josef Bacik7b128762008-07-24 12:17:14 -04003217 /*
3218 * add this inode to the orphan list so btrfs_orphan_del does
3219 * the proper thing when we hit it
3220 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003221 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3222 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003223 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003224
Josef Bacik7b128762008-07-24 12:17:14 -04003225 /* if we have links, this was a truncate, lets do that */
3226 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003227 if (!S_ISREG(inode->i_mode)) {
3228 WARN_ON(1);
3229 iput(inode);
3230 continue;
3231 }
Josef Bacik7b128762008-07-24 12:17:14 -04003232 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003233
3234 /* 1 for the orphan item deletion. */
3235 trans = btrfs_start_transaction(root, 1);
3236 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003237 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003238 ret = PTR_ERR(trans);
3239 goto out;
3240 }
3241 ret = btrfs_orphan_add(trans, inode);
3242 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003243 if (ret) {
3244 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003245 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003246 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003247
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003248 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003249 if (ret)
3250 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003251 } else {
3252 nr_unlink++;
3253 }
3254
3255 /* this will do delete_inode and everything for us */
3256 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003257 if (ret)
3258 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003259 }
Miao Xie3254c872011-11-10 20:45:05 -05003260 /* release the path since we're done with it */
3261 btrfs_release_path(path);
3262
Yan, Zhengd68fc572010-05-16 10:49:58 -04003263 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3264
3265 if (root->orphan_block_rsv)
3266 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3267 (u64)-1);
3268
3269 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003270 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003271 if (!IS_ERR(trans))
3272 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003273 }
Josef Bacik7b128762008-07-24 12:17:14 -04003274
3275 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003276 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003277 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003278 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003279
3280out:
3281 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003282 btrfs_crit(root->fs_info,
3283 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003284 btrfs_free_path(path);
3285 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003286}
3287
Chris Masond352ac62008-09-29 15:18:18 -04003288/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003289 * very simple check to peek ahead in the leaf looking for xattrs. If we
3290 * don't find any xattrs, we know there can't be any acls.
3291 *
3292 * slot is the slot the inode is in, objectid is the objectid of the inode
3293 */
3294static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3295 int slot, u64 objectid)
3296{
3297 u32 nritems = btrfs_header_nritems(leaf);
3298 struct btrfs_key found_key;
3299 int scanned = 0;
3300
3301 slot++;
3302 while (slot < nritems) {
3303 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3304
3305 /* we found a different objectid, there must not be acls */
3306 if (found_key.objectid != objectid)
3307 return 0;
3308
3309 /* we found an xattr, assume we've got an acl */
3310 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3311 return 1;
3312
3313 /*
3314 * we found a key greater than an xattr key, there can't
3315 * be any acls later on
3316 */
3317 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3318 return 0;
3319
3320 slot++;
3321 scanned++;
3322
3323 /*
3324 * it goes inode, inode backrefs, xattrs, extents,
3325 * so if there are a ton of hard links to an inode there can
3326 * be a lot of backrefs. Don't waste time searching too hard,
3327 * this is just an optimization
3328 */
3329 if (scanned >= 8)
3330 break;
3331 }
3332 /* we hit the end of the leaf before we found an xattr or
3333 * something larger than an xattr. We have to assume the inode
3334 * has acls
3335 */
3336 return 1;
3337}
3338
3339/*
Chris Masond352ac62008-09-29 15:18:18 -04003340 * read an inode from the btree into the in-memory inode
3341 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003342static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003343{
3344 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003345 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003346 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003347 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003348 struct btrfs_root *root = BTRFS_I(inode)->root;
3349 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003350 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003351 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003352 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003353 bool filled = false;
3354
3355 ret = btrfs_fill_inode(inode, &rdev);
3356 if (!ret)
3357 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003358
3359 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003360 if (!path)
3361 goto make_bad;
3362
Josef Bacikd90c7322011-05-17 09:50:54 -04003363 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003364 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003365
Chris Mason39279cc2007-06-12 06:35:45 -04003366 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003367 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003368 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003369
Chris Mason5f39d392007-10-15 16:14:19 -04003370 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003371
3372 if (filled)
3373 goto cache_acl;
3374
Chris Mason5f39d392007-10-15 16:14:19 -04003375 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3376 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003377 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003378 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003379 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3380 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003381 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003382
3383 tspec = btrfs_inode_atime(inode_item);
3384 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3385 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3386
3387 tspec = btrfs_inode_mtime(inode_item);
3388 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3389 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3390
3391 tspec = btrfs_inode_ctime(inode_item);
3392 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3393 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3394
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003395 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003396 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003397 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3398
3399 /*
3400 * If we were modified in the current generation and evicted from memory
3401 * and then re-read we need to do a full sync since we don't have any
3402 * idea about which extents were modified before we were evicted from
3403 * cache.
3404 */
3405 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3406 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3407 &BTRFS_I(inode)->runtime_flags);
3408
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003409 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003410 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003411 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003412 rdev = btrfs_inode_rdev(leaf, inode_item);
3413
Josef Bacikaec74772008-07-24 12:12:38 -04003414 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003415 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003416cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003417 /*
3418 * try to precache a NULL acl entry for files that don't have
3419 * any xattrs or acls
3420 */
Li Zefan33345d012011-04-20 10:31:50 +08003421 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3422 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003423 if (!maybe_acls)
3424 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003425
Chris Mason39279cc2007-06-12 06:35:45 -04003426 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003427
Chris Mason39279cc2007-06-12 06:35:45 -04003428 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003429 case S_IFREG:
3430 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003431 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003432 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003433 inode->i_fop = &btrfs_file_operations;
3434 inode->i_op = &btrfs_file_inode_operations;
3435 break;
3436 case S_IFDIR:
3437 inode->i_fop = &btrfs_dir_file_operations;
3438 if (root == root->fs_info->tree_root)
3439 inode->i_op = &btrfs_dir_ro_inode_operations;
3440 else
3441 inode->i_op = &btrfs_dir_inode_operations;
3442 break;
3443 case S_IFLNK:
3444 inode->i_op = &btrfs_symlink_inode_operations;
3445 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003446 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003447 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003448 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003449 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003450 init_special_inode(inode, inode->i_mode, rdev);
3451 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003452 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003453
3454 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003455 return;
3456
3457make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003458 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003459 make_bad_inode(inode);
3460}
3461
Chris Masond352ac62008-09-29 15:18:18 -04003462/*
3463 * given a leaf and an inode, copy the inode fields into the leaf
3464 */
Chris Masone02119d2008-09-05 16:13:11 -04003465static void fill_inode_item(struct btrfs_trans_handle *trans,
3466 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003467 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003468 struct inode *inode)
3469{
Liu Bo51fab692012-12-27 09:01:21 +00003470 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003471
Liu Bo51fab692012-12-27 09:01:21 +00003472 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003473
Liu Bo51fab692012-12-27 09:01:21 +00003474 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3475 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3476 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3477 &token);
3478 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3479 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003480
Liu Bo51fab692012-12-27 09:01:21 +00003481 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3482 inode->i_atime.tv_sec, &token);
3483 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3484 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003485
Liu Bo51fab692012-12-27 09:01:21 +00003486 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3487 inode->i_mtime.tv_sec, &token);
3488 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3489 inode->i_mtime.tv_nsec, &token);
3490
3491 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3492 inode->i_ctime.tv_sec, &token);
3493 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3494 inode->i_ctime.tv_nsec, &token);
3495
3496 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3497 &token);
3498 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3499 &token);
3500 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3501 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3502 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3503 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3504 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003505}
3506
Chris Masond352ac62008-09-29 15:18:18 -04003507/*
3508 * copy everything in the in-memory inode into the btree.
3509 */
Chris Mason21151332011-11-10 20:39:08 -05003510static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003511 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003512{
3513 struct btrfs_inode_item *inode_item;
3514 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003515 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003516 int ret;
3517
3518 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003519 if (!path)
3520 return -ENOMEM;
3521
Chris Masonb9473432009-03-13 11:00:37 -04003522 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003523 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3524 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003525 if (ret) {
3526 if (ret > 0)
3527 ret = -ENOENT;
3528 goto failed;
3529 }
3530
Chris Masonb4ce94d2009-02-04 09:25:08 -05003531 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003532 leaf = path->nodes[0];
3533 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003534 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003535
Chris Masone02119d2008-09-05 16:13:11 -04003536 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003537 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003538 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003539 ret = 0;
3540failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003541 btrfs_free_path(path);
3542 return ret;
3543}
3544
Chris Masond352ac62008-09-29 15:18:18 -04003545/*
Chris Mason21151332011-11-10 20:39:08 -05003546 * copy everything in the in-memory inode into the btree.
3547 */
3548noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3549 struct btrfs_root *root, struct inode *inode)
3550{
3551 int ret;
3552
3553 /*
3554 * If the inode is a free space inode, we can deadlock during commit
3555 * if we put it into the delayed code.
3556 *
3557 * The data relocation inode should also be directly updated
3558 * without delay
3559 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003560 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003561 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003562 btrfs_update_root_times(trans, root);
3563
Chris Mason21151332011-11-10 20:39:08 -05003564 ret = btrfs_delayed_update_inode(trans, root, inode);
3565 if (!ret)
3566 btrfs_set_inode_last_trans(trans, inode);
3567 return ret;
3568 }
3569
3570 return btrfs_update_inode_item(trans, root, inode);
3571}
3572
Josef Bacikbe6aef62012-10-22 15:43:12 -04003573noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3574 struct btrfs_root *root,
3575 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003576{
3577 int ret;
3578
3579 ret = btrfs_update_inode(trans, root, inode);
3580 if (ret == -ENOSPC)
3581 return btrfs_update_inode_item(trans, root, inode);
3582 return ret;
3583}
3584
3585/*
Chris Masond352ac62008-09-29 15:18:18 -04003586 * unlink helper that gets used here in inode.c and in the tree logging
3587 * recovery code. It remove a link in a directory with a given name, and
3588 * also drops the back refs in the inode to the directory
3589 */
Al Viro92986792011-03-04 17:14:37 +00003590static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3591 struct btrfs_root *root,
3592 struct inode *dir, struct inode *inode,
3593 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003594{
3595 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003596 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003597 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003598 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003599 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003600 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003601 u64 ino = btrfs_ino(inode);
3602 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003603
3604 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003605 if (!path) {
3606 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003607 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003608 }
3609
Chris Masonb9473432009-03-13 11:00:37 -04003610 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003611 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003612 name, name_len, -1);
3613 if (IS_ERR(di)) {
3614 ret = PTR_ERR(di);
3615 goto err;
3616 }
3617 if (!di) {
3618 ret = -ENOENT;
3619 goto err;
3620 }
Chris Mason5f39d392007-10-15 16:14:19 -04003621 leaf = path->nodes[0];
3622 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003623 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003624 if (ret)
3625 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003626 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003627
Li Zefan33345d012011-04-20 10:31:50 +08003628 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3629 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003630 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003631 btrfs_info(root->fs_info,
3632 "failed to delete reference to %.*s, inode %llu parent %llu",
3633 name_len, name,
3634 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003635 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003636 goto err;
3637 }
3638
Miao Xie16cdcec2011-04-22 18:12:22 +08003639 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003640 if (ret) {
3641 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003642 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003643 }
Chris Mason39279cc2007-06-12 06:35:45 -04003644
Chris Masone02119d2008-09-05 16:13:11 -04003645 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003646 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003647 if (ret != 0 && ret != -ENOENT) {
3648 btrfs_abort_transaction(trans, root, ret);
3649 goto err;
3650 }
Chris Masone02119d2008-09-05 16:13:11 -04003651
3652 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3653 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003654 if (ret == -ENOENT)
3655 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003656 else if (ret)
3657 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003658err:
3659 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003660 if (ret)
3661 goto out;
3662
3663 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003664 inode_inc_iversion(inode);
3665 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003666 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003667 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003668out:
Chris Mason39279cc2007-06-12 06:35:45 -04003669 return ret;
3670}
3671
Al Viro92986792011-03-04 17:14:37 +00003672int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3673 struct btrfs_root *root,
3674 struct inode *dir, struct inode *inode,
3675 const char *name, int name_len)
3676{
3677 int ret;
3678 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3679 if (!ret) {
3680 btrfs_drop_nlink(inode);
3681 ret = btrfs_update_inode(trans, root, inode);
3682 }
3683 return ret;
3684}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685
3686/*
3687 * helper to start transaction for unlink and rmdir.
3688 *
Josef Bacikd52be812013-05-29 14:54:47 -04003689 * unlink and rmdir are special in btrfs, they do not always free space, so
3690 * if we cannot make our reservations the normal way try and see if there is
3691 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3692 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 */
Josef Bacikd52be812013-05-29 14:54:47 -04003694static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695{
3696 struct btrfs_trans_handle *trans;
3697 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698 int ret;
3699
Josef Bacike70bea52011-10-11 14:18:24 -04003700 /*
3701 * 1 for the possible orphan item
3702 * 1 for the dir item
3703 * 1 for the dir index
3704 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003705 * 1 for the inode
3706 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003707 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003708 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3709 return trans;
3710
Josef Bacikd52be812013-05-29 14:54:47 -04003711 if (PTR_ERR(trans) == -ENOSPC) {
3712 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713
Josef Bacikd52be812013-05-29 14:54:47 -04003714 trans = btrfs_start_transaction(root, 0);
3715 if (IS_ERR(trans))
3716 return trans;
3717 ret = btrfs_cond_migrate_bytes(root->fs_info,
3718 &root->fs_info->trans_block_rsv,
3719 num_bytes, 5);
3720 if (ret) {
3721 btrfs_end_transaction(trans, root);
3722 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003723 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003724 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003725 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003726 }
Josef Bacikd52be812013-05-29 14:54:47 -04003727 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728}
3729
Chris Mason39279cc2007-06-12 06:35:45 -04003730static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3731{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003733 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003734 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003735 int ret;
3736
Josef Bacikd52be812013-05-29 14:54:47 -04003737 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003738 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003739 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003740
Chris Mason12fcfd22009-03-24 10:24:20 -04003741 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3742
Chris Masone02119d2008-09-05 16:13:11 -04003743 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3744 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003745 if (ret)
3746 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003747
Yan, Zhenga22285a2010-05-16 10:48:46 -04003748 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003749 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003750 if (ret)
3751 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 }
Josef Bacik7b128762008-07-24 12:17:14 -04003753
Tsutomu Itohb5324022011-07-19 07:27:20 +00003754out:
Josef Bacikd52be812013-05-29 14:54:47 -04003755 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003756 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003757 return ret;
3758}
3759
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003760int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3761 struct btrfs_root *root,
3762 struct inode *dir, u64 objectid,
3763 const char *name, int name_len)
3764{
3765 struct btrfs_path *path;
3766 struct extent_buffer *leaf;
3767 struct btrfs_dir_item *di;
3768 struct btrfs_key key;
3769 u64 index;
3770 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003771 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003772
3773 path = btrfs_alloc_path();
3774 if (!path)
3775 return -ENOMEM;
3776
Li Zefan33345d012011-04-20 10:31:50 +08003777 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003778 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003779 if (IS_ERR_OR_NULL(di)) {
3780 if (!di)
3781 ret = -ENOENT;
3782 else
3783 ret = PTR_ERR(di);
3784 goto out;
3785 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003786
3787 leaf = path->nodes[0];
3788 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3789 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3790 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003791 if (ret) {
3792 btrfs_abort_transaction(trans, root, ret);
3793 goto out;
3794 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003795 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003796
3797 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3798 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003799 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003801 if (ret != -ENOENT) {
3802 btrfs_abort_transaction(trans, root, ret);
3803 goto out;
3804 }
Li Zefan33345d012011-04-20 10:31:50 +08003805 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003806 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 if (IS_ERR_OR_NULL(di)) {
3808 if (!di)
3809 ret = -ENOENT;
3810 else
3811 ret = PTR_ERR(di);
3812 btrfs_abort_transaction(trans, root, ret);
3813 goto out;
3814 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003815
3816 leaf = path->nodes[0];
3817 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003818 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819 index = key.offset;
3820 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003821 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003822
Miao Xie16cdcec2011-04-22 18:12:22 +08003823 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003824 if (ret) {
3825 btrfs_abort_transaction(trans, root, ret);
3826 goto out;
3827 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003828
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003829 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003830 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003831 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003832 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003833 if (ret)
3834 btrfs_abort_transaction(trans, root, ret);
3835out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003836 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003837 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003838}
3839
Chris Mason39279cc2007-06-12 06:35:45 -04003840static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3841{
3842 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003843 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003844 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003845 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003846
David Sterbab3ae2442012-09-13 16:04:34 -06003847 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003848 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003849 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3850 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003851
Josef Bacikd52be812013-05-29 14:54:47 -04003852 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003853 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003854 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003855
Li Zefan33345d012011-04-20 10:31:50 +08003856 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003857 err = btrfs_unlink_subvol(trans, root, dir,
3858 BTRFS_I(inode)->location.objectid,
3859 dentry->d_name.name,
3860 dentry->d_name.len);
3861 goto out;
3862 }
3863
Josef Bacik7b128762008-07-24 12:17:14 -04003864 err = btrfs_orphan_add(trans, inode);
3865 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003866 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003867
Chris Mason39279cc2007-06-12 06:35:45 -04003868 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003869 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3870 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003871 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003872 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003873out:
Josef Bacikd52be812013-05-29 14:54:47 -04003874 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003875 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003876
Chris Mason39279cc2007-06-12 06:35:45 -04003877 return err;
3878}
3879
Chris Mason323ac952008-10-01 19:05:46 -04003880/*
Chris Mason39279cc2007-06-12 06:35:45 -04003881 * this can truncate away extent items, csum items and directory items.
3882 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003883 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003884 *
3885 * csum items that cross the new i_size are truncated to the new size
3886 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003887 *
3888 * min_type is the minimum key type to truncate down to. If set to 0, this
3889 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003890 */
Yan, Zheng80825102009-11-12 09:35:36 +00003891int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3892 struct btrfs_root *root,
3893 struct inode *inode,
3894 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003895{
Chris Mason39279cc2007-06-12 06:35:45 -04003896 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003897 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003898 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003899 struct btrfs_key key;
3900 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003901 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003902 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003903 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003904 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003905 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 int found_extent;
3907 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003908 int pending_del_nr = 0;
3909 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003910 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003911 int ret;
3912 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003913 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003914
3915 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003916
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003917 path = btrfs_alloc_path();
3918 if (!path)
3919 return -ENOMEM;
3920 path->reada = -1;
3921
Josef Bacik5dc562c2012-08-17 13:14:17 -04003922 /*
3923 * We want to drop from the next block forward in case this new size is
3924 * not block aligned since we will be keeping the last block of the
3925 * extent just the way it is.
3926 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003927 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003928 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3929 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003930
Miao Xie16cdcec2011-04-22 18:12:22 +08003931 /*
3932 * This function is also used to drop the items in the log tree before
3933 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3934 * it is used to drop the loged items. So we shouldn't kill the delayed
3935 * items.
3936 */
3937 if (min_type == 0 && root == BTRFS_I(inode)->root)
3938 btrfs_kill_delayed_inode_items(inode);
3939
Li Zefan33345d012011-04-20 10:31:50 +08003940 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003941 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003942 key.type = (u8)-1;
3943
Chris Mason85e21ba2008-01-29 15:11:36 -05003944search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003945 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003946 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003947 if (ret < 0) {
3948 err = ret;
3949 goto out;
3950 }
Chris Masond3977122009-01-05 21:25:51 -05003951
Chris Mason85e21ba2008-01-29 15:11:36 -05003952 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003953 /* there are no items in the tree for us to truncate, we're
3954 * done
3955 */
Yan, Zheng80825102009-11-12 09:35:36 +00003956 if (path->slots[0] == 0)
3957 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003958 path->slots[0]--;
3959 }
3960
Chris Masond3977122009-01-05 21:25:51 -05003961 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003962 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003963 leaf = path->nodes[0];
3964 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3965 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003966
Li Zefan33345d012011-04-20 10:31:50 +08003967 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003968 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003969
Chris Mason85e21ba2008-01-29 15:11:36 -05003970 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003971 break;
3972
Chris Mason5f39d392007-10-15 16:14:19 -04003973 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003974 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003975 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003976 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003977 extent_type = btrfs_file_extent_type(leaf, fi);
3978 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003979 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003980 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003981 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003982 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003983 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003984 }
Yan008630c2007-11-07 13:31:09 -05003985 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003986 }
Yan, Zheng80825102009-11-12 09:35:36 +00003987 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003988 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003989 } else {
3990 if (item_end < new_size)
3991 break;
3992 if (found_key.offset >= new_size)
3993 del_item = 1;
3994 else
3995 del_item = 0;
3996 }
Chris Mason39279cc2007-06-12 06:35:45 -04003997 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003998 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003999 if (found_type != BTRFS_EXTENT_DATA_KEY)
4000 goto delete;
4001
4002 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004003 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004004 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004005 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004006 u64 orig_num_bytes =
4007 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004008 extent_num_bytes = ALIGN(new_size -
4009 found_key.offset,
4010 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004011 btrfs_set_file_extent_num_bytes(leaf, fi,
4012 extent_num_bytes);
4013 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004014 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004015 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004016 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004017 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004018 } else {
Chris Masondb945352007-10-15 16:15:53 -04004019 extent_num_bytes =
4020 btrfs_file_extent_disk_num_bytes(leaf,
4021 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004022 extent_offset = found_key.offset -
4023 btrfs_file_extent_offset(leaf, fi);
4024
Chris Mason39279cc2007-06-12 06:35:45 -04004025 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004026 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004027 if (extent_start != 0) {
4028 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004029 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004030 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004031 }
4032 }
Chris Mason90692182008-02-08 13:49:28 -05004033 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004034 /*
4035 * we can't truncate inline items that have had
4036 * special encodings
4037 */
4038 if (!del_item &&
4039 btrfs_file_extent_compression(leaf, fi) == 0 &&
4040 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4041 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004042 u32 size = new_size - found_key.offset;
4043
4044 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004045 inode_sub_bytes(inode, item_end + 1 -
4046 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004047 }
4048 size =
4049 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004050 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004051 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004052 inode_sub_bytes(inode, item_end + 1 -
4053 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004054 }
Chris Mason39279cc2007-06-12 06:35:45 -04004055 }
Chris Mason179e29e2007-11-01 11:28:41 -04004056delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004057 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004058 if (!pending_del_nr) {
4059 /* no pending yet, add ourselves */
4060 pending_del_slot = path->slots[0];
4061 pending_del_nr = 1;
4062 } else if (pending_del_nr &&
4063 path->slots[0] + 1 == pending_del_slot) {
4064 /* hop on the pending chunk */
4065 pending_del_nr++;
4066 pending_del_slot = path->slots[0];
4067 } else {
Chris Masond3977122009-01-05 21:25:51 -05004068 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004069 }
Chris Mason39279cc2007-06-12 06:35:45 -04004070 } else {
4071 break;
4072 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004073 if (found_extent && (root->ref_cows ||
4074 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004075 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004076 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004077 extent_num_bytes, 0,
4078 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004079 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004080 BUG_ON(ret);
4081 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004082
Yan, Zheng80825102009-11-12 09:35:36 +00004083 if (found_type == BTRFS_INODE_ITEM_KEY)
4084 break;
4085
4086 if (path->slots[0] == 0 ||
4087 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004088 if (pending_del_nr) {
4089 ret = btrfs_del_items(trans, root, path,
4090 pending_del_slot,
4091 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004092 if (ret) {
4093 btrfs_abort_transaction(trans,
4094 root, ret);
4095 goto error;
4096 }
Yan, Zheng80825102009-11-12 09:35:36 +00004097 pending_del_nr = 0;
4098 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004099 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004100 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004101 } else {
4102 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004103 }
Chris Mason39279cc2007-06-12 06:35:45 -04004104 }
Yan, Zheng80825102009-11-12 09:35:36 +00004105out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004106 if (pending_del_nr) {
4107 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4108 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004109 if (ret)
4110 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004111 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004112error:
Chris Mason39279cc2007-06-12 06:35:45 -04004113 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004114 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004115}
4116
Chris Masona52d9a82007-08-27 16:49:44 -04004117/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004118 * btrfs_truncate_page - read, zero a chunk and write a page
4119 * @inode - inode that we're zeroing
4120 * @from - the offset to start zeroing
4121 * @len - the length to zero, 0 to zero the entire range respective to the
4122 * offset
4123 * @front - zero up to the offset instead of from the offset on
4124 *
4125 * This will find the page for the "from" offset and cow the page and zero the
4126 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004127 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004128int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4129 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004130{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004131 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004132 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004133 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4134 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004135 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004136 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004137 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004138 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4139 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4140 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004141 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004142 int ret = 0;
4143 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004144 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004145
Josef Bacik2aaa6652012-08-29 14:27:18 -04004146 if ((offset & (blocksize - 1)) == 0 &&
4147 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004148 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004149 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004150 if (ret)
4151 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004152
Chris Mason211c17f2008-05-15 09:13:45 -04004153again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004154 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004155 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004156 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004157 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004158 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004159 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004160
4161 page_start = page_offset(page);
4162 page_end = page_start + PAGE_CACHE_SIZE - 1;
4163
Chris Masona52d9a82007-08-27 16:49:44 -04004164 if (!PageUptodate(page)) {
4165 ret = btrfs_readpage(NULL, page);
4166 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004167 if (page->mapping != mapping) {
4168 unlock_page(page);
4169 page_cache_release(page);
4170 goto again;
4171 }
Chris Masona52d9a82007-08-27 16:49:44 -04004172 if (!PageUptodate(page)) {
4173 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004174 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004175 }
4176 }
Chris Mason211c17f2008-05-15 09:13:45 -04004177 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004178
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004179 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004180 set_page_extent_mapped(page);
4181
4182 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4183 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004184 unlock_extent_cached(io_tree, page_start, page_end,
4185 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004186 unlock_page(page);
4187 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004188 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004189 btrfs_put_ordered_extent(ordered);
4190 goto again;
4191 }
4192
Josef Bacik2ac55d42010-02-03 19:33:23 +00004193 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004194 EXTENT_DIRTY | EXTENT_DELALLOC |
4195 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004196 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004197
Josef Bacik2ac55d42010-02-03 19:33:23 +00004198 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4199 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004200 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004201 unlock_extent_cached(io_tree, page_start, page_end,
4202 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004203 goto out_unlock;
4204 }
4205
Chris Masone6dcd2d2008-07-17 12:53:50 -04004206 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004207 if (!len)
4208 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004209 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004210 if (front)
4211 memset(kaddr, 0, offset);
4212 else
4213 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004214 flush_dcache_page(page);
4215 kunmap(page);
4216 }
Chris Mason247e7432008-07-17 12:53:51 -04004217 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004218 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004219 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4220 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004221
Chris Mason89642222008-07-24 09:41:53 -04004222out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004223 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004224 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004225 unlock_page(page);
4226 page_cache_release(page);
4227out:
4228 return ret;
4229}
4230
Josef Bacik695a0d02011-03-04 15:46:53 -05004231/*
4232 * This function puts in dummy file extents for the area we're creating a hole
4233 * for. So if we are truncating this file to a larger size we need to insert
4234 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4235 * the range between oldsize and size
4236 */
Josef Bacika41ad392011-01-31 15:30:16 -05004237int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004238{
4239 struct btrfs_trans_handle *trans;
4240 struct btrfs_root *root = BTRFS_I(inode)->root;
4241 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004242 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004243 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004244 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004245 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4246 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004247 u64 last_byte;
4248 u64 cur_offset;
4249 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004250 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004251
4252 if (size <= hole_start)
4253 return 0;
4254
Yan Zheng9036c102008-10-30 14:19:41 -04004255 while (1) {
4256 struct btrfs_ordered_extent *ordered;
4257 btrfs_wait_ordered_range(inode, hole_start,
4258 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004259 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004260 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004261 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4262 if (!ordered)
4263 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004264 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4265 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004266 btrfs_put_ordered_extent(ordered);
4267 }
4268
Yan Zheng9036c102008-10-30 14:19:41 -04004269 cur_offset = hole_start;
4270 while (1) {
4271 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4272 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (IS_ERR(em)) {
4274 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004275 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 break;
4277 }
Yan Zheng9036c102008-10-30 14:19:41 -04004278 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004279 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004280 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004281 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004282 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004283
Miao Xie36423202011-12-14 20:12:02 -05004284 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004285 if (IS_ERR(trans)) {
4286 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004287 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004288 }
Yan, Zheng80825102009-11-12 09:35:36 +00004289
Josef Bacik5dc562c2012-08-17 13:14:17 -04004290 err = btrfs_drop_extents(trans, root, inode,
4291 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004292 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004293 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004295 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004296 break;
Miao Xie5b397372011-09-11 10:52:24 -04004297 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004298
Yan Zheng9036c102008-10-30 14:19:41 -04004299 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004300 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004301 0, hole_size, 0, hole_size,
4302 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004303 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004304 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004305 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004306 break;
Miao Xie5b397372011-09-11 10:52:24 -04004307 }
Yan, Zheng80825102009-11-12 09:35:36 +00004308
Josef Bacik5dc562c2012-08-17 13:14:17 -04004309 btrfs_drop_extent_cache(inode, cur_offset,
4310 cur_offset + hole_size - 1, 0);
4311 hole_em = alloc_extent_map();
4312 if (!hole_em) {
4313 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4314 &BTRFS_I(inode)->runtime_flags);
4315 goto next;
4316 }
4317 hole_em->start = cur_offset;
4318 hole_em->len = hole_size;
4319 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004320
Josef Bacik5dc562c2012-08-17 13:14:17 -04004321 hole_em->block_start = EXTENT_MAP_HOLE;
4322 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004323 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004324 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004325 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4326 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4327 hole_em->generation = trans->transid;
4328
4329 while (1) {
4330 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004331 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004332 write_unlock(&em_tree->lock);
4333 if (err != -EEXIST)
4334 break;
4335 btrfs_drop_extent_cache(inode, cur_offset,
4336 cur_offset +
4337 hole_size - 1, 0);
4338 }
4339 free_extent_map(hole_em);
4340next:
Miao Xie36423202011-12-14 20:12:02 -05004341 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004342 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004343 }
4344 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004345 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004346 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004347 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004348 break;
4349 }
4350
Yan, Zhenga22285a2010-05-16 10:48:46 -04004351 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004352 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4353 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004354 return err;
4355}
4356
Eric Sandeen3972f262013-01-12 02:57:22 +00004357static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004358{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004359 struct btrfs_root *root = BTRFS_I(inode)->root;
4360 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004361 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004362 loff_t newsize = attr->ia_size;
4363 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004364 int ret;
4365
Josef Bacika41ad392011-01-31 15:30:16 -05004366 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004367 return 0;
4368
Eric Sandeen3972f262013-01-12 02:57:22 +00004369 /*
4370 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4371 * special case where we need to update the times despite not having
4372 * these flags set. For all other operations the VFS set these flags
4373 * explicitly if it wants a timestamp update.
4374 */
4375 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4376 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4377
Josef Bacika41ad392011-01-31 15:30:16 -05004378 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004379 truncate_pagecache(inode, oldsize, newsize);
4380 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004381 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004382 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004383
Miao Xief4a2f4c2011-12-14 20:12:01 -05004384 trans = btrfs_start_transaction(root, 1);
4385 if (IS_ERR(trans))
4386 return PTR_ERR(trans);
4387
4388 i_size_write(inode, newsize);
4389 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4390 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004391 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004392 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004393
Josef Bacika41ad392011-01-31 15:30:16 -05004394 /*
4395 * We're truncating a file that used to have good data down to
4396 * zero. Make sure it gets into the ordered flush list so that
4397 * any new writes get down to disk quickly.
4398 */
4399 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004400 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4401 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004402
Josef Bacikf3fe8202013-01-07 17:03:21 -05004403 /*
4404 * 1 for the orphan item we're going to add
4405 * 1 for the orphan item deletion.
4406 */
4407 trans = btrfs_start_transaction(root, 2);
4408 if (IS_ERR(trans))
4409 return PTR_ERR(trans);
4410
4411 /*
4412 * We need to do this in case we fail at _any_ point during the
4413 * actual truncate. Once we do the truncate_setsize we could
4414 * invalidate pages which forces any outstanding ordered io to
4415 * be instantly completed which will give us extents that need
4416 * to be truncated. If we fail to get an orphan inode down we
4417 * could have left over extents that were never meant to live,
4418 * so we need to garuntee from this point on that everything
4419 * will be consistent.
4420 */
4421 ret = btrfs_orphan_add(trans, inode);
4422 btrfs_end_transaction(trans, root);
4423 if (ret)
4424 return ret;
4425
Josef Bacika41ad392011-01-31 15:30:16 -05004426 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4427 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004428
4429 /* Disable nonlocked read DIO to avoid the end less truncate */
4430 btrfs_inode_block_unlocked_dio(inode);
4431 inode_dio_wait(inode);
4432 btrfs_inode_resume_unlocked_dio(inode);
4433
Josef Bacika41ad392011-01-31 15:30:16 -05004434 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004435 if (ret && inode->i_nlink)
4436 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004437 }
4438
Josef Bacika41ad392011-01-31 15:30:16 -05004439 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004440}
4441
Chris Mason39279cc2007-06-12 06:35:45 -04004442static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4443{
4444 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004445 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004446 int err;
4447
Li Zefanb83cc962010-12-20 16:04:08 +08004448 if (btrfs_root_readonly(root))
4449 return -EROFS;
4450
Chris Mason39279cc2007-06-12 06:35:45 -04004451 err = inode_change_ok(inode, attr);
4452 if (err)
4453 return err;
4454
Chris Mason5a3f23d2009-03-31 13:27:11 -04004455 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004456 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004457 if (err)
4458 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004459 }
Yan Zheng9036c102008-10-30 14:19:41 -04004460
Christoph Hellwig10257742010-06-04 11:30:02 +02004461 if (attr->ia_valid) {
4462 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004463 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004464 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004465
Josef Bacik22c44fe2011-11-30 10:45:38 -05004466 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004467 err = btrfs_acl_chmod(inode);
4468 }
4469
Chris Mason39279cc2007-06-12 06:35:45 -04004470 return err;
4471}
Chris Mason61295eb2008-01-14 16:24:38 -05004472
Al Virobd555972010-06-07 11:35:40 -04004473void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004474{
4475 struct btrfs_trans_handle *trans;
4476 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004477 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004478 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004479 int ret;
4480
liubo1abe9b82011-03-24 11:18:59 +00004481 trace_btrfs_inode_evict(inode);
4482
Chris Mason39279cc2007-06-12 06:35:45 -04004483 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004484 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004485 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004486 goto no_delete;
4487
Chris Mason39279cc2007-06-12 06:35:45 -04004488 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004489 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004490 goto no_delete;
4491 }
Al Virobd555972010-06-07 11:35:40 -04004492 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004493 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004494
Yan, Zhengc71bf092009-11-12 09:34:40 +00004495 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004496 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004497 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004498 goto no_delete;
4499 }
4500
Yan, Zheng76dda932009-09-21 16:00:26 -04004501 if (inode->i_nlink > 0) {
4502 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4503 goto no_delete;
4504 }
4505
Miao Xie0e8c36a2012-12-19 06:59:51 +00004506 ret = btrfs_commit_inode_delayed_inode(inode);
4507 if (ret) {
4508 btrfs_orphan_del(NULL, inode);
4509 goto no_delete;
4510 }
4511
Miao Xie66d8f3d2012-09-06 04:02:28 -06004512 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004513 if (!rsv) {
4514 btrfs_orphan_del(NULL, inode);
4515 goto no_delete;
4516 }
Josef Bacik4a338542011-08-29 11:01:31 -04004517 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004518 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004519 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004520
Chris Masondbe674a2008-07-17 12:54:05 -04004521 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004522
Josef Bacik4289a662011-08-05 13:22:24 -04004523 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004524 * This is a bit simpler than btrfs_truncate since we've already
4525 * reserved our space for our orphan item in the unlink, so we just
4526 * need to reserve some slack space in case we add bytes and update
4527 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004528 */
Yan, Zheng80825102009-11-12 09:35:36 +00004529 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004530 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4531 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004532
Josef Bacik726c35f2011-09-26 15:46:06 -04004533 /*
4534 * Try and steal from the global reserve since we will
4535 * likely not use this space anyway, we want to try as
4536 * hard as possible to get this to work.
4537 */
4538 if (ret)
4539 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4540
Yan, Zhengd68fc572010-05-16 10:49:58 -04004541 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004542 btrfs_warn(root->fs_info,
4543 "Could not get space for a delete, will truncate on mount %d",
4544 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004545 btrfs_orphan_del(NULL, inode);
4546 btrfs_free_block_rsv(root, rsv);
4547 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004548 }
4549
Miao Xie0e8c36a2012-12-19 06:59:51 +00004550 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004551 if (IS_ERR(trans)) {
4552 btrfs_orphan_del(NULL, inode);
4553 btrfs_free_block_rsv(root, rsv);
4554 goto no_delete;
4555 }
4556
4557 trans->block_rsv = rsv;
4558
Yan, Zhengd68fc572010-05-16 10:49:58 -04004559 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004560 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004561 break;
4562
Miao Xie8407aa42012-09-07 01:43:32 -06004563 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004564 btrfs_end_transaction(trans, root);
4565 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004566 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004567 }
4568
Josef Bacik4289a662011-08-05 13:22:24 -04004569 btrfs_free_block_rsv(root, rsv);
4570
Yan, Zheng80825102009-11-12 09:35:36 +00004571 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004572 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004573 ret = btrfs_orphan_del(trans, inode);
4574 BUG_ON(ret);
4575 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004576
Josef Bacik4289a662011-08-05 13:22:24 -04004577 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004578 if (!(root == root->fs_info->tree_root ||
4579 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004580 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004581
Chris Mason54aa1f42007-06-22 14:16:25 -04004582 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004583 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004584no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004585 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004586 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004587 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004588}
4589
4590/*
4591 * this returns the key found in the dir entry in the location pointer.
4592 * If no dir entries were found, location->objectid is 0.
4593 */
4594static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4595 struct btrfs_key *location)
4596{
4597 const char *name = dentry->d_name.name;
4598 int namelen = dentry->d_name.len;
4599 struct btrfs_dir_item *di;
4600 struct btrfs_path *path;
4601 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004602 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004603
4604 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004605 if (!path)
4606 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004607
Li Zefan33345d012011-04-20 10:31:50 +08004608 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004609 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004610 if (IS_ERR(di))
4611 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004612
David Sterbac7040052011-04-19 18:00:01 +02004613 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004614 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004615
Chris Mason5f39d392007-10-15 16:14:19 -04004616 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004617out:
Chris Mason39279cc2007-06-12 06:35:45 -04004618 btrfs_free_path(path);
4619 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004620out_err:
4621 location->objectid = 0;
4622 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004623}
4624
4625/*
4626 * when we hit a tree root in a directory, the btrfs part of the inode
4627 * needs to be changed to reflect the root directory of the tree root. This
4628 * is kind of like crossing a mount point.
4629 */
4630static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004631 struct inode *dir,
4632 struct dentry *dentry,
4633 struct btrfs_key *location,
4634 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004635{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004636 struct btrfs_path *path;
4637 struct btrfs_root *new_root;
4638 struct btrfs_root_ref *ref;
4639 struct extent_buffer *leaf;
4640 int ret;
4641 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004642
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004643 path = btrfs_alloc_path();
4644 if (!path) {
4645 err = -ENOMEM;
4646 goto out;
4647 }
Chris Mason39279cc2007-06-12 06:35:45 -04004648
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004649 err = -ENOENT;
4650 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4651 BTRFS_I(dir)->root->root_key.objectid,
4652 location->objectid);
4653 if (ret) {
4654 if (ret < 0)
4655 err = ret;
4656 goto out;
4657 }
Chris Mason39279cc2007-06-12 06:35:45 -04004658
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004659 leaf = path->nodes[0];
4660 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004661 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004662 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4663 goto out;
4664
4665 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4666 (unsigned long)(ref + 1),
4667 dentry->d_name.len);
4668 if (ret)
4669 goto out;
4670
David Sterbab3b4aa72011-04-21 01:20:15 +02004671 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004672
4673 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4674 if (IS_ERR(new_root)) {
4675 err = PTR_ERR(new_root);
4676 goto out;
4677 }
4678
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004679 *sub_root = new_root;
4680 location->objectid = btrfs_root_dirid(&new_root->root_item);
4681 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004682 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004683 err = 0;
4684out:
4685 btrfs_free_path(path);
4686 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004687}
4688
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004689static void inode_tree_add(struct inode *inode)
4690{
4691 struct btrfs_root *root = BTRFS_I(inode)->root;
4692 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004693 struct rb_node **p;
4694 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004695 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004696
Al Viro1d3382cb2010-10-23 15:19:20 -04004697 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004698 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004699again:
4700 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004701 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004702 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004703 while (*p) {
4704 parent = *p;
4705 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4706
Li Zefan33345d012011-04-20 10:31:50 +08004707 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004708 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004709 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004710 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004711 else {
4712 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004713 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004714 rb_erase(parent, &root->inode_tree);
4715 RB_CLEAR_NODE(parent);
4716 spin_unlock(&root->inode_lock);
4717 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004718 }
4719 }
4720 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4721 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4722 spin_unlock(&root->inode_lock);
4723}
4724
4725static void inode_tree_del(struct inode *inode)
4726{
4727 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004728 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004730 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004731 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004732 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004734 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004735 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004736 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004737
Josef Bacik0af3d002010-06-21 14:48:16 -04004738 /*
4739 * Free space cache has inodes in the tree root, but the tree root has a
4740 * root_refs of 0, so this could end up dropping the tree root as a
4741 * snapshot, so we need the extra !root->fs_info->tree_root check to
4742 * make sure we don't drop it.
4743 */
4744 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4745 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004746 synchronize_srcu(&root->fs_info->subvol_srcu);
4747 spin_lock(&root->inode_lock);
4748 empty = RB_EMPTY_ROOT(&root->inode_tree);
4749 spin_unlock(&root->inode_lock);
4750 if (empty)
4751 btrfs_add_dead_root(root);
4752 }
4753}
4754
Jeff Mahoney143bede2012-03-01 14:56:26 +01004755void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004756{
4757 struct rb_node *node;
4758 struct rb_node *prev;
4759 struct btrfs_inode *entry;
4760 struct inode *inode;
4761 u64 objectid = 0;
4762
4763 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4764
4765 spin_lock(&root->inode_lock);
4766again:
4767 node = root->inode_tree.rb_node;
4768 prev = NULL;
4769 while (node) {
4770 prev = node;
4771 entry = rb_entry(node, struct btrfs_inode, rb_node);
4772
Li Zefan33345d012011-04-20 10:31:50 +08004773 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004774 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004775 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004776 node = node->rb_right;
4777 else
4778 break;
4779 }
4780 if (!node) {
4781 while (prev) {
4782 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004783 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004784 node = prev;
4785 break;
4786 }
4787 prev = rb_next(prev);
4788 }
4789 }
4790 while (node) {
4791 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004792 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004793 inode = igrab(&entry->vfs_inode);
4794 if (inode) {
4795 spin_unlock(&root->inode_lock);
4796 if (atomic_read(&inode->i_count) > 1)
4797 d_prune_aliases(inode);
4798 /*
Al Viro45321ac2010-06-07 13:43:19 -04004799 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004800 * the inode cache when its usage count
4801 * hits zero.
4802 */
4803 iput(inode);
4804 cond_resched();
4805 spin_lock(&root->inode_lock);
4806 goto again;
4807 }
4808
4809 if (cond_resched_lock(&root->inode_lock))
4810 goto again;
4811
4812 node = rb_next(node);
4813 }
4814 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004815}
4816
Chris Masone02119d2008-09-05 16:13:11 -04004817static int btrfs_init_locked_inode(struct inode *inode, void *p)
4818{
4819 struct btrfs_iget_args *args = p;
4820 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004821 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004822 return 0;
4823}
4824
4825static int btrfs_find_actor(struct inode *inode, void *opaque)
4826{
4827 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004828 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004829 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004830}
4831
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004832static struct inode *btrfs_iget_locked(struct super_block *s,
4833 u64 objectid,
4834 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004835{
4836 struct inode *inode;
4837 struct btrfs_iget_args args;
4838 args.ino = objectid;
4839 args.root = root;
4840
4841 inode = iget5_locked(s, objectid, btrfs_find_actor,
4842 btrfs_init_locked_inode,
4843 (void *)&args);
4844 return inode;
4845}
4846
Balaji Rao1a54ef82008-07-21 02:01:04 +05304847/* Get an inode object given its location and corresponding root.
4848 * Returns in *is_new if the inode was read from disk
4849 */
4850struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004851 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304852{
4853 struct inode *inode;
4854
4855 inode = btrfs_iget_locked(s, location->objectid, root);
4856 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004857 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304858
4859 if (inode->i_state & I_NEW) {
4860 BTRFS_I(inode)->root = root;
4861 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4862 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004863 if (!is_bad_inode(inode)) {
4864 inode_tree_add(inode);
4865 unlock_new_inode(inode);
4866 if (new)
4867 *new = 1;
4868 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004869 unlock_new_inode(inode);
4870 iput(inode);
4871 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004872 }
4873 }
4874
Balaji Rao1a54ef82008-07-21 02:01:04 +05304875 return inode;
4876}
4877
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004878static struct inode *new_simple_dir(struct super_block *s,
4879 struct btrfs_key *key,
4880 struct btrfs_root *root)
4881{
4882 struct inode *inode = new_inode(s);
4883
4884 if (!inode)
4885 return ERR_PTR(-ENOMEM);
4886
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004887 BTRFS_I(inode)->root = root;
4888 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004889 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004890
4891 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004892 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004893 inode->i_fop = &simple_dir_operations;
4894 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4895 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4896
4897 return inode;
4898}
4899
Chris Mason3de45862008-11-17 21:02:50 -05004900struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004901{
Chris Masond3977122009-01-05 21:25:51 -05004902 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004903 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004904 struct btrfs_root *sub_root = root;
4905 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004906 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004907 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004908
4909 if (dentry->d_name.len > BTRFS_NAME_LEN)
4910 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004911
Jeff Layton39e3c952012-11-28 11:30:53 -05004912 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004913 if (ret < 0)
4914 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004915
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004916 if (location.objectid == 0)
4917 return NULL;
4918
4919 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004920 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004921 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004922 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004923
4924 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4925
Yan, Zheng76dda932009-09-21 16:00:26 -04004926 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004927 ret = fixup_tree_root_location(root, dir, dentry,
4928 &location, &sub_root);
4929 if (ret < 0) {
4930 if (ret != -ENOENT)
4931 inode = ERR_PTR(ret);
4932 else
4933 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4934 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004935 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004936 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004937 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4938
Julia Lawall34d19ba2011-01-24 19:55:19 +00004939 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004940 down_read(&root->fs_info->cleanup_work_sem);
4941 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004942 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004943 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004944 if (ret) {
4945 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004946 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004947 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004948 }
4949
Chris Mason3de45862008-11-17 21:02:50 -05004950 return inode;
4951}
4952
Nick Pigginfe15ce42011-01-07 17:49:23 +11004953static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004954{
4955 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004956 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004957
Li Zefan848cce02012-02-21 17:04:28 +08004958 if (!inode && !IS_ROOT(dentry))
4959 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004960
Li Zefan848cce02012-02-21 17:04:28 +08004961 if (inode) {
4962 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004963 if (btrfs_root_refs(&root->root_item) == 0)
4964 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004965
4966 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4967 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004968 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004969 return 0;
4970}
4971
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004972static void btrfs_dentry_release(struct dentry *dentry)
4973{
4974 if (dentry->d_fsdata)
4975 kfree(dentry->d_fsdata);
4976}
4977
Chris Mason3de45862008-11-17 21:02:50 -05004978static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004979 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004980{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004981 struct dentry *ret;
4982
4983 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004984 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004985}
4986
Miao Xie16cdcec2011-04-22 18:12:22 +08004987unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004988 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4989};
4990
David Woodhousecbdf5a22008-08-06 19:42:33 +01004991static int btrfs_real_readdir(struct file *filp, void *dirent,
4992 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004993{
Al Viro496ad9a2013-01-23 17:07:38 -05004994 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04004995 struct btrfs_root *root = BTRFS_I(inode)->root;
4996 struct btrfs_item *item;
4997 struct btrfs_dir_item *di;
4998 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004999 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005000 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005001 struct list_head ins_list;
5002 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005003 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005004 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005005 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005006 unsigned char d_type;
5007 int over = 0;
5008 u32 di_cur;
5009 u32 di_total;
5010 u32 di_len;
5011 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005012 char tmp_name[32];
5013 char *name_ptr;
5014 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005015 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005016
5017 /* FIXME, use a real flag for deciding about the key type */
5018 if (root->fs_info->tree_root == root)
5019 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005020
Chris Mason39544012007-12-12 14:38:19 -05005021 /* special case for "." */
5022 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005023 over = filldir(dirent, ".", 1,
5024 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005025 if (over)
5026 return 0;
5027 filp->f_pos = 1;
5028 }
Chris Mason39544012007-12-12 14:38:19 -05005029 /* special case for .., just use the back ref */
5030 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005031 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005032 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005033 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005034 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005035 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005036 filp->f_pos = 2;
5037 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005038 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005039 if (!path)
5040 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005041
Josef Bacik026fd312011-05-13 10:32:11 -04005042 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005043
Miao Xie16cdcec2011-04-22 18:12:22 +08005044 if (key_type == BTRFS_DIR_INDEX_KEY) {
5045 INIT_LIST_HEAD(&ins_list);
5046 INIT_LIST_HEAD(&del_list);
5047 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5048 }
5049
Chris Mason39279cc2007-06-12 06:35:45 -04005050 btrfs_set_key_type(&key, key_type);
5051 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005052 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005053
Chris Mason39279cc2007-06-12 06:35:45 -04005054 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5055 if (ret < 0)
5056 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005057
5058 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005059 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005060 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005061 if (slot >= btrfs_header_nritems(leaf)) {
5062 ret = btrfs_next_leaf(root, path);
5063 if (ret < 0)
5064 goto err;
5065 else if (ret > 0)
5066 break;
5067 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005068 }
Chris Mason3de45862008-11-17 21:02:50 -05005069
Chris Mason5f39d392007-10-15 16:14:19 -04005070 item = btrfs_item_nr(leaf, slot);
5071 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5072
5073 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005074 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005075 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005076 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005077 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005078 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005079 if (key_type == BTRFS_DIR_INDEX_KEY &&
5080 btrfs_should_delete_dir_index(&del_list,
5081 found_key.offset))
5082 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005083
5084 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005085 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005086
Chris Mason39279cc2007-06-12 06:35:45 -04005087 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5088 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005089 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005090
5091 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005092 struct btrfs_key location;
5093
Josef Bacik22a94d42011-03-16 16:47:17 -04005094 if (verify_dir_item(root, leaf, di))
5095 break;
5096
Chris Mason5f39d392007-10-15 16:14:19 -04005097 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005098 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005099 name_ptr = tmp_name;
5100 } else {
5101 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005102 if (!name_ptr) {
5103 ret = -ENOMEM;
5104 goto err;
5105 }
Chris Mason5f39d392007-10-15 16:14:19 -04005106 }
5107 read_extent_buffer(leaf, name_ptr,
5108 (unsigned long)(di + 1), name_len);
5109
5110 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5111 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005112
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005113
Chris Mason3de45862008-11-17 21:02:50 -05005114 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005115 * skip it.
5116 *
5117 * In contrast to old kernels, we insert the snapshot's
5118 * dir item and dir index after it has been created, so
5119 * we won't find a reference to our own snapshot. We
5120 * still keep the following code for backward
5121 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005122 */
5123 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5124 location.objectid == root->root_key.objectid) {
5125 over = 0;
5126 goto skip;
5127 }
Chris Mason5f39d392007-10-15 16:14:19 -04005128 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005129 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005130 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005131
Chris Mason3de45862008-11-17 21:02:50 -05005132skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005133 if (name_ptr != tmp_name)
5134 kfree(name_ptr);
5135
Chris Mason39279cc2007-06-12 06:35:45 -04005136 if (over)
5137 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005138 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005139 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 di_cur += di_len;
5141 di = (struct btrfs_dir_item *)((char *)di + di_len);
5142 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005143next:
5144 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005145 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005146
Miao Xie16cdcec2011-04-22 18:12:22 +08005147 if (key_type == BTRFS_DIR_INDEX_KEY) {
5148 if (is_curr)
5149 filp->f_pos++;
5150 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5151 &ins_list);
5152 if (ret)
5153 goto nopos;
5154 }
5155
David Woodhouse49593bf2008-08-17 17:08:36 +01005156 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005157 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005158 /*
5159 * 32-bit glibc will use getdents64, but then strtol -
5160 * so the last number we can serve is this.
5161 */
5162 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005163 else
5164 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005165nopos:
5166 ret = 0;
5167err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005168 if (key_type == BTRFS_DIR_INDEX_KEY)
5169 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005170 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005171 return ret;
5172}
5173
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005174int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005175{
5176 struct btrfs_root *root = BTRFS_I(inode)->root;
5177 struct btrfs_trans_handle *trans;
5178 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005179 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005180
Josef Bacik72ac3c02012-05-23 14:13:11 -04005181 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005182 return 0;
5183
Liu Bo83eea1f2012-07-10 05:28:39 -06005184 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005185 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005186
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005187 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005188 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005189 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005190 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005191 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005192 if (IS_ERR(trans))
5193 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005194 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005195 }
5196 return ret;
5197}
5198
5199/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005200 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005201 * inode changes. But, it is most likely to find the inode in cache.
5202 * FIXME, needs more benchmarking...there are no reasons other than performance
5203 * to keep or drop this code.
5204 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005205static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005206{
5207 struct btrfs_root *root = BTRFS_I(inode)->root;
5208 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005209 int ret;
5210
Josef Bacik72ac3c02012-05-23 14:13:11 -04005211 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005212 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005213
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005214 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005215 if (IS_ERR(trans))
5216 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005217
5218 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005219 if (ret && ret == -ENOSPC) {
5220 /* whoops, lets try again with the full transaction */
5221 btrfs_end_transaction(trans, root);
5222 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005223 if (IS_ERR(trans))
5224 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005225
Chris Mason94b60442010-05-26 11:02:00 -04005226 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005227 }
Chris Mason39279cc2007-06-12 06:35:45 -04005228 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005229 if (BTRFS_I(inode)->delayed_node)
5230 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005231
5232 return ret;
5233}
5234
5235/*
5236 * This is a copy of file_update_time. We need this so we can return error on
5237 * ENOSPC for updating the inode in the case of file write and mmap writes.
5238 */
Josef Bacike41f9412012-03-26 09:46:47 -04005239static int btrfs_update_time(struct inode *inode, struct timespec *now,
5240 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005241{
Alexander Block2bc5565282012-06-15 09:49:33 +02005242 struct btrfs_root *root = BTRFS_I(inode)->root;
5243
5244 if (btrfs_root_readonly(root))
5245 return -EROFS;
5246
Josef Bacike41f9412012-03-26 09:46:47 -04005247 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005248 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005249 if (flags & S_CTIME)
5250 inode->i_ctime = *now;
5251 if (flags & S_MTIME)
5252 inode->i_mtime = *now;
5253 if (flags & S_ATIME)
5254 inode->i_atime = *now;
5255 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005256}
5257
Chris Masond352ac62008-09-29 15:18:18 -04005258/*
5259 * find the highest existing sequence number in a directory
5260 * and then set the in-memory index_cnt variable to reflect
5261 * free sequence numbers
5262 */
Josef Bacikaec74772008-07-24 12:12:38 -04005263static int btrfs_set_inode_index_count(struct inode *inode)
5264{
5265 struct btrfs_root *root = BTRFS_I(inode)->root;
5266 struct btrfs_key key, found_key;
5267 struct btrfs_path *path;
5268 struct extent_buffer *leaf;
5269 int ret;
5270
Li Zefan33345d012011-04-20 10:31:50 +08005271 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005272 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5273 key.offset = (u64)-1;
5274
5275 path = btrfs_alloc_path();
5276 if (!path)
5277 return -ENOMEM;
5278
5279 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5280 if (ret < 0)
5281 goto out;
5282 /* FIXME: we should be able to handle this */
5283 if (ret == 0)
5284 goto out;
5285 ret = 0;
5286
5287 /*
5288 * MAGIC NUMBER EXPLANATION:
5289 * since we search a directory based on f_pos we have to start at 2
5290 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5291 * else has to start at 2
5292 */
5293 if (path->slots[0] == 0) {
5294 BTRFS_I(inode)->index_cnt = 2;
5295 goto out;
5296 }
5297
5298 path->slots[0]--;
5299
5300 leaf = path->nodes[0];
5301 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5302
Li Zefan33345d012011-04-20 10:31:50 +08005303 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005304 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5305 BTRFS_I(inode)->index_cnt = 2;
5306 goto out;
5307 }
5308
5309 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5310out:
5311 btrfs_free_path(path);
5312 return ret;
5313}
5314
Chris Masond352ac62008-09-29 15:18:18 -04005315/*
5316 * helper to find a free sequence number in a given directory. This current
5317 * code is very simple, later versions will do smarter things in the btree
5318 */
Chris Mason3de45862008-11-17 21:02:50 -05005319int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005320{
5321 int ret = 0;
5322
5323 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005324 ret = btrfs_inode_delayed_dir_index_count(dir);
5325 if (ret) {
5326 ret = btrfs_set_inode_index_count(dir);
5327 if (ret)
5328 return ret;
5329 }
Josef Bacikaec74772008-07-24 12:12:38 -04005330 }
5331
Chris Mason00e4e6b2008-08-05 11:18:09 -04005332 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005333 BTRFS_I(dir)->index_cnt++;
5334
5335 return ret;
5336}
5337
Chris Mason39279cc2007-06-12 06:35:45 -04005338static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5339 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005340 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005341 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005342 u64 ref_objectid, u64 objectid,
5343 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005344{
5345 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005346 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005347 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005348 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005349 struct btrfs_inode_ref *ref;
5350 struct btrfs_key key[2];
5351 u32 sizes[2];
5352 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005353 int ret;
5354 int owner;
5355
Chris Mason5f39d392007-10-15 16:14:19 -04005356 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005357 if (!path)
5358 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005359
Chris Mason39279cc2007-06-12 06:35:45 -04005360 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005361 if (!inode) {
5362 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005363 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005364 }
Chris Mason39279cc2007-06-12 06:35:45 -04005365
Li Zefan581bb052011-04-20 10:06:11 +08005366 /*
5367 * we have to initialize this early, so we can reclaim the inode
5368 * number if we fail afterwards in this function.
5369 */
5370 inode->i_ino = objectid;
5371
Josef Bacikaec74772008-07-24 12:12:38 -04005372 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005373 trace_btrfs_inode_request(dir);
5374
Chris Mason3de45862008-11-17 21:02:50 -05005375 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005376 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005377 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005378 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005379 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005380 }
Josef Bacikaec74772008-07-24 12:12:38 -04005381 }
5382 /*
5383 * index_cnt is ignored for everything but a dir,
5384 * btrfs_get_inode_index_count has an explanation for the magic
5385 * number
5386 */
5387 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005388 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005389 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005390 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005391
Josef Bacik5dc562c2012-08-17 13:14:17 -04005392 /*
5393 * We could have gotten an inode number from somebody who was fsynced
5394 * and then removed in this same transaction, so let's just set full
5395 * sync since it will be a full sync anyway and this will blow away the
5396 * old info in the log.
5397 */
5398 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5399
Al Viro569254b2011-07-24 17:08:40 -04005400 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005401 owner = 0;
5402 else
5403 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005404
5405 key[0].objectid = objectid;
5406 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5407 key[0].offset = 0;
5408
Mark Fashehf1863732012-08-08 11:32:27 -07005409 /*
5410 * Start new inodes with an inode_ref. This is slightly more
5411 * efficient for small numbers of hard links since they will
5412 * be packed into one item. Extended refs will kick in if we
5413 * add more hard links than can fit in the ref item.
5414 */
Chris Mason9c583092008-01-29 15:15:18 -05005415 key[1].objectid = objectid;
5416 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5417 key[1].offset = ref_objectid;
5418
5419 sizes[0] = sizeof(struct btrfs_inode_item);
5420 sizes[1] = name_len + sizeof(*ref);
5421
Chris Masonb9473432009-03-13 11:00:37 -04005422 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005423 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5424 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005425 goto fail;
5426
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005427 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005428 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005429 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005430 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5431 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005432 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5433 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005434 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005435
5436 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5437 struct btrfs_inode_ref);
5438 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005439 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005440 ptr = (unsigned long)(ref + 1);
5441 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5442
Chris Mason5f39d392007-10-15 16:14:19 -04005443 btrfs_mark_buffer_dirty(path->nodes[0]);
5444 btrfs_free_path(path);
5445
Chris Mason39279cc2007-06-12 06:35:45 -04005446 location = &BTRFS_I(inode)->location;
5447 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005448 location->offset = 0;
5449 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5450
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005451 btrfs_inherit_iflags(inode, dir);
5452
Al Viro569254b2011-07-24 17:08:40 -04005453 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005454 if (btrfs_test_opt(root, NODATASUM))
5455 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005456 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005457 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5458 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005459 }
5460
Chris Mason39279cc2007-06-12 06:35:45 -04005461 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005463
5464 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005465 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005466
Alexander Block8ea05e32012-07-25 17:35:53 +02005467 btrfs_update_root_times(trans, root);
5468
Chris Mason39279cc2007-06-12 06:35:45 -04005469 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005470fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005471 if (dir)
5472 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005473 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005474 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005475 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005476}
5477
5478static inline u8 btrfs_inode_type(struct inode *inode)
5479{
5480 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5481}
5482
Chris Masond352ac62008-09-29 15:18:18 -04005483/*
5484 * utility function to add 'inode' into 'parent_inode' with
5485 * a give name and a given sequence number.
5486 * if 'add_backref' is true, also insert a backref from the
5487 * inode to the parent directory.
5488 */
Chris Masone02119d2008-09-05 16:13:11 -04005489int btrfs_add_link(struct btrfs_trans_handle *trans,
5490 struct inode *parent_inode, struct inode *inode,
5491 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005492{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005494 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005495 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005496 u64 ino = btrfs_ino(inode);
5497 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005498
Li Zefan33345d012011-04-20 10:31:50 +08005499 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5501 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005502 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5504 key.offset = 0;
5505 }
Chris Mason39279cc2007-06-12 06:35:45 -04005506
Li Zefan33345d012011-04-20 10:31:50 +08005507 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5509 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005510 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005512 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5513 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 }
5515
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005516 /* Nothing to clean up yet */
5517 if (ret)
5518 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005520 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5521 parent_inode, &key,
5522 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005523 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005524 goto fail_dir_item;
5525 else if (ret) {
5526 btrfs_abort_transaction(trans, root, ret);
5527 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005528 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005529
5530 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5531 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005532 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005533 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5534 ret = btrfs_update_inode(trans, root, parent_inode);
5535 if (ret)
5536 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005537 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005538
5539fail_dir_item:
5540 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5541 u64 local_index;
5542 int err;
5543 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5544 key.objectid, root->root_key.objectid,
5545 parent_ino, &local_index, name, name_len);
5546
5547 } else if (add_backref) {
5548 u64 local_index;
5549 int err;
5550
5551 err = btrfs_del_inode_ref(trans, root, name, name_len,
5552 ino, parent_ino, &local_index);
5553 }
5554 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005555}
5556
5557static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005558 struct inode *dir, struct dentry *dentry,
5559 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005560{
Josef Bacika1b075d2010-11-19 20:36:11 +00005561 int err = btrfs_add_link(trans, dir, inode,
5562 dentry->d_name.name, dentry->d_name.len,
5563 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005564 if (err > 0)
5565 err = -EEXIST;
5566 return err;
5567}
5568
Josef Bacik618e21d2007-07-11 10:18:17 -04005569static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005570 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005571{
5572 struct btrfs_trans_handle *trans;
5573 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005574 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005575 int err;
5576 int drop_inode = 0;
5577 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005578 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005579
5580 if (!new_valid_dev(rdev))
5581 return -EINVAL;
5582
Josef Bacik9ed74f22009-09-11 16:12:44 -04005583 /*
5584 * 2 for inode item and ref
5585 * 2 for dir items
5586 * 1 for xattr if selinux is on
5587 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005588 trans = btrfs_start_transaction(root, 5);
5589 if (IS_ERR(trans))
5590 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005591
Li Zefan581bb052011-04-20 10:06:11 +08005592 err = btrfs_find_free_ino(root, &objectid);
5593 if (err)
5594 goto out_unlock;
5595
Josef Bacikaec74772008-07-24 12:12:38 -04005596 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005597 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005598 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005599 if (IS_ERR(inode)) {
5600 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005601 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005602 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005603
Eric Paris2a7dba32011-02-01 11:05:39 -05005604 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005605 if (err) {
5606 drop_inode = 1;
5607 goto out_unlock;
5608 }
5609
Casey Schauflerad19db72011-12-15 10:09:07 -05005610 /*
5611 * If the active LSM wants to access the inode during
5612 * d_instantiate it needs these. Smack checks to see
5613 * if the filesystem supports xattrs by looking at the
5614 * ops vector.
5615 */
5616
5617 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005618 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005619 if (err)
5620 drop_inode = 1;
5621 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005622 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005623 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005624 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005625 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005626out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005627 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005628 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005629 if (drop_inode) {
5630 inode_dec_link_count(inode);
5631 iput(inode);
5632 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005633 return err;
5634}
5635
Chris Mason39279cc2007-06-12 06:35:45 -04005636static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005637 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005638{
5639 struct btrfs_trans_handle *trans;
5640 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005641 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005642 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005643 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005644 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005645 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005646
Josef Bacik9ed74f22009-09-11 16:12:44 -04005647 /*
5648 * 2 for inode item and ref
5649 * 2 for dir items
5650 * 1 for xattr if selinux is on
5651 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005652 trans = btrfs_start_transaction(root, 5);
5653 if (IS_ERR(trans))
5654 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005655
Li Zefan581bb052011-04-20 10:06:11 +08005656 err = btrfs_find_free_ino(root, &objectid);
5657 if (err)
5658 goto out_unlock;
5659
Josef Bacikaec74772008-07-24 12:12:38 -04005660 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005661 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005662 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005663 if (IS_ERR(inode)) {
5664 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005665 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005666 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005667 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005668
Eric Paris2a7dba32011-02-01 11:05:39 -05005669 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005670 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005671 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005672
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005673 err = btrfs_update_inode(trans, root, inode);
5674 if (err)
5675 goto out_unlock;
5676
Casey Schauflerad19db72011-12-15 10:09:07 -05005677 /*
5678 * If the active LSM wants to access the inode during
5679 * d_instantiate it needs these. Smack checks to see
5680 * if the filesystem supports xattrs by looking at the
5681 * ops vector.
5682 */
5683 inode->i_fop = &btrfs_file_operations;
5684 inode->i_op = &btrfs_file_inode_operations;
5685
Josef Bacika1b075d2010-11-19 20:36:11 +00005686 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005687 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005688 goto out_unlock;
5689
5690 inode->i_mapping->a_ops = &btrfs_aops;
5691 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5692 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5693 d_instantiate(dentry, inode);
5694
Chris Mason39279cc2007-06-12 06:35:45 -04005695out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005696 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005697 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005698 inode_dec_link_count(inode);
5699 iput(inode);
5700 }
Liu Bob53d3f52012-11-14 14:34:34 +00005701 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005702 return err;
5703}
5704
5705static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5706 struct dentry *dentry)
5707{
5708 struct btrfs_trans_handle *trans;
5709 struct btrfs_root *root = BTRFS_I(dir)->root;
5710 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005711 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005712 int err;
5713 int drop_inode = 0;
5714
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005715 /* do not allow sys_link's with other subvols of the same device */
5716 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005717 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005718
Mark Fashehf1863732012-08-08 11:32:27 -07005719 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005720 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005721
Chris Mason3de45862008-11-17 21:02:50 -05005722 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005723 if (err)
5724 goto fail;
5725
Yan, Zhenga22285a2010-05-16 10:48:46 -04005726 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005727 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005728 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005729 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005730 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005731 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005732 if (IS_ERR(trans)) {
5733 err = PTR_ERR(trans);
5734 goto fail;
5735 }
Chris Mason5f39d392007-10-15 16:14:19 -04005736
Miao Xie31534952011-04-13 13:19:21 +08005737 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005738 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005739 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005740 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005741 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005742
Josef Bacika1b075d2010-11-19 20:36:11 +00005743 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005744
Yan, Zhenga5719522009-09-24 09:17:31 -04005745 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005746 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005747 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005748 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005749 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005750 if (err)
5751 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005752 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005753 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005754 }
Chris Mason39279cc2007-06-12 06:35:45 -04005755
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005756 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005757fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005758 if (drop_inode) {
5759 inode_dec_link_count(inode);
5760 iput(inode);
5761 }
Liu Bob53d3f52012-11-14 14:34:34 +00005762 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005763 return err;
5764}
5765
Al Viro18bb1db2011-07-26 01:41:39 -04005766static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005767{
Chris Masonb9d86662008-05-02 16:13:49 -04005768 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005769 struct btrfs_trans_handle *trans;
5770 struct btrfs_root *root = BTRFS_I(dir)->root;
5771 int err = 0;
5772 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005773 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005774 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005775
Josef Bacik9ed74f22009-09-11 16:12:44 -04005776 /*
5777 * 2 items for inode and ref
5778 * 2 items for dir items
5779 * 1 for xattr if selinux is on
5780 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005781 trans = btrfs_start_transaction(root, 5);
5782 if (IS_ERR(trans))
5783 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005784
Li Zefan581bb052011-04-20 10:06:11 +08005785 err = btrfs_find_free_ino(root, &objectid);
5786 if (err)
5787 goto out_fail;
5788
Josef Bacikaec74772008-07-24 12:12:38 -04005789 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005790 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005791 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005792 if (IS_ERR(inode)) {
5793 err = PTR_ERR(inode);
5794 goto out_fail;
5795 }
Chris Mason5f39d392007-10-15 16:14:19 -04005796
Chris Mason39279cc2007-06-12 06:35:45 -04005797 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005798
Eric Paris2a7dba32011-02-01 11:05:39 -05005799 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005800 if (err)
5801 goto out_fail;
5802
Chris Mason39279cc2007-06-12 06:35:45 -04005803 inode->i_op = &btrfs_dir_inode_operations;
5804 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005805
Chris Masondbe674a2008-07-17 12:54:05 -04005806 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005807 err = btrfs_update_inode(trans, root, inode);
5808 if (err)
5809 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005810
Josef Bacika1b075d2010-11-19 20:36:11 +00005811 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5812 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005813 if (err)
5814 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005815
Chris Mason39279cc2007-06-12 06:35:45 -04005816 d_instantiate(dentry, inode);
5817 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005818
5819out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005820 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005821 if (drop_on_err)
5822 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005823 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005824 return err;
5825}
5826
Chris Masond352ac62008-09-29 15:18:18 -04005827/* helper for btfs_get_extent. Given an existing extent in the tree,
5828 * and an extent that you want to insert, deal with overlap and insert
5829 * the new extent into the tree.
5830 */
Chris Mason3b951512008-04-17 11:29:12 -04005831static int merge_extent_mapping(struct extent_map_tree *em_tree,
5832 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005833 struct extent_map *em,
5834 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005835{
5836 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005837
Chris Masone6dcd2d2008-07-17 12:53:50 -04005838 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5839 start_diff = map_start - em->start;
5840 em->start = map_start;
5841 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005842 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5843 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005844 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005845 em->block_len -= start_diff;
5846 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005847 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005848}
5849
Chris Masonc8b97812008-10-29 14:49:59 -04005850static noinline int uncompress_inline(struct btrfs_path *path,
5851 struct inode *inode, struct page *page,
5852 size_t pg_offset, u64 extent_offset,
5853 struct btrfs_file_extent_item *item)
5854{
5855 int ret;
5856 struct extent_buffer *leaf = path->nodes[0];
5857 char *tmp;
5858 size_t max_size;
5859 unsigned long inline_size;
5860 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005861 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005862
5863 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005864 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005865 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5866 inline_size = btrfs_file_extent_inline_item_len(leaf,
5867 btrfs_item_nr(leaf, path->slots[0]));
5868 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005869 if (!tmp)
5870 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005871 ptr = btrfs_file_extent_inline_start(item);
5872
5873 read_extent_buffer(leaf, tmp, ptr, inline_size);
5874
Chris Mason5b050f02008-11-11 09:34:41 -05005875 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005876 ret = btrfs_decompress(compress_type, tmp, page,
5877 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005878 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005879 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005880 unsigned long copy_size = min_t(u64,
5881 PAGE_CACHE_SIZE - pg_offset,
5882 max_size - extent_offset);
5883 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005884 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005885 }
5886 kfree(tmp);
5887 return 0;
5888}
5889
Chris Masond352ac62008-09-29 15:18:18 -04005890/*
5891 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005892 * the ugly parts come from merging extents from the disk with the in-ram
5893 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005894 * where the in-ram extents might be locked pending data=ordered completion.
5895 *
5896 * This also copies inline extents directly into the page.
5897 */
Chris Masond3977122009-01-05 21:25:51 -05005898
Chris Masona52d9a82007-08-27 16:49:44 -04005899struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005900 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005901 int create)
5902{
5903 int ret;
5904 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005905 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005906 u64 extent_start = 0;
5907 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005908 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005909 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005910 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005911 struct btrfs_root *root = BTRFS_I(inode)->root;
5912 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005913 struct extent_buffer *leaf;
5914 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005915 struct extent_map *em = NULL;
5916 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005917 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005918 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005919 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005920
Chris Masona52d9a82007-08-27 16:49:44 -04005921again:
Chris Mason890871b2009-09-02 16:24:52 -04005922 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005923 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005924 if (em)
5925 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005926 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005927
Chris Masona52d9a82007-08-27 16:49:44 -04005928 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005929 if (em->start > start || em->start + em->len <= start)
5930 free_extent_map(em);
5931 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005932 free_extent_map(em);
5933 else
5934 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005935 }
David Sterba172ddd62011-04-21 00:48:27 +02005936 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005937 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005938 err = -ENOMEM;
5939 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005941 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005942 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005943 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005944 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005945 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005946
5947 if (!path) {
5948 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005949 if (!path) {
5950 err = -ENOMEM;
5951 goto out;
5952 }
5953 /*
5954 * Chances are we'll be called again, so go ahead and do
5955 * readahead
5956 */
5957 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005958 }
5959
Chris Mason179e29e2007-11-01 11:28:41 -04005960 ret = btrfs_lookup_file_extent(trans, root, path,
5961 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005962 if (ret < 0) {
5963 err = ret;
5964 goto out;
5965 }
5966
5967 if (ret != 0) {
5968 if (path->slots[0] == 0)
5969 goto not_found;
5970 path->slots[0]--;
5971 }
5972
Chris Mason5f39d392007-10-15 16:14:19 -04005973 leaf = path->nodes[0];
5974 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005975 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005976 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005977 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5978 found_type = btrfs_key_type(&found_key);
5979 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005980 found_type != BTRFS_EXTENT_DATA_KEY) {
5981 goto not_found;
5982 }
5983
Chris Mason5f39d392007-10-15 16:14:19 -04005984 found_type = btrfs_file_extent_type(leaf, item);
5985 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005986 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005987 if (found_type == BTRFS_FILE_EXTENT_REG ||
5988 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005989 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005990 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005991 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5992 size_t size;
5993 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00005994 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005995 }
5996
5997 if (start >= extent_end) {
5998 path->slots[0]++;
5999 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6000 ret = btrfs_next_leaf(root, path);
6001 if (ret < 0) {
6002 err = ret;
6003 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006004 }
Yan Zheng9036c102008-10-30 14:19:41 -04006005 if (ret > 0)
6006 goto not_found;
6007 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006008 }
Yan Zheng9036c102008-10-30 14:19:41 -04006009 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6010 if (found_key.objectid != objectid ||
6011 found_key.type != BTRFS_EXTENT_DATA_KEY)
6012 goto not_found;
6013 if (start + len <= found_key.offset)
6014 goto not_found;
6015 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006016 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006017 em->len = found_key.offset - start;
6018 goto not_found_em;
6019 }
6020
Josef Bacikcc95bef2013-04-04 14:31:27 -04006021 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006022 if (found_type == BTRFS_FILE_EXTENT_REG ||
6023 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006024 em->start = extent_start;
6025 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006026 em->orig_start = extent_start -
6027 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006028 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6029 item);
Chris Masondb945352007-10-15 16:15:53 -04006030 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6031 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006032 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006033 goto insert;
6034 }
Li Zefan261507a02010-12-17 14:21:50 +08006035 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006036 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006037 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006038 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006039 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006040 } else {
6041 bytenr += btrfs_file_extent_offset(leaf, item);
6042 em->block_start = bytenr;
6043 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006044 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6045 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006046 }
Chris Masona52d9a82007-08-27 16:49:44 -04006047 goto insert;
6048 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006049 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006050 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006051 size_t size;
6052 size_t extent_offset;
6053 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006054
Chris Masona52d9a82007-08-27 16:49:44 -04006055 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006056 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006057 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006058 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006059 goto out;
6060 }
6061
Yan Zheng9036c102008-10-30 14:19:41 -04006062 size = btrfs_file_extent_inline_len(leaf, item);
6063 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006064 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006065 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006066 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006067 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006068 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006069 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006070 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006071 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006072 em->compress_type = compress_type;
6073 }
Yan689f9342007-10-29 11:41:07 -04006074 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006075 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006076 if (btrfs_file_extent_compression(leaf, item) !=
6077 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006078 ret = uncompress_inline(path, inode, page,
6079 pg_offset,
6080 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006081 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006082 } else {
6083 map = kmap(page);
6084 read_extent_buffer(leaf, map + pg_offset, ptr,
6085 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006086 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6087 memset(map + pg_offset + copy_size, 0,
6088 PAGE_CACHE_SIZE - pg_offset -
6089 copy_size);
6090 }
Chris Masonc8b97812008-10-29 14:49:59 -04006091 kunmap(page);
6092 }
Chris Mason179e29e2007-11-01 11:28:41 -04006093 flush_dcache_page(page);
6094 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006095 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006096 if (!trans) {
6097 kunmap(page);
6098 free_extent_map(em);
6099 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006100
David Sterbab3b4aa72011-04-21 01:20:15 +02006101 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006102 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006103
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006104 if (IS_ERR(trans))
6105 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006106 goto again;
6107 }
Chris Masonc8b97812008-10-29 14:49:59 -04006108 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006109 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006110 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006111 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006112 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006113 }
Chris Masond1310b22008-01-24 16:13:08 -05006114 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006115 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006116 goto insert;
6117 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006118 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006119 }
6120not_found:
6121 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006122 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006123 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006124not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006125 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006126 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006127insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006128 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006129 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006130 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6131 (unsigned long long)em->start,
6132 (unsigned long long)em->len,
6133 (unsigned long long)start,
6134 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006135 err = -EIO;
6136 goto out;
6137 }
Chris Masond1310b22008-01-24 16:13:08 -05006138
6139 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006140 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006141 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006142 /* it is possible that someone inserted the extent into the tree
6143 * while we had the lock dropped. It is also possible that
6144 * an overlapping map exists in the tree
6145 */
Chris Masona52d9a82007-08-27 16:49:44 -04006146 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006147 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006148
6149 ret = 0;
6150
Chris Mason3b951512008-04-17 11:29:12 -04006151 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006152 if (existing && (existing->start > start ||
6153 existing->start + existing->len <= start)) {
6154 free_extent_map(existing);
6155 existing = NULL;
6156 }
Chris Mason3b951512008-04-17 11:29:12 -04006157 if (!existing) {
6158 existing = lookup_extent_mapping(em_tree, em->start,
6159 em->len);
6160 if (existing) {
6161 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006162 em, start,
6163 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006164 free_extent_map(existing);
6165 if (err) {
6166 free_extent_map(em);
6167 em = NULL;
6168 }
6169 } else {
6170 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006171 free_extent_map(em);
6172 em = NULL;
6173 }
6174 } else {
6175 free_extent_map(em);
6176 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006177 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006178 }
Chris Masona52d9a82007-08-27 16:49:44 -04006179 }
Chris Mason890871b2009-09-02 16:24:52 -04006180 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006181out:
liubo1abe9b82011-03-24 11:18:59 +00006182
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006183 if (em)
6184 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006185
Chris Masonf4219502008-07-22 11:18:09 -04006186 if (path)
6187 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006188 if (trans) {
6189 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006190 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006191 err = ret;
6192 }
Chris Masona52d9a82007-08-27 16:49:44 -04006193 if (err) {
6194 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006195 return ERR_PTR(err);
6196 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006197 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006198 return em;
6199}
6200
Chris Masonec29ed52011-02-23 16:23:20 -05006201struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6202 size_t pg_offset, u64 start, u64 len,
6203 int create)
6204{
6205 struct extent_map *em;
6206 struct extent_map *hole_em = NULL;
6207 u64 range_start = start;
6208 u64 end;
6209 u64 found;
6210 u64 found_end;
6211 int err = 0;
6212
6213 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6214 if (IS_ERR(em))
6215 return em;
6216 if (em) {
6217 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006218 * if our em maps to
6219 * - a hole or
6220 * - a pre-alloc extent,
6221 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006222 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006223 if (em->block_start != EXTENT_MAP_HOLE &&
6224 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006225 return em;
6226 else
6227 hole_em = em;
6228 }
6229
6230 /* check to see if we've wrapped (len == -1 or similar) */
6231 end = start + len;
6232 if (end < start)
6233 end = (u64)-1;
6234 else
6235 end -= 1;
6236
6237 em = NULL;
6238
6239 /* ok, we didn't find anything, lets look for delalloc */
6240 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6241 end, len, EXTENT_DELALLOC, 1);
6242 found_end = range_start + found;
6243 if (found_end < range_start)
6244 found_end = (u64)-1;
6245
6246 /*
6247 * we didn't find anything useful, return
6248 * the original results from get_extent()
6249 */
6250 if (range_start > end || found_end <= start) {
6251 em = hole_em;
6252 hole_em = NULL;
6253 goto out;
6254 }
6255
6256 /* adjust the range_start to make sure it doesn't
6257 * go backwards from the start they passed in
6258 */
6259 range_start = max(start,range_start);
6260 found = found_end - range_start;
6261
6262 if (found > 0) {
6263 u64 hole_start = start;
6264 u64 hole_len = len;
6265
David Sterba172ddd62011-04-21 00:48:27 +02006266 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006267 if (!em) {
6268 err = -ENOMEM;
6269 goto out;
6270 }
6271 /*
6272 * when btrfs_get_extent can't find anything it
6273 * returns one huge hole
6274 *
6275 * make sure what it found really fits our range, and
6276 * adjust to make sure it is based on the start from
6277 * the caller
6278 */
6279 if (hole_em) {
6280 u64 calc_end = extent_map_end(hole_em);
6281
6282 if (calc_end <= start || (hole_em->start > end)) {
6283 free_extent_map(hole_em);
6284 hole_em = NULL;
6285 } else {
6286 hole_start = max(hole_em->start, start);
6287 hole_len = calc_end - hole_start;
6288 }
6289 }
6290 em->bdev = NULL;
6291 if (hole_em && range_start > hole_start) {
6292 /* our hole starts before our delalloc, so we
6293 * have to return just the parts of the hole
6294 * that go until the delalloc starts
6295 */
6296 em->len = min(hole_len,
6297 range_start - hole_start);
6298 em->start = hole_start;
6299 em->orig_start = hole_start;
6300 /*
6301 * don't adjust block start at all,
6302 * it is fixed at EXTENT_MAP_HOLE
6303 */
6304 em->block_start = hole_em->block_start;
6305 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006306 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6307 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006308 } else {
6309 em->start = range_start;
6310 em->len = found;
6311 em->orig_start = range_start;
6312 em->block_start = EXTENT_MAP_DELALLOC;
6313 em->block_len = found;
6314 }
6315 } else if (hole_em) {
6316 return hole_em;
6317 }
6318out:
6319
6320 free_extent_map(hole_em);
6321 if (err) {
6322 free_extent_map(em);
6323 return ERR_PTR(err);
6324 }
6325 return em;
6326}
6327
Josef Bacik4b46fce2010-05-23 11:00:55 -04006328static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6329 u64 start, u64 len)
6330{
6331 struct btrfs_root *root = BTRFS_I(inode)->root;
6332 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006333 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006334 struct btrfs_key ins;
6335 u64 alloc_hint;
6336 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006337
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006338 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006339 if (IS_ERR(trans))
6340 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341
6342 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6343
6344 alloc_hint = get_extent_allocation_hint(inode, start, len);
6345 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006346 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006347 if (ret) {
6348 em = ERR_PTR(ret);
6349 goto out;
6350 }
6351
Josef Bacik70c8a912012-10-11 16:54:30 -04006352 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006353 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006354 if (IS_ERR(em))
6355 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006356
6357 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6358 ins.offset, ins.offset, 0);
6359 if (ret) {
6360 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6361 em = ERR_PTR(ret);
6362 }
6363out:
6364 btrfs_end_transaction(trans, root);
6365 return em;
6366}
6367
Chris Mason46bfbb52010-05-26 11:04:10 -04006368/*
6369 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6370 * block must be cow'd
6371 */
6372static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006373 struct inode *inode, u64 offset, u64 *len,
6374 u64 *orig_start, u64 *orig_block_len,
6375 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006376{
6377 struct btrfs_path *path;
6378 int ret;
6379 struct extent_buffer *leaf;
6380 struct btrfs_root *root = BTRFS_I(inode)->root;
6381 struct btrfs_file_extent_item *fi;
6382 struct btrfs_key key;
6383 u64 disk_bytenr;
6384 u64 backref_offset;
6385 u64 extent_end;
6386 u64 num_bytes;
6387 int slot;
6388 int found_type;
6389
6390 path = btrfs_alloc_path();
6391 if (!path)
6392 return -ENOMEM;
6393
Li Zefan33345d012011-04-20 10:31:50 +08006394 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006395 offset, 0);
6396 if (ret < 0)
6397 goto out;
6398
6399 slot = path->slots[0];
6400 if (ret == 1) {
6401 if (slot == 0) {
6402 /* can't find the item, must cow */
6403 ret = 0;
6404 goto out;
6405 }
6406 slot--;
6407 }
6408 ret = 0;
6409 leaf = path->nodes[0];
6410 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006411 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006412 key.type != BTRFS_EXTENT_DATA_KEY) {
6413 /* not our file or wrong item type, must cow */
6414 goto out;
6415 }
6416
6417 if (key.offset > offset) {
6418 /* Wrong offset, must cow */
6419 goto out;
6420 }
6421
6422 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6423 found_type = btrfs_file_extent_type(leaf, fi);
6424 if (found_type != BTRFS_FILE_EXTENT_REG &&
6425 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6426 /* not a regular extent, must cow */
6427 goto out;
6428 }
6429 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6430 backref_offset = btrfs_file_extent_offset(leaf, fi);
6431
Josef Bacikeb384b52013-04-24 16:32:55 -04006432 *orig_start = key.offset - backref_offset;
6433 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6434 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6435
Chris Mason46bfbb52010-05-26 11:04:10 -04006436 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006437 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006438 /* extent doesn't include our full range, must cow */
6439 goto out;
6440 }
6441
6442 if (btrfs_extent_readonly(root, disk_bytenr))
6443 goto out;
6444
6445 /*
6446 * look for other files referencing this extent, if we
6447 * find any we must cow
6448 */
Li Zefan33345d012011-04-20 10:31:50 +08006449 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006450 key.offset - backref_offset, disk_bytenr))
6451 goto out;
6452
6453 /*
6454 * adjust disk_bytenr and num_bytes to cover just the bytes
6455 * in this extent we are about to write. If there
6456 * are any csums in that range we have to cow in order
6457 * to keep the csums correct
6458 */
6459 disk_bytenr += backref_offset;
6460 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006461 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006462 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6463 goto out;
6464 /*
6465 * all of the above have passed, it is safe to overwrite this extent
6466 * without cow
6467 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006468 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006469 ret = 1;
6470out:
6471 btrfs_free_path(path);
6472 return ret;
6473}
6474
Josef Bacikeb838e72012-07-31 16:28:48 -04006475static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6476 struct extent_state **cached_state, int writing)
6477{
6478 struct btrfs_ordered_extent *ordered;
6479 int ret = 0;
6480
6481 while (1) {
6482 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6483 0, cached_state);
6484 /*
6485 * We're concerned with the entire range that we're going to be
6486 * doing DIO to, so we need to make sure theres no ordered
6487 * extents in this range.
6488 */
6489 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6490 lockend - lockstart + 1);
6491
6492 /*
6493 * We need to make sure there are no buffered pages in this
6494 * range either, we could have raced between the invalidate in
6495 * generic_file_direct_write and locking the extent. The
6496 * invalidate needs to happen so that reads after a write do not
6497 * get stale data.
6498 */
6499 if (!ordered && (!writing ||
6500 !test_range_bit(&BTRFS_I(inode)->io_tree,
6501 lockstart, lockend, EXTENT_UPTODATE, 0,
6502 *cached_state)))
6503 break;
6504
6505 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6506 cached_state, GFP_NOFS);
6507
6508 if (ordered) {
6509 btrfs_start_ordered_extent(inode, ordered, 1);
6510 btrfs_put_ordered_extent(ordered);
6511 } else {
6512 /* Screw you mmap */
6513 ret = filemap_write_and_wait_range(inode->i_mapping,
6514 lockstart,
6515 lockend);
6516 if (ret)
6517 break;
6518
6519 /*
6520 * If we found a page that couldn't be invalidated just
6521 * fall back to buffered.
6522 */
6523 ret = invalidate_inode_pages2_range(inode->i_mapping,
6524 lockstart >> PAGE_CACHE_SHIFT,
6525 lockend >> PAGE_CACHE_SHIFT);
6526 if (ret)
6527 break;
6528 }
6529
6530 cond_resched();
6531 }
6532
6533 return ret;
6534}
6535
Josef Bacik69ffb542012-09-11 15:40:07 -04006536static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6537 u64 len, u64 orig_start,
6538 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006539 u64 orig_block_len, u64 ram_bytes,
6540 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006541{
6542 struct extent_map_tree *em_tree;
6543 struct extent_map *em;
6544 struct btrfs_root *root = BTRFS_I(inode)->root;
6545 int ret;
6546
6547 em_tree = &BTRFS_I(inode)->extent_tree;
6548 em = alloc_extent_map();
6549 if (!em)
6550 return ERR_PTR(-ENOMEM);
6551
6552 em->start = start;
6553 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006554 em->mod_start = start;
6555 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006556 em->len = len;
6557 em->block_len = block_len;
6558 em->block_start = block_start;
6559 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006560 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006561 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006562 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006563 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6564 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006565 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006566
6567 do {
6568 btrfs_drop_extent_cache(inode, em->start,
6569 em->start + em->len - 1, 0);
6570 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006571 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006572 write_unlock(&em_tree->lock);
6573 } while (ret == -EEXIST);
6574
6575 if (ret) {
6576 free_extent_map(em);
6577 return ERR_PTR(ret);
6578 }
6579
6580 return em;
6581}
6582
6583
Josef Bacik4b46fce2010-05-23 11:00:55 -04006584static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6585 struct buffer_head *bh_result, int create)
6586{
6587 struct extent_map *em;
6588 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006589 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006590 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006591 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006592 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006593 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006594 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006595 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006596
Miao Xie172a5042013-02-21 02:48:22 -07006597 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006598 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006599 else
Josef Bacikc3298612012-08-03 16:49:19 -04006600 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006601
Josef Bacikc3298612012-08-03 16:49:19 -04006602 lockstart = start;
6603 lockend = start + len - 1;
6604
Josef Bacikeb838e72012-07-31 16:28:48 -04006605 /*
6606 * If this errors out it's because we couldn't invalidate pagecache for
6607 * this range and we need to fallback to buffered.
6608 */
6609 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6610 return -ENOTBLK;
6611
Josef Bacik4b46fce2010-05-23 11:00:55 -04006612 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006613 if (IS_ERR(em)) {
6614 ret = PTR_ERR(em);
6615 goto unlock_err;
6616 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006617
6618 /*
6619 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6620 * io. INLINE is special, and we could probably kludge it in here, but
6621 * it's still buffered so for safety lets just fall back to the generic
6622 * buffered path.
6623 *
6624 * For COMPRESSED we _have_ to read the entire extent in so we can
6625 * decompress it, so there will be buffering required no matter what we
6626 * do, so go ahead and fallback to buffered.
6627 *
6628 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6629 * to buffered IO. Don't blame me, this is the price we pay for using
6630 * the generic code.
6631 */
6632 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6633 em->block_start == EXTENT_MAP_INLINE) {
6634 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006635 ret = -ENOTBLK;
6636 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006637 }
6638
6639 /* Just a good old fashioned hole, return */
6640 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6641 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6642 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006643 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006644 }
6645
6646 /*
6647 * We don't allocate a new extent in the following cases
6648 *
6649 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6650 * existing extent.
6651 * 2) The extent is marked as PREALLOC. We're good to go here and can
6652 * just use the extent.
6653 *
6654 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006655 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006656 len = min(len, em->len - (start - em->start));
6657 lockstart = start + len;
6658 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006659 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006660
6661 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6662 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6663 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006664 int type;
6665 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006666 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006667
6668 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6669 type = BTRFS_ORDERED_PREALLOC;
6670 else
6671 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006672 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006673 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006674
6675 /*
6676 * we're not going to log anything, but we do need
6677 * to make sure the current transaction stays open
6678 * while we look for nocow cross refs
6679 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006680 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006681 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006682 goto must_cow;
6683
Josef Bacikeb384b52013-04-24 16:32:55 -04006684 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6685 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006686 if (type == BTRFS_ORDERED_PREALLOC) {
6687 free_extent_map(em);
6688 em = create_pinned_em(inode, start, len,
6689 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006690 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006691 orig_block_len,
6692 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006693 if (IS_ERR(em)) {
6694 btrfs_end_transaction(trans, root);
6695 goto unlock_err;
6696 }
6697 }
6698
Chris Mason46bfbb52010-05-26 11:04:10 -04006699 ret = btrfs_add_ordered_extent_dio(inode, start,
6700 block_start, len, len, type);
6701 btrfs_end_transaction(trans, root);
6702 if (ret) {
6703 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006704 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006705 }
6706 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006707 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006708 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006709 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006710must_cow:
6711 /*
6712 * this will cow the extent, reset the len in case we changed
6713 * it above
6714 */
6715 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006716 free_extent_map(em);
6717 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006718 if (IS_ERR(em)) {
6719 ret = PTR_ERR(em);
6720 goto unlock_err;
6721 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006722 len = min(len, em->len - (start - em->start));
6723unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006724 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6725 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006726 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006727 bh_result->b_bdev = em->bdev;
6728 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006729 if (create) {
6730 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6731 set_buffer_new(bh_result);
6732
6733 /*
6734 * Need to update the i_size under the extent lock so buffered
6735 * readers will get the updated i_size when we unlock.
6736 */
6737 if (start + len > i_size_read(inode))
6738 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006739
Miao Xie172a5042013-02-21 02:48:22 -07006740 spin_lock(&BTRFS_I(inode)->lock);
6741 BTRFS_I(inode)->outstanding_extents++;
6742 spin_unlock(&BTRFS_I(inode)->lock);
6743
Miao Xie09348562013-02-07 10:12:07 +00006744 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6745 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6746 &cached_state, GFP_NOFS);
6747 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006748 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006749
Josef Bacikeb838e72012-07-31 16:28:48 -04006750 /*
6751 * In the case of write we need to clear and unlock the entire range,
6752 * in the case of read we need to unlock only the end area that we
6753 * aren't using if there is any left over space.
6754 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006755 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006756 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6757 lockend, unlock_bits, 1, 0,
6758 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006759 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006760 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006761 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006762
Josef Bacik4b46fce2010-05-23 11:00:55 -04006763 free_extent_map(em);
6764
6765 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006766
6767unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006768 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6769 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6770 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006771}
6772
6773struct btrfs_dio_private {
6774 struct inode *inode;
6775 u64 logical_offset;
6776 u64 disk_bytenr;
6777 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006778 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006779
6780 /* number of bios pending for this dio */
6781 atomic_t pending_bios;
6782
6783 /* IO errors */
6784 int errors;
6785
Chris Mason9be33952013-05-17 18:30:14 -04006786 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006787 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006788
6789 /* dio_bio came from fs/direct-io.c */
6790 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006791};
6792
6793static void btrfs_endio_direct_read(struct bio *bio, int err)
6794{
Miao Xiee65e1532010-11-22 03:04:43 +00006795 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006796 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6797 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006799 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006800 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006801 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006802
6803 start = dip->logical_offset;
6804 do {
6805 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6806 struct page *page = bvec->bv_page;
6807 char *kaddr;
6808 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006809 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006810 unsigned long flags;
6811
Josef Bacikc3298612012-08-03 16:49:19 -04006812 if (get_state_private(&BTRFS_I(inode)->io_tree,
6813 start, &private))
6814 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006815 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006816 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006817 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006818 csum, bvec->bv_len);
6819 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006820 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006821 local_irq_restore(flags);
6822
6823 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006824 if (csum != private) {
6825failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006826 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6827 (unsigned long long)btrfs_ino(inode),
6828 (unsigned long long)start,
6829 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006830 err = -EIO;
6831 }
6832 }
6833
6834 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 bvec++;
6836 } while (bvec <= bvec_end);
6837
6838 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006839 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006840 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006841
Josef Bacik4b46fce2010-05-23 11:00:55 -04006842 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006843
6844 /* If we had a csum failure make sure to clear the uptodate flag */
6845 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006846 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6847 dio_end_io(dio_bio, err);
6848 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006849}
6850
6851static void btrfs_endio_direct_write(struct bio *bio, int err)
6852{
6853 struct btrfs_dio_private *dip = bio->bi_private;
6854 struct inode *inode = dip->inode;
6855 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006857 u64 ordered_offset = dip->logical_offset;
6858 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006859 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006860 int ret;
6861
6862 if (err)
6863 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006864again:
6865 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6866 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006867 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006868 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006869 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006870
Josef Bacik5fd02042012-05-02 14:00:54 -04006871 ordered->work.func = finish_ordered_fn;
6872 ordered->work.flags = 0;
6873 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6874 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006875out_test:
6876 /*
6877 * our bio might span multiple ordered extents. If we haven't
6878 * completed the accounting for the whole dio, go back and try again
6879 */
6880 if (ordered_offset < dip->logical_offset + dip->bytes) {
6881 ordered_bytes = dip->logical_offset + dip->bytes -
6882 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006883 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006884 goto again;
6885 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006886out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006887 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006888
Josef Bacik4b46fce2010-05-23 11:00:55 -04006889 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006890
6891 /* If we had an error make sure to clear the uptodate flag */
6892 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006893 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6894 dio_end_io(dio_bio, err);
6895 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006896}
6897
Chris Masoneaf25d92010-05-25 09:48:28 -04006898static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6899 struct bio *bio, int mirror_num,
6900 unsigned long bio_flags, u64 offset)
6901{
6902 int ret;
6903 struct btrfs_root *root = BTRFS_I(inode)->root;
6904 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006905 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006906 return 0;
6907}
6908
Miao Xiee65e1532010-11-22 03:04:43 +00006909static void btrfs_end_dio_bio(struct bio *bio, int err)
6910{
6911 struct btrfs_dio_private *dip = bio->bi_private;
6912
6913 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006914 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006915 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006916 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006917 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006918 dip->errors = 1;
6919
6920 /*
6921 * before atomic variable goto zero, we must make sure
6922 * dip->errors is perceived to be set.
6923 */
6924 smp_mb__before_atomic_dec();
6925 }
6926
6927 /* if there are more bios still pending for this dio, just exit */
6928 if (!atomic_dec_and_test(&dip->pending_bios))
6929 goto out;
6930
Chris Mason9be33952013-05-17 18:30:14 -04006931 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006932 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006933 } else {
6934 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006935 bio_endio(dip->orig_bio, 0);
6936 }
6937out:
6938 bio_put(bio);
6939}
6940
6941static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6942 u64 first_sector, gfp_t gfp_flags)
6943{
6944 int nr_vecs = bio_get_nr_vecs(bdev);
6945 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6946}
6947
6948static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6949 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006950 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006951{
6952 int write = rw & REQ_WRITE;
6953 struct btrfs_root *root = BTRFS_I(inode)->root;
6954 int ret;
6955
Josef Bacikb812ce22012-11-16 13:56:32 -05006956 if (async_submit)
6957 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6958
Miao Xiee65e1532010-11-22 03:04:43 +00006959 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006960
6961 if (!write) {
6962 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6963 if (ret)
6964 goto err;
6965 }
Miao Xiee65e1532010-11-22 03:04:43 +00006966
Josef Bacik1ae39932011-04-06 14:41:34 -04006967 if (skip_sum)
6968 goto map;
6969
6970 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006971 ret = btrfs_wq_submit_bio(root->fs_info,
6972 inode, rw, bio, 0, 0,
6973 file_offset,
6974 __btrfs_submit_bio_start_direct_io,
6975 __btrfs_submit_bio_done);
6976 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006977 } else if (write) {
6978 /*
6979 * If we aren't doing async submit, calculate the csum of the
6980 * bio now.
6981 */
6982 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6983 if (ret)
6984 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006985 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006986 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006987 if (ret)
6988 goto err;
6989 }
Miao Xiee65e1532010-11-22 03:04:43 +00006990
Josef Bacik1ae39932011-04-06 14:41:34 -04006991map:
6992 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006993err:
6994 bio_put(bio);
6995 return ret;
6996}
6997
6998static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6999 int skip_sum)
7000{
7001 struct inode *inode = dip->inode;
7002 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007003 struct bio *bio;
7004 struct bio *orig_bio = dip->orig_bio;
7005 struct bio_vec *bvec = orig_bio->bi_io_vec;
7006 u64 start_sector = orig_bio->bi_sector;
7007 u64 file_offset = dip->logical_offset;
7008 u64 submit_len = 0;
7009 u64 map_length;
7010 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007011 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007012 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007013
Miao Xiee65e1532010-11-22 03:04:43 +00007014 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007015 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007016 &map_length, NULL, 0);
7017 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007018 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007019 return -EIO;
7020 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007021 if (map_length >= orig_bio->bi_size) {
7022 bio = orig_bio;
7023 goto submit;
7024 }
7025
David Woodhouse53b381b2013-01-29 18:40:14 -05007026 /* async crcs make it difficult to collect full stripe writes. */
7027 if (btrfs_get_alloc_profile(root, 1) &
7028 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7029 async_submit = 0;
7030 else
7031 async_submit = 1;
7032
Josef Bacik02f57c72011-04-06 14:25:44 -04007033 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7034 if (!bio)
7035 return -ENOMEM;
7036 bio->bi_private = dip;
7037 bio->bi_end_io = btrfs_end_dio_bio;
7038 atomic_inc(&dip->pending_bios);
7039
Miao Xiee65e1532010-11-22 03:04:43 +00007040 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7041 if (unlikely(map_length < submit_len + bvec->bv_len ||
7042 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7043 bvec->bv_offset) < bvec->bv_len)) {
7044 /*
7045 * inc the count before we submit the bio so
7046 * we know the end IO handler won't happen before
7047 * we inc the count. Otherwise, the dip might get freed
7048 * before we're done setting it up
7049 */
7050 atomic_inc(&dip->pending_bios);
7051 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7052 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007053 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007054 if (ret) {
7055 bio_put(bio);
7056 atomic_dec(&dip->pending_bios);
7057 goto out_err;
7058 }
7059
Miao Xiee65e1532010-11-22 03:04:43 +00007060 start_sector += submit_len >> 9;
7061 file_offset += submit_len;
7062
7063 submit_len = 0;
7064 nr_pages = 0;
7065
7066 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7067 start_sector, GFP_NOFS);
7068 if (!bio)
7069 goto out_err;
7070 bio->bi_private = dip;
7071 bio->bi_end_io = btrfs_end_dio_bio;
7072
7073 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007074 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007075 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007076 &map_length, NULL, 0);
7077 if (ret) {
7078 bio_put(bio);
7079 goto out_err;
7080 }
7081 } else {
7082 submit_len += bvec->bv_len;
7083 nr_pages ++;
7084 bvec++;
7085 }
7086 }
7087
Josef Bacik02f57c72011-04-06 14:25:44 -04007088submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007089 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007090 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007091 if (!ret)
7092 return 0;
7093
7094 bio_put(bio);
7095out_err:
7096 dip->errors = 1;
7097 /*
7098 * before atomic variable goto zero, we must
7099 * make sure dip->errors is perceived to be set.
7100 */
7101 smp_mb__before_atomic_dec();
7102 if (atomic_dec_and_test(&dip->pending_bios))
7103 bio_io_error(dip->orig_bio);
7104
7105 /* bio_end_io() will handle error, so we needn't return it */
7106 return 0;
7107}
7108
Chris Mason9be33952013-05-17 18:30:14 -04007109static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7110 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007111{
7112 struct btrfs_root *root = BTRFS_I(inode)->root;
7113 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007114 struct bio_vec *bvec = dio_bio->bi_io_vec;
7115 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007116 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007117 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007118 int ret = 0;
7119
7120 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7121
Chris Mason9be33952013-05-17 18:30:14 -04007122 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7123
7124 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007125 ret = -ENOMEM;
7126 goto free_ordered;
7127 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007128
Chris Mason9be33952013-05-17 18:30:14 -04007129 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7130 if (!dip) {
7131 ret = -ENOMEM;
7132 goto free_io_bio;
7133 }
7134
7135 dip->private = dio_bio->bi_private;
7136 io_bio->bi_private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137 dip->inode = inode;
7138 dip->logical_offset = file_offset;
7139
Josef Bacik4b46fce2010-05-23 11:00:55 -04007140 dip->bytes = 0;
7141 do {
7142 dip->bytes += bvec->bv_len;
7143 bvec++;
Chris Mason9be33952013-05-17 18:30:14 -04007144 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145
Chris Mason9be33952013-05-17 18:30:14 -04007146 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7147 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007148 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007149 dip->orig_bio = io_bio;
7150 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007151 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007152
7153 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007154 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007155 else
Chris Mason9be33952013-05-17 18:30:14 -04007156 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157
Miao Xiee65e1532010-11-22 03:04:43 +00007158 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7159 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007160 return;
Chris Mason9be33952013-05-17 18:30:14 -04007161
7162free_io_bio:
7163 bio_put(io_bio);
7164
Josef Bacik4b46fce2010-05-23 11:00:55 -04007165free_ordered:
7166 /*
7167 * If this is a write, we need to clean up the reserved space and kill
7168 * the ordered extent.
7169 */
7170 if (write) {
7171 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007172 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007173 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7174 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7175 btrfs_free_reserved_extent(root, ordered->start,
7176 ordered->disk_len);
7177 btrfs_put_ordered_extent(ordered);
7178 btrfs_put_ordered_extent(ordered);
7179 }
Chris Mason9be33952013-05-17 18:30:14 -04007180 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181}
7182
Chris Mason5a5f79b2010-05-26 21:33:37 -04007183static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7184 const struct iovec *iov, loff_t offset,
7185 unsigned long nr_segs)
7186{
7187 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007188 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007189 size_t size;
7190 unsigned long addr;
7191 unsigned blocksize_mask = root->sectorsize - 1;
7192 ssize_t retval = -EINVAL;
7193 loff_t end = offset;
7194
7195 if (offset & blocksize_mask)
7196 goto out;
7197
7198 /* Check the memory alignment. Blocks cannot straddle pages */
7199 for (seg = 0; seg < nr_segs; seg++) {
7200 addr = (unsigned long)iov[seg].iov_base;
7201 size = iov[seg].iov_len;
7202 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007203 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007204 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007205
7206 /* If this is a write we don't need to check anymore */
7207 if (rw & WRITE)
7208 continue;
7209
7210 /*
7211 * Check to make sure we don't have duplicate iov_base's in this
7212 * iovec, if so return EINVAL, otherwise we'll get csum errors
7213 * when reading back.
7214 */
7215 for (i = seg + 1; i < nr_segs; i++) {
7216 if (iov[seg].iov_base == iov[i].iov_base)
7217 goto out;
7218 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007219 }
7220 retval = 0;
7221out:
7222 return retval;
7223}
Josef Bacikeb838e72012-07-31 16:28:48 -04007224
Chris Mason16432982008-04-10 10:23:21 -04007225static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7226 const struct iovec *iov, loff_t offset,
7227 unsigned long nr_segs)
7228{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007229 struct file *file = iocb->ki_filp;
7230 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007231 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007232 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007233 bool wakeup = true;
7234 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007235 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007236
Chris Mason5a5f79b2010-05-26 21:33:37 -04007237 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007238 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007239 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007240
Miao Xie38851cc2013-02-08 07:04:11 +00007241 atomic_inc(&inode->i_dio_count);
7242 smp_mb__after_atomic_inc();
7243
Miao Xie09348562013-02-07 10:12:07 +00007244 if (rw & WRITE) {
7245 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007246 /*
7247 * If the write DIO is beyond the EOF, we need update
7248 * the isize, but it is protected by i_mutex. So we can
7249 * not unlock the i_mutex at this case.
7250 */
7251 if (offset + count <= inode->i_size) {
7252 mutex_unlock(&inode->i_mutex);
7253 relock = true;
7254 }
Miao Xie09348562013-02-07 10:12:07 +00007255 ret = btrfs_delalloc_reserve_space(inode, count);
7256 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007257 goto out;
7258 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7259 &BTRFS_I(inode)->runtime_flags))) {
7260 inode_dio_done(inode);
7261 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7262 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007263 }
7264
7265 ret = __blockdev_direct_IO(rw, iocb, inode,
7266 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7267 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007268 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007269 if (rw & WRITE) {
7270 if (ret < 0 && ret != -EIOCBQUEUED)
7271 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007272 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007273 btrfs_delalloc_release_space(inode,
7274 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007275 else
7276 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007277 }
Miao Xie38851cc2013-02-08 07:04:11 +00007278out:
Miao Xie2e60a512013-02-08 07:01:08 +00007279 if (wakeup)
7280 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007281 if (relock)
7282 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007283
7284 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007285}
7286
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007287#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7288
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007289static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7290 __u64 start, __u64 len)
7291{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007292 int ret;
7293
7294 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7295 if (ret)
7296 return ret;
7297
Chris Masonec29ed52011-02-23 16:23:20 -05007298 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007299}
7300
Chris Mason9ebefb182007-06-15 13:50:00 -04007301int btrfs_readpage(struct file *file, struct page *page)
7302{
Chris Masond1310b22008-01-24 16:13:08 -05007303 struct extent_io_tree *tree;
7304 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007305 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007306}
Chris Mason1832a6d2007-12-21 16:27:21 -05007307
Chris Mason39279cc2007-06-12 06:35:45 -04007308static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7309{
Chris Masond1310b22008-01-24 16:13:08 -05007310 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007311
7312
7313 if (current->flags & PF_MEMALLOC) {
7314 redirty_page_for_writepage(wbc, page);
7315 unlock_page(page);
7316 return 0;
7317 }
Chris Masond1310b22008-01-24 16:13:08 -05007318 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007319 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7320}
Chris Mason39279cc2007-06-12 06:35:45 -04007321
Eric Sandeen48a3b632013-04-25 20:41:01 +00007322static int btrfs_writepages(struct address_space *mapping,
7323 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007324{
Chris Masond1310b22008-01-24 16:13:08 -05007325 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007326
Chris Masond1310b22008-01-24 16:13:08 -05007327 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007328 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7329}
7330
Chris Mason3ab2fb52007-11-08 10:59:22 -05007331static int
7332btrfs_readpages(struct file *file, struct address_space *mapping,
7333 struct list_head *pages, unsigned nr_pages)
7334{
Chris Masond1310b22008-01-24 16:13:08 -05007335 struct extent_io_tree *tree;
7336 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007337 return extent_readpages(tree, mapping, pages, nr_pages,
7338 btrfs_get_extent);
7339}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007340static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007341{
Chris Masond1310b22008-01-24 16:13:08 -05007342 struct extent_io_tree *tree;
7343 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007344 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007345
Chris Masond1310b22008-01-24 16:13:08 -05007346 tree = &BTRFS_I(page->mapping->host)->io_tree;
7347 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007348 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007349 if (ret == 1) {
7350 ClearPagePrivate(page);
7351 set_page_private(page, 0);
7352 page_cache_release(page);
7353 }
7354 return ret;
7355}
Chris Mason39279cc2007-06-12 06:35:45 -04007356
Chris Masone6dcd2d2008-07-17 12:53:50 -04007357static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7358{
Chris Mason98509cf2008-09-11 15:51:43 -04007359 if (PageWriteback(page) || PageDirty(page))
7360 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007361 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007362}
7363
Chris Masona52d9a82007-08-27 16:49:44 -04007364static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7365{
Josef Bacik5fd02042012-05-02 14:00:54 -04007366 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007367 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007368 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007369 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007370 u64 page_start = page_offset(page);
7371 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007372
Chris Mason8b62b722009-09-02 16:53:46 -04007373 /*
7374 * we have the page locked, so new writeback can't start,
7375 * and the dirty bit won't be cleared while we are here.
7376 *
7377 * Wait for IO on this page so that we can safely clear
7378 * the PagePrivate2 bit and do ordered accounting
7379 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007380 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007381
Josef Bacik5fd02042012-05-02 14:00:54 -04007382 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007383 if (offset) {
7384 btrfs_releasepage(page, GFP_NOFS);
7385 return;
7386 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007387 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007388 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007389 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007390 /*
7391 * IO on this page will never be started, so we need
7392 * to account for any ordered extents now
7393 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007394 clear_extent_bit(tree, page_start, page_end,
7395 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007396 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7397 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007398 /*
7399 * whoever cleared the private bit is responsible
7400 * for the finish_ordered_io
7401 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007402 if (TestClearPagePrivate2(page) &&
7403 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7404 PAGE_CACHE_SIZE, 1)) {
7405 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007406 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007407 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007408 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007409 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007410 }
7411 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007412 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007413 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7414 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007415 __btrfs_releasepage(page, GFP_NOFS);
7416
Chris Mason4a096752008-07-21 10:29:44 -04007417 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007418 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007419 ClearPagePrivate(page);
7420 set_page_private(page, 0);
7421 page_cache_release(page);
7422 }
Chris Mason39279cc2007-06-12 06:35:45 -04007423}
7424
Chris Mason9ebefb182007-06-15 13:50:00 -04007425/*
7426 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7427 * called from a page fault handler when a page is first dirtied. Hence we must
7428 * be careful to check for EOF conditions here. We set the page up correctly
7429 * for a written page which means we get ENOSPC checking when writing into
7430 * holes and correct delalloc and unwritten extent mapping on filesystems that
7431 * support these features.
7432 *
7433 * We are not allowed to take the i_mutex here so we have to play games to
7434 * protect against truncate races as the page could now be beyond EOF. Because
7435 * vmtruncate() writes the inode size before removing pages, once we have the
7436 * page lock we can determine safely if the page is beyond EOF. If it is not
7437 * beyond EOF, then the page is guaranteed safe against truncation until we
7438 * unlock the page.
7439 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007440int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007441{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007442 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007443 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007444 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007445 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7446 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007447 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007448 char *kaddr;
7449 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007450 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007451 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007452 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007453 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007454 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007455
Jan Karab2b5ef52012-06-12 16:20:45 +02007456 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007457 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007458 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007459 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007460 reserved = 1;
7461 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007462 if (ret) {
7463 if (ret == -ENOMEM)
7464 ret = VM_FAULT_OOM;
7465 else /* -ENOSPC, -EIO, etc */
7466 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007467 if (reserved)
7468 goto out;
7469 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007470 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007471
Nick Piggin56a76f82009-03-31 15:23:23 -07007472 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007473again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007474 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007475 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007476 page_start = page_offset(page);
7477 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007478
Chris Mason9ebefb182007-06-15 13:50:00 -04007479 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007480 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007481 /* page got truncated out from underneath us */
7482 goto out_unlock;
7483 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007484 wait_on_page_writeback(page);
7485
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007486 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007487 set_page_extent_mapped(page);
7488
Chris Masoneb84ae02008-07-17 13:53:27 -04007489 /*
7490 * we can't set the delalloc bits if there are pending ordered
7491 * extents. Drop our locks and wait for them to finish
7492 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007493 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7494 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007495 unlock_extent_cached(io_tree, page_start, page_end,
7496 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007497 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007498 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007499 btrfs_put_ordered_extent(ordered);
7500 goto again;
7501 }
7502
Josef Bacikfbf19082009-10-01 17:10:23 -04007503 /*
7504 * XXX - page_mkwrite gets called every time the page is dirtied, even
7505 * if it was already dirty, so for space accounting reasons we need to
7506 * clear any delalloc bits for the range we are fixing to save. There
7507 * is probably a better way to do this, but for now keep consistent with
7508 * prepare_pages in the normal write path.
7509 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007510 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007511 EXTENT_DIRTY | EXTENT_DELALLOC |
7512 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007513 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007514
Josef Bacik2ac55d42010-02-03 19:33:23 +00007515 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7516 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007517 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007518 unlock_extent_cached(io_tree, page_start, page_end,
7519 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007520 ret = VM_FAULT_SIGBUS;
7521 goto out_unlock;
7522 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007523 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007524
7525 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007526 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007527 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007528 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007529 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007530
Chris Masone6dcd2d2008-07-17 12:53:50 -04007531 if (zero_start != PAGE_CACHE_SIZE) {
7532 kaddr = kmap(page);
7533 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7534 flush_dcache_page(page);
7535 kunmap(page);
7536 }
Chris Mason247e7432008-07-17 12:53:51 -04007537 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007538 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007539 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007540
Chris Mason257c62e2009-10-13 13:21:08 -04007541 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7542 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007543 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007544
Josef Bacik2ac55d42010-02-03 19:33:23 +00007545 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007546
7547out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007548 if (!ret) {
7549 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007550 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007551 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007552 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007553out:
Josef Bacikec39e182012-01-12 19:10:12 -05007554 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007555out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007556 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007557 return ret;
7558}
7559
Josef Bacika41ad392011-01-31 15:30:16 -05007560static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007561{
7562 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007563 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007564 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007565 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007566 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007567 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007568 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007569
Josef Bacik2aaa6652012-08-29 14:27:18 -04007570 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007571 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007572 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007573
Chris Mason4a096752008-07-21 10:29:44 -04007574 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007575 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007576
Josef Bacikfcb80c22011-05-03 10:40:22 -04007577 /*
7578 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7579 * 3 things going on here
7580 *
7581 * 1) We need to reserve space for our orphan item and the space to
7582 * delete our orphan item. Lord knows we don't want to have a dangling
7583 * orphan item because we didn't reserve space to remove it.
7584 *
7585 * 2) We need to reserve space to update our inode.
7586 *
7587 * 3) We need to have something to cache all the space that is going to
7588 * be free'd up by the truncate operation, but also have some slack
7589 * space reserved in case it uses space during the truncate (thank you
7590 * very much snapshotting).
7591 *
7592 * And we need these to all be seperate. The fact is we can use alot of
7593 * space doing the truncate, and we have no earthly idea how much space
7594 * we will use, so we need the truncate reservation to be seperate so it
7595 * doesn't end up using space reserved for updating the inode or
7596 * removing the orphan item. We also need to be able to stop the
7597 * transaction and start a new one, which means we need to be able to
7598 * update the inode several times, and we have no idea of knowing how
7599 * many times that will be, so we can't just reserve 1 item for the
7600 * entirety of the opration, so that has to be done seperately as well.
7601 * Then there is the orphan item, which does indeed need to be held on
7602 * to for the whole operation, and we need nobody to touch this reserved
7603 * space except the orphan code.
7604 *
7605 * So that leaves us with
7606 *
7607 * 1) root->orphan_block_rsv - for the orphan deletion.
7608 * 2) rsv - for the truncate reservation, which we will steal from the
7609 * transaction reservation.
7610 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7611 * updating the inode.
7612 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007613 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007614 if (!rsv)
7615 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007616 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007617 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007618
Josef Bacik907cbce2011-08-08 13:46:15 -04007619 /*
Josef Bacik07127182011-08-19 10:29:59 -04007620 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007621 * 1 for updating the inode.
7622 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007623 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007624 if (IS_ERR(trans)) {
7625 err = PTR_ERR(trans);
7626 goto out;
7627 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007628
Josef Bacik907cbce2011-08-08 13:46:15 -04007629 /* Migrate the slack space for the truncate to our reserve */
7630 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7631 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007632 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007633
Chris Mason5a3f23d2009-03-31 13:27:11 -04007634 /*
7635 * setattr is responsible for setting the ordered_data_close flag,
7636 * but that is only tested during the last file release. That
7637 * could happen well after the next commit, leaving a great big
7638 * window where new writes may get lost if someone chooses to write
7639 * to this file after truncating to zero
7640 *
7641 * The inode doesn't have any dirty data here, and so if we commit
7642 * this is a noop. If someone immediately starts writing to the inode
7643 * it is very likely we'll catch some of their writes in this
7644 * transaction, and the commit will find this file on the ordered
7645 * data list with good things to send down.
7646 *
7647 * This is a best effort solution, there is still a window where
7648 * using truncate to replace the contents of the file will
7649 * end up with a zero length file after a crash.
7650 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007651 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7652 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007653 btrfs_add_ordered_operation(trans, root, inode);
7654
Josef Bacik5dc562c2012-08-17 13:14:17 -04007655 /*
7656 * So if we truncate and then write and fsync we normally would just
7657 * write the extents that changed, which is a problem if we need to
7658 * first truncate that entire inode. So set this flag so we write out
7659 * all of the extents in the inode to the sync log so we're completely
7660 * safe.
7661 */
7662 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007663 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007664
Yan, Zheng80825102009-11-12 09:35:36 +00007665 while (1) {
7666 ret = btrfs_truncate_inode_items(trans, root, inode,
7667 inode->i_size,
7668 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007669 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007670 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007671 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007672 }
Chris Mason39279cc2007-06-12 06:35:45 -04007673
Josef Bacikfcb80c22011-05-03 10:40:22 -04007674 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007675 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007676 if (ret) {
7677 err = ret;
7678 break;
7679 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007680
Yan, Zheng80825102009-11-12 09:35:36 +00007681 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007682 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007683
7684 trans = btrfs_start_transaction(root, 2);
7685 if (IS_ERR(trans)) {
7686 ret = err = PTR_ERR(trans);
7687 trans = NULL;
7688 break;
7689 }
7690
7691 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7692 rsv, min_size);
7693 BUG_ON(ret); /* shouldn't happen */
7694 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007695 }
7696
7697 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007698 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007699 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007700 if (ret)
7701 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007702 }
7703
Chris Mason917c16b2011-11-08 14:49:59 -05007704 if (trans) {
7705 trans->block_rsv = &root->fs_info->trans_block_rsv;
7706 ret = btrfs_update_inode(trans, root, inode);
7707 if (ret && !err)
7708 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007709
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007710 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007711 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007712 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007713
7714out:
7715 btrfs_free_block_rsv(root, rsv);
7716
Josef Bacik3893e332011-01-31 16:03:11 -05007717 if (ret && !err)
7718 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007719
Josef Bacik3893e332011-01-31 16:03:11 -05007720 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007721}
7722
Sven Wegener3b963622008-06-09 21:57:42 -04007723/*
Chris Masond352ac62008-09-29 15:18:18 -04007724 * create a new subvolume directory/inode (helper for the ioctl).
7725 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007726int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007727 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007728{
Chris Mason39279cc2007-06-12 06:35:45 -04007729 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007730 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007731 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007732
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007733 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7734 new_dirid, new_dirid,
7735 S_IFDIR | (~current_umask() & S_IRWXUGO),
7736 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007737 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007738 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007739 inode->i_op = &btrfs_dir_inode_operations;
7740 inode->i_fop = &btrfs_dir_file_operations;
7741
Miklos Szeredibfe86842011-10-28 14:13:29 +02007742 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007743 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007744
Yan, Zheng76dda932009-09-21 16:00:26 -04007745 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007746
Yan, Zheng76dda932009-09-21 16:00:26 -04007747 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007748 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007749}
7750
Chris Mason39279cc2007-06-12 06:35:45 -04007751struct inode *btrfs_alloc_inode(struct super_block *sb)
7752{
7753 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007754 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007755
7756 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7757 if (!ei)
7758 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007759
7760 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007761 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007762 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007763 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007764 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007765 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007766 ei->disk_i_size = 0;
7767 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007768 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007769 ei->index_cnt = (u64)-1;
7770 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007771 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007772
Josef Bacik9e0baf62011-07-15 15:16:44 +00007773 spin_lock_init(&ei->lock);
7774 ei->outstanding_extents = 0;
7775 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007776
Josef Bacik72ac3c02012-05-23 14:13:11 -04007777 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007778 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007779
Miao Xie16cdcec2011-04-22 18:12:22 +08007780 ei->delayed_node = NULL;
7781
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007782 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007783 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007784 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7785 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007786 ei->io_tree.track_uptodate = 1;
7787 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007788 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007789 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007790 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007791 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007792 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007793 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007794 RB_CLEAR_NODE(&ei->rb_node);
7795
7796 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007797}
7798
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007799static void btrfs_i_callback(struct rcu_head *head)
7800{
7801 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007802 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7803}
7804
Chris Mason39279cc2007-06-12 06:35:45 -04007805void btrfs_destroy_inode(struct inode *inode)
7806{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007807 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007808 struct btrfs_root *root = BTRFS_I(inode)->root;
7809
Al Virob3d9b7a2012-06-09 13:51:19 -04007810 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007811 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007812 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7813 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007814 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7815 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007816
Chris Mason5a3f23d2009-03-31 13:27:11 -04007817 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007818 * This can happen where we create an inode, but somebody else also
7819 * created the same inode and we need to destroy the one we already
7820 * created.
7821 */
7822 if (!root)
7823 goto free;
7824
7825 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007826 * Make sure we're properly removed from the ordered operation
7827 * lists.
7828 */
7829 smp_mb();
7830 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007831 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007832 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007833 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007834 }
7835
Josef Bacik8a35d952012-05-23 14:26:42 -04007836 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7837 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007838 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7839 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007840 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007841 }
Josef Bacik7b128762008-07-24 12:17:14 -04007842
Chris Masond3977122009-01-05 21:25:51 -05007843 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007844 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7845 if (!ordered)
7846 break;
7847 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007848 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7849 (unsigned long long)ordered->file_offset,
7850 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007851 btrfs_remove_ordered_extent(inode, ordered);
7852 btrfs_put_ordered_extent(ordered);
7853 btrfs_put_ordered_extent(ordered);
7854 }
7855 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007856 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007857 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007858free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007859 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007860}
7861
Al Viro45321ac2010-06-07 13:43:19 -04007862int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007863{
7864 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007865
Naohiro Aota6379ef92013-06-06 09:56:34 +00007866 if (root == NULL)
7867 return 1;
7868
Liu Bofa6ac872013-02-20 14:10:23 +00007869 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007870 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007871 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007872 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007873 else
Al Viro45321ac2010-06-07 13:43:19 -04007874 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007875}
7876
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007877static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007878{
7879 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7880
7881 inode_init_once(&ei->vfs_inode);
7882}
7883
7884void btrfs_destroy_cachep(void)
7885{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007886 /*
7887 * Make sure all delayed rcu free inodes are flushed before we
7888 * destroy cache.
7889 */
7890 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007891 if (btrfs_inode_cachep)
7892 kmem_cache_destroy(btrfs_inode_cachep);
7893 if (btrfs_trans_handle_cachep)
7894 kmem_cache_destroy(btrfs_trans_handle_cachep);
7895 if (btrfs_transaction_cachep)
7896 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007897 if (btrfs_path_cachep)
7898 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007899 if (btrfs_free_space_cachep)
7900 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007901 if (btrfs_delalloc_work_cachep)
7902 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007903}
7904
7905int btrfs_init_cachep(void)
7906{
David Sterba837e1972012-09-07 03:00:48 -06007907 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007908 sizeof(struct btrfs_inode), 0,
7909 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007910 if (!btrfs_inode_cachep)
7911 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007912
David Sterba837e1972012-09-07 03:00:48 -06007913 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007914 sizeof(struct btrfs_trans_handle), 0,
7915 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007916 if (!btrfs_trans_handle_cachep)
7917 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007918
David Sterba837e1972012-09-07 03:00:48 -06007919 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007920 sizeof(struct btrfs_transaction), 0,
7921 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007922 if (!btrfs_transaction_cachep)
7923 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007924
David Sterba837e1972012-09-07 03:00:48 -06007925 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007926 sizeof(struct btrfs_path), 0,
7927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007928 if (!btrfs_path_cachep)
7929 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007930
David Sterba837e1972012-09-07 03:00:48 -06007931 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007932 sizeof(struct btrfs_free_space), 0,
7933 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7934 if (!btrfs_free_space_cachep)
7935 goto fail;
7936
Miao Xie8ccf6f192012-10-25 09:28:04 +00007937 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7938 sizeof(struct btrfs_delalloc_work), 0,
7939 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7940 NULL);
7941 if (!btrfs_delalloc_work_cachep)
7942 goto fail;
7943
Chris Mason39279cc2007-06-12 06:35:45 -04007944 return 0;
7945fail:
7946 btrfs_destroy_cachep();
7947 return -ENOMEM;
7948}
7949
7950static int btrfs_getattr(struct vfsmount *mnt,
7951 struct dentry *dentry, struct kstat *stat)
7952{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007953 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007954 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007955 u32 blocksize = inode->i_sb->s_blocksize;
7956
Chris Mason39279cc2007-06-12 06:35:45 -04007957 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007958 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007959 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007960
7961 spin_lock(&BTRFS_I(inode)->lock);
7962 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7963 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007964 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007965 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007966 return 0;
7967}
7968
Chris Masond3977122009-01-05 21:25:51 -05007969static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7970 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007971{
7972 struct btrfs_trans_handle *trans;
7973 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007974 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007975 struct inode *new_inode = new_dentry->d_inode;
7976 struct inode *old_inode = old_dentry->d_inode;
7977 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007978 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007979 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007980 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007981 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007982
Li Zefan33345d012011-04-20 10:31:50 +08007983 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007984 return -EPERM;
7985
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007986 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007987 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007988 return -EXDEV;
7989
Li Zefan33345d012011-04-20 10:31:50 +08007990 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7991 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007992 return -ENOTEMPTY;
7993
Chris Mason39279cc2007-06-12 06:35:45 -04007994 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007995 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007996 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007997
7998
7999 /* check for collisions, even if the name isn't there */
8000 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8001 new_dentry->d_name.name,
8002 new_dentry->d_name.len);
8003
8004 if (ret) {
8005 if (ret == -EEXIST) {
8006 /* we shouldn't get
8007 * eexist without a new_inode */
8008 if (!new_inode) {
8009 WARN_ON(1);
8010 return ret;
8011 }
8012 } else {
8013 /* maybe -EOVERFLOW */
8014 return ret;
8015 }
8016 }
8017 ret = 0;
8018
Chris Mason5a3f23d2009-03-31 13:27:11 -04008019 /*
8020 * we're using rename to replace one file with another.
8021 * and the replacement file is large. Start IO on it now so
8022 * we don't add too much work to the end of the transaction
8023 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008024 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008025 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8026 filemap_flush(old_inode->i_mapping);
8027
Yan, Zheng76dda932009-09-21 16:00:26 -04008028 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008029 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008030 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008031 /*
8032 * We want to reserve the absolute worst case amount of items. So if
8033 * both inodes are subvols and we need to unlink them then that would
8034 * require 4 item modifications, but if they are both normal inodes it
8035 * would require 5 item modifications, so we'll assume their normal
8036 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8037 * should cover the worst case number of items we'll modify.
8038 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008039 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008040 if (IS_ERR(trans)) {
8041 ret = PTR_ERR(trans);
8042 goto out_notrans;
8043 }
Chris Mason5f39d392007-10-15 16:14:19 -04008044
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008045 if (dest != root)
8046 btrfs_record_root_in_trans(trans, dest);
8047
Yan, Zhenga5719522009-09-24 09:17:31 -04008048 ret = btrfs_set_inode_index(new_dir, &index);
8049 if (ret)
8050 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008051
Li Zefan33345d012011-04-20 10:31:50 +08008052 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008053 /* force full log commit if subvolume involved. */
8054 root->fs_info->last_trans_log_full_commit = trans->transid;
8055 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008056 ret = btrfs_insert_inode_ref(trans, dest,
8057 new_dentry->d_name.name,
8058 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008059 old_ino,
8060 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008061 if (ret)
8062 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008063 /*
8064 * this is an ugly little race, but the rename is required
8065 * to make sure that if we crash, the inode is either at the
8066 * old name or the new one. pinning the log transaction lets
8067 * us make sure we don't allow a log commit to come in after
8068 * we unlink the name but before we add the new name back in.
8069 */
8070 btrfs_pin_log_trans(root);
8071 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008072 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008073 * make sure the inode gets flushed if it is replacing
8074 * something.
8075 */
Li Zefan33345d012011-04-20 10:31:50 +08008076 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008077 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008078
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008079 inode_inc_iversion(old_dir);
8080 inode_inc_iversion(new_dir);
8081 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008082 old_dir->i_ctime = old_dir->i_mtime = ctime;
8083 new_dir->i_ctime = new_dir->i_mtime = ctime;
8084 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008085
Chris Mason12fcfd22009-03-24 10:24:20 -04008086 if (old_dentry->d_parent != new_dentry->d_parent)
8087 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8088
Li Zefan33345d012011-04-20 10:31:50 +08008089 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008090 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8091 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8092 old_dentry->d_name.name,
8093 old_dentry->d_name.len);
8094 } else {
Al Viro92986792011-03-04 17:14:37 +00008095 ret = __btrfs_unlink_inode(trans, root, old_dir,
8096 old_dentry->d_inode,
8097 old_dentry->d_name.name,
8098 old_dentry->d_name.len);
8099 if (!ret)
8100 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008101 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008102 if (ret) {
8103 btrfs_abort_transaction(trans, root, ret);
8104 goto out_fail;
8105 }
Chris Mason39279cc2007-06-12 06:35:45 -04008106
8107 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008108 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008109 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008110 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008111 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8112 root_objectid = BTRFS_I(new_inode)->location.objectid;
8113 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8114 root_objectid,
8115 new_dentry->d_name.name,
8116 new_dentry->d_name.len);
8117 BUG_ON(new_inode->i_nlink == 0);
8118 } else {
8119 ret = btrfs_unlink_inode(trans, dest, new_dir,
8120 new_dentry->d_inode,
8121 new_dentry->d_name.name,
8122 new_dentry->d_name.len);
8123 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008124 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008125 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008126 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008127 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008128 if (ret) {
8129 btrfs_abort_transaction(trans, root, ret);
8130 goto out_fail;
8131 }
Chris Mason39279cc2007-06-12 06:35:45 -04008132 }
Josef Bacikaec74772008-07-24 12:12:38 -04008133
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008134 ret = btrfs_add_link(trans, new_dir, old_inode,
8135 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008136 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008137 if (ret) {
8138 btrfs_abort_transaction(trans, root, ret);
8139 goto out_fail;
8140 }
Chris Mason39279cc2007-06-12 06:35:45 -04008141
Li Zefan33345d012011-04-20 10:31:50 +08008142 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008143 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008144 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008145 btrfs_end_log_trans(root);
8146 }
Chris Mason39279cc2007-06-12 06:35:45 -04008147out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008148 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008149out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008150 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008151 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008152
Chris Mason39279cc2007-06-12 06:35:45 -04008153 return ret;
8154}
8155
Miao Xie8ccf6f192012-10-25 09:28:04 +00008156static void btrfs_run_delalloc_work(struct btrfs_work *work)
8157{
8158 struct btrfs_delalloc_work *delalloc_work;
8159
8160 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8161 work);
8162 if (delalloc_work->wait)
8163 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8164 else
8165 filemap_flush(delalloc_work->inode->i_mapping);
8166
8167 if (delalloc_work->delay_iput)
8168 btrfs_add_delayed_iput(delalloc_work->inode);
8169 else
8170 iput(delalloc_work->inode);
8171 complete(&delalloc_work->completion);
8172}
8173
8174struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8175 int wait, int delay_iput)
8176{
8177 struct btrfs_delalloc_work *work;
8178
8179 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8180 if (!work)
8181 return NULL;
8182
8183 init_completion(&work->completion);
8184 INIT_LIST_HEAD(&work->list);
8185 work->inode = inode;
8186 work->wait = wait;
8187 work->delay_iput = delay_iput;
8188 work->work.func = btrfs_run_delalloc_work;
8189
8190 return work;
8191}
8192
8193void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8194{
8195 wait_for_completion(&work->completion);
8196 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8197}
8198
Chris Masond352ac62008-09-29 15:18:18 -04008199/*
8200 * some fairly slow code that needs optimization. This walks the list
8201 * of all the inodes with pending delalloc and forces them to disk.
8202 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008203static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008204{
Chris Masonea8c2812008-08-04 23:17:27 -04008205 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008206 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008207 struct btrfs_delalloc_work *work, *next;
8208 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008209 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008210 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008211
Miao Xie8ccf6f192012-10-25 09:28:04 +00008212 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008213 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008214
Miao Xieeb73c1b2013-05-15 07:48:22 +00008215 spin_lock(&root->delalloc_lock);
8216 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008217 while (!list_empty(&splice)) {
8218 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008219 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008220
Miao Xieeb73c1b2013-05-15 07:48:22 +00008221 list_move_tail(&binode->delalloc_inodes,
8222 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008223 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008224 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008225 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008226 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008227 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008228 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008229
8230 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8231 if (unlikely(!work)) {
8232 ret = -ENOMEM;
8233 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008234 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008235 list_add_tail(&work->list, &works);
8236 btrfs_queue_worker(&root->fs_info->flush_workers,
8237 &work->work);
8238
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008239 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008240 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008241 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008242 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008243
Miao Xie1eafa6c2013-01-22 10:49:00 +00008244 list_for_each_entry_safe(work, next, &works, list) {
8245 list_del_init(&work->list);
8246 btrfs_wait_and_free_delalloc_work(work);
8247 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008248 return 0;
8249out:
8250 list_for_each_entry_safe(work, next, &works, list) {
8251 list_del_init(&work->list);
8252 btrfs_wait_and_free_delalloc_work(work);
8253 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008254
Miao Xieeb73c1b2013-05-15 07:48:22 +00008255 if (!list_empty_careful(&splice)) {
8256 spin_lock(&root->delalloc_lock);
8257 list_splice_tail(&splice, &root->delalloc_inodes);
8258 spin_unlock(&root->delalloc_lock);
8259 }
8260 return ret;
8261}
8262
8263int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8264{
8265 int ret;
8266
8267 if (root->fs_info->sb->s_flags & MS_RDONLY)
8268 return -EROFS;
8269
8270 ret = __start_delalloc_inodes(root, delay_iput);
8271 /*
8272 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008273 * we have to make sure the IO is actually started and that
8274 * ordered extents get created before we return
8275 */
8276 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008277 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008278 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008279 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008280 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8281 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008282 }
8283 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008284 return ret;
8285}
8286
8287int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8288 int delay_iput)
8289{
8290 struct btrfs_root *root;
8291 struct list_head splice;
8292 int ret;
8293
8294 if (fs_info->sb->s_flags & MS_RDONLY)
8295 return -EROFS;
8296
8297 INIT_LIST_HEAD(&splice);
8298
8299 spin_lock(&fs_info->delalloc_root_lock);
8300 list_splice_init(&fs_info->delalloc_roots, &splice);
8301 while (!list_empty(&splice)) {
8302 root = list_first_entry(&splice, struct btrfs_root,
8303 delalloc_root);
8304 root = btrfs_grab_fs_root(root);
8305 BUG_ON(!root);
8306 list_move_tail(&root->delalloc_root,
8307 &fs_info->delalloc_roots);
8308 spin_unlock(&fs_info->delalloc_root_lock);
8309
8310 ret = __start_delalloc_inodes(root, delay_iput);
8311 btrfs_put_fs_root(root);
8312 if (ret)
8313 goto out;
8314
8315 spin_lock(&fs_info->delalloc_root_lock);
8316 }
8317 spin_unlock(&fs_info->delalloc_root_lock);
8318
8319 atomic_inc(&fs_info->async_submit_draining);
8320 while (atomic_read(&fs_info->nr_async_submits) ||
8321 atomic_read(&fs_info->async_delalloc_pages)) {
8322 wait_event(fs_info->async_submit_wait,
8323 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8324 atomic_read(&fs_info->async_delalloc_pages) == 0));
8325 }
8326 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008327 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008328out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008329 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008330 spin_lock(&fs_info->delalloc_root_lock);
8331 list_splice_tail(&splice, &fs_info->delalloc_roots);
8332 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008333 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008334 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008335}
8336
Chris Mason39279cc2007-06-12 06:35:45 -04008337static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8338 const char *symname)
8339{
8340 struct btrfs_trans_handle *trans;
8341 struct btrfs_root *root = BTRFS_I(dir)->root;
8342 struct btrfs_path *path;
8343 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008344 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008345 int err;
8346 int drop_inode = 0;
8347 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008348 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008349 int name_len;
8350 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008351 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008352 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008353 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008354
8355 name_len = strlen(symname) + 1;
8356 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8357 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008358
Josef Bacik9ed74f22009-09-11 16:12:44 -04008359 /*
8360 * 2 items for inode item and ref
8361 * 2 items for dir items
8362 * 1 item for xattr if selinux is on
8363 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008364 trans = btrfs_start_transaction(root, 5);
8365 if (IS_ERR(trans))
8366 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008367
Li Zefan581bb052011-04-20 10:06:11 +08008368 err = btrfs_find_free_ino(root, &objectid);
8369 if (err)
8370 goto out_unlock;
8371
Josef Bacikaec74772008-07-24 12:12:38 -04008372 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008373 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008374 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008375 if (IS_ERR(inode)) {
8376 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008377 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008378 }
Chris Mason39279cc2007-06-12 06:35:45 -04008379
Eric Paris2a7dba32011-02-01 11:05:39 -05008380 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008381 if (err) {
8382 drop_inode = 1;
8383 goto out_unlock;
8384 }
8385
Casey Schauflerad19db72011-12-15 10:09:07 -05008386 /*
8387 * If the active LSM wants to access the inode during
8388 * d_instantiate it needs these. Smack checks to see
8389 * if the filesystem supports xattrs by looking at the
8390 * ops vector.
8391 */
8392 inode->i_fop = &btrfs_file_operations;
8393 inode->i_op = &btrfs_file_inode_operations;
8394
Josef Bacika1b075d2010-11-19 20:36:11 +00008395 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008396 if (err)
8397 drop_inode = 1;
8398 else {
8399 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008400 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008401 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008402 }
Chris Mason39279cc2007-06-12 06:35:45 -04008403 if (drop_inode)
8404 goto out_unlock;
8405
8406 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008407 if (!path) {
8408 err = -ENOMEM;
8409 drop_inode = 1;
8410 goto out_unlock;
8411 }
Li Zefan33345d012011-04-20 10:31:50 +08008412 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008413 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008414 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8415 datasize = btrfs_file_extent_calc_inline_size(name_len);
8416 err = btrfs_insert_empty_item(trans, root, path, &key,
8417 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008418 if (err) {
8419 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008420 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008421 goto out_unlock;
8422 }
Chris Mason5f39d392007-10-15 16:14:19 -04008423 leaf = path->nodes[0];
8424 ei = btrfs_item_ptr(leaf, path->slots[0],
8425 struct btrfs_file_extent_item);
8426 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8427 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008428 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008429 btrfs_set_file_extent_encryption(leaf, ei, 0);
8430 btrfs_set_file_extent_compression(leaf, ei, 0);
8431 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8432 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8433
Chris Mason39279cc2007-06-12 06:35:45 -04008434 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008435 write_extent_buffer(leaf, symname, ptr, name_len);
8436 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008437 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008438
Chris Mason39279cc2007-06-12 06:35:45 -04008439 inode->i_op = &btrfs_symlink_inode_operations;
8440 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008441 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008442 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008443 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008444 err = btrfs_update_inode(trans, root, inode);
8445 if (err)
8446 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008447
8448out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008449 if (!err)
8450 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008451 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008452 if (drop_inode) {
8453 inode_dec_link_count(inode);
8454 iput(inode);
8455 }
Liu Bob53d3f52012-11-14 14:34:34 +00008456 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008457 return err;
8458}
Chris Mason16432982008-04-10 10:23:21 -04008459
Josef Bacik0af3d002010-06-21 14:48:16 -04008460static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8461 u64 start, u64 num_bytes, u64 min_size,
8462 loff_t actual_len, u64 *alloc_hint,
8463 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008464{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008465 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8466 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008467 struct btrfs_root *root = BTRFS_I(inode)->root;
8468 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008469 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008470 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008471 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008472 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008473 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008474
Josef Bacik0af3d002010-06-21 14:48:16 -04008475 if (trans)
8476 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008477 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008478 if (own_trans) {
8479 trans = btrfs_start_transaction(root, 3);
8480 if (IS_ERR(trans)) {
8481 ret = PTR_ERR(trans);
8482 break;
8483 }
Yan Zhengd899e052008-10-30 14:25:28 -04008484 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008485
Chris Mason154ea282013-03-05 11:11:26 -05008486 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8487 cur_bytes = max(cur_bytes, min_size);
8488 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008489 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008490 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008491 if (own_trans)
8492 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008493 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008494 }
8495
Yan Zhengd899e052008-10-30 14:25:28 -04008496 ret = insert_reserved_file_extent(trans, inode,
8497 cur_offset, ins.objectid,
8498 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008499 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008500 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008501 if (ret) {
8502 btrfs_abort_transaction(trans, root, ret);
8503 if (own_trans)
8504 btrfs_end_transaction(trans, root);
8505 break;
8506 }
Chris Masona1ed8352009-09-11 12:27:37 -04008507 btrfs_drop_extent_cache(inode, cur_offset,
8508 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008509
Josef Bacik5dc562c2012-08-17 13:14:17 -04008510 em = alloc_extent_map();
8511 if (!em) {
8512 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8513 &BTRFS_I(inode)->runtime_flags);
8514 goto next;
8515 }
8516
8517 em->start = cur_offset;
8518 em->orig_start = cur_offset;
8519 em->len = ins.offset;
8520 em->block_start = ins.objectid;
8521 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008522 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008523 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008524 em->bdev = root->fs_info->fs_devices->latest_bdev;
8525 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8526 em->generation = trans->transid;
8527
8528 while (1) {
8529 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008530 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008531 write_unlock(&em_tree->lock);
8532 if (ret != -EEXIST)
8533 break;
8534 btrfs_drop_extent_cache(inode, cur_offset,
8535 cur_offset + ins.offset - 1,
8536 0);
8537 }
8538 free_extent_map(em);
8539next:
Yan Zhengd899e052008-10-30 14:25:28 -04008540 num_bytes -= ins.offset;
8541 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008542 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008543
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008544 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008545 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008546 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008547 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008548 (actual_len > inode->i_size) &&
8549 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008550 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008551 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008552 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008553 i_size = cur_offset;
8554 i_size_write(inode, i_size);
8555 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008556 }
8557
Yan Zhengd899e052008-10-30 14:25:28 -04008558 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008559
8560 if (ret) {
8561 btrfs_abort_transaction(trans, root, ret);
8562 if (own_trans)
8563 btrfs_end_transaction(trans, root);
8564 break;
8565 }
Yan Zhengd899e052008-10-30 14:25:28 -04008566
Josef Bacik0af3d002010-06-21 14:48:16 -04008567 if (own_trans)
8568 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008569 }
Yan Zhengd899e052008-10-30 14:25:28 -04008570 return ret;
8571}
8572
Josef Bacik0af3d002010-06-21 14:48:16 -04008573int btrfs_prealloc_file_range(struct inode *inode, int mode,
8574 u64 start, u64 num_bytes, u64 min_size,
8575 loff_t actual_len, u64 *alloc_hint)
8576{
8577 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8578 min_size, actual_len, alloc_hint,
8579 NULL);
8580}
8581
8582int btrfs_prealloc_file_range_trans(struct inode *inode,
8583 struct btrfs_trans_handle *trans, int mode,
8584 u64 start, u64 num_bytes, u64 min_size,
8585 loff_t actual_len, u64 *alloc_hint)
8586{
8587 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8588 min_size, actual_len, alloc_hint, trans);
8589}
8590
Chris Masone6dcd2d2008-07-17 12:53:50 -04008591static int btrfs_set_page_dirty(struct page *page)
8592{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008593 return __set_page_dirty_nobuffers(page);
8594}
8595
Al Viro10556cb2011-06-20 19:28:19 -04008596static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008597{
Li Zefanb83cc962010-12-20 16:04:08 +08008598 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008599 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008600
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008601 if (mask & MAY_WRITE &&
8602 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8603 if (btrfs_root_readonly(root))
8604 return -EROFS;
8605 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8606 return -EACCES;
8607 }
Al Viro2830ba72011-06-20 19:16:29 -04008608 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008609}
Chris Mason39279cc2007-06-12 06:35:45 -04008610
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008611static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008612 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008613 .lookup = btrfs_lookup,
8614 .create = btrfs_create,
8615 .unlink = btrfs_unlink,
8616 .link = btrfs_link,
8617 .mkdir = btrfs_mkdir,
8618 .rmdir = btrfs_rmdir,
8619 .rename = btrfs_rename,
8620 .symlink = btrfs_symlink,
8621 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008622 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008623 .setxattr = btrfs_setxattr,
8624 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008625 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008626 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008627 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008628 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008629};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008630static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008631 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008632 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008633 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008634};
Yan, Zheng76dda932009-09-21 16:00:26 -04008635
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008636static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008637 .llseek = generic_file_llseek,
8638 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008639 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008640 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008641#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008642 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008643#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008644 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008645 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008646};
8647
Chris Masond1310b22008-01-24 16:13:08 -05008648static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008649 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008650 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008651 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008652 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008653 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008654 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008655 .set_bit_hook = btrfs_set_bit_hook,
8656 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008657 .merge_extent_hook = btrfs_merge_extent_hook,
8658 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008659};
8660
Chris Mason35054392009-01-21 13:11:13 -05008661/*
8662 * btrfs doesn't support the bmap operation because swapfiles
8663 * use bmap to make a mapping of extents in the file. They assume
8664 * these extents won't change over the life of the file and they
8665 * use the bmap result to do IO directly to the drive.
8666 *
8667 * the btrfs bmap call would return logical addresses that aren't
8668 * suitable for IO and they also will change frequently as COW
8669 * operations happen. So, swapfile + btrfs == corruption.
8670 *
8671 * For now we're avoiding this by dropping bmap.
8672 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008673static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008674 .readpage = btrfs_readpage,
8675 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008676 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008677 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008678 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008679 .invalidatepage = btrfs_invalidatepage,
8680 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008681 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008682 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008683};
8684
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008685static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008686 .readpage = btrfs_readpage,
8687 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008688 .invalidatepage = btrfs_invalidatepage,
8689 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008690};
8691
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008692static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008693 .getattr = btrfs_getattr,
8694 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008695 .setxattr = btrfs_setxattr,
8696 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008697 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008698 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008699 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008700 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008701 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008702 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008703};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008704static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008705 .getattr = btrfs_getattr,
8706 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008707 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008708 .setxattr = btrfs_setxattr,
8709 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008710 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008711 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008712 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008713 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008714};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008715static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008716 .readlink = generic_readlink,
8717 .follow_link = page_follow_link_light,
8718 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008719 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008720 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008721 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008722 .setxattr = btrfs_setxattr,
8723 .getxattr = btrfs_getxattr,
8724 .listxattr = btrfs_listxattr,
8725 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008726 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008727 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008728};
Yan, Zheng76dda932009-09-21 16:00:26 -04008729
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008730const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008731 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008732 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008733};