blob: 9797592dc86b7c7b8398a18f61a892d70a86c24d [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
249 mutex_lock(&BTRFS_I(inode)->extent_mutex);
250 ret = btrfs_drop_extents(trans, root, inode, start,
251 aligned_end, aligned_end, &hint_byte);
252 BUG_ON(ret);
253
254 if (isize > actual_end)
255 inline_len = min_t(u64, isize, actual_end);
256 ret = insert_inline_extent(trans, root, inode, start,
257 inline_len, compressed_size,
258 compressed_pages);
259 BUG_ON(ret);
260 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
261 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
262 return 0;
263}
264
265/*
Chris Masond352ac62008-09-29 15:18:18 -0400266 * when extent_io.c finds a delayed allocation range in the file,
267 * the call backs end up in this code. The basic idea is to
268 * allocate extents on disk for the range, and create ordered data structs
269 * in ram to track those extents.
Chris Masonc8b97812008-10-29 14:49:59 -0400270 *
271 * locked_page is the page that writepage had locked already. We use
272 * it to make sure we don't do extra locks or unlocks.
273 *
274 * *page_started is set to one if we unlock locked_page and do everything
275 * required to start IO on it. It may be clean and already done with
276 * IO when we return.
Chris Masond352ac62008-09-29 15:18:18 -0400277 */
Chris Masonc8b97812008-10-29 14:49:59 -0400278static int cow_file_range(struct inode *inode, struct page *locked_page,
279 u64 start, u64 end, int *page_started)
Chris Masonb888db22007-08-27 16:49:44 -0400280{
281 struct btrfs_root *root = BTRFS_I(inode)->root;
282 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400283 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400284 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400285 unsigned long ram_size;
286 u64 orig_start;
287 u64 disk_num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500288 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400289 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400290 u64 actual_end;
Chris Masonbe20aa92007-12-17 20:14:01 -0500291 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400292 struct extent_map *em;
293 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
294 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400295 struct page **pages = NULL;
296 unsigned long nr_pages;
297 unsigned long nr_pages_ret = 0;
298 unsigned long total_compressed = 0;
299 unsigned long total_in = 0;
300 unsigned long max_compressed = 128 * 1024;
301 unsigned long max_uncompressed = 256 * 1024;
302 int i;
303 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400304
Chris Masonf9295742008-07-17 12:54:14 -0400305 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400306 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500307 btrfs_set_trans_block_group(trans, inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400308 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500309
Chris Masonc8b97812008-10-29 14:49:59 -0400310 /*
311 * compression made this loop a bit ugly, but the basic idea is to
312 * compress some pages but keep the total size of the compressed
313 * extent relatively small. If compression is off, this goto target
314 * is never used.
315 */
316again:
317 will_compress = 0;
318 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
319 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
320
321 actual_end = min_t(u64, i_size_read(inode), end + 1);
322 total_compressed = actual_end - start;
323
324 /* we want to make sure that amount of ram required to uncompress
325 * an extent is reasonable, so we limit the total size in ram
326 * of a compressed extent to 256k
327 */
328 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400329 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500330 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400331 disk_num_bytes = num_bytes;
332 total_in = 0;
333 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400334
Chris Masonc8b97812008-10-29 14:49:59 -0400335 /* we do compression for mount -o compress and when the
336 * inode has not been flagged as nocompress
337 */
338 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
339 btrfs_test_opt(root, COMPRESS)) {
340 WARN_ON(pages);
341 pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400342
Chris Masonc8b97812008-10-29 14:49:59 -0400343 /* we want to make sure the amount of IO required to satisfy
344 * a random read is reasonably small, so we limit the size
345 * of a compressed extent to 128k
346 */
347 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
348 total_compressed, pages,
349 nr_pages, &nr_pages_ret,
350 &total_in,
351 &total_compressed,
352 max_compressed);
353
354 if (!ret) {
355 unsigned long offset = total_compressed &
356 (PAGE_CACHE_SIZE - 1);
357 struct page *page = pages[nr_pages_ret - 1];
358 char *kaddr;
359
360 /* zero the tail end of the last page, we might be
361 * sending it down to disk
362 */
363 if (offset) {
364 kaddr = kmap_atomic(page, KM_USER0);
365 memset(kaddr + offset, 0,
366 PAGE_CACHE_SIZE - offset);
367 kunmap_atomic(kaddr, KM_USER0);
368 }
369 will_compress = 1;
370 }
371 }
372 if (start == 0) {
373 /* lets try to make an inline extent */
374 if (ret || total_in < (end - start + 1)) {
375 /* we didn't compress the entire range, try
376 * to make an uncompressed inline extent. This
377 * is almost sure to fail, but maybe inline sizes
378 * will get bigger later
379 */
380 ret = cow_file_range_inline(trans, root, inode,
381 start, end, 0, NULL);
382 } else {
383 ret = cow_file_range_inline(trans, root, inode,
384 start, end,
385 total_compressed, pages);
386 }
387 if (ret == 0) {
388 extent_clear_unlock_delalloc(inode,
389 &BTRFS_I(inode)->io_tree,
390 start, end, NULL,
391 1, 1, 1);
392 *page_started = 1;
393 ret = 0;
394 goto free_pages_out;
395 }
396 }
397
398 if (will_compress) {
399 /*
400 * we aren't doing an inline extent round the compressed size
401 * up to a block size boundary so the allocator does sane
402 * things
403 */
404 total_compressed = (total_compressed + blocksize - 1) &
405 ~(blocksize - 1);
406
407 /*
408 * one last check to make sure the compression is really a
409 * win, compare the page count read with the blocks on disk
410 */
411 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
412 ~(PAGE_CACHE_SIZE - 1);
413 if (total_compressed >= total_in) {
414 will_compress = 0;
415 } else {
416 disk_num_bytes = total_compressed;
417 num_bytes = total_in;
418 }
419 }
420 if (!will_compress && pages) {
421 /*
422 * the compression code ran but failed to make things smaller,
423 * free any pages it allocated and our page pointer array
424 */
425 for (i = 0; i < nr_pages_ret; i++) {
426 page_cache_release(pages[i]);
427 }
428 kfree(pages);
429 pages = NULL;
430 total_compressed = 0;
431 nr_pages_ret = 0;
432
433 /* flag the file so we don't compress in the future */
434 btrfs_set_flag(inode, NOCOMPRESS);
435 }
436
437 BUG_ON(disk_num_bytes >
438 btrfs_super_total_bytes(&root->fs_info->super_copy));
439
Chris Masone5a22172008-07-18 20:42:20 -0400440 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400441 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Masone5a22172008-07-18 20:42:20 -0400442 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400443
Chris Masonc8b97812008-10-29 14:49:59 -0400444 while(disk_num_bytes > 0) {
445 unsigned long min_bytes;
446
447 /*
448 * the max size of a compressed extent is pretty small,
449 * make the code a little less complex by forcing
450 * the allocator to find a whole compressed extent at once
451 */
452 if (will_compress)
453 min_bytes = disk_num_bytes;
454 else
455 min_bytes = root->sectorsize;
456
457 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400458 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400459 min_bytes, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400460 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500461 if (ret) {
462 WARN_ON(1);
Chris Masonc8b97812008-10-29 14:49:59 -0400463 goto free_pages_out_fail;
Chris Masonc59f8952007-12-17 20:14:04 -0500464 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400465 em = alloc_extent_map(GFP_NOFS);
466 em->start = start;
Chris Masonc8b97812008-10-29 14:49:59 -0400467
468 if (will_compress) {
469 ram_size = num_bytes;
470 em->len = num_bytes;
471 } else {
472 /* ramsize == disk size */
473 ram_size = ins.offset;
474 em->len = ins.offset;
475 }
476
Chris Masone6dcd2d2008-07-17 12:53:50 -0400477 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400478 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400479 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masonc8b97812008-10-29 14:49:59 -0400480
Chris Masone5a22172008-07-18 20:42:20 -0400481 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400482 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400483
484 if (will_compress)
485 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
486
Chris Masone6dcd2d2008-07-17 12:53:50 -0400487 while(1) {
488 spin_lock(&em_tree->lock);
489 ret = add_extent_mapping(em_tree, em);
490 spin_unlock(&em_tree->lock);
491 if (ret != -EEXIST) {
492 free_extent_map(em);
493 break;
494 }
495 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400496 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400497 }
Chris Masone5a22172008-07-18 20:42:20 -0400498 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400499
Chris Mason98d20f62008-04-14 09:46:10 -0400500 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400501 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Masonc8b97812008-10-29 14:49:59 -0400502 ram_size, cur_alloc_size, 0,
503 will_compress);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400504 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400505
506 if (disk_num_bytes < cur_alloc_size) {
507 printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
Chris Mason3b951512008-04-17 11:29:12 -0400508 cur_alloc_size);
509 break;
510 }
Chris Masonc8b97812008-10-29 14:49:59 -0400511
512 if (will_compress) {
513 /*
514 * we're doing compression, we and we need to
515 * submit the compressed extents down to the device.
516 *
517 * We lock down all the file pages, clearing their
518 * dirty bits and setting them writeback. Everyone
519 * that wants to modify the page will wait on the
520 * ordered extent above.
521 *
522 * The writeback bits on the file pages are
523 * cleared when the compressed pages are on disk
524 */
525 btrfs_end_transaction(trans, root);
526
527 if (start <= page_offset(locked_page) &&
528 page_offset(locked_page) < start + ram_size) {
529 *page_started = 1;
530 }
531
532 extent_clear_unlock_delalloc(inode,
533 &BTRFS_I(inode)->io_tree,
534 start,
535 start + ram_size - 1,
536 NULL, 1, 1, 0);
537
538 ret = btrfs_submit_compressed_write(inode, start,
539 ram_size, ins.objectid,
540 cur_alloc_size, pages,
541 nr_pages_ret);
542
543 BUG_ON(ret);
544 trans = btrfs_join_transaction(root, 1);
545 if (start + ram_size < end) {
546 start += ram_size;
547 alloc_hint = ins.objectid + ins.offset;
548 /* pages will be freed at end_bio time */
549 pages = NULL;
550 goto again;
551 } else {
552 /* we've written everything, time to go */
553 break;
554 }
555 }
556 /* we're not doing compressed IO, don't unlock the first
557 * page (which the caller expects to stay locked), don't
558 * clear any dirty bits and don't set any writeback bits
559 */
560 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
561 start, start + ram_size - 1,
562 locked_page, 0, 0, 0);
563 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500564 num_bytes -= cur_alloc_size;
565 alloc_hint = ins.objectid + ins.offset;
566 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400567 }
Chris Masonc8b97812008-10-29 14:49:59 -0400568
569 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400570out:
571 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400572
Chris Masonbe20aa92007-12-17 20:14:01 -0500573 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400574
575free_pages_out_fail:
576 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
577 start, end, locked_page, 0, 0, 0);
578free_pages_out:
579 for (i = 0; i < nr_pages_ret; i++)
580 page_cache_release(pages[i]);
581 if (pages)
582 kfree(pages);
583
584 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500585}
586
Chris Masond352ac62008-09-29 15:18:18 -0400587/*
588 * when nowcow writeback call back. This checks for snapshots or COW copies
589 * of the extents that exist in the file, and COWs the file as required.
590 *
591 * If no cow copies or snapshots exist, we write directly to the existing
592 * blocks on disk
593 */
Chris Masonc8b97812008-10-29 14:49:59 -0400594static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
595 u64 start, u64 end, int *page_started)
Chris Masonbe20aa92007-12-17 20:14:01 -0500596{
597 u64 extent_start;
598 u64 extent_end;
599 u64 bytenr;
Chris Mason1832a6d2007-12-21 16:27:21 -0500600 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500601 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500602 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400603 struct btrfs_block_group_cache *block_group;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400604 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500605 struct extent_buffer *leaf;
606 int found_type;
607 struct btrfs_path *path;
608 struct btrfs_file_extent_item *item;
609 int ret;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400610 int err = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500611 struct btrfs_key found_key;
612
Chris Masonc31f8832008-01-08 15:46:31 -0500613 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500614 path = btrfs_alloc_path();
615 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400616 trans = btrfs_join_transaction(root, 1);
617 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500618again:
619 ret = btrfs_lookup_file_extent(NULL, root, path,
620 inode->i_ino, start, 0);
621 if (ret < 0) {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400622 err = ret;
623 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500624 }
625
Chris Masonbe20aa92007-12-17 20:14:01 -0500626 if (ret != 0) {
627 if (path->slots[0] == 0)
628 goto not_found;
629 path->slots[0]--;
630 }
631
632 leaf = path->nodes[0];
633 item = btrfs_item_ptr(leaf, path->slots[0],
634 struct btrfs_file_extent_item);
635
636 /* are we inside the extent that was found? */
637 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
638 found_type = btrfs_key_type(&found_key);
639 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400640 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500641 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500642
643 found_type = btrfs_file_extent_type(leaf, item);
644 extent_start = found_key.offset;
645 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500646 u64 extent_num_bytes;
647
648 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
649 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500650 err = 0;
651
Chris Masonc8b97812008-10-29 14:49:59 -0400652 if (btrfs_file_extent_compression(leaf, item) ||
653 btrfs_file_extent_encryption(leaf,item) ||
654 btrfs_file_extent_other_encoding(leaf, item))
655 goto not_found;
656
Chris Mason1832a6d2007-12-21 16:27:21 -0500657 if (loops && start != extent_start)
658 goto not_found;
659
Chris Masonbe20aa92007-12-17 20:14:01 -0500660 if (start < extent_start || start >= extent_end)
661 goto not_found;
662
Chris Masonbe20aa92007-12-17 20:14:01 -0500663 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
664 if (bytenr == 0)
665 goto not_found;
666
Yan Zheng7ea394f2008-08-05 13:05:02 -0400667 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400668 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500669 /*
670 * we may be called by the resizer, make sure we're inside
671 * the limits of the FS
672 */
Chris Masona68d5932008-05-08 14:11:56 -0400673 block_group = btrfs_lookup_block_group(root->fs_info,
674 bytenr);
675 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500676 goto not_found;
677
Yan Zheng7ea394f2008-08-05 13:05:02 -0400678 bytenr += btrfs_file_extent_offset(leaf, item);
679 extent_num_bytes = min(end + 1, extent_end) - start;
680 ret = btrfs_add_ordered_extent(inode, start, bytenr,
Chris Masonc8b97812008-10-29 14:49:59 -0400681 extent_num_bytes,
682 extent_num_bytes, 1, 0);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400683 if (ret) {
684 err = ret;
685 goto out;
686 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500687
Yan Zheng7ea394f2008-08-05 13:05:02 -0400688 btrfs_release_path(root, path);
689 start = extent_end;
690 if (start <= end) {
691 loops++;
692 goto again;
693 }
694 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500695not_found:
Yan Zheng7ea394f2008-08-05 13:05:02 -0400696 btrfs_end_transaction(trans, root);
697 btrfs_free_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400698 return cow_file_range(inode, locked_page, start, end,
699 page_started);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400700 }
701out:
702 WARN_ON(err);
703 btrfs_end_transaction(trans, root);
704 btrfs_free_path(path);
705 return err;
Chris Masonbe20aa92007-12-17 20:14:01 -0500706}
707
Chris Masond352ac62008-09-29 15:18:18 -0400708/*
709 * extent_io.c call back to do delayed allocation processing
710 */
Chris Masonc8b97812008-10-29 14:49:59 -0400711static int run_delalloc_range(struct inode *inode, struct page *locked_page,
712 u64 start, u64 end, int *page_started)
Chris Masonbe20aa92007-12-17 20:14:01 -0500713{
714 struct btrfs_root *root = BTRFS_I(inode)->root;
715 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400716
Yanb98b6762008-01-08 15:54:37 -0500717 if (btrfs_test_opt(root, NODATACOW) ||
718 btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -0400719 ret = run_delalloc_nocow(inode, locked_page, start, end,
720 page_started);
Chris Masonbe20aa92007-12-17 20:14:01 -0500721 else
Chris Masonc8b97812008-10-29 14:49:59 -0400722 ret = cow_file_range(inode, locked_page, start, end,
723 page_started);
Chris Mason1832a6d2007-12-21 16:27:21 -0500724
Chris Masonb888db22007-08-27 16:49:44 -0400725 return ret;
726}
727
Chris Masond352ac62008-09-29 15:18:18 -0400728/*
729 * extent_io.c set_bit_hook, used to track delayed allocation
730 * bytes in this file, and to maintain the list of inodes that
731 * have pending delalloc work to be done.
732 */
Chris Mason291d6732008-01-29 15:55:23 -0500733int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500734 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500735{
Chris Masonbcbfce82008-04-22 13:26:47 -0400736 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500737 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500738 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400739 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500740 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500741 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400742 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
743 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
744 &root->fs_info->delalloc_inodes);
745 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400746 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500747 }
748 return 0;
749}
750
Chris Masond352ac62008-09-29 15:18:18 -0400751/*
752 * extent_io.c clear_bit_hook, see set_bit_hook for why
753 */
Chris Mason291d6732008-01-29 15:55:23 -0500754int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500755 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500756{
Chris Masonb0c68f82008-01-31 11:05:37 -0500757 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500758 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400759 unsigned long flags;
760
761 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500762 if (end - start + 1 > root->fs_info->delalloc_bytes) {
763 printk("warning: delalloc account %Lu %Lu\n",
764 end - start + 1, root->fs_info->delalloc_bytes);
765 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500766 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500767 } else {
768 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500769 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500770 }
Chris Masonea8c2812008-08-04 23:17:27 -0400771 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
772 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
773 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
774 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400775 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500776 }
777 return 0;
778}
779
Chris Masond352ac62008-09-29 15:18:18 -0400780/*
781 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
782 * we don't create bios that span stripes or chunks
783 */
Chris Mason239b14b2008-03-24 15:02:07 -0400784int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -0400785 size_t size, struct bio *bio,
786 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -0400787{
788 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
789 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -0400790 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400791 u64 length = 0;
792 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400793 int ret;
794
Chris Masonf2d8d742008-04-21 10:03:05 -0400795 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400796 map_tree = &root->fs_info->mapping_tree;
797 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400798 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400799 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400800
Chris Mason239b14b2008-03-24 15:02:07 -0400801 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400802 return 1;
803 }
804 return 0;
805}
806
Chris Masond352ac62008-09-29 15:18:18 -0400807/*
808 * in order to insert checksums into the metadata in large chunks,
809 * we wait until bio submission time. All the pages in the bio are
810 * checksummed and sums are attached onto the ordered extent record.
811 *
812 * At IO completion time the cums attached on the ordered extent record
813 * are inserted into the btree
814 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400815int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -0400816 int mirror_num, unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -0500817{
Chris Mason065631f2008-02-20 12:07:25 -0500818 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500819 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400820
Chris Mason3edf7d32008-07-18 06:17:13 -0400821 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400822 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400823
Chris Mason8b712842008-06-11 16:50:36 -0400824 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400825}
826
Chris Masond352ac62008-09-29 15:18:18 -0400827/*
828 * extent_io.c submission hook. This does the right thing for csum calculation on write,
829 * or reading the csums from the tree before a read
830 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400831int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -0400832 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -0400833{
834 struct btrfs_root *root = BTRFS_I(inode)->root;
835 int ret = 0;
836
Chris Masone6dcd2d2008-07-17 12:53:50 -0400837 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
838 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500839
Yan Zheng7ea394f2008-08-05 13:05:02 -0400840 if (btrfs_test_opt(root, NODATASUM) ||
841 btrfs_test_flag(inode, NODATASUM)) {
842 goto mapit;
843 }
844
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400845 if (!(rw & (1 << BIO_RW))) {
846 btrfs_lookup_bio_sums(root, inode, bio);
Chris Masonc8b97812008-10-29 14:49:59 -0400847
848 if (bio_flags & EXTENT_BIO_COMPRESSED) {
849 return btrfs_submit_compressed_read(inode, bio,
850 mirror_num, bio_flags);
851 }
852
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400853 goto mapit;
854 }
Chris Mason44b8bd72008-04-16 11:14:51 -0400855 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
856 inode, rw, bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -0400857 bio_flags, __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400858mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400859 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500860}
Chris Mason6885f302008-02-20 16:11:05 -0500861
Chris Masond352ac62008-09-29 15:18:18 -0400862/*
863 * given a list of ordered sums record them in the inode. This happens
864 * at IO completion time based on sums calculated at bio submission time.
865 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400866static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400867 struct inode *inode, u64 file_offset,
868 struct list_head *list)
869{
870 struct list_head *cur;
871 struct btrfs_ordered_sum *sum;
872
873 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400874 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400875 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400876 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
877 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400878 }
879 return 0;
880}
881
Chris Masonea8c2812008-08-04 23:17:27 -0400882int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
883{
884 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
885 GFP_NOFS);
886}
887
Chris Masond352ac62008-09-29 15:18:18 -0400888/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -0400889struct btrfs_writepage_fixup {
890 struct page *page;
891 struct btrfs_work work;
892};
893
Chris Mason247e7432008-07-17 12:53:51 -0400894void btrfs_writepage_fixup_worker(struct btrfs_work *work)
895{
896 struct btrfs_writepage_fixup *fixup;
897 struct btrfs_ordered_extent *ordered;
898 struct page *page;
899 struct inode *inode;
900 u64 page_start;
901 u64 page_end;
902
903 fixup = container_of(work, struct btrfs_writepage_fixup, work);
904 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400905again:
Chris Mason247e7432008-07-17 12:53:51 -0400906 lock_page(page);
907 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
908 ClearPageChecked(page);
909 goto out_page;
910 }
911
912 inode = page->mapping->host;
913 page_start = page_offset(page);
914 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
915
916 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400917
918 /* already ordered? We're done */
919 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
920 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400921 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400922 }
923
924 ordered = btrfs_lookup_ordered_extent(inode, page_start);
925 if (ordered) {
926 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
927 page_end, GFP_NOFS);
928 unlock_page(page);
929 btrfs_start_ordered_extent(inode, ordered, 1);
930 goto again;
931 }
Chris Mason247e7432008-07-17 12:53:51 -0400932
Chris Masonea8c2812008-08-04 23:17:27 -0400933 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400934 ClearPageChecked(page);
935out:
936 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
937out_page:
938 unlock_page(page);
939 page_cache_release(page);
940}
941
942/*
943 * There are a few paths in the higher layers of the kernel that directly
944 * set the page dirty bit without asking the filesystem if it is a
945 * good idea. This causes problems because we want to make sure COW
946 * properly happens and the data=ordered rules are followed.
947 *
Chris Masonc8b97812008-10-29 14:49:59 -0400948 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -0400949 * hasn't been properly setup for IO. We kick off an async process
950 * to fix it up. The async helper will wait for ordered extents, set
951 * the delalloc bit and make it safe to write the page.
952 */
953int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
954{
955 struct inode *inode = page->mapping->host;
956 struct btrfs_writepage_fixup *fixup;
957 struct btrfs_root *root = BTRFS_I(inode)->root;
958 int ret;
959
960 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
961 EXTENT_ORDERED, 0);
962 if (ret)
963 return 0;
964
965 if (PageChecked(page))
966 return -EAGAIN;
967
968 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
969 if (!fixup)
970 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400971
Chris Mason247e7432008-07-17 12:53:51 -0400972 SetPageChecked(page);
973 page_cache_get(page);
974 fixup->work.func = btrfs_writepage_fixup_worker;
975 fixup->page = page;
976 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
977 return -EAGAIN;
978}
979
Chris Masond352ac62008-09-29 15:18:18 -0400980/* as ordered data IO finishes, this gets called so we can finish
981 * an ordered extent if the range of bytes in the file it covers are
982 * fully written.
983 */
Chris Mason211f90e2008-07-18 11:56:15 -0400984static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400986 struct btrfs_root *root = BTRFS_I(inode)->root;
987 struct btrfs_trans_handle *trans;
988 struct btrfs_ordered_extent *ordered_extent;
989 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Zheng Yan31840ae2008-09-23 13:14:14 -0400990 struct btrfs_file_extent_item *extent_item;
991 struct btrfs_path *path = NULL;
992 struct extent_buffer *leaf;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400993 u64 alloc_hint = 0;
994 struct list_head list;
995 struct btrfs_key ins;
996 int ret;
997
998 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400999 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001000 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001001
Chris Masonf9295742008-07-17 12:54:14 -04001002 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003
1004 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1005 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001006 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1007 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001008
Zheng Yan31840ae2008-09-23 13:14:14 -04001009 path = btrfs_alloc_path();
1010 BUG_ON(!path);
1011
Chris Masone6dcd2d2008-07-17 12:53:50 -04001012 lock_extent(io_tree, ordered_extent->file_offset,
1013 ordered_extent->file_offset + ordered_extent->len - 1,
1014 GFP_NOFS);
1015
1016 INIT_LIST_HEAD(&list);
1017
Chris Masonee6e6502008-07-17 12:54:40 -04001018 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -04001019
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020 ret = btrfs_drop_extents(trans, root, inode,
1021 ordered_extent->file_offset,
1022 ordered_extent->file_offset +
1023 ordered_extent->len,
1024 ordered_extent->file_offset, &alloc_hint);
1025 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04001026
1027 ins.objectid = inode->i_ino;
1028 ins.offset = ordered_extent->file_offset;
1029 ins.type = BTRFS_EXTENT_DATA_KEY;
1030 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1031 sizeof(*extent_item));
Chris Masone6dcd2d2008-07-17 12:53:50 -04001032 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -04001033 leaf = path->nodes[0];
1034 extent_item = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_file_extent_item);
1036 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
1037 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
1038 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
1039 ordered_extent->start);
1040 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
Chris Masonc8b97812008-10-29 14:49:59 -04001041 ordered_extent->disk_len);
Zheng Yan31840ae2008-09-23 13:14:14 -04001042 btrfs_set_file_extent_offset(leaf, extent_item, 0);
Chris Masonc8b97812008-10-29 14:49:59 -04001043
1044 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1045 btrfs_set_file_extent_compression(leaf, extent_item, 1);
1046 else
1047 btrfs_set_file_extent_compression(leaf, extent_item, 0);
1048 btrfs_set_file_extent_encryption(leaf, extent_item, 0);
1049 btrfs_set_file_extent_other_encoding(leaf, extent_item, 0);
1050
1051 /* ram bytes = extent_num_bytes for now */
Zheng Yan31840ae2008-09-23 13:14:14 -04001052 btrfs_set_file_extent_num_bytes(leaf, extent_item,
1053 ordered_extent->len);
Chris Masonc8b97812008-10-29 14:49:59 -04001054 btrfs_set_file_extent_ram_bytes(leaf, extent_item,
1055 ordered_extent->len);
Zheng Yan31840ae2008-09-23 13:14:14 -04001056 btrfs_mark_buffer_dirty(leaf);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001057
Chris Masone6dcd2d2008-07-17 12:53:50 -04001058 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
1059 ordered_extent->file_offset +
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001060 ordered_extent->len - 1, 0);
Chris Masonee6e6502008-07-17 12:54:40 -04001061 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1062
Zheng Yan31840ae2008-09-23 13:14:14 -04001063 ins.objectid = ordered_extent->start;
Chris Masonc8b97812008-10-29 14:49:59 -04001064 ins.offset = ordered_extent->disk_len;
Zheng Yan31840ae2008-09-23 13:14:14 -04001065 ins.type = BTRFS_EXTENT_ITEM_KEY;
1066 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1067 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001068 trans->transid, inode->i_ino, &ins);
Zheng Yan31840ae2008-09-23 13:14:14 -04001069 BUG_ON(ret);
1070 btrfs_release_path(root, path);
1071
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001072 inode_add_bytes(inode, ordered_extent->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001073 unlock_extent(io_tree, ordered_extent->file_offset,
1074 ordered_extent->file_offset + ordered_extent->len - 1,
1075 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001076nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001077 add_pending_csums(trans, inode, ordered_extent->file_offset,
1078 &ordered_extent->list);
1079
Chris Mason34353022008-09-23 20:19:49 -04001080 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001081 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001082 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001083 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001084 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001085
Chris Masone6dcd2d2008-07-17 12:53:50 -04001086 /* once for us */
1087 btrfs_put_ordered_extent(ordered_extent);
1088 /* once for the tree */
1089 btrfs_put_ordered_extent(ordered_extent);
1090
Chris Masone6dcd2d2008-07-17 12:53:50 -04001091 btrfs_end_transaction(trans, root);
Zheng Yan31840ae2008-09-23 13:14:14 -04001092 if (path)
1093 btrfs_free_path(path);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001094 return 0;
1095}
1096
Chris Mason211f90e2008-07-18 11:56:15 -04001097int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1098 struct extent_state *state, int uptodate)
1099{
1100 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1101}
1102
Chris Masond352ac62008-09-29 15:18:18 -04001103/*
1104 * When IO fails, either with EIO or csum verification fails, we
1105 * try other mirrors that might have a good copy of the data. This
1106 * io_failure_record is used to record state as we go through all the
1107 * mirrors. If another mirror has good data, the page is set up to date
1108 * and things continue. If a good mirror can't be found, the original
1109 * bio end_io callback is called to indicate things have failed.
1110 */
Chris Mason7e383262008-04-09 16:28:12 -04001111struct io_failure_record {
1112 struct page *page;
1113 u64 start;
1114 u64 len;
1115 u64 logical;
1116 int last_mirror;
1117};
1118
Chris Mason1259ab72008-05-12 13:39:03 -04001119int btrfs_io_failed_hook(struct bio *failed_bio,
1120 struct page *page, u64 start, u64 end,
1121 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001122{
1123 struct io_failure_record *failrec = NULL;
1124 u64 private;
1125 struct extent_map *em;
1126 struct inode *inode = page->mapping->host;
1127 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001128 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001129 struct bio *bio;
1130 int num_copies;
1131 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001132 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001133 u64 logical;
Chris Masonc8b97812008-10-29 14:49:59 -04001134 unsigned long bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001135
1136 ret = get_state_private(failure_tree, start, &private);
1137 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001138 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1139 if (!failrec)
1140 return -ENOMEM;
1141 failrec->start = start;
1142 failrec->len = end - start + 1;
1143 failrec->last_mirror = 0;
1144
Chris Mason3b951512008-04-17 11:29:12 -04001145 spin_lock(&em_tree->lock);
1146 em = lookup_extent_mapping(em_tree, start, failrec->len);
1147 if (em->start > start || em->start + em->len < start) {
1148 free_extent_map(em);
1149 em = NULL;
1150 }
1151 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001152
1153 if (!em || IS_ERR(em)) {
1154 kfree(failrec);
1155 return -EIO;
1156 }
1157 logical = start - em->start;
1158 logical = em->block_start + logical;
Chris Masonc8b97812008-10-29 14:49:59 -04001159 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1160 bio_flags = EXTENT_BIO_COMPRESSED;
Chris Mason7e383262008-04-09 16:28:12 -04001161 failrec->logical = logical;
1162 free_extent_map(em);
1163 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1164 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001165 set_state_private(failure_tree, start,
1166 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001167 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001168 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001169 }
1170 num_copies = btrfs_num_copies(
1171 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1172 failrec->logical, failrec->len);
1173 failrec->last_mirror++;
1174 if (!state) {
1175 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
1176 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1177 failrec->start,
1178 EXTENT_LOCKED);
1179 if (state && state->start != failrec->start)
1180 state = NULL;
1181 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
1182 }
1183 if (!state || failrec->last_mirror > num_copies) {
1184 set_state_private(failure_tree, failrec->start, 0);
1185 clear_extent_bits(failure_tree, failrec->start,
1186 failrec->start + failrec->len - 1,
1187 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1188 kfree(failrec);
1189 return -EIO;
1190 }
1191 bio = bio_alloc(GFP_NOFS, 1);
1192 bio->bi_private = state;
1193 bio->bi_end_io = failed_bio->bi_end_io;
1194 bio->bi_sector = failrec->logical >> 9;
1195 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001196 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001197 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001198 if (failed_bio->bi_rw & (1 << BIO_RW))
1199 rw = WRITE;
1200 else
1201 rw = READ;
1202
1203 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001204 failrec->last_mirror,
1205 bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001206 return 0;
1207}
1208
Chris Masond352ac62008-09-29 15:18:18 -04001209/*
1210 * each time an IO finishes, we do a fast check in the IO failure tree
1211 * to see if we need to process or clean up an io_failure_record
1212 */
Chris Mason1259ab72008-05-12 13:39:03 -04001213int btrfs_clean_io_failures(struct inode *inode, u64 start)
1214{
1215 u64 private;
1216 u64 private_failure;
1217 struct io_failure_record *failure;
1218 int ret;
1219
1220 private = 0;
1221 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1222 (u64)-1, 1, EXTENT_DIRTY)) {
1223 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1224 start, &private_failure);
1225 if (ret == 0) {
1226 failure = (struct io_failure_record *)(unsigned long)
1227 private_failure;
1228 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1229 failure->start, 0);
1230 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1231 failure->start,
1232 failure->start + failure->len - 1,
1233 EXTENT_DIRTY | EXTENT_LOCKED,
1234 GFP_NOFS);
1235 kfree(failure);
1236 }
1237 }
Chris Mason7e383262008-04-09 16:28:12 -04001238 return 0;
1239}
1240
Chris Masond352ac62008-09-29 15:18:18 -04001241/*
1242 * when reads are done, we need to check csums to verify the data is correct
1243 * if there's a match, we allow the bio to finish. If not, we go through
1244 * the io_failure_record routines to find good copies
1245 */
Chris Mason70dec802008-01-29 09:59:12 -05001246int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1247 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001248{
Chris Mason35ebb932007-10-30 16:56:53 -04001249 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001250 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001251 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001252 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001253 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001254 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001255 struct btrfs_root *root = BTRFS_I(inode)->root;
1256 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -04001257 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001258
Yanb98b6762008-01-08 15:54:37 -05001259 if (btrfs_test_opt(root, NODATASUM) ||
1260 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001261 return 0;
Yanc2e639f2008-02-04 08:57:25 -05001262 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001263 private = state->private;
1264 ret = 0;
1265 } else {
1266 ret = get_state_private(io_tree, start, &private);
1267 }
Jens Axboebbf0d002007-10-19 09:23:07 -04001268 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -04001269 kaddr = kmap_atomic(page, KM_IRQ0);
1270 if (ret) {
1271 goto zeroit;
1272 }
Chris Masonff79f812007-10-15 16:22:25 -04001273 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1274 btrfs_csum_final(csum, (char *)&csum);
1275 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -04001276 goto zeroit;
1277 }
1278 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -04001279 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -04001280
1281 /* if the io failure tree for this inode is non-empty,
1282 * check to see if we've recovered from a failed IO
1283 */
Chris Mason1259ab72008-05-12 13:39:03 -04001284 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001285 return 0;
1286
1287zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -05001288 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
1289 page->mapping->host->i_ino, (unsigned long long)start, csum,
1290 private);
Chris Masondb945352007-10-15 16:15:53 -04001291 memset(kaddr + offset, 1, end - start + 1);
1292 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -04001293 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -04001294 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -04001295 if (private == 0)
1296 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001297 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001298}
Chris Masonb888db22007-08-27 16:49:44 -04001299
Josef Bacik7b128762008-07-24 12:17:14 -04001300/*
1301 * This creates an orphan entry for the given inode in case something goes
1302 * wrong in the middle of an unlink/truncate.
1303 */
1304int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1305{
1306 struct btrfs_root *root = BTRFS_I(inode)->root;
1307 int ret = 0;
1308
Yanbcc63ab2008-07-30 16:29:20 -04001309 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001310
1311 /* already on the orphan list, we're good */
1312 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001313 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001314 return 0;
1315 }
1316
1317 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1318
Yanbcc63ab2008-07-30 16:29:20 -04001319 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001320
1321 /*
1322 * insert an orphan item to track this unlinked/truncated file
1323 */
1324 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1325
1326 return ret;
1327}
1328
1329/*
1330 * We have done the truncate/delete so we can go ahead and remove the orphan
1331 * item for this particular inode.
1332 */
1333int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1334{
1335 struct btrfs_root *root = BTRFS_I(inode)->root;
1336 int ret = 0;
1337
Yanbcc63ab2008-07-30 16:29:20 -04001338 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001339
1340 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001341 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001342 return 0;
1343 }
1344
1345 list_del_init(&BTRFS_I(inode)->i_orphan);
1346 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001347 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001348 return 0;
1349 }
1350
Yanbcc63ab2008-07-30 16:29:20 -04001351 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001352
1353 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1354
1355 return ret;
1356}
1357
1358/*
1359 * this cleans up any orphans that may be left on the list from the last use
1360 * of this root.
1361 */
1362void btrfs_orphan_cleanup(struct btrfs_root *root)
1363{
1364 struct btrfs_path *path;
1365 struct extent_buffer *leaf;
1366 struct btrfs_item *item;
1367 struct btrfs_key key, found_key;
1368 struct btrfs_trans_handle *trans;
1369 struct inode *inode;
1370 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1371
1372 /* don't do orphan cleanup if the fs is readonly. */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001373 if (root->fs_info->sb->s_flags & MS_RDONLY)
Josef Bacik7b128762008-07-24 12:17:14 -04001374 return;
1375
1376 path = btrfs_alloc_path();
1377 if (!path)
1378 return;
1379 path->reada = -1;
1380
1381 key.objectid = BTRFS_ORPHAN_OBJECTID;
1382 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1383 key.offset = (u64)-1;
1384
Josef Bacik7b128762008-07-24 12:17:14 -04001385
1386 while (1) {
1387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1388 if (ret < 0) {
1389 printk(KERN_ERR "Error searching slot for orphan: %d"
1390 "\n", ret);
1391 break;
1392 }
1393
1394 /*
1395 * if ret == 0 means we found what we were searching for, which
1396 * is weird, but possible, so only screw with path if we didnt
1397 * find the key and see if we have stuff that matches
1398 */
1399 if (ret > 0) {
1400 if (path->slots[0] == 0)
1401 break;
1402 path->slots[0]--;
1403 }
1404
1405 /* pull out the item */
1406 leaf = path->nodes[0];
1407 item = btrfs_item_nr(leaf, path->slots[0]);
1408 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1409
1410 /* make sure the item matches what we want */
1411 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1412 break;
1413 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1414 break;
1415
1416 /* release the path since we're done with it */
1417 btrfs_release_path(root, path);
1418
1419 /*
1420 * this is where we are basically btrfs_lookup, without the
1421 * crossing root thing. we store the inode number in the
1422 * offset of the orphan item.
1423 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001424 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001425 found_key.offset, root);
1426 if (!inode)
1427 break;
1428
1429 if (inode->i_state & I_NEW) {
1430 BTRFS_I(inode)->root = root;
1431
1432 /* have to set the location manually */
1433 BTRFS_I(inode)->location.objectid = inode->i_ino;
1434 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1435 BTRFS_I(inode)->location.offset = 0;
1436
1437 btrfs_read_locked_inode(inode);
1438 unlock_new_inode(inode);
1439 }
1440
1441 /*
1442 * add this inode to the orphan list so btrfs_orphan_del does
1443 * the proper thing when we hit it
1444 */
Yanbcc63ab2008-07-30 16:29:20 -04001445 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001446 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001447 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001448
1449 /*
1450 * if this is a bad inode, means we actually succeeded in
1451 * removing the inode, but not the orphan record, which means
1452 * we need to manually delete the orphan since iput will just
1453 * do a destroy_inode
1454 */
1455 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001456 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001457 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001458 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001459 iput(inode);
1460 continue;
1461 }
1462
1463 /* if we have links, this was a truncate, lets do that */
1464 if (inode->i_nlink) {
1465 nr_truncate++;
1466 btrfs_truncate(inode);
1467 } else {
1468 nr_unlink++;
1469 }
1470
1471 /* this will do delete_inode and everything for us */
1472 iput(inode);
1473 }
1474
1475 if (nr_unlink)
1476 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1477 if (nr_truncate)
1478 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1479
1480 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * read an inode from the btree into the in-memory inode
1485 */
Chris Mason39279cc2007-06-12 06:35:45 -04001486void btrfs_read_locked_inode(struct inode *inode)
1487{
1488 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001489 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001490 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001491 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001492 struct btrfs_root *root = BTRFS_I(inode)->root;
1493 struct btrfs_key location;
1494 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001495 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001496 int ret;
1497
1498 path = btrfs_alloc_path();
1499 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001500 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001501
Chris Mason39279cc2007-06-12 06:35:45 -04001502 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001503 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001504 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001505
Chris Mason5f39d392007-10-15 16:14:19 -04001506 leaf = path->nodes[0];
1507 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1508 struct btrfs_inode_item);
1509
1510 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1511 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1512 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1513 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001514 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001515
1516 tspec = btrfs_inode_atime(inode_item);
1517 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1518 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1519
1520 tspec = btrfs_inode_mtime(inode_item);
1521 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1522 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1523
1524 tspec = btrfs_inode_ctime(inode_item);
1525 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1526 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1527
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001528 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04001529 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1530 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04001531 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001532 rdev = btrfs_inode_rdev(leaf, inode_item);
1533
Josef Bacikaec74772008-07-24 12:12:38 -04001534 BTRFS_I(inode)->index_cnt = (u64)-1;
1535
Chris Mason5f39d392007-10-15 16:14:19 -04001536 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001537 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1538 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001539 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001540 if (!BTRFS_I(inode)->block_group) {
1541 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001542 NULL, 0,
1543 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001544 }
Chris Mason39279cc2007-06-12 06:35:45 -04001545 btrfs_free_path(path);
1546 inode_item = NULL;
1547
Chris Mason39279cc2007-06-12 06:35:45 -04001548 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001549 case S_IFREG:
1550 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001551 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001552 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001553 inode->i_fop = &btrfs_file_operations;
1554 inode->i_op = &btrfs_file_inode_operations;
1555 break;
1556 case S_IFDIR:
1557 inode->i_fop = &btrfs_dir_file_operations;
1558 if (root == root->fs_info->tree_root)
1559 inode->i_op = &btrfs_dir_ro_inode_operations;
1560 else
1561 inode->i_op = &btrfs_dir_inode_operations;
1562 break;
1563 case S_IFLNK:
1564 inode->i_op = &btrfs_symlink_inode_operations;
1565 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001566 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001567 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001568 default:
1569 init_special_inode(inode, inode->i_mode, rdev);
1570 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001571 }
1572 return;
1573
1574make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001575 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001576 make_bad_inode(inode);
1577}
1578
Chris Masond352ac62008-09-29 15:18:18 -04001579/*
1580 * given a leaf and an inode, copy the inode fields into the leaf
1581 */
Chris Masone02119d2008-09-05 16:13:11 -04001582static void fill_inode_item(struct btrfs_trans_handle *trans,
1583 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04001584 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001585 struct inode *inode)
1586{
Chris Mason5f39d392007-10-15 16:14:19 -04001587 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1588 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001589 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001590 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1591 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1592
1593 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1594 inode->i_atime.tv_sec);
1595 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1596 inode->i_atime.tv_nsec);
1597
1598 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1599 inode->i_mtime.tv_sec);
1600 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1601 inode->i_mtime.tv_nsec);
1602
1603 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1604 inode->i_ctime.tv_sec);
1605 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1606 inode->i_ctime.tv_nsec);
1607
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001608 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04001609 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1610 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001611 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001612 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001613 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001614 BTRFS_I(inode)->block_group->key.objectid);
1615}
1616
Chris Masond352ac62008-09-29 15:18:18 -04001617/*
1618 * copy everything in the in-memory inode into the btree.
1619 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001620int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001621 struct btrfs_root *root,
1622 struct inode *inode)
1623{
1624 struct btrfs_inode_item *inode_item;
1625 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001626 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001627 int ret;
1628
1629 path = btrfs_alloc_path();
1630 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001631 ret = btrfs_lookup_inode(trans, root, path,
1632 &BTRFS_I(inode)->location, 1);
1633 if (ret) {
1634 if (ret > 0)
1635 ret = -ENOENT;
1636 goto failed;
1637 }
1638
Chris Mason5f39d392007-10-15 16:14:19 -04001639 leaf = path->nodes[0];
1640 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001641 struct btrfs_inode_item);
1642
Chris Masone02119d2008-09-05 16:13:11 -04001643 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001644 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001645 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001646 ret = 0;
1647failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001648 btrfs_free_path(path);
1649 return ret;
1650}
1651
1652
Chris Masond352ac62008-09-29 15:18:18 -04001653/*
1654 * unlink helper that gets used here in inode.c and in the tree logging
1655 * recovery code. It remove a link in a directory with a given name, and
1656 * also drops the back refs in the inode to the directory
1657 */
Chris Masone02119d2008-09-05 16:13:11 -04001658int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1659 struct btrfs_root *root,
1660 struct inode *dir, struct inode *inode,
1661 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04001662{
1663 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001664 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001665 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001666 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001667 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001668 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001669
1670 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001671 if (!path) {
1672 ret = -ENOMEM;
1673 goto err;
1674 }
1675
Chris Mason39279cc2007-06-12 06:35:45 -04001676 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1677 name, name_len, -1);
1678 if (IS_ERR(di)) {
1679 ret = PTR_ERR(di);
1680 goto err;
1681 }
1682 if (!di) {
1683 ret = -ENOENT;
1684 goto err;
1685 }
Chris Mason5f39d392007-10-15 16:14:19 -04001686 leaf = path->nodes[0];
1687 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001688 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001689 if (ret)
1690 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001691 btrfs_release_path(root, path);
1692
Josef Bacikaec74772008-07-24 12:12:38 -04001693 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001694 inode->i_ino,
1695 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04001696 if (ret) {
1697 printk("failed to delete reference to %.*s, "
1698 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04001699 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04001700 goto err;
1701 }
1702
Chris Mason39279cc2007-06-12 06:35:45 -04001703 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001704 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001705 if (IS_ERR(di)) {
1706 ret = PTR_ERR(di);
1707 goto err;
1708 }
1709 if (!di) {
1710 ret = -ENOENT;
1711 goto err;
1712 }
1713 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001714 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001715
Chris Masone02119d2008-09-05 16:13:11 -04001716 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1717 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04001718 BUG_ON(ret != 0 && ret != -ENOENT);
1719 if (ret != -ENOENT)
1720 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04001721
1722 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1723 dir, index);
1724 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001725err:
1726 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001727 if (ret)
1728 goto out;
1729
1730 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1731 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1732 btrfs_update_inode(trans, root, dir);
1733 btrfs_drop_nlink(inode);
1734 ret = btrfs_update_inode(trans, root, inode);
1735 dir->i_sb->s_dirt = 1;
1736out:
Chris Mason39279cc2007-06-12 06:35:45 -04001737 return ret;
1738}
1739
1740static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1741{
1742 struct btrfs_root *root;
1743 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001744 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001745 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001746 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001747
1748 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001749
1750 ret = btrfs_check_free_space(root, 1, 1);
1751 if (ret)
1752 goto fail;
1753
Chris Mason39279cc2007-06-12 06:35:45 -04001754 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001755
Chris Mason39279cc2007-06-12 06:35:45 -04001756 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04001757 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1758 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04001759
1760 if (inode->i_nlink == 0)
1761 ret = btrfs_orphan_add(trans, inode);
1762
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001763 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001764
Chris Mason89ce8a62008-06-25 16:01:31 -04001765 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001766fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001767 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001768 return ret;
1769}
1770
1771static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1772{
1773 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001774 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001775 int ret;
1776 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001777 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001778 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001779
Chris Mason925baed2008-06-25 16:01:30 -04001780 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001781 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001782 }
Yan134d4512007-10-25 15:49:25 -04001783
Chris Mason1832a6d2007-12-21 16:27:21 -05001784 ret = btrfs_check_free_space(root, 1, 1);
1785 if (ret)
1786 goto fail;
1787
Chris Mason39279cc2007-06-12 06:35:45 -04001788 trans = btrfs_start_transaction(root, 1);
1789 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001790
Josef Bacik7b128762008-07-24 12:17:14 -04001791 err = btrfs_orphan_add(trans, inode);
1792 if (err)
1793 goto fail_trans;
1794
Chris Mason39279cc2007-06-12 06:35:45 -04001795 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04001796 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1797 dentry->d_name.name, dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001798 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001799 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001800 }
Chris Mason39544012007-12-12 14:38:19 -05001801
Josef Bacik7b128762008-07-24 12:17:14 -04001802fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001803 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001804 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001805fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001806 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001807
Chris Mason39279cc2007-06-12 06:35:45 -04001808 if (ret && !err)
1809 err = ret;
1810 return err;
1811}
1812
Chris Mason39279cc2007-06-12 06:35:45 -04001813/*
Chris Mason323ac952008-10-01 19:05:46 -04001814 * when truncating bytes in a file, it is possible to avoid reading
1815 * the leaves that contain only checksum items. This can be the
1816 * majority of the IO required to delete a large file, but it must
1817 * be done carefully.
1818 *
1819 * The keys in the level just above the leaves are checked to make sure
1820 * the lowest key in a given leaf is a csum key, and starts at an offset
1821 * after the new size.
1822 *
1823 * Then the key for the next leaf is checked to make sure it also has
1824 * a checksum item for the same file. If it does, we know our target leaf
1825 * contains only checksum items, and it can be safely freed without reading
1826 * it.
1827 *
1828 * This is just an optimization targeted at large files. It may do
1829 * nothing. It will return 0 unless things went badly.
1830 */
1831static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1832 struct btrfs_root *root,
1833 struct btrfs_path *path,
1834 struct inode *inode, u64 new_size)
1835{
1836 struct btrfs_key key;
1837 int ret;
1838 int nritems;
1839 struct btrfs_key found_key;
1840 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001841 struct btrfs_leaf_ref *ref;
1842 u64 leaf_gen;
1843 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04001844
1845 path->lowest_level = 1;
1846 key.objectid = inode->i_ino;
1847 key.type = BTRFS_CSUM_ITEM_KEY;
1848 key.offset = new_size;
1849again:
1850 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1851 if (ret < 0)
1852 goto out;
1853
1854 if (path->nodes[1] == NULL) {
1855 ret = 0;
1856 goto out;
1857 }
1858 ret = 0;
1859 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1860 nritems = btrfs_header_nritems(path->nodes[1]);
1861
1862 if (!nritems)
1863 goto out;
1864
1865 if (path->slots[1] >= nritems)
1866 goto next_node;
1867
1868 /* did we find a key greater than anything we want to delete? */
1869 if (found_key.objectid > inode->i_ino ||
1870 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1871 goto out;
1872
1873 /* we check the next key in the node to make sure the leave contains
1874 * only checksum items. This comparison doesn't work if our
1875 * leaf is the last one in the node
1876 */
1877 if (path->slots[1] + 1 >= nritems) {
1878next_node:
1879 /* search forward from the last key in the node, this
1880 * will bring us into the next node in the tree
1881 */
1882 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1883
1884 /* unlikely, but we inc below, so check to be safe */
1885 if (found_key.offset == (u64)-1)
1886 goto out;
1887
1888 /* search_forward needs a path with locks held, do the
1889 * search again for the original key. It is possible
1890 * this will race with a balance and return a path that
1891 * we could modify, but this drop is just an optimization
1892 * and is allowed to miss some leaves.
1893 */
1894 btrfs_release_path(root, path);
1895 found_key.offset++;
1896
1897 /* setup a max key for search_forward */
1898 other_key.offset = (u64)-1;
1899 other_key.type = key.type;
1900 other_key.objectid = key.objectid;
1901
1902 path->keep_locks = 1;
1903 ret = btrfs_search_forward(root, &found_key, &other_key,
1904 path, 0, 0);
1905 path->keep_locks = 0;
1906 if (ret || found_key.objectid != key.objectid ||
1907 found_key.type != key.type) {
1908 ret = 0;
1909 goto out;
1910 }
1911
1912 key.offset = found_key.offset;
1913 btrfs_release_path(root, path);
1914 cond_resched();
1915 goto again;
1916 }
1917
1918 /* we know there's one more slot after us in the tree,
1919 * read that key so we can verify it is also a checksum item
1920 */
1921 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
1922
1923 if (found_key.objectid < inode->i_ino)
1924 goto next_key;
1925
1926 if (found_key.type != key.type || found_key.offset < new_size)
1927 goto next_key;
1928
1929 /*
1930 * if the key for the next leaf isn't a csum key from this objectid,
1931 * we can't be sure there aren't good items inside this leaf.
1932 * Bail out
1933 */
1934 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
1935 goto out;
1936
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001937 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
1938 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04001939 /*
1940 * it is safe to delete this leaf, it contains only
1941 * csum items from this inode at an offset >= new_size
1942 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001943 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04001944 BUG_ON(ret);
1945
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001946 if (root->ref_cows && leaf_gen < trans->transid) {
1947 ref = btrfs_alloc_leaf_ref(root, 0);
1948 if (ref) {
1949 ref->root_gen = root->root_key.offset;
1950 ref->bytenr = leaf_start;
1951 ref->owner = 0;
1952 ref->generation = leaf_gen;
1953 ref->nritems = 0;
1954
1955 ret = btrfs_add_leaf_ref(root, ref, 0);
1956 WARN_ON(ret);
1957 btrfs_free_leaf_ref(root, ref);
1958 } else {
1959 WARN_ON(1);
1960 }
1961 }
Chris Mason323ac952008-10-01 19:05:46 -04001962next_key:
1963 btrfs_release_path(root, path);
1964
1965 if (other_key.objectid == inode->i_ino &&
1966 other_key.type == key.type && other_key.offset > key.offset) {
1967 key.offset = other_key.offset;
1968 cond_resched();
1969 goto again;
1970 }
1971 ret = 0;
1972out:
1973 /* fixup any changes we've made to the path */
1974 path->lowest_level = 0;
1975 path->keep_locks = 0;
1976 btrfs_release_path(root, path);
1977 return ret;
1978}
1979
1980/*
Chris Mason39279cc2007-06-12 06:35:45 -04001981 * this can truncate away extent items, csum items and directory items.
1982 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04001983 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04001984 *
1985 * csum items that cross the new i_size are truncated to the new size
1986 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001987 *
1988 * min_type is the minimum key type to truncate down to. If set to 0, this
1989 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001990 */
Chris Masone02119d2008-09-05 16:13:11 -04001991noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1992 struct btrfs_root *root,
1993 struct inode *inode,
1994 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001995{
1996 int ret;
1997 struct btrfs_path *path;
1998 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001999 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002000 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04002001 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002002 struct btrfs_file_extent_item *fi;
2003 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002004 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002005 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002006 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002007 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002008 int found_extent;
2009 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002010 int pending_del_nr = 0;
2011 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002012 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04002013 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002014
Chris Masone02119d2008-09-05 16:13:11 -04002015 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002016 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002017 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002018 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002019 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002020
Chris Mason39279cc2007-06-12 06:35:45 -04002021 /* FIXME, add redo link to tree so we don't leak on crash */
2022 key.objectid = inode->i_ino;
2023 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002024 key.type = (u8)-1;
2025
Chris Mason85e21ba2008-01-29 15:11:36 -05002026 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04002027
2028 ret = drop_csum_leaves(trans, root, path, inode, new_size);
2029 BUG_ON(ret);
2030
Chris Mason85e21ba2008-01-29 15:11:36 -05002031search_again:
2032 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2033 if (ret < 0) {
2034 goto error;
2035 }
2036 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002037 /* there are no items in the tree for us to truncate, we're
2038 * done
2039 */
2040 if (path->slots[0] == 0) {
2041 ret = 0;
2042 goto error;
2043 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002044 path->slots[0]--;
2045 }
2046
Chris Mason39279cc2007-06-12 06:35:45 -04002047 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002048 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002049 leaf = path->nodes[0];
2050 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2051 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002052
Chris Mason5f39d392007-10-15 16:14:19 -04002053 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002054 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002055
Chris Mason85e21ba2008-01-29 15:11:36 -05002056 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002057 break;
2058
Chris Mason5f39d392007-10-15 16:14:19 -04002059 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002060 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002061 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002062 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002063 extent_type = btrfs_file_extent_type(leaf, fi);
2064 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002065 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002066 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002067 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002068 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002069 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002070 }
Yan008630c2007-11-07 13:31:09 -05002071 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002072 }
2073 if (found_type == BTRFS_CSUM_ITEM_KEY) {
2074 ret = btrfs_csum_truncate(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002075 new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002076 BUG_ON(ret);
2077 }
Chris Masone02119d2008-09-05 16:13:11 -04002078 if (item_end < new_size) {
Chris Masonb888db22007-08-27 16:49:44 -04002079 if (found_type == BTRFS_DIR_ITEM_KEY) {
2080 found_type = BTRFS_INODE_ITEM_KEY;
2081 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
2082 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05002083 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
2084 found_type = BTRFS_XATTR_ITEM_KEY;
2085 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
2086 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04002087 } else if (found_type) {
2088 found_type--;
2089 } else {
2090 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002091 }
Yana61721d2007-09-17 11:08:38 -04002092 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002093 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002094 }
Chris Masone02119d2008-09-05 16:13:11 -04002095 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002096 del_item = 1;
2097 else
2098 del_item = 0;
2099 found_extent = 0;
2100
2101 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002102 if (found_type != BTRFS_EXTENT_DATA_KEY)
2103 goto delete;
2104
2105 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002106 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002107 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002108 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04002109 u64 orig_num_bytes =
2110 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002111 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002112 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002113 extent_num_bytes = extent_num_bytes &
2114 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002115 btrfs_set_file_extent_num_bytes(leaf, fi,
2116 extent_num_bytes);
2117 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002118 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002119 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002120 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002121 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002122 } else {
Chris Masondb945352007-10-15 16:15:53 -04002123 extent_num_bytes =
2124 btrfs_file_extent_disk_num_bytes(leaf,
2125 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002126 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002127 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002128 if (extent_start != 0) {
2129 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002130 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002131 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002132 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002133 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002134 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002135 }
Chris Mason90692182008-02-08 13:49:28 -05002136 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002137 /*
2138 * we can't truncate inline items that have had
2139 * special encodings
2140 */
2141 if (!del_item &&
2142 btrfs_file_extent_compression(leaf, fi) == 0 &&
2143 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2144 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002145 u32 size = new_size - found_key.offset;
2146
2147 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002148 inode_sub_bytes(inode, item_end + 1 -
2149 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002150 }
2151 size =
2152 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002153 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002154 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002155 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002156 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002157 inode_sub_bytes(inode, item_end + 1 -
2158 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002159 }
Chris Mason39279cc2007-06-12 06:35:45 -04002160 }
Chris Mason179e29e2007-11-01 11:28:41 -04002161delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002162 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002163 if (!pending_del_nr) {
2164 /* no pending yet, add ourselves */
2165 pending_del_slot = path->slots[0];
2166 pending_del_nr = 1;
2167 } else if (pending_del_nr &&
2168 path->slots[0] + 1 == pending_del_slot) {
2169 /* hop on the pending chunk */
2170 pending_del_nr++;
2171 pending_del_slot = path->slots[0];
2172 } else {
2173 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
2174 }
Chris Mason39279cc2007-06-12 06:35:45 -04002175 } else {
2176 break;
2177 }
Chris Mason39279cc2007-06-12 06:35:45 -04002178 if (found_extent) {
2179 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002180 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002181 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002182 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002183 BUG_ON(ret);
2184 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002185next:
2186 if (path->slots[0] == 0) {
2187 if (pending_del_nr)
2188 goto del_pending;
2189 btrfs_release_path(root, path);
2190 goto search_again;
2191 }
2192
2193 path->slots[0]--;
2194 if (pending_del_nr &&
2195 path->slots[0] + 1 != pending_del_slot) {
2196 struct btrfs_key debug;
2197del_pending:
2198 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2199 pending_del_slot);
2200 ret = btrfs_del_items(trans, root, path,
2201 pending_del_slot,
2202 pending_del_nr);
2203 BUG_ON(ret);
2204 pending_del_nr = 0;
2205 btrfs_release_path(root, path);
2206 goto search_again;
2207 }
Chris Mason39279cc2007-06-12 06:35:45 -04002208 }
2209 ret = 0;
2210error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002211 if (pending_del_nr) {
2212 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2213 pending_del_nr);
2214 }
Chris Mason39279cc2007-06-12 06:35:45 -04002215 btrfs_free_path(path);
2216 inode->i_sb->s_dirt = 1;
2217 return ret;
2218}
2219
Chris Masona52d9a82007-08-27 16:49:44 -04002220/*
2221 * taken from block_truncate_page, but does cow as it zeros out
2222 * any bytes left in the last page in the file.
2223 */
2224static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2225{
2226 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002227 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002228 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2229 struct btrfs_ordered_extent *ordered;
2230 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002231 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002232 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2233 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2234 struct page *page;
2235 int ret = 0;
2236 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002237 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002238
2239 if ((offset & (blocksize - 1)) == 0)
2240 goto out;
2241
2242 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002243again:
Chris Masona52d9a82007-08-27 16:49:44 -04002244 page = grab_cache_page(mapping, index);
2245 if (!page)
2246 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002247
2248 page_start = page_offset(page);
2249 page_end = page_start + PAGE_CACHE_SIZE - 1;
2250
Chris Masona52d9a82007-08-27 16:49:44 -04002251 if (!PageUptodate(page)) {
2252 ret = btrfs_readpage(NULL, page);
2253 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002254 if (page->mapping != mapping) {
2255 unlock_page(page);
2256 page_cache_release(page);
2257 goto again;
2258 }
Chris Masona52d9a82007-08-27 16:49:44 -04002259 if (!PageUptodate(page)) {
2260 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002261 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002262 }
2263 }
Chris Mason211c17f2008-05-15 09:13:45 -04002264 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002265
2266 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2267 set_page_extent_mapped(page);
2268
2269 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2270 if (ordered) {
2271 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2272 unlock_page(page);
2273 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002274 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002275 btrfs_put_ordered_extent(ordered);
2276 goto again;
2277 }
2278
Chris Masonea8c2812008-08-04 23:17:27 -04002279 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002280 ret = 0;
2281 if (offset != PAGE_CACHE_SIZE) {
2282 kaddr = kmap(page);
2283 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2284 flush_dcache_page(page);
2285 kunmap(page);
2286 }
Chris Mason247e7432008-07-17 12:53:51 -04002287 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002288 set_page_dirty(page);
2289 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002290
Chris Mason89642222008-07-24 09:41:53 -04002291out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002292 unlock_page(page);
2293 page_cache_release(page);
2294out:
2295 return ret;
2296}
2297
2298static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2299{
2300 struct inode *inode = dentry->d_inode;
2301 int err;
2302
2303 err = inode_change_ok(inode, attr);
2304 if (err)
2305 return err;
2306
2307 if (S_ISREG(inode->i_mode) &&
2308 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
2309 struct btrfs_trans_handle *trans;
2310 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002311 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04002312
Chris Mason5f39d392007-10-15 16:14:19 -04002313 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05002314 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05002315 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04002316 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04002317 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002318
Chris Mason1b0f7c22008-01-30 14:33:02 -05002319 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04002320 goto out;
2321
Chris Mason1832a6d2007-12-21 16:27:21 -05002322 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002323 if (err)
2324 goto fail;
2325
Chris Mason39279cc2007-06-12 06:35:45 -04002326 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2327
Chris Mason5f564062008-01-22 16:47:59 -05002328 hole_size = block_end - hole_start;
Chris Mason7c2fe322008-08-20 08:51:50 -04002329 while(1) {
2330 struct btrfs_ordered_extent *ordered;
2331 btrfs_wait_ordered_range(inode, hole_start, hole_size);
2332
2333 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2334 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2335 if (ordered) {
2336 unlock_extent(io_tree, hole_start,
2337 block_end - 1, GFP_NOFS);
2338 btrfs_put_ordered_extent(ordered);
2339 } else {
2340 break;
2341 }
2342 }
Chris Mason39279cc2007-06-12 06:35:45 -04002343
Chris Mason39279cc2007-06-12 06:35:45 -04002344 trans = btrfs_start_transaction(root, 1);
2345 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04002346 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04002347 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05002348 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04002349 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04002350
Chris Mason179e29e2007-11-01 11:28:41 -04002351 if (alloc_hint != EXTENT_MAP_INLINE) {
2352 err = btrfs_insert_file_extent(trans, root,
2353 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05002354 hole_start, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04002355 hole_size, 0, hole_size,
2356 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002357 btrfs_drop_extent_cache(inode, hole_start,
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002358 (u64)-1, 0);
Chris Mason5f564062008-01-22 16:47:59 -05002359 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04002360 }
Chris Masonee6e6502008-07-17 12:54:40 -04002361 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002362 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05002363 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04002364 if (err)
2365 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002366 }
2367out:
2368 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002369
2370 if (!err && ((attr->ia_valid & ATTR_MODE)))
2371 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05002372fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002373 return err;
2374}
Chris Mason61295eb2008-01-14 16:24:38 -05002375
Chris Mason39279cc2007-06-12 06:35:45 -04002376void btrfs_delete_inode(struct inode *inode)
2377{
2378 struct btrfs_trans_handle *trans;
2379 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002380 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002381 int ret;
2382
2383 truncate_inode_pages(&inode->i_data, 0);
2384 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002385 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002386 goto no_delete;
2387 }
Chris Mason4a096752008-07-21 10:29:44 -04002388 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002389
Chris Masondbe674a2008-07-17 12:54:05 -04002390 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002391 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002392
Chris Mason39279cc2007-06-12 06:35:45 -04002393 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002394 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002395 if (ret) {
2396 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002397 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002398 }
2399
2400 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002401
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002402 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002403 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002404
Chris Mason39279cc2007-06-12 06:35:45 -04002405 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002406 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002407 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002408
2409no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002410 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002411 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002412 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002413no_delete:
2414 clear_inode(inode);
2415}
2416
2417/*
2418 * this returns the key found in the dir entry in the location pointer.
2419 * If no dir entries were found, location->objectid is 0.
2420 */
2421static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2422 struct btrfs_key *location)
2423{
2424 const char *name = dentry->d_name.name;
2425 int namelen = dentry->d_name.len;
2426 struct btrfs_dir_item *di;
2427 struct btrfs_path *path;
2428 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002429 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002430
2431 path = btrfs_alloc_path();
2432 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002433
Chris Mason39279cc2007-06-12 06:35:45 -04002434 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2435 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002436 if (IS_ERR(di))
2437 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04002438 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05002439 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04002440 }
Chris Mason5f39d392007-10-15 16:14:19 -04002441 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002442out:
Chris Mason39279cc2007-06-12 06:35:45 -04002443 btrfs_free_path(path);
2444 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002445out_err:
2446 location->objectid = 0;
2447 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002448}
2449
2450/*
2451 * when we hit a tree root in a directory, the btrfs part of the inode
2452 * needs to be changed to reflect the root directory of the tree root. This
2453 * is kind of like crossing a mount point.
2454 */
2455static int fixup_tree_root_location(struct btrfs_root *root,
2456 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002457 struct btrfs_root **sub_root,
2458 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002459{
Chris Mason39279cc2007-06-12 06:35:45 -04002460 struct btrfs_root_item *ri;
2461
2462 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2463 return 0;
2464 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2465 return 0;
2466
Josef Bacik58176a92007-08-29 15:47:34 -04002467 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2468 dentry->d_name.name,
2469 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002470 if (IS_ERR(*sub_root))
2471 return PTR_ERR(*sub_root);
2472
2473 ri = &(*sub_root)->root_item;
2474 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002475 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2476 location->offset = 0;
2477
Chris Mason39279cc2007-06-12 06:35:45 -04002478 return 0;
2479}
2480
Chris Masone02119d2008-09-05 16:13:11 -04002481static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002482{
Chris Masone02119d2008-09-05 16:13:11 -04002483 struct btrfs_inode *bi = BTRFS_I(inode);
2484
2485 bi->i_acl = NULL;
2486 bi->i_default_acl = NULL;
2487
2488 bi->generation = 0;
2489 bi->last_trans = 0;
2490 bi->logged_trans = 0;
2491 bi->delalloc_bytes = 0;
2492 bi->disk_i_size = 0;
2493 bi->flags = 0;
2494 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002495 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002496 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2497 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002498 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002499 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2500 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002501 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04002502 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002503 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002504 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002505 mutex_init(&BTRFS_I(inode)->log_mutex);
2506}
2507
2508static int btrfs_init_locked_inode(struct inode *inode, void *p)
2509{
2510 struct btrfs_iget_args *args = p;
2511 inode->i_ino = args->ino;
2512 init_btrfs_i(inode);
2513 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002514 return 0;
2515}
2516
2517static int btrfs_find_actor(struct inode *inode, void *opaque)
2518{
2519 struct btrfs_iget_args *args = opaque;
2520 return (args->ino == inode->i_ino &&
2521 args->root == BTRFS_I(inode)->root);
2522}
2523
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002524struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2525 struct btrfs_root *root, int wait)
2526{
2527 struct inode *inode;
2528 struct btrfs_iget_args args;
2529 args.ino = objectid;
2530 args.root = root;
2531
2532 if (wait) {
2533 inode = ilookup5(s, objectid, btrfs_find_actor,
2534 (void *)&args);
2535 } else {
2536 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2537 (void *)&args);
2538 }
2539 return inode;
2540}
2541
Chris Mason39279cc2007-06-12 06:35:45 -04002542struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2543 struct btrfs_root *root)
2544{
2545 struct inode *inode;
2546 struct btrfs_iget_args args;
2547 args.ino = objectid;
2548 args.root = root;
2549
2550 inode = iget5_locked(s, objectid, btrfs_find_actor,
2551 btrfs_init_locked_inode,
2552 (void *)&args);
2553 return inode;
2554}
2555
Balaji Rao1a54ef82008-07-21 02:01:04 +05302556/* Get an inode object given its location and corresponding root.
2557 * Returns in *is_new if the inode was read from disk
2558 */
2559struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2560 struct btrfs_root *root, int *is_new)
2561{
2562 struct inode *inode;
2563
2564 inode = btrfs_iget_locked(s, location->objectid, root);
2565 if (!inode)
2566 return ERR_PTR(-EACCES);
2567
2568 if (inode->i_state & I_NEW) {
2569 BTRFS_I(inode)->root = root;
2570 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2571 btrfs_read_locked_inode(inode);
2572 unlock_new_inode(inode);
2573 if (is_new)
2574 *is_new = 1;
2575 } else {
2576 if (is_new)
2577 *is_new = 0;
2578 }
2579
2580 return inode;
2581}
2582
Chris Mason39279cc2007-06-12 06:35:45 -04002583static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2584 struct nameidata *nd)
2585{
2586 struct inode * inode;
2587 struct btrfs_inode *bi = BTRFS_I(dir);
2588 struct btrfs_root *root = bi->root;
2589 struct btrfs_root *sub_root = root;
2590 struct btrfs_key location;
Balaji Rao1a54ef82008-07-21 02:01:04 +05302591 int ret, new, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002592
2593 if (dentry->d_name.len > BTRFS_NAME_LEN)
2594 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04002595
Chris Mason39279cc2007-06-12 06:35:45 -04002596 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002597
Chris Mason39279cc2007-06-12 06:35:45 -04002598 if (ret < 0)
2599 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002600
Chris Mason39279cc2007-06-12 06:35:45 -04002601 inode = NULL;
2602 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04002603 ret = fixup_tree_root_location(root, &location, &sub_root,
2604 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04002605 if (ret < 0)
2606 return ERR_PTR(ret);
2607 if (ret > 0)
2608 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05302609 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2610 if (IS_ERR(inode))
2611 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002612
Balaji Rao1a54ef82008-07-21 02:01:04 +05302613 /* the inode and parent dir are two different roots */
2614 if (new && root != sub_root) {
2615 igrab(inode);
2616 sub_root->inode = inode;
2617 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002618 }
2619 }
Josef Bacik7b128762008-07-24 12:17:14 -04002620
2621 if (unlikely(do_orphan))
2622 btrfs_orphan_cleanup(sub_root);
2623
Chris Mason39279cc2007-06-12 06:35:45 -04002624 return d_splice_alias(inode, dentry);
2625}
2626
Chris Mason39279cc2007-06-12 06:35:45 -04002627static unsigned char btrfs_filetype_table[] = {
2628 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2629};
2630
David Woodhousecbdf5a22008-08-06 19:42:33 +01002631static int btrfs_real_readdir(struct file *filp, void *dirent,
2632 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04002633{
Chris Mason6da6aba2007-12-18 16:15:09 -05002634 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002635 struct btrfs_root *root = BTRFS_I(inode)->root;
2636 struct btrfs_item *item;
2637 struct btrfs_dir_item *di;
2638 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002639 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002640 struct btrfs_path *path;
2641 int ret;
2642 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002643 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002644 int slot;
2645 int advance;
2646 unsigned char d_type;
2647 int over = 0;
2648 u32 di_cur;
2649 u32 di_total;
2650 u32 di_len;
2651 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002652 char tmp_name[32];
2653 char *name_ptr;
2654 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04002655
2656 /* FIXME, use a real flag for deciding about the key type */
2657 if (root->fs_info->tree_root == root)
2658 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002659
Chris Mason39544012007-12-12 14:38:19 -05002660 /* special case for "." */
2661 if (filp->f_pos == 0) {
2662 over = filldir(dirent, ".", 1,
2663 1, inode->i_ino,
2664 DT_DIR);
2665 if (over)
2666 return 0;
2667 filp->f_pos = 1;
2668 }
Chris Mason39544012007-12-12 14:38:19 -05002669 /* special case for .., just use the back ref */
2670 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002671 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05002672 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002673 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05002674 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01002675 return 0;
Chris Mason39544012007-12-12 14:38:19 -05002676 filp->f_pos = 2;
2677 }
2678
David Woodhouse49593bf2008-08-17 17:08:36 +01002679 path = btrfs_alloc_path();
2680 path->reada = 2;
2681
Chris Mason39279cc2007-06-12 06:35:45 -04002682 btrfs_set_key_type(&key, key_type);
2683 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002684 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04002685
Chris Mason39279cc2007-06-12 06:35:45 -04002686 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2687 if (ret < 0)
2688 goto err;
2689 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01002690
2691 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002692 leaf = path->nodes[0];
2693 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002694 slot = path->slots[0];
2695 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01002696 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002697 ret = btrfs_next_leaf(root, path);
2698 if (ret)
2699 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002700 leaf = path->nodes[0];
2701 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002702 slot = path->slots[0];
2703 } else {
2704 slot++;
2705 path->slots[0]++;
2706 }
2707 }
2708 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002709 item = btrfs_item_nr(leaf, slot);
2710 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2711
2712 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002713 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002714 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002715 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002716 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002717 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002718
2719 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01002720
Chris Mason39279cc2007-06-12 06:35:45 -04002721 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2722 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002723 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01002724
2725 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002726 struct btrfs_key location;
2727
2728 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01002729 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002730 name_ptr = tmp_name;
2731 } else {
2732 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01002733 if (!name_ptr) {
2734 ret = -ENOMEM;
2735 goto err;
2736 }
Chris Mason5f39d392007-10-15 16:14:19 -04002737 }
2738 read_extent_buffer(leaf, name_ptr,
2739 (unsigned long)(di + 1), name_len);
2740
2741 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2742 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002743 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01002744 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002745 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002746
2747 if (name_ptr != tmp_name)
2748 kfree(name_ptr);
2749
Chris Mason39279cc2007-06-12 06:35:45 -04002750 if (over)
2751 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002752
Josef Bacik5103e942007-11-16 11:45:54 -05002753 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01002754 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002755 di_cur += di_len;
2756 di = (struct btrfs_dir_item *)((char *)di + di_len);
2757 }
2758 }
David Woodhouse49593bf2008-08-17 17:08:36 +01002759
2760 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05002761 if (key_type == BTRFS_DIR_INDEX_KEY)
2762 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2763 else
2764 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002765nopos:
2766 ret = 0;
2767err:
Chris Mason39279cc2007-06-12 06:35:45 -04002768 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002769 return ret;
2770}
2771
2772int btrfs_write_inode(struct inode *inode, int wait)
2773{
2774 struct btrfs_root *root = BTRFS_I(inode)->root;
2775 struct btrfs_trans_handle *trans;
2776 int ret = 0;
2777
Chris Mason4ca8b412008-08-05 13:30:48 -04002778 if (root->fs_info->closing > 1)
2779 return 0;
2780
Chris Mason39279cc2007-06-12 06:35:45 -04002781 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002782 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002783 btrfs_set_trans_block_group(trans, inode);
2784 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002785 }
2786 return ret;
2787}
2788
2789/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002790 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002791 * inode changes. But, it is most likely to find the inode in cache.
2792 * FIXME, needs more benchmarking...there are no reasons other than performance
2793 * to keep or drop this code.
2794 */
2795void btrfs_dirty_inode(struct inode *inode)
2796{
2797 struct btrfs_root *root = BTRFS_I(inode)->root;
2798 struct btrfs_trans_handle *trans;
2799
Chris Masonf9295742008-07-17 12:54:14 -04002800 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002801 btrfs_set_trans_block_group(trans, inode);
2802 btrfs_update_inode(trans, root, inode);
2803 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002804}
2805
Chris Masond352ac62008-09-29 15:18:18 -04002806/*
2807 * find the highest existing sequence number in a directory
2808 * and then set the in-memory index_cnt variable to reflect
2809 * free sequence numbers
2810 */
Josef Bacikaec74772008-07-24 12:12:38 -04002811static int btrfs_set_inode_index_count(struct inode *inode)
2812{
2813 struct btrfs_root *root = BTRFS_I(inode)->root;
2814 struct btrfs_key key, found_key;
2815 struct btrfs_path *path;
2816 struct extent_buffer *leaf;
2817 int ret;
2818
2819 key.objectid = inode->i_ino;
2820 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2821 key.offset = (u64)-1;
2822
2823 path = btrfs_alloc_path();
2824 if (!path)
2825 return -ENOMEM;
2826
2827 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2828 if (ret < 0)
2829 goto out;
2830 /* FIXME: we should be able to handle this */
2831 if (ret == 0)
2832 goto out;
2833 ret = 0;
2834
2835 /*
2836 * MAGIC NUMBER EXPLANATION:
2837 * since we search a directory based on f_pos we have to start at 2
2838 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2839 * else has to start at 2
2840 */
2841 if (path->slots[0] == 0) {
2842 BTRFS_I(inode)->index_cnt = 2;
2843 goto out;
2844 }
2845
2846 path->slots[0]--;
2847
2848 leaf = path->nodes[0];
2849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2850
2851 if (found_key.objectid != inode->i_ino ||
2852 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2853 BTRFS_I(inode)->index_cnt = 2;
2854 goto out;
2855 }
2856
2857 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2858out:
2859 btrfs_free_path(path);
2860 return ret;
2861}
2862
Chris Masond352ac62008-09-29 15:18:18 -04002863/*
2864 * helper to find a free sequence number in a given directory. This current
2865 * code is very simple, later versions will do smarter things in the btree
2866 */
Chris Mason00e4e6b2008-08-05 11:18:09 -04002867static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2868 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002869{
2870 int ret = 0;
2871
2872 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2873 ret = btrfs_set_inode_index_count(dir);
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002874 if (ret) {
Josef Bacikaec74772008-07-24 12:12:38 -04002875 return ret;
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002876 }
Josef Bacikaec74772008-07-24 12:12:38 -04002877 }
2878
Chris Mason00e4e6b2008-08-05 11:18:09 -04002879 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002880 BTRFS_I(dir)->index_cnt++;
2881
2882 return ret;
2883}
2884
Chris Mason39279cc2007-06-12 06:35:45 -04002885static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2886 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002887 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002888 const char *name, int name_len,
2889 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002890 u64 objectid,
2891 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002892 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002893{
2894 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002895 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002896 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002897 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002898 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002899 struct btrfs_inode_ref *ref;
2900 struct btrfs_key key[2];
2901 u32 sizes[2];
2902 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002903 int ret;
2904 int owner;
2905
Chris Mason5f39d392007-10-15 16:14:19 -04002906 path = btrfs_alloc_path();
2907 BUG_ON(!path);
2908
Chris Mason39279cc2007-06-12 06:35:45 -04002909 inode = new_inode(root->fs_info->sb);
2910 if (!inode)
2911 return ERR_PTR(-ENOMEM);
2912
Josef Bacikaec74772008-07-24 12:12:38 -04002913 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002914 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002915 if (ret)
2916 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002917 }
2918 /*
2919 * index_cnt is ignored for everything but a dir,
2920 * btrfs_get_inode_index_count has an explanation for the magic
2921 * number
2922 */
Chris Masone02119d2008-09-05 16:13:11 -04002923 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04002924 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04002925 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04002926 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04002927
Chris Mason39279cc2007-06-12 06:35:45 -04002928 if (mode & S_IFDIR)
2929 owner = 0;
2930 else
2931 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002932 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002933 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002934 if (!new_inode_group) {
2935 printk("find_block group failed\n");
2936 new_inode_group = group;
2937 }
2938 BTRFS_I(inode)->block_group = new_inode_group;
Chris Mason9c583092008-01-29 15:15:18 -05002939
2940 key[0].objectid = objectid;
2941 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2942 key[0].offset = 0;
2943
2944 key[1].objectid = objectid;
2945 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2946 key[1].offset = ref_objectid;
2947
2948 sizes[0] = sizeof(struct btrfs_inode_item);
2949 sizes[1] = name_len + sizeof(*ref);
2950
2951 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2952 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002953 goto fail;
2954
Chris Mason9c583092008-01-29 15:15:18 -05002955 if (objectid > root->highest_inode)
2956 root->highest_inode = objectid;
2957
Chris Mason39279cc2007-06-12 06:35:45 -04002958 inode->i_uid = current->fsuid;
2959 inode->i_gid = current->fsgid;
2960 inode->i_mode = mode;
2961 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002962 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002963 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002964 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2965 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002966 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002967
2968 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2969 struct btrfs_inode_ref);
2970 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002971 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002972 ptr = (unsigned long)(ref + 1);
2973 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2974
Chris Mason5f39d392007-10-15 16:14:19 -04002975 btrfs_mark_buffer_dirty(path->nodes[0]);
2976 btrfs_free_path(path);
2977
Chris Mason39279cc2007-06-12 06:35:45 -04002978 location = &BTRFS_I(inode)->location;
2979 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002980 location->offset = 0;
2981 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2982
Chris Mason39279cc2007-06-12 06:35:45 -04002983 insert_inode_hash(inode);
2984 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002985fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002986 if (dir)
2987 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002988 btrfs_free_path(path);
2989 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002990}
2991
2992static inline u8 btrfs_inode_type(struct inode *inode)
2993{
2994 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2995}
2996
Chris Masond352ac62008-09-29 15:18:18 -04002997/*
2998 * utility function to add 'inode' into 'parent_inode' with
2999 * a give name and a given sequence number.
3000 * if 'add_backref' is true, also insert a backref from the
3001 * inode to the parent directory.
3002 */
Chris Masone02119d2008-09-05 16:13:11 -04003003int btrfs_add_link(struct btrfs_trans_handle *trans,
3004 struct inode *parent_inode, struct inode *inode,
3005 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003006{
3007 int ret;
3008 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003009 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003010
Chris Mason39279cc2007-06-12 06:35:45 -04003011 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003012 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3013 key.offset = 0;
3014
Chris Masone02119d2008-09-05 16:13:11 -04003015 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3016 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003017 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003018 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003019 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003020 if (add_backref) {
3021 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003022 name, name_len,
3023 inode->i_ino,
3024 parent_inode->i_ino,
3025 index);
Chris Mason9c583092008-01-29 15:15:18 -05003026 }
Chris Masondbe674a2008-07-17 12:54:05 -04003027 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003028 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003029 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003030 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003031 }
3032 return ret;
3033}
3034
3035static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003036 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003037 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003038{
Chris Masone02119d2008-09-05 16:13:11 -04003039 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3040 inode, dentry->d_name.name,
3041 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003042 if (!err) {
3043 d_instantiate(dentry, inode);
3044 return 0;
3045 }
3046 if (err > 0)
3047 err = -EEXIST;
3048 return err;
3049}
3050
Josef Bacik618e21d2007-07-11 10:18:17 -04003051static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3052 int mode, dev_t rdev)
3053{
3054 struct btrfs_trans_handle *trans;
3055 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003056 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003057 int err;
3058 int drop_inode = 0;
3059 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003060 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003061 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003062
3063 if (!new_valid_dev(rdev))
3064 return -EINVAL;
3065
Chris Mason1832a6d2007-12-21 16:27:21 -05003066 err = btrfs_check_free_space(root, 1, 0);
3067 if (err)
3068 goto fail;
3069
Josef Bacik618e21d2007-07-11 10:18:17 -04003070 trans = btrfs_start_transaction(root, 1);
3071 btrfs_set_trans_block_group(trans, dir);
3072
3073 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3074 if (err) {
3075 err = -ENOSPC;
3076 goto out_unlock;
3077 }
3078
Josef Bacikaec74772008-07-24 12:12:38 -04003079 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003080 dentry->d_name.len,
3081 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003082 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003083 err = PTR_ERR(inode);
3084 if (IS_ERR(inode))
3085 goto out_unlock;
3086
Josef Bacik33268ea2008-07-24 12:16:36 -04003087 err = btrfs_init_acl(inode, dir);
3088 if (err) {
3089 drop_inode = 1;
3090 goto out_unlock;
3091 }
3092
Josef Bacik618e21d2007-07-11 10:18:17 -04003093 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003094 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003095 if (err)
3096 drop_inode = 1;
3097 else {
3098 inode->i_op = &btrfs_special_inode_operations;
3099 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003100 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003101 }
3102 dir->i_sb->s_dirt = 1;
3103 btrfs_update_inode_block_group(trans, inode);
3104 btrfs_update_inode_block_group(trans, dir);
3105out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003106 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003107 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003108fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003109 if (drop_inode) {
3110 inode_dec_link_count(inode);
3111 iput(inode);
3112 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003113 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003114 return err;
3115}
3116
Chris Mason39279cc2007-06-12 06:35:45 -04003117static int btrfs_create(struct inode *dir, struct dentry *dentry,
3118 int mode, struct nameidata *nd)
3119{
3120 struct btrfs_trans_handle *trans;
3121 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003122 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003123 int err;
3124 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003125 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003126 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003127 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003128
Chris Mason1832a6d2007-12-21 16:27:21 -05003129 err = btrfs_check_free_space(root, 1, 0);
3130 if (err)
3131 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003132 trans = btrfs_start_transaction(root, 1);
3133 btrfs_set_trans_block_group(trans, dir);
3134
3135 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3136 if (err) {
3137 err = -ENOSPC;
3138 goto out_unlock;
3139 }
3140
Josef Bacikaec74772008-07-24 12:12:38 -04003141 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003142 dentry->d_name.len,
3143 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003144 objectid, BTRFS_I(dir)->block_group, mode,
3145 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003146 err = PTR_ERR(inode);
3147 if (IS_ERR(inode))
3148 goto out_unlock;
3149
Josef Bacik33268ea2008-07-24 12:16:36 -04003150 err = btrfs_init_acl(inode, dir);
3151 if (err) {
3152 drop_inode = 1;
3153 goto out_unlock;
3154 }
3155
Chris Mason39279cc2007-06-12 06:35:45 -04003156 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003157 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003158 if (err)
3159 drop_inode = 1;
3160 else {
3161 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003162 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003163 inode->i_fop = &btrfs_file_operations;
3164 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003165 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003166 }
3167 dir->i_sb->s_dirt = 1;
3168 btrfs_update_inode_block_group(trans, inode);
3169 btrfs_update_inode_block_group(trans, dir);
3170out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003171 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003172 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003173fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003174 if (drop_inode) {
3175 inode_dec_link_count(inode);
3176 iput(inode);
3177 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003178 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003179 return err;
3180}
3181
3182static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3183 struct dentry *dentry)
3184{
3185 struct btrfs_trans_handle *trans;
3186 struct btrfs_root *root = BTRFS_I(dir)->root;
3187 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003188 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003189 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003190 int err;
3191 int drop_inode = 0;
3192
3193 if (inode->i_nlink == 0)
3194 return -ENOENT;
3195
Chris Masone02119d2008-09-05 16:13:11 -04003196 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003197 err = btrfs_check_free_space(root, 1, 0);
3198 if (err)
3199 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003200 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003201 if (err)
3202 goto fail;
3203
Chris Mason39279cc2007-06-12 06:35:45 -04003204 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003205
Chris Mason39279cc2007-06-12 06:35:45 -04003206 btrfs_set_trans_block_group(trans, dir);
3207 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003208
Chris Mason00e4e6b2008-08-05 11:18:09 -04003209 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003210
Chris Mason39279cc2007-06-12 06:35:45 -04003211 if (err)
3212 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003213
Chris Mason39279cc2007-06-12 06:35:45 -04003214 dir->i_sb->s_dirt = 1;
3215 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003216 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003217
Chris Mason54aa1f42007-06-22 14:16:25 -04003218 if (err)
3219 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003220
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003221 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003222 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003223fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003224 if (drop_inode) {
3225 inode_dec_link_count(inode);
3226 iput(inode);
3227 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003228 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003229 return err;
3230}
3231
Chris Mason39279cc2007-06-12 06:35:45 -04003232static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3233{
Chris Masonb9d86662008-05-02 16:13:49 -04003234 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003235 struct btrfs_trans_handle *trans;
3236 struct btrfs_root *root = BTRFS_I(dir)->root;
3237 int err = 0;
3238 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003239 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003240 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003241 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003242
Chris Mason1832a6d2007-12-21 16:27:21 -05003243 err = btrfs_check_free_space(root, 1, 0);
3244 if (err)
3245 goto out_unlock;
3246
Chris Mason39279cc2007-06-12 06:35:45 -04003247 trans = btrfs_start_transaction(root, 1);
3248 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003249
Chris Mason39279cc2007-06-12 06:35:45 -04003250 if (IS_ERR(trans)) {
3251 err = PTR_ERR(trans);
3252 goto out_unlock;
3253 }
3254
3255 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3256 if (err) {
3257 err = -ENOSPC;
3258 goto out_unlock;
3259 }
3260
Josef Bacikaec74772008-07-24 12:12:38 -04003261 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003262 dentry->d_name.len,
3263 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003264 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3265 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003266 if (IS_ERR(inode)) {
3267 err = PTR_ERR(inode);
3268 goto out_fail;
3269 }
Chris Mason5f39d392007-10-15 16:14:19 -04003270
Chris Mason39279cc2007-06-12 06:35:45 -04003271 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003272
3273 err = btrfs_init_acl(inode, dir);
3274 if (err)
3275 goto out_fail;
3276
Chris Mason39279cc2007-06-12 06:35:45 -04003277 inode->i_op = &btrfs_dir_inode_operations;
3278 inode->i_fop = &btrfs_dir_file_operations;
3279 btrfs_set_trans_block_group(trans, inode);
3280
Chris Masondbe674a2008-07-17 12:54:05 -04003281 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003282 err = btrfs_update_inode(trans, root, inode);
3283 if (err)
3284 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003285
Chris Masone02119d2008-09-05 16:13:11 -04003286 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3287 inode, dentry->d_name.name,
3288 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003289 if (err)
3290 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003291
Chris Mason39279cc2007-06-12 06:35:45 -04003292 d_instantiate(dentry, inode);
3293 drop_on_err = 0;
3294 dir->i_sb->s_dirt = 1;
3295 btrfs_update_inode_block_group(trans, inode);
3296 btrfs_update_inode_block_group(trans, dir);
3297
3298out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003299 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003300 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003301
Chris Mason39279cc2007-06-12 06:35:45 -04003302out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003303 if (drop_on_err)
3304 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003305 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003306 return err;
3307}
3308
Chris Masond352ac62008-09-29 15:18:18 -04003309/* helper for btfs_get_extent. Given an existing extent in the tree,
3310 * and an extent that you want to insert, deal with overlap and insert
3311 * the new extent into the tree.
3312 */
Chris Mason3b951512008-04-17 11:29:12 -04003313static int merge_extent_mapping(struct extent_map_tree *em_tree,
3314 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003315 struct extent_map *em,
3316 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003317{
3318 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003319
Chris Masone6dcd2d2008-07-17 12:53:50 -04003320 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3321 start_diff = map_start - em->start;
3322 em->start = map_start;
3323 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003324 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3325 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003326 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003327 em->block_len -= start_diff;
3328 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003329 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003330}
3331
Chris Masonc8b97812008-10-29 14:49:59 -04003332static noinline int uncompress_inline(struct btrfs_path *path,
3333 struct inode *inode, struct page *page,
3334 size_t pg_offset, u64 extent_offset,
3335 struct btrfs_file_extent_item *item)
3336{
3337 int ret;
3338 struct extent_buffer *leaf = path->nodes[0];
3339 char *tmp;
3340 size_t max_size;
3341 unsigned long inline_size;
3342 unsigned long ptr;
3343
3344 WARN_ON(pg_offset != 0);
3345 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3346 inline_size = btrfs_file_extent_inline_item_len(leaf,
3347 btrfs_item_nr(leaf, path->slots[0]));
3348 tmp = kmalloc(inline_size, GFP_NOFS);
3349 ptr = btrfs_file_extent_inline_start(item);
3350
3351 read_extent_buffer(leaf, tmp, ptr, inline_size);
3352
3353 max_size = min(PAGE_CACHE_SIZE, max_size);
3354 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3355 inline_size, max_size);
3356 if (ret) {
3357 char *kaddr = kmap_atomic(page, KM_USER0);
3358 unsigned long copy_size = min_t(u64,
3359 PAGE_CACHE_SIZE - pg_offset,
3360 max_size - extent_offset);
3361 memset(kaddr + pg_offset, 0, copy_size);
3362 kunmap_atomic(kaddr, KM_USER0);
3363 }
3364 kfree(tmp);
3365 return 0;
3366}
3367
Chris Masond352ac62008-09-29 15:18:18 -04003368/*
3369 * a bit scary, this does extent mapping from logical file offset to the disk.
3370 * the ugly parts come from merging extents from the disk with the
3371 * in-ram representation. This gets more complex because of the data=ordered code,
3372 * where the in-ram extents might be locked pending data=ordered completion.
3373 *
3374 * This also copies inline extents directly into the page.
3375 */
Chris Masona52d9a82007-08-27 16:49:44 -04003376struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003377 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003378 int create)
3379{
3380 int ret;
3381 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003382 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003383 u64 extent_start = 0;
3384 u64 extent_end = 0;
3385 u64 objectid = inode->i_ino;
3386 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003387 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003388 struct btrfs_root *root = BTRFS_I(inode)->root;
3389 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003390 struct extent_buffer *leaf;
3391 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003392 struct extent_map *em = NULL;
3393 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003394 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003395 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003396 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003397
Chris Masona52d9a82007-08-27 16:49:44 -04003398again:
Chris Masond1310b22008-01-24 16:13:08 -05003399 spin_lock(&em_tree->lock);
3400 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003401 if (em)
3402 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003403 spin_unlock(&em_tree->lock);
3404
Chris Masona52d9a82007-08-27 16:49:44 -04003405 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003406 if (em->start > start || em->start + em->len <= start)
3407 free_extent_map(em);
3408 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003409 free_extent_map(em);
3410 else
3411 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003412 }
Chris Masond1310b22008-01-24 16:13:08 -05003413 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003414 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003415 err = -ENOMEM;
3416 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003417 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003418 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003419 em->start = EXTENT_MAP_HOLE;
3420 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003421 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003422
3423 if (!path) {
3424 path = btrfs_alloc_path();
3425 BUG_ON(!path);
3426 }
3427
Chris Mason179e29e2007-11-01 11:28:41 -04003428 ret = btrfs_lookup_file_extent(trans, root, path,
3429 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003430 if (ret < 0) {
3431 err = ret;
3432 goto out;
3433 }
3434
3435 if (ret != 0) {
3436 if (path->slots[0] == 0)
3437 goto not_found;
3438 path->slots[0]--;
3439 }
3440
Chris Mason5f39d392007-10-15 16:14:19 -04003441 leaf = path->nodes[0];
3442 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003443 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003444 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003445 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3446 found_type = btrfs_key_type(&found_key);
3447 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003448 found_type != BTRFS_EXTENT_DATA_KEY) {
3449 goto not_found;
3450 }
3451
Chris Mason5f39d392007-10-15 16:14:19 -04003452 found_type = btrfs_file_extent_type(leaf, item);
3453 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003454 compressed = btrfs_file_extent_compression(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04003455 if (found_type == BTRFS_FILE_EXTENT_REG) {
3456 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04003457 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04003458 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04003459 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04003460 em->start = start;
3461 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05003462 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04003463 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05003464 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04003465 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003466 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003467 }
3468 goto not_found_em;
3469 }
Chris Masondb945352007-10-15 16:15:53 -04003470 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3471 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04003472 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05003473 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04003474 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04003475 goto insert;
3476 }
Chris Masona52d9a82007-08-27 16:49:44 -04003477 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05003478 em->len = extent_end - extent_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003479 if (compressed) {
3480 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3481 em->block_start = bytenr;
3482 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
3483 item);
3484 } else {
3485 bytenr += btrfs_file_extent_offset(leaf, item);
3486 em->block_start = bytenr;
3487 em->block_len = em->len;
3488 }
Chris Masona52d9a82007-08-27 16:49:44 -04003489 goto insert;
3490 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05003491 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04003492 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04003493 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04003494 size_t size;
3495 size_t extent_offset;
3496 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04003497
Chris Masonc8b97812008-10-29 14:49:59 -04003498 size = btrfs_file_extent_inline_len(leaf, item);
Chris Masond1310b22008-01-24 16:13:08 -05003499 extent_end = (extent_start + size + root->sectorsize - 1) &
3500 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04003501 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04003502 em->start = start;
3503 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05003504 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04003505 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05003506 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04003507 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003508 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003509 }
3510 goto not_found_em;
3511 }
3512 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04003513
Chris Masonc8b97812008-10-29 14:49:59 -04003514 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04003515 em->start = extent_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003516 em->len = (size + root->sectorsize - 1) &
3517 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04003518 goto out;
3519 }
3520
Chris Mason70dec802008-01-29 09:59:12 -05003521 page_start = page_offset(page) + pg_offset;
3522 extent_offset = page_start - extent_start;
3523 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04003524 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04003525 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05003526 em->len = (copy_size + root->sectorsize - 1) &
3527 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003528 if (compressed)
3529 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04003530 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04003531 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04003532 if (btrfs_file_extent_compression(leaf, item) ==
3533 BTRFS_COMPRESS_ZLIB) {
3534 ret = uncompress_inline(path, inode, page,
3535 pg_offset,
3536 extent_offset, item);
3537 BUG_ON(ret);
3538 } else {
3539 map = kmap(page);
3540 read_extent_buffer(leaf, map + pg_offset, ptr,
3541 copy_size);
3542 kunmap(page);
3543 }
Chris Mason179e29e2007-11-01 11:28:41 -04003544 flush_dcache_page(page);
3545 } else if (create && PageUptodate(page)) {
3546 if (!trans) {
3547 kunmap(page);
3548 free_extent_map(em);
3549 em = NULL;
3550 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04003551 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04003552 goto again;
3553 }
Chris Masonc8b97812008-10-29 14:49:59 -04003554 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05003555 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04003556 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003557 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04003558 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04003559 }
Chris Masond1310b22008-01-24 16:13:08 -05003560 set_extent_uptodate(io_tree, em->start,
3561 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003562 goto insert;
3563 } else {
3564 printk("unkknown found_type %d\n", found_type);
3565 WARN_ON(1);
3566 }
3567not_found:
3568 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05003569 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003570not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04003571 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04003572insert:
3573 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05003574 if (em->start > start || extent_map_end(em) <= start) {
3575 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04003576 err = -EIO;
3577 goto out;
3578 }
Chris Masond1310b22008-01-24 16:13:08 -05003579
3580 err = 0;
3581 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003582 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003583 /* it is possible that someone inserted the extent into the tree
3584 * while we had the lock dropped. It is also possible that
3585 * an overlapping map exists in the tree
3586 */
Chris Masona52d9a82007-08-27 16:49:44 -04003587 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04003588 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003589
3590 ret = 0;
3591
Chris Mason3b951512008-04-17 11:29:12 -04003592 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04003593 if (existing && (existing->start > start ||
3594 existing->start + existing->len <= start)) {
3595 free_extent_map(existing);
3596 existing = NULL;
3597 }
Chris Mason3b951512008-04-17 11:29:12 -04003598 if (!existing) {
3599 existing = lookup_extent_mapping(em_tree, em->start,
3600 em->len);
3601 if (existing) {
3602 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003603 em, start,
3604 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04003605 free_extent_map(existing);
3606 if (err) {
3607 free_extent_map(em);
3608 em = NULL;
3609 }
3610 } else {
3611 err = -EIO;
3612 printk("failing to insert %Lu %Lu\n",
3613 start, len);
3614 free_extent_map(em);
3615 em = NULL;
3616 }
3617 } else {
3618 free_extent_map(em);
3619 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003620 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04003621 }
Chris Masona52d9a82007-08-27 16:49:44 -04003622 }
Chris Masond1310b22008-01-24 16:13:08 -05003623 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003624out:
Chris Masonf4219502008-07-22 11:18:09 -04003625 if (path)
3626 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04003627 if (trans) {
3628 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003629 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04003630 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003631 }
Chris Masona52d9a82007-08-27 16:49:44 -04003632 }
Chris Masona52d9a82007-08-27 16:49:44 -04003633 if (err) {
3634 free_extent_map(em);
3635 WARN_ON(1);
3636 return ERR_PTR(err);
3637 }
3638 return em;
3639}
3640
Chris Mason16432982008-04-10 10:23:21 -04003641static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3642 const struct iovec *iov, loff_t offset,
3643 unsigned long nr_segs)
3644{
Chris Masone1c4b742008-04-22 13:26:46 -04003645 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04003646}
3647
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003648static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04003649{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003650 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003651}
3652
Chris Mason9ebefb182007-06-15 13:50:00 -04003653int btrfs_readpage(struct file *file, struct page *page)
3654{
Chris Masond1310b22008-01-24 16:13:08 -05003655 struct extent_io_tree *tree;
3656 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003657 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003658}
Chris Mason1832a6d2007-12-21 16:27:21 -05003659
Chris Mason39279cc2007-06-12 06:35:45 -04003660static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3661{
Chris Masond1310b22008-01-24 16:13:08 -05003662 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04003663
3664
3665 if (current->flags & PF_MEMALLOC) {
3666 redirty_page_for_writepage(wbc, page);
3667 unlock_page(page);
3668 return 0;
3669 }
Chris Masond1310b22008-01-24 16:13:08 -05003670 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003671 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3672}
Chris Mason39279cc2007-06-12 06:35:45 -04003673
Chris Masonf4219502008-07-22 11:18:09 -04003674int btrfs_writepages(struct address_space *mapping,
3675 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04003676{
Chris Masond1310b22008-01-24 16:13:08 -05003677 struct extent_io_tree *tree;
3678 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003679 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3680}
3681
Chris Mason3ab2fb52007-11-08 10:59:22 -05003682static int
3683btrfs_readpages(struct file *file, struct address_space *mapping,
3684 struct list_head *pages, unsigned nr_pages)
3685{
Chris Masond1310b22008-01-24 16:13:08 -05003686 struct extent_io_tree *tree;
3687 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003688 return extent_readpages(tree, mapping, pages, nr_pages,
3689 btrfs_get_extent);
3690}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003691static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003692{
Chris Masond1310b22008-01-24 16:13:08 -05003693 struct extent_io_tree *tree;
3694 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003695 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003696
Chris Masond1310b22008-01-24 16:13:08 -05003697 tree = &BTRFS_I(page->mapping->host)->io_tree;
3698 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003699 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003700 if (ret == 1) {
3701 ClearPagePrivate(page);
3702 set_page_private(page, 0);
3703 page_cache_release(page);
3704 }
3705 return ret;
3706}
Chris Mason39279cc2007-06-12 06:35:45 -04003707
Chris Masone6dcd2d2008-07-17 12:53:50 -04003708static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3709{
Chris Mason98509cf2008-09-11 15:51:43 -04003710 if (PageWriteback(page) || PageDirty(page))
3711 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003712 return __btrfs_releasepage(page, gfp_flags);
3713}
3714
Chris Masona52d9a82007-08-27 16:49:44 -04003715static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3716{
Chris Masond1310b22008-01-24 16:13:08 -05003717 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003718 struct btrfs_ordered_extent *ordered;
3719 u64 page_start = page_offset(page);
3720 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003721
Chris Masone6dcd2d2008-07-17 12:53:50 -04003722 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003723 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003724 if (offset) {
3725 btrfs_releasepage(page, GFP_NOFS);
3726 return;
3727 }
3728
3729 lock_extent(tree, page_start, page_end, GFP_NOFS);
3730 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3731 page_offset(page));
3732 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003733 /*
3734 * IO on this page will never be started, so we need
3735 * to account for any ordered extents now
3736 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003737 clear_extent_bit(tree, page_start, page_end,
3738 EXTENT_DIRTY | EXTENT_DELALLOC |
3739 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003740 btrfs_finish_ordered_io(page->mapping->host,
3741 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003742 btrfs_put_ordered_extent(ordered);
3743 lock_extent(tree, page_start, page_end, GFP_NOFS);
3744 }
3745 clear_extent_bit(tree, page_start, page_end,
3746 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3747 EXTENT_ORDERED,
3748 1, 1, GFP_NOFS);
3749 __btrfs_releasepage(page, GFP_NOFS);
3750
Chris Mason4a096752008-07-21 10:29:44 -04003751 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003752 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003753 ClearPagePrivate(page);
3754 set_page_private(page, 0);
3755 page_cache_release(page);
3756 }
Chris Mason39279cc2007-06-12 06:35:45 -04003757}
3758
Chris Mason9ebefb182007-06-15 13:50:00 -04003759/*
3760 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3761 * called from a page fault handler when a page is first dirtied. Hence we must
3762 * be careful to check for EOF conditions here. We set the page up correctly
3763 * for a written page which means we get ENOSPC checking when writing into
3764 * holes and correct delalloc and unwritten extent mapping on filesystems that
3765 * support these features.
3766 *
3767 * We are not allowed to take the i_mutex here so we have to play games to
3768 * protect against truncate races as the page could now be beyond EOF. Because
3769 * vmtruncate() writes the inode size before removing pages, once we have the
3770 * page lock we can determine safely if the page is beyond EOF. If it is not
3771 * beyond EOF, then the page is guaranteed safe against truncation until we
3772 * unlock the page.
3773 */
3774int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3775{
Chris Mason6da6aba2007-12-18 16:15:09 -05003776 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003777 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003778 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3779 struct btrfs_ordered_extent *ordered;
3780 char *kaddr;
3781 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003782 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003783 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003784 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003785 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003786
Chris Mason1832a6d2007-12-21 16:27:21 -05003787 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003788 if (ret)
3789 goto out;
3790
3791 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003792again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003793 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003794 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003795 page_start = page_offset(page);
3796 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003797
Chris Mason9ebefb182007-06-15 13:50:00 -04003798 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003799 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003800 /* page got truncated out from underneath us */
3801 goto out_unlock;
3802 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003803 wait_on_page_writeback(page);
3804
3805 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3806 set_page_extent_mapped(page);
3807
Chris Masoneb84ae02008-07-17 13:53:27 -04003808 /*
3809 * we can't set the delalloc bits if there are pending ordered
3810 * extents. Drop our locks and wait for them to finish
3811 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003812 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3813 if (ordered) {
3814 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3815 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003816 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003817 btrfs_put_ordered_extent(ordered);
3818 goto again;
3819 }
3820
Chris Masonea8c2812008-08-04 23:17:27 -04003821 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003822 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003823
3824 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003825 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003826 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003827 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003828 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003829
Chris Masone6dcd2d2008-07-17 12:53:50 -04003830 if (zero_start != PAGE_CACHE_SIZE) {
3831 kaddr = kmap(page);
3832 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3833 flush_dcache_page(page);
3834 kunmap(page);
3835 }
Chris Mason247e7432008-07-17 12:53:51 -04003836 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003837 set_page_dirty(page);
3838 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003839
3840out_unlock:
3841 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003842out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003843 return ret;
3844}
3845
Chris Mason39279cc2007-06-12 06:35:45 -04003846static void btrfs_truncate(struct inode *inode)
3847{
3848 struct btrfs_root *root = BTRFS_I(inode)->root;
3849 int ret;
3850 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003851 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003852 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003853
3854 if (!S_ISREG(inode->i_mode))
3855 return;
3856 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3857 return;
3858
3859 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003860 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003861
Chris Mason39279cc2007-06-12 06:35:45 -04003862 trans = btrfs_start_transaction(root, 1);
3863 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003864 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003865
Josef Bacik7b128762008-07-24 12:17:14 -04003866 ret = btrfs_orphan_add(trans, inode);
3867 if (ret)
3868 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003869 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04003870 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05003871 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003872 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003873
Josef Bacik7b128762008-07-24 12:17:14 -04003874 ret = btrfs_orphan_del(trans, inode);
3875 BUG_ON(ret);
3876
3877out:
3878 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003879 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003880 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003881 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003882}
3883
Sven Wegener3b963622008-06-09 21:57:42 -04003884/*
3885 * Invalidate a single dcache entry at the root of the filesystem.
3886 * Needed after creation of snapshot or subvolume.
3887 */
3888void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3889 int namelen)
3890{
3891 struct dentry *alias, *entry;
3892 struct qstr qstr;
3893
3894 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3895 if (alias) {
3896 qstr.name = name;
3897 qstr.len = namelen;
3898 /* change me if btrfs ever gets a d_hash operation */
3899 qstr.hash = full_name_hash(qstr.name, qstr.len);
3900 entry = d_lookup(alias, &qstr);
3901 dput(alias);
3902 if (entry) {
3903 d_invalidate(entry);
3904 dput(entry);
3905 }
3906 }
3907}
3908
Chris Masond352ac62008-09-29 15:18:18 -04003909/*
3910 * create a new subvolume directory/inode (helper for the ioctl).
3911 */
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003912int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003913 struct btrfs_trans_handle *trans, u64 new_dirid,
3914 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003915{
Chris Mason39279cc2007-06-12 06:35:45 -04003916 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003917 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003918 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003919
Josef Bacikaec74772008-07-24 12:12:38 -04003920 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003921 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003922 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003923 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003924 inode->i_op = &btrfs_dir_inode_operations;
3925 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003926 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003927
Chris Mason39279cc2007-06-12 06:35:45 -04003928 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003929 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003930
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003931 error = btrfs_update_inode(trans, new_root, inode);
3932 if (error)
3933 return error;
3934
3935 d_instantiate(dentry, inode);
3936 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003937}
3938
Chris Masond352ac62008-09-29 15:18:18 -04003939/* helper function for file defrag and space balancing. This
3940 * forces readahead on a given range of bytes in an inode
3941 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003942unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003943 struct file_ra_state *ra, struct file *file,
3944 pgoff_t offset, pgoff_t last_index)
3945{
Chris Mason8e7bf942008-04-28 09:02:36 -04003946 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003947
Chris Mason86479a02007-09-10 19:58:16 -04003948 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3949 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04003950}
3951
Chris Mason39279cc2007-06-12 06:35:45 -04003952struct inode *btrfs_alloc_inode(struct super_block *sb)
3953{
3954 struct btrfs_inode *ei;
3955
3956 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3957 if (!ei)
3958 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003959 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003960 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003961 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003962 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3963 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003964 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003965 return &ei->vfs_inode;
3966}
3967
3968void btrfs_destroy_inode(struct inode *inode)
3969{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003970 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003971 WARN_ON(!list_empty(&inode->i_dentry));
3972 WARN_ON(inode->i_data.nrpages);
3973
Josef Bacik33268ea2008-07-24 12:16:36 -04003974 if (BTRFS_I(inode)->i_acl &&
3975 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3976 posix_acl_release(BTRFS_I(inode)->i_acl);
3977 if (BTRFS_I(inode)->i_default_acl &&
3978 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3979 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3980
Yanbcc63ab2008-07-30 16:29:20 -04003981 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003982 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3983 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3984 " list\n", inode->i_ino);
3985 dump_stack();
3986 }
Yanbcc63ab2008-07-30 16:29:20 -04003987 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003988
Chris Masone6dcd2d2008-07-17 12:53:50 -04003989 while(1) {
3990 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3991 if (!ordered)
3992 break;
3993 else {
3994 printk("found ordered extent %Lu %Lu\n",
3995 ordered->file_offset, ordered->len);
3996 btrfs_remove_ordered_extent(inode, ordered);
3997 btrfs_put_ordered_extent(ordered);
3998 btrfs_put_ordered_extent(ordered);
3999 }
4000 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004001 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004002 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4003}
4004
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004005static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004006{
4007 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4008
4009 inode_init_once(&ei->vfs_inode);
4010}
4011
4012void btrfs_destroy_cachep(void)
4013{
4014 if (btrfs_inode_cachep)
4015 kmem_cache_destroy(btrfs_inode_cachep);
4016 if (btrfs_trans_handle_cachep)
4017 kmem_cache_destroy(btrfs_trans_handle_cachep);
4018 if (btrfs_transaction_cachep)
4019 kmem_cache_destroy(btrfs_transaction_cachep);
4020 if (btrfs_bit_radix_cachep)
4021 kmem_cache_destroy(btrfs_bit_radix_cachep);
4022 if (btrfs_path_cachep)
4023 kmem_cache_destroy(btrfs_path_cachep);
4024}
4025
Chris Mason86479a02007-09-10 19:58:16 -04004026struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004027 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004028 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004029{
4030 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004031 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004032}
4033
Chris Mason39279cc2007-06-12 06:35:45 -04004034int btrfs_init_cachep(void)
4035{
Chris Mason86479a02007-09-10 19:58:16 -04004036 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004037 sizeof(struct btrfs_inode),
4038 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004039 if (!btrfs_inode_cachep)
4040 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004041 btrfs_trans_handle_cachep =
4042 btrfs_cache_create("btrfs_trans_handle_cache",
4043 sizeof(struct btrfs_trans_handle),
4044 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004045 if (!btrfs_trans_handle_cachep)
4046 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004047 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004048 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004049 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004050 if (!btrfs_transaction_cachep)
4051 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004052 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004053 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004054 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004055 if (!btrfs_path_cachep)
4056 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004057 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004058 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004059 if (!btrfs_bit_radix_cachep)
4060 goto fail;
4061 return 0;
4062fail:
4063 btrfs_destroy_cachep();
4064 return -ENOMEM;
4065}
4066
4067static int btrfs_getattr(struct vfsmount *mnt,
4068 struct dentry *dentry, struct kstat *stat)
4069{
4070 struct inode *inode = dentry->d_inode;
4071 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05004072 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004073 stat->blocks = (inode_get_bytes(inode) +
4074 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004075 return 0;
4076}
4077
4078static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
4079 struct inode * new_dir,struct dentry *new_dentry)
4080{
4081 struct btrfs_trans_handle *trans;
4082 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4083 struct inode *new_inode = new_dentry->d_inode;
4084 struct inode *old_inode = old_dentry->d_inode;
4085 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004086 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004087 int ret;
4088
4089 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4090 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4091 return -ENOTEMPTY;
4092 }
Chris Mason5f39d392007-10-15 16:14:19 -04004093
Chris Mason1832a6d2007-12-21 16:27:21 -05004094 ret = btrfs_check_free_space(root, 1, 0);
4095 if (ret)
4096 goto out_unlock;
4097
Chris Mason39279cc2007-06-12 06:35:45 -04004098 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004099
Chris Mason39279cc2007-06-12 06:35:45 -04004100 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004101
Chris Masone02119d2008-09-05 16:13:11 -04004102 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004103 old_dir->i_ctime = old_dir->i_mtime = ctime;
4104 new_dir->i_ctime = new_dir->i_mtime = ctime;
4105 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004106
Chris Masone02119d2008-09-05 16:13:11 -04004107 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4108 old_dentry->d_name.name,
4109 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004110 if (ret)
4111 goto out_fail;
4112
4113 if (new_inode) {
4114 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004115 ret = btrfs_unlink_inode(trans, root, new_dir,
4116 new_dentry->d_inode,
4117 new_dentry->d_name.name,
4118 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004119 if (ret)
4120 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004121 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004122 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004123 if (ret)
4124 goto out_fail;
4125 }
Chris Masone02119d2008-09-05 16:13:11 -04004126
Chris Mason39279cc2007-06-12 06:35:45 -04004127 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04004128 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004129 if (ret)
4130 goto out_fail;
4131
Chris Masone02119d2008-09-05 16:13:11 -04004132 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4133 old_inode, new_dentry->d_name.name,
4134 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004135 if (ret)
4136 goto out_fail;
4137
4138out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004139 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004140out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004141 return ret;
4142}
4143
Chris Masond352ac62008-09-29 15:18:18 -04004144/*
4145 * some fairly slow code that needs optimization. This walks the list
4146 * of all the inodes with pending delalloc and forces them to disk.
4147 */
Chris Masonea8c2812008-08-04 23:17:27 -04004148int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4149{
4150 struct list_head *head = &root->fs_info->delalloc_inodes;
4151 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004152 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004153 unsigned long flags;
4154
4155 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4156 while(!list_empty(head)) {
4157 binode = list_entry(head->next, struct btrfs_inode,
4158 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004159 inode = igrab(&binode->vfs_inode);
4160 if (!inode)
4161 list_del_init(&binode->delalloc_inodes);
Chris Masonea8c2812008-08-04 23:17:27 -04004162 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004163 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004164 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004165 iput(inode);
4166 }
4167 cond_resched();
Chris Masonea8c2812008-08-04 23:17:27 -04004168 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4169 }
4170 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason8c8bee12008-09-29 11:19:10 -04004171
4172 /* the filemap_flush will queue IO into the worker threads, but
4173 * we have to make sure the IO is actually started and that
4174 * ordered extents get created before we return
4175 */
4176 atomic_inc(&root->fs_info->async_submit_draining);
4177 while(atomic_read(&root->fs_info->nr_async_submits)) {
4178 wait_event(root->fs_info->async_submit_wait,
4179 (atomic_read(&root->fs_info->nr_async_submits) == 0));
4180 }
4181 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004182 return 0;
4183}
4184
Chris Mason39279cc2007-06-12 06:35:45 -04004185static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4186 const char *symname)
4187{
4188 struct btrfs_trans_handle *trans;
4189 struct btrfs_root *root = BTRFS_I(dir)->root;
4190 struct btrfs_path *path;
4191 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004192 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 int err;
4194 int drop_inode = 0;
4195 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004196 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004197 int name_len;
4198 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004199 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004200 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004201 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004202 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004203
4204 name_len = strlen(symname) + 1;
4205 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4206 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004207
Chris Mason1832a6d2007-12-21 16:27:21 -05004208 err = btrfs_check_free_space(root, 1, 0);
4209 if (err)
4210 goto out_fail;
4211
Chris Mason39279cc2007-06-12 06:35:45 -04004212 trans = btrfs_start_transaction(root, 1);
4213 btrfs_set_trans_block_group(trans, dir);
4214
4215 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4216 if (err) {
4217 err = -ENOSPC;
4218 goto out_unlock;
4219 }
4220
Josef Bacikaec74772008-07-24 12:12:38 -04004221 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004222 dentry->d_name.len,
4223 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004224 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4225 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004226 err = PTR_ERR(inode);
4227 if (IS_ERR(inode))
4228 goto out_unlock;
4229
Josef Bacik33268ea2008-07-24 12:16:36 -04004230 err = btrfs_init_acl(inode, dir);
4231 if (err) {
4232 drop_inode = 1;
4233 goto out_unlock;
4234 }
4235
Chris Mason39279cc2007-06-12 06:35:45 -04004236 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004237 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004238 if (err)
4239 drop_inode = 1;
4240 else {
4241 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004242 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004243 inode->i_fop = &btrfs_file_operations;
4244 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004245 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004246 }
4247 dir->i_sb->s_dirt = 1;
4248 btrfs_update_inode_block_group(trans, inode);
4249 btrfs_update_inode_block_group(trans, dir);
4250 if (drop_inode)
4251 goto out_unlock;
4252
4253 path = btrfs_alloc_path();
4254 BUG_ON(!path);
4255 key.objectid = inode->i_ino;
4256 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004257 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4258 datasize = btrfs_file_extent_calc_inline_size(name_len);
4259 err = btrfs_insert_empty_item(trans, root, path, &key,
4260 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004261 if (err) {
4262 drop_inode = 1;
4263 goto out_unlock;
4264 }
Chris Mason5f39d392007-10-15 16:14:19 -04004265 leaf = path->nodes[0];
4266 ei = btrfs_item_ptr(leaf, path->slots[0],
4267 struct btrfs_file_extent_item);
4268 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4269 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004270 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004271 btrfs_set_file_extent_encryption(leaf, ei, 0);
4272 btrfs_set_file_extent_compression(leaf, ei, 0);
4273 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4274 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4275
Chris Mason39279cc2007-06-12 06:35:45 -04004276 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004277 write_extent_buffer(leaf, symname, ptr, name_len);
4278 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004279 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004280
Chris Mason39279cc2007-06-12 06:35:45 -04004281 inode->i_op = &btrfs_symlink_inode_operations;
4282 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004283 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04004284 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004285 err = btrfs_update_inode(trans, root, inode);
4286 if (err)
4287 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004288
4289out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004290 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004291 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004292out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004293 if (drop_inode) {
4294 inode_dec_link_count(inode);
4295 iput(inode);
4296 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004297 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004298 return err;
4299}
Chris Mason16432982008-04-10 10:23:21 -04004300
Chris Masone6dcd2d2008-07-17 12:53:50 -04004301static int btrfs_set_page_dirty(struct page *page)
4302{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004303 return __set_page_dirty_nobuffers(page);
4304}
4305
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004306static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004307{
4308 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4309 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004310 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004311}
Chris Mason39279cc2007-06-12 06:35:45 -04004312
4313static struct inode_operations btrfs_dir_inode_operations = {
4314 .lookup = btrfs_lookup,
4315 .create = btrfs_create,
4316 .unlink = btrfs_unlink,
4317 .link = btrfs_link,
4318 .mkdir = btrfs_mkdir,
4319 .rmdir = btrfs_rmdir,
4320 .rename = btrfs_rename,
4321 .symlink = btrfs_symlink,
4322 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004323 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004324 .setxattr = btrfs_setxattr,
4325 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004326 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004327 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004328 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004329};
Chris Mason39279cc2007-06-12 06:35:45 -04004330static struct inode_operations btrfs_dir_ro_inode_operations = {
4331 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05004332 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004333};
Chris Mason39279cc2007-06-12 06:35:45 -04004334static struct file_operations btrfs_dir_file_operations = {
4335 .llseek = generic_file_llseek,
4336 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01004337 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004338 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004339#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04004340 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04004341#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04004342 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04004343 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04004344};
4345
Chris Masond1310b22008-01-24 16:13:08 -05004346static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04004347 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05004348 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04004349 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004350 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004351 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04004352 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04004353 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05004354 .set_bit_hook = btrfs_set_bit_hook,
4355 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04004356};
4357
Chris Mason39279cc2007-06-12 06:35:45 -04004358static struct address_space_operations btrfs_aops = {
4359 .readpage = btrfs_readpage,
4360 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04004361 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05004362 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04004363 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04004364 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04004365 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04004366 .invalidatepage = btrfs_invalidatepage,
4367 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004368 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04004369};
4370
4371static struct address_space_operations btrfs_symlink_aops = {
4372 .readpage = btrfs_readpage,
4373 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04004374 .invalidatepage = btrfs_invalidatepage,
4375 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04004376};
4377
4378static struct inode_operations btrfs_file_inode_operations = {
4379 .truncate = btrfs_truncate,
4380 .getattr = btrfs_getattr,
4381 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004382 .setxattr = btrfs_setxattr,
4383 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004384 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004385 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05004386 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004387};
Josef Bacik618e21d2007-07-11 10:18:17 -04004388static struct inode_operations btrfs_special_inode_operations = {
4389 .getattr = btrfs_getattr,
4390 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05004391 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004392 .setxattr = btrfs_setxattr,
4393 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04004394 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004395 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004396};
Chris Mason39279cc2007-06-12 06:35:45 -04004397static struct inode_operations btrfs_symlink_inode_operations = {
4398 .readlink = generic_readlink,
4399 .follow_link = page_follow_link_light,
4400 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05004401 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04004402};