blob: b7fa96f72ecd37bbe7a8f602b265d0987f918aa9 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040059
60struct btrfs_iget_args {
61 u64 ino;
62 struct btrfs_root *root;
63};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050073static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000076static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050080struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
82#define S_SHIFT 12
83static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
84 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
85 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
86 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
87 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
88 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
89 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
90 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
91};
92
Eric Sandeen3972f262013-01-12 02:57:22 +000093static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050094static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040095static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050096static noinline int cow_file_range(struct inode *inode,
97 struct page *locked_page,
98 u64 start, u64 end, int *page_started,
99 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400100static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
101 u64 len, u64 orig_start,
102 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400103 u64 orig_block_len, u64 ram_bytes,
104 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400105
Eric Sandeen48a3b632013-04-25 20:41:01 +0000106static int btrfs_dirty_inode(struct inode *inode);
107
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000108static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500109 struct inode *inode, struct inode *dir,
110 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500111{
112 int err;
113
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000114 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500115 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500116 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500117 return err;
118}
119
Chris Masond352ac62008-09-29 15:18:18 -0400120/*
Chris Masonc8b97812008-10-29 14:49:59 -0400121 * this does all the hard work for inserting an inline extent into
122 * the btree. The caller should have done a btrfs_drop_extents so that
123 * no overlapping inline items exist in the btree
124 */
Chris Masond3977122009-01-05 21:25:51 -0500125static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400126 struct btrfs_root *root, struct inode *inode,
127 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000128 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct page **compressed_pages)
130{
131 struct btrfs_key key;
132 struct btrfs_path *path;
133 struct extent_buffer *leaf;
134 struct page *page = NULL;
135 char *kaddr;
136 unsigned long ptr;
137 struct btrfs_file_extent_item *ei;
138 int err = 0;
139 int ret;
140 size_t cur_size = size;
141 size_t datasize;
142 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Li Zefanfe3f5662011-03-28 08:30:38 +0000144 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400145 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Chris Masond3977122009-01-05 21:25:51 -0500147 path = btrfs_alloc_path();
148 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400149 return -ENOMEM;
150
Chris Masonb9473432009-03-13 11:00:37 -0400151 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400152
Li Zefan33345d012011-04-20 10:31:50 +0800153 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400154 key.offset = start;
155 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 datasize = btrfs_file_extent_calc_inline_size(cur_size);
157
158 inode_add_bytes(inode, size);
159 ret = btrfs_insert_empty_item(trans, root, path, &key,
160 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400161 if (ret) {
162 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400163 goto fail;
164 }
165 leaf = path->nodes[0];
166 ei = btrfs_item_ptr(leaf, path->slots[0],
167 struct btrfs_file_extent_item);
168 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
169 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
170 btrfs_set_file_extent_encryption(leaf, ei, 0);
171 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
172 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
173 ptr = btrfs_file_extent_inline_start(ei);
174
Li Zefan261507a02010-12-17 14:21:50 +0800175 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400176 struct page *cpage;
177 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500178 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500180 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400181 PAGE_CACHE_SIZE);
182
Cong Wang7ac687d2011-11-25 23:14:28 +0800183 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400184 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400186
187 i++;
188 ptr += cur_size;
189 compressed_size -= cur_size;
190 }
191 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800192 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 } else {
194 page = find_get_page(inode->i_mapping,
195 start >> PAGE_CACHE_SHIFT);
196 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800197 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400198 offset = start & (PAGE_CACHE_SIZE - 1);
199 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 page_cache_release(page);
202 }
203 btrfs_mark_buffer_dirty(leaf);
204 btrfs_free_path(path);
205
Yan, Zhengc2167752009-11-12 09:34:21 +0000206 /*
207 * we're an inline extent, so nobody can
208 * extend the file past i_size without locking
209 * a page we already have locked.
210 *
211 * We must do any isize and inode updates
212 * before we unlock the pages. Otherwise we
213 * could end up racing with unlink.
214 */
Chris Masonc8b97812008-10-29 14:49:59 -0400215 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100216 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000217
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400219fail:
220 btrfs_free_path(path);
221 return err;
222}
223
224
225/*
226 * conditionally insert an inline extent into the file. This
227 * does the checks required to make sure the data is small enough
228 * to fit as an inline extent.
229 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400230static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400231 struct btrfs_root *root,
232 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000233 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400234 struct page **compressed_pages)
235{
236 u64 isize = i_size_read(inode);
237 u64 actual_end = min(end + 1, isize);
238 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000239 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 u64 data_len = inline_len;
241 int ret;
242
243 if (compressed_size)
244 data_len = compressed_size;
245
246 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400247 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400248 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
249 (!compressed_size &&
250 (actual_end & (root->sectorsize - 1)) == 0) ||
251 end + 1 < isize ||
252 data_len > root->fs_info->max_inline) {
253 return 1;
254 }
255
Josef Bacik26714852012-08-29 12:24:27 -0400256 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100257 if (ret)
258 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400259
260 if (isize > actual_end)
261 inline_len = min_t(u64, isize, actual_end);
262 ret = insert_inline_extent(trans, root, inode, start,
263 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000264 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 btrfs_abort_transaction(trans, root, ret);
267 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400268 } else if (ret == -ENOSPC) {
269 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100270 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400271
Josef Bacikbdc20e62013-02-28 13:23:38 -0500272 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400273 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400274 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 0;
276}
277
Chris Mason771ed682008-11-06 22:02:51 -0500278struct async_extent {
279 u64 start;
280 u64 ram_size;
281 u64 compressed_size;
282 struct page **pages;
283 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800284 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500285 struct list_head list;
286};
287
288struct async_cow {
289 struct inode *inode;
290 struct btrfs_root *root;
291 struct page *locked_page;
292 u64 start;
293 u64 end;
294 struct list_head extents;
295 struct btrfs_work work;
296};
297
298static noinline int add_async_extent(struct async_cow *cow,
299 u64 start, u64 ram_size,
300 u64 compressed_size,
301 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800302 unsigned long nr_pages,
303 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500304{
305 struct async_extent *async_extent;
306
307 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100308 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500309 async_extent->start = start;
310 async_extent->ram_size = ram_size;
311 async_extent->compressed_size = compressed_size;
312 async_extent->pages = pages;
313 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800314 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500315 list_add_tail(&async_extent->list, &cow->extents);
316 return 0;
317}
318
Chris Masonc8b97812008-10-29 14:49:59 -0400319/*
Chris Mason771ed682008-11-06 22:02:51 -0500320 * we create compressed extents in two phases. The first
321 * phase compresses a range of pages that have already been
322 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * This is done inside an ordered work queue, and the compression
325 * is spread across many cpus. The actual IO submission is step
326 * two, and the ordered work queue takes care of making sure that
327 * happens in the same order things were put onto the queue by
328 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400329 *
Chris Mason771ed682008-11-06 22:02:51 -0500330 * If this code finds it can't get good compression, it puts an
331 * entry onto the work queue to write the uncompressed bytes. This
332 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300333 * are written in the same order that the flusher thread sent them
334 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400335 */
Chris Mason771ed682008-11-06 22:02:51 -0500336static noinline int compress_file_range(struct inode *inode,
337 struct page *locked_page,
338 u64 start, u64 end,
339 struct async_cow *async_cow,
340 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400341{
342 struct btrfs_root *root = BTRFS_I(inode)->root;
343 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400344 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400345 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500347 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400348 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 struct page **pages = NULL;
350 unsigned long nr_pages;
351 unsigned long nr_pages_ret = 0;
352 unsigned long total_compressed = 0;
353 unsigned long total_in = 0;
354 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500355 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400356 int i;
357 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800358 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400359 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400360
Liu Bo4cb13e52012-03-29 09:57:45 -0400361 /* if this is a small write inside eof, kick off a defrag */
362 if ((end - start + 1) < 16 * 1024 &&
363 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400364 btrfs_add_inode_defrag(NULL, inode);
365
Chris Mason42dc7ba2008-12-15 11:44:56 -0500366 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400367again:
368 will_compress = 0;
369 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
370 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
371
Chris Masonf03d9301f2009-02-04 09:31:06 -0500372 /*
373 * we don't want to send crud past the end of i_size through
374 * compression, that's just a waste of CPU time. So, if the
375 * end of the file is before the start of our current
376 * requested range of bytes, we bail out to the uncompressed
377 * cleanup code that can deal with all of this.
378 *
379 * It isn't really the fastest way to fix things, but this is a
380 * very uncommon corner.
381 */
382 if (actual_end <= start)
383 goto cleanup_and_bail_uncompressed;
384
Chris Masonc8b97812008-10-29 14:49:59 -0400385 total_compressed = actual_end - start;
386
387 /* we want to make sure that amount of ram required to uncompress
388 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500389 * of a compressed extent to 128k. This is a crucial number
390 * because it also controls how easily we can spread reads across
391 * cpus for decompression.
392 *
393 * We also want to make sure the amount of IO required to do
394 * a random read is reasonably small, so we limit the size of
395 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400396 */
397 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000398 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500399 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400400 total_in = 0;
401 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400402
Chris Mason771ed682008-11-06 22:02:51 -0500403 /*
404 * we do compression for mount -o compress and when the
405 * inode has not been flagged as nocompress. This flag can
406 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400407 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200408 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500409 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000410 (BTRFS_I(inode)->force_compress) ||
411 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400412 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400413 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800414 if (!pages) {
415 /* just bail out to the uncompressed code */
416 goto cont;
417 }
Chris Mason179e29e2007-11-01 11:28:41 -0400418
Li Zefan261507a02010-12-17 14:21:50 +0800419 if (BTRFS_I(inode)->force_compress)
420 compress_type = BTRFS_I(inode)->force_compress;
421
Chris Mason4adaa612013-03-26 13:07:00 -0400422 /*
423 * we need to call clear_page_dirty_for_io on each
424 * page in the range. Otherwise applications with the file
425 * mmap'd can wander in and change the page contents while
426 * we are compressing them.
427 *
428 * If the compression fails for any reason, we set the pages
429 * dirty again later on.
430 */
431 extent_range_clear_dirty_for_io(inode, start, end);
432 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800433 ret = btrfs_compress_pages(compress_type,
434 inode->i_mapping, start,
435 total_compressed, pages,
436 nr_pages, &nr_pages_ret,
437 &total_in,
438 &total_compressed,
439 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400440
441 if (!ret) {
442 unsigned long offset = total_compressed &
443 (PAGE_CACHE_SIZE - 1);
444 struct page *page = pages[nr_pages_ret - 1];
445 char *kaddr;
446
447 /* zero the tail end of the last page, we might be
448 * sending it down to disk
449 */
450 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800451 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400452 memset(kaddr + offset, 0,
453 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800454 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400455 }
456 will_compress = 1;
457 }
458 }
Li Zefan560f7d72011-09-08 10:22:01 +0800459cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400460 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400461 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100462 if (IS_ERR(trans)) {
463 ret = PTR_ERR(trans);
464 trans = NULL;
465 goto cleanup_and_out;
466 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400467 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500468
Chris Masonc8b97812008-10-29 14:49:59 -0400469 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500470 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400471 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500472 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 */
474 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000475 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400476 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500477 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400478 ret = cow_file_range_inline(trans, root, inode,
479 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000480 total_compressed,
481 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100483 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500484 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100485 * inline extent creation worked or returned error,
486 * we don't need to create any more async work items.
487 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500488 */
Chris Masonc8b97812008-10-29 14:49:59 -0400489 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400490 &BTRFS_I(inode)->io_tree,
491 start, end, NULL,
492 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400493 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400494 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000495
496 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 goto free_pages_out;
498 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000499 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 }
501
502 if (will_compress) {
503 /*
504 * we aren't doing an inline extent round the compressed size
505 * up to a block size boundary so the allocator does sane
506 * things
507 */
Qu Wenruofda28322013-02-26 08:10:22 +0000508 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400509
510 /*
511 * one last check to make sure the compression is really a
512 * win, compare the page count read with the blocks on disk
513 */
Qu Wenruofda28322013-02-26 08:10:22 +0000514 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400515 if (total_compressed >= total_in) {
516 will_compress = 0;
517 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400518 num_bytes = total_in;
519 }
520 }
521 if (!will_compress && pages) {
522 /*
523 * the compression code ran but failed to make things smaller,
524 * free any pages it allocated and our page pointer array
525 */
526 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400527 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 page_cache_release(pages[i]);
529 }
530 kfree(pages);
531 pages = NULL;
532 total_compressed = 0;
533 nr_pages_ret = 0;
534
535 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500536 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
537 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500538 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500539 }
Chris Masonc8b97812008-10-29 14:49:59 -0400540 }
Chris Mason771ed682008-11-06 22:02:51 -0500541 if (will_compress) {
542 *num_added += 1;
543
544 /* the async work queues will take care of doing actual
545 * allocation on disk for these compressed pages,
546 * and will submit them to the elevator.
547 */
548 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800549 total_compressed, pages, nr_pages_ret,
550 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500551
Yan, Zheng24ae6362010-12-06 07:02:36 +0000552 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500553 start += num_bytes;
554 pages = NULL;
555 cond_resched();
556 goto again;
557 }
558 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500559cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500560 /*
561 * No compression, but we still need to write the pages in
562 * the file we've been given so far. redirty the locked
563 * page if it corresponds to our extent and set things up
564 * for the async work queue to run cow_file_range to do
565 * the normal delalloc dance
566 */
567 if (page_offset(locked_page) >= start &&
568 page_offset(locked_page) <= end) {
569 __set_page_dirty_nobuffers(locked_page);
570 /* unlocked later on in the async handlers */
571 }
Chris Mason4adaa612013-03-26 13:07:00 -0400572 if (redirty)
573 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800574 add_async_extent(async_cow, start, end - start + 1,
575 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500576 *num_added += 1;
577 }
578
579out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100580 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500581
582free_pages_out:
583 for (i = 0; i < nr_pages_ret; i++) {
584 WARN_ON(pages[i]->mapping);
585 page_cache_release(pages[i]);
586 }
Chris Masond3977122009-01-05 21:25:51 -0500587 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500588
589 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590
591cleanup_and_out:
592 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
593 start, end, NULL,
594 EXTENT_CLEAR_UNLOCK_PAGE |
595 EXTENT_CLEAR_DIRTY |
596 EXTENT_CLEAR_DELALLOC |
597 EXTENT_SET_WRITEBACK |
598 EXTENT_END_WRITEBACK);
599 if (!trans || IS_ERR(trans))
600 btrfs_error(root->fs_info, ret, "Failed to join transaction");
601 else
602 btrfs_abort_transaction(trans, root, ret);
603 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500604}
605
606/*
607 * phase two of compressed writeback. This is the ordered portion
608 * of the code, which only gets called in the order the work was
609 * queued. We walk all the async extents created by compress_file_range
610 * and send them down to the disk.
611 */
612static noinline int submit_compressed_extents(struct inode *inode,
613 struct async_cow *async_cow)
614{
615 struct async_extent *async_extent;
616 u64 alloc_hint = 0;
617 struct btrfs_trans_handle *trans;
618 struct btrfs_key ins;
619 struct extent_map *em;
620 struct btrfs_root *root = BTRFS_I(inode)->root;
621 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
622 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500623 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500624
625 if (list_empty(&async_cow->extents))
626 return 0;
627
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500628again:
Chris Masond3977122009-01-05 21:25:51 -0500629 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500630 async_extent = list_entry(async_cow->extents.next,
631 struct async_extent, list);
632 list_del(&async_extent->list);
633
634 io_tree = &BTRFS_I(inode)->io_tree;
635
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500636retry:
Chris Mason771ed682008-11-06 22:02:51 -0500637 /* did the compression code fall back to uncompressed IO? */
638 if (!async_extent->pages) {
639 int page_started = 0;
640 unsigned long nr_written = 0;
641
642 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000643 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100644 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500647 ret = cow_file_range(inode, async_cow->locked_page,
648 async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1,
651 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500652
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100653 /* JDM XXX */
654
Chris Mason771ed682008-11-06 22:02:51 -0500655 /*
656 * if page_started, cow_file_range inserted an
657 * inline extent and took care of all the unlocking
658 * and IO for us. Otherwise, we need to submit
659 * all those pages down to the drive.
660 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500661 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500662 extent_write_locked_range(io_tree,
663 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500664 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->ram_size - 1,
666 btrfs_get_extent,
667 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500668 else if (ret)
669 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500670 kfree(async_extent);
671 cond_resched();
672 continue;
673 }
674
675 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100676 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500677
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400678 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 if (IS_ERR(trans)) {
680 ret = PTR_ERR(trans);
681 } else {
682 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
683 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500684 async_extent->compressed_size,
685 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500686 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600687 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100688 btrfs_abort_transaction(trans, root, ret);
689 btrfs_end_transaction(trans, root);
690 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 if (ret) {
693 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 for (i = 0; i < async_extent->nr_pages; i++) {
696 WARN_ON(async_extent->pages[i]->mapping);
697 page_cache_release(async_extent->pages[i]);
698 }
699 kfree(async_extent->pages);
700 async_extent->nr_pages = 0;
701 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400703 if (ret == -ENOSPC) {
704 unlock_extent(io_tree, async_extent->start,
705 async_extent->start +
706 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100707 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400708 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500709 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500710 }
711
Yan, Zhengc2167752009-11-12 09:34:21 +0000712 /*
713 * here we're doing allocation and writeback of the
714 * compressed pages
715 */
716 btrfs_drop_extent_cache(inode, async_extent->start,
717 async_extent->start +
718 async_extent->ram_size - 1, 0);
719
David Sterba172ddd62011-04-21 00:48:27 +0200720 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000721 if (!em) {
722 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500723 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000724 }
Chris Mason771ed682008-11-06 22:02:51 -0500725 em->start = async_extent->start;
726 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500727 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400728 em->mod_start = em->start;
729 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500730
731 em->block_start = ins.objectid;
732 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500733 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400734 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500735 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800736 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500737 set_bit(EXTENT_FLAG_PINNED, &em->flags);
738 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400739 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500740
Chris Masond3977122009-01-05 21:25:51 -0500741 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400742 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400743 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400744 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500745 if (ret != -EEXIST) {
746 free_extent_map(em);
747 break;
748 }
749 btrfs_drop_extent_cache(inode, async_extent->start,
750 async_extent->start +
751 async_extent->ram_size - 1, 0);
752 }
753
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 if (ret)
755 goto out_free_reserve;
756
Li Zefan261507a02010-12-17 14:21:50 +0800757 ret = btrfs_add_ordered_extent_compress(inode,
758 async_extent->start,
759 ins.objectid,
760 async_extent->ram_size,
761 ins.offset,
762 BTRFS_ORDERED_COMPRESSED,
763 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500764 if (ret)
765 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500766
Chris Mason771ed682008-11-06 22:02:51 -0500767 /*
768 * clear dirty, set writeback and unlock the pages.
769 */
770 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400771 &BTRFS_I(inode)->io_tree,
772 async_extent->start,
773 async_extent->start +
774 async_extent->ram_size - 1,
775 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
776 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400777 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400778 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500779
780 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500781 async_extent->start,
782 async_extent->ram_size,
783 ins.objectid,
784 ins.offset, async_extent->pages,
785 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500786 alloc_hint = ins.objectid + ins.offset;
787 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 if (ret)
789 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500790 cond_resched();
791 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100792 ret = 0;
793out:
794 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795out_free_reserve:
796 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100797out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
799 async_extent->start,
800 async_extent->start +
801 async_extent->ram_size - 1,
802 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
803 EXTENT_CLEAR_UNLOCK |
804 EXTENT_CLEAR_DELALLOC |
805 EXTENT_CLEAR_DIRTY |
806 EXTENT_SET_WRITEBACK |
807 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100808 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500809 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500810}
811
Josef Bacik4b46fce2010-05-23 11:00:55 -0400812static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
813 u64 num_bytes)
814{
815 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
816 struct extent_map *em;
817 u64 alloc_hint = 0;
818
819 read_lock(&em_tree->lock);
820 em = search_extent_mapping(em_tree, start, num_bytes);
821 if (em) {
822 /*
823 * if block start isn't an actual block number then find the
824 * first block in this inode and use that as a hint. If that
825 * block is also bogus then just don't worry about it.
826 */
827 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
828 free_extent_map(em);
829 em = search_extent_mapping(em_tree, 0, 0);
830 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
831 alloc_hint = em->block_start;
832 if (em)
833 free_extent_map(em);
834 } else {
835 alloc_hint = em->block_start;
836 free_extent_map(em);
837 }
838 }
839 read_unlock(&em_tree->lock);
840
841 return alloc_hint;
842}
843
Chris Mason771ed682008-11-06 22:02:51 -0500844/*
845 * when extent_io.c finds a delayed allocation range in the file,
846 * the call backs end up in this code. The basic idea is to
847 * allocate extents on disk for the range, and create ordered data structs
848 * in ram to track those extents.
849 *
850 * locked_page is the page that writepage had locked already. We use
851 * it to make sure we don't do extra locks or unlocks.
852 *
853 * *page_started is set to one if we unlock locked_page and do everything
854 * required to start IO on it. It may be clean and already done with
855 * IO when we return.
856 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000857static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
858 struct inode *inode,
859 struct btrfs_root *root,
860 struct page *locked_page,
861 u64 start, u64 end, int *page_started,
862 unsigned long *nr_written,
863 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500864{
Chris Mason771ed682008-11-06 22:02:51 -0500865 u64 alloc_hint = 0;
866 u64 num_bytes;
867 unsigned long ram_size;
868 u64 disk_num_bytes;
869 u64 cur_alloc_size;
870 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500871 struct btrfs_key ins;
872 struct extent_map *em;
873 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
874 int ret = 0;
875
Liu Bo83eea1f2012-07-10 05:28:39 -0600876 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500877
Qu Wenruofda28322013-02-26 08:10:22 +0000878 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500879 num_bytes = max(blocksize, num_bytes);
880 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500881
Chris Mason4cb53002011-05-24 15:35:30 -0400882 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400883 if (num_bytes < 64 * 1024 &&
884 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400885 btrfs_add_inode_defrag(trans, inode);
886
Chris Mason771ed682008-11-06 22:02:51 -0500887 if (start == 0) {
888 /* lets try to make an inline extent */
889 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000890 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500891 if (ret == 0) {
892 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400893 &BTRFS_I(inode)->io_tree,
894 start, end, NULL,
895 EXTENT_CLEAR_UNLOCK_PAGE |
896 EXTENT_CLEAR_UNLOCK |
897 EXTENT_CLEAR_DELALLOC |
898 EXTENT_CLEAR_DIRTY |
899 EXTENT_SET_WRITEBACK |
900 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000901
Chris Mason771ed682008-11-06 22:02:51 -0500902 *nr_written = *nr_written +
903 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
904 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500905 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100906 } else if (ret < 0) {
907 btrfs_abort_transaction(trans, root, ret);
908 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500909 }
910 }
Chris Masonc8b97812008-10-29 14:49:59 -0400911
912 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200913 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400914
Josef Bacik4b46fce2010-05-23 11:00:55 -0400915 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400916 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400917
Chris Masond3977122009-01-05 21:25:51 -0500918 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400919 unsigned long op;
920
Josef Bacik287a0ab2010-03-19 18:07:23 +0000921 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400922 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500923 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500924 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 if (ret < 0) {
926 btrfs_abort_transaction(trans, root, ret);
927 goto out_unlock;
928 }
Chris Masond3977122009-01-05 21:25:51 -0500929
David Sterba172ddd62011-04-21 00:48:27 +0200930 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000931 if (!em) {
932 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000933 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000934 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400935 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500936 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500937 ram_size = ins.offset;
938 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400939 em->mod_start = em->start;
940 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400941
Chris Masone6dcd2d2008-07-17 12:53:50 -0400942 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400943 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500944 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400945 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400946 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400947 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400948 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400949
Chris Masond3977122009-01-05 21:25:51 -0500950 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400951 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400952 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400953 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400954 if (ret != -EEXIST) {
955 free_extent_map(em);
956 break;
957 }
958 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400959 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960 }
Liu Boace68ba2013-04-22 10:53:47 +0000961 if (ret)
962 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400963
Chris Mason98d20f62008-04-14 09:46:10 -0400964 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400965 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500966 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000967 if (ret)
968 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400969
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 if (root->root_key.objectid ==
971 BTRFS_DATA_RELOC_TREE_OBJECTID) {
972 ret = btrfs_reloc_clone_csums(inode, start,
973 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100974 if (ret) {
975 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000976 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500978 }
979
Chris Masond3977122009-01-05 21:25:51 -0500980 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400981 break;
Chris Masond3977122009-01-05 21:25:51 -0500982
Chris Masonc8b97812008-10-29 14:49:59 -0400983 /* we're not doing compressed IO, don't unlock the first
984 * page (which the caller expects to stay locked), don't
985 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400986 *
987 * Do set the Private2 bit so we know this page was properly
988 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400989 */
Chris Masona791e352009-10-08 11:27:10 -0400990 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
991 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
992 EXTENT_SET_PRIVATE2;
993
Chris Masonc8b97812008-10-29 14:49:59 -0400994 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
995 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400996 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400997 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500998 num_bytes -= cur_alloc_size;
999 alloc_hint = ins.objectid + ins.offset;
1000 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001001 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001003 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001004
Liu Boace68ba2013-04-22 10:53:47 +00001005out_reserve:
1006 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007out_unlock:
1008 extent_clear_unlock_delalloc(inode,
1009 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001010 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001011 EXTENT_CLEAR_UNLOCK_PAGE |
1012 EXTENT_CLEAR_UNLOCK |
1013 EXTENT_CLEAR_DELALLOC |
1014 EXTENT_CLEAR_DIRTY |
1015 EXTENT_SET_WRITEBACK |
1016 EXTENT_END_WRITEBACK);
1017
1018 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001019}
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001021static noinline int cow_file_range(struct inode *inode,
1022 struct page *locked_page,
1023 u64 start, u64 end, int *page_started,
1024 unsigned long *nr_written,
1025 int unlock)
1026{
1027 struct btrfs_trans_handle *trans;
1028 struct btrfs_root *root = BTRFS_I(inode)->root;
1029 int ret;
1030
1031 trans = btrfs_join_transaction(root);
1032 if (IS_ERR(trans)) {
1033 extent_clear_unlock_delalloc(inode,
1034 &BTRFS_I(inode)->io_tree,
1035 start, end, locked_page,
1036 EXTENT_CLEAR_UNLOCK_PAGE |
1037 EXTENT_CLEAR_UNLOCK |
1038 EXTENT_CLEAR_DELALLOC |
1039 EXTENT_CLEAR_DIRTY |
1040 EXTENT_SET_WRITEBACK |
1041 EXTENT_END_WRITEBACK);
1042 return PTR_ERR(trans);
1043 }
1044 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1045
1046 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1047 page_started, nr_written, unlock);
1048
1049 btrfs_end_transaction(trans, root);
1050
1051 return ret;
1052}
1053
Chris Mason771ed682008-11-06 22:02:51 -05001054/*
1055 * work queue call back to started compression on a file and pages
1056 */
1057static noinline void async_cow_start(struct btrfs_work *work)
1058{
1059 struct async_cow *async_cow;
1060 int num_added = 0;
1061 async_cow = container_of(work, struct async_cow, work);
1062
1063 compress_file_range(async_cow->inode, async_cow->locked_page,
1064 async_cow->start, async_cow->end, async_cow,
1065 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001066 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001067 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001068 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001069 }
Chris Mason771ed682008-11-06 22:02:51 -05001070}
1071
1072/*
1073 * work queue call back to submit previously compressed pages
1074 */
1075static noinline void async_cow_submit(struct btrfs_work *work)
1076{
1077 struct async_cow *async_cow;
1078 struct btrfs_root *root;
1079 unsigned long nr_pages;
1080
1081 async_cow = container_of(work, struct async_cow, work);
1082
1083 root = async_cow->root;
1084 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1085 PAGE_CACHE_SHIFT;
1086
Josef Bacik66657b32012-08-01 15:36:24 -04001087 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001088 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001089 waitqueue_active(&root->fs_info->async_submit_wait))
1090 wake_up(&root->fs_info->async_submit_wait);
1091
Chris Masond3977122009-01-05 21:25:51 -05001092 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001093 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001094}
Chris Masonc8b97812008-10-29 14:49:59 -04001095
Chris Mason771ed682008-11-06 22:02:51 -05001096static noinline void async_cow_free(struct btrfs_work *work)
1097{
1098 struct async_cow *async_cow;
1099 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001100 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001101 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001102 kfree(async_cow);
1103}
1104
1105static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1106 u64 start, u64 end, int *page_started,
1107 unsigned long *nr_written)
1108{
1109 struct async_cow *async_cow;
1110 struct btrfs_root *root = BTRFS_I(inode)->root;
1111 unsigned long nr_pages;
1112 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001113 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001114
Chris Masona3429ab2009-10-08 12:30:20 -04001115 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1116 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001117 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001118 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001120 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001121 async_cow->root = root;
1122 async_cow->locked_page = locked_page;
1123 async_cow->start = start;
1124
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001125 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001126 cur_end = end;
1127 else
1128 cur_end = min(end, start + 512 * 1024 - 1);
1129
1130 async_cow->end = cur_end;
1131 INIT_LIST_HEAD(&async_cow->extents);
1132
1133 async_cow->work.func = async_cow_start;
1134 async_cow->work.ordered_func = async_cow_submit;
1135 async_cow->work.ordered_free = async_cow_free;
1136 async_cow->work.flags = 0;
1137
Chris Mason771ed682008-11-06 22:02:51 -05001138 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1139 PAGE_CACHE_SHIFT;
1140 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1141
1142 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1143 &async_cow->work);
1144
1145 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1146 wait_event(root->fs_info->async_submit_wait,
1147 (atomic_read(&root->fs_info->async_delalloc_pages) <
1148 limit));
1149 }
1150
Chris Masond3977122009-01-05 21:25:51 -05001151 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001152 atomic_read(&root->fs_info->async_delalloc_pages)) {
1153 wait_event(root->fs_info->async_submit_wait,
1154 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1155 0));
1156 }
1157
1158 *nr_written += nr_pages;
1159 start = cur_end + 1;
1160 }
1161 *page_started = 1;
1162 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001163}
1164
Chris Masond3977122009-01-05 21:25:51 -05001165static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001166 u64 bytenr, u64 num_bytes)
1167{
1168 int ret;
1169 struct btrfs_ordered_sum *sums;
1170 LIST_HEAD(list);
1171
Yan Zheng07d400a2009-01-06 11:42:00 -05001172 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001173 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001174 if (ret == 0 && list_empty(&list))
1175 return 0;
1176
1177 while (!list_empty(&list)) {
1178 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1179 list_del(&sums->list);
1180 kfree(sums);
1181 }
1182 return 1;
1183}
1184
Chris Masond352ac62008-09-29 15:18:18 -04001185/*
1186 * when nowcow writeback call back. This checks for snapshots or COW copies
1187 * of the extents that exist in the file, and COWs the file as required.
1188 *
1189 * If no cow copies or snapshots exist, we write directly to the existing
1190 * blocks on disk
1191 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001192static noinline int run_delalloc_nocow(struct inode *inode,
1193 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001194 u64 start, u64 end, int *page_started, int force,
1195 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001196{
Chris Masonbe20aa92007-12-17 20:14:01 -05001197 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001198 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001200 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 u64 cow_start;
1204 u64 cur_offset;
1205 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 u64 disk_bytenr;
1208 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001209 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001210 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001213 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 int nocow;
1215 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001216 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001217 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001218
1219 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001220 if (!path) {
1221 extent_clear_unlock_delalloc(inode,
1222 &BTRFS_I(inode)->io_tree,
1223 start, end, locked_page,
1224 EXTENT_CLEAR_UNLOCK_PAGE |
1225 EXTENT_CLEAR_UNLOCK |
1226 EXTENT_CLEAR_DELALLOC |
1227 EXTENT_CLEAR_DIRTY |
1228 EXTENT_SET_WRITEBACK |
1229 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001230 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001231 }
Li Zefan82d59022011-04-20 10:33:24 +08001232
Liu Bo83eea1f2012-07-10 05:28:39 -06001233 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001234
1235 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001236 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001237 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001238 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001239
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001240 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001241 extent_clear_unlock_delalloc(inode,
1242 &BTRFS_I(inode)->io_tree,
1243 start, end, locked_page,
1244 EXTENT_CLEAR_UNLOCK_PAGE |
1245 EXTENT_CLEAR_UNLOCK |
1246 EXTENT_CLEAR_DELALLOC |
1247 EXTENT_CLEAR_DIRTY |
1248 EXTENT_SET_WRITEBACK |
1249 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001250 btrfs_free_path(path);
1251 return PTR_ERR(trans);
1252 }
1253
Josef Bacik74b21072011-04-13 12:02:53 -04001254 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001255
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 cow_start = (u64)-1;
1257 cur_offset = start;
1258 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001259 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001261 if (ret < 0) {
1262 btrfs_abort_transaction(trans, root, ret);
1263 goto error;
1264 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1266 leaf = path->nodes[0];
1267 btrfs_item_key_to_cpu(leaf, &found_key,
1268 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001269 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 found_key.type == BTRFS_EXTENT_DATA_KEY)
1271 path->slots[0]--;
1272 }
1273 check_prev = 0;
1274next_slot:
1275 leaf = path->nodes[0];
1276 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1277 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 if (ret < 0) {
1279 btrfs_abort_transaction(trans, root, ret);
1280 goto error;
1281 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 if (ret > 0)
1283 break;
1284 leaf = path->nodes[0];
1285 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001286
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 nocow = 0;
1288 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001289 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001291
Li Zefan33345d012011-04-20 10:31:50 +08001292 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1294 found_key.offset > end)
1295 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001296
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 if (found_key.offset > cur_offset) {
1298 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001299 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 goto out_check;
1301 }
Chris Masonc31f8832008-01-08 15:46:31 -05001302
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 fi = btrfs_item_ptr(leaf, path->slots[0],
1304 struct btrfs_file_extent_item);
1305 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001306
Josef Bacikcc95bef2013-04-04 14:31:27 -04001307 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001308 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1309 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001311 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 extent_end = found_key.offset +
1313 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001314 disk_num_bytes =
1315 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (extent_end <= start) {
1317 path->slots[0]++;
1318 goto next_slot;
1319 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001320 if (disk_bytenr == 0)
1321 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 if (btrfs_file_extent_compression(leaf, fi) ||
1323 btrfs_file_extent_encryption(leaf, fi) ||
1324 btrfs_file_extent_other_encoding(leaf, fi))
1325 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001326 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1327 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001328 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001330 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 found_key.offset -
1332 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001334 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 disk_bytenr += cur_offset - found_key.offset;
1336 num_bytes = min(end + 1, extent_end) - cur_offset;
1337 /*
1338 * force cow if csum exists in the range.
1339 * this ensure that csum for a given extent are
1340 * either valid or do not exist.
1341 */
1342 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1343 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 nocow = 1;
1345 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1346 extent_end = found_key.offset +
1347 btrfs_file_extent_inline_len(leaf, fi);
1348 extent_end = ALIGN(extent_end, root->sectorsize);
1349 } else {
1350 BUG_ON(1);
1351 }
1352out_check:
1353 if (extent_end <= start) {
1354 path->slots[0]++;
1355 goto next_slot;
1356 }
1357 if (!nocow) {
1358 if (cow_start == (u64)-1)
1359 cow_start = cur_offset;
1360 cur_offset = extent_end;
1361 if (cur_offset > end)
1362 break;
1363 path->slots[0]++;
1364 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001365 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001366
David Sterbab3b4aa72011-04-21 01:20:15 +02001367 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001369 ret = __cow_file_range(trans, inode, root, locked_page,
1370 cow_start, found_key.offset - 1,
1371 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001372 if (ret) {
1373 btrfs_abort_transaction(trans, root, ret);
1374 goto error;
1375 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001377 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001378
Yan Zhengd899e052008-10-30 14:25:28 -04001379 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1380 struct extent_map *em;
1381 struct extent_map_tree *em_tree;
1382 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001383 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001384 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001385 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001386 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001387 em->len = num_bytes;
1388 em->block_len = num_bytes;
1389 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001390 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001391 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001392 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001393 em->mod_start = em->start;
1394 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001395 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001396 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001397 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001398 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001399 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001400 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001401 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001402 if (ret != -EEXIST) {
1403 free_extent_map(em);
1404 break;
1405 }
1406 btrfs_drop_extent_cache(inode, em->start,
1407 em->start + em->len - 1, 0);
1408 }
1409 type = BTRFS_ORDERED_PREALLOC;
1410 } else {
1411 type = BTRFS_ORDERED_NOCOW;
1412 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001413
1414 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001415 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001416 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001417
Yan, Zhengefa56462010-05-16 10:49:59 -04001418 if (root->root_key.objectid ==
1419 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1420 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1421 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001422 if (ret) {
1423 btrfs_abort_transaction(trans, root, ret);
1424 goto error;
1425 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001426 }
1427
Yan Zhengd899e052008-10-30 14:25:28 -04001428 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001429 cur_offset, cur_offset + num_bytes - 1,
1430 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1431 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1432 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 cur_offset = extent_end;
1434 if (cur_offset > end)
1435 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001436 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001437 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438
Josef Bacik17ca04a2012-05-31 15:58:55 -04001439 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001441 cur_offset = end;
1442 }
1443
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001445 ret = __cow_file_range(trans, inode, root, locked_page,
1446 cow_start, end,
1447 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 if (ret) {
1449 btrfs_abort_transaction(trans, root, ret);
1450 goto error;
1451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 }
1453
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001454error:
Miao Xiea698d0752012-09-20 01:51:59 -06001455 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 if (!ret)
1457 ret = err;
1458
Josef Bacik17ca04a2012-05-31 15:58:55 -04001459 if (ret && cur_offset < end)
1460 extent_clear_unlock_delalloc(inode,
1461 &BTRFS_I(inode)->io_tree,
1462 cur_offset, end, locked_page,
1463 EXTENT_CLEAR_UNLOCK_PAGE |
1464 EXTENT_CLEAR_UNLOCK |
1465 EXTENT_CLEAR_DELALLOC |
1466 EXTENT_CLEAR_DIRTY |
1467 EXTENT_SET_WRITEBACK |
1468 EXTENT_END_WRITEBACK);
1469
Yan Zheng7ea394f2008-08-05 13:05:02 -04001470 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001471 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001472}
1473
Chris Masond352ac62008-09-29 15:18:18 -04001474/*
1475 * extent_io.c call back to do delayed allocation processing
1476 */
Chris Masonc8b97812008-10-29 14:49:59 -04001477static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001478 u64 start, u64 end, int *page_started,
1479 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001480{
Chris Masonbe20aa92007-12-17 20:14:01 -05001481 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001482 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001483
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001484 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001485 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001486 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001487 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001488 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001489 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001490 } else if (!btrfs_test_opt(root, COMPRESS) &&
1491 !(BTRFS_I(inode)->force_compress) &&
1492 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001493 ret = cow_file_range(inode, locked_page, start, end,
1494 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001495 } else {
1496 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1497 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001498 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001499 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001500 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001501 return ret;
1502}
1503
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001504static void btrfs_split_extent_hook(struct inode *inode,
1505 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001507 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001508 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001509 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001510
Josef Bacik9e0baf62011-07-15 15:16:44 +00001511 spin_lock(&BTRFS_I(inode)->lock);
1512 BTRFS_I(inode)->outstanding_extents++;
1513 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001514}
1515
1516/*
1517 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1518 * extents so we can keep track of new extents that are just merged onto old
1519 * extents, such as when we are doing sequential writes, so we can properly
1520 * account for the metadata space we'll need.
1521 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001522static void btrfs_merge_extent_hook(struct inode *inode,
1523 struct extent_state *new,
1524 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001526 /* not delalloc, ignore it */
1527 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001528 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001529
Josef Bacik9e0baf62011-07-15 15:16:44 +00001530 spin_lock(&BTRFS_I(inode)->lock);
1531 BTRFS_I(inode)->outstanding_extents--;
1532 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001533}
1534
Miao Xieeb73c1b2013-05-15 07:48:22 +00001535static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1536 struct inode *inode)
1537{
1538 spin_lock(&root->delalloc_lock);
1539 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1540 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1541 &root->delalloc_inodes);
1542 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1543 &BTRFS_I(inode)->runtime_flags);
1544 root->nr_delalloc_inodes++;
1545 if (root->nr_delalloc_inodes == 1) {
1546 spin_lock(&root->fs_info->delalloc_root_lock);
1547 BUG_ON(!list_empty(&root->delalloc_root));
1548 list_add_tail(&root->delalloc_root,
1549 &root->fs_info->delalloc_roots);
1550 spin_unlock(&root->fs_info->delalloc_root_lock);
1551 }
1552 }
1553 spin_unlock(&root->delalloc_lock);
1554}
1555
1556static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1557 struct inode *inode)
1558{
1559 spin_lock(&root->delalloc_lock);
1560 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1561 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1562 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1563 &BTRFS_I(inode)->runtime_flags);
1564 root->nr_delalloc_inodes--;
1565 if (!root->nr_delalloc_inodes) {
1566 spin_lock(&root->fs_info->delalloc_root_lock);
1567 BUG_ON(list_empty(&root->delalloc_root));
1568 list_del_init(&root->delalloc_root);
1569 spin_unlock(&root->fs_info->delalloc_root_lock);
1570 }
1571 }
1572 spin_unlock(&root->delalloc_lock);
1573}
1574
Chris Masond352ac62008-09-29 15:18:18 -04001575/*
1576 * extent_io.c set_bit_hook, used to track delayed allocation
1577 * bytes in this file, and to maintain the list of inodes that
1578 * have pending delalloc work to be done.
1579 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001580static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001581 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001582{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001583
Chris Mason75eff682008-12-15 15:54:40 -05001584 /*
1585 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001586 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001587 * bit, which is only set or cleared with irqs on
1588 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001589 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001590 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001592 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001593
Josef Bacik9e0baf62011-07-15 15:16:44 +00001594 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001595 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001596 } else {
1597 spin_lock(&BTRFS_I(inode)->lock);
1598 BTRFS_I(inode)->outstanding_extents++;
1599 spin_unlock(&BTRFS_I(inode)->lock);
1600 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001601
Miao Xie963d6782013-01-29 10:10:51 +00001602 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1603 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001604 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001606 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001607 &BTRFS_I(inode)->runtime_flags))
1608 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001609 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001610 }
Chris Mason291d6732008-01-29 15:55:23 -05001611}
1612
Chris Masond352ac62008-09-29 15:18:18 -04001613/*
1614 * extent_io.c clear_bit_hook, see set_bit_hook for why
1615 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001616static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001617 struct extent_state *state,
1618 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001619{
Chris Mason75eff682008-12-15 15:54:40 -05001620 /*
1621 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001622 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001623 * bit, which is only set or cleared with irqs on
1624 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001625 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001626 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001627 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001628 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001629
Josef Bacik9e0baf62011-07-15 15:16:44 +00001630 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001631 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001632 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1633 spin_lock(&BTRFS_I(inode)->lock);
1634 BTRFS_I(inode)->outstanding_extents--;
1635 spin_unlock(&BTRFS_I(inode)->lock);
1636 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001637
1638 if (*bits & EXTENT_DO_ACCOUNTING)
1639 btrfs_delalloc_release_metadata(inode, len);
1640
Josef Bacik0cb59c92010-07-02 12:14:14 -04001641 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1642 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001643 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644
Miao Xie963d6782013-01-29 10:10:51 +00001645 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1646 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001647 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001648 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001649 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001650 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001651 &BTRFS_I(inode)->runtime_flags))
1652 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001653 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001654 }
Chris Mason291d6732008-01-29 15:55:23 -05001655}
1656
Chris Masond352ac62008-09-29 15:18:18 -04001657/*
1658 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1659 * we don't create bios that span stripes or chunks
1660 */
David Woodhouse64a16702009-07-15 23:29:37 +01001661int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001662 size_t size, struct bio *bio,
1663 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001664{
1665 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001666 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001667 u64 length = 0;
1668 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001669 int ret;
1670
Chris Mason771ed682008-11-06 22:02:51 -05001671 if (bio_flags & EXTENT_BIO_COMPRESSED)
1672 return 0;
1673
Chris Masonf2d8d742008-04-21 10:03:05 -04001674 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001675 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001676 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001677 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001678 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001679 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001680 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001681 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001682 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001683}
1684
Chris Masond352ac62008-09-29 15:18:18 -04001685/*
1686 * in order to insert checksums into the metadata in large chunks,
1687 * we wait until bio submission time. All the pages in the bio are
1688 * checksummed and sums are attached onto the ordered extent record.
1689 *
1690 * At IO completion time the cums attached on the ordered extent record
1691 * are inserted into the btree
1692 */
Chris Masond3977122009-01-05 21:25:51 -05001693static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1694 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001695 unsigned long bio_flags,
1696 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001697{
Chris Mason065631f2008-02-20 12:07:25 -05001698 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001699 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001700
Chris Masond20f7042008-12-08 16:58:54 -05001701 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001702 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001703 return 0;
1704}
Chris Masone0156402008-04-16 11:15:20 -04001705
Chris Mason4a69a412008-11-06 22:03:00 -05001706/*
1707 * in order to insert checksums into the metadata in large chunks,
1708 * we wait until bio submission time. All the pages in the bio are
1709 * checksummed and sums are attached onto the ordered extent record.
1710 *
1711 * At IO completion time the cums attached on the ordered extent record
1712 * are inserted into the btree
1713 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001714static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001715 int mirror_num, unsigned long bio_flags,
1716 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001717{
1718 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001719 int ret;
1720
1721 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1722 if (ret)
1723 bio_endio(bio, ret);
1724 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001725}
1726
Chris Masond352ac62008-09-29 15:18:18 -04001727/*
Chris Masoncad321a2008-12-17 14:51:42 -05001728 * extent_io.c submission hook. This does the right thing for csum calculation
1729 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001730 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001731static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001732 int mirror_num, unsigned long bio_flags,
1733 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001734{
1735 struct btrfs_root *root = BTRFS_I(inode)->root;
1736 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001737 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001738 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001739 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001740
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001741 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001742
Liu Bo83eea1f2012-07-10 05:28:39 -06001743 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001744 metadata = 2;
1745
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001746 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001747 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1748 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001749 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001750
Chris Masond20f7042008-12-08 16:58:54 -05001751 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001752 ret = btrfs_submit_compressed_read(inode, bio,
1753 mirror_num,
1754 bio_flags);
1755 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001756 } else if (!skip_sum) {
1757 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1758 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001759 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001760 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001761 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001762 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001763 /* csum items have already been cloned */
1764 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1765 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001766 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001767 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001768 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001769 bio_flags, bio_offset,
1770 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001771 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001772 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001773 } else if (!skip_sum) {
1774 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1775 if (ret)
1776 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001777 }
1778
Chris Mason0b86a832008-03-24 15:01:56 -04001779mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001780 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1781
1782out:
1783 if (ret < 0)
1784 bio_endio(bio, ret);
1785 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001786}
Chris Mason6885f302008-02-20 16:11:05 -05001787
Chris Masond352ac62008-09-29 15:18:18 -04001788/*
1789 * given a list of ordered sums record them in the inode. This happens
1790 * at IO completion time based on sums calculated at bio submission time.
1791 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001792static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001793 struct inode *inode, u64 file_offset,
1794 struct list_head *list)
1795{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001796 struct btrfs_ordered_sum *sum;
1797
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001798 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001799 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001800 btrfs_csum_file_blocks(trans,
1801 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001802 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001803 }
1804 return 0;
1805}
1806
Josef Bacik2ac55d42010-02-03 19:33:23 +00001807int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1808 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001809{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001810 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001811 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001812 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001813}
1814
Chris Masond352ac62008-09-29 15:18:18 -04001815/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001816struct btrfs_writepage_fixup {
1817 struct page *page;
1818 struct btrfs_work work;
1819};
1820
Christoph Hellwigb2950862008-12-02 09:54:17 -05001821static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001822{
1823 struct btrfs_writepage_fixup *fixup;
1824 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001825 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001826 struct page *page;
1827 struct inode *inode;
1828 u64 page_start;
1829 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001830 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001831
1832 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1833 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001834again:
Chris Mason247e7432008-07-17 12:53:51 -04001835 lock_page(page);
1836 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1837 ClearPageChecked(page);
1838 goto out_page;
1839 }
1840
1841 inode = page->mapping->host;
1842 page_start = page_offset(page);
1843 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1844
Josef Bacik2ac55d42010-02-03 19:33:23 +00001845 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001846 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001847
1848 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001849 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001850 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001851
1852 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1853 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001854 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1855 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001856 unlock_page(page);
1857 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001858 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001859 goto again;
1860 }
Chris Mason247e7432008-07-17 12:53:51 -04001861
Jeff Mahoney87826df2012-02-15 16:23:57 +01001862 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1863 if (ret) {
1864 mapping_set_error(page->mapping, ret);
1865 end_extent_writepage(page, ret, page_start, page_end);
1866 ClearPageChecked(page);
1867 goto out;
1868 }
1869
Josef Bacik2ac55d42010-02-03 19:33:23 +00001870 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001871 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001872 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001873out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001874 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1875 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001876out_page:
1877 unlock_page(page);
1878 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001879 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001880}
1881
1882/*
1883 * There are a few paths in the higher layers of the kernel that directly
1884 * set the page dirty bit without asking the filesystem if it is a
1885 * good idea. This causes problems because we want to make sure COW
1886 * properly happens and the data=ordered rules are followed.
1887 *
Chris Masonc8b97812008-10-29 14:49:59 -04001888 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001889 * hasn't been properly setup for IO. We kick off an async process
1890 * to fix it up. The async helper will wait for ordered extents, set
1891 * the delalloc bit and make it safe to write the page.
1892 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001893static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001894{
1895 struct inode *inode = page->mapping->host;
1896 struct btrfs_writepage_fixup *fixup;
1897 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001898
Chris Mason8b62b722009-09-02 16:53:46 -04001899 /* this page is properly in the ordered list */
1900 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001901 return 0;
1902
1903 if (PageChecked(page))
1904 return -EAGAIN;
1905
1906 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1907 if (!fixup)
1908 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001909
Chris Mason247e7432008-07-17 12:53:51 -04001910 SetPageChecked(page);
1911 page_cache_get(page);
1912 fixup->work.func = btrfs_writepage_fixup_worker;
1913 fixup->page = page;
1914 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001915 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001916}
1917
Yan Zhengd899e052008-10-30 14:25:28 -04001918static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1919 struct inode *inode, u64 file_pos,
1920 u64 disk_bytenr, u64 disk_num_bytes,
1921 u64 num_bytes, u64 ram_bytes,
1922 u8 compression, u8 encryption,
1923 u16 other_encoding, int extent_type)
1924{
1925 struct btrfs_root *root = BTRFS_I(inode)->root;
1926 struct btrfs_file_extent_item *fi;
1927 struct btrfs_path *path;
1928 struct extent_buffer *leaf;
1929 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001930 int ret;
1931
1932 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001933 if (!path)
1934 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001935
Chris Masonb9473432009-03-13 11:00:37 -04001936 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001937
1938 /*
1939 * we may be replacing one extent in the tree with another.
1940 * The new extent is pinned in the extent map, and we don't want
1941 * to drop it from the cache until it is completely in the btree.
1942 *
1943 * So, tell btrfs_drop_extents to leave this extent in the cache.
1944 * the caller is expected to unpin it and allow it to be merged
1945 * with the others.
1946 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001947 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001948 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001949 if (ret)
1950 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001951
Li Zefan33345d012011-04-20 10:31:50 +08001952 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001953 ins.offset = file_pos;
1954 ins.type = BTRFS_EXTENT_DATA_KEY;
1955 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001956 if (ret)
1957 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001958 leaf = path->nodes[0];
1959 fi = btrfs_item_ptr(leaf, path->slots[0],
1960 struct btrfs_file_extent_item);
1961 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1962 btrfs_set_file_extent_type(leaf, fi, extent_type);
1963 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1964 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1965 btrfs_set_file_extent_offset(leaf, fi, 0);
1966 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1967 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1968 btrfs_set_file_extent_compression(leaf, fi, compression);
1969 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1970 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001971
Yan Zhengd899e052008-10-30 14:25:28 -04001972 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001973 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001974
1975 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001976
1977 ins.objectid = disk_bytenr;
1978 ins.offset = disk_num_bytes;
1979 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001980 ret = btrfs_alloc_reserved_file_extent(trans, root,
1981 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001982 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001983out:
Yan Zhengd899e052008-10-30 14:25:28 -04001984 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001985
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001986 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001987}
1988
Liu Bo38c227d2013-01-29 03:18:40 +00001989/* snapshot-aware defrag */
1990struct sa_defrag_extent_backref {
1991 struct rb_node node;
1992 struct old_sa_defrag_extent *old;
1993 u64 root_id;
1994 u64 inum;
1995 u64 file_pos;
1996 u64 extent_offset;
1997 u64 num_bytes;
1998 u64 generation;
1999};
2000
2001struct old_sa_defrag_extent {
2002 struct list_head list;
2003 struct new_sa_defrag_extent *new;
2004
2005 u64 extent_offset;
2006 u64 bytenr;
2007 u64 offset;
2008 u64 len;
2009 int count;
2010};
2011
2012struct new_sa_defrag_extent {
2013 struct rb_root root;
2014 struct list_head head;
2015 struct btrfs_path *path;
2016 struct inode *inode;
2017 u64 file_pos;
2018 u64 len;
2019 u64 bytenr;
2020 u64 disk_len;
2021 u8 compress_type;
2022};
2023
2024static int backref_comp(struct sa_defrag_extent_backref *b1,
2025 struct sa_defrag_extent_backref *b2)
2026{
2027 if (b1->root_id < b2->root_id)
2028 return -1;
2029 else if (b1->root_id > b2->root_id)
2030 return 1;
2031
2032 if (b1->inum < b2->inum)
2033 return -1;
2034 else if (b1->inum > b2->inum)
2035 return 1;
2036
2037 if (b1->file_pos < b2->file_pos)
2038 return -1;
2039 else if (b1->file_pos > b2->file_pos)
2040 return 1;
2041
2042 /*
2043 * [------------------------------] ===> (a range of space)
2044 * |<--->| |<---->| =============> (fs/file tree A)
2045 * |<---------------------------->| ===> (fs/file tree B)
2046 *
2047 * A range of space can refer to two file extents in one tree while
2048 * refer to only one file extent in another tree.
2049 *
2050 * So we may process a disk offset more than one time(two extents in A)
2051 * and locate at the same extent(one extent in B), then insert two same
2052 * backrefs(both refer to the extent in B).
2053 */
2054 return 0;
2055}
2056
2057static void backref_insert(struct rb_root *root,
2058 struct sa_defrag_extent_backref *backref)
2059{
2060 struct rb_node **p = &root->rb_node;
2061 struct rb_node *parent = NULL;
2062 struct sa_defrag_extent_backref *entry;
2063 int ret;
2064
2065 while (*p) {
2066 parent = *p;
2067 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2068
2069 ret = backref_comp(backref, entry);
2070 if (ret < 0)
2071 p = &(*p)->rb_left;
2072 else
2073 p = &(*p)->rb_right;
2074 }
2075
2076 rb_link_node(&backref->node, parent, p);
2077 rb_insert_color(&backref->node, root);
2078}
2079
2080/*
2081 * Note the backref might has changed, and in this case we just return 0.
2082 */
2083static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2084 void *ctx)
2085{
2086 struct btrfs_file_extent_item *extent;
2087 struct btrfs_fs_info *fs_info;
2088 struct old_sa_defrag_extent *old = ctx;
2089 struct new_sa_defrag_extent *new = old->new;
2090 struct btrfs_path *path = new->path;
2091 struct btrfs_key key;
2092 struct btrfs_root *root;
2093 struct sa_defrag_extent_backref *backref;
2094 struct extent_buffer *leaf;
2095 struct inode *inode = new->inode;
2096 int slot;
2097 int ret;
2098 u64 extent_offset;
2099 u64 num_bytes;
2100
2101 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2102 inum == btrfs_ino(inode))
2103 return 0;
2104
2105 key.objectid = root_id;
2106 key.type = BTRFS_ROOT_ITEM_KEY;
2107 key.offset = (u64)-1;
2108
2109 fs_info = BTRFS_I(inode)->root->fs_info;
2110 root = btrfs_read_fs_root_no_name(fs_info, &key);
2111 if (IS_ERR(root)) {
2112 if (PTR_ERR(root) == -ENOENT)
2113 return 0;
2114 WARN_ON(1);
2115 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2116 inum, offset, root_id);
2117 return PTR_ERR(root);
2118 }
2119
2120 key.objectid = inum;
2121 key.type = BTRFS_EXTENT_DATA_KEY;
2122 if (offset > (u64)-1 << 32)
2123 key.offset = 0;
2124 else
2125 key.offset = offset;
2126
2127 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2128 if (ret < 0) {
2129 WARN_ON(1);
2130 return ret;
2131 }
2132
2133 while (1) {
2134 cond_resched();
2135
2136 leaf = path->nodes[0];
2137 slot = path->slots[0];
2138
2139 if (slot >= btrfs_header_nritems(leaf)) {
2140 ret = btrfs_next_leaf(root, path);
2141 if (ret < 0) {
2142 goto out;
2143 } else if (ret > 0) {
2144 ret = 0;
2145 goto out;
2146 }
2147 continue;
2148 }
2149
2150 path->slots[0]++;
2151
2152 btrfs_item_key_to_cpu(leaf, &key, slot);
2153
2154 if (key.objectid > inum)
2155 goto out;
2156
2157 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2158 continue;
2159
2160 extent = btrfs_item_ptr(leaf, slot,
2161 struct btrfs_file_extent_item);
2162
2163 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2164 continue;
2165
2166 extent_offset = btrfs_file_extent_offset(leaf, extent);
2167 if (key.offset - extent_offset != offset)
2168 continue;
2169
2170 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2171 if (extent_offset >= old->extent_offset + old->offset +
2172 old->len || extent_offset + num_bytes <=
2173 old->extent_offset + old->offset)
2174 continue;
2175
2176 break;
2177 }
2178
2179 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2180 if (!backref) {
2181 ret = -ENOENT;
2182 goto out;
2183 }
2184
2185 backref->root_id = root_id;
2186 backref->inum = inum;
2187 backref->file_pos = offset + extent_offset;
2188 backref->num_bytes = num_bytes;
2189 backref->extent_offset = extent_offset;
2190 backref->generation = btrfs_file_extent_generation(leaf, extent);
2191 backref->old = old;
2192 backref_insert(&new->root, backref);
2193 old->count++;
2194out:
2195 btrfs_release_path(path);
2196 WARN_ON(ret);
2197 return ret;
2198}
2199
2200static noinline bool record_extent_backrefs(struct btrfs_path *path,
2201 struct new_sa_defrag_extent *new)
2202{
2203 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2204 struct old_sa_defrag_extent *old, *tmp;
2205 int ret;
2206
2207 new->path = path;
2208
2209 list_for_each_entry_safe(old, tmp, &new->head, list) {
2210 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2211 path, record_one_backref,
2212 old);
2213 BUG_ON(ret < 0 && ret != -ENOENT);
2214
2215 /* no backref to be processed for this extent */
2216 if (!old->count) {
2217 list_del(&old->list);
2218 kfree(old);
2219 }
2220 }
2221
2222 if (list_empty(&new->head))
2223 return false;
2224
2225 return true;
2226}
2227
2228static int relink_is_mergable(struct extent_buffer *leaf,
2229 struct btrfs_file_extent_item *fi,
2230 u64 disk_bytenr)
2231{
2232 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2233 return 0;
2234
2235 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2236 return 0;
2237
2238 if (btrfs_file_extent_compression(leaf, fi) ||
2239 btrfs_file_extent_encryption(leaf, fi) ||
2240 btrfs_file_extent_other_encoding(leaf, fi))
2241 return 0;
2242
2243 return 1;
2244}
2245
2246/*
2247 * Note the backref might has changed, and in this case we just return 0.
2248 */
2249static noinline int relink_extent_backref(struct btrfs_path *path,
2250 struct sa_defrag_extent_backref *prev,
2251 struct sa_defrag_extent_backref *backref)
2252{
2253 struct btrfs_file_extent_item *extent;
2254 struct btrfs_file_extent_item *item;
2255 struct btrfs_ordered_extent *ordered;
2256 struct btrfs_trans_handle *trans;
2257 struct btrfs_fs_info *fs_info;
2258 struct btrfs_root *root;
2259 struct btrfs_key key;
2260 struct extent_buffer *leaf;
2261 struct old_sa_defrag_extent *old = backref->old;
2262 struct new_sa_defrag_extent *new = old->new;
2263 struct inode *src_inode = new->inode;
2264 struct inode *inode;
2265 struct extent_state *cached = NULL;
2266 int ret = 0;
2267 u64 start;
2268 u64 len;
2269 u64 lock_start;
2270 u64 lock_end;
2271 bool merge = false;
2272 int index;
2273
2274 if (prev && prev->root_id == backref->root_id &&
2275 prev->inum == backref->inum &&
2276 prev->file_pos + prev->num_bytes == backref->file_pos)
2277 merge = true;
2278
2279 /* step 1: get root */
2280 key.objectid = backref->root_id;
2281 key.type = BTRFS_ROOT_ITEM_KEY;
2282 key.offset = (u64)-1;
2283
2284 fs_info = BTRFS_I(src_inode)->root->fs_info;
2285 index = srcu_read_lock(&fs_info->subvol_srcu);
2286
2287 root = btrfs_read_fs_root_no_name(fs_info, &key);
2288 if (IS_ERR(root)) {
2289 srcu_read_unlock(&fs_info->subvol_srcu, index);
2290 if (PTR_ERR(root) == -ENOENT)
2291 return 0;
2292 return PTR_ERR(root);
2293 }
Liu Bo38c227d2013-01-29 03:18:40 +00002294
2295 /* step 2: get inode */
2296 key.objectid = backref->inum;
2297 key.type = BTRFS_INODE_ITEM_KEY;
2298 key.offset = 0;
2299
2300 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2301 if (IS_ERR(inode)) {
2302 srcu_read_unlock(&fs_info->subvol_srcu, index);
2303 return 0;
2304 }
2305
2306 srcu_read_unlock(&fs_info->subvol_srcu, index);
2307
2308 /* step 3: relink backref */
2309 lock_start = backref->file_pos;
2310 lock_end = backref->file_pos + backref->num_bytes - 1;
2311 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2312 0, &cached);
2313
2314 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2315 if (ordered) {
2316 btrfs_put_ordered_extent(ordered);
2317 goto out_unlock;
2318 }
2319
2320 trans = btrfs_join_transaction(root);
2321 if (IS_ERR(trans)) {
2322 ret = PTR_ERR(trans);
2323 goto out_unlock;
2324 }
2325
2326 key.objectid = backref->inum;
2327 key.type = BTRFS_EXTENT_DATA_KEY;
2328 key.offset = backref->file_pos;
2329
2330 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2331 if (ret < 0) {
2332 goto out_free_path;
2333 } else if (ret > 0) {
2334 ret = 0;
2335 goto out_free_path;
2336 }
2337
2338 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2339 struct btrfs_file_extent_item);
2340
2341 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2342 backref->generation)
2343 goto out_free_path;
2344
2345 btrfs_release_path(path);
2346
2347 start = backref->file_pos;
2348 if (backref->extent_offset < old->extent_offset + old->offset)
2349 start += old->extent_offset + old->offset -
2350 backref->extent_offset;
2351
2352 len = min(backref->extent_offset + backref->num_bytes,
2353 old->extent_offset + old->offset + old->len);
2354 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2355
2356 ret = btrfs_drop_extents(trans, root, inode, start,
2357 start + len, 1);
2358 if (ret)
2359 goto out_free_path;
2360again:
2361 key.objectid = btrfs_ino(inode);
2362 key.type = BTRFS_EXTENT_DATA_KEY;
2363 key.offset = start;
2364
Liu Boa09a0a72013-03-11 09:20:58 +00002365 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002366 if (merge) {
2367 struct btrfs_file_extent_item *fi;
2368 u64 extent_len;
2369 struct btrfs_key found_key;
2370
2371 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2372 if (ret < 0)
2373 goto out_free_path;
2374
2375 path->slots[0]--;
2376 leaf = path->nodes[0];
2377 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2378
2379 fi = btrfs_item_ptr(leaf, path->slots[0],
2380 struct btrfs_file_extent_item);
2381 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2382
2383 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2384 extent_len + found_key.offset == start) {
2385 btrfs_set_file_extent_num_bytes(leaf, fi,
2386 extent_len + len);
2387 btrfs_mark_buffer_dirty(leaf);
2388 inode_add_bytes(inode, len);
2389
2390 ret = 1;
2391 goto out_free_path;
2392 } else {
2393 merge = false;
2394 btrfs_release_path(path);
2395 goto again;
2396 }
2397 }
2398
2399 ret = btrfs_insert_empty_item(trans, root, path, &key,
2400 sizeof(*extent));
2401 if (ret) {
2402 btrfs_abort_transaction(trans, root, ret);
2403 goto out_free_path;
2404 }
2405
2406 leaf = path->nodes[0];
2407 item = btrfs_item_ptr(leaf, path->slots[0],
2408 struct btrfs_file_extent_item);
2409 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2410 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2411 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2412 btrfs_set_file_extent_num_bytes(leaf, item, len);
2413 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2414 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2415 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2416 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2417 btrfs_set_file_extent_encryption(leaf, item, 0);
2418 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2419
2420 btrfs_mark_buffer_dirty(leaf);
2421 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002422 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002423
2424 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2425 new->disk_len, 0,
2426 backref->root_id, backref->inum,
2427 new->file_pos, 0); /* start - extent_offset */
2428 if (ret) {
2429 btrfs_abort_transaction(trans, root, ret);
2430 goto out_free_path;
2431 }
2432
2433 ret = 1;
2434out_free_path:
2435 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002436 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002437 btrfs_end_transaction(trans, root);
2438out_unlock:
2439 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2440 &cached, GFP_NOFS);
2441 iput(inode);
2442 return ret;
2443}
2444
2445static void relink_file_extents(struct new_sa_defrag_extent *new)
2446{
2447 struct btrfs_path *path;
2448 struct old_sa_defrag_extent *old, *tmp;
2449 struct sa_defrag_extent_backref *backref;
2450 struct sa_defrag_extent_backref *prev = NULL;
2451 struct inode *inode;
2452 struct btrfs_root *root;
2453 struct rb_node *node;
2454 int ret;
2455
2456 inode = new->inode;
2457 root = BTRFS_I(inode)->root;
2458
2459 path = btrfs_alloc_path();
2460 if (!path)
2461 return;
2462
2463 if (!record_extent_backrefs(path, new)) {
2464 btrfs_free_path(path);
2465 goto out;
2466 }
2467 btrfs_release_path(path);
2468
2469 while (1) {
2470 node = rb_first(&new->root);
2471 if (!node)
2472 break;
2473 rb_erase(node, &new->root);
2474
2475 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2476
2477 ret = relink_extent_backref(path, prev, backref);
2478 WARN_ON(ret < 0);
2479
2480 kfree(prev);
2481
2482 if (ret == 1)
2483 prev = backref;
2484 else
2485 prev = NULL;
2486 cond_resched();
2487 }
2488 kfree(prev);
2489
2490 btrfs_free_path(path);
2491
2492 list_for_each_entry_safe(old, tmp, &new->head, list) {
2493 list_del(&old->list);
2494 kfree(old);
2495 }
2496out:
2497 atomic_dec(&root->fs_info->defrag_running);
2498 wake_up(&root->fs_info->transaction_wait);
2499
2500 kfree(new);
2501}
2502
2503static struct new_sa_defrag_extent *
2504record_old_file_extents(struct inode *inode,
2505 struct btrfs_ordered_extent *ordered)
2506{
2507 struct btrfs_root *root = BTRFS_I(inode)->root;
2508 struct btrfs_path *path;
2509 struct btrfs_key key;
2510 struct old_sa_defrag_extent *old, *tmp;
2511 struct new_sa_defrag_extent *new;
2512 int ret;
2513
2514 new = kmalloc(sizeof(*new), GFP_NOFS);
2515 if (!new)
2516 return NULL;
2517
2518 new->inode = inode;
2519 new->file_pos = ordered->file_offset;
2520 new->len = ordered->len;
2521 new->bytenr = ordered->start;
2522 new->disk_len = ordered->disk_len;
2523 new->compress_type = ordered->compress_type;
2524 new->root = RB_ROOT;
2525 INIT_LIST_HEAD(&new->head);
2526
2527 path = btrfs_alloc_path();
2528 if (!path)
2529 goto out_kfree;
2530
2531 key.objectid = btrfs_ino(inode);
2532 key.type = BTRFS_EXTENT_DATA_KEY;
2533 key.offset = new->file_pos;
2534
2535 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2536 if (ret < 0)
2537 goto out_free_path;
2538 if (ret > 0 && path->slots[0] > 0)
2539 path->slots[0]--;
2540
2541 /* find out all the old extents for the file range */
2542 while (1) {
2543 struct btrfs_file_extent_item *extent;
2544 struct extent_buffer *l;
2545 int slot;
2546 u64 num_bytes;
2547 u64 offset;
2548 u64 end;
2549 u64 disk_bytenr;
2550 u64 extent_offset;
2551
2552 l = path->nodes[0];
2553 slot = path->slots[0];
2554
2555 if (slot >= btrfs_header_nritems(l)) {
2556 ret = btrfs_next_leaf(root, path);
2557 if (ret < 0)
2558 goto out_free_list;
2559 else if (ret > 0)
2560 break;
2561 continue;
2562 }
2563
2564 btrfs_item_key_to_cpu(l, &key, slot);
2565
2566 if (key.objectid != btrfs_ino(inode))
2567 break;
2568 if (key.type != BTRFS_EXTENT_DATA_KEY)
2569 break;
2570 if (key.offset >= new->file_pos + new->len)
2571 break;
2572
2573 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2574
2575 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2576 if (key.offset + num_bytes < new->file_pos)
2577 goto next;
2578
2579 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2580 if (!disk_bytenr)
2581 goto next;
2582
2583 extent_offset = btrfs_file_extent_offset(l, extent);
2584
2585 old = kmalloc(sizeof(*old), GFP_NOFS);
2586 if (!old)
2587 goto out_free_list;
2588
2589 offset = max(new->file_pos, key.offset);
2590 end = min(new->file_pos + new->len, key.offset + num_bytes);
2591
2592 old->bytenr = disk_bytenr;
2593 old->extent_offset = extent_offset;
2594 old->offset = offset - key.offset;
2595 old->len = end - offset;
2596 old->new = new;
2597 old->count = 0;
2598 list_add_tail(&old->list, &new->head);
2599next:
2600 path->slots[0]++;
2601 cond_resched();
2602 }
2603
2604 btrfs_free_path(path);
2605 atomic_inc(&root->fs_info->defrag_running);
2606
2607 return new;
2608
2609out_free_list:
2610 list_for_each_entry_safe(old, tmp, &new->head, list) {
2611 list_del(&old->list);
2612 kfree(old);
2613 }
2614out_free_path:
2615 btrfs_free_path(path);
2616out_kfree:
2617 kfree(new);
2618 return NULL;
2619}
2620
Chris Mason5d13a982009-03-13 11:41:46 -04002621/*
2622 * helper function for btrfs_finish_ordered_io, this
2623 * just reads in some of the csum leaves to prime them into ram
2624 * before we start the transaction. It limits the amount of btree
2625 * reads required while inside the transaction.
2626 */
Chris Masond352ac62008-09-29 15:18:18 -04002627/* as ordered data IO finishes, this gets called so we can finish
2628 * an ordered extent if the range of bytes in the file it covers are
2629 * fully written.
2630 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002631static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002632{
Josef Bacik5fd02042012-05-02 14:00:54 -04002633 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002634 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002635 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002636 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002637 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002638 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002639 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002640 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002641 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642
Liu Bo83eea1f2012-07-10 05:28:39 -06002643 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002644
Josef Bacik5fd02042012-05-02 14:00:54 -04002645 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2646 ret = -EIO;
2647 goto out;
2648 }
2649
Yan, Zhengc2167752009-11-12 09:34:21 +00002650 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002652 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2653 if (nolock)
2654 trans = btrfs_join_transaction_nolock(root);
2655 else
2656 trans = btrfs_join_transaction(root);
2657 if (IS_ERR(trans)) {
2658 ret = PTR_ERR(trans);
2659 trans = NULL;
2660 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002661 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002662 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2663 ret = btrfs_update_inode_fallback(trans, root, inode);
2664 if (ret) /* -ENOMEM or corruption */
2665 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002666 goto out;
2667 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002668
Josef Bacik2ac55d42010-02-03 19:33:23 +00002669 lock_extent_bits(io_tree, ordered_extent->file_offset,
2670 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002671 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002672
Liu Bo38c227d2013-01-29 03:18:40 +00002673 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2674 ordered_extent->file_offset + ordered_extent->len - 1,
2675 EXTENT_DEFRAG, 1, cached_state);
2676 if (ret) {
2677 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2678 if (last_snapshot >= BTRFS_I(inode)->generation)
2679 /* the inode is shared */
2680 new = record_old_file_extents(inode, ordered_extent);
2681
2682 clear_extent_bit(io_tree, ordered_extent->file_offset,
2683 ordered_extent->file_offset + ordered_extent->len - 1,
2684 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2685 }
2686
Josef Bacik0cb59c92010-07-02 12:14:14 -04002687 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002688 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002689 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002690 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002691 if (IS_ERR(trans)) {
2692 ret = PTR_ERR(trans);
2693 trans = NULL;
2694 goto out_unlock;
2695 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002696 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002697
Chris Masonc8b97812008-10-29 14:49:59 -04002698 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002699 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002700 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002701 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002702 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002703 ordered_extent->file_offset,
2704 ordered_extent->file_offset +
2705 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002706 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002707 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002708 ret = insert_reserved_file_extent(trans, inode,
2709 ordered_extent->file_offset,
2710 ordered_extent->start,
2711 ordered_extent->disk_len,
2712 ordered_extent->len,
2713 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002714 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002715 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002716 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002717 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2718 ordered_extent->file_offset, ordered_extent->len,
2719 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002720 if (ret < 0) {
2721 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002722 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002723 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002724
Chris Masone6dcd2d2008-07-17 12:53:50 -04002725 add_pending_csums(trans, inode, ordered_extent->file_offset,
2726 &ordered_extent->list);
2727
Josef Bacik6c760c02012-11-09 10:53:21 -05002728 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2729 ret = btrfs_update_inode_fallback(trans, root, inode);
2730 if (ret) { /* -ENOMEM or corruption */
2731 btrfs_abort_transaction(trans, root, ret);
2732 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002733 }
2734 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002735out_unlock:
2736 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2737 ordered_extent->file_offset +
2738 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002739out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002740 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002741 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002742 if (trans)
2743 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002744
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002745 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002746 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2747 ordered_extent->file_offset +
2748 ordered_extent->len - 1, NULL, GFP_NOFS);
2749
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002750 /*
2751 * If the ordered extent had an IOERR or something else went
2752 * wrong we need to return the space for this ordered extent
2753 * back to the allocator.
2754 */
2755 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2756 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2757 btrfs_free_reserved_extent(root, ordered_extent->start,
2758 ordered_extent->disk_len);
2759 }
2760
2761
Josef Bacik5fd02042012-05-02 14:00:54 -04002762 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002763 * This needs to be done to make sure anybody waiting knows we are done
2764 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002765 */
2766 btrfs_remove_ordered_extent(inode, ordered_extent);
2767
Liu Bo38c227d2013-01-29 03:18:40 +00002768 /* for snapshot-aware defrag */
2769 if (new)
2770 relink_file_extents(new);
2771
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772 /* once for us */
2773 btrfs_put_ordered_extent(ordered_extent);
2774 /* once for the tree */
2775 btrfs_put_ordered_extent(ordered_extent);
2776
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 return ret;
2778}
2779
2780static void finish_ordered_fn(struct btrfs_work *work)
2781{
2782 struct btrfs_ordered_extent *ordered_extent;
2783 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2784 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002785}
2786
Christoph Hellwigb2950862008-12-02 09:54:17 -05002787static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002788 struct extent_state *state, int uptodate)
2789{
Josef Bacik5fd02042012-05-02 14:00:54 -04002790 struct inode *inode = page->mapping->host;
2791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct btrfs_ordered_extent *ordered_extent = NULL;
2793 struct btrfs_workers *workers;
2794
liubo1abe9b82011-03-24 11:18:59 +00002795 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2796
Chris Mason8b62b722009-09-02 16:53:46 -04002797 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002798 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2799 end - start + 1, uptodate))
2800 return 0;
2801
2802 ordered_extent->work.func = finish_ordered_fn;
2803 ordered_extent->work.flags = 0;
2804
Liu Bo83eea1f2012-07-10 05:28:39 -06002805 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002806 workers = &root->fs_info->endio_freespace_worker;
2807 else
2808 workers = &root->fs_info->endio_write_workers;
2809 btrfs_queue_worker(workers, &ordered_extent->work);
2810
2811 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002812}
2813
Chris Masond352ac62008-09-29 15:18:18 -04002814/*
Chris Masond352ac62008-09-29 15:18:18 -04002815 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002816 * if there's a match, we allow the bio to finish. If not, the code in
2817 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002818 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002819static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002820 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002821{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002822 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002823 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002824 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002825 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002826 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002827 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002828 struct btrfs_root *root = BTRFS_I(inode)->root;
2829 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002830 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2831 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002832
Chris Masond20f7042008-12-08 16:58:54 -05002833 if (PageChecked(page)) {
2834 ClearPageChecked(page);
2835 goto good;
2836 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002837
2838 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002839 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002840
Yan Zheng17d217f2008-12-12 10:03:38 -05002841 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002842 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002843 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2844 GFP_NOFS);
2845 return 0;
2846 }
2847
Yanc2e639f2008-02-04 08:57:25 -05002848 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002849 private = state->private;
2850 ret = 0;
2851 } else {
2852 ret = get_state_private(io_tree, start, &private);
2853 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002854 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002855 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002856 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002857
Liu Bob0496682013-03-14 14:57:45 +00002858 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002859 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002860 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002861 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002862
Cong Wang7ac687d2011-11-25 23:14:28 +08002863 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002864good:
Chris Mason07157aa2007-08-30 08:50:51 -04002865 return 0;
2866
2867zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002868 if (__ratelimit(&_rs))
2869 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2870 (unsigned long long)btrfs_ino(page->mapping->host),
2871 (unsigned long long)start, csum,
2872 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002873 memset(kaddr + offset, 1, end - start + 1);
2874 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002875 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002876 if (private == 0)
2877 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002878 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002879}
Chris Masonb888db2b2007-08-27 16:49:44 -04002880
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002881struct delayed_iput {
2882 struct list_head list;
2883 struct inode *inode;
2884};
2885
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002886/* JDM: If this is fs-wide, why can't we add a pointer to
2887 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888void btrfs_add_delayed_iput(struct inode *inode)
2889{
2890 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2891 struct delayed_iput *delayed;
2892
2893 if (atomic_add_unless(&inode->i_count, -1, 1))
2894 return;
2895
2896 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2897 delayed->inode = inode;
2898
2899 spin_lock(&fs_info->delayed_iput_lock);
2900 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2901 spin_unlock(&fs_info->delayed_iput_lock);
2902}
2903
2904void btrfs_run_delayed_iputs(struct btrfs_root *root)
2905{
2906 LIST_HEAD(list);
2907 struct btrfs_fs_info *fs_info = root->fs_info;
2908 struct delayed_iput *delayed;
2909 int empty;
2910
2911 spin_lock(&fs_info->delayed_iput_lock);
2912 empty = list_empty(&fs_info->delayed_iputs);
2913 spin_unlock(&fs_info->delayed_iput_lock);
2914 if (empty)
2915 return;
2916
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002917 spin_lock(&fs_info->delayed_iput_lock);
2918 list_splice_init(&fs_info->delayed_iputs, &list);
2919 spin_unlock(&fs_info->delayed_iput_lock);
2920
2921 while (!list_empty(&list)) {
2922 delayed = list_entry(list.next, struct delayed_iput, list);
2923 list_del(&delayed->list);
2924 iput(delayed->inode);
2925 kfree(delayed);
2926 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002927}
2928
Yan, Zhengd68fc572010-05-16 10:49:58 -04002929/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002930 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931 * files in the subvolume, it removes orphan item and frees block_rsv
2932 * structure.
2933 */
2934void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2935 struct btrfs_root *root)
2936{
Josef Bacik90290e12011-12-02 15:44:12 -05002937 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002938 int ret;
2939
Josef Bacik8a35d952012-05-23 14:26:42 -04002940 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2942 return;
2943
Josef Bacik90290e12011-12-02 15:44:12 -05002944 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002945 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002946 spin_unlock(&root->orphan_lock);
2947 return;
2948 }
2949
2950 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2951 spin_unlock(&root->orphan_lock);
2952 return;
2953 }
2954
2955 block_rsv = root->orphan_block_rsv;
2956 root->orphan_block_rsv = NULL;
2957 spin_unlock(&root->orphan_lock);
2958
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 if (root->orphan_item_inserted &&
2960 btrfs_root_refs(&root->root_item) > 0) {
2961 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2962 root->root_key.objectid);
2963 BUG_ON(ret);
2964 root->orphan_item_inserted = 0;
2965 }
2966
Josef Bacik90290e12011-12-02 15:44:12 -05002967 if (block_rsv) {
2968 WARN_ON(block_rsv->size > 0);
2969 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970 }
2971}
2972
2973/*
Josef Bacik7b128762008-07-24 12:17:14 -04002974 * This creates an orphan entry for the given inode in case something goes
2975 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002976 *
2977 * NOTE: caller of this function should reserve 5 units of metadata for
2978 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002979 */
2980int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2981{
2982 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002983 struct btrfs_block_rsv *block_rsv = NULL;
2984 int reserve = 0;
2985 int insert = 0;
2986 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002987
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002989 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002990 if (!block_rsv)
2991 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002992 }
2993
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 spin_lock(&root->orphan_lock);
2995 if (!root->orphan_block_rsv) {
2996 root->orphan_block_rsv = block_rsv;
2997 } else if (block_rsv) {
2998 btrfs_free_block_rsv(root, block_rsv);
2999 block_rsv = NULL;
3000 }
Josef Bacik7b128762008-07-24 12:17:14 -04003001
Josef Bacik8a35d952012-05-23 14:26:42 -04003002 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3003 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003004#if 0
3005 /*
3006 * For proper ENOSPC handling, we should do orphan
3007 * cleanup when mounting. But this introduces backward
3008 * compatibility issue.
3009 */
3010 if (!xchg(&root->orphan_item_inserted, 1))
3011 insert = 2;
3012 else
3013 insert = 1;
3014#endif
3015 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003016 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003017 }
Josef Bacik7b128762008-07-24 12:17:14 -04003018
Josef Bacik72ac3c02012-05-23 14:13:11 -04003019 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3020 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003022 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003023
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 /* grab metadata reservation from transaction handle */
3025 if (reserve) {
3026 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 }
3029
3030 /* insert an orphan item to track this unlinked/truncated file */
3031 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003032 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003033 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003034 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3035 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003036 btrfs_abort_transaction(trans, root, ret);
3037 return ret;
3038 }
3039 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 }
3041
3042 /* insert an orphan item to track subvolume contains orphan files */
3043 if (insert >= 2) {
3044 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3045 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003046 if (ret && ret != -EEXIST) {
3047 btrfs_abort_transaction(trans, root, ret);
3048 return ret;
3049 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 }
3051 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003052}
3053
3054/*
3055 * We have done the truncate/delete so we can go ahead and remove the orphan
3056 * item for this particular inode.
3057 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003058static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3059 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003060{
3061 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 int delete_item = 0;
3063 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003064 int ret = 0;
3065
Yan, Zhengd68fc572010-05-16 10:49:58 -04003066 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003067 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3068 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003069 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003070
Josef Bacik72ac3c02012-05-23 14:13:11 -04003071 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3072 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003073 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 spin_unlock(&root->orphan_lock);
3075
3076 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003077 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003078 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003079 }
3080
Josef Bacik8a35d952012-05-23 14:26:42 -04003081 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003082 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003083 atomic_dec(&root->orphan_inodes);
3084 }
Josef Bacik7b128762008-07-24 12:17:14 -04003085
Yan, Zhengd68fc572010-05-16 10:49:58 -04003086 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003087}
3088
3089/*
3090 * this cleans up any orphans that may be left on the list from the last use
3091 * of this root.
3092 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003093int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003094{
3095 struct btrfs_path *path;
3096 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003097 struct btrfs_key key, found_key;
3098 struct btrfs_trans_handle *trans;
3099 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003100 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003101 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3102
Yan, Zhengd68fc572010-05-16 10:49:58 -04003103 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003104 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003105
3106 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003107 if (!path) {
3108 ret = -ENOMEM;
3109 goto out;
3110 }
Josef Bacik7b128762008-07-24 12:17:14 -04003111 path->reada = -1;
3112
3113 key.objectid = BTRFS_ORPHAN_OBJECTID;
3114 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3115 key.offset = (u64)-1;
3116
Josef Bacik7b128762008-07-24 12:17:14 -04003117 while (1) {
3118 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003119 if (ret < 0)
3120 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003121
3122 /*
3123 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003124 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003125 * find the key and see if we have stuff that matches
3126 */
3127 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003128 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003129 if (path->slots[0] == 0)
3130 break;
3131 path->slots[0]--;
3132 }
3133
3134 /* pull out the item */
3135 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003136 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3137
3138 /* make sure the item matches what we want */
3139 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3140 break;
3141 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3142 break;
3143
3144 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003145 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003146
3147 /*
3148 * this is where we are basically btrfs_lookup, without the
3149 * crossing root thing. we store the inode number in the
3150 * offset of the orphan item.
3151 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003152
3153 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003154 btrfs_err(root->fs_info,
3155 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003156 ret = -EINVAL;
3157 goto out;
3158 }
3159
3160 last_objectid = found_key.offset;
3161
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003162 found_key.objectid = found_key.offset;
3163 found_key.type = BTRFS_INODE_ITEM_KEY;
3164 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003165 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003166 ret = PTR_RET(inode);
3167 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003168 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003169
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003170 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3171 struct btrfs_root *dead_root;
3172 struct btrfs_fs_info *fs_info = root->fs_info;
3173 int is_dead_root = 0;
3174
3175 /*
3176 * this is an orphan in the tree root. Currently these
3177 * could come from 2 sources:
3178 * a) a snapshot deletion in progress
3179 * b) a free space cache inode
3180 * We need to distinguish those two, as the snapshot
3181 * orphan must not get deleted.
3182 * find_dead_roots already ran before us, so if this
3183 * is a snapshot deletion, we should find the root
3184 * in the dead_roots list
3185 */
3186 spin_lock(&fs_info->trans_lock);
3187 list_for_each_entry(dead_root, &fs_info->dead_roots,
3188 root_list) {
3189 if (dead_root->root_key.objectid ==
3190 found_key.objectid) {
3191 is_dead_root = 1;
3192 break;
3193 }
3194 }
3195 spin_unlock(&fs_info->trans_lock);
3196 if (is_dead_root) {
3197 /* prevent this orphan from being found again */
3198 key.offset = found_key.objectid - 1;
3199 continue;
3200 }
3201 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003202 /*
3203 * Inode is already gone but the orphan item is still there,
3204 * kill the orphan item.
3205 */
3206 if (ret == -ESTALE) {
3207 trans = btrfs_start_transaction(root, 1);
3208 if (IS_ERR(trans)) {
3209 ret = PTR_ERR(trans);
3210 goto out;
3211 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003212 btrfs_debug(root->fs_info, "auto deleting %Lu",
3213 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003214 ret = btrfs_del_orphan_item(trans, root,
3215 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003216 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003217 btrfs_end_transaction(trans, root);
3218 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003219 }
Josef Bacik7b128762008-07-24 12:17:14 -04003220
Josef Bacik7b128762008-07-24 12:17:14 -04003221 /*
3222 * add this inode to the orphan list so btrfs_orphan_del does
3223 * the proper thing when we hit it
3224 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003225 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3226 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003227 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003228
Josef Bacik7b128762008-07-24 12:17:14 -04003229 /* if we have links, this was a truncate, lets do that */
3230 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003231 if (!S_ISREG(inode->i_mode)) {
3232 WARN_ON(1);
3233 iput(inode);
3234 continue;
3235 }
Josef Bacik7b128762008-07-24 12:17:14 -04003236 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003237
3238 /* 1 for the orphan item deletion. */
3239 trans = btrfs_start_transaction(root, 1);
3240 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003241 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003242 ret = PTR_ERR(trans);
3243 goto out;
3244 }
3245 ret = btrfs_orphan_add(trans, inode);
3246 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003247 if (ret) {
3248 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003249 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003250 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003251
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003252 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003253 if (ret)
3254 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003255 } else {
3256 nr_unlink++;
3257 }
3258
3259 /* this will do delete_inode and everything for us */
3260 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003261 if (ret)
3262 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003263 }
Miao Xie3254c872011-11-10 20:45:05 -05003264 /* release the path since we're done with it */
3265 btrfs_release_path(path);
3266
Yan, Zhengd68fc572010-05-16 10:49:58 -04003267 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3268
3269 if (root->orphan_block_rsv)
3270 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3271 (u64)-1);
3272
3273 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003274 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003275 if (!IS_ERR(trans))
3276 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 }
Josef Bacik7b128762008-07-24 12:17:14 -04003278
3279 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003280 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003281 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003282 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003283
3284out:
3285 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003286 btrfs_crit(root->fs_info,
3287 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003288 btrfs_free_path(path);
3289 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003290}
3291
Chris Masond352ac62008-09-29 15:18:18 -04003292/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003293 * very simple check to peek ahead in the leaf looking for xattrs. If we
3294 * don't find any xattrs, we know there can't be any acls.
3295 *
3296 * slot is the slot the inode is in, objectid is the objectid of the inode
3297 */
3298static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3299 int slot, u64 objectid)
3300{
3301 u32 nritems = btrfs_header_nritems(leaf);
3302 struct btrfs_key found_key;
3303 int scanned = 0;
3304
3305 slot++;
3306 while (slot < nritems) {
3307 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3308
3309 /* we found a different objectid, there must not be acls */
3310 if (found_key.objectid != objectid)
3311 return 0;
3312
3313 /* we found an xattr, assume we've got an acl */
3314 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3315 return 1;
3316
3317 /*
3318 * we found a key greater than an xattr key, there can't
3319 * be any acls later on
3320 */
3321 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3322 return 0;
3323
3324 slot++;
3325 scanned++;
3326
3327 /*
3328 * it goes inode, inode backrefs, xattrs, extents,
3329 * so if there are a ton of hard links to an inode there can
3330 * be a lot of backrefs. Don't waste time searching too hard,
3331 * this is just an optimization
3332 */
3333 if (scanned >= 8)
3334 break;
3335 }
3336 /* we hit the end of the leaf before we found an xattr or
3337 * something larger than an xattr. We have to assume the inode
3338 * has acls
3339 */
3340 return 1;
3341}
3342
3343/*
Chris Masond352ac62008-09-29 15:18:18 -04003344 * read an inode from the btree into the in-memory inode
3345 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003346static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003347{
3348 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003349 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003350 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003351 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003352 struct btrfs_root *root = BTRFS_I(inode)->root;
3353 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003354 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003355 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003356 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003357 bool filled = false;
3358
3359 ret = btrfs_fill_inode(inode, &rdev);
3360 if (!ret)
3361 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003362
3363 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003364 if (!path)
3365 goto make_bad;
3366
Josef Bacikd90c7322011-05-17 09:50:54 -04003367 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003368 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003369
Chris Mason39279cc2007-06-12 06:35:45 -04003370 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003372 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003373
Chris Mason5f39d392007-10-15 16:14:19 -04003374 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003375
3376 if (filled)
3377 goto cache_acl;
3378
Chris Mason5f39d392007-10-15 16:14:19 -04003379 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3380 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003381 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003382 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003383 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3384 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003385 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003386
3387 tspec = btrfs_inode_atime(inode_item);
3388 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3389 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3390
3391 tspec = btrfs_inode_mtime(inode_item);
3392 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3393 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3394
3395 tspec = btrfs_inode_ctime(inode_item);
3396 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3397 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3398
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003399 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003400 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003401 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3402
3403 /*
3404 * If we were modified in the current generation and evicted from memory
3405 * and then re-read we need to do a full sync since we don't have any
3406 * idea about which extents were modified before we were evicted from
3407 * cache.
3408 */
3409 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3410 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3411 &BTRFS_I(inode)->runtime_flags);
3412
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003413 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003414 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003415 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003416 rdev = btrfs_inode_rdev(leaf, inode_item);
3417
Josef Bacikaec74772008-07-24 12:12:38 -04003418 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003419 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003420cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003421 /*
3422 * try to precache a NULL acl entry for files that don't have
3423 * any xattrs or acls
3424 */
Li Zefan33345d012011-04-20 10:31:50 +08003425 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3426 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003427 if (!maybe_acls)
3428 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003429
Chris Mason39279cc2007-06-12 06:35:45 -04003430 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003431
Chris Mason39279cc2007-06-12 06:35:45 -04003432 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003433 case S_IFREG:
3434 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003435 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003436 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003437 inode->i_fop = &btrfs_file_operations;
3438 inode->i_op = &btrfs_file_inode_operations;
3439 break;
3440 case S_IFDIR:
3441 inode->i_fop = &btrfs_dir_file_operations;
3442 if (root == root->fs_info->tree_root)
3443 inode->i_op = &btrfs_dir_ro_inode_operations;
3444 else
3445 inode->i_op = &btrfs_dir_inode_operations;
3446 break;
3447 case S_IFLNK:
3448 inode->i_op = &btrfs_symlink_inode_operations;
3449 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003450 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003451 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003452 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003453 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003454 init_special_inode(inode, inode->i_mode, rdev);
3455 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003456 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003457
3458 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003459 return;
3460
3461make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003462 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003463 make_bad_inode(inode);
3464}
3465
Chris Masond352ac62008-09-29 15:18:18 -04003466/*
3467 * given a leaf and an inode, copy the inode fields into the leaf
3468 */
Chris Masone02119d2008-09-05 16:13:11 -04003469static void fill_inode_item(struct btrfs_trans_handle *trans,
3470 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003471 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003472 struct inode *inode)
3473{
Liu Bo51fab692012-12-27 09:01:21 +00003474 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003475
Liu Bo51fab692012-12-27 09:01:21 +00003476 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003477
Liu Bo51fab692012-12-27 09:01:21 +00003478 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3479 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3480 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3481 &token);
3482 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3483 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003484
Liu Bo51fab692012-12-27 09:01:21 +00003485 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3486 inode->i_atime.tv_sec, &token);
3487 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3488 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003489
Liu Bo51fab692012-12-27 09:01:21 +00003490 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3491 inode->i_mtime.tv_sec, &token);
3492 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3493 inode->i_mtime.tv_nsec, &token);
3494
3495 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3496 inode->i_ctime.tv_sec, &token);
3497 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3498 inode->i_ctime.tv_nsec, &token);
3499
3500 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3501 &token);
3502 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3503 &token);
3504 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3505 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3506 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3507 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3508 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003509}
3510
Chris Masond352ac62008-09-29 15:18:18 -04003511/*
3512 * copy everything in the in-memory inode into the btree.
3513 */
Chris Mason21151332011-11-10 20:39:08 -05003514static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003515 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003516{
3517 struct btrfs_inode_item *inode_item;
3518 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003519 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003520 int ret;
3521
3522 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003523 if (!path)
3524 return -ENOMEM;
3525
Chris Masonb9473432009-03-13 11:00:37 -04003526 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003527 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3528 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003529 if (ret) {
3530 if (ret > 0)
3531 ret = -ENOENT;
3532 goto failed;
3533 }
3534
Chris Masonb4ce94d2009-02-04 09:25:08 -05003535 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003536 leaf = path->nodes[0];
3537 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003538 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003539
Chris Masone02119d2008-09-05 16:13:11 -04003540 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003541 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003542 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003543 ret = 0;
3544failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003545 btrfs_free_path(path);
3546 return ret;
3547}
3548
Chris Masond352ac62008-09-29 15:18:18 -04003549/*
Chris Mason21151332011-11-10 20:39:08 -05003550 * copy everything in the in-memory inode into the btree.
3551 */
3552noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3553 struct btrfs_root *root, struct inode *inode)
3554{
3555 int ret;
3556
3557 /*
3558 * If the inode is a free space inode, we can deadlock during commit
3559 * if we put it into the delayed code.
3560 *
3561 * The data relocation inode should also be directly updated
3562 * without delay
3563 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003564 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003565 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003566 btrfs_update_root_times(trans, root);
3567
Chris Mason21151332011-11-10 20:39:08 -05003568 ret = btrfs_delayed_update_inode(trans, root, inode);
3569 if (!ret)
3570 btrfs_set_inode_last_trans(trans, inode);
3571 return ret;
3572 }
3573
3574 return btrfs_update_inode_item(trans, root, inode);
3575}
3576
Josef Bacikbe6aef62012-10-22 15:43:12 -04003577noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root,
3579 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003580{
3581 int ret;
3582
3583 ret = btrfs_update_inode(trans, root, inode);
3584 if (ret == -ENOSPC)
3585 return btrfs_update_inode_item(trans, root, inode);
3586 return ret;
3587}
3588
3589/*
Chris Masond352ac62008-09-29 15:18:18 -04003590 * unlink helper that gets used here in inode.c and in the tree logging
3591 * recovery code. It remove a link in a directory with a given name, and
3592 * also drops the back refs in the inode to the directory
3593 */
Al Viro92986792011-03-04 17:14:37 +00003594static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3595 struct btrfs_root *root,
3596 struct inode *dir, struct inode *inode,
3597 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003598{
3599 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003600 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003601 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003602 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003603 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003604 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003605 u64 ino = btrfs_ino(inode);
3606 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003607
3608 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003609 if (!path) {
3610 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003611 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003612 }
3613
Chris Masonb9473432009-03-13 11:00:37 -04003614 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003615 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003616 name, name_len, -1);
3617 if (IS_ERR(di)) {
3618 ret = PTR_ERR(di);
3619 goto err;
3620 }
3621 if (!di) {
3622 ret = -ENOENT;
3623 goto err;
3624 }
Chris Mason5f39d392007-10-15 16:14:19 -04003625 leaf = path->nodes[0];
3626 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003627 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003628 if (ret)
3629 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003630 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003631
Li Zefan33345d012011-04-20 10:31:50 +08003632 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3633 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003634 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003635 btrfs_info(root->fs_info,
3636 "failed to delete reference to %.*s, inode %llu parent %llu",
3637 name_len, name,
3638 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003639 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003640 goto err;
3641 }
3642
Miao Xie16cdcec2011-04-22 18:12:22 +08003643 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003644 if (ret) {
3645 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003646 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003647 }
Chris Mason39279cc2007-06-12 06:35:45 -04003648
Chris Masone02119d2008-09-05 16:13:11 -04003649 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003650 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003651 if (ret != 0 && ret != -ENOENT) {
3652 btrfs_abort_transaction(trans, root, ret);
3653 goto err;
3654 }
Chris Masone02119d2008-09-05 16:13:11 -04003655
3656 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3657 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003658 if (ret == -ENOENT)
3659 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003660 else if (ret)
3661 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003662err:
3663 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003664 if (ret)
3665 goto out;
3666
3667 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003668 inode_inc_iversion(inode);
3669 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003670 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003671 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003672out:
Chris Mason39279cc2007-06-12 06:35:45 -04003673 return ret;
3674}
3675
Al Viro92986792011-03-04 17:14:37 +00003676int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3677 struct btrfs_root *root,
3678 struct inode *dir, struct inode *inode,
3679 const char *name, int name_len)
3680{
3681 int ret;
3682 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3683 if (!ret) {
3684 btrfs_drop_nlink(inode);
3685 ret = btrfs_update_inode(trans, root, inode);
3686 }
3687 return ret;
3688}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689
3690/*
3691 * helper to start transaction for unlink and rmdir.
3692 *
Josef Bacikd52be812013-05-29 14:54:47 -04003693 * unlink and rmdir are special in btrfs, they do not always free space, so
3694 * if we cannot make our reservations the normal way try and see if there is
3695 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3696 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003697 */
Josef Bacikd52be812013-05-29 14:54:47 -04003698static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699{
3700 struct btrfs_trans_handle *trans;
3701 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 int ret;
3703
Josef Bacike70bea52011-10-11 14:18:24 -04003704 /*
3705 * 1 for the possible orphan item
3706 * 1 for the dir item
3707 * 1 for the dir index
3708 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003709 * 1 for the inode
3710 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003711 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3713 return trans;
3714
Josef Bacikd52be812013-05-29 14:54:47 -04003715 if (PTR_ERR(trans) == -ENOSPC) {
3716 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717
Josef Bacikd52be812013-05-29 14:54:47 -04003718 trans = btrfs_start_transaction(root, 0);
3719 if (IS_ERR(trans))
3720 return trans;
3721 ret = btrfs_cond_migrate_bytes(root->fs_info,
3722 &root->fs_info->trans_block_rsv,
3723 num_bytes, 5);
3724 if (ret) {
3725 btrfs_end_transaction(trans, root);
3726 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003727 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003728 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003729 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003730 }
Josef Bacikd52be812013-05-29 14:54:47 -04003731 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732}
3733
Chris Mason39279cc2007-06-12 06:35:45 -04003734static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3735{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003736 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003738 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003739 int ret;
3740
Josef Bacikd52be812013-05-29 14:54:47 -04003741 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003742 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003743 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
Chris Mason12fcfd22009-03-24 10:24:20 -04003745 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3746
Chris Masone02119d2008-09-05 16:13:11 -04003747 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3748 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003749 if (ret)
3750 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003751
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003753 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003754 if (ret)
3755 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756 }
Josef Bacik7b128762008-07-24 12:17:14 -04003757
Tsutomu Itohb5324022011-07-19 07:27:20 +00003758out:
Josef Bacikd52be812013-05-29 14:54:47 -04003759 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003760 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003761 return ret;
3762}
3763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003764int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3765 struct btrfs_root *root,
3766 struct inode *dir, u64 objectid,
3767 const char *name, int name_len)
3768{
3769 struct btrfs_path *path;
3770 struct extent_buffer *leaf;
3771 struct btrfs_dir_item *di;
3772 struct btrfs_key key;
3773 u64 index;
3774 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003775 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003776
3777 path = btrfs_alloc_path();
3778 if (!path)
3779 return -ENOMEM;
3780
Li Zefan33345d012011-04-20 10:31:50 +08003781 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003782 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003783 if (IS_ERR_OR_NULL(di)) {
3784 if (!di)
3785 ret = -ENOENT;
3786 else
3787 ret = PTR_ERR(di);
3788 goto out;
3789 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790
3791 leaf = path->nodes[0];
3792 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3793 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3794 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003795 if (ret) {
3796 btrfs_abort_transaction(trans, root, ret);
3797 goto out;
3798 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003799 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800
3801 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3802 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003803 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret != -ENOENT) {
3806 btrfs_abort_transaction(trans, root, ret);
3807 goto out;
3808 }
Li Zefan33345d012011-04-20 10:31:50 +08003809 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003810 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003811 if (IS_ERR_OR_NULL(di)) {
3812 if (!di)
3813 ret = -ENOENT;
3814 else
3815 ret = PTR_ERR(di);
3816 btrfs_abort_transaction(trans, root, ret);
3817 goto out;
3818 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819
3820 leaf = path->nodes[0];
3821 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003822 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823 index = key.offset;
3824 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003825 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826
Miao Xie16cdcec2011-04-22 18:12:22 +08003827 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003828 if (ret) {
3829 btrfs_abort_transaction(trans, root, ret);
3830 goto out;
3831 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003832
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003834 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003836 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003837 if (ret)
3838 btrfs_abort_transaction(trans, root, ret);
3839out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003840 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003841 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003842}
3843
Chris Mason39279cc2007-06-12 06:35:45 -04003844static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3845{
3846 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003847 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003848 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003849 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003850
David Sterbab3ae2442012-09-13 16:04:34 -06003851 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003852 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003853 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3854 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003855
Josef Bacikd52be812013-05-29 14:54:47 -04003856 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003857 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003858 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003859
Li Zefan33345d012011-04-20 10:31:50 +08003860 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003861 err = btrfs_unlink_subvol(trans, root, dir,
3862 BTRFS_I(inode)->location.objectid,
3863 dentry->d_name.name,
3864 dentry->d_name.len);
3865 goto out;
3866 }
3867
Josef Bacik7b128762008-07-24 12:17:14 -04003868 err = btrfs_orphan_add(trans, inode);
3869 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003870 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003871
Chris Mason39279cc2007-06-12 06:35:45 -04003872 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003873 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3874 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003875 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003876 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003877out:
Josef Bacikd52be812013-05-29 14:54:47 -04003878 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003879 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003880
Chris Mason39279cc2007-06-12 06:35:45 -04003881 return err;
3882}
3883
Chris Mason323ac952008-10-01 19:05:46 -04003884/*
Chris Mason39279cc2007-06-12 06:35:45 -04003885 * this can truncate away extent items, csum items and directory items.
3886 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003887 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003888 *
3889 * csum items that cross the new i_size are truncated to the new size
3890 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003891 *
3892 * min_type is the minimum key type to truncate down to. If set to 0, this
3893 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003894 */
Yan, Zheng80825102009-11-12 09:35:36 +00003895int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root,
3897 struct inode *inode,
3898 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003899{
Chris Mason39279cc2007-06-12 06:35:45 -04003900 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003901 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003903 struct btrfs_key key;
3904 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003906 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003907 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003909 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 int found_extent;
3911 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003912 int pending_del_nr = 0;
3913 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003914 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003915 int ret;
3916 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003917 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003918
3919 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003920
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003921 path = btrfs_alloc_path();
3922 if (!path)
3923 return -ENOMEM;
3924 path->reada = -1;
3925
Josef Bacik5dc562c2012-08-17 13:14:17 -04003926 /*
3927 * We want to drop from the next block forward in case this new size is
3928 * not block aligned since we will be keeping the last block of the
3929 * extent just the way it is.
3930 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003931 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003932 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3933 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003934
Miao Xie16cdcec2011-04-22 18:12:22 +08003935 /*
3936 * This function is also used to drop the items in the log tree before
3937 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3938 * it is used to drop the loged items. So we shouldn't kill the delayed
3939 * items.
3940 */
3941 if (min_type == 0 && root == BTRFS_I(inode)->root)
3942 btrfs_kill_delayed_inode_items(inode);
3943
Li Zefan33345d012011-04-20 10:31:50 +08003944 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003945 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003946 key.type = (u8)-1;
3947
Chris Mason85e21ba2008-01-29 15:11:36 -05003948search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003949 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003950 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003951 if (ret < 0) {
3952 err = ret;
3953 goto out;
3954 }
Chris Masond3977122009-01-05 21:25:51 -05003955
Chris Mason85e21ba2008-01-29 15:11:36 -05003956 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003957 /* there are no items in the tree for us to truncate, we're
3958 * done
3959 */
Yan, Zheng80825102009-11-12 09:35:36 +00003960 if (path->slots[0] == 0)
3961 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003962 path->slots[0]--;
3963 }
3964
Chris Masond3977122009-01-05 21:25:51 -05003965 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003966 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003967 leaf = path->nodes[0];
3968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3969 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003970
Li Zefan33345d012011-04-20 10:31:50 +08003971 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003972 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003973
Chris Mason85e21ba2008-01-29 15:11:36 -05003974 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003975 break;
3976
Chris Mason5f39d392007-10-15 16:14:19 -04003977 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003978 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003979 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003980 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003981 extent_type = btrfs_file_extent_type(leaf, fi);
3982 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003983 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003984 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003985 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003986 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003987 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003988 }
Yan008630c2007-11-07 13:31:09 -05003989 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 }
Yan, Zheng80825102009-11-12 09:35:36 +00003991 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003992 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003993 } else {
3994 if (item_end < new_size)
3995 break;
3996 if (found_key.offset >= new_size)
3997 del_item = 1;
3998 else
3999 del_item = 0;
4000 }
Chris Mason39279cc2007-06-12 06:35:45 -04004001 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004002 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004003 if (found_type != BTRFS_EXTENT_DATA_KEY)
4004 goto delete;
4005
4006 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004007 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004008 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004009 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004010 u64 orig_num_bytes =
4011 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004012 extent_num_bytes = ALIGN(new_size -
4013 found_key.offset,
4014 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004015 btrfs_set_file_extent_num_bytes(leaf, fi,
4016 extent_num_bytes);
4017 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004018 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004019 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004020 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004021 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004022 } else {
Chris Masondb945352007-10-15 16:15:53 -04004023 extent_num_bytes =
4024 btrfs_file_extent_disk_num_bytes(leaf,
4025 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004026 extent_offset = found_key.offset -
4027 btrfs_file_extent_offset(leaf, fi);
4028
Chris Mason39279cc2007-06-12 06:35:45 -04004029 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004030 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004031 if (extent_start != 0) {
4032 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004033 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004034 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004035 }
4036 }
Chris Mason90692182008-02-08 13:49:28 -05004037 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004038 /*
4039 * we can't truncate inline items that have had
4040 * special encodings
4041 */
4042 if (!del_item &&
4043 btrfs_file_extent_compression(leaf, fi) == 0 &&
4044 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4045 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004046 u32 size = new_size - found_key.offset;
4047
4048 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004049 inode_sub_bytes(inode, item_end + 1 -
4050 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004051 }
4052 size =
4053 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004054 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004055 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004056 inode_sub_bytes(inode, item_end + 1 -
4057 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004058 }
Chris Mason39279cc2007-06-12 06:35:45 -04004059 }
Chris Mason179e29e2007-11-01 11:28:41 -04004060delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004061 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004062 if (!pending_del_nr) {
4063 /* no pending yet, add ourselves */
4064 pending_del_slot = path->slots[0];
4065 pending_del_nr = 1;
4066 } else if (pending_del_nr &&
4067 path->slots[0] + 1 == pending_del_slot) {
4068 /* hop on the pending chunk */
4069 pending_del_nr++;
4070 pending_del_slot = path->slots[0];
4071 } else {
Chris Masond3977122009-01-05 21:25:51 -05004072 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004073 }
Chris Mason39279cc2007-06-12 06:35:45 -04004074 } else {
4075 break;
4076 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004077 if (found_extent && (root->ref_cows ||
4078 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004079 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004080 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 extent_num_bytes, 0,
4082 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004083 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004084 BUG_ON(ret);
4085 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004086
Yan, Zheng80825102009-11-12 09:35:36 +00004087 if (found_type == BTRFS_INODE_ITEM_KEY)
4088 break;
4089
4090 if (path->slots[0] == 0 ||
4091 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004092 if (pending_del_nr) {
4093 ret = btrfs_del_items(trans, root, path,
4094 pending_del_slot,
4095 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret) {
4097 btrfs_abort_transaction(trans,
4098 root, ret);
4099 goto error;
4100 }
Yan, Zheng80825102009-11-12 09:35:36 +00004101 pending_del_nr = 0;
4102 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004103 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004104 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004105 } else {
4106 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004107 }
Chris Mason39279cc2007-06-12 06:35:45 -04004108 }
Yan, Zheng80825102009-11-12 09:35:36 +00004109out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004110 if (pending_del_nr) {
4111 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4112 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004113 if (ret)
4114 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004115 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004116error:
Chris Mason39279cc2007-06-12 06:35:45 -04004117 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004118 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004119}
4120
Chris Masona52d9a82007-08-27 16:49:44 -04004121/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004122 * btrfs_truncate_page - read, zero a chunk and write a page
4123 * @inode - inode that we're zeroing
4124 * @from - the offset to start zeroing
4125 * @len - the length to zero, 0 to zero the entire range respective to the
4126 * offset
4127 * @front - zero up to the offset instead of from the offset on
4128 *
4129 * This will find the page for the "from" offset and cow the page and zero the
4130 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004131 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004132int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4133 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004134{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004135 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004136 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004137 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4138 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004139 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004140 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004141 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004142 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4143 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4144 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004145 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004146 int ret = 0;
4147 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004148 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004149
Josef Bacik2aaa6652012-08-29 14:27:18 -04004150 if ((offset & (blocksize - 1)) == 0 &&
4151 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004152 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004153 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004154 if (ret)
4155 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004156
Chris Mason211c17f2008-05-15 09:13:45 -04004157again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004158 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004159 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004160 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004161 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004162 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004163 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004164
4165 page_start = page_offset(page);
4166 page_end = page_start + PAGE_CACHE_SIZE - 1;
4167
Chris Masona52d9a82007-08-27 16:49:44 -04004168 if (!PageUptodate(page)) {
4169 ret = btrfs_readpage(NULL, page);
4170 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004171 if (page->mapping != mapping) {
4172 unlock_page(page);
4173 page_cache_release(page);
4174 goto again;
4175 }
Chris Masona52d9a82007-08-27 16:49:44 -04004176 if (!PageUptodate(page)) {
4177 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004178 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004179 }
4180 }
Chris Mason211c17f2008-05-15 09:13:45 -04004181 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004182
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004183 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004184 set_page_extent_mapped(page);
4185
4186 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4187 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004188 unlock_extent_cached(io_tree, page_start, page_end,
4189 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004190 unlock_page(page);
4191 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004192 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004193 btrfs_put_ordered_extent(ordered);
4194 goto again;
4195 }
4196
Josef Bacik2ac55d42010-02-03 19:33:23 +00004197 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004198 EXTENT_DIRTY | EXTENT_DELALLOC |
4199 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004200 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004201
Josef Bacik2ac55d42010-02-03 19:33:23 +00004202 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4203 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004204 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004205 unlock_extent_cached(io_tree, page_start, page_end,
4206 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004207 goto out_unlock;
4208 }
4209
Chris Masone6dcd2d2008-07-17 12:53:50 -04004210 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004211 if (!len)
4212 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004213 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004214 if (front)
4215 memset(kaddr, 0, offset);
4216 else
4217 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004218 flush_dcache_page(page);
4219 kunmap(page);
4220 }
Chris Mason247e7432008-07-17 12:53:51 -04004221 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004222 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004223 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4224 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004225
Chris Mason89642222008-07-24 09:41:53 -04004226out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004227 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004228 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004229 unlock_page(page);
4230 page_cache_release(page);
4231out:
4232 return ret;
4233}
4234
Josef Bacik695a0d02011-03-04 15:46:53 -05004235/*
4236 * This function puts in dummy file extents for the area we're creating a hole
4237 * for. So if we are truncating this file to a larger size we need to insert
4238 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4239 * the range between oldsize and size
4240 */
Josef Bacika41ad392011-01-31 15:30:16 -05004241int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004242{
4243 struct btrfs_trans_handle *trans;
4244 struct btrfs_root *root = BTRFS_I(inode)->root;
4245 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004246 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004247 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004249 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4250 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004251 u64 last_byte;
4252 u64 cur_offset;
4253 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004254 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004255
Josef Bacika71754f2013-06-17 17:14:39 -04004256 /*
4257 * If our size started in the middle of a page we need to zero out the
4258 * rest of the page before we expand the i_size, otherwise we could
4259 * expose stale data.
4260 */
4261 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4262 if (err)
4263 return err;
4264
Yan Zheng9036c102008-10-30 14:19:41 -04004265 if (size <= hole_start)
4266 return 0;
4267
Yan Zheng9036c102008-10-30 14:19:41 -04004268 while (1) {
4269 struct btrfs_ordered_extent *ordered;
4270 btrfs_wait_ordered_range(inode, hole_start,
4271 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004272 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004273 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004274 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4275 if (!ordered)
4276 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004277 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4278 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004279 btrfs_put_ordered_extent(ordered);
4280 }
4281
Yan Zheng9036c102008-10-30 14:19:41 -04004282 cur_offset = hole_start;
4283 while (1) {
4284 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4285 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004286 if (IS_ERR(em)) {
4287 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004288 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004289 break;
4290 }
Yan Zheng9036c102008-10-30 14:19:41 -04004291 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004292 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004293 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004294 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004295 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004296
Miao Xie36423202011-12-14 20:12:02 -05004297 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004298 if (IS_ERR(trans)) {
4299 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004300 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004301 }
Yan, Zheng80825102009-11-12 09:35:36 +00004302
Josef Bacik5dc562c2012-08-17 13:14:17 -04004303 err = btrfs_drop_extents(trans, root, inode,
4304 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004305 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004306 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004308 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004309 break;
Miao Xie5b397372011-09-11 10:52:24 -04004310 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004311
Yan Zheng9036c102008-10-30 14:19:41 -04004312 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004313 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004314 0, hole_size, 0, hole_size,
4315 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004316 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004317 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004318 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004319 break;
Miao Xie5b397372011-09-11 10:52:24 -04004320 }
Yan, Zheng80825102009-11-12 09:35:36 +00004321
Josef Bacik5dc562c2012-08-17 13:14:17 -04004322 btrfs_drop_extent_cache(inode, cur_offset,
4323 cur_offset + hole_size - 1, 0);
4324 hole_em = alloc_extent_map();
4325 if (!hole_em) {
4326 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4327 &BTRFS_I(inode)->runtime_flags);
4328 goto next;
4329 }
4330 hole_em->start = cur_offset;
4331 hole_em->len = hole_size;
4332 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004333
Josef Bacik5dc562c2012-08-17 13:14:17 -04004334 hole_em->block_start = EXTENT_MAP_HOLE;
4335 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004336 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004337 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004338 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4339 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4340 hole_em->generation = trans->transid;
4341
4342 while (1) {
4343 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004344 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004345 write_unlock(&em_tree->lock);
4346 if (err != -EEXIST)
4347 break;
4348 btrfs_drop_extent_cache(inode, cur_offset,
4349 cur_offset +
4350 hole_size - 1, 0);
4351 }
4352 free_extent_map(hole_em);
4353next:
Miao Xie36423202011-12-14 20:12:02 -05004354 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004355 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004356 }
4357 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004358 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004359 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004360 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004361 break;
4362 }
4363
Yan, Zhenga22285a2010-05-16 10:48:46 -04004364 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004365 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4366 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004367 return err;
4368}
4369
Eric Sandeen3972f262013-01-12 02:57:22 +00004370static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004371{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004372 struct btrfs_root *root = BTRFS_I(inode)->root;
4373 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004374 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004375 loff_t newsize = attr->ia_size;
4376 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004377 int ret;
4378
Josef Bacika41ad392011-01-31 15:30:16 -05004379 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004380 return 0;
4381
Eric Sandeen3972f262013-01-12 02:57:22 +00004382 /*
4383 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4384 * special case where we need to update the times despite not having
4385 * these flags set. For all other operations the VFS set these flags
4386 * explicitly if it wants a timestamp update.
4387 */
4388 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4389 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4390
Josef Bacika41ad392011-01-31 15:30:16 -05004391 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004392 truncate_pagecache(inode, oldsize, newsize);
4393 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004394 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004395 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004396
Miao Xief4a2f4c2011-12-14 20:12:01 -05004397 trans = btrfs_start_transaction(root, 1);
4398 if (IS_ERR(trans))
4399 return PTR_ERR(trans);
4400
4401 i_size_write(inode, newsize);
4402 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4403 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004404 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004405 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004406
Josef Bacika41ad392011-01-31 15:30:16 -05004407 /*
4408 * We're truncating a file that used to have good data down to
4409 * zero. Make sure it gets into the ordered flush list so that
4410 * any new writes get down to disk quickly.
4411 */
4412 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004413 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4414 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004415
Josef Bacikf3fe8202013-01-07 17:03:21 -05004416 /*
4417 * 1 for the orphan item we're going to add
4418 * 1 for the orphan item deletion.
4419 */
4420 trans = btrfs_start_transaction(root, 2);
4421 if (IS_ERR(trans))
4422 return PTR_ERR(trans);
4423
4424 /*
4425 * We need to do this in case we fail at _any_ point during the
4426 * actual truncate. Once we do the truncate_setsize we could
4427 * invalidate pages which forces any outstanding ordered io to
4428 * be instantly completed which will give us extents that need
4429 * to be truncated. If we fail to get an orphan inode down we
4430 * could have left over extents that were never meant to live,
4431 * so we need to garuntee from this point on that everything
4432 * will be consistent.
4433 */
4434 ret = btrfs_orphan_add(trans, inode);
4435 btrfs_end_transaction(trans, root);
4436 if (ret)
4437 return ret;
4438
Josef Bacika41ad392011-01-31 15:30:16 -05004439 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4440 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004441
4442 /* Disable nonlocked read DIO to avoid the end less truncate */
4443 btrfs_inode_block_unlocked_dio(inode);
4444 inode_dio_wait(inode);
4445 btrfs_inode_resume_unlocked_dio(inode);
4446
Josef Bacika41ad392011-01-31 15:30:16 -05004447 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004448 if (ret && inode->i_nlink)
4449 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004450 }
4451
Josef Bacika41ad392011-01-31 15:30:16 -05004452 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004453}
4454
Chris Mason39279cc2007-06-12 06:35:45 -04004455static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4456{
4457 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004458 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004459 int err;
4460
Li Zefanb83cc962010-12-20 16:04:08 +08004461 if (btrfs_root_readonly(root))
4462 return -EROFS;
4463
Chris Mason39279cc2007-06-12 06:35:45 -04004464 err = inode_change_ok(inode, attr);
4465 if (err)
4466 return err;
4467
Chris Mason5a3f23d2009-03-31 13:27:11 -04004468 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004469 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004470 if (err)
4471 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004472 }
Yan Zheng9036c102008-10-30 14:19:41 -04004473
Christoph Hellwig10257742010-06-04 11:30:02 +02004474 if (attr->ia_valid) {
4475 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004476 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004477 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004478
Josef Bacik22c44fe2011-11-30 10:45:38 -05004479 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004480 err = btrfs_acl_chmod(inode);
4481 }
4482
Chris Mason39279cc2007-06-12 06:35:45 -04004483 return err;
4484}
Chris Mason61295eb2008-01-14 16:24:38 -05004485
Al Virobd555972010-06-07 11:35:40 -04004486void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004487{
4488 struct btrfs_trans_handle *trans;
4489 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004490 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004491 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004492 int ret;
4493
liubo1abe9b82011-03-24 11:18:59 +00004494 trace_btrfs_inode_evict(inode);
4495
Chris Mason39279cc2007-06-12 06:35:45 -04004496 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004497 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004498 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004499 goto no_delete;
4500
Chris Mason39279cc2007-06-12 06:35:45 -04004501 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004502 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004503 goto no_delete;
4504 }
Al Virobd555972010-06-07 11:35:40 -04004505 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004506 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004507
Yan, Zhengc71bf092009-11-12 09:34:40 +00004508 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004509 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004510 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004511 goto no_delete;
4512 }
4513
Yan, Zheng76dda932009-09-21 16:00:26 -04004514 if (inode->i_nlink > 0) {
4515 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4516 goto no_delete;
4517 }
4518
Miao Xie0e8c36a2012-12-19 06:59:51 +00004519 ret = btrfs_commit_inode_delayed_inode(inode);
4520 if (ret) {
4521 btrfs_orphan_del(NULL, inode);
4522 goto no_delete;
4523 }
4524
Miao Xie66d8f3d2012-09-06 04:02:28 -06004525 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004526 if (!rsv) {
4527 btrfs_orphan_del(NULL, inode);
4528 goto no_delete;
4529 }
Josef Bacik4a338542011-08-29 11:01:31 -04004530 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004531 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004532 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004533
Chris Masondbe674a2008-07-17 12:54:05 -04004534 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004535
Josef Bacik4289a662011-08-05 13:22:24 -04004536 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004537 * This is a bit simpler than btrfs_truncate since we've already
4538 * reserved our space for our orphan item in the unlink, so we just
4539 * need to reserve some slack space in case we add bytes and update
4540 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004541 */
Yan, Zheng80825102009-11-12 09:35:36 +00004542 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004543 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4544 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004545
Josef Bacik726c35f2011-09-26 15:46:06 -04004546 /*
4547 * Try and steal from the global reserve since we will
4548 * likely not use this space anyway, we want to try as
4549 * hard as possible to get this to work.
4550 */
4551 if (ret)
4552 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4553
Yan, Zhengd68fc572010-05-16 10:49:58 -04004554 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004555 btrfs_warn(root->fs_info,
4556 "Could not get space for a delete, will truncate on mount %d",
4557 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004558 btrfs_orphan_del(NULL, inode);
4559 btrfs_free_block_rsv(root, rsv);
4560 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004561 }
4562
Miao Xie0e8c36a2012-12-19 06:59:51 +00004563 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004564 if (IS_ERR(trans)) {
4565 btrfs_orphan_del(NULL, inode);
4566 btrfs_free_block_rsv(root, rsv);
4567 goto no_delete;
4568 }
4569
4570 trans->block_rsv = rsv;
4571
Yan, Zhengd68fc572010-05-16 10:49:58 -04004572 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004573 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004574 break;
4575
Miao Xie8407aa42012-09-07 01:43:32 -06004576 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004577 btrfs_end_transaction(trans, root);
4578 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004579 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004580 }
4581
Josef Bacik4289a662011-08-05 13:22:24 -04004582 btrfs_free_block_rsv(root, rsv);
4583
Yan, Zheng80825102009-11-12 09:35:36 +00004584 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004585 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004586 ret = btrfs_orphan_del(trans, inode);
4587 BUG_ON(ret);
4588 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004589
Josef Bacik4289a662011-08-05 13:22:24 -04004590 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004591 if (!(root == root->fs_info->tree_root ||
4592 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004593 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004594
Chris Mason54aa1f42007-06-22 14:16:25 -04004595 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004596 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004597no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004598 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004599 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004600 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004601}
4602
4603/*
4604 * this returns the key found in the dir entry in the location pointer.
4605 * If no dir entries were found, location->objectid is 0.
4606 */
4607static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4608 struct btrfs_key *location)
4609{
4610 const char *name = dentry->d_name.name;
4611 int namelen = dentry->d_name.len;
4612 struct btrfs_dir_item *di;
4613 struct btrfs_path *path;
4614 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004615 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004616
4617 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004618 if (!path)
4619 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004620
Li Zefan33345d012011-04-20 10:31:50 +08004621 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004622 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004623 if (IS_ERR(di))
4624 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004625
David Sterbac7040052011-04-19 18:00:01 +02004626 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004627 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004628
Chris Mason5f39d392007-10-15 16:14:19 -04004629 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004630out:
Chris Mason39279cc2007-06-12 06:35:45 -04004631 btrfs_free_path(path);
4632 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004633out_err:
4634 location->objectid = 0;
4635 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004636}
4637
4638/*
4639 * when we hit a tree root in a directory, the btrfs part of the inode
4640 * needs to be changed to reflect the root directory of the tree root. This
4641 * is kind of like crossing a mount point.
4642 */
4643static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004644 struct inode *dir,
4645 struct dentry *dentry,
4646 struct btrfs_key *location,
4647 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004648{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004649 struct btrfs_path *path;
4650 struct btrfs_root *new_root;
4651 struct btrfs_root_ref *ref;
4652 struct extent_buffer *leaf;
4653 int ret;
4654 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004655
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004656 path = btrfs_alloc_path();
4657 if (!path) {
4658 err = -ENOMEM;
4659 goto out;
4660 }
Chris Mason39279cc2007-06-12 06:35:45 -04004661
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004662 err = -ENOENT;
4663 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4664 BTRFS_I(dir)->root->root_key.objectid,
4665 location->objectid);
4666 if (ret) {
4667 if (ret < 0)
4668 err = ret;
4669 goto out;
4670 }
Chris Mason39279cc2007-06-12 06:35:45 -04004671
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004672 leaf = path->nodes[0];
4673 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004674 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004675 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4676 goto out;
4677
4678 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4679 (unsigned long)(ref + 1),
4680 dentry->d_name.len);
4681 if (ret)
4682 goto out;
4683
David Sterbab3b4aa72011-04-21 01:20:15 +02004684 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004685
4686 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4687 if (IS_ERR(new_root)) {
4688 err = PTR_ERR(new_root);
4689 goto out;
4690 }
4691
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004692 *sub_root = new_root;
4693 location->objectid = btrfs_root_dirid(&new_root->root_item);
4694 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004695 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004696 err = 0;
4697out:
4698 btrfs_free_path(path);
4699 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004700}
4701
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004702static void inode_tree_add(struct inode *inode)
4703{
4704 struct btrfs_root *root = BTRFS_I(inode)->root;
4705 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004706 struct rb_node **p;
4707 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004708 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004709
Al Viro1d3382cb2010-10-23 15:19:20 -04004710 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004711 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004712again:
4713 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004714 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004715 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004716 while (*p) {
4717 parent = *p;
4718 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4719
Li Zefan33345d012011-04-20 10:31:50 +08004720 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004721 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004722 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004723 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004724 else {
4725 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004726 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004727 rb_erase(parent, &root->inode_tree);
4728 RB_CLEAR_NODE(parent);
4729 spin_unlock(&root->inode_lock);
4730 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004731 }
4732 }
4733 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4734 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4735 spin_unlock(&root->inode_lock);
4736}
4737
4738static void inode_tree_del(struct inode *inode)
4739{
4740 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004741 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004742
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004743 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004745 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004746 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004747 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004748 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004749 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004750
Josef Bacik0af3d002010-06-21 14:48:16 -04004751 /*
4752 * Free space cache has inodes in the tree root, but the tree root has a
4753 * root_refs of 0, so this could end up dropping the tree root as a
4754 * snapshot, so we need the extra !root->fs_info->tree_root check to
4755 * make sure we don't drop it.
4756 */
4757 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4758 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004759 synchronize_srcu(&root->fs_info->subvol_srcu);
4760 spin_lock(&root->inode_lock);
4761 empty = RB_EMPTY_ROOT(&root->inode_tree);
4762 spin_unlock(&root->inode_lock);
4763 if (empty)
4764 btrfs_add_dead_root(root);
4765 }
4766}
4767
Jeff Mahoney143bede2012-03-01 14:56:26 +01004768void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004769{
4770 struct rb_node *node;
4771 struct rb_node *prev;
4772 struct btrfs_inode *entry;
4773 struct inode *inode;
4774 u64 objectid = 0;
4775
4776 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4777
4778 spin_lock(&root->inode_lock);
4779again:
4780 node = root->inode_tree.rb_node;
4781 prev = NULL;
4782 while (node) {
4783 prev = node;
4784 entry = rb_entry(node, struct btrfs_inode, rb_node);
4785
Li Zefan33345d012011-04-20 10:31:50 +08004786 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004787 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004788 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004789 node = node->rb_right;
4790 else
4791 break;
4792 }
4793 if (!node) {
4794 while (prev) {
4795 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004796 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004797 node = prev;
4798 break;
4799 }
4800 prev = rb_next(prev);
4801 }
4802 }
4803 while (node) {
4804 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004805 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004806 inode = igrab(&entry->vfs_inode);
4807 if (inode) {
4808 spin_unlock(&root->inode_lock);
4809 if (atomic_read(&inode->i_count) > 1)
4810 d_prune_aliases(inode);
4811 /*
Al Viro45321ac2010-06-07 13:43:19 -04004812 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004813 * the inode cache when its usage count
4814 * hits zero.
4815 */
4816 iput(inode);
4817 cond_resched();
4818 spin_lock(&root->inode_lock);
4819 goto again;
4820 }
4821
4822 if (cond_resched_lock(&root->inode_lock))
4823 goto again;
4824
4825 node = rb_next(node);
4826 }
4827 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004828}
4829
Chris Masone02119d2008-09-05 16:13:11 -04004830static int btrfs_init_locked_inode(struct inode *inode, void *p)
4831{
4832 struct btrfs_iget_args *args = p;
4833 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004834 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004835 return 0;
4836}
4837
4838static int btrfs_find_actor(struct inode *inode, void *opaque)
4839{
4840 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004841 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004842 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004843}
4844
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004845static struct inode *btrfs_iget_locked(struct super_block *s,
4846 u64 objectid,
4847 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004848{
4849 struct inode *inode;
4850 struct btrfs_iget_args args;
4851 args.ino = objectid;
4852 args.root = root;
4853
4854 inode = iget5_locked(s, objectid, btrfs_find_actor,
4855 btrfs_init_locked_inode,
4856 (void *)&args);
4857 return inode;
4858}
4859
Balaji Rao1a54ef82008-07-21 02:01:04 +05304860/* Get an inode object given its location and corresponding root.
4861 * Returns in *is_new if the inode was read from disk
4862 */
4863struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004864 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304865{
4866 struct inode *inode;
4867
4868 inode = btrfs_iget_locked(s, location->objectid, root);
4869 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004870 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304871
4872 if (inode->i_state & I_NEW) {
4873 BTRFS_I(inode)->root = root;
4874 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4875 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004876 if (!is_bad_inode(inode)) {
4877 inode_tree_add(inode);
4878 unlock_new_inode(inode);
4879 if (new)
4880 *new = 1;
4881 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004882 unlock_new_inode(inode);
4883 iput(inode);
4884 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004885 }
4886 }
4887
Balaji Rao1a54ef82008-07-21 02:01:04 +05304888 return inode;
4889}
4890
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004891static struct inode *new_simple_dir(struct super_block *s,
4892 struct btrfs_key *key,
4893 struct btrfs_root *root)
4894{
4895 struct inode *inode = new_inode(s);
4896
4897 if (!inode)
4898 return ERR_PTR(-ENOMEM);
4899
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004900 BTRFS_I(inode)->root = root;
4901 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004902 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004903
4904 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004905 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004906 inode->i_fop = &simple_dir_operations;
4907 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4908 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4909
4910 return inode;
4911}
4912
Chris Mason3de45862008-11-17 21:02:50 -05004913struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004914{
Chris Masond3977122009-01-05 21:25:51 -05004915 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004916 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004917 struct btrfs_root *sub_root = root;
4918 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004919 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004920 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004921
4922 if (dentry->d_name.len > BTRFS_NAME_LEN)
4923 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004924
Jeff Layton39e3c952012-11-28 11:30:53 -05004925 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004926 if (ret < 0)
4927 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004928
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004929 if (location.objectid == 0)
4930 return NULL;
4931
4932 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004933 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004934 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004935 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004936
4937 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4938
Yan, Zheng76dda932009-09-21 16:00:26 -04004939 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004940 ret = fixup_tree_root_location(root, dir, dentry,
4941 &location, &sub_root);
4942 if (ret < 0) {
4943 if (ret != -ENOENT)
4944 inode = ERR_PTR(ret);
4945 else
4946 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4947 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004948 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004949 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004950 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4951
Julia Lawall34d19ba2011-01-24 19:55:19 +00004952 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004953 down_read(&root->fs_info->cleanup_work_sem);
4954 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004955 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004956 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004957 if (ret) {
4958 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004959 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004960 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004961 }
4962
Chris Mason3de45862008-11-17 21:02:50 -05004963 return inode;
4964}
4965
Nick Pigginfe15ce42011-01-07 17:49:23 +11004966static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004967{
4968 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004969 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004970
Li Zefan848cce02012-02-21 17:04:28 +08004971 if (!inode && !IS_ROOT(dentry))
4972 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004973
Li Zefan848cce02012-02-21 17:04:28 +08004974 if (inode) {
4975 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004976 if (btrfs_root_refs(&root->root_item) == 0)
4977 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004978
4979 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4980 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004981 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004982 return 0;
4983}
4984
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004985static void btrfs_dentry_release(struct dentry *dentry)
4986{
4987 if (dentry->d_fsdata)
4988 kfree(dentry->d_fsdata);
4989}
4990
Chris Mason3de45862008-11-17 21:02:50 -05004991static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004992 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004993{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004994 struct dentry *ret;
4995
4996 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004997 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004998}
4999
Miao Xie16cdcec2011-04-22 18:12:22 +08005000unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005001 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5002};
5003
David Woodhousecbdf5a22008-08-06 19:42:33 +01005004static int btrfs_real_readdir(struct file *filp, void *dirent,
5005 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005006{
Al Viro496ad9a2013-01-23 17:07:38 -05005007 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005008 struct btrfs_root *root = BTRFS_I(inode)->root;
5009 struct btrfs_item *item;
5010 struct btrfs_dir_item *di;
5011 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005012 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005013 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005014 struct list_head ins_list;
5015 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005016 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005017 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005018 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005019 unsigned char d_type;
5020 int over = 0;
5021 u32 di_cur;
5022 u32 di_total;
5023 u32 di_len;
5024 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005025 char tmp_name[32];
5026 char *name_ptr;
5027 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005028 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005029
5030 /* FIXME, use a real flag for deciding about the key type */
5031 if (root->fs_info->tree_root == root)
5032 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005033
Chris Mason39544012007-12-12 14:38:19 -05005034 /* special case for "." */
5035 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005036 over = filldir(dirent, ".", 1,
5037 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005038 if (over)
5039 return 0;
5040 filp->f_pos = 1;
5041 }
Chris Mason39544012007-12-12 14:38:19 -05005042 /* special case for .., just use the back ref */
5043 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005044 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005045 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005046 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005047 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005048 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005049 filp->f_pos = 2;
5050 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005051 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005052 if (!path)
5053 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005054
Josef Bacik026fd312011-05-13 10:32:11 -04005055 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005056
Miao Xie16cdcec2011-04-22 18:12:22 +08005057 if (key_type == BTRFS_DIR_INDEX_KEY) {
5058 INIT_LIST_HEAD(&ins_list);
5059 INIT_LIST_HEAD(&del_list);
5060 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5061 }
5062
Chris Mason39279cc2007-06-12 06:35:45 -04005063 btrfs_set_key_type(&key, key_type);
5064 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005065 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005066
Chris Mason39279cc2007-06-12 06:35:45 -04005067 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5068 if (ret < 0)
5069 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005070
5071 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005072 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005073 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005074 if (slot >= btrfs_header_nritems(leaf)) {
5075 ret = btrfs_next_leaf(root, path);
5076 if (ret < 0)
5077 goto err;
5078 else if (ret > 0)
5079 break;
5080 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005081 }
Chris Mason3de45862008-11-17 21:02:50 -05005082
Chris Mason5f39d392007-10-15 16:14:19 -04005083 item = btrfs_item_nr(leaf, slot);
5084 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5085
5086 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005087 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005088 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005089 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005090 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005091 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005092 if (key_type == BTRFS_DIR_INDEX_KEY &&
5093 btrfs_should_delete_dir_index(&del_list,
5094 found_key.offset))
5095 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005096
5097 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005098 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005099
Chris Mason39279cc2007-06-12 06:35:45 -04005100 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5101 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005102 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005103
5104 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005105 struct btrfs_key location;
5106
Josef Bacik22a94d42011-03-16 16:47:17 -04005107 if (verify_dir_item(root, leaf, di))
5108 break;
5109
Chris Mason5f39d392007-10-15 16:14:19 -04005110 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005111 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005112 name_ptr = tmp_name;
5113 } else {
5114 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005115 if (!name_ptr) {
5116 ret = -ENOMEM;
5117 goto err;
5118 }
Chris Mason5f39d392007-10-15 16:14:19 -04005119 }
5120 read_extent_buffer(leaf, name_ptr,
5121 (unsigned long)(di + 1), name_len);
5122
5123 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5124 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005125
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005126
Chris Mason3de45862008-11-17 21:02:50 -05005127 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005128 * skip it.
5129 *
5130 * In contrast to old kernels, we insert the snapshot's
5131 * dir item and dir index after it has been created, so
5132 * we won't find a reference to our own snapshot. We
5133 * still keep the following code for backward
5134 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005135 */
5136 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5137 location.objectid == root->root_key.objectid) {
5138 over = 0;
5139 goto skip;
5140 }
Chris Mason5f39d392007-10-15 16:14:19 -04005141 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005142 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005143 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005144
Chris Mason3de45862008-11-17 21:02:50 -05005145skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005146 if (name_ptr != tmp_name)
5147 kfree(name_ptr);
5148
Chris Mason39279cc2007-06-12 06:35:45 -04005149 if (over)
5150 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005151 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005152 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005153 di_cur += di_len;
5154 di = (struct btrfs_dir_item *)((char *)di + di_len);
5155 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005156next:
5157 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005158 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005159
Miao Xie16cdcec2011-04-22 18:12:22 +08005160 if (key_type == BTRFS_DIR_INDEX_KEY) {
5161 if (is_curr)
5162 filp->f_pos++;
5163 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5164 &ins_list);
5165 if (ret)
5166 goto nopos;
5167 }
5168
David Woodhouse49593bf2008-08-17 17:08:36 +01005169 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005170 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005171 /*
5172 * 32-bit glibc will use getdents64, but then strtol -
5173 * so the last number we can serve is this.
5174 */
5175 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005176 else
5177 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005178nopos:
5179 ret = 0;
5180err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005181 if (key_type == BTRFS_DIR_INDEX_KEY)
5182 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005183 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005184 return ret;
5185}
5186
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005187int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005188{
5189 struct btrfs_root *root = BTRFS_I(inode)->root;
5190 struct btrfs_trans_handle *trans;
5191 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005192 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005193
Josef Bacik72ac3c02012-05-23 14:13:11 -04005194 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005195 return 0;
5196
Liu Bo83eea1f2012-07-10 05:28:39 -06005197 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005198 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005199
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005200 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005201 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005202 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005203 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005204 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005205 if (IS_ERR(trans))
5206 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005207 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005208 }
5209 return ret;
5210}
5211
5212/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005213 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005214 * inode changes. But, it is most likely to find the inode in cache.
5215 * FIXME, needs more benchmarking...there are no reasons other than performance
5216 * to keep or drop this code.
5217 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005218static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005219{
5220 struct btrfs_root *root = BTRFS_I(inode)->root;
5221 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005222 int ret;
5223
Josef Bacik72ac3c02012-05-23 14:13:11 -04005224 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005225 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005226
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005227 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005228 if (IS_ERR(trans))
5229 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005230
5231 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005232 if (ret && ret == -ENOSPC) {
5233 /* whoops, lets try again with the full transaction */
5234 btrfs_end_transaction(trans, root);
5235 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005236 if (IS_ERR(trans))
5237 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005238
Chris Mason94b60442010-05-26 11:02:00 -04005239 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005240 }
Chris Mason39279cc2007-06-12 06:35:45 -04005241 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005242 if (BTRFS_I(inode)->delayed_node)
5243 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005244
5245 return ret;
5246}
5247
5248/*
5249 * This is a copy of file_update_time. We need this so we can return error on
5250 * ENOSPC for updating the inode in the case of file write and mmap writes.
5251 */
Josef Bacike41f9412012-03-26 09:46:47 -04005252static int btrfs_update_time(struct inode *inode, struct timespec *now,
5253 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005254{
Alexander Block2bc5565282012-06-15 09:49:33 +02005255 struct btrfs_root *root = BTRFS_I(inode)->root;
5256
5257 if (btrfs_root_readonly(root))
5258 return -EROFS;
5259
Josef Bacike41f9412012-03-26 09:46:47 -04005260 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005261 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005262 if (flags & S_CTIME)
5263 inode->i_ctime = *now;
5264 if (flags & S_MTIME)
5265 inode->i_mtime = *now;
5266 if (flags & S_ATIME)
5267 inode->i_atime = *now;
5268 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005269}
5270
Chris Masond352ac62008-09-29 15:18:18 -04005271/*
5272 * find the highest existing sequence number in a directory
5273 * and then set the in-memory index_cnt variable to reflect
5274 * free sequence numbers
5275 */
Josef Bacikaec74772008-07-24 12:12:38 -04005276static int btrfs_set_inode_index_count(struct inode *inode)
5277{
5278 struct btrfs_root *root = BTRFS_I(inode)->root;
5279 struct btrfs_key key, found_key;
5280 struct btrfs_path *path;
5281 struct extent_buffer *leaf;
5282 int ret;
5283
Li Zefan33345d012011-04-20 10:31:50 +08005284 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005285 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5286 key.offset = (u64)-1;
5287
5288 path = btrfs_alloc_path();
5289 if (!path)
5290 return -ENOMEM;
5291
5292 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5293 if (ret < 0)
5294 goto out;
5295 /* FIXME: we should be able to handle this */
5296 if (ret == 0)
5297 goto out;
5298 ret = 0;
5299
5300 /*
5301 * MAGIC NUMBER EXPLANATION:
5302 * since we search a directory based on f_pos we have to start at 2
5303 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5304 * else has to start at 2
5305 */
5306 if (path->slots[0] == 0) {
5307 BTRFS_I(inode)->index_cnt = 2;
5308 goto out;
5309 }
5310
5311 path->slots[0]--;
5312
5313 leaf = path->nodes[0];
5314 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5315
Li Zefan33345d012011-04-20 10:31:50 +08005316 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005317 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5318 BTRFS_I(inode)->index_cnt = 2;
5319 goto out;
5320 }
5321
5322 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5323out:
5324 btrfs_free_path(path);
5325 return ret;
5326}
5327
Chris Masond352ac62008-09-29 15:18:18 -04005328/*
5329 * helper to find a free sequence number in a given directory. This current
5330 * code is very simple, later versions will do smarter things in the btree
5331 */
Chris Mason3de45862008-11-17 21:02:50 -05005332int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005333{
5334 int ret = 0;
5335
5336 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005337 ret = btrfs_inode_delayed_dir_index_count(dir);
5338 if (ret) {
5339 ret = btrfs_set_inode_index_count(dir);
5340 if (ret)
5341 return ret;
5342 }
Josef Bacikaec74772008-07-24 12:12:38 -04005343 }
5344
Chris Mason00e4e6b2008-08-05 11:18:09 -04005345 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005346 BTRFS_I(dir)->index_cnt++;
5347
5348 return ret;
5349}
5350
Chris Mason39279cc2007-06-12 06:35:45 -04005351static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5352 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005353 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005354 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005355 u64 ref_objectid, u64 objectid,
5356 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005357{
5358 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005359 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005360 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005361 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005362 struct btrfs_inode_ref *ref;
5363 struct btrfs_key key[2];
5364 u32 sizes[2];
5365 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005366 int ret;
5367 int owner;
5368
Chris Mason5f39d392007-10-15 16:14:19 -04005369 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005370 if (!path)
5371 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005372
Chris Mason39279cc2007-06-12 06:35:45 -04005373 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005374 if (!inode) {
5375 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005376 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005377 }
Chris Mason39279cc2007-06-12 06:35:45 -04005378
Li Zefan581bb052011-04-20 10:06:11 +08005379 /*
5380 * we have to initialize this early, so we can reclaim the inode
5381 * number if we fail afterwards in this function.
5382 */
5383 inode->i_ino = objectid;
5384
Josef Bacikaec74772008-07-24 12:12:38 -04005385 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005386 trace_btrfs_inode_request(dir);
5387
Chris Mason3de45862008-11-17 21:02:50 -05005388 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005389 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005390 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005391 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005392 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005393 }
Josef Bacikaec74772008-07-24 12:12:38 -04005394 }
5395 /*
5396 * index_cnt is ignored for everything but a dir,
5397 * btrfs_get_inode_index_count has an explanation for the magic
5398 * number
5399 */
5400 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005401 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005402 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005403 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005404
Josef Bacik5dc562c2012-08-17 13:14:17 -04005405 /*
5406 * We could have gotten an inode number from somebody who was fsynced
5407 * and then removed in this same transaction, so let's just set full
5408 * sync since it will be a full sync anyway and this will blow away the
5409 * old info in the log.
5410 */
5411 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5412
Al Viro569254b2011-07-24 17:08:40 -04005413 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005414 owner = 0;
5415 else
5416 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005417
5418 key[0].objectid = objectid;
5419 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5420 key[0].offset = 0;
5421
Mark Fashehf1863732012-08-08 11:32:27 -07005422 /*
5423 * Start new inodes with an inode_ref. This is slightly more
5424 * efficient for small numbers of hard links since they will
5425 * be packed into one item. Extended refs will kick in if we
5426 * add more hard links than can fit in the ref item.
5427 */
Chris Mason9c583092008-01-29 15:15:18 -05005428 key[1].objectid = objectid;
5429 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5430 key[1].offset = ref_objectid;
5431
5432 sizes[0] = sizeof(struct btrfs_inode_item);
5433 sizes[1] = name_len + sizeof(*ref);
5434
Chris Masonb9473432009-03-13 11:00:37 -04005435 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005436 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5437 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005438 goto fail;
5439
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005440 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005441 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005442 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005443 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5444 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005445 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5446 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005447 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005448
5449 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5450 struct btrfs_inode_ref);
5451 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005452 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005453 ptr = (unsigned long)(ref + 1);
5454 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5455
Chris Mason5f39d392007-10-15 16:14:19 -04005456 btrfs_mark_buffer_dirty(path->nodes[0]);
5457 btrfs_free_path(path);
5458
Chris Mason39279cc2007-06-12 06:35:45 -04005459 location = &BTRFS_I(inode)->location;
5460 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005461 location->offset = 0;
5462 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5463
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005464 btrfs_inherit_iflags(inode, dir);
5465
Al Viro569254b2011-07-24 17:08:40 -04005466 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005467 if (btrfs_test_opt(root, NODATASUM))
5468 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005469 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005470 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5471 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005472 }
5473
Chris Mason39279cc2007-06-12 06:35:45 -04005474 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005476
5477 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005478 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005479
Alexander Block8ea05e32012-07-25 17:35:53 +02005480 btrfs_update_root_times(trans, root);
5481
Chris Mason39279cc2007-06-12 06:35:45 -04005482 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005483fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005484 if (dir)
5485 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005486 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005487 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005488 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005489}
5490
5491static inline u8 btrfs_inode_type(struct inode *inode)
5492{
5493 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5494}
5495
Chris Masond352ac62008-09-29 15:18:18 -04005496/*
5497 * utility function to add 'inode' into 'parent_inode' with
5498 * a give name and a given sequence number.
5499 * if 'add_backref' is true, also insert a backref from the
5500 * inode to the parent directory.
5501 */
Chris Masone02119d2008-09-05 16:13:11 -04005502int btrfs_add_link(struct btrfs_trans_handle *trans,
5503 struct inode *parent_inode, struct inode *inode,
5504 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005505{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005507 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005508 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005509 u64 ino = btrfs_ino(inode);
5510 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005511
Li Zefan33345d012011-04-20 10:31:50 +08005512 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5514 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005515 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5517 key.offset = 0;
5518 }
Chris Mason39279cc2007-06-12 06:35:45 -04005519
Li Zefan33345d012011-04-20 10:31:50 +08005520 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5522 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005523 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005525 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5526 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 }
5528
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005529 /* Nothing to clean up yet */
5530 if (ret)
5531 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005533 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5534 parent_inode, &key,
5535 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005536 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005537 goto fail_dir_item;
5538 else if (ret) {
5539 btrfs_abort_transaction(trans, root, ret);
5540 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005541 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005542
5543 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5544 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005545 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005546 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5547 ret = btrfs_update_inode(trans, root, parent_inode);
5548 if (ret)
5549 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005550 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005551
5552fail_dir_item:
5553 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5554 u64 local_index;
5555 int err;
5556 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5557 key.objectid, root->root_key.objectid,
5558 parent_ino, &local_index, name, name_len);
5559
5560 } else if (add_backref) {
5561 u64 local_index;
5562 int err;
5563
5564 err = btrfs_del_inode_ref(trans, root, name, name_len,
5565 ino, parent_ino, &local_index);
5566 }
5567 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005568}
5569
5570static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005571 struct inode *dir, struct dentry *dentry,
5572 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005573{
Josef Bacika1b075d2010-11-19 20:36:11 +00005574 int err = btrfs_add_link(trans, dir, inode,
5575 dentry->d_name.name, dentry->d_name.len,
5576 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005577 if (err > 0)
5578 err = -EEXIST;
5579 return err;
5580}
5581
Josef Bacik618e21d2007-07-11 10:18:17 -04005582static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005583 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005584{
5585 struct btrfs_trans_handle *trans;
5586 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005587 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005588 int err;
5589 int drop_inode = 0;
5590 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005591 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005592
5593 if (!new_valid_dev(rdev))
5594 return -EINVAL;
5595
Josef Bacik9ed74f22009-09-11 16:12:44 -04005596 /*
5597 * 2 for inode item and ref
5598 * 2 for dir items
5599 * 1 for xattr if selinux is on
5600 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005601 trans = btrfs_start_transaction(root, 5);
5602 if (IS_ERR(trans))
5603 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005604
Li Zefan581bb052011-04-20 10:06:11 +08005605 err = btrfs_find_free_ino(root, &objectid);
5606 if (err)
5607 goto out_unlock;
5608
Josef Bacikaec74772008-07-24 12:12:38 -04005609 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005610 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005611 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005612 if (IS_ERR(inode)) {
5613 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005614 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005615 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005616
Eric Paris2a7dba32011-02-01 11:05:39 -05005617 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005618 if (err) {
5619 drop_inode = 1;
5620 goto out_unlock;
5621 }
5622
Casey Schauflerad19db72011-12-15 10:09:07 -05005623 /*
5624 * If the active LSM wants to access the inode during
5625 * d_instantiate it needs these. Smack checks to see
5626 * if the filesystem supports xattrs by looking at the
5627 * ops vector.
5628 */
5629
5630 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005631 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005632 if (err)
5633 drop_inode = 1;
5634 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005635 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005636 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005637 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005638 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005639out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005640 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005641 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005642 if (drop_inode) {
5643 inode_dec_link_count(inode);
5644 iput(inode);
5645 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005646 return err;
5647}
5648
Chris Mason39279cc2007-06-12 06:35:45 -04005649static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005650 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005651{
5652 struct btrfs_trans_handle *trans;
5653 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005654 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005655 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005656 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005657 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005658 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005659
Josef Bacik9ed74f22009-09-11 16:12:44 -04005660 /*
5661 * 2 for inode item and ref
5662 * 2 for dir items
5663 * 1 for xattr if selinux is on
5664 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005665 trans = btrfs_start_transaction(root, 5);
5666 if (IS_ERR(trans))
5667 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005668
Li Zefan581bb052011-04-20 10:06:11 +08005669 err = btrfs_find_free_ino(root, &objectid);
5670 if (err)
5671 goto out_unlock;
5672
Josef Bacikaec74772008-07-24 12:12:38 -04005673 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005674 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005675 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005676 if (IS_ERR(inode)) {
5677 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005678 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005679 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005680 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005681
Eric Paris2a7dba32011-02-01 11:05:39 -05005682 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005683 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005684 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005685
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005686 err = btrfs_update_inode(trans, root, inode);
5687 if (err)
5688 goto out_unlock;
5689
Casey Schauflerad19db72011-12-15 10:09:07 -05005690 /*
5691 * If the active LSM wants to access the inode during
5692 * d_instantiate it needs these. Smack checks to see
5693 * if the filesystem supports xattrs by looking at the
5694 * ops vector.
5695 */
5696 inode->i_fop = &btrfs_file_operations;
5697 inode->i_op = &btrfs_file_inode_operations;
5698
Josef Bacika1b075d2010-11-19 20:36:11 +00005699 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005700 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005701 goto out_unlock;
5702
5703 inode->i_mapping->a_ops = &btrfs_aops;
5704 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5705 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5706 d_instantiate(dentry, inode);
5707
Chris Mason39279cc2007-06-12 06:35:45 -04005708out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005709 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005710 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005711 inode_dec_link_count(inode);
5712 iput(inode);
5713 }
Liu Bob53d3f52012-11-14 14:34:34 +00005714 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005715 return err;
5716}
5717
5718static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5719 struct dentry *dentry)
5720{
5721 struct btrfs_trans_handle *trans;
5722 struct btrfs_root *root = BTRFS_I(dir)->root;
5723 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005724 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005725 int err;
5726 int drop_inode = 0;
5727
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005728 /* do not allow sys_link's with other subvols of the same device */
5729 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005730 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005731
Mark Fashehf1863732012-08-08 11:32:27 -07005732 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005733 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005734
Chris Mason3de45862008-11-17 21:02:50 -05005735 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005736 if (err)
5737 goto fail;
5738
Yan, Zhenga22285a2010-05-16 10:48:46 -04005739 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005740 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005741 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005742 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005743 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005744 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005745 if (IS_ERR(trans)) {
5746 err = PTR_ERR(trans);
5747 goto fail;
5748 }
Chris Mason5f39d392007-10-15 16:14:19 -04005749
Miao Xie31534952011-04-13 13:19:21 +08005750 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005751 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005752 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005753 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005754 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005755
Josef Bacika1b075d2010-11-19 20:36:11 +00005756 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005757
Yan, Zhenga5719522009-09-24 09:17:31 -04005758 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005759 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005760 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005761 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005762 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005763 if (err)
5764 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005765 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005766 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005767 }
Chris Mason39279cc2007-06-12 06:35:45 -04005768
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005769 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005770fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005771 if (drop_inode) {
5772 inode_dec_link_count(inode);
5773 iput(inode);
5774 }
Liu Bob53d3f52012-11-14 14:34:34 +00005775 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005776 return err;
5777}
5778
Al Viro18bb1db2011-07-26 01:41:39 -04005779static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005780{
Chris Masonb9d86662008-05-02 16:13:49 -04005781 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005782 struct btrfs_trans_handle *trans;
5783 struct btrfs_root *root = BTRFS_I(dir)->root;
5784 int err = 0;
5785 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005786 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005787 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005788
Josef Bacik9ed74f22009-09-11 16:12:44 -04005789 /*
5790 * 2 items for inode and ref
5791 * 2 items for dir items
5792 * 1 for xattr if selinux is on
5793 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005794 trans = btrfs_start_transaction(root, 5);
5795 if (IS_ERR(trans))
5796 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005797
Li Zefan581bb052011-04-20 10:06:11 +08005798 err = btrfs_find_free_ino(root, &objectid);
5799 if (err)
5800 goto out_fail;
5801
Josef Bacikaec74772008-07-24 12:12:38 -04005802 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005803 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005804 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005805 if (IS_ERR(inode)) {
5806 err = PTR_ERR(inode);
5807 goto out_fail;
5808 }
Chris Mason5f39d392007-10-15 16:14:19 -04005809
Chris Mason39279cc2007-06-12 06:35:45 -04005810 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005811
Eric Paris2a7dba32011-02-01 11:05:39 -05005812 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005813 if (err)
5814 goto out_fail;
5815
Chris Mason39279cc2007-06-12 06:35:45 -04005816 inode->i_op = &btrfs_dir_inode_operations;
5817 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005818
Chris Masondbe674a2008-07-17 12:54:05 -04005819 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005820 err = btrfs_update_inode(trans, root, inode);
5821 if (err)
5822 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005823
Josef Bacika1b075d2010-11-19 20:36:11 +00005824 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5825 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005826 if (err)
5827 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005828
Chris Mason39279cc2007-06-12 06:35:45 -04005829 d_instantiate(dentry, inode);
5830 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005831
5832out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005833 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005834 if (drop_on_err)
5835 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005836 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005837 return err;
5838}
5839
Chris Masond352ac62008-09-29 15:18:18 -04005840/* helper for btfs_get_extent. Given an existing extent in the tree,
5841 * and an extent that you want to insert, deal with overlap and insert
5842 * the new extent into the tree.
5843 */
Chris Mason3b951512008-04-17 11:29:12 -04005844static int merge_extent_mapping(struct extent_map_tree *em_tree,
5845 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005846 struct extent_map *em,
5847 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005848{
5849 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005850
Chris Masone6dcd2d2008-07-17 12:53:50 -04005851 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5852 start_diff = map_start - em->start;
5853 em->start = map_start;
5854 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005855 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5856 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005857 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005858 em->block_len -= start_diff;
5859 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005860 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005861}
5862
Chris Masonc8b97812008-10-29 14:49:59 -04005863static noinline int uncompress_inline(struct btrfs_path *path,
5864 struct inode *inode, struct page *page,
5865 size_t pg_offset, u64 extent_offset,
5866 struct btrfs_file_extent_item *item)
5867{
5868 int ret;
5869 struct extent_buffer *leaf = path->nodes[0];
5870 char *tmp;
5871 size_t max_size;
5872 unsigned long inline_size;
5873 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005874 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005875
5876 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005877 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005878 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5879 inline_size = btrfs_file_extent_inline_item_len(leaf,
5880 btrfs_item_nr(leaf, path->slots[0]));
5881 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005882 if (!tmp)
5883 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005884 ptr = btrfs_file_extent_inline_start(item);
5885
5886 read_extent_buffer(leaf, tmp, ptr, inline_size);
5887
Chris Mason5b050f02008-11-11 09:34:41 -05005888 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005889 ret = btrfs_decompress(compress_type, tmp, page,
5890 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005891 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005892 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005893 unsigned long copy_size = min_t(u64,
5894 PAGE_CACHE_SIZE - pg_offset,
5895 max_size - extent_offset);
5896 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005897 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005898 }
5899 kfree(tmp);
5900 return 0;
5901}
5902
Chris Masond352ac62008-09-29 15:18:18 -04005903/*
5904 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005905 * the ugly parts come from merging extents from the disk with the in-ram
5906 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005907 * where the in-ram extents might be locked pending data=ordered completion.
5908 *
5909 * This also copies inline extents directly into the page.
5910 */
Chris Masond3977122009-01-05 21:25:51 -05005911
Chris Masona52d9a82007-08-27 16:49:44 -04005912struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005913 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005914 int create)
5915{
5916 int ret;
5917 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005918 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005919 u64 extent_start = 0;
5920 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005921 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005922 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005923 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005924 struct btrfs_root *root = BTRFS_I(inode)->root;
5925 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005926 struct extent_buffer *leaf;
5927 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005928 struct extent_map *em = NULL;
5929 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005930 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005931 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005932 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005933
Chris Masona52d9a82007-08-27 16:49:44 -04005934again:
Chris Mason890871b2009-09-02 16:24:52 -04005935 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005936 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005937 if (em)
5938 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005939 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005940
Chris Masona52d9a82007-08-27 16:49:44 -04005941 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005942 if (em->start > start || em->start + em->len <= start)
5943 free_extent_map(em);
5944 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005945 free_extent_map(em);
5946 else
5947 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005948 }
David Sterba172ddd62011-04-21 00:48:27 +02005949 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005950 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005951 err = -ENOMEM;
5952 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005953 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005954 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005955 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005956 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005957 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005958 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005959
5960 if (!path) {
5961 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005962 if (!path) {
5963 err = -ENOMEM;
5964 goto out;
5965 }
5966 /*
5967 * Chances are we'll be called again, so go ahead and do
5968 * readahead
5969 */
5970 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005971 }
5972
Chris Mason179e29e2007-11-01 11:28:41 -04005973 ret = btrfs_lookup_file_extent(trans, root, path,
5974 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005975 if (ret < 0) {
5976 err = ret;
5977 goto out;
5978 }
5979
5980 if (ret != 0) {
5981 if (path->slots[0] == 0)
5982 goto not_found;
5983 path->slots[0]--;
5984 }
5985
Chris Mason5f39d392007-10-15 16:14:19 -04005986 leaf = path->nodes[0];
5987 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005988 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005989 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5991 found_type = btrfs_key_type(&found_key);
5992 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005993 found_type != BTRFS_EXTENT_DATA_KEY) {
5994 goto not_found;
5995 }
5996
Chris Mason5f39d392007-10-15 16:14:19 -04005997 found_type = btrfs_file_extent_type(leaf, item);
5998 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005999 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006000 if (found_type == BTRFS_FILE_EXTENT_REG ||
6001 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006002 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006003 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006004 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6005 size_t size;
6006 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006007 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006008 }
6009
6010 if (start >= extent_end) {
6011 path->slots[0]++;
6012 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6013 ret = btrfs_next_leaf(root, path);
6014 if (ret < 0) {
6015 err = ret;
6016 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006017 }
Yan Zheng9036c102008-10-30 14:19:41 -04006018 if (ret > 0)
6019 goto not_found;
6020 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006021 }
Yan Zheng9036c102008-10-30 14:19:41 -04006022 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6023 if (found_key.objectid != objectid ||
6024 found_key.type != BTRFS_EXTENT_DATA_KEY)
6025 goto not_found;
6026 if (start + len <= found_key.offset)
6027 goto not_found;
6028 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006029 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006030 em->len = found_key.offset - start;
6031 goto not_found_em;
6032 }
6033
Josef Bacikcc95bef2013-04-04 14:31:27 -04006034 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006035 if (found_type == BTRFS_FILE_EXTENT_REG ||
6036 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006037 em->start = extent_start;
6038 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006039 em->orig_start = extent_start -
6040 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006041 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6042 item);
Chris Masondb945352007-10-15 16:15:53 -04006043 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6044 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006045 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006046 goto insert;
6047 }
Li Zefan261507a02010-12-17 14:21:50 +08006048 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006049 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006050 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006051 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006052 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006053 } else {
6054 bytenr += btrfs_file_extent_offset(leaf, item);
6055 em->block_start = bytenr;
6056 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006057 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6058 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006059 }
Chris Masona52d9a82007-08-27 16:49:44 -04006060 goto insert;
6061 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006062 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006063 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006064 size_t size;
6065 size_t extent_offset;
6066 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006067
Chris Masona52d9a82007-08-27 16:49:44 -04006068 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006069 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006070 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006071 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006072 goto out;
6073 }
6074
Yan Zheng9036c102008-10-30 14:19:41 -04006075 size = btrfs_file_extent_inline_len(leaf, item);
6076 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006077 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006078 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006079 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006080 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006081 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006082 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006083 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006084 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006085 em->compress_type = compress_type;
6086 }
Yan689f9342007-10-29 11:41:07 -04006087 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006088 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006089 if (btrfs_file_extent_compression(leaf, item) !=
6090 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006091 ret = uncompress_inline(path, inode, page,
6092 pg_offset,
6093 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006094 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006095 } else {
6096 map = kmap(page);
6097 read_extent_buffer(leaf, map + pg_offset, ptr,
6098 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006099 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6100 memset(map + pg_offset + copy_size, 0,
6101 PAGE_CACHE_SIZE - pg_offset -
6102 copy_size);
6103 }
Chris Masonc8b97812008-10-29 14:49:59 -04006104 kunmap(page);
6105 }
Chris Mason179e29e2007-11-01 11:28:41 -04006106 flush_dcache_page(page);
6107 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006108 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006109 if (!trans) {
6110 kunmap(page);
6111 free_extent_map(em);
6112 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006113
David Sterbab3b4aa72011-04-21 01:20:15 +02006114 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006115 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006116
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006117 if (IS_ERR(trans))
6118 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006119 goto again;
6120 }
Chris Masonc8b97812008-10-29 14:49:59 -04006121 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006122 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006123 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006124 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006125 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006126 }
Chris Masond1310b22008-01-24 16:13:08 -05006127 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006128 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006129 goto insert;
6130 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006131 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006132 }
6133not_found:
6134 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006135 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006136 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006137not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006138 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006139 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006140insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006141 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006142 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006143 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6144 (unsigned long long)em->start,
6145 (unsigned long long)em->len,
6146 (unsigned long long)start,
6147 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006148 err = -EIO;
6149 goto out;
6150 }
Chris Masond1310b22008-01-24 16:13:08 -05006151
6152 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006153 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006154 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006155 /* it is possible that someone inserted the extent into the tree
6156 * while we had the lock dropped. It is also possible that
6157 * an overlapping map exists in the tree
6158 */
Chris Masona52d9a82007-08-27 16:49:44 -04006159 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006160 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006161
6162 ret = 0;
6163
Chris Mason3b951512008-04-17 11:29:12 -04006164 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006165 if (existing && (existing->start > start ||
6166 existing->start + existing->len <= start)) {
6167 free_extent_map(existing);
6168 existing = NULL;
6169 }
Chris Mason3b951512008-04-17 11:29:12 -04006170 if (!existing) {
6171 existing = lookup_extent_mapping(em_tree, em->start,
6172 em->len);
6173 if (existing) {
6174 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006175 em, start,
6176 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006177 free_extent_map(existing);
6178 if (err) {
6179 free_extent_map(em);
6180 em = NULL;
6181 }
6182 } else {
6183 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006184 free_extent_map(em);
6185 em = NULL;
6186 }
6187 } else {
6188 free_extent_map(em);
6189 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006190 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006191 }
Chris Masona52d9a82007-08-27 16:49:44 -04006192 }
Chris Mason890871b2009-09-02 16:24:52 -04006193 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006194out:
liubo1abe9b82011-03-24 11:18:59 +00006195
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006196 if (em)
6197 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006198
Chris Masonf4219502008-07-22 11:18:09 -04006199 if (path)
6200 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006201 if (trans) {
6202 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006203 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006204 err = ret;
6205 }
Chris Masona52d9a82007-08-27 16:49:44 -04006206 if (err) {
6207 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006208 return ERR_PTR(err);
6209 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006210 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006211 return em;
6212}
6213
Chris Masonec29ed52011-02-23 16:23:20 -05006214struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6215 size_t pg_offset, u64 start, u64 len,
6216 int create)
6217{
6218 struct extent_map *em;
6219 struct extent_map *hole_em = NULL;
6220 u64 range_start = start;
6221 u64 end;
6222 u64 found;
6223 u64 found_end;
6224 int err = 0;
6225
6226 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6227 if (IS_ERR(em))
6228 return em;
6229 if (em) {
6230 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006231 * if our em maps to
6232 * - a hole or
6233 * - a pre-alloc extent,
6234 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006235 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006236 if (em->block_start != EXTENT_MAP_HOLE &&
6237 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006238 return em;
6239 else
6240 hole_em = em;
6241 }
6242
6243 /* check to see if we've wrapped (len == -1 or similar) */
6244 end = start + len;
6245 if (end < start)
6246 end = (u64)-1;
6247 else
6248 end -= 1;
6249
6250 em = NULL;
6251
6252 /* ok, we didn't find anything, lets look for delalloc */
6253 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6254 end, len, EXTENT_DELALLOC, 1);
6255 found_end = range_start + found;
6256 if (found_end < range_start)
6257 found_end = (u64)-1;
6258
6259 /*
6260 * we didn't find anything useful, return
6261 * the original results from get_extent()
6262 */
6263 if (range_start > end || found_end <= start) {
6264 em = hole_em;
6265 hole_em = NULL;
6266 goto out;
6267 }
6268
6269 /* adjust the range_start to make sure it doesn't
6270 * go backwards from the start they passed in
6271 */
6272 range_start = max(start,range_start);
6273 found = found_end - range_start;
6274
6275 if (found > 0) {
6276 u64 hole_start = start;
6277 u64 hole_len = len;
6278
David Sterba172ddd62011-04-21 00:48:27 +02006279 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006280 if (!em) {
6281 err = -ENOMEM;
6282 goto out;
6283 }
6284 /*
6285 * when btrfs_get_extent can't find anything it
6286 * returns one huge hole
6287 *
6288 * make sure what it found really fits our range, and
6289 * adjust to make sure it is based on the start from
6290 * the caller
6291 */
6292 if (hole_em) {
6293 u64 calc_end = extent_map_end(hole_em);
6294
6295 if (calc_end <= start || (hole_em->start > end)) {
6296 free_extent_map(hole_em);
6297 hole_em = NULL;
6298 } else {
6299 hole_start = max(hole_em->start, start);
6300 hole_len = calc_end - hole_start;
6301 }
6302 }
6303 em->bdev = NULL;
6304 if (hole_em && range_start > hole_start) {
6305 /* our hole starts before our delalloc, so we
6306 * have to return just the parts of the hole
6307 * that go until the delalloc starts
6308 */
6309 em->len = min(hole_len,
6310 range_start - hole_start);
6311 em->start = hole_start;
6312 em->orig_start = hole_start;
6313 /*
6314 * don't adjust block start at all,
6315 * it is fixed at EXTENT_MAP_HOLE
6316 */
6317 em->block_start = hole_em->block_start;
6318 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006319 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6320 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006321 } else {
6322 em->start = range_start;
6323 em->len = found;
6324 em->orig_start = range_start;
6325 em->block_start = EXTENT_MAP_DELALLOC;
6326 em->block_len = found;
6327 }
6328 } else if (hole_em) {
6329 return hole_em;
6330 }
6331out:
6332
6333 free_extent_map(hole_em);
6334 if (err) {
6335 free_extent_map(em);
6336 return ERR_PTR(err);
6337 }
6338 return em;
6339}
6340
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6342 u64 start, u64 len)
6343{
6344 struct btrfs_root *root = BTRFS_I(inode)->root;
6345 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006346 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006347 struct btrfs_key ins;
6348 u64 alloc_hint;
6349 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006351 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006352 if (IS_ERR(trans))
6353 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006354
6355 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6356
6357 alloc_hint = get_extent_allocation_hint(inode, start, len);
6358 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006359 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006360 if (ret) {
6361 em = ERR_PTR(ret);
6362 goto out;
6363 }
6364
Josef Bacik70c8a912012-10-11 16:54:30 -04006365 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006366 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006367 if (IS_ERR(em))
6368 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006369
6370 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6371 ins.offset, ins.offset, 0);
6372 if (ret) {
6373 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6374 em = ERR_PTR(ret);
6375 }
6376out:
6377 btrfs_end_transaction(trans, root);
6378 return em;
6379}
6380
Chris Mason46bfbb52010-05-26 11:04:10 -04006381/*
6382 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6383 * block must be cow'd
6384 */
6385static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006386 struct inode *inode, u64 offset, u64 *len,
6387 u64 *orig_start, u64 *orig_block_len,
6388 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006389{
6390 struct btrfs_path *path;
6391 int ret;
6392 struct extent_buffer *leaf;
6393 struct btrfs_root *root = BTRFS_I(inode)->root;
6394 struct btrfs_file_extent_item *fi;
6395 struct btrfs_key key;
6396 u64 disk_bytenr;
6397 u64 backref_offset;
6398 u64 extent_end;
6399 u64 num_bytes;
6400 int slot;
6401 int found_type;
6402
6403 path = btrfs_alloc_path();
6404 if (!path)
6405 return -ENOMEM;
6406
Li Zefan33345d012011-04-20 10:31:50 +08006407 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006408 offset, 0);
6409 if (ret < 0)
6410 goto out;
6411
6412 slot = path->slots[0];
6413 if (ret == 1) {
6414 if (slot == 0) {
6415 /* can't find the item, must cow */
6416 ret = 0;
6417 goto out;
6418 }
6419 slot--;
6420 }
6421 ret = 0;
6422 leaf = path->nodes[0];
6423 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006424 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006425 key.type != BTRFS_EXTENT_DATA_KEY) {
6426 /* not our file or wrong item type, must cow */
6427 goto out;
6428 }
6429
6430 if (key.offset > offset) {
6431 /* Wrong offset, must cow */
6432 goto out;
6433 }
6434
6435 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6436 found_type = btrfs_file_extent_type(leaf, fi);
6437 if (found_type != BTRFS_FILE_EXTENT_REG &&
6438 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6439 /* not a regular extent, must cow */
6440 goto out;
6441 }
6442 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6443 backref_offset = btrfs_file_extent_offset(leaf, fi);
6444
Josef Bacikeb384b52013-04-24 16:32:55 -04006445 *orig_start = key.offset - backref_offset;
6446 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6447 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6448
Chris Mason46bfbb52010-05-26 11:04:10 -04006449 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006450 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006451 /* extent doesn't include our full range, must cow */
6452 goto out;
6453 }
6454
6455 if (btrfs_extent_readonly(root, disk_bytenr))
6456 goto out;
6457
6458 /*
6459 * look for other files referencing this extent, if we
6460 * find any we must cow
6461 */
Li Zefan33345d012011-04-20 10:31:50 +08006462 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006463 key.offset - backref_offset, disk_bytenr))
6464 goto out;
6465
6466 /*
6467 * adjust disk_bytenr and num_bytes to cover just the bytes
6468 * in this extent we are about to write. If there
6469 * are any csums in that range we have to cow in order
6470 * to keep the csums correct
6471 */
6472 disk_bytenr += backref_offset;
6473 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006474 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006475 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6476 goto out;
6477 /*
6478 * all of the above have passed, it is safe to overwrite this extent
6479 * without cow
6480 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006481 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006482 ret = 1;
6483out:
6484 btrfs_free_path(path);
6485 return ret;
6486}
6487
Josef Bacikeb838e72012-07-31 16:28:48 -04006488static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6489 struct extent_state **cached_state, int writing)
6490{
6491 struct btrfs_ordered_extent *ordered;
6492 int ret = 0;
6493
6494 while (1) {
6495 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6496 0, cached_state);
6497 /*
6498 * We're concerned with the entire range that we're going to be
6499 * doing DIO to, so we need to make sure theres no ordered
6500 * extents in this range.
6501 */
6502 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6503 lockend - lockstart + 1);
6504
6505 /*
6506 * We need to make sure there are no buffered pages in this
6507 * range either, we could have raced between the invalidate in
6508 * generic_file_direct_write and locking the extent. The
6509 * invalidate needs to happen so that reads after a write do not
6510 * get stale data.
6511 */
6512 if (!ordered && (!writing ||
6513 !test_range_bit(&BTRFS_I(inode)->io_tree,
6514 lockstart, lockend, EXTENT_UPTODATE, 0,
6515 *cached_state)))
6516 break;
6517
6518 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6519 cached_state, GFP_NOFS);
6520
6521 if (ordered) {
6522 btrfs_start_ordered_extent(inode, ordered, 1);
6523 btrfs_put_ordered_extent(ordered);
6524 } else {
6525 /* Screw you mmap */
6526 ret = filemap_write_and_wait_range(inode->i_mapping,
6527 lockstart,
6528 lockend);
6529 if (ret)
6530 break;
6531
6532 /*
6533 * If we found a page that couldn't be invalidated just
6534 * fall back to buffered.
6535 */
6536 ret = invalidate_inode_pages2_range(inode->i_mapping,
6537 lockstart >> PAGE_CACHE_SHIFT,
6538 lockend >> PAGE_CACHE_SHIFT);
6539 if (ret)
6540 break;
6541 }
6542
6543 cond_resched();
6544 }
6545
6546 return ret;
6547}
6548
Josef Bacik69ffb542012-09-11 15:40:07 -04006549static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6550 u64 len, u64 orig_start,
6551 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006552 u64 orig_block_len, u64 ram_bytes,
6553 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006554{
6555 struct extent_map_tree *em_tree;
6556 struct extent_map *em;
6557 struct btrfs_root *root = BTRFS_I(inode)->root;
6558 int ret;
6559
6560 em_tree = &BTRFS_I(inode)->extent_tree;
6561 em = alloc_extent_map();
6562 if (!em)
6563 return ERR_PTR(-ENOMEM);
6564
6565 em->start = start;
6566 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006567 em->mod_start = start;
6568 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006569 em->len = len;
6570 em->block_len = block_len;
6571 em->block_start = block_start;
6572 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006573 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006574 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006575 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006576 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6577 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006578 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006579
6580 do {
6581 btrfs_drop_extent_cache(inode, em->start,
6582 em->start + em->len - 1, 0);
6583 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006584 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006585 write_unlock(&em_tree->lock);
6586 } while (ret == -EEXIST);
6587
6588 if (ret) {
6589 free_extent_map(em);
6590 return ERR_PTR(ret);
6591 }
6592
6593 return em;
6594}
6595
6596
Josef Bacik4b46fce2010-05-23 11:00:55 -04006597static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6598 struct buffer_head *bh_result, int create)
6599{
6600 struct extent_map *em;
6601 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006602 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006603 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006604 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006605 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006606 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006607 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006608 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006609
Miao Xie172a5042013-02-21 02:48:22 -07006610 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006611 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006612 else
Josef Bacikc3298612012-08-03 16:49:19 -04006613 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006614
Josef Bacikc3298612012-08-03 16:49:19 -04006615 lockstart = start;
6616 lockend = start + len - 1;
6617
Josef Bacikeb838e72012-07-31 16:28:48 -04006618 /*
6619 * If this errors out it's because we couldn't invalidate pagecache for
6620 * this range and we need to fallback to buffered.
6621 */
6622 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6623 return -ENOTBLK;
6624
Josef Bacik4b46fce2010-05-23 11:00:55 -04006625 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006626 if (IS_ERR(em)) {
6627 ret = PTR_ERR(em);
6628 goto unlock_err;
6629 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006630
6631 /*
6632 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6633 * io. INLINE is special, and we could probably kludge it in here, but
6634 * it's still buffered so for safety lets just fall back to the generic
6635 * buffered path.
6636 *
6637 * For COMPRESSED we _have_ to read the entire extent in so we can
6638 * decompress it, so there will be buffering required no matter what we
6639 * do, so go ahead and fallback to buffered.
6640 *
6641 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6642 * to buffered IO. Don't blame me, this is the price we pay for using
6643 * the generic code.
6644 */
6645 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6646 em->block_start == EXTENT_MAP_INLINE) {
6647 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006648 ret = -ENOTBLK;
6649 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006650 }
6651
6652 /* Just a good old fashioned hole, return */
6653 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6654 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6655 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006656 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006657 }
6658
6659 /*
6660 * We don't allocate a new extent in the following cases
6661 *
6662 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6663 * existing extent.
6664 * 2) The extent is marked as PREALLOC. We're good to go here and can
6665 * just use the extent.
6666 *
6667 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006668 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006669 len = min(len, em->len - (start - em->start));
6670 lockstart = start + len;
6671 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006672 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006673
6674 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6675 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6676 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006677 int type;
6678 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006679 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006680
6681 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6682 type = BTRFS_ORDERED_PREALLOC;
6683 else
6684 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006685 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006686 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006687
6688 /*
6689 * we're not going to log anything, but we do need
6690 * to make sure the current transaction stays open
6691 * while we look for nocow cross refs
6692 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006693 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006694 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006695 goto must_cow;
6696
Josef Bacikeb384b52013-04-24 16:32:55 -04006697 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6698 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006699 if (type == BTRFS_ORDERED_PREALLOC) {
6700 free_extent_map(em);
6701 em = create_pinned_em(inode, start, len,
6702 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006703 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006704 orig_block_len,
6705 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006706 if (IS_ERR(em)) {
6707 btrfs_end_transaction(trans, root);
6708 goto unlock_err;
6709 }
6710 }
6711
Chris Mason46bfbb52010-05-26 11:04:10 -04006712 ret = btrfs_add_ordered_extent_dio(inode, start,
6713 block_start, len, len, type);
6714 btrfs_end_transaction(trans, root);
6715 if (ret) {
6716 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006717 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006718 }
6719 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006720 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006721 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006722 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006723must_cow:
6724 /*
6725 * this will cow the extent, reset the len in case we changed
6726 * it above
6727 */
6728 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006729 free_extent_map(em);
6730 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006731 if (IS_ERR(em)) {
6732 ret = PTR_ERR(em);
6733 goto unlock_err;
6734 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006735 len = min(len, em->len - (start - em->start));
6736unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006737 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6738 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006739 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006740 bh_result->b_bdev = em->bdev;
6741 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006742 if (create) {
6743 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6744 set_buffer_new(bh_result);
6745
6746 /*
6747 * Need to update the i_size under the extent lock so buffered
6748 * readers will get the updated i_size when we unlock.
6749 */
6750 if (start + len > i_size_read(inode))
6751 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006752
Miao Xie172a5042013-02-21 02:48:22 -07006753 spin_lock(&BTRFS_I(inode)->lock);
6754 BTRFS_I(inode)->outstanding_extents++;
6755 spin_unlock(&BTRFS_I(inode)->lock);
6756
Miao Xie09348562013-02-07 10:12:07 +00006757 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6758 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6759 &cached_state, GFP_NOFS);
6760 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006761 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006762
Josef Bacikeb838e72012-07-31 16:28:48 -04006763 /*
6764 * In the case of write we need to clear and unlock the entire range,
6765 * in the case of read we need to unlock only the end area that we
6766 * aren't using if there is any left over space.
6767 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006768 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006769 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6770 lockend, unlock_bits, 1, 0,
6771 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006772 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006773 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006774 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006775
Josef Bacik4b46fce2010-05-23 11:00:55 -04006776 free_extent_map(em);
6777
6778 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006779
6780unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006781 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6782 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6783 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006784}
6785
6786struct btrfs_dio_private {
6787 struct inode *inode;
6788 u64 logical_offset;
6789 u64 disk_bytenr;
6790 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006791 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006792
6793 /* number of bios pending for this dio */
6794 atomic_t pending_bios;
6795
6796 /* IO errors */
6797 int errors;
6798
Chris Mason9be33952013-05-17 18:30:14 -04006799 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006800 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006801
6802 /* dio_bio came from fs/direct-io.c */
6803 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006804};
6805
6806static void btrfs_endio_direct_read(struct bio *bio, int err)
6807{
Miao Xiee65e1532010-11-22 03:04:43 +00006808 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006809 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6810 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006811 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006812 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006813 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006814 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006815
6816 start = dip->logical_offset;
6817 do {
6818 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6819 struct page *page = bvec->bv_page;
6820 char *kaddr;
6821 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006822 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006823 unsigned long flags;
6824
Josef Bacikc3298612012-08-03 16:49:19 -04006825 if (get_state_private(&BTRFS_I(inode)->io_tree,
6826 start, &private))
6827 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006828 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006829 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006830 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006831 csum, bvec->bv_len);
6832 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006833 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006834 local_irq_restore(flags);
6835
6836 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006837 if (csum != private) {
6838failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006839 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6840 (unsigned long long)btrfs_ino(inode),
6841 (unsigned long long)start,
6842 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006843 err = -EIO;
6844 }
6845 }
6846
6847 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006848 bvec++;
6849 } while (bvec <= bvec_end);
6850
6851 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006852 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006853 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006854
Josef Bacik4b46fce2010-05-23 11:00:55 -04006855 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006856
6857 /* If we had a csum failure make sure to clear the uptodate flag */
6858 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006859 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6860 dio_end_io(dio_bio, err);
6861 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006862}
6863
6864static void btrfs_endio_direct_write(struct bio *bio, int err)
6865{
6866 struct btrfs_dio_private *dip = bio->bi_private;
6867 struct inode *inode = dip->inode;
6868 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006869 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006870 u64 ordered_offset = dip->logical_offset;
6871 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006872 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873 int ret;
6874
6875 if (err)
6876 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006877again:
6878 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6879 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006880 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006881 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006882 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006883
Josef Bacik5fd02042012-05-02 14:00:54 -04006884 ordered->work.func = finish_ordered_fn;
6885 ordered->work.flags = 0;
6886 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6887 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006888out_test:
6889 /*
6890 * our bio might span multiple ordered extents. If we haven't
6891 * completed the accounting for the whole dio, go back and try again
6892 */
6893 if (ordered_offset < dip->logical_offset + dip->bytes) {
6894 ordered_bytes = dip->logical_offset + dip->bytes -
6895 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006896 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006897 goto again;
6898 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006899out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006900 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006901
Josef Bacik4b46fce2010-05-23 11:00:55 -04006902 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006903
6904 /* If we had an error make sure to clear the uptodate flag */
6905 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006906 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6907 dio_end_io(dio_bio, err);
6908 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006909}
6910
Chris Masoneaf25d92010-05-25 09:48:28 -04006911static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6912 struct bio *bio, int mirror_num,
6913 unsigned long bio_flags, u64 offset)
6914{
6915 int ret;
6916 struct btrfs_root *root = BTRFS_I(inode)->root;
6917 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006918 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006919 return 0;
6920}
6921
Miao Xiee65e1532010-11-22 03:04:43 +00006922static void btrfs_end_dio_bio(struct bio *bio, int err)
6923{
6924 struct btrfs_dio_private *dip = bio->bi_private;
6925
6926 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006927 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006928 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006929 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006930 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006931 dip->errors = 1;
6932
6933 /*
6934 * before atomic variable goto zero, we must make sure
6935 * dip->errors is perceived to be set.
6936 */
6937 smp_mb__before_atomic_dec();
6938 }
6939
6940 /* if there are more bios still pending for this dio, just exit */
6941 if (!atomic_dec_and_test(&dip->pending_bios))
6942 goto out;
6943
Chris Mason9be33952013-05-17 18:30:14 -04006944 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006945 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006946 } else {
6947 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006948 bio_endio(dip->orig_bio, 0);
6949 }
6950out:
6951 bio_put(bio);
6952}
6953
6954static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6955 u64 first_sector, gfp_t gfp_flags)
6956{
6957 int nr_vecs = bio_get_nr_vecs(bdev);
6958 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6959}
6960
6961static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6962 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006963 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006964{
6965 int write = rw & REQ_WRITE;
6966 struct btrfs_root *root = BTRFS_I(inode)->root;
6967 int ret;
6968
Josef Bacikb812ce22012-11-16 13:56:32 -05006969 if (async_submit)
6970 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6971
Miao Xiee65e1532010-11-22 03:04:43 +00006972 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006973
6974 if (!write) {
6975 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6976 if (ret)
6977 goto err;
6978 }
Miao Xiee65e1532010-11-22 03:04:43 +00006979
Josef Bacik1ae39932011-04-06 14:41:34 -04006980 if (skip_sum)
6981 goto map;
6982
6983 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006984 ret = btrfs_wq_submit_bio(root->fs_info,
6985 inode, rw, bio, 0, 0,
6986 file_offset,
6987 __btrfs_submit_bio_start_direct_io,
6988 __btrfs_submit_bio_done);
6989 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006990 } else if (write) {
6991 /*
6992 * If we aren't doing async submit, calculate the csum of the
6993 * bio now.
6994 */
6995 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6996 if (ret)
6997 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006998 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006999 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007000 if (ret)
7001 goto err;
7002 }
Miao Xiee65e1532010-11-22 03:04:43 +00007003
Josef Bacik1ae39932011-04-06 14:41:34 -04007004map:
7005 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007006err:
7007 bio_put(bio);
7008 return ret;
7009}
7010
7011static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7012 int skip_sum)
7013{
7014 struct inode *inode = dip->inode;
7015 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007016 struct bio *bio;
7017 struct bio *orig_bio = dip->orig_bio;
7018 struct bio_vec *bvec = orig_bio->bi_io_vec;
7019 u64 start_sector = orig_bio->bi_sector;
7020 u64 file_offset = dip->logical_offset;
7021 u64 submit_len = 0;
7022 u64 map_length;
7023 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007024 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007025 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007026
Miao Xiee65e1532010-11-22 03:04:43 +00007027 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007028 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007029 &map_length, NULL, 0);
7030 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007031 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007032 return -EIO;
7033 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007034 if (map_length >= orig_bio->bi_size) {
7035 bio = orig_bio;
7036 goto submit;
7037 }
7038
David Woodhouse53b381b2013-01-29 18:40:14 -05007039 /* async crcs make it difficult to collect full stripe writes. */
7040 if (btrfs_get_alloc_profile(root, 1) &
7041 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7042 async_submit = 0;
7043 else
7044 async_submit = 1;
7045
Josef Bacik02f57c72011-04-06 14:25:44 -04007046 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7047 if (!bio)
7048 return -ENOMEM;
7049 bio->bi_private = dip;
7050 bio->bi_end_io = btrfs_end_dio_bio;
7051 atomic_inc(&dip->pending_bios);
7052
Miao Xiee65e1532010-11-22 03:04:43 +00007053 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7054 if (unlikely(map_length < submit_len + bvec->bv_len ||
7055 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7056 bvec->bv_offset) < bvec->bv_len)) {
7057 /*
7058 * inc the count before we submit the bio so
7059 * we know the end IO handler won't happen before
7060 * we inc the count. Otherwise, the dip might get freed
7061 * before we're done setting it up
7062 */
7063 atomic_inc(&dip->pending_bios);
7064 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7065 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007066 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007067 if (ret) {
7068 bio_put(bio);
7069 atomic_dec(&dip->pending_bios);
7070 goto out_err;
7071 }
7072
Miao Xiee65e1532010-11-22 03:04:43 +00007073 start_sector += submit_len >> 9;
7074 file_offset += submit_len;
7075
7076 submit_len = 0;
7077 nr_pages = 0;
7078
7079 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7080 start_sector, GFP_NOFS);
7081 if (!bio)
7082 goto out_err;
7083 bio->bi_private = dip;
7084 bio->bi_end_io = btrfs_end_dio_bio;
7085
7086 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007087 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007088 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007089 &map_length, NULL, 0);
7090 if (ret) {
7091 bio_put(bio);
7092 goto out_err;
7093 }
7094 } else {
7095 submit_len += bvec->bv_len;
7096 nr_pages ++;
7097 bvec++;
7098 }
7099 }
7100
Josef Bacik02f57c72011-04-06 14:25:44 -04007101submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007102 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007103 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007104 if (!ret)
7105 return 0;
7106
7107 bio_put(bio);
7108out_err:
7109 dip->errors = 1;
7110 /*
7111 * before atomic variable goto zero, we must
7112 * make sure dip->errors is perceived to be set.
7113 */
7114 smp_mb__before_atomic_dec();
7115 if (atomic_dec_and_test(&dip->pending_bios))
7116 bio_io_error(dip->orig_bio);
7117
7118 /* bio_end_io() will handle error, so we needn't return it */
7119 return 0;
7120}
7121
Chris Mason9be33952013-05-17 18:30:14 -04007122static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7123 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007124{
7125 struct btrfs_root *root = BTRFS_I(inode)->root;
7126 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007127 struct bio_vec *bvec = dio_bio->bi_io_vec;
7128 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007129 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007130 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007131 int ret = 0;
7132
7133 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7134
Chris Mason9be33952013-05-17 18:30:14 -04007135 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7136
7137 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007138 ret = -ENOMEM;
7139 goto free_ordered;
7140 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007141
Chris Mason9be33952013-05-17 18:30:14 -04007142 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7143 if (!dip) {
7144 ret = -ENOMEM;
7145 goto free_io_bio;
7146 }
7147
7148 dip->private = dio_bio->bi_private;
7149 io_bio->bi_private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150 dip->inode = inode;
7151 dip->logical_offset = file_offset;
7152
Josef Bacik4b46fce2010-05-23 11:00:55 -04007153 dip->bytes = 0;
7154 do {
7155 dip->bytes += bvec->bv_len;
7156 bvec++;
Chris Mason9be33952013-05-17 18:30:14 -04007157 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007158
Chris Mason9be33952013-05-17 18:30:14 -04007159 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7160 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007161 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007162 dip->orig_bio = io_bio;
7163 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007164 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007165
7166 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007167 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007168 else
Chris Mason9be33952013-05-17 18:30:14 -04007169 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007170
Miao Xiee65e1532010-11-22 03:04:43 +00007171 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7172 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007173 return;
Chris Mason9be33952013-05-17 18:30:14 -04007174
7175free_io_bio:
7176 bio_put(io_bio);
7177
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178free_ordered:
7179 /*
7180 * If this is a write, we need to clean up the reserved space and kill
7181 * the ordered extent.
7182 */
7183 if (write) {
7184 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007185 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7187 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7188 btrfs_free_reserved_extent(root, ordered->start,
7189 ordered->disk_len);
7190 btrfs_put_ordered_extent(ordered);
7191 btrfs_put_ordered_extent(ordered);
7192 }
Chris Mason9be33952013-05-17 18:30:14 -04007193 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007194}
7195
Chris Mason5a5f79b2010-05-26 21:33:37 -04007196static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7197 const struct iovec *iov, loff_t offset,
7198 unsigned long nr_segs)
7199{
7200 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007201 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007202 size_t size;
7203 unsigned long addr;
7204 unsigned blocksize_mask = root->sectorsize - 1;
7205 ssize_t retval = -EINVAL;
7206 loff_t end = offset;
7207
7208 if (offset & blocksize_mask)
7209 goto out;
7210
7211 /* Check the memory alignment. Blocks cannot straddle pages */
7212 for (seg = 0; seg < nr_segs; seg++) {
7213 addr = (unsigned long)iov[seg].iov_base;
7214 size = iov[seg].iov_len;
7215 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007216 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007217 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007218
7219 /* If this is a write we don't need to check anymore */
7220 if (rw & WRITE)
7221 continue;
7222
7223 /*
7224 * Check to make sure we don't have duplicate iov_base's in this
7225 * iovec, if so return EINVAL, otherwise we'll get csum errors
7226 * when reading back.
7227 */
7228 for (i = seg + 1; i < nr_segs; i++) {
7229 if (iov[seg].iov_base == iov[i].iov_base)
7230 goto out;
7231 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007232 }
7233 retval = 0;
7234out:
7235 return retval;
7236}
Josef Bacikeb838e72012-07-31 16:28:48 -04007237
Chris Mason16432982008-04-10 10:23:21 -04007238static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7239 const struct iovec *iov, loff_t offset,
7240 unsigned long nr_segs)
7241{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007242 struct file *file = iocb->ki_filp;
7243 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007244 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007245 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007246 bool wakeup = true;
7247 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007248 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249
Chris Mason5a5f79b2010-05-26 21:33:37 -04007250 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007251 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007252 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007253
Miao Xie38851cc2013-02-08 07:04:11 +00007254 atomic_inc(&inode->i_dio_count);
7255 smp_mb__after_atomic_inc();
7256
Miao Xie09348562013-02-07 10:12:07 +00007257 if (rw & WRITE) {
7258 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007259 /*
7260 * If the write DIO is beyond the EOF, we need update
7261 * the isize, but it is protected by i_mutex. So we can
7262 * not unlock the i_mutex at this case.
7263 */
7264 if (offset + count <= inode->i_size) {
7265 mutex_unlock(&inode->i_mutex);
7266 relock = true;
7267 }
Miao Xie09348562013-02-07 10:12:07 +00007268 ret = btrfs_delalloc_reserve_space(inode, count);
7269 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007270 goto out;
7271 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7272 &BTRFS_I(inode)->runtime_flags))) {
7273 inode_dio_done(inode);
7274 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7275 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007276 }
7277
7278 ret = __blockdev_direct_IO(rw, iocb, inode,
7279 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7280 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007281 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007282 if (rw & WRITE) {
7283 if (ret < 0 && ret != -EIOCBQUEUED)
7284 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007285 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007286 btrfs_delalloc_release_space(inode,
7287 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007288 else
7289 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007290 }
Miao Xie38851cc2013-02-08 07:04:11 +00007291out:
Miao Xie2e60a512013-02-08 07:01:08 +00007292 if (wakeup)
7293 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007294 if (relock)
7295 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007296
7297 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007298}
7299
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007300#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7301
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007302static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7303 __u64 start, __u64 len)
7304{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007305 int ret;
7306
7307 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7308 if (ret)
7309 return ret;
7310
Chris Masonec29ed52011-02-23 16:23:20 -05007311 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007312}
7313
Chris Mason9ebefb182007-06-15 13:50:00 -04007314int btrfs_readpage(struct file *file, struct page *page)
7315{
Chris Masond1310b22008-01-24 16:13:08 -05007316 struct extent_io_tree *tree;
7317 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007318 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007319}
Chris Mason1832a6d2007-12-21 16:27:21 -05007320
Chris Mason39279cc2007-06-12 06:35:45 -04007321static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7322{
Chris Masond1310b22008-01-24 16:13:08 -05007323 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007324
7325
7326 if (current->flags & PF_MEMALLOC) {
7327 redirty_page_for_writepage(wbc, page);
7328 unlock_page(page);
7329 return 0;
7330 }
Chris Masond1310b22008-01-24 16:13:08 -05007331 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007332 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7333}
Chris Mason39279cc2007-06-12 06:35:45 -04007334
Eric Sandeen48a3b632013-04-25 20:41:01 +00007335static int btrfs_writepages(struct address_space *mapping,
7336 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007337{
Chris Masond1310b22008-01-24 16:13:08 -05007338 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007339
Chris Masond1310b22008-01-24 16:13:08 -05007340 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007341 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7342}
7343
Chris Mason3ab2fb52007-11-08 10:59:22 -05007344static int
7345btrfs_readpages(struct file *file, struct address_space *mapping,
7346 struct list_head *pages, unsigned nr_pages)
7347{
Chris Masond1310b22008-01-24 16:13:08 -05007348 struct extent_io_tree *tree;
7349 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007350 return extent_readpages(tree, mapping, pages, nr_pages,
7351 btrfs_get_extent);
7352}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007353static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007354{
Chris Masond1310b22008-01-24 16:13:08 -05007355 struct extent_io_tree *tree;
7356 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007357 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007358
Chris Masond1310b22008-01-24 16:13:08 -05007359 tree = &BTRFS_I(page->mapping->host)->io_tree;
7360 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007361 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007362 if (ret == 1) {
7363 ClearPagePrivate(page);
7364 set_page_private(page, 0);
7365 page_cache_release(page);
7366 }
7367 return ret;
7368}
Chris Mason39279cc2007-06-12 06:35:45 -04007369
Chris Masone6dcd2d2008-07-17 12:53:50 -04007370static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7371{
Chris Mason98509cf2008-09-11 15:51:43 -04007372 if (PageWriteback(page) || PageDirty(page))
7373 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007374 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007375}
7376
Chris Masona52d9a82007-08-27 16:49:44 -04007377static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7378{
Josef Bacik5fd02042012-05-02 14:00:54 -04007379 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007380 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007381 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007382 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007383 u64 page_start = page_offset(page);
7384 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007385
Chris Mason8b62b722009-09-02 16:53:46 -04007386 /*
7387 * we have the page locked, so new writeback can't start,
7388 * and the dirty bit won't be cleared while we are here.
7389 *
7390 * Wait for IO on this page so that we can safely clear
7391 * the PagePrivate2 bit and do ordered accounting
7392 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007393 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007394
Josef Bacik5fd02042012-05-02 14:00:54 -04007395 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007396 if (offset) {
7397 btrfs_releasepage(page, GFP_NOFS);
7398 return;
7399 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007400 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007401 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007402 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007403 /*
7404 * IO on this page will never be started, so we need
7405 * to account for any ordered extents now
7406 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007407 clear_extent_bit(tree, page_start, page_end,
7408 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007409 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7410 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007411 /*
7412 * whoever cleared the private bit is responsible
7413 * for the finish_ordered_io
7414 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007415 if (TestClearPagePrivate2(page) &&
7416 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7417 PAGE_CACHE_SIZE, 1)) {
7418 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007419 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007420 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007421 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007422 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007423 }
7424 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007425 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007426 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7427 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007428 __btrfs_releasepage(page, GFP_NOFS);
7429
Chris Mason4a096752008-07-21 10:29:44 -04007430 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007431 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007432 ClearPagePrivate(page);
7433 set_page_private(page, 0);
7434 page_cache_release(page);
7435 }
Chris Mason39279cc2007-06-12 06:35:45 -04007436}
7437
Chris Mason9ebefb182007-06-15 13:50:00 -04007438/*
7439 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7440 * called from a page fault handler when a page is first dirtied. Hence we must
7441 * be careful to check for EOF conditions here. We set the page up correctly
7442 * for a written page which means we get ENOSPC checking when writing into
7443 * holes and correct delalloc and unwritten extent mapping on filesystems that
7444 * support these features.
7445 *
7446 * We are not allowed to take the i_mutex here so we have to play games to
7447 * protect against truncate races as the page could now be beyond EOF. Because
7448 * vmtruncate() writes the inode size before removing pages, once we have the
7449 * page lock we can determine safely if the page is beyond EOF. If it is not
7450 * beyond EOF, then the page is guaranteed safe against truncation until we
7451 * unlock the page.
7452 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007453int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007454{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007455 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007456 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007457 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007458 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7459 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007460 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007461 char *kaddr;
7462 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007463 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007464 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007465 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007466 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007467 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007468
Jan Karab2b5ef52012-06-12 16:20:45 +02007469 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007470 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007471 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007472 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007473 reserved = 1;
7474 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007475 if (ret) {
7476 if (ret == -ENOMEM)
7477 ret = VM_FAULT_OOM;
7478 else /* -ENOSPC, -EIO, etc */
7479 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007480 if (reserved)
7481 goto out;
7482 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007483 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007484
Nick Piggin56a76f82009-03-31 15:23:23 -07007485 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007486again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007487 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007488 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007489 page_start = page_offset(page);
7490 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007491
Chris Mason9ebefb182007-06-15 13:50:00 -04007492 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007493 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007494 /* page got truncated out from underneath us */
7495 goto out_unlock;
7496 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007497 wait_on_page_writeback(page);
7498
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007499 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007500 set_page_extent_mapped(page);
7501
Chris Masoneb84ae02008-07-17 13:53:27 -04007502 /*
7503 * we can't set the delalloc bits if there are pending ordered
7504 * extents. Drop our locks and wait for them to finish
7505 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7507 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007508 unlock_extent_cached(io_tree, page_start, page_end,
7509 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007510 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007511 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007512 btrfs_put_ordered_extent(ordered);
7513 goto again;
7514 }
7515
Josef Bacikfbf19082009-10-01 17:10:23 -04007516 /*
7517 * XXX - page_mkwrite gets called every time the page is dirtied, even
7518 * if it was already dirty, so for space accounting reasons we need to
7519 * clear any delalloc bits for the range we are fixing to save. There
7520 * is probably a better way to do this, but for now keep consistent with
7521 * prepare_pages in the normal write path.
7522 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007523 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007524 EXTENT_DIRTY | EXTENT_DELALLOC |
7525 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007526 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007527
Josef Bacik2ac55d42010-02-03 19:33:23 +00007528 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7529 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007530 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007531 unlock_extent_cached(io_tree, page_start, page_end,
7532 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007533 ret = VM_FAULT_SIGBUS;
7534 goto out_unlock;
7535 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007536 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007537
7538 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007539 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007540 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007541 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007542 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007543
Chris Masone6dcd2d2008-07-17 12:53:50 -04007544 if (zero_start != PAGE_CACHE_SIZE) {
7545 kaddr = kmap(page);
7546 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7547 flush_dcache_page(page);
7548 kunmap(page);
7549 }
Chris Mason247e7432008-07-17 12:53:51 -04007550 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007551 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007552 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007553
Chris Mason257c62e2009-10-13 13:21:08 -04007554 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7555 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007556 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007557
Josef Bacik2ac55d42010-02-03 19:33:23 +00007558 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007559
7560out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007561 if (!ret) {
7562 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007563 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007564 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007565 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007566out:
Josef Bacikec39e182012-01-12 19:10:12 -05007567 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007568out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007569 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007570 return ret;
7571}
7572
Josef Bacika41ad392011-01-31 15:30:16 -05007573static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007574{
7575 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007576 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007577 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007578 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007579 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007580 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007581 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007582
Chris Mason4a096752008-07-21 10:29:44 -04007583 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007584 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007585
Josef Bacikfcb80c22011-05-03 10:40:22 -04007586 /*
7587 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7588 * 3 things going on here
7589 *
7590 * 1) We need to reserve space for our orphan item and the space to
7591 * delete our orphan item. Lord knows we don't want to have a dangling
7592 * orphan item because we didn't reserve space to remove it.
7593 *
7594 * 2) We need to reserve space to update our inode.
7595 *
7596 * 3) We need to have something to cache all the space that is going to
7597 * be free'd up by the truncate operation, but also have some slack
7598 * space reserved in case it uses space during the truncate (thank you
7599 * very much snapshotting).
7600 *
7601 * And we need these to all be seperate. The fact is we can use alot of
7602 * space doing the truncate, and we have no earthly idea how much space
7603 * we will use, so we need the truncate reservation to be seperate so it
7604 * doesn't end up using space reserved for updating the inode or
7605 * removing the orphan item. We also need to be able to stop the
7606 * transaction and start a new one, which means we need to be able to
7607 * update the inode several times, and we have no idea of knowing how
7608 * many times that will be, so we can't just reserve 1 item for the
7609 * entirety of the opration, so that has to be done seperately as well.
7610 * Then there is the orphan item, which does indeed need to be held on
7611 * to for the whole operation, and we need nobody to touch this reserved
7612 * space except the orphan code.
7613 *
7614 * So that leaves us with
7615 *
7616 * 1) root->orphan_block_rsv - for the orphan deletion.
7617 * 2) rsv - for the truncate reservation, which we will steal from the
7618 * transaction reservation.
7619 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7620 * updating the inode.
7621 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007622 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007623 if (!rsv)
7624 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007625 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007626 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007627
Josef Bacik907cbce2011-08-08 13:46:15 -04007628 /*
Josef Bacik07127182011-08-19 10:29:59 -04007629 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007630 * 1 for updating the inode.
7631 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007632 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007633 if (IS_ERR(trans)) {
7634 err = PTR_ERR(trans);
7635 goto out;
7636 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007637
Josef Bacik907cbce2011-08-08 13:46:15 -04007638 /* Migrate the slack space for the truncate to our reserve */
7639 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7640 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007641 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007642
Chris Mason5a3f23d2009-03-31 13:27:11 -04007643 /*
7644 * setattr is responsible for setting the ordered_data_close flag,
7645 * but that is only tested during the last file release. That
7646 * could happen well after the next commit, leaving a great big
7647 * window where new writes may get lost if someone chooses to write
7648 * to this file after truncating to zero
7649 *
7650 * The inode doesn't have any dirty data here, and so if we commit
7651 * this is a noop. If someone immediately starts writing to the inode
7652 * it is very likely we'll catch some of their writes in this
7653 * transaction, and the commit will find this file on the ordered
7654 * data list with good things to send down.
7655 *
7656 * This is a best effort solution, there is still a window where
7657 * using truncate to replace the contents of the file will
7658 * end up with a zero length file after a crash.
7659 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007660 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7661 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007662 btrfs_add_ordered_operation(trans, root, inode);
7663
Josef Bacik5dc562c2012-08-17 13:14:17 -04007664 /*
7665 * So if we truncate and then write and fsync we normally would just
7666 * write the extents that changed, which is a problem if we need to
7667 * first truncate that entire inode. So set this flag so we write out
7668 * all of the extents in the inode to the sync log so we're completely
7669 * safe.
7670 */
7671 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007672 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007673
Yan, Zheng80825102009-11-12 09:35:36 +00007674 while (1) {
7675 ret = btrfs_truncate_inode_items(trans, root, inode,
7676 inode->i_size,
7677 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007678 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007679 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007680 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007681 }
Chris Mason39279cc2007-06-12 06:35:45 -04007682
Josef Bacikfcb80c22011-05-03 10:40:22 -04007683 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007684 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007685 if (ret) {
7686 err = ret;
7687 break;
7688 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007689
Yan, Zheng80825102009-11-12 09:35:36 +00007690 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007691 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007692
7693 trans = btrfs_start_transaction(root, 2);
7694 if (IS_ERR(trans)) {
7695 ret = err = PTR_ERR(trans);
7696 trans = NULL;
7697 break;
7698 }
7699
7700 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7701 rsv, min_size);
7702 BUG_ON(ret); /* shouldn't happen */
7703 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007704 }
7705
7706 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007707 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007708 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007709 if (ret)
7710 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007711 }
7712
Chris Mason917c16b2011-11-08 14:49:59 -05007713 if (trans) {
7714 trans->block_rsv = &root->fs_info->trans_block_rsv;
7715 ret = btrfs_update_inode(trans, root, inode);
7716 if (ret && !err)
7717 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007718
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007719 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007720 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007721 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007722
7723out:
7724 btrfs_free_block_rsv(root, rsv);
7725
Josef Bacik3893e332011-01-31 16:03:11 -05007726 if (ret && !err)
7727 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007728
Josef Bacik3893e332011-01-31 16:03:11 -05007729 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007730}
7731
Sven Wegener3b963622008-06-09 21:57:42 -04007732/*
Chris Masond352ac62008-09-29 15:18:18 -04007733 * create a new subvolume directory/inode (helper for the ioctl).
7734 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007735int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007736 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007737{
Chris Mason39279cc2007-06-12 06:35:45 -04007738 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007739 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007740 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007741
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007742 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7743 new_dirid, new_dirid,
7744 S_IFDIR | (~current_umask() & S_IRWXUGO),
7745 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007746 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007747 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007748 inode->i_op = &btrfs_dir_inode_operations;
7749 inode->i_fop = &btrfs_dir_file_operations;
7750
Miklos Szeredibfe86842011-10-28 14:13:29 +02007751 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007752 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007753
Yan, Zheng76dda932009-09-21 16:00:26 -04007754 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007755
Yan, Zheng76dda932009-09-21 16:00:26 -04007756 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007757 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007758}
7759
Chris Mason39279cc2007-06-12 06:35:45 -04007760struct inode *btrfs_alloc_inode(struct super_block *sb)
7761{
7762 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007763 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007764
7765 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7766 if (!ei)
7767 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007768
7769 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007770 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007771 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007772 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007773 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007774 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007775 ei->disk_i_size = 0;
7776 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007777 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007778 ei->index_cnt = (u64)-1;
7779 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007780 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007781
Josef Bacik9e0baf62011-07-15 15:16:44 +00007782 spin_lock_init(&ei->lock);
7783 ei->outstanding_extents = 0;
7784 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007785
Josef Bacik72ac3c02012-05-23 14:13:11 -04007786 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007787 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007788
Miao Xie16cdcec2011-04-22 18:12:22 +08007789 ei->delayed_node = NULL;
7790
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007791 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007792 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007793 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7794 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007795 ei->io_tree.track_uptodate = 1;
7796 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007797 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007798 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007799 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007800 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007801 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007802 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007803 RB_CLEAR_NODE(&ei->rb_node);
7804
7805 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007806}
7807
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007808static void btrfs_i_callback(struct rcu_head *head)
7809{
7810 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007811 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7812}
7813
Chris Mason39279cc2007-06-12 06:35:45 -04007814void btrfs_destroy_inode(struct inode *inode)
7815{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007816 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007817 struct btrfs_root *root = BTRFS_I(inode)->root;
7818
Al Virob3d9b7a2012-06-09 13:51:19 -04007819 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007820 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007821 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7822 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007823 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7824 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007825
Chris Mason5a3f23d2009-03-31 13:27:11 -04007826 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007827 * This can happen where we create an inode, but somebody else also
7828 * created the same inode and we need to destroy the one we already
7829 * created.
7830 */
7831 if (!root)
7832 goto free;
7833
7834 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007835 * Make sure we're properly removed from the ordered operation
7836 * lists.
7837 */
7838 smp_mb();
7839 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007840 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007841 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007842 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007843 }
7844
Josef Bacik8a35d952012-05-23 14:26:42 -04007845 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7846 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007847 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7848 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007849 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007850 }
Josef Bacik7b128762008-07-24 12:17:14 -04007851
Chris Masond3977122009-01-05 21:25:51 -05007852 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007853 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7854 if (!ordered)
7855 break;
7856 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007857 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7858 (unsigned long long)ordered->file_offset,
7859 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007860 btrfs_remove_ordered_extent(inode, ordered);
7861 btrfs_put_ordered_extent(ordered);
7862 btrfs_put_ordered_extent(ordered);
7863 }
7864 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007865 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007866 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007867free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007868 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007869}
7870
Al Viro45321ac2010-06-07 13:43:19 -04007871int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007872{
7873 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007874
Naohiro Aota6379ef92013-06-06 09:56:34 +00007875 if (root == NULL)
7876 return 1;
7877
Liu Bofa6ac872013-02-20 14:10:23 +00007878 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007879 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007880 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007881 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007882 else
Al Viro45321ac2010-06-07 13:43:19 -04007883 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007884}
7885
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007886static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007887{
7888 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7889
7890 inode_init_once(&ei->vfs_inode);
7891}
7892
7893void btrfs_destroy_cachep(void)
7894{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007895 /*
7896 * Make sure all delayed rcu free inodes are flushed before we
7897 * destroy cache.
7898 */
7899 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007900 if (btrfs_inode_cachep)
7901 kmem_cache_destroy(btrfs_inode_cachep);
7902 if (btrfs_trans_handle_cachep)
7903 kmem_cache_destroy(btrfs_trans_handle_cachep);
7904 if (btrfs_transaction_cachep)
7905 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007906 if (btrfs_path_cachep)
7907 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007908 if (btrfs_free_space_cachep)
7909 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007910 if (btrfs_delalloc_work_cachep)
7911 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007912}
7913
7914int btrfs_init_cachep(void)
7915{
David Sterba837e1972012-09-07 03:00:48 -06007916 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007917 sizeof(struct btrfs_inode), 0,
7918 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007919 if (!btrfs_inode_cachep)
7920 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007921
David Sterba837e1972012-09-07 03:00:48 -06007922 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007923 sizeof(struct btrfs_trans_handle), 0,
7924 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007925 if (!btrfs_trans_handle_cachep)
7926 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007927
David Sterba837e1972012-09-07 03:00:48 -06007928 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007929 sizeof(struct btrfs_transaction), 0,
7930 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007931 if (!btrfs_transaction_cachep)
7932 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007933
David Sterba837e1972012-09-07 03:00:48 -06007934 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007935 sizeof(struct btrfs_path), 0,
7936 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007937 if (!btrfs_path_cachep)
7938 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007939
David Sterba837e1972012-09-07 03:00:48 -06007940 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007941 sizeof(struct btrfs_free_space), 0,
7942 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7943 if (!btrfs_free_space_cachep)
7944 goto fail;
7945
Miao Xie8ccf6f192012-10-25 09:28:04 +00007946 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7947 sizeof(struct btrfs_delalloc_work), 0,
7948 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7949 NULL);
7950 if (!btrfs_delalloc_work_cachep)
7951 goto fail;
7952
Chris Mason39279cc2007-06-12 06:35:45 -04007953 return 0;
7954fail:
7955 btrfs_destroy_cachep();
7956 return -ENOMEM;
7957}
7958
7959static int btrfs_getattr(struct vfsmount *mnt,
7960 struct dentry *dentry, struct kstat *stat)
7961{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007962 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007963 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007964 u32 blocksize = inode->i_sb->s_blocksize;
7965
Chris Mason39279cc2007-06-12 06:35:45 -04007966 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007967 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007968 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007969
7970 spin_lock(&BTRFS_I(inode)->lock);
7971 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7972 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007973 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007974 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007975 return 0;
7976}
7977
Chris Masond3977122009-01-05 21:25:51 -05007978static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7979 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007980{
7981 struct btrfs_trans_handle *trans;
7982 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007983 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007984 struct inode *new_inode = new_dentry->d_inode;
7985 struct inode *old_inode = old_dentry->d_inode;
7986 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007987 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007988 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007989 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007990 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007991
Li Zefan33345d012011-04-20 10:31:50 +08007992 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007993 return -EPERM;
7994
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007995 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007996 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007997 return -EXDEV;
7998
Li Zefan33345d012011-04-20 10:31:50 +08007999 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8000 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008001 return -ENOTEMPTY;
8002
Chris Mason39279cc2007-06-12 06:35:45 -04008003 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008004 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008005 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008006
8007
8008 /* check for collisions, even if the name isn't there */
8009 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8010 new_dentry->d_name.name,
8011 new_dentry->d_name.len);
8012
8013 if (ret) {
8014 if (ret == -EEXIST) {
8015 /* we shouldn't get
8016 * eexist without a new_inode */
8017 if (!new_inode) {
8018 WARN_ON(1);
8019 return ret;
8020 }
8021 } else {
8022 /* maybe -EOVERFLOW */
8023 return ret;
8024 }
8025 }
8026 ret = 0;
8027
Chris Mason5a3f23d2009-03-31 13:27:11 -04008028 /*
8029 * we're using rename to replace one file with another.
8030 * and the replacement file is large. Start IO on it now so
8031 * we don't add too much work to the end of the transaction
8032 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008033 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008034 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8035 filemap_flush(old_inode->i_mapping);
8036
Yan, Zheng76dda932009-09-21 16:00:26 -04008037 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008038 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008039 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008040 /*
8041 * We want to reserve the absolute worst case amount of items. So if
8042 * both inodes are subvols and we need to unlink them then that would
8043 * require 4 item modifications, but if they are both normal inodes it
8044 * would require 5 item modifications, so we'll assume their normal
8045 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8046 * should cover the worst case number of items we'll modify.
8047 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008048 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008049 if (IS_ERR(trans)) {
8050 ret = PTR_ERR(trans);
8051 goto out_notrans;
8052 }
Chris Mason5f39d392007-10-15 16:14:19 -04008053
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008054 if (dest != root)
8055 btrfs_record_root_in_trans(trans, dest);
8056
Yan, Zhenga5719522009-09-24 09:17:31 -04008057 ret = btrfs_set_inode_index(new_dir, &index);
8058 if (ret)
8059 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008060
Li Zefan33345d012011-04-20 10:31:50 +08008061 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008062 /* force full log commit if subvolume involved. */
8063 root->fs_info->last_trans_log_full_commit = trans->transid;
8064 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008065 ret = btrfs_insert_inode_ref(trans, dest,
8066 new_dentry->d_name.name,
8067 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008068 old_ino,
8069 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008070 if (ret)
8071 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008072 /*
8073 * this is an ugly little race, but the rename is required
8074 * to make sure that if we crash, the inode is either at the
8075 * old name or the new one. pinning the log transaction lets
8076 * us make sure we don't allow a log commit to come in after
8077 * we unlink the name but before we add the new name back in.
8078 */
8079 btrfs_pin_log_trans(root);
8080 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008081 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008082 * make sure the inode gets flushed if it is replacing
8083 * something.
8084 */
Li Zefan33345d012011-04-20 10:31:50 +08008085 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008086 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008087
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008088 inode_inc_iversion(old_dir);
8089 inode_inc_iversion(new_dir);
8090 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008091 old_dir->i_ctime = old_dir->i_mtime = ctime;
8092 new_dir->i_ctime = new_dir->i_mtime = ctime;
8093 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008094
Chris Mason12fcfd22009-03-24 10:24:20 -04008095 if (old_dentry->d_parent != new_dentry->d_parent)
8096 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8097
Li Zefan33345d012011-04-20 10:31:50 +08008098 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008099 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8100 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8101 old_dentry->d_name.name,
8102 old_dentry->d_name.len);
8103 } else {
Al Viro92986792011-03-04 17:14:37 +00008104 ret = __btrfs_unlink_inode(trans, root, old_dir,
8105 old_dentry->d_inode,
8106 old_dentry->d_name.name,
8107 old_dentry->d_name.len);
8108 if (!ret)
8109 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008110 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008111 if (ret) {
8112 btrfs_abort_transaction(trans, root, ret);
8113 goto out_fail;
8114 }
Chris Mason39279cc2007-06-12 06:35:45 -04008115
8116 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008117 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008118 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008119 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008120 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8121 root_objectid = BTRFS_I(new_inode)->location.objectid;
8122 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8123 root_objectid,
8124 new_dentry->d_name.name,
8125 new_dentry->d_name.len);
8126 BUG_ON(new_inode->i_nlink == 0);
8127 } else {
8128 ret = btrfs_unlink_inode(trans, dest, new_dir,
8129 new_dentry->d_inode,
8130 new_dentry->d_name.name,
8131 new_dentry->d_name.len);
8132 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008133 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008134 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008135 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008136 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008137 if (ret) {
8138 btrfs_abort_transaction(trans, root, ret);
8139 goto out_fail;
8140 }
Chris Mason39279cc2007-06-12 06:35:45 -04008141 }
Josef Bacikaec74772008-07-24 12:12:38 -04008142
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008143 ret = btrfs_add_link(trans, new_dir, old_inode,
8144 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008145 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008146 if (ret) {
8147 btrfs_abort_transaction(trans, root, ret);
8148 goto out_fail;
8149 }
Chris Mason39279cc2007-06-12 06:35:45 -04008150
Li Zefan33345d012011-04-20 10:31:50 +08008151 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008152 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008153 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008154 btrfs_end_log_trans(root);
8155 }
Chris Mason39279cc2007-06-12 06:35:45 -04008156out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008157 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008158out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008159 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008160 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008161
Chris Mason39279cc2007-06-12 06:35:45 -04008162 return ret;
8163}
8164
Miao Xie8ccf6f192012-10-25 09:28:04 +00008165static void btrfs_run_delalloc_work(struct btrfs_work *work)
8166{
8167 struct btrfs_delalloc_work *delalloc_work;
8168
8169 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8170 work);
8171 if (delalloc_work->wait)
8172 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8173 else
8174 filemap_flush(delalloc_work->inode->i_mapping);
8175
8176 if (delalloc_work->delay_iput)
8177 btrfs_add_delayed_iput(delalloc_work->inode);
8178 else
8179 iput(delalloc_work->inode);
8180 complete(&delalloc_work->completion);
8181}
8182
8183struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8184 int wait, int delay_iput)
8185{
8186 struct btrfs_delalloc_work *work;
8187
8188 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8189 if (!work)
8190 return NULL;
8191
8192 init_completion(&work->completion);
8193 INIT_LIST_HEAD(&work->list);
8194 work->inode = inode;
8195 work->wait = wait;
8196 work->delay_iput = delay_iput;
8197 work->work.func = btrfs_run_delalloc_work;
8198
8199 return work;
8200}
8201
8202void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8203{
8204 wait_for_completion(&work->completion);
8205 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8206}
8207
Chris Masond352ac62008-09-29 15:18:18 -04008208/*
8209 * some fairly slow code that needs optimization. This walks the list
8210 * of all the inodes with pending delalloc and forces them to disk.
8211 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008212static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008213{
Chris Masonea8c2812008-08-04 23:17:27 -04008214 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008215 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008216 struct btrfs_delalloc_work *work, *next;
8217 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008218 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008219 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008220
Miao Xie8ccf6f192012-10-25 09:28:04 +00008221 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008222 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008223
Miao Xieeb73c1b2013-05-15 07:48:22 +00008224 spin_lock(&root->delalloc_lock);
8225 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008226 while (!list_empty(&splice)) {
8227 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008228 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008229
Miao Xieeb73c1b2013-05-15 07:48:22 +00008230 list_move_tail(&binode->delalloc_inodes,
8231 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008232 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008233 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008234 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008235 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008236 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008237 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008238
8239 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8240 if (unlikely(!work)) {
8241 ret = -ENOMEM;
8242 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008243 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008244 list_add_tail(&work->list, &works);
8245 btrfs_queue_worker(&root->fs_info->flush_workers,
8246 &work->work);
8247
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008248 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008249 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008250 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008251 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008252
Miao Xie1eafa6c2013-01-22 10:49:00 +00008253 list_for_each_entry_safe(work, next, &works, list) {
8254 list_del_init(&work->list);
8255 btrfs_wait_and_free_delalloc_work(work);
8256 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008257 return 0;
8258out:
8259 list_for_each_entry_safe(work, next, &works, list) {
8260 list_del_init(&work->list);
8261 btrfs_wait_and_free_delalloc_work(work);
8262 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008263
Miao Xieeb73c1b2013-05-15 07:48:22 +00008264 if (!list_empty_careful(&splice)) {
8265 spin_lock(&root->delalloc_lock);
8266 list_splice_tail(&splice, &root->delalloc_inodes);
8267 spin_unlock(&root->delalloc_lock);
8268 }
8269 return ret;
8270}
8271
8272int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8273{
8274 int ret;
8275
8276 if (root->fs_info->sb->s_flags & MS_RDONLY)
8277 return -EROFS;
8278
8279 ret = __start_delalloc_inodes(root, delay_iput);
8280 /*
8281 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008282 * we have to make sure the IO is actually started and that
8283 * ordered extents get created before we return
8284 */
8285 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008286 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008287 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008288 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008289 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8290 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008291 }
8292 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008293 return ret;
8294}
8295
8296int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8297 int delay_iput)
8298{
8299 struct btrfs_root *root;
8300 struct list_head splice;
8301 int ret;
8302
8303 if (fs_info->sb->s_flags & MS_RDONLY)
8304 return -EROFS;
8305
8306 INIT_LIST_HEAD(&splice);
8307
8308 spin_lock(&fs_info->delalloc_root_lock);
8309 list_splice_init(&fs_info->delalloc_roots, &splice);
8310 while (!list_empty(&splice)) {
8311 root = list_first_entry(&splice, struct btrfs_root,
8312 delalloc_root);
8313 root = btrfs_grab_fs_root(root);
8314 BUG_ON(!root);
8315 list_move_tail(&root->delalloc_root,
8316 &fs_info->delalloc_roots);
8317 spin_unlock(&fs_info->delalloc_root_lock);
8318
8319 ret = __start_delalloc_inodes(root, delay_iput);
8320 btrfs_put_fs_root(root);
8321 if (ret)
8322 goto out;
8323
8324 spin_lock(&fs_info->delalloc_root_lock);
8325 }
8326 spin_unlock(&fs_info->delalloc_root_lock);
8327
8328 atomic_inc(&fs_info->async_submit_draining);
8329 while (atomic_read(&fs_info->nr_async_submits) ||
8330 atomic_read(&fs_info->async_delalloc_pages)) {
8331 wait_event(fs_info->async_submit_wait,
8332 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8333 atomic_read(&fs_info->async_delalloc_pages) == 0));
8334 }
8335 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008336 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008337out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008338 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008339 spin_lock(&fs_info->delalloc_root_lock);
8340 list_splice_tail(&splice, &fs_info->delalloc_roots);
8341 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008342 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008343 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008344}
8345
Chris Mason39279cc2007-06-12 06:35:45 -04008346static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8347 const char *symname)
8348{
8349 struct btrfs_trans_handle *trans;
8350 struct btrfs_root *root = BTRFS_I(dir)->root;
8351 struct btrfs_path *path;
8352 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008353 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008354 int err;
8355 int drop_inode = 0;
8356 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008357 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008358 int name_len;
8359 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008360 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008361 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008362 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008363
8364 name_len = strlen(symname) + 1;
8365 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8366 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008367
Josef Bacik9ed74f22009-09-11 16:12:44 -04008368 /*
8369 * 2 items for inode item and ref
8370 * 2 items for dir items
8371 * 1 item for xattr if selinux is on
8372 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008373 trans = btrfs_start_transaction(root, 5);
8374 if (IS_ERR(trans))
8375 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008376
Li Zefan581bb052011-04-20 10:06:11 +08008377 err = btrfs_find_free_ino(root, &objectid);
8378 if (err)
8379 goto out_unlock;
8380
Josef Bacikaec74772008-07-24 12:12:38 -04008381 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008382 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008383 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008384 if (IS_ERR(inode)) {
8385 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008386 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008387 }
Chris Mason39279cc2007-06-12 06:35:45 -04008388
Eric Paris2a7dba32011-02-01 11:05:39 -05008389 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008390 if (err) {
8391 drop_inode = 1;
8392 goto out_unlock;
8393 }
8394
Casey Schauflerad19db72011-12-15 10:09:07 -05008395 /*
8396 * If the active LSM wants to access the inode during
8397 * d_instantiate it needs these. Smack checks to see
8398 * if the filesystem supports xattrs by looking at the
8399 * ops vector.
8400 */
8401 inode->i_fop = &btrfs_file_operations;
8402 inode->i_op = &btrfs_file_inode_operations;
8403
Josef Bacika1b075d2010-11-19 20:36:11 +00008404 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008405 if (err)
8406 drop_inode = 1;
8407 else {
8408 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008409 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008410 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008411 }
Chris Mason39279cc2007-06-12 06:35:45 -04008412 if (drop_inode)
8413 goto out_unlock;
8414
8415 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008416 if (!path) {
8417 err = -ENOMEM;
8418 drop_inode = 1;
8419 goto out_unlock;
8420 }
Li Zefan33345d012011-04-20 10:31:50 +08008421 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008422 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008423 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8424 datasize = btrfs_file_extent_calc_inline_size(name_len);
8425 err = btrfs_insert_empty_item(trans, root, path, &key,
8426 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008427 if (err) {
8428 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008429 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008430 goto out_unlock;
8431 }
Chris Mason5f39d392007-10-15 16:14:19 -04008432 leaf = path->nodes[0];
8433 ei = btrfs_item_ptr(leaf, path->slots[0],
8434 struct btrfs_file_extent_item);
8435 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8436 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008437 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008438 btrfs_set_file_extent_encryption(leaf, ei, 0);
8439 btrfs_set_file_extent_compression(leaf, ei, 0);
8440 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8441 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8442
Chris Mason39279cc2007-06-12 06:35:45 -04008443 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008444 write_extent_buffer(leaf, symname, ptr, name_len);
8445 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008446 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008447
Chris Mason39279cc2007-06-12 06:35:45 -04008448 inode->i_op = &btrfs_symlink_inode_operations;
8449 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008450 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008451 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008452 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008453 err = btrfs_update_inode(trans, root, inode);
8454 if (err)
8455 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008456
8457out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008458 if (!err)
8459 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008460 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008461 if (drop_inode) {
8462 inode_dec_link_count(inode);
8463 iput(inode);
8464 }
Liu Bob53d3f52012-11-14 14:34:34 +00008465 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008466 return err;
8467}
Chris Mason16432982008-04-10 10:23:21 -04008468
Josef Bacik0af3d002010-06-21 14:48:16 -04008469static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8470 u64 start, u64 num_bytes, u64 min_size,
8471 loff_t actual_len, u64 *alloc_hint,
8472 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008473{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008474 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8475 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008476 struct btrfs_root *root = BTRFS_I(inode)->root;
8477 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008478 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008479 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008480 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008481 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008482 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008483
Josef Bacik0af3d002010-06-21 14:48:16 -04008484 if (trans)
8485 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008486 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008487 if (own_trans) {
8488 trans = btrfs_start_transaction(root, 3);
8489 if (IS_ERR(trans)) {
8490 ret = PTR_ERR(trans);
8491 break;
8492 }
Yan Zhengd899e052008-10-30 14:25:28 -04008493 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008494
Chris Mason154ea282013-03-05 11:11:26 -05008495 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8496 cur_bytes = max(cur_bytes, min_size);
8497 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008498 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008499 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008500 if (own_trans)
8501 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008502 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008503 }
8504
Yan Zhengd899e052008-10-30 14:25:28 -04008505 ret = insert_reserved_file_extent(trans, inode,
8506 cur_offset, ins.objectid,
8507 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008508 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008509 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008510 if (ret) {
8511 btrfs_abort_transaction(trans, root, ret);
8512 if (own_trans)
8513 btrfs_end_transaction(trans, root);
8514 break;
8515 }
Chris Masona1ed8352009-09-11 12:27:37 -04008516 btrfs_drop_extent_cache(inode, cur_offset,
8517 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008518
Josef Bacik5dc562c2012-08-17 13:14:17 -04008519 em = alloc_extent_map();
8520 if (!em) {
8521 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8522 &BTRFS_I(inode)->runtime_flags);
8523 goto next;
8524 }
8525
8526 em->start = cur_offset;
8527 em->orig_start = cur_offset;
8528 em->len = ins.offset;
8529 em->block_start = ins.objectid;
8530 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008531 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008532 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008533 em->bdev = root->fs_info->fs_devices->latest_bdev;
8534 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8535 em->generation = trans->transid;
8536
8537 while (1) {
8538 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008539 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008540 write_unlock(&em_tree->lock);
8541 if (ret != -EEXIST)
8542 break;
8543 btrfs_drop_extent_cache(inode, cur_offset,
8544 cur_offset + ins.offset - 1,
8545 0);
8546 }
8547 free_extent_map(em);
8548next:
Yan Zhengd899e052008-10-30 14:25:28 -04008549 num_bytes -= ins.offset;
8550 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008551 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008552
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008553 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008554 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008555 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008556 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008557 (actual_len > inode->i_size) &&
8558 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008559 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008560 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008561 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008562 i_size = cur_offset;
8563 i_size_write(inode, i_size);
8564 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008565 }
8566
Yan Zhengd899e052008-10-30 14:25:28 -04008567 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008568
8569 if (ret) {
8570 btrfs_abort_transaction(trans, root, ret);
8571 if (own_trans)
8572 btrfs_end_transaction(trans, root);
8573 break;
8574 }
Yan Zhengd899e052008-10-30 14:25:28 -04008575
Josef Bacik0af3d002010-06-21 14:48:16 -04008576 if (own_trans)
8577 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008578 }
Yan Zhengd899e052008-10-30 14:25:28 -04008579 return ret;
8580}
8581
Josef Bacik0af3d002010-06-21 14:48:16 -04008582int btrfs_prealloc_file_range(struct inode *inode, int mode,
8583 u64 start, u64 num_bytes, u64 min_size,
8584 loff_t actual_len, u64 *alloc_hint)
8585{
8586 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8587 min_size, actual_len, alloc_hint,
8588 NULL);
8589}
8590
8591int btrfs_prealloc_file_range_trans(struct inode *inode,
8592 struct btrfs_trans_handle *trans, int mode,
8593 u64 start, u64 num_bytes, u64 min_size,
8594 loff_t actual_len, u64 *alloc_hint)
8595{
8596 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8597 min_size, actual_len, alloc_hint, trans);
8598}
8599
Chris Masone6dcd2d2008-07-17 12:53:50 -04008600static int btrfs_set_page_dirty(struct page *page)
8601{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008602 return __set_page_dirty_nobuffers(page);
8603}
8604
Al Viro10556cb2011-06-20 19:28:19 -04008605static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008606{
Li Zefanb83cc962010-12-20 16:04:08 +08008607 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008608 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008609
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008610 if (mask & MAY_WRITE &&
8611 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8612 if (btrfs_root_readonly(root))
8613 return -EROFS;
8614 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8615 return -EACCES;
8616 }
Al Viro2830ba72011-06-20 19:16:29 -04008617 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008618}
Chris Mason39279cc2007-06-12 06:35:45 -04008619
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008620static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008621 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008622 .lookup = btrfs_lookup,
8623 .create = btrfs_create,
8624 .unlink = btrfs_unlink,
8625 .link = btrfs_link,
8626 .mkdir = btrfs_mkdir,
8627 .rmdir = btrfs_rmdir,
8628 .rename = btrfs_rename,
8629 .symlink = btrfs_symlink,
8630 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008631 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008632 .setxattr = btrfs_setxattr,
8633 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008634 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008635 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008636 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008637 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008638};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008639static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008640 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008641 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008642 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008643};
Yan, Zheng76dda932009-09-21 16:00:26 -04008644
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008645static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008646 .llseek = generic_file_llseek,
8647 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008648 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008649 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008650#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008651 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008652#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008653 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008654 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008655};
8656
Chris Masond1310b22008-01-24 16:13:08 -05008657static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008658 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008659 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008660 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008661 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008662 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008663 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008664 .set_bit_hook = btrfs_set_bit_hook,
8665 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008666 .merge_extent_hook = btrfs_merge_extent_hook,
8667 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008668};
8669
Chris Mason35054392009-01-21 13:11:13 -05008670/*
8671 * btrfs doesn't support the bmap operation because swapfiles
8672 * use bmap to make a mapping of extents in the file. They assume
8673 * these extents won't change over the life of the file and they
8674 * use the bmap result to do IO directly to the drive.
8675 *
8676 * the btrfs bmap call would return logical addresses that aren't
8677 * suitable for IO and they also will change frequently as COW
8678 * operations happen. So, swapfile + btrfs == corruption.
8679 *
8680 * For now we're avoiding this by dropping bmap.
8681 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008682static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008683 .readpage = btrfs_readpage,
8684 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008685 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008686 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008687 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008688 .invalidatepage = btrfs_invalidatepage,
8689 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008690 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008691 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008692};
8693
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008694static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008695 .readpage = btrfs_readpage,
8696 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008697 .invalidatepage = btrfs_invalidatepage,
8698 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008699};
8700
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008701static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008702 .getattr = btrfs_getattr,
8703 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008704 .setxattr = btrfs_setxattr,
8705 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008706 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008707 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008708 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008709 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008710 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008711 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008712};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008713static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008714 .getattr = btrfs_getattr,
8715 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008716 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008717 .setxattr = btrfs_setxattr,
8718 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008719 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008720 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008721 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008722 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008723};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008724static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008725 .readlink = generic_readlink,
8726 .follow_link = page_follow_link_light,
8727 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008728 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008729 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008730 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008731 .setxattr = btrfs_setxattr,
8732 .getxattr = btrfs_getxattr,
8733 .listxattr = btrfs_listxattr,
8734 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008735 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008736 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008737};
Yan, Zheng76dda932009-09-21 16:00:26 -04008738
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008739const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008740 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008741 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008742};