blob: 3df0ffad976eb338a8a8293f256ffc0cde3f2efd [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>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040040#include <linux/falloc.h>
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040047#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "compat.h"
51#include "tree-log.h"
Yan Zheng5b84e8d2008-10-09 11:46:19 -040052#include "ref-cache.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
56 u64 ino;
57 struct btrfs_root *root;
58};
59
60static struct inode_operations btrfs_dir_inode_operations;
61static struct inode_operations btrfs_symlink_inode_operations;
62static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040063static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040064static struct inode_operations btrfs_file_inode_operations;
65static struct address_space_operations btrfs_aops;
66static struct address_space_operations btrfs_symlink_aops;
67static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050068static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040069
70static struct kmem_cache *btrfs_inode_cachep;
71struct kmem_cache *btrfs_trans_handle_cachep;
72struct kmem_cache *btrfs_transaction_cachep;
73struct kmem_cache *btrfs_bit_radix_cachep;
74struct kmem_cache *btrfs_path_cachep;
75
76#define S_SHIFT 12
77static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Josef Bacik7b128762008-07-24 12:17:14 -040087static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040088static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Josef Bacik7b128762008-07-24 12:17:14 -040089
Chris Masond352ac62008-09-29 15:18:18 -040090/*
91 * a very lame attempt at stopping writes when the FS is 85% full. There
92 * are countless ways this is incorrect, but it is better than nothing.
93 */
Chris Mason1832a6d2007-12-21 16:27:21 -050094int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
95 int for_del)
96{
Chris Masona2135012008-06-25 16:01:30 -040097 u64 total;
98 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050099 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -0400100 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -0500101 int ret = 0;
102
Chris Masona2135012008-06-25 16:01:30 -0400103 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
104 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
105 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -0500106 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -0500107 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -0500108 else
Chris Masonf9ef6602008-01-03 09:22:38 -0500109 thresh = total * 85;
110
111 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500112
Chris Mason1832a6d2007-12-21 16:27:21 -0500113 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
114 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400115 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500116 return ret;
117}
118
Chris Masond352ac62008-09-29 15:18:18 -0400119/*
Chris Masonc8b97812008-10-29 14:49:59 -0400120 * this does all the hard work for inserting an inline extent into
121 * the btree. The caller should have done a btrfs_drop_extents so that
122 * no overlapping inline items exist in the btree
123 */
124static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
125 struct btrfs_root *root, struct inode *inode,
126 u64 start, size_t size, size_t compressed_size,
127 struct page **compressed_pages)
128{
129 struct btrfs_key key;
130 struct btrfs_path *path;
131 struct extent_buffer *leaf;
132 struct page *page = NULL;
133 char *kaddr;
134 unsigned long ptr;
135 struct btrfs_file_extent_item *ei;
136 int err = 0;
137 int ret;
138 size_t cur_size = size;
139 size_t datasize;
140 unsigned long offset;
141 int use_compress = 0;
142
143 if (compressed_size && compressed_pages) {
144 use_compress = 1;
145 cur_size = compressed_size;
146 }
147
148 path = btrfs_alloc_path(); if (!path)
149 return -ENOMEM;
150
151 btrfs_set_trans_block_group(trans, inode);
152
153 key.objectid = inode->i_ino;
154 key.offset = start;
155 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
156 inode_add_bytes(inode, size);
157 datasize = btrfs_file_extent_calc_inline_size(cur_size);
158
159 inode_add_bytes(inode, size);
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 BUG_ON(ret);
163 if (ret) {
164 err = ret;
165 printk("got bad ret %d\n", ret);
166 goto fail;
167 }
168 leaf = path->nodes[0];
169 ei = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_file_extent_item);
171 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
172 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
173 btrfs_set_file_extent_encryption(leaf, ei, 0);
174 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
175 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
176 ptr = btrfs_file_extent_inline_start(ei);
177
178 if (use_compress) {
179 struct page *cpage;
180 int i = 0;
181 while(compressed_size > 0) {
182 cpage = compressed_pages[i];
183 cur_size = min(compressed_size,
184 PAGE_CACHE_SIZE);
185
186 kaddr = kmap(cpage);
187 write_extent_buffer(leaf, kaddr, ptr, cur_size);
188 kunmap(cpage);
189
190 i++;
191 ptr += cur_size;
192 compressed_size -= cur_size;
193 }
194 btrfs_set_file_extent_compression(leaf, ei,
195 BTRFS_COMPRESS_ZLIB);
196 } else {
197 page = find_get_page(inode->i_mapping,
198 start >> PAGE_CACHE_SHIFT);
199 btrfs_set_file_extent_compression(leaf, ei, 0);
200 kaddr = kmap_atomic(page, KM_USER0);
201 offset = start & (PAGE_CACHE_SIZE - 1);
202 write_extent_buffer(leaf, kaddr + offset, ptr, size);
203 kunmap_atomic(kaddr, KM_USER0);
204 page_cache_release(page);
205 }
206 btrfs_mark_buffer_dirty(leaf);
207 btrfs_free_path(path);
208
209 BTRFS_I(inode)->disk_i_size = inode->i_size;
210 btrfs_update_inode(trans, root, inode);
211 return 0;
212fail:
213 btrfs_free_path(path);
214 return err;
215}
216
217
218/*
219 * conditionally insert an inline extent into the file. This
220 * does the checks required to make sure the data is small enough
221 * to fit as an inline extent.
222 */
223static int cow_file_range_inline(struct btrfs_trans_handle *trans,
224 struct btrfs_root *root,
225 struct inode *inode, u64 start, u64 end,
226 size_t compressed_size,
227 struct page **compressed_pages)
228{
229 u64 isize = i_size_read(inode);
230 u64 actual_end = min(end + 1, isize);
231 u64 inline_len = actual_end - start;
232 u64 aligned_end = (end + root->sectorsize - 1) &
233 ~((u64)root->sectorsize - 1);
234 u64 hint_byte;
235 u64 data_len = inline_len;
236 int ret;
237
238 if (compressed_size)
239 data_len = compressed_size;
240
241 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400242 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400243 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
244 (!compressed_size &&
245 (actual_end & (root->sectorsize - 1)) == 0) ||
246 end + 1 < isize ||
247 data_len > root->fs_info->max_inline) {
248 return 1;
249 }
250
Chris Masonc8b97812008-10-29 14:49:59 -0400251 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Mason70b99e62008-10-31 12:46:39 -0400252 aligned_end, start, &hint_byte);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 BUG_ON(ret);
254
255 if (isize > actual_end)
256 inline_len = min_t(u64, isize, actual_end);
257 ret = insert_inline_extent(trans, root, inode, start,
258 inline_len, compressed_size,
259 compressed_pages);
260 BUG_ON(ret);
261 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400262 return 0;
263}
264
265/*
Chris Masond352ac62008-09-29 15:18:18 -0400266 * when extent_io.c finds a delayed allocation range in the file,
267 * the call backs end up in this code. The basic idea is to
268 * allocate extents on disk for the range, and create ordered data structs
269 * in ram to track those extents.
Chris Masonc8b97812008-10-29 14:49:59 -0400270 *
271 * locked_page is the page that writepage had locked already. We use
272 * it to make sure we don't do extra locks or unlocks.
273 *
274 * *page_started is set to one if we unlock locked_page and do everything
275 * required to start IO on it. It may be clean and already done with
276 * IO when we return.
Chris Masond352ac62008-09-29 15:18:18 -0400277 */
Chris Masonc8b97812008-10-29 14:49:59 -0400278static int cow_file_range(struct inode *inode, struct page *locked_page,
279 u64 start, u64 end, int *page_started)
Chris Masonb888db22007-08-27 16:49:44 -0400280{
281 struct btrfs_root *root = BTRFS_I(inode)->root;
282 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400283 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400284 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400285 unsigned long ram_size;
286 u64 orig_start;
287 u64 disk_num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500288 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400289 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400290 u64 actual_end;
Chris Masonbe20aa92007-12-17 20:14:01 -0500291 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400292 struct extent_map *em;
293 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
294 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400295 struct page **pages = NULL;
296 unsigned long nr_pages;
297 unsigned long nr_pages_ret = 0;
298 unsigned long total_compressed = 0;
299 unsigned long total_in = 0;
300 unsigned long max_compressed = 128 * 1024;
301 unsigned long max_uncompressed = 256 * 1024;
302 int i;
Yan Zheng80ff3852008-10-30 14:20:02 -0400303 int ordered_type;
Chris Masonc8b97812008-10-29 14:49:59 -0400304 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400305
Chris Masonf9295742008-07-17 12:54:14 -0400306 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400307 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500308 btrfs_set_trans_block_group(trans, inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400309 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500310
Chris Masonc8b97812008-10-29 14:49:59 -0400311 /*
312 * compression made this loop a bit ugly, but the basic idea is to
313 * compress some pages but keep the total size of the compressed
314 * extent relatively small. If compression is off, this goto target
315 * is never used.
316 */
317again:
318 will_compress = 0;
319 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
320 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
321
322 actual_end = min_t(u64, i_size_read(inode), end + 1);
323 total_compressed = actual_end - start;
324
325 /* we want to make sure that amount of ram required to uncompress
326 * an extent is reasonable, so we limit the total size in ram
327 * of a compressed extent to 256k
328 */
329 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400330 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500331 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400332 disk_num_bytes = num_bytes;
333 total_in = 0;
334 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400335
Chris Masonc8b97812008-10-29 14:49:59 -0400336 /* we do compression for mount -o compress and when the
337 * inode has not been flagged as nocompress
338 */
339 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
340 btrfs_test_opt(root, COMPRESS)) {
341 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400342 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400343
Chris Masonc8b97812008-10-29 14:49:59 -0400344 /* we want to make sure the amount of IO required to satisfy
345 * a random read is reasonably small, so we limit the size
346 * of a compressed extent to 128k
347 */
348 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
349 total_compressed, pages,
350 nr_pages, &nr_pages_ret,
351 &total_in,
352 &total_compressed,
353 max_compressed);
354
355 if (!ret) {
356 unsigned long offset = total_compressed &
357 (PAGE_CACHE_SIZE - 1);
358 struct page *page = pages[nr_pages_ret - 1];
359 char *kaddr;
360
361 /* zero the tail end of the last page, we might be
362 * sending it down to disk
363 */
364 if (offset) {
365 kaddr = kmap_atomic(page, KM_USER0);
366 memset(kaddr + offset, 0,
367 PAGE_CACHE_SIZE - offset);
368 kunmap_atomic(kaddr, KM_USER0);
369 }
370 will_compress = 1;
371 }
372 }
373 if (start == 0) {
374 /* lets try to make an inline extent */
375 if (ret || total_in < (end - start + 1)) {
376 /* we didn't compress the entire range, try
377 * to make an uncompressed inline extent. This
378 * is almost sure to fail, but maybe inline sizes
379 * will get bigger later
380 */
381 ret = cow_file_range_inline(trans, root, inode,
382 start, end, 0, NULL);
383 } else {
384 ret = cow_file_range_inline(trans, root, inode,
385 start, end,
386 total_compressed, pages);
387 }
388 if (ret == 0) {
389 extent_clear_unlock_delalloc(inode,
390 &BTRFS_I(inode)->io_tree,
391 start, end, NULL,
392 1, 1, 1);
393 *page_started = 1;
394 ret = 0;
395 goto free_pages_out;
396 }
397 }
398
399 if (will_compress) {
400 /*
401 * we aren't doing an inline extent round the compressed size
402 * up to a block size boundary so the allocator does sane
403 * things
404 */
405 total_compressed = (total_compressed + blocksize - 1) &
406 ~(blocksize - 1);
407
408 /*
409 * one last check to make sure the compression is really a
410 * win, compare the page count read with the blocks on disk
411 */
412 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
413 ~(PAGE_CACHE_SIZE - 1);
414 if (total_compressed >= total_in) {
415 will_compress = 0;
416 } else {
417 disk_num_bytes = total_compressed;
418 num_bytes = total_in;
419 }
420 }
421 if (!will_compress && pages) {
422 /*
423 * the compression code ran but failed to make things smaller,
424 * free any pages it allocated and our page pointer array
425 */
426 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400427 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400428 page_cache_release(pages[i]);
429 }
430 kfree(pages);
431 pages = NULL;
432 total_compressed = 0;
433 nr_pages_ret = 0;
434
435 /* flag the file so we don't compress in the future */
436 btrfs_set_flag(inode, NOCOMPRESS);
437 }
438
439 BUG_ON(disk_num_bytes >
440 btrfs_super_total_bytes(&root->fs_info->super_copy));
441
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400442 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400443
Chris Masonc8b97812008-10-29 14:49:59 -0400444 while(disk_num_bytes > 0) {
445 unsigned long min_bytes;
446
447 /*
448 * the max size of a compressed extent is pretty small,
449 * make the code a little less complex by forcing
450 * the allocator to find a whole compressed extent at once
451 */
452 if (will_compress)
453 min_bytes = disk_num_bytes;
454 else
455 min_bytes = root->sectorsize;
456
457 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400458 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400459 min_bytes, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400460 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500461 if (ret) {
462 WARN_ON(1);
Chris Masonc8b97812008-10-29 14:49:59 -0400463 goto free_pages_out_fail;
Chris Masonc59f8952007-12-17 20:14:04 -0500464 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400465 em = alloc_extent_map(GFP_NOFS);
466 em->start = start;
Chris Masonc8b97812008-10-29 14:49:59 -0400467
468 if (will_compress) {
469 ram_size = num_bytes;
470 em->len = num_bytes;
471 } else {
472 /* ramsize == disk size */
473 ram_size = ins.offset;
474 em->len = ins.offset;
475 }
476
Chris Masone6dcd2d2008-07-17 12:53:50 -0400477 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400478 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400479 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400480 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400481
482 if (will_compress)
483 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
484
Chris Masone6dcd2d2008-07-17 12:53:50 -0400485 while(1) {
486 spin_lock(&em_tree->lock);
487 ret = add_extent_mapping(em_tree, em);
488 spin_unlock(&em_tree->lock);
489 if (ret != -EEXIST) {
490 free_extent_map(em);
491 break;
492 }
493 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400494 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400495 }
496
Chris Mason98d20f62008-04-14 09:46:10 -0400497 cur_alloc_size = ins.offset;
Yan Zheng80ff3852008-10-30 14:20:02 -0400498 ordered_type = will_compress ? BTRFS_ORDERED_COMPRESSED : 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400499 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Yan Zheng80ff3852008-10-30 14:20:02 -0400500 ram_size, cur_alloc_size,
501 ordered_type);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400502 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400503
504 if (disk_num_bytes < cur_alloc_size) {
505 printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
Chris Mason3b951512008-04-17 11:29:12 -0400506 cur_alloc_size);
507 break;
508 }
Chris Masonc8b97812008-10-29 14:49:59 -0400509
510 if (will_compress) {
511 /*
512 * we're doing compression, we and we need to
513 * submit the compressed extents down to the device.
514 *
515 * We lock down all the file pages, clearing their
516 * dirty bits and setting them writeback. Everyone
517 * that wants to modify the page will wait on the
518 * ordered extent above.
519 *
520 * The writeback bits on the file pages are
521 * cleared when the compressed pages are on disk
522 */
523 btrfs_end_transaction(trans, root);
524
525 if (start <= page_offset(locked_page) &&
526 page_offset(locked_page) < start + ram_size) {
527 *page_started = 1;
528 }
529
530 extent_clear_unlock_delalloc(inode,
531 &BTRFS_I(inode)->io_tree,
532 start,
533 start + ram_size - 1,
534 NULL, 1, 1, 0);
535
536 ret = btrfs_submit_compressed_write(inode, start,
537 ram_size, ins.objectid,
538 cur_alloc_size, pages,
539 nr_pages_ret);
540
541 BUG_ON(ret);
542 trans = btrfs_join_transaction(root, 1);
543 if (start + ram_size < end) {
544 start += ram_size;
545 alloc_hint = ins.objectid + ins.offset;
546 /* pages will be freed at end_bio time */
547 pages = NULL;
548 goto again;
549 } else {
550 /* we've written everything, time to go */
551 break;
552 }
553 }
554 /* we're not doing compressed IO, don't unlock the first
555 * page (which the caller expects to stay locked), don't
556 * clear any dirty bits and don't set any writeback bits
557 */
558 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
559 start, start + ram_size - 1,
560 locked_page, 0, 0, 0);
561 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500562 num_bytes -= cur_alloc_size;
563 alloc_hint = ins.objectid + ins.offset;
564 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400565 }
Chris Masonc8b97812008-10-29 14:49:59 -0400566
567 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400568out:
569 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400570
Chris Masonbe20aa92007-12-17 20:14:01 -0500571 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400572
573free_pages_out_fail:
574 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
575 start, end, locked_page, 0, 0, 0);
576free_pages_out:
Chris Mason70b99e62008-10-31 12:46:39 -0400577 for (i = 0; i < nr_pages_ret; i++) {
578 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400579 page_cache_release(pages[i]);
Chris Mason70b99e62008-10-31 12:46:39 -0400580 }
Chris Masonc8b97812008-10-29 14:49:59 -0400581 if (pages)
582 kfree(pages);
583
584 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500585}
586
Chris Masond352ac62008-09-29 15:18:18 -0400587/*
588 * when nowcow writeback call back. This checks for snapshots or COW copies
589 * of the extents that exist in the file, and COWs the file as required.
590 *
591 * If no cow copies or snapshots exist, we write directly to the existing
592 * blocks on disk
593 */
Chris Masonc8b97812008-10-29 14:49:59 -0400594static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
Yan Zhengd899e052008-10-30 14:25:28 -0400595 u64 start, u64 end, int *page_started, int force)
Chris Masonbe20aa92007-12-17 20:14:01 -0500596{
Chris Masonbe20aa92007-12-17 20:14:01 -0500597 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400598 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500599 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500600 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400601 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500602 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400603 u64 cow_start;
604 u64 cur_offset;
605 u64 extent_end;
606 u64 disk_bytenr;
607 u64 num_bytes;
608 int extent_type;
609 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400610 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400611 int nocow;
612 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500613
614 path = btrfs_alloc_path();
615 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400616 trans = btrfs_join_transaction(root, 1);
617 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500618
Yan Zheng80ff3852008-10-30 14:20:02 -0400619 cow_start = (u64)-1;
620 cur_offset = start;
621 while (1) {
622 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
623 cur_offset, 0);
624 BUG_ON(ret < 0);
625 if (ret > 0 && path->slots[0] > 0 && check_prev) {
626 leaf = path->nodes[0];
627 btrfs_item_key_to_cpu(leaf, &found_key,
628 path->slots[0] - 1);
629 if (found_key.objectid == inode->i_ino &&
630 found_key.type == BTRFS_EXTENT_DATA_KEY)
631 path->slots[0]--;
632 }
633 check_prev = 0;
634next_slot:
635 leaf = path->nodes[0];
636 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
637 ret = btrfs_next_leaf(root, path);
638 if (ret < 0)
639 BUG_ON(1);
640 if (ret > 0)
641 break;
642 leaf = path->nodes[0];
643 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500644
Yan Zheng80ff3852008-10-30 14:20:02 -0400645 nocow = 0;
646 disk_bytenr = 0;
647 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500648
Yan Zheng80ff3852008-10-30 14:20:02 -0400649 if (found_key.objectid > inode->i_ino ||
650 found_key.type > BTRFS_EXTENT_DATA_KEY ||
651 found_key.offset > end)
652 break;
Chris Masonbe20aa92007-12-17 20:14:01 -0500653
Yan Zheng80ff3852008-10-30 14:20:02 -0400654 if (found_key.offset > cur_offset) {
655 extent_end = found_key.offset;
656 goto out_check;
657 }
Chris Masonc31f8832008-01-08 15:46:31 -0500658
Yan Zheng80ff3852008-10-30 14:20:02 -0400659 fi = btrfs_item_ptr(leaf, path->slots[0],
660 struct btrfs_file_extent_item);
661 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -0500662
Yan Zhengd899e052008-10-30 14:25:28 -0400663 if (extent_type == BTRFS_FILE_EXTENT_REG ||
664 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -0400665 struct btrfs_block_group_cache *block_group;
666 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
667 extent_end = found_key.offset +
668 btrfs_file_extent_num_bytes(leaf, fi);
669 if (extent_end <= start) {
670 path->slots[0]++;
671 goto next_slot;
672 }
673 if (btrfs_file_extent_compression(leaf, fi) ||
674 btrfs_file_extent_encryption(leaf, fi) ||
675 btrfs_file_extent_other_encoding(leaf, fi))
676 goto out_check;
677 if (disk_bytenr == 0)
678 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -0400679 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
680 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -0400681 if (btrfs_cross_ref_exist(trans, root, disk_bytenr))
682 goto out_check;
683 block_group = btrfs_lookup_block_group(root->fs_info,
684 disk_bytenr);
685 if (!block_group || block_group->ro)
686 goto out_check;
687 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
688 nocow = 1;
689 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
690 extent_end = found_key.offset +
691 btrfs_file_extent_inline_len(leaf, fi);
692 extent_end = ALIGN(extent_end, root->sectorsize);
693 } else {
694 BUG_ON(1);
695 }
696out_check:
697 if (extent_end <= start) {
698 path->slots[0]++;
699 goto next_slot;
700 }
701 if (!nocow) {
702 if (cow_start == (u64)-1)
703 cow_start = cur_offset;
704 cur_offset = extent_end;
705 if (cur_offset > end)
706 break;
707 path->slots[0]++;
708 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400709 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500710
Yan Zheng7ea394f2008-08-05 13:05:02 -0400711 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -0400712 if (cow_start != (u64)-1) {
713 ret = cow_file_range(inode, locked_page, cow_start,
714 found_key.offset - 1, page_started);
715 BUG_ON(ret);
716 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400717 }
Yan Zheng80ff3852008-10-30 14:20:02 -0400718
719 disk_bytenr += cur_offset - found_key.offset;
720 num_bytes = min(end + 1, extent_end) - cur_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400721 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
722 struct extent_map *em;
723 struct extent_map_tree *em_tree;
724 em_tree = &BTRFS_I(inode)->extent_tree;
725 em = alloc_extent_map(GFP_NOFS);
726 em->start = cur_offset;
727 em->len = num_bytes;
728 em->block_len = num_bytes;
729 em->block_start = disk_bytenr;
730 em->bdev = root->fs_info->fs_devices->latest_bdev;
731 set_bit(EXTENT_FLAG_PINNED, &em->flags);
732 while (1) {
733 spin_lock(&em_tree->lock);
734 ret = add_extent_mapping(em_tree, em);
735 spin_unlock(&em_tree->lock);
736 if (ret != -EEXIST) {
737 free_extent_map(em);
738 break;
739 }
740 btrfs_drop_extent_cache(inode, em->start,
741 em->start + em->len - 1, 0);
742 }
743 type = BTRFS_ORDERED_PREALLOC;
744 } else {
745 type = BTRFS_ORDERED_NOCOW;
746 }
Yan Zheng80ff3852008-10-30 14:20:02 -0400747
748 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -0400749 num_bytes, num_bytes, type);
750 BUG_ON(ret);
751 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
752 cur_offset, cur_offset + num_bytes - 1,
753 locked_page, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -0400754 cur_offset = extent_end;
755 if (cur_offset > end)
756 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400757 }
Yan Zheng80ff3852008-10-30 14:20:02 -0400758 btrfs_release_path(root, path);
759
760 if (cur_offset <= end && cow_start == (u64)-1)
761 cow_start = cur_offset;
762 if (cow_start != (u64)-1) {
763 ret = cow_file_range(inode, locked_page, cow_start, end,
764 page_started);
765 BUG_ON(ret);
766 }
767
768 ret = btrfs_end_transaction(trans, root);
769 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400770 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -0400771 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500772}
773
Chris Masond352ac62008-09-29 15:18:18 -0400774/*
775 * extent_io.c call back to do delayed allocation processing
776 */
Chris Masonc8b97812008-10-29 14:49:59 -0400777static int run_delalloc_range(struct inode *inode, struct page *locked_page,
778 u64 start, u64 end, int *page_started)
Chris Masonbe20aa92007-12-17 20:14:01 -0500779{
780 struct btrfs_root *root = BTRFS_I(inode)->root;
781 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400782
Yanb98b6762008-01-08 15:54:37 -0500783 if (btrfs_test_opt(root, NODATACOW) ||
784 btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -0400785 ret = run_delalloc_nocow(inode, locked_page, start, end,
Yan Zhengd899e052008-10-30 14:25:28 -0400786 page_started, 0);
787 else if (btrfs_test_flag(inode, PREALLOC))
788 ret = run_delalloc_nocow(inode, locked_page, start, end,
789 page_started, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500790 else
Chris Masonc8b97812008-10-29 14:49:59 -0400791 ret = cow_file_range(inode, locked_page, start, end,
792 page_started);
Chris Mason1832a6d2007-12-21 16:27:21 -0500793
Chris Masonb888db22007-08-27 16:49:44 -0400794 return ret;
795}
796
Chris Masond352ac62008-09-29 15:18:18 -0400797/*
798 * extent_io.c set_bit_hook, used to track delayed allocation
799 * bytes in this file, and to maintain the list of inodes that
800 * have pending delalloc work to be done.
801 */
Chris Mason291d6732008-01-29 15:55:23 -0500802int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500803 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500804{
Chris Masonbcbfce82008-04-22 13:26:47 -0400805 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500806 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500807 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400808 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500809 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500810 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400811 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
812 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
813 &root->fs_info->delalloc_inodes);
814 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400815 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500816 }
817 return 0;
818}
819
Chris Masond352ac62008-09-29 15:18:18 -0400820/*
821 * extent_io.c clear_bit_hook, see set_bit_hook for why
822 */
Chris Mason291d6732008-01-29 15:55:23 -0500823int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500824 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500825{
Chris Masonb0c68f82008-01-31 11:05:37 -0500826 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500827 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400828 unsigned long flags;
829
830 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500831 if (end - start + 1 > root->fs_info->delalloc_bytes) {
832 printk("warning: delalloc account %Lu %Lu\n",
833 end - start + 1, root->fs_info->delalloc_bytes);
834 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500835 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500836 } else {
837 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500838 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500839 }
Chris Masonea8c2812008-08-04 23:17:27 -0400840 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
841 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
842 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
843 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400844 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500845 }
846 return 0;
847}
848
Chris Masond352ac62008-09-29 15:18:18 -0400849/*
850 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
851 * we don't create bios that span stripes or chunks
852 */
Chris Mason239b14b2008-03-24 15:02:07 -0400853int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -0400854 size_t size, struct bio *bio,
855 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -0400856{
857 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
858 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -0400859 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400860 u64 length = 0;
861 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400862 int ret;
863
Chris Masonf2d8d742008-04-21 10:03:05 -0400864 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400865 map_tree = &root->fs_info->mapping_tree;
866 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400867 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400868 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400869
Chris Mason239b14b2008-03-24 15:02:07 -0400870 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400871 return 1;
872 }
873 return 0;
874}
875
Chris Masond352ac62008-09-29 15:18:18 -0400876/*
877 * in order to insert checksums into the metadata in large chunks,
878 * we wait until bio submission time. All the pages in the bio are
879 * checksummed and sums are attached onto the ordered extent record.
880 *
881 * At IO completion time the cums attached on the ordered extent record
882 * are inserted into the btree
883 */
Chris Mason4a69a412008-11-06 22:03:00 -0500884int __btrfs_submit_bio_start(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -0400885 int mirror_num, unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -0500886{
Chris Mason065631f2008-02-20 12:07:25 -0500887 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500888 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400889
Chris Mason3edf7d32008-07-18 06:17:13 -0400890 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400891 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -0500892 return 0;
893}
Chris Masone0156402008-04-16 11:15:20 -0400894
Chris Mason4a69a412008-11-06 22:03:00 -0500895/*
896 * in order to insert checksums into the metadata in large chunks,
897 * we wait until bio submission time. All the pages in the bio are
898 * checksummed and sums are attached onto the ordered extent record.
899 *
900 * At IO completion time the cums attached on the ordered extent record
901 * are inserted into the btree
902 */
903int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
904 int mirror_num, unsigned long bio_flags)
905{
906 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -0400907 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400908}
909
Chris Masond352ac62008-09-29 15:18:18 -0400910/*
911 * extent_io.c submission hook. This does the right thing for csum calculation on write,
912 * or reading the csums from the tree before a read
913 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400914int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -0400915 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -0400916{
917 struct btrfs_root *root = BTRFS_I(inode)->root;
918 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -0400919 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -0400920
Chris Masone6dcd2d2008-07-17 12:53:50 -0400921 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
922 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500923
Chris Mason19b9bdb2008-10-30 14:23:13 -0400924 skip_sum = btrfs_test_opt(root, NODATASUM) ||
925 btrfs_test_flag(inode, NODATASUM);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400926
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400927 if (!(rw & (1 << BIO_RW))) {
Chris Mason19b9bdb2008-10-30 14:23:13 -0400928 if (!skip_sum)
929 btrfs_lookup_bio_sums(root, inode, bio);
Chris Masonc8b97812008-10-29 14:49:59 -0400930
Chris Mason19b9bdb2008-10-30 14:23:13 -0400931 if (bio_flags & EXTENT_BIO_COMPRESSED)
Chris Masonc8b97812008-10-29 14:49:59 -0400932 return btrfs_submit_compressed_read(inode, bio,
933 mirror_num, bio_flags);
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400934 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -0400935 } else if (!skip_sum) {
936 /* we're doing a write, do the async checksumming */
937 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -0400938 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -0500939 bio_flags, __btrfs_submit_bio_start,
940 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -0400941 }
942
Chris Mason0b86a832008-03-24 15:01:56 -0400943mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400944 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500945}
Chris Mason6885f302008-02-20 16:11:05 -0500946
Chris Masond352ac62008-09-29 15:18:18 -0400947/*
948 * given a list of ordered sums record them in the inode. This happens
949 * at IO completion time based on sums calculated at bio submission time.
950 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400951static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 struct inode *inode, u64 file_offset,
953 struct list_head *list)
954{
955 struct list_head *cur;
956 struct btrfs_ordered_sum *sum;
957
958 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400959 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400960 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400961 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
962 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400963 }
964 return 0;
965}
966
Chris Masonea8c2812008-08-04 23:17:27 -0400967int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
968{
969 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
970 GFP_NOFS);
971}
972
Chris Masond352ac62008-09-29 15:18:18 -0400973/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -0400974struct btrfs_writepage_fixup {
975 struct page *page;
976 struct btrfs_work work;
977};
978
Chris Mason247e7432008-07-17 12:53:51 -0400979void btrfs_writepage_fixup_worker(struct btrfs_work *work)
980{
981 struct btrfs_writepage_fixup *fixup;
982 struct btrfs_ordered_extent *ordered;
983 struct page *page;
984 struct inode *inode;
985 u64 page_start;
986 u64 page_end;
987
988 fixup = container_of(work, struct btrfs_writepage_fixup, work);
989 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400990again:
Chris Mason247e7432008-07-17 12:53:51 -0400991 lock_page(page);
992 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
993 ClearPageChecked(page);
994 goto out_page;
995 }
996
997 inode = page->mapping->host;
998 page_start = page_offset(page);
999 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1000
1001 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001002
1003 /* already ordered? We're done */
1004 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1005 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -04001006 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001007 }
1008
1009 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1010 if (ordered) {
1011 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1012 page_end, GFP_NOFS);
1013 unlock_page(page);
1014 btrfs_start_ordered_extent(inode, ordered, 1);
1015 goto again;
1016 }
Chris Mason247e7432008-07-17 12:53:51 -04001017
Chris Masonea8c2812008-08-04 23:17:27 -04001018 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001019 ClearPageChecked(page);
1020out:
1021 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1022out_page:
1023 unlock_page(page);
1024 page_cache_release(page);
1025}
1026
1027/*
1028 * There are a few paths in the higher layers of the kernel that directly
1029 * set the page dirty bit without asking the filesystem if it is a
1030 * good idea. This causes problems because we want to make sure COW
1031 * properly happens and the data=ordered rules are followed.
1032 *
Chris Masonc8b97812008-10-29 14:49:59 -04001033 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001034 * hasn't been properly setup for IO. We kick off an async process
1035 * to fix it up. The async helper will wait for ordered extents, set
1036 * the delalloc bit and make it safe to write the page.
1037 */
1038int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1039{
1040 struct inode *inode = page->mapping->host;
1041 struct btrfs_writepage_fixup *fixup;
1042 struct btrfs_root *root = BTRFS_I(inode)->root;
1043 int ret;
1044
1045 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1046 EXTENT_ORDERED, 0);
1047 if (ret)
1048 return 0;
1049
1050 if (PageChecked(page))
1051 return -EAGAIN;
1052
1053 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1054 if (!fixup)
1055 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001056
Chris Mason247e7432008-07-17 12:53:51 -04001057 SetPageChecked(page);
1058 page_cache_get(page);
1059 fixup->work.func = btrfs_writepage_fixup_worker;
1060 fixup->page = page;
1061 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1062 return -EAGAIN;
1063}
1064
Yan Zhengd899e052008-10-30 14:25:28 -04001065static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1066 struct inode *inode, u64 file_pos,
1067 u64 disk_bytenr, u64 disk_num_bytes,
1068 u64 num_bytes, u64 ram_bytes,
1069 u8 compression, u8 encryption,
1070 u16 other_encoding, int extent_type)
1071{
1072 struct btrfs_root *root = BTRFS_I(inode)->root;
1073 struct btrfs_file_extent_item *fi;
1074 struct btrfs_path *path;
1075 struct extent_buffer *leaf;
1076 struct btrfs_key ins;
1077 u64 hint;
1078 int ret;
1079
1080 path = btrfs_alloc_path();
1081 BUG_ON(!path);
1082
1083 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1084 file_pos + num_bytes, file_pos, &hint);
1085 BUG_ON(ret);
1086
1087 ins.objectid = inode->i_ino;
1088 ins.offset = file_pos;
1089 ins.type = BTRFS_EXTENT_DATA_KEY;
1090 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1091 BUG_ON(ret);
1092 leaf = path->nodes[0];
1093 fi = btrfs_item_ptr(leaf, path->slots[0],
1094 struct btrfs_file_extent_item);
1095 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1096 btrfs_set_file_extent_type(leaf, fi, extent_type);
1097 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1098 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1099 btrfs_set_file_extent_offset(leaf, fi, 0);
1100 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1101 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1102 btrfs_set_file_extent_compression(leaf, fi, compression);
1103 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1104 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1105 btrfs_mark_buffer_dirty(leaf);
1106
1107 inode_add_bytes(inode, num_bytes);
1108 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1109
1110 ins.objectid = disk_bytenr;
1111 ins.offset = disk_num_bytes;
1112 ins.type = BTRFS_EXTENT_ITEM_KEY;
1113 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1114 root->root_key.objectid,
1115 trans->transid, inode->i_ino, &ins);
1116 BUG_ON(ret);
1117
1118 btrfs_free_path(path);
1119 return 0;
1120}
1121
Chris Masond352ac62008-09-29 15:18:18 -04001122/* as ordered data IO finishes, this gets called so we can finish
1123 * an ordered extent if the range of bytes in the file it covers are
1124 * fully written.
1125 */
Chris Mason211f90e2008-07-18 11:56:15 -04001126static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001127{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001128 struct btrfs_root *root = BTRFS_I(inode)->root;
1129 struct btrfs_trans_handle *trans;
1130 struct btrfs_ordered_extent *ordered_extent;
1131 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001132 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001133 int ret;
1134
1135 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001136 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001137 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001138
Chris Masonf9295742008-07-17 12:54:14 -04001139 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001140
1141 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1142 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001143 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1144 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001145
1146 lock_extent(io_tree, ordered_extent->file_offset,
1147 ordered_extent->file_offset + ordered_extent->len - 1,
1148 GFP_NOFS);
1149
Chris Masonc8b97812008-10-29 14:49:59 -04001150 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001151 compressed = 1;
1152 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1153 BUG_ON(compressed);
1154 ret = btrfs_mark_extent_written(trans, root, inode,
1155 ordered_extent->file_offset,
1156 ordered_extent->file_offset +
1157 ordered_extent->len);
1158 BUG_ON(ret);
1159 } else {
1160 ret = insert_reserved_file_extent(trans, inode,
1161 ordered_extent->file_offset,
1162 ordered_extent->start,
1163 ordered_extent->disk_len,
1164 ordered_extent->len,
1165 ordered_extent->len,
1166 compressed, 0, 0,
1167 BTRFS_FILE_EXTENT_REG);
1168 BUG_ON(ret);
1169 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001170 unlock_extent(io_tree, ordered_extent->file_offset,
1171 ordered_extent->file_offset + ordered_extent->len - 1,
1172 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001173nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001174 add_pending_csums(trans, inode, ordered_extent->file_offset,
1175 &ordered_extent->list);
1176
Chris Mason34353022008-09-23 20:19:49 -04001177 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001178 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001179 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001180 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001181 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001182
Chris Masone6dcd2d2008-07-17 12:53:50 -04001183 /* once for us */
1184 btrfs_put_ordered_extent(ordered_extent);
1185 /* once for the tree */
1186 btrfs_put_ordered_extent(ordered_extent);
1187
Chris Masone6dcd2d2008-07-17 12:53:50 -04001188 btrfs_end_transaction(trans, root);
1189 return 0;
1190}
1191
Chris Mason211f90e2008-07-18 11:56:15 -04001192int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1193 struct extent_state *state, int uptodate)
1194{
1195 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1196}
1197
Chris Masond352ac62008-09-29 15:18:18 -04001198/*
1199 * When IO fails, either with EIO or csum verification fails, we
1200 * try other mirrors that might have a good copy of the data. This
1201 * io_failure_record is used to record state as we go through all the
1202 * mirrors. If another mirror has good data, the page is set up to date
1203 * and things continue. If a good mirror can't be found, the original
1204 * bio end_io callback is called to indicate things have failed.
1205 */
Chris Mason7e383262008-04-09 16:28:12 -04001206struct io_failure_record {
1207 struct page *page;
1208 u64 start;
1209 u64 len;
1210 u64 logical;
1211 int last_mirror;
1212};
1213
Chris Mason1259ab72008-05-12 13:39:03 -04001214int btrfs_io_failed_hook(struct bio *failed_bio,
1215 struct page *page, u64 start, u64 end,
1216 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001217{
1218 struct io_failure_record *failrec = NULL;
1219 u64 private;
1220 struct extent_map *em;
1221 struct inode *inode = page->mapping->host;
1222 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001223 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001224 struct bio *bio;
1225 int num_copies;
1226 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001227 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001228 u64 logical;
Chris Masonc8b97812008-10-29 14:49:59 -04001229 unsigned long bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001230
1231 ret = get_state_private(failure_tree, start, &private);
1232 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001233 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1234 if (!failrec)
1235 return -ENOMEM;
1236 failrec->start = start;
1237 failrec->len = end - start + 1;
1238 failrec->last_mirror = 0;
1239
Chris Mason3b951512008-04-17 11:29:12 -04001240 spin_lock(&em_tree->lock);
1241 em = lookup_extent_mapping(em_tree, start, failrec->len);
1242 if (em->start > start || em->start + em->len < start) {
1243 free_extent_map(em);
1244 em = NULL;
1245 }
1246 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001247
1248 if (!em || IS_ERR(em)) {
1249 kfree(failrec);
1250 return -EIO;
1251 }
1252 logical = start - em->start;
1253 logical = em->block_start + logical;
Chris Masonc8b97812008-10-29 14:49:59 -04001254 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1255 bio_flags = EXTENT_BIO_COMPRESSED;
Chris Mason7e383262008-04-09 16:28:12 -04001256 failrec->logical = logical;
1257 free_extent_map(em);
1258 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1259 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001260 set_state_private(failure_tree, start,
1261 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001262 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001263 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001264 }
1265 num_copies = btrfs_num_copies(
1266 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1267 failrec->logical, failrec->len);
1268 failrec->last_mirror++;
1269 if (!state) {
1270 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
1271 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1272 failrec->start,
1273 EXTENT_LOCKED);
1274 if (state && state->start != failrec->start)
1275 state = NULL;
1276 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
1277 }
1278 if (!state || failrec->last_mirror > num_copies) {
1279 set_state_private(failure_tree, failrec->start, 0);
1280 clear_extent_bits(failure_tree, failrec->start,
1281 failrec->start + failrec->len - 1,
1282 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1283 kfree(failrec);
1284 return -EIO;
1285 }
1286 bio = bio_alloc(GFP_NOFS, 1);
1287 bio->bi_private = state;
1288 bio->bi_end_io = failed_bio->bi_end_io;
1289 bio->bi_sector = failrec->logical >> 9;
1290 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001291 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001292 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001293 if (failed_bio->bi_rw & (1 << BIO_RW))
1294 rw = WRITE;
1295 else
1296 rw = READ;
1297
1298 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001299 failrec->last_mirror,
1300 bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001301 return 0;
1302}
1303
Chris Masond352ac62008-09-29 15:18:18 -04001304/*
1305 * each time an IO finishes, we do a fast check in the IO failure tree
1306 * to see if we need to process or clean up an io_failure_record
1307 */
Chris Mason1259ab72008-05-12 13:39:03 -04001308int btrfs_clean_io_failures(struct inode *inode, u64 start)
1309{
1310 u64 private;
1311 u64 private_failure;
1312 struct io_failure_record *failure;
1313 int ret;
1314
1315 private = 0;
1316 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1317 (u64)-1, 1, EXTENT_DIRTY)) {
1318 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1319 start, &private_failure);
1320 if (ret == 0) {
1321 failure = (struct io_failure_record *)(unsigned long)
1322 private_failure;
1323 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1324 failure->start, 0);
1325 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1326 failure->start,
1327 failure->start + failure->len - 1,
1328 EXTENT_DIRTY | EXTENT_LOCKED,
1329 GFP_NOFS);
1330 kfree(failure);
1331 }
1332 }
Chris Mason7e383262008-04-09 16:28:12 -04001333 return 0;
1334}
1335
Chris Masond352ac62008-09-29 15:18:18 -04001336/*
1337 * when reads are done, we need to check csums to verify the data is correct
1338 * if there's a match, we allow the bio to finish. If not, we go through
1339 * the io_failure_record routines to find good copies
1340 */
Chris Mason70dec802008-01-29 09:59:12 -05001341int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1342 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001343{
Chris Mason35ebb932007-10-30 16:56:53 -04001344 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001345 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001346 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001347 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001348 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001349 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001350 struct btrfs_root *root = BTRFS_I(inode)->root;
1351 u32 csum = ~(u32)0;
Jens Axboebbf0d0062007-10-19 09:23:07 -04001352 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001353
Yanb98b6762008-01-08 15:54:37 -05001354 if (btrfs_test_opt(root, NODATASUM) ||
1355 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001356 return 0;
Yanc2e639f2008-02-04 08:57:25 -05001357 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001358 private = state->private;
1359 ret = 0;
1360 } else {
1361 ret = get_state_private(io_tree, start, &private);
1362 }
Jens Axboebbf0d0062007-10-19 09:23:07 -04001363 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -04001364 kaddr = kmap_atomic(page, KM_IRQ0);
1365 if (ret) {
1366 goto zeroit;
1367 }
Chris Masonff79f812007-10-15 16:22:25 -04001368 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1369 btrfs_csum_final(csum, (char *)&csum);
1370 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -04001371 goto zeroit;
1372 }
1373 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -04001374 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -04001375
1376 /* if the io failure tree for this inode is non-empty,
1377 * check to see if we've recovered from a failed IO
1378 */
Chris Mason1259ab72008-05-12 13:39:03 -04001379 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001380 return 0;
1381
1382zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -05001383 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
1384 page->mapping->host->i_ino, (unsigned long long)start, csum,
1385 private);
Chris Masondb945352007-10-15 16:15:53 -04001386 memset(kaddr + offset, 1, end - start + 1);
1387 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001388 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -04001389 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -04001390 if (private == 0)
1391 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001392 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001393}
Chris Masonb888db22007-08-27 16:49:44 -04001394
Josef Bacik7b128762008-07-24 12:17:14 -04001395/*
1396 * This creates an orphan entry for the given inode in case something goes
1397 * wrong in the middle of an unlink/truncate.
1398 */
1399int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1400{
1401 struct btrfs_root *root = BTRFS_I(inode)->root;
1402 int ret = 0;
1403
Yanbcc63ab2008-07-30 16:29:20 -04001404 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001405
1406 /* already on the orphan list, we're good */
1407 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001408 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001409 return 0;
1410 }
1411
1412 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1413
Yanbcc63ab2008-07-30 16:29:20 -04001414 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001415
1416 /*
1417 * insert an orphan item to track this unlinked/truncated file
1418 */
1419 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1420
1421 return ret;
1422}
1423
1424/*
1425 * We have done the truncate/delete so we can go ahead and remove the orphan
1426 * item for this particular inode.
1427 */
1428int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1429{
1430 struct btrfs_root *root = BTRFS_I(inode)->root;
1431 int ret = 0;
1432
Yanbcc63ab2008-07-30 16:29:20 -04001433 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001434
1435 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001436 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001437 return 0;
1438 }
1439
1440 list_del_init(&BTRFS_I(inode)->i_orphan);
1441 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001442 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001443 return 0;
1444 }
1445
Yanbcc63ab2008-07-30 16:29:20 -04001446 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001447
1448 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1449
1450 return ret;
1451}
1452
1453/*
1454 * this cleans up any orphans that may be left on the list from the last use
1455 * of this root.
1456 */
1457void btrfs_orphan_cleanup(struct btrfs_root *root)
1458{
1459 struct btrfs_path *path;
1460 struct extent_buffer *leaf;
1461 struct btrfs_item *item;
1462 struct btrfs_key key, found_key;
1463 struct btrfs_trans_handle *trans;
1464 struct inode *inode;
1465 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1466
1467 /* don't do orphan cleanup if the fs is readonly. */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001468 if (root->fs_info->sb->s_flags & MS_RDONLY)
Josef Bacik7b128762008-07-24 12:17:14 -04001469 return;
1470
1471 path = btrfs_alloc_path();
1472 if (!path)
1473 return;
1474 path->reada = -1;
1475
1476 key.objectid = BTRFS_ORPHAN_OBJECTID;
1477 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1478 key.offset = (u64)-1;
1479
Josef Bacik7b128762008-07-24 12:17:14 -04001480
1481 while (1) {
1482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1483 if (ret < 0) {
1484 printk(KERN_ERR "Error searching slot for orphan: %d"
1485 "\n", ret);
1486 break;
1487 }
1488
1489 /*
1490 * if ret == 0 means we found what we were searching for, which
1491 * is weird, but possible, so only screw with path if we didnt
1492 * find the key and see if we have stuff that matches
1493 */
1494 if (ret > 0) {
1495 if (path->slots[0] == 0)
1496 break;
1497 path->slots[0]--;
1498 }
1499
1500 /* pull out the item */
1501 leaf = path->nodes[0];
1502 item = btrfs_item_nr(leaf, path->slots[0]);
1503 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1504
1505 /* make sure the item matches what we want */
1506 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1507 break;
1508 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1509 break;
1510
1511 /* release the path since we're done with it */
1512 btrfs_release_path(root, path);
1513
1514 /*
1515 * this is where we are basically btrfs_lookup, without the
1516 * crossing root thing. we store the inode number in the
1517 * offset of the orphan item.
1518 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001519 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001520 found_key.offset, root);
1521 if (!inode)
1522 break;
1523
1524 if (inode->i_state & I_NEW) {
1525 BTRFS_I(inode)->root = root;
1526
1527 /* have to set the location manually */
1528 BTRFS_I(inode)->location.objectid = inode->i_ino;
1529 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1530 BTRFS_I(inode)->location.offset = 0;
1531
1532 btrfs_read_locked_inode(inode);
1533 unlock_new_inode(inode);
1534 }
1535
1536 /*
1537 * add this inode to the orphan list so btrfs_orphan_del does
1538 * the proper thing when we hit it
1539 */
Yanbcc63ab2008-07-30 16:29:20 -04001540 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001541 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001542 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001543
1544 /*
1545 * if this is a bad inode, means we actually succeeded in
1546 * removing the inode, but not the orphan record, which means
1547 * we need to manually delete the orphan since iput will just
1548 * do a destroy_inode
1549 */
1550 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001551 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001552 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001553 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001554 iput(inode);
1555 continue;
1556 }
1557
1558 /* if we have links, this was a truncate, lets do that */
1559 if (inode->i_nlink) {
1560 nr_truncate++;
1561 btrfs_truncate(inode);
1562 } else {
1563 nr_unlink++;
1564 }
1565
1566 /* this will do delete_inode and everything for us */
1567 iput(inode);
1568 }
1569
1570 if (nr_unlink)
1571 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1572 if (nr_truncate)
1573 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1574
1575 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001576}
1577
Chris Masond352ac62008-09-29 15:18:18 -04001578/*
1579 * read an inode from the btree into the in-memory inode
1580 */
Chris Mason39279cc2007-06-12 06:35:45 -04001581void btrfs_read_locked_inode(struct inode *inode)
1582{
1583 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001584 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001585 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001586 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001587 struct btrfs_root *root = BTRFS_I(inode)->root;
1588 struct btrfs_key location;
1589 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001590 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001591 int ret;
1592
1593 path = btrfs_alloc_path();
1594 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001595 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001596
Chris Mason39279cc2007-06-12 06:35:45 -04001597 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001598 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001599 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001600
Chris Mason5f39d392007-10-15 16:14:19 -04001601 leaf = path->nodes[0];
1602 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1603 struct btrfs_inode_item);
1604
1605 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1606 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1607 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1608 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001609 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001610
1611 tspec = btrfs_inode_atime(inode_item);
1612 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1613 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1614
1615 tspec = btrfs_inode_mtime(inode_item);
1616 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1617 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1618
1619 tspec = btrfs_inode_ctime(inode_item);
1620 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1621 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1622
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001623 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04001624 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1625 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04001626 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001627 rdev = btrfs_inode_rdev(leaf, inode_item);
1628
Josef Bacikaec74772008-07-24 12:12:38 -04001629 BTRFS_I(inode)->index_cnt = (u64)-1;
1630
Chris Mason5f39d392007-10-15 16:14:19 -04001631 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001632 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1633 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001634 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001635 if (!BTRFS_I(inode)->block_group) {
1636 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001637 NULL, 0,
1638 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001639 }
Chris Mason39279cc2007-06-12 06:35:45 -04001640 btrfs_free_path(path);
1641 inode_item = NULL;
1642
Chris Mason39279cc2007-06-12 06:35:45 -04001643 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001644 case S_IFREG:
1645 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001646 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001647 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001648 inode->i_fop = &btrfs_file_operations;
1649 inode->i_op = &btrfs_file_inode_operations;
1650 break;
1651 case S_IFDIR:
1652 inode->i_fop = &btrfs_dir_file_operations;
1653 if (root == root->fs_info->tree_root)
1654 inode->i_op = &btrfs_dir_ro_inode_operations;
1655 else
1656 inode->i_op = &btrfs_dir_inode_operations;
1657 break;
1658 case S_IFLNK:
1659 inode->i_op = &btrfs_symlink_inode_operations;
1660 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001661 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001662 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001663 default:
1664 init_special_inode(inode, inode->i_mode, rdev);
1665 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001666 }
1667 return;
1668
1669make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001670 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001671 make_bad_inode(inode);
1672}
1673
Chris Masond352ac62008-09-29 15:18:18 -04001674/*
1675 * given a leaf and an inode, copy the inode fields into the leaf
1676 */
Chris Masone02119d2008-09-05 16:13:11 -04001677static void fill_inode_item(struct btrfs_trans_handle *trans,
1678 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04001679 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001680 struct inode *inode)
1681{
Chris Mason5f39d392007-10-15 16:14:19 -04001682 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1683 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001684 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001685 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1686 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1687
1688 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1689 inode->i_atime.tv_sec);
1690 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1691 inode->i_atime.tv_nsec);
1692
1693 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1694 inode->i_mtime.tv_sec);
1695 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1696 inode->i_mtime.tv_nsec);
1697
1698 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1699 inode->i_ctime.tv_sec);
1700 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1701 inode->i_ctime.tv_nsec);
1702
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001703 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04001704 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1705 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001706 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001707 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001708 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001709 BTRFS_I(inode)->block_group->key.objectid);
1710}
1711
Chris Masond352ac62008-09-29 15:18:18 -04001712/*
1713 * copy everything in the in-memory inode into the btree.
1714 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001715int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001716 struct btrfs_root *root,
1717 struct inode *inode)
1718{
1719 struct btrfs_inode_item *inode_item;
1720 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001721 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001722 int ret;
1723
1724 path = btrfs_alloc_path();
1725 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001726 ret = btrfs_lookup_inode(trans, root, path,
1727 &BTRFS_I(inode)->location, 1);
1728 if (ret) {
1729 if (ret > 0)
1730 ret = -ENOENT;
1731 goto failed;
1732 }
1733
Chris Mason5f39d392007-10-15 16:14:19 -04001734 leaf = path->nodes[0];
1735 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001736 struct btrfs_inode_item);
1737
Chris Masone02119d2008-09-05 16:13:11 -04001738 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001739 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001740 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001741 ret = 0;
1742failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001743 btrfs_free_path(path);
1744 return ret;
1745}
1746
1747
Chris Masond352ac62008-09-29 15:18:18 -04001748/*
1749 * unlink helper that gets used here in inode.c and in the tree logging
1750 * recovery code. It remove a link in a directory with a given name, and
1751 * also drops the back refs in the inode to the directory
1752 */
Chris Masone02119d2008-09-05 16:13:11 -04001753int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1754 struct btrfs_root *root,
1755 struct inode *dir, struct inode *inode,
1756 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04001757{
1758 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001759 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001760 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001761 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001762 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001763 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001764
1765 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001766 if (!path) {
1767 ret = -ENOMEM;
1768 goto err;
1769 }
1770
Chris Mason39279cc2007-06-12 06:35:45 -04001771 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1772 name, name_len, -1);
1773 if (IS_ERR(di)) {
1774 ret = PTR_ERR(di);
1775 goto err;
1776 }
1777 if (!di) {
1778 ret = -ENOENT;
1779 goto err;
1780 }
Chris Mason5f39d392007-10-15 16:14:19 -04001781 leaf = path->nodes[0];
1782 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001783 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001784 if (ret)
1785 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001786 btrfs_release_path(root, path);
1787
Josef Bacikaec74772008-07-24 12:12:38 -04001788 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001789 inode->i_ino,
1790 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04001791 if (ret) {
1792 printk("failed to delete reference to %.*s, "
1793 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04001794 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04001795 goto err;
1796 }
1797
Chris Mason39279cc2007-06-12 06:35:45 -04001798 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001799 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001800 if (IS_ERR(di)) {
1801 ret = PTR_ERR(di);
1802 goto err;
1803 }
1804 if (!di) {
1805 ret = -ENOENT;
1806 goto err;
1807 }
1808 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001809 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001810
Chris Masone02119d2008-09-05 16:13:11 -04001811 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1812 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04001813 BUG_ON(ret != 0 && ret != -ENOENT);
1814 if (ret != -ENOENT)
1815 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04001816
1817 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1818 dir, index);
1819 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001820err:
1821 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001822 if (ret)
1823 goto out;
1824
1825 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1826 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1827 btrfs_update_inode(trans, root, dir);
1828 btrfs_drop_nlink(inode);
1829 ret = btrfs_update_inode(trans, root, inode);
1830 dir->i_sb->s_dirt = 1;
1831out:
Chris Mason39279cc2007-06-12 06:35:45 -04001832 return ret;
1833}
1834
1835static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1836{
1837 struct btrfs_root *root;
1838 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001839 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001840 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001841 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001842
1843 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001844
1845 ret = btrfs_check_free_space(root, 1, 1);
1846 if (ret)
1847 goto fail;
1848
Chris Mason39279cc2007-06-12 06:35:45 -04001849 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001850
Chris Mason39279cc2007-06-12 06:35:45 -04001851 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04001852 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1853 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04001854
1855 if (inode->i_nlink == 0)
1856 ret = btrfs_orphan_add(trans, inode);
1857
Chris Masond3c2fdc2007-09-17 10:58:06 -04001858 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001859
Chris Mason89ce8a62008-06-25 16:01:31 -04001860 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001861fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001862 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001863 return ret;
1864}
1865
1866static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1867{
1868 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001869 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001870 int ret;
1871 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001872 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001873 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001874
Chris Mason925baed2008-06-25 16:01:30 -04001875 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001876 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001877 }
Yan134d4512007-10-25 15:49:25 -04001878
Chris Mason1832a6d2007-12-21 16:27:21 -05001879 ret = btrfs_check_free_space(root, 1, 1);
1880 if (ret)
1881 goto fail;
1882
Chris Mason39279cc2007-06-12 06:35:45 -04001883 trans = btrfs_start_transaction(root, 1);
1884 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001885
Josef Bacik7b128762008-07-24 12:17:14 -04001886 err = btrfs_orphan_add(trans, inode);
1887 if (err)
1888 goto fail_trans;
1889
Chris Mason39279cc2007-06-12 06:35:45 -04001890 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04001891 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1892 dentry->d_name.name, dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001893 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001894 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001895 }
Chris Mason39544012007-12-12 14:38:19 -05001896
Josef Bacik7b128762008-07-24 12:17:14 -04001897fail_trans:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001898 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001899 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001900fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001901 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001902
Chris Mason39279cc2007-06-12 06:35:45 -04001903 if (ret && !err)
1904 err = ret;
1905 return err;
1906}
1907
Chris Mason39279cc2007-06-12 06:35:45 -04001908/*
Chris Mason323ac952008-10-01 19:05:46 -04001909 * when truncating bytes in a file, it is possible to avoid reading
1910 * the leaves that contain only checksum items. This can be the
1911 * majority of the IO required to delete a large file, but it must
1912 * be done carefully.
1913 *
1914 * The keys in the level just above the leaves are checked to make sure
1915 * the lowest key in a given leaf is a csum key, and starts at an offset
1916 * after the new size.
1917 *
1918 * Then the key for the next leaf is checked to make sure it also has
1919 * a checksum item for the same file. If it does, we know our target leaf
1920 * contains only checksum items, and it can be safely freed without reading
1921 * it.
1922 *
1923 * This is just an optimization targeted at large files. It may do
1924 * nothing. It will return 0 unless things went badly.
1925 */
1926static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1927 struct btrfs_root *root,
1928 struct btrfs_path *path,
1929 struct inode *inode, u64 new_size)
1930{
1931 struct btrfs_key key;
1932 int ret;
1933 int nritems;
1934 struct btrfs_key found_key;
1935 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001936 struct btrfs_leaf_ref *ref;
1937 u64 leaf_gen;
1938 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04001939
1940 path->lowest_level = 1;
1941 key.objectid = inode->i_ino;
1942 key.type = BTRFS_CSUM_ITEM_KEY;
1943 key.offset = new_size;
1944again:
1945 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1946 if (ret < 0)
1947 goto out;
1948
1949 if (path->nodes[1] == NULL) {
1950 ret = 0;
1951 goto out;
1952 }
1953 ret = 0;
1954 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1955 nritems = btrfs_header_nritems(path->nodes[1]);
1956
1957 if (!nritems)
1958 goto out;
1959
1960 if (path->slots[1] >= nritems)
1961 goto next_node;
1962
1963 /* did we find a key greater than anything we want to delete? */
1964 if (found_key.objectid > inode->i_ino ||
1965 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1966 goto out;
1967
1968 /* we check the next key in the node to make sure the leave contains
1969 * only checksum items. This comparison doesn't work if our
1970 * leaf is the last one in the node
1971 */
1972 if (path->slots[1] + 1 >= nritems) {
1973next_node:
1974 /* search forward from the last key in the node, this
1975 * will bring us into the next node in the tree
1976 */
1977 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1978
1979 /* unlikely, but we inc below, so check to be safe */
1980 if (found_key.offset == (u64)-1)
1981 goto out;
1982
1983 /* search_forward needs a path with locks held, do the
1984 * search again for the original key. It is possible
1985 * this will race with a balance and return a path that
1986 * we could modify, but this drop is just an optimization
1987 * and is allowed to miss some leaves.
1988 */
1989 btrfs_release_path(root, path);
1990 found_key.offset++;
1991
1992 /* setup a max key for search_forward */
1993 other_key.offset = (u64)-1;
1994 other_key.type = key.type;
1995 other_key.objectid = key.objectid;
1996
1997 path->keep_locks = 1;
1998 ret = btrfs_search_forward(root, &found_key, &other_key,
1999 path, 0, 0);
2000 path->keep_locks = 0;
2001 if (ret || found_key.objectid != key.objectid ||
2002 found_key.type != key.type) {
2003 ret = 0;
2004 goto out;
2005 }
2006
2007 key.offset = found_key.offset;
2008 btrfs_release_path(root, path);
2009 cond_resched();
2010 goto again;
2011 }
2012
2013 /* we know there's one more slot after us in the tree,
2014 * read that key so we can verify it is also a checksum item
2015 */
2016 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2017
2018 if (found_key.objectid < inode->i_ino)
2019 goto next_key;
2020
2021 if (found_key.type != key.type || found_key.offset < new_size)
2022 goto next_key;
2023
2024 /*
2025 * if the key for the next leaf isn't a csum key from this objectid,
2026 * we can't be sure there aren't good items inside this leaf.
2027 * Bail out
2028 */
2029 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2030 goto out;
2031
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002032 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2033 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002034 /*
2035 * it is safe to delete this leaf, it contains only
2036 * csum items from this inode at an offset >= new_size
2037 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002038 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002039 BUG_ON(ret);
2040
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002041 if (root->ref_cows && leaf_gen < trans->transid) {
2042 ref = btrfs_alloc_leaf_ref(root, 0);
2043 if (ref) {
2044 ref->root_gen = root->root_key.offset;
2045 ref->bytenr = leaf_start;
2046 ref->owner = 0;
2047 ref->generation = leaf_gen;
2048 ref->nritems = 0;
2049
2050 ret = btrfs_add_leaf_ref(root, ref, 0);
2051 WARN_ON(ret);
2052 btrfs_free_leaf_ref(root, ref);
2053 } else {
2054 WARN_ON(1);
2055 }
2056 }
Chris Mason323ac952008-10-01 19:05:46 -04002057next_key:
2058 btrfs_release_path(root, path);
2059
2060 if (other_key.objectid == inode->i_ino &&
2061 other_key.type == key.type && other_key.offset > key.offset) {
2062 key.offset = other_key.offset;
2063 cond_resched();
2064 goto again;
2065 }
2066 ret = 0;
2067out:
2068 /* fixup any changes we've made to the path */
2069 path->lowest_level = 0;
2070 path->keep_locks = 0;
2071 btrfs_release_path(root, path);
2072 return ret;
2073}
2074
2075/*
Chris Mason39279cc2007-06-12 06:35:45 -04002076 * this can truncate away extent items, csum items and directory items.
2077 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002078 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002079 *
2080 * csum items that cross the new i_size are truncated to the new size
2081 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002082 *
2083 * min_type is the minimum key type to truncate down to. If set to 0, this
2084 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002085 */
Chris Masone02119d2008-09-05 16:13:11 -04002086noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2087 struct btrfs_root *root,
2088 struct inode *inode,
2089 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002090{
2091 int ret;
2092 struct btrfs_path *path;
2093 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002094 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002095 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04002096 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002097 struct btrfs_file_extent_item *fi;
2098 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002099 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002100 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002101 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002102 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002103 int found_extent;
2104 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002105 int pending_del_nr = 0;
2106 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002107 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04002108 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002109
Chris Masone02119d2008-09-05 16:13:11 -04002110 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002111 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002112 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002113 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002114 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002115
Chris Mason39279cc2007-06-12 06:35:45 -04002116 /* FIXME, add redo link to tree so we don't leak on crash */
2117 key.objectid = inode->i_ino;
2118 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002119 key.type = (u8)-1;
2120
Chris Mason85e21ba2008-01-29 15:11:36 -05002121 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04002122
2123 ret = drop_csum_leaves(trans, root, path, inode, new_size);
2124 BUG_ON(ret);
2125
Chris Mason85e21ba2008-01-29 15:11:36 -05002126search_again:
2127 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2128 if (ret < 0) {
2129 goto error;
2130 }
2131 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002132 /* there are no items in the tree for us to truncate, we're
2133 * done
2134 */
2135 if (path->slots[0] == 0) {
2136 ret = 0;
2137 goto error;
2138 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002139 path->slots[0]--;
2140 }
2141
Chris Mason39279cc2007-06-12 06:35:45 -04002142 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002143 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002144 leaf = path->nodes[0];
2145 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2146 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002147
Chris Mason5f39d392007-10-15 16:14:19 -04002148 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002149 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002150
Chris Mason85e21ba2008-01-29 15:11:36 -05002151 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002152 break;
2153
Chris Mason5f39d392007-10-15 16:14:19 -04002154 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002155 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002156 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002157 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002158 extent_type = btrfs_file_extent_type(leaf, fi);
2159 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002160 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002161 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002162 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002163 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002164 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002165 }
Yan008630c2007-11-07 13:31:09 -05002166 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002167 }
2168 if (found_type == BTRFS_CSUM_ITEM_KEY) {
2169 ret = btrfs_csum_truncate(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002170 new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002171 BUG_ON(ret);
2172 }
Chris Masone02119d2008-09-05 16:13:11 -04002173 if (item_end < new_size) {
Chris Masonb888db22007-08-27 16:49:44 -04002174 if (found_type == BTRFS_DIR_ITEM_KEY) {
2175 found_type = BTRFS_INODE_ITEM_KEY;
2176 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
2177 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05002178 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
2179 found_type = BTRFS_XATTR_ITEM_KEY;
2180 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
2181 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04002182 } else if (found_type) {
2183 found_type--;
2184 } else {
2185 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002186 }
Yana61721d2007-09-17 11:08:38 -04002187 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002188 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002189 }
Chris Masone02119d2008-09-05 16:13:11 -04002190 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002191 del_item = 1;
2192 else
2193 del_item = 0;
2194 found_extent = 0;
2195
2196 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002197 if (found_type != BTRFS_EXTENT_DATA_KEY)
2198 goto delete;
2199
2200 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002201 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002202 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002203 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04002204 u64 orig_num_bytes =
2205 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002206 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002207 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002208 extent_num_bytes = extent_num_bytes &
2209 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002210 btrfs_set_file_extent_num_bytes(leaf, fi,
2211 extent_num_bytes);
2212 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002213 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002214 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002215 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002216 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002217 } else {
Chris Masondb945352007-10-15 16:15:53 -04002218 extent_num_bytes =
2219 btrfs_file_extent_disk_num_bytes(leaf,
2220 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002221 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002222 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002223 if (extent_start != 0) {
2224 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002225 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002226 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002227 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002228 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002229 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002230 }
Chris Mason90692182008-02-08 13:49:28 -05002231 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002232 /*
2233 * we can't truncate inline items that have had
2234 * special encodings
2235 */
2236 if (!del_item &&
2237 btrfs_file_extent_compression(leaf, fi) == 0 &&
2238 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2239 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002240 u32 size = new_size - found_key.offset;
2241
2242 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002243 inode_sub_bytes(inode, item_end + 1 -
2244 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002245 }
2246 size =
2247 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002248 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002249 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002250 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002251 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002252 inode_sub_bytes(inode, item_end + 1 -
2253 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002254 }
Chris Mason39279cc2007-06-12 06:35:45 -04002255 }
Chris Mason179e29e2007-11-01 11:28:41 -04002256delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002257 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002258 if (!pending_del_nr) {
2259 /* no pending yet, add ourselves */
2260 pending_del_slot = path->slots[0];
2261 pending_del_nr = 1;
2262 } else if (pending_del_nr &&
2263 path->slots[0] + 1 == pending_del_slot) {
2264 /* hop on the pending chunk */
2265 pending_del_nr++;
2266 pending_del_slot = path->slots[0];
2267 } else {
2268 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
2269 }
Chris Mason39279cc2007-06-12 06:35:45 -04002270 } else {
2271 break;
2272 }
Chris Mason39279cc2007-06-12 06:35:45 -04002273 if (found_extent) {
2274 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002275 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002276 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002277 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002278 BUG_ON(ret);
2279 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002280next:
2281 if (path->slots[0] == 0) {
2282 if (pending_del_nr)
2283 goto del_pending;
2284 btrfs_release_path(root, path);
2285 goto search_again;
2286 }
2287
2288 path->slots[0]--;
2289 if (pending_del_nr &&
2290 path->slots[0] + 1 != pending_del_slot) {
2291 struct btrfs_key debug;
2292del_pending:
2293 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2294 pending_del_slot);
2295 ret = btrfs_del_items(trans, root, path,
2296 pending_del_slot,
2297 pending_del_nr);
2298 BUG_ON(ret);
2299 pending_del_nr = 0;
2300 btrfs_release_path(root, path);
2301 goto search_again;
2302 }
Chris Mason39279cc2007-06-12 06:35:45 -04002303 }
2304 ret = 0;
2305error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002306 if (pending_del_nr) {
2307 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2308 pending_del_nr);
2309 }
Chris Mason39279cc2007-06-12 06:35:45 -04002310 btrfs_free_path(path);
2311 inode->i_sb->s_dirt = 1;
2312 return ret;
2313}
2314
Chris Masona52d9a82007-08-27 16:49:44 -04002315/*
2316 * taken from block_truncate_page, but does cow as it zeros out
2317 * any bytes left in the last page in the file.
2318 */
2319static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2320{
2321 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002322 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002323 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2324 struct btrfs_ordered_extent *ordered;
2325 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002326 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002327 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2328 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2329 struct page *page;
2330 int ret = 0;
2331 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002332 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002333
2334 if ((offset & (blocksize - 1)) == 0)
2335 goto out;
2336
2337 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002338again:
Chris Masona52d9a82007-08-27 16:49:44 -04002339 page = grab_cache_page(mapping, index);
2340 if (!page)
2341 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002342
2343 page_start = page_offset(page);
2344 page_end = page_start + PAGE_CACHE_SIZE - 1;
2345
Chris Masona52d9a82007-08-27 16:49:44 -04002346 if (!PageUptodate(page)) {
2347 ret = btrfs_readpage(NULL, page);
2348 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002349 if (page->mapping != mapping) {
2350 unlock_page(page);
2351 page_cache_release(page);
2352 goto again;
2353 }
Chris Masona52d9a82007-08-27 16:49:44 -04002354 if (!PageUptodate(page)) {
2355 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002356 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002357 }
2358 }
Chris Mason211c17f2008-05-15 09:13:45 -04002359 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002360
2361 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2362 set_page_extent_mapped(page);
2363
2364 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2365 if (ordered) {
2366 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2367 unlock_page(page);
2368 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002369 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002370 btrfs_put_ordered_extent(ordered);
2371 goto again;
2372 }
2373
Chris Masonea8c2812008-08-04 23:17:27 -04002374 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002375 ret = 0;
2376 if (offset != PAGE_CACHE_SIZE) {
2377 kaddr = kmap(page);
2378 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2379 flush_dcache_page(page);
2380 kunmap(page);
2381 }
Chris Mason247e7432008-07-17 12:53:51 -04002382 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002383 set_page_dirty(page);
2384 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002385
Chris Mason89642222008-07-24 09:41:53 -04002386out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002387 unlock_page(page);
2388 page_cache_release(page);
2389out:
2390 return ret;
2391}
2392
Yan Zheng9036c102008-10-30 14:19:41 -04002393int btrfs_cont_expand(struct inode *inode, loff_t size)
2394{
2395 struct btrfs_trans_handle *trans;
2396 struct btrfs_root *root = BTRFS_I(inode)->root;
2397 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2398 struct extent_map *em;
2399 u64 mask = root->sectorsize - 1;
2400 u64 hole_start = (inode->i_size + mask) & ~mask;
2401 u64 block_end = (size + mask) & ~mask;
2402 u64 last_byte;
2403 u64 cur_offset;
2404 u64 hole_size;
2405 int err;
2406
2407 if (size <= hole_start)
2408 return 0;
2409
2410 err = btrfs_check_free_space(root, 1, 0);
2411 if (err)
2412 return err;
2413
2414 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2415
2416 while (1) {
2417 struct btrfs_ordered_extent *ordered;
2418 btrfs_wait_ordered_range(inode, hole_start,
2419 block_end - hole_start);
2420 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2421 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2422 if (!ordered)
2423 break;
2424 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2425 btrfs_put_ordered_extent(ordered);
2426 }
2427
2428 trans = btrfs_start_transaction(root, 1);
2429 btrfs_set_trans_block_group(trans, inode);
2430
2431 cur_offset = hole_start;
2432 while (1) {
2433 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2434 block_end - cur_offset, 0);
2435 BUG_ON(IS_ERR(em) || !em);
2436 last_byte = min(extent_map_end(em), block_end);
2437 last_byte = (last_byte + mask) & ~mask;
2438 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2439 hole_size = last_byte - cur_offset;
2440 err = btrfs_insert_file_extent(trans, root,
2441 inode->i_ino, cur_offset, 0,
2442 0, hole_size, 0, hole_size,
2443 0, 0, 0);
2444 btrfs_drop_extent_cache(inode, hole_start,
2445 last_byte - 1, 0);
2446 }
2447 free_extent_map(em);
2448 cur_offset = last_byte;
2449 if (err || cur_offset >= block_end)
2450 break;
2451 }
2452
2453 btrfs_end_transaction(trans, root);
2454 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2455 return err;
2456}
2457
Chris Mason39279cc2007-06-12 06:35:45 -04002458static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2459{
2460 struct inode *inode = dentry->d_inode;
2461 int err;
2462
2463 err = inode_change_ok(inode, attr);
2464 if (err)
2465 return err;
2466
2467 if (S_ISREG(inode->i_mode) &&
2468 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002469 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002470 if (err)
2471 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002472 }
Yan Zheng9036c102008-10-30 14:19:41 -04002473
Chris Mason39279cc2007-06-12 06:35:45 -04002474 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002475
2476 if (!err && ((attr->ia_valid & ATTR_MODE)))
2477 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002478 return err;
2479}
Chris Mason61295eb2008-01-14 16:24:38 -05002480
Chris Mason39279cc2007-06-12 06:35:45 -04002481void btrfs_delete_inode(struct inode *inode)
2482{
2483 struct btrfs_trans_handle *trans;
2484 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002485 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002486 int ret;
2487
2488 truncate_inode_pages(&inode->i_data, 0);
2489 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002490 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002491 goto no_delete;
2492 }
Chris Mason4a096752008-07-21 10:29:44 -04002493 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002494
Chris Masondbe674a2008-07-17 12:54:05 -04002495 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002496 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002497
Chris Mason39279cc2007-06-12 06:35:45 -04002498 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002499 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002500 if (ret) {
2501 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002502 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002503 }
2504
2505 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002506
Chris Masond3c2fdc2007-09-17 10:58:06 -04002507 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002508 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002509
Chris Mason39279cc2007-06-12 06:35:45 -04002510 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002511 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002512 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002513
2514no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002515 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002516 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002517 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002518no_delete:
2519 clear_inode(inode);
2520}
2521
2522/*
2523 * this returns the key found in the dir entry in the location pointer.
2524 * If no dir entries were found, location->objectid is 0.
2525 */
2526static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2527 struct btrfs_key *location)
2528{
2529 const char *name = dentry->d_name.name;
2530 int namelen = dentry->d_name.len;
2531 struct btrfs_dir_item *di;
2532 struct btrfs_path *path;
2533 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002534 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002535
2536 path = btrfs_alloc_path();
2537 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002538
Chris Mason39279cc2007-06-12 06:35:45 -04002539 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2540 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002541 if (IS_ERR(di))
2542 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04002543 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05002544 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04002545 }
Chris Mason5f39d392007-10-15 16:14:19 -04002546 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002547out:
Chris Mason39279cc2007-06-12 06:35:45 -04002548 btrfs_free_path(path);
2549 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002550out_err:
2551 location->objectid = 0;
2552 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002553}
2554
2555/*
2556 * when we hit a tree root in a directory, the btrfs part of the inode
2557 * needs to be changed to reflect the root directory of the tree root. This
2558 * is kind of like crossing a mount point.
2559 */
2560static int fixup_tree_root_location(struct btrfs_root *root,
2561 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002562 struct btrfs_root **sub_root,
2563 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002564{
Chris Mason39279cc2007-06-12 06:35:45 -04002565 struct btrfs_root_item *ri;
2566
2567 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2568 return 0;
2569 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2570 return 0;
2571
Josef Bacik58176a92007-08-29 15:47:34 -04002572 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2573 dentry->d_name.name,
2574 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002575 if (IS_ERR(*sub_root))
2576 return PTR_ERR(*sub_root);
2577
2578 ri = &(*sub_root)->root_item;
2579 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002580 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2581 location->offset = 0;
2582
Chris Mason39279cc2007-06-12 06:35:45 -04002583 return 0;
2584}
2585
Chris Masone02119d2008-09-05 16:13:11 -04002586static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002587{
Chris Masone02119d2008-09-05 16:13:11 -04002588 struct btrfs_inode *bi = BTRFS_I(inode);
2589
2590 bi->i_acl = NULL;
2591 bi->i_default_acl = NULL;
2592
2593 bi->generation = 0;
2594 bi->last_trans = 0;
2595 bi->logged_trans = 0;
2596 bi->delalloc_bytes = 0;
2597 bi->disk_i_size = 0;
2598 bi->flags = 0;
2599 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002600 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002601 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2602 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002603 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002604 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2605 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002606 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04002607 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002608 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002609 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002610 mutex_init(&BTRFS_I(inode)->log_mutex);
2611}
2612
2613static int btrfs_init_locked_inode(struct inode *inode, void *p)
2614{
2615 struct btrfs_iget_args *args = p;
2616 inode->i_ino = args->ino;
2617 init_btrfs_i(inode);
2618 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002619 return 0;
2620}
2621
2622static int btrfs_find_actor(struct inode *inode, void *opaque)
2623{
2624 struct btrfs_iget_args *args = opaque;
2625 return (args->ino == inode->i_ino &&
2626 args->root == BTRFS_I(inode)->root);
2627}
2628
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002629struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2630 struct btrfs_root *root, int wait)
2631{
2632 struct inode *inode;
2633 struct btrfs_iget_args args;
2634 args.ino = objectid;
2635 args.root = root;
2636
2637 if (wait) {
2638 inode = ilookup5(s, objectid, btrfs_find_actor,
2639 (void *)&args);
2640 } else {
2641 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2642 (void *)&args);
2643 }
2644 return inode;
2645}
2646
Chris Mason39279cc2007-06-12 06:35:45 -04002647struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2648 struct btrfs_root *root)
2649{
2650 struct inode *inode;
2651 struct btrfs_iget_args args;
2652 args.ino = objectid;
2653 args.root = root;
2654
2655 inode = iget5_locked(s, objectid, btrfs_find_actor,
2656 btrfs_init_locked_inode,
2657 (void *)&args);
2658 return inode;
2659}
2660
Balaji Rao1a54ef82008-07-21 02:01:04 +05302661/* Get an inode object given its location and corresponding root.
2662 * Returns in *is_new if the inode was read from disk
2663 */
2664struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2665 struct btrfs_root *root, int *is_new)
2666{
2667 struct inode *inode;
2668
2669 inode = btrfs_iget_locked(s, location->objectid, root);
2670 if (!inode)
2671 return ERR_PTR(-EACCES);
2672
2673 if (inode->i_state & I_NEW) {
2674 BTRFS_I(inode)->root = root;
2675 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2676 btrfs_read_locked_inode(inode);
2677 unlock_new_inode(inode);
2678 if (is_new)
2679 *is_new = 1;
2680 } else {
2681 if (is_new)
2682 *is_new = 0;
2683 }
2684
2685 return inode;
2686}
2687
Chris Mason39279cc2007-06-12 06:35:45 -04002688static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2689 struct nameidata *nd)
2690{
2691 struct inode * inode;
2692 struct btrfs_inode *bi = BTRFS_I(dir);
2693 struct btrfs_root *root = bi->root;
2694 struct btrfs_root *sub_root = root;
2695 struct btrfs_key location;
Balaji Rao1a54ef82008-07-21 02:01:04 +05302696 int ret, new, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002697
2698 if (dentry->d_name.len > BTRFS_NAME_LEN)
2699 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04002700
Chris Mason39279cc2007-06-12 06:35:45 -04002701 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002702
Chris Mason39279cc2007-06-12 06:35:45 -04002703 if (ret < 0)
2704 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002705
Chris Mason39279cc2007-06-12 06:35:45 -04002706 inode = NULL;
2707 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04002708 ret = fixup_tree_root_location(root, &location, &sub_root,
2709 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04002710 if (ret < 0)
2711 return ERR_PTR(ret);
2712 if (ret > 0)
2713 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05302714 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2715 if (IS_ERR(inode))
2716 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002717
Balaji Rao1a54ef82008-07-21 02:01:04 +05302718 /* the inode and parent dir are two different roots */
2719 if (new && root != sub_root) {
2720 igrab(inode);
2721 sub_root->inode = inode;
2722 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002723 }
2724 }
Josef Bacik7b128762008-07-24 12:17:14 -04002725
2726 if (unlikely(do_orphan))
2727 btrfs_orphan_cleanup(sub_root);
2728
Chris Mason39279cc2007-06-12 06:35:45 -04002729 return d_splice_alias(inode, dentry);
2730}
2731
Chris Mason39279cc2007-06-12 06:35:45 -04002732static unsigned char btrfs_filetype_table[] = {
2733 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2734};
2735
David Woodhousecbdf5a22008-08-06 19:42:33 +01002736static int btrfs_real_readdir(struct file *filp, void *dirent,
2737 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04002738{
Chris Mason6da6aba2007-12-18 16:15:09 -05002739 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002740 struct btrfs_root *root = BTRFS_I(inode)->root;
2741 struct btrfs_item *item;
2742 struct btrfs_dir_item *di;
2743 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002744 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002745 struct btrfs_path *path;
2746 int ret;
2747 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002748 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002749 int slot;
2750 int advance;
2751 unsigned char d_type;
2752 int over = 0;
2753 u32 di_cur;
2754 u32 di_total;
2755 u32 di_len;
2756 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002757 char tmp_name[32];
2758 char *name_ptr;
2759 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04002760
2761 /* FIXME, use a real flag for deciding about the key type */
2762 if (root->fs_info->tree_root == root)
2763 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002764
Chris Mason39544012007-12-12 14:38:19 -05002765 /* special case for "." */
2766 if (filp->f_pos == 0) {
2767 over = filldir(dirent, ".", 1,
2768 1, inode->i_ino,
2769 DT_DIR);
2770 if (over)
2771 return 0;
2772 filp->f_pos = 1;
2773 }
Chris Mason39544012007-12-12 14:38:19 -05002774 /* special case for .., just use the back ref */
2775 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002776 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05002777 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002778 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05002779 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01002780 return 0;
Chris Mason39544012007-12-12 14:38:19 -05002781 filp->f_pos = 2;
2782 }
2783
David Woodhouse49593bf2008-08-17 17:08:36 +01002784 path = btrfs_alloc_path();
2785 path->reada = 2;
2786
Chris Mason39279cc2007-06-12 06:35:45 -04002787 btrfs_set_key_type(&key, key_type);
2788 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002789 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04002790
Chris Mason39279cc2007-06-12 06:35:45 -04002791 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2792 if (ret < 0)
2793 goto err;
2794 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01002795
2796 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002797 leaf = path->nodes[0];
2798 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002799 slot = path->slots[0];
2800 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01002801 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002802 ret = btrfs_next_leaf(root, path);
2803 if (ret)
2804 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002805 leaf = path->nodes[0];
2806 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002807 slot = path->slots[0];
2808 } else {
2809 slot++;
2810 path->slots[0]++;
2811 }
2812 }
2813 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002814 item = btrfs_item_nr(leaf, slot);
2815 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2816
2817 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002818 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002819 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002820 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002821 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002822 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002823
2824 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01002825
Chris Mason39279cc2007-06-12 06:35:45 -04002826 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2827 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002828 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01002829
2830 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002831 struct btrfs_key location;
2832
2833 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01002834 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002835 name_ptr = tmp_name;
2836 } else {
2837 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01002838 if (!name_ptr) {
2839 ret = -ENOMEM;
2840 goto err;
2841 }
Chris Mason5f39d392007-10-15 16:14:19 -04002842 }
2843 read_extent_buffer(leaf, name_ptr,
2844 (unsigned long)(di + 1), name_len);
2845
2846 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2847 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002848 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01002849 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002850 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002851
2852 if (name_ptr != tmp_name)
2853 kfree(name_ptr);
2854
Chris Mason39279cc2007-06-12 06:35:45 -04002855 if (over)
2856 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002857
Josef Bacik5103e942007-11-16 11:45:54 -05002858 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01002859 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002860 di_cur += di_len;
2861 di = (struct btrfs_dir_item *)((char *)di + di_len);
2862 }
2863 }
David Woodhouse49593bf2008-08-17 17:08:36 +01002864
2865 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05002866 if (key_type == BTRFS_DIR_INDEX_KEY)
2867 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2868 else
2869 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002870nopos:
2871 ret = 0;
2872err:
Chris Mason39279cc2007-06-12 06:35:45 -04002873 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002874 return ret;
2875}
2876
2877int btrfs_write_inode(struct inode *inode, int wait)
2878{
2879 struct btrfs_root *root = BTRFS_I(inode)->root;
2880 struct btrfs_trans_handle *trans;
2881 int ret = 0;
2882
Chris Mason4ca8b412008-08-05 13:30:48 -04002883 if (root->fs_info->closing > 1)
2884 return 0;
2885
Chris Mason39279cc2007-06-12 06:35:45 -04002886 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002887 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002888 btrfs_set_trans_block_group(trans, inode);
2889 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002890 }
2891 return ret;
2892}
2893
2894/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002895 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002896 * inode changes. But, it is most likely to find the inode in cache.
2897 * FIXME, needs more benchmarking...there are no reasons other than performance
2898 * to keep or drop this code.
2899 */
2900void btrfs_dirty_inode(struct inode *inode)
2901{
2902 struct btrfs_root *root = BTRFS_I(inode)->root;
2903 struct btrfs_trans_handle *trans;
2904
Chris Masonf9295742008-07-17 12:54:14 -04002905 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002906 btrfs_set_trans_block_group(trans, inode);
2907 btrfs_update_inode(trans, root, inode);
2908 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002909}
2910
Chris Masond352ac62008-09-29 15:18:18 -04002911/*
2912 * find the highest existing sequence number in a directory
2913 * and then set the in-memory index_cnt variable to reflect
2914 * free sequence numbers
2915 */
Josef Bacikaec74772008-07-24 12:12:38 -04002916static int btrfs_set_inode_index_count(struct inode *inode)
2917{
2918 struct btrfs_root *root = BTRFS_I(inode)->root;
2919 struct btrfs_key key, found_key;
2920 struct btrfs_path *path;
2921 struct extent_buffer *leaf;
2922 int ret;
2923
2924 key.objectid = inode->i_ino;
2925 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2926 key.offset = (u64)-1;
2927
2928 path = btrfs_alloc_path();
2929 if (!path)
2930 return -ENOMEM;
2931
2932 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2933 if (ret < 0)
2934 goto out;
2935 /* FIXME: we should be able to handle this */
2936 if (ret == 0)
2937 goto out;
2938 ret = 0;
2939
2940 /*
2941 * MAGIC NUMBER EXPLANATION:
2942 * since we search a directory based on f_pos we have to start at 2
2943 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2944 * else has to start at 2
2945 */
2946 if (path->slots[0] == 0) {
2947 BTRFS_I(inode)->index_cnt = 2;
2948 goto out;
2949 }
2950
2951 path->slots[0]--;
2952
2953 leaf = path->nodes[0];
2954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2955
2956 if (found_key.objectid != inode->i_ino ||
2957 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2958 BTRFS_I(inode)->index_cnt = 2;
2959 goto out;
2960 }
2961
2962 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2963out:
2964 btrfs_free_path(path);
2965 return ret;
2966}
2967
Chris Masond352ac62008-09-29 15:18:18 -04002968/*
2969 * helper to find a free sequence number in a given directory. This current
2970 * code is very simple, later versions will do smarter things in the btree
2971 */
Chris Mason00e4e6b2008-08-05 11:18:09 -04002972static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2973 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002974{
2975 int ret = 0;
2976
2977 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2978 ret = btrfs_set_inode_index_count(dir);
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002979 if (ret) {
Josef Bacikaec74772008-07-24 12:12:38 -04002980 return ret;
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002981 }
Josef Bacikaec74772008-07-24 12:12:38 -04002982 }
2983
Chris Mason00e4e6b2008-08-05 11:18:09 -04002984 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002985 BTRFS_I(dir)->index_cnt++;
2986
2987 return ret;
2988}
2989
Chris Mason39279cc2007-06-12 06:35:45 -04002990static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2991 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002992 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002993 const char *name, int name_len,
2994 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002995 u64 objectid,
2996 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002997 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002998{
2999 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003000 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04003001 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04003002 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003003 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003004 struct btrfs_inode_ref *ref;
3005 struct btrfs_key key[2];
3006 u32 sizes[2];
3007 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003008 int ret;
3009 int owner;
3010
Chris Mason5f39d392007-10-15 16:14:19 -04003011 path = btrfs_alloc_path();
3012 BUG_ON(!path);
3013
Chris Mason39279cc2007-06-12 06:35:45 -04003014 inode = new_inode(root->fs_info->sb);
3015 if (!inode)
3016 return ERR_PTR(-ENOMEM);
3017
Josef Bacikaec74772008-07-24 12:12:38 -04003018 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04003019 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04003020 if (ret)
3021 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003022 }
3023 /*
3024 * index_cnt is ignored for everything but a dir,
3025 * btrfs_get_inode_index_count has an explanation for the magic
3026 * number
3027 */
Chris Masone02119d2008-09-05 16:13:11 -04003028 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003029 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003030 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003031 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04003032
Chris Mason39279cc2007-06-12 06:35:45 -04003033 if (mode & S_IFDIR)
3034 owner = 0;
3035 else
3036 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04003037 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04003038 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04003039 if (!new_inode_group) {
3040 printk("find_block group failed\n");
3041 new_inode_group = group;
3042 }
3043 BTRFS_I(inode)->block_group = new_inode_group;
Chris Mason9c583092008-01-29 15:15:18 -05003044
3045 key[0].objectid = objectid;
3046 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3047 key[0].offset = 0;
3048
3049 key[1].objectid = objectid;
3050 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3051 key[1].offset = ref_objectid;
3052
3053 sizes[0] = sizeof(struct btrfs_inode_item);
3054 sizes[1] = name_len + sizeof(*ref);
3055
3056 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3057 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003058 goto fail;
3059
Chris Mason9c583092008-01-29 15:15:18 -05003060 if (objectid > root->highest_inode)
3061 root->highest_inode = objectid;
3062
Chris Mason39279cc2007-06-12 06:35:45 -04003063 inode->i_uid = current->fsuid;
3064 inode->i_gid = current->fsgid;
3065 inode->i_mode = mode;
3066 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003067 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003068 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003069 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3070 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003071 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003072
3073 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3074 struct btrfs_inode_ref);
3075 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003076 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003077 ptr = (unsigned long)(ref + 1);
3078 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3079
Chris Mason5f39d392007-10-15 16:14:19 -04003080 btrfs_mark_buffer_dirty(path->nodes[0]);
3081 btrfs_free_path(path);
3082
Chris Mason39279cc2007-06-12 06:35:45 -04003083 location = &BTRFS_I(inode)->location;
3084 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003085 location->offset = 0;
3086 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3087
Chris Mason39279cc2007-06-12 06:35:45 -04003088 insert_inode_hash(inode);
3089 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003090fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003091 if (dir)
3092 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003093 btrfs_free_path(path);
3094 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003095}
3096
3097static inline u8 btrfs_inode_type(struct inode *inode)
3098{
3099 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3100}
3101
Chris Masond352ac62008-09-29 15:18:18 -04003102/*
3103 * utility function to add 'inode' into 'parent_inode' with
3104 * a give name and a given sequence number.
3105 * if 'add_backref' is true, also insert a backref from the
3106 * inode to the parent directory.
3107 */
Chris Masone02119d2008-09-05 16:13:11 -04003108int btrfs_add_link(struct btrfs_trans_handle *trans,
3109 struct inode *parent_inode, struct inode *inode,
3110 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003111{
3112 int ret;
3113 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003114 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003115
Chris Mason39279cc2007-06-12 06:35:45 -04003116 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003117 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3118 key.offset = 0;
3119
Chris Masone02119d2008-09-05 16:13:11 -04003120 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3121 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003122 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003123 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003124 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003125 if (add_backref) {
3126 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003127 name, name_len,
3128 inode->i_ino,
3129 parent_inode->i_ino,
3130 index);
Chris Mason9c583092008-01-29 15:15:18 -05003131 }
Chris Masondbe674a2008-07-17 12:54:05 -04003132 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003133 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003134 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003135 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003136 }
3137 return ret;
3138}
3139
3140static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003141 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003142 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003143{
Chris Masone02119d2008-09-05 16:13:11 -04003144 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3145 inode, dentry->d_name.name,
3146 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003147 if (!err) {
3148 d_instantiate(dentry, inode);
3149 return 0;
3150 }
3151 if (err > 0)
3152 err = -EEXIST;
3153 return err;
3154}
3155
Josef Bacik618e21d2007-07-11 10:18:17 -04003156static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3157 int mode, dev_t rdev)
3158{
3159 struct btrfs_trans_handle *trans;
3160 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003161 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003162 int err;
3163 int drop_inode = 0;
3164 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003165 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003166 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003167
3168 if (!new_valid_dev(rdev))
3169 return -EINVAL;
3170
Chris Mason1832a6d2007-12-21 16:27:21 -05003171 err = btrfs_check_free_space(root, 1, 0);
3172 if (err)
3173 goto fail;
3174
Josef Bacik618e21d2007-07-11 10:18:17 -04003175 trans = btrfs_start_transaction(root, 1);
3176 btrfs_set_trans_block_group(trans, dir);
3177
3178 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3179 if (err) {
3180 err = -ENOSPC;
3181 goto out_unlock;
3182 }
3183
Josef Bacikaec74772008-07-24 12:12:38 -04003184 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003185 dentry->d_name.len,
3186 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003187 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003188 err = PTR_ERR(inode);
3189 if (IS_ERR(inode))
3190 goto out_unlock;
3191
Josef Bacik33268ea2008-07-24 12:16:36 -04003192 err = btrfs_init_acl(inode, dir);
3193 if (err) {
3194 drop_inode = 1;
3195 goto out_unlock;
3196 }
3197
Josef Bacik618e21d2007-07-11 10:18:17 -04003198 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003199 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003200 if (err)
3201 drop_inode = 1;
3202 else {
3203 inode->i_op = &btrfs_special_inode_operations;
3204 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003205 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003206 }
3207 dir->i_sb->s_dirt = 1;
3208 btrfs_update_inode_block_group(trans, inode);
3209 btrfs_update_inode_block_group(trans, dir);
3210out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003211 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003212 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003213fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003214 if (drop_inode) {
3215 inode_dec_link_count(inode);
3216 iput(inode);
3217 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003218 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003219 return err;
3220}
3221
Chris Mason39279cc2007-06-12 06:35:45 -04003222static int btrfs_create(struct inode *dir, struct dentry *dentry,
3223 int mode, struct nameidata *nd)
3224{
3225 struct btrfs_trans_handle *trans;
3226 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003227 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003228 int err;
3229 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003230 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003231 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003232 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003233
Chris Mason1832a6d2007-12-21 16:27:21 -05003234 err = btrfs_check_free_space(root, 1, 0);
3235 if (err)
3236 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003237 trans = btrfs_start_transaction(root, 1);
3238 btrfs_set_trans_block_group(trans, dir);
3239
3240 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3241 if (err) {
3242 err = -ENOSPC;
3243 goto out_unlock;
3244 }
3245
Josef Bacikaec74772008-07-24 12:12:38 -04003246 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003247 dentry->d_name.len,
3248 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003249 objectid, BTRFS_I(dir)->block_group, mode,
3250 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003251 err = PTR_ERR(inode);
3252 if (IS_ERR(inode))
3253 goto out_unlock;
3254
Josef Bacik33268ea2008-07-24 12:16:36 -04003255 err = btrfs_init_acl(inode, dir);
3256 if (err) {
3257 drop_inode = 1;
3258 goto out_unlock;
3259 }
3260
Chris Mason39279cc2007-06-12 06:35:45 -04003261 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003262 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003263 if (err)
3264 drop_inode = 1;
3265 else {
3266 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003267 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003268 inode->i_fop = &btrfs_file_operations;
3269 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003270 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003271 }
3272 dir->i_sb->s_dirt = 1;
3273 btrfs_update_inode_block_group(trans, inode);
3274 btrfs_update_inode_block_group(trans, dir);
3275out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003276 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003277 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003278fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003279 if (drop_inode) {
3280 inode_dec_link_count(inode);
3281 iput(inode);
3282 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003283 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003284 return err;
3285}
3286
3287static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3288 struct dentry *dentry)
3289{
3290 struct btrfs_trans_handle *trans;
3291 struct btrfs_root *root = BTRFS_I(dir)->root;
3292 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003293 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003294 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003295 int err;
3296 int drop_inode = 0;
3297
3298 if (inode->i_nlink == 0)
3299 return -ENOENT;
3300
Chris Masone02119d2008-09-05 16:13:11 -04003301 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003302 err = btrfs_check_free_space(root, 1, 0);
3303 if (err)
3304 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003305 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003306 if (err)
3307 goto fail;
3308
Chris Mason39279cc2007-06-12 06:35:45 -04003309 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003310
Chris Mason39279cc2007-06-12 06:35:45 -04003311 btrfs_set_trans_block_group(trans, dir);
3312 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003313
Chris Mason00e4e6b2008-08-05 11:18:09 -04003314 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003315
Chris Mason39279cc2007-06-12 06:35:45 -04003316 if (err)
3317 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003318
Chris Mason39279cc2007-06-12 06:35:45 -04003319 dir->i_sb->s_dirt = 1;
3320 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003321 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003322
Chris Mason54aa1f42007-06-22 14:16:25 -04003323 if (err)
3324 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003325
Chris Masond3c2fdc2007-09-17 10:58:06 -04003326 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003327 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003328fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003329 if (drop_inode) {
3330 inode_dec_link_count(inode);
3331 iput(inode);
3332 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003333 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003334 return err;
3335}
3336
Chris Mason39279cc2007-06-12 06:35:45 -04003337static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3338{
Chris Masonb9d86662008-05-02 16:13:49 -04003339 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003340 struct btrfs_trans_handle *trans;
3341 struct btrfs_root *root = BTRFS_I(dir)->root;
3342 int err = 0;
3343 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003344 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003345 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003346 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003347
Chris Mason1832a6d2007-12-21 16:27:21 -05003348 err = btrfs_check_free_space(root, 1, 0);
3349 if (err)
3350 goto out_unlock;
3351
Chris Mason39279cc2007-06-12 06:35:45 -04003352 trans = btrfs_start_transaction(root, 1);
3353 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003354
Chris Mason39279cc2007-06-12 06:35:45 -04003355 if (IS_ERR(trans)) {
3356 err = PTR_ERR(trans);
3357 goto out_unlock;
3358 }
3359
3360 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3361 if (err) {
3362 err = -ENOSPC;
3363 goto out_unlock;
3364 }
3365
Josef Bacikaec74772008-07-24 12:12:38 -04003366 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003367 dentry->d_name.len,
3368 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003369 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3370 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003371 if (IS_ERR(inode)) {
3372 err = PTR_ERR(inode);
3373 goto out_fail;
3374 }
Chris Mason5f39d392007-10-15 16:14:19 -04003375
Chris Mason39279cc2007-06-12 06:35:45 -04003376 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003377
3378 err = btrfs_init_acl(inode, dir);
3379 if (err)
3380 goto out_fail;
3381
Chris Mason39279cc2007-06-12 06:35:45 -04003382 inode->i_op = &btrfs_dir_inode_operations;
3383 inode->i_fop = &btrfs_dir_file_operations;
3384 btrfs_set_trans_block_group(trans, inode);
3385
Chris Masondbe674a2008-07-17 12:54:05 -04003386 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003387 err = btrfs_update_inode(trans, root, inode);
3388 if (err)
3389 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003390
Chris Masone02119d2008-09-05 16:13:11 -04003391 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3392 inode, dentry->d_name.name,
3393 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003394 if (err)
3395 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003396
Chris Mason39279cc2007-06-12 06:35:45 -04003397 d_instantiate(dentry, inode);
3398 drop_on_err = 0;
3399 dir->i_sb->s_dirt = 1;
3400 btrfs_update_inode_block_group(trans, inode);
3401 btrfs_update_inode_block_group(trans, dir);
3402
3403out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003404 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003405 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003406
Chris Mason39279cc2007-06-12 06:35:45 -04003407out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003408 if (drop_on_err)
3409 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003410 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003411 return err;
3412}
3413
Chris Masond352ac62008-09-29 15:18:18 -04003414/* helper for btfs_get_extent. Given an existing extent in the tree,
3415 * and an extent that you want to insert, deal with overlap and insert
3416 * the new extent into the tree.
3417 */
Chris Mason3b951512008-04-17 11:29:12 -04003418static int merge_extent_mapping(struct extent_map_tree *em_tree,
3419 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003420 struct extent_map *em,
3421 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003422{
3423 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003424
Chris Masone6dcd2d2008-07-17 12:53:50 -04003425 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3426 start_diff = map_start - em->start;
3427 em->start = map_start;
3428 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003429 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3430 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003431 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003432 em->block_len -= start_diff;
3433 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003434 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003435}
3436
Chris Masonc8b97812008-10-29 14:49:59 -04003437static noinline int uncompress_inline(struct btrfs_path *path,
3438 struct inode *inode, struct page *page,
3439 size_t pg_offset, u64 extent_offset,
3440 struct btrfs_file_extent_item *item)
3441{
3442 int ret;
3443 struct extent_buffer *leaf = path->nodes[0];
3444 char *tmp;
3445 size_t max_size;
3446 unsigned long inline_size;
3447 unsigned long ptr;
3448
3449 WARN_ON(pg_offset != 0);
3450 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3451 inline_size = btrfs_file_extent_inline_item_len(leaf,
3452 btrfs_item_nr(leaf, path->slots[0]));
3453 tmp = kmalloc(inline_size, GFP_NOFS);
3454 ptr = btrfs_file_extent_inline_start(item);
3455
3456 read_extent_buffer(leaf, tmp, ptr, inline_size);
3457
3458 max_size = min(PAGE_CACHE_SIZE, max_size);
3459 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3460 inline_size, max_size);
3461 if (ret) {
3462 char *kaddr = kmap_atomic(page, KM_USER0);
3463 unsigned long copy_size = min_t(u64,
3464 PAGE_CACHE_SIZE - pg_offset,
3465 max_size - extent_offset);
3466 memset(kaddr + pg_offset, 0, copy_size);
3467 kunmap_atomic(kaddr, KM_USER0);
3468 }
3469 kfree(tmp);
3470 return 0;
3471}
3472
Chris Masond352ac62008-09-29 15:18:18 -04003473/*
3474 * a bit scary, this does extent mapping from logical file offset to the disk.
3475 * the ugly parts come from merging extents from the disk with the
3476 * in-ram representation. This gets more complex because of the data=ordered code,
3477 * where the in-ram extents might be locked pending data=ordered completion.
3478 *
3479 * This also copies inline extents directly into the page.
3480 */
Chris Masona52d9a82007-08-27 16:49:44 -04003481struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003482 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003483 int create)
3484{
3485 int ret;
3486 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003487 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003488 u64 extent_start = 0;
3489 u64 extent_end = 0;
3490 u64 objectid = inode->i_ino;
3491 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003492 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003493 struct btrfs_root *root = BTRFS_I(inode)->root;
3494 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003495 struct extent_buffer *leaf;
3496 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003497 struct extent_map *em = NULL;
3498 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003499 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003500 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003501 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003502
Chris Masona52d9a82007-08-27 16:49:44 -04003503again:
Chris Masond1310b22008-01-24 16:13:08 -05003504 spin_lock(&em_tree->lock);
3505 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003506 if (em)
3507 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003508 spin_unlock(&em_tree->lock);
3509
Chris Masona52d9a82007-08-27 16:49:44 -04003510 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003511 if (em->start > start || em->start + em->len <= start)
3512 free_extent_map(em);
3513 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003514 free_extent_map(em);
3515 else
3516 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003517 }
Chris Masond1310b22008-01-24 16:13:08 -05003518 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003519 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003520 err = -ENOMEM;
3521 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003522 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003523 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003524 em->start = EXTENT_MAP_HOLE;
3525 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003526 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003527
3528 if (!path) {
3529 path = btrfs_alloc_path();
3530 BUG_ON(!path);
3531 }
3532
Chris Mason179e29e2007-11-01 11:28:41 -04003533 ret = btrfs_lookup_file_extent(trans, root, path,
3534 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003535 if (ret < 0) {
3536 err = ret;
3537 goto out;
3538 }
3539
3540 if (ret != 0) {
3541 if (path->slots[0] == 0)
3542 goto not_found;
3543 path->slots[0]--;
3544 }
3545
Chris Mason5f39d392007-10-15 16:14:19 -04003546 leaf = path->nodes[0];
3547 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003548 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003549 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003550 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3551 found_type = btrfs_key_type(&found_key);
3552 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003553 found_type != BTRFS_EXTENT_DATA_KEY) {
3554 goto not_found;
3555 }
3556
Chris Mason5f39d392007-10-15 16:14:19 -04003557 found_type = btrfs_file_extent_type(leaf, item);
3558 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003559 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04003560 if (found_type == BTRFS_FILE_EXTENT_REG ||
3561 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04003562 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04003563 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04003564 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3565 size_t size;
3566 size = btrfs_file_extent_inline_len(leaf, item);
3567 extent_end = (extent_start + size + root->sectorsize - 1) &
3568 ~((u64)root->sectorsize - 1);
3569 }
3570
3571 if (start >= extent_end) {
3572 path->slots[0]++;
3573 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3574 ret = btrfs_next_leaf(root, path);
3575 if (ret < 0) {
3576 err = ret;
3577 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003578 }
Yan Zheng9036c102008-10-30 14:19:41 -04003579 if (ret > 0)
3580 goto not_found;
3581 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04003582 }
Yan Zheng9036c102008-10-30 14:19:41 -04003583 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3584 if (found_key.objectid != objectid ||
3585 found_key.type != BTRFS_EXTENT_DATA_KEY)
3586 goto not_found;
3587 if (start + len <= found_key.offset)
3588 goto not_found;
3589 em->start = start;
3590 em->len = found_key.offset - start;
3591 goto not_found_em;
3592 }
3593
Yan Zhengd899e052008-10-30 14:25:28 -04003594 if (found_type == BTRFS_FILE_EXTENT_REG ||
3595 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04003596 em->start = extent_start;
3597 em->len = extent_end - extent_start;
Chris Masondb945352007-10-15 16:15:53 -04003598 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3599 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003600 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04003601 goto insert;
3602 }
Chris Masonc8b97812008-10-29 14:49:59 -04003603 if (compressed) {
3604 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3605 em->block_start = bytenr;
3606 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
3607 item);
3608 } else {
3609 bytenr += btrfs_file_extent_offset(leaf, item);
3610 em->block_start = bytenr;
3611 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04003612 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
3613 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04003614 }
Chris Masona52d9a82007-08-27 16:49:44 -04003615 goto insert;
3616 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003617 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04003618 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04003619 size_t size;
3620 size_t extent_offset;
3621 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04003622
Chris Masona52d9a82007-08-27 16:49:44 -04003623 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04003624 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04003625 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04003626 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04003627 goto out;
3628 }
3629
Yan Zheng9036c102008-10-30 14:19:41 -04003630 size = btrfs_file_extent_inline_len(leaf, item);
3631 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05003632 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04003633 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04003634 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05003635 em->len = (copy_size + root->sectorsize - 1) &
3636 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003637 if (compressed)
3638 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04003639 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04003640 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04003641 if (btrfs_file_extent_compression(leaf, item) ==
3642 BTRFS_COMPRESS_ZLIB) {
3643 ret = uncompress_inline(path, inode, page,
3644 pg_offset,
3645 extent_offset, item);
3646 BUG_ON(ret);
3647 } else {
3648 map = kmap(page);
3649 read_extent_buffer(leaf, map + pg_offset, ptr,
3650 copy_size);
3651 kunmap(page);
3652 }
Chris Mason179e29e2007-11-01 11:28:41 -04003653 flush_dcache_page(page);
3654 } else if (create && PageUptodate(page)) {
3655 if (!trans) {
3656 kunmap(page);
3657 free_extent_map(em);
3658 em = NULL;
3659 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04003660 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04003661 goto again;
3662 }
Chris Masonc8b97812008-10-29 14:49:59 -04003663 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05003664 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04003665 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003666 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04003667 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04003668 }
Chris Masond1310b22008-01-24 16:13:08 -05003669 set_extent_uptodate(io_tree, em->start,
3670 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003671 goto insert;
3672 } else {
3673 printk("unkknown found_type %d\n", found_type);
3674 WARN_ON(1);
3675 }
3676not_found:
3677 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05003678 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003679not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04003680 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04003681 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003682insert:
3683 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05003684 if (em->start > start || extent_map_end(em) <= start) {
3685 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04003686 err = -EIO;
3687 goto out;
3688 }
Chris Masond1310b22008-01-24 16:13:08 -05003689
3690 err = 0;
3691 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003692 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003693 /* it is possible that someone inserted the extent into the tree
3694 * while we had the lock dropped. It is also possible that
3695 * an overlapping map exists in the tree
3696 */
Chris Masona52d9a82007-08-27 16:49:44 -04003697 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04003698 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003699
3700 ret = 0;
3701
Chris Mason3b951512008-04-17 11:29:12 -04003702 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04003703 if (existing && (existing->start > start ||
3704 existing->start + existing->len <= start)) {
3705 free_extent_map(existing);
3706 existing = NULL;
3707 }
Chris Mason3b951512008-04-17 11:29:12 -04003708 if (!existing) {
3709 existing = lookup_extent_mapping(em_tree, em->start,
3710 em->len);
3711 if (existing) {
3712 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003713 em, start,
3714 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04003715 free_extent_map(existing);
3716 if (err) {
3717 free_extent_map(em);
3718 em = NULL;
3719 }
3720 } else {
3721 err = -EIO;
3722 printk("failing to insert %Lu %Lu\n",
3723 start, len);
3724 free_extent_map(em);
3725 em = NULL;
3726 }
3727 } else {
3728 free_extent_map(em);
3729 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003730 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04003731 }
Chris Masona52d9a82007-08-27 16:49:44 -04003732 }
Chris Masond1310b22008-01-24 16:13:08 -05003733 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003734out:
Chris Masonf4219502008-07-22 11:18:09 -04003735 if (path)
3736 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04003737 if (trans) {
3738 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003739 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04003740 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003741 }
Chris Masona52d9a82007-08-27 16:49:44 -04003742 }
Chris Masona52d9a82007-08-27 16:49:44 -04003743 if (err) {
3744 free_extent_map(em);
3745 WARN_ON(1);
3746 return ERR_PTR(err);
3747 }
3748 return em;
3749}
3750
Chris Mason16432982008-04-10 10:23:21 -04003751static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3752 const struct iovec *iov, loff_t offset,
3753 unsigned long nr_segs)
3754{
Chris Masone1c4b742008-04-22 13:26:46 -04003755 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04003756}
3757
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003758static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04003759{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003760 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003761}
3762
Chris Mason9ebefb182007-06-15 13:50:00 -04003763int btrfs_readpage(struct file *file, struct page *page)
3764{
Chris Masond1310b22008-01-24 16:13:08 -05003765 struct extent_io_tree *tree;
3766 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003767 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003768}
Chris Mason1832a6d2007-12-21 16:27:21 -05003769
Chris Mason39279cc2007-06-12 06:35:45 -04003770static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3771{
Chris Masond1310b22008-01-24 16:13:08 -05003772 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04003773
3774
3775 if (current->flags & PF_MEMALLOC) {
3776 redirty_page_for_writepage(wbc, page);
3777 unlock_page(page);
3778 return 0;
3779 }
Chris Masond1310b22008-01-24 16:13:08 -05003780 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003781 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3782}
Chris Mason39279cc2007-06-12 06:35:45 -04003783
Chris Masonf4219502008-07-22 11:18:09 -04003784int btrfs_writepages(struct address_space *mapping,
3785 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04003786{
Chris Masond1310b22008-01-24 16:13:08 -05003787 struct extent_io_tree *tree;
3788 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003789 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3790}
3791
Chris Mason3ab2fb52007-11-08 10:59:22 -05003792static int
3793btrfs_readpages(struct file *file, struct address_space *mapping,
3794 struct list_head *pages, unsigned nr_pages)
3795{
Chris Masond1310b22008-01-24 16:13:08 -05003796 struct extent_io_tree *tree;
3797 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003798 return extent_readpages(tree, mapping, pages, nr_pages,
3799 btrfs_get_extent);
3800}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003801static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003802{
Chris Masond1310b22008-01-24 16:13:08 -05003803 struct extent_io_tree *tree;
3804 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003805 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003806
Chris Masond1310b22008-01-24 16:13:08 -05003807 tree = &BTRFS_I(page->mapping->host)->io_tree;
3808 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003809 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003810 if (ret == 1) {
3811 ClearPagePrivate(page);
3812 set_page_private(page, 0);
3813 page_cache_release(page);
3814 }
3815 return ret;
3816}
Chris Mason39279cc2007-06-12 06:35:45 -04003817
Chris Masone6dcd2d2008-07-17 12:53:50 -04003818static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3819{
Chris Mason98509cf2008-09-11 15:51:43 -04003820 if (PageWriteback(page) || PageDirty(page))
3821 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003822 return __btrfs_releasepage(page, gfp_flags);
3823}
3824
Chris Masona52d9a82007-08-27 16:49:44 -04003825static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3826{
Chris Masond1310b22008-01-24 16:13:08 -05003827 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003828 struct btrfs_ordered_extent *ordered;
3829 u64 page_start = page_offset(page);
3830 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003831
Chris Masone6dcd2d2008-07-17 12:53:50 -04003832 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003833 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003834 if (offset) {
3835 btrfs_releasepage(page, GFP_NOFS);
3836 return;
3837 }
3838
3839 lock_extent(tree, page_start, page_end, GFP_NOFS);
3840 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3841 page_offset(page));
3842 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003843 /*
3844 * IO on this page will never be started, so we need
3845 * to account for any ordered extents now
3846 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003847 clear_extent_bit(tree, page_start, page_end,
3848 EXTENT_DIRTY | EXTENT_DELALLOC |
3849 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003850 btrfs_finish_ordered_io(page->mapping->host,
3851 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003852 btrfs_put_ordered_extent(ordered);
3853 lock_extent(tree, page_start, page_end, GFP_NOFS);
3854 }
3855 clear_extent_bit(tree, page_start, page_end,
3856 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3857 EXTENT_ORDERED,
3858 1, 1, GFP_NOFS);
3859 __btrfs_releasepage(page, GFP_NOFS);
3860
Chris Mason4a096752008-07-21 10:29:44 -04003861 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003862 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003863 ClearPagePrivate(page);
3864 set_page_private(page, 0);
3865 page_cache_release(page);
3866 }
Chris Mason39279cc2007-06-12 06:35:45 -04003867}
3868
Chris Mason9ebefb182007-06-15 13:50:00 -04003869/*
3870 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3871 * called from a page fault handler when a page is first dirtied. Hence we must
3872 * be careful to check for EOF conditions here. We set the page up correctly
3873 * for a written page which means we get ENOSPC checking when writing into
3874 * holes and correct delalloc and unwritten extent mapping on filesystems that
3875 * support these features.
3876 *
3877 * We are not allowed to take the i_mutex here so we have to play games to
3878 * protect against truncate races as the page could now be beyond EOF. Because
3879 * vmtruncate() writes the inode size before removing pages, once we have the
3880 * page lock we can determine safely if the page is beyond EOF. If it is not
3881 * beyond EOF, then the page is guaranteed safe against truncation until we
3882 * unlock the page.
3883 */
3884int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3885{
Chris Mason6da6aba2007-12-18 16:15:09 -05003886 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003887 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003888 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3889 struct btrfs_ordered_extent *ordered;
3890 char *kaddr;
3891 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003892 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003893 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003894 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003895 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003896
Chris Mason1832a6d2007-12-21 16:27:21 -05003897 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003898 if (ret)
3899 goto out;
3900
3901 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003902again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003903 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003904 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003905 page_start = page_offset(page);
3906 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003907
Chris Mason9ebefb182007-06-15 13:50:00 -04003908 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003909 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003910 /* page got truncated out from underneath us */
3911 goto out_unlock;
3912 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003913 wait_on_page_writeback(page);
3914
3915 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3916 set_page_extent_mapped(page);
3917
Chris Masoneb84ae02008-07-17 13:53:27 -04003918 /*
3919 * we can't set the delalloc bits if there are pending ordered
3920 * extents. Drop our locks and wait for them to finish
3921 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003922 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3923 if (ordered) {
3924 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3925 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003926 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003927 btrfs_put_ordered_extent(ordered);
3928 goto again;
3929 }
3930
Chris Masonea8c2812008-08-04 23:17:27 -04003931 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003932 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003933
3934 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003935 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003936 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003937 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003938 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003939
Chris Masone6dcd2d2008-07-17 12:53:50 -04003940 if (zero_start != PAGE_CACHE_SIZE) {
3941 kaddr = kmap(page);
3942 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3943 flush_dcache_page(page);
3944 kunmap(page);
3945 }
Chris Mason247e7432008-07-17 12:53:51 -04003946 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003947 set_page_dirty(page);
3948 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003949
3950out_unlock:
3951 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003952out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003953 return ret;
3954}
3955
Chris Mason39279cc2007-06-12 06:35:45 -04003956static void btrfs_truncate(struct inode *inode)
3957{
3958 struct btrfs_root *root = BTRFS_I(inode)->root;
3959 int ret;
3960 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003961 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003962 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003963
3964 if (!S_ISREG(inode->i_mode))
3965 return;
3966 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3967 return;
3968
3969 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003970 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003971
Chris Mason39279cc2007-06-12 06:35:45 -04003972 trans = btrfs_start_transaction(root, 1);
3973 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003974 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003975
Josef Bacik7b128762008-07-24 12:17:14 -04003976 ret = btrfs_orphan_add(trans, inode);
3977 if (ret)
3978 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003979 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04003980 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05003981 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003982 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003983
Josef Bacik7b128762008-07-24 12:17:14 -04003984 ret = btrfs_orphan_del(trans, inode);
3985 BUG_ON(ret);
3986
3987out:
3988 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003989 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003990 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003991 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003992}
3993
Sven Wegener3b963622008-06-09 21:57:42 -04003994/*
3995 * Invalidate a single dcache entry at the root of the filesystem.
3996 * Needed after creation of snapshot or subvolume.
3997 */
3998void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3999 int namelen)
4000{
4001 struct dentry *alias, *entry;
4002 struct qstr qstr;
4003
4004 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
4005 if (alias) {
4006 qstr.name = name;
4007 qstr.len = namelen;
4008 /* change me if btrfs ever gets a d_hash operation */
4009 qstr.hash = full_name_hash(qstr.name, qstr.len);
4010 entry = d_lookup(alias, &qstr);
4011 dput(alias);
4012 if (entry) {
4013 d_invalidate(entry);
4014 dput(entry);
4015 }
4016 }
4017}
4018
Chris Masond352ac62008-09-29 15:18:18 -04004019/*
4020 * create a new subvolume directory/inode (helper for the ioctl).
4021 */
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004022int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004023 struct btrfs_trans_handle *trans, u64 new_dirid,
4024 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04004025{
Chris Mason39279cc2007-06-12 06:35:45 -04004026 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004027 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004028 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004029
Josef Bacikaec74772008-07-24 12:12:38 -04004030 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004031 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004032 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004033 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004034 inode->i_op = &btrfs_dir_inode_operations;
4035 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04004036 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004037
Chris Mason39279cc2007-06-12 06:35:45 -04004038 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004039 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004040
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004041 error = btrfs_update_inode(trans, new_root, inode);
4042 if (error)
4043 return error;
4044
Yan Zhengd899e052008-10-30 14:25:28 -04004045 atomic_inc(&inode->i_count);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004046 d_instantiate(dentry, inode);
4047 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004048}
4049
Chris Masond352ac62008-09-29 15:18:18 -04004050/* helper function for file defrag and space balancing. This
4051 * forces readahead on a given range of bytes in an inode
4052 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004053unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004054 struct file_ra_state *ra, struct file *file,
4055 pgoff_t offset, pgoff_t last_index)
4056{
Chris Mason8e7bf942008-04-28 09:02:36 -04004057 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004058
Chris Mason86479a02007-09-10 19:58:16 -04004059 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4060 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004061}
4062
Chris Mason39279cc2007-06-12 06:35:45 -04004063struct inode *btrfs_alloc_inode(struct super_block *sb)
4064{
4065 struct btrfs_inode *ei;
4066
4067 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4068 if (!ei)
4069 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004070 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004071 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004072 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004073 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4074 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004075 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004076 return &ei->vfs_inode;
4077}
4078
4079void btrfs_destroy_inode(struct inode *inode)
4080{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004081 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004082 WARN_ON(!list_empty(&inode->i_dentry));
4083 WARN_ON(inode->i_data.nrpages);
4084
Josef Bacik33268ea2008-07-24 12:16:36 -04004085 if (BTRFS_I(inode)->i_acl &&
4086 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4087 posix_acl_release(BTRFS_I(inode)->i_acl);
4088 if (BTRFS_I(inode)->i_default_acl &&
4089 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4090 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4091
Yanbcc63ab2008-07-30 16:29:20 -04004092 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004093 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4094 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4095 " list\n", inode->i_ino);
4096 dump_stack();
4097 }
Yanbcc63ab2008-07-30 16:29:20 -04004098 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004099
Chris Masone6dcd2d2008-07-17 12:53:50 -04004100 while(1) {
4101 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4102 if (!ordered)
4103 break;
4104 else {
4105 printk("found ordered extent %Lu %Lu\n",
4106 ordered->file_offset, ordered->len);
4107 btrfs_remove_ordered_extent(inode, ordered);
4108 btrfs_put_ordered_extent(ordered);
4109 btrfs_put_ordered_extent(ordered);
4110 }
4111 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004112 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004113 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4114}
4115
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004116static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004117{
4118 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4119
4120 inode_init_once(&ei->vfs_inode);
4121}
4122
4123void btrfs_destroy_cachep(void)
4124{
4125 if (btrfs_inode_cachep)
4126 kmem_cache_destroy(btrfs_inode_cachep);
4127 if (btrfs_trans_handle_cachep)
4128 kmem_cache_destroy(btrfs_trans_handle_cachep);
4129 if (btrfs_transaction_cachep)
4130 kmem_cache_destroy(btrfs_transaction_cachep);
4131 if (btrfs_bit_radix_cachep)
4132 kmem_cache_destroy(btrfs_bit_radix_cachep);
4133 if (btrfs_path_cachep)
4134 kmem_cache_destroy(btrfs_path_cachep);
4135}
4136
Chris Mason86479a02007-09-10 19:58:16 -04004137struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004138 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004139 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004140{
4141 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004142 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004143}
4144
Chris Mason39279cc2007-06-12 06:35:45 -04004145int btrfs_init_cachep(void)
4146{
Chris Mason86479a02007-09-10 19:58:16 -04004147 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004148 sizeof(struct btrfs_inode),
4149 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004150 if (!btrfs_inode_cachep)
4151 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004152 btrfs_trans_handle_cachep =
4153 btrfs_cache_create("btrfs_trans_handle_cache",
4154 sizeof(struct btrfs_trans_handle),
4155 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004156 if (!btrfs_trans_handle_cachep)
4157 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004158 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004159 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004160 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004161 if (!btrfs_transaction_cachep)
4162 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004163 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004164 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004165 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004166 if (!btrfs_path_cachep)
4167 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004168 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004169 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004170 if (!btrfs_bit_radix_cachep)
4171 goto fail;
4172 return 0;
4173fail:
4174 btrfs_destroy_cachep();
4175 return -ENOMEM;
4176}
4177
4178static int btrfs_getattr(struct vfsmount *mnt,
4179 struct dentry *dentry, struct kstat *stat)
4180{
4181 struct inode *inode = dentry->d_inode;
4182 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05004183 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004184 stat->blocks = (inode_get_bytes(inode) +
4185 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004186 return 0;
4187}
4188
4189static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
4190 struct inode * new_dir,struct dentry *new_dentry)
4191{
4192 struct btrfs_trans_handle *trans;
4193 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4194 struct inode *new_inode = new_dentry->d_inode;
4195 struct inode *old_inode = old_dentry->d_inode;
4196 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004197 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004198 int ret;
4199
4200 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4201 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4202 return -ENOTEMPTY;
4203 }
Chris Mason5f39d392007-10-15 16:14:19 -04004204
Chris Mason1832a6d2007-12-21 16:27:21 -05004205 ret = btrfs_check_free_space(root, 1, 0);
4206 if (ret)
4207 goto out_unlock;
4208
Chris Mason39279cc2007-06-12 06:35:45 -04004209 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004210
Chris Mason39279cc2007-06-12 06:35:45 -04004211 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004212
Chris Masone02119d2008-09-05 16:13:11 -04004213 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004214 old_dir->i_ctime = old_dir->i_mtime = ctime;
4215 new_dir->i_ctime = new_dir->i_mtime = ctime;
4216 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004217
Chris Masone02119d2008-09-05 16:13:11 -04004218 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4219 old_dentry->d_name.name,
4220 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004221 if (ret)
4222 goto out_fail;
4223
4224 if (new_inode) {
4225 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004226 ret = btrfs_unlink_inode(trans, root, new_dir,
4227 new_dentry->d_inode,
4228 new_dentry->d_name.name,
4229 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004230 if (ret)
4231 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004232 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004233 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004234 if (ret)
4235 goto out_fail;
4236 }
Chris Masone02119d2008-09-05 16:13:11 -04004237
Chris Mason39279cc2007-06-12 06:35:45 -04004238 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04004239 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004240 if (ret)
4241 goto out_fail;
4242
Chris Masone02119d2008-09-05 16:13:11 -04004243 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4244 old_inode, new_dentry->d_name.name,
4245 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004246 if (ret)
4247 goto out_fail;
4248
4249out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004250 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004251out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004252 return ret;
4253}
4254
Chris Masond352ac62008-09-29 15:18:18 -04004255/*
4256 * some fairly slow code that needs optimization. This walks the list
4257 * of all the inodes with pending delalloc and forces them to disk.
4258 */
Chris Masonea8c2812008-08-04 23:17:27 -04004259int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4260{
4261 struct list_head *head = &root->fs_info->delalloc_inodes;
4262 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004263 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004264 unsigned long flags;
4265
4266 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4267 while(!list_empty(head)) {
4268 binode = list_entry(head->next, struct btrfs_inode,
4269 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004270 inode = igrab(&binode->vfs_inode);
4271 if (!inode)
4272 list_del_init(&binode->delalloc_inodes);
Chris Masonea8c2812008-08-04 23:17:27 -04004273 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004274 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004275 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004276 iput(inode);
4277 }
4278 cond_resched();
Chris Masonea8c2812008-08-04 23:17:27 -04004279 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4280 }
4281 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason8c8bee12008-09-29 11:19:10 -04004282
4283 /* the filemap_flush will queue IO into the worker threads, but
4284 * we have to make sure the IO is actually started and that
4285 * ordered extents get created before we return
4286 */
4287 atomic_inc(&root->fs_info->async_submit_draining);
4288 while(atomic_read(&root->fs_info->nr_async_submits)) {
4289 wait_event(root->fs_info->async_submit_wait,
4290 (atomic_read(&root->fs_info->nr_async_submits) == 0));
4291 }
4292 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004293 return 0;
4294}
4295
Chris Mason39279cc2007-06-12 06:35:45 -04004296static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4297 const char *symname)
4298{
4299 struct btrfs_trans_handle *trans;
4300 struct btrfs_root *root = BTRFS_I(dir)->root;
4301 struct btrfs_path *path;
4302 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004303 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004304 int err;
4305 int drop_inode = 0;
4306 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004307 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004308 int name_len;
4309 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004310 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004311 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004312 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004313 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004314
4315 name_len = strlen(symname) + 1;
4316 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4317 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004318
Chris Mason1832a6d2007-12-21 16:27:21 -05004319 err = btrfs_check_free_space(root, 1, 0);
4320 if (err)
4321 goto out_fail;
4322
Chris Mason39279cc2007-06-12 06:35:45 -04004323 trans = btrfs_start_transaction(root, 1);
4324 btrfs_set_trans_block_group(trans, dir);
4325
4326 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4327 if (err) {
4328 err = -ENOSPC;
4329 goto out_unlock;
4330 }
4331
Josef Bacikaec74772008-07-24 12:12:38 -04004332 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004333 dentry->d_name.len,
4334 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004335 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4336 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004337 err = PTR_ERR(inode);
4338 if (IS_ERR(inode))
4339 goto out_unlock;
4340
Josef Bacik33268ea2008-07-24 12:16:36 -04004341 err = btrfs_init_acl(inode, dir);
4342 if (err) {
4343 drop_inode = 1;
4344 goto out_unlock;
4345 }
4346
Chris Mason39279cc2007-06-12 06:35:45 -04004347 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004348 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004349 if (err)
4350 drop_inode = 1;
4351 else {
4352 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004353 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004354 inode->i_fop = &btrfs_file_operations;
4355 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004356 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004357 }
4358 dir->i_sb->s_dirt = 1;
4359 btrfs_update_inode_block_group(trans, inode);
4360 btrfs_update_inode_block_group(trans, dir);
4361 if (drop_inode)
4362 goto out_unlock;
4363
4364 path = btrfs_alloc_path();
4365 BUG_ON(!path);
4366 key.objectid = inode->i_ino;
4367 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004368 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4369 datasize = btrfs_file_extent_calc_inline_size(name_len);
4370 err = btrfs_insert_empty_item(trans, root, path, &key,
4371 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004372 if (err) {
4373 drop_inode = 1;
4374 goto out_unlock;
4375 }
Chris Mason5f39d392007-10-15 16:14:19 -04004376 leaf = path->nodes[0];
4377 ei = btrfs_item_ptr(leaf, path->slots[0],
4378 struct btrfs_file_extent_item);
4379 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4380 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004381 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004382 btrfs_set_file_extent_encryption(leaf, ei, 0);
4383 btrfs_set_file_extent_compression(leaf, ei, 0);
4384 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4385 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4386
Chris Mason39279cc2007-06-12 06:35:45 -04004387 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004388 write_extent_buffer(leaf, symname, ptr, name_len);
4389 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004390 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004391
Chris Mason39279cc2007-06-12 06:35:45 -04004392 inode->i_op = &btrfs_symlink_inode_operations;
4393 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004394 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004395 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004396 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004397 err = btrfs_update_inode(trans, root, inode);
4398 if (err)
4399 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004400
4401out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004402 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004403 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004404out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004405 if (drop_inode) {
4406 inode_dec_link_count(inode);
4407 iput(inode);
4408 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004409 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004410 return err;
4411}
Chris Mason16432982008-04-10 10:23:21 -04004412
Yan Zhengd899e052008-10-30 14:25:28 -04004413static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4414 u64 alloc_hint, int mode)
4415{
4416 struct btrfs_trans_handle *trans;
4417 struct btrfs_root *root = BTRFS_I(inode)->root;
4418 struct btrfs_key ins;
4419 u64 alloc_size;
4420 u64 cur_offset = start;
4421 u64 num_bytes = end - start;
4422 int ret = 0;
4423
4424 trans = btrfs_join_transaction(root, 1);
4425 BUG_ON(!trans);
4426 btrfs_set_trans_block_group(trans, inode);
4427
4428 while (num_bytes > 0) {
4429 alloc_size = min(num_bytes, root->fs_info->max_extent);
4430 ret = btrfs_reserve_extent(trans, root, alloc_size,
4431 root->sectorsize, 0, alloc_hint,
4432 (u64)-1, &ins, 1);
4433 if (ret) {
4434 WARN_ON(1);
4435 goto out;
4436 }
4437 ret = insert_reserved_file_extent(trans, inode,
4438 cur_offset, ins.objectid,
4439 ins.offset, ins.offset,
4440 ins.offset, 0, 0, 0,
4441 BTRFS_FILE_EXTENT_PREALLOC);
4442 BUG_ON(ret);
4443 num_bytes -= ins.offset;
4444 cur_offset += ins.offset;
4445 alloc_hint = ins.objectid + ins.offset;
4446 }
4447out:
4448 if (cur_offset > start) {
4449 inode->i_ctime = CURRENT_TIME;
4450 btrfs_set_flag(inode, PREALLOC);
4451 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4452 cur_offset > i_size_read(inode))
4453 btrfs_i_size_write(inode, cur_offset);
4454 ret = btrfs_update_inode(trans, root, inode);
4455 BUG_ON(ret);
4456 }
4457
4458 btrfs_end_transaction(trans, root);
4459 return ret;
4460}
4461
4462static long btrfs_fallocate(struct inode *inode, int mode,
4463 loff_t offset, loff_t len)
4464{
4465 u64 cur_offset;
4466 u64 last_byte;
4467 u64 alloc_start;
4468 u64 alloc_end;
4469 u64 alloc_hint = 0;
4470 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4471 struct extent_map *em;
4472 int ret;
4473
4474 alloc_start = offset & ~mask;
4475 alloc_end = (offset + len + mask) & ~mask;
4476
4477 mutex_lock(&inode->i_mutex);
4478 if (alloc_start > inode->i_size) {
4479 ret = btrfs_cont_expand(inode, alloc_start);
4480 if (ret)
4481 goto out;
4482 }
4483
4484 while (1) {
4485 struct btrfs_ordered_extent *ordered;
4486 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4487 alloc_end - 1, GFP_NOFS);
4488 ordered = btrfs_lookup_first_ordered_extent(inode,
4489 alloc_end - 1);
4490 if (ordered &&
4491 ordered->file_offset + ordered->len > alloc_start &&
4492 ordered->file_offset < alloc_end) {
4493 btrfs_put_ordered_extent(ordered);
4494 unlock_extent(&BTRFS_I(inode)->io_tree,
4495 alloc_start, alloc_end - 1, GFP_NOFS);
4496 btrfs_wait_ordered_range(inode, alloc_start,
4497 alloc_end - alloc_start);
4498 } else {
4499 if (ordered)
4500 btrfs_put_ordered_extent(ordered);
4501 break;
4502 }
4503 }
4504
4505 cur_offset = alloc_start;
4506 while (1) {
4507 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4508 alloc_end - cur_offset, 0);
4509 BUG_ON(IS_ERR(em) || !em);
4510 last_byte = min(extent_map_end(em), alloc_end);
4511 last_byte = (last_byte + mask) & ~mask;
4512 if (em->block_start == EXTENT_MAP_HOLE) {
4513 ret = prealloc_file_range(inode, cur_offset,
4514 last_byte, alloc_hint, mode);
4515 if (ret < 0) {
4516 free_extent_map(em);
4517 break;
4518 }
4519 }
4520 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4521 alloc_hint = em->block_start;
4522 free_extent_map(em);
4523
4524 cur_offset = last_byte;
4525 if (cur_offset >= alloc_end) {
4526 ret = 0;
4527 break;
4528 }
4529 }
4530 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4531 GFP_NOFS);
4532out:
4533 mutex_unlock(&inode->i_mutex);
4534 return ret;
4535}
4536
Chris Masone6dcd2d2008-07-17 12:53:50 -04004537static int btrfs_set_page_dirty(struct page *page)
4538{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004539 return __set_page_dirty_nobuffers(page);
4540}
4541
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004542static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004543{
4544 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4545 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004546 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004547}
Chris Mason39279cc2007-06-12 06:35:45 -04004548
4549static struct inode_operations btrfs_dir_inode_operations = {
4550 .lookup = btrfs_lookup,
4551 .create = btrfs_create,
4552 .unlink = btrfs_unlink,
4553 .link = btrfs_link,
4554 .mkdir = btrfs_mkdir,
4555 .rmdir = btrfs_rmdir,
4556 .rename = btrfs_rename,
4557 .symlink = btrfs_symlink,
4558 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004559 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004560 .setxattr = btrfs_setxattr,
4561 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004562 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004563 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004564 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004565};
Chris Mason39279cc2007-06-12 06:35:45 -04004566static struct inode_operations btrfs_dir_ro_inode_operations = {
4567 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05004568 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004569};
Chris Mason39279cc2007-06-12 06:35:45 -04004570static struct file_operations btrfs_dir_file_operations = {
4571 .llseek = generic_file_llseek,
4572 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004573 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004574 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004575#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004576 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004577#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004578 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004579 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004580};
4581
Chris Masond1310b22008-01-24 16:13:08 -05004582static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04004583 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05004584 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004585 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004586 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004587 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04004588 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04004589 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05004590 .set_bit_hook = btrfs_set_bit_hook,
4591 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004592};
4593
Chris Mason39279cc2007-06-12 06:35:45 -04004594static struct address_space_operations btrfs_aops = {
4595 .readpage = btrfs_readpage,
4596 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04004597 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05004598 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04004599 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04004600 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04004601 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04004602 .invalidatepage = btrfs_invalidatepage,
4603 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004604 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04004605};
4606
4607static struct address_space_operations btrfs_symlink_aops = {
4608 .readpage = btrfs_readpage,
4609 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04004610 .invalidatepage = btrfs_invalidatepage,
4611 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04004612};
4613
4614static struct inode_operations btrfs_file_inode_operations = {
4615 .truncate = btrfs_truncate,
4616 .getattr = btrfs_getattr,
4617 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004618 .setxattr = btrfs_setxattr,
4619 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004620 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004621 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004622 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04004623 .fallocate = btrfs_fallocate,
Chris Mason39279cc2007-06-12 06:35:45 -04004624};
Josef Bacik618e21d2007-07-11 10:18:17 -04004625static struct inode_operations btrfs_special_inode_operations = {
4626 .getattr = btrfs_getattr,
4627 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05004628 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004629 .setxattr = btrfs_setxattr,
4630 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04004631 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004632 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004633};
Chris Mason39279cc2007-06-12 06:35:45 -04004634static struct inode_operations btrfs_symlink_inode_operations = {
4635 .readlink = generic_readlink,
4636 .follow_link = page_follow_link_light,
4637 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05004638 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004639};