blob: 3817c1e49035f82314a644db7b0f7bec9fce5939 [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 }
Liu Bo38c227d2013-01-29 03:18:40 +00002265
2266 /* step 2: get inode */
2267 key.objectid = backref->inum;
2268 key.type = BTRFS_INODE_ITEM_KEY;
2269 key.offset = 0;
2270
2271 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2272 if (IS_ERR(inode)) {
2273 srcu_read_unlock(&fs_info->subvol_srcu, index);
2274 return 0;
2275 }
2276
2277 srcu_read_unlock(&fs_info->subvol_srcu, index);
2278
2279 /* step 3: relink backref */
2280 lock_start = backref->file_pos;
2281 lock_end = backref->file_pos + backref->num_bytes - 1;
2282 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2283 0, &cached);
2284
2285 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2286 if (ordered) {
2287 btrfs_put_ordered_extent(ordered);
2288 goto out_unlock;
2289 }
2290
2291 trans = btrfs_join_transaction(root);
2292 if (IS_ERR(trans)) {
2293 ret = PTR_ERR(trans);
2294 goto out_unlock;
2295 }
2296
2297 key.objectid = backref->inum;
2298 key.type = BTRFS_EXTENT_DATA_KEY;
2299 key.offset = backref->file_pos;
2300
2301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2302 if (ret < 0) {
2303 goto out_free_path;
2304 } else if (ret > 0) {
2305 ret = 0;
2306 goto out_free_path;
2307 }
2308
2309 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2310 struct btrfs_file_extent_item);
2311
2312 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2313 backref->generation)
2314 goto out_free_path;
2315
2316 btrfs_release_path(path);
2317
2318 start = backref->file_pos;
2319 if (backref->extent_offset < old->extent_offset + old->offset)
2320 start += old->extent_offset + old->offset -
2321 backref->extent_offset;
2322
2323 len = min(backref->extent_offset + backref->num_bytes,
2324 old->extent_offset + old->offset + old->len);
2325 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2326
2327 ret = btrfs_drop_extents(trans, root, inode, start,
2328 start + len, 1);
2329 if (ret)
2330 goto out_free_path;
2331again:
2332 key.objectid = btrfs_ino(inode);
2333 key.type = BTRFS_EXTENT_DATA_KEY;
2334 key.offset = start;
2335
Liu Boa09a0a72013-03-11 09:20:58 +00002336 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002337 if (merge) {
2338 struct btrfs_file_extent_item *fi;
2339 u64 extent_len;
2340 struct btrfs_key found_key;
2341
2342 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2343 if (ret < 0)
2344 goto out_free_path;
2345
2346 path->slots[0]--;
2347 leaf = path->nodes[0];
2348 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2349
2350 fi = btrfs_item_ptr(leaf, path->slots[0],
2351 struct btrfs_file_extent_item);
2352 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2353
2354 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2355 extent_len + found_key.offset == start) {
2356 btrfs_set_file_extent_num_bytes(leaf, fi,
2357 extent_len + len);
2358 btrfs_mark_buffer_dirty(leaf);
2359 inode_add_bytes(inode, len);
2360
2361 ret = 1;
2362 goto out_free_path;
2363 } else {
2364 merge = false;
2365 btrfs_release_path(path);
2366 goto again;
2367 }
2368 }
2369
2370 ret = btrfs_insert_empty_item(trans, root, path, &key,
2371 sizeof(*extent));
2372 if (ret) {
2373 btrfs_abort_transaction(trans, root, ret);
2374 goto out_free_path;
2375 }
2376
2377 leaf = path->nodes[0];
2378 item = btrfs_item_ptr(leaf, path->slots[0],
2379 struct btrfs_file_extent_item);
2380 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2381 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2382 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2383 btrfs_set_file_extent_num_bytes(leaf, item, len);
2384 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2385 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2386 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2387 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2388 btrfs_set_file_extent_encryption(leaf, item, 0);
2389 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2390
2391 btrfs_mark_buffer_dirty(leaf);
2392 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002393 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002394
2395 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2396 new->disk_len, 0,
2397 backref->root_id, backref->inum,
2398 new->file_pos, 0); /* start - extent_offset */
2399 if (ret) {
2400 btrfs_abort_transaction(trans, root, ret);
2401 goto out_free_path;
2402 }
2403
2404 ret = 1;
2405out_free_path:
2406 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002407 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002408 btrfs_end_transaction(trans, root);
2409out_unlock:
2410 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2411 &cached, GFP_NOFS);
2412 iput(inode);
2413 return ret;
2414}
2415
2416static void relink_file_extents(struct new_sa_defrag_extent *new)
2417{
2418 struct btrfs_path *path;
2419 struct old_sa_defrag_extent *old, *tmp;
2420 struct sa_defrag_extent_backref *backref;
2421 struct sa_defrag_extent_backref *prev = NULL;
2422 struct inode *inode;
2423 struct btrfs_root *root;
2424 struct rb_node *node;
2425 int ret;
2426
2427 inode = new->inode;
2428 root = BTRFS_I(inode)->root;
2429
2430 path = btrfs_alloc_path();
2431 if (!path)
2432 return;
2433
2434 if (!record_extent_backrefs(path, new)) {
2435 btrfs_free_path(path);
2436 goto out;
2437 }
2438 btrfs_release_path(path);
2439
2440 while (1) {
2441 node = rb_first(&new->root);
2442 if (!node)
2443 break;
2444 rb_erase(node, &new->root);
2445
2446 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2447
2448 ret = relink_extent_backref(path, prev, backref);
2449 WARN_ON(ret < 0);
2450
2451 kfree(prev);
2452
2453 if (ret == 1)
2454 prev = backref;
2455 else
2456 prev = NULL;
2457 cond_resched();
2458 }
2459 kfree(prev);
2460
2461 btrfs_free_path(path);
2462
2463 list_for_each_entry_safe(old, tmp, &new->head, list) {
2464 list_del(&old->list);
2465 kfree(old);
2466 }
2467out:
2468 atomic_dec(&root->fs_info->defrag_running);
2469 wake_up(&root->fs_info->transaction_wait);
2470
2471 kfree(new);
2472}
2473
2474static struct new_sa_defrag_extent *
2475record_old_file_extents(struct inode *inode,
2476 struct btrfs_ordered_extent *ordered)
2477{
2478 struct btrfs_root *root = BTRFS_I(inode)->root;
2479 struct btrfs_path *path;
2480 struct btrfs_key key;
2481 struct old_sa_defrag_extent *old, *tmp;
2482 struct new_sa_defrag_extent *new;
2483 int ret;
2484
2485 new = kmalloc(sizeof(*new), GFP_NOFS);
2486 if (!new)
2487 return NULL;
2488
2489 new->inode = inode;
2490 new->file_pos = ordered->file_offset;
2491 new->len = ordered->len;
2492 new->bytenr = ordered->start;
2493 new->disk_len = ordered->disk_len;
2494 new->compress_type = ordered->compress_type;
2495 new->root = RB_ROOT;
2496 INIT_LIST_HEAD(&new->head);
2497
2498 path = btrfs_alloc_path();
2499 if (!path)
2500 goto out_kfree;
2501
2502 key.objectid = btrfs_ino(inode);
2503 key.type = BTRFS_EXTENT_DATA_KEY;
2504 key.offset = new->file_pos;
2505
2506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2507 if (ret < 0)
2508 goto out_free_path;
2509 if (ret > 0 && path->slots[0] > 0)
2510 path->slots[0]--;
2511
2512 /* find out all the old extents for the file range */
2513 while (1) {
2514 struct btrfs_file_extent_item *extent;
2515 struct extent_buffer *l;
2516 int slot;
2517 u64 num_bytes;
2518 u64 offset;
2519 u64 end;
2520 u64 disk_bytenr;
2521 u64 extent_offset;
2522
2523 l = path->nodes[0];
2524 slot = path->slots[0];
2525
2526 if (slot >= btrfs_header_nritems(l)) {
2527 ret = btrfs_next_leaf(root, path);
2528 if (ret < 0)
2529 goto out_free_list;
2530 else if (ret > 0)
2531 break;
2532 continue;
2533 }
2534
2535 btrfs_item_key_to_cpu(l, &key, slot);
2536
2537 if (key.objectid != btrfs_ino(inode))
2538 break;
2539 if (key.type != BTRFS_EXTENT_DATA_KEY)
2540 break;
2541 if (key.offset >= new->file_pos + new->len)
2542 break;
2543
2544 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2545
2546 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2547 if (key.offset + num_bytes < new->file_pos)
2548 goto next;
2549
2550 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2551 if (!disk_bytenr)
2552 goto next;
2553
2554 extent_offset = btrfs_file_extent_offset(l, extent);
2555
2556 old = kmalloc(sizeof(*old), GFP_NOFS);
2557 if (!old)
2558 goto out_free_list;
2559
2560 offset = max(new->file_pos, key.offset);
2561 end = min(new->file_pos + new->len, key.offset + num_bytes);
2562
2563 old->bytenr = disk_bytenr;
2564 old->extent_offset = extent_offset;
2565 old->offset = offset - key.offset;
2566 old->len = end - offset;
2567 old->new = new;
2568 old->count = 0;
2569 list_add_tail(&old->list, &new->head);
2570next:
2571 path->slots[0]++;
2572 cond_resched();
2573 }
2574
2575 btrfs_free_path(path);
2576 atomic_inc(&root->fs_info->defrag_running);
2577
2578 return new;
2579
2580out_free_list:
2581 list_for_each_entry_safe(old, tmp, &new->head, list) {
2582 list_del(&old->list);
2583 kfree(old);
2584 }
2585out_free_path:
2586 btrfs_free_path(path);
2587out_kfree:
2588 kfree(new);
2589 return NULL;
2590}
2591
Chris Mason5d13a982009-03-13 11:41:46 -04002592/*
2593 * helper function for btrfs_finish_ordered_io, this
2594 * just reads in some of the csum leaves to prime them into ram
2595 * before we start the transaction. It limits the amount of btree
2596 * reads required while inside the transaction.
2597 */
Chris Masond352ac62008-09-29 15:18:18 -04002598/* as ordered data IO finishes, this gets called so we can finish
2599 * an ordered extent if the range of bytes in the file it covers are
2600 * fully written.
2601 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002602static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603{
Josef Bacik5fd02042012-05-02 14:00:54 -04002604 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002606 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002608 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002609 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002610 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002612 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002613
Liu Bo83eea1f2012-07-10 05:28:39 -06002614 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002615
Josef Bacik5fd02042012-05-02 14:00:54 -04002616 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2617 ret = -EIO;
2618 goto out;
2619 }
2620
Yan, Zhengc21677542009-11-12 09:34:21 +00002621 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002622 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002623 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2624 if (nolock)
2625 trans = btrfs_join_transaction_nolock(root);
2626 else
2627 trans = btrfs_join_transaction(root);
2628 if (IS_ERR(trans)) {
2629 ret = PTR_ERR(trans);
2630 trans = NULL;
2631 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00002632 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002633 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2634 ret = btrfs_update_inode_fallback(trans, root, inode);
2635 if (ret) /* -ENOMEM or corruption */
2636 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00002637 goto out;
2638 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002639
Josef Bacik2ac55d42010-02-03 19:33:23 +00002640 lock_extent_bits(io_tree, ordered_extent->file_offset,
2641 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002642 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643
Liu Bo38c227d2013-01-29 03:18:40 +00002644 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2645 ordered_extent->file_offset + ordered_extent->len - 1,
2646 EXTENT_DEFRAG, 1, cached_state);
2647 if (ret) {
2648 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2649 if (last_snapshot >= BTRFS_I(inode)->generation)
2650 /* the inode is shared */
2651 new = record_old_file_extents(inode, ordered_extent);
2652
2653 clear_extent_bit(io_tree, ordered_extent->file_offset,
2654 ordered_extent->file_offset + ordered_extent->len - 1,
2655 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2656 }
2657
Josef Bacik0cb59c92010-07-02 12:14:14 -04002658 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002659 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002660 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002661 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002662 if (IS_ERR(trans)) {
2663 ret = PTR_ERR(trans);
2664 trans = NULL;
2665 goto out_unlock;
2666 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002667 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00002668
Chris Masonc8b97812008-10-29 14:49:59 -04002669 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002670 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002671 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002672 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002673 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002674 ordered_extent->file_offset,
2675 ordered_extent->file_offset +
2676 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002677 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002678 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002679 ret = insert_reserved_file_extent(trans, inode,
2680 ordered_extent->file_offset,
2681 ordered_extent->start,
2682 ordered_extent->disk_len,
2683 ordered_extent->len,
2684 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002685 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002686 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002687 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002688 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2689 ordered_extent->file_offset, ordered_extent->len,
2690 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002691 if (ret < 0) {
2692 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002693 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002694 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002695
Chris Masone6dcd2d2008-07-17 12:53:50 -04002696 add_pending_csums(trans, inode, ordered_extent->file_offset,
2697 &ordered_extent->list);
2698
Josef Bacik6c760c02012-11-09 10:53:21 -05002699 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2700 ret = btrfs_update_inode_fallback(trans, root, inode);
2701 if (ret) { /* -ENOMEM or corruption */
2702 btrfs_abort_transaction(trans, root, ret);
2703 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002704 }
2705 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002706out_unlock:
2707 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2708 ordered_extent->file_offset +
2709 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002710out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002711 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002712 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002713 if (trans)
2714 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002715
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002716 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002717 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2718 ordered_extent->file_offset +
2719 ordered_extent->len - 1, NULL, GFP_NOFS);
2720
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002721 /*
2722 * If the ordered extent had an IOERR or something else went
2723 * wrong we need to return the space for this ordered extent
2724 * back to the allocator.
2725 */
2726 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2727 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2728 btrfs_free_reserved_extent(root, ordered_extent->start,
2729 ordered_extent->disk_len);
2730 }
2731
2732
Josef Bacik5fd02042012-05-02 14:00:54 -04002733 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002734 * This needs to be done to make sure anybody waiting knows we are done
2735 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002736 */
2737 btrfs_remove_ordered_extent(inode, ordered_extent);
2738
Liu Bo38c227d2013-01-29 03:18:40 +00002739 /* for snapshot-aware defrag */
2740 if (new)
2741 relink_file_extents(new);
2742
Chris Masone6dcd2d2008-07-17 12:53:50 -04002743 /* once for us */
2744 btrfs_put_ordered_extent(ordered_extent);
2745 /* once for the tree */
2746 btrfs_put_ordered_extent(ordered_extent);
2747
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 return ret;
2749}
2750
2751static void finish_ordered_fn(struct btrfs_work *work)
2752{
2753 struct btrfs_ordered_extent *ordered_extent;
2754 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2755 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002756}
2757
Christoph Hellwigb2950862008-12-02 09:54:17 -05002758static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002759 struct extent_state *state, int uptodate)
2760{
Josef Bacik5fd02042012-05-02 14:00:54 -04002761 struct inode *inode = page->mapping->host;
2762 struct btrfs_root *root = BTRFS_I(inode)->root;
2763 struct btrfs_ordered_extent *ordered_extent = NULL;
2764 struct btrfs_workers *workers;
2765
liubo1abe9b82011-03-24 11:18:59 +00002766 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2767
Chris Mason8b62b722009-09-02 16:53:46 -04002768 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002769 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2770 end - start + 1, uptodate))
2771 return 0;
2772
2773 ordered_extent->work.func = finish_ordered_fn;
2774 ordered_extent->work.flags = 0;
2775
Liu Bo83eea1f2012-07-10 05:28:39 -06002776 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 workers = &root->fs_info->endio_freespace_worker;
2778 else
2779 workers = &root->fs_info->endio_write_workers;
2780 btrfs_queue_worker(workers, &ordered_extent->work);
2781
2782 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002783}
2784
Chris Masond352ac62008-09-29 15:18:18 -04002785/*
Chris Masond352ac62008-09-29 15:18:18 -04002786 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002787 * if there's a match, we allow the bio to finish. If not, the code in
2788 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002789 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002790static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002791 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002792{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002793 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002794 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002795 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002796 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002797 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002798 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002799 struct btrfs_root *root = BTRFS_I(inode)->root;
2800 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002801 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2802 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002803
Chris Masond20f7042008-12-08 16:58:54 -05002804 if (PageChecked(page)) {
2805 ClearPageChecked(page);
2806 goto good;
2807 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002808
2809 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002810 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002811
Yan Zheng17d217f2008-12-12 10:03:38 -05002812 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002813 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002814 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2815 GFP_NOFS);
2816 return 0;
2817 }
2818
Yanc2e639f2008-02-04 08:57:25 -05002819 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002820 private = state->private;
2821 ret = 0;
2822 } else {
2823 ret = get_state_private(io_tree, start, &private);
2824 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002825 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002826 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002827 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002828
Liu Bob0496682013-03-14 14:57:45 +00002829 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002830 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002831 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002832 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002833
Cong Wang7ac687d2011-11-25 23:14:28 +08002834 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002835good:
Chris Mason07157aa2007-08-30 08:50:51 -04002836 return 0;
2837
2838zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002839 if (__ratelimit(&_rs))
2840 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2841 (unsigned long long)btrfs_ino(page->mapping->host),
2842 (unsigned long long)start, csum,
2843 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002844 memset(kaddr + offset, 1, end - start + 1);
2845 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002846 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002847 if (private == 0)
2848 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002849 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002850}
Chris Masonb888db22007-08-27 16:49:44 -04002851
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002852struct delayed_iput {
2853 struct list_head list;
2854 struct inode *inode;
2855};
2856
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002857/* JDM: If this is fs-wide, why can't we add a pointer to
2858 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002859void btrfs_add_delayed_iput(struct inode *inode)
2860{
2861 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2862 struct delayed_iput *delayed;
2863
2864 if (atomic_add_unless(&inode->i_count, -1, 1))
2865 return;
2866
2867 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2868 delayed->inode = inode;
2869
2870 spin_lock(&fs_info->delayed_iput_lock);
2871 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2872 spin_unlock(&fs_info->delayed_iput_lock);
2873}
2874
2875void btrfs_run_delayed_iputs(struct btrfs_root *root)
2876{
2877 LIST_HEAD(list);
2878 struct btrfs_fs_info *fs_info = root->fs_info;
2879 struct delayed_iput *delayed;
2880 int empty;
2881
2882 spin_lock(&fs_info->delayed_iput_lock);
2883 empty = list_empty(&fs_info->delayed_iputs);
2884 spin_unlock(&fs_info->delayed_iput_lock);
2885 if (empty)
2886 return;
2887
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888 spin_lock(&fs_info->delayed_iput_lock);
2889 list_splice_init(&fs_info->delayed_iputs, &list);
2890 spin_unlock(&fs_info->delayed_iput_lock);
2891
2892 while (!list_empty(&list)) {
2893 delayed = list_entry(list.next, struct delayed_iput, list);
2894 list_del(&delayed->list);
2895 iput(delayed->inode);
2896 kfree(delayed);
2897 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002898}
2899
Yan, Zhengd68fc572010-05-16 10:49:58 -04002900/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002901 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002902 * files in the subvolume, it removes orphan item and frees block_rsv
2903 * structure.
2904 */
2905void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2906 struct btrfs_root *root)
2907{
Josef Bacik90290e12011-12-02 15:44:12 -05002908 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002909 int ret;
2910
Josef Bacik8a35d952012-05-23 14:26:42 -04002911 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002912 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2913 return;
2914
Josef Bacik90290e12011-12-02 15:44:12 -05002915 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002916 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002917 spin_unlock(&root->orphan_lock);
2918 return;
2919 }
2920
2921 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2922 spin_unlock(&root->orphan_lock);
2923 return;
2924 }
2925
2926 block_rsv = root->orphan_block_rsv;
2927 root->orphan_block_rsv = NULL;
2928 spin_unlock(&root->orphan_lock);
2929
Yan, Zhengd68fc572010-05-16 10:49:58 -04002930 if (root->orphan_item_inserted &&
2931 btrfs_root_refs(&root->root_item) > 0) {
2932 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2933 root->root_key.objectid);
2934 BUG_ON(ret);
2935 root->orphan_item_inserted = 0;
2936 }
2937
Josef Bacik90290e12011-12-02 15:44:12 -05002938 if (block_rsv) {
2939 WARN_ON(block_rsv->size > 0);
2940 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 }
2942}
2943
2944/*
Josef Bacik7b128762008-07-24 12:17:14 -04002945 * This creates an orphan entry for the given inode in case something goes
2946 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002947 *
2948 * NOTE: caller of this function should reserve 5 units of metadata for
2949 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002950 */
2951int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2952{
2953 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 struct btrfs_block_rsv *block_rsv = NULL;
2955 int reserve = 0;
2956 int insert = 0;
2957 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002958
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002960 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002961 if (!block_rsv)
2962 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002963 }
2964
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 spin_lock(&root->orphan_lock);
2966 if (!root->orphan_block_rsv) {
2967 root->orphan_block_rsv = block_rsv;
2968 } else if (block_rsv) {
2969 btrfs_free_block_rsv(root, block_rsv);
2970 block_rsv = NULL;
2971 }
Josef Bacik7b128762008-07-24 12:17:14 -04002972
Josef Bacik8a35d952012-05-23 14:26:42 -04002973 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2974 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002975#if 0
2976 /*
2977 * For proper ENOSPC handling, we should do orphan
2978 * cleanup when mounting. But this introduces backward
2979 * compatibility issue.
2980 */
2981 if (!xchg(&root->orphan_item_inserted, 1))
2982 insert = 2;
2983 else
2984 insert = 1;
2985#endif
2986 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002987 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 }
Josef Bacik7b128762008-07-24 12:17:14 -04002989
Josef Bacik72ac3c02012-05-23 14:13:11 -04002990 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2991 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002992 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002994
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 /* grab metadata reservation from transaction handle */
2996 if (reserve) {
2997 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 }
3000
3001 /* insert an orphan item to track this unlinked/truncated file */
3002 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003003 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003004 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003005 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3006 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003007 btrfs_abort_transaction(trans, root, ret);
3008 return ret;
3009 }
3010 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003011 }
3012
3013 /* insert an orphan item to track subvolume contains orphan files */
3014 if (insert >= 2) {
3015 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3016 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003017 if (ret && ret != -EEXIST) {
3018 btrfs_abort_transaction(trans, root, ret);
3019 return ret;
3020 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 }
3022 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003023}
3024
3025/*
3026 * We have done the truncate/delete so we can go ahead and remove the orphan
3027 * item for this particular inode.
3028 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003029static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3030 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003031{
3032 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 int delete_item = 0;
3034 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003035 int ret = 0;
3036
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003038 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3039 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003041
Josef Bacik72ac3c02012-05-23 14:13:11 -04003042 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3043 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003044 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003045 spin_unlock(&root->orphan_lock);
3046
3047 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003048 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003050 }
3051
Josef Bacik8a35d952012-05-23 14:26:42 -04003052 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003054 atomic_dec(&root->orphan_inodes);
3055 }
Josef Bacik7b128762008-07-24 12:17:14 -04003056
Yan, Zhengd68fc572010-05-16 10:49:58 -04003057 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003058}
3059
3060/*
3061 * this cleans up any orphans that may be left on the list from the last use
3062 * of this root.
3063 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003064int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003065{
3066 struct btrfs_path *path;
3067 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003068 struct btrfs_key key, found_key;
3069 struct btrfs_trans_handle *trans;
3070 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003071 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003072 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3073
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003075 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003076
3077 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003078 if (!path) {
3079 ret = -ENOMEM;
3080 goto out;
3081 }
Josef Bacik7b128762008-07-24 12:17:14 -04003082 path->reada = -1;
3083
3084 key.objectid = BTRFS_ORPHAN_OBJECTID;
3085 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3086 key.offset = (u64)-1;
3087
Josef Bacik7b128762008-07-24 12:17:14 -04003088 while (1) {
3089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003090 if (ret < 0)
3091 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003092
3093 /*
3094 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003095 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003096 * find the key and see if we have stuff that matches
3097 */
3098 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003099 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003100 if (path->slots[0] == 0)
3101 break;
3102 path->slots[0]--;
3103 }
3104
3105 /* pull out the item */
3106 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3108
3109 /* make sure the item matches what we want */
3110 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3111 break;
3112 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3113 break;
3114
3115 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003116 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003117
3118 /*
3119 * this is where we are basically btrfs_lookup, without the
3120 * crossing root thing. we store the inode number in the
3121 * offset of the orphan item.
3122 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003123
3124 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003125 btrfs_err(root->fs_info,
3126 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003127 ret = -EINVAL;
3128 goto out;
3129 }
3130
3131 last_objectid = found_key.offset;
3132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 found_key.objectid = found_key.offset;
3134 found_key.type = BTRFS_INODE_ITEM_KEY;
3135 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003136 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003137 ret = PTR_RET(inode);
3138 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003140
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003141 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3142 struct btrfs_root *dead_root;
3143 struct btrfs_fs_info *fs_info = root->fs_info;
3144 int is_dead_root = 0;
3145
3146 /*
3147 * this is an orphan in the tree root. Currently these
3148 * could come from 2 sources:
3149 * a) a snapshot deletion in progress
3150 * b) a free space cache inode
3151 * We need to distinguish those two, as the snapshot
3152 * orphan must not get deleted.
3153 * find_dead_roots already ran before us, so if this
3154 * is a snapshot deletion, we should find the root
3155 * in the dead_roots list
3156 */
3157 spin_lock(&fs_info->trans_lock);
3158 list_for_each_entry(dead_root, &fs_info->dead_roots,
3159 root_list) {
3160 if (dead_root->root_key.objectid ==
3161 found_key.objectid) {
3162 is_dead_root = 1;
3163 break;
3164 }
3165 }
3166 spin_unlock(&fs_info->trans_lock);
3167 if (is_dead_root) {
3168 /* prevent this orphan from being found again */
3169 key.offset = found_key.objectid - 1;
3170 continue;
3171 }
3172 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003173 /*
3174 * Inode is already gone but the orphan item is still there,
3175 * kill the orphan item.
3176 */
3177 if (ret == -ESTALE) {
3178 trans = btrfs_start_transaction(root, 1);
3179 if (IS_ERR(trans)) {
3180 ret = PTR_ERR(trans);
3181 goto out;
3182 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003183 btrfs_debug(root->fs_info, "auto deleting %Lu",
3184 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003185 ret = btrfs_del_orphan_item(trans, root,
3186 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003187 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003188 btrfs_end_transaction(trans, root);
3189 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003190 }
Josef Bacik7b128762008-07-24 12:17:14 -04003191
Josef Bacik7b128762008-07-24 12:17:14 -04003192 /*
3193 * add this inode to the orphan list so btrfs_orphan_del does
3194 * the proper thing when we hit it
3195 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3197 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003198 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003199
Josef Bacik7b128762008-07-24 12:17:14 -04003200 /* if we have links, this was a truncate, lets do that */
3201 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003202 if (!S_ISREG(inode->i_mode)) {
3203 WARN_ON(1);
3204 iput(inode);
3205 continue;
3206 }
Josef Bacik7b128762008-07-24 12:17:14 -04003207 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003208
3209 /* 1 for the orphan item deletion. */
3210 trans = btrfs_start_transaction(root, 1);
3211 if (IS_ERR(trans)) {
3212 ret = PTR_ERR(trans);
3213 goto out;
3214 }
3215 ret = btrfs_orphan_add(trans, inode);
3216 btrfs_end_transaction(trans, root);
3217 if (ret)
3218 goto out;
3219
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003220 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003221 if (ret)
3222 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003223 } else {
3224 nr_unlink++;
3225 }
3226
3227 /* this will do delete_inode and everything for us */
3228 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003229 if (ret)
3230 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003231 }
Miao Xie3254c872011-11-10 20:45:05 -05003232 /* release the path since we're done with it */
3233 btrfs_release_path(path);
3234
Yan, Zhengd68fc572010-05-16 10:49:58 -04003235 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3236
3237 if (root->orphan_block_rsv)
3238 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3239 (u64)-1);
3240
3241 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003242 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003243 if (!IS_ERR(trans))
3244 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 }
Josef Bacik7b128762008-07-24 12:17:14 -04003246
3247 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003248 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003249 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003250 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003251
3252out:
3253 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003254 btrfs_crit(root->fs_info,
3255 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003256 btrfs_free_path(path);
3257 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003258}
3259
Chris Masond352ac62008-09-29 15:18:18 -04003260/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003261 * very simple check to peek ahead in the leaf looking for xattrs. If we
3262 * don't find any xattrs, we know there can't be any acls.
3263 *
3264 * slot is the slot the inode is in, objectid is the objectid of the inode
3265 */
3266static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3267 int slot, u64 objectid)
3268{
3269 u32 nritems = btrfs_header_nritems(leaf);
3270 struct btrfs_key found_key;
3271 int scanned = 0;
3272
3273 slot++;
3274 while (slot < nritems) {
3275 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3276
3277 /* we found a different objectid, there must not be acls */
3278 if (found_key.objectid != objectid)
3279 return 0;
3280
3281 /* we found an xattr, assume we've got an acl */
3282 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3283 return 1;
3284
3285 /*
3286 * we found a key greater than an xattr key, there can't
3287 * be any acls later on
3288 */
3289 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3290 return 0;
3291
3292 slot++;
3293 scanned++;
3294
3295 /*
3296 * it goes inode, inode backrefs, xattrs, extents,
3297 * so if there are a ton of hard links to an inode there can
3298 * be a lot of backrefs. Don't waste time searching too hard,
3299 * this is just an optimization
3300 */
3301 if (scanned >= 8)
3302 break;
3303 }
3304 /* we hit the end of the leaf before we found an xattr or
3305 * something larger than an xattr. We have to assume the inode
3306 * has acls
3307 */
3308 return 1;
3309}
3310
3311/*
Chris Masond352ac62008-09-29 15:18:18 -04003312 * read an inode from the btree into the in-memory inode
3313 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003314static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003315{
3316 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003317 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003318 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003319 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003320 struct btrfs_root *root = BTRFS_I(inode)->root;
3321 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003322 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003323 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003324 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003325 bool filled = false;
3326
3327 ret = btrfs_fill_inode(inode, &rdev);
3328 if (!ret)
3329 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003330
3331 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003332 if (!path)
3333 goto make_bad;
3334
Josef Bacikd90c7322011-05-17 09:50:54 -04003335 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003336 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003337
Chris Mason39279cc2007-06-12 06:35:45 -04003338 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003339 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003340 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003341
Chris Mason5f39d392007-10-15 16:14:19 -04003342 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003343
3344 if (filled)
3345 goto cache_acl;
3346
Chris Mason5f39d392007-10-15 16:14:19 -04003347 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3348 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003349 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003350 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003351 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3352 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003353 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003354
3355 tspec = btrfs_inode_atime(inode_item);
3356 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3357 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3358
3359 tspec = btrfs_inode_mtime(inode_item);
3360 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3361 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3362
3363 tspec = btrfs_inode_ctime(inode_item);
3364 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3365 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3366
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003367 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003368 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003369 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3370
3371 /*
3372 * If we were modified in the current generation and evicted from memory
3373 * and then re-read we need to do a full sync since we don't have any
3374 * idea about which extents were modified before we were evicted from
3375 * cache.
3376 */
3377 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3378 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3379 &BTRFS_I(inode)->runtime_flags);
3380
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003381 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003382 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003383 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003384 rdev = btrfs_inode_rdev(leaf, inode_item);
3385
Josef Bacikaec74772008-07-24 12:12:38 -04003386 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003387 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003388cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003389 /*
3390 * try to precache a NULL acl entry for files that don't have
3391 * any xattrs or acls
3392 */
Li Zefan33345d012011-04-20 10:31:50 +08003393 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3394 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003395 if (!maybe_acls)
3396 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003397
Chris Mason39279cc2007-06-12 06:35:45 -04003398 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003399
Chris Mason39279cc2007-06-12 06:35:45 -04003400 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003401 case S_IFREG:
3402 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003403 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003404 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003405 inode->i_fop = &btrfs_file_operations;
3406 inode->i_op = &btrfs_file_inode_operations;
3407 break;
3408 case S_IFDIR:
3409 inode->i_fop = &btrfs_dir_file_operations;
3410 if (root == root->fs_info->tree_root)
3411 inode->i_op = &btrfs_dir_ro_inode_operations;
3412 else
3413 inode->i_op = &btrfs_dir_inode_operations;
3414 break;
3415 case S_IFLNK:
3416 inode->i_op = &btrfs_symlink_inode_operations;
3417 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003418 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003419 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003420 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003421 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003422 init_special_inode(inode, inode->i_mode, rdev);
3423 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003424 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003425
3426 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003427 return;
3428
3429make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003430 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003431 make_bad_inode(inode);
3432}
3433
Chris Masond352ac62008-09-29 15:18:18 -04003434/*
3435 * given a leaf and an inode, copy the inode fields into the leaf
3436 */
Chris Masone02119d2008-09-05 16:13:11 -04003437static void fill_inode_item(struct btrfs_trans_handle *trans,
3438 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003439 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003440 struct inode *inode)
3441{
Liu Bo51fab692012-12-27 09:01:21 +00003442 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003443
Liu Bo51fab692012-12-27 09:01:21 +00003444 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003445
Liu Bo51fab692012-12-27 09:01:21 +00003446 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3447 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3448 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3449 &token);
3450 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3451 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003452
Liu Bo51fab692012-12-27 09:01:21 +00003453 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3454 inode->i_atime.tv_sec, &token);
3455 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3456 inode->i_atime.tv_nsec, &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_mtime(item),
3459 inode->i_mtime.tv_sec, &token);
3460 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3461 inode->i_mtime.tv_nsec, &token);
3462
3463 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3464 inode->i_ctime.tv_sec, &token);
3465 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3466 inode->i_ctime.tv_nsec, &token);
3467
3468 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3469 &token);
3470 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3471 &token);
3472 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3473 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3474 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3475 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3476 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003477}
3478
Chris Masond352ac62008-09-29 15:18:18 -04003479/*
3480 * copy everything in the in-memory inode into the btree.
3481 */
Chris Mason21151332011-11-10 20:39:08 -05003482static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003483 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003484{
3485 struct btrfs_inode_item *inode_item;
3486 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003487 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003488 int ret;
3489
3490 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003491 if (!path)
3492 return -ENOMEM;
3493
Chris Masonb9473432009-03-13 11:00:37 -04003494 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003495 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3496 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 if (ret) {
3498 if (ret > 0)
3499 ret = -ENOENT;
3500 goto failed;
3501 }
3502
Chris Masonb4ce94d2009-02-04 09:25:08 -05003503 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003504 leaf = path->nodes[0];
3505 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003506 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003507
Chris Masone02119d2008-09-05 16:13:11 -04003508 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003510 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003511 ret = 0;
3512failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003513 btrfs_free_path(path);
3514 return ret;
3515}
3516
Chris Masond352ac62008-09-29 15:18:18 -04003517/*
Chris Mason21151332011-11-10 20:39:08 -05003518 * copy everything in the in-memory inode into the btree.
3519 */
3520noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3521 struct btrfs_root *root, struct inode *inode)
3522{
3523 int ret;
3524
3525 /*
3526 * If the inode is a free space inode, we can deadlock during commit
3527 * if we put it into the delayed code.
3528 *
3529 * The data relocation inode should also be directly updated
3530 * without delay
3531 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003532 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003533 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003534 btrfs_update_root_times(trans, root);
3535
Chris Mason21151332011-11-10 20:39:08 -05003536 ret = btrfs_delayed_update_inode(trans, root, inode);
3537 if (!ret)
3538 btrfs_set_inode_last_trans(trans, inode);
3539 return ret;
3540 }
3541
3542 return btrfs_update_inode_item(trans, root, inode);
3543}
3544
Josef Bacikbe6aef62012-10-22 15:43:12 -04003545noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3546 struct btrfs_root *root,
3547 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003548{
3549 int ret;
3550
3551 ret = btrfs_update_inode(trans, root, inode);
3552 if (ret == -ENOSPC)
3553 return btrfs_update_inode_item(trans, root, inode);
3554 return ret;
3555}
3556
3557/*
Chris Masond352ac62008-09-29 15:18:18 -04003558 * unlink helper that gets used here in inode.c and in the tree logging
3559 * recovery code. It remove a link in a directory with a given name, and
3560 * also drops the back refs in the inode to the directory
3561 */
Al Viro92986792011-03-04 17:14:37 +00003562static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3563 struct btrfs_root *root,
3564 struct inode *dir, struct inode *inode,
3565 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003566{
3567 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003568 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003569 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003570 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003571 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003572 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003573 u64 ino = btrfs_ino(inode);
3574 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003575
3576 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003577 if (!path) {
3578 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003579 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003580 }
3581
Chris Masonb9473432009-03-13 11:00:37 -04003582 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003583 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003584 name, name_len, -1);
3585 if (IS_ERR(di)) {
3586 ret = PTR_ERR(di);
3587 goto err;
3588 }
3589 if (!di) {
3590 ret = -ENOENT;
3591 goto err;
3592 }
Chris Mason5f39d392007-10-15 16:14:19 -04003593 leaf = path->nodes[0];
3594 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003595 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003596 if (ret)
3597 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003598 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003599
Li Zefan33345d012011-04-20 10:31:50 +08003600 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3601 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003602 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003603 btrfs_info(root->fs_info,
3604 "failed to delete reference to %.*s, inode %llu parent %llu",
3605 name_len, name,
3606 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003607 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003608 goto err;
3609 }
3610
Miao Xie16cdcec2011-04-22 18:12:22 +08003611 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 if (ret) {
3613 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003614 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003615 }
Chris Mason39279cc2007-06-12 06:35:45 -04003616
Chris Masone02119d2008-09-05 16:13:11 -04003617 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003618 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003619 if (ret != 0 && ret != -ENOENT) {
3620 btrfs_abort_transaction(trans, root, ret);
3621 goto err;
3622 }
Chris Masone02119d2008-09-05 16:13:11 -04003623
3624 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3625 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003626 if (ret == -ENOENT)
3627 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003628 else if (ret)
3629 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003630err:
3631 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003632 if (ret)
3633 goto out;
3634
3635 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003636 inode_inc_iversion(inode);
3637 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003638 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003639 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003640out:
Chris Mason39279cc2007-06-12 06:35:45 -04003641 return ret;
3642}
3643
Al Viro92986792011-03-04 17:14:37 +00003644int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3645 struct btrfs_root *root,
3646 struct inode *dir, struct inode *inode,
3647 const char *name, int name_len)
3648{
3649 int ret;
3650 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3651 if (!ret) {
3652 btrfs_drop_nlink(inode);
3653 ret = btrfs_update_inode(trans, root, inode);
3654 }
3655 return ret;
3656}
3657
3658
Yan, Zhenga22285a2010-05-16 10:48:46 -04003659/* helper to check if there is any shared block in the path */
3660static int check_path_shared(struct btrfs_root *root,
3661 struct btrfs_path *path)
3662{
3663 struct extent_buffer *eb;
3664 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00003665 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666
3667 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003668 int ret;
3669
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670 if (!path->nodes[level])
3671 break;
3672 eb = path->nodes[level];
3673 if (!btrfs_block_can_be_shared(root, eb))
3674 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003675 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676 &refs, NULL);
3677 if (refs > 1)
3678 return 1;
3679 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003680 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681}
3682
3683/*
3684 * helper to start transaction for unlink and rmdir.
3685 *
3686 * unlink and rmdir are special in btrfs, they do not always free space.
3687 * so in enospc case, we should make sure they will free space before
3688 * allowing them to use the global metadata reservation.
3689 */
3690static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3691 struct dentry *dentry)
3692{
3693 struct btrfs_trans_handle *trans;
3694 struct btrfs_root *root = BTRFS_I(dir)->root;
3695 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696 struct btrfs_dir_item *di;
3697 struct inode *inode = dentry->d_inode;
3698 u64 index;
3699 int check_link = 1;
3700 int err = -ENOSPC;
3701 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003702 u64 ino = btrfs_ino(inode);
3703 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003704
Josef Bacike70bea52011-10-11 14:18:24 -04003705 /*
3706 * 1 for the possible orphan item
3707 * 1 for the dir item
3708 * 1 for the dir index
3709 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003710 * 1 for the inode
3711 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003712 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3714 return trans;
3715
Li Zefan33345d012011-04-20 10:31:50 +08003716 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717 return ERR_PTR(-ENOSPC);
3718
3719 /* check if there is someone else holds reference */
3720 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3721 return ERR_PTR(-ENOSPC);
3722
3723 if (atomic_read(&inode->i_count) > 2)
3724 return ERR_PTR(-ENOSPC);
3725
3726 if (xchg(&root->fs_info->enospc_unlink, 1))
3727 return ERR_PTR(-ENOSPC);
3728
3729 path = btrfs_alloc_path();
3730 if (!path) {
3731 root->fs_info->enospc_unlink = 0;
3732 return ERR_PTR(-ENOMEM);
3733 }
3734
Josef Bacik3880a1b2011-10-14 14:46:51 -04003735 /* 1 for the orphan item */
3736 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003737 if (IS_ERR(trans)) {
3738 btrfs_free_path(path);
3739 root->fs_info->enospc_unlink = 0;
3740 return trans;
3741 }
3742
3743 path->skip_locking = 1;
3744 path->search_commit_root = 1;
3745
3746 ret = btrfs_lookup_inode(trans, root, path,
3747 &BTRFS_I(dir)->location, 0);
3748 if (ret < 0) {
3749 err = ret;
3750 goto out;
3751 }
3752 if (ret == 0) {
3753 if (check_path_shared(root, path))
3754 goto out;
3755 } else {
3756 check_link = 0;
3757 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003758 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003759
3760 ret = btrfs_lookup_inode(trans, root, path,
3761 &BTRFS_I(inode)->location, 0);
3762 if (ret < 0) {
3763 err = ret;
3764 goto out;
3765 }
3766 if (ret == 0) {
3767 if (check_path_shared(root, path))
3768 goto out;
3769 } else {
3770 check_link = 0;
3771 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003772 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003773
3774 if (ret == 0 && S_ISREG(inode->i_mode)) {
3775 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003776 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003777 if (ret < 0) {
3778 err = ret;
3779 goto out;
3780 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003781 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003782 if (check_path_shared(root, path))
3783 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003784 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003785 }
3786
3787 if (!check_link) {
3788 err = 0;
3789 goto out;
3790 }
3791
Li Zefan33345d012011-04-20 10:31:50 +08003792 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003793 dentry->d_name.name, dentry->d_name.len, 0);
3794 if (IS_ERR(di)) {
3795 err = PTR_ERR(di);
3796 goto out;
3797 }
3798 if (di) {
3799 if (check_path_shared(root, path))
3800 goto out;
3801 } else {
3802 err = 0;
3803 goto out;
3804 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003805 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003806
Mark Fashehf1863732012-08-08 11:32:27 -07003807 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3808 dentry->d_name.len, ino, dir_ino, 0,
3809 &index);
3810 if (ret) {
3811 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003812 goto out;
3813 }
Mark Fashehf1863732012-08-08 11:32:27 -07003814
Yan, Zhenga22285a2010-05-16 10:48:46 -04003815 if (check_path_shared(root, path))
3816 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003817
David Sterbab3b4aa72011-04-21 01:20:15 +02003818 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003819
Miao Xie16cdcec2011-04-22 18:12:22 +08003820 /*
3821 * This is a commit root search, if we can lookup inode item and other
3822 * relative items in the commit root, it means the transaction of
3823 * dir/file creation has been committed, and the dir index item that we
3824 * delay to insert has also been inserted into the commit root. So
3825 * we needn't worry about the delayed insertion of the dir index item
3826 * here.
3827 */
Li Zefan33345d012011-04-20 10:31:50 +08003828 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003829 dentry->d_name.name, dentry->d_name.len, 0);
3830 if (IS_ERR(di)) {
3831 err = PTR_ERR(di);
3832 goto out;
3833 }
3834 BUG_ON(ret == -ENOENT);
3835 if (check_path_shared(root, path))
3836 goto out;
3837
3838 err = 0;
3839out:
3840 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003841 /* Migrate the orphan reservation over */
3842 if (!err)
3843 err = btrfs_block_rsv_migrate(trans->block_rsv,
3844 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003845 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003846
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847 if (err) {
3848 btrfs_end_transaction(trans, root);
3849 root->fs_info->enospc_unlink = 0;
3850 return ERR_PTR(err);
3851 }
3852
3853 trans->block_rsv = &root->fs_info->global_block_rsv;
3854 return trans;
3855}
3856
3857static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3858 struct btrfs_root *root)
3859{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003860 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003861 btrfs_block_rsv_release(root, trans->block_rsv,
3862 trans->bytes_reserved);
3863 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003864 BUG_ON(!root->fs_info->enospc_unlink);
3865 root->fs_info->enospc_unlink = 0;
3866 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003867 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003868}
3869
Chris Mason39279cc2007-06-12 06:35:45 -04003870static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3871{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003872 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003873 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003874 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 int ret;
3876
Yan, Zhenga22285a2010-05-16 10:48:46 -04003877 trans = __unlink_start_trans(dir, dentry);
3878 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003879 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003880
Chris Mason12fcfd22009-03-24 10:24:20 -04003881 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3882
Chris Masone02119d2008-09-05 16:13:11 -04003883 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3884 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003885 if (ret)
3886 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003887
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003889 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003890 if (ret)
3891 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003892 }
Josef Bacik7b128762008-07-24 12:17:14 -04003893
Tsutomu Itohb5324022011-07-19 07:27:20 +00003894out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003895 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003896 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003897 return ret;
3898}
3899
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003900int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3901 struct btrfs_root *root,
3902 struct inode *dir, u64 objectid,
3903 const char *name, int name_len)
3904{
3905 struct btrfs_path *path;
3906 struct extent_buffer *leaf;
3907 struct btrfs_dir_item *di;
3908 struct btrfs_key key;
3909 u64 index;
3910 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003911 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003912
3913 path = btrfs_alloc_path();
3914 if (!path)
3915 return -ENOMEM;
3916
Li Zefan33345d012011-04-20 10:31:50 +08003917 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003918 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003919 if (IS_ERR_OR_NULL(di)) {
3920 if (!di)
3921 ret = -ENOENT;
3922 else
3923 ret = PTR_ERR(di);
3924 goto out;
3925 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003926
3927 leaf = path->nodes[0];
3928 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3929 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3930 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003931 if (ret) {
3932 btrfs_abort_transaction(trans, root, ret);
3933 goto out;
3934 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003935 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003936
3937 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3938 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003939 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003941 if (ret != -ENOENT) {
3942 btrfs_abort_transaction(trans, root, ret);
3943 goto out;
3944 }
Li Zefan33345d012011-04-20 10:31:50 +08003945 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003946 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003947 if (IS_ERR_OR_NULL(di)) {
3948 if (!di)
3949 ret = -ENOENT;
3950 else
3951 ret = PTR_ERR(di);
3952 btrfs_abort_transaction(trans, root, ret);
3953 goto out;
3954 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003955
3956 leaf = path->nodes[0];
3957 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003958 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003959 index = key.offset;
3960 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003961 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003962
Miao Xie16cdcec2011-04-22 18:12:22 +08003963 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 if (ret) {
3965 btrfs_abort_transaction(trans, root, ret);
3966 goto out;
3967 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003968
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003969 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003970 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003972 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003973 if (ret)
3974 btrfs_abort_transaction(trans, root, ret);
3975out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003976 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003978}
3979
Chris Mason39279cc2007-06-12 06:35:45 -04003980static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3981{
3982 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003983 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003984 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003986
David Sterbab3ae2442012-09-13 16:04:34 -06003987 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003988 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003989 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3990 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003991
Yan, Zhenga22285a2010-05-16 10:48:46 -04003992 trans = __unlink_start_trans(dir, dentry);
3993 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003994 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003995
Li Zefan33345d012011-04-20 10:31:50 +08003996 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003997 err = btrfs_unlink_subvol(trans, root, dir,
3998 BTRFS_I(inode)->location.objectid,
3999 dentry->d_name.name,
4000 dentry->d_name.len);
4001 goto out;
4002 }
4003
Josef Bacik7b128762008-07-24 12:17:14 -04004004 err = btrfs_orphan_add(trans, inode);
4005 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004006 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004007
Chris Mason39279cc2007-06-12 06:35:45 -04004008 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004009 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4010 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004011 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004012 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004013out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004015 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004016
Chris Mason39279cc2007-06-12 06:35:45 -04004017 return err;
4018}
4019
Chris Mason323ac952008-10-01 19:05:46 -04004020/*
Chris Mason39279cc2007-06-12 06:35:45 -04004021 * this can truncate away extent items, csum items and directory items.
4022 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004023 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004024 *
4025 * csum items that cross the new i_size are truncated to the new size
4026 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004027 *
4028 * min_type is the minimum key type to truncate down to. If set to 0, this
4029 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004030 */
Yan, Zheng80825102009-11-12 09:35:36 +00004031int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4032 struct btrfs_root *root,
4033 struct inode *inode,
4034 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004035{
Chris Mason39279cc2007-06-12 06:35:45 -04004036 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004037 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004039 struct btrfs_key key;
4040 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004041 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004042 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004044 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004045 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 int found_extent;
4047 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004048 int pending_del_nr = 0;
4049 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004050 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004051 int ret;
4052 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004053 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004054
4055 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004056
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004057 path = btrfs_alloc_path();
4058 if (!path)
4059 return -ENOMEM;
4060 path->reada = -1;
4061
Josef Bacik5dc562c2012-08-17 13:14:17 -04004062 /*
4063 * We want to drop from the next block forward in case this new size is
4064 * not block aligned since we will be keeping the last block of the
4065 * extent just the way it is.
4066 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004067 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004068 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4069 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004070
Miao Xie16cdcec2011-04-22 18:12:22 +08004071 /*
4072 * This function is also used to drop the items in the log tree before
4073 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4074 * it is used to drop the loged items. So we shouldn't kill the delayed
4075 * items.
4076 */
4077 if (min_type == 0 && root == BTRFS_I(inode)->root)
4078 btrfs_kill_delayed_inode_items(inode);
4079
Li Zefan33345d012011-04-20 10:31:50 +08004080 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004081 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004082 key.type = (u8)-1;
4083
Chris Mason85e21ba2008-01-29 15:11:36 -05004084search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004085 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004086 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004087 if (ret < 0) {
4088 err = ret;
4089 goto out;
4090 }
Chris Masond3977122009-01-05 21:25:51 -05004091
Chris Mason85e21ba2008-01-29 15:11:36 -05004092 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004093 /* there are no items in the tree for us to truncate, we're
4094 * done
4095 */
Yan, Zheng80825102009-11-12 09:35:36 +00004096 if (path->slots[0] == 0)
4097 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004098 path->slots[0]--;
4099 }
4100
Chris Masond3977122009-01-05 21:25:51 -05004101 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004102 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004103 leaf = path->nodes[0];
4104 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4105 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004106
Li Zefan33345d012011-04-20 10:31:50 +08004107 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004108 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004109
Chris Mason85e21ba2008-01-29 15:11:36 -05004110 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004111 break;
4112
Chris Mason5f39d392007-10-15 16:14:19 -04004113 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004114 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004115 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004116 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004117 extent_type = btrfs_file_extent_type(leaf, fi);
4118 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004119 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004120 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004121 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004122 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004123 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004124 }
Yan008630c2007-11-07 13:31:09 -05004125 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004126 }
Yan, Zheng80825102009-11-12 09:35:36 +00004127 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004128 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004129 } else {
4130 if (item_end < new_size)
4131 break;
4132 if (found_key.offset >= new_size)
4133 del_item = 1;
4134 else
4135 del_item = 0;
4136 }
Chris Mason39279cc2007-06-12 06:35:45 -04004137 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004138 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004139 if (found_type != BTRFS_EXTENT_DATA_KEY)
4140 goto delete;
4141
4142 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004143 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004144 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004145 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004146 u64 orig_num_bytes =
4147 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004148 extent_num_bytes = ALIGN(new_size -
4149 found_key.offset,
4150 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004151 btrfs_set_file_extent_num_bytes(leaf, fi,
4152 extent_num_bytes);
4153 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004154 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004155 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004156 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004157 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004158 } else {
Chris Masondb945352007-10-15 16:15:53 -04004159 extent_num_bytes =
4160 btrfs_file_extent_disk_num_bytes(leaf,
4161 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162 extent_offset = found_key.offset -
4163 btrfs_file_extent_offset(leaf, fi);
4164
Chris Mason39279cc2007-06-12 06:35:45 -04004165 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004166 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004167 if (extent_start != 0) {
4168 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004169 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004170 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004171 }
4172 }
Chris Mason90692182008-02-08 13:49:28 -05004173 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004174 /*
4175 * we can't truncate inline items that have had
4176 * special encodings
4177 */
4178 if (!del_item &&
4179 btrfs_file_extent_compression(leaf, fi) == 0 &&
4180 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4181 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004182 u32 size = new_size - found_key.offset;
4183
4184 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004185 inode_sub_bytes(inode, item_end + 1 -
4186 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004187 }
4188 size =
4189 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004190 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004191 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004192 inode_sub_bytes(inode, item_end + 1 -
4193 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004194 }
Chris Mason39279cc2007-06-12 06:35:45 -04004195 }
Chris Mason179e29e2007-11-01 11:28:41 -04004196delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004197 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004198 if (!pending_del_nr) {
4199 /* no pending yet, add ourselves */
4200 pending_del_slot = path->slots[0];
4201 pending_del_nr = 1;
4202 } else if (pending_del_nr &&
4203 path->slots[0] + 1 == pending_del_slot) {
4204 /* hop on the pending chunk */
4205 pending_del_nr++;
4206 pending_del_slot = path->slots[0];
4207 } else {
Chris Masond3977122009-01-05 21:25:51 -05004208 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004209 }
Chris Mason39279cc2007-06-12 06:35:45 -04004210 } else {
4211 break;
4212 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004213 if (found_extent && (root->ref_cows ||
4214 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004215 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004216 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004217 extent_num_bytes, 0,
4218 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004219 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004220 BUG_ON(ret);
4221 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004222
Yan, Zheng80825102009-11-12 09:35:36 +00004223 if (found_type == BTRFS_INODE_ITEM_KEY)
4224 break;
4225
4226 if (path->slots[0] == 0 ||
4227 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004228 if (pending_del_nr) {
4229 ret = btrfs_del_items(trans, root, path,
4230 pending_del_slot,
4231 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232 if (ret) {
4233 btrfs_abort_transaction(trans,
4234 root, ret);
4235 goto error;
4236 }
Yan, Zheng80825102009-11-12 09:35:36 +00004237 pending_del_nr = 0;
4238 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004239 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004240 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004241 } else {
4242 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004243 }
Chris Mason39279cc2007-06-12 06:35:45 -04004244 }
Yan, Zheng80825102009-11-12 09:35:36 +00004245out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004246 if (pending_del_nr) {
4247 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4248 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004249 if (ret)
4250 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004251 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004252error:
Chris Mason39279cc2007-06-12 06:35:45 -04004253 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004254 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004255}
4256
Chris Masona52d9a82007-08-27 16:49:44 -04004257/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004258 * btrfs_truncate_page - read, zero a chunk and write a page
4259 * @inode - inode that we're zeroing
4260 * @from - the offset to start zeroing
4261 * @len - the length to zero, 0 to zero the entire range respective to the
4262 * offset
4263 * @front - zero up to the offset instead of from the offset on
4264 *
4265 * This will find the page for the "from" offset and cow the page and zero the
4266 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004267 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004268int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4269 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004270{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004271 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004272 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004273 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4274 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004275 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004276 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004277 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004278 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4279 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4280 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004281 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004282 int ret = 0;
4283 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004284 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004285
Josef Bacik2aaa6652012-08-29 14:27:18 -04004286 if ((offset & (blocksize - 1)) == 0 &&
4287 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004288 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004289 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004290 if (ret)
4291 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004292
Chris Mason211c17f2008-05-15 09:13:45 -04004293again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004294 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004295 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004296 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004297 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004298 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004299 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004300
4301 page_start = page_offset(page);
4302 page_end = page_start + PAGE_CACHE_SIZE - 1;
4303
Chris Masona52d9a82007-08-27 16:49:44 -04004304 if (!PageUptodate(page)) {
4305 ret = btrfs_readpage(NULL, page);
4306 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004307 if (page->mapping != mapping) {
4308 unlock_page(page);
4309 page_cache_release(page);
4310 goto again;
4311 }
Chris Masona52d9a82007-08-27 16:49:44 -04004312 if (!PageUptodate(page)) {
4313 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004314 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004315 }
4316 }
Chris Mason211c17f2008-05-15 09:13:45 -04004317 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004318
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004319 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 set_page_extent_mapped(page);
4321
4322 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4323 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004324 unlock_extent_cached(io_tree, page_start, page_end,
4325 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 unlock_page(page);
4327 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004328 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004329 btrfs_put_ordered_extent(ordered);
4330 goto again;
4331 }
4332
Josef Bacik2ac55d42010-02-03 19:33:23 +00004333 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004334 EXTENT_DIRTY | EXTENT_DELALLOC |
4335 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004336 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004337
Josef Bacik2ac55d42010-02-03 19:33:23 +00004338 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4339 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004340 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004341 unlock_extent_cached(io_tree, page_start, page_end,
4342 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004343 goto out_unlock;
4344 }
4345
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004347 if (!len)
4348 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004350 if (front)
4351 memset(kaddr, 0, offset);
4352 else
4353 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004354 flush_dcache_page(page);
4355 kunmap(page);
4356 }
Chris Mason247e7432008-07-17 12:53:51 -04004357 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004358 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004359 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4360 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004361
Chris Mason89642222008-07-24 09:41:53 -04004362out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004363 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004364 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 unlock_page(page);
4366 page_cache_release(page);
4367out:
4368 return ret;
4369}
4370
Josef Bacik695a0d02011-03-04 15:46:53 -05004371/*
4372 * This function puts in dummy file extents for the area we're creating a hole
4373 * for. So if we are truncating this file to a larger size we need to insert
4374 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4375 * the range between oldsize and size
4376 */
Josef Bacika41ad392011-01-31 15:30:16 -05004377int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004378{
4379 struct btrfs_trans_handle *trans;
4380 struct btrfs_root *root = BTRFS_I(inode)->root;
4381 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004382 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004383 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004384 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004385 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4386 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004387 u64 last_byte;
4388 u64 cur_offset;
4389 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004390 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004391
4392 if (size <= hole_start)
4393 return 0;
4394
Yan Zheng9036c102008-10-30 14:19:41 -04004395 while (1) {
4396 struct btrfs_ordered_extent *ordered;
4397 btrfs_wait_ordered_range(inode, hole_start,
4398 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004399 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004400 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004401 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4402 if (!ordered)
4403 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004404 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4405 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004406 btrfs_put_ordered_extent(ordered);
4407 }
4408
Yan Zheng9036c102008-10-30 14:19:41 -04004409 cur_offset = hole_start;
4410 while (1) {
4411 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4412 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004413 if (IS_ERR(em)) {
4414 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004415 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004416 break;
4417 }
Yan Zheng9036c102008-10-30 14:19:41 -04004418 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004419 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004420 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004421 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004422 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004423
Miao Xie36423202011-12-14 20:12:02 -05004424 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004425 if (IS_ERR(trans)) {
4426 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004427 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004428 }
Yan, Zheng80825102009-11-12 09:35:36 +00004429
Josef Bacik5dc562c2012-08-17 13:14:17 -04004430 err = btrfs_drop_extents(trans, root, inode,
4431 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004432 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004433 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004434 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004435 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004436 break;
Miao Xie5b397372011-09-11 10:52:24 -04004437 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004438
Yan Zheng9036c102008-10-30 14:19:41 -04004439 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004440 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004441 0, hole_size, 0, hole_size,
4442 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004443 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004444 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004445 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004446 break;
Miao Xie5b397372011-09-11 10:52:24 -04004447 }
Yan, Zheng80825102009-11-12 09:35:36 +00004448
Josef Bacik5dc562c2012-08-17 13:14:17 -04004449 btrfs_drop_extent_cache(inode, cur_offset,
4450 cur_offset + hole_size - 1, 0);
4451 hole_em = alloc_extent_map();
4452 if (!hole_em) {
4453 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4454 &BTRFS_I(inode)->runtime_flags);
4455 goto next;
4456 }
4457 hole_em->start = cur_offset;
4458 hole_em->len = hole_size;
4459 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004460
Josef Bacik5dc562c2012-08-17 13:14:17 -04004461 hole_em->block_start = EXTENT_MAP_HOLE;
4462 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004463 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004464 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004465 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4466 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4467 hole_em->generation = trans->transid;
4468
4469 while (1) {
4470 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004471 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004472 write_unlock(&em_tree->lock);
4473 if (err != -EEXIST)
4474 break;
4475 btrfs_drop_extent_cache(inode, cur_offset,
4476 cur_offset +
4477 hole_size - 1, 0);
4478 }
4479 free_extent_map(hole_em);
4480next:
Miao Xie36423202011-12-14 20:12:02 -05004481 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004482 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004483 }
4484 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004485 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004486 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004487 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004488 break;
4489 }
4490
Yan, Zhenga22285a2010-05-16 10:48:46 -04004491 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004492 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4493 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004494 return err;
4495}
4496
Eric Sandeen3972f262013-01-12 02:57:22 +00004497static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004498{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004499 struct btrfs_root *root = BTRFS_I(inode)->root;
4500 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004501 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004502 loff_t newsize = attr->ia_size;
4503 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004504 int ret;
4505
Josef Bacika41ad392011-01-31 15:30:16 -05004506 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004507 return 0;
4508
Eric Sandeen3972f262013-01-12 02:57:22 +00004509 /*
4510 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4511 * special case where we need to update the times despite not having
4512 * these flags set. For all other operations the VFS set these flags
4513 * explicitly if it wants a timestamp update.
4514 */
4515 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4516 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4517
Josef Bacika41ad392011-01-31 15:30:16 -05004518 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004519 truncate_pagecache(inode, oldsize, newsize);
4520 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004521 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004522 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004523
Miao Xief4a2f4c2011-12-14 20:12:01 -05004524 trans = btrfs_start_transaction(root, 1);
4525 if (IS_ERR(trans))
4526 return PTR_ERR(trans);
4527
4528 i_size_write(inode, newsize);
4529 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4530 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004531 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004532 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004533
Josef Bacika41ad392011-01-31 15:30:16 -05004534 /*
4535 * We're truncating a file that used to have good data down to
4536 * zero. Make sure it gets into the ordered flush list so that
4537 * any new writes get down to disk quickly.
4538 */
4539 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004540 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4541 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004542
Josef Bacikf3fe8202013-01-07 17:03:21 -05004543 /*
4544 * 1 for the orphan item we're going to add
4545 * 1 for the orphan item deletion.
4546 */
4547 trans = btrfs_start_transaction(root, 2);
4548 if (IS_ERR(trans))
4549 return PTR_ERR(trans);
4550
4551 /*
4552 * We need to do this in case we fail at _any_ point during the
4553 * actual truncate. Once we do the truncate_setsize we could
4554 * invalidate pages which forces any outstanding ordered io to
4555 * be instantly completed which will give us extents that need
4556 * to be truncated. If we fail to get an orphan inode down we
4557 * could have left over extents that were never meant to live,
4558 * so we need to garuntee from this point on that everything
4559 * will be consistent.
4560 */
4561 ret = btrfs_orphan_add(trans, inode);
4562 btrfs_end_transaction(trans, root);
4563 if (ret)
4564 return ret;
4565
Josef Bacika41ad392011-01-31 15:30:16 -05004566 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4567 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004568
4569 /* Disable nonlocked read DIO to avoid the end less truncate */
4570 btrfs_inode_block_unlocked_dio(inode);
4571 inode_dio_wait(inode);
4572 btrfs_inode_resume_unlocked_dio(inode);
4573
Josef Bacika41ad392011-01-31 15:30:16 -05004574 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004575 if (ret && inode->i_nlink)
4576 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004577 }
4578
Josef Bacika41ad392011-01-31 15:30:16 -05004579 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004580}
4581
Chris Mason39279cc2007-06-12 06:35:45 -04004582static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4583{
4584 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004585 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004586 int err;
4587
Li Zefanb83cc962010-12-20 16:04:08 +08004588 if (btrfs_root_readonly(root))
4589 return -EROFS;
4590
Chris Mason39279cc2007-06-12 06:35:45 -04004591 err = inode_change_ok(inode, attr);
4592 if (err)
4593 return err;
4594
Chris Mason5a3f23d2009-03-31 13:27:11 -04004595 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004596 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004597 if (err)
4598 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004599 }
Yan Zheng9036c102008-10-30 14:19:41 -04004600
Christoph Hellwig10257742010-06-04 11:30:02 +02004601 if (attr->ia_valid) {
4602 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004603 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004604 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004605
Josef Bacik22c44fe2011-11-30 10:45:38 -05004606 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004607 err = btrfs_acl_chmod(inode);
4608 }
4609
Chris Mason39279cc2007-06-12 06:35:45 -04004610 return err;
4611}
Chris Mason61295eb2008-01-14 16:24:38 -05004612
Al Virobd555972010-06-07 11:35:40 -04004613void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004614{
4615 struct btrfs_trans_handle *trans;
4616 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004617 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004618 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004619 int ret;
4620
liubo1abe9b82011-03-24 11:18:59 +00004621 trace_btrfs_inode_evict(inode);
4622
Chris Mason39279cc2007-06-12 06:35:45 -04004623 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004624 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004625 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004626 goto no_delete;
4627
Chris Mason39279cc2007-06-12 06:35:45 -04004628 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004629 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004630 goto no_delete;
4631 }
Al Virobd555972010-06-07 11:35:40 -04004632 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004633 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004634
Yan, Zhengc71bf092009-11-12 09:34:40 +00004635 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004636 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004637 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004638 goto no_delete;
4639 }
4640
Yan, Zheng76dda932009-09-21 16:00:26 -04004641 if (inode->i_nlink > 0) {
4642 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4643 goto no_delete;
4644 }
4645
Miao Xie0e8c36a2012-12-19 06:59:51 +00004646 ret = btrfs_commit_inode_delayed_inode(inode);
4647 if (ret) {
4648 btrfs_orphan_del(NULL, inode);
4649 goto no_delete;
4650 }
4651
Miao Xie66d8f3d2012-09-06 04:02:28 -06004652 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004653 if (!rsv) {
4654 btrfs_orphan_del(NULL, inode);
4655 goto no_delete;
4656 }
Josef Bacik4a338542011-08-29 11:01:31 -04004657 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004658 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004659 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004660
Chris Masondbe674a2008-07-17 12:54:05 -04004661 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004662
Josef Bacik4289a662011-08-05 13:22:24 -04004663 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004664 * This is a bit simpler than btrfs_truncate since we've already
4665 * reserved our space for our orphan item in the unlink, so we just
4666 * need to reserve some slack space in case we add bytes and update
4667 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004668 */
Yan, Zheng80825102009-11-12 09:35:36 +00004669 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004670 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4671 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004672
Josef Bacik726c35f2011-09-26 15:46:06 -04004673 /*
4674 * Try and steal from the global reserve since we will
4675 * likely not use this space anyway, we want to try as
4676 * hard as possible to get this to work.
4677 */
4678 if (ret)
4679 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4680
Yan, Zhengd68fc572010-05-16 10:49:58 -04004681 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004682 btrfs_warn(root->fs_info,
4683 "Could not get space for a delete, will truncate on mount %d",
4684 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004685 btrfs_orphan_del(NULL, inode);
4686 btrfs_free_block_rsv(root, rsv);
4687 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004688 }
4689
Miao Xie0e8c36a2012-12-19 06:59:51 +00004690 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004691 if (IS_ERR(trans)) {
4692 btrfs_orphan_del(NULL, inode);
4693 btrfs_free_block_rsv(root, rsv);
4694 goto no_delete;
4695 }
4696
4697 trans->block_rsv = rsv;
4698
Yan, Zhengd68fc572010-05-16 10:49:58 -04004699 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004700 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004701 break;
4702
Miao Xie8407aa42012-09-07 01:43:32 -06004703 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004704 btrfs_end_transaction(trans, root);
4705 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004706 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004707 }
4708
Josef Bacik4289a662011-08-05 13:22:24 -04004709 btrfs_free_block_rsv(root, rsv);
4710
Yan, Zheng80825102009-11-12 09:35:36 +00004711 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004712 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004713 ret = btrfs_orphan_del(trans, inode);
4714 BUG_ON(ret);
4715 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004716
Josef Bacik4289a662011-08-05 13:22:24 -04004717 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004718 if (!(root == root->fs_info->tree_root ||
4719 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004720 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004721
Chris Mason54aa1f42007-06-22 14:16:25 -04004722 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004723 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004724no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004725 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004726 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004727 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004728}
4729
4730/*
4731 * this returns the key found in the dir entry in the location pointer.
4732 * If no dir entries were found, location->objectid is 0.
4733 */
4734static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4735 struct btrfs_key *location)
4736{
4737 const char *name = dentry->d_name.name;
4738 int namelen = dentry->d_name.len;
4739 struct btrfs_dir_item *di;
4740 struct btrfs_path *path;
4741 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004742 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004743
4744 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004745 if (!path)
4746 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004747
Li Zefan33345d012011-04-20 10:31:50 +08004748 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004749 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004750 if (IS_ERR(di))
4751 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004752
David Sterbac7040052011-04-19 18:00:01 +02004753 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004754 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004755
Chris Mason5f39d392007-10-15 16:14:19 -04004756 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004757out:
Chris Mason39279cc2007-06-12 06:35:45 -04004758 btrfs_free_path(path);
4759 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004760out_err:
4761 location->objectid = 0;
4762 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004763}
4764
4765/*
4766 * when we hit a tree root in a directory, the btrfs part of the inode
4767 * needs to be changed to reflect the root directory of the tree root. This
4768 * is kind of like crossing a mount point.
4769 */
4770static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004771 struct inode *dir,
4772 struct dentry *dentry,
4773 struct btrfs_key *location,
4774 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004775{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004776 struct btrfs_path *path;
4777 struct btrfs_root *new_root;
4778 struct btrfs_root_ref *ref;
4779 struct extent_buffer *leaf;
4780 int ret;
4781 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004782
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004783 path = btrfs_alloc_path();
4784 if (!path) {
4785 err = -ENOMEM;
4786 goto out;
4787 }
Chris Mason39279cc2007-06-12 06:35:45 -04004788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004789 err = -ENOENT;
4790 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4791 BTRFS_I(dir)->root->root_key.objectid,
4792 location->objectid);
4793 if (ret) {
4794 if (ret < 0)
4795 err = ret;
4796 goto out;
4797 }
Chris Mason39279cc2007-06-12 06:35:45 -04004798
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004799 leaf = path->nodes[0];
4800 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004801 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004802 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4803 goto out;
4804
4805 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4806 (unsigned long)(ref + 1),
4807 dentry->d_name.len);
4808 if (ret)
4809 goto out;
4810
David Sterbab3b4aa72011-04-21 01:20:15 +02004811 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004812
4813 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4814 if (IS_ERR(new_root)) {
4815 err = PTR_ERR(new_root);
4816 goto out;
4817 }
4818
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004819 *sub_root = new_root;
4820 location->objectid = btrfs_root_dirid(&new_root->root_item);
4821 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004822 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004823 err = 0;
4824out:
4825 btrfs_free_path(path);
4826 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004827}
4828
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004829static void inode_tree_add(struct inode *inode)
4830{
4831 struct btrfs_root *root = BTRFS_I(inode)->root;
4832 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004833 struct rb_node **p;
4834 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004835 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004836
Al Viro1d3382c2010-10-23 15:19:20 -04004837 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004838 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004839again:
4840 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004841 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004842 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004843 while (*p) {
4844 parent = *p;
4845 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4846
Li Zefan33345d012011-04-20 10:31:50 +08004847 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004848 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004849 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004850 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004851 else {
4852 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004853 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004854 rb_erase(parent, &root->inode_tree);
4855 RB_CLEAR_NODE(parent);
4856 spin_unlock(&root->inode_lock);
4857 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004858 }
4859 }
4860 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4861 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4862 spin_unlock(&root->inode_lock);
4863}
4864
4865static void inode_tree_del(struct inode *inode)
4866{
4867 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004868 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004869
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004870 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004871 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004872 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004873 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004874 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004875 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004876 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004877
Josef Bacik0af3d002010-06-21 14:48:16 -04004878 /*
4879 * Free space cache has inodes in the tree root, but the tree root has a
4880 * root_refs of 0, so this could end up dropping the tree root as a
4881 * snapshot, so we need the extra !root->fs_info->tree_root check to
4882 * make sure we don't drop it.
4883 */
4884 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4885 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004886 synchronize_srcu(&root->fs_info->subvol_srcu);
4887 spin_lock(&root->inode_lock);
4888 empty = RB_EMPTY_ROOT(&root->inode_tree);
4889 spin_unlock(&root->inode_lock);
4890 if (empty)
4891 btrfs_add_dead_root(root);
4892 }
4893}
4894
Jeff Mahoney143bede2012-03-01 14:56:26 +01004895void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004896{
4897 struct rb_node *node;
4898 struct rb_node *prev;
4899 struct btrfs_inode *entry;
4900 struct inode *inode;
4901 u64 objectid = 0;
4902
4903 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4904
4905 spin_lock(&root->inode_lock);
4906again:
4907 node = root->inode_tree.rb_node;
4908 prev = NULL;
4909 while (node) {
4910 prev = node;
4911 entry = rb_entry(node, struct btrfs_inode, rb_node);
4912
Li Zefan33345d012011-04-20 10:31:50 +08004913 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004914 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004915 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004916 node = node->rb_right;
4917 else
4918 break;
4919 }
4920 if (!node) {
4921 while (prev) {
4922 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004923 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004924 node = prev;
4925 break;
4926 }
4927 prev = rb_next(prev);
4928 }
4929 }
4930 while (node) {
4931 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004932 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004933 inode = igrab(&entry->vfs_inode);
4934 if (inode) {
4935 spin_unlock(&root->inode_lock);
4936 if (atomic_read(&inode->i_count) > 1)
4937 d_prune_aliases(inode);
4938 /*
Al Viro45321ac2010-06-07 13:43:19 -04004939 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004940 * the inode cache when its usage count
4941 * hits zero.
4942 */
4943 iput(inode);
4944 cond_resched();
4945 spin_lock(&root->inode_lock);
4946 goto again;
4947 }
4948
4949 if (cond_resched_lock(&root->inode_lock))
4950 goto again;
4951
4952 node = rb_next(node);
4953 }
4954 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004955}
4956
Chris Masone02119d2008-09-05 16:13:11 -04004957static int btrfs_init_locked_inode(struct inode *inode, void *p)
4958{
4959 struct btrfs_iget_args *args = p;
4960 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004961 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004962 return 0;
4963}
4964
4965static int btrfs_find_actor(struct inode *inode, void *opaque)
4966{
4967 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004968 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004969 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004970}
4971
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004972static struct inode *btrfs_iget_locked(struct super_block *s,
4973 u64 objectid,
4974 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004975{
4976 struct inode *inode;
4977 struct btrfs_iget_args args;
4978 args.ino = objectid;
4979 args.root = root;
4980
4981 inode = iget5_locked(s, objectid, btrfs_find_actor,
4982 btrfs_init_locked_inode,
4983 (void *)&args);
4984 return inode;
4985}
4986
Balaji Rao1a54ef82008-07-21 02:01:04 +05304987/* Get an inode object given its location and corresponding root.
4988 * Returns in *is_new if the inode was read from disk
4989 */
4990struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004991 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304992{
4993 struct inode *inode;
4994
4995 inode = btrfs_iget_locked(s, location->objectid, root);
4996 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004997 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304998
4999 if (inode->i_state & I_NEW) {
5000 BTRFS_I(inode)->root = root;
5001 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5002 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005003 if (!is_bad_inode(inode)) {
5004 inode_tree_add(inode);
5005 unlock_new_inode(inode);
5006 if (new)
5007 *new = 1;
5008 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005009 unlock_new_inode(inode);
5010 iput(inode);
5011 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005012 }
5013 }
5014
Balaji Rao1a54ef82008-07-21 02:01:04 +05305015 return inode;
5016}
5017
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005018static struct inode *new_simple_dir(struct super_block *s,
5019 struct btrfs_key *key,
5020 struct btrfs_root *root)
5021{
5022 struct inode *inode = new_inode(s);
5023
5024 if (!inode)
5025 return ERR_PTR(-ENOMEM);
5026
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005027 BTRFS_I(inode)->root = root;
5028 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005029 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005030
5031 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005032 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005033 inode->i_fop = &simple_dir_operations;
5034 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5035 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5036
5037 return inode;
5038}
5039
Chris Mason3de45862008-11-17 21:02:50 -05005040struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005041{
Chris Masond3977122009-01-05 21:25:51 -05005042 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005043 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005044 struct btrfs_root *sub_root = root;
5045 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005046 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005047 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005048
5049 if (dentry->d_name.len > BTRFS_NAME_LEN)
5050 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005051
Jeff Layton39e3c952012-11-28 11:30:53 -05005052 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005053 if (ret < 0)
5054 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005055
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005056 if (location.objectid == 0)
5057 return NULL;
5058
5059 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005060 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005061 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005062 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005063
5064 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5065
Yan, Zheng76dda932009-09-21 16:00:26 -04005066 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005067 ret = fixup_tree_root_location(root, dir, dentry,
5068 &location, &sub_root);
5069 if (ret < 0) {
5070 if (ret != -ENOENT)
5071 inode = ERR_PTR(ret);
5072 else
5073 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5074 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005075 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005076 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005077 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5078
Julia Lawall34d19ba2011-01-24 19:55:19 +00005079 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005080 down_read(&root->fs_info->cleanup_work_sem);
5081 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005082 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005083 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005084 if (ret)
5085 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005086 }
5087
Chris Mason3de45862008-11-17 21:02:50 -05005088 return inode;
5089}
5090
Nick Pigginfe15ce42011-01-07 17:49:23 +11005091static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005092{
5093 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005094 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005095
Li Zefan848cce02012-02-21 17:04:28 +08005096 if (!inode && !IS_ROOT(dentry))
5097 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005098
Li Zefan848cce02012-02-21 17:04:28 +08005099 if (inode) {
5100 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005101 if (btrfs_root_refs(&root->root_item) == 0)
5102 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005103
5104 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5105 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005106 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005107 return 0;
5108}
5109
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005110static void btrfs_dentry_release(struct dentry *dentry)
5111{
5112 if (dentry->d_fsdata)
5113 kfree(dentry->d_fsdata);
5114}
5115
Chris Mason3de45862008-11-17 21:02:50 -05005116static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005117 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005118{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005119 struct dentry *ret;
5120
5121 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005122 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005123}
5124
Miao Xie16cdcec2011-04-22 18:12:22 +08005125unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005126 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5127};
5128
David Woodhousecbdf5a22008-08-06 19:42:33 +01005129static int btrfs_real_readdir(struct file *filp, void *dirent,
5130 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005131{
Al Viro496ad9a2013-01-23 17:07:38 -05005132 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005133 struct btrfs_root *root = BTRFS_I(inode)->root;
5134 struct btrfs_item *item;
5135 struct btrfs_dir_item *di;
5136 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005137 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005138 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005139 struct list_head ins_list;
5140 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005141 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005142 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005143 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005144 unsigned char d_type;
5145 int over = 0;
5146 u32 di_cur;
5147 u32 di_total;
5148 u32 di_len;
5149 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005150 char tmp_name[32];
5151 char *name_ptr;
5152 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005153 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005154
5155 /* FIXME, use a real flag for deciding about the key type */
5156 if (root->fs_info->tree_root == root)
5157 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005158
Chris Mason39544012007-12-12 14:38:19 -05005159 /* special case for "." */
5160 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005161 over = filldir(dirent, ".", 1,
5162 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005163 if (over)
5164 return 0;
5165 filp->f_pos = 1;
5166 }
Chris Mason39544012007-12-12 14:38:19 -05005167 /* special case for .., just use the back ref */
5168 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005169 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005170 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005171 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005172 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005173 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005174 filp->f_pos = 2;
5175 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005176 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005177 if (!path)
5178 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005179
Josef Bacik026fd312011-05-13 10:32:11 -04005180 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005181
Miao Xie16cdcec2011-04-22 18:12:22 +08005182 if (key_type == BTRFS_DIR_INDEX_KEY) {
5183 INIT_LIST_HEAD(&ins_list);
5184 INIT_LIST_HEAD(&del_list);
5185 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5186 }
5187
Chris Mason39279cc2007-06-12 06:35:45 -04005188 btrfs_set_key_type(&key, key_type);
5189 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005190 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005191
Chris Mason39279cc2007-06-12 06:35:45 -04005192 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5193 if (ret < 0)
5194 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005195
5196 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005197 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005198 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005199 if (slot >= btrfs_header_nritems(leaf)) {
5200 ret = btrfs_next_leaf(root, path);
5201 if (ret < 0)
5202 goto err;
5203 else if (ret > 0)
5204 break;
5205 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005206 }
Chris Mason3de45862008-11-17 21:02:50 -05005207
Chris Mason5f39d392007-10-15 16:14:19 -04005208 item = btrfs_item_nr(leaf, slot);
5209 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5210
5211 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005212 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005213 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005214 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005215 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005216 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005217 if (key_type == BTRFS_DIR_INDEX_KEY &&
5218 btrfs_should_delete_dir_index(&del_list,
5219 found_key.offset))
5220 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005221
5222 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005223 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005224
Chris Mason39279cc2007-06-12 06:35:45 -04005225 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5226 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005227 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005228
5229 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005230 struct btrfs_key location;
5231
Josef Bacik22a94d42011-03-16 16:47:17 -04005232 if (verify_dir_item(root, leaf, di))
5233 break;
5234
Chris Mason5f39d392007-10-15 16:14:19 -04005235 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005236 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005237 name_ptr = tmp_name;
5238 } else {
5239 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005240 if (!name_ptr) {
5241 ret = -ENOMEM;
5242 goto err;
5243 }
Chris Mason5f39d392007-10-15 16:14:19 -04005244 }
5245 read_extent_buffer(leaf, name_ptr,
5246 (unsigned long)(di + 1), name_len);
5247
5248 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5249 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005250
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005251
Chris Mason3de45862008-11-17 21:02:50 -05005252 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005253 * skip it.
5254 *
5255 * In contrast to old kernels, we insert the snapshot's
5256 * dir item and dir index after it has been created, so
5257 * we won't find a reference to our own snapshot. We
5258 * still keep the following code for backward
5259 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005260 */
5261 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5262 location.objectid == root->root_key.objectid) {
5263 over = 0;
5264 goto skip;
5265 }
Chris Mason5f39d392007-10-15 16:14:19 -04005266 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005267 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005268 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005269
Chris Mason3de45862008-11-17 21:02:50 -05005270skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005271 if (name_ptr != tmp_name)
5272 kfree(name_ptr);
5273
Chris Mason39279cc2007-06-12 06:35:45 -04005274 if (over)
5275 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005276 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005277 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005278 di_cur += di_len;
5279 di = (struct btrfs_dir_item *)((char *)di + di_len);
5280 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005281next:
5282 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005283 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005284
Miao Xie16cdcec2011-04-22 18:12:22 +08005285 if (key_type == BTRFS_DIR_INDEX_KEY) {
5286 if (is_curr)
5287 filp->f_pos++;
5288 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5289 &ins_list);
5290 if (ret)
5291 goto nopos;
5292 }
5293
David Woodhouse49593bf2008-08-17 17:08:36 +01005294 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005295 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005296 /*
5297 * 32-bit glibc will use getdents64, but then strtol -
5298 * so the last number we can serve is this.
5299 */
5300 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005301 else
5302 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005303nopos:
5304 ret = 0;
5305err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005306 if (key_type == BTRFS_DIR_INDEX_KEY)
5307 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005308 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005309 return ret;
5310}
5311
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005312int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005313{
5314 struct btrfs_root *root = BTRFS_I(inode)->root;
5315 struct btrfs_trans_handle *trans;
5316 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005317 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005318
Josef Bacik72ac3c02012-05-23 14:13:11 -04005319 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005320 return 0;
5321
Liu Bo83eea1f2012-07-10 05:28:39 -06005322 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005323 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005324
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005325 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005326 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005327 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005328 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005329 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005330 if (IS_ERR(trans))
5331 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005332 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005333 }
5334 return ret;
5335}
5336
5337/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005338 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005339 * inode changes. But, it is most likely to find the inode in cache.
5340 * FIXME, needs more benchmarking...there are no reasons other than performance
5341 * to keep or drop this code.
5342 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005343static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005344{
5345 struct btrfs_root *root = BTRFS_I(inode)->root;
5346 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005347 int ret;
5348
Josef Bacik72ac3c02012-05-23 14:13:11 -04005349 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005350 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005351
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005352 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005353 if (IS_ERR(trans))
5354 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005355
5356 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005357 if (ret && ret == -ENOSPC) {
5358 /* whoops, lets try again with the full transaction */
5359 btrfs_end_transaction(trans, root);
5360 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005361 if (IS_ERR(trans))
5362 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005363
Chris Mason94b60442010-05-26 11:02:00 -04005364 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005365 }
Chris Mason39279cc2007-06-12 06:35:45 -04005366 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005367 if (BTRFS_I(inode)->delayed_node)
5368 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005369
5370 return ret;
5371}
5372
5373/*
5374 * This is a copy of file_update_time. We need this so we can return error on
5375 * ENOSPC for updating the inode in the case of file write and mmap writes.
5376 */
Josef Bacike41f9412012-03-26 09:46:47 -04005377static int btrfs_update_time(struct inode *inode, struct timespec *now,
5378 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005379{
Alexander Block2bc5565282012-06-15 09:49:33 +02005380 struct btrfs_root *root = BTRFS_I(inode)->root;
5381
5382 if (btrfs_root_readonly(root))
5383 return -EROFS;
5384
Josef Bacike41f9412012-03-26 09:46:47 -04005385 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005386 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005387 if (flags & S_CTIME)
5388 inode->i_ctime = *now;
5389 if (flags & S_MTIME)
5390 inode->i_mtime = *now;
5391 if (flags & S_ATIME)
5392 inode->i_atime = *now;
5393 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005394}
5395
Chris Masond352ac62008-09-29 15:18:18 -04005396/*
5397 * find the highest existing sequence number in a directory
5398 * and then set the in-memory index_cnt variable to reflect
5399 * free sequence numbers
5400 */
Josef Bacikaec74772008-07-24 12:12:38 -04005401static int btrfs_set_inode_index_count(struct inode *inode)
5402{
5403 struct btrfs_root *root = BTRFS_I(inode)->root;
5404 struct btrfs_key key, found_key;
5405 struct btrfs_path *path;
5406 struct extent_buffer *leaf;
5407 int ret;
5408
Li Zefan33345d012011-04-20 10:31:50 +08005409 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005410 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5411 key.offset = (u64)-1;
5412
5413 path = btrfs_alloc_path();
5414 if (!path)
5415 return -ENOMEM;
5416
5417 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5418 if (ret < 0)
5419 goto out;
5420 /* FIXME: we should be able to handle this */
5421 if (ret == 0)
5422 goto out;
5423 ret = 0;
5424
5425 /*
5426 * MAGIC NUMBER EXPLANATION:
5427 * since we search a directory based on f_pos we have to start at 2
5428 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5429 * else has to start at 2
5430 */
5431 if (path->slots[0] == 0) {
5432 BTRFS_I(inode)->index_cnt = 2;
5433 goto out;
5434 }
5435
5436 path->slots[0]--;
5437
5438 leaf = path->nodes[0];
5439 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5440
Li Zefan33345d012011-04-20 10:31:50 +08005441 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005442 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5443 BTRFS_I(inode)->index_cnt = 2;
5444 goto out;
5445 }
5446
5447 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5448out:
5449 btrfs_free_path(path);
5450 return ret;
5451}
5452
Chris Masond352ac62008-09-29 15:18:18 -04005453/*
5454 * helper to find a free sequence number in a given directory. This current
5455 * code is very simple, later versions will do smarter things in the btree
5456 */
Chris Mason3de45862008-11-17 21:02:50 -05005457int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005458{
5459 int ret = 0;
5460
5461 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005462 ret = btrfs_inode_delayed_dir_index_count(dir);
5463 if (ret) {
5464 ret = btrfs_set_inode_index_count(dir);
5465 if (ret)
5466 return ret;
5467 }
Josef Bacikaec74772008-07-24 12:12:38 -04005468 }
5469
Chris Mason00e4e6b2008-08-05 11:18:09 -04005470 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005471 BTRFS_I(dir)->index_cnt++;
5472
5473 return ret;
5474}
5475
Chris Mason39279cc2007-06-12 06:35:45 -04005476static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5477 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005478 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005479 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005480 u64 ref_objectid, u64 objectid,
5481 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005482{
5483 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005484 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005485 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005486 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005487 struct btrfs_inode_ref *ref;
5488 struct btrfs_key key[2];
5489 u32 sizes[2];
5490 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005491 int ret;
5492 int owner;
5493
Chris Mason5f39d392007-10-15 16:14:19 -04005494 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005495 if (!path)
5496 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005497
Chris Mason39279cc2007-06-12 06:35:45 -04005498 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005499 if (!inode) {
5500 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005502 }
Chris Mason39279cc2007-06-12 06:35:45 -04005503
Li Zefan581bb052011-04-20 10:06:11 +08005504 /*
5505 * we have to initialize this early, so we can reclaim the inode
5506 * number if we fail afterwards in this function.
5507 */
5508 inode->i_ino = objectid;
5509
Josef Bacikaec74772008-07-24 12:12:38 -04005510 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005511 trace_btrfs_inode_request(dir);
5512
Chris Mason3de45862008-11-17 21:02:50 -05005513 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005514 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005515 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005516 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005517 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005518 }
Josef Bacikaec74772008-07-24 12:12:38 -04005519 }
5520 /*
5521 * index_cnt is ignored for everything but a dir,
5522 * btrfs_get_inode_index_count has an explanation for the magic
5523 * number
5524 */
5525 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005526 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005527 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005528 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005529
Josef Bacik5dc562c2012-08-17 13:14:17 -04005530 /*
5531 * We could have gotten an inode number from somebody who was fsynced
5532 * and then removed in this same transaction, so let's just set full
5533 * sync since it will be a full sync anyway and this will blow away the
5534 * old info in the log.
5535 */
5536 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5537
Al Viro569254b2011-07-24 17:08:40 -04005538 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005539 owner = 0;
5540 else
5541 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005542
5543 key[0].objectid = objectid;
5544 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5545 key[0].offset = 0;
5546
Mark Fashehf1863732012-08-08 11:32:27 -07005547 /*
5548 * Start new inodes with an inode_ref. This is slightly more
5549 * efficient for small numbers of hard links since they will
5550 * be packed into one item. Extended refs will kick in if we
5551 * add more hard links than can fit in the ref item.
5552 */
Chris Mason9c583092008-01-29 15:15:18 -05005553 key[1].objectid = objectid;
5554 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5555 key[1].offset = ref_objectid;
5556
5557 sizes[0] = sizeof(struct btrfs_inode_item);
5558 sizes[1] = name_len + sizeof(*ref);
5559
Chris Masonb9473432009-03-13 11:00:37 -04005560 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005561 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5562 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005563 goto fail;
5564
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005565 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005566 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005567 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005568 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5569 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005570 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5571 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005572 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005573
5574 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5575 struct btrfs_inode_ref);
5576 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005577 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005578 ptr = (unsigned long)(ref + 1);
5579 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5580
Chris Mason5f39d392007-10-15 16:14:19 -04005581 btrfs_mark_buffer_dirty(path->nodes[0]);
5582 btrfs_free_path(path);
5583
Chris Mason39279cc2007-06-12 06:35:45 -04005584 location = &BTRFS_I(inode)->location;
5585 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005586 location->offset = 0;
5587 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5588
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005589 btrfs_inherit_iflags(inode, dir);
5590
Al Viro569254b2011-07-24 17:08:40 -04005591 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005592 if (btrfs_test_opt(root, NODATASUM))
5593 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005594 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005595 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5596 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005597 }
5598
Chris Mason39279cc2007-06-12 06:35:45 -04005599 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005601
5602 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005603 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005604
Alexander Block8ea05e32012-07-25 17:35:53 +02005605 btrfs_update_root_times(trans, root);
5606
Chris Mason39279cc2007-06-12 06:35:45 -04005607 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005608fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005609 if (dir)
5610 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005611 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005612 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005613 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005614}
5615
5616static inline u8 btrfs_inode_type(struct inode *inode)
5617{
5618 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5619}
5620
Chris Masond352ac62008-09-29 15:18:18 -04005621/*
5622 * utility function to add 'inode' into 'parent_inode' with
5623 * a give name and a given sequence number.
5624 * if 'add_backref' is true, also insert a backref from the
5625 * inode to the parent directory.
5626 */
Chris Masone02119d2008-09-05 16:13:11 -04005627int btrfs_add_link(struct btrfs_trans_handle *trans,
5628 struct inode *parent_inode, struct inode *inode,
5629 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005630{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005631 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005632 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005633 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005634 u64 ino = btrfs_ino(inode);
5635 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005636
Li Zefan33345d012011-04-20 10:31:50 +08005637 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5639 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005640 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5642 key.offset = 0;
5643 }
Chris Mason39279cc2007-06-12 06:35:45 -04005644
Li Zefan33345d012011-04-20 10:31:50 +08005645 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005646 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5647 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005648 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005649 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005650 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5651 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 }
5653
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005654 /* Nothing to clean up yet */
5655 if (ret)
5656 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005658 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5659 parent_inode, &key,
5660 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005661 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005662 goto fail_dir_item;
5663 else if (ret) {
5664 btrfs_abort_transaction(trans, root, ret);
5665 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005666 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005667
5668 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5669 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005670 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005671 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5672 ret = btrfs_update_inode(trans, root, parent_inode);
5673 if (ret)
5674 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005675 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005676
5677fail_dir_item:
5678 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5679 u64 local_index;
5680 int err;
5681 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5682 key.objectid, root->root_key.objectid,
5683 parent_ino, &local_index, name, name_len);
5684
5685 } else if (add_backref) {
5686 u64 local_index;
5687 int err;
5688
5689 err = btrfs_del_inode_ref(trans, root, name, name_len,
5690 ino, parent_ino, &local_index);
5691 }
5692 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005693}
5694
5695static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005696 struct inode *dir, struct dentry *dentry,
5697 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005698{
Josef Bacika1b075d2010-11-19 20:36:11 +00005699 int err = btrfs_add_link(trans, dir, inode,
5700 dentry->d_name.name, dentry->d_name.len,
5701 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005702 if (err > 0)
5703 err = -EEXIST;
5704 return err;
5705}
5706
Josef Bacik618e21d2007-07-11 10:18:17 -04005707static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005708 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005709{
5710 struct btrfs_trans_handle *trans;
5711 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005712 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005713 int err;
5714 int drop_inode = 0;
5715 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005716 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005717
5718 if (!new_valid_dev(rdev))
5719 return -EINVAL;
5720
Josef Bacik9ed74f22009-09-11 16:12:44 -04005721 /*
5722 * 2 for inode item and ref
5723 * 2 for dir items
5724 * 1 for xattr if selinux is on
5725 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005726 trans = btrfs_start_transaction(root, 5);
5727 if (IS_ERR(trans))
5728 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005729
Li Zefan581bb052011-04-20 10:06:11 +08005730 err = btrfs_find_free_ino(root, &objectid);
5731 if (err)
5732 goto out_unlock;
5733
Josef Bacikaec74772008-07-24 12:12:38 -04005734 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005735 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005736 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005737 if (IS_ERR(inode)) {
5738 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005739 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005740 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005741
Eric Paris2a7dba32011-02-01 11:05:39 -05005742 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005743 if (err) {
5744 drop_inode = 1;
5745 goto out_unlock;
5746 }
5747
Casey Schauflerad19db72011-12-15 10:09:07 -05005748 /*
5749 * If the active LSM wants to access the inode during
5750 * d_instantiate it needs these. Smack checks to see
5751 * if the filesystem supports xattrs by looking at the
5752 * ops vector.
5753 */
5754
5755 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005756 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005757 if (err)
5758 drop_inode = 1;
5759 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005760 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005761 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005762 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005763 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005764out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005765 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005766 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005767 if (drop_inode) {
5768 inode_dec_link_count(inode);
5769 iput(inode);
5770 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005771 return err;
5772}
5773
Chris Mason39279cc2007-06-12 06:35:45 -04005774static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005775 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005776{
5777 struct btrfs_trans_handle *trans;
5778 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005779 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005780 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005781 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005782 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005783 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005784
Josef Bacik9ed74f22009-09-11 16:12:44 -04005785 /*
5786 * 2 for inode item and ref
5787 * 2 for dir items
5788 * 1 for xattr if selinux is on
5789 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005790 trans = btrfs_start_transaction(root, 5);
5791 if (IS_ERR(trans))
5792 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005793
Li Zefan581bb052011-04-20 10:06:11 +08005794 err = btrfs_find_free_ino(root, &objectid);
5795 if (err)
5796 goto out_unlock;
5797
Josef Bacikaec74772008-07-24 12:12:38 -04005798 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005799 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005800 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005801 if (IS_ERR(inode)) {
5802 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005803 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005804 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005805 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005806
Eric Paris2a7dba32011-02-01 11:05:39 -05005807 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005808 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005809 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005810
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005811 err = btrfs_update_inode(trans, root, inode);
5812 if (err)
5813 goto out_unlock;
5814
Casey Schauflerad19db72011-12-15 10:09:07 -05005815 /*
5816 * If the active LSM wants to access the inode during
5817 * d_instantiate it needs these. Smack checks to see
5818 * if the filesystem supports xattrs by looking at the
5819 * ops vector.
5820 */
5821 inode->i_fop = &btrfs_file_operations;
5822 inode->i_op = &btrfs_file_inode_operations;
5823
Josef Bacika1b075d2010-11-19 20:36:11 +00005824 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005825 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005826 goto out_unlock;
5827
5828 inode->i_mapping->a_ops = &btrfs_aops;
5829 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5830 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5831 d_instantiate(dentry, inode);
5832
Chris Mason39279cc2007-06-12 06:35:45 -04005833out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005834 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005835 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005836 inode_dec_link_count(inode);
5837 iput(inode);
5838 }
Liu Bob53d3f52012-11-14 14:34:34 +00005839 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005840 return err;
5841}
5842
5843static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5844 struct dentry *dentry)
5845{
5846 struct btrfs_trans_handle *trans;
5847 struct btrfs_root *root = BTRFS_I(dir)->root;
5848 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005849 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005850 int err;
5851 int drop_inode = 0;
5852
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005853 /* do not allow sys_link's with other subvols of the same device */
5854 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005855 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005856
Mark Fashehf1863732012-08-08 11:32:27 -07005857 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005858 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005859
Chris Mason3de45862008-11-17 21:02:50 -05005860 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005861 if (err)
5862 goto fail;
5863
Yan, Zhenga22285a2010-05-16 10:48:46 -04005864 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005865 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005866 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005867 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005868 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005869 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005870 if (IS_ERR(trans)) {
5871 err = PTR_ERR(trans);
5872 goto fail;
5873 }
Chris Mason5f39d392007-10-15 16:14:19 -04005874
Miao Xie31534952011-04-13 13:19:21 +08005875 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005876 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005877 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005878 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005879 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005880
Josef Bacika1b075d2010-11-19 20:36:11 +00005881 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005882
Yan, Zhenga5719522009-09-24 09:17:31 -04005883 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005884 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005885 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005886 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005887 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005888 if (err)
5889 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005890 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005891 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005892 }
Chris Mason39279cc2007-06-12 06:35:45 -04005893
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005894 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005895fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005896 if (drop_inode) {
5897 inode_dec_link_count(inode);
5898 iput(inode);
5899 }
Liu Bob53d3f52012-11-14 14:34:34 +00005900 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005901 return err;
5902}
5903
Al Viro18bb1db2011-07-26 01:41:39 -04005904static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005905{
Chris Masonb9d86662008-05-02 16:13:49 -04005906 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005907 struct btrfs_trans_handle *trans;
5908 struct btrfs_root *root = BTRFS_I(dir)->root;
5909 int err = 0;
5910 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005911 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005912 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005913
Josef Bacik9ed74f22009-09-11 16:12:44 -04005914 /*
5915 * 2 items for inode and ref
5916 * 2 items for dir items
5917 * 1 for xattr if selinux is on
5918 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005919 trans = btrfs_start_transaction(root, 5);
5920 if (IS_ERR(trans))
5921 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005922
Li Zefan581bb052011-04-20 10:06:11 +08005923 err = btrfs_find_free_ino(root, &objectid);
5924 if (err)
5925 goto out_fail;
5926
Josef Bacikaec74772008-07-24 12:12:38 -04005927 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005928 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005929 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005930 if (IS_ERR(inode)) {
5931 err = PTR_ERR(inode);
5932 goto out_fail;
5933 }
Chris Mason5f39d392007-10-15 16:14:19 -04005934
Chris Mason39279cc2007-06-12 06:35:45 -04005935 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005936
Eric Paris2a7dba32011-02-01 11:05:39 -05005937 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005938 if (err)
5939 goto out_fail;
5940
Chris Mason39279cc2007-06-12 06:35:45 -04005941 inode->i_op = &btrfs_dir_inode_operations;
5942 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005943
Chris Masondbe674a2008-07-17 12:54:05 -04005944 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005945 err = btrfs_update_inode(trans, root, inode);
5946 if (err)
5947 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005948
Josef Bacika1b075d2010-11-19 20:36:11 +00005949 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5950 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005951 if (err)
5952 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005953
Chris Mason39279cc2007-06-12 06:35:45 -04005954 d_instantiate(dentry, inode);
5955 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005956
5957out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005958 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005959 if (drop_on_err)
5960 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005961 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005962 return err;
5963}
5964
Chris Masond352ac62008-09-29 15:18:18 -04005965/* helper for btfs_get_extent. Given an existing extent in the tree,
5966 * and an extent that you want to insert, deal with overlap and insert
5967 * the new extent into the tree.
5968 */
Chris Mason3b951512008-04-17 11:29:12 -04005969static int merge_extent_mapping(struct extent_map_tree *em_tree,
5970 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005971 struct extent_map *em,
5972 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005973{
5974 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005975
Chris Masone6dcd2d2008-07-17 12:53:50 -04005976 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5977 start_diff = map_start - em->start;
5978 em->start = map_start;
5979 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005980 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5981 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005982 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005983 em->block_len -= start_diff;
5984 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005985 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005986}
5987
Chris Masonc8b97812008-10-29 14:49:59 -04005988static noinline int uncompress_inline(struct btrfs_path *path,
5989 struct inode *inode, struct page *page,
5990 size_t pg_offset, u64 extent_offset,
5991 struct btrfs_file_extent_item *item)
5992{
5993 int ret;
5994 struct extent_buffer *leaf = path->nodes[0];
5995 char *tmp;
5996 size_t max_size;
5997 unsigned long inline_size;
5998 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005999 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006000
6001 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006002 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006003 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6004 inline_size = btrfs_file_extent_inline_item_len(leaf,
6005 btrfs_item_nr(leaf, path->slots[0]));
6006 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006007 if (!tmp)
6008 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006009 ptr = btrfs_file_extent_inline_start(item);
6010
6011 read_extent_buffer(leaf, tmp, ptr, inline_size);
6012
Chris Mason5b050f02008-11-11 09:34:41 -05006013 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006014 ret = btrfs_decompress(compress_type, tmp, page,
6015 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006016 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006017 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006018 unsigned long copy_size = min_t(u64,
6019 PAGE_CACHE_SIZE - pg_offset,
6020 max_size - extent_offset);
6021 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006022 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006023 }
6024 kfree(tmp);
6025 return 0;
6026}
6027
Chris Masond352ac62008-09-29 15:18:18 -04006028/*
6029 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006030 * the ugly parts come from merging extents from the disk with the in-ram
6031 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006032 * where the in-ram extents might be locked pending data=ordered completion.
6033 *
6034 * This also copies inline extents directly into the page.
6035 */
Chris Masond3977122009-01-05 21:25:51 -05006036
Chris Masona52d9a82007-08-27 16:49:44 -04006037struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006038 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006039 int create)
6040{
6041 int ret;
6042 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006043 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006044 u64 extent_start = 0;
6045 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006046 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006047 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006048 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006049 struct btrfs_root *root = BTRFS_I(inode)->root;
6050 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006051 struct extent_buffer *leaf;
6052 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006053 struct extent_map *em = NULL;
6054 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006055 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006056 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006057 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006058
Chris Masona52d9a82007-08-27 16:49:44 -04006059again:
Chris Mason890871b2009-09-02 16:24:52 -04006060 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006061 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006062 if (em)
6063 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006064 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006065
Chris Masona52d9a82007-08-27 16:49:44 -04006066 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006067 if (em->start > start || em->start + em->len <= start)
6068 free_extent_map(em);
6069 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006070 free_extent_map(em);
6071 else
6072 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006073 }
David Sterba172ddd62011-04-21 00:48:27 +02006074 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006075 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006076 err = -ENOMEM;
6077 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006078 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006079 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006080 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006081 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006082 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006083 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006084
6085 if (!path) {
6086 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006087 if (!path) {
6088 err = -ENOMEM;
6089 goto out;
6090 }
6091 /*
6092 * Chances are we'll be called again, so go ahead and do
6093 * readahead
6094 */
6095 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006096 }
6097
Chris Mason179e29e2007-11-01 11:28:41 -04006098 ret = btrfs_lookup_file_extent(trans, root, path,
6099 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006100 if (ret < 0) {
6101 err = ret;
6102 goto out;
6103 }
6104
6105 if (ret != 0) {
6106 if (path->slots[0] == 0)
6107 goto not_found;
6108 path->slots[0]--;
6109 }
6110
Chris Mason5f39d392007-10-15 16:14:19 -04006111 leaf = path->nodes[0];
6112 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006113 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006114 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006115 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6116 found_type = btrfs_key_type(&found_key);
6117 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006118 found_type != BTRFS_EXTENT_DATA_KEY) {
6119 goto not_found;
6120 }
6121
Chris Mason5f39d392007-10-15 16:14:19 -04006122 found_type = btrfs_file_extent_type(leaf, item);
6123 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006124 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006125 if (found_type == BTRFS_FILE_EXTENT_REG ||
6126 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006127 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006128 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006129 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6130 size_t size;
6131 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006132 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006133 }
6134
6135 if (start >= extent_end) {
6136 path->slots[0]++;
6137 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6138 ret = btrfs_next_leaf(root, path);
6139 if (ret < 0) {
6140 err = ret;
6141 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006142 }
Yan Zheng9036c102008-10-30 14:19:41 -04006143 if (ret > 0)
6144 goto not_found;
6145 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006146 }
Yan Zheng9036c102008-10-30 14:19:41 -04006147 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6148 if (found_key.objectid != objectid ||
6149 found_key.type != BTRFS_EXTENT_DATA_KEY)
6150 goto not_found;
6151 if (start + len <= found_key.offset)
6152 goto not_found;
6153 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006154 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006155 em->len = found_key.offset - start;
6156 goto not_found_em;
6157 }
6158
Josef Bacikcc95bef2013-04-04 14:31:27 -04006159 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006160 if (found_type == BTRFS_FILE_EXTENT_REG ||
6161 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006162 em->start = extent_start;
6163 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006164 em->orig_start = extent_start -
6165 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006166 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6167 item);
Chris Masondb945352007-10-15 16:15:53 -04006168 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6169 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006170 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006171 goto insert;
6172 }
Li Zefan261507a02010-12-17 14:21:50 +08006173 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006174 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006175 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006176 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006177 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006178 } else {
6179 bytenr += btrfs_file_extent_offset(leaf, item);
6180 em->block_start = bytenr;
6181 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006182 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6183 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006184 }
Chris Masona52d9a82007-08-27 16:49:44 -04006185 goto insert;
6186 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006187 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006188 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006189 size_t size;
6190 size_t extent_offset;
6191 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006192
Chris Masona52d9a82007-08-27 16:49:44 -04006193 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006194 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006195 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006196 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006197 goto out;
6198 }
6199
Yan Zheng9036c102008-10-30 14:19:41 -04006200 size = btrfs_file_extent_inline_len(leaf, item);
6201 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006202 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006203 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006204 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006205 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006206 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006207 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006208 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006209 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006210 em->compress_type = compress_type;
6211 }
Yan689f9342007-10-29 11:41:07 -04006212 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006213 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006214 if (btrfs_file_extent_compression(leaf, item) !=
6215 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006216 ret = uncompress_inline(path, inode, page,
6217 pg_offset,
6218 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006219 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006220 } else {
6221 map = kmap(page);
6222 read_extent_buffer(leaf, map + pg_offset, ptr,
6223 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006224 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6225 memset(map + pg_offset + copy_size, 0,
6226 PAGE_CACHE_SIZE - pg_offset -
6227 copy_size);
6228 }
Chris Masonc8b97812008-10-29 14:49:59 -04006229 kunmap(page);
6230 }
Chris Mason179e29e2007-11-01 11:28:41 -04006231 flush_dcache_page(page);
6232 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006233 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006234 if (!trans) {
6235 kunmap(page);
6236 free_extent_map(em);
6237 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006238
David Sterbab3b4aa72011-04-21 01:20:15 +02006239 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006240 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006241
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006242 if (IS_ERR(trans))
6243 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006244 goto again;
6245 }
Chris Masonc8b97812008-10-29 14:49:59 -04006246 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006247 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006248 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006249 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006250 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006251 }
Chris Masond1310b22008-01-24 16:13:08 -05006252 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006253 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006254 goto insert;
6255 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006256 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006257 }
6258not_found:
6259 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006260 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006261 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006262not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006263 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006264 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006265insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006266 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006267 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006268 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6269 (unsigned long long)em->start,
6270 (unsigned long long)em->len,
6271 (unsigned long long)start,
6272 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006273 err = -EIO;
6274 goto out;
6275 }
Chris Masond1310b22008-01-24 16:13:08 -05006276
6277 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006278 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006279 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006280 /* it is possible that someone inserted the extent into the tree
6281 * while we had the lock dropped. It is also possible that
6282 * an overlapping map exists in the tree
6283 */
Chris Masona52d9a82007-08-27 16:49:44 -04006284 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006285 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006286
6287 ret = 0;
6288
Chris Mason3b951512008-04-17 11:29:12 -04006289 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006290 if (existing && (existing->start > start ||
6291 existing->start + existing->len <= start)) {
6292 free_extent_map(existing);
6293 existing = NULL;
6294 }
Chris Mason3b951512008-04-17 11:29:12 -04006295 if (!existing) {
6296 existing = lookup_extent_mapping(em_tree, em->start,
6297 em->len);
6298 if (existing) {
6299 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006300 em, start,
6301 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006302 free_extent_map(existing);
6303 if (err) {
6304 free_extent_map(em);
6305 em = NULL;
6306 }
6307 } else {
6308 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006309 free_extent_map(em);
6310 em = NULL;
6311 }
6312 } else {
6313 free_extent_map(em);
6314 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006315 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006316 }
Chris Masona52d9a82007-08-27 16:49:44 -04006317 }
Chris Mason890871b2009-09-02 16:24:52 -04006318 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006319out:
liubo1abe9b82011-03-24 11:18:59 +00006320
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006321 if (em)
6322 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006323
Chris Masonf4219502008-07-22 11:18:09 -04006324 if (path)
6325 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006326 if (trans) {
6327 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006328 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006329 err = ret;
6330 }
Chris Masona52d9a82007-08-27 16:49:44 -04006331 if (err) {
6332 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006333 return ERR_PTR(err);
6334 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006335 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006336 return em;
6337}
6338
Chris Masonec29ed52011-02-23 16:23:20 -05006339struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6340 size_t pg_offset, u64 start, u64 len,
6341 int create)
6342{
6343 struct extent_map *em;
6344 struct extent_map *hole_em = NULL;
6345 u64 range_start = start;
6346 u64 end;
6347 u64 found;
6348 u64 found_end;
6349 int err = 0;
6350
6351 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6352 if (IS_ERR(em))
6353 return em;
6354 if (em) {
6355 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006356 * if our em maps to
6357 * - a hole or
6358 * - a pre-alloc extent,
6359 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006360 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006361 if (em->block_start != EXTENT_MAP_HOLE &&
6362 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006363 return em;
6364 else
6365 hole_em = em;
6366 }
6367
6368 /* check to see if we've wrapped (len == -1 or similar) */
6369 end = start + len;
6370 if (end < start)
6371 end = (u64)-1;
6372 else
6373 end -= 1;
6374
6375 em = NULL;
6376
6377 /* ok, we didn't find anything, lets look for delalloc */
6378 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6379 end, len, EXTENT_DELALLOC, 1);
6380 found_end = range_start + found;
6381 if (found_end < range_start)
6382 found_end = (u64)-1;
6383
6384 /*
6385 * we didn't find anything useful, return
6386 * the original results from get_extent()
6387 */
6388 if (range_start > end || found_end <= start) {
6389 em = hole_em;
6390 hole_em = NULL;
6391 goto out;
6392 }
6393
6394 /* adjust the range_start to make sure it doesn't
6395 * go backwards from the start they passed in
6396 */
6397 range_start = max(start,range_start);
6398 found = found_end - range_start;
6399
6400 if (found > 0) {
6401 u64 hole_start = start;
6402 u64 hole_len = len;
6403
David Sterba172ddd62011-04-21 00:48:27 +02006404 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006405 if (!em) {
6406 err = -ENOMEM;
6407 goto out;
6408 }
6409 /*
6410 * when btrfs_get_extent can't find anything it
6411 * returns one huge hole
6412 *
6413 * make sure what it found really fits our range, and
6414 * adjust to make sure it is based on the start from
6415 * the caller
6416 */
6417 if (hole_em) {
6418 u64 calc_end = extent_map_end(hole_em);
6419
6420 if (calc_end <= start || (hole_em->start > end)) {
6421 free_extent_map(hole_em);
6422 hole_em = NULL;
6423 } else {
6424 hole_start = max(hole_em->start, start);
6425 hole_len = calc_end - hole_start;
6426 }
6427 }
6428 em->bdev = NULL;
6429 if (hole_em && range_start > hole_start) {
6430 /* our hole starts before our delalloc, so we
6431 * have to return just the parts of the hole
6432 * that go until the delalloc starts
6433 */
6434 em->len = min(hole_len,
6435 range_start - hole_start);
6436 em->start = hole_start;
6437 em->orig_start = hole_start;
6438 /*
6439 * don't adjust block start at all,
6440 * it is fixed at EXTENT_MAP_HOLE
6441 */
6442 em->block_start = hole_em->block_start;
6443 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006444 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6445 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006446 } else {
6447 em->start = range_start;
6448 em->len = found;
6449 em->orig_start = range_start;
6450 em->block_start = EXTENT_MAP_DELALLOC;
6451 em->block_len = found;
6452 }
6453 } else if (hole_em) {
6454 return hole_em;
6455 }
6456out:
6457
6458 free_extent_map(hole_em);
6459 if (err) {
6460 free_extent_map(em);
6461 return ERR_PTR(err);
6462 }
6463 return em;
6464}
6465
Josef Bacik4b46fce2010-05-23 11:00:55 -04006466static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6467 u64 start, u64 len)
6468{
6469 struct btrfs_root *root = BTRFS_I(inode)->root;
6470 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006471 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006472 struct btrfs_key ins;
6473 u64 alloc_hint;
6474 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006475
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006476 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006477 if (IS_ERR(trans))
6478 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006479
6480 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6481
6482 alloc_hint = get_extent_allocation_hint(inode, start, len);
6483 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006484 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006485 if (ret) {
6486 em = ERR_PTR(ret);
6487 goto out;
6488 }
6489
Josef Bacik70c8a912012-10-11 16:54:30 -04006490 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006491 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006492 if (IS_ERR(em))
6493 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006494
6495 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6496 ins.offset, ins.offset, 0);
6497 if (ret) {
6498 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6499 em = ERR_PTR(ret);
6500 }
6501out:
6502 btrfs_end_transaction(trans, root);
6503 return em;
6504}
6505
Chris Mason46bfbb52010-05-26 11:04:10 -04006506/*
6507 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6508 * block must be cow'd
6509 */
6510static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006511 struct inode *inode, u64 offset, u64 *len,
6512 u64 *orig_start, u64 *orig_block_len,
6513 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006514{
6515 struct btrfs_path *path;
6516 int ret;
6517 struct extent_buffer *leaf;
6518 struct btrfs_root *root = BTRFS_I(inode)->root;
6519 struct btrfs_file_extent_item *fi;
6520 struct btrfs_key key;
6521 u64 disk_bytenr;
6522 u64 backref_offset;
6523 u64 extent_end;
6524 u64 num_bytes;
6525 int slot;
6526 int found_type;
6527
6528 path = btrfs_alloc_path();
6529 if (!path)
6530 return -ENOMEM;
6531
Li Zefan33345d012011-04-20 10:31:50 +08006532 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006533 offset, 0);
6534 if (ret < 0)
6535 goto out;
6536
6537 slot = path->slots[0];
6538 if (ret == 1) {
6539 if (slot == 0) {
6540 /* can't find the item, must cow */
6541 ret = 0;
6542 goto out;
6543 }
6544 slot--;
6545 }
6546 ret = 0;
6547 leaf = path->nodes[0];
6548 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006549 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006550 key.type != BTRFS_EXTENT_DATA_KEY) {
6551 /* not our file or wrong item type, must cow */
6552 goto out;
6553 }
6554
6555 if (key.offset > offset) {
6556 /* Wrong offset, must cow */
6557 goto out;
6558 }
6559
6560 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6561 found_type = btrfs_file_extent_type(leaf, fi);
6562 if (found_type != BTRFS_FILE_EXTENT_REG &&
6563 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6564 /* not a regular extent, must cow */
6565 goto out;
6566 }
6567 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6568 backref_offset = btrfs_file_extent_offset(leaf, fi);
6569
Josef Bacikeb384b52013-04-24 16:32:55 -04006570 *orig_start = key.offset - backref_offset;
6571 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6572 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6573
Chris Mason46bfbb52010-05-26 11:04:10 -04006574 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006575 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006576 /* extent doesn't include our full range, must cow */
6577 goto out;
6578 }
6579
6580 if (btrfs_extent_readonly(root, disk_bytenr))
6581 goto out;
6582
6583 /*
6584 * look for other files referencing this extent, if we
6585 * find any we must cow
6586 */
Li Zefan33345d012011-04-20 10:31:50 +08006587 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006588 key.offset - backref_offset, disk_bytenr))
6589 goto out;
6590
6591 /*
6592 * adjust disk_bytenr and num_bytes to cover just the bytes
6593 * in this extent we are about to write. If there
6594 * are any csums in that range we have to cow in order
6595 * to keep the csums correct
6596 */
6597 disk_bytenr += backref_offset;
6598 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006599 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006600 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6601 goto out;
6602 /*
6603 * all of the above have passed, it is safe to overwrite this extent
6604 * without cow
6605 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006606 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006607 ret = 1;
6608out:
6609 btrfs_free_path(path);
6610 return ret;
6611}
6612
Josef Bacikeb838e72012-07-31 16:28:48 -04006613static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6614 struct extent_state **cached_state, int writing)
6615{
6616 struct btrfs_ordered_extent *ordered;
6617 int ret = 0;
6618
6619 while (1) {
6620 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6621 0, cached_state);
6622 /*
6623 * We're concerned with the entire range that we're going to be
6624 * doing DIO to, so we need to make sure theres no ordered
6625 * extents in this range.
6626 */
6627 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6628 lockend - lockstart + 1);
6629
6630 /*
6631 * We need to make sure there are no buffered pages in this
6632 * range either, we could have raced between the invalidate in
6633 * generic_file_direct_write and locking the extent. The
6634 * invalidate needs to happen so that reads after a write do not
6635 * get stale data.
6636 */
6637 if (!ordered && (!writing ||
6638 !test_range_bit(&BTRFS_I(inode)->io_tree,
6639 lockstart, lockend, EXTENT_UPTODATE, 0,
6640 *cached_state)))
6641 break;
6642
6643 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6644 cached_state, GFP_NOFS);
6645
6646 if (ordered) {
6647 btrfs_start_ordered_extent(inode, ordered, 1);
6648 btrfs_put_ordered_extent(ordered);
6649 } else {
6650 /* Screw you mmap */
6651 ret = filemap_write_and_wait_range(inode->i_mapping,
6652 lockstart,
6653 lockend);
6654 if (ret)
6655 break;
6656
6657 /*
6658 * If we found a page that couldn't be invalidated just
6659 * fall back to buffered.
6660 */
6661 ret = invalidate_inode_pages2_range(inode->i_mapping,
6662 lockstart >> PAGE_CACHE_SHIFT,
6663 lockend >> PAGE_CACHE_SHIFT);
6664 if (ret)
6665 break;
6666 }
6667
6668 cond_resched();
6669 }
6670
6671 return ret;
6672}
6673
Josef Bacik69ffb542012-09-11 15:40:07 -04006674static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6675 u64 len, u64 orig_start,
6676 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006677 u64 orig_block_len, u64 ram_bytes,
6678 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006679{
6680 struct extent_map_tree *em_tree;
6681 struct extent_map *em;
6682 struct btrfs_root *root = BTRFS_I(inode)->root;
6683 int ret;
6684
6685 em_tree = &BTRFS_I(inode)->extent_tree;
6686 em = alloc_extent_map();
6687 if (!em)
6688 return ERR_PTR(-ENOMEM);
6689
6690 em->start = start;
6691 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006692 em->mod_start = start;
6693 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006694 em->len = len;
6695 em->block_len = block_len;
6696 em->block_start = block_start;
6697 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006698 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006699 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006700 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006701 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6702 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006703 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006704
6705 do {
6706 btrfs_drop_extent_cache(inode, em->start,
6707 em->start + em->len - 1, 0);
6708 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006709 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006710 write_unlock(&em_tree->lock);
6711 } while (ret == -EEXIST);
6712
6713 if (ret) {
6714 free_extent_map(em);
6715 return ERR_PTR(ret);
6716 }
6717
6718 return em;
6719}
6720
6721
Josef Bacik4b46fce2010-05-23 11:00:55 -04006722static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6723 struct buffer_head *bh_result, int create)
6724{
6725 struct extent_map *em;
6726 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006727 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006728 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006729 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006730 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006731 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006732 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006733 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006734
Miao Xie172a5042013-02-21 02:48:22 -07006735 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006736 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006737 else
Josef Bacikc3298612012-08-03 16:49:19 -04006738 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006739
Josef Bacikc3298612012-08-03 16:49:19 -04006740 lockstart = start;
6741 lockend = start + len - 1;
6742
Josef Bacikeb838e72012-07-31 16:28:48 -04006743 /*
6744 * If this errors out it's because we couldn't invalidate pagecache for
6745 * this range and we need to fallback to buffered.
6746 */
6747 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6748 return -ENOTBLK;
6749
Josef Bacik4b46fce2010-05-23 11:00:55 -04006750 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006751 if (IS_ERR(em)) {
6752 ret = PTR_ERR(em);
6753 goto unlock_err;
6754 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006755
6756 /*
6757 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6758 * io. INLINE is special, and we could probably kludge it in here, but
6759 * it's still buffered so for safety lets just fall back to the generic
6760 * buffered path.
6761 *
6762 * For COMPRESSED we _have_ to read the entire extent in so we can
6763 * decompress it, so there will be buffering required no matter what we
6764 * do, so go ahead and fallback to buffered.
6765 *
6766 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6767 * to buffered IO. Don't blame me, this is the price we pay for using
6768 * the generic code.
6769 */
6770 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6771 em->block_start == EXTENT_MAP_INLINE) {
6772 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006773 ret = -ENOTBLK;
6774 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775 }
6776
6777 /* Just a good old fashioned hole, return */
6778 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6779 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6780 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006781 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 }
6783
6784 /*
6785 * We don't allocate a new extent in the following cases
6786 *
6787 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6788 * existing extent.
6789 * 2) The extent is marked as PREALLOC. We're good to go here and can
6790 * just use the extent.
6791 *
6792 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006793 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006794 len = min(len, em->len - (start - em->start));
6795 lockstart = start + len;
6796 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006797 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798
6799 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6800 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6801 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006802 int type;
6803 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006804 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805
6806 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6807 type = BTRFS_ORDERED_PREALLOC;
6808 else
6809 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006810 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006811 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006812
6813 /*
6814 * we're not going to log anything, but we do need
6815 * to make sure the current transaction stays open
6816 * while we look for nocow cross refs
6817 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006818 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006819 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006820 goto must_cow;
6821
Josef Bacikeb384b52013-04-24 16:32:55 -04006822 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6823 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006824 if (type == BTRFS_ORDERED_PREALLOC) {
6825 free_extent_map(em);
6826 em = create_pinned_em(inode, start, len,
6827 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006828 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006829 orig_block_len,
6830 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006831 if (IS_ERR(em)) {
6832 btrfs_end_transaction(trans, root);
6833 goto unlock_err;
6834 }
6835 }
6836
Chris Mason46bfbb52010-05-26 11:04:10 -04006837 ret = btrfs_add_ordered_extent_dio(inode, start,
6838 block_start, len, len, type);
6839 btrfs_end_transaction(trans, root);
6840 if (ret) {
6841 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006842 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006843 }
6844 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006845 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006846 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006847 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006848must_cow:
6849 /*
6850 * this will cow the extent, reset the len in case we changed
6851 * it above
6852 */
6853 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006854 free_extent_map(em);
6855 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006856 if (IS_ERR(em)) {
6857 ret = PTR_ERR(em);
6858 goto unlock_err;
6859 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006860 len = min(len, em->len - (start - em->start));
6861unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006862 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6863 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006864 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006865 bh_result->b_bdev = em->bdev;
6866 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006867 if (create) {
6868 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6869 set_buffer_new(bh_result);
6870
6871 /*
6872 * Need to update the i_size under the extent lock so buffered
6873 * readers will get the updated i_size when we unlock.
6874 */
6875 if (start + len > i_size_read(inode))
6876 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006877
Miao Xie172a5042013-02-21 02:48:22 -07006878 spin_lock(&BTRFS_I(inode)->lock);
6879 BTRFS_I(inode)->outstanding_extents++;
6880 spin_unlock(&BTRFS_I(inode)->lock);
6881
Miao Xie09348562013-02-07 10:12:07 +00006882 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6883 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6884 &cached_state, GFP_NOFS);
6885 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006886 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006887
Josef Bacikeb838e72012-07-31 16:28:48 -04006888 /*
6889 * In the case of write we need to clear and unlock the entire range,
6890 * in the case of read we need to unlock only the end area that we
6891 * aren't using if there is any left over space.
6892 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006893 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006894 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6895 lockend, unlock_bits, 1, 0,
6896 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006897 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006898 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006899 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006900
Josef Bacik4b46fce2010-05-23 11:00:55 -04006901 free_extent_map(em);
6902
6903 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006904
6905unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006906 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6907 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6908 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006909}
6910
6911struct btrfs_dio_private {
6912 struct inode *inode;
6913 u64 logical_offset;
6914 u64 disk_bytenr;
6915 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006916 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006917
6918 /* number of bios pending for this dio */
6919 atomic_t pending_bios;
6920
6921 /* IO errors */
6922 int errors;
6923
Chris Mason9be33952013-05-17 18:30:14 -04006924 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006925 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006926
6927 /* dio_bio came from fs/direct-io.c */
6928 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006929};
6930
6931static void btrfs_endio_direct_read(struct bio *bio, int err)
6932{
Miao Xiee65e1532010-11-22 03:04:43 +00006933 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006934 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6935 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006936 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006937 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006938 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006939 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940
6941 start = dip->logical_offset;
6942 do {
6943 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6944 struct page *page = bvec->bv_page;
6945 char *kaddr;
6946 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006947 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006948 unsigned long flags;
6949
Josef Bacikc3298612012-08-03 16:49:19 -04006950 if (get_state_private(&BTRFS_I(inode)->io_tree,
6951 start, &private))
6952 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006953 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006954 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006955 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006956 csum, bvec->bv_len);
6957 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006958 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006959 local_irq_restore(flags);
6960
6961 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006962 if (csum != private) {
6963failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006964 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6965 (unsigned long long)btrfs_ino(inode),
6966 (unsigned long long)start,
6967 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006968 err = -EIO;
6969 }
6970 }
6971
6972 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006973 bvec++;
6974 } while (bvec <= bvec_end);
6975
6976 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006977 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006978 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006979
Josef Bacik4b46fce2010-05-23 11:00:55 -04006980 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006981
6982 /* If we had a csum failure make sure to clear the uptodate flag */
6983 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006984 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6985 dio_end_io(dio_bio, err);
6986 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006987}
6988
6989static void btrfs_endio_direct_write(struct bio *bio, int err)
6990{
6991 struct btrfs_dio_private *dip = bio->bi_private;
6992 struct inode *inode = dip->inode;
6993 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006994 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006995 u64 ordered_offset = dip->logical_offset;
6996 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006997 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006998 int ret;
6999
7000 if (err)
7001 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007002again:
7003 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7004 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007005 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007006 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007007 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007008
Josef Bacik5fd02042012-05-02 14:00:54 -04007009 ordered->work.func = finish_ordered_fn;
7010 ordered->work.flags = 0;
7011 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7012 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007013out_test:
7014 /*
7015 * our bio might span multiple ordered extents. If we haven't
7016 * completed the accounting for the whole dio, go back and try again
7017 */
7018 if (ordered_offset < dip->logical_offset + dip->bytes) {
7019 ordered_bytes = dip->logical_offset + dip->bytes -
7020 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007021 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007022 goto again;
7023 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007024out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007025 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007026
Josef Bacik4b46fce2010-05-23 11:00:55 -04007027 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007028
7029 /* If we had an error make sure to clear the uptodate flag */
7030 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007031 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7032 dio_end_io(dio_bio, err);
7033 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007034}
7035
Chris Masoneaf25d92010-05-25 09:48:28 -04007036static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7037 struct bio *bio, int mirror_num,
7038 unsigned long bio_flags, u64 offset)
7039{
7040 int ret;
7041 struct btrfs_root *root = BTRFS_I(inode)->root;
7042 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007043 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007044 return 0;
7045}
7046
Miao Xiee65e1532010-11-22 03:04:43 +00007047static void btrfs_end_dio_bio(struct bio *bio, int err)
7048{
7049 struct btrfs_dio_private *dip = bio->bi_private;
7050
7051 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007052 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007053 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007054 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007055 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007056 dip->errors = 1;
7057
7058 /*
7059 * before atomic variable goto zero, we must make sure
7060 * dip->errors is perceived to be set.
7061 */
7062 smp_mb__before_atomic_dec();
7063 }
7064
7065 /* if there are more bios still pending for this dio, just exit */
7066 if (!atomic_dec_and_test(&dip->pending_bios))
7067 goto out;
7068
Chris Mason9be33952013-05-17 18:30:14 -04007069 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007070 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007071 } else {
7072 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007073 bio_endio(dip->orig_bio, 0);
7074 }
7075out:
7076 bio_put(bio);
7077}
7078
7079static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7080 u64 first_sector, gfp_t gfp_flags)
7081{
7082 int nr_vecs = bio_get_nr_vecs(bdev);
7083 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7084}
7085
7086static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7087 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007088 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007089{
7090 int write = rw & REQ_WRITE;
7091 struct btrfs_root *root = BTRFS_I(inode)->root;
7092 int ret;
7093
Josef Bacikb812ce22012-11-16 13:56:32 -05007094 if (async_submit)
7095 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7096
Miao Xiee65e1532010-11-22 03:04:43 +00007097 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007098
7099 if (!write) {
7100 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7101 if (ret)
7102 goto err;
7103 }
Miao Xiee65e1532010-11-22 03:04:43 +00007104
Josef Bacik1ae39932011-04-06 14:41:34 -04007105 if (skip_sum)
7106 goto map;
7107
7108 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007109 ret = btrfs_wq_submit_bio(root->fs_info,
7110 inode, rw, bio, 0, 0,
7111 file_offset,
7112 __btrfs_submit_bio_start_direct_io,
7113 __btrfs_submit_bio_done);
7114 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007115 } else if (write) {
7116 /*
7117 * If we aren't doing async submit, calculate the csum of the
7118 * bio now.
7119 */
7120 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7121 if (ret)
7122 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007123 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007124 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007125 if (ret)
7126 goto err;
7127 }
Miao Xiee65e1532010-11-22 03:04:43 +00007128
Josef Bacik1ae39932011-04-06 14:41:34 -04007129map:
7130 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007131err:
7132 bio_put(bio);
7133 return ret;
7134}
7135
7136static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7137 int skip_sum)
7138{
7139 struct inode *inode = dip->inode;
7140 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007141 struct bio *bio;
7142 struct bio *orig_bio = dip->orig_bio;
7143 struct bio_vec *bvec = orig_bio->bi_io_vec;
7144 u64 start_sector = orig_bio->bi_sector;
7145 u64 file_offset = dip->logical_offset;
7146 u64 submit_len = 0;
7147 u64 map_length;
7148 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007149 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007150 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007151
Miao Xiee65e1532010-11-22 03:04:43 +00007152 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007153 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007154 &map_length, NULL, 0);
7155 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007156 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007157 return -EIO;
7158 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007159 if (map_length >= orig_bio->bi_size) {
7160 bio = orig_bio;
7161 goto submit;
7162 }
7163
David Woodhouse53b381b2013-01-29 18:40:14 -05007164 /* async crcs make it difficult to collect full stripe writes. */
7165 if (btrfs_get_alloc_profile(root, 1) &
7166 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7167 async_submit = 0;
7168 else
7169 async_submit = 1;
7170
Josef Bacik02f57c72011-04-06 14:25:44 -04007171 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7172 if (!bio)
7173 return -ENOMEM;
7174 bio->bi_private = dip;
7175 bio->bi_end_io = btrfs_end_dio_bio;
7176 atomic_inc(&dip->pending_bios);
7177
Miao Xiee65e1532010-11-22 03:04:43 +00007178 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7179 if (unlikely(map_length < submit_len + bvec->bv_len ||
7180 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7181 bvec->bv_offset) < bvec->bv_len)) {
7182 /*
7183 * inc the count before we submit the bio so
7184 * we know the end IO handler won't happen before
7185 * we inc the count. Otherwise, the dip might get freed
7186 * before we're done setting it up
7187 */
7188 atomic_inc(&dip->pending_bios);
7189 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7190 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007191 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007192 if (ret) {
7193 bio_put(bio);
7194 atomic_dec(&dip->pending_bios);
7195 goto out_err;
7196 }
7197
Miao Xiee65e1532010-11-22 03:04:43 +00007198 start_sector += submit_len >> 9;
7199 file_offset += submit_len;
7200
7201 submit_len = 0;
7202 nr_pages = 0;
7203
7204 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7205 start_sector, GFP_NOFS);
7206 if (!bio)
7207 goto out_err;
7208 bio->bi_private = dip;
7209 bio->bi_end_io = btrfs_end_dio_bio;
7210
7211 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007212 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007213 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007214 &map_length, NULL, 0);
7215 if (ret) {
7216 bio_put(bio);
7217 goto out_err;
7218 }
7219 } else {
7220 submit_len += bvec->bv_len;
7221 nr_pages ++;
7222 bvec++;
7223 }
7224 }
7225
Josef Bacik02f57c72011-04-06 14:25:44 -04007226submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007227 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007228 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007229 if (!ret)
7230 return 0;
7231
7232 bio_put(bio);
7233out_err:
7234 dip->errors = 1;
7235 /*
7236 * before atomic variable goto zero, we must
7237 * make sure dip->errors is perceived to be set.
7238 */
7239 smp_mb__before_atomic_dec();
7240 if (atomic_dec_and_test(&dip->pending_bios))
7241 bio_io_error(dip->orig_bio);
7242
7243 /* bio_end_io() will handle error, so we needn't return it */
7244 return 0;
7245}
7246
Chris Mason9be33952013-05-17 18:30:14 -04007247static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7248 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249{
7250 struct btrfs_root *root = BTRFS_I(inode)->root;
7251 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007252 struct bio_vec *bvec = dio_bio->bi_io_vec;
7253 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007255 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007256 int ret = 0;
7257
7258 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7259
Chris Mason9be33952013-05-17 18:30:14 -04007260 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7261
7262 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 ret = -ENOMEM;
7264 goto free_ordered;
7265 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266
Chris Mason9be33952013-05-17 18:30:14 -04007267 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7268 if (!dip) {
7269 ret = -ENOMEM;
7270 goto free_io_bio;
7271 }
7272
7273 dip->private = dio_bio->bi_private;
7274 io_bio->bi_private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007275 dip->inode = inode;
7276 dip->logical_offset = file_offset;
7277
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278 dip->bytes = 0;
7279 do {
7280 dip->bytes += bvec->bv_len;
7281 bvec++;
Chris Mason9be33952013-05-17 18:30:14 -04007282 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283
Chris Mason9be33952013-05-17 18:30:14 -04007284 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7285 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007286 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007287 dip->orig_bio = io_bio;
7288 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007289 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007290
7291 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007292 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007293 else
Chris Mason9be33952013-05-17 18:30:14 -04007294 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007295
Miao Xiee65e1532010-11-22 03:04:43 +00007296 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7297 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007298 return;
Chris Mason9be33952013-05-17 18:30:14 -04007299
7300free_io_bio:
7301 bio_put(io_bio);
7302
Josef Bacik4b46fce2010-05-23 11:00:55 -04007303free_ordered:
7304 /*
7305 * If this is a write, we need to clean up the reserved space and kill
7306 * the ordered extent.
7307 */
7308 if (write) {
7309 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007310 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007311 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7312 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7313 btrfs_free_reserved_extent(root, ordered->start,
7314 ordered->disk_len);
7315 btrfs_put_ordered_extent(ordered);
7316 btrfs_put_ordered_extent(ordered);
7317 }
Chris Mason9be33952013-05-17 18:30:14 -04007318 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007319}
7320
Chris Mason5a5f79b2010-05-26 21:33:37 -04007321static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7322 const struct iovec *iov, loff_t offset,
7323 unsigned long nr_segs)
7324{
7325 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007326 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007327 size_t size;
7328 unsigned long addr;
7329 unsigned blocksize_mask = root->sectorsize - 1;
7330 ssize_t retval = -EINVAL;
7331 loff_t end = offset;
7332
7333 if (offset & blocksize_mask)
7334 goto out;
7335
7336 /* Check the memory alignment. Blocks cannot straddle pages */
7337 for (seg = 0; seg < nr_segs; seg++) {
7338 addr = (unsigned long)iov[seg].iov_base;
7339 size = iov[seg].iov_len;
7340 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007341 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007342 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007343
7344 /* If this is a write we don't need to check anymore */
7345 if (rw & WRITE)
7346 continue;
7347
7348 /*
7349 * Check to make sure we don't have duplicate iov_base's in this
7350 * iovec, if so return EINVAL, otherwise we'll get csum errors
7351 * when reading back.
7352 */
7353 for (i = seg + 1; i < nr_segs; i++) {
7354 if (iov[seg].iov_base == iov[i].iov_base)
7355 goto out;
7356 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007357 }
7358 retval = 0;
7359out:
7360 return retval;
7361}
Josef Bacikeb838e72012-07-31 16:28:48 -04007362
Chris Mason16432982008-04-10 10:23:21 -04007363static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7364 const struct iovec *iov, loff_t offset,
7365 unsigned long nr_segs)
7366{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007367 struct file *file = iocb->ki_filp;
7368 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007369 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007370 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007371 bool wakeup = true;
7372 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007373 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007374
Chris Mason5a5f79b2010-05-26 21:33:37 -04007375 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007376 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007377 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007378
Miao Xie38851cc2013-02-08 07:04:11 +00007379 atomic_inc(&inode->i_dio_count);
7380 smp_mb__after_atomic_inc();
7381
Miao Xie09348562013-02-07 10:12:07 +00007382 if (rw & WRITE) {
7383 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007384 /*
7385 * If the write DIO is beyond the EOF, we need update
7386 * the isize, but it is protected by i_mutex. So we can
7387 * not unlock the i_mutex at this case.
7388 */
7389 if (offset + count <= inode->i_size) {
7390 mutex_unlock(&inode->i_mutex);
7391 relock = true;
7392 }
Miao Xie09348562013-02-07 10:12:07 +00007393 ret = btrfs_delalloc_reserve_space(inode, count);
7394 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007395 goto out;
7396 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7397 &BTRFS_I(inode)->runtime_flags))) {
7398 inode_dio_done(inode);
7399 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7400 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007401 }
7402
7403 ret = __blockdev_direct_IO(rw, iocb, inode,
7404 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7405 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007406 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007407 if (rw & WRITE) {
7408 if (ret < 0 && ret != -EIOCBQUEUED)
7409 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007410 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007411 btrfs_delalloc_release_space(inode,
7412 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007413 else
7414 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007415 }
Miao Xie38851cc2013-02-08 07:04:11 +00007416out:
Miao Xie2e60a512013-02-08 07:01:08 +00007417 if (wakeup)
7418 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007419 if (relock)
7420 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007421
7422 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007423}
7424
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007425#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7426
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007427static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7428 __u64 start, __u64 len)
7429{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007430 int ret;
7431
7432 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7433 if (ret)
7434 return ret;
7435
Chris Masonec29ed52011-02-23 16:23:20 -05007436 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007437}
7438
Chris Mason9ebefb182007-06-15 13:50:00 -04007439int btrfs_readpage(struct file *file, struct page *page)
7440{
Chris Masond1310b22008-01-24 16:13:08 -05007441 struct extent_io_tree *tree;
7442 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007443 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007444}
Chris Mason1832a6d2007-12-21 16:27:21 -05007445
Chris Mason39279cc2007-06-12 06:35:45 -04007446static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7447{
Chris Masond1310b22008-01-24 16:13:08 -05007448 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007449
7450
7451 if (current->flags & PF_MEMALLOC) {
7452 redirty_page_for_writepage(wbc, page);
7453 unlock_page(page);
7454 return 0;
7455 }
Chris Masond1310b22008-01-24 16:13:08 -05007456 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007457 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7458}
Chris Mason39279cc2007-06-12 06:35:45 -04007459
Eric Sandeen48a3b632013-04-25 20:41:01 +00007460static int btrfs_writepages(struct address_space *mapping,
7461 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007462{
Chris Masond1310b22008-01-24 16:13:08 -05007463 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007464
Chris Masond1310b22008-01-24 16:13:08 -05007465 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007466 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7467}
7468
Chris Mason3ab2fb52007-11-08 10:59:22 -05007469static int
7470btrfs_readpages(struct file *file, struct address_space *mapping,
7471 struct list_head *pages, unsigned nr_pages)
7472{
Chris Masond1310b22008-01-24 16:13:08 -05007473 struct extent_io_tree *tree;
7474 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007475 return extent_readpages(tree, mapping, pages, nr_pages,
7476 btrfs_get_extent);
7477}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007478static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007479{
Chris Masond1310b22008-01-24 16:13:08 -05007480 struct extent_io_tree *tree;
7481 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007482 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007483
Chris Masond1310b22008-01-24 16:13:08 -05007484 tree = &BTRFS_I(page->mapping->host)->io_tree;
7485 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007486 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007487 if (ret == 1) {
7488 ClearPagePrivate(page);
7489 set_page_private(page, 0);
7490 page_cache_release(page);
7491 }
7492 return ret;
7493}
Chris Mason39279cc2007-06-12 06:35:45 -04007494
Chris Masone6dcd2d2008-07-17 12:53:50 -04007495static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7496{
Chris Mason98509cf2008-09-11 15:51:43 -04007497 if (PageWriteback(page) || PageDirty(page))
7498 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007499 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007500}
7501
Chris Masona52d9a82007-08-27 16:49:44 -04007502static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7503{
Josef Bacik5fd02042012-05-02 14:00:54 -04007504 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007505 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007507 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007508 u64 page_start = page_offset(page);
7509 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007510
Chris Mason8b62b722009-09-02 16:53:46 -04007511 /*
7512 * we have the page locked, so new writeback can't start,
7513 * and the dirty bit won't be cleared while we are here.
7514 *
7515 * Wait for IO on this page so that we can safely clear
7516 * the PagePrivate2 bit and do ordered accounting
7517 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007518 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007519
Josef Bacik5fd02042012-05-02 14:00:54 -04007520 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007521 if (offset) {
7522 btrfs_releasepage(page, GFP_NOFS);
7523 return;
7524 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007525 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007526 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007527 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007528 /*
7529 * IO on this page will never be started, so we need
7530 * to account for any ordered extents now
7531 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007532 clear_extent_bit(tree, page_start, page_end,
7533 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007534 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7535 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007536 /*
7537 * whoever cleared the private bit is responsible
7538 * for the finish_ordered_io
7539 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007540 if (TestClearPagePrivate2(page) &&
7541 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7542 PAGE_CACHE_SIZE, 1)) {
7543 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007544 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007545 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007546 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007547 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007548 }
7549 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007550 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007551 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7552 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007553 __btrfs_releasepage(page, GFP_NOFS);
7554
Chris Mason4a096752008-07-21 10:29:44 -04007555 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007556 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007557 ClearPagePrivate(page);
7558 set_page_private(page, 0);
7559 page_cache_release(page);
7560 }
Chris Mason39279cc2007-06-12 06:35:45 -04007561}
7562
Chris Mason9ebefb182007-06-15 13:50:00 -04007563/*
7564 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7565 * called from a page fault handler when a page is first dirtied. Hence we must
7566 * be careful to check for EOF conditions here. We set the page up correctly
7567 * for a written page which means we get ENOSPC checking when writing into
7568 * holes and correct delalloc and unwritten extent mapping on filesystems that
7569 * support these features.
7570 *
7571 * We are not allowed to take the i_mutex here so we have to play games to
7572 * protect against truncate races as the page could now be beyond EOF. Because
7573 * vmtruncate() writes the inode size before removing pages, once we have the
7574 * page lock we can determine safely if the page is beyond EOF. If it is not
7575 * beyond EOF, then the page is guaranteed safe against truncation until we
7576 * unlock the page.
7577 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007578int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007579{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007580 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007581 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007582 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7584 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007585 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007586 char *kaddr;
7587 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007588 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007589 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007590 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007591 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007592 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007593
Jan Karab2b5ef52012-06-12 16:20:45 +02007594 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007595 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007596 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007597 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007598 reserved = 1;
7599 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007600 if (ret) {
7601 if (ret == -ENOMEM)
7602 ret = VM_FAULT_OOM;
7603 else /* -ENOSPC, -EIO, etc */
7604 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007605 if (reserved)
7606 goto out;
7607 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007608 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007609
Nick Piggin56a76f82009-03-31 15:23:23 -07007610 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007611again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007612 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007613 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007614 page_start = page_offset(page);
7615 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007616
Chris Mason9ebefb182007-06-15 13:50:00 -04007617 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007618 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007619 /* page got truncated out from underneath us */
7620 goto out_unlock;
7621 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007622 wait_on_page_writeback(page);
7623
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007624 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007625 set_page_extent_mapped(page);
7626
Chris Masoneb84ae02008-07-17 13:53:27 -04007627 /*
7628 * we can't set the delalloc bits if there are pending ordered
7629 * extents. Drop our locks and wait for them to finish
7630 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007631 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7632 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007633 unlock_extent_cached(io_tree, page_start, page_end,
7634 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007635 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007636 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007637 btrfs_put_ordered_extent(ordered);
7638 goto again;
7639 }
7640
Josef Bacikfbf19082009-10-01 17:10:23 -04007641 /*
7642 * XXX - page_mkwrite gets called every time the page is dirtied, even
7643 * if it was already dirty, so for space accounting reasons we need to
7644 * clear any delalloc bits for the range we are fixing to save. There
7645 * is probably a better way to do this, but for now keep consistent with
7646 * prepare_pages in the normal write path.
7647 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007648 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007649 EXTENT_DIRTY | EXTENT_DELALLOC |
7650 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007651 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007652
Josef Bacik2ac55d42010-02-03 19:33:23 +00007653 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7654 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007655 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007656 unlock_extent_cached(io_tree, page_start, page_end,
7657 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007658 ret = VM_FAULT_SIGBUS;
7659 goto out_unlock;
7660 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007661 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007662
7663 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007664 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007665 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007666 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007667 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007668
Chris Masone6dcd2d2008-07-17 12:53:50 -04007669 if (zero_start != PAGE_CACHE_SIZE) {
7670 kaddr = kmap(page);
7671 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7672 flush_dcache_page(page);
7673 kunmap(page);
7674 }
Chris Mason247e7432008-07-17 12:53:51 -04007675 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007676 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007677 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007678
Chris Mason257c62e2009-10-13 13:21:08 -04007679 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7680 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007681 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007682
Josef Bacik2ac55d42010-02-03 19:33:23 +00007683 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007684
7685out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007686 if (!ret) {
7687 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007688 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007689 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007690 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007691out:
Josef Bacikec39e182012-01-12 19:10:12 -05007692 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007693out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007694 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007695 return ret;
7696}
7697
Josef Bacika41ad392011-01-31 15:30:16 -05007698static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007699{
7700 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007701 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007702 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007703 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007704 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007705 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007706 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007707
Josef Bacik2aaa6652012-08-29 14:27:18 -04007708 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007709 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007710 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007711
Chris Mason4a096752008-07-21 10:29:44 -04007712 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007713 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007714
Josef Bacikfcb80c22011-05-03 10:40:22 -04007715 /*
7716 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7717 * 3 things going on here
7718 *
7719 * 1) We need to reserve space for our orphan item and the space to
7720 * delete our orphan item. Lord knows we don't want to have a dangling
7721 * orphan item because we didn't reserve space to remove it.
7722 *
7723 * 2) We need to reserve space to update our inode.
7724 *
7725 * 3) We need to have something to cache all the space that is going to
7726 * be free'd up by the truncate operation, but also have some slack
7727 * space reserved in case it uses space during the truncate (thank you
7728 * very much snapshotting).
7729 *
7730 * And we need these to all be seperate. The fact is we can use alot of
7731 * space doing the truncate, and we have no earthly idea how much space
7732 * we will use, so we need the truncate reservation to be seperate so it
7733 * doesn't end up using space reserved for updating the inode or
7734 * removing the orphan item. We also need to be able to stop the
7735 * transaction and start a new one, which means we need to be able to
7736 * update the inode several times, and we have no idea of knowing how
7737 * many times that will be, so we can't just reserve 1 item for the
7738 * entirety of the opration, so that has to be done seperately as well.
7739 * Then there is the orphan item, which does indeed need to be held on
7740 * to for the whole operation, and we need nobody to touch this reserved
7741 * space except the orphan code.
7742 *
7743 * So that leaves us with
7744 *
7745 * 1) root->orphan_block_rsv - for the orphan deletion.
7746 * 2) rsv - for the truncate reservation, which we will steal from the
7747 * transaction reservation.
7748 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7749 * updating the inode.
7750 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007751 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007752 if (!rsv)
7753 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007754 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007755 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007756
Josef Bacik907cbce2011-08-08 13:46:15 -04007757 /*
Josef Bacik07127182011-08-19 10:29:59 -04007758 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007759 * 1 for updating the inode.
7760 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007761 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007762 if (IS_ERR(trans)) {
7763 err = PTR_ERR(trans);
7764 goto out;
7765 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007766
Josef Bacik907cbce2011-08-08 13:46:15 -04007767 /* Migrate the slack space for the truncate to our reserve */
7768 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7769 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007770 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007771
Chris Mason5a3f23d2009-03-31 13:27:11 -04007772 /*
7773 * setattr is responsible for setting the ordered_data_close flag,
7774 * but that is only tested during the last file release. That
7775 * could happen well after the next commit, leaving a great big
7776 * window where new writes may get lost if someone chooses to write
7777 * to this file after truncating to zero
7778 *
7779 * The inode doesn't have any dirty data here, and so if we commit
7780 * this is a noop. If someone immediately starts writing to the inode
7781 * it is very likely we'll catch some of their writes in this
7782 * transaction, and the commit will find this file on the ordered
7783 * data list with good things to send down.
7784 *
7785 * This is a best effort solution, there is still a window where
7786 * using truncate to replace the contents of the file will
7787 * end up with a zero length file after a crash.
7788 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007789 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7790 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007791 btrfs_add_ordered_operation(trans, root, inode);
7792
Josef Bacik5dc562c2012-08-17 13:14:17 -04007793 /*
7794 * So if we truncate and then write and fsync we normally would just
7795 * write the extents that changed, which is a problem if we need to
7796 * first truncate that entire inode. So set this flag so we write out
7797 * all of the extents in the inode to the sync log so we're completely
7798 * safe.
7799 */
7800 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007801 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007802
Yan, Zheng80825102009-11-12 09:35:36 +00007803 while (1) {
7804 ret = btrfs_truncate_inode_items(trans, root, inode,
7805 inode->i_size,
7806 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007807 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007808 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007809 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007810 }
Chris Mason39279cc2007-06-12 06:35:45 -04007811
Josef Bacikfcb80c22011-05-03 10:40:22 -04007812 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007813 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007814 if (ret) {
7815 err = ret;
7816 break;
7817 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007818
Yan, Zheng80825102009-11-12 09:35:36 +00007819 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007820 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007821
7822 trans = btrfs_start_transaction(root, 2);
7823 if (IS_ERR(trans)) {
7824 ret = err = PTR_ERR(trans);
7825 trans = NULL;
7826 break;
7827 }
7828
7829 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7830 rsv, min_size);
7831 BUG_ON(ret); /* shouldn't happen */
7832 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007833 }
7834
7835 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007836 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007837 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007838 if (ret)
7839 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007840 }
7841
Chris Mason917c16b2011-11-08 14:49:59 -05007842 if (trans) {
7843 trans->block_rsv = &root->fs_info->trans_block_rsv;
7844 ret = btrfs_update_inode(trans, root, inode);
7845 if (ret && !err)
7846 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007847
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007848 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007849 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007850 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007851
7852out:
7853 btrfs_free_block_rsv(root, rsv);
7854
Josef Bacik3893e332011-01-31 16:03:11 -05007855 if (ret && !err)
7856 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007857
Josef Bacik3893e332011-01-31 16:03:11 -05007858 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007859}
7860
Sven Wegener3b963622008-06-09 21:57:42 -04007861/*
Chris Masond352ac62008-09-29 15:18:18 -04007862 * create a new subvolume directory/inode (helper for the ioctl).
7863 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007864int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007865 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007866{
Chris Mason39279cc2007-06-12 06:35:45 -04007867 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007868 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007869 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007870
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007871 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7872 new_dirid, new_dirid,
7873 S_IFDIR | (~current_umask() & S_IRWXUGO),
7874 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007875 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007876 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007877 inode->i_op = &btrfs_dir_inode_operations;
7878 inode->i_fop = &btrfs_dir_file_operations;
7879
Miklos Szeredibfe86842011-10-28 14:13:29 +02007880 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007881 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007882
Yan, Zheng76dda932009-09-21 16:00:26 -04007883 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007884
Yan, Zheng76dda932009-09-21 16:00:26 -04007885 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007886 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007887}
7888
Chris Mason39279cc2007-06-12 06:35:45 -04007889struct inode *btrfs_alloc_inode(struct super_block *sb)
7890{
7891 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007892 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007893
7894 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7895 if (!ei)
7896 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007897
7898 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007899 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007900 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007901 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007902 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007903 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007904 ei->disk_i_size = 0;
7905 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007906 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007907 ei->index_cnt = (u64)-1;
7908 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007909 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007910
Josef Bacik9e0baf62011-07-15 15:16:44 +00007911 spin_lock_init(&ei->lock);
7912 ei->outstanding_extents = 0;
7913 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007914
Josef Bacik72ac3c02012-05-23 14:13:11 -04007915 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007916 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007917
Miao Xie16cdcec2011-04-22 18:12:22 +08007918 ei->delayed_node = NULL;
7919
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007920 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007921 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007922 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7923 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007924 ei->io_tree.track_uptodate = 1;
7925 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007926 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007927 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007928 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007929 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007930 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007931 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007932 RB_CLEAR_NODE(&ei->rb_node);
7933
7934 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007935}
7936
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007937static void btrfs_i_callback(struct rcu_head *head)
7938{
7939 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007940 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7941}
7942
Chris Mason39279cc2007-06-12 06:35:45 -04007943void btrfs_destroy_inode(struct inode *inode)
7944{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007945 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007946 struct btrfs_root *root = BTRFS_I(inode)->root;
7947
Al Virob3d9b7a2012-06-09 13:51:19 -04007948 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007949 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007950 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7951 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007952 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7953 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007954
Chris Mason5a3f23d2009-03-31 13:27:11 -04007955 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007956 * This can happen where we create an inode, but somebody else also
7957 * created the same inode and we need to destroy the one we already
7958 * created.
7959 */
7960 if (!root)
7961 goto free;
7962
7963 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007964 * Make sure we're properly removed from the ordered operation
7965 * lists.
7966 */
7967 smp_mb();
7968 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7969 spin_lock(&root->fs_info->ordered_extent_lock);
7970 list_del_init(&BTRFS_I(inode)->ordered_operations);
7971 spin_unlock(&root->fs_info->ordered_extent_lock);
7972 }
7973
Josef Bacik8a35d952012-05-23 14:26:42 -04007974 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7975 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007976 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7977 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007978 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007979 }
Josef Bacik7b128762008-07-24 12:17:14 -04007980
Chris Masond3977122009-01-05 21:25:51 -05007981 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007982 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7983 if (!ordered)
7984 break;
7985 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007986 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7987 (unsigned long long)ordered->file_offset,
7988 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007989 btrfs_remove_ordered_extent(inode, ordered);
7990 btrfs_put_ordered_extent(ordered);
7991 btrfs_put_ordered_extent(ordered);
7992 }
7993 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007994 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007995 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007996free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007997 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007998}
7999
Al Viro45321ac2010-06-07 13:43:19 -04008000int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008001{
8002 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008003
Naohiro Aota6379ef92013-06-06 09:56:34 +00008004 if (root == NULL)
8005 return 1;
8006
Liu Bofa6ac872013-02-20 14:10:23 +00008007 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04008008 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00008009 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04008010 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008011 else
Al Viro45321ac2010-06-07 13:43:19 -04008012 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008013}
8014
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008015static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008016{
8017 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8018
8019 inode_init_once(&ei->vfs_inode);
8020}
8021
8022void btrfs_destroy_cachep(void)
8023{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008024 /*
8025 * Make sure all delayed rcu free inodes are flushed before we
8026 * destroy cache.
8027 */
8028 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008029 if (btrfs_inode_cachep)
8030 kmem_cache_destroy(btrfs_inode_cachep);
8031 if (btrfs_trans_handle_cachep)
8032 kmem_cache_destroy(btrfs_trans_handle_cachep);
8033 if (btrfs_transaction_cachep)
8034 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008035 if (btrfs_path_cachep)
8036 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008037 if (btrfs_free_space_cachep)
8038 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008039 if (btrfs_delalloc_work_cachep)
8040 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008041}
8042
8043int btrfs_init_cachep(void)
8044{
David Sterba837e1972012-09-07 03:00:48 -06008045 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008046 sizeof(struct btrfs_inode), 0,
8047 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008048 if (!btrfs_inode_cachep)
8049 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008050
David Sterba837e1972012-09-07 03:00:48 -06008051 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008052 sizeof(struct btrfs_trans_handle), 0,
8053 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008054 if (!btrfs_trans_handle_cachep)
8055 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008056
David Sterba837e1972012-09-07 03:00:48 -06008057 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008058 sizeof(struct btrfs_transaction), 0,
8059 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008060 if (!btrfs_transaction_cachep)
8061 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008062
David Sterba837e1972012-09-07 03:00:48 -06008063 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008064 sizeof(struct btrfs_path), 0,
8065 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008066 if (!btrfs_path_cachep)
8067 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008068
David Sterba837e1972012-09-07 03:00:48 -06008069 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008070 sizeof(struct btrfs_free_space), 0,
8071 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8072 if (!btrfs_free_space_cachep)
8073 goto fail;
8074
Miao Xie8ccf6f192012-10-25 09:28:04 +00008075 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8076 sizeof(struct btrfs_delalloc_work), 0,
8077 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8078 NULL);
8079 if (!btrfs_delalloc_work_cachep)
8080 goto fail;
8081
Chris Mason39279cc2007-06-12 06:35:45 -04008082 return 0;
8083fail:
8084 btrfs_destroy_cachep();
8085 return -ENOMEM;
8086}
8087
8088static int btrfs_getattr(struct vfsmount *mnt,
8089 struct dentry *dentry, struct kstat *stat)
8090{
Miao Xiedf0af1a52013-01-29 10:11:59 +00008091 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008092 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008093 u32 blocksize = inode->i_sb->s_blocksize;
8094
Chris Mason39279cc2007-06-12 06:35:45 -04008095 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008096 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008097 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008098
8099 spin_lock(&BTRFS_I(inode)->lock);
8100 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8101 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008102 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00008103 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008104 return 0;
8105}
8106
Chris Masond3977122009-01-05 21:25:51 -05008107static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8108 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008109{
8110 struct btrfs_trans_handle *trans;
8111 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008112 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008113 struct inode *new_inode = new_dentry->d_inode;
8114 struct inode *old_inode = old_dentry->d_inode;
8115 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008116 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008117 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008118 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008119 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008120
Li Zefan33345d012011-04-20 10:31:50 +08008121 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008122 return -EPERM;
8123
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008124 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008125 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008126 return -EXDEV;
8127
Li Zefan33345d012011-04-20 10:31:50 +08008128 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8129 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008130 return -ENOTEMPTY;
8131
Chris Mason39279cc2007-06-12 06:35:45 -04008132 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008133 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008134 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008135
8136
8137 /* check for collisions, even if the name isn't there */
8138 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8139 new_dentry->d_name.name,
8140 new_dentry->d_name.len);
8141
8142 if (ret) {
8143 if (ret == -EEXIST) {
8144 /* we shouldn't get
8145 * eexist without a new_inode */
8146 if (!new_inode) {
8147 WARN_ON(1);
8148 return ret;
8149 }
8150 } else {
8151 /* maybe -EOVERFLOW */
8152 return ret;
8153 }
8154 }
8155 ret = 0;
8156
Chris Mason5a3f23d2009-03-31 13:27:11 -04008157 /*
8158 * we're using rename to replace one file with another.
8159 * and the replacement file is large. Start IO on it now so
8160 * we don't add too much work to the end of the transaction
8161 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008162 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008163 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8164 filemap_flush(old_inode->i_mapping);
8165
Yan, Zheng76dda932009-09-21 16:00:26 -04008166 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008167 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008168 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008169 /*
8170 * We want to reserve the absolute worst case amount of items. So if
8171 * both inodes are subvols and we need to unlink them then that would
8172 * require 4 item modifications, but if they are both normal inodes it
8173 * would require 5 item modifications, so we'll assume their normal
8174 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8175 * should cover the worst case number of items we'll modify.
8176 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008177 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008178 if (IS_ERR(trans)) {
8179 ret = PTR_ERR(trans);
8180 goto out_notrans;
8181 }
Chris Mason5f39d392007-10-15 16:14:19 -04008182
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008183 if (dest != root)
8184 btrfs_record_root_in_trans(trans, dest);
8185
Yan, Zhenga5719522009-09-24 09:17:31 -04008186 ret = btrfs_set_inode_index(new_dir, &index);
8187 if (ret)
8188 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008189
Li Zefan33345d012011-04-20 10:31:50 +08008190 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008191 /* force full log commit if subvolume involved. */
8192 root->fs_info->last_trans_log_full_commit = trans->transid;
8193 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008194 ret = btrfs_insert_inode_ref(trans, dest,
8195 new_dentry->d_name.name,
8196 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008197 old_ino,
8198 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008199 if (ret)
8200 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008201 /*
8202 * this is an ugly little race, but the rename is required
8203 * to make sure that if we crash, the inode is either at the
8204 * old name or the new one. pinning the log transaction lets
8205 * us make sure we don't allow a log commit to come in after
8206 * we unlink the name but before we add the new name back in.
8207 */
8208 btrfs_pin_log_trans(root);
8209 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008210 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008211 * make sure the inode gets flushed if it is replacing
8212 * something.
8213 */
Li Zefan33345d012011-04-20 10:31:50 +08008214 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008215 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008216
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008217 inode_inc_iversion(old_dir);
8218 inode_inc_iversion(new_dir);
8219 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008220 old_dir->i_ctime = old_dir->i_mtime = ctime;
8221 new_dir->i_ctime = new_dir->i_mtime = ctime;
8222 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008223
Chris Mason12fcfd22009-03-24 10:24:20 -04008224 if (old_dentry->d_parent != new_dentry->d_parent)
8225 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8226
Li Zefan33345d012011-04-20 10:31:50 +08008227 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008228 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8229 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8230 old_dentry->d_name.name,
8231 old_dentry->d_name.len);
8232 } else {
Al Viro92986792011-03-04 17:14:37 +00008233 ret = __btrfs_unlink_inode(trans, root, old_dir,
8234 old_dentry->d_inode,
8235 old_dentry->d_name.name,
8236 old_dentry->d_name.len);
8237 if (!ret)
8238 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008239 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008240 if (ret) {
8241 btrfs_abort_transaction(trans, root, ret);
8242 goto out_fail;
8243 }
Chris Mason39279cc2007-06-12 06:35:45 -04008244
8245 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008246 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008247 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008248 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008249 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8250 root_objectid = BTRFS_I(new_inode)->location.objectid;
8251 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8252 root_objectid,
8253 new_dentry->d_name.name,
8254 new_dentry->d_name.len);
8255 BUG_ON(new_inode->i_nlink == 0);
8256 } else {
8257 ret = btrfs_unlink_inode(trans, dest, new_dir,
8258 new_dentry->d_inode,
8259 new_dentry->d_name.name,
8260 new_dentry->d_name.len);
8261 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008262 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008263 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008264 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008265 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008266 if (ret) {
8267 btrfs_abort_transaction(trans, root, ret);
8268 goto out_fail;
8269 }
Chris Mason39279cc2007-06-12 06:35:45 -04008270 }
Josef Bacikaec74772008-07-24 12:12:38 -04008271
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008272 ret = btrfs_add_link(trans, new_dir, old_inode,
8273 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008274 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008275 if (ret) {
8276 btrfs_abort_transaction(trans, root, ret);
8277 goto out_fail;
8278 }
Chris Mason39279cc2007-06-12 06:35:45 -04008279
Li Zefan33345d012011-04-20 10:31:50 +08008280 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008281 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008282 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008283 btrfs_end_log_trans(root);
8284 }
Chris Mason39279cc2007-06-12 06:35:45 -04008285out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008286 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008287out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008288 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008289 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008290
Chris Mason39279cc2007-06-12 06:35:45 -04008291 return ret;
8292}
8293
Miao Xie8ccf6f192012-10-25 09:28:04 +00008294static void btrfs_run_delalloc_work(struct btrfs_work *work)
8295{
8296 struct btrfs_delalloc_work *delalloc_work;
8297
8298 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8299 work);
8300 if (delalloc_work->wait)
8301 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8302 else
8303 filemap_flush(delalloc_work->inode->i_mapping);
8304
8305 if (delalloc_work->delay_iput)
8306 btrfs_add_delayed_iput(delalloc_work->inode);
8307 else
8308 iput(delalloc_work->inode);
8309 complete(&delalloc_work->completion);
8310}
8311
8312struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8313 int wait, int delay_iput)
8314{
8315 struct btrfs_delalloc_work *work;
8316
8317 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8318 if (!work)
8319 return NULL;
8320
8321 init_completion(&work->completion);
8322 INIT_LIST_HEAD(&work->list);
8323 work->inode = inode;
8324 work->wait = wait;
8325 work->delay_iput = delay_iput;
8326 work->work.func = btrfs_run_delalloc_work;
8327
8328 return work;
8329}
8330
8331void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8332{
8333 wait_for_completion(&work->completion);
8334 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8335}
8336
Chris Masond352ac62008-09-29 15:18:18 -04008337/*
8338 * some fairly slow code that needs optimization. This walks the list
8339 * of all the inodes with pending delalloc and forces them to disk.
8340 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008341int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008342{
Chris Masonea8c2812008-08-04 23:17:27 -04008343 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008344 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008345 struct btrfs_delalloc_work *work, *next;
8346 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008347 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008348 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008349
Yan Zhengc146afa2008-11-12 14:34:12 -05008350 if (root->fs_info->sb->s_flags & MS_RDONLY)
8351 return -EROFS;
8352
Miao Xie8ccf6f192012-10-25 09:28:04 +00008353 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008354 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008355
Chris Mason75eff682008-12-15 15:54:40 -05008356 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008357 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8358 while (!list_empty(&splice)) {
8359 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008360 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008361
8362 list_del_init(&binode->delalloc_inodes);
8363
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008364 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00008365 if (!inode) {
8366 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8367 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008368 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008369 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008370
8371 list_add_tail(&binode->delalloc_inodes,
8372 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008373 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008374
8375 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8376 if (unlikely(!work)) {
8377 ret = -ENOMEM;
8378 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008379 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008380 list_add_tail(&work->list, &works);
8381 btrfs_queue_worker(&root->fs_info->flush_workers,
8382 &work->work);
8383
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008384 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008385 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008386 }
Chris Mason75eff682008-12-15 15:54:40 -05008387 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008388
Miao Xie1eafa6c2013-01-22 10:49:00 +00008389 list_for_each_entry_safe(work, next, &works, list) {
8390 list_del_init(&work->list);
8391 btrfs_wait_and_free_delalloc_work(work);
8392 }
8393
Chris Mason8c8bee12008-09-29 11:19:10 -04008394 /* the filemap_flush will queue IO into the worker threads, but
8395 * we have to make sure the IO is actually started and that
8396 * ordered extents get created before we return
8397 */
8398 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008399 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008400 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008401 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008402 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8403 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008404 }
8405 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008406 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008407out:
8408 list_for_each_entry_safe(work, next, &works, list) {
8409 list_del_init(&work->list);
8410 btrfs_wait_and_free_delalloc_work(work);
8411 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008412
8413 if (!list_empty_careful(&splice)) {
8414 spin_lock(&root->fs_info->delalloc_lock);
8415 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8416 spin_unlock(&root->fs_info->delalloc_lock);
8417 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008418 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008419}
8420
Chris Mason39279cc2007-06-12 06:35:45 -04008421static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8422 const char *symname)
8423{
8424 struct btrfs_trans_handle *trans;
8425 struct btrfs_root *root = BTRFS_I(dir)->root;
8426 struct btrfs_path *path;
8427 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008428 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008429 int err;
8430 int drop_inode = 0;
8431 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008432 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008433 int name_len;
8434 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008435 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008436 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008437 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008438
8439 name_len = strlen(symname) + 1;
8440 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8441 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008442
Josef Bacik9ed74f22009-09-11 16:12:44 -04008443 /*
8444 * 2 items for inode item and ref
8445 * 2 items for dir items
8446 * 1 item for xattr if selinux is on
8447 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008448 trans = btrfs_start_transaction(root, 5);
8449 if (IS_ERR(trans))
8450 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008451
Li Zefan581bb052011-04-20 10:06:11 +08008452 err = btrfs_find_free_ino(root, &objectid);
8453 if (err)
8454 goto out_unlock;
8455
Josef Bacikaec74772008-07-24 12:12:38 -04008456 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008457 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008458 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008459 if (IS_ERR(inode)) {
8460 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008461 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008462 }
Chris Mason39279cc2007-06-12 06:35:45 -04008463
Eric Paris2a7dba32011-02-01 11:05:39 -05008464 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008465 if (err) {
8466 drop_inode = 1;
8467 goto out_unlock;
8468 }
8469
Casey Schauflerad19db72011-12-15 10:09:07 -05008470 /*
8471 * If the active LSM wants to access the inode during
8472 * d_instantiate it needs these. Smack checks to see
8473 * if the filesystem supports xattrs by looking at the
8474 * ops vector.
8475 */
8476 inode->i_fop = &btrfs_file_operations;
8477 inode->i_op = &btrfs_file_inode_operations;
8478
Josef Bacika1b075d2010-11-19 20:36:11 +00008479 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008480 if (err)
8481 drop_inode = 1;
8482 else {
8483 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008484 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008485 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008486 }
Chris Mason39279cc2007-06-12 06:35:45 -04008487 if (drop_inode)
8488 goto out_unlock;
8489
8490 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008491 if (!path) {
8492 err = -ENOMEM;
8493 drop_inode = 1;
8494 goto out_unlock;
8495 }
Li Zefan33345d012011-04-20 10:31:50 +08008496 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008497 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008498 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8499 datasize = btrfs_file_extent_calc_inline_size(name_len);
8500 err = btrfs_insert_empty_item(trans, root, path, &key,
8501 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008502 if (err) {
8503 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008504 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008505 goto out_unlock;
8506 }
Chris Mason5f39d392007-10-15 16:14:19 -04008507 leaf = path->nodes[0];
8508 ei = btrfs_item_ptr(leaf, path->slots[0],
8509 struct btrfs_file_extent_item);
8510 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8511 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008512 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008513 btrfs_set_file_extent_encryption(leaf, ei, 0);
8514 btrfs_set_file_extent_compression(leaf, ei, 0);
8515 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8516 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8517
Chris Mason39279cc2007-06-12 06:35:45 -04008518 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008519 write_extent_buffer(leaf, symname, ptr, name_len);
8520 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008521 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008522
Chris Mason39279cc2007-06-12 06:35:45 -04008523 inode->i_op = &btrfs_symlink_inode_operations;
8524 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008525 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008526 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008527 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008528 err = btrfs_update_inode(trans, root, inode);
8529 if (err)
8530 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008531
8532out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008533 if (!err)
8534 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008535 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008536 if (drop_inode) {
8537 inode_dec_link_count(inode);
8538 iput(inode);
8539 }
Liu Bob53d3f52012-11-14 14:34:34 +00008540 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008541 return err;
8542}
Chris Mason16432982008-04-10 10:23:21 -04008543
Josef Bacik0af3d002010-06-21 14:48:16 -04008544static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8545 u64 start, u64 num_bytes, u64 min_size,
8546 loff_t actual_len, u64 *alloc_hint,
8547 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008548{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008549 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8550 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008551 struct btrfs_root *root = BTRFS_I(inode)->root;
8552 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008553 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008554 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008555 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008556 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008557 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008558
Josef Bacik0af3d002010-06-21 14:48:16 -04008559 if (trans)
8560 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008561 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008562 if (own_trans) {
8563 trans = btrfs_start_transaction(root, 3);
8564 if (IS_ERR(trans)) {
8565 ret = PTR_ERR(trans);
8566 break;
8567 }
Yan Zhengd899e052008-10-30 14:25:28 -04008568 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008569
Chris Mason154ea282013-03-05 11:11:26 -05008570 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8571 cur_bytes = max(cur_bytes, min_size);
8572 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008573 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008574 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008575 if (own_trans)
8576 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008577 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008578 }
8579
Yan Zhengd899e052008-10-30 14:25:28 -04008580 ret = insert_reserved_file_extent(trans, inode,
8581 cur_offset, ins.objectid,
8582 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008583 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008584 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008585 if (ret) {
8586 btrfs_abort_transaction(trans, root, ret);
8587 if (own_trans)
8588 btrfs_end_transaction(trans, root);
8589 break;
8590 }
Chris Masona1ed8352009-09-11 12:27:37 -04008591 btrfs_drop_extent_cache(inode, cur_offset,
8592 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008593
Josef Bacik5dc562c2012-08-17 13:14:17 -04008594 em = alloc_extent_map();
8595 if (!em) {
8596 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8597 &BTRFS_I(inode)->runtime_flags);
8598 goto next;
8599 }
8600
8601 em->start = cur_offset;
8602 em->orig_start = cur_offset;
8603 em->len = ins.offset;
8604 em->block_start = ins.objectid;
8605 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008606 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008607 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008608 em->bdev = root->fs_info->fs_devices->latest_bdev;
8609 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8610 em->generation = trans->transid;
8611
8612 while (1) {
8613 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008614 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008615 write_unlock(&em_tree->lock);
8616 if (ret != -EEXIST)
8617 break;
8618 btrfs_drop_extent_cache(inode, cur_offset,
8619 cur_offset + ins.offset - 1,
8620 0);
8621 }
8622 free_extent_map(em);
8623next:
Yan Zhengd899e052008-10-30 14:25:28 -04008624 num_bytes -= ins.offset;
8625 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008626 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008627
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008628 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008629 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008630 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008631 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008632 (actual_len > inode->i_size) &&
8633 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008634 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008635 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008636 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008637 i_size = cur_offset;
8638 i_size_write(inode, i_size);
8639 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008640 }
8641
Yan Zhengd899e052008-10-30 14:25:28 -04008642 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008643
8644 if (ret) {
8645 btrfs_abort_transaction(trans, root, ret);
8646 if (own_trans)
8647 btrfs_end_transaction(trans, root);
8648 break;
8649 }
Yan Zhengd899e052008-10-30 14:25:28 -04008650
Josef Bacik0af3d002010-06-21 14:48:16 -04008651 if (own_trans)
8652 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008653 }
Yan Zhengd899e052008-10-30 14:25:28 -04008654 return ret;
8655}
8656
Josef Bacik0af3d002010-06-21 14:48:16 -04008657int btrfs_prealloc_file_range(struct inode *inode, int mode,
8658 u64 start, u64 num_bytes, u64 min_size,
8659 loff_t actual_len, u64 *alloc_hint)
8660{
8661 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8662 min_size, actual_len, alloc_hint,
8663 NULL);
8664}
8665
8666int btrfs_prealloc_file_range_trans(struct inode *inode,
8667 struct btrfs_trans_handle *trans, int mode,
8668 u64 start, u64 num_bytes, u64 min_size,
8669 loff_t actual_len, u64 *alloc_hint)
8670{
8671 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8672 min_size, actual_len, alloc_hint, trans);
8673}
8674
Chris Masone6dcd2d2008-07-17 12:53:50 -04008675static int btrfs_set_page_dirty(struct page *page)
8676{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008677 return __set_page_dirty_nobuffers(page);
8678}
8679
Al Viro10556cb2011-06-20 19:28:19 -04008680static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008681{
Li Zefanb83cc962010-12-20 16:04:08 +08008682 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008683 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008684
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008685 if (mask & MAY_WRITE &&
8686 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8687 if (btrfs_root_readonly(root))
8688 return -EROFS;
8689 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8690 return -EACCES;
8691 }
Al Viro2830ba72011-06-20 19:16:29 -04008692 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008693}
Chris Mason39279cc2007-06-12 06:35:45 -04008694
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008695static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008696 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008697 .lookup = btrfs_lookup,
8698 .create = btrfs_create,
8699 .unlink = btrfs_unlink,
8700 .link = btrfs_link,
8701 .mkdir = btrfs_mkdir,
8702 .rmdir = btrfs_rmdir,
8703 .rename = btrfs_rename,
8704 .symlink = btrfs_symlink,
8705 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008706 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008707 .setxattr = btrfs_setxattr,
8708 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008709 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008710 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008711 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008712 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008713};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008714static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008715 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008716 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008717 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008718};
Yan, Zheng76dda932009-09-21 16:00:26 -04008719
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008720static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008721 .llseek = generic_file_llseek,
8722 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008723 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008724 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008725#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008726 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008727#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008728 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008729 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008730};
8731
Chris Masond1310b22008-01-24 16:13:08 -05008732static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008733 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008734 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008735 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008736 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008737 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008738 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008739 .set_bit_hook = btrfs_set_bit_hook,
8740 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008741 .merge_extent_hook = btrfs_merge_extent_hook,
8742 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008743};
8744
Chris Mason35054392009-01-21 13:11:13 -05008745/*
8746 * btrfs doesn't support the bmap operation because swapfiles
8747 * use bmap to make a mapping of extents in the file. They assume
8748 * these extents won't change over the life of the file and they
8749 * use the bmap result to do IO directly to the drive.
8750 *
8751 * the btrfs bmap call would return logical addresses that aren't
8752 * suitable for IO and they also will change frequently as COW
8753 * operations happen. So, swapfile + btrfs == corruption.
8754 *
8755 * For now we're avoiding this by dropping bmap.
8756 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008757static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008758 .readpage = btrfs_readpage,
8759 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008760 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008761 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008762 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008763 .invalidatepage = btrfs_invalidatepage,
8764 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008765 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008766 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008767};
8768
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008769static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008770 .readpage = btrfs_readpage,
8771 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008772 .invalidatepage = btrfs_invalidatepage,
8773 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008774};
8775
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008776static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008777 .getattr = btrfs_getattr,
8778 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008779 .setxattr = btrfs_setxattr,
8780 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008781 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008782 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008783 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008784 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008785 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008786 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008787};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008788static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008789 .getattr = btrfs_getattr,
8790 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008791 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008792 .setxattr = btrfs_setxattr,
8793 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008794 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008795 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008796 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008797 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008798};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008799static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008800 .readlink = generic_readlink,
8801 .follow_link = page_follow_link_light,
8802 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008803 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008804 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008805 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008806 .setxattr = btrfs_setxattr,
8807 .getxattr = btrfs_getxattr,
8808 .listxattr = btrfs_listxattr,
8809 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008810 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008811 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008812};
Yan, Zheng76dda932009-09-21 16:00:26 -04008813
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008814const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008815 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008816 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008817};