blob: a02d026cb0e429e4ac0e6f097a2377b702f4a96f [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * alloc.c
5 *
6 * Extent allocs and frees
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
Mark Fasheh60b11392007-02-16 11:46:50 -080030#include <linux/swap.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080031
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080038#include "aops.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "dlmglue.h"
40#include "extent_map.h"
41#include "inode.h"
42#include "journal.h"
43#include "localalloc.h"
44#include "suballoc.h"
45#include "sysfile.h"
46#include "file.h"
47#include "super.h"
48#include "uptodate.h"
49
50#include "buffer_head_io.h"
51
Mark Fashehccd979b2005-12-15 14:31:24 -080052static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -070053static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
54 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -080055
Mark Fashehdcd05382007-01-16 11:32:23 -080056/*
57 * Structures which describe a path through a btree, and functions to
58 * manipulate them.
59 *
60 * The idea here is to be as generic as possible with the tree
61 * manipulation code.
62 */
63struct ocfs2_path_item {
64 struct buffer_head *bh;
65 struct ocfs2_extent_list *el;
66};
67
68#define OCFS2_MAX_PATH_DEPTH 5
69
70struct ocfs2_path {
71 int p_tree_depth;
72 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
73};
74
75#define path_root_bh(_path) ((_path)->p_node[0].bh)
76#define path_root_el(_path) ((_path)->p_node[0].el)
77#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
78#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
79#define path_num_items(_path) ((_path)->p_tree_depth + 1)
80
81/*
82 * Reset the actual path elements so that we can re-use the structure
83 * to build another path. Generally, this involves freeing the buffer
84 * heads.
85 */
86static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
87{
88 int i, start = 0, depth = 0;
89 struct ocfs2_path_item *node;
90
91 if (keep_root)
92 start = 1;
93
94 for(i = start; i < path_num_items(path); i++) {
95 node = &path->p_node[i];
96
97 brelse(node->bh);
98 node->bh = NULL;
99 node->el = NULL;
100 }
101
102 /*
103 * Tree depth may change during truncate, or insert. If we're
104 * keeping the root extent list, then make sure that our path
105 * structure reflects the proper depth.
106 */
107 if (keep_root)
108 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
109
110 path->p_tree_depth = depth;
111}
112
113static void ocfs2_free_path(struct ocfs2_path *path)
114{
115 if (path) {
116 ocfs2_reinit_path(path, 0);
117 kfree(path);
118 }
119}
120
121/*
122 * Make the *dest path the same as src and re-initialize src path to
123 * have a root only.
124 */
125static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
126{
127 int i;
128
129 BUG_ON(path_root_bh(dest) != path_root_bh(src));
130
131 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
132 brelse(dest->p_node[i].bh);
133
134 dest->p_node[i].bh = src->p_node[i].bh;
135 dest->p_node[i].el = src->p_node[i].el;
136
137 src->p_node[i].bh = NULL;
138 src->p_node[i].el = NULL;
139 }
140}
141
142/*
143 * Insert an extent block at given index.
144 *
145 * This will not take an additional reference on eb_bh.
146 */
147static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
148 struct buffer_head *eb_bh)
149{
150 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
151
152 /*
153 * Right now, no root bh is an extent block, so this helps
154 * catch code errors with dinode trees. The assertion can be
155 * safely removed if we ever need to insert extent block
156 * structures at the root.
157 */
158 BUG_ON(index == 0);
159
160 path->p_node[index].bh = eb_bh;
161 path->p_node[index].el = &eb->h_list;
162}
163
164static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
165 struct ocfs2_extent_list *root_el)
166{
167 struct ocfs2_path *path;
168
169 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
170
171 path = kzalloc(sizeof(*path), GFP_NOFS);
172 if (path) {
173 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
174 get_bh(root_bh);
175 path_root_bh(path) = root_bh;
176 path_root_el(path) = root_el;
177 }
178
179 return path;
180}
181
182/*
183 * Allocate and initialize a new path based on a disk inode tree.
184 */
185static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
186{
187 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
188 struct ocfs2_extent_list *el = &di->id2.i_list;
189
190 return ocfs2_new_path(di_bh, el);
191}
192
193/*
194 * Convenience function to journal all components in a path.
195 */
196static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
197 struct ocfs2_path *path)
198{
199 int i, ret = 0;
200
201 if (!path)
202 goto out;
203
204 for(i = 0; i < path_num_items(path); i++) {
205 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
206 OCFS2_JOURNAL_ACCESS_WRITE);
207 if (ret < 0) {
208 mlog_errno(ret);
209 goto out;
210 }
211 }
212
213out:
214 return ret;
215}
216
217enum ocfs2_contig_type {
218 CONTIG_NONE = 0,
219 CONTIG_LEFT,
220 CONTIG_RIGHT
221};
222
Mark Fashehe48edee2007-03-07 16:46:57 -0800223
224/*
225 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
226 * ocfs2_extent_contig only work properly against leaf nodes!
227 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800228static int ocfs2_block_extent_contig(struct super_block *sb,
229 struct ocfs2_extent_rec *ext,
230 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800231{
Mark Fashehe48edee2007-03-07 16:46:57 -0800232 u64 blk_end = le64_to_cpu(ext->e_blkno);
233
234 blk_end += ocfs2_clusters_to_blocks(sb,
235 le16_to_cpu(ext->e_leaf_clusters));
236
237 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800238}
239
Mark Fashehdcd05382007-01-16 11:32:23 -0800240static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
241 struct ocfs2_extent_rec *right)
242{
Mark Fashehe48edee2007-03-07 16:46:57 -0800243 u32 left_range;
244
245 left_range = le32_to_cpu(left->e_cpos) +
246 le16_to_cpu(left->e_leaf_clusters);
247
248 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800249}
250
251static enum ocfs2_contig_type
252 ocfs2_extent_contig(struct inode *inode,
253 struct ocfs2_extent_rec *ext,
254 struct ocfs2_extent_rec *insert_rec)
255{
256 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
257
258 if (ocfs2_extents_adjacent(ext, insert_rec) &&
259 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
260 return CONTIG_RIGHT;
261
262 blkno = le64_to_cpu(ext->e_blkno);
263 if (ocfs2_extents_adjacent(insert_rec, ext) &&
264 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
265 return CONTIG_LEFT;
266
267 return CONTIG_NONE;
268}
269
270/*
271 * NOTE: We can have pretty much any combination of contiguousness and
272 * appending.
273 *
274 * The usefulness of APPEND_TAIL is more in that it lets us know that
275 * we'll have to update the path to that leaf.
276 */
277enum ocfs2_append_type {
278 APPEND_NONE = 0,
279 APPEND_TAIL,
280};
281
282struct ocfs2_insert_type {
283 enum ocfs2_append_type ins_appending;
284 enum ocfs2_contig_type ins_contig;
285 int ins_contig_index;
286 int ins_free_records;
287 int ins_tree_depth;
288};
289
Mark Fashehccd979b2005-12-15 14:31:24 -0800290/*
291 * How many free extents have we got before we need more meta data?
292 */
293int ocfs2_num_free_extents(struct ocfs2_super *osb,
294 struct inode *inode,
295 struct ocfs2_dinode *fe)
296{
297 int retval;
298 struct ocfs2_extent_list *el;
299 struct ocfs2_extent_block *eb;
300 struct buffer_head *eb_bh = NULL;
301
302 mlog_entry_void();
303
304 if (!OCFS2_IS_VALID_DINODE(fe)) {
305 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
306 retval = -EIO;
307 goto bail;
308 }
309
310 if (fe->i_last_eb_blk) {
311 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
312 &eb_bh, OCFS2_BH_CACHED, inode);
313 if (retval < 0) {
314 mlog_errno(retval);
315 goto bail;
316 }
317 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
318 el = &eb->h_list;
319 } else
320 el = &fe->id2.i_list;
321
322 BUG_ON(el->l_tree_depth != 0);
323
324 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
325bail:
326 if (eb_bh)
327 brelse(eb_bh);
328
329 mlog_exit(retval);
330 return retval;
331}
332
333/* expects array to already be allocated
334 *
335 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
336 * l_count for you
337 */
338static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700339 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800340 struct inode *inode,
341 int wanted,
342 struct ocfs2_alloc_context *meta_ac,
343 struct buffer_head *bhs[])
344{
345 int count, status, i;
346 u16 suballoc_bit_start;
347 u32 num_got;
348 u64 first_blkno;
349 struct ocfs2_extent_block *eb;
350
351 mlog_entry_void();
352
353 count = 0;
354 while (count < wanted) {
355 status = ocfs2_claim_metadata(osb,
356 handle,
357 meta_ac,
358 wanted - count,
359 &suballoc_bit_start,
360 &num_got,
361 &first_blkno);
362 if (status < 0) {
363 mlog_errno(status);
364 goto bail;
365 }
366
367 for(i = count; i < (num_got + count); i++) {
368 bhs[i] = sb_getblk(osb->sb, first_blkno);
369 if (bhs[i] == NULL) {
370 status = -EIO;
371 mlog_errno(status);
372 goto bail;
373 }
374 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
375
376 status = ocfs2_journal_access(handle, inode, bhs[i],
377 OCFS2_JOURNAL_ACCESS_CREATE);
378 if (status < 0) {
379 mlog_errno(status);
380 goto bail;
381 }
382
383 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
384 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
385 /* Ok, setup the minimal stuff here. */
386 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
387 eb->h_blkno = cpu_to_le64(first_blkno);
388 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800389 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800390 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
391 eb->h_list.l_count =
392 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
393
394 suballoc_bit_start++;
395 first_blkno++;
396
397 /* We'll also be dirtied by the caller, so
398 * this isn't absolutely necessary. */
399 status = ocfs2_journal_dirty(handle, bhs[i]);
400 if (status < 0) {
401 mlog_errno(status);
402 goto bail;
403 }
404 }
405
406 count += num_got;
407 }
408
409 status = 0;
410bail:
411 if (status < 0) {
412 for(i = 0; i < wanted; i++) {
413 if (bhs[i])
414 brelse(bhs[i]);
415 bhs[i] = NULL;
416 }
417 }
418 mlog_exit(status);
419 return status;
420}
421
422/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800423 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
424 *
425 * Returns the sum of the rightmost extent rec logical offset and
426 * cluster count.
427 *
428 * ocfs2_add_branch() uses this to determine what logical cluster
429 * value should be populated into the leftmost new branch records.
430 *
431 * ocfs2_shift_tree_depth() uses this to determine the # clusters
432 * value for the new topmost tree record.
433 */
434static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
435{
436 int i;
437
438 i = le16_to_cpu(el->l_next_free_rec) - 1;
439
440 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800441 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800442}
443
444/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800445 * Add an entire tree branch to our inode. eb_bh is the extent block
446 * to start at, if we don't want to start the branch at the dinode
447 * structure.
448 *
449 * last_eb_bh is required as we have to update it's next_leaf pointer
450 * for the new last extent block.
451 *
452 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800453 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800454 */
455static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700456 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800457 struct inode *inode,
458 struct buffer_head *fe_bh,
459 struct buffer_head *eb_bh,
460 struct buffer_head *last_eb_bh,
461 struct ocfs2_alloc_context *meta_ac)
462{
463 int status, new_blocks, i;
464 u64 next_blkno, new_last_eb_blk;
465 struct buffer_head *bh;
466 struct buffer_head **new_eb_bhs = NULL;
467 struct ocfs2_dinode *fe;
468 struct ocfs2_extent_block *eb;
469 struct ocfs2_extent_list *eb_el;
470 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800471 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800472
473 mlog_entry_void();
474
475 BUG_ON(!last_eb_bh);
476
477 fe = (struct ocfs2_dinode *) fe_bh->b_data;
478
479 if (eb_bh) {
480 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
481 el = &eb->h_list;
482 } else
483 el = &fe->id2.i_list;
484
485 /* we never add a branch to a leaf. */
486 BUG_ON(!el->l_tree_depth);
487
488 new_blocks = le16_to_cpu(el->l_tree_depth);
489
490 /* allocate the number of new eb blocks we need */
491 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
492 GFP_KERNEL);
493 if (!new_eb_bhs) {
494 status = -ENOMEM;
495 mlog_errno(status);
496 goto bail;
497 }
498
499 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
500 meta_ac, new_eb_bhs);
501 if (status < 0) {
502 mlog_errno(status);
503 goto bail;
504 }
505
Mark Fashehdcd05382007-01-16 11:32:23 -0800506 eb = (struct ocfs2_extent_block *)last_eb_bh->b_data;
507 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
508
Mark Fashehccd979b2005-12-15 14:31:24 -0800509 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
510 * linked with the rest of the tree.
511 * conversly, new_eb_bhs[0] is the new bottommost leaf.
512 *
513 * when we leave the loop, new_last_eb_blk will point to the
514 * newest leaf, and next_blkno will point to the topmost extent
515 * block. */
516 next_blkno = new_last_eb_blk = 0;
517 for(i = 0; i < new_blocks; i++) {
518 bh = new_eb_bhs[i];
519 eb = (struct ocfs2_extent_block *) bh->b_data;
520 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
521 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
522 status = -EIO;
523 goto bail;
524 }
525 eb_el = &eb->h_list;
526
527 status = ocfs2_journal_access(handle, inode, bh,
528 OCFS2_JOURNAL_ACCESS_CREATE);
529 if (status < 0) {
530 mlog_errno(status);
531 goto bail;
532 }
533
534 eb->h_next_leaf_blk = 0;
535 eb_el->l_tree_depth = cpu_to_le16(i);
536 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800537 /*
538 * This actually counts as an empty extent as
539 * c_clusters == 0
540 */
541 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -0800542 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -0800543 /*
544 * eb_el isn't always an interior node, but even leaf
545 * nodes want a zero'd flags and reserved field so
546 * this gets the whole 32 bits regardless of use.
547 */
548 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800549 if (!eb_el->l_tree_depth)
550 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
551
552 status = ocfs2_journal_dirty(handle, bh);
553 if (status < 0) {
554 mlog_errno(status);
555 goto bail;
556 }
557
558 next_blkno = le64_to_cpu(eb->h_blkno);
559 }
560
561 /* This is a bit hairy. We want to update up to three blocks
562 * here without leaving any of them in an inconsistent state
563 * in case of error. We don't have to worry about
564 * journal_dirty erroring as it won't unless we've aborted the
565 * handle (in which case we would never be here) so reserving
566 * the write with journal_access is all we need to do. */
567 status = ocfs2_journal_access(handle, inode, last_eb_bh,
568 OCFS2_JOURNAL_ACCESS_WRITE);
569 if (status < 0) {
570 mlog_errno(status);
571 goto bail;
572 }
573 status = ocfs2_journal_access(handle, inode, fe_bh,
574 OCFS2_JOURNAL_ACCESS_WRITE);
575 if (status < 0) {
576 mlog_errno(status);
577 goto bail;
578 }
579 if (eb_bh) {
580 status = ocfs2_journal_access(handle, inode, eb_bh,
581 OCFS2_JOURNAL_ACCESS_WRITE);
582 if (status < 0) {
583 mlog_errno(status);
584 goto bail;
585 }
586 }
587
588 /* Link the new branch into the rest of the tree (el will
589 * either be on the fe, or the extent block passed in. */
590 i = le16_to_cpu(el->l_next_free_rec);
591 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800592 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -0800593 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800594 le16_add_cpu(&el->l_next_free_rec, 1);
595
596 /* fe needs a new last extent block pointer, as does the
597 * next_leaf on the previously last-extent-block. */
598 fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
599
600 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
601 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
602
603 status = ocfs2_journal_dirty(handle, last_eb_bh);
604 if (status < 0)
605 mlog_errno(status);
606 status = ocfs2_journal_dirty(handle, fe_bh);
607 if (status < 0)
608 mlog_errno(status);
609 if (eb_bh) {
610 status = ocfs2_journal_dirty(handle, eb_bh);
611 if (status < 0)
612 mlog_errno(status);
613 }
614
615 status = 0;
616bail:
617 if (new_eb_bhs) {
618 for (i = 0; i < new_blocks; i++)
619 if (new_eb_bhs[i])
620 brelse(new_eb_bhs[i]);
621 kfree(new_eb_bhs);
622 }
623
624 mlog_exit(status);
625 return status;
626}
627
628/*
629 * adds another level to the allocation tree.
630 * returns back the new extent block so you can add a branch to it
631 * after this call.
632 */
633static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700634 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800635 struct inode *inode,
636 struct buffer_head *fe_bh,
637 struct ocfs2_alloc_context *meta_ac,
638 struct buffer_head **ret_new_eb_bh)
639{
640 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -0800641 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -0800642 struct buffer_head *new_eb_bh = NULL;
643 struct ocfs2_dinode *fe;
644 struct ocfs2_extent_block *eb;
645 struct ocfs2_extent_list *fe_el;
646 struct ocfs2_extent_list *eb_el;
647
648 mlog_entry_void();
649
650 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
651 &new_eb_bh);
652 if (status < 0) {
653 mlog_errno(status);
654 goto bail;
655 }
656
657 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
658 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
659 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
660 status = -EIO;
661 goto bail;
662 }
663
664 eb_el = &eb->h_list;
665 fe = (struct ocfs2_dinode *) fe_bh->b_data;
666 fe_el = &fe->id2.i_list;
667
668 status = ocfs2_journal_access(handle, inode, new_eb_bh,
669 OCFS2_JOURNAL_ACCESS_CREATE);
670 if (status < 0) {
671 mlog_errno(status);
672 goto bail;
673 }
674
675 /* copy the fe data into the new extent block */
676 eb_el->l_tree_depth = fe_el->l_tree_depth;
677 eb_el->l_next_free_rec = fe_el->l_next_free_rec;
Mark Fashehe48edee2007-03-07 16:46:57 -0800678 for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
679 eb_el->l_recs[i] = fe_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -0800680
681 status = ocfs2_journal_dirty(handle, new_eb_bh);
682 if (status < 0) {
683 mlog_errno(status);
684 goto bail;
685 }
686
687 status = ocfs2_journal_access(handle, inode, fe_bh,
688 OCFS2_JOURNAL_ACCESS_WRITE);
689 if (status < 0) {
690 mlog_errno(status);
691 goto bail;
692 }
693
Mark Fashehdcd05382007-01-16 11:32:23 -0800694 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
695
Mark Fashehccd979b2005-12-15 14:31:24 -0800696 /* update fe now */
697 le16_add_cpu(&fe_el->l_tree_depth, 1);
698 fe_el->l_recs[0].e_cpos = 0;
699 fe_el->l_recs[0].e_blkno = eb->h_blkno;
Mark Fashehe48edee2007-03-07 16:46:57 -0800700 fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
701 for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
702 memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
Mark Fashehccd979b2005-12-15 14:31:24 -0800703 fe_el->l_next_free_rec = cpu_to_le16(1);
704
705 /* If this is our 1st tree depth shift, then last_eb_blk
706 * becomes the allocated extent block */
707 if (fe_el->l_tree_depth == cpu_to_le16(1))
708 fe->i_last_eb_blk = eb->h_blkno;
709
710 status = ocfs2_journal_dirty(handle, fe_bh);
711 if (status < 0) {
712 mlog_errno(status);
713 goto bail;
714 }
715
716 *ret_new_eb_bh = new_eb_bh;
717 new_eb_bh = NULL;
718 status = 0;
719bail:
720 if (new_eb_bh)
721 brelse(new_eb_bh);
722
723 mlog_exit(status);
724 return status;
725}
726
727/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800728 * Should only be called when there is no space left in any of the
729 * leaf nodes. What we want to do is find the lowest tree depth
730 * non-leaf extent block with room for new records. There are three
731 * valid results of this search:
732 *
733 * 1) a lowest extent block is found, then we pass it back in
734 * *lowest_eb_bh and return '0'
735 *
736 * 2) the search fails to find anything, but the dinode has room. We
737 * pass NULL back in *lowest_eb_bh, but still return '0'
738 *
739 * 3) the search fails to find anything AND the dinode is full, in
740 * which case we return > 0
741 *
742 * return status < 0 indicates an error.
743 */
744static int ocfs2_find_branch_target(struct ocfs2_super *osb,
745 struct inode *inode,
746 struct buffer_head *fe_bh,
747 struct buffer_head **target_bh)
748{
749 int status = 0, i;
750 u64 blkno;
751 struct ocfs2_dinode *fe;
752 struct ocfs2_extent_block *eb;
753 struct ocfs2_extent_list *el;
754 struct buffer_head *bh = NULL;
755 struct buffer_head *lowest_bh = NULL;
756
757 mlog_entry_void();
758
759 *target_bh = NULL;
760
761 fe = (struct ocfs2_dinode *) fe_bh->b_data;
762 el = &fe->id2.i_list;
763
764 while(le16_to_cpu(el->l_tree_depth) > 1) {
765 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -0800766 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -0800767 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -0800768 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800769 status = -EIO;
770 goto bail;
771 }
772 i = le16_to_cpu(el->l_next_free_rec) - 1;
773 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
774 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800775 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -0800776 "list where extent # %d has no physical "
777 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -0800778 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -0800779 status = -EIO;
780 goto bail;
781 }
782
783 if (bh) {
784 brelse(bh);
785 bh = NULL;
786 }
787
788 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
789 inode);
790 if (status < 0) {
791 mlog_errno(status);
792 goto bail;
793 }
794
795 eb = (struct ocfs2_extent_block *) bh->b_data;
796 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
797 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
798 status = -EIO;
799 goto bail;
800 }
801 el = &eb->h_list;
802
803 if (le16_to_cpu(el->l_next_free_rec) <
804 le16_to_cpu(el->l_count)) {
805 if (lowest_bh)
806 brelse(lowest_bh);
807 lowest_bh = bh;
808 get_bh(lowest_bh);
809 }
810 }
811
812 /* If we didn't find one and the fe doesn't have any room,
813 * then return '1' */
814 if (!lowest_bh
815 && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
816 status = 1;
817
818 *target_bh = lowest_bh;
819bail:
820 if (bh)
821 brelse(bh);
822
823 mlog_exit(status);
824 return status;
825}
826
Mark Fashehe48edee2007-03-07 16:46:57 -0800827/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -0700828 * Grow a b-tree so that it has more records.
829 *
830 * We might shift the tree depth in which case existing paths should
831 * be considered invalid.
832 *
833 * Tree depth after the grow is returned via *final_depth.
834 */
835static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
836 struct buffer_head *di_bh, int *final_depth,
837 struct buffer_head *last_eb_bh,
838 struct ocfs2_alloc_context *meta_ac)
839{
840 int ret, shift;
841 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
842 int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
843 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
844 struct buffer_head *bh = NULL;
845
846 BUG_ON(meta_ac == NULL);
847
848 shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
849 if (shift < 0) {
850 ret = shift;
851 mlog_errno(ret);
852 goto out;
853 }
854
855 /* We traveled all the way to the bottom of the allocation tree
856 * and didn't find room for any more extents - we need to add
857 * another tree level */
858 if (shift) {
859 BUG_ON(bh);
860 mlog(0, "need to shift tree depth (current = %d)\n", depth);
861
862 /* ocfs2_shift_tree_depth will return us a buffer with
863 * the new extent block (so we can pass that to
864 * ocfs2_add_branch). */
865 ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
866 meta_ac, &bh);
867 if (ret < 0) {
868 mlog_errno(ret);
869 goto out;
870 }
871 depth++;
872 /* Special case: we have room now if we shifted from
873 * tree_depth 0 */
874 if (depth == 1)
875 goto out;
876 }
877
878 /* call ocfs2_add_branch to add the final part of the tree with
879 * the new data. */
880 mlog(0, "add branch. bh = %p\n", bh);
881 ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
882 meta_ac);
883 if (ret < 0) {
884 mlog_errno(ret);
885 goto out;
886 }
887
888out:
889 if (final_depth)
890 *final_depth = depth;
891 brelse(bh);
892 return ret;
893}
894
895/*
Mark Fashehe48edee2007-03-07 16:46:57 -0800896 * This is only valid for leaf nodes, which are the only ones that can
897 * have empty extents anyway.
898 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800899static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
900{
Mark Fashehe48edee2007-03-07 16:46:57 -0800901 return !rec->e_leaf_clusters;
Mark Fashehdcd05382007-01-16 11:32:23 -0800902}
903
904/*
905 * This function will discard the rightmost extent record.
906 */
907static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
908{
909 int next_free = le16_to_cpu(el->l_next_free_rec);
910 int count = le16_to_cpu(el->l_count);
911 unsigned int num_bytes;
912
913 BUG_ON(!next_free);
914 /* This will cause us to go off the end of our extent list. */
915 BUG_ON(next_free >= count);
916
917 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
918
919 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
920}
921
922static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
923 struct ocfs2_extent_rec *insert_rec)
924{
925 int i, insert_index, next_free, has_empty, num_bytes;
926 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
927 struct ocfs2_extent_rec *rec;
928
929 next_free = le16_to_cpu(el->l_next_free_rec);
930 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
931
932 BUG_ON(!next_free);
933
934 /* The tree code before us didn't allow enough room in the leaf. */
935 if (el->l_next_free_rec == el->l_count && !has_empty)
936 BUG();
937
938 /*
939 * The easiest way to approach this is to just remove the
940 * empty extent and temporarily decrement next_free.
941 */
942 if (has_empty) {
943 /*
944 * If next_free was 1 (only an empty extent), this
945 * loop won't execute, which is fine. We still want
946 * the decrement above to happen.
947 */
948 for(i = 0; i < (next_free - 1); i++)
949 el->l_recs[i] = el->l_recs[i+1];
950
951 next_free--;
952 }
953
954 /*
955 * Figure out what the new record index should be.
956 */
957 for(i = 0; i < next_free; i++) {
958 rec = &el->l_recs[i];
959
960 if (insert_cpos < le32_to_cpu(rec->e_cpos))
961 break;
962 }
963 insert_index = i;
964
965 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
966 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
967
968 BUG_ON(insert_index < 0);
969 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
970 BUG_ON(insert_index > next_free);
971
972 /*
973 * No need to memmove if we're just adding to the tail.
974 */
975 if (insert_index != next_free) {
976 BUG_ON(next_free >= le16_to_cpu(el->l_count));
977
978 num_bytes = next_free - insert_index;
979 num_bytes *= sizeof(struct ocfs2_extent_rec);
980 memmove(&el->l_recs[insert_index + 1],
981 &el->l_recs[insert_index],
982 num_bytes);
983 }
984
985 /*
986 * Either we had an empty extent, and need to re-increment or
987 * there was no empty extent on a non full rightmost leaf node,
988 * in which case we still need to increment.
989 */
990 next_free++;
991 el->l_next_free_rec = cpu_to_le16(next_free);
992 /*
993 * Make sure none of the math above just messed up our tree.
994 */
995 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
996
997 el->l_recs[insert_index] = *insert_rec;
998
999}
1000
1001/*
1002 * Create an empty extent record .
1003 *
1004 * l_next_free_rec may be updated.
1005 *
1006 * If an empty extent already exists do nothing.
1007 */
1008static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1009{
1010 int next_free = le16_to_cpu(el->l_next_free_rec);
1011
Mark Fashehe48edee2007-03-07 16:46:57 -08001012 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1013
Mark Fashehdcd05382007-01-16 11:32:23 -08001014 if (next_free == 0)
1015 goto set_and_inc;
1016
1017 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1018 return;
1019
1020 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1021 "Asked to create an empty extent in a full list:\n"
1022 "count = %u, tree depth = %u",
1023 le16_to_cpu(el->l_count),
1024 le16_to_cpu(el->l_tree_depth));
1025
1026 ocfs2_shift_records_right(el);
1027
1028set_and_inc:
1029 le16_add_cpu(&el->l_next_free_rec, 1);
1030 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1031}
1032
1033/*
1034 * For a rotation which involves two leaf nodes, the "root node" is
1035 * the lowest level tree node which contains a path to both leafs. This
1036 * resulting set of information can be used to form a complete "subtree"
1037 *
1038 * This function is passed two full paths from the dinode down to a
1039 * pair of adjacent leaves. It's task is to figure out which path
1040 * index contains the subtree root - this can be the root index itself
1041 * in a worst-case rotation.
1042 *
1043 * The array index of the subtree root is passed back.
1044 */
1045static int ocfs2_find_subtree_root(struct inode *inode,
1046 struct ocfs2_path *left,
1047 struct ocfs2_path *right)
1048{
1049 int i = 0;
1050
1051 /*
1052 * Check that the caller passed in two paths from the same tree.
1053 */
1054 BUG_ON(path_root_bh(left) != path_root_bh(right));
1055
1056 do {
1057 i++;
1058
1059 /*
1060 * The caller didn't pass two adjacent paths.
1061 */
1062 mlog_bug_on_msg(i > left->p_tree_depth,
1063 "Inode %lu, left depth %u, right depth %u\n"
1064 "left leaf blk %llu, right leaf blk %llu\n",
1065 inode->i_ino, left->p_tree_depth,
1066 right->p_tree_depth,
1067 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1068 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1069 } while (left->p_node[i].bh->b_blocknr ==
1070 right->p_node[i].bh->b_blocknr);
1071
1072 return i - 1;
1073}
1074
1075typedef void (path_insert_t)(void *, struct buffer_head *);
1076
1077/*
1078 * Traverse a btree path in search of cpos, starting at root_el.
1079 *
1080 * This code can be called with a cpos larger than the tree, in which
1081 * case it will return the rightmost path.
1082 */
1083static int __ocfs2_find_path(struct inode *inode,
1084 struct ocfs2_extent_list *root_el, u32 cpos,
1085 path_insert_t *func, void *data)
1086{
1087 int i, ret = 0;
1088 u32 range;
1089 u64 blkno;
1090 struct buffer_head *bh = NULL;
1091 struct ocfs2_extent_block *eb;
1092 struct ocfs2_extent_list *el;
1093 struct ocfs2_extent_rec *rec;
1094 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1095
1096 el = root_el;
1097 while (el->l_tree_depth) {
1098 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1099 ocfs2_error(inode->i_sb,
1100 "Inode %llu has empty extent list at "
1101 "depth %u\n",
1102 (unsigned long long)oi->ip_blkno,
1103 le16_to_cpu(el->l_tree_depth));
1104 ret = -EROFS;
1105 goto out;
1106
1107 }
1108
1109 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1110 rec = &el->l_recs[i];
1111
1112 /*
1113 * In the case that cpos is off the allocation
1114 * tree, this should just wind up returning the
1115 * rightmost record.
1116 */
1117 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001118 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001119 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1120 break;
1121 }
1122
1123 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1124 if (blkno == 0) {
1125 ocfs2_error(inode->i_sb,
1126 "Inode %llu has bad blkno in extent list "
1127 "at depth %u (index %d)\n",
1128 (unsigned long long)oi->ip_blkno,
1129 le16_to_cpu(el->l_tree_depth), i);
1130 ret = -EROFS;
1131 goto out;
1132 }
1133
1134 brelse(bh);
1135 bh = NULL;
1136 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1137 &bh, OCFS2_BH_CACHED, inode);
1138 if (ret) {
1139 mlog_errno(ret);
1140 goto out;
1141 }
1142
1143 eb = (struct ocfs2_extent_block *) bh->b_data;
1144 el = &eb->h_list;
1145 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1146 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1147 ret = -EIO;
1148 goto out;
1149 }
1150
1151 if (le16_to_cpu(el->l_next_free_rec) >
1152 le16_to_cpu(el->l_count)) {
1153 ocfs2_error(inode->i_sb,
1154 "Inode %llu has bad count in extent list "
1155 "at block %llu (next free=%u, count=%u)\n",
1156 (unsigned long long)oi->ip_blkno,
1157 (unsigned long long)bh->b_blocknr,
1158 le16_to_cpu(el->l_next_free_rec),
1159 le16_to_cpu(el->l_count));
1160 ret = -EROFS;
1161 goto out;
1162 }
1163
1164 if (func)
1165 func(data, bh);
1166 }
1167
1168out:
1169 /*
1170 * Catch any trailing bh that the loop didn't handle.
1171 */
1172 brelse(bh);
1173
1174 return ret;
1175}
1176
1177/*
1178 * Given an initialized path (that is, it has a valid root extent
1179 * list), this function will traverse the btree in search of the path
1180 * which would contain cpos.
1181 *
1182 * The path traveled is recorded in the path structure.
1183 *
1184 * Note that this will not do any comparisons on leaf node extent
1185 * records, so it will work fine in the case that we just added a tree
1186 * branch.
1187 */
1188struct find_path_data {
1189 int index;
1190 struct ocfs2_path *path;
1191};
1192static void find_path_ins(void *data, struct buffer_head *bh)
1193{
1194 struct find_path_data *fp = data;
1195
1196 get_bh(bh);
1197 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1198 fp->index++;
1199}
1200static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1201 u32 cpos)
1202{
1203 struct find_path_data data;
1204
1205 data.index = 1;
1206 data.path = path;
1207 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1208 find_path_ins, &data);
1209}
1210
1211static void find_leaf_ins(void *data, struct buffer_head *bh)
1212{
1213 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1214 struct ocfs2_extent_list *el = &eb->h_list;
1215 struct buffer_head **ret = data;
1216
1217 /* We want to retain only the leaf block. */
1218 if (le16_to_cpu(el->l_tree_depth) == 0) {
1219 get_bh(bh);
1220 *ret = bh;
1221 }
1222}
1223/*
1224 * Find the leaf block in the tree which would contain cpos. No
1225 * checking of the actual leaf is done.
1226 *
1227 * Some paths want to call this instead of allocating a path structure
1228 * and calling ocfs2_find_path().
1229 *
1230 * This function doesn't handle non btree extent lists.
1231 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001232int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1233 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001234{
1235 int ret;
1236 struct buffer_head *bh = NULL;
1237
1238 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1239 if (ret) {
1240 mlog_errno(ret);
1241 goto out;
1242 }
1243
1244 *leaf_bh = bh;
1245out:
1246 return ret;
1247}
1248
1249/*
1250 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1251 *
1252 * Basically, we've moved stuff around at the bottom of the tree and
1253 * we need to fix up the extent records above the changes to reflect
1254 * the new changes.
1255 *
1256 * left_rec: the record on the left.
1257 * left_child_el: is the child list pointed to by left_rec
1258 * right_rec: the record to the right of left_rec
1259 * right_child_el: is the child list pointed to by right_rec
1260 *
1261 * By definition, this only works on interior nodes.
1262 */
1263static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1264 struct ocfs2_extent_list *left_child_el,
1265 struct ocfs2_extent_rec *right_rec,
1266 struct ocfs2_extent_list *right_child_el)
1267{
1268 u32 left_clusters, right_end;
1269
1270 /*
1271 * Interior nodes never have holes. Their cpos is the cpos of
1272 * the leftmost record in their child list. Their cluster
1273 * count covers the full theoretical range of their child list
1274 * - the range between their cpos and the cpos of the record
1275 * immediately to their right.
1276 */
1277 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1278 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001279 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001280
1281 /*
1282 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001283 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001284 * updating e_cpos to keep the same highest cluster count.
1285 */
1286 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001287 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001288
1289 right_rec->e_cpos = left_rec->e_cpos;
1290 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1291
1292 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001293 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001294}
1295
1296/*
1297 * Adjust the adjacent root node records involved in a
1298 * rotation. left_el_blkno is passed in as a key so that we can easily
1299 * find it's index in the root list.
1300 */
1301static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1302 struct ocfs2_extent_list *left_el,
1303 struct ocfs2_extent_list *right_el,
1304 u64 left_el_blkno)
1305{
1306 int i;
1307
1308 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1309 le16_to_cpu(left_el->l_tree_depth));
1310
1311 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1312 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1313 break;
1314 }
1315
1316 /*
1317 * The path walking code should have never returned a root and
1318 * two paths which are not adjacent.
1319 */
1320 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1321
1322 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1323 &root_el->l_recs[i + 1], right_el);
1324}
1325
1326/*
1327 * We've changed a leaf block (in right_path) and need to reflect that
1328 * change back up the subtree.
1329 *
1330 * This happens in multiple places:
1331 * - When we've moved an extent record from the left path leaf to the right
1332 * path leaf to make room for an empty extent in the left path leaf.
1333 * - When our insert into the right path leaf is at the leftmost edge
1334 * and requires an update of the path immediately to it's left. This
1335 * can occur at the end of some types of rotation and appending inserts.
1336 */
1337static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1338 struct ocfs2_path *left_path,
1339 struct ocfs2_path *right_path,
1340 int subtree_index)
1341{
1342 int ret, i, idx;
1343 struct ocfs2_extent_list *el, *left_el, *right_el;
1344 struct ocfs2_extent_rec *left_rec, *right_rec;
1345 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1346
1347 /*
1348 * Update the counts and position values within all the
1349 * interior nodes to reflect the leaf rotation we just did.
1350 *
1351 * The root node is handled below the loop.
1352 *
1353 * We begin the loop with right_el and left_el pointing to the
1354 * leaf lists and work our way up.
1355 *
1356 * NOTE: within this loop, left_el and right_el always refer
1357 * to the *child* lists.
1358 */
1359 left_el = path_leaf_el(left_path);
1360 right_el = path_leaf_el(right_path);
1361 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1362 mlog(0, "Adjust records at index %u\n", i);
1363
1364 /*
1365 * One nice property of knowing that all of these
1366 * nodes are below the root is that we only deal with
1367 * the leftmost right node record and the rightmost
1368 * left node record.
1369 */
1370 el = left_path->p_node[i].el;
1371 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1372 left_rec = &el->l_recs[idx];
1373
1374 el = right_path->p_node[i].el;
1375 right_rec = &el->l_recs[0];
1376
1377 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1378 right_el);
1379
1380 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1381 if (ret)
1382 mlog_errno(ret);
1383
1384 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1385 if (ret)
1386 mlog_errno(ret);
1387
1388 /*
1389 * Setup our list pointers now so that the current
1390 * parents become children in the next iteration.
1391 */
1392 left_el = left_path->p_node[i].el;
1393 right_el = right_path->p_node[i].el;
1394 }
1395
1396 /*
1397 * At the root node, adjust the two adjacent records which
1398 * begin our path to the leaves.
1399 */
1400
1401 el = left_path->p_node[subtree_index].el;
1402 left_el = left_path->p_node[subtree_index + 1].el;
1403 right_el = right_path->p_node[subtree_index + 1].el;
1404
1405 ocfs2_adjust_root_records(el, left_el, right_el,
1406 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1407
1408 root_bh = left_path->p_node[subtree_index].bh;
1409
1410 ret = ocfs2_journal_dirty(handle, root_bh);
1411 if (ret)
1412 mlog_errno(ret);
1413}
1414
1415static int ocfs2_rotate_subtree_right(struct inode *inode,
1416 handle_t *handle,
1417 struct ocfs2_path *left_path,
1418 struct ocfs2_path *right_path,
1419 int subtree_index)
1420{
1421 int ret, i;
1422 struct buffer_head *right_leaf_bh;
1423 struct buffer_head *left_leaf_bh = NULL;
1424 struct buffer_head *root_bh;
1425 struct ocfs2_extent_list *right_el, *left_el;
1426 struct ocfs2_extent_rec move_rec;
1427
1428 left_leaf_bh = path_leaf_bh(left_path);
1429 left_el = path_leaf_el(left_path);
1430
1431 if (left_el->l_next_free_rec != left_el->l_count) {
1432 ocfs2_error(inode->i_sb,
1433 "Inode %llu has non-full interior leaf node %llu"
1434 "(next free = %u)",
1435 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1436 (unsigned long long)left_leaf_bh->b_blocknr,
1437 le16_to_cpu(left_el->l_next_free_rec));
1438 return -EROFS;
1439 }
1440
1441 /*
1442 * This extent block may already have an empty record, so we
1443 * return early if so.
1444 */
1445 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1446 return 0;
1447
1448 root_bh = left_path->p_node[subtree_index].bh;
1449 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1450
1451 ret = ocfs2_journal_access(handle, inode, root_bh,
1452 OCFS2_JOURNAL_ACCESS_WRITE);
1453 if (ret) {
1454 mlog_errno(ret);
1455 goto out;
1456 }
1457
1458 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1459 ret = ocfs2_journal_access(handle, inode,
1460 right_path->p_node[i].bh,
1461 OCFS2_JOURNAL_ACCESS_WRITE);
1462 if (ret) {
1463 mlog_errno(ret);
1464 goto out;
1465 }
1466
1467 ret = ocfs2_journal_access(handle, inode,
1468 left_path->p_node[i].bh,
1469 OCFS2_JOURNAL_ACCESS_WRITE);
1470 if (ret) {
1471 mlog_errno(ret);
1472 goto out;
1473 }
1474 }
1475
1476 right_leaf_bh = path_leaf_bh(right_path);
1477 right_el = path_leaf_el(right_path);
1478
1479 /* This is a code error, not a disk corruption. */
1480 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1481 "because rightmost leaf block %llu is empty\n",
1482 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1483 (unsigned long long)right_leaf_bh->b_blocknr);
1484
1485 ocfs2_create_empty_extent(right_el);
1486
1487 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1488 if (ret) {
1489 mlog_errno(ret);
1490 goto out;
1491 }
1492
1493 /* Do the copy now. */
1494 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1495 move_rec = left_el->l_recs[i];
1496 right_el->l_recs[0] = move_rec;
1497
1498 /*
1499 * Clear out the record we just copied and shift everything
1500 * over, leaving an empty extent in the left leaf.
1501 *
1502 * We temporarily subtract from next_free_rec so that the
1503 * shift will lose the tail record (which is now defunct).
1504 */
1505 le16_add_cpu(&left_el->l_next_free_rec, -1);
1506 ocfs2_shift_records_right(left_el);
1507 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1508 le16_add_cpu(&left_el->l_next_free_rec, 1);
1509
1510 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1511 if (ret) {
1512 mlog_errno(ret);
1513 goto out;
1514 }
1515
1516 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1517 subtree_index);
1518
1519out:
1520 return ret;
1521}
1522
1523/*
1524 * Given a full path, determine what cpos value would return us a path
1525 * containing the leaf immediately to the left of the current one.
1526 *
1527 * Will return zero if the path passed in is already the leftmost path.
1528 */
1529static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1530 struct ocfs2_path *path, u32 *cpos)
1531{
1532 int i, j, ret = 0;
1533 u64 blkno;
1534 struct ocfs2_extent_list *el;
1535
Mark Fashehe48edee2007-03-07 16:46:57 -08001536 BUG_ON(path->p_tree_depth == 0);
1537
Mark Fashehdcd05382007-01-16 11:32:23 -08001538 *cpos = 0;
1539
1540 blkno = path_leaf_bh(path)->b_blocknr;
1541
1542 /* Start at the tree node just above the leaf and work our way up. */
1543 i = path->p_tree_depth - 1;
1544 while (i >= 0) {
1545 el = path->p_node[i].el;
1546
1547 /*
1548 * Find the extent record just before the one in our
1549 * path.
1550 */
1551 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1552 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1553 if (j == 0) {
1554 if (i == 0) {
1555 /*
1556 * We've determined that the
1557 * path specified is already
1558 * the leftmost one - return a
1559 * cpos of zero.
1560 */
1561 goto out;
1562 }
1563 /*
1564 * The leftmost record points to our
1565 * leaf - we need to travel up the
1566 * tree one level.
1567 */
1568 goto next_node;
1569 }
1570
1571 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001572 *cpos = *cpos + ocfs2_rec_clusters(el,
1573 &el->l_recs[j - 1]);
1574 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08001575 goto out;
1576 }
1577 }
1578
1579 /*
1580 * If we got here, we never found a valid node where
1581 * the tree indicated one should be.
1582 */
1583 ocfs2_error(sb,
1584 "Invalid extent tree at extent block %llu\n",
1585 (unsigned long long)blkno);
1586 ret = -EROFS;
1587 goto out;
1588
1589next_node:
1590 blkno = path->p_node[i].bh->b_blocknr;
1591 i--;
1592 }
1593
1594out:
1595 return ret;
1596}
1597
1598static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1599 struct ocfs2_path *path)
1600{
1601 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1;
1602
1603 if (handle->h_buffer_credits < credits)
1604 return ocfs2_extend_trans(handle, credits);
1605
1606 return 0;
1607}
1608
1609/*
1610 * Trap the case where we're inserting into the theoretical range past
1611 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1612 * whose cpos is less than ours into the right leaf.
1613 *
1614 * It's only necessary to look at the rightmost record of the left
1615 * leaf because the logic that calls us should ensure that the
1616 * theoretical ranges in the path components above the leaves are
1617 * correct.
1618 */
1619static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1620 u32 insert_cpos)
1621{
1622 struct ocfs2_extent_list *left_el;
1623 struct ocfs2_extent_rec *rec;
1624 int next_free;
1625
1626 left_el = path_leaf_el(left_path);
1627 next_free = le16_to_cpu(left_el->l_next_free_rec);
1628 rec = &left_el->l_recs[next_free - 1];
1629
1630 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1631 return 1;
1632 return 0;
1633}
1634
1635/*
1636 * Rotate all the records in a btree right one record, starting at insert_cpos.
1637 *
1638 * The path to the rightmost leaf should be passed in.
1639 *
1640 * The array is assumed to be large enough to hold an entire path (tree depth).
1641 *
1642 * Upon succesful return from this function:
1643 *
1644 * - The 'right_path' array will contain a path to the leaf block
1645 * whose range contains e_cpos.
1646 * - That leaf block will have a single empty extent in list index 0.
1647 * - In the case that the rotation requires a post-insert update,
1648 * *ret_left_path will contain a valid path which can be passed to
1649 * ocfs2_insert_path().
1650 */
1651static int ocfs2_rotate_tree_right(struct inode *inode,
1652 handle_t *handle,
1653 u32 insert_cpos,
1654 struct ocfs2_path *right_path,
1655 struct ocfs2_path **ret_left_path)
1656{
1657 int ret, start;
1658 u32 cpos;
1659 struct ocfs2_path *left_path = NULL;
1660
1661 *ret_left_path = NULL;
1662
1663 left_path = ocfs2_new_path(path_root_bh(right_path),
1664 path_root_el(right_path));
1665 if (!left_path) {
1666 ret = -ENOMEM;
1667 mlog_errno(ret);
1668 goto out;
1669 }
1670
1671 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1672 if (ret) {
1673 mlog_errno(ret);
1674 goto out;
1675 }
1676
1677 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1678
1679 /*
1680 * What we want to do here is:
1681 *
1682 * 1) Start with the rightmost path.
1683 *
1684 * 2) Determine a path to the leaf block directly to the left
1685 * of that leaf.
1686 *
1687 * 3) Determine the 'subtree root' - the lowest level tree node
1688 * which contains a path to both leaves.
1689 *
1690 * 4) Rotate the subtree.
1691 *
1692 * 5) Find the next subtree by considering the left path to be
1693 * the new right path.
1694 *
1695 * The check at the top of this while loop also accepts
1696 * insert_cpos == cpos because cpos is only a _theoretical_
1697 * value to get us the left path - insert_cpos might very well
1698 * be filling that hole.
1699 *
1700 * Stop at a cpos of '0' because we either started at the
1701 * leftmost branch (i.e., a tree with one branch and a
1702 * rotation inside of it), or we've gone as far as we can in
1703 * rotating subtrees.
1704 */
1705 while (cpos && insert_cpos <= cpos) {
1706 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1707 insert_cpos, cpos);
1708
1709 ret = ocfs2_find_path(inode, left_path, cpos);
1710 if (ret) {
1711 mlog_errno(ret);
1712 goto out;
1713 }
1714
1715 mlog_bug_on_msg(path_leaf_bh(left_path) ==
1716 path_leaf_bh(right_path),
1717 "Inode %lu: error during insert of %u "
1718 "(left path cpos %u) results in two identical "
1719 "paths ending at %llu\n",
1720 inode->i_ino, insert_cpos, cpos,
1721 (unsigned long long)
1722 path_leaf_bh(left_path)->b_blocknr);
1723
1724 if (ocfs2_rotate_requires_path_adjustment(left_path,
1725 insert_cpos)) {
1726 mlog(0, "Path adjustment required\n");
1727
1728 /*
1729 * We've rotated the tree as much as we
1730 * should. The rest is up to
1731 * ocfs2_insert_path() to complete, after the
1732 * record insertion. We indicate this
1733 * situation by returning the left path.
1734 *
1735 * The reason we don't adjust the records here
1736 * before the record insert is that an error
1737 * later might break the rule where a parent
1738 * record e_cpos will reflect the actual
1739 * e_cpos of the 1st nonempty record of the
1740 * child list.
1741 */
1742 *ret_left_path = left_path;
1743 goto out_ret_path;
1744 }
1745
1746 start = ocfs2_find_subtree_root(inode, left_path, right_path);
1747
1748 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1749 start,
1750 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1751 right_path->p_tree_depth);
1752
1753 ret = ocfs2_extend_rotate_transaction(handle, start,
1754 right_path);
1755 if (ret) {
1756 mlog_errno(ret);
1757 goto out;
1758 }
1759
1760 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1761 right_path, start);
1762 if (ret) {
1763 mlog_errno(ret);
1764 goto out;
1765 }
1766
1767 /*
1768 * There is no need to re-read the next right path
1769 * as we know that it'll be our current left
1770 * path. Optimize by copying values instead.
1771 */
1772 ocfs2_mv_path(right_path, left_path);
1773
1774 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1775 &cpos);
1776 if (ret) {
1777 mlog_errno(ret);
1778 goto out;
1779 }
1780 }
1781
1782out:
1783 ocfs2_free_path(left_path);
1784
1785out_ret_path:
1786 return ret;
1787}
1788
1789/*
1790 * Do the final bits of extent record insertion at the target leaf
1791 * list. If this leaf is part of an allocation tree, it is assumed
1792 * that the tree above has been prepared.
1793 */
1794static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
1795 struct ocfs2_extent_list *el,
1796 struct ocfs2_insert_type *insert,
1797 struct inode *inode)
1798{
1799 int i = insert->ins_contig_index;
1800 unsigned int range;
1801 struct ocfs2_extent_rec *rec;
1802
Mark Fashehe48edee2007-03-07 16:46:57 -08001803 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08001804
1805 /*
1806 * Contiguous insert - either left or right.
1807 */
1808 if (insert->ins_contig != CONTIG_NONE) {
1809 rec = &el->l_recs[i];
1810 if (insert->ins_contig == CONTIG_LEFT) {
1811 rec->e_blkno = insert_rec->e_blkno;
1812 rec->e_cpos = insert_rec->e_cpos;
1813 }
Mark Fashehe48edee2007-03-07 16:46:57 -08001814 le16_add_cpu(&rec->e_leaf_clusters,
1815 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08001816 return;
1817 }
1818
1819 /*
1820 * Handle insert into an empty leaf.
1821 */
1822 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
1823 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
1824 ocfs2_is_empty_extent(&el->l_recs[0]))) {
1825 el->l_recs[0] = *insert_rec;
1826 el->l_next_free_rec = cpu_to_le16(1);
1827 return;
1828 }
1829
1830 /*
1831 * Appending insert.
1832 */
1833 if (insert->ins_appending == APPEND_TAIL) {
1834 i = le16_to_cpu(el->l_next_free_rec) - 1;
1835 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08001836 range = le32_to_cpu(rec->e_cpos)
1837 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001838 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
1839
1840 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
1841 le16_to_cpu(el->l_count),
1842 "inode %lu, depth %u, count %u, next free %u, "
1843 "rec.cpos %u, rec.clusters %u, "
1844 "insert.cpos %u, insert.clusters %u\n",
1845 inode->i_ino,
1846 le16_to_cpu(el->l_tree_depth),
1847 le16_to_cpu(el->l_count),
1848 le16_to_cpu(el->l_next_free_rec),
1849 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08001850 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08001851 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08001852 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08001853 i++;
1854 el->l_recs[i] = *insert_rec;
1855 le16_add_cpu(&el->l_next_free_rec, 1);
1856 return;
1857 }
1858
1859 /*
1860 * Ok, we have to rotate.
1861 *
1862 * At this point, it is safe to assume that inserting into an
1863 * empty leaf and appending to a leaf have both been handled
1864 * above.
1865 *
1866 * This leaf needs to have space, either by the empty 1st
1867 * extent record, or by virtue of an l_next_rec < l_count.
1868 */
1869 ocfs2_rotate_leaf(el, insert_rec);
1870}
1871
1872static inline void ocfs2_update_dinode_clusters(struct inode *inode,
1873 struct ocfs2_dinode *di,
1874 u32 clusters)
1875{
1876 le32_add_cpu(&di->i_clusters, clusters);
1877 spin_lock(&OCFS2_I(inode)->ip_lock);
1878 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
1879 spin_unlock(&OCFS2_I(inode)->ip_lock);
1880}
1881
1882static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
1883 struct ocfs2_extent_rec *insert_rec,
1884 struct ocfs2_path *right_path,
1885 struct ocfs2_path **ret_left_path)
1886{
1887 int ret, i, next_free;
1888 struct buffer_head *bh;
1889 struct ocfs2_extent_list *el;
1890 struct ocfs2_path *left_path = NULL;
1891
1892 *ret_left_path = NULL;
1893
1894 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08001895 * This shouldn't happen for non-trees. The extent rec cluster
1896 * count manipulation below only works for interior nodes.
1897 */
1898 BUG_ON(right_path->p_tree_depth == 0);
1899
1900 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08001901 * If our appending insert is at the leftmost edge of a leaf,
1902 * then we might need to update the rightmost records of the
1903 * neighboring path.
1904 */
1905 el = path_leaf_el(right_path);
1906 next_free = le16_to_cpu(el->l_next_free_rec);
1907 if (next_free == 0 ||
1908 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
1909 u32 left_cpos;
1910
1911 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1912 &left_cpos);
1913 if (ret) {
1914 mlog_errno(ret);
1915 goto out;
1916 }
1917
1918 mlog(0, "Append may need a left path update. cpos: %u, "
1919 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
1920 left_cpos);
1921
1922 /*
1923 * No need to worry if the append is already in the
1924 * leftmost leaf.
1925 */
1926 if (left_cpos) {
1927 left_path = ocfs2_new_path(path_root_bh(right_path),
1928 path_root_el(right_path));
1929 if (!left_path) {
1930 ret = -ENOMEM;
1931 mlog_errno(ret);
1932 goto out;
1933 }
1934
1935 ret = ocfs2_find_path(inode, left_path, left_cpos);
1936 if (ret) {
1937 mlog_errno(ret);
1938 goto out;
1939 }
1940
1941 /*
1942 * ocfs2_insert_path() will pass the left_path to the
1943 * journal for us.
1944 */
1945 }
1946 }
1947
1948 ret = ocfs2_journal_access_path(inode, handle, right_path);
1949 if (ret) {
1950 mlog_errno(ret);
1951 goto out;
1952 }
1953
1954 el = path_root_el(right_path);
1955 bh = path_root_bh(right_path);
1956 i = 0;
1957 while (1) {
Mark Fashehe48edee2007-03-07 16:46:57 -08001958 struct ocfs2_extent_rec *rec;
1959
Mark Fashehdcd05382007-01-16 11:32:23 -08001960 next_free = le16_to_cpu(el->l_next_free_rec);
1961 if (next_free == 0) {
1962 ocfs2_error(inode->i_sb,
1963 "Dinode %llu has a bad extent list",
1964 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1965 ret = -EIO;
1966 goto out;
1967 }
1968
Mark Fashehe48edee2007-03-07 16:46:57 -08001969 rec = &el->l_recs[next_free - 1];
1970
1971 rec->e_int_clusters = insert_rec->e_cpos;
1972 le32_add_cpu(&rec->e_int_clusters,
1973 le16_to_cpu(insert_rec->e_leaf_clusters));
1974 le32_add_cpu(&rec->e_int_clusters,
1975 -le32_to_cpu(rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08001976
1977 ret = ocfs2_journal_dirty(handle, bh);
1978 if (ret)
1979 mlog_errno(ret);
1980
Mark Fashehe48edee2007-03-07 16:46:57 -08001981 /* Don't touch the leaf node */
Mark Fashehdcd05382007-01-16 11:32:23 -08001982 if (++i >= right_path->p_tree_depth)
1983 break;
1984
1985 bh = right_path->p_node[i].bh;
1986 el = right_path->p_node[i].el;
1987 }
1988
1989 *ret_left_path = left_path;
1990 ret = 0;
1991out:
1992 if (ret != 0)
1993 ocfs2_free_path(left_path);
1994
1995 return ret;
1996}
1997
1998/*
1999 * This function only does inserts on an allocation b-tree. For dinode
2000 * lists, ocfs2_insert_at_leaf() is called directly.
2001 *
2002 * right_path is the path we want to do the actual insert
2003 * in. left_path should only be passed in if we need to update that
2004 * portion of the tree after an edge insert.
2005 */
2006static int ocfs2_insert_path(struct inode *inode,
2007 handle_t *handle,
2008 struct ocfs2_path *left_path,
2009 struct ocfs2_path *right_path,
2010 struct ocfs2_extent_rec *insert_rec,
2011 struct ocfs2_insert_type *insert)
2012{
2013 int ret, subtree_index;
2014 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
2015 struct ocfs2_extent_list *el;
2016
2017 /*
2018 * Pass both paths to the journal. The majority of inserts
2019 * will be touching all components anyway.
2020 */
2021 ret = ocfs2_journal_access_path(inode, handle, right_path);
2022 if (ret < 0) {
2023 mlog_errno(ret);
2024 goto out;
2025 }
2026
2027 if (left_path) {
2028 int credits = handle->h_buffer_credits;
2029
2030 /*
2031 * There's a chance that left_path got passed back to
2032 * us without being accounted for in the
2033 * journal. Extend our transaction here to be sure we
2034 * can change those blocks.
2035 */
2036 credits += left_path->p_tree_depth;
2037
2038 ret = ocfs2_extend_trans(handle, credits);
2039 if (ret < 0) {
2040 mlog_errno(ret);
2041 goto out;
2042 }
2043
2044 ret = ocfs2_journal_access_path(inode, handle, left_path);
2045 if (ret < 0) {
2046 mlog_errno(ret);
2047 goto out;
2048 }
2049 }
2050
2051 el = path_leaf_el(right_path);
2052
2053 ocfs2_insert_at_leaf(insert_rec, el, insert, inode);
2054 ret = ocfs2_journal_dirty(handle, leaf_bh);
2055 if (ret)
2056 mlog_errno(ret);
2057
2058 if (left_path) {
2059 /*
2060 * The rotate code has indicated that we need to fix
2061 * up portions of the tree after the insert.
2062 *
2063 * XXX: Should we extend the transaction here?
2064 */
2065 subtree_index = ocfs2_find_subtree_root(inode, left_path,
2066 right_path);
2067 ocfs2_complete_edge_insert(inode, handle, left_path,
2068 right_path, subtree_index);
2069 }
2070
2071 ret = 0;
2072out:
2073 return ret;
2074}
2075
2076static int ocfs2_do_insert_extent(struct inode *inode,
2077 handle_t *handle,
2078 struct buffer_head *di_bh,
2079 struct ocfs2_extent_rec *insert_rec,
2080 struct ocfs2_insert_type *type)
2081{
2082 int ret, rotate = 0;
2083 u32 cpos;
2084 struct ocfs2_path *right_path = NULL;
2085 struct ocfs2_path *left_path = NULL;
2086 struct ocfs2_dinode *di;
2087 struct ocfs2_extent_list *el;
2088
2089 di = (struct ocfs2_dinode *) di_bh->b_data;
2090 el = &di->id2.i_list;
2091
2092 ret = ocfs2_journal_access(handle, inode, di_bh,
2093 OCFS2_JOURNAL_ACCESS_WRITE);
2094 if (ret) {
2095 mlog_errno(ret);
2096 goto out;
2097 }
2098
2099 if (le16_to_cpu(el->l_tree_depth) == 0) {
2100 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
2101 goto out_update_clusters;
2102 }
2103
2104 right_path = ocfs2_new_inode_path(di_bh);
2105 if (!right_path) {
2106 ret = -ENOMEM;
2107 mlog_errno(ret);
2108 goto out;
2109 }
2110
2111 /*
2112 * Determine the path to start with. Rotations need the
2113 * rightmost path, everything else can go directly to the
2114 * target leaf.
2115 */
2116 cpos = le32_to_cpu(insert_rec->e_cpos);
2117 if (type->ins_appending == APPEND_NONE &&
2118 type->ins_contig == CONTIG_NONE) {
2119 rotate = 1;
2120 cpos = UINT_MAX;
2121 }
2122
2123 ret = ocfs2_find_path(inode, right_path, cpos);
2124 if (ret) {
2125 mlog_errno(ret);
2126 goto out;
2127 }
2128
2129 /*
2130 * Rotations and appends need special treatment - they modify
2131 * parts of the tree's above them.
2132 *
2133 * Both might pass back a path immediate to the left of the
2134 * one being inserted to. This will be cause
2135 * ocfs2_insert_path() to modify the rightmost records of
2136 * left_path to account for an edge insert.
2137 *
2138 * XXX: When modifying this code, keep in mind that an insert
2139 * can wind up skipping both of these two special cases...
2140 */
2141 if (rotate) {
2142 ret = ocfs2_rotate_tree_right(inode, handle,
2143 le32_to_cpu(insert_rec->e_cpos),
2144 right_path, &left_path);
2145 if (ret) {
2146 mlog_errno(ret);
2147 goto out;
2148 }
2149 } else if (type->ins_appending == APPEND_TAIL
2150 && type->ins_contig != CONTIG_LEFT) {
2151 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
2152 right_path, &left_path);
2153 if (ret) {
2154 mlog_errno(ret);
2155 goto out;
2156 }
2157 }
2158
2159 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
2160 insert_rec, type);
2161 if (ret) {
2162 mlog_errno(ret);
2163 goto out;
2164 }
2165
2166out_update_clusters:
2167 ocfs2_update_dinode_clusters(inode, di,
Mark Fashehe48edee2007-03-07 16:46:57 -08002168 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08002169
2170 ret = ocfs2_journal_dirty(handle, di_bh);
2171 if (ret)
2172 mlog_errno(ret);
2173
2174out:
2175 ocfs2_free_path(left_path);
2176 ocfs2_free_path(right_path);
2177
2178 return ret;
2179}
2180
2181static void ocfs2_figure_contig_type(struct inode *inode,
2182 struct ocfs2_insert_type *insert,
2183 struct ocfs2_extent_list *el,
2184 struct ocfs2_extent_rec *insert_rec)
2185{
2186 int i;
2187 enum ocfs2_contig_type contig_type = CONTIG_NONE;
2188
Mark Fashehe48edee2007-03-07 16:46:57 -08002189 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2190
Mark Fashehdcd05382007-01-16 11:32:23 -08002191 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2192 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
2193 insert_rec);
2194 if (contig_type != CONTIG_NONE) {
2195 insert->ins_contig_index = i;
2196 break;
2197 }
2198 }
2199 insert->ins_contig = contig_type;
2200}
2201
2202/*
2203 * This should only be called against the righmost leaf extent list.
2204 *
2205 * ocfs2_figure_appending_type() will figure out whether we'll have to
2206 * insert at the tail of the rightmost leaf.
2207 *
2208 * This should also work against the dinode list for tree's with 0
2209 * depth. If we consider the dinode list to be the rightmost leaf node
2210 * then the logic here makes sense.
2211 */
2212static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
2213 struct ocfs2_extent_list *el,
2214 struct ocfs2_extent_rec *insert_rec)
2215{
2216 int i;
2217 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
2218 struct ocfs2_extent_rec *rec;
2219
2220 insert->ins_appending = APPEND_NONE;
2221
Mark Fashehe48edee2007-03-07 16:46:57 -08002222 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08002223
2224 if (!el->l_next_free_rec)
2225 goto set_tail_append;
2226
2227 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
2228 /* Were all records empty? */
2229 if (le16_to_cpu(el->l_next_free_rec) == 1)
2230 goto set_tail_append;
2231 }
2232
2233 i = le16_to_cpu(el->l_next_free_rec) - 1;
2234 rec = &el->l_recs[i];
2235
Mark Fashehe48edee2007-03-07 16:46:57 -08002236 if (cpos >=
2237 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08002238 goto set_tail_append;
2239
2240 return;
2241
2242set_tail_append:
2243 insert->ins_appending = APPEND_TAIL;
2244}
2245
2246/*
2247 * Helper function called at the begining of an insert.
2248 *
2249 * This computes a few things that are commonly used in the process of
2250 * inserting into the btree:
2251 * - Whether the new extent is contiguous with an existing one.
2252 * - The current tree depth.
2253 * - Whether the insert is an appending one.
2254 * - The total # of free records in the tree.
2255 *
2256 * All of the information is stored on the ocfs2_insert_type
2257 * structure.
2258 */
2259static int ocfs2_figure_insert_type(struct inode *inode,
2260 struct buffer_head *di_bh,
2261 struct buffer_head **last_eb_bh,
2262 struct ocfs2_extent_rec *insert_rec,
2263 struct ocfs2_insert_type *insert)
2264{
2265 int ret;
2266 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2267 struct ocfs2_extent_block *eb;
2268 struct ocfs2_extent_list *el;
2269 struct ocfs2_path *path = NULL;
2270 struct buffer_head *bh = NULL;
2271
2272 el = &di->id2.i_list;
2273 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
2274
2275 if (el->l_tree_depth) {
2276 /*
2277 * If we have tree depth, we read in the
2278 * rightmost extent block ahead of time as
2279 * ocfs2_figure_insert_type() and ocfs2_add_branch()
2280 * may want it later.
2281 */
2282 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2283 le64_to_cpu(di->i_last_eb_blk), &bh,
2284 OCFS2_BH_CACHED, inode);
2285 if (ret) {
2286 mlog_exit(ret);
2287 goto out;
2288 }
2289 eb = (struct ocfs2_extent_block *) bh->b_data;
2290 el = &eb->h_list;
2291 }
2292
2293 /*
2294 * Unless we have a contiguous insert, we'll need to know if
2295 * there is room left in our allocation tree for another
2296 * extent record.
2297 *
2298 * XXX: This test is simplistic, we can search for empty
2299 * extent records too.
2300 */
2301 insert->ins_free_records = le16_to_cpu(el->l_count) -
2302 le16_to_cpu(el->l_next_free_rec);
2303
2304 if (!insert->ins_tree_depth) {
2305 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2306 ocfs2_figure_appending_type(insert, el, insert_rec);
2307 return 0;
2308 }
2309
2310 path = ocfs2_new_inode_path(di_bh);
2311 if (!path) {
2312 ret = -ENOMEM;
2313 mlog_errno(ret);
2314 goto out;
2315 }
2316
2317 /*
2318 * In the case that we're inserting past what the tree
2319 * currently accounts for, ocfs2_find_path() will return for
2320 * us the rightmost tree path. This is accounted for below in
2321 * the appending code.
2322 */
2323 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
2324 if (ret) {
2325 mlog_errno(ret);
2326 goto out;
2327 }
2328
2329 el = path_leaf_el(path);
2330
2331 /*
2332 * Now that we have the path, there's two things we want to determine:
2333 * 1) Contiguousness (also set contig_index if this is so)
2334 *
2335 * 2) Are we doing an append? We can trivially break this up
2336 * into two types of appends: simple record append, or a
2337 * rotate inside the tail leaf.
2338 */
2339 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
2340
2341 /*
2342 * The insert code isn't quite ready to deal with all cases of
2343 * left contiguousness. Specifically, if it's an insert into
2344 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08002345 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08002346 * left. For now, just catch that case and fool the layers
2347 * above us. This works just fine for tree_depth == 0, which
2348 * is why we allow that above.
2349 */
2350 if (insert->ins_contig == CONTIG_LEFT &&
2351 insert->ins_contig_index == 0)
2352 insert->ins_contig = CONTIG_NONE;
2353
2354 /*
2355 * Ok, so we can simply compare against last_eb to figure out
2356 * whether the path doesn't exist. This will only happen in
2357 * the case that we're doing a tail append, so maybe we can
2358 * take advantage of that information somehow.
2359 */
2360 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
2361 /*
2362 * Ok, ocfs2_find_path() returned us the rightmost
2363 * tree path. This might be an appending insert. There are
2364 * two cases:
2365 * 1) We're doing a true append at the tail:
2366 * -This might even be off the end of the leaf
2367 * 2) We're "appending" by rotating in the tail
2368 */
2369 ocfs2_figure_appending_type(insert, el, insert_rec);
2370 }
2371
2372out:
2373 ocfs2_free_path(path);
2374
2375 if (ret == 0)
2376 *last_eb_bh = bh;
2377 else
2378 brelse(bh);
2379 return ret;
2380}
2381
2382/*
2383 * Insert an extent into an inode btree.
2384 *
2385 * The caller needs to update fe->i_clusters
2386 */
Mark Fashehccd979b2005-12-15 14:31:24 -08002387int ocfs2_insert_extent(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07002388 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002389 struct inode *inode,
2390 struct buffer_head *fe_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08002391 u32 cpos,
Mark Fashehccd979b2005-12-15 14:31:24 -08002392 u64 start_blk,
2393 u32 new_clusters,
2394 struct ocfs2_alloc_context *meta_ac)
2395{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07002396 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08002397 struct buffer_head *last_eb_bh = NULL;
2398 struct buffer_head *bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08002399 struct ocfs2_insert_type insert = {0, };
2400 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08002401
Mark Fashehdcd05382007-01-16 11:32:23 -08002402 mlog(0, "add %u clusters at position %u to inode %llu\n",
2403 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002404
Mark Fashehdcd05382007-01-16 11:32:23 -08002405 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
2406 (OCFS2_I(inode)->ip_clusters != cpos),
2407 "Device %s, asking for sparse allocation: inode %llu, "
2408 "cpos %u, clusters %u\n",
2409 osb->dev_str,
2410 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
2411 OCFS2_I(inode)->ip_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08002412
Mark Fashehe48edee2007-03-07 16:46:57 -08002413 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08002414 rec.e_cpos = cpu_to_le32(cpos);
2415 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08002416 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08002417
Mark Fashehdcd05382007-01-16 11:32:23 -08002418 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
2419 &insert);
2420 if (status < 0) {
2421 mlog_errno(status);
2422 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002423 }
2424
Mark Fashehdcd05382007-01-16 11:32:23 -08002425 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
2426 "Insert.contig_index: %d, Insert.free_records: %d, "
2427 "Insert.tree_depth: %d\n",
2428 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
2429 insert.ins_free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08002430
Mark Fashehc3afcbb2007-05-29 14:28:51 -07002431 if (insert.ins_contig == CONTIG_NONE && insert.ins_free_records == 0) {
2432 status = ocfs2_grow_tree(inode, handle, fe_bh,
2433 &insert.ins_tree_depth, last_eb_bh,
2434 meta_ac);
2435 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002436 mlog_errno(status);
2437 goto bail;
2438 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002439 }
2440
Mark Fashehdcd05382007-01-16 11:32:23 -08002441 /* Finally, we can add clusters. This might rotate the tree for us. */
2442 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08002443 if (status < 0)
2444 mlog_errno(status);
Mark Fasheh83418972007-04-23 18:53:12 -07002445 else
2446 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08002447
2448bail:
2449 if (bh)
2450 brelse(bh);
2451
2452 if (last_eb_bh)
2453 brelse(last_eb_bh);
2454
2455 mlog_exit(status);
2456 return status;
2457}
2458
2459static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
2460{
2461 struct buffer_head *tl_bh = osb->osb_tl_bh;
2462 struct ocfs2_dinode *di;
2463 struct ocfs2_truncate_log *tl;
2464
2465 di = (struct ocfs2_dinode *) tl_bh->b_data;
2466 tl = &di->id2.i_dealloc;
2467
2468 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
2469 "slot %d, invalid truncate log parameters: used = "
2470 "%u, count = %u\n", osb->slot_num,
2471 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
2472 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
2473}
2474
2475static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
2476 unsigned int new_start)
2477{
2478 unsigned int tail_index;
2479 unsigned int current_tail;
2480
2481 /* No records, nothing to coalesce */
2482 if (!le16_to_cpu(tl->tl_used))
2483 return 0;
2484
2485 tail_index = le16_to_cpu(tl->tl_used) - 1;
2486 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
2487 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
2488
2489 return current_tail == new_start;
2490}
2491
2492static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07002493 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002494 u64 start_blk,
2495 unsigned int num_clusters)
2496{
2497 int status, index;
2498 unsigned int start_cluster, tl_count;
2499 struct inode *tl_inode = osb->osb_tl_inode;
2500 struct buffer_head *tl_bh = osb->osb_tl_bh;
2501 struct ocfs2_dinode *di;
2502 struct ocfs2_truncate_log *tl;
2503
Mark Fashehb06970532006-03-03 10:24:33 -08002504 mlog_entry("start_blk = %llu, num_clusters = %u\n",
2505 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08002506
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002507 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08002508
2509 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
2510
2511 di = (struct ocfs2_dinode *) tl_bh->b_data;
2512 tl = &di->id2.i_dealloc;
2513 if (!OCFS2_IS_VALID_DINODE(di)) {
2514 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2515 status = -EIO;
2516 goto bail;
2517 }
2518
2519 tl_count = le16_to_cpu(tl->tl_count);
2520 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
2521 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08002522 "Truncate record count on #%llu invalid "
2523 "wanted %u, actual %u\n",
2524 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002525 ocfs2_truncate_recs_per_inode(osb->sb),
2526 le16_to_cpu(tl->tl_count));
2527
2528 /* Caller should have known to flush before calling us. */
2529 index = le16_to_cpu(tl->tl_used);
2530 if (index >= tl_count) {
2531 status = -ENOSPC;
2532 mlog_errno(status);
2533 goto bail;
2534 }
2535
2536 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2537 OCFS2_JOURNAL_ACCESS_WRITE);
2538 if (status < 0) {
2539 mlog_errno(status);
2540 goto bail;
2541 }
2542
2543 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08002544 "%llu (index = %d)\n", num_clusters, start_cluster,
2545 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08002546
2547 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
2548 /*
2549 * Move index back to the record we are coalescing with.
2550 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
2551 */
2552 index--;
2553
2554 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
2555 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
2556 index, le32_to_cpu(tl->tl_recs[index].t_start),
2557 num_clusters);
2558 } else {
2559 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
2560 tl->tl_used = cpu_to_le16(index + 1);
2561 }
2562 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
2563
2564 status = ocfs2_journal_dirty(handle, tl_bh);
2565 if (status < 0) {
2566 mlog_errno(status);
2567 goto bail;
2568 }
2569
2570bail:
2571 mlog_exit(status);
2572 return status;
2573}
2574
2575static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07002576 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002577 struct inode *data_alloc_inode,
2578 struct buffer_head *data_alloc_bh)
2579{
2580 int status = 0;
2581 int i;
2582 unsigned int num_clusters;
2583 u64 start_blk;
2584 struct ocfs2_truncate_rec rec;
2585 struct ocfs2_dinode *di;
2586 struct ocfs2_truncate_log *tl;
2587 struct inode *tl_inode = osb->osb_tl_inode;
2588 struct buffer_head *tl_bh = osb->osb_tl_bh;
2589
2590 mlog_entry_void();
2591
2592 di = (struct ocfs2_dinode *) tl_bh->b_data;
2593 tl = &di->id2.i_dealloc;
2594 i = le16_to_cpu(tl->tl_used) - 1;
2595 while (i >= 0) {
2596 /* Caller has given us at least enough credits to
2597 * update the truncate log dinode */
2598 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
2599 OCFS2_JOURNAL_ACCESS_WRITE);
2600 if (status < 0) {
2601 mlog_errno(status);
2602 goto bail;
2603 }
2604
2605 tl->tl_used = cpu_to_le16(i);
2606
2607 status = ocfs2_journal_dirty(handle, tl_bh);
2608 if (status < 0) {
2609 mlog_errno(status);
2610 goto bail;
2611 }
2612
2613 /* TODO: Perhaps we can calculate the bulk of the
2614 * credits up front rather than extending like
2615 * this. */
2616 status = ocfs2_extend_trans(handle,
2617 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
2618 if (status < 0) {
2619 mlog_errno(status);
2620 goto bail;
2621 }
2622
2623 rec = tl->tl_recs[i];
2624 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
2625 le32_to_cpu(rec.t_start));
2626 num_clusters = le32_to_cpu(rec.t_clusters);
2627
2628 /* if start_blk is not set, we ignore the record as
2629 * invalid. */
2630 if (start_blk) {
2631 mlog(0, "free record %d, start = %u, clusters = %u\n",
2632 i, le32_to_cpu(rec.t_start), num_clusters);
2633
2634 status = ocfs2_free_clusters(handle, data_alloc_inode,
2635 data_alloc_bh, start_blk,
2636 num_clusters);
2637 if (status < 0) {
2638 mlog_errno(status);
2639 goto bail;
2640 }
2641 }
2642 i--;
2643 }
2644
2645bail:
2646 mlog_exit(status);
2647 return status;
2648}
2649
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002650/* Expects you to already be holding tl_inode->i_mutex */
Mark Fashehccd979b2005-12-15 14:31:24 -08002651static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2652{
2653 int status;
2654 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07002655 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08002656 struct inode *tl_inode = osb->osb_tl_inode;
2657 struct inode *data_alloc_inode = NULL;
2658 struct buffer_head *tl_bh = osb->osb_tl_bh;
2659 struct buffer_head *data_alloc_bh = NULL;
2660 struct ocfs2_dinode *di;
2661 struct ocfs2_truncate_log *tl;
2662
2663 mlog_entry_void();
2664
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002665 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08002666
2667 di = (struct ocfs2_dinode *) tl_bh->b_data;
2668 tl = &di->id2.i_dealloc;
2669 if (!OCFS2_IS_VALID_DINODE(di)) {
2670 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
2671 status = -EIO;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002672 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08002673 }
2674
2675 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08002676 mlog(0, "Flush %u records from truncate log #%llu\n",
2677 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002678 if (!num_to_flush) {
2679 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002680 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08002681 }
2682
2683 data_alloc_inode = ocfs2_get_system_file_inode(osb,
2684 GLOBAL_BITMAP_SYSTEM_INODE,
2685 OCFS2_INVALID_SLOT);
2686 if (!data_alloc_inode) {
2687 status = -EINVAL;
2688 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002689 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08002690 }
2691
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002692 mutex_lock(&data_alloc_inode->i_mutex);
2693
Mark Fasheh4bcec182006-10-09 16:02:40 -07002694 status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002695 if (status < 0) {
2696 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002697 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08002698 }
2699
Mark Fasheh65eff9c2006-10-09 17:26:22 -07002700 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002701 if (IS_ERR(handle)) {
2702 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08002703 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002704 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08002705 }
2706
2707 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
2708 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002709 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08002710 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002711
Mark Fasheh02dc1af2006-10-09 16:48:10 -07002712 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08002713
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002714out_unlock:
2715 brelse(data_alloc_bh);
2716 ocfs2_meta_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002717
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002718out_mutex:
2719 mutex_unlock(&data_alloc_inode->i_mutex);
2720 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002721
Mark Fashehe08dc8b2006-10-05 15:58:48 -07002722out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002723 mlog_exit(status);
2724 return status;
2725}
2726
2727int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
2728{
2729 int status;
2730 struct inode *tl_inode = osb->osb_tl_inode;
2731
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002732 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002733 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002734 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002735
2736 return status;
2737}
2738
David Howellsc4028952006-11-22 14:57:56 +00002739static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08002740{
2741 int status;
David Howellsc4028952006-11-22 14:57:56 +00002742 struct ocfs2_super *osb =
2743 container_of(work, struct ocfs2_super,
2744 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08002745
2746 mlog_entry_void();
2747
2748 status = ocfs2_flush_truncate_log(osb);
2749 if (status < 0)
2750 mlog_errno(status);
2751
2752 mlog_exit(status);
2753}
2754
2755#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
2756void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
2757 int cancel)
2758{
2759 if (osb->osb_tl_inode) {
2760 /* We want to push off log flushes while truncates are
2761 * still running. */
2762 if (cancel)
2763 cancel_delayed_work(&osb->osb_truncate_log_wq);
2764
2765 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
2766 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
2767 }
2768}
2769
2770static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
2771 int slot_num,
2772 struct inode **tl_inode,
2773 struct buffer_head **tl_bh)
2774{
2775 int status;
2776 struct inode *inode = NULL;
2777 struct buffer_head *bh = NULL;
2778
2779 inode = ocfs2_get_system_file_inode(osb,
2780 TRUNCATE_LOG_SYSTEM_INODE,
2781 slot_num);
2782 if (!inode) {
2783 status = -EINVAL;
2784 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
2785 goto bail;
2786 }
2787
2788 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
2789 OCFS2_BH_CACHED, inode);
2790 if (status < 0) {
2791 iput(inode);
2792 mlog_errno(status);
2793 goto bail;
2794 }
2795
2796 *tl_inode = inode;
2797 *tl_bh = bh;
2798bail:
2799 mlog_exit(status);
2800 return status;
2801}
2802
2803/* called during the 1st stage of node recovery. we stamp a clean
2804 * truncate log and pass back a copy for processing later. if the
2805 * truncate log does not require processing, a *tl_copy is set to
2806 * NULL. */
2807int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
2808 int slot_num,
2809 struct ocfs2_dinode **tl_copy)
2810{
2811 int status;
2812 struct inode *tl_inode = NULL;
2813 struct buffer_head *tl_bh = NULL;
2814 struct ocfs2_dinode *di;
2815 struct ocfs2_truncate_log *tl;
2816
2817 *tl_copy = NULL;
2818
2819 mlog(0, "recover truncate log from slot %d\n", slot_num);
2820
2821 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
2822 if (status < 0) {
2823 mlog_errno(status);
2824 goto bail;
2825 }
2826
2827 di = (struct ocfs2_dinode *) tl_bh->b_data;
2828 tl = &di->id2.i_dealloc;
2829 if (!OCFS2_IS_VALID_DINODE(di)) {
2830 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
2831 status = -EIO;
2832 goto bail;
2833 }
2834
2835 if (le16_to_cpu(tl->tl_used)) {
2836 mlog(0, "We'll have %u logs to recover\n",
2837 le16_to_cpu(tl->tl_used));
2838
2839 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
2840 if (!(*tl_copy)) {
2841 status = -ENOMEM;
2842 mlog_errno(status);
2843 goto bail;
2844 }
2845
2846 /* Assuming the write-out below goes well, this copy
2847 * will be passed back to recovery for processing. */
2848 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
2849
2850 /* All we need to do to clear the truncate log is set
2851 * tl_used. */
2852 tl->tl_used = 0;
2853
2854 status = ocfs2_write_block(osb, tl_bh, tl_inode);
2855 if (status < 0) {
2856 mlog_errno(status);
2857 goto bail;
2858 }
2859 }
2860
2861bail:
2862 if (tl_inode)
2863 iput(tl_inode);
2864 if (tl_bh)
2865 brelse(tl_bh);
2866
2867 if (status < 0 && (*tl_copy)) {
2868 kfree(*tl_copy);
2869 *tl_copy = NULL;
2870 }
2871
2872 mlog_exit(status);
2873 return status;
2874}
2875
2876int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
2877 struct ocfs2_dinode *tl_copy)
2878{
2879 int status = 0;
2880 int i;
2881 unsigned int clusters, num_recs, start_cluster;
2882 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07002883 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08002884 struct inode *tl_inode = osb->osb_tl_inode;
2885 struct ocfs2_truncate_log *tl;
2886
2887 mlog_entry_void();
2888
2889 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
2890 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
2891 return -EINVAL;
2892 }
2893
2894 tl = &tl_copy->id2.i_dealloc;
2895 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08002896 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07002897 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08002898
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002899 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002900 for(i = 0; i < num_recs; i++) {
2901 if (ocfs2_truncate_log_needs_flush(osb)) {
2902 status = __ocfs2_flush_truncate_log(osb);
2903 if (status < 0) {
2904 mlog_errno(status);
2905 goto bail_up;
2906 }
2907 }
2908
Mark Fasheh65eff9c2006-10-09 17:26:22 -07002909 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002910 if (IS_ERR(handle)) {
2911 status = PTR_ERR(handle);
2912 mlog_errno(status);
2913 goto bail_up;
2914 }
2915
2916 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
2917 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
2918 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
2919
2920 status = ocfs2_truncate_log_append(osb, handle,
2921 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07002922 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08002923 if (status < 0) {
2924 mlog_errno(status);
2925 goto bail_up;
2926 }
2927 }
2928
2929bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002930 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002931
2932 mlog_exit(status);
2933 return status;
2934}
2935
2936void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
2937{
2938 int status;
2939 struct inode *tl_inode = osb->osb_tl_inode;
2940
2941 mlog_entry_void();
2942
2943 if (tl_inode) {
2944 cancel_delayed_work(&osb->osb_truncate_log_wq);
2945 flush_workqueue(ocfs2_wq);
2946
2947 status = ocfs2_flush_truncate_log(osb);
2948 if (status < 0)
2949 mlog_errno(status);
2950
2951 brelse(osb->osb_tl_bh);
2952 iput(osb->osb_tl_inode);
2953 }
2954
2955 mlog_exit_void();
2956}
2957
2958int ocfs2_truncate_log_init(struct ocfs2_super *osb)
2959{
2960 int status;
2961 struct inode *tl_inode = NULL;
2962 struct buffer_head *tl_bh = NULL;
2963
2964 mlog_entry_void();
2965
2966 status = ocfs2_get_truncate_log_info(osb,
2967 osb->slot_num,
2968 &tl_inode,
2969 &tl_bh);
2970 if (status < 0)
2971 mlog_errno(status);
2972
2973 /* ocfs2_truncate_log_shutdown keys on the existence of
2974 * osb->osb_tl_inode so we don't set any of the osb variables
2975 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00002976 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
2977 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08002978 osb->osb_tl_bh = tl_bh;
2979 osb->osb_tl_inode = tl_inode;
2980
2981 mlog_exit(status);
2982 return status;
2983}
2984
Mark Fasheh2b604352007-06-22 15:45:27 -07002985/*
2986 * Delayed de-allocation of suballocator blocks.
2987 *
2988 * Some sets of block de-allocations might involve multiple suballocator inodes.
2989 *
2990 * The locking for this can get extremely complicated, especially when
2991 * the suballocator inodes to delete from aren't known until deep
2992 * within an unrelated codepath.
2993 *
2994 * ocfs2_extent_block structures are a good example of this - an inode
2995 * btree could have been grown by any number of nodes each allocating
2996 * out of their own suballoc inode.
2997 *
2998 * These structures allow the delay of block de-allocation until a
2999 * later time, when locking of multiple cluster inodes won't cause
3000 * deadlock.
3001 */
3002
3003/*
3004 * Describes a single block free from a suballocator
3005 */
3006struct ocfs2_cached_block_free {
3007 struct ocfs2_cached_block_free *free_next;
3008 u64 free_blk;
3009 unsigned int free_bit;
3010};
3011
3012struct ocfs2_per_slot_free_list {
3013 struct ocfs2_per_slot_free_list *f_next_suballocator;
3014 int f_inode_type;
3015 int f_slot;
3016 struct ocfs2_cached_block_free *f_first;
3017};
3018
3019static int ocfs2_free_cached_items(struct ocfs2_super *osb,
3020 int sysfile_type,
3021 int slot,
3022 struct ocfs2_cached_block_free *head)
3023{
3024 int ret;
3025 u64 bg_blkno;
3026 handle_t *handle;
3027 struct inode *inode;
3028 struct buffer_head *di_bh = NULL;
3029 struct ocfs2_cached_block_free *tmp;
3030
3031 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
3032 if (!inode) {
3033 ret = -EINVAL;
3034 mlog_errno(ret);
3035 goto out;
3036 }
3037
3038 mutex_lock(&inode->i_mutex);
3039
3040 ret = ocfs2_meta_lock(inode, &di_bh, 1);
3041 if (ret) {
3042 mlog_errno(ret);
3043 goto out_mutex;
3044 }
3045
3046 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
3047 if (IS_ERR(handle)) {
3048 ret = PTR_ERR(handle);
3049 mlog_errno(ret);
3050 goto out_unlock;
3051 }
3052
3053 while (head) {
3054 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
3055 head->free_bit);
3056 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
3057 head->free_bit, (unsigned long long)head->free_blk);
3058
3059 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
3060 head->free_bit, bg_blkno, 1);
3061 if (ret) {
3062 mlog_errno(ret);
3063 goto out_journal;
3064 }
3065
3066 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
3067 if (ret) {
3068 mlog_errno(ret);
3069 goto out_journal;
3070 }
3071
3072 tmp = head;
3073 head = head->free_next;
3074 kfree(tmp);
3075 }
3076
3077out_journal:
3078 ocfs2_commit_trans(osb, handle);
3079
3080out_unlock:
3081 ocfs2_meta_unlock(inode, 1);
3082 brelse(di_bh);
3083out_mutex:
3084 mutex_unlock(&inode->i_mutex);
3085 iput(inode);
3086out:
3087 while(head) {
3088 /* Premature exit may have left some dangling items. */
3089 tmp = head;
3090 head = head->free_next;
3091 kfree(tmp);
3092 }
3093
3094 return ret;
3095}
3096
3097int ocfs2_run_deallocs(struct ocfs2_super *osb,
3098 struct ocfs2_cached_dealloc_ctxt *ctxt)
3099{
3100 int ret = 0, ret2;
3101 struct ocfs2_per_slot_free_list *fl;
3102
3103 if (!ctxt)
3104 return 0;
3105
3106 while (ctxt->c_first_suballocator) {
3107 fl = ctxt->c_first_suballocator;
3108
3109 if (fl->f_first) {
3110 mlog(0, "Free items: (type %u, slot %d)\n",
3111 fl->f_inode_type, fl->f_slot);
3112 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
3113 fl->f_slot, fl->f_first);
3114 if (ret2)
3115 mlog_errno(ret2);
3116 if (!ret)
3117 ret = ret2;
3118 }
3119
3120 ctxt->c_first_suballocator = fl->f_next_suballocator;
3121 kfree(fl);
3122 }
3123
3124 return ret;
3125}
3126
3127static struct ocfs2_per_slot_free_list *
3128ocfs2_find_per_slot_free_list(int type,
3129 int slot,
3130 struct ocfs2_cached_dealloc_ctxt *ctxt)
3131{
3132 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
3133
3134 while (fl) {
3135 if (fl->f_inode_type == type && fl->f_slot == slot)
3136 return fl;
3137
3138 fl = fl->f_next_suballocator;
3139 }
3140
3141 fl = kmalloc(sizeof(*fl), GFP_NOFS);
3142 if (fl) {
3143 fl->f_inode_type = type;
3144 fl->f_slot = slot;
3145 fl->f_first = NULL;
3146 fl->f_next_suballocator = ctxt->c_first_suballocator;
3147
3148 ctxt->c_first_suballocator = fl;
3149 }
3150 return fl;
3151}
3152
3153static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
3154 int type, int slot, u64 blkno,
3155 unsigned int bit)
3156{
3157 int ret;
3158 struct ocfs2_per_slot_free_list *fl;
3159 struct ocfs2_cached_block_free *item;
3160
3161 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
3162 if (fl == NULL) {
3163 ret = -ENOMEM;
3164 mlog_errno(ret);
3165 goto out;
3166 }
3167
3168 item = kmalloc(sizeof(*item), GFP_NOFS);
3169 if (item == NULL) {
3170 ret = -ENOMEM;
3171 mlog_errno(ret);
3172 goto out;
3173 }
3174
3175 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
3176 type, slot, bit, (unsigned long long)blkno);
3177
3178 item->free_blk = blkno;
3179 item->free_bit = bit;
3180 item->free_next = fl->f_first;
3181
3182 fl->f_first = item;
3183
3184 ret = 0;
3185out:
3186 return ret;
3187}
3188
Mark Fasheh59a5e412007-06-22 15:52:36 -07003189static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
3190 struct ocfs2_extent_block *eb)
3191{
3192 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
3193 le16_to_cpu(eb->h_suballoc_slot),
3194 le64_to_cpu(eb->h_blkno),
3195 le16_to_cpu(eb->h_suballoc_bit));
3196}
3197
Mark Fashehccd979b2005-12-15 14:31:24 -08003198/* This function will figure out whether the currently last extent
3199 * block will be deleted, and if it will, what the new last extent
3200 * block will be so we can update his h_next_leaf_blk field, as well
3201 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08003202static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003203 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08003204 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08003205 struct buffer_head **new_last_eb)
3206{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003207 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08003208 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003209 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08003210 struct ocfs2_extent_block *eb;
3211 struct ocfs2_extent_list *el;
3212 struct buffer_head *bh = NULL;
3213
3214 *new_last_eb = NULL;
3215
Mark Fashehccd979b2005-12-15 14:31:24 -08003216 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08003217 if (!path->p_tree_depth)
3218 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08003219
3220 /* trunc to zero special case - this makes tree_depth = 0
3221 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003222 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08003223 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08003224
Mark Fashehdcd05382007-01-16 11:32:23 -08003225 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08003226 BUG_ON(!el->l_next_free_rec);
3227
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003228 /*
3229 * Make sure that this extent list will actually be empty
3230 * after we clear away the data. We can shortcut out if
3231 * there's more than one non-empty extent in the
3232 * list. Otherwise, a check of the remaining extent is
3233 * necessary.
3234 */
3235 next_free = le16_to_cpu(el->l_next_free_rec);
3236 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08003237 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003238 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08003239 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003240
3241 /* We may have a valid extent in index 1, check it. */
3242 if (next_free == 2)
3243 rec = &el->l_recs[1];
3244
3245 /*
3246 * Fall through - no more nonempty extents, so we want
3247 * to delete this leaf.
3248 */
3249 } else {
3250 if (next_free > 1)
3251 goto out;
3252
3253 rec = &el->l_recs[0];
3254 }
3255
3256 if (rec) {
3257 /*
3258 * Check it we'll only be trimming off the end of this
3259 * cluster.
3260 */
Mark Fashehe48edee2007-03-07 16:46:57 -08003261 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003262 goto out;
3263 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003264
Mark Fashehdcd05382007-01-16 11:32:23 -08003265 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
3266 if (ret) {
3267 mlog_errno(ret);
3268 goto out;
3269 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003270
Mark Fashehdcd05382007-01-16 11:32:23 -08003271 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
3272 if (ret) {
3273 mlog_errno(ret);
3274 goto out;
3275 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003276
Mark Fashehdcd05382007-01-16 11:32:23 -08003277 eb = (struct ocfs2_extent_block *) bh->b_data;
3278 el = &eb->h_list;
3279 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
3280 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
3281 ret = -EROFS;
3282 goto out;
3283 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003284
3285 *new_last_eb = bh;
3286 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08003287 mlog(0, "returning block %llu, (cpos: %u)\n",
3288 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
3289out:
3290 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003291
Mark Fashehdcd05382007-01-16 11:32:23 -08003292 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003293}
3294
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003295/*
3296 * Trim some clusters off the rightmost edge of a tree. Only called
3297 * during truncate.
3298 *
3299 * The caller needs to:
3300 * - start journaling of each path component.
3301 * - compute and fully set up any new last ext block
3302 */
3303static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
3304 handle_t *handle, struct ocfs2_truncate_context *tc,
3305 u32 clusters_to_del, u64 *delete_start)
3306{
3307 int ret, i, index = path->p_tree_depth;
3308 u32 new_edge = 0;
3309 u64 deleted_eb = 0;
3310 struct buffer_head *bh;
3311 struct ocfs2_extent_list *el;
3312 struct ocfs2_extent_rec *rec;
3313
3314 *delete_start = 0;
3315
3316 while (index >= 0) {
3317 bh = path->p_node[index].bh;
3318 el = path->p_node[index].el;
3319
3320 mlog(0, "traveling tree (index = %d, block = %llu)\n",
3321 index, (unsigned long long)bh->b_blocknr);
3322
3323 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
3324
3325 if (index !=
3326 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
3327 ocfs2_error(inode->i_sb,
3328 "Inode %lu has invalid ext. block %llu",
3329 inode->i_ino,
3330 (unsigned long long)bh->b_blocknr);
3331 ret = -EROFS;
3332 goto out;
3333 }
3334
3335find_tail_record:
3336 i = le16_to_cpu(el->l_next_free_rec) - 1;
3337 rec = &el->l_recs[i];
3338
3339 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
3340 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003341 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003342 (unsigned long long)le64_to_cpu(rec->e_blkno),
3343 le16_to_cpu(el->l_next_free_rec));
3344
Mark Fashehe48edee2007-03-07 16:46:57 -08003345 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003346
3347 if (le16_to_cpu(el->l_tree_depth) == 0) {
3348 /*
3349 * If the leaf block contains a single empty
3350 * extent and no records, we can just remove
3351 * the block.
3352 */
3353 if (i == 0 && ocfs2_is_empty_extent(rec)) {
3354 memset(rec, 0,
3355 sizeof(struct ocfs2_extent_rec));
3356 el->l_next_free_rec = cpu_to_le16(0);
3357
3358 goto delete;
3359 }
3360
3361 /*
3362 * Remove any empty extents by shifting things
3363 * left. That should make life much easier on
3364 * the code below. This condition is rare
3365 * enough that we shouldn't see a performance
3366 * hit.
3367 */
3368 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3369 le16_add_cpu(&el->l_next_free_rec, -1);
3370
3371 for(i = 0;
3372 i < le16_to_cpu(el->l_next_free_rec); i++)
3373 el->l_recs[i] = el->l_recs[i + 1];
3374
3375 memset(&el->l_recs[i], 0,
3376 sizeof(struct ocfs2_extent_rec));
3377
3378 /*
3379 * We've modified our extent list. The
3380 * simplest way to handle this change
3381 * is to being the search from the
3382 * start again.
3383 */
3384 goto find_tail_record;
3385 }
3386
Mark Fashehe48edee2007-03-07 16:46:57 -08003387 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003388
3389 /*
3390 * We'll use "new_edge" on our way back up the
3391 * tree to know what our rightmost cpos is.
3392 */
Mark Fashehe48edee2007-03-07 16:46:57 -08003393 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003394 new_edge += le32_to_cpu(rec->e_cpos);
3395
3396 /*
3397 * The caller will use this to delete data blocks.
3398 */
3399 *delete_start = le64_to_cpu(rec->e_blkno)
3400 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08003401 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003402
3403 /*
3404 * If it's now empty, remove this record.
3405 */
Mark Fashehe48edee2007-03-07 16:46:57 -08003406 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003407 memset(rec, 0,
3408 sizeof(struct ocfs2_extent_rec));
3409 le16_add_cpu(&el->l_next_free_rec, -1);
3410 }
3411 } else {
3412 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
3413 memset(rec, 0,
3414 sizeof(struct ocfs2_extent_rec));
3415 le16_add_cpu(&el->l_next_free_rec, -1);
3416
3417 goto delete;
3418 }
3419
3420 /* Can this actually happen? */
3421 if (le16_to_cpu(el->l_next_free_rec) == 0)
3422 goto delete;
3423
3424 /*
3425 * We never actually deleted any clusters
3426 * because our leaf was empty. There's no
3427 * reason to adjust the rightmost edge then.
3428 */
3429 if (new_edge == 0)
3430 goto delete;
3431
Mark Fashehe48edee2007-03-07 16:46:57 -08003432 rec->e_int_clusters = cpu_to_le32(new_edge);
3433 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003434 -le32_to_cpu(rec->e_cpos));
3435
3436 /*
3437 * A deleted child record should have been
3438 * caught above.
3439 */
Mark Fashehe48edee2007-03-07 16:46:57 -08003440 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003441 }
3442
3443delete:
3444 ret = ocfs2_journal_dirty(handle, bh);
3445 if (ret) {
3446 mlog_errno(ret);
3447 goto out;
3448 }
3449
3450 mlog(0, "extent list container %llu, after: record %d: "
3451 "(%u, %u, %llu), next = %u.\n",
3452 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08003453 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003454 (unsigned long long)le64_to_cpu(rec->e_blkno),
3455 le16_to_cpu(el->l_next_free_rec));
3456
3457 /*
3458 * We must be careful to only attempt delete of an
3459 * extent block (and not the root inode block).
3460 */
3461 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
3462 struct ocfs2_extent_block *eb =
3463 (struct ocfs2_extent_block *)bh->b_data;
3464
3465 /*
3466 * Save this for use when processing the
3467 * parent block.
3468 */
3469 deleted_eb = le64_to_cpu(eb->h_blkno);
3470
3471 mlog(0, "deleting this extent block.\n");
3472
3473 ocfs2_remove_from_cache(inode, bh);
3474
Mark Fashehe48edee2007-03-07 16:46:57 -08003475 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003476 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
3477 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
3478
Mark Fasheh59a5e412007-06-22 15:52:36 -07003479 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
3480 /* An error here is not fatal. */
3481 if (ret < 0)
3482 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003483 } else {
3484 deleted_eb = 0;
3485 }
3486
3487 index--;
3488 }
3489
3490 ret = 0;
3491out:
3492 return ret;
3493}
3494
Mark Fashehccd979b2005-12-15 14:31:24 -08003495static int ocfs2_do_truncate(struct ocfs2_super *osb,
3496 unsigned int clusters_to_del,
3497 struct inode *inode,
3498 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07003499 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08003500 struct ocfs2_truncate_context *tc,
3501 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08003502{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003503 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08003504 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08003505 struct ocfs2_extent_block *last_eb = NULL;
3506 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08003507 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003508 u64 delete_blk = 0;
3509
3510 fe = (struct ocfs2_dinode *) fe_bh->b_data;
3511
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003512 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08003513 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003514 if (status < 0) {
3515 mlog_errno(status);
3516 goto bail;
3517 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003518
Mark Fashehdcd05382007-01-16 11:32:23 -08003519 /*
3520 * Each component will be touched, so we might as well journal
3521 * here to avoid having to handle errors later.
3522 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003523 status = ocfs2_journal_access_path(inode, handle, path);
3524 if (status < 0) {
3525 mlog_errno(status);
3526 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08003527 }
3528
3529 if (last_eb_bh) {
3530 status = ocfs2_journal_access(handle, inode, last_eb_bh,
3531 OCFS2_JOURNAL_ACCESS_WRITE);
3532 if (status < 0) {
3533 mlog_errno(status);
3534 goto bail;
3535 }
3536
3537 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
3538 }
3539
3540 el = &(fe->id2.i_list);
3541
3542 /*
3543 * Lower levels depend on this never happening, but it's best
3544 * to check it up here before changing the tree.
3545 */
Mark Fashehe48edee2007-03-07 16:46:57 -08003546 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08003547 ocfs2_error(inode->i_sb,
3548 "Inode %lu has an empty extent record, depth %u\n",
3549 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003550 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08003551 goto bail;
3552 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003553
3554 spin_lock(&OCFS2_I(inode)->ip_lock);
3555 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
3556 clusters_to_del;
3557 spin_unlock(&OCFS2_I(inode)->ip_lock);
3558 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08003559
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003560 status = ocfs2_trim_tree(inode, path, handle, tc,
3561 clusters_to_del, &delete_blk);
3562 if (status) {
3563 mlog_errno(status);
3564 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08003565 }
3566
Mark Fashehdcd05382007-01-16 11:32:23 -08003567 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003568 /* trunc to zero is a special case. */
3569 el->l_tree_depth = 0;
3570 fe->i_last_eb_blk = 0;
3571 } else if (last_eb)
3572 fe->i_last_eb_blk = last_eb->h_blkno;
3573
3574 status = ocfs2_journal_dirty(handle, fe_bh);
3575 if (status < 0) {
3576 mlog_errno(status);
3577 goto bail;
3578 }
3579
3580 if (last_eb) {
3581 /* If there will be a new last extent block, then by
3582 * definition, there cannot be any leaves to the right of
3583 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08003584 last_eb->h_next_leaf_blk = 0;
3585 status = ocfs2_journal_dirty(handle, last_eb_bh);
3586 if (status < 0) {
3587 mlog_errno(status);
3588 goto bail;
3589 }
3590 }
3591
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003592 if (delete_blk) {
3593 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
3594 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08003595 if (status < 0) {
3596 mlog_errno(status);
3597 goto bail;
3598 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003599 }
3600 status = 0;
3601bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08003602
Mark Fashehccd979b2005-12-15 14:31:24 -08003603 mlog_exit(status);
3604 return status;
3605}
3606
Mark Fasheh60b11392007-02-16 11:46:50 -08003607static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
3608{
3609 set_buffer_uptodate(bh);
3610 mark_buffer_dirty(bh);
3611 return 0;
3612}
3613
3614static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
3615{
3616 set_buffer_uptodate(bh);
3617 mark_buffer_dirty(bh);
3618 return ocfs2_journal_dirty_data(handle, bh);
3619}
3620
3621static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t isize,
3622 struct page **pages, int numpages,
3623 u64 phys, handle_t *handle)
3624{
3625 int i, ret, partial = 0;
3626 void *kaddr;
3627 struct page *page;
3628 unsigned int from, to = PAGE_CACHE_SIZE;
3629 struct super_block *sb = inode->i_sb;
3630
3631 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3632
3633 if (numpages == 0)
3634 goto out;
3635
3636 from = isize & (PAGE_CACHE_SIZE - 1); /* 1st page offset */
3637 if (PAGE_CACHE_SHIFT > OCFS2_SB(sb)->s_clustersize_bits) {
3638 /*
3639 * Since 'from' has been capped to a value below page
3640 * size, this calculation won't be able to overflow
3641 * 'to'
3642 */
3643 to = ocfs2_align_bytes_to_clusters(sb, from);
3644
3645 /*
3646 * The truncate tail in this case should never contain
3647 * more than one page at maximum. The loop below also
3648 * assumes this.
3649 */
3650 BUG_ON(numpages != 1);
3651 }
3652
3653 for(i = 0; i < numpages; i++) {
3654 page = pages[i];
3655
3656 BUG_ON(from > PAGE_CACHE_SIZE);
3657 BUG_ON(to > PAGE_CACHE_SIZE);
3658
3659 ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
3660 if (ret)
3661 mlog_errno(ret);
3662
3663 kaddr = kmap_atomic(page, KM_USER0);
3664 memset(kaddr + from, 0, to - from);
3665 kunmap_atomic(kaddr, KM_USER0);
3666
3667 /*
3668 * Need to set the buffers we zero'd into uptodate
3669 * here if they aren't - ocfs2_map_page_blocks()
3670 * might've skipped some
3671 */
3672 if (ocfs2_should_order_data(inode)) {
3673 ret = walk_page_buffers(handle,
3674 page_buffers(page),
3675 from, to, &partial,
3676 ocfs2_ordered_zero_func);
3677 if (ret < 0)
3678 mlog_errno(ret);
3679 } else {
3680 ret = walk_page_buffers(handle, page_buffers(page),
3681 from, to, &partial,
3682 ocfs2_writeback_zero_func);
3683 if (ret < 0)
3684 mlog_errno(ret);
3685 }
3686
3687 if (!partial)
3688 SetPageUptodate(page);
3689
3690 flush_dcache_page(page);
3691
3692 /*
3693 * Every page after the 1st one should be completely zero'd.
3694 */
3695 from = 0;
3696 }
3697out:
3698 if (pages) {
3699 for (i = 0; i < numpages; i++) {
3700 page = pages[i];
3701 unlock_page(page);
3702 mark_page_accessed(page);
3703 page_cache_release(page);
3704 }
3705 }
3706}
3707
3708static int ocfs2_grab_eof_pages(struct inode *inode, loff_t isize, struct page **pages,
3709 int *num, u64 *phys)
3710{
3711 int i, numpages = 0, ret = 0;
3712 unsigned int csize = OCFS2_SB(inode->i_sb)->s_clustersize;
Mark Fasheh49cb8d22007-03-09 16:21:46 -08003713 unsigned int ext_flags;
Mark Fasheh60b11392007-02-16 11:46:50 -08003714 struct super_block *sb = inode->i_sb;
3715 struct address_space *mapping = inode->i_mapping;
3716 unsigned long index;
3717 u64 next_cluster_bytes;
3718
3719 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
3720
3721 /* Cluster boundary, so we don't need to grab any pages. */
3722 if ((isize & (csize - 1)) == 0)
3723 goto out;
3724
3725 ret = ocfs2_extent_map_get_blocks(inode, isize >> sb->s_blocksize_bits,
Mark Fasheh49cb8d22007-03-09 16:21:46 -08003726 phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08003727 if (ret) {
3728 mlog_errno(ret);
3729 goto out;
3730 }
3731
3732 /* Tail is a hole. */
3733 if (*phys == 0)
3734 goto out;
3735
Mark Fasheh49cb8d22007-03-09 16:21:46 -08003736 /* Tail is marked as unwritten, we can count on write to zero
3737 * in that case. */
3738 if (ext_flags & OCFS2_EXT_UNWRITTEN)
3739 goto out;
3740
Mark Fasheh60b11392007-02-16 11:46:50 -08003741 next_cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, isize);
3742 index = isize >> PAGE_CACHE_SHIFT;
3743 do {
3744 pages[numpages] = grab_cache_page(mapping, index);
3745 if (!pages[numpages]) {
3746 ret = -ENOMEM;
3747 mlog_errno(ret);
3748 goto out;
3749 }
3750
3751 numpages++;
3752 index++;
3753 } while (index < (next_cluster_bytes >> PAGE_CACHE_SHIFT));
3754
3755out:
3756 if (ret != 0) {
3757 if (pages) {
3758 for (i = 0; i < numpages; i++) {
3759 if (pages[i]) {
3760 unlock_page(pages[i]);
3761 page_cache_release(pages[i]);
3762 }
3763 }
3764 }
3765 numpages = 0;
3766 }
3767
3768 *num = numpages;
3769
3770 return ret;
3771}
3772
3773/*
3774 * Zero the area past i_size but still within an allocated
3775 * cluster. This avoids exposing nonzero data on subsequent file
3776 * extends.
3777 *
3778 * We need to call this before i_size is updated on the inode because
3779 * otherwise block_write_full_page() will skip writeout of pages past
3780 * i_size. The new_i_size parameter is passed for this reason.
3781 */
3782int ocfs2_zero_tail_for_truncate(struct inode *inode, handle_t *handle,
3783 u64 new_i_size)
3784{
3785 int ret, numpages;
Mark Fashehfa410452007-03-01 11:22:19 -08003786 loff_t endbyte;
Mark Fasheh60b11392007-02-16 11:46:50 -08003787 struct page **pages = NULL;
3788 u64 phys;
3789
3790 /*
3791 * File systems which don't support sparse files zero on every
3792 * extend.
3793 */
3794 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
3795 return 0;
3796
3797 pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
3798 sizeof(struct page *), GFP_NOFS);
3799 if (pages == NULL) {
3800 ret = -ENOMEM;
3801 mlog_errno(ret);
3802 goto out;
3803 }
3804
3805 ret = ocfs2_grab_eof_pages(inode, new_i_size, pages, &numpages, &phys);
3806 if (ret) {
3807 mlog_errno(ret);
3808 goto out;
3809 }
3810
Mark Fasheh60b11392007-02-16 11:46:50 -08003811 if (numpages == 0)
3812 goto out;
3813
3814 ocfs2_zero_cluster_pages(inode, new_i_size, pages, numpages, phys,
3815 handle);
3816
3817 /*
3818 * Initiate writeout of the pages we zero'd here. We don't
3819 * wait on them - the truncate_inode_pages() call later will
3820 * do that for us.
3821 */
Mark Fashehfa410452007-03-01 11:22:19 -08003822 endbyte = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
3823 ret = do_sync_mapping_range(inode->i_mapping, new_i_size,
3824 endbyte - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08003825 if (ret)
3826 mlog_errno(ret);
3827
3828out:
3829 if (pages)
3830 kfree(pages);
3831
3832 return ret;
3833}
3834
Mark Fashehccd979b2005-12-15 14:31:24 -08003835/*
3836 * It is expected, that by the time you call this function,
3837 * inode->i_size and fe->i_size have been adjusted.
3838 *
3839 * WARNING: This will kfree the truncate context
3840 */
3841int ocfs2_commit_truncate(struct ocfs2_super *osb,
3842 struct inode *inode,
3843 struct buffer_head *fe_bh,
3844 struct ocfs2_truncate_context *tc)
3845{
3846 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08003847 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08003848 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07003849 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003850 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08003851 struct ocfs2_path *path = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003852
3853 mlog_entry_void();
3854
Mark Fashehdcd05382007-01-16 11:32:23 -08003855 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08003856 i_size_read(inode));
3857
Mark Fashehdcd05382007-01-16 11:32:23 -08003858 path = ocfs2_new_inode_path(fe_bh);
3859 if (!path) {
3860 status = -ENOMEM;
3861 mlog_errno(status);
3862 goto bail;
3863 }
Mark Fasheh83418972007-04-23 18:53:12 -07003864
3865 ocfs2_extent_map_trunc(inode, new_highest_cpos);
3866
Mark Fashehccd979b2005-12-15 14:31:24 -08003867start:
Mark Fashehdcd05382007-01-16 11:32:23 -08003868 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003869 * Check that we still have allocation to delete.
3870 */
3871 if (OCFS2_I(inode)->ip_clusters == 0) {
3872 status = 0;
3873 goto bail;
3874 }
3875
3876 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003877 * Truncate always works against the rightmost tree branch.
3878 */
3879 status = ocfs2_find_path(inode, path, UINT_MAX);
3880 if (status) {
3881 mlog_errno(status);
3882 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08003883 }
3884
Mark Fashehdcd05382007-01-16 11:32:23 -08003885 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
3886 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
3887
3888 /*
3889 * By now, el will point to the extent list on the bottom most
3890 * portion of this tree. Only the tail record is considered in
3891 * each pass.
3892 *
3893 * We handle the following cases, in order:
3894 * - empty extent: delete the remaining branch
3895 * - remove the entire record
3896 * - remove a partial record
3897 * - no record needs to be removed (truncate has completed)
3898 */
3899 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003900 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3901 ocfs2_error(inode->i_sb,
3902 "Inode %llu has empty extent block at %llu\n",
3903 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3904 (unsigned long long)path_leaf_bh(path)->b_blocknr);
3905 status = -EROFS;
3906 goto bail;
3907 }
3908
Mark Fashehccd979b2005-12-15 14:31:24 -08003909 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08003910 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08003911 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08003912 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
3913 clusters_to_del = 0;
3914 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08003915 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08003916 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08003917 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08003918 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08003919 new_highest_cpos;
3920 } else {
3921 status = 0;
3922 goto bail;
3923 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003924
Mark Fashehdcd05382007-01-16 11:32:23 -08003925 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
3926 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
3927
3928 BUG_ON(clusters_to_del == 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003929
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003930 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08003931 tl_sem = 1;
3932 /* ocfs2_truncate_log_needs_flush guarantees us at least one
3933 * record is free for use. If there isn't any, we flush to get
3934 * an empty truncate log. */
3935 if (ocfs2_truncate_log_needs_flush(osb)) {
3936 status = __ocfs2_flush_truncate_log(osb);
3937 if (status < 0) {
3938 mlog_errno(status);
3939 goto bail;
3940 }
3941 }
3942
3943 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08003944 (struct ocfs2_dinode *)fe_bh->b_data,
3945 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07003946 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08003947 if (IS_ERR(handle)) {
3948 status = PTR_ERR(handle);
3949 handle = NULL;
3950 mlog_errno(status);
3951 goto bail;
3952 }
3953
Mark Fashehdcd05382007-01-16 11:32:23 -08003954 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
3955 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08003956 if (status < 0) {
3957 mlog_errno(status);
3958 goto bail;
3959 }
3960
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003961 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08003962 tl_sem = 0;
3963
Mark Fasheh02dc1af2006-10-09 16:48:10 -07003964 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08003965 handle = NULL;
3966
Mark Fashehdcd05382007-01-16 11:32:23 -08003967 ocfs2_reinit_path(path, 1);
3968
3969 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003970 * The check above will catch the case where we've truncated
3971 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08003972 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08003973 goto start;
3974
Mark Fashehccd979b2005-12-15 14:31:24 -08003975bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08003976
3977 ocfs2_schedule_truncate_log_flush(osb, 1);
3978
3979 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003980 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08003981
3982 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07003983 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08003984
Mark Fasheh59a5e412007-06-22 15:52:36 -07003985 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
3986
Mark Fashehdcd05382007-01-16 11:32:23 -08003987 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08003988
3989 /* This will drop the ext_alloc cluster lock for us */
3990 ocfs2_free_truncate_context(tc);
3991
3992 mlog_exit(status);
3993 return status;
3994}
3995
Mark Fashehccd979b2005-12-15 14:31:24 -08003996/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07003997 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08003998 */
3999int ocfs2_prepare_truncate(struct ocfs2_super *osb,
4000 struct inode *inode,
4001 struct buffer_head *fe_bh,
4002 struct ocfs2_truncate_context **tc)
4003{
Mark Fasheh59a5e412007-06-22 15:52:36 -07004004 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08004005 unsigned int new_i_clusters;
4006 struct ocfs2_dinode *fe;
4007 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08004008 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08004009
4010 mlog_entry_void();
4011
4012 *tc = NULL;
4013
4014 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
4015 i_size_read(inode));
4016 fe = (struct ocfs2_dinode *) fe_bh->b_data;
4017
4018 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07004019 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
4020 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08004021
Robert P. J. Daycd861282006-12-13 00:34:52 -08004022 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08004023 if (!(*tc)) {
4024 status = -ENOMEM;
4025 mlog_errno(status);
4026 goto bail;
4027 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07004028 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08004029
Mark Fashehccd979b2005-12-15 14:31:24 -08004030 if (fe->id2.i_list.l_tree_depth) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004031 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
4032 &last_eb_bh, OCFS2_BH_CACHED, inode);
4033 if (status < 0) {
4034 mlog_errno(status);
4035 goto bail;
4036 }
4037 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4038 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4039 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4040
4041 brelse(last_eb_bh);
4042 status = -EIO;
4043 goto bail;
4044 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004045 }
4046
4047 (*tc)->tc_last_eb_bh = last_eb_bh;
4048
Mark Fashehccd979b2005-12-15 14:31:24 -08004049 status = 0;
4050bail:
4051 if (status < 0) {
4052 if (*tc)
4053 ocfs2_free_truncate_context(*tc);
4054 *tc = NULL;
4055 }
4056 mlog_exit_void();
4057 return status;
4058}
4059
4060static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
4061{
Mark Fasheh59a5e412007-06-22 15:52:36 -07004062 /*
4063 * The caller is responsible for completing deallocation
4064 * before freeing the context.
4065 */
4066 if (tc->tc_dealloc.c_first_suballocator != NULL)
4067 mlog(ML_NOTICE,
4068 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08004069
4070 if (tc->tc_last_eb_bh)
4071 brelse(tc->tc_last_eb_bh);
4072
4073 kfree(tc);
4074}