blob: 3e6f0568fdb43e0eda5ffe5b798e79e7dcfee1fb [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>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040048#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040049#include "compat.h"
50#include "tree-log.h"
Yan Zheng5b84e8d2008-10-09 11:46:19 -040051#include "ref-cache.h"
Chris Masonc8b97812008-10-29 14:49:59 -040052#include "compression.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
59static struct inode_operations btrfs_dir_inode_operations;
60static struct inode_operations btrfs_symlink_inode_operations;
61static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040062static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040063static struct inode_operations btrfs_file_inode_operations;
64static struct address_space_operations btrfs_aops;
65static struct address_space_operations btrfs_symlink_aops;
66static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050067static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040068
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
72struct kmem_cache *btrfs_bit_radix_cachep;
73struct kmem_cache *btrfs_path_cachep;
74
75#define S_SHIFT 12
76static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
77 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
78 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
79 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
80 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
81 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
82 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
83 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
84};
85
Josef Bacik7b128762008-07-24 12:17:14 -040086static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040087static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Josef Bacik7b128762008-07-24 12:17:14 -040088
Chris Masond352ac62008-09-29 15:18:18 -040089/*
90 * a very lame attempt at stopping writes when the FS is 85% full. There
91 * are countless ways this is incorrect, but it is better than nothing.
92 */
Chris Mason1832a6d2007-12-21 16:27:21 -050093int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
94 int for_del)
95{
Chris Masona2135012008-06-25 16:01:30 -040096 u64 total;
97 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050098 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040099 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -0500100 int ret = 0;
101
Chris Masona2135012008-06-25 16:01:30 -0400102 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
103 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
104 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -0500106 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -0500107 else
Chris Masonf9ef6602008-01-03 09:22:38 -0500108 thresh = total * 85;
109
110 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500111
Chris Mason1832a6d2007-12-21 16:27:21 -0500112 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
113 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400114 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500115 return ret;
116}
117
Chris Masond352ac62008-09-29 15:18:18 -0400118/*
Chris Masonc8b97812008-10-29 14:49:59 -0400119 * this does all the hard work for inserting an inline extent into
120 * the btree. The caller should have done a btrfs_drop_extents so that
121 * no overlapping inline items exist in the btree
122 */
123static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
124 struct btrfs_root *root, struct inode *inode,
125 u64 start, size_t size, size_t compressed_size,
126 struct page **compressed_pages)
127{
128 struct btrfs_key key;
129 struct btrfs_path *path;
130 struct extent_buffer *leaf;
131 struct page *page = NULL;
132 char *kaddr;
133 unsigned long ptr;
134 struct btrfs_file_extent_item *ei;
135 int err = 0;
136 int ret;
137 size_t cur_size = size;
138 size_t datasize;
139 unsigned long offset;
140 int use_compress = 0;
141
142 if (compressed_size && compressed_pages) {
143 use_compress = 1;
144 cur_size = compressed_size;
145 }
146
147 path = btrfs_alloc_path(); if (!path)
148 return -ENOMEM;
149
150 btrfs_set_trans_block_group(trans, inode);
151
152 key.objectid = inode->i_ino;
153 key.offset = start;
154 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
155 inode_add_bytes(inode, size);
156 datasize = btrfs_file_extent_calc_inline_size(cur_size);
157
158 inode_add_bytes(inode, size);
159 ret = btrfs_insert_empty_item(trans, root, path, &key,
160 datasize);
161 BUG_ON(ret);
162 if (ret) {
163 err = ret;
164 printk("got bad ret %d\n", ret);
165 goto fail;
166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
177 if (use_compress) {
178 struct page *cpage;
179 int i = 0;
180 while(compressed_size > 0) {
181 cpage = compressed_pages[i];
182 cur_size = min(compressed_size,
183 PAGE_CACHE_SIZE);
184
185 kaddr = kmap(cpage);
186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
187 kunmap(cpage);
188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
194 BTRFS_COMPRESS_ZLIB);
195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
199 kaddr = kmap_atomic(page, KM_USER0);
200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
202 kunmap_atomic(kaddr, KM_USER0);
203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
206 btrfs_free_path(path);
207
208 BTRFS_I(inode)->disk_i_size = inode->i_size;
209 btrfs_update_inode(trans, root, inode);
210 return 0;
211fail:
212 btrfs_free_path(path);
213 return err;
214}
215
216
217/*
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
221 */
222static int cow_file_range_inline(struct btrfs_trans_handle *trans,
223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
225 size_t compressed_size,
226 struct page **compressed_pages)
227{
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
233 u64 hint_byte;
234 u64 data_len = inline_len;
235 int ret;
236
237 if (compressed_size)
238 data_len = compressed_size;
239
240 if (start > 0 ||
241 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
242 (!compressed_size &&
243 (actual_end & (root->sectorsize - 1)) == 0) ||
244 end + 1 < isize ||
245 data_len > root->fs_info->max_inline) {
246 return 1;
247 }
248
Chris Masonc8b97812008-10-29 14:49:59 -0400249 ret = btrfs_drop_extents(trans, root, inode, start,
250 aligned_end, aligned_end, &hint_byte);
251 BUG_ON(ret);
252
253 if (isize > actual_end)
254 inline_len = min_t(u64, isize, actual_end);
255 ret = insert_inline_extent(trans, root, inode, start,
256 inline_len, compressed_size,
257 compressed_pages);
258 BUG_ON(ret);
259 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400260 return 0;
261}
262
263/*
Chris Masond352ac62008-09-29 15:18:18 -0400264 * when extent_io.c finds a delayed allocation range in the file,
265 * the call backs end up in this code. The basic idea is to
266 * allocate extents on disk for the range, and create ordered data structs
267 * in ram to track those extents.
Chris Masonc8b97812008-10-29 14:49:59 -0400268 *
269 * locked_page is the page that writepage had locked already. We use
270 * it to make sure we don't do extra locks or unlocks.
271 *
272 * *page_started is set to one if we unlock locked_page and do everything
273 * required to start IO on it. It may be clean and already done with
274 * IO when we return.
Chris Masond352ac62008-09-29 15:18:18 -0400275 */
Chris Masonc8b97812008-10-29 14:49:59 -0400276static int cow_file_range(struct inode *inode, struct page *locked_page,
277 u64 start, u64 end, int *page_started)
Chris Masonb888db22007-08-27 16:49:44 -0400278{
279 struct btrfs_root *root = BTRFS_I(inode)->root;
280 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400281 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400282 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400283 unsigned long ram_size;
284 u64 orig_start;
285 u64 disk_num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500286 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400287 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 actual_end;
Chris Masonbe20aa92007-12-17 20:14:01 -0500289 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400290 struct extent_map *em;
291 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
292 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400293 struct page **pages = NULL;
294 unsigned long nr_pages;
295 unsigned long nr_pages_ret = 0;
296 unsigned long total_compressed = 0;
297 unsigned long total_in = 0;
298 unsigned long max_compressed = 128 * 1024;
299 unsigned long max_uncompressed = 256 * 1024;
300 int i;
Yan Zheng80ff3852008-10-30 14:20:02 -0400301 int ordered_type;
Chris Masonc8b97812008-10-29 14:49:59 -0400302 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400303
Chris Masonf9295742008-07-17 12:54:14 -0400304 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400305 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500306 btrfs_set_trans_block_group(trans, inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400307 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500308
Chris Masonc8b97812008-10-29 14:49:59 -0400309 /*
310 * compression made this loop a bit ugly, but the basic idea is to
311 * compress some pages but keep the total size of the compressed
312 * extent relatively small. If compression is off, this goto target
313 * is never used.
314 */
315again:
316 will_compress = 0;
317 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
318 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
319
320 actual_end = min_t(u64, i_size_read(inode), end + 1);
321 total_compressed = actual_end - start;
322
323 /* we want to make sure that amount of ram required to uncompress
324 * an extent is reasonable, so we limit the total size in ram
325 * of a compressed extent to 256k
326 */
327 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400328 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500329 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400330 disk_num_bytes = num_bytes;
331 total_in = 0;
332 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400333
Chris Masonc8b97812008-10-29 14:49:59 -0400334 /* we do compression for mount -o compress and when the
335 * inode has not been flagged as nocompress
336 */
337 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
338 btrfs_test_opt(root, COMPRESS)) {
339 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400340 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400341
Chris Masonc8b97812008-10-29 14:49:59 -0400342 /* we want to make sure the amount of IO required to satisfy
343 * a random read is reasonably small, so we limit the size
344 * of a compressed extent to 128k
345 */
346 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
347 total_compressed, pages,
348 nr_pages, &nr_pages_ret,
349 &total_in,
350 &total_compressed,
351 max_compressed);
352
353 if (!ret) {
354 unsigned long offset = total_compressed &
355 (PAGE_CACHE_SIZE - 1);
356 struct page *page = pages[nr_pages_ret - 1];
357 char *kaddr;
358
359 /* zero the tail end of the last page, we might be
360 * sending it down to disk
361 */
362 if (offset) {
363 kaddr = kmap_atomic(page, KM_USER0);
364 memset(kaddr + offset, 0,
365 PAGE_CACHE_SIZE - offset);
366 kunmap_atomic(kaddr, KM_USER0);
367 }
368 will_compress = 1;
369 }
370 }
371 if (start == 0) {
372 /* lets try to make an inline extent */
373 if (ret || total_in < (end - start + 1)) {
374 /* we didn't compress the entire range, try
375 * to make an uncompressed inline extent. This
376 * is almost sure to fail, but maybe inline sizes
377 * will get bigger later
378 */
379 ret = cow_file_range_inline(trans, root, inode,
380 start, end, 0, NULL);
381 } else {
382 ret = cow_file_range_inline(trans, root, inode,
383 start, end,
384 total_compressed, pages);
385 }
386 if (ret == 0) {
387 extent_clear_unlock_delalloc(inode,
388 &BTRFS_I(inode)->io_tree,
389 start, end, NULL,
390 1, 1, 1);
391 *page_started = 1;
392 ret = 0;
393 goto free_pages_out;
394 }
395 }
396
397 if (will_compress) {
398 /*
399 * we aren't doing an inline extent round the compressed size
400 * up to a block size boundary so the allocator does sane
401 * things
402 */
403 total_compressed = (total_compressed + blocksize - 1) &
404 ~(blocksize - 1);
405
406 /*
407 * one last check to make sure the compression is really a
408 * win, compare the page count read with the blocks on disk
409 */
410 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
411 ~(PAGE_CACHE_SIZE - 1);
412 if (total_compressed >= total_in) {
413 will_compress = 0;
414 } else {
415 disk_num_bytes = total_compressed;
416 num_bytes = total_in;
417 }
418 }
419 if (!will_compress && pages) {
420 /*
421 * the compression code ran but failed to make things smaller,
422 * free any pages it allocated and our page pointer array
423 */
424 for (i = 0; i < nr_pages_ret; i++) {
425 page_cache_release(pages[i]);
426 }
427 kfree(pages);
428 pages = NULL;
429 total_compressed = 0;
430 nr_pages_ret = 0;
431
432 /* flag the file so we don't compress in the future */
433 btrfs_set_flag(inode, NOCOMPRESS);
434 }
435
436 BUG_ON(disk_num_bytes >
437 btrfs_super_total_bytes(&root->fs_info->super_copy));
438
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400439 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 while(disk_num_bytes > 0) {
442 unsigned long min_bytes;
443
444 /*
445 * the max size of a compressed extent is pretty small,
446 * make the code a little less complex by forcing
447 * the allocator to find a whole compressed extent at once
448 */
449 if (will_compress)
450 min_bytes = disk_num_bytes;
451 else
452 min_bytes = root->sectorsize;
453
454 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400455 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400456 min_bytes, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400457 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500458 if (ret) {
459 WARN_ON(1);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 goto free_pages_out_fail;
Chris Masonc59f8952007-12-17 20:14:04 -0500461 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400462 em = alloc_extent_map(GFP_NOFS);
463 em->start = start;
Chris Masonc8b97812008-10-29 14:49:59 -0400464
465 if (will_compress) {
466 ram_size = num_bytes;
467 em->len = num_bytes;
468 } else {
469 /* ramsize == disk size */
470 ram_size = ins.offset;
471 em->len = ins.offset;
472 }
473
Chris Masone6dcd2d2008-07-17 12:53:50 -0400474 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400475 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400476 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400477 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400478
479 if (will_compress)
480 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
481
Chris Masone6dcd2d2008-07-17 12:53:50 -0400482 while(1) {
483 spin_lock(&em_tree->lock);
484 ret = add_extent_mapping(em_tree, em);
485 spin_unlock(&em_tree->lock);
486 if (ret != -EEXIST) {
487 free_extent_map(em);
488 break;
489 }
490 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400491 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400492 }
493
Chris Mason98d20f62008-04-14 09:46:10 -0400494 cur_alloc_size = ins.offset;
Yan Zheng80ff3852008-10-30 14:20:02 -0400495 ordered_type = will_compress ? BTRFS_ORDERED_COMPRESSED : 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400496 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Yan Zheng80ff3852008-10-30 14:20:02 -0400497 ram_size, cur_alloc_size,
498 ordered_type);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400499 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400500
501 if (disk_num_bytes < cur_alloc_size) {
502 printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
Chris Mason3b951512008-04-17 11:29:12 -0400503 cur_alloc_size);
504 break;
505 }
Chris Masonc8b97812008-10-29 14:49:59 -0400506
507 if (will_compress) {
508 /*
509 * we're doing compression, we and we need to
510 * submit the compressed extents down to the device.
511 *
512 * We lock down all the file pages, clearing their
513 * dirty bits and setting them writeback. Everyone
514 * that wants to modify the page will wait on the
515 * ordered extent above.
516 *
517 * The writeback bits on the file pages are
518 * cleared when the compressed pages are on disk
519 */
520 btrfs_end_transaction(trans, root);
521
522 if (start <= page_offset(locked_page) &&
523 page_offset(locked_page) < start + ram_size) {
524 *page_started = 1;
525 }
526
527 extent_clear_unlock_delalloc(inode,
528 &BTRFS_I(inode)->io_tree,
529 start,
530 start + ram_size - 1,
531 NULL, 1, 1, 0);
532
533 ret = btrfs_submit_compressed_write(inode, start,
534 ram_size, ins.objectid,
535 cur_alloc_size, pages,
536 nr_pages_ret);
537
538 BUG_ON(ret);
539 trans = btrfs_join_transaction(root, 1);
540 if (start + ram_size < end) {
541 start += ram_size;
542 alloc_hint = ins.objectid + ins.offset;
543 /* pages will be freed at end_bio time */
544 pages = NULL;
545 goto again;
546 } else {
547 /* we've written everything, time to go */
548 break;
549 }
550 }
551 /* we're not doing compressed IO, don't unlock the first
552 * page (which the caller expects to stay locked), don't
553 * clear any dirty bits and don't set any writeback bits
554 */
555 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
556 start, start + ram_size - 1,
557 locked_page, 0, 0, 0);
558 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500559 num_bytes -= cur_alloc_size;
560 alloc_hint = ins.objectid + ins.offset;
561 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400562 }
Chris Masonc8b97812008-10-29 14:49:59 -0400563
564 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400565out:
566 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400567
Chris Masonbe20aa92007-12-17 20:14:01 -0500568 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400569
570free_pages_out_fail:
571 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
572 start, end, locked_page, 0, 0, 0);
573free_pages_out:
574 for (i = 0; i < nr_pages_ret; i++)
575 page_cache_release(pages[i]);
576 if (pages)
577 kfree(pages);
578
579 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500580}
581
Chris Masond352ac62008-09-29 15:18:18 -0400582/*
583 * when nowcow writeback call back. This checks for snapshots or COW copies
584 * of the extents that exist in the file, and COWs the file as required.
585 *
586 * If no cow copies or snapshots exist, we write directly to the existing
587 * blocks on disk
588 */
Chris Masonc8b97812008-10-29 14:49:59 -0400589static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
590 u64 start, u64 end, int *page_started)
Chris Masonbe20aa92007-12-17 20:14:01 -0500591{
Chris Masonbe20aa92007-12-17 20:14:01 -0500592 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400593 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500594 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500595 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400596 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500597 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400598 u64 cow_start;
599 u64 cur_offset;
600 u64 extent_end;
601 u64 disk_bytenr;
602 u64 num_bytes;
603 int extent_type;
604 int ret;
605 int nocow;
606 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500607
608 path = btrfs_alloc_path();
609 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400610 trans = btrfs_join_transaction(root, 1);
611 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500612
Yan Zheng80ff3852008-10-30 14:20:02 -0400613 cow_start = (u64)-1;
614 cur_offset = start;
615 while (1) {
616 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
617 cur_offset, 0);
618 BUG_ON(ret < 0);
619 if (ret > 0 && path->slots[0] > 0 && check_prev) {
620 leaf = path->nodes[0];
621 btrfs_item_key_to_cpu(leaf, &found_key,
622 path->slots[0] - 1);
623 if (found_key.objectid == inode->i_ino &&
624 found_key.type == BTRFS_EXTENT_DATA_KEY)
625 path->slots[0]--;
626 }
627 check_prev = 0;
628next_slot:
629 leaf = path->nodes[0];
630 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
631 ret = btrfs_next_leaf(root, path);
632 if (ret < 0)
633 BUG_ON(1);
634 if (ret > 0)
635 break;
636 leaf = path->nodes[0];
637 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500638
Yan Zheng80ff3852008-10-30 14:20:02 -0400639 nocow = 0;
640 disk_bytenr = 0;
641 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500642
Yan Zheng80ff3852008-10-30 14:20:02 -0400643 if (found_key.objectid > inode->i_ino ||
644 found_key.type > BTRFS_EXTENT_DATA_KEY ||
645 found_key.offset > end)
646 break;
Chris Masonbe20aa92007-12-17 20:14:01 -0500647
Yan Zheng80ff3852008-10-30 14:20:02 -0400648 if (found_key.offset > cur_offset) {
649 extent_end = found_key.offset;
650 goto out_check;
651 }
Chris Masonc31f8832008-01-08 15:46:31 -0500652
Yan Zheng80ff3852008-10-30 14:20:02 -0400653 fi = btrfs_item_ptr(leaf, path->slots[0],
654 struct btrfs_file_extent_item);
655 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -0500656
Yan Zheng80ff3852008-10-30 14:20:02 -0400657 if (extent_type == BTRFS_FILE_EXTENT_REG) {
658 struct btrfs_block_group_cache *block_group;
659 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
660 extent_end = found_key.offset +
661 btrfs_file_extent_num_bytes(leaf, fi);
662 if (extent_end <= start) {
663 path->slots[0]++;
664 goto next_slot;
665 }
666 if (btrfs_file_extent_compression(leaf, fi) ||
667 btrfs_file_extent_encryption(leaf, fi) ||
668 btrfs_file_extent_other_encoding(leaf, fi))
669 goto out_check;
670 if (disk_bytenr == 0)
671 goto out_check;
672 if (btrfs_cross_ref_exist(trans, root, disk_bytenr))
673 goto out_check;
674 block_group = btrfs_lookup_block_group(root->fs_info,
675 disk_bytenr);
676 if (!block_group || block_group->ro)
677 goto out_check;
678 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
679 nocow = 1;
680 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
681 extent_end = found_key.offset +
682 btrfs_file_extent_inline_len(leaf, fi);
683 extent_end = ALIGN(extent_end, root->sectorsize);
684 } else {
685 BUG_ON(1);
686 }
687out_check:
688 if (extent_end <= start) {
689 path->slots[0]++;
690 goto next_slot;
691 }
692 if (!nocow) {
693 if (cow_start == (u64)-1)
694 cow_start = cur_offset;
695 cur_offset = extent_end;
696 if (cur_offset > end)
697 break;
698 path->slots[0]++;
699 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400700 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500701
Yan Zheng7ea394f2008-08-05 13:05:02 -0400702 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -0400703 if (cow_start != (u64)-1) {
704 ret = cow_file_range(inode, locked_page, cow_start,
705 found_key.offset - 1, page_started);
706 BUG_ON(ret);
707 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400708 }
Yan Zheng80ff3852008-10-30 14:20:02 -0400709
710 disk_bytenr += cur_offset - found_key.offset;
711 num_bytes = min(end + 1, extent_end) - cur_offset;
712
713 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
714 num_bytes, num_bytes,
715 BTRFS_ORDERED_NOCOW);
716 cur_offset = extent_end;
717 if (cur_offset > end)
718 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400719 }
Yan Zheng80ff3852008-10-30 14:20:02 -0400720 btrfs_release_path(root, path);
721
722 if (cur_offset <= end && cow_start == (u64)-1)
723 cow_start = cur_offset;
724 if (cow_start != (u64)-1) {
725 ret = cow_file_range(inode, locked_page, cow_start, end,
726 page_started);
727 BUG_ON(ret);
728 }
729
730 ret = btrfs_end_transaction(trans, root);
731 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400732 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -0400733 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500734}
735
Chris Masond352ac62008-09-29 15:18:18 -0400736/*
737 * extent_io.c call back to do delayed allocation processing
738 */
Chris Masonc8b97812008-10-29 14:49:59 -0400739static int run_delalloc_range(struct inode *inode, struct page *locked_page,
740 u64 start, u64 end, int *page_started)
Chris Masonbe20aa92007-12-17 20:14:01 -0500741{
742 struct btrfs_root *root = BTRFS_I(inode)->root;
743 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400744
Yanb98b6762008-01-08 15:54:37 -0500745 if (btrfs_test_opt(root, NODATACOW) ||
746 btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -0400747 ret = run_delalloc_nocow(inode, locked_page, start, end,
748 page_started);
Chris Masonbe20aa92007-12-17 20:14:01 -0500749 else
Chris Masonc8b97812008-10-29 14:49:59 -0400750 ret = cow_file_range(inode, locked_page, start, end,
751 page_started);
Chris Mason1832a6d2007-12-21 16:27:21 -0500752
Chris Masonb888db22007-08-27 16:49:44 -0400753 return ret;
754}
755
Chris Masond352ac62008-09-29 15:18:18 -0400756/*
757 * extent_io.c set_bit_hook, used to track delayed allocation
758 * bytes in this file, and to maintain the list of inodes that
759 * have pending delalloc work to be done.
760 */
Chris Mason291d6732008-01-29 15:55:23 -0500761int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500762 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500763{
Chris Masonbcbfce82008-04-22 13:26:47 -0400764 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500765 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500766 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400767 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500768 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500769 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400770 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
771 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
772 &root->fs_info->delalloc_inodes);
773 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400774 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500775 }
776 return 0;
777}
778
Chris Masond352ac62008-09-29 15:18:18 -0400779/*
780 * extent_io.c clear_bit_hook, see set_bit_hook for why
781 */
Chris Mason291d6732008-01-29 15:55:23 -0500782int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500783 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500784{
Chris Masonb0c68f82008-01-31 11:05:37 -0500785 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500786 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400787 unsigned long flags;
788
789 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500790 if (end - start + 1 > root->fs_info->delalloc_bytes) {
791 printk("warning: delalloc account %Lu %Lu\n",
792 end - start + 1, root->fs_info->delalloc_bytes);
793 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500794 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500795 } else {
796 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500797 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500798 }
Chris Masonea8c2812008-08-04 23:17:27 -0400799 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
800 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
801 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
802 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400803 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500804 }
805 return 0;
806}
807
Chris Masond352ac62008-09-29 15:18:18 -0400808/*
809 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
810 * we don't create bios that span stripes or chunks
811 */
Chris Mason239b14b2008-03-24 15:02:07 -0400812int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -0400813 size_t size, struct bio *bio,
814 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -0400815{
816 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
817 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -0400818 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400819 u64 length = 0;
820 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400821 int ret;
822
Chris Masonf2d8d742008-04-21 10:03:05 -0400823 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400824 map_tree = &root->fs_info->mapping_tree;
825 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400826 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400827 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400828
Chris Mason239b14b2008-03-24 15:02:07 -0400829 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400830 return 1;
831 }
832 return 0;
833}
834
Chris Masond352ac62008-09-29 15:18:18 -0400835/*
836 * in order to insert checksums into the metadata in large chunks,
837 * we wait until bio submission time. All the pages in the bio are
838 * checksummed and sums are attached onto the ordered extent record.
839 *
840 * At IO completion time the cums attached on the ordered extent record
841 * are inserted into the btree
842 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400843int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -0400844 int mirror_num, unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -0500845{
Chris Mason065631f2008-02-20 12:07:25 -0500846 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500847 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400848
Chris Mason3edf7d32008-07-18 06:17:13 -0400849 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400850 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400851
Chris Mason8b712842008-06-11 16:50:36 -0400852 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400853}
854
Chris Masond352ac62008-09-29 15:18:18 -0400855/*
856 * extent_io.c submission hook. This does the right thing for csum calculation on write,
857 * or reading the csums from the tree before a read
858 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400859int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -0400860 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -0400861{
862 struct btrfs_root *root = BTRFS_I(inode)->root;
863 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -0400864 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -0400865
Chris Masone6dcd2d2008-07-17 12:53:50 -0400866 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
867 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500868
Chris Mason19b9bdb2008-10-30 14:23:13 -0400869 skip_sum = btrfs_test_opt(root, NODATASUM) ||
870 btrfs_test_flag(inode, NODATASUM);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400871
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400872 if (!(rw & (1 << BIO_RW))) {
Chris Mason19b9bdb2008-10-30 14:23:13 -0400873 if (!skip_sum)
874 btrfs_lookup_bio_sums(root, inode, bio);
Chris Masonc8b97812008-10-29 14:49:59 -0400875
Chris Mason19b9bdb2008-10-30 14:23:13 -0400876 if (bio_flags & EXTENT_BIO_COMPRESSED)
Chris Masonc8b97812008-10-29 14:49:59 -0400877 return btrfs_submit_compressed_read(inode, bio,
878 mirror_num, bio_flags);
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400879 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -0400880 } else if (!skip_sum) {
881 /* we're doing a write, do the async checksumming */
882 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -0400883 inode, rw, bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -0400884 bio_flags, __btrfs_submit_bio_hook);
Chris Mason19b9bdb2008-10-30 14:23:13 -0400885 }
886
Chris Mason0b86a832008-03-24 15:01:56 -0400887mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400888 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500889}
Chris Mason6885f302008-02-20 16:11:05 -0500890
Chris Masond352ac62008-09-29 15:18:18 -0400891/*
892 * given a list of ordered sums record them in the inode. This happens
893 * at IO completion time based on sums calculated at bio submission time.
894 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400895static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400896 struct inode *inode, u64 file_offset,
897 struct list_head *list)
898{
899 struct list_head *cur;
900 struct btrfs_ordered_sum *sum;
901
902 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400903 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400904 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400905 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
906 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400907 }
908 return 0;
909}
910
Chris Masonea8c2812008-08-04 23:17:27 -0400911int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
912{
913 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
914 GFP_NOFS);
915}
916
Chris Masond352ac62008-09-29 15:18:18 -0400917/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -0400918struct btrfs_writepage_fixup {
919 struct page *page;
920 struct btrfs_work work;
921};
922
Chris Mason247e7432008-07-17 12:53:51 -0400923void btrfs_writepage_fixup_worker(struct btrfs_work *work)
924{
925 struct btrfs_writepage_fixup *fixup;
926 struct btrfs_ordered_extent *ordered;
927 struct page *page;
928 struct inode *inode;
929 u64 page_start;
930 u64 page_end;
931
932 fixup = container_of(work, struct btrfs_writepage_fixup, work);
933 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400934again:
Chris Mason247e7432008-07-17 12:53:51 -0400935 lock_page(page);
936 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
937 ClearPageChecked(page);
938 goto out_page;
939 }
940
941 inode = page->mapping->host;
942 page_start = page_offset(page);
943 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
944
945 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400946
947 /* already ordered? We're done */
948 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
949 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400950 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400951 }
952
953 ordered = btrfs_lookup_ordered_extent(inode, page_start);
954 if (ordered) {
955 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
956 page_end, GFP_NOFS);
957 unlock_page(page);
958 btrfs_start_ordered_extent(inode, ordered, 1);
959 goto again;
960 }
Chris Mason247e7432008-07-17 12:53:51 -0400961
Chris Masonea8c2812008-08-04 23:17:27 -0400962 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400963 ClearPageChecked(page);
964out:
965 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
966out_page:
967 unlock_page(page);
968 page_cache_release(page);
969}
970
971/*
972 * There are a few paths in the higher layers of the kernel that directly
973 * set the page dirty bit without asking the filesystem if it is a
974 * good idea. This causes problems because we want to make sure COW
975 * properly happens and the data=ordered rules are followed.
976 *
Chris Masonc8b97812008-10-29 14:49:59 -0400977 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -0400978 * hasn't been properly setup for IO. We kick off an async process
979 * to fix it up. The async helper will wait for ordered extents, set
980 * the delalloc bit and make it safe to write the page.
981 */
982int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
983{
984 struct inode *inode = page->mapping->host;
985 struct btrfs_writepage_fixup *fixup;
986 struct btrfs_root *root = BTRFS_I(inode)->root;
987 int ret;
988
989 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
990 EXTENT_ORDERED, 0);
991 if (ret)
992 return 0;
993
994 if (PageChecked(page))
995 return -EAGAIN;
996
997 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
998 if (!fixup)
999 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001000
Chris Mason247e7432008-07-17 12:53:51 -04001001 SetPageChecked(page);
1002 page_cache_get(page);
1003 fixup->work.func = btrfs_writepage_fixup_worker;
1004 fixup->page = page;
1005 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1006 return -EAGAIN;
1007}
1008
Chris Masond352ac62008-09-29 15:18:18 -04001009/* as ordered data IO finishes, this gets called so we can finish
1010 * an ordered extent if the range of bytes in the file it covers are
1011 * fully written.
1012 */
Chris Mason211f90e2008-07-18 11:56:15 -04001013static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001014{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001015 struct btrfs_root *root = BTRFS_I(inode)->root;
1016 struct btrfs_trans_handle *trans;
1017 struct btrfs_ordered_extent *ordered_extent;
1018 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Zheng Yan31840ae2008-09-23 13:14:14 -04001019 struct btrfs_file_extent_item *extent_item;
1020 struct btrfs_path *path = NULL;
1021 struct extent_buffer *leaf;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022 u64 alloc_hint = 0;
1023 struct list_head list;
1024 struct btrfs_key ins;
1025 int ret;
1026
1027 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001028 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001029 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001030
Chris Masonf9295742008-07-17 12:54:14 -04001031 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001032
1033 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1034 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001035 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1036 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037
Zheng Yan31840ae2008-09-23 13:14:14 -04001038 path = btrfs_alloc_path();
1039 BUG_ON(!path);
1040
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041 lock_extent(io_tree, ordered_extent->file_offset,
1042 ordered_extent->file_offset + ordered_extent->len - 1,
1043 GFP_NOFS);
1044
1045 INIT_LIST_HEAD(&list);
1046
Chris Masone6dcd2d2008-07-17 12:53:50 -04001047 ret = btrfs_drop_extents(trans, root, inode,
1048 ordered_extent->file_offset,
1049 ordered_extent->file_offset +
1050 ordered_extent->len,
1051 ordered_extent->file_offset, &alloc_hint);
1052 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04001053
1054 ins.objectid = inode->i_ino;
1055 ins.offset = ordered_extent->file_offset;
1056 ins.type = BTRFS_EXTENT_DATA_KEY;
1057 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1058 sizeof(*extent_item));
Chris Masone6dcd2d2008-07-17 12:53:50 -04001059 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04001060 leaf = path->nodes[0];
1061 extent_item = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_file_extent_item);
1063 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
1064 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
1065 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
1066 ordered_extent->start);
1067 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
Chris Masonc8b97812008-10-29 14:49:59 -04001068 ordered_extent->disk_len);
Zheng Yan31840ae2008-09-23 13:14:14 -04001069 btrfs_set_file_extent_offset(leaf, extent_item, 0);
Chris Masonc8b97812008-10-29 14:49:59 -04001070
1071 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1072 btrfs_set_file_extent_compression(leaf, extent_item, 1);
1073 else
1074 btrfs_set_file_extent_compression(leaf, extent_item, 0);
1075 btrfs_set_file_extent_encryption(leaf, extent_item, 0);
1076 btrfs_set_file_extent_other_encoding(leaf, extent_item, 0);
1077
1078 /* ram bytes = extent_num_bytes for now */
Zheng Yan31840ae2008-09-23 13:14:14 -04001079 btrfs_set_file_extent_num_bytes(leaf, extent_item,
1080 ordered_extent->len);
Chris Masonc8b97812008-10-29 14:49:59 -04001081 btrfs_set_file_extent_ram_bytes(leaf, extent_item,
1082 ordered_extent->len);
Zheng Yan31840ae2008-09-23 13:14:14 -04001083 btrfs_mark_buffer_dirty(leaf);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001084
Chris Masone6dcd2d2008-07-17 12:53:50 -04001085 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
1086 ordered_extent->file_offset +
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001087 ordered_extent->len - 1, 0);
Chris Masonee6e6502008-07-17 12:54:40 -04001088
Zheng Yan31840ae2008-09-23 13:14:14 -04001089 ins.objectid = ordered_extent->start;
Chris Masonc8b97812008-10-29 14:49:59 -04001090 ins.offset = ordered_extent->disk_len;
Zheng Yan31840ae2008-09-23 13:14:14 -04001091 ins.type = BTRFS_EXTENT_ITEM_KEY;
1092 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1093 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001094 trans->transid, inode->i_ino, &ins);
Zheng Yan31840ae2008-09-23 13:14:14 -04001095 BUG_ON(ret);
1096 btrfs_release_path(root, path);
1097
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001098 inode_add_bytes(inode, ordered_extent->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001099 unlock_extent(io_tree, ordered_extent->file_offset,
1100 ordered_extent->file_offset + ordered_extent->len - 1,
1101 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001102nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001103 add_pending_csums(trans, inode, ordered_extent->file_offset,
1104 &ordered_extent->list);
1105
Chris Mason34353022008-09-23 20:19:49 -04001106 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001107 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001108 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001109 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001110 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001111
Chris Masone6dcd2d2008-07-17 12:53:50 -04001112 /* once for us */
1113 btrfs_put_ordered_extent(ordered_extent);
1114 /* once for the tree */
1115 btrfs_put_ordered_extent(ordered_extent);
1116
Chris Masone6dcd2d2008-07-17 12:53:50 -04001117 btrfs_end_transaction(trans, root);
Zheng Yan31840ae2008-09-23 13:14:14 -04001118 if (path)
1119 btrfs_free_path(path);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001120 return 0;
1121}
1122
Chris Mason211f90e2008-07-18 11:56:15 -04001123int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1124 struct extent_state *state, int uptodate)
1125{
1126 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1127}
1128
Chris Masond352ac62008-09-29 15:18:18 -04001129/*
1130 * When IO fails, either with EIO or csum verification fails, we
1131 * try other mirrors that might have a good copy of the data. This
1132 * io_failure_record is used to record state as we go through all the
1133 * mirrors. If another mirror has good data, the page is set up to date
1134 * and things continue. If a good mirror can't be found, the original
1135 * bio end_io callback is called to indicate things have failed.
1136 */
Chris Mason7e383262008-04-09 16:28:12 -04001137struct io_failure_record {
1138 struct page *page;
1139 u64 start;
1140 u64 len;
1141 u64 logical;
1142 int last_mirror;
1143};
1144
Chris Mason1259ab72008-05-12 13:39:03 -04001145int btrfs_io_failed_hook(struct bio *failed_bio,
1146 struct page *page, u64 start, u64 end,
1147 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001148{
1149 struct io_failure_record *failrec = NULL;
1150 u64 private;
1151 struct extent_map *em;
1152 struct inode *inode = page->mapping->host;
1153 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001154 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001155 struct bio *bio;
1156 int num_copies;
1157 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001158 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001159 u64 logical;
Chris Masonc8b97812008-10-29 14:49:59 -04001160 unsigned long bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001161
1162 ret = get_state_private(failure_tree, start, &private);
1163 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001164 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1165 if (!failrec)
1166 return -ENOMEM;
1167 failrec->start = start;
1168 failrec->len = end - start + 1;
1169 failrec->last_mirror = 0;
1170
Chris Mason3b951512008-04-17 11:29:12 -04001171 spin_lock(&em_tree->lock);
1172 em = lookup_extent_mapping(em_tree, start, failrec->len);
1173 if (em->start > start || em->start + em->len < start) {
1174 free_extent_map(em);
1175 em = NULL;
1176 }
1177 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001178
1179 if (!em || IS_ERR(em)) {
1180 kfree(failrec);
1181 return -EIO;
1182 }
1183 logical = start - em->start;
1184 logical = em->block_start + logical;
Chris Masonc8b97812008-10-29 14:49:59 -04001185 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1186 bio_flags = EXTENT_BIO_COMPRESSED;
Chris Mason7e383262008-04-09 16:28:12 -04001187 failrec->logical = logical;
1188 free_extent_map(em);
1189 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1190 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001191 set_state_private(failure_tree, start,
1192 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001193 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001194 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001195 }
1196 num_copies = btrfs_num_copies(
1197 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1198 failrec->logical, failrec->len);
1199 failrec->last_mirror++;
1200 if (!state) {
1201 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
1202 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1203 failrec->start,
1204 EXTENT_LOCKED);
1205 if (state && state->start != failrec->start)
1206 state = NULL;
1207 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
1208 }
1209 if (!state || failrec->last_mirror > num_copies) {
1210 set_state_private(failure_tree, failrec->start, 0);
1211 clear_extent_bits(failure_tree, failrec->start,
1212 failrec->start + failrec->len - 1,
1213 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1214 kfree(failrec);
1215 return -EIO;
1216 }
1217 bio = bio_alloc(GFP_NOFS, 1);
1218 bio->bi_private = state;
1219 bio->bi_end_io = failed_bio->bi_end_io;
1220 bio->bi_sector = failrec->logical >> 9;
1221 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001222 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001223 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001224 if (failed_bio->bi_rw & (1 << BIO_RW))
1225 rw = WRITE;
1226 else
1227 rw = READ;
1228
1229 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001230 failrec->last_mirror,
1231 bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001232 return 0;
1233}
1234
Chris Masond352ac62008-09-29 15:18:18 -04001235/*
1236 * each time an IO finishes, we do a fast check in the IO failure tree
1237 * to see if we need to process or clean up an io_failure_record
1238 */
Chris Mason1259ab72008-05-12 13:39:03 -04001239int btrfs_clean_io_failures(struct inode *inode, u64 start)
1240{
1241 u64 private;
1242 u64 private_failure;
1243 struct io_failure_record *failure;
1244 int ret;
1245
1246 private = 0;
1247 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1248 (u64)-1, 1, EXTENT_DIRTY)) {
1249 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1250 start, &private_failure);
1251 if (ret == 0) {
1252 failure = (struct io_failure_record *)(unsigned long)
1253 private_failure;
1254 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1255 failure->start, 0);
1256 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1257 failure->start,
1258 failure->start + failure->len - 1,
1259 EXTENT_DIRTY | EXTENT_LOCKED,
1260 GFP_NOFS);
1261 kfree(failure);
1262 }
1263 }
Chris Mason7e383262008-04-09 16:28:12 -04001264 return 0;
1265}
1266
Chris Masond352ac62008-09-29 15:18:18 -04001267/*
1268 * when reads are done, we need to check csums to verify the data is correct
1269 * if there's a match, we allow the bio to finish. If not, we go through
1270 * the io_failure_record routines to find good copies
1271 */
Chris Mason70dec802008-01-29 09:59:12 -05001272int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1273 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001274{
Chris Mason35ebb932007-10-30 16:56:53 -04001275 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001276 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001277 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001278 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001279 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001280 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001281 struct btrfs_root *root = BTRFS_I(inode)->root;
1282 u32 csum = ~(u32)0;
Jens Axboebbf0d0062007-10-19 09:23:07 -04001283 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001284
Yanb98b6762008-01-08 15:54:37 -05001285 if (btrfs_test_opt(root, NODATASUM) ||
1286 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001287 return 0;
Yanc2e639f2008-02-04 08:57:25 -05001288 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001289 private = state->private;
1290 ret = 0;
1291 } else {
1292 ret = get_state_private(io_tree, start, &private);
1293 }
Jens Axboebbf0d0062007-10-19 09:23:07 -04001294 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -04001295 kaddr = kmap_atomic(page, KM_IRQ0);
1296 if (ret) {
1297 goto zeroit;
1298 }
Chris Masonff79f812007-10-15 16:22:25 -04001299 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1300 btrfs_csum_final(csum, (char *)&csum);
1301 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -04001302 goto zeroit;
1303 }
1304 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -04001305 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -04001306
1307 /* if the io failure tree for this inode is non-empty,
1308 * check to see if we've recovered from a failed IO
1309 */
Chris Mason1259ab72008-05-12 13:39:03 -04001310 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001311 return 0;
1312
1313zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -05001314 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
1315 page->mapping->host->i_ino, (unsigned long long)start, csum,
1316 private);
Chris Masondb945352007-10-15 16:15:53 -04001317 memset(kaddr + offset, 1, end - start + 1);
1318 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001319 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -04001320 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -04001321 if (private == 0)
1322 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001323 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001324}
Chris Masonb888db22007-08-27 16:49:44 -04001325
Josef Bacik7b128762008-07-24 12:17:14 -04001326/*
1327 * This creates an orphan entry for the given inode in case something goes
1328 * wrong in the middle of an unlink/truncate.
1329 */
1330int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1331{
1332 struct btrfs_root *root = BTRFS_I(inode)->root;
1333 int ret = 0;
1334
Yanbcc63ab2008-07-30 16:29:20 -04001335 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001336
1337 /* already on the orphan list, we're good */
1338 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001339 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001340 return 0;
1341 }
1342
1343 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1344
Yanbcc63ab2008-07-30 16:29:20 -04001345 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001346
1347 /*
1348 * insert an orphan item to track this unlinked/truncated file
1349 */
1350 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1351
1352 return ret;
1353}
1354
1355/*
1356 * We have done the truncate/delete so we can go ahead and remove the orphan
1357 * item for this particular inode.
1358 */
1359int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1360{
1361 struct btrfs_root *root = BTRFS_I(inode)->root;
1362 int ret = 0;
1363
Yanbcc63ab2008-07-30 16:29:20 -04001364 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001365
1366 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001367 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001368 return 0;
1369 }
1370
1371 list_del_init(&BTRFS_I(inode)->i_orphan);
1372 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001373 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001374 return 0;
1375 }
1376
Yanbcc63ab2008-07-30 16:29:20 -04001377 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001378
1379 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1380
1381 return ret;
1382}
1383
1384/*
1385 * this cleans up any orphans that may be left on the list from the last use
1386 * of this root.
1387 */
1388void btrfs_orphan_cleanup(struct btrfs_root *root)
1389{
1390 struct btrfs_path *path;
1391 struct extent_buffer *leaf;
1392 struct btrfs_item *item;
1393 struct btrfs_key key, found_key;
1394 struct btrfs_trans_handle *trans;
1395 struct inode *inode;
1396 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1397
1398 /* don't do orphan cleanup if the fs is readonly. */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001399 if (root->fs_info->sb->s_flags & MS_RDONLY)
Josef Bacik7b128762008-07-24 12:17:14 -04001400 return;
1401
1402 path = btrfs_alloc_path();
1403 if (!path)
1404 return;
1405 path->reada = -1;
1406
1407 key.objectid = BTRFS_ORPHAN_OBJECTID;
1408 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1409 key.offset = (u64)-1;
1410
Josef Bacik7b128762008-07-24 12:17:14 -04001411
1412 while (1) {
1413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1414 if (ret < 0) {
1415 printk(KERN_ERR "Error searching slot for orphan: %d"
1416 "\n", ret);
1417 break;
1418 }
1419
1420 /*
1421 * if ret == 0 means we found what we were searching for, which
1422 * is weird, but possible, so only screw with path if we didnt
1423 * find the key and see if we have stuff that matches
1424 */
1425 if (ret > 0) {
1426 if (path->slots[0] == 0)
1427 break;
1428 path->slots[0]--;
1429 }
1430
1431 /* pull out the item */
1432 leaf = path->nodes[0];
1433 item = btrfs_item_nr(leaf, path->slots[0]);
1434 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1435
1436 /* make sure the item matches what we want */
1437 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1438 break;
1439 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1440 break;
1441
1442 /* release the path since we're done with it */
1443 btrfs_release_path(root, path);
1444
1445 /*
1446 * this is where we are basically btrfs_lookup, without the
1447 * crossing root thing. we store the inode number in the
1448 * offset of the orphan item.
1449 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001450 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001451 found_key.offset, root);
1452 if (!inode)
1453 break;
1454
1455 if (inode->i_state & I_NEW) {
1456 BTRFS_I(inode)->root = root;
1457
1458 /* have to set the location manually */
1459 BTRFS_I(inode)->location.objectid = inode->i_ino;
1460 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1461 BTRFS_I(inode)->location.offset = 0;
1462
1463 btrfs_read_locked_inode(inode);
1464 unlock_new_inode(inode);
1465 }
1466
1467 /*
1468 * add this inode to the orphan list so btrfs_orphan_del does
1469 * the proper thing when we hit it
1470 */
Yanbcc63ab2008-07-30 16:29:20 -04001471 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001472 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001473 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001474
1475 /*
1476 * if this is a bad inode, means we actually succeeded in
1477 * removing the inode, but not the orphan record, which means
1478 * we need to manually delete the orphan since iput will just
1479 * do a destroy_inode
1480 */
1481 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001482 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001483 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001484 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001485 iput(inode);
1486 continue;
1487 }
1488
1489 /* if we have links, this was a truncate, lets do that */
1490 if (inode->i_nlink) {
1491 nr_truncate++;
1492 btrfs_truncate(inode);
1493 } else {
1494 nr_unlink++;
1495 }
1496
1497 /* this will do delete_inode and everything for us */
1498 iput(inode);
1499 }
1500
1501 if (nr_unlink)
1502 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1503 if (nr_truncate)
1504 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1505
1506 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001507}
1508
Chris Masond352ac62008-09-29 15:18:18 -04001509/*
1510 * read an inode from the btree into the in-memory inode
1511 */
Chris Mason39279cc2007-06-12 06:35:45 -04001512void btrfs_read_locked_inode(struct inode *inode)
1513{
1514 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001515 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001516 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001517 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001518 struct btrfs_root *root = BTRFS_I(inode)->root;
1519 struct btrfs_key location;
1520 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001521 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001522 int ret;
1523
1524 path = btrfs_alloc_path();
1525 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001526 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001527
Chris Mason39279cc2007-06-12 06:35:45 -04001528 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001529 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001530 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001531
Chris Mason5f39d392007-10-15 16:14:19 -04001532 leaf = path->nodes[0];
1533 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1534 struct btrfs_inode_item);
1535
1536 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1537 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1538 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1539 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001540 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001541
1542 tspec = btrfs_inode_atime(inode_item);
1543 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1544 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1545
1546 tspec = btrfs_inode_mtime(inode_item);
1547 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1548 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1549
1550 tspec = btrfs_inode_ctime(inode_item);
1551 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1552 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1553
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001554 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04001555 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1556 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04001557 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001558 rdev = btrfs_inode_rdev(leaf, inode_item);
1559
Josef Bacikaec74772008-07-24 12:12:38 -04001560 BTRFS_I(inode)->index_cnt = (u64)-1;
1561
Chris Mason5f39d392007-10-15 16:14:19 -04001562 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001563 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1564 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001565 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001566 if (!BTRFS_I(inode)->block_group) {
1567 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001568 NULL, 0,
1569 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001570 }
Chris Mason39279cc2007-06-12 06:35:45 -04001571 btrfs_free_path(path);
1572 inode_item = NULL;
1573
Chris Mason39279cc2007-06-12 06:35:45 -04001574 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001575 case S_IFREG:
1576 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001577 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001578 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001579 inode->i_fop = &btrfs_file_operations;
1580 inode->i_op = &btrfs_file_inode_operations;
1581 break;
1582 case S_IFDIR:
1583 inode->i_fop = &btrfs_dir_file_operations;
1584 if (root == root->fs_info->tree_root)
1585 inode->i_op = &btrfs_dir_ro_inode_operations;
1586 else
1587 inode->i_op = &btrfs_dir_inode_operations;
1588 break;
1589 case S_IFLNK:
1590 inode->i_op = &btrfs_symlink_inode_operations;
1591 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001592 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001593 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001594 default:
1595 init_special_inode(inode, inode->i_mode, rdev);
1596 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001597 }
1598 return;
1599
1600make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001601 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001602 make_bad_inode(inode);
1603}
1604
Chris Masond352ac62008-09-29 15:18:18 -04001605/*
1606 * given a leaf and an inode, copy the inode fields into the leaf
1607 */
Chris Masone02119d2008-09-05 16:13:11 -04001608static void fill_inode_item(struct btrfs_trans_handle *trans,
1609 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04001610 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001611 struct inode *inode)
1612{
Chris Mason5f39d392007-10-15 16:14:19 -04001613 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1614 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001615 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001616 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1617 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1618
1619 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1620 inode->i_atime.tv_sec);
1621 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1622 inode->i_atime.tv_nsec);
1623
1624 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1625 inode->i_mtime.tv_sec);
1626 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1627 inode->i_mtime.tv_nsec);
1628
1629 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1630 inode->i_ctime.tv_sec);
1631 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1632 inode->i_ctime.tv_nsec);
1633
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001634 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04001635 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1636 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001637 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001638 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001639 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001640 BTRFS_I(inode)->block_group->key.objectid);
1641}
1642
Chris Masond352ac62008-09-29 15:18:18 -04001643/*
1644 * copy everything in the in-memory inode into the btree.
1645 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001646int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001647 struct btrfs_root *root,
1648 struct inode *inode)
1649{
1650 struct btrfs_inode_item *inode_item;
1651 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001652 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001653 int ret;
1654
1655 path = btrfs_alloc_path();
1656 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001657 ret = btrfs_lookup_inode(trans, root, path,
1658 &BTRFS_I(inode)->location, 1);
1659 if (ret) {
1660 if (ret > 0)
1661 ret = -ENOENT;
1662 goto failed;
1663 }
1664
Chris Mason5f39d392007-10-15 16:14:19 -04001665 leaf = path->nodes[0];
1666 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001667 struct btrfs_inode_item);
1668
Chris Masone02119d2008-09-05 16:13:11 -04001669 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001670 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001671 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001672 ret = 0;
1673failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001674 btrfs_free_path(path);
1675 return ret;
1676}
1677
1678
Chris Masond352ac62008-09-29 15:18:18 -04001679/*
1680 * unlink helper that gets used here in inode.c and in the tree logging
1681 * recovery code. It remove a link in a directory with a given name, and
1682 * also drops the back refs in the inode to the directory
1683 */
Chris Masone02119d2008-09-05 16:13:11 -04001684int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1685 struct btrfs_root *root,
1686 struct inode *dir, struct inode *inode,
1687 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04001688{
1689 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001690 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001691 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001692 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001693 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001694 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001695
1696 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001697 if (!path) {
1698 ret = -ENOMEM;
1699 goto err;
1700 }
1701
Chris Mason39279cc2007-06-12 06:35:45 -04001702 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1703 name, name_len, -1);
1704 if (IS_ERR(di)) {
1705 ret = PTR_ERR(di);
1706 goto err;
1707 }
1708 if (!di) {
1709 ret = -ENOENT;
1710 goto err;
1711 }
Chris Mason5f39d392007-10-15 16:14:19 -04001712 leaf = path->nodes[0];
1713 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001714 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001715 if (ret)
1716 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001717 btrfs_release_path(root, path);
1718
Josef Bacikaec74772008-07-24 12:12:38 -04001719 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001720 inode->i_ino,
1721 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04001722 if (ret) {
1723 printk("failed to delete reference to %.*s, "
1724 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04001725 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04001726 goto err;
1727 }
1728
Chris Mason39279cc2007-06-12 06:35:45 -04001729 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001730 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001731 if (IS_ERR(di)) {
1732 ret = PTR_ERR(di);
1733 goto err;
1734 }
1735 if (!di) {
1736 ret = -ENOENT;
1737 goto err;
1738 }
1739 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001740 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001741
Chris Masone02119d2008-09-05 16:13:11 -04001742 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1743 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04001744 BUG_ON(ret != 0 && ret != -ENOENT);
1745 if (ret != -ENOENT)
1746 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04001747
1748 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1749 dir, index);
1750 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001751err:
1752 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001753 if (ret)
1754 goto out;
1755
1756 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1757 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1758 btrfs_update_inode(trans, root, dir);
1759 btrfs_drop_nlink(inode);
1760 ret = btrfs_update_inode(trans, root, inode);
1761 dir->i_sb->s_dirt = 1;
1762out:
Chris Mason39279cc2007-06-12 06:35:45 -04001763 return ret;
1764}
1765
1766static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1767{
1768 struct btrfs_root *root;
1769 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001770 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001771 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001772 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001773
1774 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001775
1776 ret = btrfs_check_free_space(root, 1, 1);
1777 if (ret)
1778 goto fail;
1779
Chris Mason39279cc2007-06-12 06:35:45 -04001780 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001781
Chris Mason39279cc2007-06-12 06:35:45 -04001782 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04001783 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1784 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04001785
1786 if (inode->i_nlink == 0)
1787 ret = btrfs_orphan_add(trans, inode);
1788
Chris Masond3c2fdc2007-09-17 10:58:06 -04001789 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001790
Chris Mason89ce8a62008-06-25 16:01:31 -04001791 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001792fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001793 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001794 return ret;
1795}
1796
1797static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1798{
1799 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001800 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001801 int ret;
1802 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001803 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001804 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001805
Chris Mason925baed2008-06-25 16:01:30 -04001806 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001807 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001808 }
Yan134d4512007-10-25 15:49:25 -04001809
Chris Mason1832a6d2007-12-21 16:27:21 -05001810 ret = btrfs_check_free_space(root, 1, 1);
1811 if (ret)
1812 goto fail;
1813
Chris Mason39279cc2007-06-12 06:35:45 -04001814 trans = btrfs_start_transaction(root, 1);
1815 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001816
Josef Bacik7b128762008-07-24 12:17:14 -04001817 err = btrfs_orphan_add(trans, inode);
1818 if (err)
1819 goto fail_trans;
1820
Chris Mason39279cc2007-06-12 06:35:45 -04001821 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04001822 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1823 dentry->d_name.name, dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001824 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001825 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001826 }
Chris Mason39544012007-12-12 14:38:19 -05001827
Josef Bacik7b128762008-07-24 12:17:14 -04001828fail_trans:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001829 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001830 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001831fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001832 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001833
Chris Mason39279cc2007-06-12 06:35:45 -04001834 if (ret && !err)
1835 err = ret;
1836 return err;
1837}
1838
Chris Mason39279cc2007-06-12 06:35:45 -04001839/*
Chris Mason323ac952008-10-01 19:05:46 -04001840 * when truncating bytes in a file, it is possible to avoid reading
1841 * the leaves that contain only checksum items. This can be the
1842 * majority of the IO required to delete a large file, but it must
1843 * be done carefully.
1844 *
1845 * The keys in the level just above the leaves are checked to make sure
1846 * the lowest key in a given leaf is a csum key, and starts at an offset
1847 * after the new size.
1848 *
1849 * Then the key for the next leaf is checked to make sure it also has
1850 * a checksum item for the same file. If it does, we know our target leaf
1851 * contains only checksum items, and it can be safely freed without reading
1852 * it.
1853 *
1854 * This is just an optimization targeted at large files. It may do
1855 * nothing. It will return 0 unless things went badly.
1856 */
1857static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 struct btrfs_path *path,
1860 struct inode *inode, u64 new_size)
1861{
1862 struct btrfs_key key;
1863 int ret;
1864 int nritems;
1865 struct btrfs_key found_key;
1866 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001867 struct btrfs_leaf_ref *ref;
1868 u64 leaf_gen;
1869 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04001870
1871 path->lowest_level = 1;
1872 key.objectid = inode->i_ino;
1873 key.type = BTRFS_CSUM_ITEM_KEY;
1874 key.offset = new_size;
1875again:
1876 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1877 if (ret < 0)
1878 goto out;
1879
1880 if (path->nodes[1] == NULL) {
1881 ret = 0;
1882 goto out;
1883 }
1884 ret = 0;
1885 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1886 nritems = btrfs_header_nritems(path->nodes[1]);
1887
1888 if (!nritems)
1889 goto out;
1890
1891 if (path->slots[1] >= nritems)
1892 goto next_node;
1893
1894 /* did we find a key greater than anything we want to delete? */
1895 if (found_key.objectid > inode->i_ino ||
1896 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1897 goto out;
1898
1899 /* we check the next key in the node to make sure the leave contains
1900 * only checksum items. This comparison doesn't work if our
1901 * leaf is the last one in the node
1902 */
1903 if (path->slots[1] + 1 >= nritems) {
1904next_node:
1905 /* search forward from the last key in the node, this
1906 * will bring us into the next node in the tree
1907 */
1908 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1909
1910 /* unlikely, but we inc below, so check to be safe */
1911 if (found_key.offset == (u64)-1)
1912 goto out;
1913
1914 /* search_forward needs a path with locks held, do the
1915 * search again for the original key. It is possible
1916 * this will race with a balance and return a path that
1917 * we could modify, but this drop is just an optimization
1918 * and is allowed to miss some leaves.
1919 */
1920 btrfs_release_path(root, path);
1921 found_key.offset++;
1922
1923 /* setup a max key for search_forward */
1924 other_key.offset = (u64)-1;
1925 other_key.type = key.type;
1926 other_key.objectid = key.objectid;
1927
1928 path->keep_locks = 1;
1929 ret = btrfs_search_forward(root, &found_key, &other_key,
1930 path, 0, 0);
1931 path->keep_locks = 0;
1932 if (ret || found_key.objectid != key.objectid ||
1933 found_key.type != key.type) {
1934 ret = 0;
1935 goto out;
1936 }
1937
1938 key.offset = found_key.offset;
1939 btrfs_release_path(root, path);
1940 cond_resched();
1941 goto again;
1942 }
1943
1944 /* we know there's one more slot after us in the tree,
1945 * read that key so we can verify it is also a checksum item
1946 */
1947 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
1948
1949 if (found_key.objectid < inode->i_ino)
1950 goto next_key;
1951
1952 if (found_key.type != key.type || found_key.offset < new_size)
1953 goto next_key;
1954
1955 /*
1956 * if the key for the next leaf isn't a csum key from this objectid,
1957 * we can't be sure there aren't good items inside this leaf.
1958 * Bail out
1959 */
1960 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
1961 goto out;
1962
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001963 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
1964 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04001965 /*
1966 * it is safe to delete this leaf, it contains only
1967 * csum items from this inode at an offset >= new_size
1968 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001969 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04001970 BUG_ON(ret);
1971
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001972 if (root->ref_cows && leaf_gen < trans->transid) {
1973 ref = btrfs_alloc_leaf_ref(root, 0);
1974 if (ref) {
1975 ref->root_gen = root->root_key.offset;
1976 ref->bytenr = leaf_start;
1977 ref->owner = 0;
1978 ref->generation = leaf_gen;
1979 ref->nritems = 0;
1980
1981 ret = btrfs_add_leaf_ref(root, ref, 0);
1982 WARN_ON(ret);
1983 btrfs_free_leaf_ref(root, ref);
1984 } else {
1985 WARN_ON(1);
1986 }
1987 }
Chris Mason323ac952008-10-01 19:05:46 -04001988next_key:
1989 btrfs_release_path(root, path);
1990
1991 if (other_key.objectid == inode->i_ino &&
1992 other_key.type == key.type && other_key.offset > key.offset) {
1993 key.offset = other_key.offset;
1994 cond_resched();
1995 goto again;
1996 }
1997 ret = 0;
1998out:
1999 /* fixup any changes we've made to the path */
2000 path->lowest_level = 0;
2001 path->keep_locks = 0;
2002 btrfs_release_path(root, path);
2003 return ret;
2004}
2005
2006/*
Chris Mason39279cc2007-06-12 06:35:45 -04002007 * this can truncate away extent items, csum items and directory items.
2008 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002009 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002010 *
2011 * csum items that cross the new i_size are truncated to the new size
2012 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002013 *
2014 * min_type is the minimum key type to truncate down to. If set to 0, this
2015 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002016 */
Chris Masone02119d2008-09-05 16:13:11 -04002017noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2018 struct btrfs_root *root,
2019 struct inode *inode,
2020 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002021{
2022 int ret;
2023 struct btrfs_path *path;
2024 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002025 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002026 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04002027 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002028 struct btrfs_file_extent_item *fi;
2029 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002030 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002031 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002032 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002033 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002034 int found_extent;
2035 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002036 int pending_del_nr = 0;
2037 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002038 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04002039 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002040
Chris Masone02119d2008-09-05 16:13:11 -04002041 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002042 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002043 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002044 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002045 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002046
Chris Mason39279cc2007-06-12 06:35:45 -04002047 /* FIXME, add redo link to tree so we don't leak on crash */
2048 key.objectid = inode->i_ino;
2049 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002050 key.type = (u8)-1;
2051
Chris Mason85e21ba2008-01-29 15:11:36 -05002052 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04002053
2054 ret = drop_csum_leaves(trans, root, path, inode, new_size);
2055 BUG_ON(ret);
2056
Chris Mason85e21ba2008-01-29 15:11:36 -05002057search_again:
2058 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2059 if (ret < 0) {
2060 goto error;
2061 }
2062 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002063 /* there are no items in the tree for us to truncate, we're
2064 * done
2065 */
2066 if (path->slots[0] == 0) {
2067 ret = 0;
2068 goto error;
2069 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002070 path->slots[0]--;
2071 }
2072
Chris Mason39279cc2007-06-12 06:35:45 -04002073 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002074 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002075 leaf = path->nodes[0];
2076 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2077 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002078
Chris Mason5f39d392007-10-15 16:14:19 -04002079 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002080 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002081
Chris Mason85e21ba2008-01-29 15:11:36 -05002082 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002083 break;
2084
Chris Mason5f39d392007-10-15 16:14:19 -04002085 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002086 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002087 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002088 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002089 extent_type = btrfs_file_extent_type(leaf, fi);
2090 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002091 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002092 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002093 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002094 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002095 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002096 }
Yan008630c2007-11-07 13:31:09 -05002097 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002098 }
2099 if (found_type == BTRFS_CSUM_ITEM_KEY) {
2100 ret = btrfs_csum_truncate(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002101 new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002102 BUG_ON(ret);
2103 }
Chris Masone02119d2008-09-05 16:13:11 -04002104 if (item_end < new_size) {
Chris Masonb888db22007-08-27 16:49:44 -04002105 if (found_type == BTRFS_DIR_ITEM_KEY) {
2106 found_type = BTRFS_INODE_ITEM_KEY;
2107 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
2108 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05002109 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
2110 found_type = BTRFS_XATTR_ITEM_KEY;
2111 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
2112 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04002113 } else if (found_type) {
2114 found_type--;
2115 } else {
2116 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002117 }
Yana61721d2007-09-17 11:08:38 -04002118 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002119 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002120 }
Chris Masone02119d2008-09-05 16:13:11 -04002121 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002122 del_item = 1;
2123 else
2124 del_item = 0;
2125 found_extent = 0;
2126
2127 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002128 if (found_type != BTRFS_EXTENT_DATA_KEY)
2129 goto delete;
2130
2131 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002132 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002133 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002134 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04002135 u64 orig_num_bytes =
2136 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002137 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002138 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002139 extent_num_bytes = extent_num_bytes &
2140 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002141 btrfs_set_file_extent_num_bytes(leaf, fi,
2142 extent_num_bytes);
2143 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002144 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002145 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002146 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002147 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002148 } else {
Chris Masondb945352007-10-15 16:15:53 -04002149 extent_num_bytes =
2150 btrfs_file_extent_disk_num_bytes(leaf,
2151 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002152 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002153 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002154 if (extent_start != 0) {
2155 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002156 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002157 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002158 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002159 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002160 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002161 }
Chris Mason90692182008-02-08 13:49:28 -05002162 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002163 /*
2164 * we can't truncate inline items that have had
2165 * special encodings
2166 */
2167 if (!del_item &&
2168 btrfs_file_extent_compression(leaf, fi) == 0 &&
2169 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2170 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002171 u32 size = new_size - found_key.offset;
2172
2173 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002174 inode_sub_bytes(inode, item_end + 1 -
2175 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002176 }
2177 size =
2178 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002179 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002180 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002181 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002182 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002183 inode_sub_bytes(inode, item_end + 1 -
2184 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002185 }
Chris Mason39279cc2007-06-12 06:35:45 -04002186 }
Chris Mason179e29e2007-11-01 11:28:41 -04002187delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002188 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002189 if (!pending_del_nr) {
2190 /* no pending yet, add ourselves */
2191 pending_del_slot = path->slots[0];
2192 pending_del_nr = 1;
2193 } else if (pending_del_nr &&
2194 path->slots[0] + 1 == pending_del_slot) {
2195 /* hop on the pending chunk */
2196 pending_del_nr++;
2197 pending_del_slot = path->slots[0];
2198 } else {
2199 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
2200 }
Chris Mason39279cc2007-06-12 06:35:45 -04002201 } else {
2202 break;
2203 }
Chris Mason39279cc2007-06-12 06:35:45 -04002204 if (found_extent) {
2205 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002206 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002207 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002208 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002209 BUG_ON(ret);
2210 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002211next:
2212 if (path->slots[0] == 0) {
2213 if (pending_del_nr)
2214 goto del_pending;
2215 btrfs_release_path(root, path);
2216 goto search_again;
2217 }
2218
2219 path->slots[0]--;
2220 if (pending_del_nr &&
2221 path->slots[0] + 1 != pending_del_slot) {
2222 struct btrfs_key debug;
2223del_pending:
2224 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2225 pending_del_slot);
2226 ret = btrfs_del_items(trans, root, path,
2227 pending_del_slot,
2228 pending_del_nr);
2229 BUG_ON(ret);
2230 pending_del_nr = 0;
2231 btrfs_release_path(root, path);
2232 goto search_again;
2233 }
Chris Mason39279cc2007-06-12 06:35:45 -04002234 }
2235 ret = 0;
2236error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002237 if (pending_del_nr) {
2238 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2239 pending_del_nr);
2240 }
Chris Mason39279cc2007-06-12 06:35:45 -04002241 btrfs_free_path(path);
2242 inode->i_sb->s_dirt = 1;
2243 return ret;
2244}
2245
Chris Masona52d9a82007-08-27 16:49:44 -04002246/*
2247 * taken from block_truncate_page, but does cow as it zeros out
2248 * any bytes left in the last page in the file.
2249 */
2250static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2251{
2252 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002253 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002254 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2255 struct btrfs_ordered_extent *ordered;
2256 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002257 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002258 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2259 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2260 struct page *page;
2261 int ret = 0;
2262 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002263 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002264
2265 if ((offset & (blocksize - 1)) == 0)
2266 goto out;
2267
2268 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002269again:
Chris Masona52d9a82007-08-27 16:49:44 -04002270 page = grab_cache_page(mapping, index);
2271 if (!page)
2272 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002273
2274 page_start = page_offset(page);
2275 page_end = page_start + PAGE_CACHE_SIZE - 1;
2276
Chris Masona52d9a82007-08-27 16:49:44 -04002277 if (!PageUptodate(page)) {
2278 ret = btrfs_readpage(NULL, page);
2279 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002280 if (page->mapping != mapping) {
2281 unlock_page(page);
2282 page_cache_release(page);
2283 goto again;
2284 }
Chris Masona52d9a82007-08-27 16:49:44 -04002285 if (!PageUptodate(page)) {
2286 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002287 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002288 }
2289 }
Chris Mason211c17f2008-05-15 09:13:45 -04002290 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002291
2292 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2293 set_page_extent_mapped(page);
2294
2295 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2296 if (ordered) {
2297 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2298 unlock_page(page);
2299 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002300 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002301 btrfs_put_ordered_extent(ordered);
2302 goto again;
2303 }
2304
Chris Masonea8c2812008-08-04 23:17:27 -04002305 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002306 ret = 0;
2307 if (offset != PAGE_CACHE_SIZE) {
2308 kaddr = kmap(page);
2309 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2310 flush_dcache_page(page);
2311 kunmap(page);
2312 }
Chris Mason247e7432008-07-17 12:53:51 -04002313 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002314 set_page_dirty(page);
2315 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002316
Chris Mason89642222008-07-24 09:41:53 -04002317out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002318 unlock_page(page);
2319 page_cache_release(page);
2320out:
2321 return ret;
2322}
2323
Yan Zheng9036c102008-10-30 14:19:41 -04002324int btrfs_cont_expand(struct inode *inode, loff_t size)
2325{
2326 struct btrfs_trans_handle *trans;
2327 struct btrfs_root *root = BTRFS_I(inode)->root;
2328 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2329 struct extent_map *em;
2330 u64 mask = root->sectorsize - 1;
2331 u64 hole_start = (inode->i_size + mask) & ~mask;
2332 u64 block_end = (size + mask) & ~mask;
2333 u64 last_byte;
2334 u64 cur_offset;
2335 u64 hole_size;
2336 int err;
2337
2338 if (size <= hole_start)
2339 return 0;
2340
2341 err = btrfs_check_free_space(root, 1, 0);
2342 if (err)
2343 return err;
2344
2345 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2346
2347 while (1) {
2348 struct btrfs_ordered_extent *ordered;
2349 btrfs_wait_ordered_range(inode, hole_start,
2350 block_end - hole_start);
2351 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2352 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2353 if (!ordered)
2354 break;
2355 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2356 btrfs_put_ordered_extent(ordered);
2357 }
2358
2359 trans = btrfs_start_transaction(root, 1);
2360 btrfs_set_trans_block_group(trans, inode);
2361
2362 cur_offset = hole_start;
2363 while (1) {
2364 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2365 block_end - cur_offset, 0);
2366 BUG_ON(IS_ERR(em) || !em);
2367 last_byte = min(extent_map_end(em), block_end);
2368 last_byte = (last_byte + mask) & ~mask;
2369 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2370 hole_size = last_byte - cur_offset;
2371 err = btrfs_insert_file_extent(trans, root,
2372 inode->i_ino, cur_offset, 0,
2373 0, hole_size, 0, hole_size,
2374 0, 0, 0);
2375 btrfs_drop_extent_cache(inode, hole_start,
2376 last_byte - 1, 0);
2377 }
2378 free_extent_map(em);
2379 cur_offset = last_byte;
2380 if (err || cur_offset >= block_end)
2381 break;
2382 }
2383
2384 btrfs_end_transaction(trans, root);
2385 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2386 return err;
2387}
2388
Chris Mason39279cc2007-06-12 06:35:45 -04002389static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2390{
2391 struct inode *inode = dentry->d_inode;
2392 int err;
2393
2394 err = inode_change_ok(inode, attr);
2395 if (err)
2396 return err;
2397
2398 if (S_ISREG(inode->i_mode) &&
2399 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002400 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002401 if (err)
2402 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002403 }
Yan Zheng9036c102008-10-30 14:19:41 -04002404
Chris Mason39279cc2007-06-12 06:35:45 -04002405 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002406
2407 if (!err && ((attr->ia_valid & ATTR_MODE)))
2408 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002409 return err;
2410}
Chris Mason61295eb2008-01-14 16:24:38 -05002411
Chris Mason39279cc2007-06-12 06:35:45 -04002412void btrfs_delete_inode(struct inode *inode)
2413{
2414 struct btrfs_trans_handle *trans;
2415 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002416 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002417 int ret;
2418
2419 truncate_inode_pages(&inode->i_data, 0);
2420 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002421 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002422 goto no_delete;
2423 }
Chris Mason4a096752008-07-21 10:29:44 -04002424 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002425
Chris Masondbe674a2008-07-17 12:54:05 -04002426 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002427 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002428
Chris Mason39279cc2007-06-12 06:35:45 -04002429 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002430 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002431 if (ret) {
2432 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002433 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002434 }
2435
2436 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002437
Chris Masond3c2fdc2007-09-17 10:58:06 -04002438 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002439 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002440
Chris Mason39279cc2007-06-12 06:35:45 -04002441 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002442 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002443 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002444
2445no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002446 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002447 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002448 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002449no_delete:
2450 clear_inode(inode);
2451}
2452
2453/*
2454 * this returns the key found in the dir entry in the location pointer.
2455 * If no dir entries were found, location->objectid is 0.
2456 */
2457static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2458 struct btrfs_key *location)
2459{
2460 const char *name = dentry->d_name.name;
2461 int namelen = dentry->d_name.len;
2462 struct btrfs_dir_item *di;
2463 struct btrfs_path *path;
2464 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002465 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002466
2467 path = btrfs_alloc_path();
2468 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002469
Chris Mason39279cc2007-06-12 06:35:45 -04002470 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2471 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002472 if (IS_ERR(di))
2473 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04002474 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05002475 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04002476 }
Chris Mason5f39d392007-10-15 16:14:19 -04002477 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002478out:
Chris Mason39279cc2007-06-12 06:35:45 -04002479 btrfs_free_path(path);
2480 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002481out_err:
2482 location->objectid = 0;
2483 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002484}
2485
2486/*
2487 * when we hit a tree root in a directory, the btrfs part of the inode
2488 * needs to be changed to reflect the root directory of the tree root. This
2489 * is kind of like crossing a mount point.
2490 */
2491static int fixup_tree_root_location(struct btrfs_root *root,
2492 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002493 struct btrfs_root **sub_root,
2494 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002495{
Chris Mason39279cc2007-06-12 06:35:45 -04002496 struct btrfs_root_item *ri;
2497
2498 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2499 return 0;
2500 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2501 return 0;
2502
Josef Bacik58176a92007-08-29 15:47:34 -04002503 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2504 dentry->d_name.name,
2505 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002506 if (IS_ERR(*sub_root))
2507 return PTR_ERR(*sub_root);
2508
2509 ri = &(*sub_root)->root_item;
2510 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002511 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2512 location->offset = 0;
2513
Chris Mason39279cc2007-06-12 06:35:45 -04002514 return 0;
2515}
2516
Chris Masone02119d2008-09-05 16:13:11 -04002517static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002518{
Chris Masone02119d2008-09-05 16:13:11 -04002519 struct btrfs_inode *bi = BTRFS_I(inode);
2520
2521 bi->i_acl = NULL;
2522 bi->i_default_acl = NULL;
2523
2524 bi->generation = 0;
2525 bi->last_trans = 0;
2526 bi->logged_trans = 0;
2527 bi->delalloc_bytes = 0;
2528 bi->disk_i_size = 0;
2529 bi->flags = 0;
2530 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002531 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002532 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2533 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002534 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002535 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2536 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002537 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04002538 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002539 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002540 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002541 mutex_init(&BTRFS_I(inode)->log_mutex);
2542}
2543
2544static int btrfs_init_locked_inode(struct inode *inode, void *p)
2545{
2546 struct btrfs_iget_args *args = p;
2547 inode->i_ino = args->ino;
2548 init_btrfs_i(inode);
2549 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002550 return 0;
2551}
2552
2553static int btrfs_find_actor(struct inode *inode, void *opaque)
2554{
2555 struct btrfs_iget_args *args = opaque;
2556 return (args->ino == inode->i_ino &&
2557 args->root == BTRFS_I(inode)->root);
2558}
2559
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002560struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2561 struct btrfs_root *root, int wait)
2562{
2563 struct inode *inode;
2564 struct btrfs_iget_args args;
2565 args.ino = objectid;
2566 args.root = root;
2567
2568 if (wait) {
2569 inode = ilookup5(s, objectid, btrfs_find_actor,
2570 (void *)&args);
2571 } else {
2572 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2573 (void *)&args);
2574 }
2575 return inode;
2576}
2577
Chris Mason39279cc2007-06-12 06:35:45 -04002578struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2579 struct btrfs_root *root)
2580{
2581 struct inode *inode;
2582 struct btrfs_iget_args args;
2583 args.ino = objectid;
2584 args.root = root;
2585
2586 inode = iget5_locked(s, objectid, btrfs_find_actor,
2587 btrfs_init_locked_inode,
2588 (void *)&args);
2589 return inode;
2590}
2591
Balaji Rao1a54ef82008-07-21 02:01:04 +05302592/* Get an inode object given its location and corresponding root.
2593 * Returns in *is_new if the inode was read from disk
2594 */
2595struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2596 struct btrfs_root *root, int *is_new)
2597{
2598 struct inode *inode;
2599
2600 inode = btrfs_iget_locked(s, location->objectid, root);
2601 if (!inode)
2602 return ERR_PTR(-EACCES);
2603
2604 if (inode->i_state & I_NEW) {
2605 BTRFS_I(inode)->root = root;
2606 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2607 btrfs_read_locked_inode(inode);
2608 unlock_new_inode(inode);
2609 if (is_new)
2610 *is_new = 1;
2611 } else {
2612 if (is_new)
2613 *is_new = 0;
2614 }
2615
2616 return inode;
2617}
2618
Chris Mason39279cc2007-06-12 06:35:45 -04002619static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2620 struct nameidata *nd)
2621{
2622 struct inode * inode;
2623 struct btrfs_inode *bi = BTRFS_I(dir);
2624 struct btrfs_root *root = bi->root;
2625 struct btrfs_root *sub_root = root;
2626 struct btrfs_key location;
Balaji Rao1a54ef82008-07-21 02:01:04 +05302627 int ret, new, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002628
2629 if (dentry->d_name.len > BTRFS_NAME_LEN)
2630 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04002631
Chris Mason39279cc2007-06-12 06:35:45 -04002632 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002633
Chris Mason39279cc2007-06-12 06:35:45 -04002634 if (ret < 0)
2635 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002636
Chris Mason39279cc2007-06-12 06:35:45 -04002637 inode = NULL;
2638 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04002639 ret = fixup_tree_root_location(root, &location, &sub_root,
2640 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04002641 if (ret < 0)
2642 return ERR_PTR(ret);
2643 if (ret > 0)
2644 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05302645 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2646 if (IS_ERR(inode))
2647 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002648
Balaji Rao1a54ef82008-07-21 02:01:04 +05302649 /* the inode and parent dir are two different roots */
2650 if (new && root != sub_root) {
2651 igrab(inode);
2652 sub_root->inode = inode;
2653 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002654 }
2655 }
Josef Bacik7b128762008-07-24 12:17:14 -04002656
2657 if (unlikely(do_orphan))
2658 btrfs_orphan_cleanup(sub_root);
2659
Chris Mason39279cc2007-06-12 06:35:45 -04002660 return d_splice_alias(inode, dentry);
2661}
2662
Chris Mason39279cc2007-06-12 06:35:45 -04002663static unsigned char btrfs_filetype_table[] = {
2664 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2665};
2666
David Woodhousecbdf5a22008-08-06 19:42:33 +01002667static int btrfs_real_readdir(struct file *filp, void *dirent,
2668 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04002669{
Chris Mason6da6aba2007-12-18 16:15:09 -05002670 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002671 struct btrfs_root *root = BTRFS_I(inode)->root;
2672 struct btrfs_item *item;
2673 struct btrfs_dir_item *di;
2674 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002675 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002676 struct btrfs_path *path;
2677 int ret;
2678 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002679 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002680 int slot;
2681 int advance;
2682 unsigned char d_type;
2683 int over = 0;
2684 u32 di_cur;
2685 u32 di_total;
2686 u32 di_len;
2687 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002688 char tmp_name[32];
2689 char *name_ptr;
2690 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04002691
2692 /* FIXME, use a real flag for deciding about the key type */
2693 if (root->fs_info->tree_root == root)
2694 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002695
Chris Mason39544012007-12-12 14:38:19 -05002696 /* special case for "." */
2697 if (filp->f_pos == 0) {
2698 over = filldir(dirent, ".", 1,
2699 1, inode->i_ino,
2700 DT_DIR);
2701 if (over)
2702 return 0;
2703 filp->f_pos = 1;
2704 }
Chris Mason39544012007-12-12 14:38:19 -05002705 /* special case for .., just use the back ref */
2706 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002707 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05002708 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002709 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05002710 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01002711 return 0;
Chris Mason39544012007-12-12 14:38:19 -05002712 filp->f_pos = 2;
2713 }
2714
David Woodhouse49593bf2008-08-17 17:08:36 +01002715 path = btrfs_alloc_path();
2716 path->reada = 2;
2717
Chris Mason39279cc2007-06-12 06:35:45 -04002718 btrfs_set_key_type(&key, key_type);
2719 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002720 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04002721
Chris Mason39279cc2007-06-12 06:35:45 -04002722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2723 if (ret < 0)
2724 goto err;
2725 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01002726
2727 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002728 leaf = path->nodes[0];
2729 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002730 slot = path->slots[0];
2731 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01002732 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002733 ret = btrfs_next_leaf(root, path);
2734 if (ret)
2735 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002736 leaf = path->nodes[0];
2737 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002738 slot = path->slots[0];
2739 } else {
2740 slot++;
2741 path->slots[0]++;
2742 }
2743 }
2744 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002745 item = btrfs_item_nr(leaf, slot);
2746 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2747
2748 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002749 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002750 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002751 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002752 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002753 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002754
2755 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01002756
Chris Mason39279cc2007-06-12 06:35:45 -04002757 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2758 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002759 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01002760
2761 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002762 struct btrfs_key location;
2763
2764 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01002765 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002766 name_ptr = tmp_name;
2767 } else {
2768 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01002769 if (!name_ptr) {
2770 ret = -ENOMEM;
2771 goto err;
2772 }
Chris Mason5f39d392007-10-15 16:14:19 -04002773 }
2774 read_extent_buffer(leaf, name_ptr,
2775 (unsigned long)(di + 1), name_len);
2776
2777 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2778 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002779 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01002780 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002781 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002782
2783 if (name_ptr != tmp_name)
2784 kfree(name_ptr);
2785
Chris Mason39279cc2007-06-12 06:35:45 -04002786 if (over)
2787 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002788
Josef Bacik5103e942007-11-16 11:45:54 -05002789 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01002790 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002791 di_cur += di_len;
2792 di = (struct btrfs_dir_item *)((char *)di + di_len);
2793 }
2794 }
David Woodhouse49593bf2008-08-17 17:08:36 +01002795
2796 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05002797 if (key_type == BTRFS_DIR_INDEX_KEY)
2798 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2799 else
2800 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002801nopos:
2802 ret = 0;
2803err:
Chris Mason39279cc2007-06-12 06:35:45 -04002804 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002805 return ret;
2806}
2807
2808int btrfs_write_inode(struct inode *inode, int wait)
2809{
2810 struct btrfs_root *root = BTRFS_I(inode)->root;
2811 struct btrfs_trans_handle *trans;
2812 int ret = 0;
2813
Chris Mason4ca8b412008-08-05 13:30:48 -04002814 if (root->fs_info->closing > 1)
2815 return 0;
2816
Chris Mason39279cc2007-06-12 06:35:45 -04002817 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002818 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002819 btrfs_set_trans_block_group(trans, inode);
2820 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002821 }
2822 return ret;
2823}
2824
2825/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002826 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002827 * inode changes. But, it is most likely to find the inode in cache.
2828 * FIXME, needs more benchmarking...there are no reasons other than performance
2829 * to keep or drop this code.
2830 */
2831void btrfs_dirty_inode(struct inode *inode)
2832{
2833 struct btrfs_root *root = BTRFS_I(inode)->root;
2834 struct btrfs_trans_handle *trans;
2835
Chris Masonf9295742008-07-17 12:54:14 -04002836 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002837 btrfs_set_trans_block_group(trans, inode);
2838 btrfs_update_inode(trans, root, inode);
2839 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002840}
2841
Chris Masond352ac62008-09-29 15:18:18 -04002842/*
2843 * find the highest existing sequence number in a directory
2844 * and then set the in-memory index_cnt variable to reflect
2845 * free sequence numbers
2846 */
Josef Bacikaec74772008-07-24 12:12:38 -04002847static int btrfs_set_inode_index_count(struct inode *inode)
2848{
2849 struct btrfs_root *root = BTRFS_I(inode)->root;
2850 struct btrfs_key key, found_key;
2851 struct btrfs_path *path;
2852 struct extent_buffer *leaf;
2853 int ret;
2854
2855 key.objectid = inode->i_ino;
2856 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2857 key.offset = (u64)-1;
2858
2859 path = btrfs_alloc_path();
2860 if (!path)
2861 return -ENOMEM;
2862
2863 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2864 if (ret < 0)
2865 goto out;
2866 /* FIXME: we should be able to handle this */
2867 if (ret == 0)
2868 goto out;
2869 ret = 0;
2870
2871 /*
2872 * MAGIC NUMBER EXPLANATION:
2873 * since we search a directory based on f_pos we have to start at 2
2874 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2875 * else has to start at 2
2876 */
2877 if (path->slots[0] == 0) {
2878 BTRFS_I(inode)->index_cnt = 2;
2879 goto out;
2880 }
2881
2882 path->slots[0]--;
2883
2884 leaf = path->nodes[0];
2885 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2886
2887 if (found_key.objectid != inode->i_ino ||
2888 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2889 BTRFS_I(inode)->index_cnt = 2;
2890 goto out;
2891 }
2892
2893 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2894out:
2895 btrfs_free_path(path);
2896 return ret;
2897}
2898
Chris Masond352ac62008-09-29 15:18:18 -04002899/*
2900 * helper to find a free sequence number in a given directory. This current
2901 * code is very simple, later versions will do smarter things in the btree
2902 */
Chris Mason00e4e6b2008-08-05 11:18:09 -04002903static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2904 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002905{
2906 int ret = 0;
2907
2908 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2909 ret = btrfs_set_inode_index_count(dir);
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002910 if (ret) {
Josef Bacikaec74772008-07-24 12:12:38 -04002911 return ret;
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002912 }
Josef Bacikaec74772008-07-24 12:12:38 -04002913 }
2914
Chris Mason00e4e6b2008-08-05 11:18:09 -04002915 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002916 BTRFS_I(dir)->index_cnt++;
2917
2918 return ret;
2919}
2920
Chris Mason39279cc2007-06-12 06:35:45 -04002921static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2922 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002923 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002924 const char *name, int name_len,
2925 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002926 u64 objectid,
2927 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002928 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002929{
2930 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002931 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002932 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002933 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002934 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002935 struct btrfs_inode_ref *ref;
2936 struct btrfs_key key[2];
2937 u32 sizes[2];
2938 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002939 int ret;
2940 int owner;
2941
Chris Mason5f39d392007-10-15 16:14:19 -04002942 path = btrfs_alloc_path();
2943 BUG_ON(!path);
2944
Chris Mason39279cc2007-06-12 06:35:45 -04002945 inode = new_inode(root->fs_info->sb);
2946 if (!inode)
2947 return ERR_PTR(-ENOMEM);
2948
Josef Bacikaec74772008-07-24 12:12:38 -04002949 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002950 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002951 if (ret)
2952 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002953 }
2954 /*
2955 * index_cnt is ignored for everything but a dir,
2956 * btrfs_get_inode_index_count has an explanation for the magic
2957 * number
2958 */
Chris Masone02119d2008-09-05 16:13:11 -04002959 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04002960 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04002961 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04002962 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04002963
Chris Mason39279cc2007-06-12 06:35:45 -04002964 if (mode & S_IFDIR)
2965 owner = 0;
2966 else
2967 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002968 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002969 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002970 if (!new_inode_group) {
2971 printk("find_block group failed\n");
2972 new_inode_group = group;
2973 }
2974 BTRFS_I(inode)->block_group = new_inode_group;
Chris Mason9c583092008-01-29 15:15:18 -05002975
2976 key[0].objectid = objectid;
2977 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2978 key[0].offset = 0;
2979
2980 key[1].objectid = objectid;
2981 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2982 key[1].offset = ref_objectid;
2983
2984 sizes[0] = sizeof(struct btrfs_inode_item);
2985 sizes[1] = name_len + sizeof(*ref);
2986
2987 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2988 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002989 goto fail;
2990
Chris Mason9c583092008-01-29 15:15:18 -05002991 if (objectid > root->highest_inode)
2992 root->highest_inode = objectid;
2993
Chris Mason39279cc2007-06-12 06:35:45 -04002994 inode->i_uid = current->fsuid;
2995 inode->i_gid = current->fsgid;
2996 inode->i_mode = mode;
2997 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002998 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002999 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003000 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3001 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003002 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003003
3004 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3005 struct btrfs_inode_ref);
3006 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003007 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003008 ptr = (unsigned long)(ref + 1);
3009 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3010
Chris Mason5f39d392007-10-15 16:14:19 -04003011 btrfs_mark_buffer_dirty(path->nodes[0]);
3012 btrfs_free_path(path);
3013
Chris Mason39279cc2007-06-12 06:35:45 -04003014 location = &BTRFS_I(inode)->location;
3015 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003016 location->offset = 0;
3017 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3018
Chris Mason39279cc2007-06-12 06:35:45 -04003019 insert_inode_hash(inode);
3020 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003021fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003022 if (dir)
3023 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003024 btrfs_free_path(path);
3025 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003026}
3027
3028static inline u8 btrfs_inode_type(struct inode *inode)
3029{
3030 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3031}
3032
Chris Masond352ac62008-09-29 15:18:18 -04003033/*
3034 * utility function to add 'inode' into 'parent_inode' with
3035 * a give name and a given sequence number.
3036 * if 'add_backref' is true, also insert a backref from the
3037 * inode to the parent directory.
3038 */
Chris Masone02119d2008-09-05 16:13:11 -04003039int btrfs_add_link(struct btrfs_trans_handle *trans,
3040 struct inode *parent_inode, struct inode *inode,
3041 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003042{
3043 int ret;
3044 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003045 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003046
Chris Mason39279cc2007-06-12 06:35:45 -04003047 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003048 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3049 key.offset = 0;
3050
Chris Masone02119d2008-09-05 16:13:11 -04003051 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3052 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003053 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003054 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003055 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003056 if (add_backref) {
3057 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003058 name, name_len,
3059 inode->i_ino,
3060 parent_inode->i_ino,
3061 index);
Chris Mason9c583092008-01-29 15:15:18 -05003062 }
Chris Masondbe674a2008-07-17 12:54:05 -04003063 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003064 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003065 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003066 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003067 }
3068 return ret;
3069}
3070
3071static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003072 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003073 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003074{
Chris Masone02119d2008-09-05 16:13:11 -04003075 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3076 inode, dentry->d_name.name,
3077 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003078 if (!err) {
3079 d_instantiate(dentry, inode);
3080 return 0;
3081 }
3082 if (err > 0)
3083 err = -EEXIST;
3084 return err;
3085}
3086
Josef Bacik618e21d2007-07-11 10:18:17 -04003087static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3088 int mode, dev_t rdev)
3089{
3090 struct btrfs_trans_handle *trans;
3091 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003092 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003093 int err;
3094 int drop_inode = 0;
3095 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003096 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003097 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003098
3099 if (!new_valid_dev(rdev))
3100 return -EINVAL;
3101
Chris Mason1832a6d2007-12-21 16:27:21 -05003102 err = btrfs_check_free_space(root, 1, 0);
3103 if (err)
3104 goto fail;
3105
Josef Bacik618e21d2007-07-11 10:18:17 -04003106 trans = btrfs_start_transaction(root, 1);
3107 btrfs_set_trans_block_group(trans, dir);
3108
3109 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3110 if (err) {
3111 err = -ENOSPC;
3112 goto out_unlock;
3113 }
3114
Josef Bacikaec74772008-07-24 12:12:38 -04003115 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003116 dentry->d_name.len,
3117 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003118 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003119 err = PTR_ERR(inode);
3120 if (IS_ERR(inode))
3121 goto out_unlock;
3122
Josef Bacik33268ea2008-07-24 12:16:36 -04003123 err = btrfs_init_acl(inode, dir);
3124 if (err) {
3125 drop_inode = 1;
3126 goto out_unlock;
3127 }
3128
Josef Bacik618e21d2007-07-11 10:18:17 -04003129 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003130 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003131 if (err)
3132 drop_inode = 1;
3133 else {
3134 inode->i_op = &btrfs_special_inode_operations;
3135 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003136 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003137 }
3138 dir->i_sb->s_dirt = 1;
3139 btrfs_update_inode_block_group(trans, inode);
3140 btrfs_update_inode_block_group(trans, dir);
3141out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003142 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003143 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003144fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003145 if (drop_inode) {
3146 inode_dec_link_count(inode);
3147 iput(inode);
3148 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003149 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003150 return err;
3151}
3152
Chris Mason39279cc2007-06-12 06:35:45 -04003153static int btrfs_create(struct inode *dir, struct dentry *dentry,
3154 int mode, struct nameidata *nd)
3155{
3156 struct btrfs_trans_handle *trans;
3157 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003158 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003159 int err;
3160 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003161 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003162 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003163 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003164
Chris Mason1832a6d2007-12-21 16:27:21 -05003165 err = btrfs_check_free_space(root, 1, 0);
3166 if (err)
3167 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003168 trans = btrfs_start_transaction(root, 1);
3169 btrfs_set_trans_block_group(trans, dir);
3170
3171 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3172 if (err) {
3173 err = -ENOSPC;
3174 goto out_unlock;
3175 }
3176
Josef Bacikaec74772008-07-24 12:12:38 -04003177 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003178 dentry->d_name.len,
3179 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003180 objectid, BTRFS_I(dir)->block_group, mode,
3181 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003182 err = PTR_ERR(inode);
3183 if (IS_ERR(inode))
3184 goto out_unlock;
3185
Josef Bacik33268ea2008-07-24 12:16:36 -04003186 err = btrfs_init_acl(inode, dir);
3187 if (err) {
3188 drop_inode = 1;
3189 goto out_unlock;
3190 }
3191
Chris Mason39279cc2007-06-12 06:35:45 -04003192 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003193 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003194 if (err)
3195 drop_inode = 1;
3196 else {
3197 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003198 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003199 inode->i_fop = &btrfs_file_operations;
3200 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003201 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003202 }
3203 dir->i_sb->s_dirt = 1;
3204 btrfs_update_inode_block_group(trans, inode);
3205 btrfs_update_inode_block_group(trans, dir);
3206out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003207 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003208 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003209fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003210 if (drop_inode) {
3211 inode_dec_link_count(inode);
3212 iput(inode);
3213 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003214 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003215 return err;
3216}
3217
3218static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3219 struct dentry *dentry)
3220{
3221 struct btrfs_trans_handle *trans;
3222 struct btrfs_root *root = BTRFS_I(dir)->root;
3223 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003224 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003225 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003226 int err;
3227 int drop_inode = 0;
3228
3229 if (inode->i_nlink == 0)
3230 return -ENOENT;
3231
Chris Masone02119d2008-09-05 16:13:11 -04003232 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003233 err = btrfs_check_free_space(root, 1, 0);
3234 if (err)
3235 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003236 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003237 if (err)
3238 goto fail;
3239
Chris Mason39279cc2007-06-12 06:35:45 -04003240 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003241
Chris Mason39279cc2007-06-12 06:35:45 -04003242 btrfs_set_trans_block_group(trans, dir);
3243 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003244
Chris Mason00e4e6b2008-08-05 11:18:09 -04003245 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003246
Chris Mason39279cc2007-06-12 06:35:45 -04003247 if (err)
3248 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003249
Chris Mason39279cc2007-06-12 06:35:45 -04003250 dir->i_sb->s_dirt = 1;
3251 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003252 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003253
Chris Mason54aa1f42007-06-22 14:16:25 -04003254 if (err)
3255 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003256
Chris Masond3c2fdc2007-09-17 10:58:06 -04003257 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003258 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003259fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003260 if (drop_inode) {
3261 inode_dec_link_count(inode);
3262 iput(inode);
3263 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003264 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003265 return err;
3266}
3267
Chris Mason39279cc2007-06-12 06:35:45 -04003268static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3269{
Chris Masonb9d86662008-05-02 16:13:49 -04003270 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003271 struct btrfs_trans_handle *trans;
3272 struct btrfs_root *root = BTRFS_I(dir)->root;
3273 int err = 0;
3274 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003275 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003276 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003277 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003278
Chris Mason1832a6d2007-12-21 16:27:21 -05003279 err = btrfs_check_free_space(root, 1, 0);
3280 if (err)
3281 goto out_unlock;
3282
Chris Mason39279cc2007-06-12 06:35:45 -04003283 trans = btrfs_start_transaction(root, 1);
3284 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003285
Chris Mason39279cc2007-06-12 06:35:45 -04003286 if (IS_ERR(trans)) {
3287 err = PTR_ERR(trans);
3288 goto out_unlock;
3289 }
3290
3291 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3292 if (err) {
3293 err = -ENOSPC;
3294 goto out_unlock;
3295 }
3296
Josef Bacikaec74772008-07-24 12:12:38 -04003297 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003298 dentry->d_name.len,
3299 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003300 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3301 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003302 if (IS_ERR(inode)) {
3303 err = PTR_ERR(inode);
3304 goto out_fail;
3305 }
Chris Mason5f39d392007-10-15 16:14:19 -04003306
Chris Mason39279cc2007-06-12 06:35:45 -04003307 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003308
3309 err = btrfs_init_acl(inode, dir);
3310 if (err)
3311 goto out_fail;
3312
Chris Mason39279cc2007-06-12 06:35:45 -04003313 inode->i_op = &btrfs_dir_inode_operations;
3314 inode->i_fop = &btrfs_dir_file_operations;
3315 btrfs_set_trans_block_group(trans, inode);
3316
Chris Masondbe674a2008-07-17 12:54:05 -04003317 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003318 err = btrfs_update_inode(trans, root, inode);
3319 if (err)
3320 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003321
Chris Masone02119d2008-09-05 16:13:11 -04003322 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3323 inode, dentry->d_name.name,
3324 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003325 if (err)
3326 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003327
Chris Mason39279cc2007-06-12 06:35:45 -04003328 d_instantiate(dentry, inode);
3329 drop_on_err = 0;
3330 dir->i_sb->s_dirt = 1;
3331 btrfs_update_inode_block_group(trans, inode);
3332 btrfs_update_inode_block_group(trans, dir);
3333
3334out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003335 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003336 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003337
Chris Mason39279cc2007-06-12 06:35:45 -04003338out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003339 if (drop_on_err)
3340 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003341 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003342 return err;
3343}
3344
Chris Masond352ac62008-09-29 15:18:18 -04003345/* helper for btfs_get_extent. Given an existing extent in the tree,
3346 * and an extent that you want to insert, deal with overlap and insert
3347 * the new extent into the tree.
3348 */
Chris Mason3b951512008-04-17 11:29:12 -04003349static int merge_extent_mapping(struct extent_map_tree *em_tree,
3350 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003351 struct extent_map *em,
3352 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003353{
3354 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003355
Chris Masone6dcd2d2008-07-17 12:53:50 -04003356 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3357 start_diff = map_start - em->start;
3358 em->start = map_start;
3359 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003360 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3361 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003362 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003363 em->block_len -= start_diff;
3364 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003365 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003366}
3367
Chris Masonc8b97812008-10-29 14:49:59 -04003368static noinline int uncompress_inline(struct btrfs_path *path,
3369 struct inode *inode, struct page *page,
3370 size_t pg_offset, u64 extent_offset,
3371 struct btrfs_file_extent_item *item)
3372{
3373 int ret;
3374 struct extent_buffer *leaf = path->nodes[0];
3375 char *tmp;
3376 size_t max_size;
3377 unsigned long inline_size;
3378 unsigned long ptr;
3379
3380 WARN_ON(pg_offset != 0);
3381 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3382 inline_size = btrfs_file_extent_inline_item_len(leaf,
3383 btrfs_item_nr(leaf, path->slots[0]));
3384 tmp = kmalloc(inline_size, GFP_NOFS);
3385 ptr = btrfs_file_extent_inline_start(item);
3386
3387 read_extent_buffer(leaf, tmp, ptr, inline_size);
3388
3389 max_size = min(PAGE_CACHE_SIZE, max_size);
3390 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3391 inline_size, max_size);
3392 if (ret) {
3393 char *kaddr = kmap_atomic(page, KM_USER0);
3394 unsigned long copy_size = min_t(u64,
3395 PAGE_CACHE_SIZE - pg_offset,
3396 max_size - extent_offset);
3397 memset(kaddr + pg_offset, 0, copy_size);
3398 kunmap_atomic(kaddr, KM_USER0);
3399 }
3400 kfree(tmp);
3401 return 0;
3402}
3403
Chris Masond352ac62008-09-29 15:18:18 -04003404/*
3405 * a bit scary, this does extent mapping from logical file offset to the disk.
3406 * the ugly parts come from merging extents from the disk with the
3407 * in-ram representation. This gets more complex because of the data=ordered code,
3408 * where the in-ram extents might be locked pending data=ordered completion.
3409 *
3410 * This also copies inline extents directly into the page.
3411 */
Chris Masona52d9a82007-08-27 16:49:44 -04003412struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003413 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003414 int create)
3415{
3416 int ret;
3417 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003418 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003419 u64 extent_start = 0;
3420 u64 extent_end = 0;
3421 u64 objectid = inode->i_ino;
3422 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003423 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003424 struct btrfs_root *root = BTRFS_I(inode)->root;
3425 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003426 struct extent_buffer *leaf;
3427 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003428 struct extent_map *em = NULL;
3429 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003430 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003431 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003432 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003433
Chris Masona52d9a82007-08-27 16:49:44 -04003434again:
Chris Masond1310b22008-01-24 16:13:08 -05003435 spin_lock(&em_tree->lock);
3436 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003437 if (em)
3438 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003439 spin_unlock(&em_tree->lock);
3440
Chris Masona52d9a82007-08-27 16:49:44 -04003441 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003442 if (em->start > start || em->start + em->len <= start)
3443 free_extent_map(em);
3444 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003445 free_extent_map(em);
3446 else
3447 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003448 }
Chris Masond1310b22008-01-24 16:13:08 -05003449 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003450 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003451 err = -ENOMEM;
3452 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003453 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003454 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003455 em->start = EXTENT_MAP_HOLE;
3456 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003457 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003458
3459 if (!path) {
3460 path = btrfs_alloc_path();
3461 BUG_ON(!path);
3462 }
3463
Chris Mason179e29e2007-11-01 11:28:41 -04003464 ret = btrfs_lookup_file_extent(trans, root, path,
3465 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003466 if (ret < 0) {
3467 err = ret;
3468 goto out;
3469 }
3470
3471 if (ret != 0) {
3472 if (path->slots[0] == 0)
3473 goto not_found;
3474 path->slots[0]--;
3475 }
3476
Chris Mason5f39d392007-10-15 16:14:19 -04003477 leaf = path->nodes[0];
3478 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003479 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003480 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003481 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3482 found_type = btrfs_key_type(&found_key);
3483 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003484 found_type != BTRFS_EXTENT_DATA_KEY) {
3485 goto not_found;
3486 }
3487
Chris Mason5f39d392007-10-15 16:14:19 -04003488 found_type = btrfs_file_extent_type(leaf, item);
3489 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003490 compressed = btrfs_file_extent_compression(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04003491 if (found_type == BTRFS_FILE_EXTENT_REG) {
3492 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04003493 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04003494 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3495 size_t size;
3496 size = btrfs_file_extent_inline_len(leaf, item);
3497 extent_end = (extent_start + size + root->sectorsize - 1) &
3498 ~((u64)root->sectorsize - 1);
3499 }
3500
3501 if (start >= extent_end) {
3502 path->slots[0]++;
3503 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3504 ret = btrfs_next_leaf(root, path);
3505 if (ret < 0) {
3506 err = ret;
3507 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003508 }
Yan Zheng9036c102008-10-30 14:19:41 -04003509 if (ret > 0)
3510 goto not_found;
3511 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04003512 }
Yan Zheng9036c102008-10-30 14:19:41 -04003513 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3514 if (found_key.objectid != objectid ||
3515 found_key.type != BTRFS_EXTENT_DATA_KEY)
3516 goto not_found;
3517 if (start + len <= found_key.offset)
3518 goto not_found;
3519 em->start = start;
3520 em->len = found_key.offset - start;
3521 goto not_found_em;
3522 }
3523
3524 if (found_type == BTRFS_FILE_EXTENT_REG) {
3525 em->start = extent_start;
3526 em->len = extent_end - extent_start;
Chris Masondb945352007-10-15 16:15:53 -04003527 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3528 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003529 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04003530 goto insert;
3531 }
Chris Masonc8b97812008-10-29 14:49:59 -04003532 if (compressed) {
3533 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3534 em->block_start = bytenr;
3535 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
3536 item);
3537 } else {
3538 bytenr += btrfs_file_extent_offset(leaf, item);
3539 em->block_start = bytenr;
3540 em->block_len = em->len;
3541 }
Chris Masona52d9a82007-08-27 16:49:44 -04003542 goto insert;
3543 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003544 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04003545 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04003546 size_t size;
3547 size_t extent_offset;
3548 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04003549
Chris Masona52d9a82007-08-27 16:49:44 -04003550 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04003551 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04003552 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04003553 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04003554 goto out;
3555 }
3556
Yan Zheng9036c102008-10-30 14:19:41 -04003557 size = btrfs_file_extent_inline_len(leaf, item);
3558 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05003559 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04003560 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04003561 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05003562 em->len = (copy_size + root->sectorsize - 1) &
3563 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003564 if (compressed)
3565 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04003566 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04003567 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04003568 if (btrfs_file_extent_compression(leaf, item) ==
3569 BTRFS_COMPRESS_ZLIB) {
3570 ret = uncompress_inline(path, inode, page,
3571 pg_offset,
3572 extent_offset, item);
3573 BUG_ON(ret);
3574 } else {
3575 map = kmap(page);
3576 read_extent_buffer(leaf, map + pg_offset, ptr,
3577 copy_size);
3578 kunmap(page);
3579 }
Chris Mason179e29e2007-11-01 11:28:41 -04003580 flush_dcache_page(page);
3581 } else if (create && PageUptodate(page)) {
3582 if (!trans) {
3583 kunmap(page);
3584 free_extent_map(em);
3585 em = NULL;
3586 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04003587 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04003588 goto again;
3589 }
Chris Masonc8b97812008-10-29 14:49:59 -04003590 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05003591 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04003592 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003593 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04003594 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04003595 }
Chris Masond1310b22008-01-24 16:13:08 -05003596 set_extent_uptodate(io_tree, em->start,
3597 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003598 goto insert;
3599 } else {
3600 printk("unkknown found_type %d\n", found_type);
3601 WARN_ON(1);
3602 }
3603not_found:
3604 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05003605 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003606not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04003607 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04003608 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003609insert:
3610 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05003611 if (em->start > start || extent_map_end(em) <= start) {
3612 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04003613 err = -EIO;
3614 goto out;
3615 }
Chris Masond1310b22008-01-24 16:13:08 -05003616
3617 err = 0;
3618 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003619 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003620 /* it is possible that someone inserted the extent into the tree
3621 * while we had the lock dropped. It is also possible that
3622 * an overlapping map exists in the tree
3623 */
Chris Masona52d9a82007-08-27 16:49:44 -04003624 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04003625 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003626
3627 ret = 0;
3628
Chris Mason3b951512008-04-17 11:29:12 -04003629 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04003630 if (existing && (existing->start > start ||
3631 existing->start + existing->len <= start)) {
3632 free_extent_map(existing);
3633 existing = NULL;
3634 }
Chris Mason3b951512008-04-17 11:29:12 -04003635 if (!existing) {
3636 existing = lookup_extent_mapping(em_tree, em->start,
3637 em->len);
3638 if (existing) {
3639 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003640 em, start,
3641 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04003642 free_extent_map(existing);
3643 if (err) {
3644 free_extent_map(em);
3645 em = NULL;
3646 }
3647 } else {
3648 err = -EIO;
3649 printk("failing to insert %Lu %Lu\n",
3650 start, len);
3651 free_extent_map(em);
3652 em = NULL;
3653 }
3654 } else {
3655 free_extent_map(em);
3656 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003657 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04003658 }
Chris Masona52d9a82007-08-27 16:49:44 -04003659 }
Chris Masond1310b22008-01-24 16:13:08 -05003660 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003661out:
Chris Masonf4219502008-07-22 11:18:09 -04003662 if (path)
3663 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04003664 if (trans) {
3665 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003666 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04003667 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003668 }
Chris Masona52d9a82007-08-27 16:49:44 -04003669 }
Chris Masona52d9a82007-08-27 16:49:44 -04003670 if (err) {
3671 free_extent_map(em);
3672 WARN_ON(1);
3673 return ERR_PTR(err);
3674 }
3675 return em;
3676}
3677
Chris Mason16432982008-04-10 10:23:21 -04003678static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3679 const struct iovec *iov, loff_t offset,
3680 unsigned long nr_segs)
3681{
Chris Masone1c4b742008-04-22 13:26:46 -04003682 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04003683}
3684
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003685static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04003686{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003687 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003688}
3689
Chris Mason9ebefb182007-06-15 13:50:00 -04003690int btrfs_readpage(struct file *file, struct page *page)
3691{
Chris Masond1310b22008-01-24 16:13:08 -05003692 struct extent_io_tree *tree;
3693 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003694 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003695}
Chris Mason1832a6d2007-12-21 16:27:21 -05003696
Chris Mason39279cc2007-06-12 06:35:45 -04003697static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3698{
Chris Masond1310b22008-01-24 16:13:08 -05003699 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04003700
3701
3702 if (current->flags & PF_MEMALLOC) {
3703 redirty_page_for_writepage(wbc, page);
3704 unlock_page(page);
3705 return 0;
3706 }
Chris Masond1310b22008-01-24 16:13:08 -05003707 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003708 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3709}
Chris Mason39279cc2007-06-12 06:35:45 -04003710
Chris Masonf4219502008-07-22 11:18:09 -04003711int btrfs_writepages(struct address_space *mapping,
3712 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04003713{
Chris Masond1310b22008-01-24 16:13:08 -05003714 struct extent_io_tree *tree;
3715 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003716 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3717}
3718
Chris Mason3ab2fb52007-11-08 10:59:22 -05003719static int
3720btrfs_readpages(struct file *file, struct address_space *mapping,
3721 struct list_head *pages, unsigned nr_pages)
3722{
Chris Masond1310b22008-01-24 16:13:08 -05003723 struct extent_io_tree *tree;
3724 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003725 return extent_readpages(tree, mapping, pages, nr_pages,
3726 btrfs_get_extent);
3727}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003728static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003729{
Chris Masond1310b22008-01-24 16:13:08 -05003730 struct extent_io_tree *tree;
3731 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003732 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003733
Chris Masond1310b22008-01-24 16:13:08 -05003734 tree = &BTRFS_I(page->mapping->host)->io_tree;
3735 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003736 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003737 if (ret == 1) {
3738 ClearPagePrivate(page);
3739 set_page_private(page, 0);
3740 page_cache_release(page);
3741 }
3742 return ret;
3743}
Chris Mason39279cc2007-06-12 06:35:45 -04003744
Chris Masone6dcd2d2008-07-17 12:53:50 -04003745static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3746{
Chris Mason98509cf2008-09-11 15:51:43 -04003747 if (PageWriteback(page) || PageDirty(page))
3748 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003749 return __btrfs_releasepage(page, gfp_flags);
3750}
3751
Chris Masona52d9a82007-08-27 16:49:44 -04003752static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3753{
Chris Masond1310b22008-01-24 16:13:08 -05003754 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003755 struct btrfs_ordered_extent *ordered;
3756 u64 page_start = page_offset(page);
3757 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003758
Chris Masone6dcd2d2008-07-17 12:53:50 -04003759 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003760 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003761 if (offset) {
3762 btrfs_releasepage(page, GFP_NOFS);
3763 return;
3764 }
3765
3766 lock_extent(tree, page_start, page_end, GFP_NOFS);
3767 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3768 page_offset(page));
3769 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003770 /*
3771 * IO on this page will never be started, so we need
3772 * to account for any ordered extents now
3773 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003774 clear_extent_bit(tree, page_start, page_end,
3775 EXTENT_DIRTY | EXTENT_DELALLOC |
3776 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003777 btrfs_finish_ordered_io(page->mapping->host,
3778 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003779 btrfs_put_ordered_extent(ordered);
3780 lock_extent(tree, page_start, page_end, GFP_NOFS);
3781 }
3782 clear_extent_bit(tree, page_start, page_end,
3783 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3784 EXTENT_ORDERED,
3785 1, 1, GFP_NOFS);
3786 __btrfs_releasepage(page, GFP_NOFS);
3787
Chris Mason4a096752008-07-21 10:29:44 -04003788 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003789 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003790 ClearPagePrivate(page);
3791 set_page_private(page, 0);
3792 page_cache_release(page);
3793 }
Chris Mason39279cc2007-06-12 06:35:45 -04003794}
3795
Chris Mason9ebefb182007-06-15 13:50:00 -04003796/*
3797 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3798 * called from a page fault handler when a page is first dirtied. Hence we must
3799 * be careful to check for EOF conditions here. We set the page up correctly
3800 * for a written page which means we get ENOSPC checking when writing into
3801 * holes and correct delalloc and unwritten extent mapping on filesystems that
3802 * support these features.
3803 *
3804 * We are not allowed to take the i_mutex here so we have to play games to
3805 * protect against truncate races as the page could now be beyond EOF. Because
3806 * vmtruncate() writes the inode size before removing pages, once we have the
3807 * page lock we can determine safely if the page is beyond EOF. If it is not
3808 * beyond EOF, then the page is guaranteed safe against truncation until we
3809 * unlock the page.
3810 */
3811int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3812{
Chris Mason6da6aba2007-12-18 16:15:09 -05003813 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003814 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003815 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3816 struct btrfs_ordered_extent *ordered;
3817 char *kaddr;
3818 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003819 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003820 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003821 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003822 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003823
Chris Mason1832a6d2007-12-21 16:27:21 -05003824 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003825 if (ret)
3826 goto out;
3827
3828 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003829again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003830 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003831 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003832 page_start = page_offset(page);
3833 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003834
Chris Mason9ebefb182007-06-15 13:50:00 -04003835 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003836 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003837 /* page got truncated out from underneath us */
3838 goto out_unlock;
3839 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003840 wait_on_page_writeback(page);
3841
3842 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3843 set_page_extent_mapped(page);
3844
Chris Masoneb84ae02008-07-17 13:53:27 -04003845 /*
3846 * we can't set the delalloc bits if there are pending ordered
3847 * extents. Drop our locks and wait for them to finish
3848 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003849 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3850 if (ordered) {
3851 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3852 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003853 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003854 btrfs_put_ordered_extent(ordered);
3855 goto again;
3856 }
3857
Chris Masonea8c2812008-08-04 23:17:27 -04003858 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003859 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003860
3861 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003862 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003863 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003864 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003865 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003866
Chris Masone6dcd2d2008-07-17 12:53:50 -04003867 if (zero_start != PAGE_CACHE_SIZE) {
3868 kaddr = kmap(page);
3869 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3870 flush_dcache_page(page);
3871 kunmap(page);
3872 }
Chris Mason247e7432008-07-17 12:53:51 -04003873 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003874 set_page_dirty(page);
3875 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003876
3877out_unlock:
3878 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003879out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003880 return ret;
3881}
3882
Chris Mason39279cc2007-06-12 06:35:45 -04003883static void btrfs_truncate(struct inode *inode)
3884{
3885 struct btrfs_root *root = BTRFS_I(inode)->root;
3886 int ret;
3887 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003888 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003889 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003890
3891 if (!S_ISREG(inode->i_mode))
3892 return;
3893 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3894 return;
3895
3896 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003897 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003898
Chris Mason39279cc2007-06-12 06:35:45 -04003899 trans = btrfs_start_transaction(root, 1);
3900 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003901 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003902
Josef Bacik7b128762008-07-24 12:17:14 -04003903 ret = btrfs_orphan_add(trans, inode);
3904 if (ret)
3905 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04003907 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05003908 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003909 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003910
Josef Bacik7b128762008-07-24 12:17:14 -04003911 ret = btrfs_orphan_del(trans, inode);
3912 BUG_ON(ret);
3913
3914out:
3915 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003916 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003917 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003918 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003919}
3920
Sven Wegener3b963622008-06-09 21:57:42 -04003921/*
3922 * Invalidate a single dcache entry at the root of the filesystem.
3923 * Needed after creation of snapshot or subvolume.
3924 */
3925void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3926 int namelen)
3927{
3928 struct dentry *alias, *entry;
3929 struct qstr qstr;
3930
3931 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3932 if (alias) {
3933 qstr.name = name;
3934 qstr.len = namelen;
3935 /* change me if btrfs ever gets a d_hash operation */
3936 qstr.hash = full_name_hash(qstr.name, qstr.len);
3937 entry = d_lookup(alias, &qstr);
3938 dput(alias);
3939 if (entry) {
3940 d_invalidate(entry);
3941 dput(entry);
3942 }
3943 }
3944}
3945
Chris Masond352ac62008-09-29 15:18:18 -04003946/*
3947 * create a new subvolume directory/inode (helper for the ioctl).
3948 */
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003949int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003950 struct btrfs_trans_handle *trans, u64 new_dirid,
3951 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003952{
Chris Mason39279cc2007-06-12 06:35:45 -04003953 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003954 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003955 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003956
Josef Bacikaec74772008-07-24 12:12:38 -04003957 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003958 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003959 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003960 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003961 inode->i_op = &btrfs_dir_inode_operations;
3962 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003963 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003964
Chris Mason39279cc2007-06-12 06:35:45 -04003965 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003966 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003967
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003968 error = btrfs_update_inode(trans, new_root, inode);
3969 if (error)
3970 return error;
3971
3972 d_instantiate(dentry, inode);
3973 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003974}
3975
Chris Masond352ac62008-09-29 15:18:18 -04003976/* helper function for file defrag and space balancing. This
3977 * forces readahead on a given range of bytes in an inode
3978 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003979unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003980 struct file_ra_state *ra, struct file *file,
3981 pgoff_t offset, pgoff_t last_index)
3982{
Chris Mason8e7bf942008-04-28 09:02:36 -04003983 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003984
Chris Mason86479a02007-09-10 19:58:16 -04003985 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3986 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04003987}
3988
Chris Mason39279cc2007-06-12 06:35:45 -04003989struct inode *btrfs_alloc_inode(struct super_block *sb)
3990{
3991 struct btrfs_inode *ei;
3992
3993 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3994 if (!ei)
3995 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003996 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003997 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003998 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003999 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4000 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004001 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004002 return &ei->vfs_inode;
4003}
4004
4005void btrfs_destroy_inode(struct inode *inode)
4006{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004007 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004008 WARN_ON(!list_empty(&inode->i_dentry));
4009 WARN_ON(inode->i_data.nrpages);
4010
Josef Bacik33268ea2008-07-24 12:16:36 -04004011 if (BTRFS_I(inode)->i_acl &&
4012 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4013 posix_acl_release(BTRFS_I(inode)->i_acl);
4014 if (BTRFS_I(inode)->i_default_acl &&
4015 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4016 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4017
Yanbcc63ab2008-07-30 16:29:20 -04004018 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004019 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4020 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4021 " list\n", inode->i_ino);
4022 dump_stack();
4023 }
Yanbcc63ab2008-07-30 16:29:20 -04004024 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004025
Chris Masone6dcd2d2008-07-17 12:53:50 -04004026 while(1) {
4027 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4028 if (!ordered)
4029 break;
4030 else {
4031 printk("found ordered extent %Lu %Lu\n",
4032 ordered->file_offset, ordered->len);
4033 btrfs_remove_ordered_extent(inode, ordered);
4034 btrfs_put_ordered_extent(ordered);
4035 btrfs_put_ordered_extent(ordered);
4036 }
4037 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004038 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004039 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4040}
4041
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004042static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004043{
4044 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4045
4046 inode_init_once(&ei->vfs_inode);
4047}
4048
4049void btrfs_destroy_cachep(void)
4050{
4051 if (btrfs_inode_cachep)
4052 kmem_cache_destroy(btrfs_inode_cachep);
4053 if (btrfs_trans_handle_cachep)
4054 kmem_cache_destroy(btrfs_trans_handle_cachep);
4055 if (btrfs_transaction_cachep)
4056 kmem_cache_destroy(btrfs_transaction_cachep);
4057 if (btrfs_bit_radix_cachep)
4058 kmem_cache_destroy(btrfs_bit_radix_cachep);
4059 if (btrfs_path_cachep)
4060 kmem_cache_destroy(btrfs_path_cachep);
4061}
4062
Chris Mason86479a02007-09-10 19:58:16 -04004063struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004064 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004065 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004066{
4067 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004068 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004069}
4070
Chris Mason39279cc2007-06-12 06:35:45 -04004071int btrfs_init_cachep(void)
4072{
Chris Mason86479a02007-09-10 19:58:16 -04004073 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004074 sizeof(struct btrfs_inode),
4075 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004076 if (!btrfs_inode_cachep)
4077 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004078 btrfs_trans_handle_cachep =
4079 btrfs_cache_create("btrfs_trans_handle_cache",
4080 sizeof(struct btrfs_trans_handle),
4081 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004082 if (!btrfs_trans_handle_cachep)
4083 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004084 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004085 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004086 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004087 if (!btrfs_transaction_cachep)
4088 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004089 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004090 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004091 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004092 if (!btrfs_path_cachep)
4093 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004094 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004095 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004096 if (!btrfs_bit_radix_cachep)
4097 goto fail;
4098 return 0;
4099fail:
4100 btrfs_destroy_cachep();
4101 return -ENOMEM;
4102}
4103
4104static int btrfs_getattr(struct vfsmount *mnt,
4105 struct dentry *dentry, struct kstat *stat)
4106{
4107 struct inode *inode = dentry->d_inode;
4108 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05004109 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004110 stat->blocks = (inode_get_bytes(inode) +
4111 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004112 return 0;
4113}
4114
4115static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
4116 struct inode * new_dir,struct dentry *new_dentry)
4117{
4118 struct btrfs_trans_handle *trans;
4119 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4120 struct inode *new_inode = new_dentry->d_inode;
4121 struct inode *old_inode = old_dentry->d_inode;
4122 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004123 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004124 int ret;
4125
4126 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4127 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4128 return -ENOTEMPTY;
4129 }
Chris Mason5f39d392007-10-15 16:14:19 -04004130
Chris Mason1832a6d2007-12-21 16:27:21 -05004131 ret = btrfs_check_free_space(root, 1, 0);
4132 if (ret)
4133 goto out_unlock;
4134
Chris Mason39279cc2007-06-12 06:35:45 -04004135 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004136
Chris Mason39279cc2007-06-12 06:35:45 -04004137 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004138
Chris Masone02119d2008-09-05 16:13:11 -04004139 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004140 old_dir->i_ctime = old_dir->i_mtime = ctime;
4141 new_dir->i_ctime = new_dir->i_mtime = ctime;
4142 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004143
Chris Masone02119d2008-09-05 16:13:11 -04004144 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4145 old_dentry->d_name.name,
4146 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004147 if (ret)
4148 goto out_fail;
4149
4150 if (new_inode) {
4151 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004152 ret = btrfs_unlink_inode(trans, root, new_dir,
4153 new_dentry->d_inode,
4154 new_dentry->d_name.name,
4155 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004156 if (ret)
4157 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004158 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004159 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004160 if (ret)
4161 goto out_fail;
4162 }
Chris Masone02119d2008-09-05 16:13:11 -04004163
Chris Mason39279cc2007-06-12 06:35:45 -04004164 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04004165 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004166 if (ret)
4167 goto out_fail;
4168
Chris Masone02119d2008-09-05 16:13:11 -04004169 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4170 old_inode, new_dentry->d_name.name,
4171 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004172 if (ret)
4173 goto out_fail;
4174
4175out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004176 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004177out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004178 return ret;
4179}
4180
Chris Masond352ac62008-09-29 15:18:18 -04004181/*
4182 * some fairly slow code that needs optimization. This walks the list
4183 * of all the inodes with pending delalloc and forces them to disk.
4184 */
Chris Masonea8c2812008-08-04 23:17:27 -04004185int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4186{
4187 struct list_head *head = &root->fs_info->delalloc_inodes;
4188 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004189 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004190 unsigned long flags;
4191
4192 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4193 while(!list_empty(head)) {
4194 binode = list_entry(head->next, struct btrfs_inode,
4195 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004196 inode = igrab(&binode->vfs_inode);
4197 if (!inode)
4198 list_del_init(&binode->delalloc_inodes);
Chris Masonea8c2812008-08-04 23:17:27 -04004199 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004200 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004201 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004202 iput(inode);
4203 }
4204 cond_resched();
Chris Masonea8c2812008-08-04 23:17:27 -04004205 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4206 }
4207 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason8c8bee12008-09-29 11:19:10 -04004208
4209 /* the filemap_flush will queue IO into the worker threads, but
4210 * we have to make sure the IO is actually started and that
4211 * ordered extents get created before we return
4212 */
4213 atomic_inc(&root->fs_info->async_submit_draining);
4214 while(atomic_read(&root->fs_info->nr_async_submits)) {
4215 wait_event(root->fs_info->async_submit_wait,
4216 (atomic_read(&root->fs_info->nr_async_submits) == 0));
4217 }
4218 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004219 return 0;
4220}
4221
Chris Mason39279cc2007-06-12 06:35:45 -04004222static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4223 const char *symname)
4224{
4225 struct btrfs_trans_handle *trans;
4226 struct btrfs_root *root = BTRFS_I(dir)->root;
4227 struct btrfs_path *path;
4228 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004229 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004230 int err;
4231 int drop_inode = 0;
4232 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004233 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004234 int name_len;
4235 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004236 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004237 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004238 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004239 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004240
4241 name_len = strlen(symname) + 1;
4242 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4243 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004244
Chris Mason1832a6d2007-12-21 16:27:21 -05004245 err = btrfs_check_free_space(root, 1, 0);
4246 if (err)
4247 goto out_fail;
4248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 trans = btrfs_start_transaction(root, 1);
4250 btrfs_set_trans_block_group(trans, dir);
4251
4252 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4253 if (err) {
4254 err = -ENOSPC;
4255 goto out_unlock;
4256 }
4257
Josef Bacikaec74772008-07-24 12:12:38 -04004258 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004259 dentry->d_name.len,
4260 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004261 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4262 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004263 err = PTR_ERR(inode);
4264 if (IS_ERR(inode))
4265 goto out_unlock;
4266
Josef Bacik33268ea2008-07-24 12:16:36 -04004267 err = btrfs_init_acl(inode, dir);
4268 if (err) {
4269 drop_inode = 1;
4270 goto out_unlock;
4271 }
4272
Chris Mason39279cc2007-06-12 06:35:45 -04004273 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004274 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004275 if (err)
4276 drop_inode = 1;
4277 else {
4278 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004279 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004280 inode->i_fop = &btrfs_file_operations;
4281 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004282 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004283 }
4284 dir->i_sb->s_dirt = 1;
4285 btrfs_update_inode_block_group(trans, inode);
4286 btrfs_update_inode_block_group(trans, dir);
4287 if (drop_inode)
4288 goto out_unlock;
4289
4290 path = btrfs_alloc_path();
4291 BUG_ON(!path);
4292 key.objectid = inode->i_ino;
4293 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004294 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4295 datasize = btrfs_file_extent_calc_inline_size(name_len);
4296 err = btrfs_insert_empty_item(trans, root, path, &key,
4297 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004298 if (err) {
4299 drop_inode = 1;
4300 goto out_unlock;
4301 }
Chris Mason5f39d392007-10-15 16:14:19 -04004302 leaf = path->nodes[0];
4303 ei = btrfs_item_ptr(leaf, path->slots[0],
4304 struct btrfs_file_extent_item);
4305 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4306 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004307 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004308 btrfs_set_file_extent_encryption(leaf, ei, 0);
4309 btrfs_set_file_extent_compression(leaf, ei, 0);
4310 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4311 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4312
Chris Mason39279cc2007-06-12 06:35:45 -04004313 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004314 write_extent_buffer(leaf, symname, ptr, name_len);
4315 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004316 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004317
Chris Mason39279cc2007-06-12 06:35:45 -04004318 inode->i_op = &btrfs_symlink_inode_operations;
4319 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004320 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04004321 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004322 err = btrfs_update_inode(trans, root, inode);
4323 if (err)
4324 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004325
4326out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004327 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004328 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004329out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004330 if (drop_inode) {
4331 inode_dec_link_count(inode);
4332 iput(inode);
4333 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004334 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004335 return err;
4336}
Chris Mason16432982008-04-10 10:23:21 -04004337
Chris Masone6dcd2d2008-07-17 12:53:50 -04004338static int btrfs_set_page_dirty(struct page *page)
4339{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004340 return __set_page_dirty_nobuffers(page);
4341}
4342
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004343static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004344{
4345 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4346 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004347 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004348}
Chris Mason39279cc2007-06-12 06:35:45 -04004349
4350static struct inode_operations btrfs_dir_inode_operations = {
4351 .lookup = btrfs_lookup,
4352 .create = btrfs_create,
4353 .unlink = btrfs_unlink,
4354 .link = btrfs_link,
4355 .mkdir = btrfs_mkdir,
4356 .rmdir = btrfs_rmdir,
4357 .rename = btrfs_rename,
4358 .symlink = btrfs_symlink,
4359 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004360 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004361 .setxattr = btrfs_setxattr,
4362 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004363 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004364 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004365 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004366};
Chris Mason39279cc2007-06-12 06:35:45 -04004367static struct inode_operations btrfs_dir_ro_inode_operations = {
4368 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05004369 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004370};
Chris Mason39279cc2007-06-12 06:35:45 -04004371static struct file_operations btrfs_dir_file_operations = {
4372 .llseek = generic_file_llseek,
4373 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004374 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004375 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004376#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004377 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004378#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004379 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004380 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004381};
4382
Chris Masond1310b22008-01-24 16:13:08 -05004383static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04004384 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05004385 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004386 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004387 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004388 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04004389 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04004390 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05004391 .set_bit_hook = btrfs_set_bit_hook,
4392 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004393};
4394
Chris Mason39279cc2007-06-12 06:35:45 -04004395static struct address_space_operations btrfs_aops = {
4396 .readpage = btrfs_readpage,
4397 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04004398 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05004399 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04004400 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04004401 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04004402 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04004403 .invalidatepage = btrfs_invalidatepage,
4404 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004405 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04004406};
4407
4408static struct address_space_operations btrfs_symlink_aops = {
4409 .readpage = btrfs_readpage,
4410 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04004411 .invalidatepage = btrfs_invalidatepage,
4412 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04004413};
4414
4415static struct inode_operations btrfs_file_inode_operations = {
4416 .truncate = btrfs_truncate,
4417 .getattr = btrfs_getattr,
4418 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004419 .setxattr = btrfs_setxattr,
4420 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004421 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004422 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004423 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004424};
Josef Bacik618e21d2007-07-11 10:18:17 -04004425static struct inode_operations btrfs_special_inode_operations = {
4426 .getattr = btrfs_getattr,
4427 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05004428 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004429 .setxattr = btrfs_setxattr,
4430 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04004431 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004432 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004433};
Chris Mason39279cc2007-06-12 06:35:45 -04004434static struct inode_operations btrfs_symlink_inode_operations = {
4435 .readlink = generic_readlink,
4436 .follow_link = page_follow_link_light,
4437 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05004438 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004439};