blob: ec63d7af34601a9b92540fac02c2bc73f1779580 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040059
60struct btrfs_iget_args {
61 u64 ino;
62 struct btrfs_root *root;
63};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050073static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000076static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050080struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
82#define S_SHIFT 12
83static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
84 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
85 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
86 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
87 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
88 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
89 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
90 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
91};
92
Eric Sandeen3972f262013-01-12 02:57:22 +000093static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050094static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040095static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050096static noinline int cow_file_range(struct inode *inode,
97 struct page *locked_page,
98 u64 start, u64 end, int *page_started,
99 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400100static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
101 u64 len, u64 orig_start,
102 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400103 u64 orig_block_len, u64 ram_bytes,
104 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400105
Eric Sandeen48a3b632013-04-25 20:41:01 +0000106static int btrfs_dirty_inode(struct inode *inode);
107
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000108static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500109 struct inode *inode, struct inode *dir,
110 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500111{
112 int err;
113
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000114 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500115 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500116 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500117 return err;
118}
119
Chris Masond352ac62008-09-29 15:18:18 -0400120/*
Chris Masonc8b97812008-10-29 14:49:59 -0400121 * this does all the hard work for inserting an inline extent into
122 * the btree. The caller should have done a btrfs_drop_extents so that
123 * no overlapping inline items exist in the btree
124 */
Chris Masond3977122009-01-05 21:25:51 -0500125static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400126 struct btrfs_root *root, struct inode *inode,
127 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000128 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct page **compressed_pages)
130{
131 struct btrfs_key key;
132 struct btrfs_path *path;
133 struct extent_buffer *leaf;
134 struct page *page = NULL;
135 char *kaddr;
136 unsigned long ptr;
137 struct btrfs_file_extent_item *ei;
138 int err = 0;
139 int ret;
140 size_t cur_size = size;
141 size_t datasize;
142 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Li Zefanfe3f5662011-03-28 08:30:38 +0000144 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400145 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Chris Masond3977122009-01-05 21:25:51 -0500147 path = btrfs_alloc_path();
148 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400149 return -ENOMEM;
150
Chris Masonb9473432009-03-13 11:00:37 -0400151 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400152
Li Zefan33345d012011-04-20 10:31:50 +0800153 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400154 key.offset = start;
155 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 datasize = btrfs_file_extent_calc_inline_size(cur_size);
157
158 inode_add_bytes(inode, size);
159 ret = btrfs_insert_empty_item(trans, root, path, &key,
160 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400161 if (ret) {
162 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400163 goto fail;
164 }
165 leaf = path->nodes[0];
166 ei = btrfs_item_ptr(leaf, path->slots[0],
167 struct btrfs_file_extent_item);
168 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
169 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
170 btrfs_set_file_extent_encryption(leaf, ei, 0);
171 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
172 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
173 ptr = btrfs_file_extent_inline_start(ei);
174
Li Zefan261507a02010-12-17 14:21:50 +0800175 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400176 struct page *cpage;
177 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500178 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500180 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400181 PAGE_CACHE_SIZE);
182
Cong Wang7ac687d2011-11-25 23:14:28 +0800183 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400184 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400186
187 i++;
188 ptr += cur_size;
189 compressed_size -= cur_size;
190 }
191 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800192 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 } else {
194 page = find_get_page(inode->i_mapping,
195 start >> PAGE_CACHE_SHIFT);
196 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800197 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400198 offset = start & (PAGE_CACHE_SIZE - 1);
199 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 page_cache_release(page);
202 }
203 btrfs_mark_buffer_dirty(leaf);
204 btrfs_free_path(path);
205
Yan, Zhengc2167752009-11-12 09:34:21 +0000206 /*
207 * we're an inline extent, so nobody can
208 * extend the file past i_size without locking
209 * a page we already have locked.
210 *
211 * We must do any isize and inode updates
212 * before we unlock the pages. Otherwise we
213 * could end up racing with unlink.
214 */
Chris Masonc8b97812008-10-29 14:49:59 -0400215 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100216 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000217
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400219fail:
220 btrfs_free_path(path);
221 return err;
222}
223
224
225/*
226 * conditionally insert an inline extent into the file. This
227 * does the checks required to make sure the data is small enough
228 * to fit as an inline extent.
229 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400230static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400231 struct btrfs_root *root,
232 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000233 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400234 struct page **compressed_pages)
235{
236 u64 isize = i_size_read(inode);
237 u64 actual_end = min(end + 1, isize);
238 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000239 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 u64 data_len = inline_len;
241 int ret;
242
243 if (compressed_size)
244 data_len = compressed_size;
245
246 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400247 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400248 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
249 (!compressed_size &&
250 (actual_end & (root->sectorsize - 1)) == 0) ||
251 end + 1 < isize ||
252 data_len > root->fs_info->max_inline) {
253 return 1;
254 }
255
Josef Bacik26714852012-08-29 12:24:27 -0400256 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100257 if (ret)
258 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400259
260 if (isize > actual_end)
261 inline_len = min_t(u64, isize, actual_end);
262 ret = insert_inline_extent(trans, root, inode, start,
263 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000264 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 btrfs_abort_transaction(trans, root, ret);
267 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400268 } else if (ret == -ENOSPC) {
269 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100270 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400271
Josef Bacikbdc20e62013-02-28 13:23:38 -0500272 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400273 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400274 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 0;
276}
277
Chris Mason771ed682008-11-06 22:02:51 -0500278struct async_extent {
279 u64 start;
280 u64 ram_size;
281 u64 compressed_size;
282 struct page **pages;
283 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800284 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500285 struct list_head list;
286};
287
288struct async_cow {
289 struct inode *inode;
290 struct btrfs_root *root;
291 struct page *locked_page;
292 u64 start;
293 u64 end;
294 struct list_head extents;
295 struct btrfs_work work;
296};
297
298static noinline int add_async_extent(struct async_cow *cow,
299 u64 start, u64 ram_size,
300 u64 compressed_size,
301 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800302 unsigned long nr_pages,
303 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500304{
305 struct async_extent *async_extent;
306
307 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100308 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500309 async_extent->start = start;
310 async_extent->ram_size = ram_size;
311 async_extent->compressed_size = compressed_size;
312 async_extent->pages = pages;
313 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800314 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500315 list_add_tail(&async_extent->list, &cow->extents);
316 return 0;
317}
318
Chris Masonc8b97812008-10-29 14:49:59 -0400319/*
Chris Mason771ed682008-11-06 22:02:51 -0500320 * we create compressed extents in two phases. The first
321 * phase compresses a range of pages that have already been
322 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * This is done inside an ordered work queue, and the compression
325 * is spread across many cpus. The actual IO submission is step
326 * two, and the ordered work queue takes care of making sure that
327 * happens in the same order things were put onto the queue by
328 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400329 *
Chris Mason771ed682008-11-06 22:02:51 -0500330 * If this code finds it can't get good compression, it puts an
331 * entry onto the work queue to write the uncompressed bytes. This
332 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300333 * are written in the same order that the flusher thread sent them
334 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400335 */
Chris Mason771ed682008-11-06 22:02:51 -0500336static noinline int compress_file_range(struct inode *inode,
337 struct page *locked_page,
338 u64 start, u64 end,
339 struct async_cow *async_cow,
340 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400341{
342 struct btrfs_root *root = BTRFS_I(inode)->root;
343 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400344 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400345 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500347 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400348 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 struct page **pages = NULL;
350 unsigned long nr_pages;
351 unsigned long nr_pages_ret = 0;
352 unsigned long total_compressed = 0;
353 unsigned long total_in = 0;
354 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500355 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400356 int i;
357 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800358 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400359 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400360
Liu Bo4cb13e52012-03-29 09:57:45 -0400361 /* if this is a small write inside eof, kick off a defrag */
362 if ((end - start + 1) < 16 * 1024 &&
363 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400364 btrfs_add_inode_defrag(NULL, inode);
365
Chris Mason42dc7ba2008-12-15 11:44:56 -0500366 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400367again:
368 will_compress = 0;
369 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
370 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
371
Chris Masonf03d9302009-02-04 09:31:06 -0500372 /*
373 * we don't want to send crud past the end of i_size through
374 * compression, that's just a waste of CPU time. So, if the
375 * end of the file is before the start of our current
376 * requested range of bytes, we bail out to the uncompressed
377 * cleanup code that can deal with all of this.
378 *
379 * It isn't really the fastest way to fix things, but this is a
380 * very uncommon corner.
381 */
382 if (actual_end <= start)
383 goto cleanup_and_bail_uncompressed;
384
Chris Masonc8b97812008-10-29 14:49:59 -0400385 total_compressed = actual_end - start;
386
387 /* we want to make sure that amount of ram required to uncompress
388 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500389 * of a compressed extent to 128k. This is a crucial number
390 * because it also controls how easily we can spread reads across
391 * cpus for decompression.
392 *
393 * We also want to make sure the amount of IO required to do
394 * a random read is reasonably small, so we limit the size of
395 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400396 */
397 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000398 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500399 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400400 total_in = 0;
401 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400402
Chris Mason771ed682008-11-06 22:02:51 -0500403 /*
404 * we do compression for mount -o compress and when the
405 * inode has not been flagged as nocompress. This flag can
406 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400407 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200408 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500409 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000410 (BTRFS_I(inode)->force_compress) ||
411 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400412 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400413 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800414 if (!pages) {
415 /* just bail out to the uncompressed code */
416 goto cont;
417 }
Chris Mason179e29e2007-11-01 11:28:41 -0400418
Li Zefan261507a02010-12-17 14:21:50 +0800419 if (BTRFS_I(inode)->force_compress)
420 compress_type = BTRFS_I(inode)->force_compress;
421
Chris Mason4adaa612013-03-26 13:07:00 -0400422 /*
423 * we need to call clear_page_dirty_for_io on each
424 * page in the range. Otherwise applications with the file
425 * mmap'd can wander in and change the page contents while
426 * we are compressing them.
427 *
428 * If the compression fails for any reason, we set the pages
429 * dirty again later on.
430 */
431 extent_range_clear_dirty_for_io(inode, start, end);
432 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800433 ret = btrfs_compress_pages(compress_type,
434 inode->i_mapping, start,
435 total_compressed, pages,
436 nr_pages, &nr_pages_ret,
437 &total_in,
438 &total_compressed,
439 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400440
441 if (!ret) {
442 unsigned long offset = total_compressed &
443 (PAGE_CACHE_SIZE - 1);
444 struct page *page = pages[nr_pages_ret - 1];
445 char *kaddr;
446
447 /* zero the tail end of the last page, we might be
448 * sending it down to disk
449 */
450 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800451 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400452 memset(kaddr + offset, 0,
453 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800454 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400455 }
456 will_compress = 1;
457 }
458 }
Li Zefan560f7d72011-09-08 10:22:01 +0800459cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400460 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400461 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100462 if (IS_ERR(trans)) {
463 ret = PTR_ERR(trans);
464 trans = NULL;
465 goto cleanup_and_out;
466 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400467 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500468
Chris Masonc8b97812008-10-29 14:49:59 -0400469 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500470 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400471 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500472 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 */
474 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000475 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400476 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500477 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400478 ret = cow_file_range_inline(trans, root, inode,
479 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000480 total_compressed,
481 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100483 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500484 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100485 * inline extent creation worked or returned error,
486 * we don't need to create any more async work items.
487 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500488 */
Chris Masonc8b97812008-10-29 14:49:59 -0400489 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400490 &BTRFS_I(inode)->io_tree,
491 start, end, NULL,
492 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400493 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400494 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000495
496 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 goto free_pages_out;
498 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000499 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 }
501
502 if (will_compress) {
503 /*
504 * we aren't doing an inline extent round the compressed size
505 * up to a block size boundary so the allocator does sane
506 * things
507 */
Qu Wenruofda28322013-02-26 08:10:22 +0000508 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400509
510 /*
511 * one last check to make sure the compression is really a
512 * win, compare the page count read with the blocks on disk
513 */
Qu Wenruofda28322013-02-26 08:10:22 +0000514 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400515 if (total_compressed >= total_in) {
516 will_compress = 0;
517 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400518 num_bytes = total_in;
519 }
520 }
521 if (!will_compress && pages) {
522 /*
523 * the compression code ran but failed to make things smaller,
524 * free any pages it allocated and our page pointer array
525 */
526 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400527 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 page_cache_release(pages[i]);
529 }
530 kfree(pages);
531 pages = NULL;
532 total_compressed = 0;
533 nr_pages_ret = 0;
534
535 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500536 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
537 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500538 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500539 }
Chris Masonc8b97812008-10-29 14:49:59 -0400540 }
Chris Mason771ed682008-11-06 22:02:51 -0500541 if (will_compress) {
542 *num_added += 1;
543
544 /* the async work queues will take care of doing actual
545 * allocation on disk for these compressed pages,
546 * and will submit them to the elevator.
547 */
548 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800549 total_compressed, pages, nr_pages_ret,
550 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500551
Yan, Zheng24ae6362010-12-06 07:02:36 +0000552 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500553 start += num_bytes;
554 pages = NULL;
555 cond_resched();
556 goto again;
557 }
558 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500559cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500560 /*
561 * No compression, but we still need to write the pages in
562 * the file we've been given so far. redirty the locked
563 * page if it corresponds to our extent and set things up
564 * for the async work queue to run cow_file_range to do
565 * the normal delalloc dance
566 */
567 if (page_offset(locked_page) >= start &&
568 page_offset(locked_page) <= end) {
569 __set_page_dirty_nobuffers(locked_page);
570 /* unlocked later on in the async handlers */
571 }
Chris Mason4adaa612013-03-26 13:07:00 -0400572 if (redirty)
573 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800574 add_async_extent(async_cow, start, end - start + 1,
575 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500576 *num_added += 1;
577 }
578
579out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100580 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500581
582free_pages_out:
583 for (i = 0; i < nr_pages_ret; i++) {
584 WARN_ON(pages[i]->mapping);
585 page_cache_release(pages[i]);
586 }
Chris Masond3977122009-01-05 21:25:51 -0500587 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500588
589 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590
591cleanup_and_out:
592 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
593 start, end, NULL,
594 EXTENT_CLEAR_UNLOCK_PAGE |
595 EXTENT_CLEAR_DIRTY |
596 EXTENT_CLEAR_DELALLOC |
597 EXTENT_SET_WRITEBACK |
598 EXTENT_END_WRITEBACK);
599 if (!trans || IS_ERR(trans))
600 btrfs_error(root->fs_info, ret, "Failed to join transaction");
601 else
602 btrfs_abort_transaction(trans, root, ret);
603 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500604}
605
606/*
607 * phase two of compressed writeback. This is the ordered portion
608 * of the code, which only gets called in the order the work was
609 * queued. We walk all the async extents created by compress_file_range
610 * and send them down to the disk.
611 */
612static noinline int submit_compressed_extents(struct inode *inode,
613 struct async_cow *async_cow)
614{
615 struct async_extent *async_extent;
616 u64 alloc_hint = 0;
617 struct btrfs_trans_handle *trans;
618 struct btrfs_key ins;
619 struct extent_map *em;
620 struct btrfs_root *root = BTRFS_I(inode)->root;
621 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
622 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500623 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500624
625 if (list_empty(&async_cow->extents))
626 return 0;
627
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500628again:
Chris Masond3977122009-01-05 21:25:51 -0500629 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500630 async_extent = list_entry(async_cow->extents.next,
631 struct async_extent, list);
632 list_del(&async_extent->list);
633
634 io_tree = &BTRFS_I(inode)->io_tree;
635
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500636retry:
Chris Mason771ed682008-11-06 22:02:51 -0500637 /* did the compression code fall back to uncompressed IO? */
638 if (!async_extent->pages) {
639 int page_started = 0;
640 unsigned long nr_written = 0;
641
642 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000643 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100644 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500647 ret = cow_file_range(inode, async_cow->locked_page,
648 async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1,
651 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500652
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100653 /* JDM XXX */
654
Chris Mason771ed682008-11-06 22:02:51 -0500655 /*
656 * if page_started, cow_file_range inserted an
657 * inline extent and took care of all the unlocking
658 * and IO for us. Otherwise, we need to submit
659 * all those pages down to the drive.
660 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500661 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500662 extent_write_locked_range(io_tree,
663 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500664 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->ram_size - 1,
666 btrfs_get_extent,
667 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500668 else if (ret)
669 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500670 kfree(async_extent);
671 cond_resched();
672 continue;
673 }
674
675 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100676 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500677
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400678 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 if (IS_ERR(trans)) {
680 ret = PTR_ERR(trans);
681 } else {
682 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
683 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500684 async_extent->compressed_size,
685 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500686 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600687 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100688 btrfs_abort_transaction(trans, root, ret);
689 btrfs_end_transaction(trans, root);
690 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 if (ret) {
693 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 for (i = 0; i < async_extent->nr_pages; i++) {
696 WARN_ON(async_extent->pages[i]->mapping);
697 page_cache_release(async_extent->pages[i]);
698 }
699 kfree(async_extent->pages);
700 async_extent->nr_pages = 0;
701 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100703 if (ret == -ENOSPC)
704 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500705 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500706 }
707
Yan, Zhengc2167752009-11-12 09:34:21 +0000708 /*
709 * here we're doing allocation and writeback of the
710 * compressed pages
711 */
712 btrfs_drop_extent_cache(inode, async_extent->start,
713 async_extent->start +
714 async_extent->ram_size - 1, 0);
715
David Sterba172ddd62011-04-21 00:48:27 +0200716 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500717 if (!em)
718 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500719 em->start = async_extent->start;
720 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500721 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400722 em->mod_start = em->start;
723 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500724
725 em->block_start = ins.objectid;
726 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500727 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400728 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500729 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800730 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500731 set_bit(EXTENT_FLAG_PINNED, &em->flags);
732 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400733 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500734
Chris Masond3977122009-01-05 21:25:51 -0500735 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400736 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400737 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400738 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500739 if (ret != -EEXIST) {
740 free_extent_map(em);
741 break;
742 }
743 btrfs_drop_extent_cache(inode, async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1, 0);
746 }
747
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500748 if (ret)
749 goto out_free_reserve;
750
Li Zefan261507a02010-12-17 14:21:50 +0800751 ret = btrfs_add_ordered_extent_compress(inode,
752 async_extent->start,
753 ins.objectid,
754 async_extent->ram_size,
755 ins.offset,
756 BTRFS_ORDERED_COMPRESSED,
757 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500758 if (ret)
759 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500760
Chris Mason771ed682008-11-06 22:02:51 -0500761 /*
762 * clear dirty, set writeback and unlock the pages.
763 */
764 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400765 &BTRFS_I(inode)->io_tree,
766 async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1,
769 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
770 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400771 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400772 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500773
774 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500775 async_extent->start,
776 async_extent->ram_size,
777 ins.objectid,
778 ins.offset, async_extent->pages,
779 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500780 alloc_hint = ins.objectid + ins.offset;
781 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500782 if (ret)
783 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500784 cond_resched();
785 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100786 ret = 0;
787out:
788 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789out_free_reserve:
790 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100791out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1,
796 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
797 EXTENT_CLEAR_UNLOCK |
798 EXTENT_CLEAR_DELALLOC |
799 EXTENT_CLEAR_DIRTY |
800 EXTENT_SET_WRITEBACK |
801 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100802 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500803 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500804}
805
Josef Bacik4b46fce2010-05-23 11:00:55 -0400806static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
807 u64 num_bytes)
808{
809 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
810 struct extent_map *em;
811 u64 alloc_hint = 0;
812
813 read_lock(&em_tree->lock);
814 em = search_extent_mapping(em_tree, start, num_bytes);
815 if (em) {
816 /*
817 * if block start isn't an actual block number then find the
818 * first block in this inode and use that as a hint. If that
819 * block is also bogus then just don't worry about it.
820 */
821 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
822 free_extent_map(em);
823 em = search_extent_mapping(em_tree, 0, 0);
824 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
825 alloc_hint = em->block_start;
826 if (em)
827 free_extent_map(em);
828 } else {
829 alloc_hint = em->block_start;
830 free_extent_map(em);
831 }
832 }
833 read_unlock(&em_tree->lock);
834
835 return alloc_hint;
836}
837
Chris Mason771ed682008-11-06 22:02:51 -0500838/*
839 * when extent_io.c finds a delayed allocation range in the file,
840 * the call backs end up in this code. The basic idea is to
841 * allocate extents on disk for the range, and create ordered data structs
842 * in ram to track those extents.
843 *
844 * locked_page is the page that writepage had locked already. We use
845 * it to make sure we don't do extra locks or unlocks.
846 *
847 * *page_started is set to one if we unlock locked_page and do everything
848 * required to start IO on it. It may be clean and already done with
849 * IO when we return.
850 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000851static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
852 struct inode *inode,
853 struct btrfs_root *root,
854 struct page *locked_page,
855 u64 start, u64 end, int *page_started,
856 unsigned long *nr_written,
857 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500858{
Chris Mason771ed682008-11-06 22:02:51 -0500859 u64 alloc_hint = 0;
860 u64 num_bytes;
861 unsigned long ram_size;
862 u64 disk_num_bytes;
863 u64 cur_alloc_size;
864 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500865 struct btrfs_key ins;
866 struct extent_map *em;
867 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
868 int ret = 0;
869
Liu Bo83eea1f2012-07-10 05:28:39 -0600870 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500871
Qu Wenruofda28322013-02-26 08:10:22 +0000872 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500873 num_bytes = max(blocksize, num_bytes);
874 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500875
Chris Mason4cb53002011-05-24 15:35:30 -0400876 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400877 if (num_bytes < 64 * 1024 &&
878 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400879 btrfs_add_inode_defrag(trans, inode);
880
Chris Mason771ed682008-11-06 22:02:51 -0500881 if (start == 0) {
882 /* lets try to make an inline extent */
883 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000884 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500885 if (ret == 0) {
886 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400887 &BTRFS_I(inode)->io_tree,
888 start, end, NULL,
889 EXTENT_CLEAR_UNLOCK_PAGE |
890 EXTENT_CLEAR_UNLOCK |
891 EXTENT_CLEAR_DELALLOC |
892 EXTENT_CLEAR_DIRTY |
893 EXTENT_SET_WRITEBACK |
894 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000895
Chris Mason771ed682008-11-06 22:02:51 -0500896 *nr_written = *nr_written +
897 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
898 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500899 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 } else if (ret < 0) {
901 btrfs_abort_transaction(trans, root, ret);
902 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500903 }
904 }
Chris Masonc8b97812008-10-29 14:49:59 -0400905
906 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200907 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400908
Josef Bacik4b46fce2010-05-23 11:00:55 -0400909 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400910 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400911
Chris Masond3977122009-01-05 21:25:51 -0500912 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400913 unsigned long op;
914
Josef Bacik287a0ab2010-03-19 18:07:23 +0000915 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400916 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500917 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500918 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 if (ret < 0) {
920 btrfs_abort_transaction(trans, root, ret);
921 goto out_unlock;
922 }
Chris Masond3977122009-01-05 21:25:51 -0500923
David Sterba172ddd62011-04-21 00:48:27 +0200924 em = alloc_extent_map();
Liu Boace68ba2013-04-22 10:53:47 +0000925 if (!em)
926 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500928 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500929 ram_size = ins.offset;
930 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400931 em->mod_start = em->start;
932 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400933
Chris Masone6dcd2d2008-07-17 12:53:50 -0400934 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400935 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500936 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400937 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400939 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400940 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400941
Chris Masond3977122009-01-05 21:25:51 -0500942 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400943 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400944 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400945 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400946 if (ret != -EEXIST) {
947 free_extent_map(em);
948 break;
949 }
950 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400951 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 }
Liu Boace68ba2013-04-22 10:53:47 +0000953 if (ret)
954 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400955
Chris Mason98d20f62008-04-14 09:46:10 -0400956 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400957 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500958 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000959 if (ret)
960 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400961
Yan Zheng17d217f2008-12-12 10:03:38 -0500962 if (root->root_key.objectid ==
963 BTRFS_DATA_RELOC_TREE_OBJECTID) {
964 ret = btrfs_reloc_clone_csums(inode, start,
965 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 if (ret) {
967 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000968 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100969 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 }
971
Chris Masond3977122009-01-05 21:25:51 -0500972 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400973 break;
Chris Masond3977122009-01-05 21:25:51 -0500974
Chris Masonc8b97812008-10-29 14:49:59 -0400975 /* we're not doing compressed IO, don't unlock the first
976 * page (which the caller expects to stay locked), don't
977 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400978 *
979 * Do set the Private2 bit so we know this page was properly
980 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400981 */
Chris Masona791e352009-10-08 11:27:10 -0400982 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
983 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
984 EXTENT_SET_PRIVATE2;
985
Chris Masonc8b97812008-10-29 14:49:59 -0400986 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
987 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400988 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400989 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500990 num_bytes -= cur_alloc_size;
991 alloc_hint = ins.objectid + ins.offset;
992 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400993 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500995 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000996
Liu Boace68ba2013-04-22 10:53:47 +0000997out_reserve:
998 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999out_unlock:
1000 extent_clear_unlock_delalloc(inode,
1001 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001002 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003 EXTENT_CLEAR_UNLOCK_PAGE |
1004 EXTENT_CLEAR_UNLOCK |
1005 EXTENT_CLEAR_DELALLOC |
1006 EXTENT_CLEAR_DIRTY |
1007 EXTENT_SET_WRITEBACK |
1008 EXTENT_END_WRITEBACK);
1009
1010 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001011}
Chris Masonc8b97812008-10-29 14:49:59 -04001012
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001013static noinline int cow_file_range(struct inode *inode,
1014 struct page *locked_page,
1015 u64 start, u64 end, int *page_started,
1016 unsigned long *nr_written,
1017 int unlock)
1018{
1019 struct btrfs_trans_handle *trans;
1020 struct btrfs_root *root = BTRFS_I(inode)->root;
1021 int ret;
1022
1023 trans = btrfs_join_transaction(root);
1024 if (IS_ERR(trans)) {
1025 extent_clear_unlock_delalloc(inode,
1026 &BTRFS_I(inode)->io_tree,
1027 start, end, locked_page,
1028 EXTENT_CLEAR_UNLOCK_PAGE |
1029 EXTENT_CLEAR_UNLOCK |
1030 EXTENT_CLEAR_DELALLOC |
1031 EXTENT_CLEAR_DIRTY |
1032 EXTENT_SET_WRITEBACK |
1033 EXTENT_END_WRITEBACK);
1034 return PTR_ERR(trans);
1035 }
1036 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1037
1038 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1039 page_started, nr_written, unlock);
1040
1041 btrfs_end_transaction(trans, root);
1042
1043 return ret;
1044}
1045
Chris Mason771ed682008-11-06 22:02:51 -05001046/*
1047 * work queue call back to started compression on a file and pages
1048 */
1049static noinline void async_cow_start(struct btrfs_work *work)
1050{
1051 struct async_cow *async_cow;
1052 int num_added = 0;
1053 async_cow = container_of(work, struct async_cow, work);
1054
1055 compress_file_range(async_cow->inode, async_cow->locked_page,
1056 async_cow->start, async_cow->end, async_cow,
1057 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001058 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001059 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001060 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001061 }
Chris Mason771ed682008-11-06 22:02:51 -05001062}
1063
1064/*
1065 * work queue call back to submit previously compressed pages
1066 */
1067static noinline void async_cow_submit(struct btrfs_work *work)
1068{
1069 struct async_cow *async_cow;
1070 struct btrfs_root *root;
1071 unsigned long nr_pages;
1072
1073 async_cow = container_of(work, struct async_cow, work);
1074
1075 root = async_cow->root;
1076 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1077 PAGE_CACHE_SHIFT;
1078
Josef Bacik66657b32012-08-01 15:36:24 -04001079 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001080 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001081 waitqueue_active(&root->fs_info->async_submit_wait))
1082 wake_up(&root->fs_info->async_submit_wait);
1083
Chris Masond3977122009-01-05 21:25:51 -05001084 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001085 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001086}
Chris Masonc8b97812008-10-29 14:49:59 -04001087
Chris Mason771ed682008-11-06 22:02:51 -05001088static noinline void async_cow_free(struct btrfs_work *work)
1089{
1090 struct async_cow *async_cow;
1091 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001092 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001093 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001094 kfree(async_cow);
1095}
1096
1097static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1098 u64 start, u64 end, int *page_started,
1099 unsigned long *nr_written)
1100{
1101 struct async_cow *async_cow;
1102 struct btrfs_root *root = BTRFS_I(inode)->root;
1103 unsigned long nr_pages;
1104 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001105 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001106
Chris Masona3429ab2009-10-08 12:30:20 -04001107 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1108 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001109 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001110 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001111 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001112 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001113 async_cow->root = root;
1114 async_cow->locked_page = locked_page;
1115 async_cow->start = start;
1116
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001117 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001118 cur_end = end;
1119 else
1120 cur_end = min(end, start + 512 * 1024 - 1);
1121
1122 async_cow->end = cur_end;
1123 INIT_LIST_HEAD(&async_cow->extents);
1124
1125 async_cow->work.func = async_cow_start;
1126 async_cow->work.ordered_func = async_cow_submit;
1127 async_cow->work.ordered_free = async_cow_free;
1128 async_cow->work.flags = 0;
1129
Chris Mason771ed682008-11-06 22:02:51 -05001130 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1131 PAGE_CACHE_SHIFT;
1132 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1133
1134 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1135 &async_cow->work);
1136
1137 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1138 wait_event(root->fs_info->async_submit_wait,
1139 (atomic_read(&root->fs_info->async_delalloc_pages) <
1140 limit));
1141 }
1142
Chris Masond3977122009-01-05 21:25:51 -05001143 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001144 atomic_read(&root->fs_info->async_delalloc_pages)) {
1145 wait_event(root->fs_info->async_submit_wait,
1146 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1147 0));
1148 }
1149
1150 *nr_written += nr_pages;
1151 start = cur_end + 1;
1152 }
1153 *page_started = 1;
1154 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001155}
1156
Chris Masond3977122009-01-05 21:25:51 -05001157static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001158 u64 bytenr, u64 num_bytes)
1159{
1160 int ret;
1161 struct btrfs_ordered_sum *sums;
1162 LIST_HEAD(list);
1163
Yan Zheng07d400a2009-01-06 11:42:00 -05001164 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001165 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001166 if (ret == 0 && list_empty(&list))
1167 return 0;
1168
1169 while (!list_empty(&list)) {
1170 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1171 list_del(&sums->list);
1172 kfree(sums);
1173 }
1174 return 1;
1175}
1176
Chris Masond352ac62008-09-29 15:18:18 -04001177/*
1178 * when nowcow writeback call back. This checks for snapshots or COW copies
1179 * of the extents that exist in the file, and COWs the file as required.
1180 *
1181 * If no cow copies or snapshots exist, we write directly to the existing
1182 * blocks on disk
1183 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001184static noinline int run_delalloc_nocow(struct inode *inode,
1185 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001186 u64 start, u64 end, int *page_started, int force,
1187 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001188{
Chris Masonbe20aa92007-12-17 20:14:01 -05001189 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001190 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001191 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001192 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001193 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001194 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001195 u64 cow_start;
1196 u64 cur_offset;
1197 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001198 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001199 u64 disk_bytenr;
1200 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001201 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001202 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001204 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001205 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 int nocow;
1207 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001208 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001209 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001210
1211 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001212 if (!path) {
1213 extent_clear_unlock_delalloc(inode,
1214 &BTRFS_I(inode)->io_tree,
1215 start, end, locked_page,
1216 EXTENT_CLEAR_UNLOCK_PAGE |
1217 EXTENT_CLEAR_UNLOCK |
1218 EXTENT_CLEAR_DELALLOC |
1219 EXTENT_CLEAR_DIRTY |
1220 EXTENT_SET_WRITEBACK |
1221 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001222 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001223 }
Li Zefan82d59022011-04-20 10:33:24 +08001224
Liu Bo83eea1f2012-07-10 05:28:39 -06001225 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001226
1227 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001228 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001229 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001230 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001231
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001232 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001233 extent_clear_unlock_delalloc(inode,
1234 &BTRFS_I(inode)->io_tree,
1235 start, end, locked_page,
1236 EXTENT_CLEAR_UNLOCK_PAGE |
1237 EXTENT_CLEAR_UNLOCK |
1238 EXTENT_CLEAR_DELALLOC |
1239 EXTENT_CLEAR_DIRTY |
1240 EXTENT_SET_WRITEBACK |
1241 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 btrfs_free_path(path);
1243 return PTR_ERR(trans);
1244 }
1245
Josef Bacik74b21072011-04-13 12:02:53 -04001246 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001247
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 cow_start = (u64)-1;
1249 cur_offset = start;
1250 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001251 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001253 if (ret < 0) {
1254 btrfs_abort_transaction(trans, root, ret);
1255 goto error;
1256 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1258 leaf = path->nodes[0];
1259 btrfs_item_key_to_cpu(leaf, &found_key,
1260 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001261 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 found_key.type == BTRFS_EXTENT_DATA_KEY)
1263 path->slots[0]--;
1264 }
1265 check_prev = 0;
1266next_slot:
1267 leaf = path->nodes[0];
1268 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1269 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001270 if (ret < 0) {
1271 btrfs_abort_transaction(trans, root, ret);
1272 goto error;
1273 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 if (ret > 0)
1275 break;
1276 leaf = path->nodes[0];
1277 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001278
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 nocow = 0;
1280 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001281 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001283
Li Zefan33345d012011-04-20 10:31:50 +08001284 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1286 found_key.offset > end)
1287 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 if (found_key.offset > cur_offset) {
1290 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001291 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 goto out_check;
1293 }
Chris Masonc31f8832008-01-08 15:46:31 -05001294
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 fi = btrfs_item_ptr(leaf, path->slots[0],
1296 struct btrfs_file_extent_item);
1297 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001298
Josef Bacikcc95bef2013-04-04 14:31:27 -04001299 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001300 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1301 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001303 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 extent_end = found_key.offset +
1305 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001306 disk_num_bytes =
1307 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 if (extent_end <= start) {
1309 path->slots[0]++;
1310 goto next_slot;
1311 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001312 if (disk_bytenr == 0)
1313 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 if (btrfs_file_extent_compression(leaf, fi) ||
1315 btrfs_file_extent_encryption(leaf, fi) ||
1316 btrfs_file_extent_other_encoding(leaf, fi))
1317 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001318 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1319 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001320 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001322 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001323 found_key.offset -
1324 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001325 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001326 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001327 disk_bytenr += cur_offset - found_key.offset;
1328 num_bytes = min(end + 1, extent_end) - cur_offset;
1329 /*
1330 * force cow if csum exists in the range.
1331 * this ensure that csum for a given extent are
1332 * either valid or do not exist.
1333 */
1334 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1335 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 nocow = 1;
1337 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1338 extent_end = found_key.offset +
1339 btrfs_file_extent_inline_len(leaf, fi);
1340 extent_end = ALIGN(extent_end, root->sectorsize);
1341 } else {
1342 BUG_ON(1);
1343 }
1344out_check:
1345 if (extent_end <= start) {
1346 path->slots[0]++;
1347 goto next_slot;
1348 }
1349 if (!nocow) {
1350 if (cow_start == (u64)-1)
1351 cow_start = cur_offset;
1352 cur_offset = extent_end;
1353 if (cur_offset > end)
1354 break;
1355 path->slots[0]++;
1356 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001357 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001358
David Sterbab3b4aa72011-04-21 01:20:15 +02001359 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001361 ret = __cow_file_range(trans, inode, root, locked_page,
1362 cow_start, found_key.offset - 1,
1363 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001364 if (ret) {
1365 btrfs_abort_transaction(trans, root, ret);
1366 goto error;
1367 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001369 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001370
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1372 struct extent_map *em;
1373 struct extent_map_tree *em_tree;
1374 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001375 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001376 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001377 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001378 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001379 em->len = num_bytes;
1380 em->block_len = num_bytes;
1381 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001382 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001383 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001384 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001385 em->mod_start = em->start;
1386 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001387 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001388 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001389 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001390 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001391 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001392 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001393 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001394 if (ret != -EEXIST) {
1395 free_extent_map(em);
1396 break;
1397 }
1398 btrfs_drop_extent_cache(inode, em->start,
1399 em->start + em->len - 1, 0);
1400 }
1401 type = BTRFS_ORDERED_PREALLOC;
1402 } else {
1403 type = BTRFS_ORDERED_NOCOW;
1404 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001405
1406 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001407 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001408 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001409
Yan, Zhengefa56462010-05-16 10:49:59 -04001410 if (root->root_key.objectid ==
1411 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1412 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1413 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001414 if (ret) {
1415 btrfs_abort_transaction(trans, root, ret);
1416 goto error;
1417 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001418 }
1419
Yan Zhengd899e052008-10-30 14:25:28 -04001420 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001421 cur_offset, cur_offset + num_bytes - 1,
1422 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1423 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1424 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 cur_offset = extent_end;
1426 if (cur_offset > end)
1427 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001428 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001429 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001430
Josef Bacik17ca04a2012-05-31 15:58:55 -04001431 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001433 cur_offset = end;
1434 }
1435
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001437 ret = __cow_file_range(trans, inode, root, locked_page,
1438 cow_start, end,
1439 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001440 if (ret) {
1441 btrfs_abort_transaction(trans, root, ret);
1442 goto error;
1443 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 }
1445
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446error:
Miao Xiea698d0752012-09-20 01:51:59 -06001447 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 if (!ret)
1449 ret = err;
1450
Josef Bacik17ca04a2012-05-31 15:58:55 -04001451 if (ret && cur_offset < end)
1452 extent_clear_unlock_delalloc(inode,
1453 &BTRFS_I(inode)->io_tree,
1454 cur_offset, end, locked_page,
1455 EXTENT_CLEAR_UNLOCK_PAGE |
1456 EXTENT_CLEAR_UNLOCK |
1457 EXTENT_CLEAR_DELALLOC |
1458 EXTENT_CLEAR_DIRTY |
1459 EXTENT_SET_WRITEBACK |
1460 EXTENT_END_WRITEBACK);
1461
Yan Zheng7ea394f2008-08-05 13:05:02 -04001462 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001463 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001464}
1465
Chris Masond352ac62008-09-29 15:18:18 -04001466/*
1467 * extent_io.c call back to do delayed allocation processing
1468 */
Chris Masonc8b97812008-10-29 14:49:59 -04001469static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001470 u64 start, u64 end, int *page_started,
1471 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001472{
Chris Masonbe20aa92007-12-17 20:14:01 -05001473 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001474 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001475
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001476 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001477 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001478 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001479 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001480 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001481 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001482 } else if (!btrfs_test_opt(root, COMPRESS) &&
1483 !(BTRFS_I(inode)->force_compress) &&
1484 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001485 ret = cow_file_range(inode, locked_page, start, end,
1486 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001487 } else {
1488 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1489 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001490 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001491 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001492 }
Chris Masonb888db22007-08-27 16:49:44 -04001493 return ret;
1494}
1495
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001496static void btrfs_split_extent_hook(struct inode *inode,
1497 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001499 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001500 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001501 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502
Josef Bacik9e0baf62011-07-15 15:16:44 +00001503 spin_lock(&BTRFS_I(inode)->lock);
1504 BTRFS_I(inode)->outstanding_extents++;
1505 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001506}
1507
1508/*
1509 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1510 * extents so we can keep track of new extents that are just merged onto old
1511 * extents, such as when we are doing sequential writes, so we can properly
1512 * account for the metadata space we'll need.
1513 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001514static void btrfs_merge_extent_hook(struct inode *inode,
1515 struct extent_state *new,
1516 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001518 /* not delalloc, ignore it */
1519 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001520 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521
Josef Bacik9e0baf62011-07-15 15:16:44 +00001522 spin_lock(&BTRFS_I(inode)->lock);
1523 BTRFS_I(inode)->outstanding_extents--;
1524 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001525}
1526
Chris Masond352ac62008-09-29 15:18:18 -04001527/*
1528 * extent_io.c set_bit_hook, used to track delayed allocation
1529 * bytes in this file, and to maintain the list of inodes that
1530 * have pending delalloc work to be done.
1531 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001532static void btrfs_set_bit_hook(struct inode *inode,
1533 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001534{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001535
Chris Mason75eff682008-12-15 15:54:40 -05001536 /*
1537 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001538 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001539 * bit, which is only set or cleared with irqs on
1540 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001541 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001542 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001544 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001545
Josef Bacik9e0baf62011-07-15 15:16:44 +00001546 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001547 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001548 } else {
1549 spin_lock(&BTRFS_I(inode)->lock);
1550 BTRFS_I(inode)->outstanding_extents++;
1551 spin_unlock(&BTRFS_I(inode)->lock);
1552 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001553
Miao Xie963d6782013-01-29 10:10:51 +00001554 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1555 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001556 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001558 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1559 &BTRFS_I(inode)->runtime_flags)) {
1560 spin_lock(&root->fs_info->delalloc_lock);
1561 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1562 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1563 &root->fs_info->delalloc_inodes);
1564 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1565 &BTRFS_I(inode)->runtime_flags);
1566 }
1567 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001568 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001569 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001570 }
Chris Mason291d6732008-01-29 15:55:23 -05001571}
1572
Chris Masond352ac62008-09-29 15:18:18 -04001573/*
1574 * extent_io.c clear_bit_hook, see set_bit_hook for why
1575 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001576static void btrfs_clear_bit_hook(struct inode *inode,
1577 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001578{
Chris Mason75eff682008-12-15 15:54:40 -05001579 /*
1580 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001581 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001582 * bit, which is only set or cleared with irqs on
1583 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001584 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001585 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001586 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001587 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001588
Josef Bacik9e0baf62011-07-15 15:16:44 +00001589 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001590 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001591 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1592 spin_lock(&BTRFS_I(inode)->lock);
1593 BTRFS_I(inode)->outstanding_extents--;
1594 spin_unlock(&BTRFS_I(inode)->lock);
1595 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001596
1597 if (*bits & EXTENT_DO_ACCOUNTING)
1598 btrfs_delalloc_release_metadata(inode, len);
1599
Josef Bacik0cb59c92010-07-02 12:14:14 -04001600 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1601 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603
Miao Xie963d6782013-01-29 10:10:51 +00001604 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1605 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001606 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001607 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001608 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001609 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1610 &BTRFS_I(inode)->runtime_flags)) {
1611 spin_lock(&root->fs_info->delalloc_lock);
1612 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1613 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1614 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1615 &BTRFS_I(inode)->runtime_flags);
1616 }
1617 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001618 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001619 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001620 }
Chris Mason291d6732008-01-29 15:55:23 -05001621}
1622
Chris Masond352ac62008-09-29 15:18:18 -04001623/*
1624 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1625 * we don't create bios that span stripes or chunks
1626 */
David Woodhouse64a16702009-07-15 23:29:37 +01001627int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001628 size_t size, struct bio *bio,
1629 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001630{
1631 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001632 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001633 u64 length = 0;
1634 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001635 int ret;
1636
Chris Mason771ed682008-11-06 22:02:51 -05001637 if (bio_flags & EXTENT_BIO_COMPRESSED)
1638 return 0;
1639
Chris Masonf2d8d742008-04-21 10:03:05 -04001640 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001641 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001642 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001643 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001644 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001645 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001646 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001647 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001648 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001649}
1650
Chris Masond352ac62008-09-29 15:18:18 -04001651/*
1652 * in order to insert checksums into the metadata in large chunks,
1653 * we wait until bio submission time. All the pages in the bio are
1654 * checksummed and sums are attached onto the ordered extent record.
1655 *
1656 * At IO completion time the cums attached on the ordered extent record
1657 * are inserted into the btree
1658 */
Chris Masond3977122009-01-05 21:25:51 -05001659static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1660 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001661 unsigned long bio_flags,
1662 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001663{
Chris Mason065631f2008-02-20 12:07:25 -05001664 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001665 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001666
Chris Masond20f7042008-12-08 16:58:54 -05001667 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001668 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001669 return 0;
1670}
Chris Masone0156402008-04-16 11:15:20 -04001671
Chris Mason4a69a412008-11-06 22:03:00 -05001672/*
1673 * in order to insert checksums into the metadata in large chunks,
1674 * we wait until bio submission time. All the pages in the bio are
1675 * checksummed and sums are attached onto the ordered extent record.
1676 *
1677 * At IO completion time the cums attached on the ordered extent record
1678 * are inserted into the btree
1679 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001680static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001681 int mirror_num, unsigned long bio_flags,
1682 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001683{
1684 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001685 int ret;
1686
1687 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1688 if (ret)
1689 bio_endio(bio, ret);
1690 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001691}
1692
Chris Masond352ac62008-09-29 15:18:18 -04001693/*
Chris Masoncad321a2008-12-17 14:51:42 -05001694 * extent_io.c submission hook. This does the right thing for csum calculation
1695 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001696 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001697static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001698 int mirror_num, unsigned long bio_flags,
1699 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001700{
1701 struct btrfs_root *root = BTRFS_I(inode)->root;
1702 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001703 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001704 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001705 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001706
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001707 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001708
Liu Bo83eea1f2012-07-10 05:28:39 -06001709 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001710 metadata = 2;
1711
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001712 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001713 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1714 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001715 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001716
Chris Masond20f7042008-12-08 16:58:54 -05001717 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001718 ret = btrfs_submit_compressed_read(inode, bio,
1719 mirror_num,
1720 bio_flags);
1721 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001722 } else if (!skip_sum) {
1723 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1724 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001725 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001726 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001727 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001728 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001729 /* csum items have already been cloned */
1730 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1731 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001732 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001733 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001734 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001735 bio_flags, bio_offset,
1736 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001737 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001738 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001739 } else if (!skip_sum) {
1740 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1741 if (ret)
1742 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001743 }
1744
Chris Mason0b86a832008-03-24 15:01:56 -04001745mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001746 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1747
1748out:
1749 if (ret < 0)
1750 bio_endio(bio, ret);
1751 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001752}
Chris Mason6885f302008-02-20 16:11:05 -05001753
Chris Masond352ac62008-09-29 15:18:18 -04001754/*
1755 * given a list of ordered sums record them in the inode. This happens
1756 * at IO completion time based on sums calculated at bio submission time.
1757 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001758static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001759 struct inode *inode, u64 file_offset,
1760 struct list_head *list)
1761{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001762 struct btrfs_ordered_sum *sum;
1763
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001764 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001765 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001766 btrfs_csum_file_blocks(trans,
1767 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001768 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001769 }
1770 return 0;
1771}
1772
Josef Bacik2ac55d42010-02-03 19:33:23 +00001773int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1774 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001775{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001776 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001777 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001778 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001779}
1780
Chris Masond352ac62008-09-29 15:18:18 -04001781/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001782struct btrfs_writepage_fixup {
1783 struct page *page;
1784 struct btrfs_work work;
1785};
1786
Christoph Hellwigb2950862008-12-02 09:54:17 -05001787static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001788{
1789 struct btrfs_writepage_fixup *fixup;
1790 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001791 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001792 struct page *page;
1793 struct inode *inode;
1794 u64 page_start;
1795 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001796 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001797
1798 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1799 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001800again:
Chris Mason247e7432008-07-17 12:53:51 -04001801 lock_page(page);
1802 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1803 ClearPageChecked(page);
1804 goto out_page;
1805 }
1806
1807 inode = page->mapping->host;
1808 page_start = page_offset(page);
1809 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1810
Josef Bacik2ac55d42010-02-03 19:33:23 +00001811 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001812 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001813
1814 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001815 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001816 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001817
1818 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1819 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001820 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1821 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001822 unlock_page(page);
1823 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001824 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001825 goto again;
1826 }
Chris Mason247e7432008-07-17 12:53:51 -04001827
Jeff Mahoney87826df2012-02-15 16:23:57 +01001828 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1829 if (ret) {
1830 mapping_set_error(page->mapping, ret);
1831 end_extent_writepage(page, ret, page_start, page_end);
1832 ClearPageChecked(page);
1833 goto out;
1834 }
1835
Josef Bacik2ac55d42010-02-03 19:33:23 +00001836 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001837 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001838 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001839out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001840 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1841 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001842out_page:
1843 unlock_page(page);
1844 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001845 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001846}
1847
1848/*
1849 * There are a few paths in the higher layers of the kernel that directly
1850 * set the page dirty bit without asking the filesystem if it is a
1851 * good idea. This causes problems because we want to make sure COW
1852 * properly happens and the data=ordered rules are followed.
1853 *
Chris Masonc8b97812008-10-29 14:49:59 -04001854 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001855 * hasn't been properly setup for IO. We kick off an async process
1856 * to fix it up. The async helper will wait for ordered extents, set
1857 * the delalloc bit and make it safe to write the page.
1858 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001859static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001860{
1861 struct inode *inode = page->mapping->host;
1862 struct btrfs_writepage_fixup *fixup;
1863 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001864
Chris Mason8b62b722009-09-02 16:53:46 -04001865 /* this page is properly in the ordered list */
1866 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001867 return 0;
1868
1869 if (PageChecked(page))
1870 return -EAGAIN;
1871
1872 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1873 if (!fixup)
1874 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001875
Chris Mason247e7432008-07-17 12:53:51 -04001876 SetPageChecked(page);
1877 page_cache_get(page);
1878 fixup->work.func = btrfs_writepage_fixup_worker;
1879 fixup->page = page;
1880 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001881 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001882}
1883
Yan Zhengd899e052008-10-30 14:25:28 -04001884static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1885 struct inode *inode, u64 file_pos,
1886 u64 disk_bytenr, u64 disk_num_bytes,
1887 u64 num_bytes, u64 ram_bytes,
1888 u8 compression, u8 encryption,
1889 u16 other_encoding, int extent_type)
1890{
1891 struct btrfs_root *root = BTRFS_I(inode)->root;
1892 struct btrfs_file_extent_item *fi;
1893 struct btrfs_path *path;
1894 struct extent_buffer *leaf;
1895 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001896 int ret;
1897
1898 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001899 if (!path)
1900 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001901
Chris Masonb9473432009-03-13 11:00:37 -04001902 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001903
1904 /*
1905 * we may be replacing one extent in the tree with another.
1906 * The new extent is pinned in the extent map, and we don't want
1907 * to drop it from the cache until it is completely in the btree.
1908 *
1909 * So, tell btrfs_drop_extents to leave this extent in the cache.
1910 * the caller is expected to unpin it and allow it to be merged
1911 * with the others.
1912 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001913 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001914 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 if (ret)
1916 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001917
Li Zefan33345d012011-04-20 10:31:50 +08001918 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001919 ins.offset = file_pos;
1920 ins.type = BTRFS_EXTENT_DATA_KEY;
1921 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 if (ret)
1923 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001924 leaf = path->nodes[0];
1925 fi = btrfs_item_ptr(leaf, path->slots[0],
1926 struct btrfs_file_extent_item);
1927 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1928 btrfs_set_file_extent_type(leaf, fi, extent_type);
1929 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1930 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1931 btrfs_set_file_extent_offset(leaf, fi, 0);
1932 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1933 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1934 btrfs_set_file_extent_compression(leaf, fi, compression);
1935 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1936 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001937
Yan Zhengd899e052008-10-30 14:25:28 -04001938 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001939 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001940
1941 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001942
1943 ins.objectid = disk_bytenr;
1944 ins.offset = disk_num_bytes;
1945 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 ret = btrfs_alloc_reserved_file_extent(trans, root,
1947 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001948 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001949out:
Yan Zhengd899e052008-10-30 14:25:28 -04001950 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001951
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001953}
1954
Liu Bo38c227d2013-01-29 03:18:40 +00001955/* snapshot-aware defrag */
1956struct sa_defrag_extent_backref {
1957 struct rb_node node;
1958 struct old_sa_defrag_extent *old;
1959 u64 root_id;
1960 u64 inum;
1961 u64 file_pos;
1962 u64 extent_offset;
1963 u64 num_bytes;
1964 u64 generation;
1965};
1966
1967struct old_sa_defrag_extent {
1968 struct list_head list;
1969 struct new_sa_defrag_extent *new;
1970
1971 u64 extent_offset;
1972 u64 bytenr;
1973 u64 offset;
1974 u64 len;
1975 int count;
1976};
1977
1978struct new_sa_defrag_extent {
1979 struct rb_root root;
1980 struct list_head head;
1981 struct btrfs_path *path;
1982 struct inode *inode;
1983 u64 file_pos;
1984 u64 len;
1985 u64 bytenr;
1986 u64 disk_len;
1987 u8 compress_type;
1988};
1989
1990static int backref_comp(struct sa_defrag_extent_backref *b1,
1991 struct sa_defrag_extent_backref *b2)
1992{
1993 if (b1->root_id < b2->root_id)
1994 return -1;
1995 else if (b1->root_id > b2->root_id)
1996 return 1;
1997
1998 if (b1->inum < b2->inum)
1999 return -1;
2000 else if (b1->inum > b2->inum)
2001 return 1;
2002
2003 if (b1->file_pos < b2->file_pos)
2004 return -1;
2005 else if (b1->file_pos > b2->file_pos)
2006 return 1;
2007
2008 /*
2009 * [------------------------------] ===> (a range of space)
2010 * |<--->| |<---->| =============> (fs/file tree A)
2011 * |<---------------------------->| ===> (fs/file tree B)
2012 *
2013 * A range of space can refer to two file extents in one tree while
2014 * refer to only one file extent in another tree.
2015 *
2016 * So we may process a disk offset more than one time(two extents in A)
2017 * and locate at the same extent(one extent in B), then insert two same
2018 * backrefs(both refer to the extent in B).
2019 */
2020 return 0;
2021}
2022
2023static void backref_insert(struct rb_root *root,
2024 struct sa_defrag_extent_backref *backref)
2025{
2026 struct rb_node **p = &root->rb_node;
2027 struct rb_node *parent = NULL;
2028 struct sa_defrag_extent_backref *entry;
2029 int ret;
2030
2031 while (*p) {
2032 parent = *p;
2033 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2034
2035 ret = backref_comp(backref, entry);
2036 if (ret < 0)
2037 p = &(*p)->rb_left;
2038 else
2039 p = &(*p)->rb_right;
2040 }
2041
2042 rb_link_node(&backref->node, parent, p);
2043 rb_insert_color(&backref->node, root);
2044}
2045
2046/*
2047 * Note the backref might has changed, and in this case we just return 0.
2048 */
2049static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2050 void *ctx)
2051{
2052 struct btrfs_file_extent_item *extent;
2053 struct btrfs_fs_info *fs_info;
2054 struct old_sa_defrag_extent *old = ctx;
2055 struct new_sa_defrag_extent *new = old->new;
2056 struct btrfs_path *path = new->path;
2057 struct btrfs_key key;
2058 struct btrfs_root *root;
2059 struct sa_defrag_extent_backref *backref;
2060 struct extent_buffer *leaf;
2061 struct inode *inode = new->inode;
2062 int slot;
2063 int ret;
2064 u64 extent_offset;
2065 u64 num_bytes;
2066
2067 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2068 inum == btrfs_ino(inode))
2069 return 0;
2070
2071 key.objectid = root_id;
2072 key.type = BTRFS_ROOT_ITEM_KEY;
2073 key.offset = (u64)-1;
2074
2075 fs_info = BTRFS_I(inode)->root->fs_info;
2076 root = btrfs_read_fs_root_no_name(fs_info, &key);
2077 if (IS_ERR(root)) {
2078 if (PTR_ERR(root) == -ENOENT)
2079 return 0;
2080 WARN_ON(1);
2081 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2082 inum, offset, root_id);
2083 return PTR_ERR(root);
2084 }
2085
2086 key.objectid = inum;
2087 key.type = BTRFS_EXTENT_DATA_KEY;
2088 if (offset > (u64)-1 << 32)
2089 key.offset = 0;
2090 else
2091 key.offset = offset;
2092
2093 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2094 if (ret < 0) {
2095 WARN_ON(1);
2096 return ret;
2097 }
2098
2099 while (1) {
2100 cond_resched();
2101
2102 leaf = path->nodes[0];
2103 slot = path->slots[0];
2104
2105 if (slot >= btrfs_header_nritems(leaf)) {
2106 ret = btrfs_next_leaf(root, path);
2107 if (ret < 0) {
2108 goto out;
2109 } else if (ret > 0) {
2110 ret = 0;
2111 goto out;
2112 }
2113 continue;
2114 }
2115
2116 path->slots[0]++;
2117
2118 btrfs_item_key_to_cpu(leaf, &key, slot);
2119
2120 if (key.objectid > inum)
2121 goto out;
2122
2123 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2124 continue;
2125
2126 extent = btrfs_item_ptr(leaf, slot,
2127 struct btrfs_file_extent_item);
2128
2129 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2130 continue;
2131
2132 extent_offset = btrfs_file_extent_offset(leaf, extent);
2133 if (key.offset - extent_offset != offset)
2134 continue;
2135
2136 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2137 if (extent_offset >= old->extent_offset + old->offset +
2138 old->len || extent_offset + num_bytes <=
2139 old->extent_offset + old->offset)
2140 continue;
2141
2142 break;
2143 }
2144
2145 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2146 if (!backref) {
2147 ret = -ENOENT;
2148 goto out;
2149 }
2150
2151 backref->root_id = root_id;
2152 backref->inum = inum;
2153 backref->file_pos = offset + extent_offset;
2154 backref->num_bytes = num_bytes;
2155 backref->extent_offset = extent_offset;
2156 backref->generation = btrfs_file_extent_generation(leaf, extent);
2157 backref->old = old;
2158 backref_insert(&new->root, backref);
2159 old->count++;
2160out:
2161 btrfs_release_path(path);
2162 WARN_ON(ret);
2163 return ret;
2164}
2165
2166static noinline bool record_extent_backrefs(struct btrfs_path *path,
2167 struct new_sa_defrag_extent *new)
2168{
2169 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2170 struct old_sa_defrag_extent *old, *tmp;
2171 int ret;
2172
2173 new->path = path;
2174
2175 list_for_each_entry_safe(old, tmp, &new->head, list) {
2176 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2177 path, record_one_backref,
2178 old);
2179 BUG_ON(ret < 0 && ret != -ENOENT);
2180
2181 /* no backref to be processed for this extent */
2182 if (!old->count) {
2183 list_del(&old->list);
2184 kfree(old);
2185 }
2186 }
2187
2188 if (list_empty(&new->head))
2189 return false;
2190
2191 return true;
2192}
2193
2194static int relink_is_mergable(struct extent_buffer *leaf,
2195 struct btrfs_file_extent_item *fi,
2196 u64 disk_bytenr)
2197{
2198 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2199 return 0;
2200
2201 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2202 return 0;
2203
2204 if (btrfs_file_extent_compression(leaf, fi) ||
2205 btrfs_file_extent_encryption(leaf, fi) ||
2206 btrfs_file_extent_other_encoding(leaf, fi))
2207 return 0;
2208
2209 return 1;
2210}
2211
2212/*
2213 * Note the backref might has changed, and in this case we just return 0.
2214 */
2215static noinline int relink_extent_backref(struct btrfs_path *path,
2216 struct sa_defrag_extent_backref *prev,
2217 struct sa_defrag_extent_backref *backref)
2218{
2219 struct btrfs_file_extent_item *extent;
2220 struct btrfs_file_extent_item *item;
2221 struct btrfs_ordered_extent *ordered;
2222 struct btrfs_trans_handle *trans;
2223 struct btrfs_fs_info *fs_info;
2224 struct btrfs_root *root;
2225 struct btrfs_key key;
2226 struct extent_buffer *leaf;
2227 struct old_sa_defrag_extent *old = backref->old;
2228 struct new_sa_defrag_extent *new = old->new;
2229 struct inode *src_inode = new->inode;
2230 struct inode *inode;
2231 struct extent_state *cached = NULL;
2232 int ret = 0;
2233 u64 start;
2234 u64 len;
2235 u64 lock_start;
2236 u64 lock_end;
2237 bool merge = false;
2238 int index;
2239
2240 if (prev && prev->root_id == backref->root_id &&
2241 prev->inum == backref->inum &&
2242 prev->file_pos + prev->num_bytes == backref->file_pos)
2243 merge = true;
2244
2245 /* step 1: get root */
2246 key.objectid = backref->root_id;
2247 key.type = BTRFS_ROOT_ITEM_KEY;
2248 key.offset = (u64)-1;
2249
2250 fs_info = BTRFS_I(src_inode)->root->fs_info;
2251 index = srcu_read_lock(&fs_info->subvol_srcu);
2252
2253 root = btrfs_read_fs_root_no_name(fs_info, &key);
2254 if (IS_ERR(root)) {
2255 srcu_read_unlock(&fs_info->subvol_srcu, index);
2256 if (PTR_ERR(root) == -ENOENT)
2257 return 0;
2258 return PTR_ERR(root);
2259 }
2260 if (btrfs_root_refs(&root->root_item) == 0) {
2261 srcu_read_unlock(&fs_info->subvol_srcu, index);
2262 /* parse ENOENT to 0 */
2263 return 0;
2264 }
2265
2266 /* step 2: get inode */
2267 key.objectid = backref->inum;
2268 key.type = BTRFS_INODE_ITEM_KEY;
2269 key.offset = 0;
2270
2271 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2272 if (IS_ERR(inode)) {
2273 srcu_read_unlock(&fs_info->subvol_srcu, index);
2274 return 0;
2275 }
2276
2277 srcu_read_unlock(&fs_info->subvol_srcu, index);
2278
2279 /* step 3: relink backref */
2280 lock_start = backref->file_pos;
2281 lock_end = backref->file_pos + backref->num_bytes - 1;
2282 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2283 0, &cached);
2284
2285 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2286 if (ordered) {
2287 btrfs_put_ordered_extent(ordered);
2288 goto out_unlock;
2289 }
2290
2291 trans = btrfs_join_transaction(root);
2292 if (IS_ERR(trans)) {
2293 ret = PTR_ERR(trans);
2294 goto out_unlock;
2295 }
2296
2297 key.objectid = backref->inum;
2298 key.type = BTRFS_EXTENT_DATA_KEY;
2299 key.offset = backref->file_pos;
2300
2301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2302 if (ret < 0) {
2303 goto out_free_path;
2304 } else if (ret > 0) {
2305 ret = 0;
2306 goto out_free_path;
2307 }
2308
2309 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2310 struct btrfs_file_extent_item);
2311
2312 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2313 backref->generation)
2314 goto out_free_path;
2315
2316 btrfs_release_path(path);
2317
2318 start = backref->file_pos;
2319 if (backref->extent_offset < old->extent_offset + old->offset)
2320 start += old->extent_offset + old->offset -
2321 backref->extent_offset;
2322
2323 len = min(backref->extent_offset + backref->num_bytes,
2324 old->extent_offset + old->offset + old->len);
2325 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2326
2327 ret = btrfs_drop_extents(trans, root, inode, start,
2328 start + len, 1);
2329 if (ret)
2330 goto out_free_path;
2331again:
2332 key.objectid = btrfs_ino(inode);
2333 key.type = BTRFS_EXTENT_DATA_KEY;
2334 key.offset = start;
2335
Liu Boa09a0a72013-03-11 09:20:58 +00002336 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002337 if (merge) {
2338 struct btrfs_file_extent_item *fi;
2339 u64 extent_len;
2340 struct btrfs_key found_key;
2341
2342 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2343 if (ret < 0)
2344 goto out_free_path;
2345
2346 path->slots[0]--;
2347 leaf = path->nodes[0];
2348 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2349
2350 fi = btrfs_item_ptr(leaf, path->slots[0],
2351 struct btrfs_file_extent_item);
2352 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2353
2354 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2355 extent_len + found_key.offset == start) {
2356 btrfs_set_file_extent_num_bytes(leaf, fi,
2357 extent_len + len);
2358 btrfs_mark_buffer_dirty(leaf);
2359 inode_add_bytes(inode, len);
2360
2361 ret = 1;
2362 goto out_free_path;
2363 } else {
2364 merge = false;
2365 btrfs_release_path(path);
2366 goto again;
2367 }
2368 }
2369
2370 ret = btrfs_insert_empty_item(trans, root, path, &key,
2371 sizeof(*extent));
2372 if (ret) {
2373 btrfs_abort_transaction(trans, root, ret);
2374 goto out_free_path;
2375 }
2376
2377 leaf = path->nodes[0];
2378 item = btrfs_item_ptr(leaf, path->slots[0],
2379 struct btrfs_file_extent_item);
2380 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2381 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2382 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2383 btrfs_set_file_extent_num_bytes(leaf, item, len);
2384 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2385 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2386 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2387 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2388 btrfs_set_file_extent_encryption(leaf, item, 0);
2389 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2390
2391 btrfs_mark_buffer_dirty(leaf);
2392 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002393 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002394
2395 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2396 new->disk_len, 0,
2397 backref->root_id, backref->inum,
2398 new->file_pos, 0); /* start - extent_offset */
2399 if (ret) {
2400 btrfs_abort_transaction(trans, root, ret);
2401 goto out_free_path;
2402 }
2403
2404 ret = 1;
2405out_free_path:
2406 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002407 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002408 btrfs_end_transaction(trans, root);
2409out_unlock:
2410 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2411 &cached, GFP_NOFS);
2412 iput(inode);
2413 return ret;
2414}
2415
2416static void relink_file_extents(struct new_sa_defrag_extent *new)
2417{
2418 struct btrfs_path *path;
2419 struct old_sa_defrag_extent *old, *tmp;
2420 struct sa_defrag_extent_backref *backref;
2421 struct sa_defrag_extent_backref *prev = NULL;
2422 struct inode *inode;
2423 struct btrfs_root *root;
2424 struct rb_node *node;
2425 int ret;
2426
2427 inode = new->inode;
2428 root = BTRFS_I(inode)->root;
2429
2430 path = btrfs_alloc_path();
2431 if (!path)
2432 return;
2433
2434 if (!record_extent_backrefs(path, new)) {
2435 btrfs_free_path(path);
2436 goto out;
2437 }
2438 btrfs_release_path(path);
2439
2440 while (1) {
2441 node = rb_first(&new->root);
2442 if (!node)
2443 break;
2444 rb_erase(node, &new->root);
2445
2446 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2447
2448 ret = relink_extent_backref(path, prev, backref);
2449 WARN_ON(ret < 0);
2450
2451 kfree(prev);
2452
2453 if (ret == 1)
2454 prev = backref;
2455 else
2456 prev = NULL;
2457 cond_resched();
2458 }
2459 kfree(prev);
2460
2461 btrfs_free_path(path);
2462
2463 list_for_each_entry_safe(old, tmp, &new->head, list) {
2464 list_del(&old->list);
2465 kfree(old);
2466 }
2467out:
2468 atomic_dec(&root->fs_info->defrag_running);
2469 wake_up(&root->fs_info->transaction_wait);
2470
2471 kfree(new);
2472}
2473
2474static struct new_sa_defrag_extent *
2475record_old_file_extents(struct inode *inode,
2476 struct btrfs_ordered_extent *ordered)
2477{
2478 struct btrfs_root *root = BTRFS_I(inode)->root;
2479 struct btrfs_path *path;
2480 struct btrfs_key key;
2481 struct old_sa_defrag_extent *old, *tmp;
2482 struct new_sa_defrag_extent *new;
2483 int ret;
2484
2485 new = kmalloc(sizeof(*new), GFP_NOFS);
2486 if (!new)
2487 return NULL;
2488
2489 new->inode = inode;
2490 new->file_pos = ordered->file_offset;
2491 new->len = ordered->len;
2492 new->bytenr = ordered->start;
2493 new->disk_len = ordered->disk_len;
2494 new->compress_type = ordered->compress_type;
2495 new->root = RB_ROOT;
2496 INIT_LIST_HEAD(&new->head);
2497
2498 path = btrfs_alloc_path();
2499 if (!path)
2500 goto out_kfree;
2501
2502 key.objectid = btrfs_ino(inode);
2503 key.type = BTRFS_EXTENT_DATA_KEY;
2504 key.offset = new->file_pos;
2505
2506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2507 if (ret < 0)
2508 goto out_free_path;
2509 if (ret > 0 && path->slots[0] > 0)
2510 path->slots[0]--;
2511
2512 /* find out all the old extents for the file range */
2513 while (1) {
2514 struct btrfs_file_extent_item *extent;
2515 struct extent_buffer *l;
2516 int slot;
2517 u64 num_bytes;
2518 u64 offset;
2519 u64 end;
2520 u64 disk_bytenr;
2521 u64 extent_offset;
2522
2523 l = path->nodes[0];
2524 slot = path->slots[0];
2525
2526 if (slot >= btrfs_header_nritems(l)) {
2527 ret = btrfs_next_leaf(root, path);
2528 if (ret < 0)
2529 goto out_free_list;
2530 else if (ret > 0)
2531 break;
2532 continue;
2533 }
2534
2535 btrfs_item_key_to_cpu(l, &key, slot);
2536
2537 if (key.objectid != btrfs_ino(inode))
2538 break;
2539 if (key.type != BTRFS_EXTENT_DATA_KEY)
2540 break;
2541 if (key.offset >= new->file_pos + new->len)
2542 break;
2543
2544 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2545
2546 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2547 if (key.offset + num_bytes < new->file_pos)
2548 goto next;
2549
2550 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2551 if (!disk_bytenr)
2552 goto next;
2553
2554 extent_offset = btrfs_file_extent_offset(l, extent);
2555
2556 old = kmalloc(sizeof(*old), GFP_NOFS);
2557 if (!old)
2558 goto out_free_list;
2559
2560 offset = max(new->file_pos, key.offset);
2561 end = min(new->file_pos + new->len, key.offset + num_bytes);
2562
2563 old->bytenr = disk_bytenr;
2564 old->extent_offset = extent_offset;
2565 old->offset = offset - key.offset;
2566 old->len = end - offset;
2567 old->new = new;
2568 old->count = 0;
2569 list_add_tail(&old->list, &new->head);
2570next:
2571 path->slots[0]++;
2572 cond_resched();
2573 }
2574
2575 btrfs_free_path(path);
2576 atomic_inc(&root->fs_info->defrag_running);
2577
2578 return new;
2579
2580out_free_list:
2581 list_for_each_entry_safe(old, tmp, &new->head, list) {
2582 list_del(&old->list);
2583 kfree(old);
2584 }
2585out_free_path:
2586 btrfs_free_path(path);
2587out_kfree:
2588 kfree(new);
2589 return NULL;
2590}
2591
Chris Mason5d13a982009-03-13 11:41:46 -04002592/*
2593 * helper function for btrfs_finish_ordered_io, this
2594 * just reads in some of the csum leaves to prime them into ram
2595 * before we start the transaction. It limits the amount of btree
2596 * reads required while inside the transaction.
2597 */
Chris Masond352ac62008-09-29 15:18:18 -04002598/* as ordered data IO finishes, this gets called so we can finish
2599 * an ordered extent if the range of bytes in the file it covers are
2600 * fully written.
2601 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002602static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603{
Josef Bacik5fd02042012-05-02 14:00:54 -04002604 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002606 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002608 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002609 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002610 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002611 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002612 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002613
Liu Bo83eea1f2012-07-10 05:28:39 -06002614 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002615
Josef Bacik5fd02042012-05-02 14:00:54 -04002616 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2617 ret = -EIO;
2618 goto out;
2619 }
2620
Yan, Zhengc2167752009-11-12 09:34:21 +00002621 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002622 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002623 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2624 if (nolock)
2625 trans = btrfs_join_transaction_nolock(root);
2626 else
2627 trans = btrfs_join_transaction(root);
2628 if (IS_ERR(trans)) {
2629 ret = PTR_ERR(trans);
2630 trans = NULL;
2631 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002632 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002633 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2634 ret = btrfs_update_inode_fallback(trans, root, inode);
2635 if (ret) /* -ENOMEM or corruption */
2636 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002637 goto out;
2638 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002639
Josef Bacik2ac55d42010-02-03 19:33:23 +00002640 lock_extent_bits(io_tree, ordered_extent->file_offset,
2641 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002642 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643
Liu Bo38c227d2013-01-29 03:18:40 +00002644 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2645 ordered_extent->file_offset + ordered_extent->len - 1,
2646 EXTENT_DEFRAG, 1, cached_state);
2647 if (ret) {
2648 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2649 if (last_snapshot >= BTRFS_I(inode)->generation)
2650 /* the inode is shared */
2651 new = record_old_file_extents(inode, ordered_extent);
2652
2653 clear_extent_bit(io_tree, ordered_extent->file_offset,
2654 ordered_extent->file_offset + ordered_extent->len - 1,
2655 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2656 }
2657
Josef Bacik0cb59c92010-07-02 12:14:14 -04002658 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002659 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002660 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002661 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002662 if (IS_ERR(trans)) {
2663 ret = PTR_ERR(trans);
2664 trans = NULL;
2665 goto out_unlock;
2666 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002667 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002668
Chris Masonc8b97812008-10-29 14:49:59 -04002669 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002670 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002671 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002672 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002673 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002674 ordered_extent->file_offset,
2675 ordered_extent->file_offset +
2676 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002677 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002678 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002679 ret = insert_reserved_file_extent(trans, inode,
2680 ordered_extent->file_offset,
2681 ordered_extent->start,
2682 ordered_extent->disk_len,
2683 ordered_extent->len,
2684 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002685 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002686 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002687 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002688 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2689 ordered_extent->file_offset, ordered_extent->len,
2690 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002691 if (ret < 0) {
2692 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002693 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002694 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002695
Chris Masone6dcd2d2008-07-17 12:53:50 -04002696 add_pending_csums(trans, inode, ordered_extent->file_offset,
2697 &ordered_extent->list);
2698
Josef Bacik6c760c02012-11-09 10:53:21 -05002699 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2700 ret = btrfs_update_inode_fallback(trans, root, inode);
2701 if (ret) { /* -ENOMEM or corruption */
2702 btrfs_abort_transaction(trans, root, ret);
2703 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002704 }
2705 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002706out_unlock:
2707 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2708 ordered_extent->file_offset +
2709 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002710out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002711 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002712 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002713 if (trans)
2714 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002715
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002716 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002717 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2718 ordered_extent->file_offset +
2719 ordered_extent->len - 1, NULL, GFP_NOFS);
2720
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002721 /*
2722 * If the ordered extent had an IOERR or something else went
2723 * wrong we need to return the space for this ordered extent
2724 * back to the allocator.
2725 */
2726 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2727 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2728 btrfs_free_reserved_extent(root, ordered_extent->start,
2729 ordered_extent->disk_len);
2730 }
2731
2732
Josef Bacik5fd02042012-05-02 14:00:54 -04002733 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002734 * This needs to be done to make sure anybody waiting knows we are done
2735 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002736 */
2737 btrfs_remove_ordered_extent(inode, ordered_extent);
2738
Liu Bo38c227d2013-01-29 03:18:40 +00002739 /* for snapshot-aware defrag */
2740 if (new)
2741 relink_file_extents(new);
2742
Chris Masone6dcd2d2008-07-17 12:53:50 -04002743 /* once for us */
2744 btrfs_put_ordered_extent(ordered_extent);
2745 /* once for the tree */
2746 btrfs_put_ordered_extent(ordered_extent);
2747
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 return ret;
2749}
2750
2751static void finish_ordered_fn(struct btrfs_work *work)
2752{
2753 struct btrfs_ordered_extent *ordered_extent;
2754 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2755 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002756}
2757
Christoph Hellwigb2950862008-12-02 09:54:17 -05002758static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002759 struct extent_state *state, int uptodate)
2760{
Josef Bacik5fd02042012-05-02 14:00:54 -04002761 struct inode *inode = page->mapping->host;
2762 struct btrfs_root *root = BTRFS_I(inode)->root;
2763 struct btrfs_ordered_extent *ordered_extent = NULL;
2764 struct btrfs_workers *workers;
2765
liubo1abe9b82011-03-24 11:18:59 +00002766 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2767
Chris Mason8b62b722009-09-02 16:53:46 -04002768 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002769 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2770 end - start + 1, uptodate))
2771 return 0;
2772
2773 ordered_extent->work.func = finish_ordered_fn;
2774 ordered_extent->work.flags = 0;
2775
Liu Bo83eea1f2012-07-10 05:28:39 -06002776 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 workers = &root->fs_info->endio_freespace_worker;
2778 else
2779 workers = &root->fs_info->endio_write_workers;
2780 btrfs_queue_worker(workers, &ordered_extent->work);
2781
2782 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002783}
2784
Chris Masond352ac62008-09-29 15:18:18 -04002785/*
Chris Masond352ac62008-09-29 15:18:18 -04002786 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002787 * if there's a match, we allow the bio to finish. If not, the code in
2788 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002789 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002790static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002791 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002792{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002793 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002794 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002795 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002796 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002797 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002798 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002799 struct btrfs_root *root = BTRFS_I(inode)->root;
2800 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002801 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2802 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002803
Chris Masond20f7042008-12-08 16:58:54 -05002804 if (PageChecked(page)) {
2805 ClearPageChecked(page);
2806 goto good;
2807 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002808
2809 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002810 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002811
Yan Zheng17d217f2008-12-12 10:03:38 -05002812 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002813 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002814 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2815 GFP_NOFS);
2816 return 0;
2817 }
2818
Yanc2e639f2008-02-04 08:57:25 -05002819 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002820 private = state->private;
2821 ret = 0;
2822 } else {
2823 ret = get_state_private(io_tree, start, &private);
2824 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002825 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002826 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002827 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002828
Liu Bob0496682013-03-14 14:57:45 +00002829 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002830 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002831 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002832 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002833
Cong Wang7ac687d2011-11-25 23:14:28 +08002834 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002835good:
Chris Mason07157aa2007-08-30 08:50:51 -04002836 return 0;
2837
2838zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002839 if (__ratelimit(&_rs))
2840 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2841 (unsigned long long)btrfs_ino(page->mapping->host),
2842 (unsigned long long)start, csum,
2843 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002844 memset(kaddr + offset, 1, end - start + 1);
2845 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002846 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002847 if (private == 0)
2848 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002849 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002850}
Chris Masonb888db22007-08-27 16:49:44 -04002851
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002852struct delayed_iput {
2853 struct list_head list;
2854 struct inode *inode;
2855};
2856
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002857/* JDM: If this is fs-wide, why can't we add a pointer to
2858 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002859void btrfs_add_delayed_iput(struct inode *inode)
2860{
2861 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2862 struct delayed_iput *delayed;
2863
2864 if (atomic_add_unless(&inode->i_count, -1, 1))
2865 return;
2866
2867 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2868 delayed->inode = inode;
2869
2870 spin_lock(&fs_info->delayed_iput_lock);
2871 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2872 spin_unlock(&fs_info->delayed_iput_lock);
2873}
2874
2875void btrfs_run_delayed_iputs(struct btrfs_root *root)
2876{
2877 LIST_HEAD(list);
2878 struct btrfs_fs_info *fs_info = root->fs_info;
2879 struct delayed_iput *delayed;
2880 int empty;
2881
2882 spin_lock(&fs_info->delayed_iput_lock);
2883 empty = list_empty(&fs_info->delayed_iputs);
2884 spin_unlock(&fs_info->delayed_iput_lock);
2885 if (empty)
2886 return;
2887
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888 spin_lock(&fs_info->delayed_iput_lock);
2889 list_splice_init(&fs_info->delayed_iputs, &list);
2890 spin_unlock(&fs_info->delayed_iput_lock);
2891
2892 while (!list_empty(&list)) {
2893 delayed = list_entry(list.next, struct delayed_iput, list);
2894 list_del(&delayed->list);
2895 iput(delayed->inode);
2896 kfree(delayed);
2897 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002898}
2899
Yan, Zhengd68fc572010-05-16 10:49:58 -04002900/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002901 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002902 * files in the subvolume, it removes orphan item and frees block_rsv
2903 * structure.
2904 */
2905void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2906 struct btrfs_root *root)
2907{
Josef Bacik90290e12011-12-02 15:44:12 -05002908 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002909 int ret;
2910
Josef Bacik8a35d952012-05-23 14:26:42 -04002911 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002912 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2913 return;
2914
Josef Bacik90290e12011-12-02 15:44:12 -05002915 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002916 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002917 spin_unlock(&root->orphan_lock);
2918 return;
2919 }
2920
2921 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2922 spin_unlock(&root->orphan_lock);
2923 return;
2924 }
2925
2926 block_rsv = root->orphan_block_rsv;
2927 root->orphan_block_rsv = NULL;
2928 spin_unlock(&root->orphan_lock);
2929
Yan, Zhengd68fc572010-05-16 10:49:58 -04002930 if (root->orphan_item_inserted &&
2931 btrfs_root_refs(&root->root_item) > 0) {
2932 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2933 root->root_key.objectid);
2934 BUG_ON(ret);
2935 root->orphan_item_inserted = 0;
2936 }
2937
Josef Bacik90290e12011-12-02 15:44:12 -05002938 if (block_rsv) {
2939 WARN_ON(block_rsv->size > 0);
2940 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 }
2942}
2943
2944/*
Josef Bacik7b128762008-07-24 12:17:14 -04002945 * This creates an orphan entry for the given inode in case something goes
2946 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002947 *
2948 * NOTE: caller of this function should reserve 5 units of metadata for
2949 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002950 */
2951int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2952{
2953 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 struct btrfs_block_rsv *block_rsv = NULL;
2955 int reserve = 0;
2956 int insert = 0;
2957 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002958
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002960 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002961 if (!block_rsv)
2962 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002963 }
2964
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 spin_lock(&root->orphan_lock);
2966 if (!root->orphan_block_rsv) {
2967 root->orphan_block_rsv = block_rsv;
2968 } else if (block_rsv) {
2969 btrfs_free_block_rsv(root, block_rsv);
2970 block_rsv = NULL;
2971 }
Josef Bacik7b128762008-07-24 12:17:14 -04002972
Josef Bacik8a35d952012-05-23 14:26:42 -04002973 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2974 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002975#if 0
2976 /*
2977 * For proper ENOSPC handling, we should do orphan
2978 * cleanup when mounting. But this introduces backward
2979 * compatibility issue.
2980 */
2981 if (!xchg(&root->orphan_item_inserted, 1))
2982 insert = 2;
2983 else
2984 insert = 1;
2985#endif
2986 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002987 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 }
Josef Bacik7b128762008-07-24 12:17:14 -04002989
Josef Bacik72ac3c02012-05-23 14:13:11 -04002990 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2991 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002992 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002994
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 /* grab metadata reservation from transaction handle */
2996 if (reserve) {
2997 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 }
3000
3001 /* insert an orphan item to track this unlinked/truncated file */
3002 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003003 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003004 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003005 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3006 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003007 btrfs_abort_transaction(trans, root, ret);
3008 return ret;
3009 }
3010 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003011 }
3012
3013 /* insert an orphan item to track subvolume contains orphan files */
3014 if (insert >= 2) {
3015 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3016 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003017 if (ret && ret != -EEXIST) {
3018 btrfs_abort_transaction(trans, root, ret);
3019 return ret;
3020 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 }
3022 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003023}
3024
3025/*
3026 * We have done the truncate/delete so we can go ahead and remove the orphan
3027 * item for this particular inode.
3028 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003029static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3030 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003031{
3032 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 int delete_item = 0;
3034 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003035 int ret = 0;
3036
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003038 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3039 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003041
Josef Bacik72ac3c02012-05-23 14:13:11 -04003042 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3043 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003044 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003045 spin_unlock(&root->orphan_lock);
3046
3047 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003048 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003050 }
3051
Josef Bacik8a35d952012-05-23 14:26:42 -04003052 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003054 atomic_dec(&root->orphan_inodes);
3055 }
Josef Bacik7b128762008-07-24 12:17:14 -04003056
Yan, Zhengd68fc572010-05-16 10:49:58 -04003057 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003058}
3059
3060/*
3061 * this cleans up any orphans that may be left on the list from the last use
3062 * of this root.
3063 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003064int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003065{
3066 struct btrfs_path *path;
3067 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003068 struct btrfs_key key, found_key;
3069 struct btrfs_trans_handle *trans;
3070 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003071 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003072 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3073
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003075 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003076
3077 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003078 if (!path) {
3079 ret = -ENOMEM;
3080 goto out;
3081 }
Josef Bacik7b128762008-07-24 12:17:14 -04003082 path->reada = -1;
3083
3084 key.objectid = BTRFS_ORPHAN_OBJECTID;
3085 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3086 key.offset = (u64)-1;
3087
Josef Bacik7b128762008-07-24 12:17:14 -04003088 while (1) {
3089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003090 if (ret < 0)
3091 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003092
3093 /*
3094 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003095 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003096 * find the key and see if we have stuff that matches
3097 */
3098 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003099 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003100 if (path->slots[0] == 0)
3101 break;
3102 path->slots[0]--;
3103 }
3104
3105 /* pull out the item */
3106 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3108
3109 /* make sure the item matches what we want */
3110 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3111 break;
3112 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3113 break;
3114
3115 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003116 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003117
3118 /*
3119 * this is where we are basically btrfs_lookup, without the
3120 * crossing root thing. we store the inode number in the
3121 * offset of the orphan item.
3122 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003123
3124 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003125 btrfs_err(root->fs_info,
3126 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003127 ret = -EINVAL;
3128 goto out;
3129 }
3130
3131 last_objectid = found_key.offset;
3132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 found_key.objectid = found_key.offset;
3134 found_key.type = BTRFS_INODE_ITEM_KEY;
3135 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003136 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003137 ret = PTR_RET(inode);
3138 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003140
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003141 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3142 struct btrfs_root *dead_root;
3143 struct btrfs_fs_info *fs_info = root->fs_info;
3144 int is_dead_root = 0;
3145
3146 /*
3147 * this is an orphan in the tree root. Currently these
3148 * could come from 2 sources:
3149 * a) a snapshot deletion in progress
3150 * b) a free space cache inode
3151 * We need to distinguish those two, as the snapshot
3152 * orphan must not get deleted.
3153 * find_dead_roots already ran before us, so if this
3154 * is a snapshot deletion, we should find the root
3155 * in the dead_roots list
3156 */
3157 spin_lock(&fs_info->trans_lock);
3158 list_for_each_entry(dead_root, &fs_info->dead_roots,
3159 root_list) {
3160 if (dead_root->root_key.objectid ==
3161 found_key.objectid) {
3162 is_dead_root = 1;
3163 break;
3164 }
3165 }
3166 spin_unlock(&fs_info->trans_lock);
3167 if (is_dead_root) {
3168 /* prevent this orphan from being found again */
3169 key.offset = found_key.objectid - 1;
3170 continue;
3171 }
3172 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003173 /*
3174 * Inode is already gone but the orphan item is still there,
3175 * kill the orphan item.
3176 */
3177 if (ret == -ESTALE) {
3178 trans = btrfs_start_transaction(root, 1);
3179 if (IS_ERR(trans)) {
3180 ret = PTR_ERR(trans);
3181 goto out;
3182 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003183 btrfs_debug(root->fs_info, "auto deleting %Lu",
3184 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003185 ret = btrfs_del_orphan_item(trans, root,
3186 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003187 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003188 btrfs_end_transaction(trans, root);
3189 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003190 }
Josef Bacik7b128762008-07-24 12:17:14 -04003191
Josef Bacik7b128762008-07-24 12:17:14 -04003192 /*
3193 * add this inode to the orphan list so btrfs_orphan_del does
3194 * the proper thing when we hit it
3195 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3197 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003198 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003199
Josef Bacik7b128762008-07-24 12:17:14 -04003200 /* if we have links, this was a truncate, lets do that */
3201 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003202 if (!S_ISREG(inode->i_mode)) {
3203 WARN_ON(1);
3204 iput(inode);
3205 continue;
3206 }
Josef Bacik7b128762008-07-24 12:17:14 -04003207 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003208
3209 /* 1 for the orphan item deletion. */
3210 trans = btrfs_start_transaction(root, 1);
3211 if (IS_ERR(trans)) {
3212 ret = PTR_ERR(trans);
3213 goto out;
3214 }
3215 ret = btrfs_orphan_add(trans, inode);
3216 btrfs_end_transaction(trans, root);
3217 if (ret)
3218 goto out;
3219
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003220 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003221 if (ret)
3222 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003223 } else {
3224 nr_unlink++;
3225 }
3226
3227 /* this will do delete_inode and everything for us */
3228 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003229 if (ret)
3230 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003231 }
Miao Xie3254c872011-11-10 20:45:05 -05003232 /* release the path since we're done with it */
3233 btrfs_release_path(path);
3234
Yan, Zhengd68fc572010-05-16 10:49:58 -04003235 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3236
3237 if (root->orphan_block_rsv)
3238 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3239 (u64)-1);
3240
3241 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003242 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003243 if (!IS_ERR(trans))
3244 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 }
Josef Bacik7b128762008-07-24 12:17:14 -04003246
3247 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003248 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003249 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003250 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003251
3252out:
3253 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003254 btrfs_crit(root->fs_info,
3255 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003256 btrfs_free_path(path);
3257 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003258}
3259
Chris Masond352ac62008-09-29 15:18:18 -04003260/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003261 * very simple check to peek ahead in the leaf looking for xattrs. If we
3262 * don't find any xattrs, we know there can't be any acls.
3263 *
3264 * slot is the slot the inode is in, objectid is the objectid of the inode
3265 */
3266static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3267 int slot, u64 objectid)
3268{
3269 u32 nritems = btrfs_header_nritems(leaf);
3270 struct btrfs_key found_key;
3271 int scanned = 0;
3272
3273 slot++;
3274 while (slot < nritems) {
3275 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3276
3277 /* we found a different objectid, there must not be acls */
3278 if (found_key.objectid != objectid)
3279 return 0;
3280
3281 /* we found an xattr, assume we've got an acl */
3282 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3283 return 1;
3284
3285 /*
3286 * we found a key greater than an xattr key, there can't
3287 * be any acls later on
3288 */
3289 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3290 return 0;
3291
3292 slot++;
3293 scanned++;
3294
3295 /*
3296 * it goes inode, inode backrefs, xattrs, extents,
3297 * so if there are a ton of hard links to an inode there can
3298 * be a lot of backrefs. Don't waste time searching too hard,
3299 * this is just an optimization
3300 */
3301 if (scanned >= 8)
3302 break;
3303 }
3304 /* we hit the end of the leaf before we found an xattr or
3305 * something larger than an xattr. We have to assume the inode
3306 * has acls
3307 */
3308 return 1;
3309}
3310
3311/*
Chris Masond352ac62008-09-29 15:18:18 -04003312 * read an inode from the btree into the in-memory inode
3313 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003314static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003315{
3316 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003317 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003318 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003319 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003320 struct btrfs_root *root = BTRFS_I(inode)->root;
3321 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003322 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003323 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003324 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003325 bool filled = false;
3326
3327 ret = btrfs_fill_inode(inode, &rdev);
3328 if (!ret)
3329 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003330
3331 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003332 if (!path)
3333 goto make_bad;
3334
Josef Bacikd90c7322011-05-17 09:50:54 -04003335 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003336 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003337
Chris Mason39279cc2007-06-12 06:35:45 -04003338 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003339 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003340 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003341
Chris Mason5f39d392007-10-15 16:14:19 -04003342 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003343
3344 if (filled)
3345 goto cache_acl;
3346
Chris Mason5f39d392007-10-15 16:14:19 -04003347 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3348 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003349 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003350 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003351 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3352 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003353 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003354
3355 tspec = btrfs_inode_atime(inode_item);
3356 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3357 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3358
3359 tspec = btrfs_inode_mtime(inode_item);
3360 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3361 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3362
3363 tspec = btrfs_inode_ctime(inode_item);
3364 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3365 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3366
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003367 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003368 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003369 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3370
3371 /*
3372 * If we were modified in the current generation and evicted from memory
3373 * and then re-read we need to do a full sync since we don't have any
3374 * idea about which extents were modified before we were evicted from
3375 * cache.
3376 */
3377 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3378 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3379 &BTRFS_I(inode)->runtime_flags);
3380
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003381 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003382 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003383 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003384 rdev = btrfs_inode_rdev(leaf, inode_item);
3385
Josef Bacikaec74772008-07-24 12:12:38 -04003386 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003387 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003388cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003389 /*
3390 * try to precache a NULL acl entry for files that don't have
3391 * any xattrs or acls
3392 */
Li Zefan33345d012011-04-20 10:31:50 +08003393 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3394 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003395 if (!maybe_acls)
3396 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003397
Chris Mason39279cc2007-06-12 06:35:45 -04003398 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003399
Chris Mason39279cc2007-06-12 06:35:45 -04003400 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003401 case S_IFREG:
3402 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003403 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003404 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003405 inode->i_fop = &btrfs_file_operations;
3406 inode->i_op = &btrfs_file_inode_operations;
3407 break;
3408 case S_IFDIR:
3409 inode->i_fop = &btrfs_dir_file_operations;
3410 if (root == root->fs_info->tree_root)
3411 inode->i_op = &btrfs_dir_ro_inode_operations;
3412 else
3413 inode->i_op = &btrfs_dir_inode_operations;
3414 break;
3415 case S_IFLNK:
3416 inode->i_op = &btrfs_symlink_inode_operations;
3417 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003418 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003419 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003420 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003421 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003422 init_special_inode(inode, inode->i_mode, rdev);
3423 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003424 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003425
3426 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003427 return;
3428
3429make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003430 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003431 make_bad_inode(inode);
3432}
3433
Chris Masond352ac62008-09-29 15:18:18 -04003434/*
3435 * given a leaf and an inode, copy the inode fields into the leaf
3436 */
Chris Masone02119d2008-09-05 16:13:11 -04003437static void fill_inode_item(struct btrfs_trans_handle *trans,
3438 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003439 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003440 struct inode *inode)
3441{
Liu Bo51fab692012-12-27 09:01:21 +00003442 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003443
Liu Bo51fab692012-12-27 09:01:21 +00003444 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003445
Liu Bo51fab692012-12-27 09:01:21 +00003446 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3447 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3448 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3449 &token);
3450 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3451 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003452
Liu Bo51fab692012-12-27 09:01:21 +00003453 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3454 inode->i_atime.tv_sec, &token);
3455 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3456 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003457
Liu Bo51fab692012-12-27 09:01:21 +00003458 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3459 inode->i_mtime.tv_sec, &token);
3460 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3461 inode->i_mtime.tv_nsec, &token);
3462
3463 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3464 inode->i_ctime.tv_sec, &token);
3465 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3466 inode->i_ctime.tv_nsec, &token);
3467
3468 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3469 &token);
3470 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3471 &token);
3472 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3473 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3474 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3475 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3476 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003477}
3478
Chris Masond352ac62008-09-29 15:18:18 -04003479/*
3480 * copy everything in the in-memory inode into the btree.
3481 */
Chris Mason21151332011-11-10 20:39:08 -05003482static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003483 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003484{
3485 struct btrfs_inode_item *inode_item;
3486 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003487 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003488 int ret;
3489
3490 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003491 if (!path)
3492 return -ENOMEM;
3493
Chris Masonb9473432009-03-13 11:00:37 -04003494 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003495 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3496 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 if (ret) {
3498 if (ret > 0)
3499 ret = -ENOENT;
3500 goto failed;
3501 }
3502
Chris Masonb4ce94d2009-02-04 09:25:08 -05003503 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003504 leaf = path->nodes[0];
3505 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003506 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003507
Chris Masone02119d2008-09-05 16:13:11 -04003508 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003510 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003511 ret = 0;
3512failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003513 btrfs_free_path(path);
3514 return ret;
3515}
3516
Chris Masond352ac62008-09-29 15:18:18 -04003517/*
Chris Mason21151332011-11-10 20:39:08 -05003518 * copy everything in the in-memory inode into the btree.
3519 */
3520noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3521 struct btrfs_root *root, struct inode *inode)
3522{
3523 int ret;
3524
3525 /*
3526 * If the inode is a free space inode, we can deadlock during commit
3527 * if we put it into the delayed code.
3528 *
3529 * The data relocation inode should also be directly updated
3530 * without delay
3531 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003532 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003533 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003534 btrfs_update_root_times(trans, root);
3535
Chris Mason21151332011-11-10 20:39:08 -05003536 ret = btrfs_delayed_update_inode(trans, root, inode);
3537 if (!ret)
3538 btrfs_set_inode_last_trans(trans, inode);
3539 return ret;
3540 }
3541
3542 return btrfs_update_inode_item(trans, root, inode);
3543}
3544
Josef Bacikbe6aef62012-10-22 15:43:12 -04003545noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3546 struct btrfs_root *root,
3547 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003548{
3549 int ret;
3550
3551 ret = btrfs_update_inode(trans, root, inode);
3552 if (ret == -ENOSPC)
3553 return btrfs_update_inode_item(trans, root, inode);
3554 return ret;
3555}
3556
3557/*
Chris Masond352ac62008-09-29 15:18:18 -04003558 * unlink helper that gets used here in inode.c and in the tree logging
3559 * recovery code. It remove a link in a directory with a given name, and
3560 * also drops the back refs in the inode to the directory
3561 */
Al Viro92986792011-03-04 17:14:37 +00003562static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3563 struct btrfs_root *root,
3564 struct inode *dir, struct inode *inode,
3565 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003566{
3567 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003568 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003569 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003570 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003571 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003572 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003573 u64 ino = btrfs_ino(inode);
3574 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003575
3576 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003577 if (!path) {
3578 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003579 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003580 }
3581
Chris Masonb9473432009-03-13 11:00:37 -04003582 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003583 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003584 name, name_len, -1);
3585 if (IS_ERR(di)) {
3586 ret = PTR_ERR(di);
3587 goto err;
3588 }
3589 if (!di) {
3590 ret = -ENOENT;
3591 goto err;
3592 }
Chris Mason5f39d392007-10-15 16:14:19 -04003593 leaf = path->nodes[0];
3594 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003595 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003596 if (ret)
3597 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003598 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003599
Li Zefan33345d012011-04-20 10:31:50 +08003600 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3601 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003602 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003603 btrfs_info(root->fs_info,
3604 "failed to delete reference to %.*s, inode %llu parent %llu",
3605 name_len, name,
3606 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003607 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003608 goto err;
3609 }
3610
Miao Xie16cdcec2011-04-22 18:12:22 +08003611 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 if (ret) {
3613 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003614 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003615 }
Chris Mason39279cc2007-06-12 06:35:45 -04003616
Chris Masone02119d2008-09-05 16:13:11 -04003617 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003618 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003619 if (ret != 0 && ret != -ENOENT) {
3620 btrfs_abort_transaction(trans, root, ret);
3621 goto err;
3622 }
Chris Masone02119d2008-09-05 16:13:11 -04003623
3624 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3625 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003626 if (ret == -ENOENT)
3627 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003628 else if (ret)
3629 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003630err:
3631 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003632 if (ret)
3633 goto out;
3634
3635 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003636 inode_inc_iversion(inode);
3637 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003638 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003639 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003640out:
Chris Mason39279cc2007-06-12 06:35:45 -04003641 return ret;
3642}
3643
Al Viro92986792011-03-04 17:14:37 +00003644int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3645 struct btrfs_root *root,
3646 struct inode *dir, struct inode *inode,
3647 const char *name, int name_len)
3648{
3649 int ret;
3650 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3651 if (!ret) {
3652 btrfs_drop_nlink(inode);
3653 ret = btrfs_update_inode(trans, root, inode);
3654 }
3655 return ret;
3656}
3657
3658
Yan, Zhenga22285a2010-05-16 10:48:46 -04003659/* helper to check if there is any shared block in the path */
3660static int check_path_shared(struct btrfs_root *root,
3661 struct btrfs_path *path)
3662{
3663 struct extent_buffer *eb;
3664 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003665 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666
3667 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003668 int ret;
3669
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670 if (!path->nodes[level])
3671 break;
3672 eb = path->nodes[level];
3673 if (!btrfs_block_can_be_shared(root, eb))
3674 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003675 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676 &refs, NULL);
3677 if (refs > 1)
3678 return 1;
3679 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003680 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681}
3682
3683/*
3684 * helper to start transaction for unlink and rmdir.
3685 *
3686 * unlink and rmdir are special in btrfs, they do not always free space.
3687 * so in enospc case, we should make sure they will free space before
3688 * allowing them to use the global metadata reservation.
3689 */
3690static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3691 struct dentry *dentry)
3692{
3693 struct btrfs_trans_handle *trans;
3694 struct btrfs_root *root = BTRFS_I(dir)->root;
3695 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696 struct btrfs_dir_item *di;
3697 struct inode *inode = dentry->d_inode;
3698 u64 index;
3699 int check_link = 1;
3700 int err = -ENOSPC;
3701 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003702 u64 ino = btrfs_ino(inode);
3703 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003704
Josef Bacike70bea52011-10-11 14:18:24 -04003705 /*
3706 * 1 for the possible orphan item
3707 * 1 for the dir item
3708 * 1 for the dir index
3709 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003710 * 1 for the inode
3711 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003712 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3714 return trans;
3715
Li Zefan33345d012011-04-20 10:31:50 +08003716 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717 return ERR_PTR(-ENOSPC);
3718
3719 /* check if there is someone else holds reference */
3720 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3721 return ERR_PTR(-ENOSPC);
3722
3723 if (atomic_read(&inode->i_count) > 2)
3724 return ERR_PTR(-ENOSPC);
3725
3726 if (xchg(&root->fs_info->enospc_unlink, 1))
3727 return ERR_PTR(-ENOSPC);
3728
3729 path = btrfs_alloc_path();
3730 if (!path) {
3731 root->fs_info->enospc_unlink = 0;
3732 return ERR_PTR(-ENOMEM);
3733 }
3734
Josef Bacik3880a1b2011-10-14 14:46:51 -04003735 /* 1 for the orphan item */
3736 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003737 if (IS_ERR(trans)) {
3738 btrfs_free_path(path);
3739 root->fs_info->enospc_unlink = 0;
3740 return trans;
3741 }
3742
3743 path->skip_locking = 1;
3744 path->search_commit_root = 1;
3745
3746 ret = btrfs_lookup_inode(trans, root, path,
3747 &BTRFS_I(dir)->location, 0);
3748 if (ret < 0) {
3749 err = ret;
3750 goto out;
3751 }
3752 if (ret == 0) {
3753 if (check_path_shared(root, path))
3754 goto out;
3755 } else {
3756 check_link = 0;
3757 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003758 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003759
3760 ret = btrfs_lookup_inode(trans, root, path,
3761 &BTRFS_I(inode)->location, 0);
3762 if (ret < 0) {
3763 err = ret;
3764 goto out;
3765 }
3766 if (ret == 0) {
3767 if (check_path_shared(root, path))
3768 goto out;
3769 } else {
3770 check_link = 0;
3771 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003772 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003773
3774 if (ret == 0 && S_ISREG(inode->i_mode)) {
3775 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003776 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003777 if (ret < 0) {
3778 err = ret;
3779 goto out;
3780 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003781 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003782 if (check_path_shared(root, path))
3783 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003784 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003785 }
3786
3787 if (!check_link) {
3788 err = 0;
3789 goto out;
3790 }
3791
Li Zefan33345d012011-04-20 10:31:50 +08003792 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003793 dentry->d_name.name, dentry->d_name.len, 0);
3794 if (IS_ERR(di)) {
3795 err = PTR_ERR(di);
3796 goto out;
3797 }
3798 if (di) {
3799 if (check_path_shared(root, path))
3800 goto out;
3801 } else {
3802 err = 0;
3803 goto out;
3804 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003805 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003806
Mark Fashehf1863732012-08-08 11:32:27 -07003807 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3808 dentry->d_name.len, ino, dir_ino, 0,
3809 &index);
3810 if (ret) {
3811 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003812 goto out;
3813 }
Mark Fashehf1863732012-08-08 11:32:27 -07003814
Yan, Zhenga22285a2010-05-16 10:48:46 -04003815 if (check_path_shared(root, path))
3816 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003817
David Sterbab3b4aa72011-04-21 01:20:15 +02003818 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003819
Miao Xie16cdcec2011-04-22 18:12:22 +08003820 /*
3821 * This is a commit root search, if we can lookup inode item and other
3822 * relative items in the commit root, it means the transaction of
3823 * dir/file creation has been committed, and the dir index item that we
3824 * delay to insert has also been inserted into the commit root. So
3825 * we needn't worry about the delayed insertion of the dir index item
3826 * here.
3827 */
Li Zefan33345d012011-04-20 10:31:50 +08003828 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003829 dentry->d_name.name, dentry->d_name.len, 0);
3830 if (IS_ERR(di)) {
3831 err = PTR_ERR(di);
3832 goto out;
3833 }
3834 BUG_ON(ret == -ENOENT);
3835 if (check_path_shared(root, path))
3836 goto out;
3837
3838 err = 0;
3839out:
3840 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003841 /* Migrate the orphan reservation over */
3842 if (!err)
3843 err = btrfs_block_rsv_migrate(trans->block_rsv,
3844 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003845 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003846
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847 if (err) {
3848 btrfs_end_transaction(trans, root);
3849 root->fs_info->enospc_unlink = 0;
3850 return ERR_PTR(err);
3851 }
3852
3853 trans->block_rsv = &root->fs_info->global_block_rsv;
3854 return trans;
3855}
3856
3857static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3858 struct btrfs_root *root)
3859{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003860 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003861 btrfs_block_rsv_release(root, trans->block_rsv,
3862 trans->bytes_reserved);
3863 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003864 BUG_ON(!root->fs_info->enospc_unlink);
3865 root->fs_info->enospc_unlink = 0;
3866 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003867 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003868}
3869
Chris Mason39279cc2007-06-12 06:35:45 -04003870static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3871{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003872 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003873 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003874 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 int ret;
3876
Yan, Zhenga22285a2010-05-16 10:48:46 -04003877 trans = __unlink_start_trans(dir, dentry);
3878 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003879 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003880
Chris Mason12fcfd22009-03-24 10:24:20 -04003881 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3882
Chris Masone02119d2008-09-05 16:13:11 -04003883 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3884 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003885 if (ret)
3886 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003887
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003889 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003890 if (ret)
3891 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003892 }
Josef Bacik7b128762008-07-24 12:17:14 -04003893
Tsutomu Itohb5324022011-07-19 07:27:20 +00003894out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003895 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003896 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003897 return ret;
3898}
3899
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003900int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3901 struct btrfs_root *root,
3902 struct inode *dir, u64 objectid,
3903 const char *name, int name_len)
3904{
3905 struct btrfs_path *path;
3906 struct extent_buffer *leaf;
3907 struct btrfs_dir_item *di;
3908 struct btrfs_key key;
3909 u64 index;
3910 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003911 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003912
3913 path = btrfs_alloc_path();
3914 if (!path)
3915 return -ENOMEM;
3916
Li Zefan33345d012011-04-20 10:31:50 +08003917 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003918 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003919 if (IS_ERR_OR_NULL(di)) {
3920 if (!di)
3921 ret = -ENOENT;
3922 else
3923 ret = PTR_ERR(di);
3924 goto out;
3925 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003926
3927 leaf = path->nodes[0];
3928 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3929 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3930 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003931 if (ret) {
3932 btrfs_abort_transaction(trans, root, ret);
3933 goto out;
3934 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003935 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003936
3937 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3938 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003939 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003941 if (ret != -ENOENT) {
3942 btrfs_abort_transaction(trans, root, ret);
3943 goto out;
3944 }
Li Zefan33345d012011-04-20 10:31:50 +08003945 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003946 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003947 if (IS_ERR_OR_NULL(di)) {
3948 if (!di)
3949 ret = -ENOENT;
3950 else
3951 ret = PTR_ERR(di);
3952 btrfs_abort_transaction(trans, root, ret);
3953 goto out;
3954 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003955
3956 leaf = path->nodes[0];
3957 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003958 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003959 index = key.offset;
3960 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003961 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003962
Miao Xie16cdcec2011-04-22 18:12:22 +08003963 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 if (ret) {
3965 btrfs_abort_transaction(trans, root, ret);
3966 goto out;
3967 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003968
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003969 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003970 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003972 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003973 if (ret)
3974 btrfs_abort_transaction(trans, root, ret);
3975out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003976 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003977 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003978}
3979
Chris Mason39279cc2007-06-12 06:35:45 -04003980static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3981{
3982 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003983 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003984 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003986
David Sterbab3ae2442012-09-13 16:04:34 -06003987 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003988 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003989 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3990 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003991
Yan, Zhenga22285a2010-05-16 10:48:46 -04003992 trans = __unlink_start_trans(dir, dentry);
3993 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003994 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003995
Li Zefan33345d012011-04-20 10:31:50 +08003996 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003997 err = btrfs_unlink_subvol(trans, root, dir,
3998 BTRFS_I(inode)->location.objectid,
3999 dentry->d_name.name,
4000 dentry->d_name.len);
4001 goto out;
4002 }
4003
Josef Bacik7b128762008-07-24 12:17:14 -04004004 err = btrfs_orphan_add(trans, inode);
4005 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004006 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004007
Chris Mason39279cc2007-06-12 06:35:45 -04004008 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004009 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4010 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004011 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004012 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004013out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004014 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004015 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004016
Chris Mason39279cc2007-06-12 06:35:45 -04004017 return err;
4018}
4019
Chris Mason323ac952008-10-01 19:05:46 -04004020/*
Chris Mason39279cc2007-06-12 06:35:45 -04004021 * this can truncate away extent items, csum items and directory items.
4022 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004023 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004024 *
4025 * csum items that cross the new i_size are truncated to the new size
4026 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004027 *
4028 * min_type is the minimum key type to truncate down to. If set to 0, this
4029 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004030 */
Yan, Zheng80825102009-11-12 09:35:36 +00004031int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4032 struct btrfs_root *root,
4033 struct inode *inode,
4034 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004035{
Chris Mason39279cc2007-06-12 06:35:45 -04004036 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004037 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004039 struct btrfs_key key;
4040 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004041 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004042 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004044 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004045 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 int found_extent;
4047 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004048 int pending_del_nr = 0;
4049 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004050 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004051 int ret;
4052 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004053 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004054
4055 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004056
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004057 path = btrfs_alloc_path();
4058 if (!path)
4059 return -ENOMEM;
4060 path->reada = -1;
4061
Josef Bacik5dc562c2012-08-17 13:14:17 -04004062 /*
4063 * We want to drop from the next block forward in case this new size is
4064 * not block aligned since we will be keeping the last block of the
4065 * extent just the way it is.
4066 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004067 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004068 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4069 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004070
Miao Xie16cdcec2011-04-22 18:12:22 +08004071 /*
4072 * This function is also used to drop the items in the log tree before
4073 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4074 * it is used to drop the loged items. So we shouldn't kill the delayed
4075 * items.
4076 */
4077 if (min_type == 0 && root == BTRFS_I(inode)->root)
4078 btrfs_kill_delayed_inode_items(inode);
4079
Li Zefan33345d012011-04-20 10:31:50 +08004080 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004081 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004082 key.type = (u8)-1;
4083
Chris Mason85e21ba2008-01-29 15:11:36 -05004084search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004085 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004086 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004087 if (ret < 0) {
4088 err = ret;
4089 goto out;
4090 }
Chris Masond3977122009-01-05 21:25:51 -05004091
Chris Mason85e21ba2008-01-29 15:11:36 -05004092 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004093 /* there are no items in the tree for us to truncate, we're
4094 * done
4095 */
Yan, Zheng80825102009-11-12 09:35:36 +00004096 if (path->slots[0] == 0)
4097 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004098 path->slots[0]--;
4099 }
4100
Chris Masond3977122009-01-05 21:25:51 -05004101 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004102 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004103 leaf = path->nodes[0];
4104 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4105 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004106
Li Zefan33345d012011-04-20 10:31:50 +08004107 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004108 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004109
Chris Mason85e21ba2008-01-29 15:11:36 -05004110 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004111 break;
4112
Chris Mason5f39d392007-10-15 16:14:19 -04004113 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004114 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004115 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004116 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004117 extent_type = btrfs_file_extent_type(leaf, fi);
4118 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004119 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004120 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004121 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004122 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004123 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004124 }
Yan008630c2007-11-07 13:31:09 -05004125 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004126 }
Yan, Zheng80825102009-11-12 09:35:36 +00004127 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004128 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004129 } else {
4130 if (item_end < new_size)
4131 break;
4132 if (found_key.offset >= new_size)
4133 del_item = 1;
4134 else
4135 del_item = 0;
4136 }
Chris Mason39279cc2007-06-12 06:35:45 -04004137 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004138 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004139 if (found_type != BTRFS_EXTENT_DATA_KEY)
4140 goto delete;
4141
4142 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004143 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004144 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004145 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004146 u64 orig_num_bytes =
4147 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004148 extent_num_bytes = ALIGN(new_size -
4149 found_key.offset,
4150 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004151 btrfs_set_file_extent_num_bytes(leaf, fi,
4152 extent_num_bytes);
4153 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004154 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004155 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004156 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004157 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004158 } else {
Chris Masondb945352007-10-15 16:15:53 -04004159 extent_num_bytes =
4160 btrfs_file_extent_disk_num_bytes(leaf,
4161 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162 extent_offset = found_key.offset -
4163 btrfs_file_extent_offset(leaf, fi);
4164
Chris Mason39279cc2007-06-12 06:35:45 -04004165 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004166 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004167 if (extent_start != 0) {
4168 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004169 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004170 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004171 }
4172 }
Chris Mason90692182008-02-08 13:49:28 -05004173 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004174 /*
4175 * we can't truncate inline items that have had
4176 * special encodings
4177 */
4178 if (!del_item &&
4179 btrfs_file_extent_compression(leaf, fi) == 0 &&
4180 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4181 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004182 u32 size = new_size - found_key.offset;
4183
4184 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004185 inode_sub_bytes(inode, item_end + 1 -
4186 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004187 }
4188 size =
4189 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004190 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004191 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004192 inode_sub_bytes(inode, item_end + 1 -
4193 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004194 }
Chris Mason39279cc2007-06-12 06:35:45 -04004195 }
Chris Mason179e29e2007-11-01 11:28:41 -04004196delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004197 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004198 if (!pending_del_nr) {
4199 /* no pending yet, add ourselves */
4200 pending_del_slot = path->slots[0];
4201 pending_del_nr = 1;
4202 } else if (pending_del_nr &&
4203 path->slots[0] + 1 == pending_del_slot) {
4204 /* hop on the pending chunk */
4205 pending_del_nr++;
4206 pending_del_slot = path->slots[0];
4207 } else {
Chris Masond3977122009-01-05 21:25:51 -05004208 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004209 }
Chris Mason39279cc2007-06-12 06:35:45 -04004210 } else {
4211 break;
4212 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004213 if (found_extent && (root->ref_cows ||
4214 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004215 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004216 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004217 extent_num_bytes, 0,
4218 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004219 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004220 BUG_ON(ret);
4221 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004222
Yan, Zheng80825102009-11-12 09:35:36 +00004223 if (found_type == BTRFS_INODE_ITEM_KEY)
4224 break;
4225
4226 if (path->slots[0] == 0 ||
4227 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004228 if (pending_del_nr) {
4229 ret = btrfs_del_items(trans, root, path,
4230 pending_del_slot,
4231 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232 if (ret) {
4233 btrfs_abort_transaction(trans,
4234 root, ret);
4235 goto error;
4236 }
Yan, Zheng80825102009-11-12 09:35:36 +00004237 pending_del_nr = 0;
4238 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004239 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004240 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004241 } else {
4242 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004243 }
Chris Mason39279cc2007-06-12 06:35:45 -04004244 }
Yan, Zheng80825102009-11-12 09:35:36 +00004245out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004246 if (pending_del_nr) {
4247 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4248 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004249 if (ret)
4250 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004251 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004252error:
Chris Mason39279cc2007-06-12 06:35:45 -04004253 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004254 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004255}
4256
Chris Masona52d9a82007-08-27 16:49:44 -04004257/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004258 * btrfs_truncate_page - read, zero a chunk and write a page
4259 * @inode - inode that we're zeroing
4260 * @from - the offset to start zeroing
4261 * @len - the length to zero, 0 to zero the entire range respective to the
4262 * offset
4263 * @front - zero up to the offset instead of from the offset on
4264 *
4265 * This will find the page for the "from" offset and cow the page and zero the
4266 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004267 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004268int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4269 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004270{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004271 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004272 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004273 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4274 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004275 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004276 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004277 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004278 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4279 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4280 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004281 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004282 int ret = 0;
4283 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004284 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004285
Josef Bacik2aaa6652012-08-29 14:27:18 -04004286 if ((offset & (blocksize - 1)) == 0 &&
4287 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004288 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004289 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004290 if (ret)
4291 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004292
Chris Mason211c17f2008-05-15 09:13:45 -04004293again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004294 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004295 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004296 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004297 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004298 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004299 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004300
4301 page_start = page_offset(page);
4302 page_end = page_start + PAGE_CACHE_SIZE - 1;
4303
Chris Masona52d9a82007-08-27 16:49:44 -04004304 if (!PageUptodate(page)) {
4305 ret = btrfs_readpage(NULL, page);
4306 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004307 if (page->mapping != mapping) {
4308 unlock_page(page);
4309 page_cache_release(page);
4310 goto again;
4311 }
Chris Masona52d9a82007-08-27 16:49:44 -04004312 if (!PageUptodate(page)) {
4313 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004314 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004315 }
4316 }
Chris Mason211c17f2008-05-15 09:13:45 -04004317 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004318
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004319 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 set_page_extent_mapped(page);
4321
4322 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4323 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004324 unlock_extent_cached(io_tree, page_start, page_end,
4325 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 unlock_page(page);
4327 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004328 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004329 btrfs_put_ordered_extent(ordered);
4330 goto again;
4331 }
4332
Josef Bacik2ac55d42010-02-03 19:33:23 +00004333 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004334 EXTENT_DIRTY | EXTENT_DELALLOC |
4335 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004336 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004337
Josef Bacik2ac55d42010-02-03 19:33:23 +00004338 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4339 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004340 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004341 unlock_extent_cached(io_tree, page_start, page_end,
4342 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004343 goto out_unlock;
4344 }
4345
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004347 if (!len)
4348 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004350 if (front)
4351 memset(kaddr, 0, offset);
4352 else
4353 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004354 flush_dcache_page(page);
4355 kunmap(page);
4356 }
Chris Mason247e7432008-07-17 12:53:51 -04004357 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004358 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004359 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4360 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004361
Chris Mason89642222008-07-24 09:41:53 -04004362out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004363 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004364 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 unlock_page(page);
4366 page_cache_release(page);
4367out:
4368 return ret;
4369}
4370
Josef Bacik695a0d02011-03-04 15:46:53 -05004371/*
4372 * This function puts in dummy file extents for the area we're creating a hole
4373 * for. So if we are truncating this file to a larger size we need to insert
4374 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4375 * the range between oldsize and size
4376 */
Josef Bacika41ad392011-01-31 15:30:16 -05004377int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004378{
4379 struct btrfs_trans_handle *trans;
4380 struct btrfs_root *root = BTRFS_I(inode)->root;
4381 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004382 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004383 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004384 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004385 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4386 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004387 u64 last_byte;
4388 u64 cur_offset;
4389 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004390 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004391
4392 if (size <= hole_start)
4393 return 0;
4394
Yan Zheng9036c102008-10-30 14:19:41 -04004395 while (1) {
4396 struct btrfs_ordered_extent *ordered;
4397 btrfs_wait_ordered_range(inode, hole_start,
4398 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004399 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004400 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004401 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4402 if (!ordered)
4403 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004404 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4405 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004406 btrfs_put_ordered_extent(ordered);
4407 }
4408
Yan Zheng9036c102008-10-30 14:19:41 -04004409 cur_offset = hole_start;
4410 while (1) {
4411 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4412 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004413 if (IS_ERR(em)) {
4414 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004415 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004416 break;
4417 }
Yan Zheng9036c102008-10-30 14:19:41 -04004418 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004419 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004420 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004421 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004422 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004423
Miao Xie36423202011-12-14 20:12:02 -05004424 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004425 if (IS_ERR(trans)) {
4426 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004427 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004428 }
Yan, Zheng80825102009-11-12 09:35:36 +00004429
Josef Bacik5dc562c2012-08-17 13:14:17 -04004430 err = btrfs_drop_extents(trans, root, inode,
4431 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004432 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004433 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004434 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004435 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004436 break;
Miao Xie5b397372011-09-11 10:52:24 -04004437 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004438
Yan Zheng9036c102008-10-30 14:19:41 -04004439 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004440 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004441 0, hole_size, 0, hole_size,
4442 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004443 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004444 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004445 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004446 break;
Miao Xie5b397372011-09-11 10:52:24 -04004447 }
Yan, Zheng80825102009-11-12 09:35:36 +00004448
Josef Bacik5dc562c2012-08-17 13:14:17 -04004449 btrfs_drop_extent_cache(inode, cur_offset,
4450 cur_offset + hole_size - 1, 0);
4451 hole_em = alloc_extent_map();
4452 if (!hole_em) {
4453 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4454 &BTRFS_I(inode)->runtime_flags);
4455 goto next;
4456 }
4457 hole_em->start = cur_offset;
4458 hole_em->len = hole_size;
4459 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004460
Josef Bacik5dc562c2012-08-17 13:14:17 -04004461 hole_em->block_start = EXTENT_MAP_HOLE;
4462 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004463 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004464 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004465 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4466 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4467 hole_em->generation = trans->transid;
4468
4469 while (1) {
4470 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004471 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004472 write_unlock(&em_tree->lock);
4473 if (err != -EEXIST)
4474 break;
4475 btrfs_drop_extent_cache(inode, cur_offset,
4476 cur_offset +
4477 hole_size - 1, 0);
4478 }
4479 free_extent_map(hole_em);
4480next:
Miao Xie36423202011-12-14 20:12:02 -05004481 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004482 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004483 }
4484 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004485 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004486 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004487 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004488 break;
4489 }
4490
Yan, Zhenga22285a2010-05-16 10:48:46 -04004491 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004492 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4493 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004494 return err;
4495}
4496
Eric Sandeen3972f262013-01-12 02:57:22 +00004497static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004498{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004499 struct btrfs_root *root = BTRFS_I(inode)->root;
4500 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004501 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004502 loff_t newsize = attr->ia_size;
4503 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004504 int ret;
4505
Josef Bacika41ad392011-01-31 15:30:16 -05004506 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004507 return 0;
4508
Eric Sandeen3972f262013-01-12 02:57:22 +00004509 /*
4510 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4511 * special case where we need to update the times despite not having
4512 * these flags set. For all other operations the VFS set these flags
4513 * explicitly if it wants a timestamp update.
4514 */
4515 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4516 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4517
Josef Bacika41ad392011-01-31 15:30:16 -05004518 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004519 truncate_pagecache(inode, oldsize, newsize);
4520 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004521 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004522 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004523
Miao Xief4a2f4c2011-12-14 20:12:01 -05004524 trans = btrfs_start_transaction(root, 1);
4525 if (IS_ERR(trans))
4526 return PTR_ERR(trans);
4527
4528 i_size_write(inode, newsize);
4529 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4530 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004531 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004532 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004533
Josef Bacika41ad392011-01-31 15:30:16 -05004534 /*
4535 * We're truncating a file that used to have good data down to
4536 * zero. Make sure it gets into the ordered flush list so that
4537 * any new writes get down to disk quickly.
4538 */
4539 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004540 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4541 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004542
Josef Bacikf3fe8202013-01-07 17:03:21 -05004543 /*
4544 * 1 for the orphan item we're going to add
4545 * 1 for the orphan item deletion.
4546 */
4547 trans = btrfs_start_transaction(root, 2);
4548 if (IS_ERR(trans))
4549 return PTR_ERR(trans);
4550
4551 /*
4552 * We need to do this in case we fail at _any_ point during the
4553 * actual truncate. Once we do the truncate_setsize we could
4554 * invalidate pages which forces any outstanding ordered io to
4555 * be instantly completed which will give us extents that need
4556 * to be truncated. If we fail to get an orphan inode down we
4557 * could have left over extents that were never meant to live,
4558 * so we need to garuntee from this point on that everything
4559 * will be consistent.
4560 */
4561 ret = btrfs_orphan_add(trans, inode);
4562 btrfs_end_transaction(trans, root);
4563 if (ret)
4564 return ret;
4565
Josef Bacika41ad392011-01-31 15:30:16 -05004566 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4567 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004568
4569 /* Disable nonlocked read DIO to avoid the end less truncate */
4570 btrfs_inode_block_unlocked_dio(inode);
4571 inode_dio_wait(inode);
4572 btrfs_inode_resume_unlocked_dio(inode);
4573
Josef Bacika41ad392011-01-31 15:30:16 -05004574 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004575 if (ret && inode->i_nlink)
4576 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004577 }
4578
Josef Bacika41ad392011-01-31 15:30:16 -05004579 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004580}
4581
Chris Mason39279cc2007-06-12 06:35:45 -04004582static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4583{
4584 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004585 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004586 int err;
4587
Li Zefanb83cc962010-12-20 16:04:08 +08004588 if (btrfs_root_readonly(root))
4589 return -EROFS;
4590
Chris Mason39279cc2007-06-12 06:35:45 -04004591 err = inode_change_ok(inode, attr);
4592 if (err)
4593 return err;
4594
Chris Mason5a3f23d2009-03-31 13:27:11 -04004595 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004596 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004597 if (err)
4598 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004599 }
Yan Zheng9036c102008-10-30 14:19:41 -04004600
Christoph Hellwig10257742010-06-04 11:30:02 +02004601 if (attr->ia_valid) {
4602 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004603 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004604 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004605
Josef Bacik22c44fe2011-11-30 10:45:38 -05004606 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004607 err = btrfs_acl_chmod(inode);
4608 }
4609
Chris Mason39279cc2007-06-12 06:35:45 -04004610 return err;
4611}
Chris Mason61295eb2008-01-14 16:24:38 -05004612
Al Virobd555972010-06-07 11:35:40 -04004613void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004614{
4615 struct btrfs_trans_handle *trans;
4616 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004617 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004618 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004619 int ret;
4620
liubo1abe9b82011-03-24 11:18:59 +00004621 trace_btrfs_inode_evict(inode);
4622
Chris Mason39279cc2007-06-12 06:35:45 -04004623 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004624 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004625 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004626 goto no_delete;
4627
Chris Mason39279cc2007-06-12 06:35:45 -04004628 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004629 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004630 goto no_delete;
4631 }
Al Virobd555972010-06-07 11:35:40 -04004632 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004633 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004634
Yan, Zhengc71bf092009-11-12 09:34:40 +00004635 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004636 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004637 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004638 goto no_delete;
4639 }
4640
Yan, Zheng76dda932009-09-21 16:00:26 -04004641 if (inode->i_nlink > 0) {
4642 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4643 goto no_delete;
4644 }
4645
Miao Xie0e8c36a2012-12-19 06:59:51 +00004646 ret = btrfs_commit_inode_delayed_inode(inode);
4647 if (ret) {
4648 btrfs_orphan_del(NULL, inode);
4649 goto no_delete;
4650 }
4651
Miao Xie66d8f3d2012-09-06 04:02:28 -06004652 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004653 if (!rsv) {
4654 btrfs_orphan_del(NULL, inode);
4655 goto no_delete;
4656 }
Josef Bacik4a338542011-08-29 11:01:31 -04004657 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004658 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004659 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004660
Chris Masondbe674a2008-07-17 12:54:05 -04004661 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004662
Josef Bacik4289a662011-08-05 13:22:24 -04004663 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004664 * This is a bit simpler than btrfs_truncate since we've already
4665 * reserved our space for our orphan item in the unlink, so we just
4666 * need to reserve some slack space in case we add bytes and update
4667 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004668 */
Yan, Zheng80825102009-11-12 09:35:36 +00004669 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004670 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4671 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004672
Josef Bacik726c35f2011-09-26 15:46:06 -04004673 /*
4674 * Try and steal from the global reserve since we will
4675 * likely not use this space anyway, we want to try as
4676 * hard as possible to get this to work.
4677 */
4678 if (ret)
4679 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4680
Yan, Zhengd68fc572010-05-16 10:49:58 -04004681 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004682 btrfs_warn(root->fs_info,
4683 "Could not get space for a delete, will truncate on mount %d",
4684 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004685 btrfs_orphan_del(NULL, inode);
4686 btrfs_free_block_rsv(root, rsv);
4687 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004688 }
4689
Miao Xie0e8c36a2012-12-19 06:59:51 +00004690 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004691 if (IS_ERR(trans)) {
4692 btrfs_orphan_del(NULL, inode);
4693 btrfs_free_block_rsv(root, rsv);
4694 goto no_delete;
4695 }
4696
4697 trans->block_rsv = rsv;
4698
Yan, Zhengd68fc572010-05-16 10:49:58 -04004699 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004700 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004701 break;
4702
Miao Xie8407aa42012-09-07 01:43:32 -06004703 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004704 btrfs_end_transaction(trans, root);
4705 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004706 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004707 }
4708
Josef Bacik4289a662011-08-05 13:22:24 -04004709 btrfs_free_block_rsv(root, rsv);
4710
Yan, Zheng80825102009-11-12 09:35:36 +00004711 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004712 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004713 ret = btrfs_orphan_del(trans, inode);
4714 BUG_ON(ret);
4715 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004716
Josef Bacik4289a662011-08-05 13:22:24 -04004717 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004718 if (!(root == root->fs_info->tree_root ||
4719 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004720 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004721
Chris Mason54aa1f42007-06-22 14:16:25 -04004722 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004723 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004724no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004725 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004726 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004727}
4728
4729/*
4730 * this returns the key found in the dir entry in the location pointer.
4731 * If no dir entries were found, location->objectid is 0.
4732 */
4733static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4734 struct btrfs_key *location)
4735{
4736 const char *name = dentry->d_name.name;
4737 int namelen = dentry->d_name.len;
4738 struct btrfs_dir_item *di;
4739 struct btrfs_path *path;
4740 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004741 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004742
4743 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004744 if (!path)
4745 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004746
Li Zefan33345d012011-04-20 10:31:50 +08004747 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004748 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004749 if (IS_ERR(di))
4750 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004751
David Sterbac7040052011-04-19 18:00:01 +02004752 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004753 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004754
Chris Mason5f39d392007-10-15 16:14:19 -04004755 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004756out:
Chris Mason39279cc2007-06-12 06:35:45 -04004757 btrfs_free_path(path);
4758 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004759out_err:
4760 location->objectid = 0;
4761 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004762}
4763
4764/*
4765 * when we hit a tree root in a directory, the btrfs part of the inode
4766 * needs to be changed to reflect the root directory of the tree root. This
4767 * is kind of like crossing a mount point.
4768 */
4769static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004770 struct inode *dir,
4771 struct dentry *dentry,
4772 struct btrfs_key *location,
4773 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004774{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004775 struct btrfs_path *path;
4776 struct btrfs_root *new_root;
4777 struct btrfs_root_ref *ref;
4778 struct extent_buffer *leaf;
4779 int ret;
4780 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004781
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004782 path = btrfs_alloc_path();
4783 if (!path) {
4784 err = -ENOMEM;
4785 goto out;
4786 }
Chris Mason39279cc2007-06-12 06:35:45 -04004787
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004788 err = -ENOENT;
4789 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4790 BTRFS_I(dir)->root->root_key.objectid,
4791 location->objectid);
4792 if (ret) {
4793 if (ret < 0)
4794 err = ret;
4795 goto out;
4796 }
Chris Mason39279cc2007-06-12 06:35:45 -04004797
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004798 leaf = path->nodes[0];
4799 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004800 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004801 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4802 goto out;
4803
4804 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4805 (unsigned long)(ref + 1),
4806 dentry->d_name.len);
4807 if (ret)
4808 goto out;
4809
David Sterbab3b4aa72011-04-21 01:20:15 +02004810 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004811
4812 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4813 if (IS_ERR(new_root)) {
4814 err = PTR_ERR(new_root);
4815 goto out;
4816 }
4817
4818 if (btrfs_root_refs(&new_root->root_item) == 0) {
4819 err = -ENOENT;
4820 goto out;
4821 }
4822
4823 *sub_root = new_root;
4824 location->objectid = btrfs_root_dirid(&new_root->root_item);
4825 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004826 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004827 err = 0;
4828out:
4829 btrfs_free_path(path);
4830 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004831}
4832
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004833static void inode_tree_add(struct inode *inode)
4834{
4835 struct btrfs_root *root = BTRFS_I(inode)->root;
4836 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004837 struct rb_node **p;
4838 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004839 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004840again:
4841 p = &root->inode_tree.rb_node;
4842 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004843
Al Viro1d3382cb2010-10-23 15:19:20 -04004844 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004845 return;
4846
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004847 spin_lock(&root->inode_lock);
4848 while (*p) {
4849 parent = *p;
4850 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4851
Li Zefan33345d012011-04-20 10:31:50 +08004852 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004853 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004854 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004855 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004856 else {
4857 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004858 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004859 rb_erase(parent, &root->inode_tree);
4860 RB_CLEAR_NODE(parent);
4861 spin_unlock(&root->inode_lock);
4862 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004863 }
4864 }
4865 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4866 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4867 spin_unlock(&root->inode_lock);
4868}
4869
4870static void inode_tree_del(struct inode *inode)
4871{
4872 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004873 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004874
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004875 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004876 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004877 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004879 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004880 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004881 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004882
Josef Bacik0af3d002010-06-21 14:48:16 -04004883 /*
4884 * Free space cache has inodes in the tree root, but the tree root has a
4885 * root_refs of 0, so this could end up dropping the tree root as a
4886 * snapshot, so we need the extra !root->fs_info->tree_root check to
4887 * make sure we don't drop it.
4888 */
4889 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4890 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004891 synchronize_srcu(&root->fs_info->subvol_srcu);
4892 spin_lock(&root->inode_lock);
4893 empty = RB_EMPTY_ROOT(&root->inode_tree);
4894 spin_unlock(&root->inode_lock);
4895 if (empty)
4896 btrfs_add_dead_root(root);
4897 }
4898}
4899
Jeff Mahoney143bede2012-03-01 14:56:26 +01004900void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004901{
4902 struct rb_node *node;
4903 struct rb_node *prev;
4904 struct btrfs_inode *entry;
4905 struct inode *inode;
4906 u64 objectid = 0;
4907
4908 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4909
4910 spin_lock(&root->inode_lock);
4911again:
4912 node = root->inode_tree.rb_node;
4913 prev = NULL;
4914 while (node) {
4915 prev = node;
4916 entry = rb_entry(node, struct btrfs_inode, rb_node);
4917
Li Zefan33345d012011-04-20 10:31:50 +08004918 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004919 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004920 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004921 node = node->rb_right;
4922 else
4923 break;
4924 }
4925 if (!node) {
4926 while (prev) {
4927 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004928 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004929 node = prev;
4930 break;
4931 }
4932 prev = rb_next(prev);
4933 }
4934 }
4935 while (node) {
4936 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004937 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004938 inode = igrab(&entry->vfs_inode);
4939 if (inode) {
4940 spin_unlock(&root->inode_lock);
4941 if (atomic_read(&inode->i_count) > 1)
4942 d_prune_aliases(inode);
4943 /*
Al Viro45321ac2010-06-07 13:43:19 -04004944 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004945 * the inode cache when its usage count
4946 * hits zero.
4947 */
4948 iput(inode);
4949 cond_resched();
4950 spin_lock(&root->inode_lock);
4951 goto again;
4952 }
4953
4954 if (cond_resched_lock(&root->inode_lock))
4955 goto again;
4956
4957 node = rb_next(node);
4958 }
4959 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004960}
4961
Chris Masone02119d2008-09-05 16:13:11 -04004962static int btrfs_init_locked_inode(struct inode *inode, void *p)
4963{
4964 struct btrfs_iget_args *args = p;
4965 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004966 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004967 return 0;
4968}
4969
4970static int btrfs_find_actor(struct inode *inode, void *opaque)
4971{
4972 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004973 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004974 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004975}
4976
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004977static struct inode *btrfs_iget_locked(struct super_block *s,
4978 u64 objectid,
4979 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004980{
4981 struct inode *inode;
4982 struct btrfs_iget_args args;
4983 args.ino = objectid;
4984 args.root = root;
4985
4986 inode = iget5_locked(s, objectid, btrfs_find_actor,
4987 btrfs_init_locked_inode,
4988 (void *)&args);
4989 return inode;
4990}
4991
Balaji Rao1a54ef82008-07-21 02:01:04 +05304992/* Get an inode object given its location and corresponding root.
4993 * Returns in *is_new if the inode was read from disk
4994 */
4995struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004996 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304997{
4998 struct inode *inode;
4999
5000 inode = btrfs_iget_locked(s, location->objectid, root);
5001 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005002 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305003
5004 if (inode->i_state & I_NEW) {
5005 BTRFS_I(inode)->root = root;
5006 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5007 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005008 if (!is_bad_inode(inode)) {
5009 inode_tree_add(inode);
5010 unlock_new_inode(inode);
5011 if (new)
5012 *new = 1;
5013 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005014 unlock_new_inode(inode);
5015 iput(inode);
5016 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005017 }
5018 }
5019
Balaji Rao1a54ef82008-07-21 02:01:04 +05305020 return inode;
5021}
5022
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005023static struct inode *new_simple_dir(struct super_block *s,
5024 struct btrfs_key *key,
5025 struct btrfs_root *root)
5026{
5027 struct inode *inode = new_inode(s);
5028
5029 if (!inode)
5030 return ERR_PTR(-ENOMEM);
5031
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005032 BTRFS_I(inode)->root = root;
5033 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005034 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005035
5036 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005037 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005038 inode->i_fop = &simple_dir_operations;
5039 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5040 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5041
5042 return inode;
5043}
5044
Chris Mason3de45862008-11-17 21:02:50 -05005045struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005046{
Chris Masond3977122009-01-05 21:25:51 -05005047 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005048 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005049 struct btrfs_root *sub_root = root;
5050 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005051 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005052 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005053
5054 if (dentry->d_name.len > BTRFS_NAME_LEN)
5055 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005056
Jeff Layton39e3c952012-11-28 11:30:53 -05005057 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005058 if (ret < 0)
5059 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005060
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005061 if (location.objectid == 0)
5062 return NULL;
5063
5064 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005065 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005066 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005067 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005068
5069 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5070
Yan, Zheng76dda932009-09-21 16:00:26 -04005071 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005072 ret = fixup_tree_root_location(root, dir, dentry,
5073 &location, &sub_root);
5074 if (ret < 0) {
5075 if (ret != -ENOENT)
5076 inode = ERR_PTR(ret);
5077 else
5078 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5079 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005080 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005081 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005082 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5083
Julia Lawall34d19ba2011-01-24 19:55:19 +00005084 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005085 down_read(&root->fs_info->cleanup_work_sem);
5086 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005087 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005088 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005089 if (ret)
5090 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005091 }
5092
Chris Mason3de45862008-11-17 21:02:50 -05005093 return inode;
5094}
5095
Nick Pigginfe15ce42011-01-07 17:49:23 +11005096static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005097{
5098 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005099 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005100
Li Zefan848cce02012-02-21 17:04:28 +08005101 if (!inode && !IS_ROOT(dentry))
5102 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005103
Li Zefan848cce02012-02-21 17:04:28 +08005104 if (inode) {
5105 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005106 if (btrfs_root_refs(&root->root_item) == 0)
5107 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005108
5109 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5110 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005111 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005112 return 0;
5113}
5114
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005115static void btrfs_dentry_release(struct dentry *dentry)
5116{
5117 if (dentry->d_fsdata)
5118 kfree(dentry->d_fsdata);
5119}
5120
Chris Mason3de45862008-11-17 21:02:50 -05005121static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005122 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005123{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005124 struct dentry *ret;
5125
5126 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005127 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005128}
5129
Miao Xie16cdcec2011-04-22 18:12:22 +08005130unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005131 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5132};
5133
David Woodhousecbdf5a22008-08-06 19:42:33 +01005134static int btrfs_real_readdir(struct file *filp, void *dirent,
5135 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005136{
Al Viro496ad9a2013-01-23 17:07:38 -05005137 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005138 struct btrfs_root *root = BTRFS_I(inode)->root;
5139 struct btrfs_item *item;
5140 struct btrfs_dir_item *di;
5141 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005142 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005143 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005144 struct list_head ins_list;
5145 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005146 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005147 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005148 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005149 unsigned char d_type;
5150 int over = 0;
5151 u32 di_cur;
5152 u32 di_total;
5153 u32 di_len;
5154 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005155 char tmp_name[32];
5156 char *name_ptr;
5157 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005158 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005159
5160 /* FIXME, use a real flag for deciding about the key type */
5161 if (root->fs_info->tree_root == root)
5162 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005163
Chris Mason39544012007-12-12 14:38:19 -05005164 /* special case for "." */
5165 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005166 over = filldir(dirent, ".", 1,
5167 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005168 if (over)
5169 return 0;
5170 filp->f_pos = 1;
5171 }
Chris Mason39544012007-12-12 14:38:19 -05005172 /* special case for .., just use the back ref */
5173 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005174 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005175 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005176 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005177 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005178 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005179 filp->f_pos = 2;
5180 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005181 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005182 if (!path)
5183 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005184
Josef Bacik026fd312011-05-13 10:32:11 -04005185 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005186
Miao Xie16cdcec2011-04-22 18:12:22 +08005187 if (key_type == BTRFS_DIR_INDEX_KEY) {
5188 INIT_LIST_HEAD(&ins_list);
5189 INIT_LIST_HEAD(&del_list);
5190 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5191 }
5192
Chris Mason39279cc2007-06-12 06:35:45 -04005193 btrfs_set_key_type(&key, key_type);
5194 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005195 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005196
Chris Mason39279cc2007-06-12 06:35:45 -04005197 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5198 if (ret < 0)
5199 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005200
5201 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005202 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005203 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005204 if (slot >= btrfs_header_nritems(leaf)) {
5205 ret = btrfs_next_leaf(root, path);
5206 if (ret < 0)
5207 goto err;
5208 else if (ret > 0)
5209 break;
5210 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005211 }
Chris Mason3de45862008-11-17 21:02:50 -05005212
Chris Mason5f39d392007-10-15 16:14:19 -04005213 item = btrfs_item_nr(leaf, slot);
5214 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5215
5216 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005217 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005218 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005219 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005220 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005221 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005222 if (key_type == BTRFS_DIR_INDEX_KEY &&
5223 btrfs_should_delete_dir_index(&del_list,
5224 found_key.offset))
5225 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005226
5227 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005228 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005229
Chris Mason39279cc2007-06-12 06:35:45 -04005230 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5231 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005232 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005233
5234 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005235 struct btrfs_key location;
5236
Josef Bacik22a94d42011-03-16 16:47:17 -04005237 if (verify_dir_item(root, leaf, di))
5238 break;
5239
Chris Mason5f39d392007-10-15 16:14:19 -04005240 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005241 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005242 name_ptr = tmp_name;
5243 } else {
5244 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005245 if (!name_ptr) {
5246 ret = -ENOMEM;
5247 goto err;
5248 }
Chris Mason5f39d392007-10-15 16:14:19 -04005249 }
5250 read_extent_buffer(leaf, name_ptr,
5251 (unsigned long)(di + 1), name_len);
5252
5253 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5254 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005255
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005256
Chris Mason3de45862008-11-17 21:02:50 -05005257 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005258 * skip it.
5259 *
5260 * In contrast to old kernels, we insert the snapshot's
5261 * dir item and dir index after it has been created, so
5262 * we won't find a reference to our own snapshot. We
5263 * still keep the following code for backward
5264 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005265 */
5266 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5267 location.objectid == root->root_key.objectid) {
5268 over = 0;
5269 goto skip;
5270 }
Chris Mason5f39d392007-10-15 16:14:19 -04005271 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005272 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005273 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005274
Chris Mason3de45862008-11-17 21:02:50 -05005275skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005276 if (name_ptr != tmp_name)
5277 kfree(name_ptr);
5278
Chris Mason39279cc2007-06-12 06:35:45 -04005279 if (over)
5280 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005281 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005282 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005283 di_cur += di_len;
5284 di = (struct btrfs_dir_item *)((char *)di + di_len);
5285 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005286next:
5287 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005288 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005289
Miao Xie16cdcec2011-04-22 18:12:22 +08005290 if (key_type == BTRFS_DIR_INDEX_KEY) {
5291 if (is_curr)
5292 filp->f_pos++;
5293 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5294 &ins_list);
5295 if (ret)
5296 goto nopos;
5297 }
5298
David Woodhouse49593bf2008-08-17 17:08:36 +01005299 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005300 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005301 /*
5302 * 32-bit glibc will use getdents64, but then strtol -
5303 * so the last number we can serve is this.
5304 */
5305 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005306 else
5307 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005308nopos:
5309 ret = 0;
5310err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005311 if (key_type == BTRFS_DIR_INDEX_KEY)
5312 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005313 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005314 return ret;
5315}
5316
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005317int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005318{
5319 struct btrfs_root *root = BTRFS_I(inode)->root;
5320 struct btrfs_trans_handle *trans;
5321 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005322 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005323
Josef Bacik72ac3c02012-05-23 14:13:11 -04005324 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005325 return 0;
5326
Liu Bo83eea1f2012-07-10 05:28:39 -06005327 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005328 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005329
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005330 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005331 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005332 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005333 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005334 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005335 if (IS_ERR(trans))
5336 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005337 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005338 }
5339 return ret;
5340}
5341
5342/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005343 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005344 * inode changes. But, it is most likely to find the inode in cache.
5345 * FIXME, needs more benchmarking...there are no reasons other than performance
5346 * to keep or drop this code.
5347 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005348static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005349{
5350 struct btrfs_root *root = BTRFS_I(inode)->root;
5351 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005352 int ret;
5353
Josef Bacik72ac3c02012-05-23 14:13:11 -04005354 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005355 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005356
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005357 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005358 if (IS_ERR(trans))
5359 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005360
5361 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005362 if (ret && ret == -ENOSPC) {
5363 /* whoops, lets try again with the full transaction */
5364 btrfs_end_transaction(trans, root);
5365 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005366 if (IS_ERR(trans))
5367 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005368
Chris Mason94b60442010-05-26 11:02:00 -04005369 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005370 }
Chris Mason39279cc2007-06-12 06:35:45 -04005371 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005372 if (BTRFS_I(inode)->delayed_node)
5373 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005374
5375 return ret;
5376}
5377
5378/*
5379 * This is a copy of file_update_time. We need this so we can return error on
5380 * ENOSPC for updating the inode in the case of file write and mmap writes.
5381 */
Josef Bacike41f9412012-03-26 09:46:47 -04005382static int btrfs_update_time(struct inode *inode, struct timespec *now,
5383 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005384{
Alexander Block2bc5565282012-06-15 09:49:33 +02005385 struct btrfs_root *root = BTRFS_I(inode)->root;
5386
5387 if (btrfs_root_readonly(root))
5388 return -EROFS;
5389
Josef Bacike41f9412012-03-26 09:46:47 -04005390 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005391 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005392 if (flags & S_CTIME)
5393 inode->i_ctime = *now;
5394 if (flags & S_MTIME)
5395 inode->i_mtime = *now;
5396 if (flags & S_ATIME)
5397 inode->i_atime = *now;
5398 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005399}
5400
Chris Masond352ac62008-09-29 15:18:18 -04005401/*
5402 * find the highest existing sequence number in a directory
5403 * and then set the in-memory index_cnt variable to reflect
5404 * free sequence numbers
5405 */
Josef Bacikaec74772008-07-24 12:12:38 -04005406static int btrfs_set_inode_index_count(struct inode *inode)
5407{
5408 struct btrfs_root *root = BTRFS_I(inode)->root;
5409 struct btrfs_key key, found_key;
5410 struct btrfs_path *path;
5411 struct extent_buffer *leaf;
5412 int ret;
5413
Li Zefan33345d012011-04-20 10:31:50 +08005414 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005415 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5416 key.offset = (u64)-1;
5417
5418 path = btrfs_alloc_path();
5419 if (!path)
5420 return -ENOMEM;
5421
5422 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5423 if (ret < 0)
5424 goto out;
5425 /* FIXME: we should be able to handle this */
5426 if (ret == 0)
5427 goto out;
5428 ret = 0;
5429
5430 /*
5431 * MAGIC NUMBER EXPLANATION:
5432 * since we search a directory based on f_pos we have to start at 2
5433 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5434 * else has to start at 2
5435 */
5436 if (path->slots[0] == 0) {
5437 BTRFS_I(inode)->index_cnt = 2;
5438 goto out;
5439 }
5440
5441 path->slots[0]--;
5442
5443 leaf = path->nodes[0];
5444 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5445
Li Zefan33345d012011-04-20 10:31:50 +08005446 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005447 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5448 BTRFS_I(inode)->index_cnt = 2;
5449 goto out;
5450 }
5451
5452 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5453out:
5454 btrfs_free_path(path);
5455 return ret;
5456}
5457
Chris Masond352ac62008-09-29 15:18:18 -04005458/*
5459 * helper to find a free sequence number in a given directory. This current
5460 * code is very simple, later versions will do smarter things in the btree
5461 */
Chris Mason3de45862008-11-17 21:02:50 -05005462int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005463{
5464 int ret = 0;
5465
5466 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005467 ret = btrfs_inode_delayed_dir_index_count(dir);
5468 if (ret) {
5469 ret = btrfs_set_inode_index_count(dir);
5470 if (ret)
5471 return ret;
5472 }
Josef Bacikaec74772008-07-24 12:12:38 -04005473 }
5474
Chris Mason00e4e6b2008-08-05 11:18:09 -04005475 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005476 BTRFS_I(dir)->index_cnt++;
5477
5478 return ret;
5479}
5480
Chris Mason39279cc2007-06-12 06:35:45 -04005481static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5482 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005483 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005484 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005485 u64 ref_objectid, u64 objectid,
5486 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005487{
5488 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005489 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005490 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005491 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005492 struct btrfs_inode_ref *ref;
5493 struct btrfs_key key[2];
5494 u32 sizes[2];
5495 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005496 int ret;
5497 int owner;
5498
Chris Mason5f39d392007-10-15 16:14:19 -04005499 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005500 if (!path)
5501 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005502
Chris Mason39279cc2007-06-12 06:35:45 -04005503 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005504 if (!inode) {
5505 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005506 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005507 }
Chris Mason39279cc2007-06-12 06:35:45 -04005508
Li Zefan581bb052011-04-20 10:06:11 +08005509 /*
5510 * we have to initialize this early, so we can reclaim the inode
5511 * number if we fail afterwards in this function.
5512 */
5513 inode->i_ino = objectid;
5514
Josef Bacikaec74772008-07-24 12:12:38 -04005515 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005516 trace_btrfs_inode_request(dir);
5517
Chris Mason3de45862008-11-17 21:02:50 -05005518 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005519 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005520 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005521 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005522 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005523 }
Josef Bacikaec74772008-07-24 12:12:38 -04005524 }
5525 /*
5526 * index_cnt is ignored for everything but a dir,
5527 * btrfs_get_inode_index_count has an explanation for the magic
5528 * number
5529 */
5530 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005531 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005532 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005533 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005534
Josef Bacik5dc562c2012-08-17 13:14:17 -04005535 /*
5536 * We could have gotten an inode number from somebody who was fsynced
5537 * and then removed in this same transaction, so let's just set full
5538 * sync since it will be a full sync anyway and this will blow away the
5539 * old info in the log.
5540 */
5541 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5542
Al Viro569254b2011-07-24 17:08:40 -04005543 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005544 owner = 0;
5545 else
5546 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005547
5548 key[0].objectid = objectid;
5549 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5550 key[0].offset = 0;
5551
Mark Fashehf1863732012-08-08 11:32:27 -07005552 /*
5553 * Start new inodes with an inode_ref. This is slightly more
5554 * efficient for small numbers of hard links since they will
5555 * be packed into one item. Extended refs will kick in if we
5556 * add more hard links than can fit in the ref item.
5557 */
Chris Mason9c583092008-01-29 15:15:18 -05005558 key[1].objectid = objectid;
5559 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5560 key[1].offset = ref_objectid;
5561
5562 sizes[0] = sizeof(struct btrfs_inode_item);
5563 sizes[1] = name_len + sizeof(*ref);
5564
Chris Masonb9473432009-03-13 11:00:37 -04005565 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005566 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5567 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005568 goto fail;
5569
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005570 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005571 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005572 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005573 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5574 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005575 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5576 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005577 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005578
5579 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5580 struct btrfs_inode_ref);
5581 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005582 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005583 ptr = (unsigned long)(ref + 1);
5584 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5585
Chris Mason5f39d392007-10-15 16:14:19 -04005586 btrfs_mark_buffer_dirty(path->nodes[0]);
5587 btrfs_free_path(path);
5588
Chris Mason39279cc2007-06-12 06:35:45 -04005589 location = &BTRFS_I(inode)->location;
5590 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005591 location->offset = 0;
5592 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5593
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005594 btrfs_inherit_iflags(inode, dir);
5595
Al Viro569254b2011-07-24 17:08:40 -04005596 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005597 if (btrfs_test_opt(root, NODATASUM))
5598 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005599 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005600 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5601 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005602 }
5603
Chris Mason39279cc2007-06-12 06:35:45 -04005604 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005606
5607 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005608 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005609
Alexander Block8ea05e32012-07-25 17:35:53 +02005610 btrfs_update_root_times(trans, root);
5611
Chris Mason39279cc2007-06-12 06:35:45 -04005612 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005613fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005614 if (dir)
5615 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005616 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005617 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005618 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005619}
5620
5621static inline u8 btrfs_inode_type(struct inode *inode)
5622{
5623 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5624}
5625
Chris Masond352ac62008-09-29 15:18:18 -04005626/*
5627 * utility function to add 'inode' into 'parent_inode' with
5628 * a give name and a given sequence number.
5629 * if 'add_backref' is true, also insert a backref from the
5630 * inode to the parent directory.
5631 */
Chris Masone02119d2008-09-05 16:13:11 -04005632int btrfs_add_link(struct btrfs_trans_handle *trans,
5633 struct inode *parent_inode, struct inode *inode,
5634 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005635{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005636 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005637 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005638 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005639 u64 ino = btrfs_ino(inode);
5640 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005641
Li Zefan33345d012011-04-20 10:31:50 +08005642 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005643 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5644 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005645 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005646 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5647 key.offset = 0;
5648 }
Chris Mason39279cc2007-06-12 06:35:45 -04005649
Li Zefan33345d012011-04-20 10:31:50 +08005650 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5652 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005653 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005654 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005655 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5656 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005657 }
5658
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005659 /* Nothing to clean up yet */
5660 if (ret)
5661 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005662
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005663 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5664 parent_inode, &key,
5665 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005666 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005667 goto fail_dir_item;
5668 else if (ret) {
5669 btrfs_abort_transaction(trans, root, ret);
5670 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005671 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005672
5673 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5674 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005675 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005676 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5677 ret = btrfs_update_inode(trans, root, parent_inode);
5678 if (ret)
5679 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005680 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005681
5682fail_dir_item:
5683 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5684 u64 local_index;
5685 int err;
5686 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5687 key.objectid, root->root_key.objectid,
5688 parent_ino, &local_index, name, name_len);
5689
5690 } else if (add_backref) {
5691 u64 local_index;
5692 int err;
5693
5694 err = btrfs_del_inode_ref(trans, root, name, name_len,
5695 ino, parent_ino, &local_index);
5696 }
5697 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005698}
5699
5700static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005701 struct inode *dir, struct dentry *dentry,
5702 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005703{
Josef Bacika1b075d2010-11-19 20:36:11 +00005704 int err = btrfs_add_link(trans, dir, inode,
5705 dentry->d_name.name, dentry->d_name.len,
5706 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005707 if (err > 0)
5708 err = -EEXIST;
5709 return err;
5710}
5711
Josef Bacik618e21d2007-07-11 10:18:17 -04005712static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005713 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005714{
5715 struct btrfs_trans_handle *trans;
5716 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005717 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005718 int err;
5719 int drop_inode = 0;
5720 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005721 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005722
5723 if (!new_valid_dev(rdev))
5724 return -EINVAL;
5725
Josef Bacik9ed74f22009-09-11 16:12:44 -04005726 /*
5727 * 2 for inode item and ref
5728 * 2 for dir items
5729 * 1 for xattr if selinux is on
5730 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005731 trans = btrfs_start_transaction(root, 5);
5732 if (IS_ERR(trans))
5733 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005734
Li Zefan581bb052011-04-20 10:06:11 +08005735 err = btrfs_find_free_ino(root, &objectid);
5736 if (err)
5737 goto out_unlock;
5738
Josef Bacikaec74772008-07-24 12:12:38 -04005739 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005740 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005741 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005742 if (IS_ERR(inode)) {
5743 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005744 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005745 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005746
Eric Paris2a7dba32011-02-01 11:05:39 -05005747 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005748 if (err) {
5749 drop_inode = 1;
5750 goto out_unlock;
5751 }
5752
Casey Schauflerad19db72011-12-15 10:09:07 -05005753 /*
5754 * If the active LSM wants to access the inode during
5755 * d_instantiate it needs these. Smack checks to see
5756 * if the filesystem supports xattrs by looking at the
5757 * ops vector.
5758 */
5759
5760 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005761 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005762 if (err)
5763 drop_inode = 1;
5764 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005765 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005766 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005767 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005768 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005769out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005770 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005771 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005772 if (drop_inode) {
5773 inode_dec_link_count(inode);
5774 iput(inode);
5775 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005776 return err;
5777}
5778
Chris Mason39279cc2007-06-12 06:35:45 -04005779static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005780 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005781{
5782 struct btrfs_trans_handle *trans;
5783 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005784 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005785 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005786 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005787 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005788 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005789
Josef Bacik9ed74f22009-09-11 16:12:44 -04005790 /*
5791 * 2 for inode item and ref
5792 * 2 for dir items
5793 * 1 for xattr if selinux is on
5794 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005795 trans = btrfs_start_transaction(root, 5);
5796 if (IS_ERR(trans))
5797 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005798
Li Zefan581bb052011-04-20 10:06:11 +08005799 err = btrfs_find_free_ino(root, &objectid);
5800 if (err)
5801 goto out_unlock;
5802
Josef Bacikaec74772008-07-24 12:12:38 -04005803 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005804 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005805 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005806 if (IS_ERR(inode)) {
5807 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005808 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005809 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005810 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005811
Eric Paris2a7dba32011-02-01 11:05:39 -05005812 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005813 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005814 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005815
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005816 err = btrfs_update_inode(trans, root, inode);
5817 if (err)
5818 goto out_unlock;
5819
Casey Schauflerad19db72011-12-15 10:09:07 -05005820 /*
5821 * If the active LSM wants to access the inode during
5822 * d_instantiate it needs these. Smack checks to see
5823 * if the filesystem supports xattrs by looking at the
5824 * ops vector.
5825 */
5826 inode->i_fop = &btrfs_file_operations;
5827 inode->i_op = &btrfs_file_inode_operations;
5828
Josef Bacika1b075d2010-11-19 20:36:11 +00005829 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005830 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005831 goto out_unlock;
5832
5833 inode->i_mapping->a_ops = &btrfs_aops;
5834 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5835 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5836 d_instantiate(dentry, inode);
5837
Chris Mason39279cc2007-06-12 06:35:45 -04005838out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005839 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005840 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005841 inode_dec_link_count(inode);
5842 iput(inode);
5843 }
Liu Bob53d3f52012-11-14 14:34:34 +00005844 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005845 return err;
5846}
5847
5848static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5849 struct dentry *dentry)
5850{
5851 struct btrfs_trans_handle *trans;
5852 struct btrfs_root *root = BTRFS_I(dir)->root;
5853 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005854 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005855 int err;
5856 int drop_inode = 0;
5857
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005858 /* do not allow sys_link's with other subvols of the same device */
5859 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005860 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005861
Mark Fashehf1863732012-08-08 11:32:27 -07005862 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005863 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005864
Chris Mason3de45862008-11-17 21:02:50 -05005865 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005866 if (err)
5867 goto fail;
5868
Yan, Zhenga22285a2010-05-16 10:48:46 -04005869 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005870 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005871 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005872 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005873 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005874 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005875 if (IS_ERR(trans)) {
5876 err = PTR_ERR(trans);
5877 goto fail;
5878 }
Chris Mason5f39d392007-10-15 16:14:19 -04005879
Miao Xie31534952011-04-13 13:19:21 +08005880 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005881 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005882 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005883 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005884 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005885
Josef Bacika1b075d2010-11-19 20:36:11 +00005886 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005887
Yan, Zhenga5719522009-09-24 09:17:31 -04005888 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005889 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005890 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005891 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005892 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005893 if (err)
5894 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005895 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005896 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005897 }
Chris Mason39279cc2007-06-12 06:35:45 -04005898
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005899 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005900fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005901 if (drop_inode) {
5902 inode_dec_link_count(inode);
5903 iput(inode);
5904 }
Liu Bob53d3f52012-11-14 14:34:34 +00005905 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005906 return err;
5907}
5908
Al Viro18bb1db2011-07-26 01:41:39 -04005909static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005910{
Chris Masonb9d86662008-05-02 16:13:49 -04005911 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 struct btrfs_trans_handle *trans;
5913 struct btrfs_root *root = BTRFS_I(dir)->root;
5914 int err = 0;
5915 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005916 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005917 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005918
Josef Bacik9ed74f22009-09-11 16:12:44 -04005919 /*
5920 * 2 items for inode and ref
5921 * 2 items for dir items
5922 * 1 for xattr if selinux is on
5923 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005924 trans = btrfs_start_transaction(root, 5);
5925 if (IS_ERR(trans))
5926 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005927
Li Zefan581bb052011-04-20 10:06:11 +08005928 err = btrfs_find_free_ino(root, &objectid);
5929 if (err)
5930 goto out_fail;
5931
Josef Bacikaec74772008-07-24 12:12:38 -04005932 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005933 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005934 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005935 if (IS_ERR(inode)) {
5936 err = PTR_ERR(inode);
5937 goto out_fail;
5938 }
Chris Mason5f39d392007-10-15 16:14:19 -04005939
Chris Mason39279cc2007-06-12 06:35:45 -04005940 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005941
Eric Paris2a7dba32011-02-01 11:05:39 -05005942 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005943 if (err)
5944 goto out_fail;
5945
Chris Mason39279cc2007-06-12 06:35:45 -04005946 inode->i_op = &btrfs_dir_inode_operations;
5947 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005948
Chris Masondbe674a2008-07-17 12:54:05 -04005949 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005950 err = btrfs_update_inode(trans, root, inode);
5951 if (err)
5952 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005953
Josef Bacika1b075d2010-11-19 20:36:11 +00005954 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5955 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005956 if (err)
5957 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005958
Chris Mason39279cc2007-06-12 06:35:45 -04005959 d_instantiate(dentry, inode);
5960 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005961
5962out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005963 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005964 if (drop_on_err)
5965 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005966 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005967 return err;
5968}
5969
Chris Masond352ac62008-09-29 15:18:18 -04005970/* helper for btfs_get_extent. Given an existing extent in the tree,
5971 * and an extent that you want to insert, deal with overlap and insert
5972 * the new extent into the tree.
5973 */
Chris Mason3b951512008-04-17 11:29:12 -04005974static int merge_extent_mapping(struct extent_map_tree *em_tree,
5975 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005976 struct extent_map *em,
5977 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005978{
5979 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005980
Chris Masone6dcd2d2008-07-17 12:53:50 -04005981 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5982 start_diff = map_start - em->start;
5983 em->start = map_start;
5984 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005985 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5986 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005987 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005988 em->block_len -= start_diff;
5989 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005990 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005991}
5992
Chris Masonc8b97812008-10-29 14:49:59 -04005993static noinline int uncompress_inline(struct btrfs_path *path,
5994 struct inode *inode, struct page *page,
5995 size_t pg_offset, u64 extent_offset,
5996 struct btrfs_file_extent_item *item)
5997{
5998 int ret;
5999 struct extent_buffer *leaf = path->nodes[0];
6000 char *tmp;
6001 size_t max_size;
6002 unsigned long inline_size;
6003 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006004 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006005
6006 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006007 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006008 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6009 inline_size = btrfs_file_extent_inline_item_len(leaf,
6010 btrfs_item_nr(leaf, path->slots[0]));
6011 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006012 if (!tmp)
6013 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006014 ptr = btrfs_file_extent_inline_start(item);
6015
6016 read_extent_buffer(leaf, tmp, ptr, inline_size);
6017
Chris Mason5b050f02008-11-11 09:34:41 -05006018 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006019 ret = btrfs_decompress(compress_type, tmp, page,
6020 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006021 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006022 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006023 unsigned long copy_size = min_t(u64,
6024 PAGE_CACHE_SIZE - pg_offset,
6025 max_size - extent_offset);
6026 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006027 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006028 }
6029 kfree(tmp);
6030 return 0;
6031}
6032
Chris Masond352ac62008-09-29 15:18:18 -04006033/*
6034 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006035 * the ugly parts come from merging extents from the disk with the in-ram
6036 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006037 * where the in-ram extents might be locked pending data=ordered completion.
6038 *
6039 * This also copies inline extents directly into the page.
6040 */
Chris Masond3977122009-01-05 21:25:51 -05006041
Chris Masona52d9a82007-08-27 16:49:44 -04006042struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006043 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006044 int create)
6045{
6046 int ret;
6047 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006048 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006049 u64 extent_start = 0;
6050 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006051 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006052 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006053 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006054 struct btrfs_root *root = BTRFS_I(inode)->root;
6055 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006056 struct extent_buffer *leaf;
6057 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006058 struct extent_map *em = NULL;
6059 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006060 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006061 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006062 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006063
Chris Masona52d9a82007-08-27 16:49:44 -04006064again:
Chris Mason890871b2009-09-02 16:24:52 -04006065 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006066 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006067 if (em)
6068 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006069 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006070
Chris Masona52d9a82007-08-27 16:49:44 -04006071 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006072 if (em->start > start || em->start + em->len <= start)
6073 free_extent_map(em);
6074 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006075 free_extent_map(em);
6076 else
6077 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006078 }
David Sterba172ddd62011-04-21 00:48:27 +02006079 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006080 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006081 err = -ENOMEM;
6082 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006083 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006084 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006085 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006086 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006087 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006088 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006089
6090 if (!path) {
6091 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006092 if (!path) {
6093 err = -ENOMEM;
6094 goto out;
6095 }
6096 /*
6097 * Chances are we'll be called again, so go ahead and do
6098 * readahead
6099 */
6100 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006101 }
6102
Chris Mason179e29e2007-11-01 11:28:41 -04006103 ret = btrfs_lookup_file_extent(trans, root, path,
6104 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006105 if (ret < 0) {
6106 err = ret;
6107 goto out;
6108 }
6109
6110 if (ret != 0) {
6111 if (path->slots[0] == 0)
6112 goto not_found;
6113 path->slots[0]--;
6114 }
6115
Chris Mason5f39d392007-10-15 16:14:19 -04006116 leaf = path->nodes[0];
6117 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006118 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006119 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006120 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6121 found_type = btrfs_key_type(&found_key);
6122 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006123 found_type != BTRFS_EXTENT_DATA_KEY) {
6124 goto not_found;
6125 }
6126
Chris Mason5f39d392007-10-15 16:14:19 -04006127 found_type = btrfs_file_extent_type(leaf, item);
6128 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006129 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006130 if (found_type == BTRFS_FILE_EXTENT_REG ||
6131 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006132 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006133 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006134 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6135 size_t size;
6136 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006137 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006138 }
6139
6140 if (start >= extent_end) {
6141 path->slots[0]++;
6142 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6143 ret = btrfs_next_leaf(root, path);
6144 if (ret < 0) {
6145 err = ret;
6146 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006147 }
Yan Zheng9036c102008-10-30 14:19:41 -04006148 if (ret > 0)
6149 goto not_found;
6150 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006151 }
Yan Zheng9036c102008-10-30 14:19:41 -04006152 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6153 if (found_key.objectid != objectid ||
6154 found_key.type != BTRFS_EXTENT_DATA_KEY)
6155 goto not_found;
6156 if (start + len <= found_key.offset)
6157 goto not_found;
6158 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006159 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006160 em->len = found_key.offset - start;
6161 goto not_found_em;
6162 }
6163
Josef Bacikcc95bef2013-04-04 14:31:27 -04006164 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006165 if (found_type == BTRFS_FILE_EXTENT_REG ||
6166 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006167 em->start = extent_start;
6168 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006169 em->orig_start = extent_start -
6170 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006171 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6172 item);
Chris Masondb945352007-10-15 16:15:53 -04006173 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6174 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006175 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006176 goto insert;
6177 }
Li Zefan261507a02010-12-17 14:21:50 +08006178 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006179 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006180 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006181 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006182 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006183 } else {
6184 bytenr += btrfs_file_extent_offset(leaf, item);
6185 em->block_start = bytenr;
6186 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006187 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6188 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006189 }
Chris Masona52d9a82007-08-27 16:49:44 -04006190 goto insert;
6191 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006192 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006193 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006194 size_t size;
6195 size_t extent_offset;
6196 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006197
Chris Masona52d9a82007-08-27 16:49:44 -04006198 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006199 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006200 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006201 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006202 goto out;
6203 }
6204
Yan Zheng9036c102008-10-30 14:19:41 -04006205 size = btrfs_file_extent_inline_len(leaf, item);
6206 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006207 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006208 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006209 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006210 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006211 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006212 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006213 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006214 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006215 em->compress_type = compress_type;
6216 }
Yan689f9342007-10-29 11:41:07 -04006217 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006218 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006219 if (btrfs_file_extent_compression(leaf, item) !=
6220 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006221 ret = uncompress_inline(path, inode, page,
6222 pg_offset,
6223 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006224 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006225 } else {
6226 map = kmap(page);
6227 read_extent_buffer(leaf, map + pg_offset, ptr,
6228 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006229 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6230 memset(map + pg_offset + copy_size, 0,
6231 PAGE_CACHE_SIZE - pg_offset -
6232 copy_size);
6233 }
Chris Masonc8b97812008-10-29 14:49:59 -04006234 kunmap(page);
6235 }
Chris Mason179e29e2007-11-01 11:28:41 -04006236 flush_dcache_page(page);
6237 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006238 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006239 if (!trans) {
6240 kunmap(page);
6241 free_extent_map(em);
6242 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006243
David Sterbab3b4aa72011-04-21 01:20:15 +02006244 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006245 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006246
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006247 if (IS_ERR(trans))
6248 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006249 goto again;
6250 }
Chris Masonc8b97812008-10-29 14:49:59 -04006251 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006252 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006253 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006254 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006255 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006256 }
Chris Masond1310b22008-01-24 16:13:08 -05006257 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006258 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006259 goto insert;
6260 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006261 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006262 }
6263not_found:
6264 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006265 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006266 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006267not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006268 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006269 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006270insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006271 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006272 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006273 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6274 (unsigned long long)em->start,
6275 (unsigned long long)em->len,
6276 (unsigned long long)start,
6277 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006278 err = -EIO;
6279 goto out;
6280 }
Chris Masond1310b22008-01-24 16:13:08 -05006281
6282 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006283 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006284 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006285 /* it is possible that someone inserted the extent into the tree
6286 * while we had the lock dropped. It is also possible that
6287 * an overlapping map exists in the tree
6288 */
Chris Masona52d9a82007-08-27 16:49:44 -04006289 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006290 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006291
6292 ret = 0;
6293
Chris Mason3b951512008-04-17 11:29:12 -04006294 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006295 if (existing && (existing->start > start ||
6296 existing->start + existing->len <= start)) {
6297 free_extent_map(existing);
6298 existing = NULL;
6299 }
Chris Mason3b951512008-04-17 11:29:12 -04006300 if (!existing) {
6301 existing = lookup_extent_mapping(em_tree, em->start,
6302 em->len);
6303 if (existing) {
6304 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006305 em, start,
6306 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006307 free_extent_map(existing);
6308 if (err) {
6309 free_extent_map(em);
6310 em = NULL;
6311 }
6312 } else {
6313 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006314 free_extent_map(em);
6315 em = NULL;
6316 }
6317 } else {
6318 free_extent_map(em);
6319 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006320 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006321 }
Chris Masona52d9a82007-08-27 16:49:44 -04006322 }
Chris Mason890871b2009-09-02 16:24:52 -04006323 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006324out:
liubo1abe9b82011-03-24 11:18:59 +00006325
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006326 if (em)
6327 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006328
Chris Masonf4219502008-07-22 11:18:09 -04006329 if (path)
6330 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006331 if (trans) {
6332 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006333 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006334 err = ret;
6335 }
Chris Masona52d9a82007-08-27 16:49:44 -04006336 if (err) {
6337 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006338 return ERR_PTR(err);
6339 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006340 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006341 return em;
6342}
6343
Chris Masonec29ed52011-02-23 16:23:20 -05006344struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6345 size_t pg_offset, u64 start, u64 len,
6346 int create)
6347{
6348 struct extent_map *em;
6349 struct extent_map *hole_em = NULL;
6350 u64 range_start = start;
6351 u64 end;
6352 u64 found;
6353 u64 found_end;
6354 int err = 0;
6355
6356 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6357 if (IS_ERR(em))
6358 return em;
6359 if (em) {
6360 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006361 * if our em maps to
6362 * - a hole or
6363 * - a pre-alloc extent,
6364 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006365 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006366 if (em->block_start != EXTENT_MAP_HOLE &&
6367 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006368 return em;
6369 else
6370 hole_em = em;
6371 }
6372
6373 /* check to see if we've wrapped (len == -1 or similar) */
6374 end = start + len;
6375 if (end < start)
6376 end = (u64)-1;
6377 else
6378 end -= 1;
6379
6380 em = NULL;
6381
6382 /* ok, we didn't find anything, lets look for delalloc */
6383 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6384 end, len, EXTENT_DELALLOC, 1);
6385 found_end = range_start + found;
6386 if (found_end < range_start)
6387 found_end = (u64)-1;
6388
6389 /*
6390 * we didn't find anything useful, return
6391 * the original results from get_extent()
6392 */
6393 if (range_start > end || found_end <= start) {
6394 em = hole_em;
6395 hole_em = NULL;
6396 goto out;
6397 }
6398
6399 /* adjust the range_start to make sure it doesn't
6400 * go backwards from the start they passed in
6401 */
6402 range_start = max(start,range_start);
6403 found = found_end - range_start;
6404
6405 if (found > 0) {
6406 u64 hole_start = start;
6407 u64 hole_len = len;
6408
David Sterba172ddd62011-04-21 00:48:27 +02006409 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006410 if (!em) {
6411 err = -ENOMEM;
6412 goto out;
6413 }
6414 /*
6415 * when btrfs_get_extent can't find anything it
6416 * returns one huge hole
6417 *
6418 * make sure what it found really fits our range, and
6419 * adjust to make sure it is based on the start from
6420 * the caller
6421 */
6422 if (hole_em) {
6423 u64 calc_end = extent_map_end(hole_em);
6424
6425 if (calc_end <= start || (hole_em->start > end)) {
6426 free_extent_map(hole_em);
6427 hole_em = NULL;
6428 } else {
6429 hole_start = max(hole_em->start, start);
6430 hole_len = calc_end - hole_start;
6431 }
6432 }
6433 em->bdev = NULL;
6434 if (hole_em && range_start > hole_start) {
6435 /* our hole starts before our delalloc, so we
6436 * have to return just the parts of the hole
6437 * that go until the delalloc starts
6438 */
6439 em->len = min(hole_len,
6440 range_start - hole_start);
6441 em->start = hole_start;
6442 em->orig_start = hole_start;
6443 /*
6444 * don't adjust block start at all,
6445 * it is fixed at EXTENT_MAP_HOLE
6446 */
6447 em->block_start = hole_em->block_start;
6448 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006449 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6450 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006451 } else {
6452 em->start = range_start;
6453 em->len = found;
6454 em->orig_start = range_start;
6455 em->block_start = EXTENT_MAP_DELALLOC;
6456 em->block_len = found;
6457 }
6458 } else if (hole_em) {
6459 return hole_em;
6460 }
6461out:
6462
6463 free_extent_map(hole_em);
6464 if (err) {
6465 free_extent_map(em);
6466 return ERR_PTR(err);
6467 }
6468 return em;
6469}
6470
Josef Bacik4b46fce2010-05-23 11:00:55 -04006471static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6472 u64 start, u64 len)
6473{
6474 struct btrfs_root *root = BTRFS_I(inode)->root;
6475 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006476 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006477 struct btrfs_key ins;
6478 u64 alloc_hint;
6479 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006480
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006481 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006482 if (IS_ERR(trans))
6483 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006484
6485 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6486
6487 alloc_hint = get_extent_allocation_hint(inode, start, len);
6488 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006489 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006490 if (ret) {
6491 em = ERR_PTR(ret);
6492 goto out;
6493 }
6494
Josef Bacik70c8a912012-10-11 16:54:30 -04006495 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006496 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006497 if (IS_ERR(em))
6498 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006499
6500 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6501 ins.offset, ins.offset, 0);
6502 if (ret) {
6503 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6504 em = ERR_PTR(ret);
6505 }
6506out:
6507 btrfs_end_transaction(trans, root);
6508 return em;
6509}
6510
Chris Mason46bfbb52010-05-26 11:04:10 -04006511/*
6512 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6513 * block must be cow'd
6514 */
6515static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006516 struct inode *inode, u64 offset, u64 *len,
6517 u64 *orig_start, u64 *orig_block_len,
6518 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006519{
6520 struct btrfs_path *path;
6521 int ret;
6522 struct extent_buffer *leaf;
6523 struct btrfs_root *root = BTRFS_I(inode)->root;
6524 struct btrfs_file_extent_item *fi;
6525 struct btrfs_key key;
6526 u64 disk_bytenr;
6527 u64 backref_offset;
6528 u64 extent_end;
6529 u64 num_bytes;
6530 int slot;
6531 int found_type;
6532
6533 path = btrfs_alloc_path();
6534 if (!path)
6535 return -ENOMEM;
6536
Li Zefan33345d012011-04-20 10:31:50 +08006537 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006538 offset, 0);
6539 if (ret < 0)
6540 goto out;
6541
6542 slot = path->slots[0];
6543 if (ret == 1) {
6544 if (slot == 0) {
6545 /* can't find the item, must cow */
6546 ret = 0;
6547 goto out;
6548 }
6549 slot--;
6550 }
6551 ret = 0;
6552 leaf = path->nodes[0];
6553 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006554 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006555 key.type != BTRFS_EXTENT_DATA_KEY) {
6556 /* not our file or wrong item type, must cow */
6557 goto out;
6558 }
6559
6560 if (key.offset > offset) {
6561 /* Wrong offset, must cow */
6562 goto out;
6563 }
6564
6565 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6566 found_type = btrfs_file_extent_type(leaf, fi);
6567 if (found_type != BTRFS_FILE_EXTENT_REG &&
6568 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6569 /* not a regular extent, must cow */
6570 goto out;
6571 }
6572 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6573 backref_offset = btrfs_file_extent_offset(leaf, fi);
6574
Josef Bacikeb384b52013-04-24 16:32:55 -04006575 *orig_start = key.offset - backref_offset;
6576 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6577 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6578
Chris Mason46bfbb52010-05-26 11:04:10 -04006579 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006580 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006581 /* extent doesn't include our full range, must cow */
6582 goto out;
6583 }
6584
6585 if (btrfs_extent_readonly(root, disk_bytenr))
6586 goto out;
6587
6588 /*
6589 * look for other files referencing this extent, if we
6590 * find any we must cow
6591 */
Li Zefan33345d012011-04-20 10:31:50 +08006592 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006593 key.offset - backref_offset, disk_bytenr))
6594 goto out;
6595
6596 /*
6597 * adjust disk_bytenr and num_bytes to cover just the bytes
6598 * in this extent we are about to write. If there
6599 * are any csums in that range we have to cow in order
6600 * to keep the csums correct
6601 */
6602 disk_bytenr += backref_offset;
6603 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006604 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006605 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6606 goto out;
6607 /*
6608 * all of the above have passed, it is safe to overwrite this extent
6609 * without cow
6610 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006611 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006612 ret = 1;
6613out:
6614 btrfs_free_path(path);
6615 return ret;
6616}
6617
Josef Bacikeb838e72012-07-31 16:28:48 -04006618static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6619 struct extent_state **cached_state, int writing)
6620{
6621 struct btrfs_ordered_extent *ordered;
6622 int ret = 0;
6623
6624 while (1) {
6625 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6626 0, cached_state);
6627 /*
6628 * We're concerned with the entire range that we're going to be
6629 * doing DIO to, so we need to make sure theres no ordered
6630 * extents in this range.
6631 */
6632 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6633 lockend - lockstart + 1);
6634
6635 /*
6636 * We need to make sure there are no buffered pages in this
6637 * range either, we could have raced between the invalidate in
6638 * generic_file_direct_write and locking the extent. The
6639 * invalidate needs to happen so that reads after a write do not
6640 * get stale data.
6641 */
6642 if (!ordered && (!writing ||
6643 !test_range_bit(&BTRFS_I(inode)->io_tree,
6644 lockstart, lockend, EXTENT_UPTODATE, 0,
6645 *cached_state)))
6646 break;
6647
6648 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6649 cached_state, GFP_NOFS);
6650
6651 if (ordered) {
6652 btrfs_start_ordered_extent(inode, ordered, 1);
6653 btrfs_put_ordered_extent(ordered);
6654 } else {
6655 /* Screw you mmap */
6656 ret = filemap_write_and_wait_range(inode->i_mapping,
6657 lockstart,
6658 lockend);
6659 if (ret)
6660 break;
6661
6662 /*
6663 * If we found a page that couldn't be invalidated just
6664 * fall back to buffered.
6665 */
6666 ret = invalidate_inode_pages2_range(inode->i_mapping,
6667 lockstart >> PAGE_CACHE_SHIFT,
6668 lockend >> PAGE_CACHE_SHIFT);
6669 if (ret)
6670 break;
6671 }
6672
6673 cond_resched();
6674 }
6675
6676 return ret;
6677}
6678
Josef Bacik69ffb542012-09-11 15:40:07 -04006679static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6680 u64 len, u64 orig_start,
6681 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006682 u64 orig_block_len, u64 ram_bytes,
6683 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006684{
6685 struct extent_map_tree *em_tree;
6686 struct extent_map *em;
6687 struct btrfs_root *root = BTRFS_I(inode)->root;
6688 int ret;
6689
6690 em_tree = &BTRFS_I(inode)->extent_tree;
6691 em = alloc_extent_map();
6692 if (!em)
6693 return ERR_PTR(-ENOMEM);
6694
6695 em->start = start;
6696 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006697 em->mod_start = start;
6698 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006699 em->len = len;
6700 em->block_len = block_len;
6701 em->block_start = block_start;
6702 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006703 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006704 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006705 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006706 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6707 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006708 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006709
6710 do {
6711 btrfs_drop_extent_cache(inode, em->start,
6712 em->start + em->len - 1, 0);
6713 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006714 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006715 write_unlock(&em_tree->lock);
6716 } while (ret == -EEXIST);
6717
6718 if (ret) {
6719 free_extent_map(em);
6720 return ERR_PTR(ret);
6721 }
6722
6723 return em;
6724}
6725
6726
Josef Bacik4b46fce2010-05-23 11:00:55 -04006727static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6728 struct buffer_head *bh_result, int create)
6729{
6730 struct extent_map *em;
6731 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006732 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006734 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006735 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006736 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006737 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006738 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006739
Miao Xie172a5042013-02-21 02:48:22 -07006740 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006741 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006742 else
Josef Bacikc3298612012-08-03 16:49:19 -04006743 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006744
Josef Bacikc3298612012-08-03 16:49:19 -04006745 lockstart = start;
6746 lockend = start + len - 1;
6747
Josef Bacikeb838e72012-07-31 16:28:48 -04006748 /*
6749 * If this errors out it's because we couldn't invalidate pagecache for
6750 * this range and we need to fallback to buffered.
6751 */
6752 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6753 return -ENOTBLK;
6754
Josef Bacik4b46fce2010-05-23 11:00:55 -04006755 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006756 if (IS_ERR(em)) {
6757 ret = PTR_ERR(em);
6758 goto unlock_err;
6759 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006760
6761 /*
6762 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6763 * io. INLINE is special, and we could probably kludge it in here, but
6764 * it's still buffered so for safety lets just fall back to the generic
6765 * buffered path.
6766 *
6767 * For COMPRESSED we _have_ to read the entire extent in so we can
6768 * decompress it, so there will be buffering required no matter what we
6769 * do, so go ahead and fallback to buffered.
6770 *
6771 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6772 * to buffered IO. Don't blame me, this is the price we pay for using
6773 * the generic code.
6774 */
6775 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6776 em->block_start == EXTENT_MAP_INLINE) {
6777 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006778 ret = -ENOTBLK;
6779 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006780 }
6781
6782 /* Just a good old fashioned hole, return */
6783 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6784 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6785 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006786 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006787 }
6788
6789 /*
6790 * We don't allocate a new extent in the following cases
6791 *
6792 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6793 * existing extent.
6794 * 2) The extent is marked as PREALLOC. We're good to go here and can
6795 * just use the extent.
6796 *
6797 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006798 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006799 len = min(len, em->len - (start - em->start));
6800 lockstart = start + len;
6801 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006802 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006803
6804 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6805 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6806 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006807 int type;
6808 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006809 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006810
6811 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6812 type = BTRFS_ORDERED_PREALLOC;
6813 else
6814 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006815 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006816 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006817
6818 /*
6819 * we're not going to log anything, but we do need
6820 * to make sure the current transaction stays open
6821 * while we look for nocow cross refs
6822 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006823 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006824 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006825 goto must_cow;
6826
Josef Bacikeb384b52013-04-24 16:32:55 -04006827 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6828 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006829 if (type == BTRFS_ORDERED_PREALLOC) {
6830 free_extent_map(em);
6831 em = create_pinned_em(inode, start, len,
6832 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006833 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006834 orig_block_len,
6835 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006836 if (IS_ERR(em)) {
6837 btrfs_end_transaction(trans, root);
6838 goto unlock_err;
6839 }
6840 }
6841
Chris Mason46bfbb52010-05-26 11:04:10 -04006842 ret = btrfs_add_ordered_extent_dio(inode, start,
6843 block_start, len, len, type);
6844 btrfs_end_transaction(trans, root);
6845 if (ret) {
6846 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006847 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006848 }
6849 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006850 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006851 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006852 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006853must_cow:
6854 /*
6855 * this will cow the extent, reset the len in case we changed
6856 * it above
6857 */
6858 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006859 free_extent_map(em);
6860 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006861 if (IS_ERR(em)) {
6862 ret = PTR_ERR(em);
6863 goto unlock_err;
6864 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006865 len = min(len, em->len - (start - em->start));
6866unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6868 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006869 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006870 bh_result->b_bdev = em->bdev;
6871 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006872 if (create) {
6873 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6874 set_buffer_new(bh_result);
6875
6876 /*
6877 * Need to update the i_size under the extent lock so buffered
6878 * readers will get the updated i_size when we unlock.
6879 */
6880 if (start + len > i_size_read(inode))
6881 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006882
Miao Xie172a5042013-02-21 02:48:22 -07006883 spin_lock(&BTRFS_I(inode)->lock);
6884 BTRFS_I(inode)->outstanding_extents++;
6885 spin_unlock(&BTRFS_I(inode)->lock);
6886
Miao Xie09348562013-02-07 10:12:07 +00006887 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6888 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6889 &cached_state, GFP_NOFS);
6890 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006891 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006892
Josef Bacikeb838e72012-07-31 16:28:48 -04006893 /*
6894 * In the case of write we need to clear and unlock the entire range,
6895 * in the case of read we need to unlock only the end area that we
6896 * aren't using if there is any left over space.
6897 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006898 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006899 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6900 lockend, unlock_bits, 1, 0,
6901 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006902 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006903 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006904 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006905
Josef Bacik4b46fce2010-05-23 11:00:55 -04006906 free_extent_map(em);
6907
6908 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006909
6910unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006911 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6912 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6913 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006914}
6915
6916struct btrfs_dio_private {
6917 struct inode *inode;
6918 u64 logical_offset;
6919 u64 disk_bytenr;
6920 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006921 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006922
6923 /* number of bios pending for this dio */
6924 atomic_t pending_bios;
6925
6926 /* IO errors */
6927 int errors;
6928
6929 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006930};
6931
6932static void btrfs_endio_direct_read(struct bio *bio, int err)
6933{
Miao Xiee65e1532010-11-22 03:04:43 +00006934 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006935 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6936 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006937 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006938 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006939 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940
6941 start = dip->logical_offset;
6942 do {
6943 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6944 struct page *page = bvec->bv_page;
6945 char *kaddr;
6946 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006947 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006948 unsigned long flags;
6949
Josef Bacikc3298612012-08-03 16:49:19 -04006950 if (get_state_private(&BTRFS_I(inode)->io_tree,
6951 start, &private))
6952 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006953 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006954 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006955 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006956 csum, bvec->bv_len);
6957 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006958 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006959 local_irq_restore(flags);
6960
6961 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006962 if (csum != private) {
6963failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006964 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6965 (unsigned long long)btrfs_ino(inode),
6966 (unsigned long long)start,
6967 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006968 err = -EIO;
6969 }
6970 }
6971
6972 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006973 bvec++;
6974 } while (bvec <= bvec_end);
6975
6976 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006977 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006978 bio->bi_private = dip->private;
6979
Josef Bacik4b46fce2010-05-23 11:00:55 -04006980 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006981
6982 /* If we had a csum failure make sure to clear the uptodate flag */
6983 if (err)
6984 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006985 dio_end_io(bio, err);
6986}
6987
6988static void btrfs_endio_direct_write(struct bio *bio, int err)
6989{
6990 struct btrfs_dio_private *dip = bio->bi_private;
6991 struct inode *inode = dip->inode;
6992 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006993 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006994 u64 ordered_offset = dip->logical_offset;
6995 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006996 int ret;
6997
6998 if (err)
6999 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007000again:
7001 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7002 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007003 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007004 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007005 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007006
Josef Bacik5fd02042012-05-02 14:00:54 -04007007 ordered->work.func = finish_ordered_fn;
7008 ordered->work.flags = 0;
7009 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7010 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007011out_test:
7012 /*
7013 * our bio might span multiple ordered extents. If we haven't
7014 * completed the accounting for the whole dio, go back and try again
7015 */
7016 if (ordered_offset < dip->logical_offset + dip->bytes) {
7017 ordered_bytes = dip->logical_offset + dip->bytes -
7018 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007019 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007020 goto again;
7021 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007022out_done:
7023 bio->bi_private = dip->private;
7024
Josef Bacik4b46fce2010-05-23 11:00:55 -04007025 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007026
7027 /* If we had an error make sure to clear the uptodate flag */
7028 if (err)
7029 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007030 dio_end_io(bio, err);
7031}
7032
Chris Masoneaf25d92010-05-25 09:48:28 -04007033static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7034 struct bio *bio, int mirror_num,
7035 unsigned long bio_flags, u64 offset)
7036{
7037 int ret;
7038 struct btrfs_root *root = BTRFS_I(inode)->root;
7039 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007040 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007041 return 0;
7042}
7043
Miao Xiee65e1532010-11-22 03:04:43 +00007044static void btrfs_end_dio_bio(struct bio *bio, int err)
7045{
7046 struct btrfs_dio_private *dip = bio->bi_private;
7047
7048 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007049 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007050 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007051 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007052 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007053 dip->errors = 1;
7054
7055 /*
7056 * before atomic variable goto zero, we must make sure
7057 * dip->errors is perceived to be set.
7058 */
7059 smp_mb__before_atomic_dec();
7060 }
7061
7062 /* if there are more bios still pending for this dio, just exit */
7063 if (!atomic_dec_and_test(&dip->pending_bios))
7064 goto out;
7065
7066 if (dip->errors)
7067 bio_io_error(dip->orig_bio);
7068 else {
7069 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
7070 bio_endio(dip->orig_bio, 0);
7071 }
7072out:
7073 bio_put(bio);
7074}
7075
7076static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7077 u64 first_sector, gfp_t gfp_flags)
7078{
7079 int nr_vecs = bio_get_nr_vecs(bdev);
7080 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7081}
7082
7083static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7084 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007085 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007086{
7087 int write = rw & REQ_WRITE;
7088 struct btrfs_root *root = BTRFS_I(inode)->root;
7089 int ret;
7090
Josef Bacikb812ce22012-11-16 13:56:32 -05007091 if (async_submit)
7092 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7093
Miao Xiee65e1532010-11-22 03:04:43 +00007094 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007095
7096 if (!write) {
7097 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7098 if (ret)
7099 goto err;
7100 }
Miao Xiee65e1532010-11-22 03:04:43 +00007101
Josef Bacik1ae39932011-04-06 14:41:34 -04007102 if (skip_sum)
7103 goto map;
7104
7105 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007106 ret = btrfs_wq_submit_bio(root->fs_info,
7107 inode, rw, bio, 0, 0,
7108 file_offset,
7109 __btrfs_submit_bio_start_direct_io,
7110 __btrfs_submit_bio_done);
7111 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007112 } else if (write) {
7113 /*
7114 * If we aren't doing async submit, calculate the csum of the
7115 * bio now.
7116 */
7117 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7118 if (ret)
7119 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007120 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007121 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007122 if (ret)
7123 goto err;
7124 }
Miao Xiee65e1532010-11-22 03:04:43 +00007125
Josef Bacik1ae39932011-04-06 14:41:34 -04007126map:
7127 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007128err:
7129 bio_put(bio);
7130 return ret;
7131}
7132
7133static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7134 int skip_sum)
7135{
7136 struct inode *inode = dip->inode;
7137 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007138 struct bio *bio;
7139 struct bio *orig_bio = dip->orig_bio;
7140 struct bio_vec *bvec = orig_bio->bi_io_vec;
7141 u64 start_sector = orig_bio->bi_sector;
7142 u64 file_offset = dip->logical_offset;
7143 u64 submit_len = 0;
7144 u64 map_length;
7145 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007146 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007147 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007148
Miao Xiee65e1532010-11-22 03:04:43 +00007149 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007150 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007151 &map_length, NULL, 0);
7152 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007153 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007154 return -EIO;
7155 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007156 if (map_length >= orig_bio->bi_size) {
7157 bio = orig_bio;
7158 goto submit;
7159 }
7160
David Woodhouse53b381b2013-01-29 18:40:14 -05007161 /* async crcs make it difficult to collect full stripe writes. */
7162 if (btrfs_get_alloc_profile(root, 1) &
7163 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7164 async_submit = 0;
7165 else
7166 async_submit = 1;
7167
Josef Bacik02f57c72011-04-06 14:25:44 -04007168 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7169 if (!bio)
7170 return -ENOMEM;
7171 bio->bi_private = dip;
7172 bio->bi_end_io = btrfs_end_dio_bio;
7173 atomic_inc(&dip->pending_bios);
7174
Miao Xiee65e1532010-11-22 03:04:43 +00007175 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7176 if (unlikely(map_length < submit_len + bvec->bv_len ||
7177 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7178 bvec->bv_offset) < bvec->bv_len)) {
7179 /*
7180 * inc the count before we submit the bio so
7181 * we know the end IO handler won't happen before
7182 * we inc the count. Otherwise, the dip might get freed
7183 * before we're done setting it up
7184 */
7185 atomic_inc(&dip->pending_bios);
7186 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7187 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007188 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007189 if (ret) {
7190 bio_put(bio);
7191 atomic_dec(&dip->pending_bios);
7192 goto out_err;
7193 }
7194
Miao Xiee65e1532010-11-22 03:04:43 +00007195 start_sector += submit_len >> 9;
7196 file_offset += submit_len;
7197
7198 submit_len = 0;
7199 nr_pages = 0;
7200
7201 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7202 start_sector, GFP_NOFS);
7203 if (!bio)
7204 goto out_err;
7205 bio->bi_private = dip;
7206 bio->bi_end_io = btrfs_end_dio_bio;
7207
7208 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007209 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007210 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007211 &map_length, NULL, 0);
7212 if (ret) {
7213 bio_put(bio);
7214 goto out_err;
7215 }
7216 } else {
7217 submit_len += bvec->bv_len;
7218 nr_pages ++;
7219 bvec++;
7220 }
7221 }
7222
Josef Bacik02f57c72011-04-06 14:25:44 -04007223submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007224 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007225 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007226 if (!ret)
7227 return 0;
7228
7229 bio_put(bio);
7230out_err:
7231 dip->errors = 1;
7232 /*
7233 * before atomic variable goto zero, we must
7234 * make sure dip->errors is perceived to be set.
7235 */
7236 smp_mb__before_atomic_dec();
7237 if (atomic_dec_and_test(&dip->pending_bios))
7238 bio_io_error(dip->orig_bio);
7239
7240 /* bio_end_io() will handle error, so we needn't return it */
7241 return 0;
7242}
7243
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7245 loff_t file_offset)
7246{
7247 struct btrfs_root *root = BTRFS_I(inode)->root;
7248 struct btrfs_dio_private *dip;
7249 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007250 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007251 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 int ret = 0;
7253
7254 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7255
7256 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7257 if (!dip) {
7258 ret = -ENOMEM;
7259 goto free_ordered;
7260 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261
7262 dip->private = bio->bi_private;
7263 dip->inode = inode;
7264 dip->logical_offset = file_offset;
7265
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266 dip->bytes = 0;
7267 do {
7268 dip->bytes += bvec->bv_len;
7269 bvec++;
7270 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
7271
Chris Mason46bfbb52010-05-26 11:04:10 -04007272 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007273 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007274 dip->errors = 0;
7275 dip->orig_bio = bio;
7276 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277
7278 if (write)
7279 bio->bi_end_io = btrfs_endio_direct_write;
7280 else
7281 bio->bi_end_io = btrfs_endio_direct_read;
7282
Miao Xiee65e1532010-11-22 03:04:43 +00007283 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7284 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007285 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007286free_ordered:
7287 /*
7288 * If this is a write, we need to clean up the reserved space and kill
7289 * the ordered extent.
7290 */
7291 if (write) {
7292 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007293 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7295 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7296 btrfs_free_reserved_extent(root, ordered->start,
7297 ordered->disk_len);
7298 btrfs_put_ordered_extent(ordered);
7299 btrfs_put_ordered_extent(ordered);
7300 }
7301 bio_endio(bio, ret);
7302}
7303
Chris Mason5a5f79b2010-05-26 21:33:37 -04007304static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7305 const struct iovec *iov, loff_t offset,
7306 unsigned long nr_segs)
7307{
7308 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007309 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007310 size_t size;
7311 unsigned long addr;
7312 unsigned blocksize_mask = root->sectorsize - 1;
7313 ssize_t retval = -EINVAL;
7314 loff_t end = offset;
7315
7316 if (offset & blocksize_mask)
7317 goto out;
7318
7319 /* Check the memory alignment. Blocks cannot straddle pages */
7320 for (seg = 0; seg < nr_segs; seg++) {
7321 addr = (unsigned long)iov[seg].iov_base;
7322 size = iov[seg].iov_len;
7323 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007324 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007325 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007326
7327 /* If this is a write we don't need to check anymore */
7328 if (rw & WRITE)
7329 continue;
7330
7331 /*
7332 * Check to make sure we don't have duplicate iov_base's in this
7333 * iovec, if so return EINVAL, otherwise we'll get csum errors
7334 * when reading back.
7335 */
7336 for (i = seg + 1; i < nr_segs; i++) {
7337 if (iov[seg].iov_base == iov[i].iov_base)
7338 goto out;
7339 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007340 }
7341 retval = 0;
7342out:
7343 return retval;
7344}
Josef Bacikeb838e72012-07-31 16:28:48 -04007345
Chris Mason16432982008-04-10 10:23:21 -04007346static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7347 const struct iovec *iov, loff_t offset,
7348 unsigned long nr_segs)
7349{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007350 struct file *file = iocb->ki_filp;
7351 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007352 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007353 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007354 bool wakeup = true;
7355 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007356 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007357
Chris Mason5a5f79b2010-05-26 21:33:37 -04007358 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007359 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007360 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007361
Miao Xie38851cc2013-02-08 07:04:11 +00007362 atomic_inc(&inode->i_dio_count);
7363 smp_mb__after_atomic_inc();
7364
Miao Xie09348562013-02-07 10:12:07 +00007365 if (rw & WRITE) {
7366 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007367 /*
7368 * If the write DIO is beyond the EOF, we need update
7369 * the isize, but it is protected by i_mutex. So we can
7370 * not unlock the i_mutex at this case.
7371 */
7372 if (offset + count <= inode->i_size) {
7373 mutex_unlock(&inode->i_mutex);
7374 relock = true;
7375 }
Miao Xie09348562013-02-07 10:12:07 +00007376 ret = btrfs_delalloc_reserve_space(inode, count);
7377 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007378 goto out;
7379 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7380 &BTRFS_I(inode)->runtime_flags))) {
7381 inode_dio_done(inode);
7382 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7383 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007384 }
7385
7386 ret = __blockdev_direct_IO(rw, iocb, inode,
7387 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7388 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007389 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007390 if (rw & WRITE) {
7391 if (ret < 0 && ret != -EIOCBQUEUED)
7392 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007393 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007394 btrfs_delalloc_release_space(inode,
7395 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007396 else
7397 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007398 }
Miao Xie38851cc2013-02-08 07:04:11 +00007399out:
Miao Xie2e60a512013-02-08 07:01:08 +00007400 if (wakeup)
7401 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007402 if (relock)
7403 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007404
7405 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007406}
7407
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007408#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7409
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007410static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7411 __u64 start, __u64 len)
7412{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007413 int ret;
7414
7415 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7416 if (ret)
7417 return ret;
7418
Chris Masonec29ed52011-02-23 16:23:20 -05007419 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007420}
7421
Chris Mason9ebefb182007-06-15 13:50:00 -04007422int btrfs_readpage(struct file *file, struct page *page)
7423{
Chris Masond1310b22008-01-24 16:13:08 -05007424 struct extent_io_tree *tree;
7425 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007426 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007427}
Chris Mason1832a6d2007-12-21 16:27:21 -05007428
Chris Mason39279cc2007-06-12 06:35:45 -04007429static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7430{
Chris Masond1310b22008-01-24 16:13:08 -05007431 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007432
7433
7434 if (current->flags & PF_MEMALLOC) {
7435 redirty_page_for_writepage(wbc, page);
7436 unlock_page(page);
7437 return 0;
7438 }
Chris Masond1310b22008-01-24 16:13:08 -05007439 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007440 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7441}
Chris Mason39279cc2007-06-12 06:35:45 -04007442
Eric Sandeen48a3b632013-04-25 20:41:01 +00007443static int btrfs_writepages(struct address_space *mapping,
7444 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007445{
Chris Masond1310b22008-01-24 16:13:08 -05007446 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007447
Chris Masond1310b22008-01-24 16:13:08 -05007448 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007449 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7450}
7451
Chris Mason3ab2fb52007-11-08 10:59:22 -05007452static int
7453btrfs_readpages(struct file *file, struct address_space *mapping,
7454 struct list_head *pages, unsigned nr_pages)
7455{
Chris Masond1310b22008-01-24 16:13:08 -05007456 struct extent_io_tree *tree;
7457 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007458 return extent_readpages(tree, mapping, pages, nr_pages,
7459 btrfs_get_extent);
7460}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007461static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007462{
Chris Masond1310b22008-01-24 16:13:08 -05007463 struct extent_io_tree *tree;
7464 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007465 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007466
Chris Masond1310b22008-01-24 16:13:08 -05007467 tree = &BTRFS_I(page->mapping->host)->io_tree;
7468 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007469 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007470 if (ret == 1) {
7471 ClearPagePrivate(page);
7472 set_page_private(page, 0);
7473 page_cache_release(page);
7474 }
7475 return ret;
7476}
Chris Mason39279cc2007-06-12 06:35:45 -04007477
Chris Masone6dcd2d2008-07-17 12:53:50 -04007478static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7479{
Chris Mason98509cf2008-09-11 15:51:43 -04007480 if (PageWriteback(page) || PageDirty(page))
7481 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007482 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007483}
7484
Chris Masona52d9a82007-08-27 16:49:44 -04007485static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7486{
Josef Bacik5fd02042012-05-02 14:00:54 -04007487 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007488 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007489 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007490 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007491 u64 page_start = page_offset(page);
7492 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007493
Chris Mason8b62b722009-09-02 16:53:46 -04007494 /*
7495 * we have the page locked, so new writeback can't start,
7496 * and the dirty bit won't be cleared while we are here.
7497 *
7498 * Wait for IO on this page so that we can safely clear
7499 * the PagePrivate2 bit and do ordered accounting
7500 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007501 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007502
Josef Bacik5fd02042012-05-02 14:00:54 -04007503 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007504 if (offset) {
7505 btrfs_releasepage(page, GFP_NOFS);
7506 return;
7507 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007508 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007509 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007510 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007511 /*
7512 * IO on this page will never be started, so we need
7513 * to account for any ordered extents now
7514 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007515 clear_extent_bit(tree, page_start, page_end,
7516 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007517 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7518 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007519 /*
7520 * whoever cleared the private bit is responsible
7521 * for the finish_ordered_io
7522 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007523 if (TestClearPagePrivate2(page) &&
7524 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7525 PAGE_CACHE_SIZE, 1)) {
7526 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007527 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007528 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007529 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007530 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007531 }
7532 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007533 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007534 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7535 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007536 __btrfs_releasepage(page, GFP_NOFS);
7537
Chris Mason4a096752008-07-21 10:29:44 -04007538 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007539 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007540 ClearPagePrivate(page);
7541 set_page_private(page, 0);
7542 page_cache_release(page);
7543 }
Chris Mason39279cc2007-06-12 06:35:45 -04007544}
7545
Chris Mason9ebefb182007-06-15 13:50:00 -04007546/*
7547 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7548 * called from a page fault handler when a page is first dirtied. Hence we must
7549 * be careful to check for EOF conditions here. We set the page up correctly
7550 * for a written page which means we get ENOSPC checking when writing into
7551 * holes and correct delalloc and unwritten extent mapping on filesystems that
7552 * support these features.
7553 *
7554 * We are not allowed to take the i_mutex here so we have to play games to
7555 * protect against truncate races as the page could now be beyond EOF. Because
7556 * vmtruncate() writes the inode size before removing pages, once we have the
7557 * page lock we can determine safely if the page is beyond EOF. If it is not
7558 * beyond EOF, then the page is guaranteed safe against truncation until we
7559 * unlock the page.
7560 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007561int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007562{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007563 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007564 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007565 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007566 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7567 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007568 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007569 char *kaddr;
7570 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007571 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007572 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007573 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007574 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007575 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007576
Jan Karab2b5ef52012-06-12 16:20:45 +02007577 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007578 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007579 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007580 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007581 reserved = 1;
7582 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007583 if (ret) {
7584 if (ret == -ENOMEM)
7585 ret = VM_FAULT_OOM;
7586 else /* -ENOSPC, -EIO, etc */
7587 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007588 if (reserved)
7589 goto out;
7590 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007591 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007592
Nick Piggin56a76f82009-03-31 15:23:23 -07007593 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007594again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007595 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007596 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007597 page_start = page_offset(page);
7598 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007599
Chris Mason9ebefb182007-06-15 13:50:00 -04007600 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007601 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007602 /* page got truncated out from underneath us */
7603 goto out_unlock;
7604 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007605 wait_on_page_writeback(page);
7606
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007607 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007608 set_page_extent_mapped(page);
7609
Chris Masoneb84ae02008-07-17 13:53:27 -04007610 /*
7611 * we can't set the delalloc bits if there are pending ordered
7612 * extents. Drop our locks and wait for them to finish
7613 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007614 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7615 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007616 unlock_extent_cached(io_tree, page_start, page_end,
7617 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007618 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007619 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007620 btrfs_put_ordered_extent(ordered);
7621 goto again;
7622 }
7623
Josef Bacikfbf19082009-10-01 17:10:23 -04007624 /*
7625 * XXX - page_mkwrite gets called every time the page is dirtied, even
7626 * if it was already dirty, so for space accounting reasons we need to
7627 * clear any delalloc bits for the range we are fixing to save. There
7628 * is probably a better way to do this, but for now keep consistent with
7629 * prepare_pages in the normal write path.
7630 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007631 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007632 EXTENT_DIRTY | EXTENT_DELALLOC |
7633 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007634 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007635
Josef Bacik2ac55d42010-02-03 19:33:23 +00007636 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7637 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007638 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007639 unlock_extent_cached(io_tree, page_start, page_end,
7640 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007641 ret = VM_FAULT_SIGBUS;
7642 goto out_unlock;
7643 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007644 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007645
7646 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007647 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007648 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007649 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007650 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007651
Chris Masone6dcd2d2008-07-17 12:53:50 -04007652 if (zero_start != PAGE_CACHE_SIZE) {
7653 kaddr = kmap(page);
7654 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7655 flush_dcache_page(page);
7656 kunmap(page);
7657 }
Chris Mason247e7432008-07-17 12:53:51 -04007658 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007659 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007660 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007661
Chris Mason257c62e2009-10-13 13:21:08 -04007662 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7663 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007664 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007665
Josef Bacik2ac55d42010-02-03 19:33:23 +00007666 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007667
7668out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007669 if (!ret) {
7670 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007671 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007672 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007673 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007674out:
Josef Bacikec39e182012-01-12 19:10:12 -05007675 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007676out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007677 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007678 return ret;
7679}
7680
Josef Bacika41ad392011-01-31 15:30:16 -05007681static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007682{
7683 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007684 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007685 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007686 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007687 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007688 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007689 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007690
Josef Bacik2aaa6652012-08-29 14:27:18 -04007691 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007692 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007693 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007694
Chris Mason4a096752008-07-21 10:29:44 -04007695 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007696 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007697
Josef Bacikfcb80c22011-05-03 10:40:22 -04007698 /*
7699 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7700 * 3 things going on here
7701 *
7702 * 1) We need to reserve space for our orphan item and the space to
7703 * delete our orphan item. Lord knows we don't want to have a dangling
7704 * orphan item because we didn't reserve space to remove it.
7705 *
7706 * 2) We need to reserve space to update our inode.
7707 *
7708 * 3) We need to have something to cache all the space that is going to
7709 * be free'd up by the truncate operation, but also have some slack
7710 * space reserved in case it uses space during the truncate (thank you
7711 * very much snapshotting).
7712 *
7713 * And we need these to all be seperate. The fact is we can use alot of
7714 * space doing the truncate, and we have no earthly idea how much space
7715 * we will use, so we need the truncate reservation to be seperate so it
7716 * doesn't end up using space reserved for updating the inode or
7717 * removing the orphan item. We also need to be able to stop the
7718 * transaction and start a new one, which means we need to be able to
7719 * update the inode several times, and we have no idea of knowing how
7720 * many times that will be, so we can't just reserve 1 item for the
7721 * entirety of the opration, so that has to be done seperately as well.
7722 * Then there is the orphan item, which does indeed need to be held on
7723 * to for the whole operation, and we need nobody to touch this reserved
7724 * space except the orphan code.
7725 *
7726 * So that leaves us with
7727 *
7728 * 1) root->orphan_block_rsv - for the orphan deletion.
7729 * 2) rsv - for the truncate reservation, which we will steal from the
7730 * transaction reservation.
7731 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7732 * updating the inode.
7733 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007734 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007735 if (!rsv)
7736 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007737 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007738 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007739
Josef Bacik907cbce2011-08-08 13:46:15 -04007740 /*
Josef Bacik07127182011-08-19 10:29:59 -04007741 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007742 * 1 for updating the inode.
7743 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007744 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007745 if (IS_ERR(trans)) {
7746 err = PTR_ERR(trans);
7747 goto out;
7748 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007749
Josef Bacik907cbce2011-08-08 13:46:15 -04007750 /* Migrate the slack space for the truncate to our reserve */
7751 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7752 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007753 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007754
Chris Mason5a3f23d2009-03-31 13:27:11 -04007755 /*
7756 * setattr is responsible for setting the ordered_data_close flag,
7757 * but that is only tested during the last file release. That
7758 * could happen well after the next commit, leaving a great big
7759 * window where new writes may get lost if someone chooses to write
7760 * to this file after truncating to zero
7761 *
7762 * The inode doesn't have any dirty data here, and so if we commit
7763 * this is a noop. If someone immediately starts writing to the inode
7764 * it is very likely we'll catch some of their writes in this
7765 * transaction, and the commit will find this file on the ordered
7766 * data list with good things to send down.
7767 *
7768 * This is a best effort solution, there is still a window where
7769 * using truncate to replace the contents of the file will
7770 * end up with a zero length file after a crash.
7771 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007772 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7773 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007774 btrfs_add_ordered_operation(trans, root, inode);
7775
Josef Bacik5dc562c2012-08-17 13:14:17 -04007776 /*
7777 * So if we truncate and then write and fsync we normally would just
7778 * write the extents that changed, which is a problem if we need to
7779 * first truncate that entire inode. So set this flag so we write out
7780 * all of the extents in the inode to the sync log so we're completely
7781 * safe.
7782 */
7783 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007784 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007785
Yan, Zheng80825102009-11-12 09:35:36 +00007786 while (1) {
7787 ret = btrfs_truncate_inode_items(trans, root, inode,
7788 inode->i_size,
7789 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007790 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007791 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007792 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007793 }
Chris Mason39279cc2007-06-12 06:35:45 -04007794
Josef Bacikfcb80c22011-05-03 10:40:22 -04007795 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007796 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007797 if (ret) {
7798 err = ret;
7799 break;
7800 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007801
Yan, Zheng80825102009-11-12 09:35:36 +00007802 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007803 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007804
7805 trans = btrfs_start_transaction(root, 2);
7806 if (IS_ERR(trans)) {
7807 ret = err = PTR_ERR(trans);
7808 trans = NULL;
7809 break;
7810 }
7811
7812 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7813 rsv, min_size);
7814 BUG_ON(ret); /* shouldn't happen */
7815 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007816 }
7817
7818 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007819 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007820 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007821 if (ret)
7822 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007823 }
7824
Chris Mason917c16b2011-11-08 14:49:59 -05007825 if (trans) {
7826 trans->block_rsv = &root->fs_info->trans_block_rsv;
7827 ret = btrfs_update_inode(trans, root, inode);
7828 if (ret && !err)
7829 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007830
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007831 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007832 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007833 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007834
7835out:
7836 btrfs_free_block_rsv(root, rsv);
7837
Josef Bacik3893e332011-01-31 16:03:11 -05007838 if (ret && !err)
7839 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007840
Josef Bacik3893e332011-01-31 16:03:11 -05007841 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007842}
7843
Sven Wegener3b963622008-06-09 21:57:42 -04007844/*
Chris Masond352ac62008-09-29 15:18:18 -04007845 * create a new subvolume directory/inode (helper for the ioctl).
7846 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007847int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007848 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007849{
Chris Mason39279cc2007-06-12 06:35:45 -04007850 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007851 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007852 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007853
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007854 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7855 new_dirid, new_dirid,
7856 S_IFDIR | (~current_umask() & S_IRWXUGO),
7857 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007858 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007859 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007860 inode->i_op = &btrfs_dir_inode_operations;
7861 inode->i_fop = &btrfs_dir_file_operations;
7862
Miklos Szeredibfe86842011-10-28 14:13:29 +02007863 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007864 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007865
Yan, Zheng76dda932009-09-21 16:00:26 -04007866 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007867
Yan, Zheng76dda932009-09-21 16:00:26 -04007868 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007869 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007870}
7871
Chris Mason39279cc2007-06-12 06:35:45 -04007872struct inode *btrfs_alloc_inode(struct super_block *sb)
7873{
7874 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007875 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007876
7877 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7878 if (!ei)
7879 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007880
7881 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007882 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007883 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007884 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007885 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007886 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007887 ei->disk_i_size = 0;
7888 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007889 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007890 ei->index_cnt = (u64)-1;
7891 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007892 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007893
Josef Bacik9e0baf62011-07-15 15:16:44 +00007894 spin_lock_init(&ei->lock);
7895 ei->outstanding_extents = 0;
7896 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007897
Josef Bacik72ac3c02012-05-23 14:13:11 -04007898 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007899 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007900
Miao Xie16cdcec2011-04-22 18:12:22 +08007901 ei->delayed_node = NULL;
7902
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007903 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007904 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007905 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7906 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007907 ei->io_tree.track_uptodate = 1;
7908 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007909 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007910 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007911 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007912 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007913 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007914 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007915 RB_CLEAR_NODE(&ei->rb_node);
7916
7917 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007918}
7919
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007920static void btrfs_i_callback(struct rcu_head *head)
7921{
7922 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007923 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7924}
7925
Chris Mason39279cc2007-06-12 06:35:45 -04007926void btrfs_destroy_inode(struct inode *inode)
7927{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007928 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007929 struct btrfs_root *root = BTRFS_I(inode)->root;
7930
Al Virob3d9b7a2012-06-09 13:51:19 -04007931 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007932 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007933 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7934 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007935 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7936 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007937
Chris Mason5a3f23d2009-03-31 13:27:11 -04007938 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007939 * This can happen where we create an inode, but somebody else also
7940 * created the same inode and we need to destroy the one we already
7941 * created.
7942 */
7943 if (!root)
7944 goto free;
7945
7946 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007947 * Make sure we're properly removed from the ordered operation
7948 * lists.
7949 */
7950 smp_mb();
7951 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7952 spin_lock(&root->fs_info->ordered_extent_lock);
7953 list_del_init(&BTRFS_I(inode)->ordered_operations);
7954 spin_unlock(&root->fs_info->ordered_extent_lock);
7955 }
7956
Josef Bacik8a35d952012-05-23 14:26:42 -04007957 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7958 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007959 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7960 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007961 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007962 }
Josef Bacik7b128762008-07-24 12:17:14 -04007963
Chris Masond3977122009-01-05 21:25:51 -05007964 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007965 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7966 if (!ordered)
7967 break;
7968 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007969 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7970 (unsigned long long)ordered->file_offset,
7971 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007972 btrfs_remove_ordered_extent(inode, ordered);
7973 btrfs_put_ordered_extent(ordered);
7974 btrfs_put_ordered_extent(ordered);
7975 }
7976 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007977 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007978 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007979free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007980 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007981 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007982}
7983
Al Viro45321ac2010-06-07 13:43:19 -04007984int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007985{
7986 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007987
Liu Bofa6ac872013-02-20 14:10:23 +00007988 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007989 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007990 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007991 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007992 else
Al Viro45321ac2010-06-07 13:43:19 -04007993 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007994}
7995
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007996static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007997{
7998 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7999
8000 inode_init_once(&ei->vfs_inode);
8001}
8002
8003void btrfs_destroy_cachep(void)
8004{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008005 /*
8006 * Make sure all delayed rcu free inodes are flushed before we
8007 * destroy cache.
8008 */
8009 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008010 if (btrfs_inode_cachep)
8011 kmem_cache_destroy(btrfs_inode_cachep);
8012 if (btrfs_trans_handle_cachep)
8013 kmem_cache_destroy(btrfs_trans_handle_cachep);
8014 if (btrfs_transaction_cachep)
8015 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008016 if (btrfs_path_cachep)
8017 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008018 if (btrfs_free_space_cachep)
8019 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008020 if (btrfs_delalloc_work_cachep)
8021 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008022}
8023
8024int btrfs_init_cachep(void)
8025{
David Sterba837e1972012-09-07 03:00:48 -06008026 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008027 sizeof(struct btrfs_inode), 0,
8028 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008029 if (!btrfs_inode_cachep)
8030 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008031
David Sterba837e1972012-09-07 03:00:48 -06008032 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008033 sizeof(struct btrfs_trans_handle), 0,
8034 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008035 if (!btrfs_trans_handle_cachep)
8036 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008037
David Sterba837e1972012-09-07 03:00:48 -06008038 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008039 sizeof(struct btrfs_transaction), 0,
8040 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008041 if (!btrfs_transaction_cachep)
8042 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008043
David Sterba837e1972012-09-07 03:00:48 -06008044 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008045 sizeof(struct btrfs_path), 0,
8046 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008047 if (!btrfs_path_cachep)
8048 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008049
David Sterba837e1972012-09-07 03:00:48 -06008050 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008051 sizeof(struct btrfs_free_space), 0,
8052 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8053 if (!btrfs_free_space_cachep)
8054 goto fail;
8055
Miao Xie8ccf6f192012-10-25 09:28:04 +00008056 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8057 sizeof(struct btrfs_delalloc_work), 0,
8058 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8059 NULL);
8060 if (!btrfs_delalloc_work_cachep)
8061 goto fail;
8062
Chris Mason39279cc2007-06-12 06:35:45 -04008063 return 0;
8064fail:
8065 btrfs_destroy_cachep();
8066 return -ENOMEM;
8067}
8068
8069static int btrfs_getattr(struct vfsmount *mnt,
8070 struct dentry *dentry, struct kstat *stat)
8071{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008072 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008073 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008074 u32 blocksize = inode->i_sb->s_blocksize;
8075
Chris Mason39279cc2007-06-12 06:35:45 -04008076 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008077 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008078 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008079
8080 spin_lock(&BTRFS_I(inode)->lock);
8081 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8082 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008083 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008084 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008085 return 0;
8086}
8087
Chris Masond3977122009-01-05 21:25:51 -05008088static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8089 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008090{
8091 struct btrfs_trans_handle *trans;
8092 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008093 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008094 struct inode *new_inode = new_dentry->d_inode;
8095 struct inode *old_inode = old_dentry->d_inode;
8096 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008097 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008098 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008099 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008100 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008101
Li Zefan33345d012011-04-20 10:31:50 +08008102 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008103 return -EPERM;
8104
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008105 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008106 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008107 return -EXDEV;
8108
Li Zefan33345d012011-04-20 10:31:50 +08008109 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8110 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008111 return -ENOTEMPTY;
8112
Chris Mason39279cc2007-06-12 06:35:45 -04008113 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008114 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008115 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008116
8117
8118 /* check for collisions, even if the name isn't there */
8119 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8120 new_dentry->d_name.name,
8121 new_dentry->d_name.len);
8122
8123 if (ret) {
8124 if (ret == -EEXIST) {
8125 /* we shouldn't get
8126 * eexist without a new_inode */
8127 if (!new_inode) {
8128 WARN_ON(1);
8129 return ret;
8130 }
8131 } else {
8132 /* maybe -EOVERFLOW */
8133 return ret;
8134 }
8135 }
8136 ret = 0;
8137
Chris Mason5a3f23d2009-03-31 13:27:11 -04008138 /*
8139 * we're using rename to replace one file with another.
8140 * and the replacement file is large. Start IO on it now so
8141 * we don't add too much work to the end of the transaction
8142 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008143 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008144 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8145 filemap_flush(old_inode->i_mapping);
8146
Yan, Zheng76dda932009-09-21 16:00:26 -04008147 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008148 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008149 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008150 /*
8151 * We want to reserve the absolute worst case amount of items. So if
8152 * both inodes are subvols and we need to unlink them then that would
8153 * require 4 item modifications, but if they are both normal inodes it
8154 * would require 5 item modifications, so we'll assume their normal
8155 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8156 * should cover the worst case number of items we'll modify.
8157 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008158 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008159 if (IS_ERR(trans)) {
8160 ret = PTR_ERR(trans);
8161 goto out_notrans;
8162 }
Chris Mason5f39d392007-10-15 16:14:19 -04008163
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008164 if (dest != root)
8165 btrfs_record_root_in_trans(trans, dest);
8166
Yan, Zhenga5719522009-09-24 09:17:31 -04008167 ret = btrfs_set_inode_index(new_dir, &index);
8168 if (ret)
8169 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008170
Li Zefan33345d012011-04-20 10:31:50 +08008171 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008172 /* force full log commit if subvolume involved. */
8173 root->fs_info->last_trans_log_full_commit = trans->transid;
8174 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008175 ret = btrfs_insert_inode_ref(trans, dest,
8176 new_dentry->d_name.name,
8177 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008178 old_ino,
8179 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008180 if (ret)
8181 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008182 /*
8183 * this is an ugly little race, but the rename is required
8184 * to make sure that if we crash, the inode is either at the
8185 * old name or the new one. pinning the log transaction lets
8186 * us make sure we don't allow a log commit to come in after
8187 * we unlink the name but before we add the new name back in.
8188 */
8189 btrfs_pin_log_trans(root);
8190 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008191 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008192 * make sure the inode gets flushed if it is replacing
8193 * something.
8194 */
Li Zefan33345d012011-04-20 10:31:50 +08008195 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008196 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008197
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008198 inode_inc_iversion(old_dir);
8199 inode_inc_iversion(new_dir);
8200 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008201 old_dir->i_ctime = old_dir->i_mtime = ctime;
8202 new_dir->i_ctime = new_dir->i_mtime = ctime;
8203 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008204
Chris Mason12fcfd22009-03-24 10:24:20 -04008205 if (old_dentry->d_parent != new_dentry->d_parent)
8206 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8207
Li Zefan33345d012011-04-20 10:31:50 +08008208 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008209 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8210 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8211 old_dentry->d_name.name,
8212 old_dentry->d_name.len);
8213 } else {
Al Viro92986792011-03-04 17:14:37 +00008214 ret = __btrfs_unlink_inode(trans, root, old_dir,
8215 old_dentry->d_inode,
8216 old_dentry->d_name.name,
8217 old_dentry->d_name.len);
8218 if (!ret)
8219 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008220 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008221 if (ret) {
8222 btrfs_abort_transaction(trans, root, ret);
8223 goto out_fail;
8224 }
Chris Mason39279cc2007-06-12 06:35:45 -04008225
8226 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008227 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008228 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008229 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008230 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8231 root_objectid = BTRFS_I(new_inode)->location.objectid;
8232 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8233 root_objectid,
8234 new_dentry->d_name.name,
8235 new_dentry->d_name.len);
8236 BUG_ON(new_inode->i_nlink == 0);
8237 } else {
8238 ret = btrfs_unlink_inode(trans, dest, new_dir,
8239 new_dentry->d_inode,
8240 new_dentry->d_name.name,
8241 new_dentry->d_name.len);
8242 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008243 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008244 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008245 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008246 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008247 if (ret) {
8248 btrfs_abort_transaction(trans, root, ret);
8249 goto out_fail;
8250 }
Chris Mason39279cc2007-06-12 06:35:45 -04008251 }
Josef Bacikaec74772008-07-24 12:12:38 -04008252
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008253 ret = btrfs_add_link(trans, new_dir, old_inode,
8254 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008255 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008256 if (ret) {
8257 btrfs_abort_transaction(trans, root, ret);
8258 goto out_fail;
8259 }
Chris Mason39279cc2007-06-12 06:35:45 -04008260
Li Zefan33345d012011-04-20 10:31:50 +08008261 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008262 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008263 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008264 btrfs_end_log_trans(root);
8265 }
Chris Mason39279cc2007-06-12 06:35:45 -04008266out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008267 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008268out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008269 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008270 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008271
Chris Mason39279cc2007-06-12 06:35:45 -04008272 return ret;
8273}
8274
Miao Xie8ccf6f192012-10-25 09:28:04 +00008275static void btrfs_run_delalloc_work(struct btrfs_work *work)
8276{
8277 struct btrfs_delalloc_work *delalloc_work;
8278
8279 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8280 work);
8281 if (delalloc_work->wait)
8282 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8283 else
8284 filemap_flush(delalloc_work->inode->i_mapping);
8285
8286 if (delalloc_work->delay_iput)
8287 btrfs_add_delayed_iput(delalloc_work->inode);
8288 else
8289 iput(delalloc_work->inode);
8290 complete(&delalloc_work->completion);
8291}
8292
8293struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8294 int wait, int delay_iput)
8295{
8296 struct btrfs_delalloc_work *work;
8297
8298 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8299 if (!work)
8300 return NULL;
8301
8302 init_completion(&work->completion);
8303 INIT_LIST_HEAD(&work->list);
8304 work->inode = inode;
8305 work->wait = wait;
8306 work->delay_iput = delay_iput;
8307 work->work.func = btrfs_run_delalloc_work;
8308
8309 return work;
8310}
8311
8312void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8313{
8314 wait_for_completion(&work->completion);
8315 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8316}
8317
Chris Masond352ac62008-09-29 15:18:18 -04008318/*
8319 * some fairly slow code that needs optimization. This walks the list
8320 * of all the inodes with pending delalloc and forces them to disk.
8321 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008322int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008323{
Chris Masonea8c2812008-08-04 23:17:27 -04008324 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008325 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008326 struct btrfs_delalloc_work *work, *next;
8327 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008328 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008329 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008330
Yan Zhengc146afa2008-11-12 14:34:12 -05008331 if (root->fs_info->sb->s_flags & MS_RDONLY)
8332 return -EROFS;
8333
Miao Xie8ccf6f192012-10-25 09:28:04 +00008334 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008335 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008336
Chris Mason75eff682008-12-15 15:54:40 -05008337 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008338 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8339 while (!list_empty(&splice)) {
8340 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008341 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008342
8343 list_del_init(&binode->delalloc_inodes);
8344
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008345 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008346 if (!inode) {
8347 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8348 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008349 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008350 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008351
8352 list_add_tail(&binode->delalloc_inodes,
8353 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008354 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008355
8356 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8357 if (unlikely(!work)) {
8358 ret = -ENOMEM;
8359 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008360 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008361 list_add_tail(&work->list, &works);
8362 btrfs_queue_worker(&root->fs_info->flush_workers,
8363 &work->work);
8364
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008365 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008366 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008367 }
Chris Mason75eff682008-12-15 15:54:40 -05008368 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008369
Miao Xie1eafa6c2013-01-22 10:49:00 +00008370 list_for_each_entry_safe(work, next, &works, list) {
8371 list_del_init(&work->list);
8372 btrfs_wait_and_free_delalloc_work(work);
8373 }
8374
Chris Mason8c8bee12008-09-29 11:19:10 -04008375 /* the filemap_flush will queue IO into the worker threads, but
8376 * we have to make sure the IO is actually started and that
8377 * ordered extents get created before we return
8378 */
8379 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008380 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008381 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008382 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008383 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8384 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008385 }
8386 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008387 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008388out:
8389 list_for_each_entry_safe(work, next, &works, list) {
8390 list_del_init(&work->list);
8391 btrfs_wait_and_free_delalloc_work(work);
8392 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008393
8394 if (!list_empty_careful(&splice)) {
8395 spin_lock(&root->fs_info->delalloc_lock);
8396 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8397 spin_unlock(&root->fs_info->delalloc_lock);
8398 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008399 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008400}
8401
Chris Mason39279cc2007-06-12 06:35:45 -04008402static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8403 const char *symname)
8404{
8405 struct btrfs_trans_handle *trans;
8406 struct btrfs_root *root = BTRFS_I(dir)->root;
8407 struct btrfs_path *path;
8408 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008409 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008410 int err;
8411 int drop_inode = 0;
8412 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008413 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008414 int name_len;
8415 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008416 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008417 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008418 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008419
8420 name_len = strlen(symname) + 1;
8421 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8422 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008423
Josef Bacik9ed74f22009-09-11 16:12:44 -04008424 /*
8425 * 2 items for inode item and ref
8426 * 2 items for dir items
8427 * 1 item for xattr if selinux is on
8428 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008429 trans = btrfs_start_transaction(root, 5);
8430 if (IS_ERR(trans))
8431 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008432
Li Zefan581bb052011-04-20 10:06:11 +08008433 err = btrfs_find_free_ino(root, &objectid);
8434 if (err)
8435 goto out_unlock;
8436
Josef Bacikaec74772008-07-24 12:12:38 -04008437 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008438 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008439 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008440 if (IS_ERR(inode)) {
8441 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008442 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008443 }
Chris Mason39279cc2007-06-12 06:35:45 -04008444
Eric Paris2a7dba32011-02-01 11:05:39 -05008445 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008446 if (err) {
8447 drop_inode = 1;
8448 goto out_unlock;
8449 }
8450
Casey Schauflerad19db72011-12-15 10:09:07 -05008451 /*
8452 * If the active LSM wants to access the inode during
8453 * d_instantiate it needs these. Smack checks to see
8454 * if the filesystem supports xattrs by looking at the
8455 * ops vector.
8456 */
8457 inode->i_fop = &btrfs_file_operations;
8458 inode->i_op = &btrfs_file_inode_operations;
8459
Josef Bacika1b075d2010-11-19 20:36:11 +00008460 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008461 if (err)
8462 drop_inode = 1;
8463 else {
8464 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008465 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008466 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008467 }
Chris Mason39279cc2007-06-12 06:35:45 -04008468 if (drop_inode)
8469 goto out_unlock;
8470
8471 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008472 if (!path) {
8473 err = -ENOMEM;
8474 drop_inode = 1;
8475 goto out_unlock;
8476 }
Li Zefan33345d012011-04-20 10:31:50 +08008477 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008478 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008479 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8480 datasize = btrfs_file_extent_calc_inline_size(name_len);
8481 err = btrfs_insert_empty_item(trans, root, path, &key,
8482 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008483 if (err) {
8484 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008485 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008486 goto out_unlock;
8487 }
Chris Mason5f39d392007-10-15 16:14:19 -04008488 leaf = path->nodes[0];
8489 ei = btrfs_item_ptr(leaf, path->slots[0],
8490 struct btrfs_file_extent_item);
8491 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8492 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008493 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008494 btrfs_set_file_extent_encryption(leaf, ei, 0);
8495 btrfs_set_file_extent_compression(leaf, ei, 0);
8496 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8497 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8498
Chris Mason39279cc2007-06-12 06:35:45 -04008499 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008500 write_extent_buffer(leaf, symname, ptr, name_len);
8501 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008502 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008503
Chris Mason39279cc2007-06-12 06:35:45 -04008504 inode->i_op = &btrfs_symlink_inode_operations;
8505 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008506 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008507 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008508 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008509 err = btrfs_update_inode(trans, root, inode);
8510 if (err)
8511 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008512
8513out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008514 if (!err)
8515 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008516 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008517 if (drop_inode) {
8518 inode_dec_link_count(inode);
8519 iput(inode);
8520 }
Liu Bob53d3f52012-11-14 14:34:34 +00008521 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008522 return err;
8523}
Chris Mason16432982008-04-10 10:23:21 -04008524
Josef Bacik0af3d002010-06-21 14:48:16 -04008525static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8526 u64 start, u64 num_bytes, u64 min_size,
8527 loff_t actual_len, u64 *alloc_hint,
8528 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008529{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008530 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8531 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008532 struct btrfs_root *root = BTRFS_I(inode)->root;
8533 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008534 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008535 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008536 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008537 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008538 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008539
Josef Bacik0af3d002010-06-21 14:48:16 -04008540 if (trans)
8541 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008542 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008543 if (own_trans) {
8544 trans = btrfs_start_transaction(root, 3);
8545 if (IS_ERR(trans)) {
8546 ret = PTR_ERR(trans);
8547 break;
8548 }
Yan Zhengd899e052008-10-30 14:25:28 -04008549 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008550
Chris Mason154ea282013-03-05 11:11:26 -05008551 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8552 cur_bytes = max(cur_bytes, min_size);
8553 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008554 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008555 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008556 if (own_trans)
8557 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008558 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008559 }
8560
Yan Zhengd899e052008-10-30 14:25:28 -04008561 ret = insert_reserved_file_extent(trans, inode,
8562 cur_offset, ins.objectid,
8563 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008564 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008565 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008566 if (ret) {
8567 btrfs_abort_transaction(trans, root, ret);
8568 if (own_trans)
8569 btrfs_end_transaction(trans, root);
8570 break;
8571 }
Chris Masona1ed8352009-09-11 12:27:37 -04008572 btrfs_drop_extent_cache(inode, cur_offset,
8573 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008574
Josef Bacik5dc562c2012-08-17 13:14:17 -04008575 em = alloc_extent_map();
8576 if (!em) {
8577 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8578 &BTRFS_I(inode)->runtime_flags);
8579 goto next;
8580 }
8581
8582 em->start = cur_offset;
8583 em->orig_start = cur_offset;
8584 em->len = ins.offset;
8585 em->block_start = ins.objectid;
8586 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008587 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008588 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008589 em->bdev = root->fs_info->fs_devices->latest_bdev;
8590 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8591 em->generation = trans->transid;
8592
8593 while (1) {
8594 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008595 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008596 write_unlock(&em_tree->lock);
8597 if (ret != -EEXIST)
8598 break;
8599 btrfs_drop_extent_cache(inode, cur_offset,
8600 cur_offset + ins.offset - 1,
8601 0);
8602 }
8603 free_extent_map(em);
8604next:
Yan Zhengd899e052008-10-30 14:25:28 -04008605 num_bytes -= ins.offset;
8606 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008607 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008608
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008609 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008610 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008611 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008612 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008613 (actual_len > inode->i_size) &&
8614 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008615 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008616 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008617 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008618 i_size = cur_offset;
8619 i_size_write(inode, i_size);
8620 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008621 }
8622
Yan Zhengd899e052008-10-30 14:25:28 -04008623 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008624
8625 if (ret) {
8626 btrfs_abort_transaction(trans, root, ret);
8627 if (own_trans)
8628 btrfs_end_transaction(trans, root);
8629 break;
8630 }
Yan Zhengd899e052008-10-30 14:25:28 -04008631
Josef Bacik0af3d002010-06-21 14:48:16 -04008632 if (own_trans)
8633 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008634 }
Yan Zhengd899e052008-10-30 14:25:28 -04008635 return ret;
8636}
8637
Josef Bacik0af3d002010-06-21 14:48:16 -04008638int btrfs_prealloc_file_range(struct inode *inode, int mode,
8639 u64 start, u64 num_bytes, u64 min_size,
8640 loff_t actual_len, u64 *alloc_hint)
8641{
8642 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8643 min_size, actual_len, alloc_hint,
8644 NULL);
8645}
8646
8647int btrfs_prealloc_file_range_trans(struct inode *inode,
8648 struct btrfs_trans_handle *trans, int mode,
8649 u64 start, u64 num_bytes, u64 min_size,
8650 loff_t actual_len, u64 *alloc_hint)
8651{
8652 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8653 min_size, actual_len, alloc_hint, trans);
8654}
8655
Chris Masone6dcd2d2008-07-17 12:53:50 -04008656static int btrfs_set_page_dirty(struct page *page)
8657{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008658 return __set_page_dirty_nobuffers(page);
8659}
8660
Al Viro10556cb2011-06-20 19:28:19 -04008661static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008662{
Li Zefanb83cc962010-12-20 16:04:08 +08008663 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008664 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008665
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008666 if (mask & MAY_WRITE &&
8667 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8668 if (btrfs_root_readonly(root))
8669 return -EROFS;
8670 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8671 return -EACCES;
8672 }
Al Viro2830ba72011-06-20 19:16:29 -04008673 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008674}
Chris Mason39279cc2007-06-12 06:35:45 -04008675
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008676static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008677 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008678 .lookup = btrfs_lookup,
8679 .create = btrfs_create,
8680 .unlink = btrfs_unlink,
8681 .link = btrfs_link,
8682 .mkdir = btrfs_mkdir,
8683 .rmdir = btrfs_rmdir,
8684 .rename = btrfs_rename,
8685 .symlink = btrfs_symlink,
8686 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008687 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008688 .setxattr = btrfs_setxattr,
8689 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008690 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008691 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008692 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008693 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008694};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008695static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008696 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008697 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008698 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008699};
Yan, Zheng76dda932009-09-21 16:00:26 -04008700
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008701static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008702 .llseek = generic_file_llseek,
8703 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008704 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008705 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008706#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008707 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008708#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008709 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008710 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008711};
8712
Chris Masond1310b22008-01-24 16:13:08 -05008713static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008714 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008715 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008716 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008717 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008718 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008719 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008720 .set_bit_hook = btrfs_set_bit_hook,
8721 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008722 .merge_extent_hook = btrfs_merge_extent_hook,
8723 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008724};
8725
Chris Mason35054392009-01-21 13:11:13 -05008726/*
8727 * btrfs doesn't support the bmap operation because swapfiles
8728 * use bmap to make a mapping of extents in the file. They assume
8729 * these extents won't change over the life of the file and they
8730 * use the bmap result to do IO directly to the drive.
8731 *
8732 * the btrfs bmap call would return logical addresses that aren't
8733 * suitable for IO and they also will change frequently as COW
8734 * operations happen. So, swapfile + btrfs == corruption.
8735 *
8736 * For now we're avoiding this by dropping bmap.
8737 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008738static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008739 .readpage = btrfs_readpage,
8740 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008741 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008742 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008743 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008744 .invalidatepage = btrfs_invalidatepage,
8745 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008746 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008747 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008748};
8749
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008750static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008751 .readpage = btrfs_readpage,
8752 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008753 .invalidatepage = btrfs_invalidatepage,
8754 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008755};
8756
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008757static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008758 .getattr = btrfs_getattr,
8759 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008760 .setxattr = btrfs_setxattr,
8761 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008762 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008763 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008764 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008765 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008766 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008767 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008768};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008769static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008770 .getattr = btrfs_getattr,
8771 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008772 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008773 .setxattr = btrfs_setxattr,
8774 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008775 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008776 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008777 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008778 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008779};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008780static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008781 .readlink = generic_readlink,
8782 .follow_link = page_follow_link_light,
8783 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008784 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008785 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008786 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008787 .setxattr = btrfs_setxattr,
8788 .getxattr = btrfs_getxattr,
8789 .listxattr = btrfs_listxattr,
8790 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008791 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008792 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008793};
Yan, Zheng76dda932009-09-21 16:00:26 -04008794
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008795const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008796 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008797 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008798};