blob: 19eef3e852b09774a6c64feae5d0e0e6cd37c019 [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, Zhengc21677542009-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, Zhengc21677542009-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 Bacik7a7eaa402011-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, Zhengc21677542009-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, Zhengc21677542009-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 Bacik7a7eaa402011-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, Zhengc21677542009-11-12 09:34:21 +0000691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 if (ret) {
693 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 for (i = 0; i < async_extent->nr_pages; i++) {
696 WARN_ON(async_extent->pages[i]->mapping);
697 page_cache_release(async_extent->pages[i]);
698 }
699 kfree(async_extent->pages);
700 async_extent->nr_pages = 0;
701 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100703 if (ret == -ENOSPC)
704 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500705 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500706 }
707
Yan, Zhengc21677542009-11-12 09:34:21 +0000708 /*
709 * here we're doing allocation and writeback of the
710 * compressed pages
711 */
712 btrfs_drop_extent_cache(inode, async_extent->start,
713 async_extent->start +
714 async_extent->ram_size - 1, 0);
715
David Sterba172ddd62011-04-21 00:48:27 +0200716 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000717 if (!em) {
718 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000720 }
Chris Mason771ed682008-11-06 22:02:51 -0500721 em->start = async_extent->start;
722 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500723 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400724 em->mod_start = em->start;
725 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500726
727 em->block_start = ins.objectid;
728 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500729 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400730 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500731 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800732 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500733 set_bit(EXTENT_FLAG_PINNED, &em->flags);
734 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400735 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500736
Chris Masond3977122009-01-05 21:25:51 -0500737 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400738 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400739 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400740 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500741 if (ret != -EEXIST) {
742 free_extent_map(em);
743 break;
744 }
745 btrfs_drop_extent_cache(inode, async_extent->start,
746 async_extent->start +
747 async_extent->ram_size - 1, 0);
748 }
749
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500750 if (ret)
751 goto out_free_reserve;
752
Li Zefan261507a02010-12-17 14:21:50 +0800753 ret = btrfs_add_ordered_extent_compress(inode,
754 async_extent->start,
755 ins.objectid,
756 async_extent->ram_size,
757 ins.offset,
758 BTRFS_ORDERED_COMPRESSED,
759 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500760 if (ret)
761 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500762
Chris Mason771ed682008-11-06 22:02:51 -0500763 /*
764 * clear dirty, set writeback and unlock the pages.
765 */
766 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400767 &BTRFS_I(inode)->io_tree,
768 async_extent->start,
769 async_extent->start +
770 async_extent->ram_size - 1,
771 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
772 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400773 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400774 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500775
776 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500777 async_extent->start,
778 async_extent->ram_size,
779 ins.objectid,
780 ins.offset, async_extent->pages,
781 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500782 alloc_hint = ins.objectid + ins.offset;
783 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500784 if (ret)
785 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500786 cond_resched();
787 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100788 ret = 0;
789out:
790 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791out_free_reserve:
792 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100793out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
795 async_extent->start,
796 async_extent->start +
797 async_extent->ram_size - 1,
798 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
799 EXTENT_CLEAR_UNLOCK |
800 EXTENT_CLEAR_DELALLOC |
801 EXTENT_CLEAR_DIRTY |
802 EXTENT_SET_WRITEBACK |
803 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100804 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500806}
807
Josef Bacik4b46fce2010-05-23 11:00:55 -0400808static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
809 u64 num_bytes)
810{
811 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
812 struct extent_map *em;
813 u64 alloc_hint = 0;
814
815 read_lock(&em_tree->lock);
816 em = search_extent_mapping(em_tree, start, num_bytes);
817 if (em) {
818 /*
819 * if block start isn't an actual block number then find the
820 * first block in this inode and use that as a hint. If that
821 * block is also bogus then just don't worry about it.
822 */
823 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
824 free_extent_map(em);
825 em = search_extent_mapping(em_tree, 0, 0);
826 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
827 alloc_hint = em->block_start;
828 if (em)
829 free_extent_map(em);
830 } else {
831 alloc_hint = em->block_start;
832 free_extent_map(em);
833 }
834 }
835 read_unlock(&em_tree->lock);
836
837 return alloc_hint;
838}
839
Chris Mason771ed682008-11-06 22:02:51 -0500840/*
841 * when extent_io.c finds a delayed allocation range in the file,
842 * the call backs end up in this code. The basic idea is to
843 * allocate extents on disk for the range, and create ordered data structs
844 * in ram to track those extents.
845 *
846 * locked_page is the page that writepage had locked already. We use
847 * it to make sure we don't do extra locks or unlocks.
848 *
849 * *page_started is set to one if we unlock locked_page and do everything
850 * required to start IO on it. It may be clean and already done with
851 * IO when we return.
852 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000853static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
854 struct inode *inode,
855 struct btrfs_root *root,
856 struct page *locked_page,
857 u64 start, u64 end, int *page_started,
858 unsigned long *nr_written,
859 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500860{
Chris Mason771ed682008-11-06 22:02:51 -0500861 u64 alloc_hint = 0;
862 u64 num_bytes;
863 unsigned long ram_size;
864 u64 disk_num_bytes;
865 u64 cur_alloc_size;
866 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500867 struct btrfs_key ins;
868 struct extent_map *em;
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870 int ret = 0;
871
Liu Bo83eea1f2012-07-10 05:28:39 -0600872 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500873
Qu Wenruofda28322013-02-26 08:10:22 +0000874 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500875 num_bytes = max(blocksize, num_bytes);
876 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500877
Chris Mason4cb53002011-05-24 15:35:30 -0400878 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400879 if (num_bytes < 64 * 1024 &&
880 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400881 btrfs_add_inode_defrag(trans, inode);
882
Chris Mason771ed682008-11-06 22:02:51 -0500883 if (start == 0) {
884 /* lets try to make an inline extent */
885 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000886 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500887 if (ret == 0) {
888 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400889 &BTRFS_I(inode)->io_tree,
890 start, end, NULL,
891 EXTENT_CLEAR_UNLOCK_PAGE |
892 EXTENT_CLEAR_UNLOCK |
893 EXTENT_CLEAR_DELALLOC |
894 EXTENT_CLEAR_DIRTY |
895 EXTENT_SET_WRITEBACK |
896 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000897
Chris Mason771ed682008-11-06 22:02:51 -0500898 *nr_written = *nr_written +
899 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
900 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500901 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100902 } else if (ret < 0) {
903 btrfs_abort_transaction(trans, root, ret);
904 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500905 }
906 }
Chris Masonc8b97812008-10-29 14:49:59 -0400907
908 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200909 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400910
Josef Bacik4b46fce2010-05-23 11:00:55 -0400911 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400912 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400913
Chris Masond3977122009-01-05 21:25:51 -0500914 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400915 unsigned long op;
916
Josef Bacik287a0ab2010-03-19 18:07:23 +0000917 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500919 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500920 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 if (ret < 0) {
922 btrfs_abort_transaction(trans, root, ret);
923 goto out_unlock;
924 }
Chris Masond3977122009-01-05 21:25:51 -0500925
David Sterba172ddd62011-04-21 00:48:27 +0200926 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000927 if (!em) {
928 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000929 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000930 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400931 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500932 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500933 ram_size = ins.offset;
934 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400935 em->mod_start = em->start;
936 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400937
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400939 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500940 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400941 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400942 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400943 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400944 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400945
Chris Masond3977122009-01-05 21:25:51 -0500946 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400947 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400948 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400949 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400950 if (ret != -EEXIST) {
951 free_extent_map(em);
952 break;
953 }
954 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400955 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400956 }
Liu Boace68ba2013-04-22 10:53:47 +0000957 if (ret)
958 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400959
Chris Mason98d20f62008-04-14 09:46:10 -0400960 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400961 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500962 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000963 if (ret)
964 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400965
Yan Zheng17d217f2008-12-12 10:03:38 -0500966 if (root->root_key.objectid ==
967 BTRFS_DATA_RELOC_TREE_OBJECTID) {
968 ret = btrfs_reloc_clone_csums(inode, start,
969 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 if (ret) {
971 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000972 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100973 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500974 }
975
Chris Masond3977122009-01-05 21:25:51 -0500976 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400977 break;
Chris Masond3977122009-01-05 21:25:51 -0500978
Chris Masonc8b97812008-10-29 14:49:59 -0400979 /* we're not doing compressed IO, don't unlock the first
980 * page (which the caller expects to stay locked), don't
981 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400982 *
983 * Do set the Private2 bit so we know this page was properly
984 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400985 */
Chris Masona791e352009-10-08 11:27:10 -0400986 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
987 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
988 EXTENT_SET_PRIVATE2;
989
Chris Masonc8b97812008-10-29 14:49:59 -0400990 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
991 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400992 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400993 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500994 num_bytes -= cur_alloc_size;
995 alloc_hint = ins.objectid + ins.offset;
996 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400997 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100998out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500999 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001000
Liu Boace68ba2013-04-22 10:53:47 +00001001out_reserve:
1002 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003out_unlock:
1004 extent_clear_unlock_delalloc(inode,
1005 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001006 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 EXTENT_CLEAR_UNLOCK_PAGE |
1008 EXTENT_CLEAR_UNLOCK |
1009 EXTENT_CLEAR_DELALLOC |
1010 EXTENT_CLEAR_DIRTY |
1011 EXTENT_SET_WRITEBACK |
1012 EXTENT_END_WRITEBACK);
1013
1014 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001015}
Chris Masonc8b97812008-10-29 14:49:59 -04001016
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001017static noinline int cow_file_range(struct inode *inode,
1018 struct page *locked_page,
1019 u64 start, u64 end, int *page_started,
1020 unsigned long *nr_written,
1021 int unlock)
1022{
1023 struct btrfs_trans_handle *trans;
1024 struct btrfs_root *root = BTRFS_I(inode)->root;
1025 int ret;
1026
1027 trans = btrfs_join_transaction(root);
1028 if (IS_ERR(trans)) {
1029 extent_clear_unlock_delalloc(inode,
1030 &BTRFS_I(inode)->io_tree,
1031 start, end, locked_page,
1032 EXTENT_CLEAR_UNLOCK_PAGE |
1033 EXTENT_CLEAR_UNLOCK |
1034 EXTENT_CLEAR_DELALLOC |
1035 EXTENT_CLEAR_DIRTY |
1036 EXTENT_SET_WRITEBACK |
1037 EXTENT_END_WRITEBACK);
1038 return PTR_ERR(trans);
1039 }
1040 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1041
1042 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1043 page_started, nr_written, unlock);
1044
1045 btrfs_end_transaction(trans, root);
1046
1047 return ret;
1048}
1049
Chris Mason771ed682008-11-06 22:02:51 -05001050/*
1051 * work queue call back to started compression on a file and pages
1052 */
1053static noinline void async_cow_start(struct btrfs_work *work)
1054{
1055 struct async_cow *async_cow;
1056 int num_added = 0;
1057 async_cow = container_of(work, struct async_cow, work);
1058
1059 compress_file_range(async_cow->inode, async_cow->locked_page,
1060 async_cow->start, async_cow->end, async_cow,
1061 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001062 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001063 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001064 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001065 }
Chris Mason771ed682008-11-06 22:02:51 -05001066}
1067
1068/*
1069 * work queue call back to submit previously compressed pages
1070 */
1071static noinline void async_cow_submit(struct btrfs_work *work)
1072{
1073 struct async_cow *async_cow;
1074 struct btrfs_root *root;
1075 unsigned long nr_pages;
1076
1077 async_cow = container_of(work, struct async_cow, work);
1078
1079 root = async_cow->root;
1080 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1081 PAGE_CACHE_SHIFT;
1082
Josef Bacik66657b32012-08-01 15:36:24 -04001083 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001084 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001085 waitqueue_active(&root->fs_info->async_submit_wait))
1086 wake_up(&root->fs_info->async_submit_wait);
1087
Chris Masond3977122009-01-05 21:25:51 -05001088 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001089 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001090}
Chris Masonc8b97812008-10-29 14:49:59 -04001091
Chris Mason771ed682008-11-06 22:02:51 -05001092static noinline void async_cow_free(struct btrfs_work *work)
1093{
1094 struct async_cow *async_cow;
1095 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001096 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001097 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001098 kfree(async_cow);
1099}
1100
1101static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1102 u64 start, u64 end, int *page_started,
1103 unsigned long *nr_written)
1104{
1105 struct async_cow *async_cow;
1106 struct btrfs_root *root = BTRFS_I(inode)->root;
1107 unsigned long nr_pages;
1108 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001109 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001110
Chris Masona3429ab2009-10-08 12:30:20 -04001111 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1112 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001113 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001114 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001115 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001116 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001117 async_cow->root = root;
1118 async_cow->locked_page = locked_page;
1119 async_cow->start = start;
1120
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001121 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001122 cur_end = end;
1123 else
1124 cur_end = min(end, start + 512 * 1024 - 1);
1125
1126 async_cow->end = cur_end;
1127 INIT_LIST_HEAD(&async_cow->extents);
1128
1129 async_cow->work.func = async_cow_start;
1130 async_cow->work.ordered_func = async_cow_submit;
1131 async_cow->work.ordered_free = async_cow_free;
1132 async_cow->work.flags = 0;
1133
Chris Mason771ed682008-11-06 22:02:51 -05001134 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1135 PAGE_CACHE_SHIFT;
1136 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1137
1138 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1139 &async_cow->work);
1140
1141 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1142 wait_event(root->fs_info->async_submit_wait,
1143 (atomic_read(&root->fs_info->async_delalloc_pages) <
1144 limit));
1145 }
1146
Chris Masond3977122009-01-05 21:25:51 -05001147 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001148 atomic_read(&root->fs_info->async_delalloc_pages)) {
1149 wait_event(root->fs_info->async_submit_wait,
1150 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1151 0));
1152 }
1153
1154 *nr_written += nr_pages;
1155 start = cur_end + 1;
1156 }
1157 *page_started = 1;
1158 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001159}
1160
Chris Masond3977122009-01-05 21:25:51 -05001161static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001162 u64 bytenr, u64 num_bytes)
1163{
1164 int ret;
1165 struct btrfs_ordered_sum *sums;
1166 LIST_HEAD(list);
1167
Yan Zheng07d400a2009-01-06 11:42:00 -05001168 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001169 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001170 if (ret == 0 && list_empty(&list))
1171 return 0;
1172
1173 while (!list_empty(&list)) {
1174 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1175 list_del(&sums->list);
1176 kfree(sums);
1177 }
1178 return 1;
1179}
1180
Chris Masond352ac62008-09-29 15:18:18 -04001181/*
1182 * when nowcow writeback call back. This checks for snapshots or COW copies
1183 * of the extents that exist in the file, and COWs the file as required.
1184 *
1185 * If no cow copies or snapshots exist, we write directly to the existing
1186 * blocks on disk
1187 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001188static noinline int run_delalloc_nocow(struct inode *inode,
1189 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001190 u64 start, u64 end, int *page_started, int force,
1191 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001192{
Chris Masonbe20aa92007-12-17 20:14:01 -05001193 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001194 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001195 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001196 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001198 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001199 u64 cow_start;
1200 u64 cur_offset;
1201 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001202 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 u64 disk_bytenr;
1204 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001205 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001206 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001208 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001209 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 int nocow;
1211 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001212 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001213 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001214
1215 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001216 if (!path) {
1217 extent_clear_unlock_delalloc(inode,
1218 &BTRFS_I(inode)->io_tree,
1219 start, end, locked_page,
1220 EXTENT_CLEAR_UNLOCK_PAGE |
1221 EXTENT_CLEAR_UNLOCK |
1222 EXTENT_CLEAR_DELALLOC |
1223 EXTENT_CLEAR_DIRTY |
1224 EXTENT_SET_WRITEBACK |
1225 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001226 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001227 }
Li Zefan82d59022011-04-20 10:33:24 +08001228
Liu Bo83eea1f2012-07-10 05:28:39 -06001229 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001230
1231 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001232 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001233 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001234 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001235
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001236 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001237 extent_clear_unlock_delalloc(inode,
1238 &BTRFS_I(inode)->io_tree,
1239 start, end, locked_page,
1240 EXTENT_CLEAR_UNLOCK_PAGE |
1241 EXTENT_CLEAR_UNLOCK |
1242 EXTENT_CLEAR_DELALLOC |
1243 EXTENT_CLEAR_DIRTY |
1244 EXTENT_SET_WRITEBACK |
1245 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001246 btrfs_free_path(path);
1247 return PTR_ERR(trans);
1248 }
1249
Josef Bacik74b21072011-04-13 12:02:53 -04001250 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 cow_start = (u64)-1;
1253 cur_offset = start;
1254 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001255 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001257 if (ret < 0) {
1258 btrfs_abort_transaction(trans, root, ret);
1259 goto error;
1260 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1262 leaf = path->nodes[0];
1263 btrfs_item_key_to_cpu(leaf, &found_key,
1264 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001265 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 found_key.type == BTRFS_EXTENT_DATA_KEY)
1267 path->slots[0]--;
1268 }
1269 check_prev = 0;
1270next_slot:
1271 leaf = path->nodes[0];
1272 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1273 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001274 if (ret < 0) {
1275 btrfs_abort_transaction(trans, root, ret);
1276 goto error;
1277 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 if (ret > 0)
1279 break;
1280 leaf = path->nodes[0];
1281 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001282
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 nocow = 0;
1284 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001285 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001287
Li Zefan33345d012011-04-20 10:31:50 +08001288 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1290 found_key.offset > end)
1291 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 if (found_key.offset > cur_offset) {
1294 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001295 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 goto out_check;
1297 }
Chris Masonc31f8832008-01-08 15:46:31 -05001298
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 fi = btrfs_item_ptr(leaf, path->slots[0],
1300 struct btrfs_file_extent_item);
1301 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
Josef Bacikcc95bef2013-04-04 14:31:27 -04001303 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001304 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1305 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001307 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 extent_end = found_key.offset +
1309 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001310 disk_num_bytes =
1311 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 if (extent_end <= start) {
1313 path->slots[0]++;
1314 goto next_slot;
1315 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001316 if (disk_bytenr == 0)
1317 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 if (btrfs_file_extent_compression(leaf, fi) ||
1319 btrfs_file_extent_encryption(leaf, fi) ||
1320 btrfs_file_extent_other_encoding(leaf, fi))
1321 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001322 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1323 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001324 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001326 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001327 found_key.offset -
1328 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001331 disk_bytenr += cur_offset - found_key.offset;
1332 num_bytes = min(end + 1, extent_end) - cur_offset;
1333 /*
1334 * force cow if csum exists in the range.
1335 * this ensure that csum for a given extent are
1336 * either valid or do not exist.
1337 */
1338 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1339 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 nocow = 1;
1341 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1342 extent_end = found_key.offset +
1343 btrfs_file_extent_inline_len(leaf, fi);
1344 extent_end = ALIGN(extent_end, root->sectorsize);
1345 } else {
1346 BUG_ON(1);
1347 }
1348out_check:
1349 if (extent_end <= start) {
1350 path->slots[0]++;
1351 goto next_slot;
1352 }
1353 if (!nocow) {
1354 if (cow_start == (u64)-1)
1355 cow_start = cur_offset;
1356 cur_offset = extent_end;
1357 if (cur_offset > end)
1358 break;
1359 path->slots[0]++;
1360 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001361 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001362
David Sterbab3b4aa72011-04-21 01:20:15 +02001363 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001365 ret = __cow_file_range(trans, inode, root, locked_page,
1366 cow_start, found_key.offset - 1,
1367 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001368 if (ret) {
1369 btrfs_abort_transaction(trans, root, ret);
1370 goto error;
1371 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001373 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001374
Yan Zhengd899e052008-10-30 14:25:28 -04001375 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1376 struct extent_map *em;
1377 struct extent_map_tree *em_tree;
1378 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001379 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001380 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001381 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001382 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001383 em->len = num_bytes;
1384 em->block_len = num_bytes;
1385 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001386 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001387 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001389 em->mod_start = em->start;
1390 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001391 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001392 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001393 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001394 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001395 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001396 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001397 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001398 if (ret != -EEXIST) {
1399 free_extent_map(em);
1400 break;
1401 }
1402 btrfs_drop_extent_cache(inode, em->start,
1403 em->start + em->len - 1, 0);
1404 }
1405 type = BTRFS_ORDERED_PREALLOC;
1406 } else {
1407 type = BTRFS_ORDERED_NOCOW;
1408 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001409
1410 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001411 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001412 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001413
Yan, Zhengefa56462010-05-16 10:49:59 -04001414 if (root->root_key.objectid ==
1415 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1416 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1417 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001418 if (ret) {
1419 btrfs_abort_transaction(trans, root, ret);
1420 goto error;
1421 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001422 }
1423
Yan Zhengd899e052008-10-30 14:25:28 -04001424 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001425 cur_offset, cur_offset + num_bytes - 1,
1426 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1427 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1428 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 cur_offset = extent_end;
1430 if (cur_offset > end)
1431 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001432 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001433 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001434
Josef Bacik17ca04a2012-05-31 15:58:55 -04001435 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001437 cur_offset = end;
1438 }
1439
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001441 ret = __cow_file_range(trans, inode, root, locked_page,
1442 cow_start, end,
1443 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 if (ret) {
1445 btrfs_abort_transaction(trans, root, ret);
1446 goto error;
1447 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001448 }
1449
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001450error:
Miao Xiea698d0752012-09-20 01:51:59 -06001451 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001452 if (!ret)
1453 ret = err;
1454
Josef Bacik17ca04a2012-05-31 15:58:55 -04001455 if (ret && cur_offset < end)
1456 extent_clear_unlock_delalloc(inode,
1457 &BTRFS_I(inode)->io_tree,
1458 cur_offset, end, locked_page,
1459 EXTENT_CLEAR_UNLOCK_PAGE |
1460 EXTENT_CLEAR_UNLOCK |
1461 EXTENT_CLEAR_DELALLOC |
1462 EXTENT_CLEAR_DIRTY |
1463 EXTENT_SET_WRITEBACK |
1464 EXTENT_END_WRITEBACK);
1465
Yan Zheng7ea394f2008-08-05 13:05:02 -04001466 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001467 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001468}
1469
Chris Masond352ac62008-09-29 15:18:18 -04001470/*
1471 * extent_io.c call back to do delayed allocation processing
1472 */
Chris Masonc8b97812008-10-29 14:49:59 -04001473static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001474 u64 start, u64 end, int *page_started,
1475 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001476{
Chris Masonbe20aa92007-12-17 20:14:01 -05001477 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001478 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001479
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001480 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001481 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001482 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001483 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001484 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001485 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001486 } else if (!btrfs_test_opt(root, COMPRESS) &&
1487 !(BTRFS_I(inode)->force_compress) &&
1488 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001489 ret = cow_file_range(inode, locked_page, start, end,
1490 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001491 } else {
1492 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1493 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001494 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001495 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001496 }
Chris Masonb888db22007-08-27 16:49:44 -04001497 return ret;
1498}
1499
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001500static void btrfs_split_extent_hook(struct inode *inode,
1501 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001503 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001504 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001505 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506
Josef Bacik9e0baf62011-07-15 15:16:44 +00001507 spin_lock(&BTRFS_I(inode)->lock);
1508 BTRFS_I(inode)->outstanding_extents++;
1509 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001510}
1511
1512/*
1513 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1514 * extents so we can keep track of new extents that are just merged onto old
1515 * extents, such as when we are doing sequential writes, so we can properly
1516 * account for the metadata space we'll need.
1517 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001518static void btrfs_merge_extent_hook(struct inode *inode,
1519 struct extent_state *new,
1520 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001522 /* not delalloc, ignore it */
1523 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001524 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525
Josef Bacik9e0baf62011-07-15 15:16:44 +00001526 spin_lock(&BTRFS_I(inode)->lock);
1527 BTRFS_I(inode)->outstanding_extents--;
1528 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001529}
1530
Chris Masond352ac62008-09-29 15:18:18 -04001531/*
1532 * extent_io.c set_bit_hook, used to track delayed allocation
1533 * bytes in this file, and to maintain the list of inodes that
1534 * have pending delalloc work to be done.
1535 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001536static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001537 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001538{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001539
Chris Mason75eff682008-12-15 15:54:40 -05001540 /*
1541 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001542 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001543 * bit, which is only set or cleared with irqs on
1544 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001546 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001547 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001548 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001549
Josef Bacik9e0baf62011-07-15 15:16:44 +00001550 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001551 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001552 } else {
1553 spin_lock(&BTRFS_I(inode)->lock);
1554 BTRFS_I(inode)->outstanding_extents++;
1555 spin_unlock(&BTRFS_I(inode)->lock);
1556 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001557
Miao Xie963d6782013-01-29 10:10:51 +00001558 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1559 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001560 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001561 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a52013-01-29 10:11:59 +00001562 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1563 &BTRFS_I(inode)->runtime_flags)) {
1564 spin_lock(&root->fs_info->delalloc_lock);
1565 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1566 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1567 &root->fs_info->delalloc_inodes);
1568 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1569 &BTRFS_I(inode)->runtime_flags);
1570 }
1571 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001572 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001573 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001574 }
Chris Mason291d6732008-01-29 15:55:23 -05001575}
1576
Chris Masond352ac62008-09-29 15:18:18 -04001577/*
1578 * extent_io.c clear_bit_hook, see set_bit_hook for why
1579 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001580static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001581 struct extent_state *state,
1582 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001583{
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);
Chris Masonbcbfce82008-04-22 13:26:47 -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 if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1597 spin_lock(&BTRFS_I(inode)->lock);
1598 BTRFS_I(inode)->outstanding_extents--;
1599 spin_unlock(&BTRFS_I(inode)->lock);
1600 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001601
1602 if (*bits & EXTENT_DO_ACCOUNTING)
1603 btrfs_delalloc_release_metadata(inode, len);
1604
Josef Bacik0cb59c92010-07-02 12:14:14 -04001605 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1606 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001607 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001608
Miao Xie963d6782013-01-29 10:10:51 +00001609 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1610 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001611 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001612 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001613 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a52013-01-29 10:11:59 +00001614 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1615 &BTRFS_I(inode)->runtime_flags)) {
1616 spin_lock(&root->fs_info->delalloc_lock);
1617 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1618 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1619 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1620 &BTRFS_I(inode)->runtime_flags);
1621 }
1622 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001623 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001624 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001625 }
Chris Mason291d6732008-01-29 15:55:23 -05001626}
1627
Chris Masond352ac62008-09-29 15:18:18 -04001628/*
1629 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1630 * we don't create bios that span stripes or chunks
1631 */
David Woodhouse64a16702009-07-15 23:29:37 +01001632int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001633 size_t size, struct bio *bio,
1634 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001635{
1636 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001637 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001638 u64 length = 0;
1639 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001640 int ret;
1641
Chris Mason771ed682008-11-06 22:02:51 -05001642 if (bio_flags & EXTENT_BIO_COMPRESSED)
1643 return 0;
1644
Chris Masonf2d8d742008-04-21 10:03:05 -04001645 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001646 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001647 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001648 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001649 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001650 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001651 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001652 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001653 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001654}
1655
Chris Masond352ac62008-09-29 15:18:18 -04001656/*
1657 * in order to insert checksums into the metadata in large chunks,
1658 * we wait until bio submission time. All the pages in the bio are
1659 * checksummed and sums are attached onto the ordered extent record.
1660 *
1661 * At IO completion time the cums attached on the ordered extent record
1662 * are inserted into the btree
1663 */
Chris Masond3977122009-01-05 21:25:51 -05001664static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1665 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001666 unsigned long bio_flags,
1667 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001668{
Chris Mason065631f2008-02-20 12:07:25 -05001669 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001670 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001671
Chris Masond20f7042008-12-08 16:58:54 -05001672 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001673 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001674 return 0;
1675}
Chris Masone0156402008-04-16 11:15:20 -04001676
Chris Mason4a69a412008-11-06 22:03:00 -05001677/*
1678 * in order to insert checksums into the metadata in large chunks,
1679 * we wait until bio submission time. All the pages in the bio are
1680 * checksummed and sums are attached onto the ordered extent record.
1681 *
1682 * At IO completion time the cums attached on the ordered extent record
1683 * are inserted into the btree
1684 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001685static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001686 int mirror_num, unsigned long bio_flags,
1687 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001688{
1689 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001690 int ret;
1691
1692 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1693 if (ret)
1694 bio_endio(bio, ret);
1695 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001696}
1697
Chris Masond352ac62008-09-29 15:18:18 -04001698/*
Chris Masoncad321a2008-12-17 14:51:42 -05001699 * extent_io.c submission hook. This does the right thing for csum calculation
1700 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001701 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001702static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001703 int mirror_num, unsigned long bio_flags,
1704 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001705{
1706 struct btrfs_root *root = BTRFS_I(inode)->root;
1707 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001708 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001709 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001710 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001711
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001712 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001713
Liu Bo83eea1f2012-07-10 05:28:39 -06001714 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001715 metadata = 2;
1716
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001717 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001718 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1719 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001720 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001721
Chris Masond20f7042008-12-08 16:58:54 -05001722 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001723 ret = btrfs_submit_compressed_read(inode, bio,
1724 mirror_num,
1725 bio_flags);
1726 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001727 } else if (!skip_sum) {
1728 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1729 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001730 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001731 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001732 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001733 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001734 /* csum items have already been cloned */
1735 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1736 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001737 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001738 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001739 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001740 bio_flags, bio_offset,
1741 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001742 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001743 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001744 } else if (!skip_sum) {
1745 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1746 if (ret)
1747 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001748 }
1749
Chris Mason0b86a832008-03-24 15:01:56 -04001750mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001751 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1752
1753out:
1754 if (ret < 0)
1755 bio_endio(bio, ret);
1756 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001757}
Chris Mason6885f302008-02-20 16:11:05 -05001758
Chris Masond352ac62008-09-29 15:18:18 -04001759/*
1760 * given a list of ordered sums record them in the inode. This happens
1761 * at IO completion time based on sums calculated at bio submission time.
1762 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001763static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001764 struct inode *inode, u64 file_offset,
1765 struct list_head *list)
1766{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001767 struct btrfs_ordered_sum *sum;
1768
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001769 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001770 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001771 btrfs_csum_file_blocks(trans,
1772 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001773 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001774 }
1775 return 0;
1776}
1777
Josef Bacik2ac55d42010-02-03 19:33:23 +00001778int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1779 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001780{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001781 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001782 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001783 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001784}
1785
Chris Masond352ac62008-09-29 15:18:18 -04001786/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001787struct btrfs_writepage_fixup {
1788 struct page *page;
1789 struct btrfs_work work;
1790};
1791
Christoph Hellwigb2950862008-12-02 09:54:17 -05001792static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001793{
1794 struct btrfs_writepage_fixup *fixup;
1795 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001796 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001797 struct page *page;
1798 struct inode *inode;
1799 u64 page_start;
1800 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001801 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001802
1803 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1804 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001805again:
Chris Mason247e7432008-07-17 12:53:51 -04001806 lock_page(page);
1807 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1808 ClearPageChecked(page);
1809 goto out_page;
1810 }
1811
1812 inode = page->mapping->host;
1813 page_start = page_offset(page);
1814 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1815
Josef Bacik2ac55d42010-02-03 19:33:23 +00001816 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001817 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001818
1819 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001820 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001821 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001822
1823 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1824 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001825 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1826 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001827 unlock_page(page);
1828 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001829 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001830 goto again;
1831 }
Chris Mason247e7432008-07-17 12:53:51 -04001832
Jeff Mahoney87826df2012-02-15 16:23:57 +01001833 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1834 if (ret) {
1835 mapping_set_error(page->mapping, ret);
1836 end_extent_writepage(page, ret, page_start, page_end);
1837 ClearPageChecked(page);
1838 goto out;
1839 }
1840
Josef Bacik2ac55d42010-02-03 19:33:23 +00001841 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001842 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001843 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001844out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001845 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1846 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001847out_page:
1848 unlock_page(page);
1849 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001850 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001851}
1852
1853/*
1854 * There are a few paths in the higher layers of the kernel that directly
1855 * set the page dirty bit without asking the filesystem if it is a
1856 * good idea. This causes problems because we want to make sure COW
1857 * properly happens and the data=ordered rules are followed.
1858 *
Chris Masonc8b97812008-10-29 14:49:59 -04001859 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001860 * hasn't been properly setup for IO. We kick off an async process
1861 * to fix it up. The async helper will wait for ordered extents, set
1862 * the delalloc bit and make it safe to write the page.
1863 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001864static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001865{
1866 struct inode *inode = page->mapping->host;
1867 struct btrfs_writepage_fixup *fixup;
1868 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001869
Chris Mason8b62b722009-09-02 16:53:46 -04001870 /* this page is properly in the ordered list */
1871 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001872 return 0;
1873
1874 if (PageChecked(page))
1875 return -EAGAIN;
1876
1877 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1878 if (!fixup)
1879 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001880
Chris Mason247e7432008-07-17 12:53:51 -04001881 SetPageChecked(page);
1882 page_cache_get(page);
1883 fixup->work.func = btrfs_writepage_fixup_worker;
1884 fixup->page = page;
1885 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001886 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001887}
1888
Yan Zhengd899e052008-10-30 14:25:28 -04001889static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1890 struct inode *inode, u64 file_pos,
1891 u64 disk_bytenr, u64 disk_num_bytes,
1892 u64 num_bytes, u64 ram_bytes,
1893 u8 compression, u8 encryption,
1894 u16 other_encoding, int extent_type)
1895{
1896 struct btrfs_root *root = BTRFS_I(inode)->root;
1897 struct btrfs_file_extent_item *fi;
1898 struct btrfs_path *path;
1899 struct extent_buffer *leaf;
1900 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001901 int ret;
1902
1903 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001904 if (!path)
1905 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001906
Chris Masonb9473432009-03-13 11:00:37 -04001907 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001908
1909 /*
1910 * we may be replacing one extent in the tree with another.
1911 * The new extent is pinned in the extent map, and we don't want
1912 * to drop it from the cache until it is completely in the btree.
1913 *
1914 * So, tell btrfs_drop_extents to leave this extent in the cache.
1915 * the caller is expected to unpin it and allow it to be merged
1916 * with the others.
1917 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001918 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001919 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001920 if (ret)
1921 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001922
Li Zefan33345d012011-04-20 10:31:50 +08001923 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001924 ins.offset = file_pos;
1925 ins.type = BTRFS_EXTENT_DATA_KEY;
1926 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001927 if (ret)
1928 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001929 leaf = path->nodes[0];
1930 fi = btrfs_item_ptr(leaf, path->slots[0],
1931 struct btrfs_file_extent_item);
1932 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1933 btrfs_set_file_extent_type(leaf, fi, extent_type);
1934 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1935 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1936 btrfs_set_file_extent_offset(leaf, fi, 0);
1937 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1938 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1939 btrfs_set_file_extent_compression(leaf, fi, compression);
1940 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1941 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001942
Yan Zhengd899e052008-10-30 14:25:28 -04001943 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001944 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001945
1946 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001947
1948 ins.objectid = disk_bytenr;
1949 ins.offset = disk_num_bytes;
1950 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001951 ret = btrfs_alloc_reserved_file_extent(trans, root,
1952 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001953 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001954out:
Yan Zhengd899e052008-10-30 14:25:28 -04001955 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001956
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001957 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001958}
1959
Liu Bo38c227d2013-01-29 03:18:40 +00001960/* snapshot-aware defrag */
1961struct sa_defrag_extent_backref {
1962 struct rb_node node;
1963 struct old_sa_defrag_extent *old;
1964 u64 root_id;
1965 u64 inum;
1966 u64 file_pos;
1967 u64 extent_offset;
1968 u64 num_bytes;
1969 u64 generation;
1970};
1971
1972struct old_sa_defrag_extent {
1973 struct list_head list;
1974 struct new_sa_defrag_extent *new;
1975
1976 u64 extent_offset;
1977 u64 bytenr;
1978 u64 offset;
1979 u64 len;
1980 int count;
1981};
1982
1983struct new_sa_defrag_extent {
1984 struct rb_root root;
1985 struct list_head head;
1986 struct btrfs_path *path;
1987 struct inode *inode;
1988 u64 file_pos;
1989 u64 len;
1990 u64 bytenr;
1991 u64 disk_len;
1992 u8 compress_type;
1993};
1994
1995static int backref_comp(struct sa_defrag_extent_backref *b1,
1996 struct sa_defrag_extent_backref *b2)
1997{
1998 if (b1->root_id < b2->root_id)
1999 return -1;
2000 else if (b1->root_id > b2->root_id)
2001 return 1;
2002
2003 if (b1->inum < b2->inum)
2004 return -1;
2005 else if (b1->inum > b2->inum)
2006 return 1;
2007
2008 if (b1->file_pos < b2->file_pos)
2009 return -1;
2010 else if (b1->file_pos > b2->file_pos)
2011 return 1;
2012
2013 /*
2014 * [------------------------------] ===> (a range of space)
2015 * |<--->| |<---->| =============> (fs/file tree A)
2016 * |<---------------------------->| ===> (fs/file tree B)
2017 *
2018 * A range of space can refer to two file extents in one tree while
2019 * refer to only one file extent in another tree.
2020 *
2021 * So we may process a disk offset more than one time(two extents in A)
2022 * and locate at the same extent(one extent in B), then insert two same
2023 * backrefs(both refer to the extent in B).
2024 */
2025 return 0;
2026}
2027
2028static void backref_insert(struct rb_root *root,
2029 struct sa_defrag_extent_backref *backref)
2030{
2031 struct rb_node **p = &root->rb_node;
2032 struct rb_node *parent = NULL;
2033 struct sa_defrag_extent_backref *entry;
2034 int ret;
2035
2036 while (*p) {
2037 parent = *p;
2038 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2039
2040 ret = backref_comp(backref, entry);
2041 if (ret < 0)
2042 p = &(*p)->rb_left;
2043 else
2044 p = &(*p)->rb_right;
2045 }
2046
2047 rb_link_node(&backref->node, parent, p);
2048 rb_insert_color(&backref->node, root);
2049}
2050
2051/*
2052 * Note the backref might has changed, and in this case we just return 0.
2053 */
2054static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2055 void *ctx)
2056{
2057 struct btrfs_file_extent_item *extent;
2058 struct btrfs_fs_info *fs_info;
2059 struct old_sa_defrag_extent *old = ctx;
2060 struct new_sa_defrag_extent *new = old->new;
2061 struct btrfs_path *path = new->path;
2062 struct btrfs_key key;
2063 struct btrfs_root *root;
2064 struct sa_defrag_extent_backref *backref;
2065 struct extent_buffer *leaf;
2066 struct inode *inode = new->inode;
2067 int slot;
2068 int ret;
2069 u64 extent_offset;
2070 u64 num_bytes;
2071
2072 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2073 inum == btrfs_ino(inode))
2074 return 0;
2075
2076 key.objectid = root_id;
2077 key.type = BTRFS_ROOT_ITEM_KEY;
2078 key.offset = (u64)-1;
2079
2080 fs_info = BTRFS_I(inode)->root->fs_info;
2081 root = btrfs_read_fs_root_no_name(fs_info, &key);
2082 if (IS_ERR(root)) {
2083 if (PTR_ERR(root) == -ENOENT)
2084 return 0;
2085 WARN_ON(1);
2086 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2087 inum, offset, root_id);
2088 return PTR_ERR(root);
2089 }
2090
2091 key.objectid = inum;
2092 key.type = BTRFS_EXTENT_DATA_KEY;
2093 if (offset > (u64)-1 << 32)
2094 key.offset = 0;
2095 else
2096 key.offset = offset;
2097
2098 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2099 if (ret < 0) {
2100 WARN_ON(1);
2101 return ret;
2102 }
2103
2104 while (1) {
2105 cond_resched();
2106
2107 leaf = path->nodes[0];
2108 slot = path->slots[0];
2109
2110 if (slot >= btrfs_header_nritems(leaf)) {
2111 ret = btrfs_next_leaf(root, path);
2112 if (ret < 0) {
2113 goto out;
2114 } else if (ret > 0) {
2115 ret = 0;
2116 goto out;
2117 }
2118 continue;
2119 }
2120
2121 path->slots[0]++;
2122
2123 btrfs_item_key_to_cpu(leaf, &key, slot);
2124
2125 if (key.objectid > inum)
2126 goto out;
2127
2128 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2129 continue;
2130
2131 extent = btrfs_item_ptr(leaf, slot,
2132 struct btrfs_file_extent_item);
2133
2134 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2135 continue;
2136
2137 extent_offset = btrfs_file_extent_offset(leaf, extent);
2138 if (key.offset - extent_offset != offset)
2139 continue;
2140
2141 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2142 if (extent_offset >= old->extent_offset + old->offset +
2143 old->len || extent_offset + num_bytes <=
2144 old->extent_offset + old->offset)
2145 continue;
2146
2147 break;
2148 }
2149
2150 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2151 if (!backref) {
2152 ret = -ENOENT;
2153 goto out;
2154 }
2155
2156 backref->root_id = root_id;
2157 backref->inum = inum;
2158 backref->file_pos = offset + extent_offset;
2159 backref->num_bytes = num_bytes;
2160 backref->extent_offset = extent_offset;
2161 backref->generation = btrfs_file_extent_generation(leaf, extent);
2162 backref->old = old;
2163 backref_insert(&new->root, backref);
2164 old->count++;
2165out:
2166 btrfs_release_path(path);
2167 WARN_ON(ret);
2168 return ret;
2169}
2170
2171static noinline bool record_extent_backrefs(struct btrfs_path *path,
2172 struct new_sa_defrag_extent *new)
2173{
2174 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2175 struct old_sa_defrag_extent *old, *tmp;
2176 int ret;
2177
2178 new->path = path;
2179
2180 list_for_each_entry_safe(old, tmp, &new->head, list) {
2181 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2182 path, record_one_backref,
2183 old);
2184 BUG_ON(ret < 0 && ret != -ENOENT);
2185
2186 /* no backref to be processed for this extent */
2187 if (!old->count) {
2188 list_del(&old->list);
2189 kfree(old);
2190 }
2191 }
2192
2193 if (list_empty(&new->head))
2194 return false;
2195
2196 return true;
2197}
2198
2199static int relink_is_mergable(struct extent_buffer *leaf,
2200 struct btrfs_file_extent_item *fi,
2201 u64 disk_bytenr)
2202{
2203 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2204 return 0;
2205
2206 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2207 return 0;
2208
2209 if (btrfs_file_extent_compression(leaf, fi) ||
2210 btrfs_file_extent_encryption(leaf, fi) ||
2211 btrfs_file_extent_other_encoding(leaf, fi))
2212 return 0;
2213
2214 return 1;
2215}
2216
2217/*
2218 * Note the backref might has changed, and in this case we just return 0.
2219 */
2220static noinline int relink_extent_backref(struct btrfs_path *path,
2221 struct sa_defrag_extent_backref *prev,
2222 struct sa_defrag_extent_backref *backref)
2223{
2224 struct btrfs_file_extent_item *extent;
2225 struct btrfs_file_extent_item *item;
2226 struct btrfs_ordered_extent *ordered;
2227 struct btrfs_trans_handle *trans;
2228 struct btrfs_fs_info *fs_info;
2229 struct btrfs_root *root;
2230 struct btrfs_key key;
2231 struct extent_buffer *leaf;
2232 struct old_sa_defrag_extent *old = backref->old;
2233 struct new_sa_defrag_extent *new = old->new;
2234 struct inode *src_inode = new->inode;
2235 struct inode *inode;
2236 struct extent_state *cached = NULL;
2237 int ret = 0;
2238 u64 start;
2239 u64 len;
2240 u64 lock_start;
2241 u64 lock_end;
2242 bool merge = false;
2243 int index;
2244
2245 if (prev && prev->root_id == backref->root_id &&
2246 prev->inum == backref->inum &&
2247 prev->file_pos + prev->num_bytes == backref->file_pos)
2248 merge = true;
2249
2250 /* step 1: get root */
2251 key.objectid = backref->root_id;
2252 key.type = BTRFS_ROOT_ITEM_KEY;
2253 key.offset = (u64)-1;
2254
2255 fs_info = BTRFS_I(src_inode)->root->fs_info;
2256 index = srcu_read_lock(&fs_info->subvol_srcu);
2257
2258 root = btrfs_read_fs_root_no_name(fs_info, &key);
2259 if (IS_ERR(root)) {
2260 srcu_read_unlock(&fs_info->subvol_srcu, index);
2261 if (PTR_ERR(root) == -ENOENT)
2262 return 0;
2263 return PTR_ERR(root);
2264 }
2265 if (btrfs_root_refs(&root->root_item) == 0) {
2266 srcu_read_unlock(&fs_info->subvol_srcu, index);
2267 /* parse ENOENT to 0 */
2268 return 0;
2269 }
2270
2271 /* step 2: get inode */
2272 key.objectid = backref->inum;
2273 key.type = BTRFS_INODE_ITEM_KEY;
2274 key.offset = 0;
2275
2276 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2277 if (IS_ERR(inode)) {
2278 srcu_read_unlock(&fs_info->subvol_srcu, index);
2279 return 0;
2280 }
2281
2282 srcu_read_unlock(&fs_info->subvol_srcu, index);
2283
2284 /* step 3: relink backref */
2285 lock_start = backref->file_pos;
2286 lock_end = backref->file_pos + backref->num_bytes - 1;
2287 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2288 0, &cached);
2289
2290 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2291 if (ordered) {
2292 btrfs_put_ordered_extent(ordered);
2293 goto out_unlock;
2294 }
2295
2296 trans = btrfs_join_transaction(root);
2297 if (IS_ERR(trans)) {
2298 ret = PTR_ERR(trans);
2299 goto out_unlock;
2300 }
2301
2302 key.objectid = backref->inum;
2303 key.type = BTRFS_EXTENT_DATA_KEY;
2304 key.offset = backref->file_pos;
2305
2306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2307 if (ret < 0) {
2308 goto out_free_path;
2309 } else if (ret > 0) {
2310 ret = 0;
2311 goto out_free_path;
2312 }
2313
2314 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2315 struct btrfs_file_extent_item);
2316
2317 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2318 backref->generation)
2319 goto out_free_path;
2320
2321 btrfs_release_path(path);
2322
2323 start = backref->file_pos;
2324 if (backref->extent_offset < old->extent_offset + old->offset)
2325 start += old->extent_offset + old->offset -
2326 backref->extent_offset;
2327
2328 len = min(backref->extent_offset + backref->num_bytes,
2329 old->extent_offset + old->offset + old->len);
2330 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2331
2332 ret = btrfs_drop_extents(trans, root, inode, start,
2333 start + len, 1);
2334 if (ret)
2335 goto out_free_path;
2336again:
2337 key.objectid = btrfs_ino(inode);
2338 key.type = BTRFS_EXTENT_DATA_KEY;
2339 key.offset = start;
2340
Liu Boa09a0a72013-03-11 09:20:58 +00002341 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002342 if (merge) {
2343 struct btrfs_file_extent_item *fi;
2344 u64 extent_len;
2345 struct btrfs_key found_key;
2346
2347 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2348 if (ret < 0)
2349 goto out_free_path;
2350
2351 path->slots[0]--;
2352 leaf = path->nodes[0];
2353 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2354
2355 fi = btrfs_item_ptr(leaf, path->slots[0],
2356 struct btrfs_file_extent_item);
2357 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2358
2359 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2360 extent_len + found_key.offset == start) {
2361 btrfs_set_file_extent_num_bytes(leaf, fi,
2362 extent_len + len);
2363 btrfs_mark_buffer_dirty(leaf);
2364 inode_add_bytes(inode, len);
2365
2366 ret = 1;
2367 goto out_free_path;
2368 } else {
2369 merge = false;
2370 btrfs_release_path(path);
2371 goto again;
2372 }
2373 }
2374
2375 ret = btrfs_insert_empty_item(trans, root, path, &key,
2376 sizeof(*extent));
2377 if (ret) {
2378 btrfs_abort_transaction(trans, root, ret);
2379 goto out_free_path;
2380 }
2381
2382 leaf = path->nodes[0];
2383 item = btrfs_item_ptr(leaf, path->slots[0],
2384 struct btrfs_file_extent_item);
2385 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2386 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2387 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2388 btrfs_set_file_extent_num_bytes(leaf, item, len);
2389 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2390 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2391 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2392 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2393 btrfs_set_file_extent_encryption(leaf, item, 0);
2394 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2395
2396 btrfs_mark_buffer_dirty(leaf);
2397 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002398 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002399
2400 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2401 new->disk_len, 0,
2402 backref->root_id, backref->inum,
2403 new->file_pos, 0); /* start - extent_offset */
2404 if (ret) {
2405 btrfs_abort_transaction(trans, root, ret);
2406 goto out_free_path;
2407 }
2408
2409 ret = 1;
2410out_free_path:
2411 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002412 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002413 btrfs_end_transaction(trans, root);
2414out_unlock:
2415 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2416 &cached, GFP_NOFS);
2417 iput(inode);
2418 return ret;
2419}
2420
2421static void relink_file_extents(struct new_sa_defrag_extent *new)
2422{
2423 struct btrfs_path *path;
2424 struct old_sa_defrag_extent *old, *tmp;
2425 struct sa_defrag_extent_backref *backref;
2426 struct sa_defrag_extent_backref *prev = NULL;
2427 struct inode *inode;
2428 struct btrfs_root *root;
2429 struct rb_node *node;
2430 int ret;
2431
2432 inode = new->inode;
2433 root = BTRFS_I(inode)->root;
2434
2435 path = btrfs_alloc_path();
2436 if (!path)
2437 return;
2438
2439 if (!record_extent_backrefs(path, new)) {
2440 btrfs_free_path(path);
2441 goto out;
2442 }
2443 btrfs_release_path(path);
2444
2445 while (1) {
2446 node = rb_first(&new->root);
2447 if (!node)
2448 break;
2449 rb_erase(node, &new->root);
2450
2451 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2452
2453 ret = relink_extent_backref(path, prev, backref);
2454 WARN_ON(ret < 0);
2455
2456 kfree(prev);
2457
2458 if (ret == 1)
2459 prev = backref;
2460 else
2461 prev = NULL;
2462 cond_resched();
2463 }
2464 kfree(prev);
2465
2466 btrfs_free_path(path);
2467
2468 list_for_each_entry_safe(old, tmp, &new->head, list) {
2469 list_del(&old->list);
2470 kfree(old);
2471 }
2472out:
2473 atomic_dec(&root->fs_info->defrag_running);
2474 wake_up(&root->fs_info->transaction_wait);
2475
2476 kfree(new);
2477}
2478
2479static struct new_sa_defrag_extent *
2480record_old_file_extents(struct inode *inode,
2481 struct btrfs_ordered_extent *ordered)
2482{
2483 struct btrfs_root *root = BTRFS_I(inode)->root;
2484 struct btrfs_path *path;
2485 struct btrfs_key key;
2486 struct old_sa_defrag_extent *old, *tmp;
2487 struct new_sa_defrag_extent *new;
2488 int ret;
2489
2490 new = kmalloc(sizeof(*new), GFP_NOFS);
2491 if (!new)
2492 return NULL;
2493
2494 new->inode = inode;
2495 new->file_pos = ordered->file_offset;
2496 new->len = ordered->len;
2497 new->bytenr = ordered->start;
2498 new->disk_len = ordered->disk_len;
2499 new->compress_type = ordered->compress_type;
2500 new->root = RB_ROOT;
2501 INIT_LIST_HEAD(&new->head);
2502
2503 path = btrfs_alloc_path();
2504 if (!path)
2505 goto out_kfree;
2506
2507 key.objectid = btrfs_ino(inode);
2508 key.type = BTRFS_EXTENT_DATA_KEY;
2509 key.offset = new->file_pos;
2510
2511 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2512 if (ret < 0)
2513 goto out_free_path;
2514 if (ret > 0 && path->slots[0] > 0)
2515 path->slots[0]--;
2516
2517 /* find out all the old extents for the file range */
2518 while (1) {
2519 struct btrfs_file_extent_item *extent;
2520 struct extent_buffer *l;
2521 int slot;
2522 u64 num_bytes;
2523 u64 offset;
2524 u64 end;
2525 u64 disk_bytenr;
2526 u64 extent_offset;
2527
2528 l = path->nodes[0];
2529 slot = path->slots[0];
2530
2531 if (slot >= btrfs_header_nritems(l)) {
2532 ret = btrfs_next_leaf(root, path);
2533 if (ret < 0)
2534 goto out_free_list;
2535 else if (ret > 0)
2536 break;
2537 continue;
2538 }
2539
2540 btrfs_item_key_to_cpu(l, &key, slot);
2541
2542 if (key.objectid != btrfs_ino(inode))
2543 break;
2544 if (key.type != BTRFS_EXTENT_DATA_KEY)
2545 break;
2546 if (key.offset >= new->file_pos + new->len)
2547 break;
2548
2549 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2550
2551 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2552 if (key.offset + num_bytes < new->file_pos)
2553 goto next;
2554
2555 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2556 if (!disk_bytenr)
2557 goto next;
2558
2559 extent_offset = btrfs_file_extent_offset(l, extent);
2560
2561 old = kmalloc(sizeof(*old), GFP_NOFS);
2562 if (!old)
2563 goto out_free_list;
2564
2565 offset = max(new->file_pos, key.offset);
2566 end = min(new->file_pos + new->len, key.offset + num_bytes);
2567
2568 old->bytenr = disk_bytenr;
2569 old->extent_offset = extent_offset;
2570 old->offset = offset - key.offset;
2571 old->len = end - offset;
2572 old->new = new;
2573 old->count = 0;
2574 list_add_tail(&old->list, &new->head);
2575next:
2576 path->slots[0]++;
2577 cond_resched();
2578 }
2579
2580 btrfs_free_path(path);
2581 atomic_inc(&root->fs_info->defrag_running);
2582
2583 return new;
2584
2585out_free_list:
2586 list_for_each_entry_safe(old, tmp, &new->head, list) {
2587 list_del(&old->list);
2588 kfree(old);
2589 }
2590out_free_path:
2591 btrfs_free_path(path);
2592out_kfree:
2593 kfree(new);
2594 return NULL;
2595}
2596
Chris Mason5d13a982009-03-13 11:41:46 -04002597/*
2598 * helper function for btrfs_finish_ordered_io, this
2599 * just reads in some of the csum leaves to prime them into ram
2600 * before we start the transaction. It limits the amount of btree
2601 * reads required while inside the transaction.
2602 */
Chris Masond352ac62008-09-29 15:18:18 -04002603/* as ordered data IO finishes, this gets called so we can finish
2604 * an ordered extent if the range of bytes in the file it covers are
2605 * fully written.
2606 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002607static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608{
Josef Bacik5fd02042012-05-02 14:00:54 -04002609 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002610 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002611 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002612 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002613 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002614 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002615 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002616 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002617 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002618
Liu Bo83eea1f2012-07-10 05:28:39 -06002619 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002620
Josef Bacik5fd02042012-05-02 14:00:54 -04002621 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2622 ret = -EIO;
2623 goto out;
2624 }
2625
Yan, Zhengc21677542009-11-12 09:34:21 +00002626 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002627 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002628 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2629 if (nolock)
2630 trans = btrfs_join_transaction_nolock(root);
2631 else
2632 trans = btrfs_join_transaction(root);
2633 if (IS_ERR(trans)) {
2634 ret = PTR_ERR(trans);
2635 trans = NULL;
2636 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00002637 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002638 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2639 ret = btrfs_update_inode_fallback(trans, root, inode);
2640 if (ret) /* -ENOMEM or corruption */
2641 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00002642 goto out;
2643 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002644
Josef Bacik2ac55d42010-02-03 19:33:23 +00002645 lock_extent_bits(io_tree, ordered_extent->file_offset,
2646 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002647 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002648
Liu Bo38c227d2013-01-29 03:18:40 +00002649 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2650 ordered_extent->file_offset + ordered_extent->len - 1,
2651 EXTENT_DEFRAG, 1, cached_state);
2652 if (ret) {
2653 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2654 if (last_snapshot >= BTRFS_I(inode)->generation)
2655 /* the inode is shared */
2656 new = record_old_file_extents(inode, ordered_extent);
2657
2658 clear_extent_bit(io_tree, ordered_extent->file_offset,
2659 ordered_extent->file_offset + ordered_extent->len - 1,
2660 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2661 }
2662
Josef Bacik0cb59c92010-07-02 12:14:14 -04002663 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002664 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002665 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002666 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002667 if (IS_ERR(trans)) {
2668 ret = PTR_ERR(trans);
2669 trans = NULL;
2670 goto out_unlock;
2671 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002672 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00002673
Chris Masonc8b97812008-10-29 14:49:59 -04002674 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002675 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002676 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002677 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002678 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002679 ordered_extent->file_offset,
2680 ordered_extent->file_offset +
2681 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002682 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002683 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002684 ret = insert_reserved_file_extent(trans, inode,
2685 ordered_extent->file_offset,
2686 ordered_extent->start,
2687 ordered_extent->disk_len,
2688 ordered_extent->len,
2689 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002690 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002691 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002692 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002693 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2694 ordered_extent->file_offset, ordered_extent->len,
2695 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002696 if (ret < 0) {
2697 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002698 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002699 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002700
Chris Masone6dcd2d2008-07-17 12:53:50 -04002701 add_pending_csums(trans, inode, ordered_extent->file_offset,
2702 &ordered_extent->list);
2703
Josef Bacik6c760c02012-11-09 10:53:21 -05002704 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2705 ret = btrfs_update_inode_fallback(trans, root, inode);
2706 if (ret) { /* -ENOMEM or corruption */
2707 btrfs_abort_transaction(trans, root, ret);
2708 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002709 }
2710 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002711out_unlock:
2712 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2713 ordered_extent->file_offset +
2714 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002715out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002716 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002717 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002718 if (trans)
2719 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002720
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002721 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002722 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2723 ordered_extent->file_offset +
2724 ordered_extent->len - 1, NULL, GFP_NOFS);
2725
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002726 /*
2727 * If the ordered extent had an IOERR or something else went
2728 * wrong we need to return the space for this ordered extent
2729 * back to the allocator.
2730 */
2731 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2732 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2733 btrfs_free_reserved_extent(root, ordered_extent->start,
2734 ordered_extent->disk_len);
2735 }
2736
2737
Josef Bacik5fd02042012-05-02 14:00:54 -04002738 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002739 * This needs to be done to make sure anybody waiting knows we are done
2740 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002741 */
2742 btrfs_remove_ordered_extent(inode, ordered_extent);
2743
Liu Bo38c227d2013-01-29 03:18:40 +00002744 /* for snapshot-aware defrag */
2745 if (new)
2746 relink_file_extents(new);
2747
Chris Masone6dcd2d2008-07-17 12:53:50 -04002748 /* once for us */
2749 btrfs_put_ordered_extent(ordered_extent);
2750 /* once for the tree */
2751 btrfs_put_ordered_extent(ordered_extent);
2752
Josef Bacik5fd02042012-05-02 14:00:54 -04002753 return ret;
2754}
2755
2756static void finish_ordered_fn(struct btrfs_work *work)
2757{
2758 struct btrfs_ordered_extent *ordered_extent;
2759 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2760 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002761}
2762
Christoph Hellwigb2950862008-12-02 09:54:17 -05002763static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002764 struct extent_state *state, int uptodate)
2765{
Josef Bacik5fd02042012-05-02 14:00:54 -04002766 struct inode *inode = page->mapping->host;
2767 struct btrfs_root *root = BTRFS_I(inode)->root;
2768 struct btrfs_ordered_extent *ordered_extent = NULL;
2769 struct btrfs_workers *workers;
2770
liubo1abe9b82011-03-24 11:18:59 +00002771 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2772
Chris Mason8b62b722009-09-02 16:53:46 -04002773 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002774 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2775 end - start + 1, uptodate))
2776 return 0;
2777
2778 ordered_extent->work.func = finish_ordered_fn;
2779 ordered_extent->work.flags = 0;
2780
Liu Bo83eea1f2012-07-10 05:28:39 -06002781 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002782 workers = &root->fs_info->endio_freespace_worker;
2783 else
2784 workers = &root->fs_info->endio_write_workers;
2785 btrfs_queue_worker(workers, &ordered_extent->work);
2786
2787 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002788}
2789
Chris Masond352ac62008-09-29 15:18:18 -04002790/*
Chris Masond352ac62008-09-29 15:18:18 -04002791 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002792 * if there's a match, we allow the bio to finish. If not, the code in
2793 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002794 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002795static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002796 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002797{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002798 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002799 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002800 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002801 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002802 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002803 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002804 struct btrfs_root *root = BTRFS_I(inode)->root;
2805 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002806 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2807 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002808
Chris Masond20f7042008-12-08 16:58:54 -05002809 if (PageChecked(page)) {
2810 ClearPageChecked(page);
2811 goto good;
2812 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002813
2814 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002815 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002816
Yan Zheng17d217f2008-12-12 10:03:38 -05002817 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002818 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002819 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2820 GFP_NOFS);
2821 return 0;
2822 }
2823
Yanc2e639f2008-02-04 08:57:25 -05002824 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002825 private = state->private;
2826 ret = 0;
2827 } else {
2828 ret = get_state_private(io_tree, start, &private);
2829 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002830 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002831 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002832 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002833
Liu Bob0496682013-03-14 14:57:45 +00002834 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002835 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002836 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002837 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002838
Cong Wang7ac687d2011-11-25 23:14:28 +08002839 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002840good:
Chris Mason07157aa2007-08-30 08:50:51 -04002841 return 0;
2842
2843zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002844 if (__ratelimit(&_rs))
2845 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2846 (unsigned long long)btrfs_ino(page->mapping->host),
2847 (unsigned long long)start, csum,
2848 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002849 memset(kaddr + offset, 1, end - start + 1);
2850 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002851 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002852 if (private == 0)
2853 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002854 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002855}
Chris Masonb888db22007-08-27 16:49:44 -04002856
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002857struct delayed_iput {
2858 struct list_head list;
2859 struct inode *inode;
2860};
2861
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002862/* JDM: If this is fs-wide, why can't we add a pointer to
2863 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002864void btrfs_add_delayed_iput(struct inode *inode)
2865{
2866 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2867 struct delayed_iput *delayed;
2868
2869 if (atomic_add_unless(&inode->i_count, -1, 1))
2870 return;
2871
2872 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2873 delayed->inode = inode;
2874
2875 spin_lock(&fs_info->delayed_iput_lock);
2876 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2877 spin_unlock(&fs_info->delayed_iput_lock);
2878}
2879
2880void btrfs_run_delayed_iputs(struct btrfs_root *root)
2881{
2882 LIST_HEAD(list);
2883 struct btrfs_fs_info *fs_info = root->fs_info;
2884 struct delayed_iput *delayed;
2885 int empty;
2886
2887 spin_lock(&fs_info->delayed_iput_lock);
2888 empty = list_empty(&fs_info->delayed_iputs);
2889 spin_unlock(&fs_info->delayed_iput_lock);
2890 if (empty)
2891 return;
2892
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002893 spin_lock(&fs_info->delayed_iput_lock);
2894 list_splice_init(&fs_info->delayed_iputs, &list);
2895 spin_unlock(&fs_info->delayed_iput_lock);
2896
2897 while (!list_empty(&list)) {
2898 delayed = list_entry(list.next, struct delayed_iput, list);
2899 list_del(&delayed->list);
2900 iput(delayed->inode);
2901 kfree(delayed);
2902 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002903}
2904
Yan, Zhengd68fc572010-05-16 10:49:58 -04002905/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002906 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002907 * files in the subvolume, it removes orphan item and frees block_rsv
2908 * structure.
2909 */
2910void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2911 struct btrfs_root *root)
2912{
Josef Bacik90290e12011-12-02 15:44:12 -05002913 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002914 int ret;
2915
Josef Bacik8a35d952012-05-23 14:26:42 -04002916 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002917 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2918 return;
2919
Josef Bacik90290e12011-12-02 15:44:12 -05002920 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002921 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002922 spin_unlock(&root->orphan_lock);
2923 return;
2924 }
2925
2926 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2927 spin_unlock(&root->orphan_lock);
2928 return;
2929 }
2930
2931 block_rsv = root->orphan_block_rsv;
2932 root->orphan_block_rsv = NULL;
2933 spin_unlock(&root->orphan_lock);
2934
Yan, Zhengd68fc572010-05-16 10:49:58 -04002935 if (root->orphan_item_inserted &&
2936 btrfs_root_refs(&root->root_item) > 0) {
2937 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2938 root->root_key.objectid);
2939 BUG_ON(ret);
2940 root->orphan_item_inserted = 0;
2941 }
2942
Josef Bacik90290e12011-12-02 15:44:12 -05002943 if (block_rsv) {
2944 WARN_ON(block_rsv->size > 0);
2945 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002946 }
2947}
2948
2949/*
Josef Bacik7b128762008-07-24 12:17:14 -04002950 * This creates an orphan entry for the given inode in case something goes
2951 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952 *
2953 * NOTE: caller of this function should reserve 5 units of metadata for
2954 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002955 */
2956int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2957{
2958 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 struct btrfs_block_rsv *block_rsv = NULL;
2960 int reserve = 0;
2961 int insert = 0;
2962 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002963
Yan, Zhengd68fc572010-05-16 10:49:58 -04002964 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002965 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002966 if (!block_rsv)
2967 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002968 }
2969
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970 spin_lock(&root->orphan_lock);
2971 if (!root->orphan_block_rsv) {
2972 root->orphan_block_rsv = block_rsv;
2973 } else if (block_rsv) {
2974 btrfs_free_block_rsv(root, block_rsv);
2975 block_rsv = NULL;
2976 }
Josef Bacik7b128762008-07-24 12:17:14 -04002977
Josef Bacik8a35d952012-05-23 14:26:42 -04002978 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2979 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002980#if 0
2981 /*
2982 * For proper ENOSPC handling, we should do orphan
2983 * cleanup when mounting. But this introduces backward
2984 * compatibility issue.
2985 */
2986 if (!xchg(&root->orphan_item_inserted, 1))
2987 insert = 2;
2988 else
2989 insert = 1;
2990#endif
2991 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002992 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 }
Josef Bacik7b128762008-07-24 12:17:14 -04002994
Josef Bacik72ac3c02012-05-23 14:13:11 -04002995 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2996 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002998 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002999
Yan, Zhengd68fc572010-05-16 10:49:58 -04003000 /* grab metadata reservation from transaction handle */
3001 if (reserve) {
3002 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003003 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003004 }
3005
3006 /* insert an orphan item to track this unlinked/truncated file */
3007 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003008 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003009 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003010 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3011 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003012 btrfs_abort_transaction(trans, root, ret);
3013 return ret;
3014 }
3015 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003016 }
3017
3018 /* insert an orphan item to track subvolume contains orphan files */
3019 if (insert >= 2) {
3020 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3021 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003022 if (ret && ret != -EEXIST) {
3023 btrfs_abort_transaction(trans, root, ret);
3024 return ret;
3025 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 }
3027 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003028}
3029
3030/*
3031 * We have done the truncate/delete so we can go ahead and remove the orphan
3032 * item for this particular inode.
3033 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003034static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3035 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003036{
3037 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003038 int delete_item = 0;
3039 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003040 int ret = 0;
3041
Yan, Zhengd68fc572010-05-16 10:49:58 -04003042 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003043 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3044 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003045 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003046
Josef Bacik72ac3c02012-05-23 14:13:11 -04003047 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3048 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003049 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 spin_unlock(&root->orphan_lock);
3051
3052 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003053 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003054 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003055 }
3056
Josef Bacik8a35d952012-05-23 14:26:42 -04003057 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003058 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003059 atomic_dec(&root->orphan_inodes);
3060 }
Josef Bacik7b128762008-07-24 12:17:14 -04003061
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003063}
3064
3065/*
3066 * this cleans up any orphans that may be left on the list from the last use
3067 * of this root.
3068 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003069int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003070{
3071 struct btrfs_path *path;
3072 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003073 struct btrfs_key key, found_key;
3074 struct btrfs_trans_handle *trans;
3075 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003076 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003077 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3078
Yan, Zhengd68fc572010-05-16 10:49:58 -04003079 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003080 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003081
3082 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003083 if (!path) {
3084 ret = -ENOMEM;
3085 goto out;
3086 }
Josef Bacik7b128762008-07-24 12:17:14 -04003087 path->reada = -1;
3088
3089 key.objectid = BTRFS_ORPHAN_OBJECTID;
3090 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3091 key.offset = (u64)-1;
3092
Josef Bacik7b128762008-07-24 12:17:14 -04003093 while (1) {
3094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003095 if (ret < 0)
3096 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003097
3098 /*
3099 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003100 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003101 * find the key and see if we have stuff that matches
3102 */
3103 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003104 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003105 if (path->slots[0] == 0)
3106 break;
3107 path->slots[0]--;
3108 }
3109
3110 /* pull out the item */
3111 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003112 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3113
3114 /* make sure the item matches what we want */
3115 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3116 break;
3117 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3118 break;
3119
3120 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003121 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003122
3123 /*
3124 * this is where we are basically btrfs_lookup, without the
3125 * crossing root thing. we store the inode number in the
3126 * offset of the orphan item.
3127 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003128
3129 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003130 btrfs_err(root->fs_info,
3131 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003132 ret = -EINVAL;
3133 goto out;
3134 }
3135
3136 last_objectid = found_key.offset;
3137
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003138 found_key.objectid = found_key.offset;
3139 found_key.type = BTRFS_INODE_ITEM_KEY;
3140 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003141 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003142 ret = PTR_RET(inode);
3143 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003144 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003145
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003146 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3147 struct btrfs_root *dead_root;
3148 struct btrfs_fs_info *fs_info = root->fs_info;
3149 int is_dead_root = 0;
3150
3151 /*
3152 * this is an orphan in the tree root. Currently these
3153 * could come from 2 sources:
3154 * a) a snapshot deletion in progress
3155 * b) a free space cache inode
3156 * We need to distinguish those two, as the snapshot
3157 * orphan must not get deleted.
3158 * find_dead_roots already ran before us, so if this
3159 * is a snapshot deletion, we should find the root
3160 * in the dead_roots list
3161 */
3162 spin_lock(&fs_info->trans_lock);
3163 list_for_each_entry(dead_root, &fs_info->dead_roots,
3164 root_list) {
3165 if (dead_root->root_key.objectid ==
3166 found_key.objectid) {
3167 is_dead_root = 1;
3168 break;
3169 }
3170 }
3171 spin_unlock(&fs_info->trans_lock);
3172 if (is_dead_root) {
3173 /* prevent this orphan from being found again */
3174 key.offset = found_key.objectid - 1;
3175 continue;
3176 }
3177 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003178 /*
3179 * Inode is already gone but the orphan item is still there,
3180 * kill the orphan item.
3181 */
3182 if (ret == -ESTALE) {
3183 trans = btrfs_start_transaction(root, 1);
3184 if (IS_ERR(trans)) {
3185 ret = PTR_ERR(trans);
3186 goto out;
3187 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003188 btrfs_debug(root->fs_info, "auto deleting %Lu",
3189 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003190 ret = btrfs_del_orphan_item(trans, root,
3191 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003192 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003193 btrfs_end_transaction(trans, root);
3194 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003195 }
Josef Bacik7b128762008-07-24 12:17:14 -04003196
Josef Bacik7b128762008-07-24 12:17:14 -04003197 /*
3198 * add this inode to the orphan list so btrfs_orphan_del does
3199 * the proper thing when we hit it
3200 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003201 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3202 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003203 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003204
Josef Bacik7b128762008-07-24 12:17:14 -04003205 /* if we have links, this was a truncate, lets do that */
3206 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003207 if (!S_ISREG(inode->i_mode)) {
3208 WARN_ON(1);
3209 iput(inode);
3210 continue;
3211 }
Josef Bacik7b128762008-07-24 12:17:14 -04003212 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003213
3214 /* 1 for the orphan item deletion. */
3215 trans = btrfs_start_transaction(root, 1);
3216 if (IS_ERR(trans)) {
3217 ret = PTR_ERR(trans);
3218 goto out;
3219 }
3220 ret = btrfs_orphan_add(trans, inode);
3221 btrfs_end_transaction(trans, root);
3222 if (ret)
3223 goto out;
3224
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003225 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003226 if (ret)
3227 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003228 } else {
3229 nr_unlink++;
3230 }
3231
3232 /* this will do delete_inode and everything for us */
3233 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003234 if (ret)
3235 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003236 }
Miao Xie3254c872011-11-10 20:45:05 -05003237 /* release the path since we're done with it */
3238 btrfs_release_path(path);
3239
Yan, Zhengd68fc572010-05-16 10:49:58 -04003240 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3241
3242 if (root->orphan_block_rsv)
3243 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3244 (u64)-1);
3245
3246 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003247 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003248 if (!IS_ERR(trans))
3249 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 }
Josef Bacik7b128762008-07-24 12:17:14 -04003251
3252 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003253 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003254 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003255 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003256
3257out:
3258 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003259 btrfs_crit(root->fs_info,
3260 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003261 btrfs_free_path(path);
3262 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003263}
3264
Chris Masond352ac62008-09-29 15:18:18 -04003265/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003266 * very simple check to peek ahead in the leaf looking for xattrs. If we
3267 * don't find any xattrs, we know there can't be any acls.
3268 *
3269 * slot is the slot the inode is in, objectid is the objectid of the inode
3270 */
3271static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3272 int slot, u64 objectid)
3273{
3274 u32 nritems = btrfs_header_nritems(leaf);
3275 struct btrfs_key found_key;
3276 int scanned = 0;
3277
3278 slot++;
3279 while (slot < nritems) {
3280 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3281
3282 /* we found a different objectid, there must not be acls */
3283 if (found_key.objectid != objectid)
3284 return 0;
3285
3286 /* we found an xattr, assume we've got an acl */
3287 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3288 return 1;
3289
3290 /*
3291 * we found a key greater than an xattr key, there can't
3292 * be any acls later on
3293 */
3294 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3295 return 0;
3296
3297 slot++;
3298 scanned++;
3299
3300 /*
3301 * it goes inode, inode backrefs, xattrs, extents,
3302 * so if there are a ton of hard links to an inode there can
3303 * be a lot of backrefs. Don't waste time searching too hard,
3304 * this is just an optimization
3305 */
3306 if (scanned >= 8)
3307 break;
3308 }
3309 /* we hit the end of the leaf before we found an xattr or
3310 * something larger than an xattr. We have to assume the inode
3311 * has acls
3312 */
3313 return 1;
3314}
3315
3316/*
Chris Masond352ac62008-09-29 15:18:18 -04003317 * read an inode from the btree into the in-memory inode
3318 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003319static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003320{
3321 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003322 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003323 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003324 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003325 struct btrfs_root *root = BTRFS_I(inode)->root;
3326 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003327 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003328 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003329 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003330 bool filled = false;
3331
3332 ret = btrfs_fill_inode(inode, &rdev);
3333 if (!ret)
3334 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003335
3336 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003337 if (!path)
3338 goto make_bad;
3339
Josef Bacikd90c7322011-05-17 09:50:54 -04003340 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003341 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003342
Chris Mason39279cc2007-06-12 06:35:45 -04003343 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003344 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003345 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003346
Chris Mason5f39d392007-10-15 16:14:19 -04003347 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003348
3349 if (filled)
3350 goto cache_acl;
3351
Chris Mason5f39d392007-10-15 16:14:19 -04003352 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3353 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003354 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003355 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003356 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3357 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003358 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003359
3360 tspec = btrfs_inode_atime(inode_item);
3361 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3362 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3363
3364 tspec = btrfs_inode_mtime(inode_item);
3365 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3366 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3367
3368 tspec = btrfs_inode_ctime(inode_item);
3369 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3370 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3371
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003372 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003373 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003374 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3375
3376 /*
3377 * If we were modified in the current generation and evicted from memory
3378 * and then re-read we need to do a full sync since we don't have any
3379 * idea about which extents were modified before we were evicted from
3380 * cache.
3381 */
3382 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3383 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3384 &BTRFS_I(inode)->runtime_flags);
3385
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003386 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003387 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003388 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003389 rdev = btrfs_inode_rdev(leaf, inode_item);
3390
Josef Bacikaec74772008-07-24 12:12:38 -04003391 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003392 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003393cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003394 /*
3395 * try to precache a NULL acl entry for files that don't have
3396 * any xattrs or acls
3397 */
Li Zefan33345d012011-04-20 10:31:50 +08003398 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3399 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003400 if (!maybe_acls)
3401 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003402
Chris Mason39279cc2007-06-12 06:35:45 -04003403 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003404
Chris Mason39279cc2007-06-12 06:35:45 -04003405 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003406 case S_IFREG:
3407 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003408 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003409 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003410 inode->i_fop = &btrfs_file_operations;
3411 inode->i_op = &btrfs_file_inode_operations;
3412 break;
3413 case S_IFDIR:
3414 inode->i_fop = &btrfs_dir_file_operations;
3415 if (root == root->fs_info->tree_root)
3416 inode->i_op = &btrfs_dir_ro_inode_operations;
3417 else
3418 inode->i_op = &btrfs_dir_inode_operations;
3419 break;
3420 case S_IFLNK:
3421 inode->i_op = &btrfs_symlink_inode_operations;
3422 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003423 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003424 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003425 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003426 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003427 init_special_inode(inode, inode->i_mode, rdev);
3428 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003429 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003430
3431 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003432 return;
3433
3434make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003435 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003436 make_bad_inode(inode);
3437}
3438
Chris Masond352ac62008-09-29 15:18:18 -04003439/*
3440 * given a leaf and an inode, copy the inode fields into the leaf
3441 */
Chris Masone02119d2008-09-05 16:13:11 -04003442static void fill_inode_item(struct btrfs_trans_handle *trans,
3443 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003444 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003445 struct inode *inode)
3446{
Liu Bo51fab692012-12-27 09:01:21 +00003447 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003448
Liu Bo51fab692012-12-27 09:01:21 +00003449 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003450
Liu Bo51fab692012-12-27 09:01:21 +00003451 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3452 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3453 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3454 &token);
3455 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3456 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003457
Liu Bo51fab692012-12-27 09:01:21 +00003458 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3459 inode->i_atime.tv_sec, &token);
3460 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3461 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003462
Liu Bo51fab692012-12-27 09:01:21 +00003463 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3464 inode->i_mtime.tv_sec, &token);
3465 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3466 inode->i_mtime.tv_nsec, &token);
3467
3468 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3469 inode->i_ctime.tv_sec, &token);
3470 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3471 inode->i_ctime.tv_nsec, &token);
3472
3473 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3474 &token);
3475 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3476 &token);
3477 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3478 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3479 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3480 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3481 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003482}
3483
Chris Masond352ac62008-09-29 15:18:18 -04003484/*
3485 * copy everything in the in-memory inode into the btree.
3486 */
Chris Mason21151332011-11-10 20:39:08 -05003487static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003488 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003489{
3490 struct btrfs_inode_item *inode_item;
3491 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003492 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003493 int ret;
3494
3495 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003496 if (!path)
3497 return -ENOMEM;
3498
Chris Masonb9473432009-03-13 11:00:37 -04003499 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003500 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3501 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003502 if (ret) {
3503 if (ret > 0)
3504 ret = -ENOENT;
3505 goto failed;
3506 }
3507
Chris Masonb4ce94d2009-02-04 09:25:08 -05003508 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 leaf = path->nodes[0];
3510 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003511 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003512
Chris Masone02119d2008-09-05 16:13:11 -04003513 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003514 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003515 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003516 ret = 0;
3517failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003518 btrfs_free_path(path);
3519 return ret;
3520}
3521
Chris Masond352ac62008-09-29 15:18:18 -04003522/*
Chris Mason21151332011-11-10 20:39:08 -05003523 * copy everything in the in-memory inode into the btree.
3524 */
3525noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3526 struct btrfs_root *root, struct inode *inode)
3527{
3528 int ret;
3529
3530 /*
3531 * If the inode is a free space inode, we can deadlock during commit
3532 * if we put it into the delayed code.
3533 *
3534 * The data relocation inode should also be directly updated
3535 * without delay
3536 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003537 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003538 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003539 btrfs_update_root_times(trans, root);
3540
Chris Mason21151332011-11-10 20:39:08 -05003541 ret = btrfs_delayed_update_inode(trans, root, inode);
3542 if (!ret)
3543 btrfs_set_inode_last_trans(trans, inode);
3544 return ret;
3545 }
3546
3547 return btrfs_update_inode_item(trans, root, inode);
3548}
3549
Josef Bacikbe6aef62012-10-22 15:43:12 -04003550noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *root,
3552 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003553{
3554 int ret;
3555
3556 ret = btrfs_update_inode(trans, root, inode);
3557 if (ret == -ENOSPC)
3558 return btrfs_update_inode_item(trans, root, inode);
3559 return ret;
3560}
3561
3562/*
Chris Masond352ac62008-09-29 15:18:18 -04003563 * unlink helper that gets used here in inode.c and in the tree logging
3564 * recovery code. It remove a link in a directory with a given name, and
3565 * also drops the back refs in the inode to the directory
3566 */
Al Viro92986792011-03-04 17:14:37 +00003567static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3568 struct btrfs_root *root,
3569 struct inode *dir, struct inode *inode,
3570 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003571{
3572 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003573 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003574 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003575 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003576 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003577 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003578 u64 ino = btrfs_ino(inode);
3579 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003580
3581 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003582 if (!path) {
3583 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003584 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003585 }
3586
Chris Masonb9473432009-03-13 11:00:37 -04003587 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003588 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003589 name, name_len, -1);
3590 if (IS_ERR(di)) {
3591 ret = PTR_ERR(di);
3592 goto err;
3593 }
3594 if (!di) {
3595 ret = -ENOENT;
3596 goto err;
3597 }
Chris Mason5f39d392007-10-15 16:14:19 -04003598 leaf = path->nodes[0];
3599 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003600 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003601 if (ret)
3602 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003603 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003604
Li Zefan33345d012011-04-20 10:31:50 +08003605 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3606 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003607 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003608 btrfs_info(root->fs_info,
3609 "failed to delete reference to %.*s, inode %llu parent %llu",
3610 name_len, name,
3611 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003613 goto err;
3614 }
3615
Miao Xie16cdcec2011-04-22 18:12:22 +08003616 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003617 if (ret) {
3618 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003619 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003620 }
Chris Mason39279cc2007-06-12 06:35:45 -04003621
Chris Masone02119d2008-09-05 16:13:11 -04003622 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003623 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003624 if (ret != 0 && ret != -ENOENT) {
3625 btrfs_abort_transaction(trans, root, ret);
3626 goto err;
3627 }
Chris Masone02119d2008-09-05 16:13:11 -04003628
3629 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3630 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003631 if (ret == -ENOENT)
3632 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003633 else if (ret)
3634 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003635err:
3636 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003637 if (ret)
3638 goto out;
3639
3640 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003641 inode_inc_iversion(inode);
3642 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003643 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003644 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003645out:
Chris Mason39279cc2007-06-12 06:35:45 -04003646 return ret;
3647}
3648
Al Viro92986792011-03-04 17:14:37 +00003649int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3650 struct btrfs_root *root,
3651 struct inode *dir, struct inode *inode,
3652 const char *name, int name_len)
3653{
3654 int ret;
3655 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3656 if (!ret) {
3657 btrfs_drop_nlink(inode);
3658 ret = btrfs_update_inode(trans, root, inode);
3659 }
3660 return ret;
3661}
3662
3663
Yan, Zhenga22285a2010-05-16 10:48:46 -04003664/* helper to check if there is any shared block in the path */
3665static int check_path_shared(struct btrfs_root *root,
3666 struct btrfs_path *path)
3667{
3668 struct extent_buffer *eb;
3669 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00003670 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003671
3672 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003673 int ret;
3674
Yan, Zhenga22285a2010-05-16 10:48:46 -04003675 if (!path->nodes[level])
3676 break;
3677 eb = path->nodes[level];
3678 if (!btrfs_block_can_be_shared(root, eb))
3679 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003680 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681 &refs, NULL);
3682 if (refs > 1)
3683 return 1;
3684 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003685 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003686}
3687
3688/*
3689 * helper to start transaction for unlink and rmdir.
3690 *
3691 * unlink and rmdir are special in btrfs, they do not always free space.
3692 * so in enospc case, we should make sure they will free space before
3693 * allowing them to use the global metadata reservation.
3694 */
3695static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3696 struct dentry *dentry)
3697{
3698 struct btrfs_trans_handle *trans;
3699 struct btrfs_root *root = BTRFS_I(dir)->root;
3700 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701 struct btrfs_dir_item *di;
3702 struct inode *inode = dentry->d_inode;
3703 u64 index;
3704 int check_link = 1;
3705 int err = -ENOSPC;
3706 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003707 u64 ino = btrfs_ino(inode);
3708 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003709
Josef Bacike70bea52011-10-11 14:18:24 -04003710 /*
3711 * 1 for the possible orphan item
3712 * 1 for the dir item
3713 * 1 for the dir index
3714 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003715 * 1 for the inode
3716 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003717 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3719 return trans;
3720
Li Zefan33345d012011-04-20 10:31:50 +08003721 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003722 return ERR_PTR(-ENOSPC);
3723
3724 /* check if there is someone else holds reference */
3725 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3726 return ERR_PTR(-ENOSPC);
3727
3728 if (atomic_read(&inode->i_count) > 2)
3729 return ERR_PTR(-ENOSPC);
3730
3731 if (xchg(&root->fs_info->enospc_unlink, 1))
3732 return ERR_PTR(-ENOSPC);
3733
3734 path = btrfs_alloc_path();
3735 if (!path) {
3736 root->fs_info->enospc_unlink = 0;
3737 return ERR_PTR(-ENOMEM);
3738 }
3739
Josef Bacik3880a1b2011-10-14 14:46:51 -04003740 /* 1 for the orphan item */
3741 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003742 if (IS_ERR(trans)) {
3743 btrfs_free_path(path);
3744 root->fs_info->enospc_unlink = 0;
3745 return trans;
3746 }
3747
3748 path->skip_locking = 1;
3749 path->search_commit_root = 1;
3750
3751 ret = btrfs_lookup_inode(trans, root, path,
3752 &BTRFS_I(dir)->location, 0);
3753 if (ret < 0) {
3754 err = ret;
3755 goto out;
3756 }
3757 if (ret == 0) {
3758 if (check_path_shared(root, path))
3759 goto out;
3760 } else {
3761 check_link = 0;
3762 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003763 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003764
3765 ret = btrfs_lookup_inode(trans, root, path,
3766 &BTRFS_I(inode)->location, 0);
3767 if (ret < 0) {
3768 err = ret;
3769 goto out;
3770 }
3771 if (ret == 0) {
3772 if (check_path_shared(root, path))
3773 goto out;
3774 } else {
3775 check_link = 0;
3776 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003777 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003778
3779 if (ret == 0 && S_ISREG(inode->i_mode)) {
3780 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003781 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003782 if (ret < 0) {
3783 err = ret;
3784 goto out;
3785 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003786 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003787 if (check_path_shared(root, path))
3788 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003789 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003790 }
3791
3792 if (!check_link) {
3793 err = 0;
3794 goto out;
3795 }
3796
Li Zefan33345d012011-04-20 10:31:50 +08003797 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003798 dentry->d_name.name, dentry->d_name.len, 0);
3799 if (IS_ERR(di)) {
3800 err = PTR_ERR(di);
3801 goto out;
3802 }
3803 if (di) {
3804 if (check_path_shared(root, path))
3805 goto out;
3806 } else {
3807 err = 0;
3808 goto out;
3809 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003810 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003811
Mark Fashehf1863732012-08-08 11:32:27 -07003812 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3813 dentry->d_name.len, ino, dir_ino, 0,
3814 &index);
3815 if (ret) {
3816 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003817 goto out;
3818 }
Mark Fashehf1863732012-08-08 11:32:27 -07003819
Yan, Zhenga22285a2010-05-16 10:48:46 -04003820 if (check_path_shared(root, path))
3821 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003822
David Sterbab3b4aa72011-04-21 01:20:15 +02003823 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003824
Miao Xie16cdcec2011-04-22 18:12:22 +08003825 /*
3826 * This is a commit root search, if we can lookup inode item and other
3827 * relative items in the commit root, it means the transaction of
3828 * dir/file creation has been committed, and the dir index item that we
3829 * delay to insert has also been inserted into the commit root. So
3830 * we needn't worry about the delayed insertion of the dir index item
3831 * here.
3832 */
Li Zefan33345d012011-04-20 10:31:50 +08003833 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003834 dentry->d_name.name, dentry->d_name.len, 0);
3835 if (IS_ERR(di)) {
3836 err = PTR_ERR(di);
3837 goto out;
3838 }
3839 BUG_ON(ret == -ENOENT);
3840 if (check_path_shared(root, path))
3841 goto out;
3842
3843 err = 0;
3844out:
3845 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003846 /* Migrate the orphan reservation over */
3847 if (!err)
3848 err = btrfs_block_rsv_migrate(trans->block_rsv,
3849 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003850 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003851
Yan, Zhenga22285a2010-05-16 10:48:46 -04003852 if (err) {
3853 btrfs_end_transaction(trans, root);
3854 root->fs_info->enospc_unlink = 0;
3855 return ERR_PTR(err);
3856 }
3857
3858 trans->block_rsv = &root->fs_info->global_block_rsv;
3859 return trans;
3860}
3861
3862static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3863 struct btrfs_root *root)
3864{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003865 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003866 btrfs_block_rsv_release(root, trans->block_rsv,
3867 trans->bytes_reserved);
3868 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003869 BUG_ON(!root->fs_info->enospc_unlink);
3870 root->fs_info->enospc_unlink = 0;
3871 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003872 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003873}
3874
Chris Mason39279cc2007-06-12 06:35:45 -04003875static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3876{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003877 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003878 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003879 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003880 int ret;
3881
Yan, Zhenga22285a2010-05-16 10:48:46 -04003882 trans = __unlink_start_trans(dir, dentry);
3883 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003884 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003885
Chris Mason12fcfd22009-03-24 10:24:20 -04003886 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3887
Chris Masone02119d2008-09-05 16:13:11 -04003888 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3889 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003890 if (ret)
3891 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003892
Yan, Zhenga22285a2010-05-16 10:48:46 -04003893 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003894 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003895 if (ret)
3896 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003897 }
Josef Bacik7b128762008-07-24 12:17:14 -04003898
Tsutomu Itohb5324022011-07-19 07:27:20 +00003899out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003900 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003901 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003902 return ret;
3903}
3904
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003905int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3906 struct btrfs_root *root,
3907 struct inode *dir, u64 objectid,
3908 const char *name, int name_len)
3909{
3910 struct btrfs_path *path;
3911 struct extent_buffer *leaf;
3912 struct btrfs_dir_item *di;
3913 struct btrfs_key key;
3914 u64 index;
3915 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003916 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003917
3918 path = btrfs_alloc_path();
3919 if (!path)
3920 return -ENOMEM;
3921
Li Zefan33345d012011-04-20 10:31:50 +08003922 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003923 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003924 if (IS_ERR_OR_NULL(di)) {
3925 if (!di)
3926 ret = -ENOENT;
3927 else
3928 ret = PTR_ERR(di);
3929 goto out;
3930 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003931
3932 leaf = path->nodes[0];
3933 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3934 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3935 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003936 if (ret) {
3937 btrfs_abort_transaction(trans, root, ret);
3938 goto out;
3939 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003940 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003941
3942 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3943 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003944 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003946 if (ret != -ENOENT) {
3947 btrfs_abort_transaction(trans, root, ret);
3948 goto out;
3949 }
Li Zefan33345d012011-04-20 10:31:50 +08003950 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003951 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003952 if (IS_ERR_OR_NULL(di)) {
3953 if (!di)
3954 ret = -ENOENT;
3955 else
3956 ret = PTR_ERR(di);
3957 btrfs_abort_transaction(trans, root, ret);
3958 goto out;
3959 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003960
3961 leaf = path->nodes[0];
3962 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003963 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003964 index = key.offset;
3965 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003966 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003967
Miao Xie16cdcec2011-04-22 18:12:22 +08003968 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003969 if (ret) {
3970 btrfs_abort_transaction(trans, root, ret);
3971 goto out;
3972 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003973
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003974 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003975 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003976 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003977 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003978 if (ret)
3979 btrfs_abort_transaction(trans, root, ret);
3980out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003981 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003982 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003983}
3984
Chris Mason39279cc2007-06-12 06:35:45 -04003985static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3986{
3987 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003988 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003989 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003991
David Sterbab3ae2442012-09-13 16:04:34 -06003992 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003993 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003994 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3995 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003996
Yan, Zhenga22285a2010-05-16 10:48:46 -04003997 trans = __unlink_start_trans(dir, dentry);
3998 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003999 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004000
Li Zefan33345d012011-04-20 10:31:50 +08004001 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004002 err = btrfs_unlink_subvol(trans, root, dir,
4003 BTRFS_I(inode)->location.objectid,
4004 dentry->d_name.name,
4005 dentry->d_name.len);
4006 goto out;
4007 }
4008
Josef Bacik7b128762008-07-24 12:17:14 -04004009 err = btrfs_orphan_add(trans, inode);
4010 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004011 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004012
Chris Mason39279cc2007-06-12 06:35:45 -04004013 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004014 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4015 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004016 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004017 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004018out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004020 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004021
Chris Mason39279cc2007-06-12 06:35:45 -04004022 return err;
4023}
4024
Chris Mason323ac952008-10-01 19:05:46 -04004025/*
Chris Mason39279cc2007-06-12 06:35:45 -04004026 * this can truncate away extent items, csum items and directory items.
4027 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004028 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004029 *
4030 * csum items that cross the new i_size are truncated to the new size
4031 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004032 *
4033 * min_type is the minimum key type to truncate down to. If set to 0, this
4034 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004035 */
Yan, Zheng80825102009-11-12 09:35:36 +00004036int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4037 struct btrfs_root *root,
4038 struct inode *inode,
4039 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004040{
Chris Mason39279cc2007-06-12 06:35:45 -04004041 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004042 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004043 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004044 struct btrfs_key key;
4045 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004047 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004049 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004050 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004051 int found_extent;
4052 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004053 int pending_del_nr = 0;
4054 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004055 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004056 int ret;
4057 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004058 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004059
4060 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004061
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004062 path = btrfs_alloc_path();
4063 if (!path)
4064 return -ENOMEM;
4065 path->reada = -1;
4066
Josef Bacik5dc562c2012-08-17 13:14:17 -04004067 /*
4068 * We want to drop from the next block forward in case this new size is
4069 * not block aligned since we will be keeping the last block of the
4070 * extent just the way it is.
4071 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004072 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004073 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4074 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004075
Miao Xie16cdcec2011-04-22 18:12:22 +08004076 /*
4077 * This function is also used to drop the items in the log tree before
4078 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4079 * it is used to drop the loged items. So we shouldn't kill the delayed
4080 * items.
4081 */
4082 if (min_type == 0 && root == BTRFS_I(inode)->root)
4083 btrfs_kill_delayed_inode_items(inode);
4084
Li Zefan33345d012011-04-20 10:31:50 +08004085 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004086 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004087 key.type = (u8)-1;
4088
Chris Mason85e21ba2008-01-29 15:11:36 -05004089search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004090 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004091 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004092 if (ret < 0) {
4093 err = ret;
4094 goto out;
4095 }
Chris Masond3977122009-01-05 21:25:51 -05004096
Chris Mason85e21ba2008-01-29 15:11:36 -05004097 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004098 /* there are no items in the tree for us to truncate, we're
4099 * done
4100 */
Yan, Zheng80825102009-11-12 09:35:36 +00004101 if (path->slots[0] == 0)
4102 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004103 path->slots[0]--;
4104 }
4105
Chris Masond3977122009-01-05 21:25:51 -05004106 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004107 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004108 leaf = path->nodes[0];
4109 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4110 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004111
Li Zefan33345d012011-04-20 10:31:50 +08004112 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004113 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004114
Chris Mason85e21ba2008-01-29 15:11:36 -05004115 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004116 break;
4117
Chris Mason5f39d392007-10-15 16:14:19 -04004118 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004119 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004120 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004121 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004122 extent_type = btrfs_file_extent_type(leaf, fi);
4123 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004124 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004125 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004126 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004127 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004128 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004129 }
Yan008630c2007-11-07 13:31:09 -05004130 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004131 }
Yan, Zheng80825102009-11-12 09:35:36 +00004132 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004133 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004134 } else {
4135 if (item_end < new_size)
4136 break;
4137 if (found_key.offset >= new_size)
4138 del_item = 1;
4139 else
4140 del_item = 0;
4141 }
Chris Mason39279cc2007-06-12 06:35:45 -04004142 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004143 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004144 if (found_type != BTRFS_EXTENT_DATA_KEY)
4145 goto delete;
4146
4147 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004148 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004149 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004150 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004151 u64 orig_num_bytes =
4152 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004153 extent_num_bytes = ALIGN(new_size -
4154 found_key.offset,
4155 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004156 btrfs_set_file_extent_num_bytes(leaf, fi,
4157 extent_num_bytes);
4158 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004159 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004160 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004161 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004162 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004163 } else {
Chris Masondb945352007-10-15 16:15:53 -04004164 extent_num_bytes =
4165 btrfs_file_extent_disk_num_bytes(leaf,
4166 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004167 extent_offset = found_key.offset -
4168 btrfs_file_extent_offset(leaf, fi);
4169
Chris Mason39279cc2007-06-12 06:35:45 -04004170 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004171 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004172 if (extent_start != 0) {
4173 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004174 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004175 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004176 }
4177 }
Chris Mason90692182008-02-08 13:49:28 -05004178 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004179 /*
4180 * we can't truncate inline items that have had
4181 * special encodings
4182 */
4183 if (!del_item &&
4184 btrfs_file_extent_compression(leaf, fi) == 0 &&
4185 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4186 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004187 u32 size = new_size - found_key.offset;
4188
4189 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004190 inode_sub_bytes(inode, item_end + 1 -
4191 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004192 }
4193 size =
4194 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004195 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004196 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004197 inode_sub_bytes(inode, item_end + 1 -
4198 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004199 }
Chris Mason39279cc2007-06-12 06:35:45 -04004200 }
Chris Mason179e29e2007-11-01 11:28:41 -04004201delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004202 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004203 if (!pending_del_nr) {
4204 /* no pending yet, add ourselves */
4205 pending_del_slot = path->slots[0];
4206 pending_del_nr = 1;
4207 } else if (pending_del_nr &&
4208 path->slots[0] + 1 == pending_del_slot) {
4209 /* hop on the pending chunk */
4210 pending_del_nr++;
4211 pending_del_slot = path->slots[0];
4212 } else {
Chris Masond3977122009-01-05 21:25:51 -05004213 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004214 }
Chris Mason39279cc2007-06-12 06:35:45 -04004215 } else {
4216 break;
4217 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004218 if (found_extent && (root->ref_cows ||
4219 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004220 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004221 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004222 extent_num_bytes, 0,
4223 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004224 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004225 BUG_ON(ret);
4226 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004227
Yan, Zheng80825102009-11-12 09:35:36 +00004228 if (found_type == BTRFS_INODE_ITEM_KEY)
4229 break;
4230
4231 if (path->slots[0] == 0 ||
4232 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004233 if (pending_del_nr) {
4234 ret = btrfs_del_items(trans, root, path,
4235 pending_del_slot,
4236 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004237 if (ret) {
4238 btrfs_abort_transaction(trans,
4239 root, ret);
4240 goto error;
4241 }
Yan, Zheng80825102009-11-12 09:35:36 +00004242 pending_del_nr = 0;
4243 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004244 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004245 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004246 } else {
4247 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004248 }
Chris Mason39279cc2007-06-12 06:35:45 -04004249 }
Yan, Zheng80825102009-11-12 09:35:36 +00004250out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004251 if (pending_del_nr) {
4252 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4253 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (ret)
4255 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004256 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004257error:
Chris Mason39279cc2007-06-12 06:35:45 -04004258 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004259 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004260}
4261
Chris Masona52d9a82007-08-27 16:49:44 -04004262/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004263 * btrfs_truncate_page - read, zero a chunk and write a page
4264 * @inode - inode that we're zeroing
4265 * @from - the offset to start zeroing
4266 * @len - the length to zero, 0 to zero the entire range respective to the
4267 * offset
4268 * @front - zero up to the offset instead of from the offset on
4269 *
4270 * This will find the page for the "from" offset and cow the page and zero the
4271 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004272 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004273int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4274 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004275{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004276 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004277 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004278 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4279 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004280 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004281 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004282 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004283 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4284 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4285 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004286 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004287 int ret = 0;
4288 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004289 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004290
Josef Bacik2aaa6652012-08-29 14:27:18 -04004291 if ((offset & (blocksize - 1)) == 0 &&
4292 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004293 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004294 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004295 if (ret)
4296 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004297
Chris Mason211c17f2008-05-15 09:13:45 -04004298again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004299 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004300 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004301 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004302 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004303 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004304 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004305
4306 page_start = page_offset(page);
4307 page_end = page_start + PAGE_CACHE_SIZE - 1;
4308
Chris Masona52d9a82007-08-27 16:49:44 -04004309 if (!PageUptodate(page)) {
4310 ret = btrfs_readpage(NULL, page);
4311 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004312 if (page->mapping != mapping) {
4313 unlock_page(page);
4314 page_cache_release(page);
4315 goto again;
4316 }
Chris Masona52d9a82007-08-27 16:49:44 -04004317 if (!PageUptodate(page)) {
4318 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004319 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004320 }
4321 }
Chris Mason211c17f2008-05-15 09:13:45 -04004322 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004323
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004324 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004325 set_page_extent_mapped(page);
4326
4327 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4328 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004329 unlock_extent_cached(io_tree, page_start, page_end,
4330 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004331 unlock_page(page);
4332 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004333 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004334 btrfs_put_ordered_extent(ordered);
4335 goto again;
4336 }
4337
Josef Bacik2ac55d42010-02-03 19:33:23 +00004338 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004339 EXTENT_DIRTY | EXTENT_DELALLOC |
4340 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004341 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004342
Josef Bacik2ac55d42010-02-03 19:33:23 +00004343 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4344 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004345 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004346 unlock_extent_cached(io_tree, page_start, page_end,
4347 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004348 goto out_unlock;
4349 }
4350
Chris Masone6dcd2d2008-07-17 12:53:50 -04004351 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004352 if (!len)
4353 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004354 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004355 if (front)
4356 memset(kaddr, 0, offset);
4357 else
4358 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359 flush_dcache_page(page);
4360 kunmap(page);
4361 }
Chris Mason247e7432008-07-17 12:53:51 -04004362 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004363 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004364 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4365 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004366
Chris Mason89642222008-07-24 09:41:53 -04004367out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004368 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004369 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004370 unlock_page(page);
4371 page_cache_release(page);
4372out:
4373 return ret;
4374}
4375
Josef Bacik695a0d02011-03-04 15:46:53 -05004376/*
4377 * This function puts in dummy file extents for the area we're creating a hole
4378 * for. So if we are truncating this file to a larger size we need to insert
4379 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4380 * the range between oldsize and size
4381 */
Josef Bacika41ad392011-01-31 15:30:16 -05004382int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004383{
4384 struct btrfs_trans_handle *trans;
4385 struct btrfs_root *root = BTRFS_I(inode)->root;
4386 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004387 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004388 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004389 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004390 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4391 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004392 u64 last_byte;
4393 u64 cur_offset;
4394 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004395 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004396
4397 if (size <= hole_start)
4398 return 0;
4399
Yan Zheng9036c102008-10-30 14:19:41 -04004400 while (1) {
4401 struct btrfs_ordered_extent *ordered;
4402 btrfs_wait_ordered_range(inode, hole_start,
4403 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004404 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004405 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004406 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4407 if (!ordered)
4408 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004409 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4410 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004411 btrfs_put_ordered_extent(ordered);
4412 }
4413
Yan Zheng9036c102008-10-30 14:19:41 -04004414 cur_offset = hole_start;
4415 while (1) {
4416 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4417 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004418 if (IS_ERR(em)) {
4419 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004420 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004421 break;
4422 }
Yan Zheng9036c102008-10-30 14:19:41 -04004423 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004424 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004425 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004426 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004427 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004428
Miao Xie36423202011-12-14 20:12:02 -05004429 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004430 if (IS_ERR(trans)) {
4431 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004432 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004433 }
Yan, Zheng80825102009-11-12 09:35:36 +00004434
Josef Bacik5dc562c2012-08-17 13:14:17 -04004435 err = btrfs_drop_extents(trans, root, inode,
4436 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004437 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004438 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004439 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004440 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004441 break;
Miao Xie5b397372011-09-11 10:52:24 -04004442 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004443
Yan Zheng9036c102008-10-30 14:19:41 -04004444 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004445 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004446 0, hole_size, 0, hole_size,
4447 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004448 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004449 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004450 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004451 break;
Miao Xie5b397372011-09-11 10:52:24 -04004452 }
Yan, Zheng80825102009-11-12 09:35:36 +00004453
Josef Bacik5dc562c2012-08-17 13:14:17 -04004454 btrfs_drop_extent_cache(inode, cur_offset,
4455 cur_offset + hole_size - 1, 0);
4456 hole_em = alloc_extent_map();
4457 if (!hole_em) {
4458 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4459 &BTRFS_I(inode)->runtime_flags);
4460 goto next;
4461 }
4462 hole_em->start = cur_offset;
4463 hole_em->len = hole_size;
4464 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004465
Josef Bacik5dc562c2012-08-17 13:14:17 -04004466 hole_em->block_start = EXTENT_MAP_HOLE;
4467 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004468 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004469 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004470 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4471 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4472 hole_em->generation = trans->transid;
4473
4474 while (1) {
4475 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004476 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004477 write_unlock(&em_tree->lock);
4478 if (err != -EEXIST)
4479 break;
4480 btrfs_drop_extent_cache(inode, cur_offset,
4481 cur_offset +
4482 hole_size - 1, 0);
4483 }
4484 free_extent_map(hole_em);
4485next:
Miao Xie36423202011-12-14 20:12:02 -05004486 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004487 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004488 }
4489 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004490 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004491 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004492 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004493 break;
4494 }
4495
Yan, Zhenga22285a2010-05-16 10:48:46 -04004496 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004497 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4498 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004499 return err;
4500}
4501
Eric Sandeen3972f262013-01-12 02:57:22 +00004502static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004503{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004504 struct btrfs_root *root = BTRFS_I(inode)->root;
4505 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004506 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004507 loff_t newsize = attr->ia_size;
4508 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004509 int ret;
4510
Josef Bacika41ad392011-01-31 15:30:16 -05004511 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004512 return 0;
4513
Eric Sandeen3972f262013-01-12 02:57:22 +00004514 /*
4515 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4516 * special case where we need to update the times despite not having
4517 * these flags set. For all other operations the VFS set these flags
4518 * explicitly if it wants a timestamp update.
4519 */
4520 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4521 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4522
Josef Bacika41ad392011-01-31 15:30:16 -05004523 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004524 truncate_pagecache(inode, oldsize, newsize);
4525 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004526 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004527 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004528
Miao Xief4a2f4c2011-12-14 20:12:01 -05004529 trans = btrfs_start_transaction(root, 1);
4530 if (IS_ERR(trans))
4531 return PTR_ERR(trans);
4532
4533 i_size_write(inode, newsize);
4534 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4535 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004536 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004537 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004538
Josef Bacika41ad392011-01-31 15:30:16 -05004539 /*
4540 * We're truncating a file that used to have good data down to
4541 * zero. Make sure it gets into the ordered flush list so that
4542 * any new writes get down to disk quickly.
4543 */
4544 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004545 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4546 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004547
Josef Bacikf3fe8202013-01-07 17:03:21 -05004548 /*
4549 * 1 for the orphan item we're going to add
4550 * 1 for the orphan item deletion.
4551 */
4552 trans = btrfs_start_transaction(root, 2);
4553 if (IS_ERR(trans))
4554 return PTR_ERR(trans);
4555
4556 /*
4557 * We need to do this in case we fail at _any_ point during the
4558 * actual truncate. Once we do the truncate_setsize we could
4559 * invalidate pages which forces any outstanding ordered io to
4560 * be instantly completed which will give us extents that need
4561 * to be truncated. If we fail to get an orphan inode down we
4562 * could have left over extents that were never meant to live,
4563 * so we need to garuntee from this point on that everything
4564 * will be consistent.
4565 */
4566 ret = btrfs_orphan_add(trans, inode);
4567 btrfs_end_transaction(trans, root);
4568 if (ret)
4569 return ret;
4570
Josef Bacika41ad392011-01-31 15:30:16 -05004571 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4572 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004573
4574 /* Disable nonlocked read DIO to avoid the end less truncate */
4575 btrfs_inode_block_unlocked_dio(inode);
4576 inode_dio_wait(inode);
4577 btrfs_inode_resume_unlocked_dio(inode);
4578
Josef Bacika41ad392011-01-31 15:30:16 -05004579 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004580 if (ret && inode->i_nlink)
4581 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004582 }
4583
Josef Bacika41ad392011-01-31 15:30:16 -05004584 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004585}
4586
Chris Mason39279cc2007-06-12 06:35:45 -04004587static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4588{
4589 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004590 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004591 int err;
4592
Li Zefanb83cc962010-12-20 16:04:08 +08004593 if (btrfs_root_readonly(root))
4594 return -EROFS;
4595
Chris Mason39279cc2007-06-12 06:35:45 -04004596 err = inode_change_ok(inode, attr);
4597 if (err)
4598 return err;
4599
Chris Mason5a3f23d2009-03-31 13:27:11 -04004600 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004601 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004602 if (err)
4603 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004604 }
Yan Zheng9036c102008-10-30 14:19:41 -04004605
Christoph Hellwig10257742010-06-04 11:30:02 +02004606 if (attr->ia_valid) {
4607 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004608 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004609 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004610
Josef Bacik22c44fe2011-11-30 10:45:38 -05004611 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004612 err = btrfs_acl_chmod(inode);
4613 }
4614
Chris Mason39279cc2007-06-12 06:35:45 -04004615 return err;
4616}
Chris Mason61295eb2008-01-14 16:24:38 -05004617
Al Virobd555972010-06-07 11:35:40 -04004618void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004619{
4620 struct btrfs_trans_handle *trans;
4621 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004622 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004623 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004624 int ret;
4625
liubo1abe9b82011-03-24 11:18:59 +00004626 trace_btrfs_inode_evict(inode);
4627
Chris Mason39279cc2007-06-12 06:35:45 -04004628 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004629 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004630 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004631 goto no_delete;
4632
Chris Mason39279cc2007-06-12 06:35:45 -04004633 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004634 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004635 goto no_delete;
4636 }
Al Virobd555972010-06-07 11:35:40 -04004637 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004638 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004639
Yan, Zhengc71bf092009-11-12 09:34:40 +00004640 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004641 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004642 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004643 goto no_delete;
4644 }
4645
Yan, Zheng76dda932009-09-21 16:00:26 -04004646 if (inode->i_nlink > 0) {
4647 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4648 goto no_delete;
4649 }
4650
Miao Xie0e8c36a2012-12-19 06:59:51 +00004651 ret = btrfs_commit_inode_delayed_inode(inode);
4652 if (ret) {
4653 btrfs_orphan_del(NULL, inode);
4654 goto no_delete;
4655 }
4656
Miao Xie66d8f3d2012-09-06 04:02:28 -06004657 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004658 if (!rsv) {
4659 btrfs_orphan_del(NULL, inode);
4660 goto no_delete;
4661 }
Josef Bacik4a338542011-08-29 11:01:31 -04004662 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004663 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004664 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004665
Chris Masondbe674a2008-07-17 12:54:05 -04004666 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004667
Josef Bacik4289a662011-08-05 13:22:24 -04004668 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004669 * This is a bit simpler than btrfs_truncate since we've already
4670 * reserved our space for our orphan item in the unlink, so we just
4671 * need to reserve some slack space in case we add bytes and update
4672 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004673 */
Yan, Zheng80825102009-11-12 09:35:36 +00004674 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004675 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4676 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004677
Josef Bacik726c35f2011-09-26 15:46:06 -04004678 /*
4679 * Try and steal from the global reserve since we will
4680 * likely not use this space anyway, we want to try as
4681 * hard as possible to get this to work.
4682 */
4683 if (ret)
4684 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4685
Yan, Zhengd68fc572010-05-16 10:49:58 -04004686 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004687 btrfs_warn(root->fs_info,
4688 "Could not get space for a delete, will truncate on mount %d",
4689 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004690 btrfs_orphan_del(NULL, inode);
4691 btrfs_free_block_rsv(root, rsv);
4692 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004693 }
4694
Miao Xie0e8c36a2012-12-19 06:59:51 +00004695 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004696 if (IS_ERR(trans)) {
4697 btrfs_orphan_del(NULL, inode);
4698 btrfs_free_block_rsv(root, rsv);
4699 goto no_delete;
4700 }
4701
4702 trans->block_rsv = rsv;
4703
Yan, Zhengd68fc572010-05-16 10:49:58 -04004704 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004705 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004706 break;
4707
Miao Xie8407aa42012-09-07 01:43:32 -06004708 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004709 btrfs_end_transaction(trans, root);
4710 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004711 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004712 }
4713
Josef Bacik4289a662011-08-05 13:22:24 -04004714 btrfs_free_block_rsv(root, rsv);
4715
Yan, Zheng80825102009-11-12 09:35:36 +00004716 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004717 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004718 ret = btrfs_orphan_del(trans, inode);
4719 BUG_ON(ret);
4720 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004721
Josef Bacik4289a662011-08-05 13:22:24 -04004722 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004723 if (!(root == root->fs_info->tree_root ||
4724 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004725 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004726
Chris Mason54aa1f42007-06-22 14:16:25 -04004727 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004728 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004729no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004730 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004731 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004732 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004733}
4734
4735/*
4736 * this returns the key found in the dir entry in the location pointer.
4737 * If no dir entries were found, location->objectid is 0.
4738 */
4739static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4740 struct btrfs_key *location)
4741{
4742 const char *name = dentry->d_name.name;
4743 int namelen = dentry->d_name.len;
4744 struct btrfs_dir_item *di;
4745 struct btrfs_path *path;
4746 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004747 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004748
4749 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004750 if (!path)
4751 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004752
Li Zefan33345d012011-04-20 10:31:50 +08004753 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004754 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004755 if (IS_ERR(di))
4756 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004757
David Sterbac7040052011-04-19 18:00:01 +02004758 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004759 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004760
Chris Mason5f39d392007-10-15 16:14:19 -04004761 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004762out:
Chris Mason39279cc2007-06-12 06:35:45 -04004763 btrfs_free_path(path);
4764 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004765out_err:
4766 location->objectid = 0;
4767 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004768}
4769
4770/*
4771 * when we hit a tree root in a directory, the btrfs part of the inode
4772 * needs to be changed to reflect the root directory of the tree root. This
4773 * is kind of like crossing a mount point.
4774 */
4775static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004776 struct inode *dir,
4777 struct dentry *dentry,
4778 struct btrfs_key *location,
4779 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004780{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004781 struct btrfs_path *path;
4782 struct btrfs_root *new_root;
4783 struct btrfs_root_ref *ref;
4784 struct extent_buffer *leaf;
4785 int ret;
4786 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004787
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004788 path = btrfs_alloc_path();
4789 if (!path) {
4790 err = -ENOMEM;
4791 goto out;
4792 }
Chris Mason39279cc2007-06-12 06:35:45 -04004793
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004794 err = -ENOENT;
4795 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4796 BTRFS_I(dir)->root->root_key.objectid,
4797 location->objectid);
4798 if (ret) {
4799 if (ret < 0)
4800 err = ret;
4801 goto out;
4802 }
Chris Mason39279cc2007-06-12 06:35:45 -04004803
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004804 leaf = path->nodes[0];
4805 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004806 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004807 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4808 goto out;
4809
4810 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4811 (unsigned long)(ref + 1),
4812 dentry->d_name.len);
4813 if (ret)
4814 goto out;
4815
David Sterbab3b4aa72011-04-21 01:20:15 +02004816 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004817
4818 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4819 if (IS_ERR(new_root)) {
4820 err = PTR_ERR(new_root);
4821 goto out;
4822 }
4823
4824 if (btrfs_root_refs(&new_root->root_item) == 0) {
4825 err = -ENOENT;
4826 goto out;
4827 }
4828
4829 *sub_root = new_root;
4830 location->objectid = btrfs_root_dirid(&new_root->root_item);
4831 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004832 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004833 err = 0;
4834out:
4835 btrfs_free_path(path);
4836 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004837}
4838
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004839static void inode_tree_add(struct inode *inode)
4840{
4841 struct btrfs_root *root = BTRFS_I(inode)->root;
4842 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004843 struct rb_node **p;
4844 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004845 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004846
Al Viro1d3382c2010-10-23 15:19:20 -04004847 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004848 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004849again:
4850 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004851 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004852 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004853 while (*p) {
4854 parent = *p;
4855 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4856
Li Zefan33345d012011-04-20 10:31:50 +08004857 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004858 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004859 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004860 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004861 else {
4862 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004863 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004864 rb_erase(parent, &root->inode_tree);
4865 RB_CLEAR_NODE(parent);
4866 spin_unlock(&root->inode_lock);
4867 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004868 }
4869 }
4870 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4871 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4872 spin_unlock(&root->inode_lock);
4873}
4874
4875static void inode_tree_del(struct inode *inode)
4876{
4877 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004878 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004879
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004880 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004881 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004882 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004883 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004884 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004885 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004886 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004887
Josef Bacik0af3d002010-06-21 14:48:16 -04004888 /*
4889 * Free space cache has inodes in the tree root, but the tree root has a
4890 * root_refs of 0, so this could end up dropping the tree root as a
4891 * snapshot, so we need the extra !root->fs_info->tree_root check to
4892 * make sure we don't drop it.
4893 */
4894 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4895 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004896 synchronize_srcu(&root->fs_info->subvol_srcu);
4897 spin_lock(&root->inode_lock);
4898 empty = RB_EMPTY_ROOT(&root->inode_tree);
4899 spin_unlock(&root->inode_lock);
4900 if (empty)
4901 btrfs_add_dead_root(root);
4902 }
4903}
4904
Jeff Mahoney143bede2012-03-01 14:56:26 +01004905void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004906{
4907 struct rb_node *node;
4908 struct rb_node *prev;
4909 struct btrfs_inode *entry;
4910 struct inode *inode;
4911 u64 objectid = 0;
4912
4913 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4914
4915 spin_lock(&root->inode_lock);
4916again:
4917 node = root->inode_tree.rb_node;
4918 prev = NULL;
4919 while (node) {
4920 prev = node;
4921 entry = rb_entry(node, struct btrfs_inode, rb_node);
4922
Li Zefan33345d012011-04-20 10:31:50 +08004923 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004924 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004925 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004926 node = node->rb_right;
4927 else
4928 break;
4929 }
4930 if (!node) {
4931 while (prev) {
4932 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004933 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004934 node = prev;
4935 break;
4936 }
4937 prev = rb_next(prev);
4938 }
4939 }
4940 while (node) {
4941 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004942 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004943 inode = igrab(&entry->vfs_inode);
4944 if (inode) {
4945 spin_unlock(&root->inode_lock);
4946 if (atomic_read(&inode->i_count) > 1)
4947 d_prune_aliases(inode);
4948 /*
Al Viro45321ac2010-06-07 13:43:19 -04004949 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004950 * the inode cache when its usage count
4951 * hits zero.
4952 */
4953 iput(inode);
4954 cond_resched();
4955 spin_lock(&root->inode_lock);
4956 goto again;
4957 }
4958
4959 if (cond_resched_lock(&root->inode_lock))
4960 goto again;
4961
4962 node = rb_next(node);
4963 }
4964 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004965}
4966
Chris Masone02119d2008-09-05 16:13:11 -04004967static int btrfs_init_locked_inode(struct inode *inode, void *p)
4968{
4969 struct btrfs_iget_args *args = p;
4970 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004971 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004972 return 0;
4973}
4974
4975static int btrfs_find_actor(struct inode *inode, void *opaque)
4976{
4977 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004978 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004979 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004980}
4981
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004982static struct inode *btrfs_iget_locked(struct super_block *s,
4983 u64 objectid,
4984 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004985{
4986 struct inode *inode;
4987 struct btrfs_iget_args args;
4988 args.ino = objectid;
4989 args.root = root;
4990
4991 inode = iget5_locked(s, objectid, btrfs_find_actor,
4992 btrfs_init_locked_inode,
4993 (void *)&args);
4994 return inode;
4995}
4996
Balaji Rao1a54ef82008-07-21 02:01:04 +05304997/* Get an inode object given its location and corresponding root.
4998 * Returns in *is_new if the inode was read from disk
4999 */
5000struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005001 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305002{
5003 struct inode *inode;
5004
5005 inode = btrfs_iget_locked(s, location->objectid, root);
5006 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005007 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305008
5009 if (inode->i_state & I_NEW) {
5010 BTRFS_I(inode)->root = root;
5011 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5012 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005013 if (!is_bad_inode(inode)) {
5014 inode_tree_add(inode);
5015 unlock_new_inode(inode);
5016 if (new)
5017 *new = 1;
5018 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005019 unlock_new_inode(inode);
5020 iput(inode);
5021 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005022 }
5023 }
5024
Balaji Rao1a54ef82008-07-21 02:01:04 +05305025 return inode;
5026}
5027
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005028static struct inode *new_simple_dir(struct super_block *s,
5029 struct btrfs_key *key,
5030 struct btrfs_root *root)
5031{
5032 struct inode *inode = new_inode(s);
5033
5034 if (!inode)
5035 return ERR_PTR(-ENOMEM);
5036
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005037 BTRFS_I(inode)->root = root;
5038 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005039 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005040
5041 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005042 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005043 inode->i_fop = &simple_dir_operations;
5044 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5045 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5046
5047 return inode;
5048}
5049
Chris Mason3de45862008-11-17 21:02:50 -05005050struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005051{
Chris Masond3977122009-01-05 21:25:51 -05005052 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005053 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005054 struct btrfs_root *sub_root = root;
5055 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005056 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005057 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005058
5059 if (dentry->d_name.len > BTRFS_NAME_LEN)
5060 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005061
Jeff Layton39e3c952012-11-28 11:30:53 -05005062 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005063 if (ret < 0)
5064 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005065
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005066 if (location.objectid == 0)
5067 return NULL;
5068
5069 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005070 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005071 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005072 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005073
5074 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5075
Yan, Zheng76dda932009-09-21 16:00:26 -04005076 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005077 ret = fixup_tree_root_location(root, dir, dentry,
5078 &location, &sub_root);
5079 if (ret < 0) {
5080 if (ret != -ENOENT)
5081 inode = ERR_PTR(ret);
5082 else
5083 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5084 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005085 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005086 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005087 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5088
Julia Lawall34d19ba2011-01-24 19:55:19 +00005089 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005090 down_read(&root->fs_info->cleanup_work_sem);
5091 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005092 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005093 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005094 if (ret)
5095 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005096 }
5097
Chris Mason3de45862008-11-17 21:02:50 -05005098 return inode;
5099}
5100
Nick Pigginfe15ce42011-01-07 17:49:23 +11005101static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005102{
5103 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005104 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005105
Li Zefan848cce02012-02-21 17:04:28 +08005106 if (!inode && !IS_ROOT(dentry))
5107 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005108
Li Zefan848cce02012-02-21 17:04:28 +08005109 if (inode) {
5110 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005111 if (btrfs_root_refs(&root->root_item) == 0)
5112 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005113
5114 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5115 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005116 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005117 return 0;
5118}
5119
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005120static void btrfs_dentry_release(struct dentry *dentry)
5121{
5122 if (dentry->d_fsdata)
5123 kfree(dentry->d_fsdata);
5124}
5125
Chris Mason3de45862008-11-17 21:02:50 -05005126static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005127 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005128{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005129 struct dentry *ret;
5130
5131 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005132 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005133}
5134
Miao Xie16cdcec2011-04-22 18:12:22 +08005135unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005136 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5137};
5138
David Woodhousecbdf5a22008-08-06 19:42:33 +01005139static int btrfs_real_readdir(struct file *filp, void *dirent,
5140 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005141{
Al Viro496ad9a2013-01-23 17:07:38 -05005142 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005143 struct btrfs_root *root = BTRFS_I(inode)->root;
5144 struct btrfs_item *item;
5145 struct btrfs_dir_item *di;
5146 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005147 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005148 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005149 struct list_head ins_list;
5150 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005151 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005152 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005153 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005154 unsigned char d_type;
5155 int over = 0;
5156 u32 di_cur;
5157 u32 di_total;
5158 u32 di_len;
5159 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005160 char tmp_name[32];
5161 char *name_ptr;
5162 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005163 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005164
5165 /* FIXME, use a real flag for deciding about the key type */
5166 if (root->fs_info->tree_root == root)
5167 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005168
Chris Mason39544012007-12-12 14:38:19 -05005169 /* special case for "." */
5170 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005171 over = filldir(dirent, ".", 1,
5172 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005173 if (over)
5174 return 0;
5175 filp->f_pos = 1;
5176 }
Chris Mason39544012007-12-12 14:38:19 -05005177 /* special case for .., just use the back ref */
5178 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005179 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005180 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005181 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005182 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005183 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005184 filp->f_pos = 2;
5185 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005186 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005187 if (!path)
5188 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005189
Josef Bacik026fd312011-05-13 10:32:11 -04005190 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005191
Miao Xie16cdcec2011-04-22 18:12:22 +08005192 if (key_type == BTRFS_DIR_INDEX_KEY) {
5193 INIT_LIST_HEAD(&ins_list);
5194 INIT_LIST_HEAD(&del_list);
5195 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5196 }
5197
Chris Mason39279cc2007-06-12 06:35:45 -04005198 btrfs_set_key_type(&key, key_type);
5199 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005200 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005201
Chris Mason39279cc2007-06-12 06:35:45 -04005202 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5203 if (ret < 0)
5204 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005205
5206 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005207 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005208 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005209 if (slot >= btrfs_header_nritems(leaf)) {
5210 ret = btrfs_next_leaf(root, path);
5211 if (ret < 0)
5212 goto err;
5213 else if (ret > 0)
5214 break;
5215 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005216 }
Chris Mason3de45862008-11-17 21:02:50 -05005217
Chris Mason5f39d392007-10-15 16:14:19 -04005218 item = btrfs_item_nr(leaf, slot);
5219 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5220
5221 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005222 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005223 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005224 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005225 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005226 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005227 if (key_type == BTRFS_DIR_INDEX_KEY &&
5228 btrfs_should_delete_dir_index(&del_list,
5229 found_key.offset))
5230 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005231
5232 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005233 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005234
Chris Mason39279cc2007-06-12 06:35:45 -04005235 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5236 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005237 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005238
5239 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005240 struct btrfs_key location;
5241
Josef Bacik22a94d42011-03-16 16:47:17 -04005242 if (verify_dir_item(root, leaf, di))
5243 break;
5244
Chris Mason5f39d392007-10-15 16:14:19 -04005245 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005246 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005247 name_ptr = tmp_name;
5248 } else {
5249 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005250 if (!name_ptr) {
5251 ret = -ENOMEM;
5252 goto err;
5253 }
Chris Mason5f39d392007-10-15 16:14:19 -04005254 }
5255 read_extent_buffer(leaf, name_ptr,
5256 (unsigned long)(di + 1), name_len);
5257
5258 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5259 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005260
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005261
Chris Mason3de45862008-11-17 21:02:50 -05005262 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005263 * skip it.
5264 *
5265 * In contrast to old kernels, we insert the snapshot's
5266 * dir item and dir index after it has been created, so
5267 * we won't find a reference to our own snapshot. We
5268 * still keep the following code for backward
5269 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005270 */
5271 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5272 location.objectid == root->root_key.objectid) {
5273 over = 0;
5274 goto skip;
5275 }
Chris Mason5f39d392007-10-15 16:14:19 -04005276 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005277 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005278 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005279
Chris Mason3de45862008-11-17 21:02:50 -05005280skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005281 if (name_ptr != tmp_name)
5282 kfree(name_ptr);
5283
Chris Mason39279cc2007-06-12 06:35:45 -04005284 if (over)
5285 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005286 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005287 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005288 di_cur += di_len;
5289 di = (struct btrfs_dir_item *)((char *)di + di_len);
5290 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005291next:
5292 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005293 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005294
Miao Xie16cdcec2011-04-22 18:12:22 +08005295 if (key_type == BTRFS_DIR_INDEX_KEY) {
5296 if (is_curr)
5297 filp->f_pos++;
5298 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5299 &ins_list);
5300 if (ret)
5301 goto nopos;
5302 }
5303
David Woodhouse49593bf2008-08-17 17:08:36 +01005304 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005305 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005306 /*
5307 * 32-bit glibc will use getdents64, but then strtol -
5308 * so the last number we can serve is this.
5309 */
5310 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005311 else
5312 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005313nopos:
5314 ret = 0;
5315err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005316 if (key_type == BTRFS_DIR_INDEX_KEY)
5317 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005318 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005319 return ret;
5320}
5321
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005322int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005323{
5324 struct btrfs_root *root = BTRFS_I(inode)->root;
5325 struct btrfs_trans_handle *trans;
5326 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005327 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005328
Josef Bacik72ac3c02012-05-23 14:13:11 -04005329 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005330 return 0;
5331
Liu Bo83eea1f2012-07-10 05:28:39 -06005332 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005333 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005334
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005335 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005336 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005337 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005338 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005339 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005340 if (IS_ERR(trans))
5341 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005342 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005343 }
5344 return ret;
5345}
5346
5347/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005348 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005349 * inode changes. But, it is most likely to find the inode in cache.
5350 * FIXME, needs more benchmarking...there are no reasons other than performance
5351 * to keep or drop this code.
5352 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005353static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005354{
5355 struct btrfs_root *root = BTRFS_I(inode)->root;
5356 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005357 int ret;
5358
Josef Bacik72ac3c02012-05-23 14:13:11 -04005359 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005360 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005361
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005362 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005363 if (IS_ERR(trans))
5364 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005365
5366 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005367 if (ret && ret == -ENOSPC) {
5368 /* whoops, lets try again with the full transaction */
5369 btrfs_end_transaction(trans, root);
5370 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005371 if (IS_ERR(trans))
5372 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005373
Chris Mason94b60442010-05-26 11:02:00 -04005374 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005375 }
Chris Mason39279cc2007-06-12 06:35:45 -04005376 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005377 if (BTRFS_I(inode)->delayed_node)
5378 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005379
5380 return ret;
5381}
5382
5383/*
5384 * This is a copy of file_update_time. We need this so we can return error on
5385 * ENOSPC for updating the inode in the case of file write and mmap writes.
5386 */
Josef Bacike41f9412012-03-26 09:46:47 -04005387static int btrfs_update_time(struct inode *inode, struct timespec *now,
5388 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005389{
Alexander Block2bc5565282012-06-15 09:49:33 +02005390 struct btrfs_root *root = BTRFS_I(inode)->root;
5391
5392 if (btrfs_root_readonly(root))
5393 return -EROFS;
5394
Josef Bacike41f9412012-03-26 09:46:47 -04005395 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005396 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005397 if (flags & S_CTIME)
5398 inode->i_ctime = *now;
5399 if (flags & S_MTIME)
5400 inode->i_mtime = *now;
5401 if (flags & S_ATIME)
5402 inode->i_atime = *now;
5403 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005404}
5405
Chris Masond352ac62008-09-29 15:18:18 -04005406/*
5407 * find the highest existing sequence number in a directory
5408 * and then set the in-memory index_cnt variable to reflect
5409 * free sequence numbers
5410 */
Josef Bacikaec74772008-07-24 12:12:38 -04005411static int btrfs_set_inode_index_count(struct inode *inode)
5412{
5413 struct btrfs_root *root = BTRFS_I(inode)->root;
5414 struct btrfs_key key, found_key;
5415 struct btrfs_path *path;
5416 struct extent_buffer *leaf;
5417 int ret;
5418
Li Zefan33345d012011-04-20 10:31:50 +08005419 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005420 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5421 key.offset = (u64)-1;
5422
5423 path = btrfs_alloc_path();
5424 if (!path)
5425 return -ENOMEM;
5426
5427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5428 if (ret < 0)
5429 goto out;
5430 /* FIXME: we should be able to handle this */
5431 if (ret == 0)
5432 goto out;
5433 ret = 0;
5434
5435 /*
5436 * MAGIC NUMBER EXPLANATION:
5437 * since we search a directory based on f_pos we have to start at 2
5438 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5439 * else has to start at 2
5440 */
5441 if (path->slots[0] == 0) {
5442 BTRFS_I(inode)->index_cnt = 2;
5443 goto out;
5444 }
5445
5446 path->slots[0]--;
5447
5448 leaf = path->nodes[0];
5449 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5450
Li Zefan33345d012011-04-20 10:31:50 +08005451 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005452 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5453 BTRFS_I(inode)->index_cnt = 2;
5454 goto out;
5455 }
5456
5457 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5458out:
5459 btrfs_free_path(path);
5460 return ret;
5461}
5462
Chris Masond352ac62008-09-29 15:18:18 -04005463/*
5464 * helper to find a free sequence number in a given directory. This current
5465 * code is very simple, later versions will do smarter things in the btree
5466 */
Chris Mason3de45862008-11-17 21:02:50 -05005467int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005468{
5469 int ret = 0;
5470
5471 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005472 ret = btrfs_inode_delayed_dir_index_count(dir);
5473 if (ret) {
5474 ret = btrfs_set_inode_index_count(dir);
5475 if (ret)
5476 return ret;
5477 }
Josef Bacikaec74772008-07-24 12:12:38 -04005478 }
5479
Chris Mason00e4e6b2008-08-05 11:18:09 -04005480 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005481 BTRFS_I(dir)->index_cnt++;
5482
5483 return ret;
5484}
5485
Chris Mason39279cc2007-06-12 06:35:45 -04005486static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5487 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005488 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005489 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005490 u64 ref_objectid, u64 objectid,
5491 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005492{
5493 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005494 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005495 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005496 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005497 struct btrfs_inode_ref *ref;
5498 struct btrfs_key key[2];
5499 u32 sizes[2];
5500 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005501 int ret;
5502 int owner;
5503
Chris Mason5f39d392007-10-15 16:14:19 -04005504 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005505 if (!path)
5506 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005507
Chris Mason39279cc2007-06-12 06:35:45 -04005508 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005509 if (!inode) {
5510 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005511 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005512 }
Chris Mason39279cc2007-06-12 06:35:45 -04005513
Li Zefan581bb052011-04-20 10:06:11 +08005514 /*
5515 * we have to initialize this early, so we can reclaim the inode
5516 * number if we fail afterwards in this function.
5517 */
5518 inode->i_ino = objectid;
5519
Josef Bacikaec74772008-07-24 12:12:38 -04005520 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005521 trace_btrfs_inode_request(dir);
5522
Chris Mason3de45862008-11-17 21:02:50 -05005523 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005524 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005525 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005526 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005527 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005528 }
Josef Bacikaec74772008-07-24 12:12:38 -04005529 }
5530 /*
5531 * index_cnt is ignored for everything but a dir,
5532 * btrfs_get_inode_index_count has an explanation for the magic
5533 * number
5534 */
5535 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005536 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005537 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005538 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005539
Josef Bacik5dc562c2012-08-17 13:14:17 -04005540 /*
5541 * We could have gotten an inode number from somebody who was fsynced
5542 * and then removed in this same transaction, so let's just set full
5543 * sync since it will be a full sync anyway and this will blow away the
5544 * old info in the log.
5545 */
5546 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5547
Al Viro569254b2011-07-24 17:08:40 -04005548 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005549 owner = 0;
5550 else
5551 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005552
5553 key[0].objectid = objectid;
5554 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5555 key[0].offset = 0;
5556
Mark Fashehf1863732012-08-08 11:32:27 -07005557 /*
5558 * Start new inodes with an inode_ref. This is slightly more
5559 * efficient for small numbers of hard links since they will
5560 * be packed into one item. Extended refs will kick in if we
5561 * add more hard links than can fit in the ref item.
5562 */
Chris Mason9c583092008-01-29 15:15:18 -05005563 key[1].objectid = objectid;
5564 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5565 key[1].offset = ref_objectid;
5566
5567 sizes[0] = sizeof(struct btrfs_inode_item);
5568 sizes[1] = name_len + sizeof(*ref);
5569
Chris Masonb9473432009-03-13 11:00:37 -04005570 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005571 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5572 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005573 goto fail;
5574
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005575 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005576 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005577 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005578 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5579 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005580 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5581 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005582 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005583
5584 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5585 struct btrfs_inode_ref);
5586 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005587 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005588 ptr = (unsigned long)(ref + 1);
5589 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5590
Chris Mason5f39d392007-10-15 16:14:19 -04005591 btrfs_mark_buffer_dirty(path->nodes[0]);
5592 btrfs_free_path(path);
5593
Chris Mason39279cc2007-06-12 06:35:45 -04005594 location = &BTRFS_I(inode)->location;
5595 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005596 location->offset = 0;
5597 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5598
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005599 btrfs_inherit_iflags(inode, dir);
5600
Al Viro569254b2011-07-24 17:08:40 -04005601 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005602 if (btrfs_test_opt(root, NODATASUM))
5603 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005604 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005605 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5606 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005607 }
5608
Chris Mason39279cc2007-06-12 06:35:45 -04005609 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005611
5612 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005613 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005614
Alexander Block8ea05e32012-07-25 17:35:53 +02005615 btrfs_update_root_times(trans, root);
5616
Chris Mason39279cc2007-06-12 06:35:45 -04005617 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005618fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005619 if (dir)
5620 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005621 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005622 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005623 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005624}
5625
5626static inline u8 btrfs_inode_type(struct inode *inode)
5627{
5628 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5629}
5630
Chris Masond352ac62008-09-29 15:18:18 -04005631/*
5632 * utility function to add 'inode' into 'parent_inode' with
5633 * a give name and a given sequence number.
5634 * if 'add_backref' is true, also insert a backref from the
5635 * inode to the parent directory.
5636 */
Chris Masone02119d2008-09-05 16:13:11 -04005637int btrfs_add_link(struct btrfs_trans_handle *trans,
5638 struct inode *parent_inode, struct inode *inode,
5639 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005640{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005642 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005643 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005644 u64 ino = btrfs_ino(inode);
5645 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005646
Li Zefan33345d012011-04-20 10:31:50 +08005647 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5649 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005650 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5652 key.offset = 0;
5653 }
Chris Mason39279cc2007-06-12 06:35:45 -04005654
Li Zefan33345d012011-04-20 10:31:50 +08005655 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005656 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5657 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005658 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005660 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5661 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005662 }
5663
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005664 /* Nothing to clean up yet */
5665 if (ret)
5666 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005667
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005668 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5669 parent_inode, &key,
5670 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005671 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005672 goto fail_dir_item;
5673 else if (ret) {
5674 btrfs_abort_transaction(trans, root, ret);
5675 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005676 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005677
5678 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5679 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005680 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005681 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5682 ret = btrfs_update_inode(trans, root, parent_inode);
5683 if (ret)
5684 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005685 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005686
5687fail_dir_item:
5688 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5689 u64 local_index;
5690 int err;
5691 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5692 key.objectid, root->root_key.objectid,
5693 parent_ino, &local_index, name, name_len);
5694
5695 } else if (add_backref) {
5696 u64 local_index;
5697 int err;
5698
5699 err = btrfs_del_inode_ref(trans, root, name, name_len,
5700 ino, parent_ino, &local_index);
5701 }
5702 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005703}
5704
5705static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005706 struct inode *dir, struct dentry *dentry,
5707 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005708{
Josef Bacika1b075d2010-11-19 20:36:11 +00005709 int err = btrfs_add_link(trans, dir, inode,
5710 dentry->d_name.name, dentry->d_name.len,
5711 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005712 if (err > 0)
5713 err = -EEXIST;
5714 return err;
5715}
5716
Josef Bacik618e21d2007-07-11 10:18:17 -04005717static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005718 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005719{
5720 struct btrfs_trans_handle *trans;
5721 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005722 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005723 int err;
5724 int drop_inode = 0;
5725 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005726 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005727
5728 if (!new_valid_dev(rdev))
5729 return -EINVAL;
5730
Josef Bacik9ed74f22009-09-11 16:12:44 -04005731 /*
5732 * 2 for inode item and ref
5733 * 2 for dir items
5734 * 1 for xattr if selinux is on
5735 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005736 trans = btrfs_start_transaction(root, 5);
5737 if (IS_ERR(trans))
5738 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005739
Li Zefan581bb052011-04-20 10:06:11 +08005740 err = btrfs_find_free_ino(root, &objectid);
5741 if (err)
5742 goto out_unlock;
5743
Josef Bacikaec74772008-07-24 12:12:38 -04005744 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005745 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005746 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005747 if (IS_ERR(inode)) {
5748 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005749 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005750 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005751
Eric Paris2a7dba32011-02-01 11:05:39 -05005752 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005753 if (err) {
5754 drop_inode = 1;
5755 goto out_unlock;
5756 }
5757
Casey Schauflerad19db72011-12-15 10:09:07 -05005758 /*
5759 * If the active LSM wants to access the inode during
5760 * d_instantiate it needs these. Smack checks to see
5761 * if the filesystem supports xattrs by looking at the
5762 * ops vector.
5763 */
5764
5765 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005766 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005767 if (err)
5768 drop_inode = 1;
5769 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005770 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005771 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005772 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005773 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005774out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005775 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005776 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005777 if (drop_inode) {
5778 inode_dec_link_count(inode);
5779 iput(inode);
5780 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005781 return err;
5782}
5783
Chris Mason39279cc2007-06-12 06:35:45 -04005784static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005785 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005786{
5787 struct btrfs_trans_handle *trans;
5788 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005789 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005790 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005791 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005792 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005793 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005794
Josef Bacik9ed74f22009-09-11 16:12:44 -04005795 /*
5796 * 2 for inode item and ref
5797 * 2 for dir items
5798 * 1 for xattr if selinux is on
5799 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005800 trans = btrfs_start_transaction(root, 5);
5801 if (IS_ERR(trans))
5802 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005803
Li Zefan581bb052011-04-20 10:06:11 +08005804 err = btrfs_find_free_ino(root, &objectid);
5805 if (err)
5806 goto out_unlock;
5807
Josef Bacikaec74772008-07-24 12:12:38 -04005808 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005809 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005810 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005811 if (IS_ERR(inode)) {
5812 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005813 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005814 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005815 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005816
Eric Paris2a7dba32011-02-01 11:05:39 -05005817 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005818 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005819 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005820
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005821 err = btrfs_update_inode(trans, root, inode);
5822 if (err)
5823 goto out_unlock;
5824
Casey Schauflerad19db72011-12-15 10:09:07 -05005825 /*
5826 * If the active LSM wants to access the inode during
5827 * d_instantiate it needs these. Smack checks to see
5828 * if the filesystem supports xattrs by looking at the
5829 * ops vector.
5830 */
5831 inode->i_fop = &btrfs_file_operations;
5832 inode->i_op = &btrfs_file_inode_operations;
5833
Josef Bacika1b075d2010-11-19 20:36:11 +00005834 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005835 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005836 goto out_unlock;
5837
5838 inode->i_mapping->a_ops = &btrfs_aops;
5839 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5840 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5841 d_instantiate(dentry, inode);
5842
Chris Mason39279cc2007-06-12 06:35:45 -04005843out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005844 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005845 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005846 inode_dec_link_count(inode);
5847 iput(inode);
5848 }
Liu Bob53d3f52012-11-14 14:34:34 +00005849 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005850 return err;
5851}
5852
5853static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5854 struct dentry *dentry)
5855{
5856 struct btrfs_trans_handle *trans;
5857 struct btrfs_root *root = BTRFS_I(dir)->root;
5858 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005859 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005860 int err;
5861 int drop_inode = 0;
5862
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005863 /* do not allow sys_link's with other subvols of the same device */
5864 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005865 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005866
Mark Fashehf1863732012-08-08 11:32:27 -07005867 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005868 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005869
Chris Mason3de45862008-11-17 21:02:50 -05005870 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005871 if (err)
5872 goto fail;
5873
Yan, Zhenga22285a2010-05-16 10:48:46 -04005874 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005875 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005876 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005877 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005878 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005879 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005880 if (IS_ERR(trans)) {
5881 err = PTR_ERR(trans);
5882 goto fail;
5883 }
Chris Mason5f39d392007-10-15 16:14:19 -04005884
Miao Xie31534952011-04-13 13:19:21 +08005885 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005886 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005887 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005888 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005889 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005890
Josef Bacika1b075d2010-11-19 20:36:11 +00005891 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005892
Yan, Zhenga5719522009-09-24 09:17:31 -04005893 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005894 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005895 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005896 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005897 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005898 if (err)
5899 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005900 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005901 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005902 }
Chris Mason39279cc2007-06-12 06:35:45 -04005903
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005904 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005905fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005906 if (drop_inode) {
5907 inode_dec_link_count(inode);
5908 iput(inode);
5909 }
Liu Bob53d3f52012-11-14 14:34:34 +00005910 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005911 return err;
5912}
5913
Al Viro18bb1db2011-07-26 01:41:39 -04005914static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005915{
Chris Masonb9d86662008-05-02 16:13:49 -04005916 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005917 struct btrfs_trans_handle *trans;
5918 struct btrfs_root *root = BTRFS_I(dir)->root;
5919 int err = 0;
5920 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005921 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005922 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005923
Josef Bacik9ed74f22009-09-11 16:12:44 -04005924 /*
5925 * 2 items for inode and ref
5926 * 2 items for dir items
5927 * 1 for xattr if selinux is on
5928 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005929 trans = btrfs_start_transaction(root, 5);
5930 if (IS_ERR(trans))
5931 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005932
Li Zefan581bb052011-04-20 10:06:11 +08005933 err = btrfs_find_free_ino(root, &objectid);
5934 if (err)
5935 goto out_fail;
5936
Josef Bacikaec74772008-07-24 12:12:38 -04005937 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005938 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005939 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005940 if (IS_ERR(inode)) {
5941 err = PTR_ERR(inode);
5942 goto out_fail;
5943 }
Chris Mason5f39d392007-10-15 16:14:19 -04005944
Chris Mason39279cc2007-06-12 06:35:45 -04005945 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005946
Eric Paris2a7dba32011-02-01 11:05:39 -05005947 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005948 if (err)
5949 goto out_fail;
5950
Chris Mason39279cc2007-06-12 06:35:45 -04005951 inode->i_op = &btrfs_dir_inode_operations;
5952 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005953
Chris Masondbe674a2008-07-17 12:54:05 -04005954 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005955 err = btrfs_update_inode(trans, root, inode);
5956 if (err)
5957 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005958
Josef Bacika1b075d2010-11-19 20:36:11 +00005959 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5960 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005961 if (err)
5962 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005963
Chris Mason39279cc2007-06-12 06:35:45 -04005964 d_instantiate(dentry, inode);
5965 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005966
5967out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005968 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005969 if (drop_on_err)
5970 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005971 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005972 return err;
5973}
5974
Chris Masond352ac62008-09-29 15:18:18 -04005975/* helper for btfs_get_extent. Given an existing extent in the tree,
5976 * and an extent that you want to insert, deal with overlap and insert
5977 * the new extent into the tree.
5978 */
Chris Mason3b951512008-04-17 11:29:12 -04005979static int merge_extent_mapping(struct extent_map_tree *em_tree,
5980 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005981 struct extent_map *em,
5982 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005983{
5984 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005985
Chris Masone6dcd2d2008-07-17 12:53:50 -04005986 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5987 start_diff = map_start - em->start;
5988 em->start = map_start;
5989 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005990 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5991 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005992 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005993 em->block_len -= start_diff;
5994 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005995 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005996}
5997
Chris Masonc8b97812008-10-29 14:49:59 -04005998static noinline int uncompress_inline(struct btrfs_path *path,
5999 struct inode *inode, struct page *page,
6000 size_t pg_offset, u64 extent_offset,
6001 struct btrfs_file_extent_item *item)
6002{
6003 int ret;
6004 struct extent_buffer *leaf = path->nodes[0];
6005 char *tmp;
6006 size_t max_size;
6007 unsigned long inline_size;
6008 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006009 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006010
6011 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006012 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006013 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6014 inline_size = btrfs_file_extent_inline_item_len(leaf,
6015 btrfs_item_nr(leaf, path->slots[0]));
6016 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006017 if (!tmp)
6018 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006019 ptr = btrfs_file_extent_inline_start(item);
6020
6021 read_extent_buffer(leaf, tmp, ptr, inline_size);
6022
Chris Mason5b050f02008-11-11 09:34:41 -05006023 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006024 ret = btrfs_decompress(compress_type, tmp, page,
6025 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006026 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006027 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006028 unsigned long copy_size = min_t(u64,
6029 PAGE_CACHE_SIZE - pg_offset,
6030 max_size - extent_offset);
6031 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006032 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006033 }
6034 kfree(tmp);
6035 return 0;
6036}
6037
Chris Masond352ac62008-09-29 15:18:18 -04006038/*
6039 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006040 * the ugly parts come from merging extents from the disk with the in-ram
6041 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006042 * where the in-ram extents might be locked pending data=ordered completion.
6043 *
6044 * This also copies inline extents directly into the page.
6045 */
Chris Masond3977122009-01-05 21:25:51 -05006046
Chris Masona52d9a82007-08-27 16:49:44 -04006047struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006048 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006049 int create)
6050{
6051 int ret;
6052 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006053 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006054 u64 extent_start = 0;
6055 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006056 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006057 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006058 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006059 struct btrfs_root *root = BTRFS_I(inode)->root;
6060 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006061 struct extent_buffer *leaf;
6062 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006063 struct extent_map *em = NULL;
6064 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006065 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006066 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006067 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006068
Chris Masona52d9a82007-08-27 16:49:44 -04006069again:
Chris Mason890871b2009-09-02 16:24:52 -04006070 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006071 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006072 if (em)
6073 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006074 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006075
Chris Masona52d9a82007-08-27 16:49:44 -04006076 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006077 if (em->start > start || em->start + em->len <= start)
6078 free_extent_map(em);
6079 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006080 free_extent_map(em);
6081 else
6082 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006083 }
David Sterba172ddd62011-04-21 00:48:27 +02006084 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006085 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006086 err = -ENOMEM;
6087 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006088 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006089 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006090 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006091 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006092 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006093 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006094
6095 if (!path) {
6096 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006097 if (!path) {
6098 err = -ENOMEM;
6099 goto out;
6100 }
6101 /*
6102 * Chances are we'll be called again, so go ahead and do
6103 * readahead
6104 */
6105 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006106 }
6107
Chris Mason179e29e2007-11-01 11:28:41 -04006108 ret = btrfs_lookup_file_extent(trans, root, path,
6109 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006110 if (ret < 0) {
6111 err = ret;
6112 goto out;
6113 }
6114
6115 if (ret != 0) {
6116 if (path->slots[0] == 0)
6117 goto not_found;
6118 path->slots[0]--;
6119 }
6120
Chris Mason5f39d392007-10-15 16:14:19 -04006121 leaf = path->nodes[0];
6122 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006123 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006124 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006125 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6126 found_type = btrfs_key_type(&found_key);
6127 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006128 found_type != BTRFS_EXTENT_DATA_KEY) {
6129 goto not_found;
6130 }
6131
Chris Mason5f39d392007-10-15 16:14:19 -04006132 found_type = btrfs_file_extent_type(leaf, item);
6133 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006134 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006135 if (found_type == BTRFS_FILE_EXTENT_REG ||
6136 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006137 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006138 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006139 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6140 size_t size;
6141 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006142 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006143 }
6144
6145 if (start >= extent_end) {
6146 path->slots[0]++;
6147 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6148 ret = btrfs_next_leaf(root, path);
6149 if (ret < 0) {
6150 err = ret;
6151 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006152 }
Yan Zheng9036c102008-10-30 14:19:41 -04006153 if (ret > 0)
6154 goto not_found;
6155 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006156 }
Yan Zheng9036c102008-10-30 14:19:41 -04006157 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6158 if (found_key.objectid != objectid ||
6159 found_key.type != BTRFS_EXTENT_DATA_KEY)
6160 goto not_found;
6161 if (start + len <= found_key.offset)
6162 goto not_found;
6163 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006164 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006165 em->len = found_key.offset - start;
6166 goto not_found_em;
6167 }
6168
Josef Bacikcc95bef2013-04-04 14:31:27 -04006169 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006170 if (found_type == BTRFS_FILE_EXTENT_REG ||
6171 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006172 em->start = extent_start;
6173 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006174 em->orig_start = extent_start -
6175 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006176 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6177 item);
Chris Masondb945352007-10-15 16:15:53 -04006178 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6179 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006180 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006181 goto insert;
6182 }
Li Zefan261507a02010-12-17 14:21:50 +08006183 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006184 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006185 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006186 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006187 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006188 } else {
6189 bytenr += btrfs_file_extent_offset(leaf, item);
6190 em->block_start = bytenr;
6191 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006192 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6193 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006194 }
Chris Masona52d9a82007-08-27 16:49:44 -04006195 goto insert;
6196 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006197 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006198 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006199 size_t size;
6200 size_t extent_offset;
6201 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006202
Chris Masona52d9a82007-08-27 16:49:44 -04006203 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006204 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006205 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006206 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006207 goto out;
6208 }
6209
Yan Zheng9036c102008-10-30 14:19:41 -04006210 size = btrfs_file_extent_inline_len(leaf, item);
6211 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006212 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006213 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006214 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006215 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006216 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006217 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006218 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006219 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006220 em->compress_type = compress_type;
6221 }
Yan689f9342007-10-29 11:41:07 -04006222 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006223 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006224 if (btrfs_file_extent_compression(leaf, item) !=
6225 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006226 ret = uncompress_inline(path, inode, page,
6227 pg_offset,
6228 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006229 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006230 } else {
6231 map = kmap(page);
6232 read_extent_buffer(leaf, map + pg_offset, ptr,
6233 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006234 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6235 memset(map + pg_offset + copy_size, 0,
6236 PAGE_CACHE_SIZE - pg_offset -
6237 copy_size);
6238 }
Chris Masonc8b97812008-10-29 14:49:59 -04006239 kunmap(page);
6240 }
Chris Mason179e29e2007-11-01 11:28:41 -04006241 flush_dcache_page(page);
6242 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006243 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006244 if (!trans) {
6245 kunmap(page);
6246 free_extent_map(em);
6247 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006248
David Sterbab3b4aa72011-04-21 01:20:15 +02006249 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006250 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006251
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006252 if (IS_ERR(trans))
6253 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006254 goto again;
6255 }
Chris Masonc8b97812008-10-29 14:49:59 -04006256 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006257 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006258 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006259 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006260 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006261 }
Chris Masond1310b22008-01-24 16:13:08 -05006262 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006263 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006264 goto insert;
6265 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006266 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006267 }
6268not_found:
6269 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006270 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006271 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006272not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006273 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006274 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006275insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006276 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006277 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006278 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6279 (unsigned long long)em->start,
6280 (unsigned long long)em->len,
6281 (unsigned long long)start,
6282 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006283 err = -EIO;
6284 goto out;
6285 }
Chris Masond1310b22008-01-24 16:13:08 -05006286
6287 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006288 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006289 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006290 /* it is possible that someone inserted the extent into the tree
6291 * while we had the lock dropped. It is also possible that
6292 * an overlapping map exists in the tree
6293 */
Chris Masona52d9a82007-08-27 16:49:44 -04006294 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006295 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006296
6297 ret = 0;
6298
Chris Mason3b951512008-04-17 11:29:12 -04006299 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006300 if (existing && (existing->start > start ||
6301 existing->start + existing->len <= start)) {
6302 free_extent_map(existing);
6303 existing = NULL;
6304 }
Chris Mason3b951512008-04-17 11:29:12 -04006305 if (!existing) {
6306 existing = lookup_extent_mapping(em_tree, em->start,
6307 em->len);
6308 if (existing) {
6309 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006310 em, start,
6311 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006312 free_extent_map(existing);
6313 if (err) {
6314 free_extent_map(em);
6315 em = NULL;
6316 }
6317 } else {
6318 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006319 free_extent_map(em);
6320 em = NULL;
6321 }
6322 } else {
6323 free_extent_map(em);
6324 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006325 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006326 }
Chris Masona52d9a82007-08-27 16:49:44 -04006327 }
Chris Mason890871b2009-09-02 16:24:52 -04006328 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006329out:
liubo1abe9b82011-03-24 11:18:59 +00006330
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006331 if (em)
6332 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006333
Chris Masonf4219502008-07-22 11:18:09 -04006334 if (path)
6335 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006336 if (trans) {
6337 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006338 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006339 err = ret;
6340 }
Chris Masona52d9a82007-08-27 16:49:44 -04006341 if (err) {
6342 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006343 return ERR_PTR(err);
6344 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006345 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006346 return em;
6347}
6348
Chris Masonec29ed52011-02-23 16:23:20 -05006349struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6350 size_t pg_offset, u64 start, u64 len,
6351 int create)
6352{
6353 struct extent_map *em;
6354 struct extent_map *hole_em = NULL;
6355 u64 range_start = start;
6356 u64 end;
6357 u64 found;
6358 u64 found_end;
6359 int err = 0;
6360
6361 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6362 if (IS_ERR(em))
6363 return em;
6364 if (em) {
6365 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006366 * if our em maps to
6367 * - a hole or
6368 * - a pre-alloc extent,
6369 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006370 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006371 if (em->block_start != EXTENT_MAP_HOLE &&
6372 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006373 return em;
6374 else
6375 hole_em = em;
6376 }
6377
6378 /* check to see if we've wrapped (len == -1 or similar) */
6379 end = start + len;
6380 if (end < start)
6381 end = (u64)-1;
6382 else
6383 end -= 1;
6384
6385 em = NULL;
6386
6387 /* ok, we didn't find anything, lets look for delalloc */
6388 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6389 end, len, EXTENT_DELALLOC, 1);
6390 found_end = range_start + found;
6391 if (found_end < range_start)
6392 found_end = (u64)-1;
6393
6394 /*
6395 * we didn't find anything useful, return
6396 * the original results from get_extent()
6397 */
6398 if (range_start > end || found_end <= start) {
6399 em = hole_em;
6400 hole_em = NULL;
6401 goto out;
6402 }
6403
6404 /* adjust the range_start to make sure it doesn't
6405 * go backwards from the start they passed in
6406 */
6407 range_start = max(start,range_start);
6408 found = found_end - range_start;
6409
6410 if (found > 0) {
6411 u64 hole_start = start;
6412 u64 hole_len = len;
6413
David Sterba172ddd62011-04-21 00:48:27 +02006414 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006415 if (!em) {
6416 err = -ENOMEM;
6417 goto out;
6418 }
6419 /*
6420 * when btrfs_get_extent can't find anything it
6421 * returns one huge hole
6422 *
6423 * make sure what it found really fits our range, and
6424 * adjust to make sure it is based on the start from
6425 * the caller
6426 */
6427 if (hole_em) {
6428 u64 calc_end = extent_map_end(hole_em);
6429
6430 if (calc_end <= start || (hole_em->start > end)) {
6431 free_extent_map(hole_em);
6432 hole_em = NULL;
6433 } else {
6434 hole_start = max(hole_em->start, start);
6435 hole_len = calc_end - hole_start;
6436 }
6437 }
6438 em->bdev = NULL;
6439 if (hole_em && range_start > hole_start) {
6440 /* our hole starts before our delalloc, so we
6441 * have to return just the parts of the hole
6442 * that go until the delalloc starts
6443 */
6444 em->len = min(hole_len,
6445 range_start - hole_start);
6446 em->start = hole_start;
6447 em->orig_start = hole_start;
6448 /*
6449 * don't adjust block start at all,
6450 * it is fixed at EXTENT_MAP_HOLE
6451 */
6452 em->block_start = hole_em->block_start;
6453 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006454 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6455 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006456 } else {
6457 em->start = range_start;
6458 em->len = found;
6459 em->orig_start = range_start;
6460 em->block_start = EXTENT_MAP_DELALLOC;
6461 em->block_len = found;
6462 }
6463 } else if (hole_em) {
6464 return hole_em;
6465 }
6466out:
6467
6468 free_extent_map(hole_em);
6469 if (err) {
6470 free_extent_map(em);
6471 return ERR_PTR(err);
6472 }
6473 return em;
6474}
6475
Josef Bacik4b46fce2010-05-23 11:00:55 -04006476static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6477 u64 start, u64 len)
6478{
6479 struct btrfs_root *root = BTRFS_I(inode)->root;
6480 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006481 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006482 struct btrfs_key ins;
6483 u64 alloc_hint;
6484 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006485
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006486 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006487 if (IS_ERR(trans))
6488 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006489
6490 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6491
6492 alloc_hint = get_extent_allocation_hint(inode, start, len);
6493 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006494 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006495 if (ret) {
6496 em = ERR_PTR(ret);
6497 goto out;
6498 }
6499
Josef Bacik70c8a912012-10-11 16:54:30 -04006500 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006501 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006502 if (IS_ERR(em))
6503 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006504
6505 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6506 ins.offset, ins.offset, 0);
6507 if (ret) {
6508 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6509 em = ERR_PTR(ret);
6510 }
6511out:
6512 btrfs_end_transaction(trans, root);
6513 return em;
6514}
6515
Chris Mason46bfbb52010-05-26 11:04:10 -04006516/*
6517 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6518 * block must be cow'd
6519 */
6520static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006521 struct inode *inode, u64 offset, u64 *len,
6522 u64 *orig_start, u64 *orig_block_len,
6523 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006524{
6525 struct btrfs_path *path;
6526 int ret;
6527 struct extent_buffer *leaf;
6528 struct btrfs_root *root = BTRFS_I(inode)->root;
6529 struct btrfs_file_extent_item *fi;
6530 struct btrfs_key key;
6531 u64 disk_bytenr;
6532 u64 backref_offset;
6533 u64 extent_end;
6534 u64 num_bytes;
6535 int slot;
6536 int found_type;
6537
6538 path = btrfs_alloc_path();
6539 if (!path)
6540 return -ENOMEM;
6541
Li Zefan33345d012011-04-20 10:31:50 +08006542 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006543 offset, 0);
6544 if (ret < 0)
6545 goto out;
6546
6547 slot = path->slots[0];
6548 if (ret == 1) {
6549 if (slot == 0) {
6550 /* can't find the item, must cow */
6551 ret = 0;
6552 goto out;
6553 }
6554 slot--;
6555 }
6556 ret = 0;
6557 leaf = path->nodes[0];
6558 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006559 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006560 key.type != BTRFS_EXTENT_DATA_KEY) {
6561 /* not our file or wrong item type, must cow */
6562 goto out;
6563 }
6564
6565 if (key.offset > offset) {
6566 /* Wrong offset, must cow */
6567 goto out;
6568 }
6569
6570 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6571 found_type = btrfs_file_extent_type(leaf, fi);
6572 if (found_type != BTRFS_FILE_EXTENT_REG &&
6573 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6574 /* not a regular extent, must cow */
6575 goto out;
6576 }
6577 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6578 backref_offset = btrfs_file_extent_offset(leaf, fi);
6579
Josef Bacikeb384b52013-04-24 16:32:55 -04006580 *orig_start = key.offset - backref_offset;
6581 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6582 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6583
Chris Mason46bfbb52010-05-26 11:04:10 -04006584 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006585 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006586 /* extent doesn't include our full range, must cow */
6587 goto out;
6588 }
6589
6590 if (btrfs_extent_readonly(root, disk_bytenr))
6591 goto out;
6592
6593 /*
6594 * look for other files referencing this extent, if we
6595 * find any we must cow
6596 */
Li Zefan33345d012011-04-20 10:31:50 +08006597 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006598 key.offset - backref_offset, disk_bytenr))
6599 goto out;
6600
6601 /*
6602 * adjust disk_bytenr and num_bytes to cover just the bytes
6603 * in this extent we are about to write. If there
6604 * are any csums in that range we have to cow in order
6605 * to keep the csums correct
6606 */
6607 disk_bytenr += backref_offset;
6608 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006609 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006610 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6611 goto out;
6612 /*
6613 * all of the above have passed, it is safe to overwrite this extent
6614 * without cow
6615 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006616 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006617 ret = 1;
6618out:
6619 btrfs_free_path(path);
6620 return ret;
6621}
6622
Josef Bacikeb838e72012-07-31 16:28:48 -04006623static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6624 struct extent_state **cached_state, int writing)
6625{
6626 struct btrfs_ordered_extent *ordered;
6627 int ret = 0;
6628
6629 while (1) {
6630 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6631 0, cached_state);
6632 /*
6633 * We're concerned with the entire range that we're going to be
6634 * doing DIO to, so we need to make sure theres no ordered
6635 * extents in this range.
6636 */
6637 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6638 lockend - lockstart + 1);
6639
6640 /*
6641 * We need to make sure there are no buffered pages in this
6642 * range either, we could have raced between the invalidate in
6643 * generic_file_direct_write and locking the extent. The
6644 * invalidate needs to happen so that reads after a write do not
6645 * get stale data.
6646 */
6647 if (!ordered && (!writing ||
6648 !test_range_bit(&BTRFS_I(inode)->io_tree,
6649 lockstart, lockend, EXTENT_UPTODATE, 0,
6650 *cached_state)))
6651 break;
6652
6653 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6654 cached_state, GFP_NOFS);
6655
6656 if (ordered) {
6657 btrfs_start_ordered_extent(inode, ordered, 1);
6658 btrfs_put_ordered_extent(ordered);
6659 } else {
6660 /* Screw you mmap */
6661 ret = filemap_write_and_wait_range(inode->i_mapping,
6662 lockstart,
6663 lockend);
6664 if (ret)
6665 break;
6666
6667 /*
6668 * If we found a page that couldn't be invalidated just
6669 * fall back to buffered.
6670 */
6671 ret = invalidate_inode_pages2_range(inode->i_mapping,
6672 lockstart >> PAGE_CACHE_SHIFT,
6673 lockend >> PAGE_CACHE_SHIFT);
6674 if (ret)
6675 break;
6676 }
6677
6678 cond_resched();
6679 }
6680
6681 return ret;
6682}
6683
Josef Bacik69ffb542012-09-11 15:40:07 -04006684static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6685 u64 len, u64 orig_start,
6686 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006687 u64 orig_block_len, u64 ram_bytes,
6688 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006689{
6690 struct extent_map_tree *em_tree;
6691 struct extent_map *em;
6692 struct btrfs_root *root = BTRFS_I(inode)->root;
6693 int ret;
6694
6695 em_tree = &BTRFS_I(inode)->extent_tree;
6696 em = alloc_extent_map();
6697 if (!em)
6698 return ERR_PTR(-ENOMEM);
6699
6700 em->start = start;
6701 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006702 em->mod_start = start;
6703 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006704 em->len = len;
6705 em->block_len = block_len;
6706 em->block_start = block_start;
6707 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006708 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006709 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006710 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006711 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6712 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006713 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006714
6715 do {
6716 btrfs_drop_extent_cache(inode, em->start,
6717 em->start + em->len - 1, 0);
6718 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006719 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006720 write_unlock(&em_tree->lock);
6721 } while (ret == -EEXIST);
6722
6723 if (ret) {
6724 free_extent_map(em);
6725 return ERR_PTR(ret);
6726 }
6727
6728 return em;
6729}
6730
6731
Josef Bacik4b46fce2010-05-23 11:00:55 -04006732static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6733 struct buffer_head *bh_result, int create)
6734{
6735 struct extent_map *em;
6736 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006737 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006738 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006739 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006740 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006741 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006742 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006743 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006744
Miao Xie172a5042013-02-21 02:48:22 -07006745 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006746 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006747 else
Josef Bacikc3298612012-08-03 16:49:19 -04006748 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006749
Josef Bacikc3298612012-08-03 16:49:19 -04006750 lockstart = start;
6751 lockend = start + len - 1;
6752
Josef Bacikeb838e72012-07-31 16:28:48 -04006753 /*
6754 * If this errors out it's because we couldn't invalidate pagecache for
6755 * this range and we need to fallback to buffered.
6756 */
6757 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6758 return -ENOTBLK;
6759
Josef Bacik4b46fce2010-05-23 11:00:55 -04006760 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006761 if (IS_ERR(em)) {
6762 ret = PTR_ERR(em);
6763 goto unlock_err;
6764 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006765
6766 /*
6767 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6768 * io. INLINE is special, and we could probably kludge it in here, but
6769 * it's still buffered so for safety lets just fall back to the generic
6770 * buffered path.
6771 *
6772 * For COMPRESSED we _have_ to read the entire extent in so we can
6773 * decompress it, so there will be buffering required no matter what we
6774 * do, so go ahead and fallback to buffered.
6775 *
6776 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6777 * to buffered IO. Don't blame me, this is the price we pay for using
6778 * the generic code.
6779 */
6780 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6781 em->block_start == EXTENT_MAP_INLINE) {
6782 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006783 ret = -ENOTBLK;
6784 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006785 }
6786
6787 /* Just a good old fashioned hole, return */
6788 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6789 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6790 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006791 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006792 }
6793
6794 /*
6795 * We don't allocate a new extent in the following cases
6796 *
6797 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6798 * existing extent.
6799 * 2) The extent is marked as PREALLOC. We're good to go here and can
6800 * just use the extent.
6801 *
6802 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006803 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006804 len = min(len, em->len - (start - em->start));
6805 lockstart = start + len;
6806 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006807 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808
6809 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6810 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6811 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006812 int type;
6813 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006814 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006815
6816 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6817 type = BTRFS_ORDERED_PREALLOC;
6818 else
6819 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006820 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006821 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006822
6823 /*
6824 * we're not going to log anything, but we do need
6825 * to make sure the current transaction stays open
6826 * while we look for nocow cross refs
6827 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006828 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006829 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006830 goto must_cow;
6831
Josef Bacikeb384b52013-04-24 16:32:55 -04006832 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6833 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006834 if (type == BTRFS_ORDERED_PREALLOC) {
6835 free_extent_map(em);
6836 em = create_pinned_em(inode, start, len,
6837 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006838 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006839 orig_block_len,
6840 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006841 if (IS_ERR(em)) {
6842 btrfs_end_transaction(trans, root);
6843 goto unlock_err;
6844 }
6845 }
6846
Chris Mason46bfbb52010-05-26 11:04:10 -04006847 ret = btrfs_add_ordered_extent_dio(inode, start,
6848 block_start, len, len, type);
6849 btrfs_end_transaction(trans, root);
6850 if (ret) {
6851 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006852 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006853 }
6854 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006855 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006856 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006858must_cow:
6859 /*
6860 * this will cow the extent, reset the len in case we changed
6861 * it above
6862 */
6863 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006864 free_extent_map(em);
6865 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006866 if (IS_ERR(em)) {
6867 ret = PTR_ERR(em);
6868 goto unlock_err;
6869 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006870 len = min(len, em->len - (start - em->start));
6871unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6873 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006874 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006875 bh_result->b_bdev = em->bdev;
6876 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006877 if (create) {
6878 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6879 set_buffer_new(bh_result);
6880
6881 /*
6882 * Need to update the i_size under the extent lock so buffered
6883 * readers will get the updated i_size when we unlock.
6884 */
6885 if (start + len > i_size_read(inode))
6886 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006887
Miao Xie172a5042013-02-21 02:48:22 -07006888 spin_lock(&BTRFS_I(inode)->lock);
6889 BTRFS_I(inode)->outstanding_extents++;
6890 spin_unlock(&BTRFS_I(inode)->lock);
6891
Miao Xie09348562013-02-07 10:12:07 +00006892 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6893 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6894 &cached_state, GFP_NOFS);
6895 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006896 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006897
Josef Bacikeb838e72012-07-31 16:28:48 -04006898 /*
6899 * In the case of write we need to clear and unlock the entire range,
6900 * in the case of read we need to unlock only the end area that we
6901 * aren't using if there is any left over space.
6902 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006903 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006904 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6905 lockend, unlock_bits, 1, 0,
6906 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006907 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006908 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006909 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006910
Josef Bacik4b46fce2010-05-23 11:00:55 -04006911 free_extent_map(em);
6912
6913 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006914
6915unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006916 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6917 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6918 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919}
6920
6921struct btrfs_dio_private {
6922 struct inode *inode;
6923 u64 logical_offset;
6924 u64 disk_bytenr;
6925 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006926 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006927
6928 /* number of bios pending for this dio */
6929 atomic_t pending_bios;
6930
6931 /* IO errors */
6932 int errors;
6933
6934 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006935};
6936
6937static void btrfs_endio_direct_read(struct bio *bio, int err)
6938{
Miao Xiee65e1532010-11-22 03:04:43 +00006939 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6941 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006942 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006943 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006944 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945
6946 start = dip->logical_offset;
6947 do {
6948 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6949 struct page *page = bvec->bv_page;
6950 char *kaddr;
6951 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006952 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006953 unsigned long flags;
6954
Josef Bacikc3298612012-08-03 16:49:19 -04006955 if (get_state_private(&BTRFS_I(inode)->io_tree,
6956 start, &private))
6957 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006958 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006959 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006960 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006961 csum, bvec->bv_len);
6962 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006963 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006964 local_irq_restore(flags);
6965
6966 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006967 if (csum != private) {
6968failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006969 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6970 (unsigned long long)btrfs_ino(inode),
6971 (unsigned long long)start,
6972 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006973 err = -EIO;
6974 }
6975 }
6976
6977 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006978 bvec++;
6979 } while (bvec <= bvec_end);
6980
6981 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006982 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006983 bio->bi_private = dip->private;
6984
Josef Bacik4b46fce2010-05-23 11:00:55 -04006985 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006986
6987 /* If we had a csum failure make sure to clear the uptodate flag */
6988 if (err)
6989 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006990 dio_end_io(bio, err);
6991}
6992
6993static void btrfs_endio_direct_write(struct bio *bio, int err)
6994{
6995 struct btrfs_dio_private *dip = bio->bi_private;
6996 struct inode *inode = dip->inode;
6997 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006998 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006999 u64 ordered_offset = dip->logical_offset;
7000 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007001 int ret;
7002
7003 if (err)
7004 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007005again:
7006 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7007 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007008 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007009 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007010 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007011
Josef Bacik5fd02042012-05-02 14:00:54 -04007012 ordered->work.func = finish_ordered_fn;
7013 ordered->work.flags = 0;
7014 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7015 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007016out_test:
7017 /*
7018 * our bio might span multiple ordered extents. If we haven't
7019 * completed the accounting for the whole dio, go back and try again
7020 */
7021 if (ordered_offset < dip->logical_offset + dip->bytes) {
7022 ordered_bytes = dip->logical_offset + dip->bytes -
7023 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007024 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007025 goto again;
7026 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007027out_done:
7028 bio->bi_private = dip->private;
7029
Josef Bacik4b46fce2010-05-23 11:00:55 -04007030 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007031
7032 /* If we had an error make sure to clear the uptodate flag */
7033 if (err)
7034 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007035 dio_end_io(bio, err);
7036}
7037
Chris Masoneaf25d92010-05-25 09:48:28 -04007038static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7039 struct bio *bio, int mirror_num,
7040 unsigned long bio_flags, u64 offset)
7041{
7042 int ret;
7043 struct btrfs_root *root = BTRFS_I(inode)->root;
7044 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007045 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007046 return 0;
7047}
7048
Miao Xiee65e1532010-11-22 03:04:43 +00007049static void btrfs_end_dio_bio(struct bio *bio, int err)
7050{
7051 struct btrfs_dio_private *dip = bio->bi_private;
7052
7053 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007054 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007055 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007056 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007057 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007058 dip->errors = 1;
7059
7060 /*
7061 * before atomic variable goto zero, we must make sure
7062 * dip->errors is perceived to be set.
7063 */
7064 smp_mb__before_atomic_dec();
7065 }
7066
7067 /* if there are more bios still pending for this dio, just exit */
7068 if (!atomic_dec_and_test(&dip->pending_bios))
7069 goto out;
7070
7071 if (dip->errors)
7072 bio_io_error(dip->orig_bio);
7073 else {
7074 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7075 bio_endio(dip->orig_bio, 0);
7076 }
7077out:
7078 bio_put(bio);
7079}
7080
7081static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7082 u64 first_sector, gfp_t gfp_flags)
7083{
7084 int nr_vecs = bio_get_nr_vecs(bdev);
7085 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7086}
7087
7088static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7089 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007090 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007091{
7092 int write = rw & REQ_WRITE;
7093 struct btrfs_root *root = BTRFS_I(inode)->root;
7094 int ret;
7095
Josef Bacikb812ce22012-11-16 13:56:32 -05007096 if (async_submit)
7097 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7098
Miao Xiee65e1532010-11-22 03:04:43 +00007099 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007100
7101 if (!write) {
7102 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7103 if (ret)
7104 goto err;
7105 }
Miao Xiee65e1532010-11-22 03:04:43 +00007106
Josef Bacik1ae39932011-04-06 14:41:34 -04007107 if (skip_sum)
7108 goto map;
7109
7110 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007111 ret = btrfs_wq_submit_bio(root->fs_info,
7112 inode, rw, bio, 0, 0,
7113 file_offset,
7114 __btrfs_submit_bio_start_direct_io,
7115 __btrfs_submit_bio_done);
7116 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007117 } else if (write) {
7118 /*
7119 * If we aren't doing async submit, calculate the csum of the
7120 * bio now.
7121 */
7122 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7123 if (ret)
7124 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007125 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007126 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007127 if (ret)
7128 goto err;
7129 }
Miao Xiee65e1532010-11-22 03:04:43 +00007130
Josef Bacik1ae39932011-04-06 14:41:34 -04007131map:
7132 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007133err:
7134 bio_put(bio);
7135 return ret;
7136}
7137
7138static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7139 int skip_sum)
7140{
7141 struct inode *inode = dip->inode;
7142 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007143 struct bio *bio;
7144 struct bio *orig_bio = dip->orig_bio;
7145 struct bio_vec *bvec = orig_bio->bi_io_vec;
7146 u64 start_sector = orig_bio->bi_sector;
7147 u64 file_offset = dip->logical_offset;
7148 u64 submit_len = 0;
7149 u64 map_length;
7150 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007151 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007152 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007153
Miao Xiee65e1532010-11-22 03:04:43 +00007154 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007155 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007156 &map_length, NULL, 0);
7157 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007158 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007159 return -EIO;
7160 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007161 if (map_length >= orig_bio->bi_size) {
7162 bio = orig_bio;
7163 goto submit;
7164 }
7165
David Woodhouse53b381b2013-01-29 18:40:14 -05007166 /* async crcs make it difficult to collect full stripe writes. */
7167 if (btrfs_get_alloc_profile(root, 1) &
7168 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7169 async_submit = 0;
7170 else
7171 async_submit = 1;
7172
Josef Bacik02f57c72011-04-06 14:25:44 -04007173 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7174 if (!bio)
7175 return -ENOMEM;
7176 bio->bi_private = dip;
7177 bio->bi_end_io = btrfs_end_dio_bio;
7178 atomic_inc(&dip->pending_bios);
7179
Miao Xiee65e1532010-11-22 03:04:43 +00007180 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7181 if (unlikely(map_length < submit_len + bvec->bv_len ||
7182 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7183 bvec->bv_offset) < bvec->bv_len)) {
7184 /*
7185 * inc the count before we submit the bio so
7186 * we know the end IO handler won't happen before
7187 * we inc the count. Otherwise, the dip might get freed
7188 * before we're done setting it up
7189 */
7190 atomic_inc(&dip->pending_bios);
7191 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7192 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007193 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007194 if (ret) {
7195 bio_put(bio);
7196 atomic_dec(&dip->pending_bios);
7197 goto out_err;
7198 }
7199
Miao Xiee65e1532010-11-22 03:04:43 +00007200 start_sector += submit_len >> 9;
7201 file_offset += submit_len;
7202
7203 submit_len = 0;
7204 nr_pages = 0;
7205
7206 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7207 start_sector, GFP_NOFS);
7208 if (!bio)
7209 goto out_err;
7210 bio->bi_private = dip;
7211 bio->bi_end_io = btrfs_end_dio_bio;
7212
7213 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007214 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007215 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007216 &map_length, NULL, 0);
7217 if (ret) {
7218 bio_put(bio);
7219 goto out_err;
7220 }
7221 } else {
7222 submit_len += bvec->bv_len;
7223 nr_pages ++;
7224 bvec++;
7225 }
7226 }
7227
Josef Bacik02f57c72011-04-06 14:25:44 -04007228submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007229 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007230 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007231 if (!ret)
7232 return 0;
7233
7234 bio_put(bio);
7235out_err:
7236 dip->errors = 1;
7237 /*
7238 * before atomic variable goto zero, we must
7239 * make sure dip->errors is perceived to be set.
7240 */
7241 smp_mb__before_atomic_dec();
7242 if (atomic_dec_and_test(&dip->pending_bios))
7243 bio_io_error(dip->orig_bio);
7244
7245 /* bio_end_io() will handle error, so we needn't return it */
7246 return 0;
7247}
7248
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7250 loff_t file_offset)
7251{
7252 struct btrfs_root *root = BTRFS_I(inode)->root;
7253 struct btrfs_dio_private *dip;
7254 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007256 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257 int ret = 0;
7258
7259 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7260
7261 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7262 if (!dip) {
7263 ret = -ENOMEM;
7264 goto free_ordered;
7265 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266
7267 dip->private = bio->bi_private;
7268 dip->inode = inode;
7269 dip->logical_offset = file_offset;
7270
Josef Bacik4b46fce2010-05-23 11:00:55 -04007271 dip->bytes = 0;
7272 do {
7273 dip->bytes += bvec->bv_len;
7274 bvec++;
7275 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7276
Chris Mason46bfbb52010-05-26 11:04:10 -04007277 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007279 dip->errors = 0;
7280 dip->orig_bio = bio;
7281 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007282
7283 if (write)
7284 bio->bi_end_io = btrfs_endio_direct_write;
7285 else
7286 bio->bi_end_io = btrfs_endio_direct_read;
7287
Miao Xiee65e1532010-11-22 03:04:43 +00007288 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7289 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007290 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007291free_ordered:
7292 /*
7293 * If this is a write, we need to clean up the reserved space and kill
7294 * the ordered extent.
7295 */
7296 if (write) {
7297 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007298 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7300 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7301 btrfs_free_reserved_extent(root, ordered->start,
7302 ordered->disk_len);
7303 btrfs_put_ordered_extent(ordered);
7304 btrfs_put_ordered_extent(ordered);
7305 }
7306 bio_endio(bio, ret);
7307}
7308
Chris Mason5a5f79b2010-05-26 21:33:37 -04007309static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7310 const struct iovec *iov, loff_t offset,
7311 unsigned long nr_segs)
7312{
7313 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007314 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007315 size_t size;
7316 unsigned long addr;
7317 unsigned blocksize_mask = root->sectorsize - 1;
7318 ssize_t retval = -EINVAL;
7319 loff_t end = offset;
7320
7321 if (offset & blocksize_mask)
7322 goto out;
7323
7324 /* Check the memory alignment. Blocks cannot straddle pages */
7325 for (seg = 0; seg < nr_segs; seg++) {
7326 addr = (unsigned long)iov[seg].iov_base;
7327 size = iov[seg].iov_len;
7328 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007329 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007330 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007331
7332 /* If this is a write we don't need to check anymore */
7333 if (rw & WRITE)
7334 continue;
7335
7336 /*
7337 * Check to make sure we don't have duplicate iov_base's in this
7338 * iovec, if so return EINVAL, otherwise we'll get csum errors
7339 * when reading back.
7340 */
7341 for (i = seg + 1; i < nr_segs; i++) {
7342 if (iov[seg].iov_base == iov[i].iov_base)
7343 goto out;
7344 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007345 }
7346 retval = 0;
7347out:
7348 return retval;
7349}
Josef Bacikeb838e72012-07-31 16:28:48 -04007350
Chris Mason16432982008-04-10 10:23:21 -04007351static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7352 const struct iovec *iov, loff_t offset,
7353 unsigned long nr_segs)
7354{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007355 struct file *file = iocb->ki_filp;
7356 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007357 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007358 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007359 bool wakeup = true;
7360 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007361 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007362
Chris Mason5a5f79b2010-05-26 21:33:37 -04007363 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007364 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007365 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007366
Miao Xie38851cc2013-02-08 07:04:11 +00007367 atomic_inc(&inode->i_dio_count);
7368 smp_mb__after_atomic_inc();
7369
Miao Xie09348562013-02-07 10:12:07 +00007370 if (rw & WRITE) {
7371 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007372 /*
7373 * If the write DIO is beyond the EOF, we need update
7374 * the isize, but it is protected by i_mutex. So we can
7375 * not unlock the i_mutex at this case.
7376 */
7377 if (offset + count <= inode->i_size) {
7378 mutex_unlock(&inode->i_mutex);
7379 relock = true;
7380 }
Miao Xie09348562013-02-07 10:12:07 +00007381 ret = btrfs_delalloc_reserve_space(inode, count);
7382 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007383 goto out;
7384 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7385 &BTRFS_I(inode)->runtime_flags))) {
7386 inode_dio_done(inode);
7387 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7388 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007389 }
7390
7391 ret = __blockdev_direct_IO(rw, iocb, inode,
7392 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7393 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007394 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007395 if (rw & WRITE) {
7396 if (ret < 0 && ret != -EIOCBQUEUED)
7397 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007398 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007399 btrfs_delalloc_release_space(inode,
7400 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007401 else
7402 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007403 }
Miao Xie38851cc2013-02-08 07:04:11 +00007404out:
Miao Xie2e60a512013-02-08 07:01:08 +00007405 if (wakeup)
7406 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007407 if (relock)
7408 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007409
7410 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007411}
7412
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007413#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7414
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007415static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7416 __u64 start, __u64 len)
7417{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007418 int ret;
7419
7420 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7421 if (ret)
7422 return ret;
7423
Chris Masonec29ed52011-02-23 16:23:20 -05007424 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007425}
7426
Chris Mason9ebefb182007-06-15 13:50:00 -04007427int btrfs_readpage(struct file *file, struct page *page)
7428{
Chris Masond1310b22008-01-24 16:13:08 -05007429 struct extent_io_tree *tree;
7430 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007431 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007432}
Chris Mason1832a6d2007-12-21 16:27:21 -05007433
Chris Mason39279cc2007-06-12 06:35:45 -04007434static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7435{
Chris Masond1310b22008-01-24 16:13:08 -05007436 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007437
7438
7439 if (current->flags & PF_MEMALLOC) {
7440 redirty_page_for_writepage(wbc, page);
7441 unlock_page(page);
7442 return 0;
7443 }
Chris Masond1310b22008-01-24 16:13:08 -05007444 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007445 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7446}
Chris Mason39279cc2007-06-12 06:35:45 -04007447
Eric Sandeen48a3b632013-04-25 20:41:01 +00007448static int btrfs_writepages(struct address_space *mapping,
7449 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007450{
Chris Masond1310b22008-01-24 16:13:08 -05007451 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007452
Chris Masond1310b22008-01-24 16:13:08 -05007453 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007454 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7455}
7456
Chris Mason3ab2fb52007-11-08 10:59:22 -05007457static int
7458btrfs_readpages(struct file *file, struct address_space *mapping,
7459 struct list_head *pages, unsigned nr_pages)
7460{
Chris Masond1310b22008-01-24 16:13:08 -05007461 struct extent_io_tree *tree;
7462 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007463 return extent_readpages(tree, mapping, pages, nr_pages,
7464 btrfs_get_extent);
7465}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007466static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007467{
Chris Masond1310b22008-01-24 16:13:08 -05007468 struct extent_io_tree *tree;
7469 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007470 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007471
Chris Masond1310b22008-01-24 16:13:08 -05007472 tree = &BTRFS_I(page->mapping->host)->io_tree;
7473 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007474 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007475 if (ret == 1) {
7476 ClearPagePrivate(page);
7477 set_page_private(page, 0);
7478 page_cache_release(page);
7479 }
7480 return ret;
7481}
Chris Mason39279cc2007-06-12 06:35:45 -04007482
Chris Masone6dcd2d2008-07-17 12:53:50 -04007483static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7484{
Chris Mason98509cf2008-09-11 15:51:43 -04007485 if (PageWriteback(page) || PageDirty(page))
7486 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007487 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007488}
7489
Chris Masona52d9a82007-08-27 16:49:44 -04007490static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7491{
Josef Bacik5fd02042012-05-02 14:00:54 -04007492 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007493 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007494 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007495 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007496 u64 page_start = page_offset(page);
7497 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007498
Chris Mason8b62b722009-09-02 16:53:46 -04007499 /*
7500 * we have the page locked, so new writeback can't start,
7501 * and the dirty bit won't be cleared while we are here.
7502 *
7503 * Wait for IO on this page so that we can safely clear
7504 * the PagePrivate2 bit and do ordered accounting
7505 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007507
Josef Bacik5fd02042012-05-02 14:00:54 -04007508 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007509 if (offset) {
7510 btrfs_releasepage(page, GFP_NOFS);
7511 return;
7512 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007513 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007514 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007515 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007516 /*
7517 * IO on this page will never be started, so we need
7518 * to account for any ordered extents now
7519 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007520 clear_extent_bit(tree, page_start, page_end,
7521 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007522 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7523 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007524 /*
7525 * whoever cleared the private bit is responsible
7526 * for the finish_ordered_io
7527 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007528 if (TestClearPagePrivate2(page) &&
7529 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7530 PAGE_CACHE_SIZE, 1)) {
7531 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007532 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007534 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007535 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007536 }
7537 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007538 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007539 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7540 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007541 __btrfs_releasepage(page, GFP_NOFS);
7542
Chris Mason4a096752008-07-21 10:29:44 -04007543 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007544 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007545 ClearPagePrivate(page);
7546 set_page_private(page, 0);
7547 page_cache_release(page);
7548 }
Chris Mason39279cc2007-06-12 06:35:45 -04007549}
7550
Chris Mason9ebefb182007-06-15 13:50:00 -04007551/*
7552 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7553 * called from a page fault handler when a page is first dirtied. Hence we must
7554 * be careful to check for EOF conditions here. We set the page up correctly
7555 * for a written page which means we get ENOSPC checking when writing into
7556 * holes and correct delalloc and unwritten extent mapping on filesystems that
7557 * support these features.
7558 *
7559 * We are not allowed to take the i_mutex here so we have to play games to
7560 * protect against truncate races as the page could now be beyond EOF. Because
7561 * vmtruncate() writes the inode size before removing pages, once we have the
7562 * page lock we can determine safely if the page is beyond EOF. If it is not
7563 * beyond EOF, then the page is guaranteed safe against truncation until we
7564 * unlock the page.
7565 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007566int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007567{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007568 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007569 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007570 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007571 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7572 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007573 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007574 char *kaddr;
7575 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007576 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007577 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007578 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007579 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007580 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007581
Jan Karab2b5ef52012-06-12 16:20:45 +02007582 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007583 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007584 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007585 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007586 reserved = 1;
7587 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007588 if (ret) {
7589 if (ret == -ENOMEM)
7590 ret = VM_FAULT_OOM;
7591 else /* -ENOSPC, -EIO, etc */
7592 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007593 if (reserved)
7594 goto out;
7595 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007596 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007597
Nick Piggin56a76f82009-03-31 15:23:23 -07007598 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007599again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007600 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007601 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007602 page_start = page_offset(page);
7603 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007604
Chris Mason9ebefb182007-06-15 13:50:00 -04007605 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007606 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007607 /* page got truncated out from underneath us */
7608 goto out_unlock;
7609 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007610 wait_on_page_writeback(page);
7611
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007612 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007613 set_page_extent_mapped(page);
7614
Chris Masoneb84ae02008-07-17 13:53:27 -04007615 /*
7616 * we can't set the delalloc bits if there are pending ordered
7617 * extents. Drop our locks and wait for them to finish
7618 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007619 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7620 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007621 unlock_extent_cached(io_tree, page_start, page_end,
7622 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007623 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007624 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007625 btrfs_put_ordered_extent(ordered);
7626 goto again;
7627 }
7628
Josef Bacikfbf19082009-10-01 17:10:23 -04007629 /*
7630 * XXX - page_mkwrite gets called every time the page is dirtied, even
7631 * if it was already dirty, so for space accounting reasons we need to
7632 * clear any delalloc bits for the range we are fixing to save. There
7633 * is probably a better way to do this, but for now keep consistent with
7634 * prepare_pages in the normal write path.
7635 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007636 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007637 EXTENT_DIRTY | EXTENT_DELALLOC |
7638 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007639 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007640
Josef Bacik2ac55d42010-02-03 19:33:23 +00007641 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7642 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007643 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007644 unlock_extent_cached(io_tree, page_start, page_end,
7645 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007646 ret = VM_FAULT_SIGBUS;
7647 goto out_unlock;
7648 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007649 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007650
7651 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007652 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007653 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007654 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007655 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007656
Chris Masone6dcd2d2008-07-17 12:53:50 -04007657 if (zero_start != PAGE_CACHE_SIZE) {
7658 kaddr = kmap(page);
7659 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7660 flush_dcache_page(page);
7661 kunmap(page);
7662 }
Chris Mason247e7432008-07-17 12:53:51 -04007663 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007664 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007665 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007666
Chris Mason257c62e2009-10-13 13:21:08 -04007667 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7668 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007669 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007670
Josef Bacik2ac55d42010-02-03 19:33:23 +00007671 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007672
7673out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007674 if (!ret) {
7675 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007676 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007677 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007678 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007679out:
Josef Bacikec39e182012-01-12 19:10:12 -05007680 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007681out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007682 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007683 return ret;
7684}
7685
Josef Bacika41ad392011-01-31 15:30:16 -05007686static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007687{
7688 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007689 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007690 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007691 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007692 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007693 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007694 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007695
Josef Bacik2aaa6652012-08-29 14:27:18 -04007696 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007697 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007698 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007699
Chris Mason4a096752008-07-21 10:29:44 -04007700 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007701 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007702
Josef Bacikfcb80c22011-05-03 10:40:22 -04007703 /*
7704 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7705 * 3 things going on here
7706 *
7707 * 1) We need to reserve space for our orphan item and the space to
7708 * delete our orphan item. Lord knows we don't want to have a dangling
7709 * orphan item because we didn't reserve space to remove it.
7710 *
7711 * 2) We need to reserve space to update our inode.
7712 *
7713 * 3) We need to have something to cache all the space that is going to
7714 * be free'd up by the truncate operation, but also have some slack
7715 * space reserved in case it uses space during the truncate (thank you
7716 * very much snapshotting).
7717 *
7718 * And we need these to all be seperate. The fact is we can use alot of
7719 * space doing the truncate, and we have no earthly idea how much space
7720 * we will use, so we need the truncate reservation to be seperate so it
7721 * doesn't end up using space reserved for updating the inode or
7722 * removing the orphan item. We also need to be able to stop the
7723 * transaction and start a new one, which means we need to be able to
7724 * update the inode several times, and we have no idea of knowing how
7725 * many times that will be, so we can't just reserve 1 item for the
7726 * entirety of the opration, so that has to be done seperately as well.
7727 * Then there is the orphan item, which does indeed need to be held on
7728 * to for the whole operation, and we need nobody to touch this reserved
7729 * space except the orphan code.
7730 *
7731 * So that leaves us with
7732 *
7733 * 1) root->orphan_block_rsv - for the orphan deletion.
7734 * 2) rsv - for the truncate reservation, which we will steal from the
7735 * transaction reservation.
7736 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7737 * updating the inode.
7738 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007739 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007740 if (!rsv)
7741 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007742 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007743 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007744
Josef Bacik907cbce2011-08-08 13:46:15 -04007745 /*
Josef Bacik07127182011-08-19 10:29:59 -04007746 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007747 * 1 for updating the inode.
7748 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007749 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007750 if (IS_ERR(trans)) {
7751 err = PTR_ERR(trans);
7752 goto out;
7753 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007754
Josef Bacik907cbce2011-08-08 13:46:15 -04007755 /* Migrate the slack space for the truncate to our reserve */
7756 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7757 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007758 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007759
Chris Mason5a3f23d2009-03-31 13:27:11 -04007760 /*
7761 * setattr is responsible for setting the ordered_data_close flag,
7762 * but that is only tested during the last file release. That
7763 * could happen well after the next commit, leaving a great big
7764 * window where new writes may get lost if someone chooses to write
7765 * to this file after truncating to zero
7766 *
7767 * The inode doesn't have any dirty data here, and so if we commit
7768 * this is a noop. If someone immediately starts writing to the inode
7769 * it is very likely we'll catch some of their writes in this
7770 * transaction, and the commit will find this file on the ordered
7771 * data list with good things to send down.
7772 *
7773 * This is a best effort solution, there is still a window where
7774 * using truncate to replace the contents of the file will
7775 * end up with a zero length file after a crash.
7776 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007777 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7778 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007779 btrfs_add_ordered_operation(trans, root, inode);
7780
Josef Bacik5dc562c2012-08-17 13:14:17 -04007781 /*
7782 * So if we truncate and then write and fsync we normally would just
7783 * write the extents that changed, which is a problem if we need to
7784 * first truncate that entire inode. So set this flag so we write out
7785 * all of the extents in the inode to the sync log so we're completely
7786 * safe.
7787 */
7788 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007789 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007790
Yan, Zheng80825102009-11-12 09:35:36 +00007791 while (1) {
7792 ret = btrfs_truncate_inode_items(trans, root, inode,
7793 inode->i_size,
7794 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007795 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007796 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007797 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007798 }
Chris Mason39279cc2007-06-12 06:35:45 -04007799
Josef Bacikfcb80c22011-05-03 10:40:22 -04007800 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007801 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007802 if (ret) {
7803 err = ret;
7804 break;
7805 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007806
Yan, Zheng80825102009-11-12 09:35:36 +00007807 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007808 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007809
7810 trans = btrfs_start_transaction(root, 2);
7811 if (IS_ERR(trans)) {
7812 ret = err = PTR_ERR(trans);
7813 trans = NULL;
7814 break;
7815 }
7816
7817 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7818 rsv, min_size);
7819 BUG_ON(ret); /* shouldn't happen */
7820 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007821 }
7822
7823 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007824 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007825 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007826 if (ret)
7827 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007828 }
7829
Chris Mason917c16b2011-11-08 14:49:59 -05007830 if (trans) {
7831 trans->block_rsv = &root->fs_info->trans_block_rsv;
7832 ret = btrfs_update_inode(trans, root, inode);
7833 if (ret && !err)
7834 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007835
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007836 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007837 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007838 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007839
7840out:
7841 btrfs_free_block_rsv(root, rsv);
7842
Josef Bacik3893e332011-01-31 16:03:11 -05007843 if (ret && !err)
7844 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007845
Josef Bacik3893e332011-01-31 16:03:11 -05007846 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007847}
7848
Sven Wegener3b963622008-06-09 21:57:42 -04007849/*
Chris Masond352ac62008-09-29 15:18:18 -04007850 * create a new subvolume directory/inode (helper for the ioctl).
7851 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007852int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007853 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007854{
Chris Mason39279cc2007-06-12 06:35:45 -04007855 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007856 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007857 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007858
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007859 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7860 new_dirid, new_dirid,
7861 S_IFDIR | (~current_umask() & S_IRWXUGO),
7862 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007863 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007864 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007865 inode->i_op = &btrfs_dir_inode_operations;
7866 inode->i_fop = &btrfs_dir_file_operations;
7867
Miklos Szeredibfe86842011-10-28 14:13:29 +02007868 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007869 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007870
Yan, Zheng76dda932009-09-21 16:00:26 -04007871 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007872
Yan, Zheng76dda932009-09-21 16:00:26 -04007873 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007874 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007875}
7876
Chris Mason39279cc2007-06-12 06:35:45 -04007877struct inode *btrfs_alloc_inode(struct super_block *sb)
7878{
7879 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007880 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007881
7882 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7883 if (!ei)
7884 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007885
7886 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007887 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007888 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007889 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007890 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007891 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007892 ei->disk_i_size = 0;
7893 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007894 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007895 ei->index_cnt = (u64)-1;
7896 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007897 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007898
Josef Bacik9e0baf62011-07-15 15:16:44 +00007899 spin_lock_init(&ei->lock);
7900 ei->outstanding_extents = 0;
7901 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007902
Josef Bacik72ac3c02012-05-23 14:13:11 -04007903 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007904 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007905
Miao Xie16cdcec2011-04-22 18:12:22 +08007906 ei->delayed_node = NULL;
7907
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007908 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007909 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007910 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7911 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007912 ei->io_tree.track_uptodate = 1;
7913 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007914 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007915 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007916 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007917 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007918 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007919 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007920 RB_CLEAR_NODE(&ei->rb_node);
7921
7922 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007923}
7924
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007925static void btrfs_i_callback(struct rcu_head *head)
7926{
7927 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007928 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7929}
7930
Chris Mason39279cc2007-06-12 06:35:45 -04007931void btrfs_destroy_inode(struct inode *inode)
7932{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007933 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007934 struct btrfs_root *root = BTRFS_I(inode)->root;
7935
Al Virob3d9b7a2012-06-09 13:51:19 -04007936 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007937 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007938 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7939 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007940 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7941 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007942
Chris Mason5a3f23d2009-03-31 13:27:11 -04007943 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007944 * This can happen where we create an inode, but somebody else also
7945 * created the same inode and we need to destroy the one we already
7946 * created.
7947 */
7948 if (!root)
7949 goto free;
7950
7951 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007952 * Make sure we're properly removed from the ordered operation
7953 * lists.
7954 */
7955 smp_mb();
7956 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7957 spin_lock(&root->fs_info->ordered_extent_lock);
7958 list_del_init(&BTRFS_I(inode)->ordered_operations);
7959 spin_unlock(&root->fs_info->ordered_extent_lock);
7960 }
7961
Josef Bacik8a35d952012-05-23 14:26:42 -04007962 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7963 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007964 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7965 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007966 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007967 }
Josef Bacik7b128762008-07-24 12:17:14 -04007968
Chris Masond3977122009-01-05 21:25:51 -05007969 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007970 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7971 if (!ordered)
7972 break;
7973 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007974 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7975 (unsigned long long)ordered->file_offset,
7976 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007977 btrfs_remove_ordered_extent(inode, ordered);
7978 btrfs_put_ordered_extent(ordered);
7979 btrfs_put_ordered_extent(ordered);
7980 }
7981 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007982 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007983 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007984free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007985 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007986}
7987
Al Viro45321ac2010-06-07 13:43:19 -04007988int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007989{
7990 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007991
Liu Bofa6ac872013-02-20 14:10:23 +00007992 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007993 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007994 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007995 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007996 else
Al Viro45321ac2010-06-07 13:43:19 -04007997 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007998}
7999
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008000static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008001{
8002 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8003
8004 inode_init_once(&ei->vfs_inode);
8005}
8006
8007void btrfs_destroy_cachep(void)
8008{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008009 /*
8010 * Make sure all delayed rcu free inodes are flushed before we
8011 * destroy cache.
8012 */
8013 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008014 if (btrfs_inode_cachep)
8015 kmem_cache_destroy(btrfs_inode_cachep);
8016 if (btrfs_trans_handle_cachep)
8017 kmem_cache_destroy(btrfs_trans_handle_cachep);
8018 if (btrfs_transaction_cachep)
8019 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008020 if (btrfs_path_cachep)
8021 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008022 if (btrfs_free_space_cachep)
8023 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008024 if (btrfs_delalloc_work_cachep)
8025 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008026}
8027
8028int btrfs_init_cachep(void)
8029{
David Sterba837e1972012-09-07 03:00:48 -06008030 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008031 sizeof(struct btrfs_inode), 0,
8032 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008033 if (!btrfs_inode_cachep)
8034 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008035
David Sterba837e1972012-09-07 03:00:48 -06008036 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008037 sizeof(struct btrfs_trans_handle), 0,
8038 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008039 if (!btrfs_trans_handle_cachep)
8040 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008041
David Sterba837e1972012-09-07 03:00:48 -06008042 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008043 sizeof(struct btrfs_transaction), 0,
8044 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008045 if (!btrfs_transaction_cachep)
8046 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008047
David Sterba837e1972012-09-07 03:00:48 -06008048 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008049 sizeof(struct btrfs_path), 0,
8050 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008051 if (!btrfs_path_cachep)
8052 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008053
David Sterba837e1972012-09-07 03:00:48 -06008054 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008055 sizeof(struct btrfs_free_space), 0,
8056 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8057 if (!btrfs_free_space_cachep)
8058 goto fail;
8059
Miao Xie8ccf6f192012-10-25 09:28:04 +00008060 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8061 sizeof(struct btrfs_delalloc_work), 0,
8062 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8063 NULL);
8064 if (!btrfs_delalloc_work_cachep)
8065 goto fail;
8066
Chris Mason39279cc2007-06-12 06:35:45 -04008067 return 0;
8068fail:
8069 btrfs_destroy_cachep();
8070 return -ENOMEM;
8071}
8072
8073static int btrfs_getattr(struct vfsmount *mnt,
8074 struct dentry *dentry, struct kstat *stat)
8075{
Miao Xiedf0af1a52013-01-29 10:11:59 +00008076 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008077 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008078 u32 blocksize = inode->i_sb->s_blocksize;
8079
Chris Mason39279cc2007-06-12 06:35:45 -04008080 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008081 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008082 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008083
8084 spin_lock(&BTRFS_I(inode)->lock);
8085 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8086 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008087 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00008088 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008089 return 0;
8090}
8091
Chris Masond3977122009-01-05 21:25:51 -05008092static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8093 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008094{
8095 struct btrfs_trans_handle *trans;
8096 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008097 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008098 struct inode *new_inode = new_dentry->d_inode;
8099 struct inode *old_inode = old_dentry->d_inode;
8100 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008101 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008102 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008103 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008104 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008105
Li Zefan33345d012011-04-20 10:31:50 +08008106 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008107 return -EPERM;
8108
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008109 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008110 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008111 return -EXDEV;
8112
Li Zefan33345d012011-04-20 10:31:50 +08008113 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8114 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008115 return -ENOTEMPTY;
8116
Chris Mason39279cc2007-06-12 06:35:45 -04008117 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008118 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008119 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008120
8121
8122 /* check for collisions, even if the name isn't there */
8123 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8124 new_dentry->d_name.name,
8125 new_dentry->d_name.len);
8126
8127 if (ret) {
8128 if (ret == -EEXIST) {
8129 /* we shouldn't get
8130 * eexist without a new_inode */
8131 if (!new_inode) {
8132 WARN_ON(1);
8133 return ret;
8134 }
8135 } else {
8136 /* maybe -EOVERFLOW */
8137 return ret;
8138 }
8139 }
8140 ret = 0;
8141
Chris Mason5a3f23d2009-03-31 13:27:11 -04008142 /*
8143 * we're using rename to replace one file with another.
8144 * and the replacement file is large. Start IO on it now so
8145 * we don't add too much work to the end of the transaction
8146 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008147 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008148 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8149 filemap_flush(old_inode->i_mapping);
8150
Yan, Zheng76dda932009-09-21 16:00:26 -04008151 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008152 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008153 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008154 /*
8155 * We want to reserve the absolute worst case amount of items. So if
8156 * both inodes are subvols and we need to unlink them then that would
8157 * require 4 item modifications, but if they are both normal inodes it
8158 * would require 5 item modifications, so we'll assume their normal
8159 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8160 * should cover the worst case number of items we'll modify.
8161 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008162 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008163 if (IS_ERR(trans)) {
8164 ret = PTR_ERR(trans);
8165 goto out_notrans;
8166 }
Chris Mason5f39d392007-10-15 16:14:19 -04008167
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008168 if (dest != root)
8169 btrfs_record_root_in_trans(trans, dest);
8170
Yan, Zhenga5719522009-09-24 09:17:31 -04008171 ret = btrfs_set_inode_index(new_dir, &index);
8172 if (ret)
8173 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008174
Li Zefan33345d012011-04-20 10:31:50 +08008175 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008176 /* force full log commit if subvolume involved. */
8177 root->fs_info->last_trans_log_full_commit = trans->transid;
8178 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008179 ret = btrfs_insert_inode_ref(trans, dest,
8180 new_dentry->d_name.name,
8181 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008182 old_ino,
8183 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008184 if (ret)
8185 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008186 /*
8187 * this is an ugly little race, but the rename is required
8188 * to make sure that if we crash, the inode is either at the
8189 * old name or the new one. pinning the log transaction lets
8190 * us make sure we don't allow a log commit to come in after
8191 * we unlink the name but before we add the new name back in.
8192 */
8193 btrfs_pin_log_trans(root);
8194 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008195 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008196 * make sure the inode gets flushed if it is replacing
8197 * something.
8198 */
Li Zefan33345d012011-04-20 10:31:50 +08008199 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008200 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008201
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008202 inode_inc_iversion(old_dir);
8203 inode_inc_iversion(new_dir);
8204 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008205 old_dir->i_ctime = old_dir->i_mtime = ctime;
8206 new_dir->i_ctime = new_dir->i_mtime = ctime;
8207 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008208
Chris Mason12fcfd22009-03-24 10:24:20 -04008209 if (old_dentry->d_parent != new_dentry->d_parent)
8210 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8211
Li Zefan33345d012011-04-20 10:31:50 +08008212 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008213 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8214 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8215 old_dentry->d_name.name,
8216 old_dentry->d_name.len);
8217 } else {
Al Viro92986792011-03-04 17:14:37 +00008218 ret = __btrfs_unlink_inode(trans, root, old_dir,
8219 old_dentry->d_inode,
8220 old_dentry->d_name.name,
8221 old_dentry->d_name.len);
8222 if (!ret)
8223 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008224 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008225 if (ret) {
8226 btrfs_abort_transaction(trans, root, ret);
8227 goto out_fail;
8228 }
Chris Mason39279cc2007-06-12 06:35:45 -04008229
8230 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008231 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008232 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008233 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008234 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8235 root_objectid = BTRFS_I(new_inode)->location.objectid;
8236 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8237 root_objectid,
8238 new_dentry->d_name.name,
8239 new_dentry->d_name.len);
8240 BUG_ON(new_inode->i_nlink == 0);
8241 } else {
8242 ret = btrfs_unlink_inode(trans, dest, new_dir,
8243 new_dentry->d_inode,
8244 new_dentry->d_name.name,
8245 new_dentry->d_name.len);
8246 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008247 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008248 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008249 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008250 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008251 if (ret) {
8252 btrfs_abort_transaction(trans, root, ret);
8253 goto out_fail;
8254 }
Chris Mason39279cc2007-06-12 06:35:45 -04008255 }
Josef Bacikaec74772008-07-24 12:12:38 -04008256
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008257 ret = btrfs_add_link(trans, new_dir, old_inode,
8258 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008259 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008260 if (ret) {
8261 btrfs_abort_transaction(trans, root, ret);
8262 goto out_fail;
8263 }
Chris Mason39279cc2007-06-12 06:35:45 -04008264
Li Zefan33345d012011-04-20 10:31:50 +08008265 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008266 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008267 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008268 btrfs_end_log_trans(root);
8269 }
Chris Mason39279cc2007-06-12 06:35:45 -04008270out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008271 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008272out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008273 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008274 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008275
Chris Mason39279cc2007-06-12 06:35:45 -04008276 return ret;
8277}
8278
Miao Xie8ccf6f192012-10-25 09:28:04 +00008279static void btrfs_run_delalloc_work(struct btrfs_work *work)
8280{
8281 struct btrfs_delalloc_work *delalloc_work;
8282
8283 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8284 work);
8285 if (delalloc_work->wait)
8286 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8287 else
8288 filemap_flush(delalloc_work->inode->i_mapping);
8289
8290 if (delalloc_work->delay_iput)
8291 btrfs_add_delayed_iput(delalloc_work->inode);
8292 else
8293 iput(delalloc_work->inode);
8294 complete(&delalloc_work->completion);
8295}
8296
8297struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8298 int wait, int delay_iput)
8299{
8300 struct btrfs_delalloc_work *work;
8301
8302 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8303 if (!work)
8304 return NULL;
8305
8306 init_completion(&work->completion);
8307 INIT_LIST_HEAD(&work->list);
8308 work->inode = inode;
8309 work->wait = wait;
8310 work->delay_iput = delay_iput;
8311 work->work.func = btrfs_run_delalloc_work;
8312
8313 return work;
8314}
8315
8316void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8317{
8318 wait_for_completion(&work->completion);
8319 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8320}
8321
Chris Masond352ac62008-09-29 15:18:18 -04008322/*
8323 * some fairly slow code that needs optimization. This walks the list
8324 * of all the inodes with pending delalloc and forces them to disk.
8325 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008326int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008327{
Chris Masonea8c2812008-08-04 23:17:27 -04008328 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008329 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008330 struct btrfs_delalloc_work *work, *next;
8331 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008332 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008333 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008334
Yan Zhengc146afa2008-11-12 14:34:12 -05008335 if (root->fs_info->sb->s_flags & MS_RDONLY)
8336 return -EROFS;
8337
Miao Xie8ccf6f192012-10-25 09:28:04 +00008338 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008339 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008340
Chris Mason75eff682008-12-15 15:54:40 -05008341 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008342 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8343 while (!list_empty(&splice)) {
8344 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008345 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008346
8347 list_del_init(&binode->delalloc_inodes);
8348
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008349 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00008350 if (!inode) {
8351 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8352 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008353 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008354 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008355
8356 list_add_tail(&binode->delalloc_inodes,
8357 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008358 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008359
8360 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8361 if (unlikely(!work)) {
8362 ret = -ENOMEM;
8363 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008364 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008365 list_add_tail(&work->list, &works);
8366 btrfs_queue_worker(&root->fs_info->flush_workers,
8367 &work->work);
8368
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008369 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008370 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008371 }
Chris Mason75eff682008-12-15 15:54:40 -05008372 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008373
Miao Xie1eafa6c2013-01-22 10:49:00 +00008374 list_for_each_entry_safe(work, next, &works, list) {
8375 list_del_init(&work->list);
8376 btrfs_wait_and_free_delalloc_work(work);
8377 }
8378
Chris Mason8c8bee12008-09-29 11:19:10 -04008379 /* the filemap_flush will queue IO into the worker threads, but
8380 * we have to make sure the IO is actually started and that
8381 * ordered extents get created before we return
8382 */
8383 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008384 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008385 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008386 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008387 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8388 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008389 }
8390 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008391 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008392out:
8393 list_for_each_entry_safe(work, next, &works, list) {
8394 list_del_init(&work->list);
8395 btrfs_wait_and_free_delalloc_work(work);
8396 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008397
8398 if (!list_empty_careful(&splice)) {
8399 spin_lock(&root->fs_info->delalloc_lock);
8400 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8401 spin_unlock(&root->fs_info->delalloc_lock);
8402 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008403 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008404}
8405
Chris Mason39279cc2007-06-12 06:35:45 -04008406static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8407 const char *symname)
8408{
8409 struct btrfs_trans_handle *trans;
8410 struct btrfs_root *root = BTRFS_I(dir)->root;
8411 struct btrfs_path *path;
8412 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008413 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008414 int err;
8415 int drop_inode = 0;
8416 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008417 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008418 int name_len;
8419 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008420 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008421 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008422 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008423
8424 name_len = strlen(symname) + 1;
8425 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8426 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008427
Josef Bacik9ed74f22009-09-11 16:12:44 -04008428 /*
8429 * 2 items for inode item and ref
8430 * 2 items for dir items
8431 * 1 item for xattr if selinux is on
8432 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008433 trans = btrfs_start_transaction(root, 5);
8434 if (IS_ERR(trans))
8435 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008436
Li Zefan581bb052011-04-20 10:06:11 +08008437 err = btrfs_find_free_ino(root, &objectid);
8438 if (err)
8439 goto out_unlock;
8440
Josef Bacikaec74772008-07-24 12:12:38 -04008441 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008442 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008443 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008444 if (IS_ERR(inode)) {
8445 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008446 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008447 }
Chris Mason39279cc2007-06-12 06:35:45 -04008448
Eric Paris2a7dba32011-02-01 11:05:39 -05008449 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008450 if (err) {
8451 drop_inode = 1;
8452 goto out_unlock;
8453 }
8454
Casey Schauflerad19db72011-12-15 10:09:07 -05008455 /*
8456 * If the active LSM wants to access the inode during
8457 * d_instantiate it needs these. Smack checks to see
8458 * if the filesystem supports xattrs by looking at the
8459 * ops vector.
8460 */
8461 inode->i_fop = &btrfs_file_operations;
8462 inode->i_op = &btrfs_file_inode_operations;
8463
Josef Bacika1b075d2010-11-19 20:36:11 +00008464 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008465 if (err)
8466 drop_inode = 1;
8467 else {
8468 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008469 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008470 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008471 }
Chris Mason39279cc2007-06-12 06:35:45 -04008472 if (drop_inode)
8473 goto out_unlock;
8474
8475 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008476 if (!path) {
8477 err = -ENOMEM;
8478 drop_inode = 1;
8479 goto out_unlock;
8480 }
Li Zefan33345d012011-04-20 10:31:50 +08008481 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008482 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008483 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8484 datasize = btrfs_file_extent_calc_inline_size(name_len);
8485 err = btrfs_insert_empty_item(trans, root, path, &key,
8486 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008487 if (err) {
8488 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008489 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008490 goto out_unlock;
8491 }
Chris Mason5f39d392007-10-15 16:14:19 -04008492 leaf = path->nodes[0];
8493 ei = btrfs_item_ptr(leaf, path->slots[0],
8494 struct btrfs_file_extent_item);
8495 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8496 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008497 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008498 btrfs_set_file_extent_encryption(leaf, ei, 0);
8499 btrfs_set_file_extent_compression(leaf, ei, 0);
8500 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8501 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8502
Chris Mason39279cc2007-06-12 06:35:45 -04008503 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008504 write_extent_buffer(leaf, symname, ptr, name_len);
8505 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008506 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008507
Chris Mason39279cc2007-06-12 06:35:45 -04008508 inode->i_op = &btrfs_symlink_inode_operations;
8509 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008510 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008511 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008512 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008513 err = btrfs_update_inode(trans, root, inode);
8514 if (err)
8515 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008516
8517out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008518 if (!err)
8519 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008520 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008521 if (drop_inode) {
8522 inode_dec_link_count(inode);
8523 iput(inode);
8524 }
Liu Bob53d3f52012-11-14 14:34:34 +00008525 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008526 return err;
8527}
Chris Mason16432982008-04-10 10:23:21 -04008528
Josef Bacik0af3d002010-06-21 14:48:16 -04008529static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8530 u64 start, u64 num_bytes, u64 min_size,
8531 loff_t actual_len, u64 *alloc_hint,
8532 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008533{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008534 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8535 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008536 struct btrfs_root *root = BTRFS_I(inode)->root;
8537 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008538 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008539 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008540 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008541 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008542 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008543
Josef Bacik0af3d002010-06-21 14:48:16 -04008544 if (trans)
8545 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008546 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008547 if (own_trans) {
8548 trans = btrfs_start_transaction(root, 3);
8549 if (IS_ERR(trans)) {
8550 ret = PTR_ERR(trans);
8551 break;
8552 }
Yan Zhengd899e052008-10-30 14:25:28 -04008553 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008554
Chris Mason154ea282013-03-05 11:11:26 -05008555 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8556 cur_bytes = max(cur_bytes, min_size);
8557 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008558 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008559 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008560 if (own_trans)
8561 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008562 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008563 }
8564
Yan Zhengd899e052008-10-30 14:25:28 -04008565 ret = insert_reserved_file_extent(trans, inode,
8566 cur_offset, ins.objectid,
8567 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008568 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008569 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008570 if (ret) {
8571 btrfs_abort_transaction(trans, root, ret);
8572 if (own_trans)
8573 btrfs_end_transaction(trans, root);
8574 break;
8575 }
Chris Masona1ed8352009-09-11 12:27:37 -04008576 btrfs_drop_extent_cache(inode, cur_offset,
8577 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008578
Josef Bacik5dc562c2012-08-17 13:14:17 -04008579 em = alloc_extent_map();
8580 if (!em) {
8581 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8582 &BTRFS_I(inode)->runtime_flags);
8583 goto next;
8584 }
8585
8586 em->start = cur_offset;
8587 em->orig_start = cur_offset;
8588 em->len = ins.offset;
8589 em->block_start = ins.objectid;
8590 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008591 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008592 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008593 em->bdev = root->fs_info->fs_devices->latest_bdev;
8594 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8595 em->generation = trans->transid;
8596
8597 while (1) {
8598 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008599 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008600 write_unlock(&em_tree->lock);
8601 if (ret != -EEXIST)
8602 break;
8603 btrfs_drop_extent_cache(inode, cur_offset,
8604 cur_offset + ins.offset - 1,
8605 0);
8606 }
8607 free_extent_map(em);
8608next:
Yan Zhengd899e052008-10-30 14:25:28 -04008609 num_bytes -= ins.offset;
8610 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008611 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008612
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008613 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008614 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008615 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008616 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008617 (actual_len > inode->i_size) &&
8618 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008619 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008620 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008621 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008622 i_size = cur_offset;
8623 i_size_write(inode, i_size);
8624 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008625 }
8626
Yan Zhengd899e052008-10-30 14:25:28 -04008627 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008628
8629 if (ret) {
8630 btrfs_abort_transaction(trans, root, ret);
8631 if (own_trans)
8632 btrfs_end_transaction(trans, root);
8633 break;
8634 }
Yan Zhengd899e052008-10-30 14:25:28 -04008635
Josef Bacik0af3d002010-06-21 14:48:16 -04008636 if (own_trans)
8637 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008638 }
Yan Zhengd899e052008-10-30 14:25:28 -04008639 return ret;
8640}
8641
Josef Bacik0af3d002010-06-21 14:48:16 -04008642int btrfs_prealloc_file_range(struct inode *inode, int mode,
8643 u64 start, u64 num_bytes, u64 min_size,
8644 loff_t actual_len, u64 *alloc_hint)
8645{
8646 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8647 min_size, actual_len, alloc_hint,
8648 NULL);
8649}
8650
8651int btrfs_prealloc_file_range_trans(struct inode *inode,
8652 struct btrfs_trans_handle *trans, int mode,
8653 u64 start, u64 num_bytes, u64 min_size,
8654 loff_t actual_len, u64 *alloc_hint)
8655{
8656 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8657 min_size, actual_len, alloc_hint, trans);
8658}
8659
Chris Masone6dcd2d2008-07-17 12:53:50 -04008660static int btrfs_set_page_dirty(struct page *page)
8661{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008662 return __set_page_dirty_nobuffers(page);
8663}
8664
Al Viro10556cb2011-06-20 19:28:19 -04008665static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008666{
Li Zefanb83cc962010-12-20 16:04:08 +08008667 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008668 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008669
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008670 if (mask & MAY_WRITE &&
8671 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8672 if (btrfs_root_readonly(root))
8673 return -EROFS;
8674 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8675 return -EACCES;
8676 }
Al Viro2830ba72011-06-20 19:16:29 -04008677 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008678}
Chris Mason39279cc2007-06-12 06:35:45 -04008679
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008680static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008681 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008682 .lookup = btrfs_lookup,
8683 .create = btrfs_create,
8684 .unlink = btrfs_unlink,
8685 .link = btrfs_link,
8686 .mkdir = btrfs_mkdir,
8687 .rmdir = btrfs_rmdir,
8688 .rename = btrfs_rename,
8689 .symlink = btrfs_symlink,
8690 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008691 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008692 .setxattr = btrfs_setxattr,
8693 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008694 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008695 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008696 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008697 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008698};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008699static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008700 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008701 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008702 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008703};
Yan, Zheng76dda932009-09-21 16:00:26 -04008704
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008705static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008706 .llseek = generic_file_llseek,
8707 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008708 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008709 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008710#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008711 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008712#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008713 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008714 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008715};
8716
Chris Masond1310b22008-01-24 16:13:08 -05008717static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008718 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008719 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008720 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008721 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008722 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008723 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008724 .set_bit_hook = btrfs_set_bit_hook,
8725 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008726 .merge_extent_hook = btrfs_merge_extent_hook,
8727 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008728};
8729
Chris Mason35054392009-01-21 13:11:13 -05008730/*
8731 * btrfs doesn't support the bmap operation because swapfiles
8732 * use bmap to make a mapping of extents in the file. They assume
8733 * these extents won't change over the life of the file and they
8734 * use the bmap result to do IO directly to the drive.
8735 *
8736 * the btrfs bmap call would return logical addresses that aren't
8737 * suitable for IO and they also will change frequently as COW
8738 * operations happen. So, swapfile + btrfs == corruption.
8739 *
8740 * For now we're avoiding this by dropping bmap.
8741 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008742static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008743 .readpage = btrfs_readpage,
8744 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008745 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008746 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008747 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008748 .invalidatepage = btrfs_invalidatepage,
8749 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008750 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008751 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008752};
8753
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008754static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008755 .readpage = btrfs_readpage,
8756 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008757 .invalidatepage = btrfs_invalidatepage,
8758 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008759};
8760
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008761static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008762 .getattr = btrfs_getattr,
8763 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008764 .setxattr = btrfs_setxattr,
8765 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008766 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008767 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008768 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008769 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008770 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008771 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008772};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008773static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008774 .getattr = btrfs_getattr,
8775 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008776 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008777 .setxattr = btrfs_setxattr,
8778 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008779 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008780 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008781 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008782 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008783};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008784static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008785 .readlink = generic_readlink,
8786 .follow_link = page_follow_link_light,
8787 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008788 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008789 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008790 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008791 .setxattr = btrfs_setxattr,
8792 .getxattr = btrfs_getxattr,
8793 .listxattr = btrfs_listxattr,
8794 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008795 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008796 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008797};
Yan, Zheng76dda932009-09-21 16:00:26 -04008798
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008799const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008800 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008801 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008802};