file.c 42.8 KB
Newer Older
Chris Mason's avatar
Chris Mason committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

Chris Mason's avatar
Chris Mason committed
19
20
21
22
23
24
25
26
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/highmem.h>
#include <linux/time.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/backing-dev.h>
#include <linux/mpage.h>
27
#include <linux/falloc.h>
Chris Mason's avatar
Chris Mason committed
28
29
30
31
#include <linux/swap.h>
#include <linux/writeback.h>
#include <linux/statfs.h>
#include <linux/compat.h>
32
#include <linux/slab.h>
Chris Mason's avatar
Chris Mason committed
33
34
35
36
37
38
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
#include "btrfs_inode.h"
#include "ioctl.h"
#include "print-tree.h"
39
40
#include "tree-log.h"
#include "locking.h"
41
#include "compat.h"
Chris Mason's avatar
Chris Mason committed
42

Chris Mason's avatar
Chris Mason committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * when auto defrag is enabled we
 * queue up these defrag structs to remember which
 * inodes need defragging passes
 */
struct inode_defrag {
	struct rb_node rb_node;
	/* objectid */
	u64 ino;
	/*
	 * transid where the defrag was added, we search for
	 * extents newer than this
	 */
	u64 transid;

	/* root objectid */
	u64 root;

	/* last offset we were able to defrag */
	u64 last_offset;

	/* if we've wrapped around back to zero once already */
	int cycled;
};

/* pop a record for an inode into the defrag tree.  The lock
 * must be held already
 *
 * If you're inserting a record for an older transid than an
 * existing record, the transid already in the tree is lowered
 *
 * If an existing record is found the defrag item you
 * pass in is freed
 */
static int __btrfs_add_inode_defrag(struct inode *inode,
				    struct inode_defrag *defrag)
{
	struct btrfs_root *root = BTRFS_I(inode)->root;
	struct inode_defrag *entry;
	struct rb_node **p;
	struct rb_node *parent = NULL;

	p = &root->fs_info->defrag_inodes.rb_node;
	while (*p) {
		parent = *p;
		entry = rb_entry(parent, struct inode_defrag, rb_node);

		if (defrag->ino < entry->ino)
			p = &parent->rb_left;
		else if (defrag->ino > entry->ino)
			p = &parent->rb_right;
		else {
			/* if we're reinserting an entry for
			 * an old defrag run, make sure to
			 * lower the transid of our existing record
			 */
			if (defrag->transid < entry->transid)
				entry->transid = defrag->transid;
			if (defrag->last_offset > entry->last_offset)
				entry->last_offset = defrag->last_offset;
			goto exists;
		}
	}
	BTRFS_I(inode)->in_defrag = 1;
	rb_link_node(&defrag->rb_node, parent, p);
	rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
	return 0;

exists:
	kfree(defrag);
	return 0;

}

/*
 * insert a defrag record for this inode if auto defrag is
 * enabled
 */
int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
			   struct inode *inode)
{
	struct btrfs_root *root = BTRFS_I(inode)->root;
	struct inode_defrag *defrag;
	int ret = 0;
	u64 transid;

	if (!btrfs_test_opt(root, AUTO_DEFRAG))
		return 0;

	if (root->fs_info->closing)
		return 0;

	if (BTRFS_I(inode)->in_defrag)
		return 0;

	if (trans)
		transid = trans->transid;
	else
		transid = BTRFS_I(inode)->root->last_trans;

	defrag = kzalloc(sizeof(*defrag), GFP_NOFS);
	if (!defrag)
		return -ENOMEM;

147
	defrag->ino = btrfs_ino(inode);
Chris Mason's avatar
Chris Mason committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
	defrag->transid = transid;
	defrag->root = root->root_key.objectid;

	spin_lock(&root->fs_info->defrag_inodes_lock);
	if (!BTRFS_I(inode)->in_defrag)
		ret = __btrfs_add_inode_defrag(inode, defrag);
	spin_unlock(&root->fs_info->defrag_inodes_lock);
	return ret;
}

/*
 * must be called with the defrag_inodes lock held
 */
struct inode_defrag *btrfs_find_defrag_inode(struct btrfs_fs_info *info, u64 ino,
					     struct rb_node **next)
{
	struct inode_defrag *entry = NULL;
	struct rb_node *p;
	struct rb_node *parent = NULL;

	p = info->defrag_inodes.rb_node;
	while (p) {
		parent = p;
		entry = rb_entry(parent, struct inode_defrag, rb_node);

		if (ino < entry->ino)
			p = parent->rb_left;
		else if (ino > entry->ino)
			p = parent->rb_right;
		else
			return entry;
	}

	if (next) {
		while (parent && ino > entry->ino) {
			parent = rb_next(parent);
			entry = rb_entry(parent, struct inode_defrag, rb_node);
		}
		*next = parent;
	}
	return NULL;
}

/*
 * run through the list of inodes in the FS that need
 * defragging
 */
int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
{
	struct inode_defrag *defrag;
	struct btrfs_root *inode_root;
	struct inode *inode;
	struct rb_node *n;
	struct btrfs_key key;
	struct btrfs_ioctl_defrag_range_args range;
	u64 first_ino = 0;
	int num_defrag;
	int defrag_batch = 1024;

	memset(&range, 0, sizeof(range));
	range.len = (u64)-1;

	atomic_inc(&fs_info->defrag_running);
	spin_lock(&fs_info->defrag_inodes_lock);
	while(1) {
		n = NULL;

		/* find an inode to defrag */
		defrag = btrfs_find_defrag_inode(fs_info, first_ino, &n);
		if (!defrag) {
			if (n)
				defrag = rb_entry(n, struct inode_defrag, rb_node);
			else if (first_ino) {
				first_ino = 0;
				continue;
			} else {
				break;
			}
		}

		/* remove it from the rbtree */
		first_ino = defrag->ino + 1;
		rb_erase(&defrag->rb_node, &fs_info->defrag_inodes);

		if (fs_info->closing)
			goto next_free;

		spin_unlock(&fs_info->defrag_inodes_lock);

		/* get the inode */
		key.objectid = defrag->root;
		btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
		key.offset = (u64)-1;
		inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
		if (IS_ERR(inode_root))
			goto next;

		key.objectid = defrag->ino;
		btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
		key.offset = 0;

		inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
		if (IS_ERR(inode))
			goto next;

		/* do a chunk of defrag */
		BTRFS_I(inode)->in_defrag = 0;
		range.start = defrag->last_offset;
		num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
					       defrag_batch);
		/*
		 * if we filled the whole defrag batch, there
		 * must be more work to do.  Queue this defrag
		 * again
		 */
		if (num_defrag == defrag_batch) {
			defrag->last_offset = range.start;
			__btrfs_add_inode_defrag(inode, defrag);
			/*
			 * we don't want to kfree defrag, we added it back to
			 * the rbtree
			 */
			defrag = NULL;
		} else if (defrag->last_offset && !defrag->cycled) {
			/*
			 * we didn't fill our defrag batch, but
			 * we didn't start at zero.  Make sure we loop
			 * around to the start of the file.
			 */
			defrag->last_offset = 0;
			defrag->cycled = 1;
			__btrfs_add_inode_defrag(inode, defrag);
			defrag = NULL;
		}

		iput(inode);
next:
		spin_lock(&fs_info->defrag_inodes_lock);
next_free:
		kfree(defrag);
	}
	spin_unlock(&fs_info->defrag_inodes_lock);

	atomic_dec(&fs_info->defrag_running);

	/*
	 * during unmount, we use the transaction_wait queue to
	 * wait for the defragger to stop
	 */
	wake_up(&fs_info->transaction_wait);
	return 0;
}
Chris Mason's avatar
Chris Mason committed
300

Chris Mason's avatar
Chris Mason committed
301
302
303
/* simple helper to fault in pages and copy.  This should go away
 * and be replaced with calls into generic code.
 */
304
static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
Josef Bacik's avatar
Josef Bacik committed
305
					 size_t write_bytes,
306
					 struct page **prepared_pages,
307
					 struct iov_iter *i)
Chris Mason's avatar
Chris Mason committed
308
{
309
	size_t copied = 0;
Josef Bacik's avatar
Josef Bacik committed
310
	size_t total_copied = 0;
311
	int pg = 0;
Chris Mason's avatar
Chris Mason committed
312
313
	int offset = pos & (PAGE_CACHE_SIZE - 1);

314
	while (write_bytes > 0) {
Chris Mason's avatar
Chris Mason committed
315
316
		size_t count = min_t(size_t,
				     PAGE_CACHE_SIZE - offset, write_bytes);
317
		struct page *page = prepared_pages[pg];
318
319
320
321
322
323
324
325
326
		/*
		 * Copy data from userspace to the current page
		 *
		 * Disable pagefault to avoid recursive lock since
		 * the pages are already locked
		 */
		pagefault_disable();
		copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
		pagefault_enable();
327

Chris Mason's avatar
Chris Mason committed
328
329
		/* Flush processor's dcache for this page */
		flush_dcache_page(page);
330
331
332
333
334
335
336
337
338
339
340
341
342

		/*
		 * if we get a partial write, we can end up with
		 * partially up to date pages.  These add
		 * a lot of complexity, so make sure they don't
		 * happen by forcing this copy to be retried.
		 *
		 * The rest of the btrfs_file_write code will fall
		 * back to page at a time copies after we return 0.
		 */
		if (!PageUptodate(page) && copied < count)
			copied = 0;

343
344
		iov_iter_advance(i, copied);
		write_bytes -= copied;
345
		total_copied += copied;
Chris Mason's avatar
Chris Mason committed
346

347
		/* Return to btrfs_file_aio_write to fault page */
Josef Bacik's avatar
Josef Bacik committed
348
		if (unlikely(copied == 0))
349
			break;
350
351
352
353
354
355
356

		if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
			offset += copied;
		} else {
			pg++;
			offset = 0;
		}
Chris Mason's avatar
Chris Mason committed
357
	}
358
	return total_copied;
Chris Mason's avatar
Chris Mason committed
359
360
}

Chris Mason's avatar
Chris Mason committed
361
362
363
/*
 * unlocks pages after btrfs_file_write is done with them
 */
364
void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason's avatar
Chris Mason committed
365
366
367
{
	size_t i;
	for (i = 0; i < num_pages; i++) {
Chris Mason's avatar
Chris Mason committed
368
369
370
371
		/* page checked is some magic around finding pages that
		 * have been modified without going through btrfs_set_page_dirty
		 * clear it here
		 */
Chris Mason's avatar
Chris Mason committed
372
		ClearPageChecked(pages[i]);
Chris Mason's avatar
Chris Mason committed
373
374
375
376
377
378
		unlock_page(pages[i]);
		mark_page_accessed(pages[i]);
		page_cache_release(pages[i]);
	}
}

Chris Mason's avatar
Chris Mason committed
379
380
381
382
383
384
385
386
/*
 * after copy_from_user, pages need to be dirtied and we need to make
 * sure holes are created between the current EOF and the start of
 * any next extents (if required).
 *
 * this also makes the decision about creating an inline extent vs
 * doing real data extents, marking pages dirty and delalloc as required.
 */
387
388
389
390
int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
		      struct page **pages, size_t num_pages,
		      loff_t pos, size_t write_bytes,
		      struct extent_state **cached)
Chris Mason's avatar
Chris Mason committed
391
392
{
	int err = 0;
393
	int i;
394
	u64 num_bytes;
395
396
397
398
	u64 start_pos;
	u64 end_of_last_block;
	u64 end_pos = pos + write_bytes;
	loff_t isize = i_size_read(inode);
Chris Mason's avatar
Chris Mason committed
399

400
	start_pos = pos & ~((u64)root->sectorsize - 1);
401
402
	num_bytes = (write_bytes + pos - start_pos +
		    root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason's avatar
Chris Mason committed
403

404
	end_of_last_block = start_pos + num_bytes - 1;
405
	err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
406
					cached);
Josef Bacik's avatar
Josef Bacik committed
407
408
	if (err)
		return err;
Josef Bacik's avatar
Josef Bacik committed
409

410
411
412
413
414
	for (i = 0; i < num_pages; i++) {
		struct page *p = pages[i];
		SetPageUptodate(p);
		ClearPageChecked(p);
		set_page_dirty(p);
415
	}
Josef Bacik's avatar
Josef Bacik committed
416
417
418
419
420
421
422

	/*
	 * we've only changed i_size in ram, and we haven't updated
	 * the disk i_size.  There is no need to log the inode
	 * at this time.
	 */
	if (end_pos > isize)
423
		i_size_write(inode, end_pos);
424
	return 0;
Chris Mason's avatar
Chris Mason committed
425
426
}

Chris Mason's avatar
Chris Mason committed
427
428
429
430
/*
 * this drops all the extents in the cache that intersect the range
 * [start, end].  Existing extents are split as required.
 */
431
432
int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
			    int skip_pinned)
433
434
{
	struct extent_map *em;
435
436
	struct extent_map *split = NULL;
	struct extent_map *split2 = NULL;
437
	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
438
	u64 len = end - start + 1;
439
440
	int ret;
	int testend = 1;
441
	unsigned long flags;
442
	int compressed = 0;
443

444
	WARN_ON(end < start);
445
	if (end == (u64)-1) {
446
		len = (u64)-1;
447
448
		testend = 0;
	}
449
	while (1) {
450
		if (!split)
451
			split = alloc_extent_map();
452
		if (!split2)
453
			split2 = alloc_extent_map();
454
		BUG_ON(!split || !split2);
455

456
		write_lock(&em_tree->lock);
457
		em = lookup_extent_mapping(em_tree, start, len);
458
		if (!em) {
459
			write_unlock(&em_tree->lock);
460
			break;
461
		}
462
463
		flags = em->flags;
		if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
464
			if (testend && em->start + em->len >= start + len) {
465
				free_extent_map(em);
466
				write_unlock(&em_tree->lock);
467
468
				break;
			}
469
470
			start = em->start + em->len;
			if (testend)
471
472
				len = start + len - (em->start + em->len);
			free_extent_map(em);
473
			write_unlock(&em_tree->lock);
474
475
			continue;
		}
476
		compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
477
		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
478
		remove_extent_mapping(em_tree, em);
479
480
481
482
483

		if (em->block_start < EXTENT_MAP_LAST_BYTE &&
		    em->start < start) {
			split->start = em->start;
			split->len = start - em->start;
484
			split->orig_start = em->orig_start;
485
			split->block_start = em->block_start;
486
487
488
489
490
491

			if (compressed)
				split->block_len = em->block_len;
			else
				split->block_len = split->len;

492
			split->bdev = em->bdev;
493
			split->flags = flags;
494
			split->compress_type = em->compress_type;
495
496
497
498
499
500
501
502
503
504
505
506
507
			ret = add_extent_mapping(em_tree, split);
			BUG_ON(ret);
			free_extent_map(split);
			split = split2;
			split2 = NULL;
		}
		if (em->block_start < EXTENT_MAP_LAST_BYTE &&
		    testend && em->start + em->len > start + len) {
			u64 diff = start + len - em->start;

			split->start = start + len;
			split->len = em->start + em->len - (start + len);
			split->bdev = em->bdev;
508
			split->flags = flags;
509
			split->compress_type = em->compress_type;
510

511
512
513
			if (compressed) {
				split->block_len = em->block_len;
				split->block_start = em->block_start;
514
				split->orig_start = em->orig_start;
515
516
517
			} else {
				split->block_len = split->len;
				split->block_start = em->block_start + diff;
518
				split->orig_start = split->start;
519
			}
520
521
522
523
524
525

			ret = add_extent_mapping(em_tree, split);
			BUG_ON(ret);
			free_extent_map(split);
			split = NULL;
		}
526
		write_unlock(&em_tree->lock);
527

528
529
530
531
532
		/* once for us */
		free_extent_map(em);
		/* once for the tree*/
		free_extent_map(em);
	}
533
534
535
536
	if (split)
		free_extent_map(split);
	if (split2)
		free_extent_map(split2);
537
538
539
	return 0;
}

Chris Mason's avatar
Chris Mason committed
540
541
542
543
544
545
546
547
548
/*
 * this is very complex, but the basic idea is to drop all extents
 * in the range start - end.  hint_block is filled in with a block number
 * that would be a good hint to the block allocator for this file.
 *
 * If an extent intersects the range but is not entirely inside the range
 * it is either truncated or split.  Anything entirely inside the range
 * is deleted from the tree.
 */
Yan, Zheng's avatar
Yan, Zheng committed
549
550
int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
		       u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason's avatar
Chris Mason committed
551
{
Yan, Zheng's avatar
Yan, Zheng committed
552
	struct btrfs_root *root = BTRFS_I(inode)->root;
553
	struct extent_buffer *leaf;
Yan, Zheng's avatar
Yan, Zheng committed
554
	struct btrfs_file_extent_item *fi;
Chris Mason's avatar
Chris Mason committed
555
	struct btrfs_path *path;
556
	struct btrfs_key key;
Yan, Zheng's avatar
Yan, Zheng committed
557
	struct btrfs_key new_key;
558
	u64 ino = btrfs_ino(inode);
Yan, Zheng's avatar
Yan, Zheng committed
559
560
561
562
563
564
565
566
	u64 search_start = start;
	u64 disk_bytenr = 0;
	u64 num_bytes = 0;
	u64 extent_offset = 0;
	u64 extent_end = 0;
	int del_nr = 0;
	int del_slot = 0;
	int extent_type;
Chris Mason's avatar
Chris Mason committed
567
	int recow;
568
	int ret;
Chris Mason's avatar
Chris Mason committed
569

570
571
	if (drop_cache)
		btrfs_drop_extent_cache(inode, start, end - 1, 0);
572

Chris Mason's avatar
Chris Mason committed
573
574
575
	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
Yan, Zheng's avatar
Yan, Zheng committed
576

577
	while (1) {
Chris Mason's avatar
Chris Mason committed
578
		recow = 0;
579
		ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Mason's avatar
Chris Mason committed
580
581
					       search_start, -1);
		if (ret < 0)
Yan, Zheng's avatar
Yan, Zheng committed
582
583
584
585
			break;
		if (ret > 0 && path->slots[0] > 0 && search_start == start) {
			leaf = path->nodes[0];
			btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
586
			if (key.objectid == ino &&
Yan, Zheng's avatar
Yan, Zheng committed
587
588
			    key.type == BTRFS_EXTENT_DATA_KEY)
				path->slots[0]--;
Chris Mason's avatar
Chris Mason committed
589
		}
Yan, Zheng's avatar
Yan, Zheng committed
590
		ret = 0;
591
next_slot:
592
		leaf = path->nodes[0];
Yan, Zheng's avatar
Yan, Zheng committed
593
594
595
596
597
598
599
600
		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
			BUG_ON(del_nr > 0);
			ret = btrfs_next_leaf(root, path);
			if (ret < 0)
				break;
			if (ret > 0) {
				ret = 0;
				break;
601
			}
Yan, Zheng's avatar
Yan, Zheng committed
602
603
604
605
606
			leaf = path->nodes[0];
			recow = 1;
		}

		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
607
		if (key.objectid > ino ||
Yan, Zheng's avatar
Yan, Zheng committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
		    key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
			break;

		fi = btrfs_item_ptr(leaf, path->slots[0],
				    struct btrfs_file_extent_item);
		extent_type = btrfs_file_extent_type(leaf, fi);

		if (extent_type == BTRFS_FILE_EXTENT_REG ||
		    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
			disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
			num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
			extent_offset = btrfs_file_extent_offset(leaf, fi);
			extent_end = key.offset +
				btrfs_file_extent_num_bytes(leaf, fi);
		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
			extent_end = key.offset +
				btrfs_file_extent_inline_len(leaf, fi);
625
		} else {
Yan, Zheng's avatar
Yan, Zheng committed
626
			WARN_ON(1);
627
			extent_end = search_start;
Chris Mason's avatar
Chris Mason committed
628
629
		}

Yan, Zheng's avatar
Yan, Zheng committed
630
631
		if (extent_end <= search_start) {
			path->slots[0]++;
632
			goto next_slot;
Chris Mason's avatar
Chris Mason committed
633
634
		}

Yan, Zheng's avatar
Yan, Zheng committed
635
636
		search_start = max(key.offset, start);
		if (recow) {
637
			btrfs_release_path(path);
Yan, Zheng's avatar
Yan, Zheng committed
638
			continue;
Chris Mason's avatar
Chris Mason committed
639
		}
Yan Zheng's avatar
Yan Zheng committed
640

Yan, Zheng's avatar
Yan, Zheng committed
641
642
643
644
645
646
647
648
649
650
651
652
653
		/*
		 *     | - range to drop - |
		 *  | -------- extent -------- |
		 */
		if (start > key.offset && end < extent_end) {
			BUG_ON(del_nr > 0);
			BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);

			memcpy(&new_key, &key, sizeof(new_key));
			new_key.offset = start;
			ret = btrfs_duplicate_item(trans, root, path,
						   &new_key);
			if (ret == -EAGAIN) {
654
				btrfs_release_path(path);
Yan, Zheng's avatar
Yan, Zheng committed
655
				continue;
Yan Zheng's avatar
Yan Zheng committed
656
			}
Yan, Zheng's avatar
Yan, Zheng committed
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
			if (ret < 0)
				break;

			leaf = path->nodes[0];
			fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
					    struct btrfs_file_extent_item);
			btrfs_set_file_extent_num_bytes(leaf, fi,
							start - key.offset);

			fi = btrfs_item_ptr(leaf, path->slots[0],
					    struct btrfs_file_extent_item);

			extent_offset += start - key.offset;
			btrfs_set_file_extent_offset(leaf, fi, extent_offset);
			btrfs_set_file_extent_num_bytes(leaf, fi,
							extent_end - start);
			btrfs_mark_buffer_dirty(leaf);

			if (disk_bytenr > 0) {
676
				ret = btrfs_inc_extent_ref(trans, root,
Yan, Zheng's avatar
Yan, Zheng committed
677
678
679
680
						disk_bytenr, num_bytes, 0,
						root->root_key.objectid,
						new_key.objectid,
						start - extent_offset);
681
				BUG_ON(ret);
Yan, Zheng's avatar
Yan, Zheng committed
682
				*hint_byte = disk_bytenr;
683
			}
Yan, Zheng's avatar
Yan, Zheng committed
684
			key.offset = start;
Yan Zheng's avatar
Yan Zheng committed
685
		}
Yan, Zheng's avatar
Yan, Zheng committed
686
687
688
689
690
691
		/*
		 *  | ---- range to drop ----- |
		 *      | -------- extent -------- |
		 */
		if (start <= key.offset && end < extent_end) {
			BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
Yan Zheng's avatar
Yan Zheng committed
692

Yan, Zheng's avatar
Yan, Zheng committed
693
694
695
			memcpy(&new_key, &key, sizeof(new_key));
			new_key.offset = end;
			btrfs_set_item_key_safe(trans, root, path, &new_key);
Yan Zheng's avatar
Yan Zheng committed
696

Yan, Zheng's avatar
Yan, Zheng committed
697
698
699
700
701
702
703
704
			extent_offset += end - key.offset;
			btrfs_set_file_extent_offset(leaf, fi, extent_offset);
			btrfs_set_file_extent_num_bytes(leaf, fi,
							extent_end - end);
			btrfs_mark_buffer_dirty(leaf);
			if (disk_bytenr > 0) {
				inode_sub_bytes(inode, end - key.offset);
				*hint_byte = disk_bytenr;
Chris Mason's avatar
Chris Mason committed
705
			}
Yan, Zheng's avatar
Yan, Zheng committed
706
			break;
Chris Mason's avatar
Chris Mason committed
707
		}
708

Yan, Zheng's avatar
Yan, Zheng committed
709
710
711
712
713
714
715
716
		search_start = extent_end;
		/*
		 *       | ---- range to drop ----- |
		 *  | -------- extent -------- |
		 */
		if (start > key.offset && end >= extent_end) {
			BUG_ON(del_nr > 0);
			BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
717

Yan, Zheng's avatar
Yan, Zheng committed
718
719
720
721
722
723
724
725
726
			btrfs_set_file_extent_num_bytes(leaf, fi,
							start - key.offset);
			btrfs_mark_buffer_dirty(leaf);
			if (disk_bytenr > 0) {
				inode_sub_bytes(inode, extent_end - start);
				*hint_byte = disk_bytenr;
			}
			if (end == extent_end)
				break;
727

Yan, Zheng's avatar
Yan, Zheng committed
728
729
			path->slots[0]++;
			goto next_slot;
Zheng Yan's avatar
Zheng Yan committed
730
731
		}

Yan, Zheng's avatar
Yan, Zheng committed
732
733
734
735
736
737
738
739
740
741
742
743
		/*
		 *  | ---- range to drop ----- |
		 *    | ------ extent ------ |
		 */
		if (start <= key.offset && end >= extent_end) {
			if (del_nr == 0) {
				del_slot = path->slots[0];
				del_nr = 1;
			} else {
				BUG_ON(del_slot + del_nr != path->slots[0]);
				del_nr++;
			}
Zheng Yan's avatar
Zheng Yan committed
744

Yan, Zheng's avatar
Yan, Zheng committed
745
			if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
746
				inode_sub_bytes(inode,
Yan, Zheng's avatar
Yan, Zheng committed
747
748
749
750
						extent_end - key.offset);
				extent_end = ALIGN(extent_end,
						   root->sectorsize);
			} else if (disk_bytenr > 0) {
Zheng Yan's avatar
Zheng Yan committed
751
				ret = btrfs_free_extent(trans, root,
Yan, Zheng's avatar
Yan, Zheng committed
752
753
						disk_bytenr, num_bytes, 0,
						root->root_key.objectid,
754
						key.objectid, key.offset -
Yan, Zheng's avatar
Yan, Zheng committed
755
						extent_offset);
Zheng Yan's avatar
Zheng Yan committed
756
				BUG_ON(ret);
Yan, Zheng's avatar
Yan, Zheng committed
757
758
759
				inode_sub_bytes(inode,
						extent_end - key.offset);
				*hint_byte = disk_bytenr;
Zheng Yan's avatar
Zheng Yan committed
760
761
			}

Yan, Zheng's avatar
Yan, Zheng committed
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
			if (end == extent_end)
				break;

			if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
				path->slots[0]++;
				goto next_slot;
			}

			ret = btrfs_del_items(trans, root, path, del_slot,
					      del_nr);
			BUG_ON(ret);

			del_nr = 0;
			del_slot = 0;

777
			btrfs_release_path(path);
Yan, Zheng's avatar
Yan, Zheng committed
778
			continue;
Chris Mason's avatar
Chris Mason committed
779
		}
Yan, Zheng's avatar
Yan, Zheng committed
780
781

		BUG_ON(1);
Chris Mason's avatar
Chris Mason committed
782
	}
Yan, Zheng's avatar
Yan, Zheng committed
783
784
785
786

	if (del_nr > 0) {
		ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
		BUG_ON(ret);
Yan Zheng's avatar
Yan Zheng committed
787
	}
Yan, Zheng's avatar
Yan, Zheng committed
788
789

	btrfs_free_path(path);
Chris Mason's avatar
Chris Mason committed
790
791
792
	return ret;
}

Yan Zheng's avatar
Yan Zheng committed
793
static int extent_mergeable(struct extent_buffer *leaf, int slot,
794
795
			    u64 objectid, u64 bytenr, u64 orig_offset,
			    u64 *start, u64 *end)
Yan Zheng's avatar
Yan Zheng committed
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
{
	struct btrfs_file_extent_item *fi;
	struct btrfs_key key;
	u64 extent_end;

	if (slot < 0 || slot >= btrfs_header_nritems(leaf))
		return 0;

	btrfs_item_key_to_cpu(leaf, &key, slot);
	if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
		return 0;

	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
	if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
	    btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
811
	    btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zheng's avatar
Yan Zheng committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
	    btrfs_file_extent_compression(leaf, fi) ||
	    btrfs_file_extent_encryption(leaf, fi) ||
	    btrfs_file_extent_other_encoding(leaf, fi))
		return 0;

	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
	if ((*start && *start != key.offset) || (*end && *end != extent_end))
		return 0;

	*start = key.offset;
	*end = extent_end;
	return 1;
}

/*
 * Mark extent in the range start - end as written.
 *
 * This changes extent type from 'pre-allocated' to 'regular'. If only
 * part of extent is marked as written, the extent will be split into
 * two or three.
 */
int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
			      struct inode *inode, u64 start, u64 end)
{
Yan, Zheng's avatar
Yan, Zheng committed
836
	struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng's avatar
Yan Zheng committed
837
838
839
840
	struct extent_buffer *leaf;
	struct btrfs_path *path;
	struct btrfs_file_extent_item *fi;
	struct btrfs_key key;
Yan, Zheng's avatar
Yan, Zheng committed
841
	struct btrfs_key new_key;
Yan Zheng's avatar
Yan Zheng committed
842
843
844
	u64 bytenr;
	u64 num_bytes;
	u64 extent_end;
845
	u64 orig_offset;
Yan Zheng's avatar
Yan Zheng committed
846
847
	u64 other_start;
	u64 other_end;
Yan, Zheng's avatar
Yan, Zheng committed
848
849
850
	u64 split;
	int del_nr = 0;
	int del_slot = 0;
851
	int recow;
Yan Zheng's avatar
Yan Zheng committed
852
	int ret;
853
	u64 ino = btrfs_ino(inode);
Yan Zheng's avatar
Yan Zheng committed
854
855
856
857
858
859

	btrfs_drop_extent_cache(inode, start, end - 1, 0);

	path = btrfs_alloc_path();
	BUG_ON(!path);
again:
860
	recow = 0;
Yan, Zheng's avatar
Yan, Zheng committed
861
	split = start;
862
	key.objectid = ino;
Yan Zheng's avatar
Yan Zheng committed
863
	key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng's avatar
Yan, Zheng committed
864
	key.offset = split;
Yan Zheng's avatar
Yan Zheng committed
865
866

	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
867
868
	if (ret < 0)
		goto out;
Yan Zheng's avatar
Yan Zheng committed
869
870
871
872
873
	if (ret > 0 && path->slots[0] > 0)
		path->slots[0]--;

	leaf = path->nodes[0];
	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
874
	BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
Yan Zheng's avatar
Yan Zheng committed
875
876
	fi = btrfs_item_ptr(leaf, path->slots[0],
			    struct btrfs_file_extent_item);
Yan, Zheng's avatar
Yan, Zheng committed
877
878
	BUG_ON(btrfs_file_extent_type(leaf, fi) !=
	       BTRFS_FILE_EXTENT_PREALLOC);
Yan Zheng's avatar
Yan Zheng committed
879
880
881
882
883
	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
	BUG_ON(key.offset > start || extent_end < end);

	bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
	num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
884
	orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
885
886
887
888
889
890
	memcpy(&new_key, &key, sizeof(new_key));

	if (start == key.offset && end < extent_end) {
		other_start = 0;
		other_end = start;
		if (extent_mergeable(leaf, path->slots[0] - 1,
891
				     ino, bytenr, orig_offset,
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
				     &other_start, &other_end)) {
			new_key.offset = end;
			btrfs_set_item_key_safe(trans, root, path, &new_key);
			fi = btrfs_item_ptr(leaf, path->slots[0],
					    struct btrfs_file_extent_item);
			btrfs_set_file_extent_num_bytes(leaf, fi,
							extent_end - end);
			btrfs_set_file_extent_offset(leaf, fi,
						     end - orig_offset);
			fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
					    struct btrfs_file_extent_item);
			btrfs_set_file_extent_num_bytes(leaf, fi,
							end - other_start);
			btrfs_mark_buffer_dirty(leaf);
			goto out;
		}
	}

	if (start > key.offset && end == extent_end) {
		other_start = end;
		other_end = 0;
		if (extent_mergeable(leaf, path->slots[0] + 1,
914
				     ino, bytenr, orig_offset,
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
				     &other_start, &other_end)) {
			fi = btrfs_item_ptr(leaf, path->slots[0],
					    struct btrfs_file_extent_item);
			btrfs_set_file_extent_num_bytes(leaf, fi,
							start - key.offset);
			path->slots[0]++;
			new_key.offset = start;
			btrfs_set_item_key_safe(trans, root, path, &new_key);

			fi = btrfs_item_ptr(leaf, path->slots[0],
					    struct btrfs_file_extent_item);
			btrfs_set_file_extent_num_bytes(leaf, fi,
							other_end - start);
			btrfs_set_file_extent_offset(leaf, fi,
						     start - orig_offset);
			btrfs_mark_buffer_dirty(leaf);
			goto out;
		}
	}
Yan Zheng's avatar
Yan Zheng committed
934

Yan, Zheng's avatar
Yan, Zheng committed
935
936
937
938
939
940
941
	while (start > key.offset || end < extent_end) {
		if (key.offset == start)
			split = end;

		new_key.offset = split;
		ret = btrfs_duplicate_item(trans, root, path, &new_key);
		if (ret == -EAGAIN) {
942
			btrfs_release_path(path);
Yan, Zheng's avatar
Yan, Zheng committed
943
			goto again;
Yan Zheng's avatar
Yan Zheng committed
944
		}
Yan, Zheng's avatar
Yan, Zheng committed
945
		BUG_ON(ret < 0);
Yan Zheng's avatar
Yan Zheng committed
946

Yan, Zheng's avatar
Yan, Zheng committed
947
948
		leaf = path->nodes[0];
		fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zheng's avatar
Yan Zheng committed
949
950
				    struct btrfs_file_extent_item);
		btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng's avatar
Yan, Zheng committed
951
952
953
954
955
956
957
958
						split - key.offset);

		fi = btrfs_item_ptr(leaf, path->slots[0],
				    struct btrfs_file_extent_item);

		btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
		btrfs_set_file_extent_num_bytes(leaf, fi,
						extent_end - split);
Yan Zheng's avatar
Yan Zheng committed
959
960
		btrfs_mark_buffer_dirty(leaf);

Yan, Zheng's avatar
Yan, Zheng committed
961
962
		ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
					   root->root_key.objectid,
963
					   ino, orig_offset);
Yan Zheng's avatar
Yan Zheng committed
964
965
		BUG_ON(ret);

Yan, Zheng's avatar
Yan, Zheng committed
966
967
968
969
		if (split == start) {
			key.offset = start;
		} else {
			BUG_ON(start != key.offset);
Yan Zheng's avatar
Yan Zheng committed
970
			path->slots[0]--;
Yan, Zheng's avatar
Yan, Zheng committed
971
			extent_end = end;
Yan Zheng's avatar
Yan Zheng committed
972
		}
973
		recow = 1;
Yan Zheng's avatar
Yan Zheng committed
974
975
	}

Yan, Zheng's avatar
Yan, Zheng committed
976
977
	other_start = end;
	other_end = 0;
978
	if (extent_mergeable(leaf, path->slots[0] + 1,
979
			     ino, bytenr, orig_offset,
980
981
			     &other_start, &other_end)) {
		if (recow) {
982
			btrfs_release_path(path);
983
984
			goto again;
		}
Yan, Zheng's avatar
Yan, Zheng committed
985
986
987
988
989
		extent_end = other_end;
		del_slot = path->slots[0] + 1;
		del_nr++;
		ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
					0, root->root_key.objectid,
990
					ino, orig_offset);
Yan, Zheng's avatar
Yan, Zheng committed
991
		BUG_ON(ret);
Yan Zheng's avatar
Yan Zheng committed
992
	}
Yan, Zheng's avatar
Yan, Zheng committed
993
994
	other_start = 0;
	other_end = start;
995
	if (extent_mergeable(leaf, path->slots[0] - 1,
996
			     ino, bytenr, orig_offset,
997
998
			     &other_start, &other_end)) {
		if (recow) {
999
			btrfs_release_path(path);
1000
			goto again;
For faster browsing, not all history is shown. View entire blame