blob: d59dddfb1f9655673801dad7aae82e2c2c99810f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Chris Mason39279cc2007-06-12 06:35:45 -040059
60struct btrfs_iget_args {
61 u64 ino;
62 struct btrfs_root *root;
63};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
67static const struct inode_operations btrfs_dir_ro_inode_operations;
68static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
71static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050073static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000076static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_trans_handle_cachep;
78struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050080struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
82#define S_SHIFT 12
83static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
84 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
85 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
86 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
87 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
88 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
89 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
90 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
91};
92
Eric Sandeen3972f262013-01-12 02:57:22 +000093static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050094static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040095static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050096static noinline int cow_file_range(struct inode *inode,
97 struct page *locked_page,
98 u64 start, u64 end, int *page_started,
99 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400100static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
101 u64 len, u64 orig_start,
102 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400103 u64 orig_block_len, u64 ram_bytes,
104 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400105
Eric Sandeen48a3b632013-04-25 20:41:01 +0000106static int btrfs_dirty_inode(struct inode *inode);
107
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000108static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500109 struct inode *inode, struct inode *dir,
110 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500111{
112 int err;
113
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000114 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500115 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500116 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500117 return err;
118}
119
Chris Masond352ac62008-09-29 15:18:18 -0400120/*
Chris Masonc8b97812008-10-29 14:49:59 -0400121 * this does all the hard work for inserting an inline extent into
122 * the btree. The caller should have done a btrfs_drop_extents so that
123 * no overlapping inline items exist in the btree
124 */
Chris Masond3977122009-01-05 21:25:51 -0500125static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400126 struct btrfs_root *root, struct inode *inode,
127 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000128 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct page **compressed_pages)
130{
131 struct btrfs_key key;
132 struct btrfs_path *path;
133 struct extent_buffer *leaf;
134 struct page *page = NULL;
135 char *kaddr;
136 unsigned long ptr;
137 struct btrfs_file_extent_item *ei;
138 int err = 0;
139 int ret;
140 size_t cur_size = size;
141 size_t datasize;
142 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400143
Li Zefanfe3f5662011-03-28 08:30:38 +0000144 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400145 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Chris Masond3977122009-01-05 21:25:51 -0500147 path = btrfs_alloc_path();
148 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400149 return -ENOMEM;
150
Chris Masonb9473432009-03-13 11:00:37 -0400151 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400152
Li Zefan33345d012011-04-20 10:31:50 +0800153 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400154 key.offset = start;
155 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 datasize = btrfs_file_extent_calc_inline_size(cur_size);
157
158 inode_add_bytes(inode, size);
159 ret = btrfs_insert_empty_item(trans, root, path, &key,
160 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400161 if (ret) {
162 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400163 goto fail;
164 }
165 leaf = path->nodes[0];
166 ei = btrfs_item_ptr(leaf, path->slots[0],
167 struct btrfs_file_extent_item);
168 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
169 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
170 btrfs_set_file_extent_encryption(leaf, ei, 0);
171 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
172 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
173 ptr = btrfs_file_extent_inline_start(ei);
174
Li Zefan261507a02010-12-17 14:21:50 +0800175 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400176 struct page *cpage;
177 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500178 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500180 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400181 PAGE_CACHE_SIZE);
182
Cong Wang7ac687d2011-11-25 23:14:28 +0800183 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400184 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400186
187 i++;
188 ptr += cur_size;
189 compressed_size -= cur_size;
190 }
191 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800192 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 } else {
194 page = find_get_page(inode->i_mapping,
195 start >> PAGE_CACHE_SHIFT);
196 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800197 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400198 offset = start & (PAGE_CACHE_SIZE - 1);
199 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 page_cache_release(page);
202 }
203 btrfs_mark_buffer_dirty(leaf);
204 btrfs_free_path(path);
205
Yan, Zhengc2167752009-11-12 09:34:21 +0000206 /*
207 * we're an inline extent, so nobody can
208 * extend the file past i_size without locking
209 * a page we already have locked.
210 *
211 * We must do any isize and inode updates
212 * before we unlock the pages. Otherwise we
213 * could end up racing with unlink.
214 */
Chris Masonc8b97812008-10-29 14:49:59 -0400215 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100216 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000217
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400219fail:
220 btrfs_free_path(path);
221 return err;
222}
223
224
225/*
226 * conditionally insert an inline extent into the file. This
227 * does the checks required to make sure the data is small enough
228 * to fit as an inline extent.
229 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400230static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400231 struct btrfs_root *root,
232 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000233 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400234 struct page **compressed_pages)
235{
236 u64 isize = i_size_read(inode);
237 u64 actual_end = min(end + 1, isize);
238 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000239 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 u64 data_len = inline_len;
241 int ret;
242
243 if (compressed_size)
244 data_len = compressed_size;
245
246 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400247 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400248 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
249 (!compressed_size &&
250 (actual_end & (root->sectorsize - 1)) == 0) ||
251 end + 1 < isize ||
252 data_len > root->fs_info->max_inline) {
253 return 1;
254 }
255
Josef Bacik26714852012-08-29 12:24:27 -0400256 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100257 if (ret)
258 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400259
260 if (isize > actual_end)
261 inline_len = min_t(u64, isize, actual_end);
262 ret = insert_inline_extent(trans, root, inode, start,
263 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000264 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 btrfs_abort_transaction(trans, root, ret);
267 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400268 } else if (ret == -ENOSPC) {
269 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100270 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400271
Josef Bacikbdc20e62013-02-28 13:23:38 -0500272 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400273 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400274 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400275 return 0;
276}
277
Chris Mason771ed682008-11-06 22:02:51 -0500278struct async_extent {
279 u64 start;
280 u64 ram_size;
281 u64 compressed_size;
282 struct page **pages;
283 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800284 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500285 struct list_head list;
286};
287
288struct async_cow {
289 struct inode *inode;
290 struct btrfs_root *root;
291 struct page *locked_page;
292 u64 start;
293 u64 end;
294 struct list_head extents;
295 struct btrfs_work work;
296};
297
298static noinline int add_async_extent(struct async_cow *cow,
299 u64 start, u64 ram_size,
300 u64 compressed_size,
301 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800302 unsigned long nr_pages,
303 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500304{
305 struct async_extent *async_extent;
306
307 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100308 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500309 async_extent->start = start;
310 async_extent->ram_size = ram_size;
311 async_extent->compressed_size = compressed_size;
312 async_extent->pages = pages;
313 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800314 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500315 list_add_tail(&async_extent->list, &cow->extents);
316 return 0;
317}
318
Chris Masonc8b97812008-10-29 14:49:59 -0400319/*
Chris Mason771ed682008-11-06 22:02:51 -0500320 * we create compressed extents in two phases. The first
321 * phase compresses a range of pages that have already been
322 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * This is done inside an ordered work queue, and the compression
325 * is spread across many cpus. The actual IO submission is step
326 * two, and the ordered work queue takes care of making sure that
327 * happens in the same order things were put onto the queue by
328 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400329 *
Chris Mason771ed682008-11-06 22:02:51 -0500330 * If this code finds it can't get good compression, it puts an
331 * entry onto the work queue to write the uncompressed bytes. This
332 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300333 * are written in the same order that the flusher thread sent them
334 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400335 */
Chris Mason771ed682008-11-06 22:02:51 -0500336static noinline int compress_file_range(struct inode *inode,
337 struct page *locked_page,
338 u64 start, u64 end,
339 struct async_cow *async_cow,
340 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400341{
342 struct btrfs_root *root = BTRFS_I(inode)->root;
343 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400344 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400345 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500347 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400348 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 struct page **pages = NULL;
350 unsigned long nr_pages;
351 unsigned long nr_pages_ret = 0;
352 unsigned long total_compressed = 0;
353 unsigned long total_in = 0;
354 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500355 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400356 int i;
357 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800358 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400359 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400360
Liu Bo4cb13e52012-03-29 09:57:45 -0400361 /* if this is a small write inside eof, kick off a defrag */
362 if ((end - start + 1) < 16 * 1024 &&
363 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400364 btrfs_add_inode_defrag(NULL, inode);
365
Chris Mason42dc7ba2008-12-15 11:44:56 -0500366 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400367again:
368 will_compress = 0;
369 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
370 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
371
Chris Masonf03d9301f2009-02-04 09:31:06 -0500372 /*
373 * we don't want to send crud past the end of i_size through
374 * compression, that's just a waste of CPU time. So, if the
375 * end of the file is before the start of our current
376 * requested range of bytes, we bail out to the uncompressed
377 * cleanup code that can deal with all of this.
378 *
379 * It isn't really the fastest way to fix things, but this is a
380 * very uncommon corner.
381 */
382 if (actual_end <= start)
383 goto cleanup_and_bail_uncompressed;
384
Chris Masonc8b97812008-10-29 14:49:59 -0400385 total_compressed = actual_end - start;
386
387 /* we want to make sure that amount of ram required to uncompress
388 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500389 * of a compressed extent to 128k. This is a crucial number
390 * because it also controls how easily we can spread reads across
391 * cpus for decompression.
392 *
393 * We also want to make sure the amount of IO required to do
394 * a random read is reasonably small, so we limit the size of
395 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400396 */
397 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000398 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500399 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400400 total_in = 0;
401 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400402
Chris Mason771ed682008-11-06 22:02:51 -0500403 /*
404 * we do compression for mount -o compress and when the
405 * inode has not been flagged as nocompress. This flag can
406 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400407 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200408 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500409 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000410 (BTRFS_I(inode)->force_compress) ||
411 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400412 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400413 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800414 if (!pages) {
415 /* just bail out to the uncompressed code */
416 goto cont;
417 }
Chris Mason179e29e2007-11-01 11:28:41 -0400418
Li Zefan261507a02010-12-17 14:21:50 +0800419 if (BTRFS_I(inode)->force_compress)
420 compress_type = BTRFS_I(inode)->force_compress;
421
Chris Mason4adaa612013-03-26 13:07:00 -0400422 /*
423 * we need to call clear_page_dirty_for_io on each
424 * page in the range. Otherwise applications with the file
425 * mmap'd can wander in and change the page contents while
426 * we are compressing them.
427 *
428 * If the compression fails for any reason, we set the pages
429 * dirty again later on.
430 */
431 extent_range_clear_dirty_for_io(inode, start, end);
432 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800433 ret = btrfs_compress_pages(compress_type,
434 inode->i_mapping, start,
435 total_compressed, pages,
436 nr_pages, &nr_pages_ret,
437 &total_in,
438 &total_compressed,
439 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400440
441 if (!ret) {
442 unsigned long offset = total_compressed &
443 (PAGE_CACHE_SIZE - 1);
444 struct page *page = pages[nr_pages_ret - 1];
445 char *kaddr;
446
447 /* zero the tail end of the last page, we might be
448 * sending it down to disk
449 */
450 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800451 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400452 memset(kaddr + offset, 0,
453 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800454 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400455 }
456 will_compress = 1;
457 }
458 }
Li Zefan560f7d72011-09-08 10:22:01 +0800459cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400460 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400461 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100462 if (IS_ERR(trans)) {
463 ret = PTR_ERR(trans);
464 trans = NULL;
465 goto cleanup_and_out;
466 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400467 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500468
Chris Masonc8b97812008-10-29 14:49:59 -0400469 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500470 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400471 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500472 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 */
474 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000475 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400476 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500477 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400478 ret = cow_file_range_inline(trans, root, inode,
479 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000480 total_compressed,
481 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100483 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500484 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100485 * inline extent creation worked or returned error,
486 * we don't need to create any more async work items.
487 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500488 */
Chris Masonc8b97812008-10-29 14:49:59 -0400489 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400490 &BTRFS_I(inode)->io_tree,
491 start, end, NULL,
492 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400493 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400494 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000495
496 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400497 goto free_pages_out;
498 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000499 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 }
501
502 if (will_compress) {
503 /*
504 * we aren't doing an inline extent round the compressed size
505 * up to a block size boundary so the allocator does sane
506 * things
507 */
Qu Wenruofda28322013-02-26 08:10:22 +0000508 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400509
510 /*
511 * one last check to make sure the compression is really a
512 * win, compare the page count read with the blocks on disk
513 */
Qu Wenruofda28322013-02-26 08:10:22 +0000514 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400515 if (total_compressed >= total_in) {
516 will_compress = 0;
517 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400518 num_bytes = total_in;
519 }
520 }
521 if (!will_compress && pages) {
522 /*
523 * the compression code ran but failed to make things smaller,
524 * free any pages it allocated and our page pointer array
525 */
526 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400527 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 page_cache_release(pages[i]);
529 }
530 kfree(pages);
531 pages = NULL;
532 total_compressed = 0;
533 nr_pages_ret = 0;
534
535 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500536 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
537 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500538 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500539 }
Chris Masonc8b97812008-10-29 14:49:59 -0400540 }
Chris Mason771ed682008-11-06 22:02:51 -0500541 if (will_compress) {
542 *num_added += 1;
543
544 /* the async work queues will take care of doing actual
545 * allocation on disk for these compressed pages,
546 * and will submit them to the elevator.
547 */
548 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800549 total_compressed, pages, nr_pages_ret,
550 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500551
Yan, Zheng24ae6362010-12-06 07:02:36 +0000552 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500553 start += num_bytes;
554 pages = NULL;
555 cond_resched();
556 goto again;
557 }
558 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500559cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500560 /*
561 * No compression, but we still need to write the pages in
562 * the file we've been given so far. redirty the locked
563 * page if it corresponds to our extent and set things up
564 * for the async work queue to run cow_file_range to do
565 * the normal delalloc dance
566 */
567 if (page_offset(locked_page) >= start &&
568 page_offset(locked_page) <= end) {
569 __set_page_dirty_nobuffers(locked_page);
570 /* unlocked later on in the async handlers */
571 }
Chris Mason4adaa612013-03-26 13:07:00 -0400572 if (redirty)
573 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800574 add_async_extent(async_cow, start, end - start + 1,
575 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500576 *num_added += 1;
577 }
578
579out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100580 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500581
582free_pages_out:
583 for (i = 0; i < nr_pages_ret; i++) {
584 WARN_ON(pages[i]->mapping);
585 page_cache_release(pages[i]);
586 }
Chris Masond3977122009-01-05 21:25:51 -0500587 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500588
589 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590
591cleanup_and_out:
592 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
593 start, end, NULL,
594 EXTENT_CLEAR_UNLOCK_PAGE |
595 EXTENT_CLEAR_DIRTY |
596 EXTENT_CLEAR_DELALLOC |
597 EXTENT_SET_WRITEBACK |
598 EXTENT_END_WRITEBACK);
599 if (!trans || IS_ERR(trans))
600 btrfs_error(root->fs_info, ret, "Failed to join transaction");
601 else
602 btrfs_abort_transaction(trans, root, ret);
603 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500604}
605
606/*
607 * phase two of compressed writeback. This is the ordered portion
608 * of the code, which only gets called in the order the work was
609 * queued. We walk all the async extents created by compress_file_range
610 * and send them down to the disk.
611 */
612static noinline int submit_compressed_extents(struct inode *inode,
613 struct async_cow *async_cow)
614{
615 struct async_extent *async_extent;
616 u64 alloc_hint = 0;
617 struct btrfs_trans_handle *trans;
618 struct btrfs_key ins;
619 struct extent_map *em;
620 struct btrfs_root *root = BTRFS_I(inode)->root;
621 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
622 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500623 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500624
625 if (list_empty(&async_cow->extents))
626 return 0;
627
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500628again:
Chris Masond3977122009-01-05 21:25:51 -0500629 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500630 async_extent = list_entry(async_cow->extents.next,
631 struct async_extent, list);
632 list_del(&async_extent->list);
633
634 io_tree = &BTRFS_I(inode)->io_tree;
635
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500636retry:
Chris Mason771ed682008-11-06 22:02:51 -0500637 /* did the compression code fall back to uncompressed IO? */
638 if (!async_extent->pages) {
639 int page_started = 0;
640 unsigned long nr_written = 0;
641
642 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000643 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100644 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500647 ret = cow_file_range(inode, async_cow->locked_page,
648 async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1,
651 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500652
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100653 /* JDM XXX */
654
Chris Mason771ed682008-11-06 22:02:51 -0500655 /*
656 * if page_started, cow_file_range inserted an
657 * inline extent and took care of all the unlocking
658 * and IO for us. Otherwise, we need to submit
659 * all those pages down to the drive.
660 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500661 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500662 extent_write_locked_range(io_tree,
663 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500664 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->ram_size - 1,
666 btrfs_get_extent,
667 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500668 else if (ret)
669 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500670 kfree(async_extent);
671 cond_resched();
672 continue;
673 }
674
675 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100676 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500677
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400678 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 if (IS_ERR(trans)) {
680 ret = PTR_ERR(trans);
681 } else {
682 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
683 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500684 async_extent->compressed_size,
685 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500686 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600687 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100688 btrfs_abort_transaction(trans, root, ret);
689 btrfs_end_transaction(trans, root);
690 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 if (ret) {
693 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 for (i = 0; i < async_extent->nr_pages; i++) {
696 WARN_ON(async_extent->pages[i]->mapping);
697 page_cache_release(async_extent->pages[i]);
698 }
699 kfree(async_extent->pages);
700 async_extent->nr_pages = 0;
701 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702
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 Masonb888db2b2007-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 Masonb888db2b2007-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,
David Sterba41074882013-04-29 13:38:46 +00001533 struct extent_state *state, unsigned long *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,
David Sterba41074882013-04-29 13:38:46 +00001577 struct extent_state *state,
1578 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001579{
Chris Mason75eff682008-12-15 15:54:40 -05001580 /*
1581 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001582 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001583 * bit, which is only set or cleared with irqs on
1584 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001585 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001586 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001587 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001588 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001589
Josef Bacik9e0baf62011-07-15 15:16:44 +00001590 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001592 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1593 spin_lock(&BTRFS_I(inode)->lock);
1594 BTRFS_I(inode)->outstanding_extents--;
1595 spin_unlock(&BTRFS_I(inode)->lock);
1596 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001597
1598 if (*bits & EXTENT_DO_ACCOUNTING)
1599 btrfs_delalloc_release_metadata(inode, len);
1600
Josef Bacik0cb59c92010-07-02 12:14:14 -04001601 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1602 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001604
Miao Xie963d6782013-01-29 10:10:51 +00001605 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1606 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001607 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001608 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001609 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001610 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1611 &BTRFS_I(inode)->runtime_flags)) {
1612 spin_lock(&root->fs_info->delalloc_lock);
1613 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1614 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1615 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1616 &BTRFS_I(inode)->runtime_flags);
1617 }
1618 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001619 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001620 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001621 }
Chris Mason291d6732008-01-29 15:55:23 -05001622}
1623
Chris Masond352ac62008-09-29 15:18:18 -04001624/*
1625 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1626 * we don't create bios that span stripes or chunks
1627 */
David Woodhouse64a16702009-07-15 23:29:37 +01001628int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001629 size_t size, struct bio *bio,
1630 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001631{
1632 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001633 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001634 u64 length = 0;
1635 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001636 int ret;
1637
Chris Mason771ed682008-11-06 22:02:51 -05001638 if (bio_flags & EXTENT_BIO_COMPRESSED)
1639 return 0;
1640
Chris Masonf2d8d742008-04-21 10:03:05 -04001641 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001642 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001643 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001644 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001645 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001646 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001647 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001648 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001649 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001650}
1651
Chris Masond352ac62008-09-29 15:18:18 -04001652/*
1653 * in order to insert checksums into the metadata in large chunks,
1654 * we wait until bio submission time. All the pages in the bio are
1655 * checksummed and sums are attached onto the ordered extent record.
1656 *
1657 * At IO completion time the cums attached on the ordered extent record
1658 * are inserted into the btree
1659 */
Chris Masond3977122009-01-05 21:25:51 -05001660static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1661 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001662 unsigned long bio_flags,
1663 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001664{
Chris Mason065631f2008-02-20 12:07:25 -05001665 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001666 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001667
Chris Masond20f7042008-12-08 16:58:54 -05001668 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001669 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001670 return 0;
1671}
Chris Masone0156402008-04-16 11:15:20 -04001672
Chris Mason4a69a412008-11-06 22:03:00 -05001673/*
1674 * in order to insert checksums into the metadata in large chunks,
1675 * we wait until bio submission time. All the pages in the bio are
1676 * checksummed and sums are attached onto the ordered extent record.
1677 *
1678 * At IO completion time the cums attached on the ordered extent record
1679 * are inserted into the btree
1680 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001681static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001682 int mirror_num, unsigned long bio_flags,
1683 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001684{
1685 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001686 int ret;
1687
1688 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1689 if (ret)
1690 bio_endio(bio, ret);
1691 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001692}
1693
Chris Masond352ac62008-09-29 15:18:18 -04001694/*
Chris Masoncad321a2008-12-17 14:51:42 -05001695 * extent_io.c submission hook. This does the right thing for csum calculation
1696 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001697 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001698static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001699 int mirror_num, unsigned long bio_flags,
1700 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001701{
1702 struct btrfs_root *root = BTRFS_I(inode)->root;
1703 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001704 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001705 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001706 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001707
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001708 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001709
Liu Bo83eea1f2012-07-10 05:28:39 -06001710 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001711 metadata = 2;
1712
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001713 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001714 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1715 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001716 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001717
Chris Masond20f7042008-12-08 16:58:54 -05001718 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001719 ret = btrfs_submit_compressed_read(inode, bio,
1720 mirror_num,
1721 bio_flags);
1722 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001723 } else if (!skip_sum) {
1724 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1725 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001726 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001727 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001728 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001729 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001730 /* csum items have already been cloned */
1731 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1732 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001733 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001734 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001735 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001736 bio_flags, bio_offset,
1737 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001738 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001739 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001740 } else if (!skip_sum) {
1741 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1742 if (ret)
1743 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001744 }
1745
Chris Mason0b86a832008-03-24 15:01:56 -04001746mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001747 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1748
1749out:
1750 if (ret < 0)
1751 bio_endio(bio, ret);
1752 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001753}
Chris Mason6885f302008-02-20 16:11:05 -05001754
Chris Masond352ac62008-09-29 15:18:18 -04001755/*
1756 * given a list of ordered sums record them in the inode. This happens
1757 * at IO completion time based on sums calculated at bio submission time.
1758 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001759static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001760 struct inode *inode, u64 file_offset,
1761 struct list_head *list)
1762{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001763 struct btrfs_ordered_sum *sum;
1764
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001765 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001766 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001767 btrfs_csum_file_blocks(trans,
1768 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001769 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001770 }
1771 return 0;
1772}
1773
Josef Bacik2ac55d42010-02-03 19:33:23 +00001774int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1775 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001776{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001777 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001778 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001779 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001780}
1781
Chris Masond352ac62008-09-29 15:18:18 -04001782/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001783struct btrfs_writepage_fixup {
1784 struct page *page;
1785 struct btrfs_work work;
1786};
1787
Christoph Hellwigb2950862008-12-02 09:54:17 -05001788static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001789{
1790 struct btrfs_writepage_fixup *fixup;
1791 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001792 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001793 struct page *page;
1794 struct inode *inode;
1795 u64 page_start;
1796 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001797 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001798
1799 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1800 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001801again:
Chris Mason247e7432008-07-17 12:53:51 -04001802 lock_page(page);
1803 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1804 ClearPageChecked(page);
1805 goto out_page;
1806 }
1807
1808 inode = page->mapping->host;
1809 page_start = page_offset(page);
1810 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1811
Josef Bacik2ac55d42010-02-03 19:33:23 +00001812 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001813 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001814
1815 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001816 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001817 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001818
1819 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1820 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001821 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1822 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001823 unlock_page(page);
1824 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001825 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001826 goto again;
1827 }
Chris Mason247e7432008-07-17 12:53:51 -04001828
Jeff Mahoney87826df2012-02-15 16:23:57 +01001829 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1830 if (ret) {
1831 mapping_set_error(page->mapping, ret);
1832 end_extent_writepage(page, ret, page_start, page_end);
1833 ClearPageChecked(page);
1834 goto out;
1835 }
1836
Josef Bacik2ac55d42010-02-03 19:33:23 +00001837 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001838 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001839 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001840out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001841 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1842 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001843out_page:
1844 unlock_page(page);
1845 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001846 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001847}
1848
1849/*
1850 * There are a few paths in the higher layers of the kernel that directly
1851 * set the page dirty bit without asking the filesystem if it is a
1852 * good idea. This causes problems because we want to make sure COW
1853 * properly happens and the data=ordered rules are followed.
1854 *
Chris Masonc8b97812008-10-29 14:49:59 -04001855 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001856 * hasn't been properly setup for IO. We kick off an async process
1857 * to fix it up. The async helper will wait for ordered extents, set
1858 * the delalloc bit and make it safe to write the page.
1859 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001860static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001861{
1862 struct inode *inode = page->mapping->host;
1863 struct btrfs_writepage_fixup *fixup;
1864 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001865
Chris Mason8b62b722009-09-02 16:53:46 -04001866 /* this page is properly in the ordered list */
1867 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001868 return 0;
1869
1870 if (PageChecked(page))
1871 return -EAGAIN;
1872
1873 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1874 if (!fixup)
1875 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001876
Chris Mason247e7432008-07-17 12:53:51 -04001877 SetPageChecked(page);
1878 page_cache_get(page);
1879 fixup->work.func = btrfs_writepage_fixup_worker;
1880 fixup->page = page;
1881 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001882 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001883}
1884
Yan Zhengd899e052008-10-30 14:25:28 -04001885static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1886 struct inode *inode, u64 file_pos,
1887 u64 disk_bytenr, u64 disk_num_bytes,
1888 u64 num_bytes, u64 ram_bytes,
1889 u8 compression, u8 encryption,
1890 u16 other_encoding, int extent_type)
1891{
1892 struct btrfs_root *root = BTRFS_I(inode)->root;
1893 struct btrfs_file_extent_item *fi;
1894 struct btrfs_path *path;
1895 struct extent_buffer *leaf;
1896 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001897 int ret;
1898
1899 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001900 if (!path)
1901 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001902
Chris Masonb9473432009-03-13 11:00:37 -04001903 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001904
1905 /*
1906 * we may be replacing one extent in the tree with another.
1907 * The new extent is pinned in the extent map, and we don't want
1908 * to drop it from the cache until it is completely in the btree.
1909 *
1910 * So, tell btrfs_drop_extents to leave this extent in the cache.
1911 * the caller is expected to unpin it and allow it to be merged
1912 * with the others.
1913 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001914 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001915 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 if (ret)
1917 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001918
Li Zefan33345d012011-04-20 10:31:50 +08001919 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001920 ins.offset = file_pos;
1921 ins.type = BTRFS_EXTENT_DATA_KEY;
1922 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923 if (ret)
1924 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001925 leaf = path->nodes[0];
1926 fi = btrfs_item_ptr(leaf, path->slots[0],
1927 struct btrfs_file_extent_item);
1928 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1929 btrfs_set_file_extent_type(leaf, fi, extent_type);
1930 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1931 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1932 btrfs_set_file_extent_offset(leaf, fi, 0);
1933 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1934 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1935 btrfs_set_file_extent_compression(leaf, fi, compression);
1936 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1937 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001938
Yan Zhengd899e052008-10-30 14:25:28 -04001939 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001940 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001941
1942 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001943
1944 ins.objectid = disk_bytenr;
1945 ins.offset = disk_num_bytes;
1946 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001947 ret = btrfs_alloc_reserved_file_extent(trans, root,
1948 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001949 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001950out:
Yan Zhengd899e052008-10-30 14:25:28 -04001951 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001952
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001953 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001954}
1955
Liu Bo38c227d2013-01-29 03:18:40 +00001956/* snapshot-aware defrag */
1957struct sa_defrag_extent_backref {
1958 struct rb_node node;
1959 struct old_sa_defrag_extent *old;
1960 u64 root_id;
1961 u64 inum;
1962 u64 file_pos;
1963 u64 extent_offset;
1964 u64 num_bytes;
1965 u64 generation;
1966};
1967
1968struct old_sa_defrag_extent {
1969 struct list_head list;
1970 struct new_sa_defrag_extent *new;
1971
1972 u64 extent_offset;
1973 u64 bytenr;
1974 u64 offset;
1975 u64 len;
1976 int count;
1977};
1978
1979struct new_sa_defrag_extent {
1980 struct rb_root root;
1981 struct list_head head;
1982 struct btrfs_path *path;
1983 struct inode *inode;
1984 u64 file_pos;
1985 u64 len;
1986 u64 bytenr;
1987 u64 disk_len;
1988 u8 compress_type;
1989};
1990
1991static int backref_comp(struct sa_defrag_extent_backref *b1,
1992 struct sa_defrag_extent_backref *b2)
1993{
1994 if (b1->root_id < b2->root_id)
1995 return -1;
1996 else if (b1->root_id > b2->root_id)
1997 return 1;
1998
1999 if (b1->inum < b2->inum)
2000 return -1;
2001 else if (b1->inum > b2->inum)
2002 return 1;
2003
2004 if (b1->file_pos < b2->file_pos)
2005 return -1;
2006 else if (b1->file_pos > b2->file_pos)
2007 return 1;
2008
2009 /*
2010 * [------------------------------] ===> (a range of space)
2011 * |<--->| |<---->| =============> (fs/file tree A)
2012 * |<---------------------------->| ===> (fs/file tree B)
2013 *
2014 * A range of space can refer to two file extents in one tree while
2015 * refer to only one file extent in another tree.
2016 *
2017 * So we may process a disk offset more than one time(two extents in A)
2018 * and locate at the same extent(one extent in B), then insert two same
2019 * backrefs(both refer to the extent in B).
2020 */
2021 return 0;
2022}
2023
2024static void backref_insert(struct rb_root *root,
2025 struct sa_defrag_extent_backref *backref)
2026{
2027 struct rb_node **p = &root->rb_node;
2028 struct rb_node *parent = NULL;
2029 struct sa_defrag_extent_backref *entry;
2030 int ret;
2031
2032 while (*p) {
2033 parent = *p;
2034 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2035
2036 ret = backref_comp(backref, entry);
2037 if (ret < 0)
2038 p = &(*p)->rb_left;
2039 else
2040 p = &(*p)->rb_right;
2041 }
2042
2043 rb_link_node(&backref->node, parent, p);
2044 rb_insert_color(&backref->node, root);
2045}
2046
2047/*
2048 * Note the backref might has changed, and in this case we just return 0.
2049 */
2050static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2051 void *ctx)
2052{
2053 struct btrfs_file_extent_item *extent;
2054 struct btrfs_fs_info *fs_info;
2055 struct old_sa_defrag_extent *old = ctx;
2056 struct new_sa_defrag_extent *new = old->new;
2057 struct btrfs_path *path = new->path;
2058 struct btrfs_key key;
2059 struct btrfs_root *root;
2060 struct sa_defrag_extent_backref *backref;
2061 struct extent_buffer *leaf;
2062 struct inode *inode = new->inode;
2063 int slot;
2064 int ret;
2065 u64 extent_offset;
2066 u64 num_bytes;
2067
2068 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2069 inum == btrfs_ino(inode))
2070 return 0;
2071
2072 key.objectid = root_id;
2073 key.type = BTRFS_ROOT_ITEM_KEY;
2074 key.offset = (u64)-1;
2075
2076 fs_info = BTRFS_I(inode)->root->fs_info;
2077 root = btrfs_read_fs_root_no_name(fs_info, &key);
2078 if (IS_ERR(root)) {
2079 if (PTR_ERR(root) == -ENOENT)
2080 return 0;
2081 WARN_ON(1);
2082 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2083 inum, offset, root_id);
2084 return PTR_ERR(root);
2085 }
2086
2087 key.objectid = inum;
2088 key.type = BTRFS_EXTENT_DATA_KEY;
2089 if (offset > (u64)-1 << 32)
2090 key.offset = 0;
2091 else
2092 key.offset = offset;
2093
2094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2095 if (ret < 0) {
2096 WARN_ON(1);
2097 return ret;
2098 }
2099
2100 while (1) {
2101 cond_resched();
2102
2103 leaf = path->nodes[0];
2104 slot = path->slots[0];
2105
2106 if (slot >= btrfs_header_nritems(leaf)) {
2107 ret = btrfs_next_leaf(root, path);
2108 if (ret < 0) {
2109 goto out;
2110 } else if (ret > 0) {
2111 ret = 0;
2112 goto out;
2113 }
2114 continue;
2115 }
2116
2117 path->slots[0]++;
2118
2119 btrfs_item_key_to_cpu(leaf, &key, slot);
2120
2121 if (key.objectid > inum)
2122 goto out;
2123
2124 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2125 continue;
2126
2127 extent = btrfs_item_ptr(leaf, slot,
2128 struct btrfs_file_extent_item);
2129
2130 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2131 continue;
2132
2133 extent_offset = btrfs_file_extent_offset(leaf, extent);
2134 if (key.offset - extent_offset != offset)
2135 continue;
2136
2137 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2138 if (extent_offset >= old->extent_offset + old->offset +
2139 old->len || extent_offset + num_bytes <=
2140 old->extent_offset + old->offset)
2141 continue;
2142
2143 break;
2144 }
2145
2146 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2147 if (!backref) {
2148 ret = -ENOENT;
2149 goto out;
2150 }
2151
2152 backref->root_id = root_id;
2153 backref->inum = inum;
2154 backref->file_pos = offset + extent_offset;
2155 backref->num_bytes = num_bytes;
2156 backref->extent_offset = extent_offset;
2157 backref->generation = btrfs_file_extent_generation(leaf, extent);
2158 backref->old = old;
2159 backref_insert(&new->root, backref);
2160 old->count++;
2161out:
2162 btrfs_release_path(path);
2163 WARN_ON(ret);
2164 return ret;
2165}
2166
2167static noinline bool record_extent_backrefs(struct btrfs_path *path,
2168 struct new_sa_defrag_extent *new)
2169{
2170 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2171 struct old_sa_defrag_extent *old, *tmp;
2172 int ret;
2173
2174 new->path = path;
2175
2176 list_for_each_entry_safe(old, tmp, &new->head, list) {
2177 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2178 path, record_one_backref,
2179 old);
2180 BUG_ON(ret < 0 && ret != -ENOENT);
2181
2182 /* no backref to be processed for this extent */
2183 if (!old->count) {
2184 list_del(&old->list);
2185 kfree(old);
2186 }
2187 }
2188
2189 if (list_empty(&new->head))
2190 return false;
2191
2192 return true;
2193}
2194
2195static int relink_is_mergable(struct extent_buffer *leaf,
2196 struct btrfs_file_extent_item *fi,
2197 u64 disk_bytenr)
2198{
2199 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2200 return 0;
2201
2202 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2203 return 0;
2204
2205 if (btrfs_file_extent_compression(leaf, fi) ||
2206 btrfs_file_extent_encryption(leaf, fi) ||
2207 btrfs_file_extent_other_encoding(leaf, fi))
2208 return 0;
2209
2210 return 1;
2211}
2212
2213/*
2214 * Note the backref might has changed, and in this case we just return 0.
2215 */
2216static noinline int relink_extent_backref(struct btrfs_path *path,
2217 struct sa_defrag_extent_backref *prev,
2218 struct sa_defrag_extent_backref *backref)
2219{
2220 struct btrfs_file_extent_item *extent;
2221 struct btrfs_file_extent_item *item;
2222 struct btrfs_ordered_extent *ordered;
2223 struct btrfs_trans_handle *trans;
2224 struct btrfs_fs_info *fs_info;
2225 struct btrfs_root *root;
2226 struct btrfs_key key;
2227 struct extent_buffer *leaf;
2228 struct old_sa_defrag_extent *old = backref->old;
2229 struct new_sa_defrag_extent *new = old->new;
2230 struct inode *src_inode = new->inode;
2231 struct inode *inode;
2232 struct extent_state *cached = NULL;
2233 int ret = 0;
2234 u64 start;
2235 u64 len;
2236 u64 lock_start;
2237 u64 lock_end;
2238 bool merge = false;
2239 int index;
2240
2241 if (prev && prev->root_id == backref->root_id &&
2242 prev->inum == backref->inum &&
2243 prev->file_pos + prev->num_bytes == backref->file_pos)
2244 merge = true;
2245
2246 /* step 1: get root */
2247 key.objectid = backref->root_id;
2248 key.type = BTRFS_ROOT_ITEM_KEY;
2249 key.offset = (u64)-1;
2250
2251 fs_info = BTRFS_I(src_inode)->root->fs_info;
2252 index = srcu_read_lock(&fs_info->subvol_srcu);
2253
2254 root = btrfs_read_fs_root_no_name(fs_info, &key);
2255 if (IS_ERR(root)) {
2256 srcu_read_unlock(&fs_info->subvol_srcu, index);
2257 if (PTR_ERR(root) == -ENOENT)
2258 return 0;
2259 return PTR_ERR(root);
2260 }
2261 if (btrfs_root_refs(&root->root_item) == 0) {
2262 srcu_read_unlock(&fs_info->subvol_srcu, index);
2263 /* parse ENOENT to 0 */
2264 return 0;
2265 }
2266
2267 /* step 2: get inode */
2268 key.objectid = backref->inum;
2269 key.type = BTRFS_INODE_ITEM_KEY;
2270 key.offset = 0;
2271
2272 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2273 if (IS_ERR(inode)) {
2274 srcu_read_unlock(&fs_info->subvol_srcu, index);
2275 return 0;
2276 }
2277
2278 srcu_read_unlock(&fs_info->subvol_srcu, index);
2279
2280 /* step 3: relink backref */
2281 lock_start = backref->file_pos;
2282 lock_end = backref->file_pos + backref->num_bytes - 1;
2283 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2284 0, &cached);
2285
2286 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2287 if (ordered) {
2288 btrfs_put_ordered_extent(ordered);
2289 goto out_unlock;
2290 }
2291
2292 trans = btrfs_join_transaction(root);
2293 if (IS_ERR(trans)) {
2294 ret = PTR_ERR(trans);
2295 goto out_unlock;
2296 }
2297
2298 key.objectid = backref->inum;
2299 key.type = BTRFS_EXTENT_DATA_KEY;
2300 key.offset = backref->file_pos;
2301
2302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2303 if (ret < 0) {
2304 goto out_free_path;
2305 } else if (ret > 0) {
2306 ret = 0;
2307 goto out_free_path;
2308 }
2309
2310 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2311 struct btrfs_file_extent_item);
2312
2313 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2314 backref->generation)
2315 goto out_free_path;
2316
2317 btrfs_release_path(path);
2318
2319 start = backref->file_pos;
2320 if (backref->extent_offset < old->extent_offset + old->offset)
2321 start += old->extent_offset + old->offset -
2322 backref->extent_offset;
2323
2324 len = min(backref->extent_offset + backref->num_bytes,
2325 old->extent_offset + old->offset + old->len);
2326 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2327
2328 ret = btrfs_drop_extents(trans, root, inode, start,
2329 start + len, 1);
2330 if (ret)
2331 goto out_free_path;
2332again:
2333 key.objectid = btrfs_ino(inode);
2334 key.type = BTRFS_EXTENT_DATA_KEY;
2335 key.offset = start;
2336
Liu Boa09a0a72013-03-11 09:20:58 +00002337 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002338 if (merge) {
2339 struct btrfs_file_extent_item *fi;
2340 u64 extent_len;
2341 struct btrfs_key found_key;
2342
2343 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2344 if (ret < 0)
2345 goto out_free_path;
2346
2347 path->slots[0]--;
2348 leaf = path->nodes[0];
2349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2350
2351 fi = btrfs_item_ptr(leaf, path->slots[0],
2352 struct btrfs_file_extent_item);
2353 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2354
2355 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2356 extent_len + found_key.offset == start) {
2357 btrfs_set_file_extent_num_bytes(leaf, fi,
2358 extent_len + len);
2359 btrfs_mark_buffer_dirty(leaf);
2360 inode_add_bytes(inode, len);
2361
2362 ret = 1;
2363 goto out_free_path;
2364 } else {
2365 merge = false;
2366 btrfs_release_path(path);
2367 goto again;
2368 }
2369 }
2370
2371 ret = btrfs_insert_empty_item(trans, root, path, &key,
2372 sizeof(*extent));
2373 if (ret) {
2374 btrfs_abort_transaction(trans, root, ret);
2375 goto out_free_path;
2376 }
2377
2378 leaf = path->nodes[0];
2379 item = btrfs_item_ptr(leaf, path->slots[0],
2380 struct btrfs_file_extent_item);
2381 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2382 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2383 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2384 btrfs_set_file_extent_num_bytes(leaf, item, len);
2385 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2386 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2387 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2388 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2389 btrfs_set_file_extent_encryption(leaf, item, 0);
2390 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2391
2392 btrfs_mark_buffer_dirty(leaf);
2393 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002394 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002395
2396 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2397 new->disk_len, 0,
2398 backref->root_id, backref->inum,
2399 new->file_pos, 0); /* start - extent_offset */
2400 if (ret) {
2401 btrfs_abort_transaction(trans, root, ret);
2402 goto out_free_path;
2403 }
2404
2405 ret = 1;
2406out_free_path:
2407 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002408 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002409 btrfs_end_transaction(trans, root);
2410out_unlock:
2411 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2412 &cached, GFP_NOFS);
2413 iput(inode);
2414 return ret;
2415}
2416
2417static void relink_file_extents(struct new_sa_defrag_extent *new)
2418{
2419 struct btrfs_path *path;
2420 struct old_sa_defrag_extent *old, *tmp;
2421 struct sa_defrag_extent_backref *backref;
2422 struct sa_defrag_extent_backref *prev = NULL;
2423 struct inode *inode;
2424 struct btrfs_root *root;
2425 struct rb_node *node;
2426 int ret;
2427
2428 inode = new->inode;
2429 root = BTRFS_I(inode)->root;
2430
2431 path = btrfs_alloc_path();
2432 if (!path)
2433 return;
2434
2435 if (!record_extent_backrefs(path, new)) {
2436 btrfs_free_path(path);
2437 goto out;
2438 }
2439 btrfs_release_path(path);
2440
2441 while (1) {
2442 node = rb_first(&new->root);
2443 if (!node)
2444 break;
2445 rb_erase(node, &new->root);
2446
2447 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2448
2449 ret = relink_extent_backref(path, prev, backref);
2450 WARN_ON(ret < 0);
2451
2452 kfree(prev);
2453
2454 if (ret == 1)
2455 prev = backref;
2456 else
2457 prev = NULL;
2458 cond_resched();
2459 }
2460 kfree(prev);
2461
2462 btrfs_free_path(path);
2463
2464 list_for_each_entry_safe(old, tmp, &new->head, list) {
2465 list_del(&old->list);
2466 kfree(old);
2467 }
2468out:
2469 atomic_dec(&root->fs_info->defrag_running);
2470 wake_up(&root->fs_info->transaction_wait);
2471
2472 kfree(new);
2473}
2474
2475static struct new_sa_defrag_extent *
2476record_old_file_extents(struct inode *inode,
2477 struct btrfs_ordered_extent *ordered)
2478{
2479 struct btrfs_root *root = BTRFS_I(inode)->root;
2480 struct btrfs_path *path;
2481 struct btrfs_key key;
2482 struct old_sa_defrag_extent *old, *tmp;
2483 struct new_sa_defrag_extent *new;
2484 int ret;
2485
2486 new = kmalloc(sizeof(*new), GFP_NOFS);
2487 if (!new)
2488 return NULL;
2489
2490 new->inode = inode;
2491 new->file_pos = ordered->file_offset;
2492 new->len = ordered->len;
2493 new->bytenr = ordered->start;
2494 new->disk_len = ordered->disk_len;
2495 new->compress_type = ordered->compress_type;
2496 new->root = RB_ROOT;
2497 INIT_LIST_HEAD(&new->head);
2498
2499 path = btrfs_alloc_path();
2500 if (!path)
2501 goto out_kfree;
2502
2503 key.objectid = btrfs_ino(inode);
2504 key.type = BTRFS_EXTENT_DATA_KEY;
2505 key.offset = new->file_pos;
2506
2507 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2508 if (ret < 0)
2509 goto out_free_path;
2510 if (ret > 0 && path->slots[0] > 0)
2511 path->slots[0]--;
2512
2513 /* find out all the old extents for the file range */
2514 while (1) {
2515 struct btrfs_file_extent_item *extent;
2516 struct extent_buffer *l;
2517 int slot;
2518 u64 num_bytes;
2519 u64 offset;
2520 u64 end;
2521 u64 disk_bytenr;
2522 u64 extent_offset;
2523
2524 l = path->nodes[0];
2525 slot = path->slots[0];
2526
2527 if (slot >= btrfs_header_nritems(l)) {
2528 ret = btrfs_next_leaf(root, path);
2529 if (ret < 0)
2530 goto out_free_list;
2531 else if (ret > 0)
2532 break;
2533 continue;
2534 }
2535
2536 btrfs_item_key_to_cpu(l, &key, slot);
2537
2538 if (key.objectid != btrfs_ino(inode))
2539 break;
2540 if (key.type != BTRFS_EXTENT_DATA_KEY)
2541 break;
2542 if (key.offset >= new->file_pos + new->len)
2543 break;
2544
2545 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2546
2547 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2548 if (key.offset + num_bytes < new->file_pos)
2549 goto next;
2550
2551 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2552 if (!disk_bytenr)
2553 goto next;
2554
2555 extent_offset = btrfs_file_extent_offset(l, extent);
2556
2557 old = kmalloc(sizeof(*old), GFP_NOFS);
2558 if (!old)
2559 goto out_free_list;
2560
2561 offset = max(new->file_pos, key.offset);
2562 end = min(new->file_pos + new->len, key.offset + num_bytes);
2563
2564 old->bytenr = disk_bytenr;
2565 old->extent_offset = extent_offset;
2566 old->offset = offset - key.offset;
2567 old->len = end - offset;
2568 old->new = new;
2569 old->count = 0;
2570 list_add_tail(&old->list, &new->head);
2571next:
2572 path->slots[0]++;
2573 cond_resched();
2574 }
2575
2576 btrfs_free_path(path);
2577 atomic_inc(&root->fs_info->defrag_running);
2578
2579 return new;
2580
2581out_free_list:
2582 list_for_each_entry_safe(old, tmp, &new->head, list) {
2583 list_del(&old->list);
2584 kfree(old);
2585 }
2586out_free_path:
2587 btrfs_free_path(path);
2588out_kfree:
2589 kfree(new);
2590 return NULL;
2591}
2592
Chris Mason5d13a982009-03-13 11:41:46 -04002593/*
2594 * helper function for btrfs_finish_ordered_io, this
2595 * just reads in some of the csum leaves to prime them into ram
2596 * before we start the transaction. It limits the amount of btree
2597 * reads required while inside the transaction.
2598 */
Chris Masond352ac62008-09-29 15:18:18 -04002599/* as ordered data IO finishes, this gets called so we can finish
2600 * an ordered extent if the range of bytes in the file it covers are
2601 * fully written.
2602 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002603static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002604{
Josef Bacik5fd02042012-05-02 14:00:54 -04002605 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002607 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002609 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002610 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002611 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002612 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002613 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002614
Liu Bo83eea1f2012-07-10 05:28:39 -06002615 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002616
Josef Bacik5fd02042012-05-02 14:00:54 -04002617 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2618 ret = -EIO;
2619 goto out;
2620 }
2621
Yan, Zhengc2167752009-11-12 09:34:21 +00002622 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002623 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002624 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2625 if (nolock)
2626 trans = btrfs_join_transaction_nolock(root);
2627 else
2628 trans = btrfs_join_transaction(root);
2629 if (IS_ERR(trans)) {
2630 ret = PTR_ERR(trans);
2631 trans = NULL;
2632 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002633 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002634 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2635 ret = btrfs_update_inode_fallback(trans, root, inode);
2636 if (ret) /* -ENOMEM or corruption */
2637 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002638 goto out;
2639 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002640
Josef Bacik2ac55d42010-02-03 19:33:23 +00002641 lock_extent_bits(io_tree, ordered_extent->file_offset,
2642 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002643 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002644
Liu Bo38c227d2013-01-29 03:18:40 +00002645 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2646 ordered_extent->file_offset + ordered_extent->len - 1,
2647 EXTENT_DEFRAG, 1, cached_state);
2648 if (ret) {
2649 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2650 if (last_snapshot >= BTRFS_I(inode)->generation)
2651 /* the inode is shared */
2652 new = record_old_file_extents(inode, ordered_extent);
2653
2654 clear_extent_bit(io_tree, ordered_extent->file_offset,
2655 ordered_extent->file_offset + ordered_extent->len - 1,
2656 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2657 }
2658
Josef Bacik0cb59c92010-07-02 12:14:14 -04002659 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002660 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002661 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002662 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002663 if (IS_ERR(trans)) {
2664 ret = PTR_ERR(trans);
2665 trans = NULL;
2666 goto out_unlock;
2667 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002668 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002669
Chris Masonc8b97812008-10-29 14:49:59 -04002670 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002671 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002672 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002673 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002674 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002675 ordered_extent->file_offset,
2676 ordered_extent->file_offset +
2677 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002678 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002679 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002680 ret = insert_reserved_file_extent(trans, inode,
2681 ordered_extent->file_offset,
2682 ordered_extent->start,
2683 ordered_extent->disk_len,
2684 ordered_extent->len,
2685 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002686 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002687 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002688 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002689 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2690 ordered_extent->file_offset, ordered_extent->len,
2691 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 if (ret < 0) {
2693 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002694 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002695 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002696
Chris Masone6dcd2d2008-07-17 12:53:50 -04002697 add_pending_csums(trans, inode, ordered_extent->file_offset,
2698 &ordered_extent->list);
2699
Josef Bacik6c760c02012-11-09 10:53:21 -05002700 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2701 ret = btrfs_update_inode_fallback(trans, root, inode);
2702 if (ret) { /* -ENOMEM or corruption */
2703 btrfs_abort_transaction(trans, root, ret);
2704 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002705 }
2706 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002707out_unlock:
2708 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2709 ordered_extent->file_offset +
2710 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002711out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002712 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002713 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002714 if (trans)
2715 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002716
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002717 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002718 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2719 ordered_extent->file_offset +
2720 ordered_extent->len - 1, NULL, GFP_NOFS);
2721
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002722 /*
2723 * If the ordered extent had an IOERR or something else went
2724 * wrong we need to return the space for this ordered extent
2725 * back to the allocator.
2726 */
2727 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2728 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2729 btrfs_free_reserved_extent(root, ordered_extent->start,
2730 ordered_extent->disk_len);
2731 }
2732
2733
Josef Bacik5fd02042012-05-02 14:00:54 -04002734 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002735 * This needs to be done to make sure anybody waiting knows we are done
2736 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002737 */
2738 btrfs_remove_ordered_extent(inode, ordered_extent);
2739
Liu Bo38c227d2013-01-29 03:18:40 +00002740 /* for snapshot-aware defrag */
2741 if (new)
2742 relink_file_extents(new);
2743
Chris Masone6dcd2d2008-07-17 12:53:50 -04002744 /* once for us */
2745 btrfs_put_ordered_extent(ordered_extent);
2746 /* once for the tree */
2747 btrfs_put_ordered_extent(ordered_extent);
2748
Josef Bacik5fd02042012-05-02 14:00:54 -04002749 return ret;
2750}
2751
2752static void finish_ordered_fn(struct btrfs_work *work)
2753{
2754 struct btrfs_ordered_extent *ordered_extent;
2755 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2756 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002757}
2758
Christoph Hellwigb2950862008-12-02 09:54:17 -05002759static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002760 struct extent_state *state, int uptodate)
2761{
Josef Bacik5fd02042012-05-02 14:00:54 -04002762 struct inode *inode = page->mapping->host;
2763 struct btrfs_root *root = BTRFS_I(inode)->root;
2764 struct btrfs_ordered_extent *ordered_extent = NULL;
2765 struct btrfs_workers *workers;
2766
liubo1abe9b82011-03-24 11:18:59 +00002767 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2768
Chris Mason8b62b722009-09-02 16:53:46 -04002769 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002770 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2771 end - start + 1, uptodate))
2772 return 0;
2773
2774 ordered_extent->work.func = finish_ordered_fn;
2775 ordered_extent->work.flags = 0;
2776
Liu Bo83eea1f2012-07-10 05:28:39 -06002777 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002778 workers = &root->fs_info->endio_freespace_worker;
2779 else
2780 workers = &root->fs_info->endio_write_workers;
2781 btrfs_queue_worker(workers, &ordered_extent->work);
2782
2783 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002784}
2785
Chris Masond352ac62008-09-29 15:18:18 -04002786/*
Chris Masond352ac62008-09-29 15:18:18 -04002787 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002788 * if there's a match, we allow the bio to finish. If not, the code in
2789 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002790 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002791static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002792 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002793{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002794 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002795 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002796 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002797 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002798 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002799 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002800 struct btrfs_root *root = BTRFS_I(inode)->root;
2801 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002802 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2803 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002804
Chris Masond20f7042008-12-08 16:58:54 -05002805 if (PageChecked(page)) {
2806 ClearPageChecked(page);
2807 goto good;
2808 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002809
2810 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002811 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002812
Yan Zheng17d217f2008-12-12 10:03:38 -05002813 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002814 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002815 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2816 GFP_NOFS);
2817 return 0;
2818 }
2819
Yanc2e639f2008-02-04 08:57:25 -05002820 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002821 private = state->private;
2822 ret = 0;
2823 } else {
2824 ret = get_state_private(io_tree, start, &private);
2825 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002826 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002827 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002828 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002829
Liu Bob0496682013-03-14 14:57:45 +00002830 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002831 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002832 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002833 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002834
Cong Wang7ac687d2011-11-25 23:14:28 +08002835 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002836good:
Chris Mason07157aa2007-08-30 08:50:51 -04002837 return 0;
2838
2839zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002840 if (__ratelimit(&_rs))
2841 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2842 (unsigned long long)btrfs_ino(page->mapping->host),
2843 (unsigned long long)start, csum,
2844 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002845 memset(kaddr + offset, 1, end - start + 1);
2846 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002847 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002848 if (private == 0)
2849 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002850 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002851}
Chris Masonb888db2b2007-08-27 16:49:44 -04002852
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002853struct delayed_iput {
2854 struct list_head list;
2855 struct inode *inode;
2856};
2857
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002858/* JDM: If this is fs-wide, why can't we add a pointer to
2859 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002860void btrfs_add_delayed_iput(struct inode *inode)
2861{
2862 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2863 struct delayed_iput *delayed;
2864
2865 if (atomic_add_unless(&inode->i_count, -1, 1))
2866 return;
2867
2868 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2869 delayed->inode = inode;
2870
2871 spin_lock(&fs_info->delayed_iput_lock);
2872 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2873 spin_unlock(&fs_info->delayed_iput_lock);
2874}
2875
2876void btrfs_run_delayed_iputs(struct btrfs_root *root)
2877{
2878 LIST_HEAD(list);
2879 struct btrfs_fs_info *fs_info = root->fs_info;
2880 struct delayed_iput *delayed;
2881 int empty;
2882
2883 spin_lock(&fs_info->delayed_iput_lock);
2884 empty = list_empty(&fs_info->delayed_iputs);
2885 spin_unlock(&fs_info->delayed_iput_lock);
2886 if (empty)
2887 return;
2888
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002889 spin_lock(&fs_info->delayed_iput_lock);
2890 list_splice_init(&fs_info->delayed_iputs, &list);
2891 spin_unlock(&fs_info->delayed_iput_lock);
2892
2893 while (!list_empty(&list)) {
2894 delayed = list_entry(list.next, struct delayed_iput, list);
2895 list_del(&delayed->list);
2896 iput(delayed->inode);
2897 kfree(delayed);
2898 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002899}
2900
Yan, Zhengd68fc572010-05-16 10:49:58 -04002901/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002902 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002903 * files in the subvolume, it removes orphan item and frees block_rsv
2904 * structure.
2905 */
2906void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2907 struct btrfs_root *root)
2908{
Josef Bacik90290e12011-12-02 15:44:12 -05002909 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002910 int ret;
2911
Josef Bacik8a35d952012-05-23 14:26:42 -04002912 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002913 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2914 return;
2915
Josef Bacik90290e12011-12-02 15:44:12 -05002916 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002917 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002918 spin_unlock(&root->orphan_lock);
2919 return;
2920 }
2921
2922 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2923 spin_unlock(&root->orphan_lock);
2924 return;
2925 }
2926
2927 block_rsv = root->orphan_block_rsv;
2928 root->orphan_block_rsv = NULL;
2929 spin_unlock(&root->orphan_lock);
2930
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931 if (root->orphan_item_inserted &&
2932 btrfs_root_refs(&root->root_item) > 0) {
2933 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2934 root->root_key.objectid);
2935 BUG_ON(ret);
2936 root->orphan_item_inserted = 0;
2937 }
2938
Josef Bacik90290e12011-12-02 15:44:12 -05002939 if (block_rsv) {
2940 WARN_ON(block_rsv->size > 0);
2941 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942 }
2943}
2944
2945/*
Josef Bacik7b128762008-07-24 12:17:14 -04002946 * This creates an orphan entry for the given inode in case something goes
2947 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948 *
2949 * NOTE: caller of this function should reserve 5 units of metadata for
2950 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002951 */
2952int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2953{
2954 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002955 struct btrfs_block_rsv *block_rsv = NULL;
2956 int reserve = 0;
2957 int insert = 0;
2958 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002959
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002961 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002962 if (!block_rsv)
2963 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002964 }
2965
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 spin_lock(&root->orphan_lock);
2967 if (!root->orphan_block_rsv) {
2968 root->orphan_block_rsv = block_rsv;
2969 } else if (block_rsv) {
2970 btrfs_free_block_rsv(root, block_rsv);
2971 block_rsv = NULL;
2972 }
Josef Bacik7b128762008-07-24 12:17:14 -04002973
Josef Bacik8a35d952012-05-23 14:26:42 -04002974 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2975 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002976#if 0
2977 /*
2978 * For proper ENOSPC handling, we should do orphan
2979 * cleanup when mounting. But this introduces backward
2980 * compatibility issue.
2981 */
2982 if (!xchg(&root->orphan_item_inserted, 1))
2983 insert = 2;
2984 else
2985 insert = 1;
2986#endif
2987 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002988 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002989 }
Josef Bacik7b128762008-07-24 12:17:14 -04002990
Josef Bacik72ac3c02012-05-23 14:13:11 -04002991 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2992 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002995
Yan, Zhengd68fc572010-05-16 10:49:58 -04002996 /* grab metadata reservation from transaction handle */
2997 if (reserve) {
2998 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002999 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003000 }
3001
3002 /* insert an orphan item to track this unlinked/truncated file */
3003 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003004 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003005 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003006 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3007 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003008 btrfs_abort_transaction(trans, root, ret);
3009 return ret;
3010 }
3011 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003012 }
3013
3014 /* insert an orphan item to track subvolume contains orphan files */
3015 if (insert >= 2) {
3016 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3017 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 if (ret && ret != -EEXIST) {
3019 btrfs_abort_transaction(trans, root, ret);
3020 return ret;
3021 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003022 }
3023 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003024}
3025
3026/*
3027 * We have done the truncate/delete so we can go ahead and remove the orphan
3028 * item for this particular inode.
3029 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003030static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3031 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003032{
3033 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003034 int delete_item = 0;
3035 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003036 int ret = 0;
3037
Yan, Zhengd68fc572010-05-16 10:49:58 -04003038 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003039 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3040 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003041 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003042
Josef Bacik72ac3c02012-05-23 14:13:11 -04003043 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3044 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003045 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 spin_unlock(&root->orphan_lock);
3047
3048 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003049 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003050 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003051 }
3052
Josef Bacik8a35d952012-05-23 14:26:42 -04003053 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003054 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003055 atomic_dec(&root->orphan_inodes);
3056 }
Josef Bacik7b128762008-07-24 12:17:14 -04003057
Yan, Zhengd68fc572010-05-16 10:49:58 -04003058 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003059}
3060
3061/*
3062 * this cleans up any orphans that may be left on the list from the last use
3063 * of this root.
3064 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003065int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003066{
3067 struct btrfs_path *path;
3068 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003069 struct btrfs_key key, found_key;
3070 struct btrfs_trans_handle *trans;
3071 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003072 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003073 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3074
Yan, Zhengd68fc572010-05-16 10:49:58 -04003075 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003076 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003077
3078 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003079 if (!path) {
3080 ret = -ENOMEM;
3081 goto out;
3082 }
Josef Bacik7b128762008-07-24 12:17:14 -04003083 path->reada = -1;
3084
3085 key.objectid = BTRFS_ORPHAN_OBJECTID;
3086 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3087 key.offset = (u64)-1;
3088
Josef Bacik7b128762008-07-24 12:17:14 -04003089 while (1) {
3090 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003091 if (ret < 0)
3092 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003093
3094 /*
3095 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003096 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003097 * find the key and see if we have stuff that matches
3098 */
3099 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003100 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003101 if (path->slots[0] == 0)
3102 break;
3103 path->slots[0]--;
3104 }
3105
3106 /* pull out the item */
3107 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003108 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3109
3110 /* make sure the item matches what we want */
3111 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3112 break;
3113 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3114 break;
3115
3116 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003117 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003118
3119 /*
3120 * this is where we are basically btrfs_lookup, without the
3121 * crossing root thing. we store the inode number in the
3122 * offset of the orphan item.
3123 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003124
3125 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003126 btrfs_err(root->fs_info,
3127 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003128 ret = -EINVAL;
3129 goto out;
3130 }
3131
3132 last_objectid = found_key.offset;
3133
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003134 found_key.objectid = found_key.offset;
3135 found_key.type = BTRFS_INODE_ITEM_KEY;
3136 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003137 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003138 ret = PTR_RET(inode);
3139 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003140 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003141
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003142 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3143 struct btrfs_root *dead_root;
3144 struct btrfs_fs_info *fs_info = root->fs_info;
3145 int is_dead_root = 0;
3146
3147 /*
3148 * this is an orphan in the tree root. Currently these
3149 * could come from 2 sources:
3150 * a) a snapshot deletion in progress
3151 * b) a free space cache inode
3152 * We need to distinguish those two, as the snapshot
3153 * orphan must not get deleted.
3154 * find_dead_roots already ran before us, so if this
3155 * is a snapshot deletion, we should find the root
3156 * in the dead_roots list
3157 */
3158 spin_lock(&fs_info->trans_lock);
3159 list_for_each_entry(dead_root, &fs_info->dead_roots,
3160 root_list) {
3161 if (dead_root->root_key.objectid ==
3162 found_key.objectid) {
3163 is_dead_root = 1;
3164 break;
3165 }
3166 }
3167 spin_unlock(&fs_info->trans_lock);
3168 if (is_dead_root) {
3169 /* prevent this orphan from being found again */
3170 key.offset = found_key.objectid - 1;
3171 continue;
3172 }
3173 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003174 /*
3175 * Inode is already gone but the orphan item is still there,
3176 * kill the orphan item.
3177 */
3178 if (ret == -ESTALE) {
3179 trans = btrfs_start_transaction(root, 1);
3180 if (IS_ERR(trans)) {
3181 ret = PTR_ERR(trans);
3182 goto out;
3183 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003184 btrfs_debug(root->fs_info, "auto deleting %Lu",
3185 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003186 ret = btrfs_del_orphan_item(trans, root,
3187 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003188 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003189 btrfs_end_transaction(trans, root);
3190 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003191 }
Josef Bacik7b128762008-07-24 12:17:14 -04003192
Josef Bacik7b128762008-07-24 12:17:14 -04003193 /*
3194 * add this inode to the orphan list so btrfs_orphan_del does
3195 * the proper thing when we hit it
3196 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003197 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3198 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003199 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003200
Josef Bacik7b128762008-07-24 12:17:14 -04003201 /* if we have links, this was a truncate, lets do that */
3202 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003203 if (!S_ISREG(inode->i_mode)) {
3204 WARN_ON(1);
3205 iput(inode);
3206 continue;
3207 }
Josef Bacik7b128762008-07-24 12:17:14 -04003208 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003209
3210 /* 1 for the orphan item deletion. */
3211 trans = btrfs_start_transaction(root, 1);
3212 if (IS_ERR(trans)) {
3213 ret = PTR_ERR(trans);
3214 goto out;
3215 }
3216 ret = btrfs_orphan_add(trans, inode);
3217 btrfs_end_transaction(trans, root);
3218 if (ret)
3219 goto out;
3220
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003221 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003222 if (ret)
3223 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003224 } else {
3225 nr_unlink++;
3226 }
3227
3228 /* this will do delete_inode and everything for us */
3229 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003230 if (ret)
3231 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003232 }
Miao Xie3254c872011-11-10 20:45:05 -05003233 /* release the path since we're done with it */
3234 btrfs_release_path(path);
3235
Yan, Zhengd68fc572010-05-16 10:49:58 -04003236 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3237
3238 if (root->orphan_block_rsv)
3239 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3240 (u64)-1);
3241
3242 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003243 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003244 if (!IS_ERR(trans))
3245 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246 }
Josef Bacik7b128762008-07-24 12:17:14 -04003247
3248 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003249 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003250 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003251 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003252
3253out:
3254 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003255 btrfs_crit(root->fs_info,
3256 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003257 btrfs_free_path(path);
3258 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003259}
3260
Chris Masond352ac62008-09-29 15:18:18 -04003261/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003262 * very simple check to peek ahead in the leaf looking for xattrs. If we
3263 * don't find any xattrs, we know there can't be any acls.
3264 *
3265 * slot is the slot the inode is in, objectid is the objectid of the inode
3266 */
3267static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3268 int slot, u64 objectid)
3269{
3270 u32 nritems = btrfs_header_nritems(leaf);
3271 struct btrfs_key found_key;
3272 int scanned = 0;
3273
3274 slot++;
3275 while (slot < nritems) {
3276 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3277
3278 /* we found a different objectid, there must not be acls */
3279 if (found_key.objectid != objectid)
3280 return 0;
3281
3282 /* we found an xattr, assume we've got an acl */
3283 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
3284 return 1;
3285
3286 /*
3287 * we found a key greater than an xattr key, there can't
3288 * be any acls later on
3289 */
3290 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3291 return 0;
3292
3293 slot++;
3294 scanned++;
3295
3296 /*
3297 * it goes inode, inode backrefs, xattrs, extents,
3298 * so if there are a ton of hard links to an inode there can
3299 * be a lot of backrefs. Don't waste time searching too hard,
3300 * this is just an optimization
3301 */
3302 if (scanned >= 8)
3303 break;
3304 }
3305 /* we hit the end of the leaf before we found an xattr or
3306 * something larger than an xattr. We have to assume the inode
3307 * has acls
3308 */
3309 return 1;
3310}
3311
3312/*
Chris Masond352ac62008-09-29 15:18:18 -04003313 * read an inode from the btree into the in-memory inode
3314 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003315static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003316{
3317 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003318 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003319 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003320 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003321 struct btrfs_root *root = BTRFS_I(inode)->root;
3322 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003323 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003324 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003325 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003326 bool filled = false;
3327
3328 ret = btrfs_fill_inode(inode, &rdev);
3329 if (!ret)
3330 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003331
3332 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003333 if (!path)
3334 goto make_bad;
3335
Josef Bacikd90c7322011-05-17 09:50:54 -04003336 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003337 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003338
Chris Mason39279cc2007-06-12 06:35:45 -04003339 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003340 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003341 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003342
Chris Mason5f39d392007-10-15 16:14:19 -04003343 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003344
3345 if (filled)
3346 goto cache_acl;
3347
Chris Mason5f39d392007-10-15 16:14:19 -04003348 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3349 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003350 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003351 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003352 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3353 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003354 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003355
3356 tspec = btrfs_inode_atime(inode_item);
3357 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3358 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3359
3360 tspec = btrfs_inode_mtime(inode_item);
3361 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3362 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3363
3364 tspec = btrfs_inode_ctime(inode_item);
3365 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3366 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3367
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003368 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003369 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003370 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3371
3372 /*
3373 * If we were modified in the current generation and evicted from memory
3374 * and then re-read we need to do a full sync since we don't have any
3375 * idea about which extents were modified before we were evicted from
3376 * cache.
3377 */
3378 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3379 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3380 &BTRFS_I(inode)->runtime_flags);
3381
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003382 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003383 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003384 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003385 rdev = btrfs_inode_rdev(leaf, inode_item);
3386
Josef Bacikaec74772008-07-24 12:12:38 -04003387 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003388 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003389cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003390 /*
3391 * try to precache a NULL acl entry for files that don't have
3392 * any xattrs or acls
3393 */
Li Zefan33345d012011-04-20 10:31:50 +08003394 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3395 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003396 if (!maybe_acls)
3397 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003398
Chris Mason39279cc2007-06-12 06:35:45 -04003399 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003400
Chris Mason39279cc2007-06-12 06:35:45 -04003401 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003402 case S_IFREG:
3403 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003404 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003405 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003406 inode->i_fop = &btrfs_file_operations;
3407 inode->i_op = &btrfs_file_inode_operations;
3408 break;
3409 case S_IFDIR:
3410 inode->i_fop = &btrfs_dir_file_operations;
3411 if (root == root->fs_info->tree_root)
3412 inode->i_op = &btrfs_dir_ro_inode_operations;
3413 else
3414 inode->i_op = &btrfs_dir_inode_operations;
3415 break;
3416 case S_IFLNK:
3417 inode->i_op = &btrfs_symlink_inode_operations;
3418 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003419 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003420 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003421 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003422 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003423 init_special_inode(inode, inode->i_mode, rdev);
3424 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003425 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003426
3427 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003428 return;
3429
3430make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003431 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003432 make_bad_inode(inode);
3433}
3434
Chris Masond352ac62008-09-29 15:18:18 -04003435/*
3436 * given a leaf and an inode, copy the inode fields into the leaf
3437 */
Chris Masone02119d2008-09-05 16:13:11 -04003438static void fill_inode_item(struct btrfs_trans_handle *trans,
3439 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003440 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003441 struct inode *inode)
3442{
Liu Bo51fab692012-12-27 09:01:21 +00003443 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003444
Liu Bo51fab692012-12-27 09:01:21 +00003445 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003446
Liu Bo51fab692012-12-27 09:01:21 +00003447 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3448 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3449 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3450 &token);
3451 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3452 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003453
Liu Bo51fab692012-12-27 09:01:21 +00003454 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3455 inode->i_atime.tv_sec, &token);
3456 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3457 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003458
Liu Bo51fab692012-12-27 09:01:21 +00003459 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3460 inode->i_mtime.tv_sec, &token);
3461 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3462 inode->i_mtime.tv_nsec, &token);
3463
3464 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3465 inode->i_ctime.tv_sec, &token);
3466 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3467 inode->i_ctime.tv_nsec, &token);
3468
3469 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3470 &token);
3471 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3472 &token);
3473 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3474 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3475 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3476 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3477 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003478}
3479
Chris Masond352ac62008-09-29 15:18:18 -04003480/*
3481 * copy everything in the in-memory inode into the btree.
3482 */
Chris Mason21151332011-11-10 20:39:08 -05003483static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003484 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003485{
3486 struct btrfs_inode_item *inode_item;
3487 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003488 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003489 int ret;
3490
3491 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003492 if (!path)
3493 return -ENOMEM;
3494
Chris Masonb9473432009-03-13 11:00:37 -04003495 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003496 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3497 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003498 if (ret) {
3499 if (ret > 0)
3500 ret = -ENOENT;
3501 goto failed;
3502 }
3503
Chris Masonb4ce94d2009-02-04 09:25:08 -05003504 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003505 leaf = path->nodes[0];
3506 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003507 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003508
Chris Masone02119d2008-09-05 16:13:11 -04003509 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003510 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003511 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003512 ret = 0;
3513failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003514 btrfs_free_path(path);
3515 return ret;
3516}
3517
Chris Masond352ac62008-09-29 15:18:18 -04003518/*
Chris Mason21151332011-11-10 20:39:08 -05003519 * copy everything in the in-memory inode into the btree.
3520 */
3521noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3522 struct btrfs_root *root, struct inode *inode)
3523{
3524 int ret;
3525
3526 /*
3527 * If the inode is a free space inode, we can deadlock during commit
3528 * if we put it into the delayed code.
3529 *
3530 * The data relocation inode should also be directly updated
3531 * without delay
3532 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003533 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003534 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003535 btrfs_update_root_times(trans, root);
3536
Chris Mason21151332011-11-10 20:39:08 -05003537 ret = btrfs_delayed_update_inode(trans, root, inode);
3538 if (!ret)
3539 btrfs_set_inode_last_trans(trans, inode);
3540 return ret;
3541 }
3542
3543 return btrfs_update_inode_item(trans, root, inode);
3544}
3545
Josef Bacikbe6aef62012-10-22 15:43:12 -04003546noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3547 struct btrfs_root *root,
3548 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003549{
3550 int ret;
3551
3552 ret = btrfs_update_inode(trans, root, inode);
3553 if (ret == -ENOSPC)
3554 return btrfs_update_inode_item(trans, root, inode);
3555 return ret;
3556}
3557
3558/*
Chris Masond352ac62008-09-29 15:18:18 -04003559 * unlink helper that gets used here in inode.c and in the tree logging
3560 * recovery code. It remove a link in a directory with a given name, and
3561 * also drops the back refs in the inode to the directory
3562 */
Al Viro92986792011-03-04 17:14:37 +00003563static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3564 struct btrfs_root *root,
3565 struct inode *dir, struct inode *inode,
3566 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003567{
3568 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003569 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003570 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003571 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003572 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003573 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003574 u64 ino = btrfs_ino(inode);
3575 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003576
3577 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003578 if (!path) {
3579 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003580 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003581 }
3582
Chris Masonb9473432009-03-13 11:00:37 -04003583 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003584 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003585 name, name_len, -1);
3586 if (IS_ERR(di)) {
3587 ret = PTR_ERR(di);
3588 goto err;
3589 }
3590 if (!di) {
3591 ret = -ENOENT;
3592 goto err;
3593 }
Chris Mason5f39d392007-10-15 16:14:19 -04003594 leaf = path->nodes[0];
3595 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003596 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003597 if (ret)
3598 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003599 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003600
Li Zefan33345d012011-04-20 10:31:50 +08003601 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3602 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003603 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003604 btrfs_info(root->fs_info,
3605 "failed to delete reference to %.*s, inode %llu parent %llu",
3606 name_len, name,
3607 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003608 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003609 goto err;
3610 }
3611
Miao Xie16cdcec2011-04-22 18:12:22 +08003612 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003613 if (ret) {
3614 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003615 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003616 }
Chris Mason39279cc2007-06-12 06:35:45 -04003617
Chris Masone02119d2008-09-05 16:13:11 -04003618 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003619 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003620 if (ret != 0 && ret != -ENOENT) {
3621 btrfs_abort_transaction(trans, root, ret);
3622 goto err;
3623 }
Chris Masone02119d2008-09-05 16:13:11 -04003624
3625 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3626 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003627 if (ret == -ENOENT)
3628 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003629 else if (ret)
3630 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003631err:
3632 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003633 if (ret)
3634 goto out;
3635
3636 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003637 inode_inc_iversion(inode);
3638 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003639 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003640 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003641out:
Chris Mason39279cc2007-06-12 06:35:45 -04003642 return ret;
3643}
3644
Al Viro92986792011-03-04 17:14:37 +00003645int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3646 struct btrfs_root *root,
3647 struct inode *dir, struct inode *inode,
3648 const char *name, int name_len)
3649{
3650 int ret;
3651 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3652 if (!ret) {
3653 btrfs_drop_nlink(inode);
3654 ret = btrfs_update_inode(trans, root, inode);
3655 }
3656 return ret;
3657}
3658
3659
Yan, Zhenga22285a2010-05-16 10:48:46 -04003660/* helper to check if there is any shared block in the path */
3661static int check_path_shared(struct btrfs_root *root,
3662 struct btrfs_path *path)
3663{
3664 struct extent_buffer *eb;
3665 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00003666 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667
3668 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00003669 int ret;
3670
Yan, Zhenga22285a2010-05-16 10:48:46 -04003671 if (!path->nodes[level])
3672 break;
3673 eb = path->nodes[level];
3674 if (!btrfs_block_can_be_shared(root, eb))
3675 continue;
Josef Bacik3173a182013-03-07 14:22:04 -05003676 ret = btrfs_lookup_extent_info(NULL, root, eb->start, level, 1,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003677 &refs, NULL);
3678 if (refs > 1)
3679 return 1;
3680 }
Josef Bacikdedefd72011-01-24 21:43:18 +00003681 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682}
3683
3684/*
3685 * helper to start transaction for unlink and rmdir.
3686 *
3687 * unlink and rmdir are special in btrfs, they do not always free space.
3688 * so in enospc case, we should make sure they will free space before
3689 * allowing them to use the global metadata reservation.
3690 */
3691static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3692 struct dentry *dentry)
3693{
3694 struct btrfs_trans_handle *trans;
3695 struct btrfs_root *root = BTRFS_I(dir)->root;
3696 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003697 struct btrfs_dir_item *di;
3698 struct inode *inode = dentry->d_inode;
3699 u64 index;
3700 int check_link = 1;
3701 int err = -ENOSPC;
3702 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003703 u64 ino = btrfs_ino(inode);
3704 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003705
Josef Bacike70bea52011-10-11 14:18:24 -04003706 /*
3707 * 1 for the possible orphan item
3708 * 1 for the dir item
3709 * 1 for the dir index
3710 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003711 * 1 for the inode
3712 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003713 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003714 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3715 return trans;
3716
Li Zefan33345d012011-04-20 10:31:50 +08003717 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718 return ERR_PTR(-ENOSPC);
3719
3720 /* check if there is someone else holds reference */
3721 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3722 return ERR_PTR(-ENOSPC);
3723
3724 if (atomic_read(&inode->i_count) > 2)
3725 return ERR_PTR(-ENOSPC);
3726
3727 if (xchg(&root->fs_info->enospc_unlink, 1))
3728 return ERR_PTR(-ENOSPC);
3729
3730 path = btrfs_alloc_path();
3731 if (!path) {
3732 root->fs_info->enospc_unlink = 0;
3733 return ERR_PTR(-ENOMEM);
3734 }
3735
Josef Bacik3880a1b2011-10-14 14:46:51 -04003736 /* 1 for the orphan item */
3737 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003738 if (IS_ERR(trans)) {
3739 btrfs_free_path(path);
3740 root->fs_info->enospc_unlink = 0;
3741 return trans;
3742 }
3743
3744 path->skip_locking = 1;
3745 path->search_commit_root = 1;
3746
3747 ret = btrfs_lookup_inode(trans, root, path,
3748 &BTRFS_I(dir)->location, 0);
3749 if (ret < 0) {
3750 err = ret;
3751 goto out;
3752 }
3753 if (ret == 0) {
3754 if (check_path_shared(root, path))
3755 goto out;
3756 } else {
3757 check_link = 0;
3758 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003759 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003760
3761 ret = btrfs_lookup_inode(trans, root, path,
3762 &BTRFS_I(inode)->location, 0);
3763 if (ret < 0) {
3764 err = ret;
3765 goto out;
3766 }
3767 if (ret == 0) {
3768 if (check_path_shared(root, path))
3769 goto out;
3770 } else {
3771 check_link = 0;
3772 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003773 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774
3775 if (ret == 0 && S_ISREG(inode->i_mode)) {
3776 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003777 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003778 if (ret < 0) {
3779 err = ret;
3780 goto out;
3781 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003782 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003783 if (check_path_shared(root, path))
3784 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003785 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003786 }
3787
3788 if (!check_link) {
3789 err = 0;
3790 goto out;
3791 }
3792
Li Zefan33345d012011-04-20 10:31:50 +08003793 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003794 dentry->d_name.name, dentry->d_name.len, 0);
3795 if (IS_ERR(di)) {
3796 err = PTR_ERR(di);
3797 goto out;
3798 }
3799 if (di) {
3800 if (check_path_shared(root, path))
3801 goto out;
3802 } else {
3803 err = 0;
3804 goto out;
3805 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003806 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003807
Mark Fashehf1863732012-08-08 11:32:27 -07003808 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3809 dentry->d_name.len, ino, dir_ino, 0,
3810 &index);
3811 if (ret) {
3812 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003813 goto out;
3814 }
Mark Fashehf1863732012-08-08 11:32:27 -07003815
Yan, Zhenga22285a2010-05-16 10:48:46 -04003816 if (check_path_shared(root, path))
3817 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003818
David Sterbab3b4aa72011-04-21 01:20:15 +02003819 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003820
Miao Xie16cdcec2011-04-22 18:12:22 +08003821 /*
3822 * This is a commit root search, if we can lookup inode item and other
3823 * relative items in the commit root, it means the transaction of
3824 * dir/file creation has been committed, and the dir index item that we
3825 * delay to insert has also been inserted into the commit root. So
3826 * we needn't worry about the delayed insertion of the dir index item
3827 * here.
3828 */
Li Zefan33345d012011-04-20 10:31:50 +08003829 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003830 dentry->d_name.name, dentry->d_name.len, 0);
3831 if (IS_ERR(di)) {
3832 err = PTR_ERR(di);
3833 goto out;
3834 }
3835 BUG_ON(ret == -ENOENT);
3836 if (check_path_shared(root, path))
3837 goto out;
3838
3839 err = 0;
3840out:
3841 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003842 /* Migrate the orphan reservation over */
3843 if (!err)
3844 err = btrfs_block_rsv_migrate(trans->block_rsv,
3845 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003846 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003847
Yan, Zhenga22285a2010-05-16 10:48:46 -04003848 if (err) {
3849 btrfs_end_transaction(trans, root);
3850 root->fs_info->enospc_unlink = 0;
3851 return ERR_PTR(err);
3852 }
3853
3854 trans->block_rsv = &root->fs_info->global_block_rsv;
3855 return trans;
3856}
3857
3858static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3859 struct btrfs_root *root)
3860{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003861 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003862 btrfs_block_rsv_release(root, trans->block_rsv,
3863 trans->bytes_reserved);
3864 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003865 BUG_ON(!root->fs_info->enospc_unlink);
3866 root->fs_info->enospc_unlink = 0;
3867 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003868 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003869}
3870
Chris Mason39279cc2007-06-12 06:35:45 -04003871static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3872{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003873 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003875 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003876 int ret;
3877
Yan, Zhenga22285a2010-05-16 10:48:46 -04003878 trans = __unlink_start_trans(dir, dentry);
3879 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003880 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003881
Chris Mason12fcfd22009-03-24 10:24:20 -04003882 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3883
Chris Masone02119d2008-09-05 16:13:11 -04003884 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3885 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003886 if (ret)
3887 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003888
Yan, Zhenga22285a2010-05-16 10:48:46 -04003889 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003890 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003891 if (ret)
3892 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003893 }
Josef Bacik7b128762008-07-24 12:17:14 -04003894
Tsutomu Itohb5324022011-07-19 07:27:20 +00003895out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003896 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003897 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003898 return ret;
3899}
3900
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003901int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3902 struct btrfs_root *root,
3903 struct inode *dir, u64 objectid,
3904 const char *name, int name_len)
3905{
3906 struct btrfs_path *path;
3907 struct extent_buffer *leaf;
3908 struct btrfs_dir_item *di;
3909 struct btrfs_key key;
3910 u64 index;
3911 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003912 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003913
3914 path = btrfs_alloc_path();
3915 if (!path)
3916 return -ENOMEM;
3917
Li Zefan33345d012011-04-20 10:31:50 +08003918 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003919 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003920 if (IS_ERR_OR_NULL(di)) {
3921 if (!di)
3922 ret = -ENOENT;
3923 else
3924 ret = PTR_ERR(di);
3925 goto out;
3926 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003927
3928 leaf = path->nodes[0];
3929 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3930 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3931 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003932 if (ret) {
3933 btrfs_abort_transaction(trans, root, ret);
3934 goto out;
3935 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003936 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003937
3938 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3939 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003940 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003941 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003942 if (ret != -ENOENT) {
3943 btrfs_abort_transaction(trans, root, ret);
3944 goto out;
3945 }
Li Zefan33345d012011-04-20 10:31:50 +08003946 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003947 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003948 if (IS_ERR_OR_NULL(di)) {
3949 if (!di)
3950 ret = -ENOENT;
3951 else
3952 ret = PTR_ERR(di);
3953 btrfs_abort_transaction(trans, root, ret);
3954 goto out;
3955 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003956
3957 leaf = path->nodes[0];
3958 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003959 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003960 index = key.offset;
3961 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003962 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003963
Miao Xie16cdcec2011-04-22 18:12:22 +08003964 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003965 if (ret) {
3966 btrfs_abort_transaction(trans, root, ret);
3967 goto out;
3968 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003969
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003970 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003971 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003972 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003973 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 if (ret)
3975 btrfs_abort_transaction(trans, root, ret);
3976out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003977 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003978 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979}
3980
Chris Mason39279cc2007-06-12 06:35:45 -04003981static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3982{
3983 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003984 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003986 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003987
David Sterbab3ae2442012-09-13 16:04:34 -06003988 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003989 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003990 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3991 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003992
Yan, Zhenga22285a2010-05-16 10:48:46 -04003993 trans = __unlink_start_trans(dir, dentry);
3994 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003995 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003996
Li Zefan33345d012011-04-20 10:31:50 +08003997 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003998 err = btrfs_unlink_subvol(trans, root, dir,
3999 BTRFS_I(inode)->location.objectid,
4000 dentry->d_name.name,
4001 dentry->d_name.len);
4002 goto out;
4003 }
4004
Josef Bacik7b128762008-07-24 12:17:14 -04004005 err = btrfs_orphan_add(trans, inode);
4006 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004007 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004008
Chris Mason39279cc2007-06-12 06:35:45 -04004009 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004010 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4011 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004012 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004013 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004014out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04004015 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004016 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004017
Chris Mason39279cc2007-06-12 06:35:45 -04004018 return err;
4019}
4020
Chris Mason323ac952008-10-01 19:05:46 -04004021/*
Chris Mason39279cc2007-06-12 06:35:45 -04004022 * this can truncate away extent items, csum items and directory items.
4023 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004024 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004025 *
4026 * csum items that cross the new i_size are truncated to the new size
4027 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004028 *
4029 * min_type is the minimum key type to truncate down to. If set to 0, this
4030 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004031 */
Yan, Zheng80825102009-11-12 09:35:36 +00004032int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4033 struct btrfs_root *root,
4034 struct inode *inode,
4035 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004036{
Chris Mason39279cc2007-06-12 06:35:45 -04004037 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004038 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004039 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004040 struct btrfs_key key;
4041 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004042 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004043 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004044 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004045 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004046 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004047 int found_extent;
4048 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004049 int pending_del_nr = 0;
4050 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004051 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004052 int ret;
4053 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004054 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004055
4056 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004057
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004058 path = btrfs_alloc_path();
4059 if (!path)
4060 return -ENOMEM;
4061 path->reada = -1;
4062
Josef Bacik5dc562c2012-08-17 13:14:17 -04004063 /*
4064 * We want to drop from the next block forward in case this new size is
4065 * not block aligned since we will be keeping the last block of the
4066 * extent just the way it is.
4067 */
Josef Bacik0af3d002010-06-21 14:48:16 -04004068 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004069 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4070 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004071
Miao Xie16cdcec2011-04-22 18:12:22 +08004072 /*
4073 * This function is also used to drop the items in the log tree before
4074 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4075 * it is used to drop the loged items. So we shouldn't kill the delayed
4076 * items.
4077 */
4078 if (min_type == 0 && root == BTRFS_I(inode)->root)
4079 btrfs_kill_delayed_inode_items(inode);
4080
Li Zefan33345d012011-04-20 10:31:50 +08004081 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004082 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004083 key.type = (u8)-1;
4084
Chris Mason85e21ba2008-01-29 15:11:36 -05004085search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004086 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004087 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004088 if (ret < 0) {
4089 err = ret;
4090 goto out;
4091 }
Chris Masond3977122009-01-05 21:25:51 -05004092
Chris Mason85e21ba2008-01-29 15:11:36 -05004093 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004094 /* there are no items in the tree for us to truncate, we're
4095 * done
4096 */
Yan, Zheng80825102009-11-12 09:35:36 +00004097 if (path->slots[0] == 0)
4098 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004099 path->slots[0]--;
4100 }
4101
Chris Masond3977122009-01-05 21:25:51 -05004102 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004103 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004104 leaf = path->nodes[0];
4105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4106 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04004107
Li Zefan33345d012011-04-20 10:31:50 +08004108 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004109 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004110
Chris Mason85e21ba2008-01-29 15:11:36 -05004111 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004112 break;
4113
Chris Mason5f39d392007-10-15 16:14:19 -04004114 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004115 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004116 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004117 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004118 extent_type = btrfs_file_extent_type(leaf, fi);
4119 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004120 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004121 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004122 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004123 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004124 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004125 }
Yan008630c2007-11-07 13:31:09 -05004126 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004127 }
Yan, Zheng80825102009-11-12 09:35:36 +00004128 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004129 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004130 } else {
4131 if (item_end < new_size)
4132 break;
4133 if (found_key.offset >= new_size)
4134 del_item = 1;
4135 else
4136 del_item = 0;
4137 }
Chris Mason39279cc2007-06-12 06:35:45 -04004138 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004139 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004140 if (found_type != BTRFS_EXTENT_DATA_KEY)
4141 goto delete;
4142
4143 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004144 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004145 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004146 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004147 u64 orig_num_bytes =
4148 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004149 extent_num_bytes = ALIGN(new_size -
4150 found_key.offset,
4151 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004152 btrfs_set_file_extent_num_bytes(leaf, fi,
4153 extent_num_bytes);
4154 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004155 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004156 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004157 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004158 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004159 } else {
Chris Masondb945352007-10-15 16:15:53 -04004160 extent_num_bytes =
4161 btrfs_file_extent_disk_num_bytes(leaf,
4162 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004163 extent_offset = found_key.offset -
4164 btrfs_file_extent_offset(leaf, fi);
4165
Chris Mason39279cc2007-06-12 06:35:45 -04004166 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004167 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004168 if (extent_start != 0) {
4169 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004170 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004171 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004172 }
4173 }
Chris Mason90692182008-02-08 13:49:28 -05004174 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004175 /*
4176 * we can't truncate inline items that have had
4177 * special encodings
4178 */
4179 if (!del_item &&
4180 btrfs_file_extent_compression(leaf, fi) == 0 &&
4181 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4182 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004183 u32 size = new_size - found_key.offset;
4184
4185 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004186 inode_sub_bytes(inode, item_end + 1 -
4187 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004188 }
4189 size =
4190 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004191 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004192 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004193 inode_sub_bytes(inode, item_end + 1 -
4194 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004195 }
Chris Mason39279cc2007-06-12 06:35:45 -04004196 }
Chris Mason179e29e2007-11-01 11:28:41 -04004197delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004198 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004199 if (!pending_del_nr) {
4200 /* no pending yet, add ourselves */
4201 pending_del_slot = path->slots[0];
4202 pending_del_nr = 1;
4203 } else if (pending_del_nr &&
4204 path->slots[0] + 1 == pending_del_slot) {
4205 /* hop on the pending chunk */
4206 pending_del_nr++;
4207 pending_del_slot = path->slots[0];
4208 } else {
Chris Masond3977122009-01-05 21:25:51 -05004209 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004210 }
Chris Mason39279cc2007-06-12 06:35:45 -04004211 } else {
4212 break;
4213 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004214 if (found_extent && (root->ref_cows ||
4215 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004216 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004217 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004218 extent_num_bytes, 0,
4219 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004220 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004221 BUG_ON(ret);
4222 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004223
Yan, Zheng80825102009-11-12 09:35:36 +00004224 if (found_type == BTRFS_INODE_ITEM_KEY)
4225 break;
4226
4227 if (path->slots[0] == 0 ||
4228 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004229 if (pending_del_nr) {
4230 ret = btrfs_del_items(trans, root, path,
4231 pending_del_slot,
4232 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 if (ret) {
4234 btrfs_abort_transaction(trans,
4235 root, ret);
4236 goto error;
4237 }
Yan, Zheng80825102009-11-12 09:35:36 +00004238 pending_del_nr = 0;
4239 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004240 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004241 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004242 } else {
4243 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004244 }
Chris Mason39279cc2007-06-12 06:35:45 -04004245 }
Yan, Zheng80825102009-11-12 09:35:36 +00004246out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004247 if (pending_del_nr) {
4248 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4249 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004250 if (ret)
4251 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004252 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004253error:
Chris Mason39279cc2007-06-12 06:35:45 -04004254 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004255 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004256}
4257
Chris Masona52d9a82007-08-27 16:49:44 -04004258/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004259 * btrfs_truncate_page - read, zero a chunk and write a page
4260 * @inode - inode that we're zeroing
4261 * @from - the offset to start zeroing
4262 * @len - the length to zero, 0 to zero the entire range respective to the
4263 * offset
4264 * @front - zero up to the offset instead of from the offset on
4265 *
4266 * This will find the page for the "from" offset and cow the page and zero the
4267 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004268 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004269int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4270 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004271{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004272 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004273 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004274 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4275 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004276 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004277 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004278 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004279 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4280 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4281 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004282 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004283 int ret = 0;
4284 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004285 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004286
Josef Bacik2aaa6652012-08-29 14:27:18 -04004287 if ((offset & (blocksize - 1)) == 0 &&
4288 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004289 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004290 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004291 if (ret)
4292 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004293
Chris Mason211c17f2008-05-15 09:13:45 -04004294again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004295 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004296 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004297 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004298 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004299 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004300 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004301
4302 page_start = page_offset(page);
4303 page_end = page_start + PAGE_CACHE_SIZE - 1;
4304
Chris Masona52d9a82007-08-27 16:49:44 -04004305 if (!PageUptodate(page)) {
4306 ret = btrfs_readpage(NULL, page);
4307 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004308 if (page->mapping != mapping) {
4309 unlock_page(page);
4310 page_cache_release(page);
4311 goto again;
4312 }
Chris Masona52d9a82007-08-27 16:49:44 -04004313 if (!PageUptodate(page)) {
4314 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004315 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004316 }
4317 }
Chris Mason211c17f2008-05-15 09:13:45 -04004318 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004319
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004320 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004321 set_page_extent_mapped(page);
4322
4323 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4324 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004325 unlock_extent_cached(io_tree, page_start, page_end,
4326 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004327 unlock_page(page);
4328 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004329 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004330 btrfs_put_ordered_extent(ordered);
4331 goto again;
4332 }
4333
Josef Bacik2ac55d42010-02-03 19:33:23 +00004334 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004335 EXTENT_DIRTY | EXTENT_DELALLOC |
4336 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004337 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004338
Josef Bacik2ac55d42010-02-03 19:33:23 +00004339 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4340 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004341 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004342 unlock_extent_cached(io_tree, page_start, page_end,
4343 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004344 goto out_unlock;
4345 }
4346
Chris Masone6dcd2d2008-07-17 12:53:50 -04004347 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004348 if (!len)
4349 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004350 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004351 if (front)
4352 memset(kaddr, 0, offset);
4353 else
4354 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004355 flush_dcache_page(page);
4356 kunmap(page);
4357 }
Chris Mason247e7432008-07-17 12:53:51 -04004358 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004359 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004360 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4361 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004362
Chris Mason89642222008-07-24 09:41:53 -04004363out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004364 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004365 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004366 unlock_page(page);
4367 page_cache_release(page);
4368out:
4369 return ret;
4370}
4371
Josef Bacik695a0d02011-03-04 15:46:53 -05004372/*
4373 * This function puts in dummy file extents for the area we're creating a hole
4374 * for. So if we are truncating this file to a larger size we need to insert
4375 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4376 * the range between oldsize and size
4377 */
Josef Bacika41ad392011-01-31 15:30:16 -05004378int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004379{
4380 struct btrfs_trans_handle *trans;
4381 struct btrfs_root *root = BTRFS_I(inode)->root;
4382 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004383 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004384 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004385 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004386 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4387 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004388 u64 last_byte;
4389 u64 cur_offset;
4390 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004391 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004392
4393 if (size <= hole_start)
4394 return 0;
4395
Yan Zheng9036c102008-10-30 14:19:41 -04004396 while (1) {
4397 struct btrfs_ordered_extent *ordered;
4398 btrfs_wait_ordered_range(inode, hole_start,
4399 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004400 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004401 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004402 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4403 if (!ordered)
4404 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004405 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4406 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004407 btrfs_put_ordered_extent(ordered);
4408 }
4409
Yan Zheng9036c102008-10-30 14:19:41 -04004410 cur_offset = hole_start;
4411 while (1) {
4412 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4413 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004414 if (IS_ERR(em)) {
4415 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004416 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004417 break;
4418 }
Yan Zheng9036c102008-10-30 14:19:41 -04004419 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004420 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004421 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004422 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004423 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004424
Miao Xie36423202011-12-14 20:12:02 -05004425 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004426 if (IS_ERR(trans)) {
4427 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004428 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004429 }
Yan, Zheng80825102009-11-12 09:35:36 +00004430
Josef Bacik5dc562c2012-08-17 13:14:17 -04004431 err = btrfs_drop_extents(trans, root, inode,
4432 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004433 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004434 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004435 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004436 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004437 break;
Miao Xie5b397372011-09-11 10:52:24 -04004438 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004439
Yan Zheng9036c102008-10-30 14:19:41 -04004440 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004441 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004442 0, hole_size, 0, hole_size,
4443 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004444 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004445 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004446 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004447 break;
Miao Xie5b397372011-09-11 10:52:24 -04004448 }
Yan, Zheng80825102009-11-12 09:35:36 +00004449
Josef Bacik5dc562c2012-08-17 13:14:17 -04004450 btrfs_drop_extent_cache(inode, cur_offset,
4451 cur_offset + hole_size - 1, 0);
4452 hole_em = alloc_extent_map();
4453 if (!hole_em) {
4454 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4455 &BTRFS_I(inode)->runtime_flags);
4456 goto next;
4457 }
4458 hole_em->start = cur_offset;
4459 hole_em->len = hole_size;
4460 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004461
Josef Bacik5dc562c2012-08-17 13:14:17 -04004462 hole_em->block_start = EXTENT_MAP_HOLE;
4463 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004464 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004465 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004466 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4467 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4468 hole_em->generation = trans->transid;
4469
4470 while (1) {
4471 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004472 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004473 write_unlock(&em_tree->lock);
4474 if (err != -EEXIST)
4475 break;
4476 btrfs_drop_extent_cache(inode, cur_offset,
4477 cur_offset +
4478 hole_size - 1, 0);
4479 }
4480 free_extent_map(hole_em);
4481next:
Miao Xie36423202011-12-14 20:12:02 -05004482 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004483 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004484 }
4485 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004486 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004487 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004488 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004489 break;
4490 }
4491
Yan, Zhenga22285a2010-05-16 10:48:46 -04004492 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004493 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4494 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004495 return err;
4496}
4497
Eric Sandeen3972f262013-01-12 02:57:22 +00004498static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004499{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004500 struct btrfs_root *root = BTRFS_I(inode)->root;
4501 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004502 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004503 loff_t newsize = attr->ia_size;
4504 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004505 int ret;
4506
Josef Bacika41ad392011-01-31 15:30:16 -05004507 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004508 return 0;
4509
Eric Sandeen3972f262013-01-12 02:57:22 +00004510 /*
4511 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4512 * special case where we need to update the times despite not having
4513 * these flags set. For all other operations the VFS set these flags
4514 * explicitly if it wants a timestamp update.
4515 */
4516 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4517 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4518
Josef Bacika41ad392011-01-31 15:30:16 -05004519 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004520 truncate_pagecache(inode, oldsize, newsize);
4521 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004522 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004523 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004524
Miao Xief4a2f4c2011-12-14 20:12:01 -05004525 trans = btrfs_start_transaction(root, 1);
4526 if (IS_ERR(trans))
4527 return PTR_ERR(trans);
4528
4529 i_size_write(inode, newsize);
4530 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4531 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004532 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004533 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004534
Josef Bacika41ad392011-01-31 15:30:16 -05004535 /*
4536 * We're truncating a file that used to have good data down to
4537 * zero. Make sure it gets into the ordered flush list so that
4538 * any new writes get down to disk quickly.
4539 */
4540 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004541 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4542 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004543
Josef Bacikf3fe8202013-01-07 17:03:21 -05004544 /*
4545 * 1 for the orphan item we're going to add
4546 * 1 for the orphan item deletion.
4547 */
4548 trans = btrfs_start_transaction(root, 2);
4549 if (IS_ERR(trans))
4550 return PTR_ERR(trans);
4551
4552 /*
4553 * We need to do this in case we fail at _any_ point during the
4554 * actual truncate. Once we do the truncate_setsize we could
4555 * invalidate pages which forces any outstanding ordered io to
4556 * be instantly completed which will give us extents that need
4557 * to be truncated. If we fail to get an orphan inode down we
4558 * could have left over extents that were never meant to live,
4559 * so we need to garuntee from this point on that everything
4560 * will be consistent.
4561 */
4562 ret = btrfs_orphan_add(trans, inode);
4563 btrfs_end_transaction(trans, root);
4564 if (ret)
4565 return ret;
4566
Josef Bacika41ad392011-01-31 15:30:16 -05004567 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4568 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004569
4570 /* Disable nonlocked read DIO to avoid the end less truncate */
4571 btrfs_inode_block_unlocked_dio(inode);
4572 inode_dio_wait(inode);
4573 btrfs_inode_resume_unlocked_dio(inode);
4574
Josef Bacika41ad392011-01-31 15:30:16 -05004575 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004576 if (ret && inode->i_nlink)
4577 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004578 }
4579
Josef Bacika41ad392011-01-31 15:30:16 -05004580 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004581}
4582
Chris Mason39279cc2007-06-12 06:35:45 -04004583static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4584{
4585 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004586 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004587 int err;
4588
Li Zefanb83cc962010-12-20 16:04:08 +08004589 if (btrfs_root_readonly(root))
4590 return -EROFS;
4591
Chris Mason39279cc2007-06-12 06:35:45 -04004592 err = inode_change_ok(inode, attr);
4593 if (err)
4594 return err;
4595
Chris Mason5a3f23d2009-03-31 13:27:11 -04004596 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004597 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004598 if (err)
4599 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004600 }
Yan Zheng9036c102008-10-30 14:19:41 -04004601
Christoph Hellwig10257742010-06-04 11:30:02 +02004602 if (attr->ia_valid) {
4603 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004604 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004605 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004606
Josef Bacik22c44fe2011-11-30 10:45:38 -05004607 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004608 err = btrfs_acl_chmod(inode);
4609 }
4610
Chris Mason39279cc2007-06-12 06:35:45 -04004611 return err;
4612}
Chris Mason61295eb2008-01-14 16:24:38 -05004613
Al Virobd555972010-06-07 11:35:40 -04004614void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004615{
4616 struct btrfs_trans_handle *trans;
4617 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004618 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004619 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004620 int ret;
4621
liubo1abe9b82011-03-24 11:18:59 +00004622 trace_btrfs_inode_evict(inode);
4623
Chris Mason39279cc2007-06-12 06:35:45 -04004624 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004625 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004626 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004627 goto no_delete;
4628
Chris Mason39279cc2007-06-12 06:35:45 -04004629 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004630 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 goto no_delete;
4632 }
Al Virobd555972010-06-07 11:35:40 -04004633 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004634 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004635
Yan, Zhengc71bf092009-11-12 09:34:40 +00004636 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004637 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004638 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004639 goto no_delete;
4640 }
4641
Yan, Zheng76dda932009-09-21 16:00:26 -04004642 if (inode->i_nlink > 0) {
4643 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4644 goto no_delete;
4645 }
4646
Miao Xie0e8c36a2012-12-19 06:59:51 +00004647 ret = btrfs_commit_inode_delayed_inode(inode);
4648 if (ret) {
4649 btrfs_orphan_del(NULL, inode);
4650 goto no_delete;
4651 }
4652
Miao Xie66d8f3d2012-09-06 04:02:28 -06004653 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004654 if (!rsv) {
4655 btrfs_orphan_del(NULL, inode);
4656 goto no_delete;
4657 }
Josef Bacik4a338542011-08-29 11:01:31 -04004658 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004659 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004660 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004661
Chris Masondbe674a2008-07-17 12:54:05 -04004662 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004663
Josef Bacik4289a662011-08-05 13:22:24 -04004664 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004665 * This is a bit simpler than btrfs_truncate since we've already
4666 * reserved our space for our orphan item in the unlink, so we just
4667 * need to reserve some slack space in case we add bytes and update
4668 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004669 */
Yan, Zheng80825102009-11-12 09:35:36 +00004670 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004671 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4672 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004673
Josef Bacik726c35f2011-09-26 15:46:06 -04004674 /*
4675 * Try and steal from the global reserve since we will
4676 * likely not use this space anyway, we want to try as
4677 * hard as possible to get this to work.
4678 */
4679 if (ret)
4680 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4681
Yan, Zhengd68fc572010-05-16 10:49:58 -04004682 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004683 btrfs_warn(root->fs_info,
4684 "Could not get space for a delete, will truncate on mount %d",
4685 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004686 btrfs_orphan_del(NULL, inode);
4687 btrfs_free_block_rsv(root, rsv);
4688 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004689 }
4690
Miao Xie0e8c36a2012-12-19 06:59:51 +00004691 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004692 if (IS_ERR(trans)) {
4693 btrfs_orphan_del(NULL, inode);
4694 btrfs_free_block_rsv(root, rsv);
4695 goto no_delete;
4696 }
4697
4698 trans->block_rsv = rsv;
4699
Yan, Zhengd68fc572010-05-16 10:49:58 -04004700 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004701 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004702 break;
4703
Miao Xie8407aa42012-09-07 01:43:32 -06004704 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004705 btrfs_end_transaction(trans, root);
4706 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004707 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004708 }
4709
Josef Bacik4289a662011-08-05 13:22:24 -04004710 btrfs_free_block_rsv(root, rsv);
4711
Yan, Zheng80825102009-11-12 09:35:36 +00004712 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004713 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004714 ret = btrfs_orphan_del(trans, inode);
4715 BUG_ON(ret);
4716 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004717
Josef Bacik4289a662011-08-05 13:22:24 -04004718 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004719 if (!(root == root->fs_info->tree_root ||
4720 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004721 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004722
Chris Mason54aa1f42007-06-22 14:16:25 -04004723 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004724 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004725no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004726 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004727 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004728}
4729
4730/*
4731 * this returns the key found in the dir entry in the location pointer.
4732 * If no dir entries were found, location->objectid is 0.
4733 */
4734static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4735 struct btrfs_key *location)
4736{
4737 const char *name = dentry->d_name.name;
4738 int namelen = dentry->d_name.len;
4739 struct btrfs_dir_item *di;
4740 struct btrfs_path *path;
4741 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004742 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004743
4744 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004745 if (!path)
4746 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004747
Li Zefan33345d012011-04-20 10:31:50 +08004748 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004749 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004750 if (IS_ERR(di))
4751 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004752
David Sterbac7040052011-04-19 18:00:01 +02004753 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004754 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004755
Chris Mason5f39d392007-10-15 16:14:19 -04004756 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004757out:
Chris Mason39279cc2007-06-12 06:35:45 -04004758 btrfs_free_path(path);
4759 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004760out_err:
4761 location->objectid = 0;
4762 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004763}
4764
4765/*
4766 * when we hit a tree root in a directory, the btrfs part of the inode
4767 * needs to be changed to reflect the root directory of the tree root. This
4768 * is kind of like crossing a mount point.
4769 */
4770static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004771 struct inode *dir,
4772 struct dentry *dentry,
4773 struct btrfs_key *location,
4774 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004775{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004776 struct btrfs_path *path;
4777 struct btrfs_root *new_root;
4778 struct btrfs_root_ref *ref;
4779 struct extent_buffer *leaf;
4780 int ret;
4781 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004782
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004783 path = btrfs_alloc_path();
4784 if (!path) {
4785 err = -ENOMEM;
4786 goto out;
4787 }
Chris Mason39279cc2007-06-12 06:35:45 -04004788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004789 err = -ENOENT;
4790 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4791 BTRFS_I(dir)->root->root_key.objectid,
4792 location->objectid);
4793 if (ret) {
4794 if (ret < 0)
4795 err = ret;
4796 goto out;
4797 }
Chris Mason39279cc2007-06-12 06:35:45 -04004798
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004799 leaf = path->nodes[0];
4800 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004801 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004802 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4803 goto out;
4804
4805 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4806 (unsigned long)(ref + 1),
4807 dentry->d_name.len);
4808 if (ret)
4809 goto out;
4810
David Sterbab3b4aa72011-04-21 01:20:15 +02004811 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004812
4813 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4814 if (IS_ERR(new_root)) {
4815 err = PTR_ERR(new_root);
4816 goto out;
4817 }
4818
4819 if (btrfs_root_refs(&new_root->root_item) == 0) {
4820 err = -ENOENT;
4821 goto out;
4822 }
4823
4824 *sub_root = new_root;
4825 location->objectid = btrfs_root_dirid(&new_root->root_item);
4826 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004827 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004828 err = 0;
4829out:
4830 btrfs_free_path(path);
4831 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004832}
4833
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004834static void inode_tree_add(struct inode *inode)
4835{
4836 struct btrfs_root *root = BTRFS_I(inode)->root;
4837 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004838 struct rb_node **p;
4839 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004840 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004841again:
4842 p = &root->inode_tree.rb_node;
4843 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004844
Al Viro1d3382cb2010-10-23 15:19:20 -04004845 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004846 return;
4847
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004848 spin_lock(&root->inode_lock);
4849 while (*p) {
4850 parent = *p;
4851 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4852
Li Zefan33345d012011-04-20 10:31:50 +08004853 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004854 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004855 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004856 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004857 else {
4858 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004859 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004860 rb_erase(parent, &root->inode_tree);
4861 RB_CLEAR_NODE(parent);
4862 spin_unlock(&root->inode_lock);
4863 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004864 }
4865 }
4866 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4867 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4868 spin_unlock(&root->inode_lock);
4869}
4870
4871static void inode_tree_del(struct inode *inode)
4872{
4873 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004874 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004875
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004876 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004877 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004878 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004879 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004880 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004881 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004882 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004883
Josef Bacik0af3d002010-06-21 14:48:16 -04004884 /*
4885 * Free space cache has inodes in the tree root, but the tree root has a
4886 * root_refs of 0, so this could end up dropping the tree root as a
4887 * snapshot, so we need the extra !root->fs_info->tree_root check to
4888 * make sure we don't drop it.
4889 */
4890 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4891 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004892 synchronize_srcu(&root->fs_info->subvol_srcu);
4893 spin_lock(&root->inode_lock);
4894 empty = RB_EMPTY_ROOT(&root->inode_tree);
4895 spin_unlock(&root->inode_lock);
4896 if (empty)
4897 btrfs_add_dead_root(root);
4898 }
4899}
4900
Jeff Mahoney143bede2012-03-01 14:56:26 +01004901void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004902{
4903 struct rb_node *node;
4904 struct rb_node *prev;
4905 struct btrfs_inode *entry;
4906 struct inode *inode;
4907 u64 objectid = 0;
4908
4909 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4910
4911 spin_lock(&root->inode_lock);
4912again:
4913 node = root->inode_tree.rb_node;
4914 prev = NULL;
4915 while (node) {
4916 prev = node;
4917 entry = rb_entry(node, struct btrfs_inode, rb_node);
4918
Li Zefan33345d012011-04-20 10:31:50 +08004919 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004920 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004921 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004922 node = node->rb_right;
4923 else
4924 break;
4925 }
4926 if (!node) {
4927 while (prev) {
4928 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004929 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004930 node = prev;
4931 break;
4932 }
4933 prev = rb_next(prev);
4934 }
4935 }
4936 while (node) {
4937 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004938 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004939 inode = igrab(&entry->vfs_inode);
4940 if (inode) {
4941 spin_unlock(&root->inode_lock);
4942 if (atomic_read(&inode->i_count) > 1)
4943 d_prune_aliases(inode);
4944 /*
Al Viro45321ac2010-06-07 13:43:19 -04004945 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004946 * the inode cache when its usage count
4947 * hits zero.
4948 */
4949 iput(inode);
4950 cond_resched();
4951 spin_lock(&root->inode_lock);
4952 goto again;
4953 }
4954
4955 if (cond_resched_lock(&root->inode_lock))
4956 goto again;
4957
4958 node = rb_next(node);
4959 }
4960 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004961}
4962
Chris Masone02119d2008-09-05 16:13:11 -04004963static int btrfs_init_locked_inode(struct inode *inode, void *p)
4964{
4965 struct btrfs_iget_args *args = p;
4966 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004967 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004968 return 0;
4969}
4970
4971static int btrfs_find_actor(struct inode *inode, void *opaque)
4972{
4973 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004974 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004975 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004976}
4977
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004978static struct inode *btrfs_iget_locked(struct super_block *s,
4979 u64 objectid,
4980 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004981{
4982 struct inode *inode;
4983 struct btrfs_iget_args args;
4984 args.ino = objectid;
4985 args.root = root;
4986
4987 inode = iget5_locked(s, objectid, btrfs_find_actor,
4988 btrfs_init_locked_inode,
4989 (void *)&args);
4990 return inode;
4991}
4992
Balaji Rao1a54ef82008-07-21 02:01:04 +05304993/* Get an inode object given its location and corresponding root.
4994 * Returns in *is_new if the inode was read from disk
4995 */
4996struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004997 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304998{
4999 struct inode *inode;
5000
5001 inode = btrfs_iget_locked(s, location->objectid, root);
5002 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005003 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305004
5005 if (inode->i_state & I_NEW) {
5006 BTRFS_I(inode)->root = root;
5007 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
5008 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005009 if (!is_bad_inode(inode)) {
5010 inode_tree_add(inode);
5011 unlock_new_inode(inode);
5012 if (new)
5013 *new = 1;
5014 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005015 unlock_new_inode(inode);
5016 iput(inode);
5017 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005018 }
5019 }
5020
Balaji Rao1a54ef82008-07-21 02:01:04 +05305021 return inode;
5022}
5023
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005024static struct inode *new_simple_dir(struct super_block *s,
5025 struct btrfs_key *key,
5026 struct btrfs_root *root)
5027{
5028 struct inode *inode = new_inode(s);
5029
5030 if (!inode)
5031 return ERR_PTR(-ENOMEM);
5032
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005033 BTRFS_I(inode)->root = root;
5034 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005035 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005036
5037 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005038 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005039 inode->i_fop = &simple_dir_operations;
5040 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5041 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5042
5043 return inode;
5044}
5045
Chris Mason3de45862008-11-17 21:02:50 -05005046struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005047{
Chris Masond3977122009-01-05 21:25:51 -05005048 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005049 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005050 struct btrfs_root *sub_root = root;
5051 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005052 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005053 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005054
5055 if (dentry->d_name.len > BTRFS_NAME_LEN)
5056 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005057
Jeff Layton39e3c952012-11-28 11:30:53 -05005058 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005059 if (ret < 0)
5060 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005061
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005062 if (location.objectid == 0)
5063 return NULL;
5064
5065 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005066 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005067 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005068 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005069
5070 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5071
Yan, Zheng76dda932009-09-21 16:00:26 -04005072 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005073 ret = fixup_tree_root_location(root, dir, dentry,
5074 &location, &sub_root);
5075 if (ret < 0) {
5076 if (ret != -ENOENT)
5077 inode = ERR_PTR(ret);
5078 else
5079 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5080 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005081 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005082 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005083 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5084
Julia Lawall34d19ba2011-01-24 19:55:19 +00005085 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005086 down_read(&root->fs_info->cleanup_work_sem);
5087 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005088 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005089 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005090 if (ret)
5091 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005092 }
5093
Chris Mason3de45862008-11-17 21:02:50 -05005094 return inode;
5095}
5096
Nick Pigginfe15ce42011-01-07 17:49:23 +11005097static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005098{
5099 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005100 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005101
Li Zefan848cce02012-02-21 17:04:28 +08005102 if (!inode && !IS_ROOT(dentry))
5103 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005104
Li Zefan848cce02012-02-21 17:04:28 +08005105 if (inode) {
5106 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005107 if (btrfs_root_refs(&root->root_item) == 0)
5108 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005109
5110 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5111 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005112 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005113 return 0;
5114}
5115
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005116static void btrfs_dentry_release(struct dentry *dentry)
5117{
5118 if (dentry->d_fsdata)
5119 kfree(dentry->d_fsdata);
5120}
5121
Chris Mason3de45862008-11-17 21:02:50 -05005122static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005123 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005124{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005125 struct dentry *ret;
5126
5127 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005128 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005129}
5130
Miao Xie16cdcec2011-04-22 18:12:22 +08005131unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005132 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5133};
5134
David Woodhousecbdf5a22008-08-06 19:42:33 +01005135static int btrfs_real_readdir(struct file *filp, void *dirent,
5136 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005137{
Al Viro496ad9a2013-01-23 17:07:38 -05005138 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04005139 struct btrfs_root *root = BTRFS_I(inode)->root;
5140 struct btrfs_item *item;
5141 struct btrfs_dir_item *di;
5142 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005143 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005144 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005145 struct list_head ins_list;
5146 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005147 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005148 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005149 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005150 unsigned char d_type;
5151 int over = 0;
5152 u32 di_cur;
5153 u32 di_total;
5154 u32 di_len;
5155 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005156 char tmp_name[32];
5157 char *name_ptr;
5158 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08005159 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005160
5161 /* FIXME, use a real flag for deciding about the key type */
5162 if (root->fs_info->tree_root == root)
5163 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005164
Chris Mason39544012007-12-12 14:38:19 -05005165 /* special case for "." */
5166 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005167 over = filldir(dirent, ".", 1,
5168 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005169 if (over)
5170 return 0;
5171 filp->f_pos = 1;
5172 }
Chris Mason39544012007-12-12 14:38:19 -05005173 /* special case for .., just use the back ref */
5174 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005175 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005176 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005177 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005178 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005179 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005180 filp->f_pos = 2;
5181 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005182 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005183 if (!path)
5184 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005185
Josef Bacik026fd312011-05-13 10:32:11 -04005186 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005187
Miao Xie16cdcec2011-04-22 18:12:22 +08005188 if (key_type == BTRFS_DIR_INDEX_KEY) {
5189 INIT_LIST_HEAD(&ins_list);
5190 INIT_LIST_HEAD(&del_list);
5191 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5192 }
5193
Chris Mason39279cc2007-06-12 06:35:45 -04005194 btrfs_set_key_type(&key, key_type);
5195 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005196 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005197
Chris Mason39279cc2007-06-12 06:35:45 -04005198 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5199 if (ret < 0)
5200 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005201
5202 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005203 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005204 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005205 if (slot >= btrfs_header_nritems(leaf)) {
5206 ret = btrfs_next_leaf(root, path);
5207 if (ret < 0)
5208 goto err;
5209 else if (ret > 0)
5210 break;
5211 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005212 }
Chris Mason3de45862008-11-17 21:02:50 -05005213
Chris Mason5f39d392007-10-15 16:14:19 -04005214 item = btrfs_item_nr(leaf, slot);
5215 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5216
5217 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005218 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005219 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005220 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005221 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005222 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005223 if (key_type == BTRFS_DIR_INDEX_KEY &&
5224 btrfs_should_delete_dir_index(&del_list,
5225 found_key.offset))
5226 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005227
5228 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005229 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005230
Chris Mason39279cc2007-06-12 06:35:45 -04005231 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5232 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005233 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005234
5235 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005236 struct btrfs_key location;
5237
Josef Bacik22a94d42011-03-16 16:47:17 -04005238 if (verify_dir_item(root, leaf, di))
5239 break;
5240
Chris Mason5f39d392007-10-15 16:14:19 -04005241 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005242 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005243 name_ptr = tmp_name;
5244 } else {
5245 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005246 if (!name_ptr) {
5247 ret = -ENOMEM;
5248 goto err;
5249 }
Chris Mason5f39d392007-10-15 16:14:19 -04005250 }
5251 read_extent_buffer(leaf, name_ptr,
5252 (unsigned long)(di + 1), name_len);
5253
5254 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5255 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005256
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005257
Chris Mason3de45862008-11-17 21:02:50 -05005258 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005259 * skip it.
5260 *
5261 * In contrast to old kernels, we insert the snapshot's
5262 * dir item and dir index after it has been created, so
5263 * we won't find a reference to our own snapshot. We
5264 * still keep the following code for backward
5265 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005266 */
5267 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5268 location.objectid == root->root_key.objectid) {
5269 over = 0;
5270 goto skip;
5271 }
Chris Mason5f39d392007-10-15 16:14:19 -04005272 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005273 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005274 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005275
Chris Mason3de45862008-11-17 21:02:50 -05005276skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005277 if (name_ptr != tmp_name)
5278 kfree(name_ptr);
5279
Chris Mason39279cc2007-06-12 06:35:45 -04005280 if (over)
5281 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005282 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005283 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005284 di_cur += di_len;
5285 di = (struct btrfs_dir_item *)((char *)di + di_len);
5286 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005287next:
5288 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005289 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005290
Miao Xie16cdcec2011-04-22 18:12:22 +08005291 if (key_type == BTRFS_DIR_INDEX_KEY) {
5292 if (is_curr)
5293 filp->f_pos++;
5294 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5295 &ins_list);
5296 if (ret)
5297 goto nopos;
5298 }
5299
David Woodhouse49593bf2008-08-17 17:08:36 +01005300 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005301 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005302 /*
5303 * 32-bit glibc will use getdents64, but then strtol -
5304 * so the last number we can serve is this.
5305 */
5306 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005307 else
5308 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005309nopos:
5310 ret = 0;
5311err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005312 if (key_type == BTRFS_DIR_INDEX_KEY)
5313 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005314 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005315 return ret;
5316}
5317
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005318int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005319{
5320 struct btrfs_root *root = BTRFS_I(inode)->root;
5321 struct btrfs_trans_handle *trans;
5322 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005323 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005324
Josef Bacik72ac3c02012-05-23 14:13:11 -04005325 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005326 return 0;
5327
Liu Bo83eea1f2012-07-10 05:28:39 -06005328 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005329 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005330
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005331 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005332 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005333 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005334 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005335 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005336 if (IS_ERR(trans))
5337 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005338 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005339 }
5340 return ret;
5341}
5342
5343/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005344 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005345 * inode changes. But, it is most likely to find the inode in cache.
5346 * FIXME, needs more benchmarking...there are no reasons other than performance
5347 * to keep or drop this code.
5348 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005349static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005350{
5351 struct btrfs_root *root = BTRFS_I(inode)->root;
5352 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005353 int ret;
5354
Josef Bacik72ac3c02012-05-23 14:13:11 -04005355 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005356 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005357
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005358 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005359 if (IS_ERR(trans))
5360 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005361
5362 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005363 if (ret && ret == -ENOSPC) {
5364 /* whoops, lets try again with the full transaction */
5365 btrfs_end_transaction(trans, root);
5366 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005367 if (IS_ERR(trans))
5368 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005369
Chris Mason94b60442010-05-26 11:02:00 -04005370 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005371 }
Chris Mason39279cc2007-06-12 06:35:45 -04005372 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005373 if (BTRFS_I(inode)->delayed_node)
5374 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005375
5376 return ret;
5377}
5378
5379/*
5380 * This is a copy of file_update_time. We need this so we can return error on
5381 * ENOSPC for updating the inode in the case of file write and mmap writes.
5382 */
Josef Bacike41f9412012-03-26 09:46:47 -04005383static int btrfs_update_time(struct inode *inode, struct timespec *now,
5384 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005385{
Alexander Block2bc5565282012-06-15 09:49:33 +02005386 struct btrfs_root *root = BTRFS_I(inode)->root;
5387
5388 if (btrfs_root_readonly(root))
5389 return -EROFS;
5390
Josef Bacike41f9412012-03-26 09:46:47 -04005391 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005392 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005393 if (flags & S_CTIME)
5394 inode->i_ctime = *now;
5395 if (flags & S_MTIME)
5396 inode->i_mtime = *now;
5397 if (flags & S_ATIME)
5398 inode->i_atime = *now;
5399 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005400}
5401
Chris Masond352ac62008-09-29 15:18:18 -04005402/*
5403 * find the highest existing sequence number in a directory
5404 * and then set the in-memory index_cnt variable to reflect
5405 * free sequence numbers
5406 */
Josef Bacikaec74772008-07-24 12:12:38 -04005407static int btrfs_set_inode_index_count(struct inode *inode)
5408{
5409 struct btrfs_root *root = BTRFS_I(inode)->root;
5410 struct btrfs_key key, found_key;
5411 struct btrfs_path *path;
5412 struct extent_buffer *leaf;
5413 int ret;
5414
Li Zefan33345d012011-04-20 10:31:50 +08005415 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005416 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5417 key.offset = (u64)-1;
5418
5419 path = btrfs_alloc_path();
5420 if (!path)
5421 return -ENOMEM;
5422
5423 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5424 if (ret < 0)
5425 goto out;
5426 /* FIXME: we should be able to handle this */
5427 if (ret == 0)
5428 goto out;
5429 ret = 0;
5430
5431 /*
5432 * MAGIC NUMBER EXPLANATION:
5433 * since we search a directory based on f_pos we have to start at 2
5434 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5435 * else has to start at 2
5436 */
5437 if (path->slots[0] == 0) {
5438 BTRFS_I(inode)->index_cnt = 2;
5439 goto out;
5440 }
5441
5442 path->slots[0]--;
5443
5444 leaf = path->nodes[0];
5445 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5446
Li Zefan33345d012011-04-20 10:31:50 +08005447 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005448 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5449 BTRFS_I(inode)->index_cnt = 2;
5450 goto out;
5451 }
5452
5453 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5454out:
5455 btrfs_free_path(path);
5456 return ret;
5457}
5458
Chris Masond352ac62008-09-29 15:18:18 -04005459/*
5460 * helper to find a free sequence number in a given directory. This current
5461 * code is very simple, later versions will do smarter things in the btree
5462 */
Chris Mason3de45862008-11-17 21:02:50 -05005463int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005464{
5465 int ret = 0;
5466
5467 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005468 ret = btrfs_inode_delayed_dir_index_count(dir);
5469 if (ret) {
5470 ret = btrfs_set_inode_index_count(dir);
5471 if (ret)
5472 return ret;
5473 }
Josef Bacikaec74772008-07-24 12:12:38 -04005474 }
5475
Chris Mason00e4e6b2008-08-05 11:18:09 -04005476 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005477 BTRFS_I(dir)->index_cnt++;
5478
5479 return ret;
5480}
5481
Chris Mason39279cc2007-06-12 06:35:45 -04005482static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5483 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005484 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005485 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005486 u64 ref_objectid, u64 objectid,
5487 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005488{
5489 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005490 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005491 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005492 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005493 struct btrfs_inode_ref *ref;
5494 struct btrfs_key key[2];
5495 u32 sizes[2];
5496 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005497 int ret;
5498 int owner;
5499
Chris Mason5f39d392007-10-15 16:14:19 -04005500 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005501 if (!path)
5502 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005503
Chris Mason39279cc2007-06-12 06:35:45 -04005504 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005505 if (!inode) {
5506 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005507 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005508 }
Chris Mason39279cc2007-06-12 06:35:45 -04005509
Li Zefan581bb052011-04-20 10:06:11 +08005510 /*
5511 * we have to initialize this early, so we can reclaim the inode
5512 * number if we fail afterwards in this function.
5513 */
5514 inode->i_ino = objectid;
5515
Josef Bacikaec74772008-07-24 12:12:38 -04005516 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005517 trace_btrfs_inode_request(dir);
5518
Chris Mason3de45862008-11-17 21:02:50 -05005519 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005520 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005521 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005522 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005523 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005524 }
Josef Bacikaec74772008-07-24 12:12:38 -04005525 }
5526 /*
5527 * index_cnt is ignored for everything but a dir,
5528 * btrfs_get_inode_index_count has an explanation for the magic
5529 * number
5530 */
5531 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005532 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005533 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005534 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005535
Josef Bacik5dc562c2012-08-17 13:14:17 -04005536 /*
5537 * We could have gotten an inode number from somebody who was fsynced
5538 * and then removed in this same transaction, so let's just set full
5539 * sync since it will be a full sync anyway and this will blow away the
5540 * old info in the log.
5541 */
5542 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5543
Al Viro569254b2011-07-24 17:08:40 -04005544 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005545 owner = 0;
5546 else
5547 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005548
5549 key[0].objectid = objectid;
5550 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5551 key[0].offset = 0;
5552
Mark Fashehf1863732012-08-08 11:32:27 -07005553 /*
5554 * Start new inodes with an inode_ref. This is slightly more
5555 * efficient for small numbers of hard links since they will
5556 * be packed into one item. Extended refs will kick in if we
5557 * add more hard links than can fit in the ref item.
5558 */
Chris Mason9c583092008-01-29 15:15:18 -05005559 key[1].objectid = objectid;
5560 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5561 key[1].offset = ref_objectid;
5562
5563 sizes[0] = sizeof(struct btrfs_inode_item);
5564 sizes[1] = name_len + sizeof(*ref);
5565
Chris Masonb9473432009-03-13 11:00:37 -04005566 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005567 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5568 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005569 goto fail;
5570
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005571 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005572 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005573 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005574 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5575 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005576 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5577 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005578 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005579
5580 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5581 struct btrfs_inode_ref);
5582 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005583 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005584 ptr = (unsigned long)(ref + 1);
5585 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5586
Chris Mason5f39d392007-10-15 16:14:19 -04005587 btrfs_mark_buffer_dirty(path->nodes[0]);
5588 btrfs_free_path(path);
5589
Chris Mason39279cc2007-06-12 06:35:45 -04005590 location = &BTRFS_I(inode)->location;
5591 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005592 location->offset = 0;
5593 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5594
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005595 btrfs_inherit_iflags(inode, dir);
5596
Al Viro569254b2011-07-24 17:08:40 -04005597 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005598 if (btrfs_test_opt(root, NODATASUM))
5599 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005600 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005601 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5602 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005603 }
5604
Chris Mason39279cc2007-06-12 06:35:45 -04005605 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005607
5608 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005609 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005610
Alexander Block8ea05e32012-07-25 17:35:53 +02005611 btrfs_update_root_times(trans, root);
5612
Chris Mason39279cc2007-06-12 06:35:45 -04005613 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005614fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005615 if (dir)
5616 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005617 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005618 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005619 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005620}
5621
5622static inline u8 btrfs_inode_type(struct inode *inode)
5623{
5624 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5625}
5626
Chris Masond352ac62008-09-29 15:18:18 -04005627/*
5628 * utility function to add 'inode' into 'parent_inode' with
5629 * a give name and a given sequence number.
5630 * if 'add_backref' is true, also insert a backref from the
5631 * inode to the parent directory.
5632 */
Chris Masone02119d2008-09-05 16:13:11 -04005633int btrfs_add_link(struct btrfs_trans_handle *trans,
5634 struct inode *parent_inode, struct inode *inode,
5635 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005636{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005637 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005638 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005639 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005640 u64 ino = btrfs_ino(inode);
5641 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005642
Li Zefan33345d012011-04-20 10:31:50 +08005643 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005644 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5645 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005646 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005647 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5648 key.offset = 0;
5649 }
Chris Mason39279cc2007-06-12 06:35:45 -04005650
Li Zefan33345d012011-04-20 10:31:50 +08005651 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5653 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005654 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005655 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005656 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5657 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005658 }
5659
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005660 /* Nothing to clean up yet */
5661 if (ret)
5662 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005663
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005664 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5665 parent_inode, &key,
5666 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005667 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005668 goto fail_dir_item;
5669 else if (ret) {
5670 btrfs_abort_transaction(trans, root, ret);
5671 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005672 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005673
5674 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5675 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005676 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005677 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5678 ret = btrfs_update_inode(trans, root, parent_inode);
5679 if (ret)
5680 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005681 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005682
5683fail_dir_item:
5684 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5685 u64 local_index;
5686 int err;
5687 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5688 key.objectid, root->root_key.objectid,
5689 parent_ino, &local_index, name, name_len);
5690
5691 } else if (add_backref) {
5692 u64 local_index;
5693 int err;
5694
5695 err = btrfs_del_inode_ref(trans, root, name, name_len,
5696 ino, parent_ino, &local_index);
5697 }
5698 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005699}
5700
5701static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005702 struct inode *dir, struct dentry *dentry,
5703 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005704{
Josef Bacika1b075d2010-11-19 20:36:11 +00005705 int err = btrfs_add_link(trans, dir, inode,
5706 dentry->d_name.name, dentry->d_name.len,
5707 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005708 if (err > 0)
5709 err = -EEXIST;
5710 return err;
5711}
5712
Josef Bacik618e21d2007-07-11 10:18:17 -04005713static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005714 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005715{
5716 struct btrfs_trans_handle *trans;
5717 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005718 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005719 int err;
5720 int drop_inode = 0;
5721 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005722 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005723
5724 if (!new_valid_dev(rdev))
5725 return -EINVAL;
5726
Josef Bacik9ed74f22009-09-11 16:12:44 -04005727 /*
5728 * 2 for inode item and ref
5729 * 2 for dir items
5730 * 1 for xattr if selinux is on
5731 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005732 trans = btrfs_start_transaction(root, 5);
5733 if (IS_ERR(trans))
5734 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005735
Li Zefan581bb052011-04-20 10:06:11 +08005736 err = btrfs_find_free_ino(root, &objectid);
5737 if (err)
5738 goto out_unlock;
5739
Josef Bacikaec74772008-07-24 12:12:38 -04005740 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005741 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005742 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005743 if (IS_ERR(inode)) {
5744 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005745 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005746 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005747
Eric Paris2a7dba32011-02-01 11:05:39 -05005748 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005749 if (err) {
5750 drop_inode = 1;
5751 goto out_unlock;
5752 }
5753
Casey Schauflerad19db72011-12-15 10:09:07 -05005754 /*
5755 * If the active LSM wants to access the inode during
5756 * d_instantiate it needs these. Smack checks to see
5757 * if the filesystem supports xattrs by looking at the
5758 * ops vector.
5759 */
5760
5761 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005762 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005763 if (err)
5764 drop_inode = 1;
5765 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005766 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005767 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005768 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005769 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005770out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005771 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005772 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005773 if (drop_inode) {
5774 inode_dec_link_count(inode);
5775 iput(inode);
5776 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005777 return err;
5778}
5779
Chris Mason39279cc2007-06-12 06:35:45 -04005780static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005781 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005782{
5783 struct btrfs_trans_handle *trans;
5784 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005785 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005786 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005787 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005788 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005789 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005790
Josef Bacik9ed74f22009-09-11 16:12:44 -04005791 /*
5792 * 2 for inode item and ref
5793 * 2 for dir items
5794 * 1 for xattr if selinux is on
5795 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005796 trans = btrfs_start_transaction(root, 5);
5797 if (IS_ERR(trans))
5798 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005799
Li Zefan581bb052011-04-20 10:06:11 +08005800 err = btrfs_find_free_ino(root, &objectid);
5801 if (err)
5802 goto out_unlock;
5803
Josef Bacikaec74772008-07-24 12:12:38 -04005804 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005805 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005806 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005807 if (IS_ERR(inode)) {
5808 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005809 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005810 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005811 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005812
Eric Paris2a7dba32011-02-01 11:05:39 -05005813 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005814 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005815 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005816
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005817 err = btrfs_update_inode(trans, root, inode);
5818 if (err)
5819 goto out_unlock;
5820
Casey Schauflerad19db72011-12-15 10:09:07 -05005821 /*
5822 * If the active LSM wants to access the inode during
5823 * d_instantiate it needs these. Smack checks to see
5824 * if the filesystem supports xattrs by looking at the
5825 * ops vector.
5826 */
5827 inode->i_fop = &btrfs_file_operations;
5828 inode->i_op = &btrfs_file_inode_operations;
5829
Josef Bacika1b075d2010-11-19 20:36:11 +00005830 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005831 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005832 goto out_unlock;
5833
5834 inode->i_mapping->a_ops = &btrfs_aops;
5835 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5836 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5837 d_instantiate(dentry, inode);
5838
Chris Mason39279cc2007-06-12 06:35:45 -04005839out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005840 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005841 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005842 inode_dec_link_count(inode);
5843 iput(inode);
5844 }
Liu Bob53d3f52012-11-14 14:34:34 +00005845 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005846 return err;
5847}
5848
5849static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5850 struct dentry *dentry)
5851{
5852 struct btrfs_trans_handle *trans;
5853 struct btrfs_root *root = BTRFS_I(dir)->root;
5854 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005855 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005856 int err;
5857 int drop_inode = 0;
5858
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005859 /* do not allow sys_link's with other subvols of the same device */
5860 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005861 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005862
Mark Fashehf1863732012-08-08 11:32:27 -07005863 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005864 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005865
Chris Mason3de45862008-11-17 21:02:50 -05005866 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005867 if (err)
5868 goto fail;
5869
Yan, Zhenga22285a2010-05-16 10:48:46 -04005870 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005871 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005872 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005873 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005874 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005875 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005876 if (IS_ERR(trans)) {
5877 err = PTR_ERR(trans);
5878 goto fail;
5879 }
Chris Mason5f39d392007-10-15 16:14:19 -04005880
Miao Xie31534952011-04-13 13:19:21 +08005881 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005882 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005883 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005884 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005885 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005886
Josef Bacika1b075d2010-11-19 20:36:11 +00005887 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005888
Yan, Zhenga5719522009-09-24 09:17:31 -04005889 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005890 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005891 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005892 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005893 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005894 if (err)
5895 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005896 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005897 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005898 }
Chris Mason39279cc2007-06-12 06:35:45 -04005899
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005900 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005901fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005902 if (drop_inode) {
5903 inode_dec_link_count(inode);
5904 iput(inode);
5905 }
Liu Bob53d3f52012-11-14 14:34:34 +00005906 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005907 return err;
5908}
5909
Al Viro18bb1db2011-07-26 01:41:39 -04005910static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005911{
Chris Masonb9d86662008-05-02 16:13:49 -04005912 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005913 struct btrfs_trans_handle *trans;
5914 struct btrfs_root *root = BTRFS_I(dir)->root;
5915 int err = 0;
5916 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005917 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005918 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005919
Josef Bacik9ed74f22009-09-11 16:12:44 -04005920 /*
5921 * 2 items for inode and ref
5922 * 2 items for dir items
5923 * 1 for xattr if selinux is on
5924 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005925 trans = btrfs_start_transaction(root, 5);
5926 if (IS_ERR(trans))
5927 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005928
Li Zefan581bb052011-04-20 10:06:11 +08005929 err = btrfs_find_free_ino(root, &objectid);
5930 if (err)
5931 goto out_fail;
5932
Josef Bacikaec74772008-07-24 12:12:38 -04005933 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005934 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005935 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005936 if (IS_ERR(inode)) {
5937 err = PTR_ERR(inode);
5938 goto out_fail;
5939 }
Chris Mason5f39d392007-10-15 16:14:19 -04005940
Chris Mason39279cc2007-06-12 06:35:45 -04005941 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005942
Eric Paris2a7dba32011-02-01 11:05:39 -05005943 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005944 if (err)
5945 goto out_fail;
5946
Chris Mason39279cc2007-06-12 06:35:45 -04005947 inode->i_op = &btrfs_dir_inode_operations;
5948 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005949
Chris Masondbe674a2008-07-17 12:54:05 -04005950 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005951 err = btrfs_update_inode(trans, root, inode);
5952 if (err)
5953 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005954
Josef Bacika1b075d2010-11-19 20:36:11 +00005955 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5956 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005957 if (err)
5958 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005959
Chris Mason39279cc2007-06-12 06:35:45 -04005960 d_instantiate(dentry, inode);
5961 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005962
5963out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005964 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005965 if (drop_on_err)
5966 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005967 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005968 return err;
5969}
5970
Chris Masond352ac62008-09-29 15:18:18 -04005971/* helper for btfs_get_extent. Given an existing extent in the tree,
5972 * and an extent that you want to insert, deal with overlap and insert
5973 * the new extent into the tree.
5974 */
Chris Mason3b951512008-04-17 11:29:12 -04005975static int merge_extent_mapping(struct extent_map_tree *em_tree,
5976 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005977 struct extent_map *em,
5978 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005979{
5980 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005981
Chris Masone6dcd2d2008-07-17 12:53:50 -04005982 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5983 start_diff = map_start - em->start;
5984 em->start = map_start;
5985 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005986 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5987 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005988 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005989 em->block_len -= start_diff;
5990 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005991 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005992}
5993
Chris Masonc8b97812008-10-29 14:49:59 -04005994static noinline int uncompress_inline(struct btrfs_path *path,
5995 struct inode *inode, struct page *page,
5996 size_t pg_offset, u64 extent_offset,
5997 struct btrfs_file_extent_item *item)
5998{
5999 int ret;
6000 struct extent_buffer *leaf = path->nodes[0];
6001 char *tmp;
6002 size_t max_size;
6003 unsigned long inline_size;
6004 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006005 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006006
6007 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006008 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006009 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6010 inline_size = btrfs_file_extent_inline_item_len(leaf,
6011 btrfs_item_nr(leaf, path->slots[0]));
6012 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006013 if (!tmp)
6014 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006015 ptr = btrfs_file_extent_inline_start(item);
6016
6017 read_extent_buffer(leaf, tmp, ptr, inline_size);
6018
Chris Mason5b050f02008-11-11 09:34:41 -05006019 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006020 ret = btrfs_decompress(compress_type, tmp, page,
6021 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006022 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08006023 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04006024 unsigned long copy_size = min_t(u64,
6025 PAGE_CACHE_SIZE - pg_offset,
6026 max_size - extent_offset);
6027 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08006028 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04006029 }
6030 kfree(tmp);
6031 return 0;
6032}
6033
Chris Masond352ac62008-09-29 15:18:18 -04006034/*
6035 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006036 * the ugly parts come from merging extents from the disk with the in-ram
6037 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006038 * where the in-ram extents might be locked pending data=ordered completion.
6039 *
6040 * This also copies inline extents directly into the page.
6041 */
Chris Masond3977122009-01-05 21:25:51 -05006042
Chris Masona52d9a82007-08-27 16:49:44 -04006043struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006044 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006045 int create)
6046{
6047 int ret;
6048 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04006049 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04006050 u64 extent_start = 0;
6051 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006052 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006053 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006054 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006055 struct btrfs_root *root = BTRFS_I(inode)->root;
6056 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006057 struct extent_buffer *leaf;
6058 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006059 struct extent_map *em = NULL;
6060 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006061 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006062 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08006063 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04006064
Chris Masona52d9a82007-08-27 16:49:44 -04006065again:
Chris Mason890871b2009-09-02 16:24:52 -04006066 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006067 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006068 if (em)
6069 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006070 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006071
Chris Masona52d9a82007-08-27 16:49:44 -04006072 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006073 if (em->start > start || em->start + em->len <= start)
6074 free_extent_map(em);
6075 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006076 free_extent_map(em);
6077 else
6078 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006079 }
David Sterba172ddd62011-04-21 00:48:27 +02006080 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006081 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006082 err = -ENOMEM;
6083 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006084 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006085 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006086 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006087 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006088 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006089 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006090
6091 if (!path) {
6092 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006093 if (!path) {
6094 err = -ENOMEM;
6095 goto out;
6096 }
6097 /*
6098 * Chances are we'll be called again, so go ahead and do
6099 * readahead
6100 */
6101 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006102 }
6103
Chris Mason179e29e2007-11-01 11:28:41 -04006104 ret = btrfs_lookup_file_extent(trans, root, path,
6105 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006106 if (ret < 0) {
6107 err = ret;
6108 goto out;
6109 }
6110
6111 if (ret != 0) {
6112 if (path->slots[0] == 0)
6113 goto not_found;
6114 path->slots[0]--;
6115 }
6116
Chris Mason5f39d392007-10-15 16:14:19 -04006117 leaf = path->nodes[0];
6118 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006119 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006120 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006121 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6122 found_type = btrfs_key_type(&found_key);
6123 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006124 found_type != BTRFS_EXTENT_DATA_KEY) {
6125 goto not_found;
6126 }
6127
Chris Mason5f39d392007-10-15 16:14:19 -04006128 found_type = btrfs_file_extent_type(leaf, item);
6129 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006130 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006131 if (found_type == BTRFS_FILE_EXTENT_REG ||
6132 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006133 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006134 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006135 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6136 size_t size;
6137 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006138 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006139 }
6140
6141 if (start >= extent_end) {
6142 path->slots[0]++;
6143 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6144 ret = btrfs_next_leaf(root, path);
6145 if (ret < 0) {
6146 err = ret;
6147 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006148 }
Yan Zheng9036c102008-10-30 14:19:41 -04006149 if (ret > 0)
6150 goto not_found;
6151 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006152 }
Yan Zheng9036c102008-10-30 14:19:41 -04006153 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6154 if (found_key.objectid != objectid ||
6155 found_key.type != BTRFS_EXTENT_DATA_KEY)
6156 goto not_found;
6157 if (start + len <= found_key.offset)
6158 goto not_found;
6159 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006160 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006161 em->len = found_key.offset - start;
6162 goto not_found_em;
6163 }
6164
Josef Bacikcc95bef2013-04-04 14:31:27 -04006165 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006166 if (found_type == BTRFS_FILE_EXTENT_REG ||
6167 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006168 em->start = extent_start;
6169 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006170 em->orig_start = extent_start -
6171 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006172 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6173 item);
Chris Masondb945352007-10-15 16:15:53 -04006174 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6175 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006176 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006177 goto insert;
6178 }
Li Zefan261507a02010-12-17 14:21:50 +08006179 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006180 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006181 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006182 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006183 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006184 } else {
6185 bytenr += btrfs_file_extent_offset(leaf, item);
6186 em->block_start = bytenr;
6187 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006188 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6189 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006190 }
Chris Masona52d9a82007-08-27 16:49:44 -04006191 goto insert;
6192 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006193 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006194 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006195 size_t size;
6196 size_t extent_offset;
6197 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006198
Chris Masona52d9a82007-08-27 16:49:44 -04006199 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006200 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006201 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006202 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006203 goto out;
6204 }
6205
Yan Zheng9036c102008-10-30 14:19:41 -04006206 size = btrfs_file_extent_inline_len(leaf, item);
6207 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006208 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006209 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006210 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006211 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006212 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006213 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006214 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006215 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006216 em->compress_type = compress_type;
6217 }
Yan689f9342007-10-29 11:41:07 -04006218 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006219 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006220 if (btrfs_file_extent_compression(leaf, item) !=
6221 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006222 ret = uncompress_inline(path, inode, page,
6223 pg_offset,
6224 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006225 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006226 } else {
6227 map = kmap(page);
6228 read_extent_buffer(leaf, map + pg_offset, ptr,
6229 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006230 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6231 memset(map + pg_offset + copy_size, 0,
6232 PAGE_CACHE_SIZE - pg_offset -
6233 copy_size);
6234 }
Chris Masonc8b97812008-10-29 14:49:59 -04006235 kunmap(page);
6236 }
Chris Mason179e29e2007-11-01 11:28:41 -04006237 flush_dcache_page(page);
6238 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006239 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006240 if (!trans) {
6241 kunmap(page);
6242 free_extent_map(em);
6243 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006244
David Sterbab3b4aa72011-04-21 01:20:15 +02006245 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006246 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006247
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006248 if (IS_ERR(trans))
6249 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006250 goto again;
6251 }
Chris Masonc8b97812008-10-29 14:49:59 -04006252 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006253 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006254 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006255 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006256 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006257 }
Chris Masond1310b22008-01-24 16:13:08 -05006258 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006259 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006260 goto insert;
6261 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006262 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006263 }
6264not_found:
6265 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006266 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006267 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006268not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006269 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006270 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006271insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006272 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006273 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006274 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6275 (unsigned long long)em->start,
6276 (unsigned long long)em->len,
6277 (unsigned long long)start,
6278 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006279 err = -EIO;
6280 goto out;
6281 }
Chris Masond1310b22008-01-24 16:13:08 -05006282
6283 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006284 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006285 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006286 /* it is possible that someone inserted the extent into the tree
6287 * while we had the lock dropped. It is also possible that
6288 * an overlapping map exists in the tree
6289 */
Chris Masona52d9a82007-08-27 16:49:44 -04006290 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006291 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006292
6293 ret = 0;
6294
Chris Mason3b951512008-04-17 11:29:12 -04006295 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006296 if (existing && (existing->start > start ||
6297 existing->start + existing->len <= start)) {
6298 free_extent_map(existing);
6299 existing = NULL;
6300 }
Chris Mason3b951512008-04-17 11:29:12 -04006301 if (!existing) {
6302 existing = lookup_extent_mapping(em_tree, em->start,
6303 em->len);
6304 if (existing) {
6305 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006306 em, start,
6307 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006308 free_extent_map(existing);
6309 if (err) {
6310 free_extent_map(em);
6311 em = NULL;
6312 }
6313 } else {
6314 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006315 free_extent_map(em);
6316 em = NULL;
6317 }
6318 } else {
6319 free_extent_map(em);
6320 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006321 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006322 }
Chris Masona52d9a82007-08-27 16:49:44 -04006323 }
Chris Mason890871b2009-09-02 16:24:52 -04006324 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006325out:
liubo1abe9b82011-03-24 11:18:59 +00006326
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006327 if (em)
6328 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006329
Chris Masonf4219502008-07-22 11:18:09 -04006330 if (path)
6331 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006332 if (trans) {
6333 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006334 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006335 err = ret;
6336 }
Chris Masona52d9a82007-08-27 16:49:44 -04006337 if (err) {
6338 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006339 return ERR_PTR(err);
6340 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006341 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006342 return em;
6343}
6344
Chris Masonec29ed52011-02-23 16:23:20 -05006345struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6346 size_t pg_offset, u64 start, u64 len,
6347 int create)
6348{
6349 struct extent_map *em;
6350 struct extent_map *hole_em = NULL;
6351 u64 range_start = start;
6352 u64 end;
6353 u64 found;
6354 u64 found_end;
6355 int err = 0;
6356
6357 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6358 if (IS_ERR(em))
6359 return em;
6360 if (em) {
6361 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006362 * if our em maps to
6363 * - a hole or
6364 * - a pre-alloc extent,
6365 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006366 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006367 if (em->block_start != EXTENT_MAP_HOLE &&
6368 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006369 return em;
6370 else
6371 hole_em = em;
6372 }
6373
6374 /* check to see if we've wrapped (len == -1 or similar) */
6375 end = start + len;
6376 if (end < start)
6377 end = (u64)-1;
6378 else
6379 end -= 1;
6380
6381 em = NULL;
6382
6383 /* ok, we didn't find anything, lets look for delalloc */
6384 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6385 end, len, EXTENT_DELALLOC, 1);
6386 found_end = range_start + found;
6387 if (found_end < range_start)
6388 found_end = (u64)-1;
6389
6390 /*
6391 * we didn't find anything useful, return
6392 * the original results from get_extent()
6393 */
6394 if (range_start > end || found_end <= start) {
6395 em = hole_em;
6396 hole_em = NULL;
6397 goto out;
6398 }
6399
6400 /* adjust the range_start to make sure it doesn't
6401 * go backwards from the start they passed in
6402 */
6403 range_start = max(start,range_start);
6404 found = found_end - range_start;
6405
6406 if (found > 0) {
6407 u64 hole_start = start;
6408 u64 hole_len = len;
6409
David Sterba172ddd62011-04-21 00:48:27 +02006410 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006411 if (!em) {
6412 err = -ENOMEM;
6413 goto out;
6414 }
6415 /*
6416 * when btrfs_get_extent can't find anything it
6417 * returns one huge hole
6418 *
6419 * make sure what it found really fits our range, and
6420 * adjust to make sure it is based on the start from
6421 * the caller
6422 */
6423 if (hole_em) {
6424 u64 calc_end = extent_map_end(hole_em);
6425
6426 if (calc_end <= start || (hole_em->start > end)) {
6427 free_extent_map(hole_em);
6428 hole_em = NULL;
6429 } else {
6430 hole_start = max(hole_em->start, start);
6431 hole_len = calc_end - hole_start;
6432 }
6433 }
6434 em->bdev = NULL;
6435 if (hole_em && range_start > hole_start) {
6436 /* our hole starts before our delalloc, so we
6437 * have to return just the parts of the hole
6438 * that go until the delalloc starts
6439 */
6440 em->len = min(hole_len,
6441 range_start - hole_start);
6442 em->start = hole_start;
6443 em->orig_start = hole_start;
6444 /*
6445 * don't adjust block start at all,
6446 * it is fixed at EXTENT_MAP_HOLE
6447 */
6448 em->block_start = hole_em->block_start;
6449 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006450 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6451 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006452 } else {
6453 em->start = range_start;
6454 em->len = found;
6455 em->orig_start = range_start;
6456 em->block_start = EXTENT_MAP_DELALLOC;
6457 em->block_len = found;
6458 }
6459 } else if (hole_em) {
6460 return hole_em;
6461 }
6462out:
6463
6464 free_extent_map(hole_em);
6465 if (err) {
6466 free_extent_map(em);
6467 return ERR_PTR(err);
6468 }
6469 return em;
6470}
6471
Josef Bacik4b46fce2010-05-23 11:00:55 -04006472static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6473 u64 start, u64 len)
6474{
6475 struct btrfs_root *root = BTRFS_I(inode)->root;
6476 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006477 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006478 struct btrfs_key ins;
6479 u64 alloc_hint;
6480 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006481
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006482 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006483 if (IS_ERR(trans))
6484 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006485
6486 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6487
6488 alloc_hint = get_extent_allocation_hint(inode, start, len);
6489 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006490 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006491 if (ret) {
6492 em = ERR_PTR(ret);
6493 goto out;
6494 }
6495
Josef Bacik70c8a912012-10-11 16:54:30 -04006496 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006497 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006498 if (IS_ERR(em))
6499 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006500
6501 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6502 ins.offset, ins.offset, 0);
6503 if (ret) {
6504 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6505 em = ERR_PTR(ret);
6506 }
6507out:
6508 btrfs_end_transaction(trans, root);
6509 return em;
6510}
6511
Chris Mason46bfbb52010-05-26 11:04:10 -04006512/*
6513 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6514 * block must be cow'd
6515 */
6516static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
Josef Bacikeb384b52013-04-24 16:32:55 -04006517 struct inode *inode, u64 offset, u64 *len,
6518 u64 *orig_start, u64 *orig_block_len,
6519 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006520{
6521 struct btrfs_path *path;
6522 int ret;
6523 struct extent_buffer *leaf;
6524 struct btrfs_root *root = BTRFS_I(inode)->root;
6525 struct btrfs_file_extent_item *fi;
6526 struct btrfs_key key;
6527 u64 disk_bytenr;
6528 u64 backref_offset;
6529 u64 extent_end;
6530 u64 num_bytes;
6531 int slot;
6532 int found_type;
6533
6534 path = btrfs_alloc_path();
6535 if (!path)
6536 return -ENOMEM;
6537
Li Zefan33345d012011-04-20 10:31:50 +08006538 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006539 offset, 0);
6540 if (ret < 0)
6541 goto out;
6542
6543 slot = path->slots[0];
6544 if (ret == 1) {
6545 if (slot == 0) {
6546 /* can't find the item, must cow */
6547 ret = 0;
6548 goto out;
6549 }
6550 slot--;
6551 }
6552 ret = 0;
6553 leaf = path->nodes[0];
6554 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006555 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006556 key.type != BTRFS_EXTENT_DATA_KEY) {
6557 /* not our file or wrong item type, must cow */
6558 goto out;
6559 }
6560
6561 if (key.offset > offset) {
6562 /* Wrong offset, must cow */
6563 goto out;
6564 }
6565
6566 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6567 found_type = btrfs_file_extent_type(leaf, fi);
6568 if (found_type != BTRFS_FILE_EXTENT_REG &&
6569 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6570 /* not a regular extent, must cow */
6571 goto out;
6572 }
6573 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6574 backref_offset = btrfs_file_extent_offset(leaf, fi);
6575
Josef Bacikeb384b52013-04-24 16:32:55 -04006576 *orig_start = key.offset - backref_offset;
6577 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6578 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6579
Chris Mason46bfbb52010-05-26 11:04:10 -04006580 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikeb384b52013-04-24 16:32:55 -04006581 if (extent_end < offset + *len) {
Chris Mason46bfbb52010-05-26 11:04:10 -04006582 /* extent doesn't include our full range, must cow */
6583 goto out;
6584 }
6585
6586 if (btrfs_extent_readonly(root, disk_bytenr))
6587 goto out;
6588
6589 /*
6590 * look for other files referencing this extent, if we
6591 * find any we must cow
6592 */
Li Zefan33345d012011-04-20 10:31:50 +08006593 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006594 key.offset - backref_offset, disk_bytenr))
6595 goto out;
6596
6597 /*
6598 * adjust disk_bytenr and num_bytes to cover just the bytes
6599 * in this extent we are about to write. If there
6600 * are any csums in that range we have to cow in order
6601 * to keep the csums correct
6602 */
6603 disk_bytenr += backref_offset;
6604 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006605 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006606 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6607 goto out;
6608 /*
6609 * all of the above have passed, it is safe to overwrite this extent
6610 * without cow
6611 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006612 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006613 ret = 1;
6614out:
6615 btrfs_free_path(path);
6616 return ret;
6617}
6618
Josef Bacikeb838e72012-07-31 16:28:48 -04006619static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6620 struct extent_state **cached_state, int writing)
6621{
6622 struct btrfs_ordered_extent *ordered;
6623 int ret = 0;
6624
6625 while (1) {
6626 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6627 0, cached_state);
6628 /*
6629 * We're concerned with the entire range that we're going to be
6630 * doing DIO to, so we need to make sure theres no ordered
6631 * extents in this range.
6632 */
6633 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6634 lockend - lockstart + 1);
6635
6636 /*
6637 * We need to make sure there are no buffered pages in this
6638 * range either, we could have raced between the invalidate in
6639 * generic_file_direct_write and locking the extent. The
6640 * invalidate needs to happen so that reads after a write do not
6641 * get stale data.
6642 */
6643 if (!ordered && (!writing ||
6644 !test_range_bit(&BTRFS_I(inode)->io_tree,
6645 lockstart, lockend, EXTENT_UPTODATE, 0,
6646 *cached_state)))
6647 break;
6648
6649 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6650 cached_state, GFP_NOFS);
6651
6652 if (ordered) {
6653 btrfs_start_ordered_extent(inode, ordered, 1);
6654 btrfs_put_ordered_extent(ordered);
6655 } else {
6656 /* Screw you mmap */
6657 ret = filemap_write_and_wait_range(inode->i_mapping,
6658 lockstart,
6659 lockend);
6660 if (ret)
6661 break;
6662
6663 /*
6664 * If we found a page that couldn't be invalidated just
6665 * fall back to buffered.
6666 */
6667 ret = invalidate_inode_pages2_range(inode->i_mapping,
6668 lockstart >> PAGE_CACHE_SHIFT,
6669 lockend >> PAGE_CACHE_SHIFT);
6670 if (ret)
6671 break;
6672 }
6673
6674 cond_resched();
6675 }
6676
6677 return ret;
6678}
6679
Josef Bacik69ffb542012-09-11 15:40:07 -04006680static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6681 u64 len, u64 orig_start,
6682 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006683 u64 orig_block_len, u64 ram_bytes,
6684 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006685{
6686 struct extent_map_tree *em_tree;
6687 struct extent_map *em;
6688 struct btrfs_root *root = BTRFS_I(inode)->root;
6689 int ret;
6690
6691 em_tree = &BTRFS_I(inode)->extent_tree;
6692 em = alloc_extent_map();
6693 if (!em)
6694 return ERR_PTR(-ENOMEM);
6695
6696 em->start = start;
6697 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006698 em->mod_start = start;
6699 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006700 em->len = len;
6701 em->block_len = block_len;
6702 em->block_start = block_start;
6703 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006704 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006705 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006706 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006707 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6708 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006709 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006710
6711 do {
6712 btrfs_drop_extent_cache(inode, em->start,
6713 em->start + em->len - 1, 0);
6714 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006715 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006716 write_unlock(&em_tree->lock);
6717 } while (ret == -EEXIST);
6718
6719 if (ret) {
6720 free_extent_map(em);
6721 return ERR_PTR(ret);
6722 }
6723
6724 return em;
6725}
6726
6727
Josef Bacik4b46fce2010-05-23 11:00:55 -04006728static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6729 struct buffer_head *bh_result, int create)
6730{
6731 struct extent_map *em;
6732 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006733 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006734 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006735 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006736 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006737 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006738 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006739 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006740
Miao Xie172a5042013-02-21 02:48:22 -07006741 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006742 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006743 else
Josef Bacikc3298612012-08-03 16:49:19 -04006744 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006745
Josef Bacikc3298612012-08-03 16:49:19 -04006746 lockstart = start;
6747 lockend = start + len - 1;
6748
Josef Bacikeb838e72012-07-31 16:28:48 -04006749 /*
6750 * If this errors out it's because we couldn't invalidate pagecache for
6751 * this range and we need to fallback to buffered.
6752 */
6753 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6754 return -ENOTBLK;
6755
Josef Bacik4b46fce2010-05-23 11:00:55 -04006756 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006757 if (IS_ERR(em)) {
6758 ret = PTR_ERR(em);
6759 goto unlock_err;
6760 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006761
6762 /*
6763 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6764 * io. INLINE is special, and we could probably kludge it in here, but
6765 * it's still buffered so for safety lets just fall back to the generic
6766 * buffered path.
6767 *
6768 * For COMPRESSED we _have_ to read the entire extent in so we can
6769 * decompress it, so there will be buffering required no matter what we
6770 * do, so go ahead and fallback to buffered.
6771 *
6772 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6773 * to buffered IO. Don't blame me, this is the price we pay for using
6774 * the generic code.
6775 */
6776 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6777 em->block_start == EXTENT_MAP_INLINE) {
6778 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006779 ret = -ENOTBLK;
6780 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006781 }
6782
6783 /* Just a good old fashioned hole, return */
6784 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6785 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6786 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006787 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006788 }
6789
6790 /*
6791 * We don't allocate a new extent in the following cases
6792 *
6793 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6794 * existing extent.
6795 * 2) The extent is marked as PREALLOC. We're good to go here and can
6796 * just use the extent.
6797 *
6798 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006799 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006800 len = min(len, em->len - (start - em->start));
6801 lockstart = start + len;
6802 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006803 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006804
6805 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6806 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6807 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808 int type;
6809 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006810 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006811
6812 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6813 type = BTRFS_ORDERED_PREALLOC;
6814 else
6815 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006816 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006818
6819 /*
6820 * we're not going to log anything, but we do need
6821 * to make sure the current transaction stays open
6822 * while we look for nocow cross refs
6823 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006824 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006825 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006826 goto must_cow;
6827
Josef Bacikeb384b52013-04-24 16:32:55 -04006828 if (can_nocow_odirect(trans, inode, start, &len, &orig_start,
6829 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006830 if (type == BTRFS_ORDERED_PREALLOC) {
6831 free_extent_map(em);
6832 em = create_pinned_em(inode, start, len,
6833 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006834 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006835 orig_block_len,
6836 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006837 if (IS_ERR(em)) {
6838 btrfs_end_transaction(trans, root);
6839 goto unlock_err;
6840 }
6841 }
6842
Chris Mason46bfbb52010-05-26 11:04:10 -04006843 ret = btrfs_add_ordered_extent_dio(inode, start,
6844 block_start, len, len, type);
6845 btrfs_end_transaction(trans, root);
6846 if (ret) {
6847 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006848 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006849 }
6850 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006851 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006852 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006853 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006854must_cow:
6855 /*
6856 * this will cow the extent, reset the len in case we changed
6857 * it above
6858 */
6859 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006860 free_extent_map(em);
6861 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006862 if (IS_ERR(em)) {
6863 ret = PTR_ERR(em);
6864 goto unlock_err;
6865 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006866 len = min(len, em->len - (start - em->start));
6867unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006868 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6869 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006870 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006871 bh_result->b_bdev = em->bdev;
6872 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006873 if (create) {
6874 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6875 set_buffer_new(bh_result);
6876
6877 /*
6878 * Need to update the i_size under the extent lock so buffered
6879 * readers will get the updated i_size when we unlock.
6880 */
6881 if (start + len > i_size_read(inode))
6882 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006883
Miao Xie172a5042013-02-21 02:48:22 -07006884 spin_lock(&BTRFS_I(inode)->lock);
6885 BTRFS_I(inode)->outstanding_extents++;
6886 spin_unlock(&BTRFS_I(inode)->lock);
6887
Miao Xie09348562013-02-07 10:12:07 +00006888 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6889 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6890 &cached_state, GFP_NOFS);
6891 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006892 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006893
Josef Bacikeb838e72012-07-31 16:28:48 -04006894 /*
6895 * In the case of write we need to clear and unlock the entire range,
6896 * in the case of read we need to unlock only the end area that we
6897 * aren't using if there is any left over space.
6898 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006899 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006900 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6901 lockend, unlock_bits, 1, 0,
6902 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006903 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006904 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006905 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006906
Josef Bacik4b46fce2010-05-23 11:00:55 -04006907 free_extent_map(em);
6908
6909 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006910
6911unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006912 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6913 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6914 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915}
6916
6917struct btrfs_dio_private {
6918 struct inode *inode;
6919 u64 logical_offset;
6920 u64 disk_bytenr;
6921 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006923
6924 /* number of bios pending for this dio */
6925 atomic_t pending_bios;
6926
6927 /* IO errors */
6928 int errors;
6929
Chris Mason9be33952013-05-17 18:30:14 -04006930 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006931 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006932
6933 /* dio_bio came from fs/direct-io.c */
6934 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006935};
6936
6937static void btrfs_endio_direct_read(struct bio *bio, int err)
6938{
Miao Xiee65e1532010-11-22 03:04:43 +00006939 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6941 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006942 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006943 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006944 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006945 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006946
6947 start = dip->logical_offset;
6948 do {
6949 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6950 struct page *page = bvec->bv_page;
6951 char *kaddr;
6952 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006953 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006954 unsigned long flags;
6955
Josef Bacikc3298612012-08-03 16:49:19 -04006956 if (get_state_private(&BTRFS_I(inode)->io_tree,
6957 start, &private))
6958 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006959 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006960 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006961 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006962 csum, bvec->bv_len);
6963 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006964 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006965 local_irq_restore(flags);
6966
6967 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006968 if (csum != private) {
6969failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006970 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6971 (unsigned long long)btrfs_ino(inode),
6972 (unsigned long long)start,
6973 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006974 err = -EIO;
6975 }
6976 }
6977
6978 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006979 bvec++;
6980 } while (bvec <= bvec_end);
6981
6982 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006983 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006984 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006985
Josef Bacik4b46fce2010-05-23 11:00:55 -04006986 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006987
6988 /* If we had a csum failure make sure to clear the uptodate flag */
6989 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006990 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6991 dio_end_io(dio_bio, err);
6992 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006993}
6994
6995static void btrfs_endio_direct_write(struct bio *bio, int err)
6996{
6997 struct btrfs_dio_private *dip = bio->bi_private;
6998 struct inode *inode = dip->inode;
6999 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007000 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007001 u64 ordered_offset = dip->logical_offset;
7002 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007003 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007004 int ret;
7005
7006 if (err)
7007 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007008again:
7009 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7010 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007011 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007012 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007013 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007014
Josef Bacik5fd02042012-05-02 14:00:54 -04007015 ordered->work.func = finish_ordered_fn;
7016 ordered->work.flags = 0;
7017 btrfs_queue_worker(&root->fs_info->endio_write_workers,
7018 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007019out_test:
7020 /*
7021 * our bio might span multiple ordered extents. If we haven't
7022 * completed the accounting for the whole dio, go back and try again
7023 */
7024 if (ordered_offset < dip->logical_offset + dip->bytes) {
7025 ordered_bytes = dip->logical_offset + dip->bytes -
7026 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007027 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007028 goto again;
7029 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007030out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007031 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007032
Josef Bacik4b46fce2010-05-23 11:00:55 -04007033 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007034
7035 /* If we had an error make sure to clear the uptodate flag */
7036 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007037 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7038 dio_end_io(dio_bio, err);
7039 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007040}
7041
Chris Masoneaf25d92010-05-25 09:48:28 -04007042static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7043 struct bio *bio, int mirror_num,
7044 unsigned long bio_flags, u64 offset)
7045{
7046 int ret;
7047 struct btrfs_root *root = BTRFS_I(inode)->root;
7048 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007049 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007050 return 0;
7051}
7052
Miao Xiee65e1532010-11-22 03:04:43 +00007053static void btrfs_end_dio_bio(struct bio *bio, int err)
7054{
7055 struct btrfs_dio_private *dip = bio->bi_private;
7056
7057 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08007058 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00007059 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08007060 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00007061 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007062 dip->errors = 1;
7063
7064 /*
7065 * before atomic variable goto zero, we must make sure
7066 * dip->errors is perceived to be set.
7067 */
7068 smp_mb__before_atomic_dec();
7069 }
7070
7071 /* if there are more bios still pending for this dio, just exit */
7072 if (!atomic_dec_and_test(&dip->pending_bios))
7073 goto out;
7074
Chris Mason9be33952013-05-17 18:30:14 -04007075 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007076 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007077 } else {
7078 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007079 bio_endio(dip->orig_bio, 0);
7080 }
7081out:
7082 bio_put(bio);
7083}
7084
7085static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7086 u64 first_sector, gfp_t gfp_flags)
7087{
7088 int nr_vecs = bio_get_nr_vecs(bdev);
7089 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7090}
7091
7092static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7093 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007094 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007095{
7096 int write = rw & REQ_WRITE;
7097 struct btrfs_root *root = BTRFS_I(inode)->root;
7098 int ret;
7099
Josef Bacikb812ce22012-11-16 13:56:32 -05007100 if (async_submit)
7101 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7102
Miao Xiee65e1532010-11-22 03:04:43 +00007103 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007104
7105 if (!write) {
7106 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7107 if (ret)
7108 goto err;
7109 }
Miao Xiee65e1532010-11-22 03:04:43 +00007110
Josef Bacik1ae39932011-04-06 14:41:34 -04007111 if (skip_sum)
7112 goto map;
7113
7114 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007115 ret = btrfs_wq_submit_bio(root->fs_info,
7116 inode, rw, bio, 0, 0,
7117 file_offset,
7118 __btrfs_submit_bio_start_direct_io,
7119 __btrfs_submit_bio_done);
7120 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007121 } else if (write) {
7122 /*
7123 * If we aren't doing async submit, calculate the csum of the
7124 * bio now.
7125 */
7126 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7127 if (ret)
7128 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007129 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007130 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007131 if (ret)
7132 goto err;
7133 }
Miao Xiee65e1532010-11-22 03:04:43 +00007134
Josef Bacik1ae39932011-04-06 14:41:34 -04007135map:
7136 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007137err:
7138 bio_put(bio);
7139 return ret;
7140}
7141
7142static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7143 int skip_sum)
7144{
7145 struct inode *inode = dip->inode;
7146 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007147 struct bio *bio;
7148 struct bio *orig_bio = dip->orig_bio;
7149 struct bio_vec *bvec = orig_bio->bi_io_vec;
7150 u64 start_sector = orig_bio->bi_sector;
7151 u64 file_offset = dip->logical_offset;
7152 u64 submit_len = 0;
7153 u64 map_length;
7154 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007155 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007156 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007157
Miao Xiee65e1532010-11-22 03:04:43 +00007158 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007159 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007160 &map_length, NULL, 0);
7161 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007162 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007163 return -EIO;
7164 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007165 if (map_length >= orig_bio->bi_size) {
7166 bio = orig_bio;
7167 goto submit;
7168 }
7169
David Woodhouse53b381b2013-01-29 18:40:14 -05007170 /* async crcs make it difficult to collect full stripe writes. */
7171 if (btrfs_get_alloc_profile(root, 1) &
7172 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7173 async_submit = 0;
7174 else
7175 async_submit = 1;
7176
Josef Bacik02f57c72011-04-06 14:25:44 -04007177 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7178 if (!bio)
7179 return -ENOMEM;
7180 bio->bi_private = dip;
7181 bio->bi_end_io = btrfs_end_dio_bio;
7182 atomic_inc(&dip->pending_bios);
7183
Miao Xiee65e1532010-11-22 03:04:43 +00007184 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7185 if (unlikely(map_length < submit_len + bvec->bv_len ||
7186 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7187 bvec->bv_offset) < bvec->bv_len)) {
7188 /*
7189 * inc the count before we submit the bio so
7190 * we know the end IO handler won't happen before
7191 * we inc the count. Otherwise, the dip might get freed
7192 * before we're done setting it up
7193 */
7194 atomic_inc(&dip->pending_bios);
7195 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7196 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007197 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007198 if (ret) {
7199 bio_put(bio);
7200 atomic_dec(&dip->pending_bios);
7201 goto out_err;
7202 }
7203
Miao Xiee65e1532010-11-22 03:04:43 +00007204 start_sector += submit_len >> 9;
7205 file_offset += submit_len;
7206
7207 submit_len = 0;
7208 nr_pages = 0;
7209
7210 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7211 start_sector, GFP_NOFS);
7212 if (!bio)
7213 goto out_err;
7214 bio->bi_private = dip;
7215 bio->bi_end_io = btrfs_end_dio_bio;
7216
7217 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007218 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007219 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007220 &map_length, NULL, 0);
7221 if (ret) {
7222 bio_put(bio);
7223 goto out_err;
7224 }
7225 } else {
7226 submit_len += bvec->bv_len;
7227 nr_pages ++;
7228 bvec++;
7229 }
7230 }
7231
Josef Bacik02f57c72011-04-06 14:25:44 -04007232submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007233 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007234 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007235 if (!ret)
7236 return 0;
7237
7238 bio_put(bio);
7239out_err:
7240 dip->errors = 1;
7241 /*
7242 * before atomic variable goto zero, we must
7243 * make sure dip->errors is perceived to be set.
7244 */
7245 smp_mb__before_atomic_dec();
7246 if (atomic_dec_and_test(&dip->pending_bios))
7247 bio_io_error(dip->orig_bio);
7248
7249 /* bio_end_io() will handle error, so we needn't return it */
7250 return 0;
7251}
7252
Chris Mason9be33952013-05-17 18:30:14 -04007253static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7254 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255{
7256 struct btrfs_root *root = BTRFS_I(inode)->root;
7257 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007258 struct bio_vec *bvec = dio_bio->bi_io_vec;
7259 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007260 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007261 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007262 int ret = 0;
7263
7264 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7265
Chris Mason9be33952013-05-17 18:30:14 -04007266 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7267
7268 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007269 ret = -ENOMEM;
7270 goto free_ordered;
7271 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272
Chris Mason9be33952013-05-17 18:30:14 -04007273 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7274 if (!dip) {
7275 ret = -ENOMEM;
7276 goto free_io_bio;
7277 }
7278
7279 dip->private = dio_bio->bi_private;
7280 io_bio->bi_private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 dip->inode = inode;
7282 dip->logical_offset = file_offset;
7283
Josef Bacik4b46fce2010-05-23 11:00:55 -04007284 dip->bytes = 0;
7285 do {
7286 dip->bytes += bvec->bv_len;
7287 bvec++;
Chris Mason9be33952013-05-17 18:30:14 -04007288 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007289
Chris Mason9be33952013-05-17 18:30:14 -04007290 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7291 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007292 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007293 dip->orig_bio = io_bio;
7294 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007295 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007296
7297 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007298 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299 else
Chris Mason9be33952013-05-17 18:30:14 -04007300 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007301
Miao Xiee65e1532010-11-22 03:04:43 +00007302 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7303 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007304 return;
Chris Mason9be33952013-05-17 18:30:14 -04007305
7306free_io_bio:
7307 bio_put(io_bio);
7308
Josef Bacik4b46fce2010-05-23 11:00:55 -04007309free_ordered:
7310 /*
7311 * If this is a write, we need to clean up the reserved space and kill
7312 * the ordered extent.
7313 */
7314 if (write) {
7315 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007316 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007317 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7318 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7319 btrfs_free_reserved_extent(root, ordered->start,
7320 ordered->disk_len);
7321 btrfs_put_ordered_extent(ordered);
7322 btrfs_put_ordered_extent(ordered);
7323 }
Chris Mason9be33952013-05-17 18:30:14 -04007324 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325}
7326
Chris Mason5a5f79b2010-05-26 21:33:37 -04007327static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7328 const struct iovec *iov, loff_t offset,
7329 unsigned long nr_segs)
7330{
7331 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007332 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007333 size_t size;
7334 unsigned long addr;
7335 unsigned blocksize_mask = root->sectorsize - 1;
7336 ssize_t retval = -EINVAL;
7337 loff_t end = offset;
7338
7339 if (offset & blocksize_mask)
7340 goto out;
7341
7342 /* Check the memory alignment. Blocks cannot straddle pages */
7343 for (seg = 0; seg < nr_segs; seg++) {
7344 addr = (unsigned long)iov[seg].iov_base;
7345 size = iov[seg].iov_len;
7346 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007347 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007348 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007349
7350 /* If this is a write we don't need to check anymore */
7351 if (rw & WRITE)
7352 continue;
7353
7354 /*
7355 * Check to make sure we don't have duplicate iov_base's in this
7356 * iovec, if so return EINVAL, otherwise we'll get csum errors
7357 * when reading back.
7358 */
7359 for (i = seg + 1; i < nr_segs; i++) {
7360 if (iov[seg].iov_base == iov[i].iov_base)
7361 goto out;
7362 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007363 }
7364 retval = 0;
7365out:
7366 return retval;
7367}
Josef Bacikeb838e72012-07-31 16:28:48 -04007368
Chris Mason16432982008-04-10 10:23:21 -04007369static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7370 const struct iovec *iov, loff_t offset,
7371 unsigned long nr_segs)
7372{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373 struct file *file = iocb->ki_filp;
7374 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007375 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007376 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007377 bool wakeup = true;
7378 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007379 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007380
Chris Mason5a5f79b2010-05-26 21:33:37 -04007381 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007382 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007383 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007384
Miao Xie38851cc2013-02-08 07:04:11 +00007385 atomic_inc(&inode->i_dio_count);
7386 smp_mb__after_atomic_inc();
7387
Miao Xie09348562013-02-07 10:12:07 +00007388 if (rw & WRITE) {
7389 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007390 /*
7391 * If the write DIO is beyond the EOF, we need update
7392 * the isize, but it is protected by i_mutex. So we can
7393 * not unlock the i_mutex at this case.
7394 */
7395 if (offset + count <= inode->i_size) {
7396 mutex_unlock(&inode->i_mutex);
7397 relock = true;
7398 }
Miao Xie09348562013-02-07 10:12:07 +00007399 ret = btrfs_delalloc_reserve_space(inode, count);
7400 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007401 goto out;
7402 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7403 &BTRFS_I(inode)->runtime_flags))) {
7404 inode_dio_done(inode);
7405 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7406 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007407 }
7408
7409 ret = __blockdev_direct_IO(rw, iocb, inode,
7410 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7411 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007412 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007413 if (rw & WRITE) {
7414 if (ret < 0 && ret != -EIOCBQUEUED)
7415 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007416 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007417 btrfs_delalloc_release_space(inode,
7418 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007419 else
7420 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007421 }
Miao Xie38851cc2013-02-08 07:04:11 +00007422out:
Miao Xie2e60a512013-02-08 07:01:08 +00007423 if (wakeup)
7424 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007425 if (relock)
7426 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007427
7428 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007429}
7430
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007431#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7432
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007433static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7434 __u64 start, __u64 len)
7435{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007436 int ret;
7437
7438 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7439 if (ret)
7440 return ret;
7441
Chris Masonec29ed52011-02-23 16:23:20 -05007442 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007443}
7444
Chris Mason9ebefb182007-06-15 13:50:00 -04007445int btrfs_readpage(struct file *file, struct page *page)
7446{
Chris Masond1310b22008-01-24 16:13:08 -05007447 struct extent_io_tree *tree;
7448 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007449 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007450}
Chris Mason1832a6d2007-12-21 16:27:21 -05007451
Chris Mason39279cc2007-06-12 06:35:45 -04007452static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7453{
Chris Masond1310b22008-01-24 16:13:08 -05007454 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007455
7456
7457 if (current->flags & PF_MEMALLOC) {
7458 redirty_page_for_writepage(wbc, page);
7459 unlock_page(page);
7460 return 0;
7461 }
Chris Masond1310b22008-01-24 16:13:08 -05007462 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007463 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7464}
Chris Mason39279cc2007-06-12 06:35:45 -04007465
Eric Sandeen48a3b632013-04-25 20:41:01 +00007466static int btrfs_writepages(struct address_space *mapping,
7467 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007468{
Chris Masond1310b22008-01-24 16:13:08 -05007469 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007470
Chris Masond1310b22008-01-24 16:13:08 -05007471 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007472 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7473}
7474
Chris Mason3ab2fb52007-11-08 10:59:22 -05007475static int
7476btrfs_readpages(struct file *file, struct address_space *mapping,
7477 struct list_head *pages, unsigned nr_pages)
7478{
Chris Masond1310b22008-01-24 16:13:08 -05007479 struct extent_io_tree *tree;
7480 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007481 return extent_readpages(tree, mapping, pages, nr_pages,
7482 btrfs_get_extent);
7483}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007484static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007485{
Chris Masond1310b22008-01-24 16:13:08 -05007486 struct extent_io_tree *tree;
7487 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007488 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007489
Chris Masond1310b22008-01-24 16:13:08 -05007490 tree = &BTRFS_I(page->mapping->host)->io_tree;
7491 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007492 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007493 if (ret == 1) {
7494 ClearPagePrivate(page);
7495 set_page_private(page, 0);
7496 page_cache_release(page);
7497 }
7498 return ret;
7499}
Chris Mason39279cc2007-06-12 06:35:45 -04007500
Chris Masone6dcd2d2008-07-17 12:53:50 -04007501static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7502{
Chris Mason98509cf2008-09-11 15:51:43 -04007503 if (PageWriteback(page) || PageDirty(page))
7504 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007505 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506}
7507
Chris Masona52d9a82007-08-27 16:49:44 -04007508static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7509{
Josef Bacik5fd02042012-05-02 14:00:54 -04007510 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007511 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007512 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007513 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007514 u64 page_start = page_offset(page);
7515 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007516
Chris Mason8b62b722009-09-02 16:53:46 -04007517 /*
7518 * we have the page locked, so new writeback can't start,
7519 * and the dirty bit won't be cleared while we are here.
7520 *
7521 * Wait for IO on this page so that we can safely clear
7522 * the PagePrivate2 bit and do ordered accounting
7523 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007524 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007525
Josef Bacik5fd02042012-05-02 14:00:54 -04007526 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007527 if (offset) {
7528 btrfs_releasepage(page, GFP_NOFS);
7529 return;
7530 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007531 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007532 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007534 /*
7535 * IO on this page will never be started, so we need
7536 * to account for any ordered extents now
7537 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007538 clear_extent_bit(tree, page_start, page_end,
7539 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007540 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7541 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007542 /*
7543 * whoever cleared the private bit is responsible
7544 * for the finish_ordered_io
7545 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007546 if (TestClearPagePrivate2(page) &&
7547 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7548 PAGE_CACHE_SIZE, 1)) {
7549 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007550 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007551 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007552 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007553 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007554 }
7555 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007556 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007557 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7558 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007559 __btrfs_releasepage(page, GFP_NOFS);
7560
Chris Mason4a096752008-07-21 10:29:44 -04007561 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007562 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007563 ClearPagePrivate(page);
7564 set_page_private(page, 0);
7565 page_cache_release(page);
7566 }
Chris Mason39279cc2007-06-12 06:35:45 -04007567}
7568
Chris Mason9ebefb182007-06-15 13:50:00 -04007569/*
7570 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7571 * called from a page fault handler when a page is first dirtied. Hence we must
7572 * be careful to check for EOF conditions here. We set the page up correctly
7573 * for a written page which means we get ENOSPC checking when writing into
7574 * holes and correct delalloc and unwritten extent mapping on filesystems that
7575 * support these features.
7576 *
7577 * We are not allowed to take the i_mutex here so we have to play games to
7578 * protect against truncate races as the page could now be beyond EOF. Because
7579 * vmtruncate() writes the inode size before removing pages, once we have the
7580 * page lock we can determine safely if the page is beyond EOF. If it is not
7581 * beyond EOF, then the page is guaranteed safe against truncation until we
7582 * unlock the page.
7583 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007584int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007585{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007586 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007587 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007588 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007589 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7590 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007591 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007592 char *kaddr;
7593 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007594 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007595 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007596 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007597 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007598 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007599
Jan Karab2b5ef52012-06-12 16:20:45 +02007600 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007601 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007602 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007603 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007604 reserved = 1;
7605 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007606 if (ret) {
7607 if (ret == -ENOMEM)
7608 ret = VM_FAULT_OOM;
7609 else /* -ENOSPC, -EIO, etc */
7610 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007611 if (reserved)
7612 goto out;
7613 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007614 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007615
Nick Piggin56a76f82009-03-31 15:23:23 -07007616 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007617again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007618 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007619 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007620 page_start = page_offset(page);
7621 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007622
Chris Mason9ebefb182007-06-15 13:50:00 -04007623 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007624 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007625 /* page got truncated out from underneath us */
7626 goto out_unlock;
7627 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007628 wait_on_page_writeback(page);
7629
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007630 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007631 set_page_extent_mapped(page);
7632
Chris Masoneb84ae02008-07-17 13:53:27 -04007633 /*
7634 * we can't set the delalloc bits if there are pending ordered
7635 * extents. Drop our locks and wait for them to finish
7636 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007637 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7638 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007639 unlock_extent_cached(io_tree, page_start, page_end,
7640 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007641 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007642 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007643 btrfs_put_ordered_extent(ordered);
7644 goto again;
7645 }
7646
Josef Bacikfbf19082009-10-01 17:10:23 -04007647 /*
7648 * XXX - page_mkwrite gets called every time the page is dirtied, even
7649 * if it was already dirty, so for space accounting reasons we need to
7650 * clear any delalloc bits for the range we are fixing to save. There
7651 * is probably a better way to do this, but for now keep consistent with
7652 * prepare_pages in the normal write path.
7653 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007654 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007655 EXTENT_DIRTY | EXTENT_DELALLOC |
7656 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007657 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007658
Josef Bacik2ac55d42010-02-03 19:33:23 +00007659 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7660 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007661 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007662 unlock_extent_cached(io_tree, page_start, page_end,
7663 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007664 ret = VM_FAULT_SIGBUS;
7665 goto out_unlock;
7666 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007667 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007668
7669 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007670 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007671 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007672 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007673 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007674
Chris Masone6dcd2d2008-07-17 12:53:50 -04007675 if (zero_start != PAGE_CACHE_SIZE) {
7676 kaddr = kmap(page);
7677 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7678 flush_dcache_page(page);
7679 kunmap(page);
7680 }
Chris Mason247e7432008-07-17 12:53:51 -04007681 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007682 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007683 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007684
Chris Mason257c62e2009-10-13 13:21:08 -04007685 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7686 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007687 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007688
Josef Bacik2ac55d42010-02-03 19:33:23 +00007689 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007690
7691out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007692 if (!ret) {
7693 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007694 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007695 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007696 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007697out:
Josef Bacikec39e182012-01-12 19:10:12 -05007698 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007699out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007700 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007701 return ret;
7702}
7703
Josef Bacika41ad392011-01-31 15:30:16 -05007704static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007705{
7706 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007707 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007708 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007709 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007710 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007711 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007712 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007713
Josef Bacik2aaa6652012-08-29 14:27:18 -04007714 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007715 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007716 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007717
Chris Mason4a096752008-07-21 10:29:44 -04007718 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007719 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007720
Josef Bacikfcb80c22011-05-03 10:40:22 -04007721 /*
7722 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7723 * 3 things going on here
7724 *
7725 * 1) We need to reserve space for our orphan item and the space to
7726 * delete our orphan item. Lord knows we don't want to have a dangling
7727 * orphan item because we didn't reserve space to remove it.
7728 *
7729 * 2) We need to reserve space to update our inode.
7730 *
7731 * 3) We need to have something to cache all the space that is going to
7732 * be free'd up by the truncate operation, but also have some slack
7733 * space reserved in case it uses space during the truncate (thank you
7734 * very much snapshotting).
7735 *
7736 * And we need these to all be seperate. The fact is we can use alot of
7737 * space doing the truncate, and we have no earthly idea how much space
7738 * we will use, so we need the truncate reservation to be seperate so it
7739 * doesn't end up using space reserved for updating the inode or
7740 * removing the orphan item. We also need to be able to stop the
7741 * transaction and start a new one, which means we need to be able to
7742 * update the inode several times, and we have no idea of knowing how
7743 * many times that will be, so we can't just reserve 1 item for the
7744 * entirety of the opration, so that has to be done seperately as well.
7745 * Then there is the orphan item, which does indeed need to be held on
7746 * to for the whole operation, and we need nobody to touch this reserved
7747 * space except the orphan code.
7748 *
7749 * So that leaves us with
7750 *
7751 * 1) root->orphan_block_rsv - for the orphan deletion.
7752 * 2) rsv - for the truncate reservation, which we will steal from the
7753 * transaction reservation.
7754 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7755 * updating the inode.
7756 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007757 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007758 if (!rsv)
7759 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007760 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007761 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007762
Josef Bacik907cbce2011-08-08 13:46:15 -04007763 /*
Josef Bacik07127182011-08-19 10:29:59 -04007764 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007765 * 1 for updating the inode.
7766 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007767 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007768 if (IS_ERR(trans)) {
7769 err = PTR_ERR(trans);
7770 goto out;
7771 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007772
Josef Bacik907cbce2011-08-08 13:46:15 -04007773 /* Migrate the slack space for the truncate to our reserve */
7774 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7775 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007776 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007777
Chris Mason5a3f23d2009-03-31 13:27:11 -04007778 /*
7779 * setattr is responsible for setting the ordered_data_close flag,
7780 * but that is only tested during the last file release. That
7781 * could happen well after the next commit, leaving a great big
7782 * window where new writes may get lost if someone chooses to write
7783 * to this file after truncating to zero
7784 *
7785 * The inode doesn't have any dirty data here, and so if we commit
7786 * this is a noop. If someone immediately starts writing to the inode
7787 * it is very likely we'll catch some of their writes in this
7788 * transaction, and the commit will find this file on the ordered
7789 * data list with good things to send down.
7790 *
7791 * This is a best effort solution, there is still a window where
7792 * using truncate to replace the contents of the file will
7793 * end up with a zero length file after a crash.
7794 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007795 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7796 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007797 btrfs_add_ordered_operation(trans, root, inode);
7798
Josef Bacik5dc562c2012-08-17 13:14:17 -04007799 /*
7800 * So if we truncate and then write and fsync we normally would just
7801 * write the extents that changed, which is a problem if we need to
7802 * first truncate that entire inode. So set this flag so we write out
7803 * all of the extents in the inode to the sync log so we're completely
7804 * safe.
7805 */
7806 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007807 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007808
Yan, Zheng80825102009-11-12 09:35:36 +00007809 while (1) {
7810 ret = btrfs_truncate_inode_items(trans, root, inode,
7811 inode->i_size,
7812 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007813 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007814 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007815 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007816 }
Chris Mason39279cc2007-06-12 06:35:45 -04007817
Josef Bacikfcb80c22011-05-03 10:40:22 -04007818 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007819 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007820 if (ret) {
7821 err = ret;
7822 break;
7823 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007824
Yan, Zheng80825102009-11-12 09:35:36 +00007825 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007826 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007827
7828 trans = btrfs_start_transaction(root, 2);
7829 if (IS_ERR(trans)) {
7830 ret = err = PTR_ERR(trans);
7831 trans = NULL;
7832 break;
7833 }
7834
7835 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7836 rsv, min_size);
7837 BUG_ON(ret); /* shouldn't happen */
7838 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007839 }
7840
7841 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007842 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007843 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007844 if (ret)
7845 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007846 }
7847
Chris Mason917c16b2011-11-08 14:49:59 -05007848 if (trans) {
7849 trans->block_rsv = &root->fs_info->trans_block_rsv;
7850 ret = btrfs_update_inode(trans, root, inode);
7851 if (ret && !err)
7852 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007853
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007854 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007855 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007856 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007857
7858out:
7859 btrfs_free_block_rsv(root, rsv);
7860
Josef Bacik3893e332011-01-31 16:03:11 -05007861 if (ret && !err)
7862 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007863
Josef Bacik3893e332011-01-31 16:03:11 -05007864 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007865}
7866
Sven Wegener3b963622008-06-09 21:57:42 -04007867/*
Chris Masond352ac62008-09-29 15:18:18 -04007868 * create a new subvolume directory/inode (helper for the ioctl).
7869 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007870int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007871 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007872{
Chris Mason39279cc2007-06-12 06:35:45 -04007873 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007874 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007875 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007876
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007877 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7878 new_dirid, new_dirid,
7879 S_IFDIR | (~current_umask() & S_IRWXUGO),
7880 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007881 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007882 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007883 inode->i_op = &btrfs_dir_inode_operations;
7884 inode->i_fop = &btrfs_dir_file_operations;
7885
Miklos Szeredibfe86842011-10-28 14:13:29 +02007886 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007887 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007888
Yan, Zheng76dda932009-09-21 16:00:26 -04007889 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007890
Yan, Zheng76dda932009-09-21 16:00:26 -04007891 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007892 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007893}
7894
Chris Mason39279cc2007-06-12 06:35:45 -04007895struct inode *btrfs_alloc_inode(struct super_block *sb)
7896{
7897 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007898 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007899
7900 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7901 if (!ei)
7902 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007903
7904 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007905 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007906 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007907 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007908 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007909 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007910 ei->disk_i_size = 0;
7911 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007912 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007913 ei->index_cnt = (u64)-1;
7914 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007915 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007916
Josef Bacik9e0baf62011-07-15 15:16:44 +00007917 spin_lock_init(&ei->lock);
7918 ei->outstanding_extents = 0;
7919 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007920
Josef Bacik72ac3c02012-05-23 14:13:11 -04007921 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007922 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007923
Miao Xie16cdcec2011-04-22 18:12:22 +08007924 ei->delayed_node = NULL;
7925
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007926 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007927 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007928 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7929 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007930 ei->io_tree.track_uptodate = 1;
7931 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007932 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007933 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007934 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007935 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007936 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007937 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007938 RB_CLEAR_NODE(&ei->rb_node);
7939
7940 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007941}
7942
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007943static void btrfs_i_callback(struct rcu_head *head)
7944{
7945 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007946 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7947}
7948
Chris Mason39279cc2007-06-12 06:35:45 -04007949void btrfs_destroy_inode(struct inode *inode)
7950{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007951 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007952 struct btrfs_root *root = BTRFS_I(inode)->root;
7953
Al Virob3d9b7a2012-06-09 13:51:19 -04007954 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007955 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007956 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7957 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007958 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7959 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007960
Chris Mason5a3f23d2009-03-31 13:27:11 -04007961 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007962 * This can happen where we create an inode, but somebody else also
7963 * created the same inode and we need to destroy the one we already
7964 * created.
7965 */
7966 if (!root)
7967 goto free;
7968
7969 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007970 * Make sure we're properly removed from the ordered operation
7971 * lists.
7972 */
7973 smp_mb();
7974 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7975 spin_lock(&root->fs_info->ordered_extent_lock);
7976 list_del_init(&BTRFS_I(inode)->ordered_operations);
7977 spin_unlock(&root->fs_info->ordered_extent_lock);
7978 }
7979
Josef Bacik8a35d952012-05-23 14:26:42 -04007980 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7981 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007982 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7983 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007984 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007985 }
Josef Bacik7b128762008-07-24 12:17:14 -04007986
Chris Masond3977122009-01-05 21:25:51 -05007987 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007988 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7989 if (!ordered)
7990 break;
7991 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007992 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7993 (unsigned long long)ordered->file_offset,
7994 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007995 btrfs_remove_ordered_extent(inode, ordered);
7996 btrfs_put_ordered_extent(ordered);
7997 btrfs_put_ordered_extent(ordered);
7998 }
7999 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008000 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008001 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008002free:
Miao Xie16cdcec2011-04-22 18:12:22 +08008003 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008004 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008005}
8006
Al Viro45321ac2010-06-07 13:43:19 -04008007int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008008{
8009 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008010
Liu Bofa6ac872013-02-20 14:10:23 +00008011 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04008012 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00008013 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04008014 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008015 else
Al Viro45321ac2010-06-07 13:43:19 -04008016 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008017}
8018
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008019static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008020{
8021 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8022
8023 inode_init_once(&ei->vfs_inode);
8024}
8025
8026void btrfs_destroy_cachep(void)
8027{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008028 /*
8029 * Make sure all delayed rcu free inodes are flushed before we
8030 * destroy cache.
8031 */
8032 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008033 if (btrfs_inode_cachep)
8034 kmem_cache_destroy(btrfs_inode_cachep);
8035 if (btrfs_trans_handle_cachep)
8036 kmem_cache_destroy(btrfs_trans_handle_cachep);
8037 if (btrfs_transaction_cachep)
8038 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008039 if (btrfs_path_cachep)
8040 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008041 if (btrfs_free_space_cachep)
8042 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008043 if (btrfs_delalloc_work_cachep)
8044 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008045}
8046
8047int btrfs_init_cachep(void)
8048{
David Sterba837e1972012-09-07 03:00:48 -06008049 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008050 sizeof(struct btrfs_inode), 0,
8051 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008052 if (!btrfs_inode_cachep)
8053 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008054
David Sterba837e1972012-09-07 03:00:48 -06008055 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008056 sizeof(struct btrfs_trans_handle), 0,
8057 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008058 if (!btrfs_trans_handle_cachep)
8059 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008060
David Sterba837e1972012-09-07 03:00:48 -06008061 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008062 sizeof(struct btrfs_transaction), 0,
8063 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008064 if (!btrfs_transaction_cachep)
8065 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008066
David Sterba837e1972012-09-07 03:00:48 -06008067 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008068 sizeof(struct btrfs_path), 0,
8069 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008070 if (!btrfs_path_cachep)
8071 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008072
David Sterba837e1972012-09-07 03:00:48 -06008073 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008074 sizeof(struct btrfs_free_space), 0,
8075 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8076 if (!btrfs_free_space_cachep)
8077 goto fail;
8078
Miao Xie8ccf6f192012-10-25 09:28:04 +00008079 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8080 sizeof(struct btrfs_delalloc_work), 0,
8081 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8082 NULL);
8083 if (!btrfs_delalloc_work_cachep)
8084 goto fail;
8085
Chris Mason39279cc2007-06-12 06:35:45 -04008086 return 0;
8087fail:
8088 btrfs_destroy_cachep();
8089 return -ENOMEM;
8090}
8091
8092static int btrfs_getattr(struct vfsmount *mnt,
8093 struct dentry *dentry, struct kstat *stat)
8094{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008095 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008096 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008097 u32 blocksize = inode->i_sb->s_blocksize;
8098
Chris Mason39279cc2007-06-12 06:35:45 -04008099 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008100 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008101 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008102
8103 spin_lock(&BTRFS_I(inode)->lock);
8104 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8105 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008106 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008107 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008108 return 0;
8109}
8110
Chris Masond3977122009-01-05 21:25:51 -05008111static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8112 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008113{
8114 struct btrfs_trans_handle *trans;
8115 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008116 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008117 struct inode *new_inode = new_dentry->d_inode;
8118 struct inode *old_inode = old_dentry->d_inode;
8119 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008120 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008121 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008122 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008123 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008124
Li Zefan33345d012011-04-20 10:31:50 +08008125 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008126 return -EPERM;
8127
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008128 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008129 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008130 return -EXDEV;
8131
Li Zefan33345d012011-04-20 10:31:50 +08008132 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8133 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008134 return -ENOTEMPTY;
8135
Chris Mason39279cc2007-06-12 06:35:45 -04008136 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008137 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008138 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008139
8140
8141 /* check for collisions, even if the name isn't there */
8142 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8143 new_dentry->d_name.name,
8144 new_dentry->d_name.len);
8145
8146 if (ret) {
8147 if (ret == -EEXIST) {
8148 /* we shouldn't get
8149 * eexist without a new_inode */
8150 if (!new_inode) {
8151 WARN_ON(1);
8152 return ret;
8153 }
8154 } else {
8155 /* maybe -EOVERFLOW */
8156 return ret;
8157 }
8158 }
8159 ret = 0;
8160
Chris Mason5a3f23d2009-03-31 13:27:11 -04008161 /*
8162 * we're using rename to replace one file with another.
8163 * and the replacement file is large. Start IO on it now so
8164 * we don't add too much work to the end of the transaction
8165 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008166 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008167 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8168 filemap_flush(old_inode->i_mapping);
8169
Yan, Zheng76dda932009-09-21 16:00:26 -04008170 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008171 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008172 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008173 /*
8174 * We want to reserve the absolute worst case amount of items. So if
8175 * both inodes are subvols and we need to unlink them then that would
8176 * require 4 item modifications, but if they are both normal inodes it
8177 * would require 5 item modifications, so we'll assume their normal
8178 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8179 * should cover the worst case number of items we'll modify.
8180 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008181 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008182 if (IS_ERR(trans)) {
8183 ret = PTR_ERR(trans);
8184 goto out_notrans;
8185 }
Chris Mason5f39d392007-10-15 16:14:19 -04008186
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008187 if (dest != root)
8188 btrfs_record_root_in_trans(trans, dest);
8189
Yan, Zhenga5719522009-09-24 09:17:31 -04008190 ret = btrfs_set_inode_index(new_dir, &index);
8191 if (ret)
8192 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008193
Li Zefan33345d012011-04-20 10:31:50 +08008194 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008195 /* force full log commit if subvolume involved. */
8196 root->fs_info->last_trans_log_full_commit = trans->transid;
8197 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008198 ret = btrfs_insert_inode_ref(trans, dest,
8199 new_dentry->d_name.name,
8200 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008201 old_ino,
8202 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008203 if (ret)
8204 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008205 /*
8206 * this is an ugly little race, but the rename is required
8207 * to make sure that if we crash, the inode is either at the
8208 * old name or the new one. pinning the log transaction lets
8209 * us make sure we don't allow a log commit to come in after
8210 * we unlink the name but before we add the new name back in.
8211 */
8212 btrfs_pin_log_trans(root);
8213 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008214 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008215 * make sure the inode gets flushed if it is replacing
8216 * something.
8217 */
Li Zefan33345d012011-04-20 10:31:50 +08008218 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008219 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008220
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008221 inode_inc_iversion(old_dir);
8222 inode_inc_iversion(new_dir);
8223 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008224 old_dir->i_ctime = old_dir->i_mtime = ctime;
8225 new_dir->i_ctime = new_dir->i_mtime = ctime;
8226 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008227
Chris Mason12fcfd22009-03-24 10:24:20 -04008228 if (old_dentry->d_parent != new_dentry->d_parent)
8229 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8230
Li Zefan33345d012011-04-20 10:31:50 +08008231 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008232 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8233 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8234 old_dentry->d_name.name,
8235 old_dentry->d_name.len);
8236 } else {
Al Viro92986792011-03-04 17:14:37 +00008237 ret = __btrfs_unlink_inode(trans, root, old_dir,
8238 old_dentry->d_inode,
8239 old_dentry->d_name.name,
8240 old_dentry->d_name.len);
8241 if (!ret)
8242 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008243 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008244 if (ret) {
8245 btrfs_abort_transaction(trans, root, ret);
8246 goto out_fail;
8247 }
Chris Mason39279cc2007-06-12 06:35:45 -04008248
8249 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008250 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008251 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008252 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008253 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8254 root_objectid = BTRFS_I(new_inode)->location.objectid;
8255 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8256 root_objectid,
8257 new_dentry->d_name.name,
8258 new_dentry->d_name.len);
8259 BUG_ON(new_inode->i_nlink == 0);
8260 } else {
8261 ret = btrfs_unlink_inode(trans, dest, new_dir,
8262 new_dentry->d_inode,
8263 new_dentry->d_name.name,
8264 new_dentry->d_name.len);
8265 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008266 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008267 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008268 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008269 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008270 if (ret) {
8271 btrfs_abort_transaction(trans, root, ret);
8272 goto out_fail;
8273 }
Chris Mason39279cc2007-06-12 06:35:45 -04008274 }
Josef Bacikaec74772008-07-24 12:12:38 -04008275
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008276 ret = btrfs_add_link(trans, new_dir, old_inode,
8277 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008278 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008279 if (ret) {
8280 btrfs_abort_transaction(trans, root, ret);
8281 goto out_fail;
8282 }
Chris Mason39279cc2007-06-12 06:35:45 -04008283
Li Zefan33345d012011-04-20 10:31:50 +08008284 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008285 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008286 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008287 btrfs_end_log_trans(root);
8288 }
Chris Mason39279cc2007-06-12 06:35:45 -04008289out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008290 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008291out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008292 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008293 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008294
Chris Mason39279cc2007-06-12 06:35:45 -04008295 return ret;
8296}
8297
Miao Xie8ccf6f192012-10-25 09:28:04 +00008298static void btrfs_run_delalloc_work(struct btrfs_work *work)
8299{
8300 struct btrfs_delalloc_work *delalloc_work;
8301
8302 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8303 work);
8304 if (delalloc_work->wait)
8305 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8306 else
8307 filemap_flush(delalloc_work->inode->i_mapping);
8308
8309 if (delalloc_work->delay_iput)
8310 btrfs_add_delayed_iput(delalloc_work->inode);
8311 else
8312 iput(delalloc_work->inode);
8313 complete(&delalloc_work->completion);
8314}
8315
8316struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8317 int wait, int delay_iput)
8318{
8319 struct btrfs_delalloc_work *work;
8320
8321 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8322 if (!work)
8323 return NULL;
8324
8325 init_completion(&work->completion);
8326 INIT_LIST_HEAD(&work->list);
8327 work->inode = inode;
8328 work->wait = wait;
8329 work->delay_iput = delay_iput;
8330 work->work.func = btrfs_run_delalloc_work;
8331
8332 return work;
8333}
8334
8335void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8336{
8337 wait_for_completion(&work->completion);
8338 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8339}
8340
Chris Masond352ac62008-09-29 15:18:18 -04008341/*
8342 * some fairly slow code that needs optimization. This walks the list
8343 * of all the inodes with pending delalloc and forces them to disk.
8344 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00008345int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008346{
Chris Masonea8c2812008-08-04 23:17:27 -04008347 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008348 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008349 struct btrfs_delalloc_work *work, *next;
8350 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008351 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008352 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008353
Yan Zhengc146afa2008-11-12 14:34:12 -05008354 if (root->fs_info->sb->s_flags & MS_RDONLY)
8355 return -EROFS;
8356
Miao Xie8ccf6f192012-10-25 09:28:04 +00008357 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008358 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008359
Chris Mason75eff682008-12-15 15:54:40 -05008360 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008361 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
8362 while (!list_empty(&splice)) {
8363 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008364 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008365
8366 list_del_init(&binode->delalloc_inodes);
8367
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008368 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008369 if (!inode) {
8370 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
8371 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008372 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008373 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008374
8375 list_add_tail(&binode->delalloc_inodes,
8376 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05008377 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008378
8379 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8380 if (unlikely(!work)) {
8381 ret = -ENOMEM;
8382 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008383 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008384 list_add_tail(&work->list, &works);
8385 btrfs_queue_worker(&root->fs_info->flush_workers,
8386 &work->work);
8387
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008388 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05008389 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008390 }
Chris Mason75eff682008-12-15 15:54:40 -05008391 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008392
Miao Xie1eafa6c2013-01-22 10:49:00 +00008393 list_for_each_entry_safe(work, next, &works, list) {
8394 list_del_init(&work->list);
8395 btrfs_wait_and_free_delalloc_work(work);
8396 }
8397
Chris Mason8c8bee12008-09-29 11:19:10 -04008398 /* the filemap_flush will queue IO into the worker threads, but
8399 * we have to make sure the IO is actually started and that
8400 * ordered extents get created before we return
8401 */
8402 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008403 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008404 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008405 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008406 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8407 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008408 }
8409 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008410 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008411out:
8412 list_for_each_entry_safe(work, next, &works, list) {
8413 list_del_init(&work->list);
8414 btrfs_wait_and_free_delalloc_work(work);
8415 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008416
8417 if (!list_empty_careful(&splice)) {
8418 spin_lock(&root->fs_info->delalloc_lock);
8419 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
8420 spin_unlock(&root->fs_info->delalloc_lock);
8421 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008422 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008423}
8424
Chris Mason39279cc2007-06-12 06:35:45 -04008425static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8426 const char *symname)
8427{
8428 struct btrfs_trans_handle *trans;
8429 struct btrfs_root *root = BTRFS_I(dir)->root;
8430 struct btrfs_path *path;
8431 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008432 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008433 int err;
8434 int drop_inode = 0;
8435 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008436 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008437 int name_len;
8438 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008439 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008440 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008441 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008442
8443 name_len = strlen(symname) + 1;
8444 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8445 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008446
Josef Bacik9ed74f22009-09-11 16:12:44 -04008447 /*
8448 * 2 items for inode item and ref
8449 * 2 items for dir items
8450 * 1 item for xattr if selinux is on
8451 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008452 trans = btrfs_start_transaction(root, 5);
8453 if (IS_ERR(trans))
8454 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008455
Li Zefan581bb052011-04-20 10:06:11 +08008456 err = btrfs_find_free_ino(root, &objectid);
8457 if (err)
8458 goto out_unlock;
8459
Josef Bacikaec74772008-07-24 12:12:38 -04008460 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008461 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008462 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008463 if (IS_ERR(inode)) {
8464 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008465 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008466 }
Chris Mason39279cc2007-06-12 06:35:45 -04008467
Eric Paris2a7dba32011-02-01 11:05:39 -05008468 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008469 if (err) {
8470 drop_inode = 1;
8471 goto out_unlock;
8472 }
8473
Casey Schauflerad19db72011-12-15 10:09:07 -05008474 /*
8475 * If the active LSM wants to access the inode during
8476 * d_instantiate it needs these. Smack checks to see
8477 * if the filesystem supports xattrs by looking at the
8478 * ops vector.
8479 */
8480 inode->i_fop = &btrfs_file_operations;
8481 inode->i_op = &btrfs_file_inode_operations;
8482
Josef Bacika1b075d2010-11-19 20:36:11 +00008483 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008484 if (err)
8485 drop_inode = 1;
8486 else {
8487 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008488 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008489 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008490 }
Chris Mason39279cc2007-06-12 06:35:45 -04008491 if (drop_inode)
8492 goto out_unlock;
8493
8494 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008495 if (!path) {
8496 err = -ENOMEM;
8497 drop_inode = 1;
8498 goto out_unlock;
8499 }
Li Zefan33345d012011-04-20 10:31:50 +08008500 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008501 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008502 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8503 datasize = btrfs_file_extent_calc_inline_size(name_len);
8504 err = btrfs_insert_empty_item(trans, root, path, &key,
8505 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008506 if (err) {
8507 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008508 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008509 goto out_unlock;
8510 }
Chris Mason5f39d392007-10-15 16:14:19 -04008511 leaf = path->nodes[0];
8512 ei = btrfs_item_ptr(leaf, path->slots[0],
8513 struct btrfs_file_extent_item);
8514 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8515 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008516 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008517 btrfs_set_file_extent_encryption(leaf, ei, 0);
8518 btrfs_set_file_extent_compression(leaf, ei, 0);
8519 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8520 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8521
Chris Mason39279cc2007-06-12 06:35:45 -04008522 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008523 write_extent_buffer(leaf, symname, ptr, name_len);
8524 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008525 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008526
Chris Mason39279cc2007-06-12 06:35:45 -04008527 inode->i_op = &btrfs_symlink_inode_operations;
8528 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008529 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008530 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008531 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008532 err = btrfs_update_inode(trans, root, inode);
8533 if (err)
8534 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008535
8536out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008537 if (!err)
8538 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008539 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008540 if (drop_inode) {
8541 inode_dec_link_count(inode);
8542 iput(inode);
8543 }
Liu Bob53d3f52012-11-14 14:34:34 +00008544 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008545 return err;
8546}
Chris Mason16432982008-04-10 10:23:21 -04008547
Josef Bacik0af3d002010-06-21 14:48:16 -04008548static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8549 u64 start, u64 num_bytes, u64 min_size,
8550 loff_t actual_len, u64 *alloc_hint,
8551 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008552{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008553 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8554 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008555 struct btrfs_root *root = BTRFS_I(inode)->root;
8556 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008557 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008558 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008559 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008560 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008561 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008562
Josef Bacik0af3d002010-06-21 14:48:16 -04008563 if (trans)
8564 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008565 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008566 if (own_trans) {
8567 trans = btrfs_start_transaction(root, 3);
8568 if (IS_ERR(trans)) {
8569 ret = PTR_ERR(trans);
8570 break;
8571 }
Yan Zhengd899e052008-10-30 14:25:28 -04008572 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008573
Chris Mason154ea282013-03-05 11:11:26 -05008574 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8575 cur_bytes = max(cur_bytes, min_size);
8576 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008577 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008578 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008579 if (own_trans)
8580 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008581 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008582 }
8583
Yan Zhengd899e052008-10-30 14:25:28 -04008584 ret = insert_reserved_file_extent(trans, inode,
8585 cur_offset, ins.objectid,
8586 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008587 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008588 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008589 if (ret) {
8590 btrfs_abort_transaction(trans, root, ret);
8591 if (own_trans)
8592 btrfs_end_transaction(trans, root);
8593 break;
8594 }
Chris Masona1ed8352009-09-11 12:27:37 -04008595 btrfs_drop_extent_cache(inode, cur_offset,
8596 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008597
Josef Bacik5dc562c2012-08-17 13:14:17 -04008598 em = alloc_extent_map();
8599 if (!em) {
8600 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8601 &BTRFS_I(inode)->runtime_flags);
8602 goto next;
8603 }
8604
8605 em->start = cur_offset;
8606 em->orig_start = cur_offset;
8607 em->len = ins.offset;
8608 em->block_start = ins.objectid;
8609 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008610 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008611 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008612 em->bdev = root->fs_info->fs_devices->latest_bdev;
8613 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8614 em->generation = trans->transid;
8615
8616 while (1) {
8617 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008618 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008619 write_unlock(&em_tree->lock);
8620 if (ret != -EEXIST)
8621 break;
8622 btrfs_drop_extent_cache(inode, cur_offset,
8623 cur_offset + ins.offset - 1,
8624 0);
8625 }
8626 free_extent_map(em);
8627next:
Yan Zhengd899e052008-10-30 14:25:28 -04008628 num_bytes -= ins.offset;
8629 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008630 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008631
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008632 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008633 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008634 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008635 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008636 (actual_len > inode->i_size) &&
8637 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008638 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008639 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008640 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008641 i_size = cur_offset;
8642 i_size_write(inode, i_size);
8643 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008644 }
8645
Yan Zhengd899e052008-10-30 14:25:28 -04008646 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008647
8648 if (ret) {
8649 btrfs_abort_transaction(trans, root, ret);
8650 if (own_trans)
8651 btrfs_end_transaction(trans, root);
8652 break;
8653 }
Yan Zhengd899e052008-10-30 14:25:28 -04008654
Josef Bacik0af3d002010-06-21 14:48:16 -04008655 if (own_trans)
8656 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008657 }
Yan Zhengd899e052008-10-30 14:25:28 -04008658 return ret;
8659}
8660
Josef Bacik0af3d002010-06-21 14:48:16 -04008661int btrfs_prealloc_file_range(struct inode *inode, int mode,
8662 u64 start, u64 num_bytes, u64 min_size,
8663 loff_t actual_len, u64 *alloc_hint)
8664{
8665 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8666 min_size, actual_len, alloc_hint,
8667 NULL);
8668}
8669
8670int btrfs_prealloc_file_range_trans(struct inode *inode,
8671 struct btrfs_trans_handle *trans, int mode,
8672 u64 start, u64 num_bytes, u64 min_size,
8673 loff_t actual_len, u64 *alloc_hint)
8674{
8675 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8676 min_size, actual_len, alloc_hint, trans);
8677}
8678
Chris Masone6dcd2d2008-07-17 12:53:50 -04008679static int btrfs_set_page_dirty(struct page *page)
8680{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008681 return __set_page_dirty_nobuffers(page);
8682}
8683
Al Viro10556cb2011-06-20 19:28:19 -04008684static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008685{
Li Zefanb83cc962010-12-20 16:04:08 +08008686 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008687 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008688
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008689 if (mask & MAY_WRITE &&
8690 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8691 if (btrfs_root_readonly(root))
8692 return -EROFS;
8693 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8694 return -EACCES;
8695 }
Al Viro2830ba72011-06-20 19:16:29 -04008696 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008697}
Chris Mason39279cc2007-06-12 06:35:45 -04008698
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008699static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008700 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008701 .lookup = btrfs_lookup,
8702 .create = btrfs_create,
8703 .unlink = btrfs_unlink,
8704 .link = btrfs_link,
8705 .mkdir = btrfs_mkdir,
8706 .rmdir = btrfs_rmdir,
8707 .rename = btrfs_rename,
8708 .symlink = btrfs_symlink,
8709 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008710 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008711 .setxattr = btrfs_setxattr,
8712 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008713 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008714 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008715 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008716 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008717};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008718static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008719 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008720 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008721 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008722};
Yan, Zheng76dda932009-09-21 16:00:26 -04008723
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008724static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008725 .llseek = generic_file_llseek,
8726 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008727 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008728 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008729#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008730 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008731#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008732 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008733 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008734};
8735
Chris Masond1310b22008-01-24 16:13:08 -05008736static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008737 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008738 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008739 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008740 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008741 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008742 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008743 .set_bit_hook = btrfs_set_bit_hook,
8744 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008745 .merge_extent_hook = btrfs_merge_extent_hook,
8746 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008747};
8748
Chris Mason35054392009-01-21 13:11:13 -05008749/*
8750 * btrfs doesn't support the bmap operation because swapfiles
8751 * use bmap to make a mapping of extents in the file. They assume
8752 * these extents won't change over the life of the file and they
8753 * use the bmap result to do IO directly to the drive.
8754 *
8755 * the btrfs bmap call would return logical addresses that aren't
8756 * suitable for IO and they also will change frequently as COW
8757 * operations happen. So, swapfile + btrfs == corruption.
8758 *
8759 * For now we're avoiding this by dropping bmap.
8760 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008761static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008762 .readpage = btrfs_readpage,
8763 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008764 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008765 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008766 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008767 .invalidatepage = btrfs_invalidatepage,
8768 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008769 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008770 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008771};
8772
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008773static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008774 .readpage = btrfs_readpage,
8775 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008776 .invalidatepage = btrfs_invalidatepage,
8777 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008778};
8779
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008780static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008781 .getattr = btrfs_getattr,
8782 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008783 .setxattr = btrfs_setxattr,
8784 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008785 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008786 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008787 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008788 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008789 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008790 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008791};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008792static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008793 .getattr = btrfs_getattr,
8794 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008795 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008796 .setxattr = btrfs_setxattr,
8797 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008798 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008799 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008800 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008801 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008802};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008803static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008804 .readlink = generic_readlink,
8805 .follow_link = page_follow_link_light,
8806 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008807 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008808 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008809 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008810 .setxattr = btrfs_setxattr,
8811 .getxattr = btrfs_getxattr,
8812 .listxattr = btrfs_listxattr,
8813 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008814 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008815 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008816};
Yan, Zheng76dda932009-09-21 16:00:26 -04008817
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008818const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008819 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008820 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008821};