blob: 509112da6118de97476f381e4d691f66812b75a4 [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 Masonb888db22007-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 Masonb888db22007-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 Masonf03d9302009-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 Masonf03d9302009-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
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400703 if (ret == -ENOSPC) {
704 unlock_extent(io_tree, async_extent->start,
705 async_extent->start +
706 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100707 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400708 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500709 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500710 }
711
Yan, Zhengc2167752009-11-12 09:34:21 +0000712 /*
713 * here we're doing allocation and writeback of the
714 * compressed pages
715 */
716 btrfs_drop_extent_cache(inode, async_extent->start,
717 async_extent->start +
718 async_extent->ram_size - 1, 0);
719
David Sterba172ddd62011-04-21 00:48:27 +0200720 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000721 if (!em) {
722 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500723 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000724 }
Chris Mason771ed682008-11-06 22:02:51 -0500725 em->start = async_extent->start;
726 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500727 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400728 em->mod_start = em->start;
729 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500730
731 em->block_start = ins.objectid;
732 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500733 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400734 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500735 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800736 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500737 set_bit(EXTENT_FLAG_PINNED, &em->flags);
738 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400739 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500740
Chris Masond3977122009-01-05 21:25:51 -0500741 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400742 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400743 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400744 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500745 if (ret != -EEXIST) {
746 free_extent_map(em);
747 break;
748 }
749 btrfs_drop_extent_cache(inode, async_extent->start,
750 async_extent->start +
751 async_extent->ram_size - 1, 0);
752 }
753
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 if (ret)
755 goto out_free_reserve;
756
Li Zefan261507a02010-12-17 14:21:50 +0800757 ret = btrfs_add_ordered_extent_compress(inode,
758 async_extent->start,
759 ins.objectid,
760 async_extent->ram_size,
761 ins.offset,
762 BTRFS_ORDERED_COMPRESSED,
763 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500764 if (ret)
765 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500766
Chris Mason771ed682008-11-06 22:02:51 -0500767 /*
768 * clear dirty, set writeback and unlock the pages.
769 */
770 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400771 &BTRFS_I(inode)->io_tree,
772 async_extent->start,
773 async_extent->start +
774 async_extent->ram_size - 1,
775 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
776 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400777 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400778 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500779
780 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500781 async_extent->start,
782 async_extent->ram_size,
783 ins.objectid,
784 ins.offset, async_extent->pages,
785 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500786 alloc_hint = ins.objectid + ins.offset;
787 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 if (ret)
789 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500790 cond_resched();
791 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100792 ret = 0;
793out:
794 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795out_free_reserve:
796 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100797out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
799 async_extent->start,
800 async_extent->start +
801 async_extent->ram_size - 1,
802 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
803 EXTENT_CLEAR_UNLOCK |
804 EXTENT_CLEAR_DELALLOC |
805 EXTENT_CLEAR_DIRTY |
806 EXTENT_SET_WRITEBACK |
807 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100808 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500809 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500810}
811
Josef Bacik4b46fce2010-05-23 11:00:55 -0400812static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
813 u64 num_bytes)
814{
815 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
816 struct extent_map *em;
817 u64 alloc_hint = 0;
818
819 read_lock(&em_tree->lock);
820 em = search_extent_mapping(em_tree, start, num_bytes);
821 if (em) {
822 /*
823 * if block start isn't an actual block number then find the
824 * first block in this inode and use that as a hint. If that
825 * block is also bogus then just don't worry about it.
826 */
827 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
828 free_extent_map(em);
829 em = search_extent_mapping(em_tree, 0, 0);
830 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
831 alloc_hint = em->block_start;
832 if (em)
833 free_extent_map(em);
834 } else {
835 alloc_hint = em->block_start;
836 free_extent_map(em);
837 }
838 }
839 read_unlock(&em_tree->lock);
840
841 return alloc_hint;
842}
843
Chris Mason771ed682008-11-06 22:02:51 -0500844/*
845 * when extent_io.c finds a delayed allocation range in the file,
846 * the call backs end up in this code. The basic idea is to
847 * allocate extents on disk for the range, and create ordered data structs
848 * in ram to track those extents.
849 *
850 * locked_page is the page that writepage had locked already. We use
851 * it to make sure we don't do extra locks or unlocks.
852 *
853 * *page_started is set to one if we unlock locked_page and do everything
854 * required to start IO on it. It may be clean and already done with
855 * IO when we return.
856 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000857static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
858 struct inode *inode,
859 struct btrfs_root *root,
860 struct page *locked_page,
861 u64 start, u64 end, int *page_started,
862 unsigned long *nr_written,
863 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500864{
Chris Mason771ed682008-11-06 22:02:51 -0500865 u64 alloc_hint = 0;
866 u64 num_bytes;
867 unsigned long ram_size;
868 u64 disk_num_bytes;
869 u64 cur_alloc_size;
870 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500871 struct btrfs_key ins;
872 struct extent_map *em;
873 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
874 int ret = 0;
875
Liu Bo83eea1f2012-07-10 05:28:39 -0600876 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500877
Qu Wenruofda28322013-02-26 08:10:22 +0000878 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500879 num_bytes = max(blocksize, num_bytes);
880 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500881
Chris Mason4cb53002011-05-24 15:35:30 -0400882 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400883 if (num_bytes < 64 * 1024 &&
884 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400885 btrfs_add_inode_defrag(trans, inode);
886
Chris Mason771ed682008-11-06 22:02:51 -0500887 if (start == 0) {
888 /* lets try to make an inline extent */
889 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000890 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500891 if (ret == 0) {
892 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400893 &BTRFS_I(inode)->io_tree,
894 start, end, NULL,
895 EXTENT_CLEAR_UNLOCK_PAGE |
896 EXTENT_CLEAR_UNLOCK |
897 EXTENT_CLEAR_DELALLOC |
898 EXTENT_CLEAR_DIRTY |
899 EXTENT_SET_WRITEBACK |
900 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000901
Chris Mason771ed682008-11-06 22:02:51 -0500902 *nr_written = *nr_written +
903 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
904 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500905 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100906 } else if (ret < 0) {
907 btrfs_abort_transaction(trans, root, ret);
908 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500909 }
910 }
Chris Masonc8b97812008-10-29 14:49:59 -0400911
912 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200913 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400914
Josef Bacik4b46fce2010-05-23 11:00:55 -0400915 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400916 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400917
Chris Masond3977122009-01-05 21:25:51 -0500918 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400919 unsigned long op;
920
Josef Bacik287a0ab2010-03-19 18:07:23 +0000921 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400922 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500923 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500924 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 if (ret < 0) {
926 btrfs_abort_transaction(trans, root, ret);
927 goto out_unlock;
928 }
Chris Masond3977122009-01-05 21:25:51 -0500929
David Sterba172ddd62011-04-21 00:48:27 +0200930 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000931 if (!em) {
932 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000933 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000934 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400935 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500936 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500937 ram_size = ins.offset;
938 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400939 em->mod_start = em->start;
940 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400941
Chris Masone6dcd2d2008-07-17 12:53:50 -0400942 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400943 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500944 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400945 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400946 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400947 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400948 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400949
Chris Masond3977122009-01-05 21:25:51 -0500950 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400951 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400952 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400953 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400954 if (ret != -EEXIST) {
955 free_extent_map(em);
956 break;
957 }
958 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400959 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960 }
Liu Boace68ba2013-04-22 10:53:47 +0000961 if (ret)
962 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400963
Chris Mason98d20f62008-04-14 09:46:10 -0400964 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400965 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500966 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000967 if (ret)
968 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400969
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 if (root->root_key.objectid ==
971 BTRFS_DATA_RELOC_TREE_OBJECTID) {
972 ret = btrfs_reloc_clone_csums(inode, start,
973 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100974 if (ret) {
975 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000976 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500978 }
979
Chris Masond3977122009-01-05 21:25:51 -0500980 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400981 break;
Chris Masond3977122009-01-05 21:25:51 -0500982
Chris Masonc8b97812008-10-29 14:49:59 -0400983 /* we're not doing compressed IO, don't unlock the first
984 * page (which the caller expects to stay locked), don't
985 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400986 *
987 * Do set the Private2 bit so we know this page was properly
988 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400989 */
Chris Masona791e352009-10-08 11:27:10 -0400990 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
991 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
992 EXTENT_SET_PRIVATE2;
993
Chris Masonc8b97812008-10-29 14:49:59 -0400994 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
995 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400996 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400997 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500998 num_bytes -= cur_alloc_size;
999 alloc_hint = ins.objectid + ins.offset;
1000 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001001 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001003 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001004
Liu Boace68ba2013-04-22 10:53:47 +00001005out_reserve:
1006 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007out_unlock:
1008 extent_clear_unlock_delalloc(inode,
1009 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001010 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001011 EXTENT_CLEAR_UNLOCK_PAGE |
1012 EXTENT_CLEAR_UNLOCK |
1013 EXTENT_CLEAR_DELALLOC |
1014 EXTENT_CLEAR_DIRTY |
1015 EXTENT_SET_WRITEBACK |
1016 EXTENT_END_WRITEBACK);
1017
1018 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001019}
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001021static noinline int cow_file_range(struct inode *inode,
1022 struct page *locked_page,
1023 u64 start, u64 end, int *page_started,
1024 unsigned long *nr_written,
1025 int unlock)
1026{
1027 struct btrfs_trans_handle *trans;
1028 struct btrfs_root *root = BTRFS_I(inode)->root;
1029 int ret;
1030
1031 trans = btrfs_join_transaction(root);
1032 if (IS_ERR(trans)) {
1033 extent_clear_unlock_delalloc(inode,
1034 &BTRFS_I(inode)->io_tree,
1035 start, end, locked_page,
1036 EXTENT_CLEAR_UNLOCK_PAGE |
1037 EXTENT_CLEAR_UNLOCK |
1038 EXTENT_CLEAR_DELALLOC |
1039 EXTENT_CLEAR_DIRTY |
1040 EXTENT_SET_WRITEBACK |
1041 EXTENT_END_WRITEBACK);
1042 return PTR_ERR(trans);
1043 }
1044 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1045
1046 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1047 page_started, nr_written, unlock);
1048
1049 btrfs_end_transaction(trans, root);
1050
1051 return ret;
1052}
1053
Chris Mason771ed682008-11-06 22:02:51 -05001054/*
1055 * work queue call back to started compression on a file and pages
1056 */
1057static noinline void async_cow_start(struct btrfs_work *work)
1058{
1059 struct async_cow *async_cow;
1060 int num_added = 0;
1061 async_cow = container_of(work, struct async_cow, work);
1062
1063 compress_file_range(async_cow->inode, async_cow->locked_page,
1064 async_cow->start, async_cow->end, async_cow,
1065 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001066 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001067 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001068 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001069 }
Chris Mason771ed682008-11-06 22:02:51 -05001070}
1071
1072/*
1073 * work queue call back to submit previously compressed pages
1074 */
1075static noinline void async_cow_submit(struct btrfs_work *work)
1076{
1077 struct async_cow *async_cow;
1078 struct btrfs_root *root;
1079 unsigned long nr_pages;
1080
1081 async_cow = container_of(work, struct async_cow, work);
1082
1083 root = async_cow->root;
1084 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1085 PAGE_CACHE_SHIFT;
1086
Josef Bacik66657b32012-08-01 15:36:24 -04001087 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001088 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001089 waitqueue_active(&root->fs_info->async_submit_wait))
1090 wake_up(&root->fs_info->async_submit_wait);
1091
Chris Masond3977122009-01-05 21:25:51 -05001092 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001093 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001094}
Chris Masonc8b97812008-10-29 14:49:59 -04001095
Chris Mason771ed682008-11-06 22:02:51 -05001096static noinline void async_cow_free(struct btrfs_work *work)
1097{
1098 struct async_cow *async_cow;
1099 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001100 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001101 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001102 kfree(async_cow);
1103}
1104
1105static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1106 u64 start, u64 end, int *page_started,
1107 unsigned long *nr_written)
1108{
1109 struct async_cow *async_cow;
1110 struct btrfs_root *root = BTRFS_I(inode)->root;
1111 unsigned long nr_pages;
1112 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001113 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001114
Chris Masona3429ab2009-10-08 12:30:20 -04001115 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1116 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001117 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001118 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001120 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001121 async_cow->root = root;
1122 async_cow->locked_page = locked_page;
1123 async_cow->start = start;
1124
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001125 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001126 cur_end = end;
1127 else
1128 cur_end = min(end, start + 512 * 1024 - 1);
1129
1130 async_cow->end = cur_end;
1131 INIT_LIST_HEAD(&async_cow->extents);
1132
1133 async_cow->work.func = async_cow_start;
1134 async_cow->work.ordered_func = async_cow_submit;
1135 async_cow->work.ordered_free = async_cow_free;
1136 async_cow->work.flags = 0;
1137
Chris Mason771ed682008-11-06 22:02:51 -05001138 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1139 PAGE_CACHE_SHIFT;
1140 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1141
1142 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1143 &async_cow->work);
1144
1145 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1146 wait_event(root->fs_info->async_submit_wait,
1147 (atomic_read(&root->fs_info->async_delalloc_pages) <
1148 limit));
1149 }
1150
Chris Masond3977122009-01-05 21:25:51 -05001151 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001152 atomic_read(&root->fs_info->async_delalloc_pages)) {
1153 wait_event(root->fs_info->async_submit_wait,
1154 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1155 0));
1156 }
1157
1158 *nr_written += nr_pages;
1159 start = cur_end + 1;
1160 }
1161 *page_started = 1;
1162 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001163}
1164
Chris Masond3977122009-01-05 21:25:51 -05001165static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001166 u64 bytenr, u64 num_bytes)
1167{
1168 int ret;
1169 struct btrfs_ordered_sum *sums;
1170 LIST_HEAD(list);
1171
Yan Zheng07d400a2009-01-06 11:42:00 -05001172 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001173 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001174 if (ret == 0 && list_empty(&list))
1175 return 0;
1176
1177 while (!list_empty(&list)) {
1178 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1179 list_del(&sums->list);
1180 kfree(sums);
1181 }
1182 return 1;
1183}
1184
Chris Masond352ac62008-09-29 15:18:18 -04001185/*
1186 * when nowcow writeback call back. This checks for snapshots or COW copies
1187 * of the extents that exist in the file, and COWs the file as required.
1188 *
1189 * If no cow copies or snapshots exist, we write directly to the existing
1190 * blocks on disk
1191 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001192static noinline int run_delalloc_nocow(struct inode *inode,
1193 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001194 u64 start, u64 end, int *page_started, int force,
1195 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001196{
Chris Masonbe20aa92007-12-17 20:14:01 -05001197 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001198 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001200 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 u64 cow_start;
1204 u64 cur_offset;
1205 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 u64 disk_bytenr;
1208 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001209 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001210 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001213 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 int nocow;
1215 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001216 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001217 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001218
1219 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001220 if (!path) {
1221 extent_clear_unlock_delalloc(inode,
1222 &BTRFS_I(inode)->io_tree,
1223 start, end, locked_page,
1224 EXTENT_CLEAR_UNLOCK_PAGE |
1225 EXTENT_CLEAR_UNLOCK |
1226 EXTENT_CLEAR_DELALLOC |
1227 EXTENT_CLEAR_DIRTY |
1228 EXTENT_SET_WRITEBACK |
1229 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001230 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001231 }
Li Zefan82d59022011-04-20 10:33:24 +08001232
Liu Bo83eea1f2012-07-10 05:28:39 -06001233 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001234
1235 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001236 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001237 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001238 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001239
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001240 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001241 extent_clear_unlock_delalloc(inode,
1242 &BTRFS_I(inode)->io_tree,
1243 start, end, locked_page,
1244 EXTENT_CLEAR_UNLOCK_PAGE |
1245 EXTENT_CLEAR_UNLOCK |
1246 EXTENT_CLEAR_DELALLOC |
1247 EXTENT_CLEAR_DIRTY |
1248 EXTENT_SET_WRITEBACK |
1249 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 btrfs_free_path(path);
1251 return PTR_ERR(trans);
1252 }
1253
Josef Bacik74b21072011-04-13 12:02:53 -04001254 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001255
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 cow_start = (u64)-1;
1257 cur_offset = start;
1258 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001259 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001261 if (ret < 0) {
1262 btrfs_abort_transaction(trans, root, ret);
1263 goto error;
1264 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1266 leaf = path->nodes[0];
1267 btrfs_item_key_to_cpu(leaf, &found_key,
1268 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001269 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 found_key.type == BTRFS_EXTENT_DATA_KEY)
1271 path->slots[0]--;
1272 }
1273 check_prev = 0;
1274next_slot:
1275 leaf = path->nodes[0];
1276 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1277 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 if (ret < 0) {
1279 btrfs_abort_transaction(trans, root, ret);
1280 goto error;
1281 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 if (ret > 0)
1283 break;
1284 leaf = path->nodes[0];
1285 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001286
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 nocow = 0;
1288 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001289 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001291
Li Zefan33345d012011-04-20 10:31:50 +08001292 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1294 found_key.offset > end)
1295 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001296
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 if (found_key.offset > cur_offset) {
1298 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001299 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 goto out_check;
1301 }
Chris Masonc31f8832008-01-08 15:46:31 -05001302
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 fi = btrfs_item_ptr(leaf, path->slots[0],
1304 struct btrfs_file_extent_item);
1305 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001306
Josef Bacikcc95bef2013-04-04 14:31:27 -04001307 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001308 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1309 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001311 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 extent_end = found_key.offset +
1313 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001314 disk_num_bytes =
1315 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (extent_end <= start) {
1317 path->slots[0]++;
1318 goto next_slot;
1319 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001320 if (disk_bytenr == 0)
1321 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 if (btrfs_file_extent_compression(leaf, fi) ||
1323 btrfs_file_extent_encryption(leaf, fi) ||
1324 btrfs_file_extent_other_encoding(leaf, fi))
1325 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001326 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1327 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001328 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001330 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 found_key.offset -
1332 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001334 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 disk_bytenr += cur_offset - found_key.offset;
1336 num_bytes = min(end + 1, extent_end) - cur_offset;
1337 /*
1338 * force cow if csum exists in the range.
1339 * this ensure that csum for a given extent are
1340 * either valid or do not exist.
1341 */
1342 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1343 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 nocow = 1;
1345 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1346 extent_end = found_key.offset +
1347 btrfs_file_extent_inline_len(leaf, fi);
1348 extent_end = ALIGN(extent_end, root->sectorsize);
1349 } else {
1350 BUG_ON(1);
1351 }
1352out_check:
1353 if (extent_end <= start) {
1354 path->slots[0]++;
1355 goto next_slot;
1356 }
1357 if (!nocow) {
1358 if (cow_start == (u64)-1)
1359 cow_start = cur_offset;
1360 cur_offset = extent_end;
1361 if (cur_offset > end)
1362 break;
1363 path->slots[0]++;
1364 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001365 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001366
David Sterbab3b4aa72011-04-21 01:20:15 +02001367 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001369 ret = __cow_file_range(trans, inode, root, locked_page,
1370 cow_start, found_key.offset - 1,
1371 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001372 if (ret) {
1373 btrfs_abort_transaction(trans, root, ret);
1374 goto error;
1375 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001377 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001378
Yan Zhengd899e052008-10-30 14:25:28 -04001379 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1380 struct extent_map *em;
1381 struct extent_map_tree *em_tree;
1382 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001383 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001384 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001385 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001386 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001387 em->len = num_bytes;
1388 em->block_len = num_bytes;
1389 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001390 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001391 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001392 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001393 em->mod_start = em->start;
1394 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001395 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001396 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001397 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001398 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001399 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001400 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001401 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001402 if (ret != -EEXIST) {
1403 free_extent_map(em);
1404 break;
1405 }
1406 btrfs_drop_extent_cache(inode, em->start,
1407 em->start + em->len - 1, 0);
1408 }
1409 type = BTRFS_ORDERED_PREALLOC;
1410 } else {
1411 type = BTRFS_ORDERED_NOCOW;
1412 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001413
1414 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001415 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001416 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001417
Yan, Zhengefa56462010-05-16 10:49:59 -04001418 if (root->root_key.objectid ==
1419 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1420 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1421 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001422 if (ret) {
1423 btrfs_abort_transaction(trans, root, ret);
1424 goto error;
1425 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001426 }
1427
Yan Zhengd899e052008-10-30 14:25:28 -04001428 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001429 cur_offset, cur_offset + num_bytes - 1,
1430 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1431 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1432 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 cur_offset = extent_end;
1434 if (cur_offset > end)
1435 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001436 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001437 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438
Josef Bacik17ca04a2012-05-31 15:58:55 -04001439 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001441 cur_offset = end;
1442 }
1443
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001445 ret = __cow_file_range(trans, inode, root, locked_page,
1446 cow_start, end,
1447 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 if (ret) {
1449 btrfs_abort_transaction(trans, root, ret);
1450 goto error;
1451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 }
1453
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001454error:
Miao Xiea698d0752012-09-20 01:51:59 -06001455 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 if (!ret)
1457 ret = err;
1458
Josef Bacik17ca04a2012-05-31 15:58:55 -04001459 if (ret && cur_offset < end)
1460 extent_clear_unlock_delalloc(inode,
1461 &BTRFS_I(inode)->io_tree,
1462 cur_offset, end, locked_page,
1463 EXTENT_CLEAR_UNLOCK_PAGE |
1464 EXTENT_CLEAR_UNLOCK |
1465 EXTENT_CLEAR_DELALLOC |
1466 EXTENT_CLEAR_DIRTY |
1467 EXTENT_SET_WRITEBACK |
1468 EXTENT_END_WRITEBACK);
1469
Yan Zheng7ea394f2008-08-05 13:05:02 -04001470 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001471 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001472}
1473
Chris Masond352ac62008-09-29 15:18:18 -04001474/*
1475 * extent_io.c call back to do delayed allocation processing
1476 */
Chris Masonc8b97812008-10-29 14:49:59 -04001477static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001478 u64 start, u64 end, int *page_started,
1479 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001480{
Chris Masonbe20aa92007-12-17 20:14:01 -05001481 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001482 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001483
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001484 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001485 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001486 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001487 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001488 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001489 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001490 } else if (!btrfs_test_opt(root, COMPRESS) &&
1491 !(BTRFS_I(inode)->force_compress) &&
1492 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001493 ret = cow_file_range(inode, locked_page, start, end,
1494 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001495 } else {
1496 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1497 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001498 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001499 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001500 }
Chris Masonb888db22007-08-27 16:49:44 -04001501 return ret;
1502}
1503
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001504static void btrfs_split_extent_hook(struct inode *inode,
1505 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001507 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001508 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001509 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001510
Josef Bacik9e0baf62011-07-15 15:16:44 +00001511 spin_lock(&BTRFS_I(inode)->lock);
1512 BTRFS_I(inode)->outstanding_extents++;
1513 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001514}
1515
1516/*
1517 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1518 * extents so we can keep track of new extents that are just merged onto old
1519 * extents, such as when we are doing sequential writes, so we can properly
1520 * account for the metadata space we'll need.
1521 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001522static void btrfs_merge_extent_hook(struct inode *inode,
1523 struct extent_state *new,
1524 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526 /* not delalloc, ignore it */
1527 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001528 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001529
Josef Bacik9e0baf62011-07-15 15:16:44 +00001530 spin_lock(&BTRFS_I(inode)->lock);
1531 BTRFS_I(inode)->outstanding_extents--;
1532 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001533}
1534
Miao Xieeb73c1b2013-05-15 07:48:22 +00001535static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1536 struct inode *inode)
1537{
1538 spin_lock(&root->delalloc_lock);
1539 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1540 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1541 &root->delalloc_inodes);
1542 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1543 &BTRFS_I(inode)->runtime_flags);
1544 root->nr_delalloc_inodes++;
1545 if (root->nr_delalloc_inodes == 1) {
1546 spin_lock(&root->fs_info->delalloc_root_lock);
1547 BUG_ON(!list_empty(&root->delalloc_root));
1548 list_add_tail(&root->delalloc_root,
1549 &root->fs_info->delalloc_roots);
1550 spin_unlock(&root->fs_info->delalloc_root_lock);
1551 }
1552 }
1553 spin_unlock(&root->delalloc_lock);
1554}
1555
1556static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1557 struct inode *inode)
1558{
1559 spin_lock(&root->delalloc_lock);
1560 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1561 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1562 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1563 &BTRFS_I(inode)->runtime_flags);
1564 root->nr_delalloc_inodes--;
1565 if (!root->nr_delalloc_inodes) {
1566 spin_lock(&root->fs_info->delalloc_root_lock);
1567 BUG_ON(list_empty(&root->delalloc_root));
1568 list_del_init(&root->delalloc_root);
1569 spin_unlock(&root->fs_info->delalloc_root_lock);
1570 }
1571 }
1572 spin_unlock(&root->delalloc_lock);
1573}
1574
Chris Masond352ac62008-09-29 15:18:18 -04001575/*
1576 * extent_io.c set_bit_hook, used to track delayed allocation
1577 * bytes in this file, and to maintain the list of inodes that
1578 * have pending delalloc work to be done.
1579 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001580static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001581 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001582{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001583
Chris Mason75eff682008-12-15 15:54:40 -05001584 /*
1585 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001586 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001587 * bit, which is only set or cleared with irqs on
1588 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001589 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001590 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001592 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001593
Josef Bacik9e0baf62011-07-15 15:16:44 +00001594 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001595 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001596 } else {
1597 spin_lock(&BTRFS_I(inode)->lock);
1598 BTRFS_I(inode)->outstanding_extents++;
1599 spin_unlock(&BTRFS_I(inode)->lock);
1600 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001601
Miao Xie963d6782013-01-29 10:10:51 +00001602 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1603 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001604 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001606 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001607 &BTRFS_I(inode)->runtime_flags))
1608 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001609 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001610 }
Chris Mason291d6732008-01-29 15:55:23 -05001611}
1612
Chris Masond352ac62008-09-29 15:18:18 -04001613/*
1614 * extent_io.c clear_bit_hook, see set_bit_hook for why
1615 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001616static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001617 struct extent_state *state,
1618 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001619{
Chris Mason75eff682008-12-15 15:54:40 -05001620 /*
1621 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001622 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001623 * bit, which is only set or cleared with irqs on
1624 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001625 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001626 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001627 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001628 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001629
Josef Bacik9e0baf62011-07-15 15:16:44 +00001630 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001631 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001632 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1633 spin_lock(&BTRFS_I(inode)->lock);
1634 BTRFS_I(inode)->outstanding_extents--;
1635 spin_unlock(&BTRFS_I(inode)->lock);
1636 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001637
1638 if (*bits & EXTENT_DO_ACCOUNTING)
1639 btrfs_delalloc_release_metadata(inode, len);
1640
Josef Bacik0cb59c92010-07-02 12:14:14 -04001641 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1642 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001643 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644
Miao Xie963d6782013-01-29 10:10:51 +00001645 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1646 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001647 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001648 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001649 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001650 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001651 &BTRFS_I(inode)->runtime_flags))
1652 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001653 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001654 }
Chris Mason291d6732008-01-29 15:55:23 -05001655}
1656
Chris Masond352ac62008-09-29 15:18:18 -04001657/*
1658 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1659 * we don't create bios that span stripes or chunks
1660 */
David Woodhouse64a16702009-07-15 23:29:37 +01001661int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001662 size_t size, struct bio *bio,
1663 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001664{
1665 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001666 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001667 u64 length = 0;
1668 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001669 int ret;
1670
Chris Mason771ed682008-11-06 22:02:51 -05001671 if (bio_flags & EXTENT_BIO_COMPRESSED)
1672 return 0;
1673
Chris Masonf2d8d742008-04-21 10:03:05 -04001674 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001675 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001676 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001677 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001678 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001679 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001680 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001681 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001682 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001683}
1684
Chris Masond352ac62008-09-29 15:18:18 -04001685/*
1686 * in order to insert checksums into the metadata in large chunks,
1687 * we wait until bio submission time. All the pages in the bio are
1688 * checksummed and sums are attached onto the ordered extent record.
1689 *
1690 * At IO completion time the cums attached on the ordered extent record
1691 * are inserted into the btree
1692 */
Chris Masond3977122009-01-05 21:25:51 -05001693static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1694 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001695 unsigned long bio_flags,
1696 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001697{
Chris Mason065631f2008-02-20 12:07:25 -05001698 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001699 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001700
Chris Masond20f7042008-12-08 16:58:54 -05001701 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001702 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001703 return 0;
1704}
Chris Masone0156402008-04-16 11:15:20 -04001705
Chris Mason4a69a412008-11-06 22:03:00 -05001706/*
1707 * in order to insert checksums into the metadata in large chunks,
1708 * we wait until bio submission time. All the pages in the bio are
1709 * checksummed and sums are attached onto the ordered extent record.
1710 *
1711 * At IO completion time the cums attached on the ordered extent record
1712 * are inserted into the btree
1713 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001714static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001715 int mirror_num, unsigned long bio_flags,
1716 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001717{
1718 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001719 int ret;
1720
1721 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1722 if (ret)
1723 bio_endio(bio, ret);
1724 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001725}
1726
Chris Masond352ac62008-09-29 15:18:18 -04001727/*
Chris Masoncad321a2008-12-17 14:51:42 -05001728 * extent_io.c submission hook. This does the right thing for csum calculation
1729 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001730 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001731static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001732 int mirror_num, unsigned long bio_flags,
1733 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001734{
1735 struct btrfs_root *root = BTRFS_I(inode)->root;
1736 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001737 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001738 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001739 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001740
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001741 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001742
Liu Bo83eea1f2012-07-10 05:28:39 -06001743 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001744 metadata = 2;
1745
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001746 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001747 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1748 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001749 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001750
Chris Masond20f7042008-12-08 16:58:54 -05001751 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001752 ret = btrfs_submit_compressed_read(inode, bio,
1753 mirror_num,
1754 bio_flags);
1755 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001756 } else if (!skip_sum) {
1757 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1758 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001759 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001760 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001761 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001762 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001763 /* csum items have already been cloned */
1764 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1765 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001766 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001767 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001768 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001769 bio_flags, bio_offset,
1770 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001771 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001772 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001773 } else if (!skip_sum) {
1774 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1775 if (ret)
1776 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001777 }
1778
Chris Mason0b86a832008-03-24 15:01:56 -04001779mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001780 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1781
1782out:
1783 if (ret < 0)
1784 bio_endio(bio, ret);
1785 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001786}
Chris Mason6885f302008-02-20 16:11:05 -05001787
Chris Masond352ac62008-09-29 15:18:18 -04001788/*
1789 * given a list of ordered sums record them in the inode. This happens
1790 * at IO completion time based on sums calculated at bio submission time.
1791 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001792static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001793 struct inode *inode, u64 file_offset,
1794 struct list_head *list)
1795{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001796 struct btrfs_ordered_sum *sum;
1797
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001798 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001799 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001800 btrfs_csum_file_blocks(trans,
1801 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001802 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001803 }
1804 return 0;
1805}
1806
Josef Bacik2ac55d42010-02-03 19:33:23 +00001807int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1808 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001809{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001810 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001811 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001812 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001813}
1814
Chris Masond352ac62008-09-29 15:18:18 -04001815/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001816struct btrfs_writepage_fixup {
1817 struct page *page;
1818 struct btrfs_work work;
1819};
1820
Christoph Hellwigb2950862008-12-02 09:54:17 -05001821static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001822{
1823 struct btrfs_writepage_fixup *fixup;
1824 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001825 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001826 struct page *page;
1827 struct inode *inode;
1828 u64 page_start;
1829 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001830 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001831
1832 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1833 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001834again:
Chris Mason247e7432008-07-17 12:53:51 -04001835 lock_page(page);
1836 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1837 ClearPageChecked(page);
1838 goto out_page;
1839 }
1840
1841 inode = page->mapping->host;
1842 page_start = page_offset(page);
1843 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1844
Josef Bacik2ac55d42010-02-03 19:33:23 +00001845 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001846 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001847
1848 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001849 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001850 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001851
1852 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1853 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001854 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1855 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001856 unlock_page(page);
1857 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001858 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001859 goto again;
1860 }
Chris Mason247e7432008-07-17 12:53:51 -04001861
Jeff Mahoney87826df2012-02-15 16:23:57 +01001862 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1863 if (ret) {
1864 mapping_set_error(page->mapping, ret);
1865 end_extent_writepage(page, ret, page_start, page_end);
1866 ClearPageChecked(page);
1867 goto out;
1868 }
1869
Josef Bacik2ac55d42010-02-03 19:33:23 +00001870 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001871 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001872 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001873out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001874 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1875 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001876out_page:
1877 unlock_page(page);
1878 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001879 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001880}
1881
1882/*
1883 * There are a few paths in the higher layers of the kernel that directly
1884 * set the page dirty bit without asking the filesystem if it is a
1885 * good idea. This causes problems because we want to make sure COW
1886 * properly happens and the data=ordered rules are followed.
1887 *
Chris Masonc8b97812008-10-29 14:49:59 -04001888 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001889 * hasn't been properly setup for IO. We kick off an async process
1890 * to fix it up. The async helper will wait for ordered extents, set
1891 * the delalloc bit and make it safe to write the page.
1892 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001893static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001894{
1895 struct inode *inode = page->mapping->host;
1896 struct btrfs_writepage_fixup *fixup;
1897 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001898
Chris Mason8b62b722009-09-02 16:53:46 -04001899 /* this page is properly in the ordered list */
1900 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001901 return 0;
1902
1903 if (PageChecked(page))
1904 return -EAGAIN;
1905
1906 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1907 if (!fixup)
1908 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001909
Chris Mason247e7432008-07-17 12:53:51 -04001910 SetPageChecked(page);
1911 page_cache_get(page);
1912 fixup->work.func = btrfs_writepage_fixup_worker;
1913 fixup->page = page;
1914 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001915 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001916}
1917
Yan Zhengd899e052008-10-30 14:25:28 -04001918static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1919 struct inode *inode, u64 file_pos,
1920 u64 disk_bytenr, u64 disk_num_bytes,
1921 u64 num_bytes, u64 ram_bytes,
1922 u8 compression, u8 encryption,
1923 u16 other_encoding, int extent_type)
1924{
1925 struct btrfs_root *root = BTRFS_I(inode)->root;
1926 struct btrfs_file_extent_item *fi;
1927 struct btrfs_path *path;
1928 struct extent_buffer *leaf;
1929 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001930 int ret;
1931
1932 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001933 if (!path)
1934 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001935
Chris Masonb9473432009-03-13 11:00:37 -04001936 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001937
1938 /*
1939 * we may be replacing one extent in the tree with another.
1940 * The new extent is pinned in the extent map, and we don't want
1941 * to drop it from the cache until it is completely in the btree.
1942 *
1943 * So, tell btrfs_drop_extents to leave this extent in the cache.
1944 * the caller is expected to unpin it and allow it to be merged
1945 * with the others.
1946 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001947 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001948 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001949 if (ret)
1950 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001951
Li Zefan33345d012011-04-20 10:31:50 +08001952 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001953 ins.offset = file_pos;
1954 ins.type = BTRFS_EXTENT_DATA_KEY;
1955 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001956 if (ret)
1957 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001958 leaf = path->nodes[0];
1959 fi = btrfs_item_ptr(leaf, path->slots[0],
1960 struct btrfs_file_extent_item);
1961 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1962 btrfs_set_file_extent_type(leaf, fi, extent_type);
1963 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1964 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1965 btrfs_set_file_extent_offset(leaf, fi, 0);
1966 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1967 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1968 btrfs_set_file_extent_compression(leaf, fi, compression);
1969 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1970 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001971
Yan Zhengd899e052008-10-30 14:25:28 -04001972 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001973 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001974
1975 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001976
1977 ins.objectid = disk_bytenr;
1978 ins.offset = disk_num_bytes;
1979 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001980 ret = btrfs_alloc_reserved_file_extent(trans, root,
1981 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001982 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001983out:
Yan Zhengd899e052008-10-30 14:25:28 -04001984 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001985
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001986 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001987}
1988
Liu Bo38c227d2013-01-29 03:18:40 +00001989/* snapshot-aware defrag */
1990struct sa_defrag_extent_backref {
1991 struct rb_node node;
1992 struct old_sa_defrag_extent *old;
1993 u64 root_id;
1994 u64 inum;
1995 u64 file_pos;
1996 u64 extent_offset;
1997 u64 num_bytes;
1998 u64 generation;
1999};
2000
2001struct old_sa_defrag_extent {
2002 struct list_head list;
2003 struct new_sa_defrag_extent *new;
2004
2005 u64 extent_offset;
2006 u64 bytenr;
2007 u64 offset;
2008 u64 len;
2009 int count;
2010};
2011
2012struct new_sa_defrag_extent {
2013 struct rb_root root;
2014 struct list_head head;
2015 struct btrfs_path *path;
2016 struct inode *inode;
2017 u64 file_pos;
2018 u64 len;
2019 u64 bytenr;
2020 u64 disk_len;
2021 u8 compress_type;
2022};
2023
2024static int backref_comp(struct sa_defrag_extent_backref *b1,
2025 struct sa_defrag_extent_backref *b2)
2026{
2027 if (b1->root_id < b2->root_id)
2028 return -1;
2029 else if (b1->root_id > b2->root_id)
2030 return 1;
2031
2032 if (b1->inum < b2->inum)
2033 return -1;
2034 else if (b1->inum > b2->inum)
2035 return 1;
2036
2037 if (b1->file_pos < b2->file_pos)
2038 return -1;
2039 else if (b1->file_pos > b2->file_pos)
2040 return 1;
2041
2042 /*
2043 * [------------------------------] ===> (a range of space)
2044 * |<--->| |<---->| =============> (fs/file tree A)
2045 * |<---------------------------->| ===> (fs/file tree B)
2046 *
2047 * A range of space can refer to two file extents in one tree while
2048 * refer to only one file extent in another tree.
2049 *
2050 * So we may process a disk offset more than one time(two extents in A)
2051 * and locate at the same extent(one extent in B), then insert two same
2052 * backrefs(both refer to the extent in B).
2053 */
2054 return 0;
2055}
2056
2057static void backref_insert(struct rb_root *root,
2058 struct sa_defrag_extent_backref *backref)
2059{
2060 struct rb_node **p = &root->rb_node;
2061 struct rb_node *parent = NULL;
2062 struct sa_defrag_extent_backref *entry;
2063 int ret;
2064
2065 while (*p) {
2066 parent = *p;
2067 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2068
2069 ret = backref_comp(backref, entry);
2070 if (ret < 0)
2071 p = &(*p)->rb_left;
2072 else
2073 p = &(*p)->rb_right;
2074 }
2075
2076 rb_link_node(&backref->node, parent, p);
2077 rb_insert_color(&backref->node, root);
2078}
2079
2080/*
2081 * Note the backref might has changed, and in this case we just return 0.
2082 */
2083static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2084 void *ctx)
2085{
2086 struct btrfs_file_extent_item *extent;
2087 struct btrfs_fs_info *fs_info;
2088 struct old_sa_defrag_extent *old = ctx;
2089 struct new_sa_defrag_extent *new = old->new;
2090 struct btrfs_path *path = new->path;
2091 struct btrfs_key key;
2092 struct btrfs_root *root;
2093 struct sa_defrag_extent_backref *backref;
2094 struct extent_buffer *leaf;
2095 struct inode *inode = new->inode;
2096 int slot;
2097 int ret;
2098 u64 extent_offset;
2099 u64 num_bytes;
2100
2101 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2102 inum == btrfs_ino(inode))
2103 return 0;
2104
2105 key.objectid = root_id;
2106 key.type = BTRFS_ROOT_ITEM_KEY;
2107 key.offset = (u64)-1;
2108
2109 fs_info = BTRFS_I(inode)->root->fs_info;
2110 root = btrfs_read_fs_root_no_name(fs_info, &key);
2111 if (IS_ERR(root)) {
2112 if (PTR_ERR(root) == -ENOENT)
2113 return 0;
2114 WARN_ON(1);
2115 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2116 inum, offset, root_id);
2117 return PTR_ERR(root);
2118 }
2119
2120 key.objectid = inum;
2121 key.type = BTRFS_EXTENT_DATA_KEY;
2122 if (offset > (u64)-1 << 32)
2123 key.offset = 0;
2124 else
2125 key.offset = offset;
2126
2127 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2128 if (ret < 0) {
2129 WARN_ON(1);
2130 return ret;
2131 }
2132
2133 while (1) {
2134 cond_resched();
2135
2136 leaf = path->nodes[0];
2137 slot = path->slots[0];
2138
2139 if (slot >= btrfs_header_nritems(leaf)) {
2140 ret = btrfs_next_leaf(root, path);
2141 if (ret < 0) {
2142 goto out;
2143 } else if (ret > 0) {
2144 ret = 0;
2145 goto out;
2146 }
2147 continue;
2148 }
2149
2150 path->slots[0]++;
2151
2152 btrfs_item_key_to_cpu(leaf, &key, slot);
2153
2154 if (key.objectid > inum)
2155 goto out;
2156
2157 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2158 continue;
2159
2160 extent = btrfs_item_ptr(leaf, slot,
2161 struct btrfs_file_extent_item);
2162
2163 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2164 continue;
2165
2166 extent_offset = btrfs_file_extent_offset(leaf, extent);
2167 if (key.offset - extent_offset != offset)
2168 continue;
2169
2170 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2171 if (extent_offset >= old->extent_offset + old->offset +
2172 old->len || extent_offset + num_bytes <=
2173 old->extent_offset + old->offset)
2174 continue;
2175
2176 break;
2177 }
2178
2179 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2180 if (!backref) {
2181 ret = -ENOENT;
2182 goto out;
2183 }
2184
2185 backref->root_id = root_id;
2186 backref->inum = inum;
2187 backref->file_pos = offset + extent_offset;
2188 backref->num_bytes = num_bytes;
2189 backref->extent_offset = extent_offset;
2190 backref->generation = btrfs_file_extent_generation(leaf, extent);
2191 backref->old = old;
2192 backref_insert(&new->root, backref);
2193 old->count++;
2194out:
2195 btrfs_release_path(path);
2196 WARN_ON(ret);
2197 return ret;
2198}
2199
2200static noinline bool record_extent_backrefs(struct btrfs_path *path,
2201 struct new_sa_defrag_extent *new)
2202{
2203 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2204 struct old_sa_defrag_extent *old, *tmp;
2205 int ret;
2206
2207 new->path = path;
2208
2209 list_for_each_entry_safe(old, tmp, &new->head, list) {
2210 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2211 path, record_one_backref,
2212 old);
2213 BUG_ON(ret < 0 && ret != -ENOENT);
2214
2215 /* no backref to be processed for this extent */
2216 if (!old->count) {
2217 list_del(&old->list);
2218 kfree(old);
2219 }
2220 }
2221
2222 if (list_empty(&new->head))
2223 return false;
2224
2225 return true;
2226}
2227
2228static int relink_is_mergable(struct extent_buffer *leaf,
2229 struct btrfs_file_extent_item *fi,
2230 u64 disk_bytenr)
2231{
2232 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2233 return 0;
2234
2235 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2236 return 0;
2237
2238 if (btrfs_file_extent_compression(leaf, fi) ||
2239 btrfs_file_extent_encryption(leaf, fi) ||
2240 btrfs_file_extent_other_encoding(leaf, fi))
2241 return 0;
2242
2243 return 1;
2244}
2245
2246/*
2247 * Note the backref might has changed, and in this case we just return 0.
2248 */
2249static noinline int relink_extent_backref(struct btrfs_path *path,
2250 struct sa_defrag_extent_backref *prev,
2251 struct sa_defrag_extent_backref *backref)
2252{
2253 struct btrfs_file_extent_item *extent;
2254 struct btrfs_file_extent_item *item;
2255 struct btrfs_ordered_extent *ordered;
2256 struct btrfs_trans_handle *trans;
2257 struct btrfs_fs_info *fs_info;
2258 struct btrfs_root *root;
2259 struct btrfs_key key;
2260 struct extent_buffer *leaf;
2261 struct old_sa_defrag_extent *old = backref->old;
2262 struct new_sa_defrag_extent *new = old->new;
2263 struct inode *src_inode = new->inode;
2264 struct inode *inode;
2265 struct extent_state *cached = NULL;
2266 int ret = 0;
2267 u64 start;
2268 u64 len;
2269 u64 lock_start;
2270 u64 lock_end;
2271 bool merge = false;
2272 int index;
2273
2274 if (prev && prev->root_id == backref->root_id &&
2275 prev->inum == backref->inum &&
2276 prev->file_pos + prev->num_bytes == backref->file_pos)
2277 merge = true;
2278
2279 /* step 1: get root */
2280 key.objectid = backref->root_id;
2281 key.type = BTRFS_ROOT_ITEM_KEY;
2282 key.offset = (u64)-1;
2283
2284 fs_info = BTRFS_I(src_inode)->root->fs_info;
2285 index = srcu_read_lock(&fs_info->subvol_srcu);
2286
2287 root = btrfs_read_fs_root_no_name(fs_info, &key);
2288 if (IS_ERR(root)) {
2289 srcu_read_unlock(&fs_info->subvol_srcu, index);
2290 if (PTR_ERR(root) == -ENOENT)
2291 return 0;
2292 return PTR_ERR(root);
2293 }
Liu Bo38c227d2013-01-29 03:18:40 +00002294
2295 /* step 2: get inode */
2296 key.objectid = backref->inum;
2297 key.type = BTRFS_INODE_ITEM_KEY;
2298 key.offset = 0;
2299
2300 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2301 if (IS_ERR(inode)) {
2302 srcu_read_unlock(&fs_info->subvol_srcu, index);
2303 return 0;
2304 }
2305
2306 srcu_read_unlock(&fs_info->subvol_srcu, index);
2307
2308 /* step 3: relink backref */
2309 lock_start = backref->file_pos;
2310 lock_end = backref->file_pos + backref->num_bytes - 1;
2311 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2312 0, &cached);
2313
2314 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2315 if (ordered) {
2316 btrfs_put_ordered_extent(ordered);
2317 goto out_unlock;
2318 }
2319
2320 trans = btrfs_join_transaction(root);
2321 if (IS_ERR(trans)) {
2322 ret = PTR_ERR(trans);
2323 goto out_unlock;
2324 }
2325
2326 key.objectid = backref->inum;
2327 key.type = BTRFS_EXTENT_DATA_KEY;
2328 key.offset = backref->file_pos;
2329
2330 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2331 if (ret < 0) {
2332 goto out_free_path;
2333 } else if (ret > 0) {
2334 ret = 0;
2335 goto out_free_path;
2336 }
2337
2338 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2339 struct btrfs_file_extent_item);
2340
2341 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2342 backref->generation)
2343 goto out_free_path;
2344
2345 btrfs_release_path(path);
2346
2347 start = backref->file_pos;
2348 if (backref->extent_offset < old->extent_offset + old->offset)
2349 start += old->extent_offset + old->offset -
2350 backref->extent_offset;
2351
2352 len = min(backref->extent_offset + backref->num_bytes,
2353 old->extent_offset + old->offset + old->len);
2354 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2355
2356 ret = btrfs_drop_extents(trans, root, inode, start,
2357 start + len, 1);
2358 if (ret)
2359 goto out_free_path;
2360again:
2361 key.objectid = btrfs_ino(inode);
2362 key.type = BTRFS_EXTENT_DATA_KEY;
2363 key.offset = start;
2364
Liu Boa09a0a72013-03-11 09:20:58 +00002365 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002366 if (merge) {
2367 struct btrfs_file_extent_item *fi;
2368 u64 extent_len;
2369 struct btrfs_key found_key;
2370
2371 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2372 if (ret < 0)
2373 goto out_free_path;
2374
2375 path->slots[0]--;
2376 leaf = path->nodes[0];
2377 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2378
2379 fi = btrfs_item_ptr(leaf, path->slots[0],
2380 struct btrfs_file_extent_item);
2381 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2382
2383 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2384 extent_len + found_key.offset == start) {
2385 btrfs_set_file_extent_num_bytes(leaf, fi,
2386 extent_len + len);
2387 btrfs_mark_buffer_dirty(leaf);
2388 inode_add_bytes(inode, len);
2389
2390 ret = 1;
2391 goto out_free_path;
2392 } else {
2393 merge = false;
2394 btrfs_release_path(path);
2395 goto again;
2396 }
2397 }
2398
2399 ret = btrfs_insert_empty_item(trans, root, path, &key,
2400 sizeof(*extent));
2401 if (ret) {
2402 btrfs_abort_transaction(trans, root, ret);
2403 goto out_free_path;
2404 }
2405
2406 leaf = path->nodes[0];
2407 item = btrfs_item_ptr(leaf, path->slots[0],
2408 struct btrfs_file_extent_item);
2409 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2410 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2411 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2412 btrfs_set_file_extent_num_bytes(leaf, item, len);
2413 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2414 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2415 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2416 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2417 btrfs_set_file_extent_encryption(leaf, item, 0);
2418 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2419
2420 btrfs_mark_buffer_dirty(leaf);
2421 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002422 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002423
2424 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2425 new->disk_len, 0,
2426 backref->root_id, backref->inum,
2427 new->file_pos, 0); /* start - extent_offset */
2428 if (ret) {
2429 btrfs_abort_transaction(trans, root, ret);
2430 goto out_free_path;
2431 }
2432
2433 ret = 1;
2434out_free_path:
2435 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002436 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002437 btrfs_end_transaction(trans, root);
2438out_unlock:
2439 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2440 &cached, GFP_NOFS);
2441 iput(inode);
2442 return ret;
2443}
2444
2445static void relink_file_extents(struct new_sa_defrag_extent *new)
2446{
2447 struct btrfs_path *path;
2448 struct old_sa_defrag_extent *old, *tmp;
2449 struct sa_defrag_extent_backref *backref;
2450 struct sa_defrag_extent_backref *prev = NULL;
2451 struct inode *inode;
2452 struct btrfs_root *root;
2453 struct rb_node *node;
2454 int ret;
2455
2456 inode = new->inode;
2457 root = BTRFS_I(inode)->root;
2458
2459 path = btrfs_alloc_path();
2460 if (!path)
2461 return;
2462
2463 if (!record_extent_backrefs(path, new)) {
2464 btrfs_free_path(path);
2465 goto out;
2466 }
2467 btrfs_release_path(path);
2468
2469 while (1) {
2470 node = rb_first(&new->root);
2471 if (!node)
2472 break;
2473 rb_erase(node, &new->root);
2474
2475 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2476
2477 ret = relink_extent_backref(path, prev, backref);
2478 WARN_ON(ret < 0);
2479
2480 kfree(prev);
2481
2482 if (ret == 1)
2483 prev = backref;
2484 else
2485 prev = NULL;
2486 cond_resched();
2487 }
2488 kfree(prev);
2489
2490 btrfs_free_path(path);
2491
2492 list_for_each_entry_safe(old, tmp, &new->head, list) {
2493 list_del(&old->list);
2494 kfree(old);
2495 }
2496out:
2497 atomic_dec(&root->fs_info->defrag_running);
2498 wake_up(&root->fs_info->transaction_wait);
2499
2500 kfree(new);
2501}
2502
2503static struct new_sa_defrag_extent *
2504record_old_file_extents(struct inode *inode,
2505 struct btrfs_ordered_extent *ordered)
2506{
2507 struct btrfs_root *root = BTRFS_I(inode)->root;
2508 struct btrfs_path *path;
2509 struct btrfs_key key;
2510 struct old_sa_defrag_extent *old, *tmp;
2511 struct new_sa_defrag_extent *new;
2512 int ret;
2513
2514 new = kmalloc(sizeof(*new), GFP_NOFS);
2515 if (!new)
2516 return NULL;
2517
2518 new->inode = inode;
2519 new->file_pos = ordered->file_offset;
2520 new->len = ordered->len;
2521 new->bytenr = ordered->start;
2522 new->disk_len = ordered->disk_len;
2523 new->compress_type = ordered->compress_type;
2524 new->root = RB_ROOT;
2525 INIT_LIST_HEAD(&new->head);
2526
2527 path = btrfs_alloc_path();
2528 if (!path)
2529 goto out_kfree;
2530
2531 key.objectid = btrfs_ino(inode);
2532 key.type = BTRFS_EXTENT_DATA_KEY;
2533 key.offset = new->file_pos;
2534
2535 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2536 if (ret < 0)
2537 goto out_free_path;
2538 if (ret > 0 && path->slots[0] > 0)
2539 path->slots[0]--;
2540
2541 /* find out all the old extents for the file range */
2542 while (1) {
2543 struct btrfs_file_extent_item *extent;
2544 struct extent_buffer *l;
2545 int slot;
2546 u64 num_bytes;
2547 u64 offset;
2548 u64 end;
2549 u64 disk_bytenr;
2550 u64 extent_offset;
2551
2552 l = path->nodes[0];
2553 slot = path->slots[0];
2554
2555 if (slot >= btrfs_header_nritems(l)) {
2556 ret = btrfs_next_leaf(root, path);
2557 if (ret < 0)
2558 goto out_free_list;
2559 else if (ret > 0)
2560 break;
2561 continue;
2562 }
2563
2564 btrfs_item_key_to_cpu(l, &key, slot);
2565
2566 if (key.objectid != btrfs_ino(inode))
2567 break;
2568 if (key.type != BTRFS_EXTENT_DATA_KEY)
2569 break;
2570 if (key.offset >= new->file_pos + new->len)
2571 break;
2572
2573 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2574
2575 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2576 if (key.offset + num_bytes < new->file_pos)
2577 goto next;
2578
2579 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2580 if (!disk_bytenr)
2581 goto next;
2582
2583 extent_offset = btrfs_file_extent_offset(l, extent);
2584
2585 old = kmalloc(sizeof(*old), GFP_NOFS);
2586 if (!old)
2587 goto out_free_list;
2588
2589 offset = max(new->file_pos, key.offset);
2590 end = min(new->file_pos + new->len, key.offset + num_bytes);
2591
2592 old->bytenr = disk_bytenr;
2593 old->extent_offset = extent_offset;
2594 old->offset = offset - key.offset;
2595 old->len = end - offset;
2596 old->new = new;
2597 old->count = 0;
2598 list_add_tail(&old->list, &new->head);
2599next:
2600 path->slots[0]++;
2601 cond_resched();
2602 }
2603
2604 btrfs_free_path(path);
2605 atomic_inc(&root->fs_info->defrag_running);
2606
2607 return new;
2608
2609out_free_list:
2610 list_for_each_entry_safe(old, tmp, &new->head, list) {
2611 list_del(&old->list);
2612 kfree(old);
2613 }
2614out_free_path:
2615 btrfs_free_path(path);
2616out_kfree:
2617 kfree(new);
2618 return NULL;
2619}
2620
Chris Mason5d13a982009-03-13 11:41:46 -04002621/*
2622 * helper function for btrfs_finish_ordered_io, this
2623 * just reads in some of the csum leaves to prime them into ram
2624 * before we start the transaction. It limits the amount of btree
2625 * reads required while inside the transaction.
2626 */
Chris Masond352ac62008-09-29 15:18:18 -04002627/* as ordered data IO finishes, this gets called so we can finish
2628 * an ordered extent if the range of bytes in the file it covers are
2629 * fully written.
2630 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002631static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002632{
Josef Bacik5fd02042012-05-02 14:00:54 -04002633 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002634 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002635 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002636 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002637 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002638 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002639 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002640 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002641 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642
Liu Bo83eea1f2012-07-10 05:28:39 -06002643 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002644
Josef Bacik5fd02042012-05-02 14:00:54 -04002645 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2646 ret = -EIO;
2647 goto out;
2648 }
2649
Yan, Zhengc2167752009-11-12 09:34:21 +00002650 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002652 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2653 if (nolock)
2654 trans = btrfs_join_transaction_nolock(root);
2655 else
2656 trans = btrfs_join_transaction(root);
2657 if (IS_ERR(trans)) {
2658 ret = PTR_ERR(trans);
2659 trans = NULL;
2660 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002661 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002662 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2663 ret = btrfs_update_inode_fallback(trans, root, inode);
2664 if (ret) /* -ENOMEM or corruption */
2665 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002666 goto out;
2667 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002668
Josef Bacik2ac55d42010-02-03 19:33:23 +00002669 lock_extent_bits(io_tree, ordered_extent->file_offset,
2670 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002671 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002672
Liu Bo38c227d2013-01-29 03:18:40 +00002673 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2674 ordered_extent->file_offset + ordered_extent->len - 1,
2675 EXTENT_DEFRAG, 1, cached_state);
2676 if (ret) {
2677 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2678 if (last_snapshot >= BTRFS_I(inode)->generation)
2679 /* the inode is shared */
2680 new = record_old_file_extents(inode, ordered_extent);
2681
2682 clear_extent_bit(io_tree, ordered_extent->file_offset,
2683 ordered_extent->file_offset + ordered_extent->len - 1,
2684 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2685 }
2686
Josef Bacik0cb59c92010-07-02 12:14:14 -04002687 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002688 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002689 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002690 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002691 if (IS_ERR(trans)) {
2692 ret = PTR_ERR(trans);
2693 trans = NULL;
2694 goto out_unlock;
2695 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002696 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002697
Chris Masonc8b97812008-10-29 14:49:59 -04002698 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002699 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002700 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002701 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002702 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002703 ordered_extent->file_offset,
2704 ordered_extent->file_offset +
2705 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002706 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002707 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002708 ret = insert_reserved_file_extent(trans, inode,
2709 ordered_extent->file_offset,
2710 ordered_extent->start,
2711 ordered_extent->disk_len,
2712 ordered_extent->len,
2713 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002714 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002715 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002716 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002717 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2718 ordered_extent->file_offset, ordered_extent->len,
2719 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002720 if (ret < 0) {
2721 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002722 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002723 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002724
Chris Masone6dcd2d2008-07-17 12:53:50 -04002725 add_pending_csums(trans, inode, ordered_extent->file_offset,
2726 &ordered_extent->list);
2727
Josef Bacik6c760c02012-11-09 10:53:21 -05002728 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2729 ret = btrfs_update_inode_fallback(trans, root, inode);
2730 if (ret) { /* -ENOMEM or corruption */
2731 btrfs_abort_transaction(trans, root, ret);
2732 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002733 }
2734 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002735out_unlock:
2736 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2737 ordered_extent->file_offset +
2738 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002739out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002740 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002741 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002742 if (trans)
2743 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002744
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002745 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002746 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2747 ordered_extent->file_offset +
2748 ordered_extent->len - 1, NULL, GFP_NOFS);
2749
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002750 /*
2751 * If the ordered extent had an IOERR or something else went
2752 * wrong we need to return the space for this ordered extent
2753 * back to the allocator.
2754 */
2755 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2756 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2757 btrfs_free_reserved_extent(root, ordered_extent->start,
2758 ordered_extent->disk_len);
2759 }
2760
2761
Josef Bacik5fd02042012-05-02 14:00:54 -04002762 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002763 * This needs to be done to make sure anybody waiting knows we are done
2764 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002765 */
2766 btrfs_remove_ordered_extent(inode, ordered_extent);
2767
Liu Bo38c227d2013-01-29 03:18:40 +00002768 /* for snapshot-aware defrag */
2769 if (new)
2770 relink_file_extents(new);
2771
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772 /* once for us */
2773 btrfs_put_ordered_extent(ordered_extent);
2774 /* once for the tree */
2775 btrfs_put_ordered_extent(ordered_extent);
2776
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 return ret;
2778}
2779
2780static void finish_ordered_fn(struct btrfs_work *work)
2781{
2782 struct btrfs_ordered_extent *ordered_extent;
2783 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2784 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002785}
2786
Christoph Hellwigb2950862008-12-02 09:54:17 -05002787static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002788 struct extent_state *state, int uptodate)
2789{
Josef Bacik5fd02042012-05-02 14:00:54 -04002790 struct inode *inode = page->mapping->host;
2791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct btrfs_ordered_extent *ordered_extent = NULL;
2793 struct btrfs_workers *workers;
2794
liubo1abe9b82011-03-24 11:18:59 +00002795 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2796
Chris Mason8b62b722009-09-02 16:53:46 -04002797 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002798 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2799 end - start + 1, uptodate))
2800 return 0;
2801
2802 ordered_extent->work.func = finish_ordered_fn;
2803 ordered_extent->work.flags = 0;
2804
Liu Bo83eea1f2012-07-10 05:28:39 -06002805 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002806 workers = &root->fs_info->endio_freespace_worker;
2807 else
2808 workers = &root->fs_info->endio_write_workers;
2809 btrfs_queue_worker(workers, &ordered_extent->work);
2810
2811 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002812}
2813
Chris Masond352ac62008-09-29 15:18:18 -04002814/*
Chris Masond352ac62008-09-29 15:18:18 -04002815 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002816 * if there's a match, we allow the bio to finish. If not, the code in
2817 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002818 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002819static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002820 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002821{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002822 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002823 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002824 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002825 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002826 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002827 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002828 struct btrfs_root *root = BTRFS_I(inode)->root;
2829 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002830 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2831 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002832
Chris Masond20f7042008-12-08 16:58:54 -05002833 if (PageChecked(page)) {
2834 ClearPageChecked(page);
2835 goto good;
2836 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002837
2838 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002839 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002840
Yan Zheng17d217f2008-12-12 10:03:38 -05002841 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002842 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002843 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2844 GFP_NOFS);
2845 return 0;
2846 }
2847
Yanc2e639f2008-02-04 08:57:25 -05002848 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002849 private = state->private;
2850 ret = 0;
2851 } else {
2852 ret = get_state_private(io_tree, start, &private);
2853 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002854 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002855 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002856 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002857
Liu Bob0496682013-03-14 14:57:45 +00002858 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002859 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002860 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002861 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002862
Cong Wang7ac687d2011-11-25 23:14:28 +08002863 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002864good:
Chris Mason07157aa2007-08-30 08:50:51 -04002865 return 0;
2866
2867zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002868 if (__ratelimit(&_rs))
2869 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2870 (unsigned long long)btrfs_ino(page->mapping->host),
2871 (unsigned long long)start, csum,
2872 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002873 memset(kaddr + offset, 1, end - start + 1);
2874 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002875 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002876 if (private == 0)
2877 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002878 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002879}
Chris Masonb888db22007-08-27 16:49:44 -04002880
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002881struct delayed_iput {
2882 struct list_head list;
2883 struct inode *inode;
2884};
2885
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002886/* JDM: If this is fs-wide, why can't we add a pointer to
2887 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888void btrfs_add_delayed_iput(struct inode *inode)
2889{
2890 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2891 struct delayed_iput *delayed;
2892
2893 if (atomic_add_unless(&inode->i_count, -1, 1))
2894 return;
2895
2896 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2897 delayed->inode = inode;
2898
2899 spin_lock(&fs_info->delayed_iput_lock);
2900 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2901 spin_unlock(&fs_info->delayed_iput_lock);
2902}
2903
2904void btrfs_run_delayed_iputs(struct btrfs_root *root)
2905{
2906 LIST_HEAD(list);
2907 struct btrfs_fs_info *fs_info = root->fs_info;
2908 struct delayed_iput *delayed;
2909 int empty;
2910
2911 spin_lock(&fs_info->delayed_iput_lock);
2912 empty = list_empty(&fs_info->delayed_iputs);
2913 spin_unlock(&fs_info->delayed_iput_lock);
2914 if (empty)
2915 return;
2916
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002917 spin_lock(&fs_info->delayed_iput_lock);
2918 list_splice_init(&fs_info->delayed_iputs, &list);
2919 spin_unlock(&fs_info->delayed_iput_lock);
2920
2921 while (!list_empty(&list)) {
2922 delayed = list_entry(list.next, struct delayed_iput, list);
2923 list_del(&delayed->list);
2924 iput(delayed->inode);
2925 kfree(delayed);
2926 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002927}
2928
Yan, Zhengd68fc572010-05-16 10:49:58 -04002929/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002930 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931 * files in the subvolume, it removes orphan item and frees block_rsv
2932 * structure.
2933 */
2934void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2935 struct btrfs_root *root)
2936{
Josef Bacik90290e12011-12-02 15:44:12 -05002937 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002938 int ret;
2939
Josef Bacik8a35d952012-05-23 14:26:42 -04002940 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2942 return;
2943
Josef Bacik90290e12011-12-02 15:44:12 -05002944 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002945 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002946 spin_unlock(&root->orphan_lock);
2947 return;
2948 }
2949
2950 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2951 spin_unlock(&root->orphan_lock);
2952 return;
2953 }
2954
2955 block_rsv = root->orphan_block_rsv;
2956 root->orphan_block_rsv = NULL;
2957 spin_unlock(&root->orphan_lock);
2958
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 if (root->orphan_item_inserted &&
2960 btrfs_root_refs(&root->root_item) > 0) {
2961 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2962 root->root_key.objectid);
2963 BUG_ON(ret);
2964 root->orphan_item_inserted = 0;
2965 }
2966
Josef Bacik90290e12011-12-02 15:44:12 -05002967 if (block_rsv) {
2968 WARN_ON(block_rsv->size > 0);
2969 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970 }
2971}
2972
2973/*
Josef Bacik7b128762008-07-24 12:17:14 -04002974 * This creates an orphan entry for the given inode in case something goes
2975 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002976 *
2977 * NOTE: caller of this function should reserve 5 units of metadata for
2978 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002979 */
2980int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2981{
2982 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002983 struct btrfs_block_rsv *block_rsv = NULL;
2984 int reserve = 0;
2985 int insert = 0;
2986 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002987
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002989 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002990 if (!block_rsv)
2991 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002992 }
2993
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 spin_lock(&root->orphan_lock);
2995 if (!root->orphan_block_rsv) {
2996 root->orphan_block_rsv = block_rsv;
2997 } else if (block_rsv) {
2998 btrfs_free_block_rsv(root, block_rsv);
2999 block_rsv = NULL;
3000 }
Josef Bacik7b128762008-07-24 12:17:14 -04003001
Josef Bacik8a35d952012-05-23 14:26:42 -04003002 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3003 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003004#if 0
3005 /*
3006 * For proper ENOSPC handling, we should do orphan
3007 * cleanup when mounting. But this introduces backward
3008 * compatibility issue.
3009 */
3010 if (!xchg(&root->orphan_item_inserted, 1))
3011 insert = 2;
3012 else
3013 insert = 1;
3014#endif
3015 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003016 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 }
Josef Bacik7b128762008-07-24 12:17:14 -04003018
Josef Bacik72ac3c02012-05-23 14:13:11 -04003019 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3020 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003022 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003023
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 /* grab metadata reservation from transaction handle */
3025 if (reserve) {
3026 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 }
3029
3030 /* insert an orphan item to track this unlinked/truncated file */
3031 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003032 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003033 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003034 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3035 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003036 btrfs_abort_transaction(trans, root, ret);
3037 return ret;
3038 }
3039 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 }
3041
3042 /* insert an orphan item to track subvolume contains orphan files */
3043 if (insert >= 2) {
3044 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3045 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003046 if (ret && ret != -EEXIST) {
3047 btrfs_abort_transaction(trans, root, ret);
3048 return ret;
3049 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 }
3051 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003052}
3053
3054/*
3055 * We have done the truncate/delete so we can go ahead and remove the orphan
3056 * item for this particular inode.
3057 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003058static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3059 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003060{
3061 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 int delete_item = 0;
3063 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003064 int ret = 0;
3065
Yan, Zhengd68fc572010-05-16 10:49:58 -04003066 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003067 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3068 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003069 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003070
Josef Bacik72ac3c02012-05-23 14:13:11 -04003071 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3072 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003073 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 spin_unlock(&root->orphan_lock);
3075
3076 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003077 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003078 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003079 }
3080
Josef Bacik8a35d952012-05-23 14:26:42 -04003081 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003082 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003083 atomic_dec(&root->orphan_inodes);
3084 }
Josef Bacik7b128762008-07-24 12:17:14 -04003085
Yan, Zhengd68fc572010-05-16 10:49:58 -04003086 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003087}
3088
3089/*
3090 * this cleans up any orphans that may be left on the list from the last use
3091 * of this root.
3092 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003093int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003094{
3095 struct btrfs_path *path;
3096 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003097 struct btrfs_key key, found_key;
3098 struct btrfs_trans_handle *trans;
3099 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003100 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003101 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3102
Yan, Zhengd68fc572010-05-16 10:49:58 -04003103 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003104 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003105
3106 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003107 if (!path) {
3108 ret = -ENOMEM;
3109 goto out;
3110 }
Josef Bacik7b128762008-07-24 12:17:14 -04003111 path->reada = -1;
3112
3113 key.objectid = BTRFS_ORPHAN_OBJECTID;
3114 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3115 key.offset = (u64)-1;
3116
Josef Bacik7b128762008-07-24 12:17:14 -04003117 while (1) {
3118 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003119 if (ret < 0)
3120 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003121
3122 /*
3123 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003124 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003125 * find the key and see if we have stuff that matches
3126 */
3127 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003128 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003129 if (path->slots[0] == 0)
3130 break;
3131 path->slots[0]--;
3132 }
3133
3134 /* pull out the item */
3135 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003136 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3137
3138 /* make sure the item matches what we want */
3139 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3140 break;
3141 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3142 break;
3143
3144 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003145 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003146
3147 /*
3148 * this is where we are basically btrfs_lookup, without the
3149 * crossing root thing. we store the inode number in the
3150 * offset of the orphan item.
3151 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003152
3153 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003154 btrfs_err(root->fs_info,
3155 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003156 ret = -EINVAL;
3157 goto out;
3158 }
3159
3160 last_objectid = found_key.offset;
3161
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003162 found_key.objectid = found_key.offset;
3163 found_key.type = BTRFS_INODE_ITEM_KEY;
3164 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003165 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003166 ret = PTR_RET(inode);
3167 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003168 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003169
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003170 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3171 struct btrfs_root *dead_root;
3172 struct btrfs_fs_info *fs_info = root->fs_info;
3173 int is_dead_root = 0;
3174
3175 /*
3176 * this is an orphan in the tree root. Currently these
3177 * could come from 2 sources:
3178 * a) a snapshot deletion in progress
3179 * b) a free space cache inode
3180 * We need to distinguish those two, as the snapshot
3181 * orphan must not get deleted.
3182 * find_dead_roots already ran before us, so if this
3183 * is a snapshot deletion, we should find the root
3184 * in the dead_roots list
3185 */
3186 spin_lock(&fs_info->trans_lock);
3187 list_for_each_entry(dead_root, &fs_info->dead_roots,
3188 root_list) {
3189 if (dead_root->root_key.objectid ==
3190 found_key.objectid) {
3191 is_dead_root = 1;
3192 break;
3193 }
3194 }
3195 spin_unlock(&fs_info->trans_lock);
3196 if (is_dead_root) {
3197 /* prevent this orphan from being found again */
3198 key.offset = found_key.objectid - 1;
3199 continue;
3200 }
3201 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003202 /*
3203 * Inode is already gone but the orphan item is still there,
3204 * kill the orphan item.
3205 */
3206 if (ret == -ESTALE) {
3207 trans = btrfs_start_transaction(root, 1);
3208 if (IS_ERR(trans)) {
3209 ret = PTR_ERR(trans);
3210 goto out;
3211 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003212 btrfs_debug(root->fs_info, "auto deleting %Lu",
3213 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003214 ret = btrfs_del_orphan_item(trans, root,
3215 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003216 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003217 btrfs_end_transaction(trans, root);
3218 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003219 }
Josef Bacik7b128762008-07-24 12:17:14 -04003220
Josef Bacik7b128762008-07-24 12:17:14 -04003221 /*
3222 * add this inode to the orphan list so btrfs_orphan_del does
3223 * the proper thing when we hit it
3224 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003225 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3226 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003227 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003228
Josef Bacik7b128762008-07-24 12:17:14 -04003229 /* if we have links, this was a truncate, lets do that */
3230 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003231 if (!S_ISREG(inode->i_mode)) {
3232 WARN_ON(1);
3233 iput(inode);
3234 continue;
3235 }
Josef Bacik7b128762008-07-24 12:17:14 -04003236 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003237
3238 /* 1 for the orphan item deletion. */
3239 trans = btrfs_start_transaction(root, 1);
3240 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003241 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003242 ret = PTR_ERR(trans);
3243 goto out;
3244 }
3245 ret = btrfs_orphan_add(trans, inode);
3246 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003247 if (ret) {
3248 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003249 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003250 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003251
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003252 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003253 if (ret)
3254 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003255 } else {
3256 nr_unlink++;
3257 }
3258
3259 /* this will do delete_inode and everything for us */
3260 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003261 if (ret)
3262 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003263 }
Miao Xie3254c872011-11-10 20:45:05 -05003264 /* release the path since we're done with it */
3265 btrfs_release_path(path);
3266
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3268
3269 if (root->orphan_block_rsv)
3270 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3271 (u64)-1);
3272
3273 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003274 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003275 if (!IS_ERR(trans))
3276 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 }
Josef Bacik7b128762008-07-24 12:17:14 -04003278
3279 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003280 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003281 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003282 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003283
3284out:
3285 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003286 btrfs_crit(root->fs_info,
3287 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003288 btrfs_free_path(path);
3289 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003290}
3291
Chris Masond352ac62008-09-29 15:18:18 -04003292/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003293 * very simple check to peek ahead in the leaf looking for xattrs. If we
3294 * don't find any xattrs, we know there can't be any acls.
3295 *
3296 * slot is the slot the inode is in, objectid is the objectid of the inode
3297 */
3298static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3299 int slot, u64 objectid)
3300{
3301 u32 nritems = btrfs_header_nritems(leaf);
3302 struct btrfs_key found_key;
3303 int scanned = 0;
3304
3305 slot++;
3306 while (slot < nritems) {
3307 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3308
3309 /* we found a different objectid, there must not be acls */
3310 if (found_key.objectid != objectid)
3311 return 0;
3312
3313 /* we found an xattr, assume we've got an acl */
3314 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3315 return 1;
3316
3317 /*
3318 * we found a key greater than an xattr key, there can't
3319 * be any acls later on
3320 */
3321 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3322 return 0;
3323
3324 slot++;
3325 scanned++;
3326
3327 /*
3328 * it goes inode, inode backrefs, xattrs, extents,
3329 * so if there are a ton of hard links to an inode there can
3330 * be a lot of backrefs. Don't waste time searching too hard,
3331 * this is just an optimization
3332 */
3333 if (scanned >= 8)
3334 break;
3335 }
3336 /* we hit the end of the leaf before we found an xattr or
3337 * something larger than an xattr. We have to assume the inode
3338 * has acls
3339 */
3340 return 1;
3341}
3342
3343/*
Chris Masond352ac62008-09-29 15:18:18 -04003344 * read an inode from the btree into the in-memory inode
3345 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003346static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003347{
3348 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003349 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003350 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003351 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003352 struct btrfs_root *root = BTRFS_I(inode)->root;
3353 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003354 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003355 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003356 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003357 bool filled = false;
3358
3359 ret = btrfs_fill_inode(inode, &rdev);
3360 if (!ret)
3361 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003362
3363 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003364 if (!path)
3365 goto make_bad;
3366
Josef Bacikd90c7322011-05-17 09:50:54 -04003367 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003368 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003369
Chris Mason39279cc2007-06-12 06:35:45 -04003370 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003372 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003373
Chris Mason5f39d392007-10-15 16:14:19 -04003374 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003375
3376 if (filled)
3377 goto cache_acl;
3378
Chris Mason5f39d392007-10-15 16:14:19 -04003379 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3380 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003381 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003382 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003383 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3384 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003385 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003386
3387 tspec = btrfs_inode_atime(inode_item);
3388 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3389 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3390
3391 tspec = btrfs_inode_mtime(inode_item);
3392 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3393 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3394
3395 tspec = btrfs_inode_ctime(inode_item);
3396 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3397 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3398
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003399 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003400 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003401 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3402
3403 /*
3404 * If we were modified in the current generation and evicted from memory
3405 * and then re-read we need to do a full sync since we don't have any
3406 * idea about which extents were modified before we were evicted from
3407 * cache.
3408 */
3409 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3410 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3411 &BTRFS_I(inode)->runtime_flags);
3412
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003413 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003414 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003415 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003416 rdev = btrfs_inode_rdev(leaf, inode_item);
3417
Josef Bacikaec74772008-07-24 12:12:38 -04003418 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003419 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003420cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003421 /*
3422 * try to precache a NULL acl entry for files that don't have
3423 * any xattrs or acls
3424 */
Li Zefan33345d012011-04-20 10:31:50 +08003425 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3426 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003427 if (!maybe_acls)
3428 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003429
Chris Mason39279cc2007-06-12 06:35:45 -04003430 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003431
Chris Mason39279cc2007-06-12 06:35:45 -04003432 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003433 case S_IFREG:
3434 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003435 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003436 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003437 inode->i_fop = &btrfs_file_operations;
3438 inode->i_op = &btrfs_file_inode_operations;
3439 break;
3440 case S_IFDIR:
3441 inode->i_fop = &btrfs_dir_file_operations;
3442 if (root == root->fs_info->tree_root)
3443 inode->i_op = &btrfs_dir_ro_inode_operations;
3444 else
3445 inode->i_op = &btrfs_dir_inode_operations;
3446 break;
3447 case S_IFLNK:
3448 inode->i_op = &btrfs_symlink_inode_operations;
3449 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003450 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003451 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003452 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003453 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003454 init_special_inode(inode, inode->i_mode, rdev);
3455 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003456 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003457
3458 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003459 return;
3460
3461make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003462 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003463 make_bad_inode(inode);
3464}
3465
Chris Masond352ac62008-09-29 15:18:18 -04003466/*
3467 * given a leaf and an inode, copy the inode fields into the leaf
3468 */
Chris Masone02119d2008-09-05 16:13:11 -04003469static void fill_inode_item(struct btrfs_trans_handle *trans,
3470 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003471 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003472 struct inode *inode)
3473{
Liu Bo51fab692012-12-27 09:01:21 +00003474 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003475
Liu Bo51fab692012-12-27 09:01:21 +00003476 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003477
Liu Bo51fab692012-12-27 09:01:21 +00003478 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3479 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3480 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3481 &token);
3482 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3483 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003484
Liu Bo51fab692012-12-27 09:01:21 +00003485 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3486 inode->i_atime.tv_sec, &token);
3487 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3488 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003489
Liu Bo51fab692012-12-27 09:01:21 +00003490 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3491 inode->i_mtime.tv_sec, &token);
3492 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3493 inode->i_mtime.tv_nsec, &token);
3494
3495 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3496 inode->i_ctime.tv_sec, &token);
3497 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3498 inode->i_ctime.tv_nsec, &token);
3499
3500 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3501 &token);
3502 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3503 &token);
3504 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3505 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3506 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3507 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3508 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003509}
3510
Chris Masond352ac62008-09-29 15:18:18 -04003511/*
3512 * copy everything in the in-memory inode into the btree.
3513 */
Chris Mason21151332011-11-10 20:39:08 -05003514static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003515 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003516{
3517 struct btrfs_inode_item *inode_item;
3518 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003519 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003520 int ret;
3521
3522 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003523 if (!path)
3524 return -ENOMEM;
3525
Chris Masonb9473432009-03-13 11:00:37 -04003526 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003527 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3528 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003529 if (ret) {
3530 if (ret > 0)
3531 ret = -ENOENT;
3532 goto failed;
3533 }
3534
Chris Masonb4ce94d2009-02-04 09:25:08 -05003535 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003536 leaf = path->nodes[0];
3537 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003538 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003539
Chris Masone02119d2008-09-05 16:13:11 -04003540 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003541 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003542 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003543 ret = 0;
3544failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003545 btrfs_free_path(path);
3546 return ret;
3547}
3548
Chris Masond352ac62008-09-29 15:18:18 -04003549/*
Chris Mason21151332011-11-10 20:39:08 -05003550 * copy everything in the in-memory inode into the btree.
3551 */
3552noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3553 struct btrfs_root *root, struct inode *inode)
3554{
3555 int ret;
3556
3557 /*
3558 * If the inode is a free space inode, we can deadlock during commit
3559 * if we put it into the delayed code.
3560 *
3561 * The data relocation inode should also be directly updated
3562 * without delay
3563 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003564 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003565 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003566 btrfs_update_root_times(trans, root);
3567
Chris Mason21151332011-11-10 20:39:08 -05003568 ret = btrfs_delayed_update_inode(trans, root, inode);
3569 if (!ret)
3570 btrfs_set_inode_last_trans(trans, inode);
3571 return ret;
3572 }
3573
3574 return btrfs_update_inode_item(trans, root, inode);
3575}
3576
Josef Bacikbe6aef62012-10-22 15:43:12 -04003577noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root,
3579 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003580{
3581 int ret;
3582
3583 ret = btrfs_update_inode(trans, root, inode);
3584 if (ret == -ENOSPC)
3585 return btrfs_update_inode_item(trans, root, inode);
3586 return ret;
3587}
3588
3589/*
Chris Masond352ac62008-09-29 15:18:18 -04003590 * unlink helper that gets used here in inode.c and in the tree logging
3591 * recovery code. It remove a link in a directory with a given name, and
3592 * also drops the back refs in the inode to the directory
3593 */
Al Viro92986792011-03-04 17:14:37 +00003594static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3595 struct btrfs_root *root,
3596 struct inode *dir, struct inode *inode,
3597 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003598{
3599 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003600 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003601 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003602 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003603 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003604 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003605 u64 ino = btrfs_ino(inode);
3606 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003607
3608 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003609 if (!path) {
3610 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003611 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003612 }
3613
Chris Masonb9473432009-03-13 11:00:37 -04003614 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003615 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003616 name, name_len, -1);
3617 if (IS_ERR(di)) {
3618 ret = PTR_ERR(di);
3619 goto err;
3620 }
3621 if (!di) {
3622 ret = -ENOENT;
3623 goto err;
3624 }
Chris Mason5f39d392007-10-15 16:14:19 -04003625 leaf = path->nodes[0];
3626 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003627 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003628 if (ret)
3629 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003630 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003631
Li Zefan33345d012011-04-20 10:31:50 +08003632 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3633 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003634 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003635 btrfs_info(root->fs_info,
3636 "failed to delete reference to %.*s, inode %llu parent %llu",
3637 name_len, name,
3638 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003639 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003640 goto err;
3641 }
3642
Miao Xie16cdcec2011-04-22 18:12:22 +08003643 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003644 if (ret) {
3645 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003646 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003647 }
Chris Mason39279cc2007-06-12 06:35:45 -04003648
Chris Masone02119d2008-09-05 16:13:11 -04003649 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003650 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003651 if (ret != 0 && ret != -ENOENT) {
3652 btrfs_abort_transaction(trans, root, ret);
3653 goto err;
3654 }
Chris Masone02119d2008-09-05 16:13:11 -04003655
3656 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3657 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003658 if (ret == -ENOENT)
3659 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003660 else if (ret)
3661 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003662err:
3663 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003664 if (ret)
3665 goto out;
3666
3667 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003668 inode_inc_iversion(inode);
3669 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003670 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003671 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003672out:
Chris Mason39279cc2007-06-12 06:35:45 -04003673 return ret;
3674}
3675
Al Viro92986792011-03-04 17:14:37 +00003676int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3677 struct btrfs_root *root,
3678 struct inode *dir, struct inode *inode,
3679 const char *name, int name_len)
3680{
3681 int ret;
3682 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3683 if (!ret) {
3684 btrfs_drop_nlink(inode);
3685 ret = btrfs_update_inode(trans, root, inode);
3686 }
3687 return ret;
3688}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689
3690/*
3691 * helper to start transaction for unlink and rmdir.
3692 *
Josef Bacikd52be812013-05-29 14:54:47 -04003693 * unlink and rmdir are special in btrfs, they do not always free space, so
3694 * if we cannot make our reservations the normal way try and see if there is
3695 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3696 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003697 */
Josef Bacikd52be812013-05-29 14:54:47 -04003698static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699{
3700 struct btrfs_trans_handle *trans;
3701 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 int ret;
3703
Josef Bacike70bea52011-10-11 14:18:24 -04003704 /*
3705 * 1 for the possible orphan item
3706 * 1 for the dir item
3707 * 1 for the dir index
3708 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003709 * 1 for the inode
3710 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003711 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3713 return trans;
3714
Josef Bacikd52be812013-05-29 14:54:47 -04003715 if (PTR_ERR(trans) == -ENOSPC) {
3716 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717
Josef Bacikd52be812013-05-29 14:54:47 -04003718 trans = btrfs_start_transaction(root, 0);
3719 if (IS_ERR(trans))
3720 return trans;
3721 ret = btrfs_cond_migrate_bytes(root->fs_info,
3722 &root->fs_info->trans_block_rsv,
3723 num_bytes, 5);
3724 if (ret) {
3725 btrfs_end_transaction(trans, root);
3726 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003727 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003728 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003729 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003730 }
Josef Bacikd52be812013-05-29 14:54:47 -04003731 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732}
3733
Chris Mason39279cc2007-06-12 06:35:45 -04003734static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3735{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003736 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003738 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003739 int ret;
3740
Josef Bacikd52be812013-05-29 14:54:47 -04003741 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003742 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003743 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
Chris Mason12fcfd22009-03-24 10:24:20 -04003745 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3746
Chris Masone02119d2008-09-05 16:13:11 -04003747 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3748 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003749 if (ret)
3750 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003751
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003753 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003754 if (ret)
3755 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756 }
Josef Bacik7b128762008-07-24 12:17:14 -04003757
Tsutomu Itohb5324022011-07-19 07:27:20 +00003758out:
Josef Bacikd52be812013-05-29 14:54:47 -04003759 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003760 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003761 return ret;
3762}
3763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003764int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3765 struct btrfs_root *root,
3766 struct inode *dir, u64 objectid,
3767 const char *name, int name_len)
3768{
3769 struct btrfs_path *path;
3770 struct extent_buffer *leaf;
3771 struct btrfs_dir_item *di;
3772 struct btrfs_key key;
3773 u64 index;
3774 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003775 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003776
3777 path = btrfs_alloc_path();
3778 if (!path)
3779 return -ENOMEM;
3780
Li Zefan33345d012011-04-20 10:31:50 +08003781 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003782 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003783 if (IS_ERR_OR_NULL(di)) {
3784 if (!di)
3785 ret = -ENOENT;
3786 else
3787 ret = PTR_ERR(di);
3788 goto out;
3789 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790
3791 leaf = path->nodes[0];
3792 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3793 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3794 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003795 if (ret) {
3796 btrfs_abort_transaction(trans, root, ret);
3797 goto out;
3798 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003799 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800
3801 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3802 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003803 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret != -ENOENT) {
3806 btrfs_abort_transaction(trans, root, ret);
3807 goto out;
3808 }
Li Zefan33345d012011-04-20 10:31:50 +08003809 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003810 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003811 if (IS_ERR_OR_NULL(di)) {
3812 if (!di)
3813 ret = -ENOENT;
3814 else
3815 ret = PTR_ERR(di);
3816 btrfs_abort_transaction(trans, root, ret);
3817 goto out;
3818 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819
3820 leaf = path->nodes[0];
3821 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003822 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823 index = key.offset;
3824 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003825 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003828 if (ret) {
3829 btrfs_abort_transaction(trans, root, ret);
3830 goto out;
3831 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003832
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003834 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003836 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003837 if (ret)
3838 btrfs_abort_transaction(trans, root, ret);
3839out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003840 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003841 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003842}
3843
Chris Mason39279cc2007-06-12 06:35:45 -04003844static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3845{
3846 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003847 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003848 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003849 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003850
David Sterbab3ae2442012-09-13 16:04:34 -06003851 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003852 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003853 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3854 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003855
Josef Bacikd52be812013-05-29 14:54:47 -04003856 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003857 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003858 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003859
Li Zefan33345d012011-04-20 10:31:50 +08003860 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003861 err = btrfs_unlink_subvol(trans, root, dir,
3862 BTRFS_I(inode)->location.objectid,
3863 dentry->d_name.name,
3864 dentry->d_name.len);
3865 goto out;
3866 }
3867
Josef Bacik7b128762008-07-24 12:17:14 -04003868 err = btrfs_orphan_add(trans, inode);
3869 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003870 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003871
Chris Mason39279cc2007-06-12 06:35:45 -04003872 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003873 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3874 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003875 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003876 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003877out:
Josef Bacikd52be812013-05-29 14:54:47 -04003878 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003879 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003880
Chris Mason39279cc2007-06-12 06:35:45 -04003881 return err;
3882}
3883
Chris Mason323ac952008-10-01 19:05:46 -04003884/*
Chris Mason39279cc2007-06-12 06:35:45 -04003885 * this can truncate away extent items, csum items and directory items.
3886 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003887 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003888 *
3889 * csum items that cross the new i_size are truncated to the new size
3890 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003891 *
3892 * min_type is the minimum key type to truncate down to. If set to 0, this
3893 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003894 */
Yan, Zheng80825102009-11-12 09:35:36 +00003895int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root,
3897 struct inode *inode,
3898 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003899{
Chris Mason39279cc2007-06-12 06:35:45 -04003900 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003901 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003903 struct btrfs_key key;
3904 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003906 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003907 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003909 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 int found_extent;
3911 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003912 int pending_del_nr = 0;
3913 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003914 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003915 int ret;
3916 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003917 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003918
3919 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003920
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003921 path = btrfs_alloc_path();
3922 if (!path)
3923 return -ENOMEM;
3924 path->reada = -1;
3925
Josef Bacik5dc562c2012-08-17 13:14:17 -04003926 /*
3927 * We want to drop from the next block forward in case this new size is
3928 * not block aligned since we will be keeping the last block of the
3929 * extent just the way it is.
3930 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003931 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003932 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3933 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003934
Miao Xie16cdcec2011-04-22 18:12:22 +08003935 /*
3936 * This function is also used to drop the items in the log tree before
3937 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3938 * it is used to drop the loged items. So we shouldn't kill the delayed
3939 * items.
3940 */
3941 if (min_type == 0 && root == BTRFS_I(inode)->root)
3942 btrfs_kill_delayed_inode_items(inode);
3943
Li Zefan33345d012011-04-20 10:31:50 +08003944 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003945 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003946 key.type = (u8)-1;
3947
Chris Mason85e21ba2008-01-29 15:11:36 -05003948search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003949 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003950 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003951 if (ret < 0) {
3952 err = ret;
3953 goto out;
3954 }
Chris Masond3977122009-01-05 21:25:51 -05003955
Chris Mason85e21ba2008-01-29 15:11:36 -05003956 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003957 /* there are no items in the tree for us to truncate, we're
3958 * done
3959 */
Yan, Zheng80825102009-11-12 09:35:36 +00003960 if (path->slots[0] == 0)
3961 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003962 path->slots[0]--;
3963 }
3964
Chris Masond3977122009-01-05 21:25:51 -05003965 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003966 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003967 leaf = path->nodes[0];
3968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3969 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003970
Li Zefan33345d012011-04-20 10:31:50 +08003971 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003972 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003973
Chris Mason85e21ba2008-01-29 15:11:36 -05003974 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003975 break;
3976
Chris Mason5f39d392007-10-15 16:14:19 -04003977 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003978 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003979 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003980 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003981 extent_type = btrfs_file_extent_type(leaf, fi);
3982 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003983 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003984 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003985 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003986 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003987 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003988 }
Yan008630c2007-11-07 13:31:09 -05003989 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 }
Yan, Zheng80825102009-11-12 09:35:36 +00003991 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003992 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003993 } else {
3994 if (item_end < new_size)
3995 break;
3996 if (found_key.offset >= new_size)
3997 del_item = 1;
3998 else
3999 del_item = 0;
4000 }
Chris Mason39279cc2007-06-12 06:35:45 -04004001 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004002 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004003 if (found_type != BTRFS_EXTENT_DATA_KEY)
4004 goto delete;
4005
4006 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004007 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004008 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004009 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004010 u64 orig_num_bytes =
4011 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004012 extent_num_bytes = ALIGN(new_size -
4013 found_key.offset,
4014 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004015 btrfs_set_file_extent_num_bytes(leaf, fi,
4016 extent_num_bytes);
4017 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004018 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004019 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004020 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004021 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004022 } else {
Chris Masondb945352007-10-15 16:15:53 -04004023 extent_num_bytes =
4024 btrfs_file_extent_disk_num_bytes(leaf,
4025 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004026 extent_offset = found_key.offset -
4027 btrfs_file_extent_offset(leaf, fi);
4028
Chris Mason39279cc2007-06-12 06:35:45 -04004029 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004030 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004031 if (extent_start != 0) {
4032 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004033 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004034 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004035 }
4036 }
Chris Mason90692182008-02-08 13:49:28 -05004037 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004038 /*
4039 * we can't truncate inline items that have had
4040 * special encodings
4041 */
4042 if (!del_item &&
4043 btrfs_file_extent_compression(leaf, fi) == 0 &&
4044 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4045 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004046 u32 size = new_size - found_key.offset;
4047
4048 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004049 inode_sub_bytes(inode, item_end + 1 -
4050 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004051 }
4052 size =
4053 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004054 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004055 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004056 inode_sub_bytes(inode, item_end + 1 -
4057 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004058 }
Chris Mason39279cc2007-06-12 06:35:45 -04004059 }
Chris Mason179e29e2007-11-01 11:28:41 -04004060delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004061 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004062 if (!pending_del_nr) {
4063 /* no pending yet, add ourselves */
4064 pending_del_slot = path->slots[0];
4065 pending_del_nr = 1;
4066 } else if (pending_del_nr &&
4067 path->slots[0] + 1 == pending_del_slot) {
4068 /* hop on the pending chunk */
4069 pending_del_nr++;
4070 pending_del_slot = path->slots[0];
4071 } else {
Chris Masond3977122009-01-05 21:25:51 -05004072 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004073 }
Chris Mason39279cc2007-06-12 06:35:45 -04004074 } else {
4075 break;
4076 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004077 if (found_extent && (root->ref_cows ||
4078 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004079 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004080 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 extent_num_bytes, 0,
4082 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004083 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004084 BUG_ON(ret);
4085 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004086
Yan, Zheng80825102009-11-12 09:35:36 +00004087 if (found_type == BTRFS_INODE_ITEM_KEY)
4088 break;
4089
4090 if (path->slots[0] == 0 ||
4091 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004092 if (pending_del_nr) {
4093 ret = btrfs_del_items(trans, root, path,
4094 pending_del_slot,
4095 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret) {
4097 btrfs_abort_transaction(trans,
4098 root, ret);
4099 goto error;
4100 }
Yan, Zheng80825102009-11-12 09:35:36 +00004101 pending_del_nr = 0;
4102 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004103 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004104 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004105 } else {
4106 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004107 }
Chris Mason39279cc2007-06-12 06:35:45 -04004108 }
Yan, Zheng80825102009-11-12 09:35:36 +00004109out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004110 if (pending_del_nr) {
4111 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4112 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 if (ret)
4114 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004115 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116error:
Chris Mason39279cc2007-06-12 06:35:45 -04004117 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004118 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004119}
4120
Chris Masona52d9a82007-08-27 16:49:44 -04004121/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004122 * btrfs_truncate_page - read, zero a chunk and write a page
4123 * @inode - inode that we're zeroing
4124 * @from - the offset to start zeroing
4125 * @len - the length to zero, 0 to zero the entire range respective to the
4126 * offset
4127 * @front - zero up to the offset instead of from the offset on
4128 *
4129 * This will find the page for the "from" offset and cow the page and zero the
4130 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004131 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004132int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4133 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004134{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004135 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004136 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004137 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4138 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004139 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004140 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004141 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004142 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4143 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4144 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004145 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004146 int ret = 0;
4147 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004148 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004149
Josef Bacik2aaa6652012-08-29 14:27:18 -04004150 if ((offset & (blocksize - 1)) == 0 &&
4151 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004152 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004153 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004154 if (ret)
4155 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004156
Chris Mason211c17f2008-05-15 09:13:45 -04004157again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004158 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004159 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004160 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004161 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004162 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004163 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004164
4165 page_start = page_offset(page);
4166 page_end = page_start + PAGE_CACHE_SIZE - 1;
4167
Chris Masona52d9a82007-08-27 16:49:44 -04004168 if (!PageUptodate(page)) {
4169 ret = btrfs_readpage(NULL, page);
4170 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004171 if (page->mapping != mapping) {
4172 unlock_page(page);
4173 page_cache_release(page);
4174 goto again;
4175 }
Chris Masona52d9a82007-08-27 16:49:44 -04004176 if (!PageUptodate(page)) {
4177 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004178 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004179 }
4180 }
Chris Mason211c17f2008-05-15 09:13:45 -04004181 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004182
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004183 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004184 set_page_extent_mapped(page);
4185
4186 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4187 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004188 unlock_extent_cached(io_tree, page_start, page_end,
4189 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004190 unlock_page(page);
4191 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004192 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004193 btrfs_put_ordered_extent(ordered);
4194 goto again;
4195 }
4196
Josef Bacik2ac55d42010-02-03 19:33:23 +00004197 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004198 EXTENT_DIRTY | EXTENT_DELALLOC |
4199 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004200 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004201
Josef Bacik2ac55d42010-02-03 19:33:23 +00004202 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4203 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004204 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004205 unlock_extent_cached(io_tree, page_start, page_end,
4206 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004207 goto out_unlock;
4208 }
4209
Chris Masone6dcd2d2008-07-17 12:53:50 -04004210 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004211 if (!len)
4212 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004213 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004214 if (front)
4215 memset(kaddr, 0, offset);
4216 else
4217 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004218 flush_dcache_page(page);
4219 kunmap(page);
4220 }
Chris Mason247e7432008-07-17 12:53:51 -04004221 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004222 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004223 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4224 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004225
Chris Mason89642222008-07-24 09:41:53 -04004226out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004227 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004228 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004229 unlock_page(page);
4230 page_cache_release(page);
4231out:
4232 return ret;
4233}
4234
Josef Bacik695a0d02011-03-04 15:46:53 -05004235/*
4236 * This function puts in dummy file extents for the area we're creating a hole
4237 * for. So if we are truncating this file to a larger size we need to insert
4238 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4239 * the range between oldsize and size
4240 */
Josef Bacika41ad392011-01-31 15:30:16 -05004241int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004242{
4243 struct btrfs_trans_handle *trans;
4244 struct btrfs_root *root = BTRFS_I(inode)->root;
4245 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004246 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004247 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004249 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4250 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004251 u64 last_byte;
4252 u64 cur_offset;
4253 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004254 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004255
4256 if (size <= hole_start)
4257 return 0;
4258
Yan Zheng9036c102008-10-30 14:19:41 -04004259 while (1) {
4260 struct btrfs_ordered_extent *ordered;
4261 btrfs_wait_ordered_range(inode, hole_start,
4262 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004263 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004264 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004265 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4266 if (!ordered)
4267 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004268 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4269 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004270 btrfs_put_ordered_extent(ordered);
4271 }
4272
Yan Zheng9036c102008-10-30 14:19:41 -04004273 cur_offset = hole_start;
4274 while (1) {
4275 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4276 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 if (IS_ERR(em)) {
4278 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004279 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004280 break;
4281 }
Yan Zheng9036c102008-10-30 14:19:41 -04004282 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004283 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004284 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004285 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004286 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004287
Miao Xie36423202011-12-14 20:12:02 -05004288 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004289 if (IS_ERR(trans)) {
4290 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004291 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004292 }
Yan, Zheng80825102009-11-12 09:35:36 +00004293
Josef Bacik5dc562c2012-08-17 13:14:17 -04004294 err = btrfs_drop_extents(trans, root, inode,
4295 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004296 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004297 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004298 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004299 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004300 break;
Miao Xie5b397372011-09-11 10:52:24 -04004301 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004302
Yan Zheng9036c102008-10-30 14:19:41 -04004303 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004304 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004305 0, hole_size, 0, hole_size,
4306 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004307 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004309 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004310 break;
Miao Xie5b397372011-09-11 10:52:24 -04004311 }
Yan, Zheng80825102009-11-12 09:35:36 +00004312
Josef Bacik5dc562c2012-08-17 13:14:17 -04004313 btrfs_drop_extent_cache(inode, cur_offset,
4314 cur_offset + hole_size - 1, 0);
4315 hole_em = alloc_extent_map();
4316 if (!hole_em) {
4317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4318 &BTRFS_I(inode)->runtime_flags);
4319 goto next;
4320 }
4321 hole_em->start = cur_offset;
4322 hole_em->len = hole_size;
4323 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004324
Josef Bacik5dc562c2012-08-17 13:14:17 -04004325 hole_em->block_start = EXTENT_MAP_HOLE;
4326 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004327 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004328 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004329 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4330 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4331 hole_em->generation = trans->transid;
4332
4333 while (1) {
4334 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004335 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004336 write_unlock(&em_tree->lock);
4337 if (err != -EEXIST)
4338 break;
4339 btrfs_drop_extent_cache(inode, cur_offset,
4340 cur_offset +
4341 hole_size - 1, 0);
4342 }
4343 free_extent_map(hole_em);
4344next:
Miao Xie36423202011-12-14 20:12:02 -05004345 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004346 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004347 }
4348 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004349 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004350 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004351 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004352 break;
4353 }
4354
Yan, Zhenga22285a2010-05-16 10:48:46 -04004355 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004356 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4357 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004358 return err;
4359}
4360
Eric Sandeen3972f262013-01-12 02:57:22 +00004361static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004362{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004363 struct btrfs_root *root = BTRFS_I(inode)->root;
4364 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004365 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004366 loff_t newsize = attr->ia_size;
4367 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004368 int ret;
4369
Josef Bacika41ad392011-01-31 15:30:16 -05004370 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004371 return 0;
4372
Eric Sandeen3972f262013-01-12 02:57:22 +00004373 /*
4374 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4375 * special case where we need to update the times despite not having
4376 * these flags set. For all other operations the VFS set these flags
4377 * explicitly if it wants a timestamp update.
4378 */
4379 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4380 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4381
Josef Bacika41ad392011-01-31 15:30:16 -05004382 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004383 truncate_pagecache(inode, oldsize, newsize);
4384 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004385 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004386 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004387
Miao Xief4a2f4c2011-12-14 20:12:01 -05004388 trans = btrfs_start_transaction(root, 1);
4389 if (IS_ERR(trans))
4390 return PTR_ERR(trans);
4391
4392 i_size_write(inode, newsize);
4393 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4394 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004395 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004396 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004397
Josef Bacika41ad392011-01-31 15:30:16 -05004398 /*
4399 * We're truncating a file that used to have good data down to
4400 * zero. Make sure it gets into the ordered flush list so that
4401 * any new writes get down to disk quickly.
4402 */
4403 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004404 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4405 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004406
Josef Bacikf3fe8202013-01-07 17:03:21 -05004407 /*
4408 * 1 for the orphan item we're going to add
4409 * 1 for the orphan item deletion.
4410 */
4411 trans = btrfs_start_transaction(root, 2);
4412 if (IS_ERR(trans))
4413 return PTR_ERR(trans);
4414
4415 /*
4416 * We need to do this in case we fail at _any_ point during the
4417 * actual truncate. Once we do the truncate_setsize we could
4418 * invalidate pages which forces any outstanding ordered io to
4419 * be instantly completed which will give us extents that need
4420 * to be truncated. If we fail to get an orphan inode down we
4421 * could have left over extents that were never meant to live,
4422 * so we need to garuntee from this point on that everything
4423 * will be consistent.
4424 */
4425 ret = btrfs_orphan_add(trans, inode);
4426 btrfs_end_transaction(trans, root);
4427 if (ret)
4428 return ret;
4429
Josef Bacika41ad392011-01-31 15:30:16 -05004430 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4431 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004432
4433 /* Disable nonlocked read DIO to avoid the end less truncate */
4434 btrfs_inode_block_unlocked_dio(inode);
4435 inode_dio_wait(inode);
4436 btrfs_inode_resume_unlocked_dio(inode);
4437
Josef Bacika41ad392011-01-31 15:30:16 -05004438 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004439 if (ret && inode->i_nlink)
4440 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004441 }
4442
Josef Bacika41ad392011-01-31 15:30:16 -05004443 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004444}
4445
Chris Mason39279cc2007-06-12 06:35:45 -04004446static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4447{
4448 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004449 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004450 int err;
4451
Li Zefanb83cc962010-12-20 16:04:08 +08004452 if (btrfs_root_readonly(root))
4453 return -EROFS;
4454
Chris Mason39279cc2007-06-12 06:35:45 -04004455 err = inode_change_ok(inode, attr);
4456 if (err)
4457 return err;
4458
Chris Mason5a3f23d2009-03-31 13:27:11 -04004459 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004460 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004461 if (err)
4462 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004463 }
Yan Zheng9036c102008-10-30 14:19:41 -04004464
Christoph Hellwig10257742010-06-04 11:30:02 +02004465 if (attr->ia_valid) {
4466 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004467 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004468 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004469
Josef Bacik22c44fe2011-11-30 10:45:38 -05004470 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004471 err = btrfs_acl_chmod(inode);
4472 }
4473
Chris Mason39279cc2007-06-12 06:35:45 -04004474 return err;
4475}
Chris Mason61295eb2008-01-14 16:24:38 -05004476
Al Virobd555972010-06-07 11:35:40 -04004477void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004478{
4479 struct btrfs_trans_handle *trans;
4480 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004481 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004482 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004483 int ret;
4484
liubo1abe9b82011-03-24 11:18:59 +00004485 trace_btrfs_inode_evict(inode);
4486
Chris Mason39279cc2007-06-12 06:35:45 -04004487 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004488 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004489 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004490 goto no_delete;
4491
Chris Mason39279cc2007-06-12 06:35:45 -04004492 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004493 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004494 goto no_delete;
4495 }
Al Virobd555972010-06-07 11:35:40 -04004496 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004497 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004498
Yan, Zhengc71bf092009-11-12 09:34:40 +00004499 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004500 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004501 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004502 goto no_delete;
4503 }
4504
Yan, Zheng76dda932009-09-21 16:00:26 -04004505 if (inode->i_nlink > 0) {
4506 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4507 goto no_delete;
4508 }
4509
Miao Xie0e8c36a2012-12-19 06:59:51 +00004510 ret = btrfs_commit_inode_delayed_inode(inode);
4511 if (ret) {
4512 btrfs_orphan_del(NULL, inode);
4513 goto no_delete;
4514 }
4515
Miao Xie66d8f3d2012-09-06 04:02:28 -06004516 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004517 if (!rsv) {
4518 btrfs_orphan_del(NULL, inode);
4519 goto no_delete;
4520 }
Josef Bacik4a338542011-08-29 11:01:31 -04004521 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004522 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004523 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004524
Chris Masondbe674a2008-07-17 12:54:05 -04004525 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004526
Josef Bacik4289a662011-08-05 13:22:24 -04004527 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004528 * This is a bit simpler than btrfs_truncate since we've already
4529 * reserved our space for our orphan item in the unlink, so we just
4530 * need to reserve some slack space in case we add bytes and update
4531 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004532 */
Yan, Zheng80825102009-11-12 09:35:36 +00004533 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004534 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4535 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004536
Josef Bacik726c35f2011-09-26 15:46:06 -04004537 /*
4538 * Try and steal from the global reserve since we will
4539 * likely not use this space anyway, we want to try as
4540 * hard as possible to get this to work.
4541 */
4542 if (ret)
4543 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4544
Yan, Zhengd68fc572010-05-16 10:49:58 -04004545 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004546 btrfs_warn(root->fs_info,
4547 "Could not get space for a delete, will truncate on mount %d",
4548 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004549 btrfs_orphan_del(NULL, inode);
4550 btrfs_free_block_rsv(root, rsv);
4551 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004552 }
4553
Miao Xie0e8c36a2012-12-19 06:59:51 +00004554 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004555 if (IS_ERR(trans)) {
4556 btrfs_orphan_del(NULL, inode);
4557 btrfs_free_block_rsv(root, rsv);
4558 goto no_delete;
4559 }
4560
4561 trans->block_rsv = rsv;
4562
Yan, Zhengd68fc572010-05-16 10:49:58 -04004563 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004564 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004565 break;
4566
Miao Xie8407aa42012-09-07 01:43:32 -06004567 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004568 btrfs_end_transaction(trans, root);
4569 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004570 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004571 }
4572
Josef Bacik4289a662011-08-05 13:22:24 -04004573 btrfs_free_block_rsv(root, rsv);
4574
Yan, Zheng80825102009-11-12 09:35:36 +00004575 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004576 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004577 ret = btrfs_orphan_del(trans, inode);
4578 BUG_ON(ret);
4579 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004580
Josef Bacik4289a662011-08-05 13:22:24 -04004581 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004582 if (!(root == root->fs_info->tree_root ||
4583 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004584 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004585
Chris Mason54aa1f42007-06-22 14:16:25 -04004586 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004587 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004588no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004589 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004590 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004591 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004592}
4593
4594/*
4595 * this returns the key found in the dir entry in the location pointer.
4596 * If no dir entries were found, location->objectid is 0.
4597 */
4598static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4599 struct btrfs_key *location)
4600{
4601 const char *name = dentry->d_name.name;
4602 int namelen = dentry->d_name.len;
4603 struct btrfs_dir_item *di;
4604 struct btrfs_path *path;
4605 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004606 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004607
4608 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004609 if (!path)
4610 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004611
Li Zefan33345d012011-04-20 10:31:50 +08004612 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004613 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004614 if (IS_ERR(di))
4615 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004616
David Sterbac7040052011-04-19 18:00:01 +02004617 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004618 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004619
Chris Mason5f39d392007-10-15 16:14:19 -04004620 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004621out:
Chris Mason39279cc2007-06-12 06:35:45 -04004622 btrfs_free_path(path);
4623 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004624out_err:
4625 location->objectid = 0;
4626 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004627}
4628
4629/*
4630 * when we hit a tree root in a directory, the btrfs part of the inode
4631 * needs to be changed to reflect the root directory of the tree root. This
4632 * is kind of like crossing a mount point.
4633 */
4634static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004635 struct inode *dir,
4636 struct dentry *dentry,
4637 struct btrfs_key *location,
4638 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004639{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004640 struct btrfs_path *path;
4641 struct btrfs_root *new_root;
4642 struct btrfs_root_ref *ref;
4643 struct extent_buffer *leaf;
4644 int ret;
4645 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004646
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004647 path = btrfs_alloc_path();
4648 if (!path) {
4649 err = -ENOMEM;
4650 goto out;
4651 }
Chris Mason39279cc2007-06-12 06:35:45 -04004652
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004653 err = -ENOENT;
4654 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4655 BTRFS_I(dir)->root->root_key.objectid,
4656 location->objectid);
4657 if (ret) {
4658 if (ret < 0)
4659 err = ret;
4660 goto out;
4661 }
Chris Mason39279cc2007-06-12 06:35:45 -04004662
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004663 leaf = path->nodes[0];
4664 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004665 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004666 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4667 goto out;
4668
4669 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4670 (unsigned long)(ref + 1),
4671 dentry->d_name.len);
4672 if (ret)
4673 goto out;
4674
David Sterbab3b4aa72011-04-21 01:20:15 +02004675 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004676
4677 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4678 if (IS_ERR(new_root)) {
4679 err = PTR_ERR(new_root);
4680 goto out;
4681 }
4682
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004683 *sub_root = new_root;
4684 location->objectid = btrfs_root_dirid(&new_root->root_item);
4685 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004686 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004687 err = 0;
4688out:
4689 btrfs_free_path(path);
4690 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004691}
4692
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004693static void inode_tree_add(struct inode *inode)
4694{
4695 struct btrfs_root *root = BTRFS_I(inode)->root;
4696 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004697 struct rb_node **p;
4698 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004699 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004700
Al Viro1d3382cb2010-10-23 15:19:20 -04004701 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004702 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004703again:
4704 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004705 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004706 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004707 while (*p) {
4708 parent = *p;
4709 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4710
Li Zefan33345d012011-04-20 10:31:50 +08004711 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004712 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004713 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004714 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004715 else {
4716 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004717 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004718 rb_erase(parent, &root->inode_tree);
4719 RB_CLEAR_NODE(parent);
4720 spin_unlock(&root->inode_lock);
4721 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004722 }
4723 }
4724 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4725 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4726 spin_unlock(&root->inode_lock);
4727}
4728
4729static void inode_tree_del(struct inode *inode)
4730{
4731 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004732 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004734 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004735 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004736 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004737 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004738 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004739 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004740 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004741
Josef Bacik0af3d002010-06-21 14:48:16 -04004742 /*
4743 * Free space cache has inodes in the tree root, but the tree root has a
4744 * root_refs of 0, so this could end up dropping the tree root as a
4745 * snapshot, so we need the extra !root->fs_info->tree_root check to
4746 * make sure we don't drop it.
4747 */
4748 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4749 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004750 synchronize_srcu(&root->fs_info->subvol_srcu);
4751 spin_lock(&root->inode_lock);
4752 empty = RB_EMPTY_ROOT(&root->inode_tree);
4753 spin_unlock(&root->inode_lock);
4754 if (empty)
4755 btrfs_add_dead_root(root);
4756 }
4757}
4758
Jeff Mahoney143bede2012-03-01 14:56:26 +01004759void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004760{
4761 struct rb_node *node;
4762 struct rb_node *prev;
4763 struct btrfs_inode *entry;
4764 struct inode *inode;
4765 u64 objectid = 0;
4766
4767 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4768
4769 spin_lock(&root->inode_lock);
4770again:
4771 node = root->inode_tree.rb_node;
4772 prev = NULL;
4773 while (node) {
4774 prev = node;
4775 entry = rb_entry(node, struct btrfs_inode, rb_node);
4776
Li Zefan33345d012011-04-20 10:31:50 +08004777 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004778 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004779 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004780 node = node->rb_right;
4781 else
4782 break;
4783 }
4784 if (!node) {
4785 while (prev) {
4786 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004787 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004788 node = prev;
4789 break;
4790 }
4791 prev = rb_next(prev);
4792 }
4793 }
4794 while (node) {
4795 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004796 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004797 inode = igrab(&entry->vfs_inode);
4798 if (inode) {
4799 spin_unlock(&root->inode_lock);
4800 if (atomic_read(&inode->i_count) > 1)
4801 d_prune_aliases(inode);
4802 /*
Al Viro45321ac2010-06-07 13:43:19 -04004803 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004804 * the inode cache when its usage count
4805 * hits zero.
4806 */
4807 iput(inode);
4808 cond_resched();
4809 spin_lock(&root->inode_lock);
4810 goto again;
4811 }
4812
4813 if (cond_resched_lock(&root->inode_lock))
4814 goto again;
4815
4816 node = rb_next(node);
4817 }
4818 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004819}
4820
Chris Masone02119d2008-09-05 16:13:11 -04004821static int btrfs_init_locked_inode(struct inode *inode, void *p)
4822{
4823 struct btrfs_iget_args *args = p;
4824 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004825 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004826 return 0;
4827}
4828
4829static int btrfs_find_actor(struct inode *inode, void *opaque)
4830{
4831 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004832 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004833 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004834}
4835
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004836static struct inode *btrfs_iget_locked(struct super_block *s,
4837 u64 objectid,
4838 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004839{
4840 struct inode *inode;
4841 struct btrfs_iget_args args;
4842 args.ino = objectid;
4843 args.root = root;
4844
4845 inode = iget5_locked(s, objectid, btrfs_find_actor,
4846 btrfs_init_locked_inode,
4847 (void *)&args);
4848 return inode;
4849}
4850
Balaji Rao1a54ef82008-07-21 02:01:04 +05304851/* Get an inode object given its location and corresponding root.
4852 * Returns in *is_new if the inode was read from disk
4853 */
4854struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004855 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304856{
4857 struct inode *inode;
4858
4859 inode = btrfs_iget_locked(s, location->objectid, root);
4860 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004861 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304862
4863 if (inode->i_state & I_NEW) {
4864 BTRFS_I(inode)->root = root;
4865 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4866 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004867 if (!is_bad_inode(inode)) {
4868 inode_tree_add(inode);
4869 unlock_new_inode(inode);
4870 if (new)
4871 *new = 1;
4872 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004873 unlock_new_inode(inode);
4874 iput(inode);
4875 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004876 }
4877 }
4878
Balaji Rao1a54ef82008-07-21 02:01:04 +05304879 return inode;
4880}
4881
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004882static struct inode *new_simple_dir(struct super_block *s,
4883 struct btrfs_key *key,
4884 struct btrfs_root *root)
4885{
4886 struct inode *inode = new_inode(s);
4887
4888 if (!inode)
4889 return ERR_PTR(-ENOMEM);
4890
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004891 BTRFS_I(inode)->root = root;
4892 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004893 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004894
4895 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004896 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004897 inode->i_fop = &simple_dir_operations;
4898 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4899 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4900
4901 return inode;
4902}
4903
Chris Mason3de45862008-11-17 21:02:50 -05004904struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004905{
Chris Masond3977122009-01-05 21:25:51 -05004906 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004907 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004908 struct btrfs_root *sub_root = root;
4909 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004910 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004911 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004912
4913 if (dentry->d_name.len > BTRFS_NAME_LEN)
4914 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004915
Jeff Layton39e3c952012-11-28 11:30:53 -05004916 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004917 if (ret < 0)
4918 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004919
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004920 if (location.objectid == 0)
4921 return NULL;
4922
4923 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004924 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004925 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004926 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004927
4928 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4929
Yan, Zheng76dda932009-09-21 16:00:26 -04004930 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004931 ret = fixup_tree_root_location(root, dir, dentry,
4932 &location, &sub_root);
4933 if (ret < 0) {
4934 if (ret != -ENOENT)
4935 inode = ERR_PTR(ret);
4936 else
4937 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4938 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004939 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004940 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004941 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4942
Julia Lawall34d19ba2011-01-24 19:55:19 +00004943 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004944 down_read(&root->fs_info->cleanup_work_sem);
4945 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004946 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004947 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004948 if (ret) {
4949 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004950 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004951 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004952 }
4953
Chris Mason3de45862008-11-17 21:02:50 -05004954 return inode;
4955}
4956
Nick Pigginfe15ce42011-01-07 17:49:23 +11004957static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004958{
4959 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004960 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004961
Li Zefan848cce02012-02-21 17:04:28 +08004962 if (!inode && !IS_ROOT(dentry))
4963 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004964
Li Zefan848cce02012-02-21 17:04:28 +08004965 if (inode) {
4966 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004967 if (btrfs_root_refs(&root->root_item) == 0)
4968 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004969
4970 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4971 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004972 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004973 return 0;
4974}
4975
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004976static void btrfs_dentry_release(struct dentry *dentry)
4977{
4978 if (dentry->d_fsdata)
4979 kfree(dentry->d_fsdata);
4980}
4981
Chris Mason3de45862008-11-17 21:02:50 -05004982static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004983 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004984{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004985 struct dentry *ret;
4986
4987 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004988 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004989}
4990
Miao Xie16cdcec2011-04-22 18:12:22 +08004991unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004992 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4993};
4994
David Woodhousecbdf5a22008-08-06 19:42:33 +01004995static int btrfs_real_readdir(struct file *filp, void *dirent,
4996 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004997{
Al Viro496ad9a2013-01-23 17:07:38 -05004998 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04004999 struct btrfs_root *root = BTRFS_I(inode)->root;
5000 struct btrfs_item *item;
5001 struct btrfs_dir_item *di;
5002 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005003 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005004 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005005 struct list_head ins_list;
5006 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005007 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005008 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005009 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005010 unsigned char d_type;
5011 int over = 0;
5012 u32 di_cur;
5013 u32 di_total;
5014 u32 di_len;
5015 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005016 char tmp_name[32];
5017 char *name_ptr;
5018 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005019 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005020
5021 /* FIXME, use a real flag for deciding about the key type */
5022 if (root->fs_info->tree_root == root)
5023 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005024
Chris Mason39544012007-12-12 14:38:19 -05005025 /* special case for "." */
5026 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005027 over = filldir(dirent, ".", 1,
5028 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005029 if (over)
5030 return 0;
5031 filp->f_pos = 1;
5032 }
Chris Mason39544012007-12-12 14:38:19 -05005033 /* special case for .., just use the back ref */
5034 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005035 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005036 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005037 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005038 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005039 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005040 filp->f_pos = 2;
5041 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005042 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005043 if (!path)
5044 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005045
Josef Bacik026fd312011-05-13 10:32:11 -04005046 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005047
Miao Xie16cdcec2011-04-22 18:12:22 +08005048 if (key_type == BTRFS_DIR_INDEX_KEY) {
5049 INIT_LIST_HEAD(&ins_list);
5050 INIT_LIST_HEAD(&del_list);
5051 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5052 }
5053
Chris Mason39279cc2007-06-12 06:35:45 -04005054 btrfs_set_key_type(&key, key_type);
5055 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005056 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005057
Chris Mason39279cc2007-06-12 06:35:45 -04005058 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5059 if (ret < 0)
5060 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005061
5062 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005063 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005064 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005065 if (slot >= btrfs_header_nritems(leaf)) {
5066 ret = btrfs_next_leaf(root, path);
5067 if (ret < 0)
5068 goto err;
5069 else if (ret > 0)
5070 break;
5071 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005072 }
Chris Mason3de45862008-11-17 21:02:50 -05005073
Chris Mason5f39d392007-10-15 16:14:19 -04005074 item = btrfs_item_nr(leaf, slot);
5075 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5076
5077 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005078 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005079 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005080 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005081 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005082 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005083 if (key_type == BTRFS_DIR_INDEX_KEY &&
5084 btrfs_should_delete_dir_index(&del_list,
5085 found_key.offset))
5086 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005087
5088 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005089 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005090
Chris Mason39279cc2007-06-12 06:35:45 -04005091 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5092 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005093 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005094
5095 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005096 struct btrfs_key location;
5097
Josef Bacik22a94d42011-03-16 16:47:17 -04005098 if (verify_dir_item(root, leaf, di))
5099 break;
5100
Chris Mason5f39d392007-10-15 16:14:19 -04005101 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005102 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005103 name_ptr = tmp_name;
5104 } else {
5105 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005106 if (!name_ptr) {
5107 ret = -ENOMEM;
5108 goto err;
5109 }
Chris Mason5f39d392007-10-15 16:14:19 -04005110 }
5111 read_extent_buffer(leaf, name_ptr,
5112 (unsigned long)(di + 1), name_len);
5113
5114 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5115 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005116
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005117
Chris Mason3de45862008-11-17 21:02:50 -05005118 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005119 * skip it.
5120 *
5121 * In contrast to old kernels, we insert the snapshot's
5122 * dir item and dir index after it has been created, so
5123 * we won't find a reference to our own snapshot. We
5124 * still keep the following code for backward
5125 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005126 */
5127 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5128 location.objectid == root->root_key.objectid) {
5129 over = 0;
5130 goto skip;
5131 }
Chris Mason5f39d392007-10-15 16:14:19 -04005132 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005133 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005134 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005135
Chris Mason3de45862008-11-17 21:02:50 -05005136skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005137 if (name_ptr != tmp_name)
5138 kfree(name_ptr);
5139
Chris Mason39279cc2007-06-12 06:35:45 -04005140 if (over)
5141 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005142 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005143 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005144 di_cur += di_len;
5145 di = (struct btrfs_dir_item *)((char *)di + di_len);
5146 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005147next:
5148 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005149 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005150
Miao Xie16cdcec2011-04-22 18:12:22 +08005151 if (key_type == BTRFS_DIR_INDEX_KEY) {
5152 if (is_curr)
5153 filp->f_pos++;
5154 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5155 &ins_list);
5156 if (ret)
5157 goto nopos;
5158 }
5159
David Woodhouse49593bf2008-08-17 17:08:36 +01005160 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005161 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005162 /*
5163 * 32-bit glibc will use getdents64, but then strtol -
5164 * so the last number we can serve is this.
5165 */
5166 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005167 else
5168 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005169nopos:
5170 ret = 0;
5171err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005172 if (key_type == BTRFS_DIR_INDEX_KEY)
5173 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005174 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005175 return ret;
5176}
5177
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005178int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005179{
5180 struct btrfs_root *root = BTRFS_I(inode)->root;
5181 struct btrfs_trans_handle *trans;
5182 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005183 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005184
Josef Bacik72ac3c02012-05-23 14:13:11 -04005185 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005186 return 0;
5187
Liu Bo83eea1f2012-07-10 05:28:39 -06005188 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005189 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005190
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005191 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005192 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005193 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005194 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005195 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005196 if (IS_ERR(trans))
5197 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005198 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005199 }
5200 return ret;
5201}
5202
5203/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005204 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005205 * inode changes. But, it is most likely to find the inode in cache.
5206 * FIXME, needs more benchmarking...there are no reasons other than performance
5207 * to keep or drop this code.
5208 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005209static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005210{
5211 struct btrfs_root *root = BTRFS_I(inode)->root;
5212 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005213 int ret;
5214
Josef Bacik72ac3c02012-05-23 14:13:11 -04005215 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005216 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005217
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005218 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005219 if (IS_ERR(trans))
5220 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005221
5222 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005223 if (ret && ret == -ENOSPC) {
5224 /* whoops, lets try again with the full transaction */
5225 btrfs_end_transaction(trans, root);
5226 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005227 if (IS_ERR(trans))
5228 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005229
Chris Mason94b60442010-05-26 11:02:00 -04005230 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005231 }
Chris Mason39279cc2007-06-12 06:35:45 -04005232 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005233 if (BTRFS_I(inode)->delayed_node)
5234 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005235
5236 return ret;
5237}
5238
5239/*
5240 * This is a copy of file_update_time. We need this so we can return error on
5241 * ENOSPC for updating the inode in the case of file write and mmap writes.
5242 */
Josef Bacike41f9412012-03-26 09:46:47 -04005243static int btrfs_update_time(struct inode *inode, struct timespec *now,
5244 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005245{
Alexander Block2bc5565282012-06-15 09:49:33 +02005246 struct btrfs_root *root = BTRFS_I(inode)->root;
5247
5248 if (btrfs_root_readonly(root))
5249 return -EROFS;
5250
Josef Bacike41f9412012-03-26 09:46:47 -04005251 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005252 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005253 if (flags & S_CTIME)
5254 inode->i_ctime = *now;
5255 if (flags & S_MTIME)
5256 inode->i_mtime = *now;
5257 if (flags & S_ATIME)
5258 inode->i_atime = *now;
5259 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005260}
5261
Chris Masond352ac62008-09-29 15:18:18 -04005262/*
5263 * find the highest existing sequence number in a directory
5264 * and then set the in-memory index_cnt variable to reflect
5265 * free sequence numbers
5266 */
Josef Bacikaec74772008-07-24 12:12:38 -04005267static int btrfs_set_inode_index_count(struct inode *inode)
5268{
5269 struct btrfs_root *root = BTRFS_I(inode)->root;
5270 struct btrfs_key key, found_key;
5271 struct btrfs_path *path;
5272 struct extent_buffer *leaf;
5273 int ret;
5274
Li Zefan33345d012011-04-20 10:31:50 +08005275 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005276 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5277 key.offset = (u64)-1;
5278
5279 path = btrfs_alloc_path();
5280 if (!path)
5281 return -ENOMEM;
5282
5283 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5284 if (ret < 0)
5285 goto out;
5286 /* FIXME: we should be able to handle this */
5287 if (ret == 0)
5288 goto out;
5289 ret = 0;
5290
5291 /*
5292 * MAGIC NUMBER EXPLANATION:
5293 * since we search a directory based on f_pos we have to start at 2
5294 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5295 * else has to start at 2
5296 */
5297 if (path->slots[0] == 0) {
5298 BTRFS_I(inode)->index_cnt = 2;
5299 goto out;
5300 }
5301
5302 path->slots[0]--;
5303
5304 leaf = path->nodes[0];
5305 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5306
Li Zefan33345d012011-04-20 10:31:50 +08005307 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005308 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5309 BTRFS_I(inode)->index_cnt = 2;
5310 goto out;
5311 }
5312
5313 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5314out:
5315 btrfs_free_path(path);
5316 return ret;
5317}
5318
Chris Masond352ac62008-09-29 15:18:18 -04005319/*
5320 * helper to find a free sequence number in a given directory. This current
5321 * code is very simple, later versions will do smarter things in the btree
5322 */
Chris Mason3de45862008-11-17 21:02:50 -05005323int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005324{
5325 int ret = 0;
5326
5327 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005328 ret = btrfs_inode_delayed_dir_index_count(dir);
5329 if (ret) {
5330 ret = btrfs_set_inode_index_count(dir);
5331 if (ret)
5332 return ret;
5333 }
Josef Bacikaec74772008-07-24 12:12:38 -04005334 }
5335
Chris Mason00e4e6b2008-08-05 11:18:09 -04005336 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005337 BTRFS_I(dir)->index_cnt++;
5338
5339 return ret;
5340}
5341
Chris Mason39279cc2007-06-12 06:35:45 -04005342static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5343 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005344 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005345 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005346 u64 ref_objectid, u64 objectid,
5347 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005348{
5349 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005350 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005351 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005352 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005353 struct btrfs_inode_ref *ref;
5354 struct btrfs_key key[2];
5355 u32 sizes[2];
5356 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005357 int ret;
5358 int owner;
5359
Chris Mason5f39d392007-10-15 16:14:19 -04005360 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005361 if (!path)
5362 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005363
Chris Mason39279cc2007-06-12 06:35:45 -04005364 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005365 if (!inode) {
5366 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005367 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005368 }
Chris Mason39279cc2007-06-12 06:35:45 -04005369
Li Zefan581bb052011-04-20 10:06:11 +08005370 /*
5371 * we have to initialize this early, so we can reclaim the inode
5372 * number if we fail afterwards in this function.
5373 */
5374 inode->i_ino = objectid;
5375
Josef Bacikaec74772008-07-24 12:12:38 -04005376 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005377 trace_btrfs_inode_request(dir);
5378
Chris Mason3de45862008-11-17 21:02:50 -05005379 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005380 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005381 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005382 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005383 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005384 }
Josef Bacikaec74772008-07-24 12:12:38 -04005385 }
5386 /*
5387 * index_cnt is ignored for everything but a dir,
5388 * btrfs_get_inode_index_count has an explanation for the magic
5389 * number
5390 */
5391 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005392 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005393 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005394 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005395
Josef Bacik5dc562c2012-08-17 13:14:17 -04005396 /*
5397 * We could have gotten an inode number from somebody who was fsynced
5398 * and then removed in this same transaction, so let's just set full
5399 * sync since it will be a full sync anyway and this will blow away the
5400 * old info in the log.
5401 */
5402 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5403
Al Viro569254b2011-07-24 17:08:40 -04005404 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005405 owner = 0;
5406 else
5407 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005408
5409 key[0].objectid = objectid;
5410 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5411 key[0].offset = 0;
5412
Mark Fashehf1863732012-08-08 11:32:27 -07005413 /*
5414 * Start new inodes with an inode_ref. This is slightly more
5415 * efficient for small numbers of hard links since they will
5416 * be packed into one item. Extended refs will kick in if we
5417 * add more hard links than can fit in the ref item.
5418 */
Chris Mason9c583092008-01-29 15:15:18 -05005419 key[1].objectid = objectid;
5420 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5421 key[1].offset = ref_objectid;
5422
5423 sizes[0] = sizeof(struct btrfs_inode_item);
5424 sizes[1] = name_len + sizeof(*ref);
5425
Chris Masonb9473432009-03-13 11:00:37 -04005426 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005427 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5428 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005429 goto fail;
5430
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005431 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005432 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005433 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005434 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5435 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005436 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5437 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005438 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005439
5440 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5441 struct btrfs_inode_ref);
5442 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005443 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005444 ptr = (unsigned long)(ref + 1);
5445 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5446
Chris Mason5f39d392007-10-15 16:14:19 -04005447 btrfs_mark_buffer_dirty(path->nodes[0]);
5448 btrfs_free_path(path);
5449
Chris Mason39279cc2007-06-12 06:35:45 -04005450 location = &BTRFS_I(inode)->location;
5451 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005452 location->offset = 0;
5453 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5454
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005455 btrfs_inherit_iflags(inode, dir);
5456
Al Viro569254b2011-07-24 17:08:40 -04005457 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005458 if (btrfs_test_opt(root, NODATASUM))
5459 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005460 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005461 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5462 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005463 }
5464
Chris Mason39279cc2007-06-12 06:35:45 -04005465 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005466 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005467
5468 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005469 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005470
Alexander Block8ea05e32012-07-25 17:35:53 +02005471 btrfs_update_root_times(trans, root);
5472
Chris Mason39279cc2007-06-12 06:35:45 -04005473 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005474fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005475 if (dir)
5476 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005477 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005478 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005479 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005480}
5481
5482static inline u8 btrfs_inode_type(struct inode *inode)
5483{
5484 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5485}
5486
Chris Masond352ac62008-09-29 15:18:18 -04005487/*
5488 * utility function to add 'inode' into 'parent_inode' with
5489 * a give name and a given sequence number.
5490 * if 'add_backref' is true, also insert a backref from the
5491 * inode to the parent directory.
5492 */
Chris Masone02119d2008-09-05 16:13:11 -04005493int btrfs_add_link(struct btrfs_trans_handle *trans,
5494 struct inode *parent_inode, struct inode *inode,
5495 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005496{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005497 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005498 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005499 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005500 u64 ino = btrfs_ino(inode);
5501 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005502
Li Zefan33345d012011-04-20 10:31:50 +08005503 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5505 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005506 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5508 key.offset = 0;
5509 }
Chris Mason39279cc2007-06-12 06:35:45 -04005510
Li Zefan33345d012011-04-20 10:31:50 +08005511 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5513 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005514 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005515 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005516 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5517 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 }
5519
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005520 /* Nothing to clean up yet */
5521 if (ret)
5522 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005524 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5525 parent_inode, &key,
5526 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005527 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005528 goto fail_dir_item;
5529 else if (ret) {
5530 btrfs_abort_transaction(trans, root, ret);
5531 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005532 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005533
5534 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5535 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005536 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005537 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5538 ret = btrfs_update_inode(trans, root, parent_inode);
5539 if (ret)
5540 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005541 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005542
5543fail_dir_item:
5544 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5545 u64 local_index;
5546 int err;
5547 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5548 key.objectid, root->root_key.objectid,
5549 parent_ino, &local_index, name, name_len);
5550
5551 } else if (add_backref) {
5552 u64 local_index;
5553 int err;
5554
5555 err = btrfs_del_inode_ref(trans, root, name, name_len,
5556 ino, parent_ino, &local_index);
5557 }
5558 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005559}
5560
5561static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005562 struct inode *dir, struct dentry *dentry,
5563 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005564{
Josef Bacika1b075d2010-11-19 20:36:11 +00005565 int err = btrfs_add_link(trans, dir, inode,
5566 dentry->d_name.name, dentry->d_name.len,
5567 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005568 if (err > 0)
5569 err = -EEXIST;
5570 return err;
5571}
5572
Josef Bacik618e21d2007-07-11 10:18:17 -04005573static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005574 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005575{
5576 struct btrfs_trans_handle *trans;
5577 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005578 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005579 int err;
5580 int drop_inode = 0;
5581 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005582 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005583
5584 if (!new_valid_dev(rdev))
5585 return -EINVAL;
5586
Josef Bacik9ed74f22009-09-11 16:12:44 -04005587 /*
5588 * 2 for inode item and ref
5589 * 2 for dir items
5590 * 1 for xattr if selinux is on
5591 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005592 trans = btrfs_start_transaction(root, 5);
5593 if (IS_ERR(trans))
5594 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005595
Li Zefan581bb052011-04-20 10:06:11 +08005596 err = btrfs_find_free_ino(root, &objectid);
5597 if (err)
5598 goto out_unlock;
5599
Josef Bacikaec74772008-07-24 12:12:38 -04005600 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005601 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005602 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005603 if (IS_ERR(inode)) {
5604 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005605 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005606 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005607
Eric Paris2a7dba32011-02-01 11:05:39 -05005608 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005609 if (err) {
5610 drop_inode = 1;
5611 goto out_unlock;
5612 }
5613
Casey Schauflerad19db72011-12-15 10:09:07 -05005614 /*
5615 * If the active LSM wants to access the inode during
5616 * d_instantiate it needs these. Smack checks to see
5617 * if the filesystem supports xattrs by looking at the
5618 * ops vector.
5619 */
5620
5621 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005622 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005623 if (err)
5624 drop_inode = 1;
5625 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005626 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005627 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005628 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005629 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005630out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005631 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005632 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005633 if (drop_inode) {
5634 inode_dec_link_count(inode);
5635 iput(inode);
5636 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005637 return err;
5638}
5639
Chris Mason39279cc2007-06-12 06:35:45 -04005640static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005641 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005642{
5643 struct btrfs_trans_handle *trans;
5644 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005645 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005646 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005647 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005648 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005649 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005650
Josef Bacik9ed74f22009-09-11 16:12:44 -04005651 /*
5652 * 2 for inode item and ref
5653 * 2 for dir items
5654 * 1 for xattr if selinux is on
5655 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005656 trans = btrfs_start_transaction(root, 5);
5657 if (IS_ERR(trans))
5658 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005659
Li Zefan581bb052011-04-20 10:06:11 +08005660 err = btrfs_find_free_ino(root, &objectid);
5661 if (err)
5662 goto out_unlock;
5663
Josef Bacikaec74772008-07-24 12:12:38 -04005664 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005665 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005666 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005667 if (IS_ERR(inode)) {
5668 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005669 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005670 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005671 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005672
Eric Paris2a7dba32011-02-01 11:05:39 -05005673 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005674 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005675 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005676
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005677 err = btrfs_update_inode(trans, root, inode);
5678 if (err)
5679 goto out_unlock;
5680
Casey Schauflerad19db72011-12-15 10:09:07 -05005681 /*
5682 * If the active LSM wants to access the inode during
5683 * d_instantiate it needs these. Smack checks to see
5684 * if the filesystem supports xattrs by looking at the
5685 * ops vector.
5686 */
5687 inode->i_fop = &btrfs_file_operations;
5688 inode->i_op = &btrfs_file_inode_operations;
5689
Josef Bacika1b075d2010-11-19 20:36:11 +00005690 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005691 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005692 goto out_unlock;
5693
5694 inode->i_mapping->a_ops = &btrfs_aops;
5695 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5696 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5697 d_instantiate(dentry, inode);
5698
Chris Mason39279cc2007-06-12 06:35:45 -04005699out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005700 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005701 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005702 inode_dec_link_count(inode);
5703 iput(inode);
5704 }
Liu Bob53d3f52012-11-14 14:34:34 +00005705 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005706 return err;
5707}
5708
5709static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5710 struct dentry *dentry)
5711{
5712 struct btrfs_trans_handle *trans;
5713 struct btrfs_root *root = BTRFS_I(dir)->root;
5714 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005715 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005716 int err;
5717 int drop_inode = 0;
5718
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005719 /* do not allow sys_link's with other subvols of the same device */
5720 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005721 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005722
Mark Fashehf1863732012-08-08 11:32:27 -07005723 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005724 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005725
Chris Mason3de45862008-11-17 21:02:50 -05005726 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005727 if (err)
5728 goto fail;
5729
Yan, Zhenga22285a2010-05-16 10:48:46 -04005730 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005731 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005732 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005733 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005734 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005735 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005736 if (IS_ERR(trans)) {
5737 err = PTR_ERR(trans);
5738 goto fail;
5739 }
Chris Mason5f39d392007-10-15 16:14:19 -04005740
Miao Xie31534952011-04-13 13:19:21 +08005741 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005742 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005743 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005744 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005745 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005746
Josef Bacika1b075d2010-11-19 20:36:11 +00005747 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005748
Yan, Zhenga5719522009-09-24 09:17:31 -04005749 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005750 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005751 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005752 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005753 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005754 if (err)
5755 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005756 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005757 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005758 }
Chris Mason39279cc2007-06-12 06:35:45 -04005759
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005760 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005761fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005762 if (drop_inode) {
5763 inode_dec_link_count(inode);
5764 iput(inode);
5765 }
Liu Bob53d3f52012-11-14 14:34:34 +00005766 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005767 return err;
5768}
5769
Al Viro18bb1db2011-07-26 01:41:39 -04005770static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005771{
Chris Masonb9d86662008-05-02 16:13:49 -04005772 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005773 struct btrfs_trans_handle *trans;
5774 struct btrfs_root *root = BTRFS_I(dir)->root;
5775 int err = 0;
5776 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005777 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005778 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005779
Josef Bacik9ed74f22009-09-11 16:12:44 -04005780 /*
5781 * 2 items for inode and ref
5782 * 2 items for dir items
5783 * 1 for xattr if selinux is on
5784 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005785 trans = btrfs_start_transaction(root, 5);
5786 if (IS_ERR(trans))
5787 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005788
Li Zefan581bb052011-04-20 10:06:11 +08005789 err = btrfs_find_free_ino(root, &objectid);
5790 if (err)
5791 goto out_fail;
5792
Josef Bacikaec74772008-07-24 12:12:38 -04005793 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005794 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005795 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005796 if (IS_ERR(inode)) {
5797 err = PTR_ERR(inode);
5798 goto out_fail;
5799 }
Chris Mason5f39d392007-10-15 16:14:19 -04005800
Chris Mason39279cc2007-06-12 06:35:45 -04005801 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005802
Eric Paris2a7dba32011-02-01 11:05:39 -05005803 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005804 if (err)
5805 goto out_fail;
5806
Chris Mason39279cc2007-06-12 06:35:45 -04005807 inode->i_op = &btrfs_dir_inode_operations;
5808 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005809
Chris Masondbe674a2008-07-17 12:54:05 -04005810 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005811 err = btrfs_update_inode(trans, root, inode);
5812 if (err)
5813 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005814
Josef Bacika1b075d2010-11-19 20:36:11 +00005815 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5816 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005817 if (err)
5818 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005819
Chris Mason39279cc2007-06-12 06:35:45 -04005820 d_instantiate(dentry, inode);
5821 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005822
5823out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005824 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005825 if (drop_on_err)
5826 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005827 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005828 return err;
5829}
5830
Chris Masond352ac62008-09-29 15:18:18 -04005831/* helper for btfs_get_extent. Given an existing extent in the tree,
5832 * and an extent that you want to insert, deal with overlap and insert
5833 * the new extent into the tree.
5834 */
Chris Mason3b951512008-04-17 11:29:12 -04005835static int merge_extent_mapping(struct extent_map_tree *em_tree,
5836 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005837 struct extent_map *em,
5838 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005839{
5840 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005841
Chris Masone6dcd2d2008-07-17 12:53:50 -04005842 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5843 start_diff = map_start - em->start;
5844 em->start = map_start;
5845 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005846 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5847 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005848 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005849 em->block_len -= start_diff;
5850 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005851 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005852}
5853
Chris Masonc8b97812008-10-29 14:49:59 -04005854static noinline int uncompress_inline(struct btrfs_path *path,
5855 struct inode *inode, struct page *page,
5856 size_t pg_offset, u64 extent_offset,
5857 struct btrfs_file_extent_item *item)
5858{
5859 int ret;
5860 struct extent_buffer *leaf = path->nodes[0];
5861 char *tmp;
5862 size_t max_size;
5863 unsigned long inline_size;
5864 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005865 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005866
5867 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005868 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005869 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5870 inline_size = btrfs_file_extent_inline_item_len(leaf,
5871 btrfs_item_nr(leaf, path->slots[0]));
5872 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005873 if (!tmp)
5874 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005875 ptr = btrfs_file_extent_inline_start(item);
5876
5877 read_extent_buffer(leaf, tmp, ptr, inline_size);
5878
Chris Mason5b050f02008-11-11 09:34:41 -05005879 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005880 ret = btrfs_decompress(compress_type, tmp, page,
5881 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005882 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005883 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005884 unsigned long copy_size = min_t(u64,
5885 PAGE_CACHE_SIZE - pg_offset,
5886 max_size - extent_offset);
5887 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005888 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005889 }
5890 kfree(tmp);
5891 return 0;
5892}
5893
Chris Masond352ac62008-09-29 15:18:18 -04005894/*
5895 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005896 * the ugly parts come from merging extents from the disk with the in-ram
5897 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005898 * where the in-ram extents might be locked pending data=ordered completion.
5899 *
5900 * This also copies inline extents directly into the page.
5901 */
Chris Masond3977122009-01-05 21:25:51 -05005902
Chris Masona52d9a82007-08-27 16:49:44 -04005903struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005904 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005905 int create)
5906{
5907 int ret;
5908 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005909 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005910 u64 extent_start = 0;
5911 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005912 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005913 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005914 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005915 struct btrfs_root *root = BTRFS_I(inode)->root;
5916 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005917 struct extent_buffer *leaf;
5918 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005919 struct extent_map *em = NULL;
5920 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005921 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005922 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005923 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005924
Chris Masona52d9a82007-08-27 16:49:44 -04005925again:
Chris Mason890871b2009-09-02 16:24:52 -04005926 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005927 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005928 if (em)
5929 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005930 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005931
Chris Masona52d9a82007-08-27 16:49:44 -04005932 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005933 if (em->start > start || em->start + em->len <= start)
5934 free_extent_map(em);
5935 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005936 free_extent_map(em);
5937 else
5938 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005939 }
David Sterba172ddd62011-04-21 00:48:27 +02005940 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005941 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005942 err = -ENOMEM;
5943 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005945 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005946 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005947 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005948 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005949 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005950
5951 if (!path) {
5952 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005953 if (!path) {
5954 err = -ENOMEM;
5955 goto out;
5956 }
5957 /*
5958 * Chances are we'll be called again, so go ahead and do
5959 * readahead
5960 */
5961 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005962 }
5963
Chris Mason179e29e2007-11-01 11:28:41 -04005964 ret = btrfs_lookup_file_extent(trans, root, path,
5965 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005966 if (ret < 0) {
5967 err = ret;
5968 goto out;
5969 }
5970
5971 if (ret != 0) {
5972 if (path->slots[0] == 0)
5973 goto not_found;
5974 path->slots[0]--;
5975 }
5976
Chris Mason5f39d392007-10-15 16:14:19 -04005977 leaf = path->nodes[0];
5978 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005979 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005980 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005981 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5982 found_type = btrfs_key_type(&found_key);
5983 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005984 found_type != BTRFS_EXTENT_DATA_KEY) {
5985 goto not_found;
5986 }
5987
Chris Mason5f39d392007-10-15 16:14:19 -04005988 found_type = btrfs_file_extent_type(leaf, item);
5989 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005990 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005991 if (found_type == BTRFS_FILE_EXTENT_REG ||
5992 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005993 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005994 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005995 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5996 size_t size;
5997 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00005998 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005999 }
6000
6001 if (start >= extent_end) {
6002 path->slots[0]++;
6003 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6004 ret = btrfs_next_leaf(root, path);
6005 if (ret < 0) {
6006 err = ret;
6007 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006008 }
Yan Zheng9036c102008-10-30 14:19:41 -04006009 if (ret > 0)
6010 goto not_found;
6011 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006012 }
Yan Zheng9036c102008-10-30 14:19:41 -04006013 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6014 if (found_key.objectid != objectid ||
6015 found_key.type != BTRFS_EXTENT_DATA_KEY)
6016 goto not_found;
6017 if (start + len <= found_key.offset)
6018 goto not_found;
6019 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006020 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006021 em->len = found_key.offset - start;
6022 goto not_found_em;
6023 }
6024
Josef Bacikcc95bef2013-04-04 14:31:27 -04006025 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006026 if (found_type == BTRFS_FILE_EXTENT_REG ||
6027 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006028 em->start = extent_start;
6029 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006030 em->orig_start = extent_start -
6031 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006032 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6033 item);
Chris Masondb945352007-10-15 16:15:53 -04006034 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6035 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006036 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006037 goto insert;
6038 }
Li Zefan261507a02010-12-17 14:21:50 +08006039 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006040 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006041 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006042 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006043 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006044 } else {
6045 bytenr += btrfs_file_extent_offset(leaf, item);
6046 em->block_start = bytenr;
6047 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006048 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6049 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006050 }
Chris Masona52d9a82007-08-27 16:49:44 -04006051 goto insert;
6052 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006053 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006054 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006055 size_t size;
6056 size_t extent_offset;
6057 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006058
Chris Masona52d9a82007-08-27 16:49:44 -04006059 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006060 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006061 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006062 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006063 goto out;
6064 }
6065
Yan Zheng9036c102008-10-30 14:19:41 -04006066 size = btrfs_file_extent_inline_len(leaf, item);
6067 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006068 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006069 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006070 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006071 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006072 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006073 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006074 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006075 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006076 em->compress_type = compress_type;
6077 }
Yan689f9342007-10-29 11:41:07 -04006078 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006079 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006080 if (btrfs_file_extent_compression(leaf, item) !=
6081 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006082 ret = uncompress_inline(path, inode, page,
6083 pg_offset,
6084 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006085 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006086 } else {
6087 map = kmap(page);
6088 read_extent_buffer(leaf, map + pg_offset, ptr,
6089 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006090 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6091 memset(map + pg_offset + copy_size, 0,
6092 PAGE_CACHE_SIZE - pg_offset -
6093 copy_size);
6094 }
Chris Masonc8b97812008-10-29 14:49:59 -04006095 kunmap(page);
6096 }
Chris Mason179e29e2007-11-01 11:28:41 -04006097 flush_dcache_page(page);
6098 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006099 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006100 if (!trans) {
6101 kunmap(page);
6102 free_extent_map(em);
6103 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006104
David Sterbab3b4aa72011-04-21 01:20:15 +02006105 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006106 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006107
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006108 if (IS_ERR(trans))
6109 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006110 goto again;
6111 }
Chris Masonc8b97812008-10-29 14:49:59 -04006112 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006113 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006114 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006115 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006116 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006117 }
Chris Masond1310b22008-01-24 16:13:08 -05006118 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006119 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006120 goto insert;
6121 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006122 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006123 }
6124not_found:
6125 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006126 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006127 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006128not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006129 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006130 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006131insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006132 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006133 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006134 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6135 (unsigned long long)em->start,
6136 (unsigned long long)em->len,
6137 (unsigned long long)start,
6138 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006139 err = -EIO;
6140 goto out;
6141 }
Chris Masond1310b22008-01-24 16:13:08 -05006142
6143 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006144 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006145 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006146 /* it is possible that someone inserted the extent into the tree
6147 * while we had the lock dropped. It is also possible that
6148 * an overlapping map exists in the tree
6149 */
Chris Masona52d9a82007-08-27 16:49:44 -04006150 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006151 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006152
6153 ret = 0;
6154
Chris Mason3b951512008-04-17 11:29:12 -04006155 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006156 if (existing && (existing->start > start ||
6157 existing->start + existing->len <= start)) {
6158 free_extent_map(existing);
6159 existing = NULL;
6160 }
Chris Mason3b951512008-04-17 11:29:12 -04006161 if (!existing) {
6162 existing = lookup_extent_mapping(em_tree, em->start,
6163 em->len);
6164 if (existing) {
6165 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006166 em, start,
6167 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006168 free_extent_map(existing);
6169 if (err) {
6170 free_extent_map(em);
6171 em = NULL;
6172 }
6173 } else {
6174 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006175 free_extent_map(em);
6176 em = NULL;
6177 }
6178 } else {
6179 free_extent_map(em);
6180 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006181 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006182 }
Chris Masona52d9a82007-08-27 16:49:44 -04006183 }
Chris Mason890871b2009-09-02 16:24:52 -04006184 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006185out:
liubo1abe9b82011-03-24 11:18:59 +00006186
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006187 if (em)
6188 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006189
Chris Masonf4219502008-07-22 11:18:09 -04006190 if (path)
6191 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006192 if (trans) {
6193 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006194 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006195 err = ret;
6196 }
Chris Masona52d9a82007-08-27 16:49:44 -04006197 if (err) {
6198 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006199 return ERR_PTR(err);
6200 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006201 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006202 return em;
6203}
6204
Chris Masonec29ed52011-02-23 16:23:20 -05006205struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6206 size_t pg_offset, u64 start, u64 len,
6207 int create)
6208{
6209 struct extent_map *em;
6210 struct extent_map *hole_em = NULL;
6211 u64 range_start = start;
6212 u64 end;
6213 u64 found;
6214 u64 found_end;
6215 int err = 0;
6216
6217 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6218 if (IS_ERR(em))
6219 return em;
6220 if (em) {
6221 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006222 * if our em maps to
6223 * - a hole or
6224 * - a pre-alloc extent,
6225 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006226 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006227 if (em->block_start != EXTENT_MAP_HOLE &&
6228 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006229 return em;
6230 else
6231 hole_em = em;
6232 }
6233
6234 /* check to see if we've wrapped (len == -1 or similar) */
6235 end = start + len;
6236 if (end < start)
6237 end = (u64)-1;
6238 else
6239 end -= 1;
6240
6241 em = NULL;
6242
6243 /* ok, we didn't find anything, lets look for delalloc */
6244 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6245 end, len, EXTENT_DELALLOC, 1);
6246 found_end = range_start + found;
6247 if (found_end < range_start)
6248 found_end = (u64)-1;
6249
6250 /*
6251 * we didn't find anything useful, return
6252 * the original results from get_extent()
6253 */
6254 if (range_start > end || found_end <= start) {
6255 em = hole_em;
6256 hole_em = NULL;
6257 goto out;
6258 }
6259
6260 /* adjust the range_start to make sure it doesn't
6261 * go backwards from the start they passed in
6262 */
6263 range_start = max(start,range_start);
6264 found = found_end - range_start;
6265
6266 if (found > 0) {
6267 u64 hole_start = start;
6268 u64 hole_len = len;
6269
David Sterba172ddd62011-04-21 00:48:27 +02006270 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006271 if (!em) {
6272 err = -ENOMEM;
6273 goto out;
6274 }
6275 /*
6276 * when btrfs_get_extent can't find anything it
6277 * returns one huge hole
6278 *
6279 * make sure what it found really fits our range, and
6280 * adjust to make sure it is based on the start from
6281 * the caller
6282 */
6283 if (hole_em) {
6284 u64 calc_end = extent_map_end(hole_em);
6285
6286 if (calc_end <= start || (hole_em->start > end)) {
6287 free_extent_map(hole_em);
6288 hole_em = NULL;
6289 } else {
6290 hole_start = max(hole_em->start, start);
6291 hole_len = calc_end - hole_start;
6292 }
6293 }
6294 em->bdev = NULL;
6295 if (hole_em && range_start > hole_start) {
6296 /* our hole starts before our delalloc, so we
6297 * have to return just the parts of the hole
6298 * that go until the delalloc starts
6299 */
6300 em->len = min(hole_len,
6301 range_start - hole_start);
6302 em->start = hole_start;
6303 em->orig_start = hole_start;
6304 /*
6305 * don't adjust block start at all,
6306 * it is fixed at EXTENT_MAP_HOLE
6307 */
6308 em->block_start = hole_em->block_start;
6309 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006310 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6311 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006312 } else {
6313 em->start = range_start;
6314 em->len = found;
6315 em->orig_start = range_start;
6316 em->block_start = EXTENT_MAP_DELALLOC;
6317 em->block_len = found;
6318 }
6319 } else if (hole_em) {
6320 return hole_em;
6321 }
6322out:
6323
6324 free_extent_map(hole_em);
6325 if (err) {
6326 free_extent_map(em);
6327 return ERR_PTR(err);
6328 }
6329 return em;
6330}
6331
Josef Bacik4b46fce2010-05-23 11:00:55 -04006332static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6333 u64 start, u64 len)
6334{
6335 struct btrfs_root *root = BTRFS_I(inode)->root;
6336 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006337 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006338 struct btrfs_key ins;
6339 u64 alloc_hint;
6340 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006342 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006343 if (IS_ERR(trans))
6344 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006345
6346 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6347
6348 alloc_hint = get_extent_allocation_hint(inode, start, len);
6349 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006350 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006351 if (ret) {
6352 em = ERR_PTR(ret);
6353 goto out;
6354 }
6355
Josef Bacik70c8a912012-10-11 16:54:30 -04006356 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006357 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006358 if (IS_ERR(em))
6359 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006360
6361 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6362 ins.offset, ins.offset, 0);
6363 if (ret) {
6364 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6365 em = ERR_PTR(ret);
6366 }
6367out:
6368 btrfs_end_transaction(trans, root);
6369 return em;
6370}
6371
Chris Mason46bfbb52010-05-26 11:04:10 -04006372/*
6373 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6374 * block must be cow'd
6375 */
6376static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006377 struct inode *inode, u64 offset, u64 *len,
6378 u64 *orig_start, u64 *orig_block_len,
6379 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006380{
6381 struct btrfs_path *path;
6382 int ret;
6383 struct extent_buffer *leaf;
6384 struct btrfs_root *root = BTRFS_I(inode)->root;
6385 struct btrfs_file_extent_item *fi;
6386 struct btrfs_key key;
6387 u64 disk_bytenr;
6388 u64 backref_offset;
6389 u64 extent_end;
6390 u64 num_bytes;
6391 int slot;
6392 int found_type;
6393
6394 path = btrfs_alloc_path();
6395 if (!path)
6396 return -ENOMEM;
6397
Li Zefan33345d012011-04-20 10:31:50 +08006398 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006399 offset, 0);
6400 if (ret < 0)
6401 goto out;
6402
6403 slot = path->slots[0];
6404 if (ret == 1) {
6405 if (slot == 0) {
6406 /* can't find the item, must cow */
6407 ret = 0;
6408 goto out;
6409 }
6410 slot--;
6411 }
6412 ret = 0;
6413 leaf = path->nodes[0];
6414 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006415 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006416 key.type != BTRFS_EXTENT_DATA_KEY) {
6417 /* not our file or wrong item type, must cow */
6418 goto out;
6419 }
6420
6421 if (key.offset > offset) {
6422 /* Wrong offset, must cow */
6423 goto out;
6424 }
6425
6426 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6427 found_type = btrfs_file_extent_type(leaf, fi);
6428 if (found_type != BTRFS_FILE_EXTENT_REG &&
6429 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6430 /* not a regular extent, must cow */
6431 goto out;
6432 }
6433 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6434 backref_offset = btrfs_file_extent_offset(leaf, fi);
6435
Josef Bacikeb384b52013-04-24 16:32:55 -04006436 *orig_start = key.offset - backref_offset;
6437 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6438 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6439
Chris Mason46bfbb52010-05-26 11:04:10 -04006440 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006441 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006442 /* extent doesn't include our full range, must cow */
6443 goto out;
6444 }
6445
6446 if (btrfs_extent_readonly(root, disk_bytenr))
6447 goto out;
6448
6449 /*
6450 * look for other files referencing this extent, if we
6451 * find any we must cow
6452 */
Li Zefan33345d012011-04-20 10:31:50 +08006453 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006454 key.offset - backref_offset, disk_bytenr))
6455 goto out;
6456
6457 /*
6458 * adjust disk_bytenr and num_bytes to cover just the bytes
6459 * in this extent we are about to write. If there
6460 * are any csums in that range we have to cow in order
6461 * to keep the csums correct
6462 */
6463 disk_bytenr += backref_offset;
6464 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006465 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006466 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6467 goto out;
6468 /*
6469 * all of the above have passed, it is safe to overwrite this extent
6470 * without cow
6471 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006472 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006473 ret = 1;
6474out:
6475 btrfs_free_path(path);
6476 return ret;
6477}
6478
Josef Bacikeb838e72012-07-31 16:28:48 -04006479static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6480 struct extent_state **cached_state, int writing)
6481{
6482 struct btrfs_ordered_extent *ordered;
6483 int ret = 0;
6484
6485 while (1) {
6486 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6487 0, cached_state);
6488 /*
6489 * We're concerned with the entire range that we're going to be
6490 * doing DIO to, so we need to make sure theres no ordered
6491 * extents in this range.
6492 */
6493 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6494 lockend - lockstart + 1);
6495
6496 /*
6497 * We need to make sure there are no buffered pages in this
6498 * range either, we could have raced between the invalidate in
6499 * generic_file_direct_write and locking the extent. The
6500 * invalidate needs to happen so that reads after a write do not
6501 * get stale data.
6502 */
6503 if (!ordered && (!writing ||
6504 !test_range_bit(&BTRFS_I(inode)->io_tree,
6505 lockstart, lockend, EXTENT_UPTODATE, 0,
6506 *cached_state)))
6507 break;
6508
6509 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6510 cached_state, GFP_NOFS);
6511
6512 if (ordered) {
6513 btrfs_start_ordered_extent(inode, ordered, 1);
6514 btrfs_put_ordered_extent(ordered);
6515 } else {
6516 /* Screw you mmap */
6517 ret = filemap_write_and_wait_range(inode->i_mapping,
6518 lockstart,
6519 lockend);
6520 if (ret)
6521 break;
6522
6523 /*
6524 * If we found a page that couldn't be invalidated just
6525 * fall back to buffered.
6526 */
6527 ret = invalidate_inode_pages2_range(inode->i_mapping,
6528 lockstart >> PAGE_CACHE_SHIFT,
6529 lockend >> PAGE_CACHE_SHIFT);
6530 if (ret)
6531 break;
6532 }
6533
6534 cond_resched();
6535 }
6536
6537 return ret;
6538}
6539
Josef Bacik69ffb542012-09-11 15:40:07 -04006540static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6541 u64 len, u64 orig_start,
6542 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006543 u64 orig_block_len, u64 ram_bytes,
6544 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006545{
6546 struct extent_map_tree *em_tree;
6547 struct extent_map *em;
6548 struct btrfs_root *root = BTRFS_I(inode)->root;
6549 int ret;
6550
6551 em_tree = &BTRFS_I(inode)->extent_tree;
6552 em = alloc_extent_map();
6553 if (!em)
6554 return ERR_PTR(-ENOMEM);
6555
6556 em->start = start;
6557 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006558 em->mod_start = start;
6559 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006560 em->len = len;
6561 em->block_len = block_len;
6562 em->block_start = block_start;
6563 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006564 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006565 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006566 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006567 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6568 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006569 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006570
6571 do {
6572 btrfs_drop_extent_cache(inode, em->start,
6573 em->start + em->len - 1, 0);
6574 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006575 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006576 write_unlock(&em_tree->lock);
6577 } while (ret == -EEXIST);
6578
6579 if (ret) {
6580 free_extent_map(em);
6581 return ERR_PTR(ret);
6582 }
6583
6584 return em;
6585}
6586
6587
Josef Bacik4b46fce2010-05-23 11:00:55 -04006588static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6589 struct buffer_head *bh_result, int create)
6590{
6591 struct extent_map *em;
6592 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006593 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006594 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006595 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006596 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006597 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006598 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006599 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006600
Miao Xie172a5042013-02-21 02:48:22 -07006601 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006602 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006603 else
Josef Bacikc3298612012-08-03 16:49:19 -04006604 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006605
Josef Bacikc3298612012-08-03 16:49:19 -04006606 lockstart = start;
6607 lockend = start + len - 1;
6608
Josef Bacikeb838e72012-07-31 16:28:48 -04006609 /*
6610 * If this errors out it's because we couldn't invalidate pagecache for
6611 * this range and we need to fallback to buffered.
6612 */
6613 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6614 return -ENOTBLK;
6615
Josef Bacik4b46fce2010-05-23 11:00:55 -04006616 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006617 if (IS_ERR(em)) {
6618 ret = PTR_ERR(em);
6619 goto unlock_err;
6620 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006621
6622 /*
6623 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6624 * io. INLINE is special, and we could probably kludge it in here, but
6625 * it's still buffered so for safety lets just fall back to the generic
6626 * buffered path.
6627 *
6628 * For COMPRESSED we _have_ to read the entire extent in so we can
6629 * decompress it, so there will be buffering required no matter what we
6630 * do, so go ahead and fallback to buffered.
6631 *
6632 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6633 * to buffered IO. Don't blame me, this is the price we pay for using
6634 * the generic code.
6635 */
6636 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6637 em->block_start == EXTENT_MAP_INLINE) {
6638 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006639 ret = -ENOTBLK;
6640 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006641 }
6642
6643 /* Just a good old fashioned hole, return */
6644 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6645 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6646 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006647 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006648 }
6649
6650 /*
6651 * We don't allocate a new extent in the following cases
6652 *
6653 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6654 * existing extent.
6655 * 2) The extent is marked as PREALLOC. We're good to go here and can
6656 * just use the extent.
6657 *
6658 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006659 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006660 len = min(len, em->len - (start - em->start));
6661 lockstart = start + len;
6662 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006663 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006664
6665 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6666 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6667 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006668 int type;
6669 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006670 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006671
6672 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6673 type = BTRFS_ORDERED_PREALLOC;
6674 else
6675 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006676 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006677 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006678
6679 /*
6680 * we're not going to log anything, but we do need
6681 * to make sure the current transaction stays open
6682 * while we look for nocow cross refs
6683 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006684 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006685 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006686 goto must_cow;
6687
Josef Bacikeb384b52013-04-24 16:32:55 -04006688 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6689 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006690 if (type == BTRFS_ORDERED_PREALLOC) {
6691 free_extent_map(em);
6692 em = create_pinned_em(inode, start, len,
6693 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006694 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006695 orig_block_len,
6696 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006697 if (IS_ERR(em)) {
6698 btrfs_end_transaction(trans, root);
6699 goto unlock_err;
6700 }
6701 }
6702
Chris Mason46bfbb52010-05-26 11:04:10 -04006703 ret = btrfs_add_ordered_extent_dio(inode, start,
6704 block_start, len, len, type);
6705 btrfs_end_transaction(trans, root);
6706 if (ret) {
6707 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006708 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006709 }
6710 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006711 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006712 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006713 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006714must_cow:
6715 /*
6716 * this will cow the extent, reset the len in case we changed
6717 * it above
6718 */
6719 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006720 free_extent_map(em);
6721 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006722 if (IS_ERR(em)) {
6723 ret = PTR_ERR(em);
6724 goto unlock_err;
6725 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006726 len = min(len, em->len - (start - em->start));
6727unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006728 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6729 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006730 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006731 bh_result->b_bdev = em->bdev;
6732 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006733 if (create) {
6734 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6735 set_buffer_new(bh_result);
6736
6737 /*
6738 * Need to update the i_size under the extent lock so buffered
6739 * readers will get the updated i_size when we unlock.
6740 */
6741 if (start + len > i_size_read(inode))
6742 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006743
Miao Xie172a5042013-02-21 02:48:22 -07006744 spin_lock(&BTRFS_I(inode)->lock);
6745 BTRFS_I(inode)->outstanding_extents++;
6746 spin_unlock(&BTRFS_I(inode)->lock);
6747
Miao Xie09348562013-02-07 10:12:07 +00006748 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6749 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6750 &cached_state, GFP_NOFS);
6751 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006752 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006753
Josef Bacikeb838e72012-07-31 16:28:48 -04006754 /*
6755 * In the case of write we need to clear and unlock the entire range,
6756 * in the case of read we need to unlock only the end area that we
6757 * aren't using if there is any left over space.
6758 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006759 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006760 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6761 lockend, unlock_bits, 1, 0,
6762 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006763 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006764 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006765 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006766
Josef Bacik4b46fce2010-05-23 11:00:55 -04006767 free_extent_map(em);
6768
6769 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006770
6771unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006772 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6773 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6774 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775}
6776
6777struct btrfs_dio_private {
6778 struct inode *inode;
6779 u64 logical_offset;
6780 u64 disk_bytenr;
6781 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006783
6784 /* number of bios pending for this dio */
6785 atomic_t pending_bios;
6786
6787 /* IO errors */
6788 int errors;
6789
Chris Mason9be33952013-05-17 18:30:14 -04006790 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006791 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006792
6793 /* dio_bio came from fs/direct-io.c */
6794 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006795};
6796
6797static void btrfs_endio_direct_read(struct bio *bio, int err)
6798{
Miao Xiee65e1532010-11-22 03:04:43 +00006799 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006800 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6801 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006802 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006803 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006804 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006806
6807 start = dip->logical_offset;
6808 do {
6809 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6810 struct page *page = bvec->bv_page;
6811 char *kaddr;
6812 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006813 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006814 unsigned long flags;
6815
Josef Bacikc3298612012-08-03 16:49:19 -04006816 if (get_state_private(&BTRFS_I(inode)->io_tree,
6817 start, &private))
6818 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006819 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006820 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006821 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 csum, bvec->bv_len);
6823 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006824 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825 local_irq_restore(flags);
6826
6827 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006828 if (csum != private) {
6829failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006830 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6831 (unsigned long long)btrfs_ino(inode),
6832 (unsigned long long)start,
6833 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006834 err = -EIO;
6835 }
6836 }
6837
6838 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839 bvec++;
6840 } while (bvec <= bvec_end);
6841
6842 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006843 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006844 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006845
Josef Bacik4b46fce2010-05-23 11:00:55 -04006846 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006847
6848 /* If we had a csum failure make sure to clear the uptodate flag */
6849 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006850 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6851 dio_end_io(dio_bio, err);
6852 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006853}
6854
6855static void btrfs_endio_direct_write(struct bio *bio, int err)
6856{
6857 struct btrfs_dio_private *dip = bio->bi_private;
6858 struct inode *inode = dip->inode;
6859 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006860 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006861 u64 ordered_offset = dip->logical_offset;
6862 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006863 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006864 int ret;
6865
6866 if (err)
6867 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006868again:
6869 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6870 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006871 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006873 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006874
Josef Bacik5fd02042012-05-02 14:00:54 -04006875 ordered->work.func = finish_ordered_fn;
6876 ordered->work.flags = 0;
6877 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6878 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006879out_test:
6880 /*
6881 * our bio might span multiple ordered extents. If we haven't
6882 * completed the accounting for the whole dio, go back and try again
6883 */
6884 if (ordered_offset < dip->logical_offset + dip->bytes) {
6885 ordered_bytes = dip->logical_offset + dip->bytes -
6886 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006887 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006888 goto again;
6889 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006890out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006891 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006892
Josef Bacik4b46fce2010-05-23 11:00:55 -04006893 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006894
6895 /* If we had an error make sure to clear the uptodate flag */
6896 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006897 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6898 dio_end_io(dio_bio, err);
6899 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006900}
6901
Chris Masoneaf25d92010-05-25 09:48:28 -04006902static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6903 struct bio *bio, int mirror_num,
6904 unsigned long bio_flags, u64 offset)
6905{
6906 int ret;
6907 struct btrfs_root *root = BTRFS_I(inode)->root;
6908 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006909 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006910 return 0;
6911}
6912
Miao Xiee65e1532010-11-22 03:04:43 +00006913static void btrfs_end_dio_bio(struct bio *bio, int err)
6914{
6915 struct btrfs_dio_private *dip = bio->bi_private;
6916
6917 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006918 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006919 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006920 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006921 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006922 dip->errors = 1;
6923
6924 /*
6925 * before atomic variable goto zero, we must make sure
6926 * dip->errors is perceived to be set.
6927 */
6928 smp_mb__before_atomic_dec();
6929 }
6930
6931 /* if there are more bios still pending for this dio, just exit */
6932 if (!atomic_dec_and_test(&dip->pending_bios))
6933 goto out;
6934
Chris Mason9be33952013-05-17 18:30:14 -04006935 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006936 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006937 } else {
6938 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006939 bio_endio(dip->orig_bio, 0);
6940 }
6941out:
6942 bio_put(bio);
6943}
6944
6945static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6946 u64 first_sector, gfp_t gfp_flags)
6947{
6948 int nr_vecs = bio_get_nr_vecs(bdev);
6949 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6950}
6951
6952static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6953 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006954 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006955{
6956 int write = rw & REQ_WRITE;
6957 struct btrfs_root *root = BTRFS_I(inode)->root;
6958 int ret;
6959
Josef Bacikb812ce22012-11-16 13:56:32 -05006960 if (async_submit)
6961 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6962
Miao Xiee65e1532010-11-22 03:04:43 +00006963 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006964
6965 if (!write) {
6966 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6967 if (ret)
6968 goto err;
6969 }
Miao Xiee65e1532010-11-22 03:04:43 +00006970
Josef Bacik1ae39932011-04-06 14:41:34 -04006971 if (skip_sum)
6972 goto map;
6973
6974 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006975 ret = btrfs_wq_submit_bio(root->fs_info,
6976 inode, rw, bio, 0, 0,
6977 file_offset,
6978 __btrfs_submit_bio_start_direct_io,
6979 __btrfs_submit_bio_done);
6980 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006981 } else if (write) {
6982 /*
6983 * If we aren't doing async submit, calculate the csum of the
6984 * bio now.
6985 */
6986 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6987 if (ret)
6988 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006989 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006990 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006991 if (ret)
6992 goto err;
6993 }
Miao Xiee65e1532010-11-22 03:04:43 +00006994
Josef Bacik1ae39932011-04-06 14:41:34 -04006995map:
6996 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006997err:
6998 bio_put(bio);
6999 return ret;
7000}
7001
7002static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7003 int skip_sum)
7004{
7005 struct inode *inode = dip->inode;
7006 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007007 struct bio *bio;
7008 struct bio *orig_bio = dip->orig_bio;
7009 struct bio_vec *bvec = orig_bio->bi_io_vec;
7010 u64 start_sector = orig_bio->bi_sector;
7011 u64 file_offset = dip->logical_offset;
7012 u64 submit_len = 0;
7013 u64 map_length;
7014 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007015 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007016 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007017
Miao Xiee65e1532010-11-22 03:04:43 +00007018 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007019 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007020 &map_length, NULL, 0);
7021 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007022 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007023 return -EIO;
7024 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007025 if (map_length >= orig_bio->bi_size) {
7026 bio = orig_bio;
7027 goto submit;
7028 }
7029
David Woodhouse53b381b2013-01-29 18:40:14 -05007030 /* async crcs make it difficult to collect full stripe writes. */
7031 if (btrfs_get_alloc_profile(root, 1) &
7032 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7033 async_submit = 0;
7034 else
7035 async_submit = 1;
7036
Josef Bacik02f57c72011-04-06 14:25:44 -04007037 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7038 if (!bio)
7039 return -ENOMEM;
7040 bio->bi_private = dip;
7041 bio->bi_end_io = btrfs_end_dio_bio;
7042 atomic_inc(&dip->pending_bios);
7043
Miao Xiee65e1532010-11-22 03:04:43 +00007044 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7045 if (unlikely(map_length < submit_len + bvec->bv_len ||
7046 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7047 bvec->bv_offset) < bvec->bv_len)) {
7048 /*
7049 * inc the count before we submit the bio so
7050 * we know the end IO handler won't happen before
7051 * we inc the count. Otherwise, the dip might get freed
7052 * before we're done setting it up
7053 */
7054 atomic_inc(&dip->pending_bios);
7055 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7056 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007057 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007058 if (ret) {
7059 bio_put(bio);
7060 atomic_dec(&dip->pending_bios);
7061 goto out_err;
7062 }
7063
Miao Xiee65e1532010-11-22 03:04:43 +00007064 start_sector += submit_len >> 9;
7065 file_offset += submit_len;
7066
7067 submit_len = 0;
7068 nr_pages = 0;
7069
7070 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7071 start_sector, GFP_NOFS);
7072 if (!bio)
7073 goto out_err;
7074 bio->bi_private = dip;
7075 bio->bi_end_io = btrfs_end_dio_bio;
7076
7077 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007078 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007079 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007080 &map_length, NULL, 0);
7081 if (ret) {
7082 bio_put(bio);
7083 goto out_err;
7084 }
7085 } else {
7086 submit_len += bvec->bv_len;
7087 nr_pages ++;
7088 bvec++;
7089 }
7090 }
7091
Josef Bacik02f57c72011-04-06 14:25:44 -04007092submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007093 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007094 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007095 if (!ret)
7096 return 0;
7097
7098 bio_put(bio);
7099out_err:
7100 dip->errors = 1;
7101 /*
7102 * before atomic variable goto zero, we must
7103 * make sure dip->errors is perceived to be set.
7104 */
7105 smp_mb__before_atomic_dec();
7106 if (atomic_dec_and_test(&dip->pending_bios))
7107 bio_io_error(dip->orig_bio);
7108
7109 /* bio_end_io() will handle error, so we needn't return it */
7110 return 0;
7111}
7112
Chris Mason9be33952013-05-17 18:30:14 -04007113static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7114 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007115{
7116 struct btrfs_root *root = BTRFS_I(inode)->root;
7117 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007118 struct bio_vec *bvec = dio_bio->bi_io_vec;
7119 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007120 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007121 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007122 int ret = 0;
7123
7124 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7125
Chris Mason9be33952013-05-17 18:30:14 -04007126 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7127
7128 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007129 ret = -ENOMEM;
7130 goto free_ordered;
7131 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007132
Chris Mason9be33952013-05-17 18:30:14 -04007133 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7134 if (!dip) {
7135 ret = -ENOMEM;
7136 goto free_io_bio;
7137 }
7138
7139 dip->private = dio_bio->bi_private;
7140 io_bio->bi_private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007141 dip->inode = inode;
7142 dip->logical_offset = file_offset;
7143
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144 dip->bytes = 0;
7145 do {
7146 dip->bytes += bvec->bv_len;
7147 bvec++;
Chris Mason9be33952013-05-17 18:30:14 -04007148 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007149
Chris Mason9be33952013-05-17 18:30:14 -04007150 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7151 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007152 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007153 dip->orig_bio = io_bio;
7154 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007155 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007156
7157 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007158 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007159 else
Chris Mason9be33952013-05-17 18:30:14 -04007160 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007161
Miao Xiee65e1532010-11-22 03:04:43 +00007162 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7163 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007164 return;
Chris Mason9be33952013-05-17 18:30:14 -04007165
7166free_io_bio:
7167 bio_put(io_bio);
7168
Josef Bacik4b46fce2010-05-23 11:00:55 -04007169free_ordered:
7170 /*
7171 * If this is a write, we need to clean up the reserved space and kill
7172 * the ordered extent.
7173 */
7174 if (write) {
7175 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007176 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007177 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7178 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7179 btrfs_free_reserved_extent(root, ordered->start,
7180 ordered->disk_len);
7181 btrfs_put_ordered_extent(ordered);
7182 btrfs_put_ordered_extent(ordered);
7183 }
Chris Mason9be33952013-05-17 18:30:14 -04007184 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007185}
7186
Chris Mason5a5f79b2010-05-26 21:33:37 -04007187static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7188 const struct iovec *iov, loff_t offset,
7189 unsigned long nr_segs)
7190{
7191 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007192 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007193 size_t size;
7194 unsigned long addr;
7195 unsigned blocksize_mask = root->sectorsize - 1;
7196 ssize_t retval = -EINVAL;
7197 loff_t end = offset;
7198
7199 if (offset & blocksize_mask)
7200 goto out;
7201
7202 /* Check the memory alignment. Blocks cannot straddle pages */
7203 for (seg = 0; seg < nr_segs; seg++) {
7204 addr = (unsigned long)iov[seg].iov_base;
7205 size = iov[seg].iov_len;
7206 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007207 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007208 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007209
7210 /* If this is a write we don't need to check anymore */
7211 if (rw & WRITE)
7212 continue;
7213
7214 /*
7215 * Check to make sure we don't have duplicate iov_base's in this
7216 * iovec, if so return EINVAL, otherwise we'll get csum errors
7217 * when reading back.
7218 */
7219 for (i = seg + 1; i < nr_segs; i++) {
7220 if (iov[seg].iov_base == iov[i].iov_base)
7221 goto out;
7222 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007223 }
7224 retval = 0;
7225out:
7226 return retval;
7227}
Josef Bacikeb838e72012-07-31 16:28:48 -04007228
Chris Mason16432982008-04-10 10:23:21 -04007229static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7230 const struct iovec *iov, loff_t offset,
7231 unsigned long nr_segs)
7232{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007233 struct file *file = iocb->ki_filp;
7234 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007235 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007236 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007237 bool wakeup = true;
7238 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007239 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240
Chris Mason5a5f79b2010-05-26 21:33:37 -04007241 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007242 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007243 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007244
Miao Xie38851cc2013-02-08 07:04:11 +00007245 atomic_inc(&inode->i_dio_count);
7246 smp_mb__after_atomic_inc();
7247
Miao Xie09348562013-02-07 10:12:07 +00007248 if (rw & WRITE) {
7249 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007250 /*
7251 * If the write DIO is beyond the EOF, we need update
7252 * the isize, but it is protected by i_mutex. So we can
7253 * not unlock the i_mutex at this case.
7254 */
7255 if (offset + count <= inode->i_size) {
7256 mutex_unlock(&inode->i_mutex);
7257 relock = true;
7258 }
Miao Xie09348562013-02-07 10:12:07 +00007259 ret = btrfs_delalloc_reserve_space(inode, count);
7260 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007261 goto out;
7262 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7263 &BTRFS_I(inode)->runtime_flags))) {
7264 inode_dio_done(inode);
7265 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7266 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007267 }
7268
7269 ret = __blockdev_direct_IO(rw, iocb, inode,
7270 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7271 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007272 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007273 if (rw & WRITE) {
7274 if (ret < 0 && ret != -EIOCBQUEUED)
7275 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007276 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007277 btrfs_delalloc_release_space(inode,
7278 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007279 else
7280 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007281 }
Miao Xie38851cc2013-02-08 07:04:11 +00007282out:
Miao Xie2e60a512013-02-08 07:01:08 +00007283 if (wakeup)
7284 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007285 if (relock)
7286 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007287
7288 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007289}
7290
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007291#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7292
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007293static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7294 __u64 start, __u64 len)
7295{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007296 int ret;
7297
7298 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7299 if (ret)
7300 return ret;
7301
Chris Masonec29ed52011-02-23 16:23:20 -05007302 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007303}
7304
Chris Mason9ebefb182007-06-15 13:50:00 -04007305int btrfs_readpage(struct file *file, struct page *page)
7306{
Chris Masond1310b22008-01-24 16:13:08 -05007307 struct extent_io_tree *tree;
7308 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007309 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007310}
Chris Mason1832a6d2007-12-21 16:27:21 -05007311
Chris Mason39279cc2007-06-12 06:35:45 -04007312static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7313{
Chris Masond1310b22008-01-24 16:13:08 -05007314 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007315
7316
7317 if (current->flags & PF_MEMALLOC) {
7318 redirty_page_for_writepage(wbc, page);
7319 unlock_page(page);
7320 return 0;
7321 }
Chris Masond1310b22008-01-24 16:13:08 -05007322 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007323 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7324}
Chris Mason39279cc2007-06-12 06:35:45 -04007325
Eric Sandeen48a3b632013-04-25 20:41:01 +00007326static int btrfs_writepages(struct address_space *mapping,
7327 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007328{
Chris Masond1310b22008-01-24 16:13:08 -05007329 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007330
Chris Masond1310b22008-01-24 16:13:08 -05007331 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007332 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7333}
7334
Chris Mason3ab2fb52007-11-08 10:59:22 -05007335static int
7336btrfs_readpages(struct file *file, struct address_space *mapping,
7337 struct list_head *pages, unsigned nr_pages)
7338{
Chris Masond1310b22008-01-24 16:13:08 -05007339 struct extent_io_tree *tree;
7340 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007341 return extent_readpages(tree, mapping, pages, nr_pages,
7342 btrfs_get_extent);
7343}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007344static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007345{
Chris Masond1310b22008-01-24 16:13:08 -05007346 struct extent_io_tree *tree;
7347 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007348 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007349
Chris Masond1310b22008-01-24 16:13:08 -05007350 tree = &BTRFS_I(page->mapping->host)->io_tree;
7351 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007352 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007353 if (ret == 1) {
7354 ClearPagePrivate(page);
7355 set_page_private(page, 0);
7356 page_cache_release(page);
7357 }
7358 return ret;
7359}
Chris Mason39279cc2007-06-12 06:35:45 -04007360
Chris Masone6dcd2d2008-07-17 12:53:50 -04007361static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7362{
Chris Mason98509cf2008-09-11 15:51:43 -04007363 if (PageWriteback(page) || PageDirty(page))
7364 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007365 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007366}
7367
Chris Masona52d9a82007-08-27 16:49:44 -04007368static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7369{
Josef Bacik5fd02042012-05-02 14:00:54 -04007370 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007371 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007372 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007373 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007374 u64 page_start = page_offset(page);
7375 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007376
Chris Mason8b62b722009-09-02 16:53:46 -04007377 /*
7378 * we have the page locked, so new writeback can't start,
7379 * and the dirty bit won't be cleared while we are here.
7380 *
7381 * Wait for IO on this page so that we can safely clear
7382 * the PagePrivate2 bit and do ordered accounting
7383 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007384 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007385
Josef Bacik5fd02042012-05-02 14:00:54 -04007386 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007387 if (offset) {
7388 btrfs_releasepage(page, GFP_NOFS);
7389 return;
7390 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007391 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007392 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007393 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007394 /*
7395 * IO on this page will never be started, so we need
7396 * to account for any ordered extents now
7397 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007398 clear_extent_bit(tree, page_start, page_end,
7399 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007400 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7401 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007402 /*
7403 * whoever cleared the private bit is responsible
7404 * for the finish_ordered_io
7405 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007406 if (TestClearPagePrivate2(page) &&
7407 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7408 PAGE_CACHE_SIZE, 1)) {
7409 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007410 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007411 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007412 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007413 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007414 }
7415 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007416 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007417 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7418 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007419 __btrfs_releasepage(page, GFP_NOFS);
7420
Chris Mason4a096752008-07-21 10:29:44 -04007421 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007422 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007423 ClearPagePrivate(page);
7424 set_page_private(page, 0);
7425 page_cache_release(page);
7426 }
Chris Mason39279cc2007-06-12 06:35:45 -04007427}
7428
Chris Mason9ebefb182007-06-15 13:50:00 -04007429/*
7430 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7431 * called from a page fault handler when a page is first dirtied. Hence we must
7432 * be careful to check for EOF conditions here. We set the page up correctly
7433 * for a written page which means we get ENOSPC checking when writing into
7434 * holes and correct delalloc and unwritten extent mapping on filesystems that
7435 * support these features.
7436 *
7437 * We are not allowed to take the i_mutex here so we have to play games to
7438 * protect against truncate races as the page could now be beyond EOF. Because
7439 * vmtruncate() writes the inode size before removing pages, once we have the
7440 * page lock we can determine safely if the page is beyond EOF. If it is not
7441 * beyond EOF, then the page is guaranteed safe against truncation until we
7442 * unlock the page.
7443 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007444int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007445{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007446 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007447 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007448 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007449 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7450 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007451 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007452 char *kaddr;
7453 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007454 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007455 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007456 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007457 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007458 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007459
Jan Karab2b5ef52012-06-12 16:20:45 +02007460 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007461 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007462 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007463 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007464 reserved = 1;
7465 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007466 if (ret) {
7467 if (ret == -ENOMEM)
7468 ret = VM_FAULT_OOM;
7469 else /* -ENOSPC, -EIO, etc */
7470 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007471 if (reserved)
7472 goto out;
7473 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007474 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007475
Nick Piggin56a76f82009-03-31 15:23:23 -07007476 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007477again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007478 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007479 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007480 page_start = page_offset(page);
7481 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007482
Chris Mason9ebefb182007-06-15 13:50:00 -04007483 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007484 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007485 /* page got truncated out from underneath us */
7486 goto out_unlock;
7487 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007488 wait_on_page_writeback(page);
7489
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007490 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007491 set_page_extent_mapped(page);
7492
Chris Masoneb84ae02008-07-17 13:53:27 -04007493 /*
7494 * we can't set the delalloc bits if there are pending ordered
7495 * extents. Drop our locks and wait for them to finish
7496 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007497 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7498 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007499 unlock_extent_cached(io_tree, page_start, page_end,
7500 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007501 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007502 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007503 btrfs_put_ordered_extent(ordered);
7504 goto again;
7505 }
7506
Josef Bacikfbf19082009-10-01 17:10:23 -04007507 /*
7508 * XXX - page_mkwrite gets called every time the page is dirtied, even
7509 * if it was already dirty, so for space accounting reasons we need to
7510 * clear any delalloc bits for the range we are fixing to save. There
7511 * is probably a better way to do this, but for now keep consistent with
7512 * prepare_pages in the normal write path.
7513 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007514 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007515 EXTENT_DIRTY | EXTENT_DELALLOC |
7516 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007517 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007518
Josef Bacik2ac55d42010-02-03 19:33:23 +00007519 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7520 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007521 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007522 unlock_extent_cached(io_tree, page_start, page_end,
7523 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007524 ret = VM_FAULT_SIGBUS;
7525 goto out_unlock;
7526 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007527 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007528
7529 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007530 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007531 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007532 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007534
Chris Masone6dcd2d2008-07-17 12:53:50 -04007535 if (zero_start != PAGE_CACHE_SIZE) {
7536 kaddr = kmap(page);
7537 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7538 flush_dcache_page(page);
7539 kunmap(page);
7540 }
Chris Mason247e7432008-07-17 12:53:51 -04007541 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007542 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007543 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007544
Chris Mason257c62e2009-10-13 13:21:08 -04007545 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7546 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007547 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007548
Josef Bacik2ac55d42010-02-03 19:33:23 +00007549 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007550
7551out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007552 if (!ret) {
7553 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007554 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007555 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007556 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007557out:
Josef Bacikec39e182012-01-12 19:10:12 -05007558 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007559out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007560 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007561 return ret;
7562}
7563
Josef Bacika41ad392011-01-31 15:30:16 -05007564static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007565{
7566 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007567 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007568 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007569 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007570 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007571 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007572 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007573
Josef Bacik2aaa6652012-08-29 14:27:18 -04007574 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007575 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007576 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007577
Chris Mason4a096752008-07-21 10:29:44 -04007578 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007579 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007580
Josef Bacikfcb80c22011-05-03 10:40:22 -04007581 /*
7582 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7583 * 3 things going on here
7584 *
7585 * 1) We need to reserve space for our orphan item and the space to
7586 * delete our orphan item. Lord knows we don't want to have a dangling
7587 * orphan item because we didn't reserve space to remove it.
7588 *
7589 * 2) We need to reserve space to update our inode.
7590 *
7591 * 3) We need to have something to cache all the space that is going to
7592 * be free'd up by the truncate operation, but also have some slack
7593 * space reserved in case it uses space during the truncate (thank you
7594 * very much snapshotting).
7595 *
7596 * And we need these to all be seperate. The fact is we can use alot of
7597 * space doing the truncate, and we have no earthly idea how much space
7598 * we will use, so we need the truncate reservation to be seperate so it
7599 * doesn't end up using space reserved for updating the inode or
7600 * removing the orphan item. We also need to be able to stop the
7601 * transaction and start a new one, which means we need to be able to
7602 * update the inode several times, and we have no idea of knowing how
7603 * many times that will be, so we can't just reserve 1 item for the
7604 * entirety of the opration, so that has to be done seperately as well.
7605 * Then there is the orphan item, which does indeed need to be held on
7606 * to for the whole operation, and we need nobody to touch this reserved
7607 * space except the orphan code.
7608 *
7609 * So that leaves us with
7610 *
7611 * 1) root->orphan_block_rsv - for the orphan deletion.
7612 * 2) rsv - for the truncate reservation, which we will steal from the
7613 * transaction reservation.
7614 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7615 * updating the inode.
7616 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007617 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007618 if (!rsv)
7619 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007620 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007621 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007622
Josef Bacik907cbce2011-08-08 13:46:15 -04007623 /*
Josef Bacik07127182011-08-19 10:29:59 -04007624 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007625 * 1 for updating the inode.
7626 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007627 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007628 if (IS_ERR(trans)) {
7629 err = PTR_ERR(trans);
7630 goto out;
7631 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007632
Josef Bacik907cbce2011-08-08 13:46:15 -04007633 /* Migrate the slack space for the truncate to our reserve */
7634 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7635 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007636 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007637
Chris Mason5a3f23d2009-03-31 13:27:11 -04007638 /*
7639 * setattr is responsible for setting the ordered_data_close flag,
7640 * but that is only tested during the last file release. That
7641 * could happen well after the next commit, leaving a great big
7642 * window where new writes may get lost if someone chooses to write
7643 * to this file after truncating to zero
7644 *
7645 * The inode doesn't have any dirty data here, and so if we commit
7646 * this is a noop. If someone immediately starts writing to the inode
7647 * it is very likely we'll catch some of their writes in this
7648 * transaction, and the commit will find this file on the ordered
7649 * data list with good things to send down.
7650 *
7651 * This is a best effort solution, there is still a window where
7652 * using truncate to replace the contents of the file will
7653 * end up with a zero length file after a crash.
7654 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007655 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7656 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007657 btrfs_add_ordered_operation(trans, root, inode);
7658
Josef Bacik5dc562c2012-08-17 13:14:17 -04007659 /*
7660 * So if we truncate and then write and fsync we normally would just
7661 * write the extents that changed, which is a problem if we need to
7662 * first truncate that entire inode. So set this flag so we write out
7663 * all of the extents in the inode to the sync log so we're completely
7664 * safe.
7665 */
7666 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007667 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007668
Yan, Zheng80825102009-11-12 09:35:36 +00007669 while (1) {
7670 ret = btrfs_truncate_inode_items(trans, root, inode,
7671 inode->i_size,
7672 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007673 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007674 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007675 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007676 }
Chris Mason39279cc2007-06-12 06:35:45 -04007677
Josef Bacikfcb80c22011-05-03 10:40:22 -04007678 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007679 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007680 if (ret) {
7681 err = ret;
7682 break;
7683 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007684
Yan, Zheng80825102009-11-12 09:35:36 +00007685 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007686 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007687
7688 trans = btrfs_start_transaction(root, 2);
7689 if (IS_ERR(trans)) {
7690 ret = err = PTR_ERR(trans);
7691 trans = NULL;
7692 break;
7693 }
7694
7695 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7696 rsv, min_size);
7697 BUG_ON(ret); /* shouldn't happen */
7698 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007699 }
7700
7701 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007702 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007703 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007704 if (ret)
7705 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007706 }
7707
Chris Mason917c16b2011-11-08 14:49:59 -05007708 if (trans) {
7709 trans->block_rsv = &root->fs_info->trans_block_rsv;
7710 ret = btrfs_update_inode(trans, root, inode);
7711 if (ret && !err)
7712 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007713
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007714 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007715 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007716 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007717
7718out:
7719 btrfs_free_block_rsv(root, rsv);
7720
Josef Bacik3893e332011-01-31 16:03:11 -05007721 if (ret && !err)
7722 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007723
Josef Bacik3893e332011-01-31 16:03:11 -05007724 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007725}
7726
Sven Wegener3b963622008-06-09 21:57:42 -04007727/*
Chris Masond352ac62008-09-29 15:18:18 -04007728 * create a new subvolume directory/inode (helper for the ioctl).
7729 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007730int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007731 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007732{
Chris Mason39279cc2007-06-12 06:35:45 -04007733 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007734 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007735 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007736
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007737 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7738 new_dirid, new_dirid,
7739 S_IFDIR | (~current_umask() & S_IRWXUGO),
7740 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007741 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007742 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007743 inode->i_op = &btrfs_dir_inode_operations;
7744 inode->i_fop = &btrfs_dir_file_operations;
7745
Miklos Szeredibfe86842011-10-28 14:13:29 +02007746 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007747 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007748
Yan, Zheng76dda932009-09-21 16:00:26 -04007749 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007750
Yan, Zheng76dda932009-09-21 16:00:26 -04007751 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007752 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007753}
7754
Chris Mason39279cc2007-06-12 06:35:45 -04007755struct inode *btrfs_alloc_inode(struct super_block *sb)
7756{
7757 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007758 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007759
7760 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7761 if (!ei)
7762 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007763
7764 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007765 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007766 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007767 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007768 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007769 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007770 ei->disk_i_size = 0;
7771 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007772 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007773 ei->index_cnt = (u64)-1;
7774 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007775 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007776
Josef Bacik9e0baf62011-07-15 15:16:44 +00007777 spin_lock_init(&ei->lock);
7778 ei->outstanding_extents = 0;
7779 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007780
Josef Bacik72ac3c02012-05-23 14:13:11 -04007781 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007782 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007783
Miao Xie16cdcec2011-04-22 18:12:22 +08007784 ei->delayed_node = NULL;
7785
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007786 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007787 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007788 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7789 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007790 ei->io_tree.track_uptodate = 1;
7791 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007792 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007793 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007794 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007795 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007796 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007797 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007798 RB_CLEAR_NODE(&ei->rb_node);
7799
7800 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007801}
7802
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007803static void btrfs_i_callback(struct rcu_head *head)
7804{
7805 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007806 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7807}
7808
Chris Mason39279cc2007-06-12 06:35:45 -04007809void btrfs_destroy_inode(struct inode *inode)
7810{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007811 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007812 struct btrfs_root *root = BTRFS_I(inode)->root;
7813
Al Virob3d9b7a2012-06-09 13:51:19 -04007814 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007815 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007816 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7817 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007818 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7819 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007820
Chris Mason5a3f23d2009-03-31 13:27:11 -04007821 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007822 * This can happen where we create an inode, but somebody else also
7823 * created the same inode and we need to destroy the one we already
7824 * created.
7825 */
7826 if (!root)
7827 goto free;
7828
7829 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007830 * Make sure we're properly removed from the ordered operation
7831 * lists.
7832 */
7833 smp_mb();
7834 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007835 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007836 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007837 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007838 }
7839
Josef Bacik8a35d952012-05-23 14:26:42 -04007840 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7841 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007842 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7843 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007844 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007845 }
Josef Bacik7b128762008-07-24 12:17:14 -04007846
Chris Masond3977122009-01-05 21:25:51 -05007847 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007848 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7849 if (!ordered)
7850 break;
7851 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007852 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7853 (unsigned long long)ordered->file_offset,
7854 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007855 btrfs_remove_ordered_extent(inode, ordered);
7856 btrfs_put_ordered_extent(ordered);
7857 btrfs_put_ordered_extent(ordered);
7858 }
7859 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007860 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007861 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007862free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007863 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007864}
7865
Al Viro45321ac2010-06-07 13:43:19 -04007866int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007867{
7868 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007869
Naohiro Aota6379ef92013-06-06 09:56:34 +00007870 if (root == NULL)
7871 return 1;
7872
Liu Bofa6ac872013-02-20 14:10:23 +00007873 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007874 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007875 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007876 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007877 else
Al Viro45321ac2010-06-07 13:43:19 -04007878 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007879}
7880
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007881static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007882{
7883 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7884
7885 inode_init_once(&ei->vfs_inode);
7886}
7887
7888void btrfs_destroy_cachep(void)
7889{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007890 /*
7891 * Make sure all delayed rcu free inodes are flushed before we
7892 * destroy cache.
7893 */
7894 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007895 if (btrfs_inode_cachep)
7896 kmem_cache_destroy(btrfs_inode_cachep);
7897 if (btrfs_trans_handle_cachep)
7898 kmem_cache_destroy(btrfs_trans_handle_cachep);
7899 if (btrfs_transaction_cachep)
7900 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007901 if (btrfs_path_cachep)
7902 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007903 if (btrfs_free_space_cachep)
7904 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007905 if (btrfs_delalloc_work_cachep)
7906 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007907}
7908
7909int btrfs_init_cachep(void)
7910{
David Sterba837e1972012-09-07 03:00:48 -06007911 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007912 sizeof(struct btrfs_inode), 0,
7913 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007914 if (!btrfs_inode_cachep)
7915 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007916
David Sterba837e1972012-09-07 03:00:48 -06007917 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007918 sizeof(struct btrfs_trans_handle), 0,
7919 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007920 if (!btrfs_trans_handle_cachep)
7921 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007922
David Sterba837e1972012-09-07 03:00:48 -06007923 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007924 sizeof(struct btrfs_transaction), 0,
7925 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007926 if (!btrfs_transaction_cachep)
7927 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007928
David Sterba837e1972012-09-07 03:00:48 -06007929 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007930 sizeof(struct btrfs_path), 0,
7931 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007932 if (!btrfs_path_cachep)
7933 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007934
David Sterba837e1972012-09-07 03:00:48 -06007935 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007936 sizeof(struct btrfs_free_space), 0,
7937 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7938 if (!btrfs_free_space_cachep)
7939 goto fail;
7940
Miao Xie8ccf6f192012-10-25 09:28:04 +00007941 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7942 sizeof(struct btrfs_delalloc_work), 0,
7943 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7944 NULL);
7945 if (!btrfs_delalloc_work_cachep)
7946 goto fail;
7947
Chris Mason39279cc2007-06-12 06:35:45 -04007948 return 0;
7949fail:
7950 btrfs_destroy_cachep();
7951 return -ENOMEM;
7952}
7953
7954static int btrfs_getattr(struct vfsmount *mnt,
7955 struct dentry *dentry, struct kstat *stat)
7956{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007957 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007958 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007959 u32 blocksize = inode->i_sb->s_blocksize;
7960
Chris Mason39279cc2007-06-12 06:35:45 -04007961 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007962 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007963 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007964
7965 spin_lock(&BTRFS_I(inode)->lock);
7966 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7967 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007968 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007969 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007970 return 0;
7971}
7972
Chris Masond3977122009-01-05 21:25:51 -05007973static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7974 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007975{
7976 struct btrfs_trans_handle *trans;
7977 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007978 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007979 struct inode *new_inode = new_dentry->d_inode;
7980 struct inode *old_inode = old_dentry->d_inode;
7981 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007982 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007983 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007984 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007985 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007986
Li Zefan33345d012011-04-20 10:31:50 +08007987 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007988 return -EPERM;
7989
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007990 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007991 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007992 return -EXDEV;
7993
Li Zefan33345d012011-04-20 10:31:50 +08007994 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7995 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007996 return -ENOTEMPTY;
7997
Chris Mason39279cc2007-06-12 06:35:45 -04007998 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007999 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008000 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008001
8002
8003 /* check for collisions, even if the name isn't there */
8004 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8005 new_dentry->d_name.name,
8006 new_dentry->d_name.len);
8007
8008 if (ret) {
8009 if (ret == -EEXIST) {
8010 /* we shouldn't get
8011 * eexist without a new_inode */
8012 if (!new_inode) {
8013 WARN_ON(1);
8014 return ret;
8015 }
8016 } else {
8017 /* maybe -EOVERFLOW */
8018 return ret;
8019 }
8020 }
8021 ret = 0;
8022
Chris Mason5a3f23d2009-03-31 13:27:11 -04008023 /*
8024 * we're using rename to replace one file with another.
8025 * and the replacement file is large. Start IO on it now so
8026 * we don't add too much work to the end of the transaction
8027 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008028 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008029 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8030 filemap_flush(old_inode->i_mapping);
8031
Yan, Zheng76dda932009-09-21 16:00:26 -04008032 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008033 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008034 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008035 /*
8036 * We want to reserve the absolute worst case amount of items. So if
8037 * both inodes are subvols and we need to unlink them then that would
8038 * require 4 item modifications, but if they are both normal inodes it
8039 * would require 5 item modifications, so we'll assume their normal
8040 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8041 * should cover the worst case number of items we'll modify.
8042 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008043 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008044 if (IS_ERR(trans)) {
8045 ret = PTR_ERR(trans);
8046 goto out_notrans;
8047 }
Chris Mason5f39d392007-10-15 16:14:19 -04008048
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008049 if (dest != root)
8050 btrfs_record_root_in_trans(trans, dest);
8051
Yan, Zhenga5719522009-09-24 09:17:31 -04008052 ret = btrfs_set_inode_index(new_dir, &index);
8053 if (ret)
8054 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008055
Li Zefan33345d012011-04-20 10:31:50 +08008056 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008057 /* force full log commit if subvolume involved. */
8058 root->fs_info->last_trans_log_full_commit = trans->transid;
8059 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008060 ret = btrfs_insert_inode_ref(trans, dest,
8061 new_dentry->d_name.name,
8062 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008063 old_ino,
8064 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008065 if (ret)
8066 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008067 /*
8068 * this is an ugly little race, but the rename is required
8069 * to make sure that if we crash, the inode is either at the
8070 * old name or the new one. pinning the log transaction lets
8071 * us make sure we don't allow a log commit to come in after
8072 * we unlink the name but before we add the new name back in.
8073 */
8074 btrfs_pin_log_trans(root);
8075 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008076 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008077 * make sure the inode gets flushed if it is replacing
8078 * something.
8079 */
Li Zefan33345d012011-04-20 10:31:50 +08008080 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008081 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008082
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008083 inode_inc_iversion(old_dir);
8084 inode_inc_iversion(new_dir);
8085 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008086 old_dir->i_ctime = old_dir->i_mtime = ctime;
8087 new_dir->i_ctime = new_dir->i_mtime = ctime;
8088 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008089
Chris Mason12fcfd22009-03-24 10:24:20 -04008090 if (old_dentry->d_parent != new_dentry->d_parent)
8091 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8092
Li Zefan33345d012011-04-20 10:31:50 +08008093 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008094 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8095 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8096 old_dentry->d_name.name,
8097 old_dentry->d_name.len);
8098 } else {
Al Viro92986792011-03-04 17:14:37 +00008099 ret = __btrfs_unlink_inode(trans, root, old_dir,
8100 old_dentry->d_inode,
8101 old_dentry->d_name.name,
8102 old_dentry->d_name.len);
8103 if (!ret)
8104 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008105 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008106 if (ret) {
8107 btrfs_abort_transaction(trans, root, ret);
8108 goto out_fail;
8109 }
Chris Mason39279cc2007-06-12 06:35:45 -04008110
8111 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008112 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008113 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008114 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008115 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8116 root_objectid = BTRFS_I(new_inode)->location.objectid;
8117 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8118 root_objectid,
8119 new_dentry->d_name.name,
8120 new_dentry->d_name.len);
8121 BUG_ON(new_inode->i_nlink == 0);
8122 } else {
8123 ret = btrfs_unlink_inode(trans, dest, new_dir,
8124 new_dentry->d_inode,
8125 new_dentry->d_name.name,
8126 new_dentry->d_name.len);
8127 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008128 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008129 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008130 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008131 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008132 if (ret) {
8133 btrfs_abort_transaction(trans, root, ret);
8134 goto out_fail;
8135 }
Chris Mason39279cc2007-06-12 06:35:45 -04008136 }
Josef Bacikaec74772008-07-24 12:12:38 -04008137
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008138 ret = btrfs_add_link(trans, new_dir, old_inode,
8139 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008140 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008141 if (ret) {
8142 btrfs_abort_transaction(trans, root, ret);
8143 goto out_fail;
8144 }
Chris Mason39279cc2007-06-12 06:35:45 -04008145
Li Zefan33345d012011-04-20 10:31:50 +08008146 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008147 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008148 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008149 btrfs_end_log_trans(root);
8150 }
Chris Mason39279cc2007-06-12 06:35:45 -04008151out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008152 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008153out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008154 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008155 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008156
Chris Mason39279cc2007-06-12 06:35:45 -04008157 return ret;
8158}
8159
Miao Xie8ccf6f192012-10-25 09:28:04 +00008160static void btrfs_run_delalloc_work(struct btrfs_work *work)
8161{
8162 struct btrfs_delalloc_work *delalloc_work;
8163
8164 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8165 work);
8166 if (delalloc_work->wait)
8167 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8168 else
8169 filemap_flush(delalloc_work->inode->i_mapping);
8170
8171 if (delalloc_work->delay_iput)
8172 btrfs_add_delayed_iput(delalloc_work->inode);
8173 else
8174 iput(delalloc_work->inode);
8175 complete(&delalloc_work->completion);
8176}
8177
8178struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8179 int wait, int delay_iput)
8180{
8181 struct btrfs_delalloc_work *work;
8182
8183 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8184 if (!work)
8185 return NULL;
8186
8187 init_completion(&work->completion);
8188 INIT_LIST_HEAD(&work->list);
8189 work->inode = inode;
8190 work->wait = wait;
8191 work->delay_iput = delay_iput;
8192 work->work.func = btrfs_run_delalloc_work;
8193
8194 return work;
8195}
8196
8197void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8198{
8199 wait_for_completion(&work->completion);
8200 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8201}
8202
Chris Masond352ac62008-09-29 15:18:18 -04008203/*
8204 * some fairly slow code that needs optimization. This walks the list
8205 * of all the inodes with pending delalloc and forces them to disk.
8206 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008207static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008208{
Chris Masonea8c2812008-08-04 23:17:27 -04008209 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008210 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008211 struct btrfs_delalloc_work *work, *next;
8212 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008213 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008214 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008215
Miao Xie8ccf6f192012-10-25 09:28:04 +00008216 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008217 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008218
Miao Xieeb73c1b2013-05-15 07:48:22 +00008219 spin_lock(&root->delalloc_lock);
8220 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008221 while (!list_empty(&splice)) {
8222 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008223 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008224
Miao Xieeb73c1b2013-05-15 07:48:22 +00008225 list_move_tail(&binode->delalloc_inodes,
8226 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008227 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008228 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008229 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008230 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008231 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008232 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008233
8234 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8235 if (unlikely(!work)) {
8236 ret = -ENOMEM;
8237 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008238 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008239 list_add_tail(&work->list, &works);
8240 btrfs_queue_worker(&root->fs_info->flush_workers,
8241 &work->work);
8242
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008243 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008244 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008245 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008246 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008247
Miao Xie1eafa6c2013-01-22 10:49:00 +00008248 list_for_each_entry_safe(work, next, &works, list) {
8249 list_del_init(&work->list);
8250 btrfs_wait_and_free_delalloc_work(work);
8251 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008252 return 0;
8253out:
8254 list_for_each_entry_safe(work, next, &works, list) {
8255 list_del_init(&work->list);
8256 btrfs_wait_and_free_delalloc_work(work);
8257 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008258
Miao Xieeb73c1b2013-05-15 07:48:22 +00008259 if (!list_empty_careful(&splice)) {
8260 spin_lock(&root->delalloc_lock);
8261 list_splice_tail(&splice, &root->delalloc_inodes);
8262 spin_unlock(&root->delalloc_lock);
8263 }
8264 return ret;
8265}
8266
8267int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8268{
8269 int ret;
8270
8271 if (root->fs_info->sb->s_flags & MS_RDONLY)
8272 return -EROFS;
8273
8274 ret = __start_delalloc_inodes(root, delay_iput);
8275 /*
8276 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008277 * we have to make sure the IO is actually started and that
8278 * ordered extents get created before we return
8279 */
8280 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008281 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008282 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008283 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008284 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8285 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008286 }
8287 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008288 return ret;
8289}
8290
8291int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8292 int delay_iput)
8293{
8294 struct btrfs_root *root;
8295 struct list_head splice;
8296 int ret;
8297
8298 if (fs_info->sb->s_flags & MS_RDONLY)
8299 return -EROFS;
8300
8301 INIT_LIST_HEAD(&splice);
8302
8303 spin_lock(&fs_info->delalloc_root_lock);
8304 list_splice_init(&fs_info->delalloc_roots, &splice);
8305 while (!list_empty(&splice)) {
8306 root = list_first_entry(&splice, struct btrfs_root,
8307 delalloc_root);
8308 root = btrfs_grab_fs_root(root);
8309 BUG_ON(!root);
8310 list_move_tail(&root->delalloc_root,
8311 &fs_info->delalloc_roots);
8312 spin_unlock(&fs_info->delalloc_root_lock);
8313
8314 ret = __start_delalloc_inodes(root, delay_iput);
8315 btrfs_put_fs_root(root);
8316 if (ret)
8317 goto out;
8318
8319 spin_lock(&fs_info->delalloc_root_lock);
8320 }
8321 spin_unlock(&fs_info->delalloc_root_lock);
8322
8323 atomic_inc(&fs_info->async_submit_draining);
8324 while (atomic_read(&fs_info->nr_async_submits) ||
8325 atomic_read(&fs_info->async_delalloc_pages)) {
8326 wait_event(fs_info->async_submit_wait,
8327 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8328 atomic_read(&fs_info->async_delalloc_pages) == 0));
8329 }
8330 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008331 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008332out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008333 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008334 spin_lock(&fs_info->delalloc_root_lock);
8335 list_splice_tail(&splice, &fs_info->delalloc_roots);
8336 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008337 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008338 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008339}
8340
Chris Mason39279cc2007-06-12 06:35:45 -04008341static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8342 const char *symname)
8343{
8344 struct btrfs_trans_handle *trans;
8345 struct btrfs_root *root = BTRFS_I(dir)->root;
8346 struct btrfs_path *path;
8347 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008348 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008349 int err;
8350 int drop_inode = 0;
8351 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008352 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008353 int name_len;
8354 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008355 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008356 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008357 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008358
8359 name_len = strlen(symname) + 1;
8360 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8361 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008362
Josef Bacik9ed74f22009-09-11 16:12:44 -04008363 /*
8364 * 2 items for inode item and ref
8365 * 2 items for dir items
8366 * 1 item for xattr if selinux is on
8367 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008368 trans = btrfs_start_transaction(root, 5);
8369 if (IS_ERR(trans))
8370 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008371
Li Zefan581bb052011-04-20 10:06:11 +08008372 err = btrfs_find_free_ino(root, &objectid);
8373 if (err)
8374 goto out_unlock;
8375
Josef Bacikaec74772008-07-24 12:12:38 -04008376 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008377 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008378 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008379 if (IS_ERR(inode)) {
8380 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008381 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008382 }
Chris Mason39279cc2007-06-12 06:35:45 -04008383
Eric Paris2a7dba32011-02-01 11:05:39 -05008384 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008385 if (err) {
8386 drop_inode = 1;
8387 goto out_unlock;
8388 }
8389
Casey Schauflerad19db72011-12-15 10:09:07 -05008390 /*
8391 * If the active LSM wants to access the inode during
8392 * d_instantiate it needs these. Smack checks to see
8393 * if the filesystem supports xattrs by looking at the
8394 * ops vector.
8395 */
8396 inode->i_fop = &btrfs_file_operations;
8397 inode->i_op = &btrfs_file_inode_operations;
8398
Josef Bacika1b075d2010-11-19 20:36:11 +00008399 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008400 if (err)
8401 drop_inode = 1;
8402 else {
8403 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008404 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008405 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008406 }
Chris Mason39279cc2007-06-12 06:35:45 -04008407 if (drop_inode)
8408 goto out_unlock;
8409
8410 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008411 if (!path) {
8412 err = -ENOMEM;
8413 drop_inode = 1;
8414 goto out_unlock;
8415 }
Li Zefan33345d012011-04-20 10:31:50 +08008416 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008417 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008418 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8419 datasize = btrfs_file_extent_calc_inline_size(name_len);
8420 err = btrfs_insert_empty_item(trans, root, path, &key,
8421 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008422 if (err) {
8423 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008424 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008425 goto out_unlock;
8426 }
Chris Mason5f39d392007-10-15 16:14:19 -04008427 leaf = path->nodes[0];
8428 ei = btrfs_item_ptr(leaf, path->slots[0],
8429 struct btrfs_file_extent_item);
8430 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8431 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008432 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008433 btrfs_set_file_extent_encryption(leaf, ei, 0);
8434 btrfs_set_file_extent_compression(leaf, ei, 0);
8435 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8436 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8437
Chris Mason39279cc2007-06-12 06:35:45 -04008438 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008439 write_extent_buffer(leaf, symname, ptr, name_len);
8440 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008441 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008442
Chris Mason39279cc2007-06-12 06:35:45 -04008443 inode->i_op = &btrfs_symlink_inode_operations;
8444 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008445 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008446 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008447 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008448 err = btrfs_update_inode(trans, root, inode);
8449 if (err)
8450 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008451
8452out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008453 if (!err)
8454 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008455 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008456 if (drop_inode) {
8457 inode_dec_link_count(inode);
8458 iput(inode);
8459 }
Liu Bob53d3f52012-11-14 14:34:34 +00008460 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008461 return err;
8462}
Chris Mason16432982008-04-10 10:23:21 -04008463
Josef Bacik0af3d002010-06-21 14:48:16 -04008464static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8465 u64 start, u64 num_bytes, u64 min_size,
8466 loff_t actual_len, u64 *alloc_hint,
8467 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008468{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008469 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8470 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008471 struct btrfs_root *root = BTRFS_I(inode)->root;
8472 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008473 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008474 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008475 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008476 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008477 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008478
Josef Bacik0af3d002010-06-21 14:48:16 -04008479 if (trans)
8480 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008481 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008482 if (own_trans) {
8483 trans = btrfs_start_transaction(root, 3);
8484 if (IS_ERR(trans)) {
8485 ret = PTR_ERR(trans);
8486 break;
8487 }
Yan Zhengd899e052008-10-30 14:25:28 -04008488 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008489
Chris Mason154ea282013-03-05 11:11:26 -05008490 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8491 cur_bytes = max(cur_bytes, min_size);
8492 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008493 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008494 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008495 if (own_trans)
8496 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008497 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008498 }
8499
Yan Zhengd899e052008-10-30 14:25:28 -04008500 ret = insert_reserved_file_extent(trans, inode,
8501 cur_offset, ins.objectid,
8502 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008503 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008504 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008505 if (ret) {
8506 btrfs_abort_transaction(trans, root, ret);
8507 if (own_trans)
8508 btrfs_end_transaction(trans, root);
8509 break;
8510 }
Chris Masona1ed8352009-09-11 12:27:37 -04008511 btrfs_drop_extent_cache(inode, cur_offset,
8512 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008513
Josef Bacik5dc562c2012-08-17 13:14:17 -04008514 em = alloc_extent_map();
8515 if (!em) {
8516 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8517 &BTRFS_I(inode)->runtime_flags);
8518 goto next;
8519 }
8520
8521 em->start = cur_offset;
8522 em->orig_start = cur_offset;
8523 em->len = ins.offset;
8524 em->block_start = ins.objectid;
8525 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008526 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008527 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008528 em->bdev = root->fs_info->fs_devices->latest_bdev;
8529 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8530 em->generation = trans->transid;
8531
8532 while (1) {
8533 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008534 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008535 write_unlock(&em_tree->lock);
8536 if (ret != -EEXIST)
8537 break;
8538 btrfs_drop_extent_cache(inode, cur_offset,
8539 cur_offset + ins.offset - 1,
8540 0);
8541 }
8542 free_extent_map(em);
8543next:
Yan Zhengd899e052008-10-30 14:25:28 -04008544 num_bytes -= ins.offset;
8545 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008546 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008547
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008548 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008549 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008550 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008551 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008552 (actual_len > inode->i_size) &&
8553 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008554 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008555 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008556 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008557 i_size = cur_offset;
8558 i_size_write(inode, i_size);
8559 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008560 }
8561
Yan Zhengd899e052008-10-30 14:25:28 -04008562 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008563
8564 if (ret) {
8565 btrfs_abort_transaction(trans, root, ret);
8566 if (own_trans)
8567 btrfs_end_transaction(trans, root);
8568 break;
8569 }
Yan Zhengd899e052008-10-30 14:25:28 -04008570
Josef Bacik0af3d002010-06-21 14:48:16 -04008571 if (own_trans)
8572 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008573 }
Yan Zhengd899e052008-10-30 14:25:28 -04008574 return ret;
8575}
8576
Josef Bacik0af3d002010-06-21 14:48:16 -04008577int btrfs_prealloc_file_range(struct inode *inode, int mode,
8578 u64 start, u64 num_bytes, u64 min_size,
8579 loff_t actual_len, u64 *alloc_hint)
8580{
8581 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8582 min_size, actual_len, alloc_hint,
8583 NULL);
8584}
8585
8586int btrfs_prealloc_file_range_trans(struct inode *inode,
8587 struct btrfs_trans_handle *trans, int mode,
8588 u64 start, u64 num_bytes, u64 min_size,
8589 loff_t actual_len, u64 *alloc_hint)
8590{
8591 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8592 min_size, actual_len, alloc_hint, trans);
8593}
8594
Chris Masone6dcd2d2008-07-17 12:53:50 -04008595static int btrfs_set_page_dirty(struct page *page)
8596{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008597 return __set_page_dirty_nobuffers(page);
8598}
8599
Al Viro10556cb2011-06-20 19:28:19 -04008600static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008601{
Li Zefanb83cc962010-12-20 16:04:08 +08008602 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008603 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008604
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008605 if (mask & MAY_WRITE &&
8606 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8607 if (btrfs_root_readonly(root))
8608 return -EROFS;
8609 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8610 return -EACCES;
8611 }
Al Viro2830ba72011-06-20 19:16:29 -04008612 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008613}
Chris Mason39279cc2007-06-12 06:35:45 -04008614
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008615static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008616 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008617 .lookup = btrfs_lookup,
8618 .create = btrfs_create,
8619 .unlink = btrfs_unlink,
8620 .link = btrfs_link,
8621 .mkdir = btrfs_mkdir,
8622 .rmdir = btrfs_rmdir,
8623 .rename = btrfs_rename,
8624 .symlink = btrfs_symlink,
8625 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008626 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008627 .setxattr = btrfs_setxattr,
8628 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008629 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008630 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008631 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008632 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008633};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008634static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008635 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008636 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008637 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008638};
Yan, Zheng76dda932009-09-21 16:00:26 -04008639
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008640static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008641 .llseek = generic_file_llseek,
8642 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008643 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008644 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008645#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008646 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008647#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008648 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008649 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008650};
8651
Chris Masond1310b22008-01-24 16:13:08 -05008652static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008653 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008654 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008655 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008656 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008657 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008658 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008659 .set_bit_hook = btrfs_set_bit_hook,
8660 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008661 .merge_extent_hook = btrfs_merge_extent_hook,
8662 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008663};
8664
Chris Mason35054392009-01-21 13:11:13 -05008665/*
8666 * btrfs doesn't support the bmap operation because swapfiles
8667 * use bmap to make a mapping of extents in the file. They assume
8668 * these extents won't change over the life of the file and they
8669 * use the bmap result to do IO directly to the drive.
8670 *
8671 * the btrfs bmap call would return logical addresses that aren't
8672 * suitable for IO and they also will change frequently as COW
8673 * operations happen. So, swapfile + btrfs == corruption.
8674 *
8675 * For now we're avoiding this by dropping bmap.
8676 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008677static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008678 .readpage = btrfs_readpage,
8679 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008680 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008681 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008682 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008683 .invalidatepage = btrfs_invalidatepage,
8684 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008685 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008686 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008687};
8688
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008689static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008690 .readpage = btrfs_readpage,
8691 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008692 .invalidatepage = btrfs_invalidatepage,
8693 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008694};
8695
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008696static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008697 .getattr = btrfs_getattr,
8698 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008699 .setxattr = btrfs_setxattr,
8700 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008701 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008702 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008703 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008704 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008705 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008706 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008707};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008708static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008709 .getattr = btrfs_getattr,
8710 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008711 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008712 .setxattr = btrfs_setxattr,
8713 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008714 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008715 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008716 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008717 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008718};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008719static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008720 .readlink = generic_readlink,
8721 .follow_link = page_follow_link_light,
8722 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008723 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008724 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008725 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008726 .setxattr = btrfs_setxattr,
8727 .getxattr = btrfs_getxattr,
8728 .listxattr = btrfs_listxattr,
8729 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008730 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008731 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008732};
Yan, Zheng76dda932009-09-21 16:00:26 -04008733
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008734const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008735 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008736 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008737};