blob: a26882144e8fa577624a404f41a2dc4269194812 [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/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700122 * All the elements of src into dest. After this call, src could be freed
123 * without affecting dest.
124 *
125 * Both paths should have the same root. Any non-root elements of dest
126 * will be freed.
127 */
128static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
129{
130 int i;
131
132 BUG_ON(path_root_bh(dest) != path_root_bh(src));
133 BUG_ON(path_root_el(dest) != path_root_el(src));
134
135 ocfs2_reinit_path(dest, 1);
136
137 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
138 dest->p_node[i].bh = src->p_node[i].bh;
139 dest->p_node[i].el = src->p_node[i].el;
140
141 if (dest->p_node[i].bh)
142 get_bh(dest->p_node[i].bh);
143 }
144}
145
146/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800147 * Make the *dest path the same as src and re-initialize src path to
148 * have a root only.
149 */
150static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
151{
152 int i;
153
154 BUG_ON(path_root_bh(dest) != path_root_bh(src));
155
156 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
157 brelse(dest->p_node[i].bh);
158
159 dest->p_node[i].bh = src->p_node[i].bh;
160 dest->p_node[i].el = src->p_node[i].el;
161
162 src->p_node[i].bh = NULL;
163 src->p_node[i].el = NULL;
164 }
165}
166
167/*
168 * Insert an extent block at given index.
169 *
170 * This will not take an additional reference on eb_bh.
171 */
172static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
173 struct buffer_head *eb_bh)
174{
175 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
176
177 /*
178 * Right now, no root bh is an extent block, so this helps
179 * catch code errors with dinode trees. The assertion can be
180 * safely removed if we ever need to insert extent block
181 * structures at the root.
182 */
183 BUG_ON(index == 0);
184
185 path->p_node[index].bh = eb_bh;
186 path->p_node[index].el = &eb->h_list;
187}
188
189static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
190 struct ocfs2_extent_list *root_el)
191{
192 struct ocfs2_path *path;
193
194 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
195
196 path = kzalloc(sizeof(*path), GFP_NOFS);
197 if (path) {
198 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
199 get_bh(root_bh);
200 path_root_bh(path) = root_bh;
201 path_root_el(path) = root_el;
202 }
203
204 return path;
205}
206
207/*
208 * Allocate and initialize a new path based on a disk inode tree.
209 */
210static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
211{
212 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
213 struct ocfs2_extent_list *el = &di->id2.i_list;
214
215 return ocfs2_new_path(di_bh, el);
216}
217
218/*
219 * Convenience function to journal all components in a path.
220 */
221static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
222 struct ocfs2_path *path)
223{
224 int i, ret = 0;
225
226 if (!path)
227 goto out;
228
229 for(i = 0; i < path_num_items(path); i++) {
230 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
231 OCFS2_JOURNAL_ACCESS_WRITE);
232 if (ret < 0) {
233 mlog_errno(ret);
234 goto out;
235 }
236 }
237
238out:
239 return ret;
240}
241
Mark Fasheh328d5752007-06-18 10:48:04 -0700242/*
243 * Return the index of the extent record which contains cluster #v_cluster.
244 * -1 is returned if it was not found.
245 *
246 * Should work fine on interior and exterior nodes.
247 */
248int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
249{
250 int ret = -1;
251 int i;
252 struct ocfs2_extent_rec *rec;
253 u32 rec_end, rec_start, clusters;
254
255 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
256 rec = &el->l_recs[i];
257
258 rec_start = le32_to_cpu(rec->e_cpos);
259 clusters = ocfs2_rec_clusters(el, rec);
260
261 rec_end = rec_start + clusters;
262
263 if (v_cluster >= rec_start && v_cluster < rec_end) {
264 ret = i;
265 break;
266 }
267 }
268
269 return ret;
270}
271
Mark Fashehdcd05382007-01-16 11:32:23 -0800272enum ocfs2_contig_type {
273 CONTIG_NONE = 0,
274 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700275 CONTIG_RIGHT,
276 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800277};
278
Mark Fashehe48edee2007-03-07 16:46:57 -0800279
280/*
281 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
282 * ocfs2_extent_contig only work properly against leaf nodes!
283 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800284static int ocfs2_block_extent_contig(struct super_block *sb,
285 struct ocfs2_extent_rec *ext,
286 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800287{
Mark Fashehe48edee2007-03-07 16:46:57 -0800288 u64 blk_end = le64_to_cpu(ext->e_blkno);
289
290 blk_end += ocfs2_clusters_to_blocks(sb,
291 le16_to_cpu(ext->e_leaf_clusters));
292
293 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800294}
295
Mark Fashehdcd05382007-01-16 11:32:23 -0800296static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
297 struct ocfs2_extent_rec *right)
298{
Mark Fashehe48edee2007-03-07 16:46:57 -0800299 u32 left_range;
300
301 left_range = le32_to_cpu(left->e_cpos) +
302 le16_to_cpu(left->e_leaf_clusters);
303
304 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800305}
306
307static enum ocfs2_contig_type
308 ocfs2_extent_contig(struct inode *inode,
309 struct ocfs2_extent_rec *ext,
310 struct ocfs2_extent_rec *insert_rec)
311{
312 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
313
Mark Fasheh328d5752007-06-18 10:48:04 -0700314 /*
315 * Refuse to coalesce extent records with different flag
316 * fields - we don't want to mix unwritten extents with user
317 * data.
318 */
319 if (ext->e_flags != insert_rec->e_flags)
320 return CONTIG_NONE;
321
Mark Fashehdcd05382007-01-16 11:32:23 -0800322 if (ocfs2_extents_adjacent(ext, insert_rec) &&
323 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
324 return CONTIG_RIGHT;
325
326 blkno = le64_to_cpu(ext->e_blkno);
327 if (ocfs2_extents_adjacent(insert_rec, ext) &&
328 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
329 return CONTIG_LEFT;
330
331 return CONTIG_NONE;
332}
333
334/*
335 * NOTE: We can have pretty much any combination of contiguousness and
336 * appending.
337 *
338 * The usefulness of APPEND_TAIL is more in that it lets us know that
339 * we'll have to update the path to that leaf.
340 */
341enum ocfs2_append_type {
342 APPEND_NONE = 0,
343 APPEND_TAIL,
344};
345
Mark Fasheh328d5752007-06-18 10:48:04 -0700346enum ocfs2_split_type {
347 SPLIT_NONE = 0,
348 SPLIT_LEFT,
349 SPLIT_RIGHT,
350};
351
Mark Fashehdcd05382007-01-16 11:32:23 -0800352struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700353 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800354 enum ocfs2_append_type ins_appending;
355 enum ocfs2_contig_type ins_contig;
356 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800357 int ins_tree_depth;
358};
359
Mark Fasheh328d5752007-06-18 10:48:04 -0700360struct ocfs2_merge_ctxt {
361 enum ocfs2_contig_type c_contig_type;
362 int c_has_empty_extent;
363 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700364};
365
Mark Fashehccd979b2005-12-15 14:31:24 -0800366/*
367 * How many free extents have we got before we need more meta data?
368 */
369int ocfs2_num_free_extents(struct ocfs2_super *osb,
370 struct inode *inode,
371 struct ocfs2_dinode *fe)
372{
373 int retval;
374 struct ocfs2_extent_list *el;
375 struct ocfs2_extent_block *eb;
376 struct buffer_head *eb_bh = NULL;
377
378 mlog_entry_void();
379
380 if (!OCFS2_IS_VALID_DINODE(fe)) {
381 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
382 retval = -EIO;
383 goto bail;
384 }
385
386 if (fe->i_last_eb_blk) {
387 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
388 &eb_bh, OCFS2_BH_CACHED, inode);
389 if (retval < 0) {
390 mlog_errno(retval);
391 goto bail;
392 }
393 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
394 el = &eb->h_list;
395 } else
396 el = &fe->id2.i_list;
397
398 BUG_ON(el->l_tree_depth != 0);
399
400 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
401bail:
402 if (eb_bh)
403 brelse(eb_bh);
404
405 mlog_exit(retval);
406 return retval;
407}
408
409/* expects array to already be allocated
410 *
411 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
412 * l_count for you
413 */
414static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700415 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 struct inode *inode,
417 int wanted,
418 struct ocfs2_alloc_context *meta_ac,
419 struct buffer_head *bhs[])
420{
421 int count, status, i;
422 u16 suballoc_bit_start;
423 u32 num_got;
424 u64 first_blkno;
425 struct ocfs2_extent_block *eb;
426
427 mlog_entry_void();
428
429 count = 0;
430 while (count < wanted) {
431 status = ocfs2_claim_metadata(osb,
432 handle,
433 meta_ac,
434 wanted - count,
435 &suballoc_bit_start,
436 &num_got,
437 &first_blkno);
438 if (status < 0) {
439 mlog_errno(status);
440 goto bail;
441 }
442
443 for(i = count; i < (num_got + count); i++) {
444 bhs[i] = sb_getblk(osb->sb, first_blkno);
445 if (bhs[i] == NULL) {
446 status = -EIO;
447 mlog_errno(status);
448 goto bail;
449 }
450 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
451
452 status = ocfs2_journal_access(handle, inode, bhs[i],
453 OCFS2_JOURNAL_ACCESS_CREATE);
454 if (status < 0) {
455 mlog_errno(status);
456 goto bail;
457 }
458
459 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
460 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
461 /* Ok, setup the minimal stuff here. */
462 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
463 eb->h_blkno = cpu_to_le64(first_blkno);
464 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800465 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800466 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
467 eb->h_list.l_count =
468 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
469
470 suballoc_bit_start++;
471 first_blkno++;
472
473 /* We'll also be dirtied by the caller, so
474 * this isn't absolutely necessary. */
475 status = ocfs2_journal_dirty(handle, bhs[i]);
476 if (status < 0) {
477 mlog_errno(status);
478 goto bail;
479 }
480 }
481
482 count += num_got;
483 }
484
485 status = 0;
486bail:
487 if (status < 0) {
488 for(i = 0; i < wanted; i++) {
489 if (bhs[i])
490 brelse(bhs[i]);
491 bhs[i] = NULL;
492 }
493 }
494 mlog_exit(status);
495 return status;
496}
497
498/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800499 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
500 *
501 * Returns the sum of the rightmost extent rec logical offset and
502 * cluster count.
503 *
504 * ocfs2_add_branch() uses this to determine what logical cluster
505 * value should be populated into the leftmost new branch records.
506 *
507 * ocfs2_shift_tree_depth() uses this to determine the # clusters
508 * value for the new topmost tree record.
509 */
510static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
511{
512 int i;
513
514 i = le16_to_cpu(el->l_next_free_rec) - 1;
515
516 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800517 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800518}
519
520/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800521 * Add an entire tree branch to our inode. eb_bh is the extent block
522 * to start at, if we don't want to start the branch at the dinode
523 * structure.
524 *
525 * last_eb_bh is required as we have to update it's next_leaf pointer
526 * for the new last extent block.
527 *
528 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800529 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800530 */
531static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700532 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800533 struct inode *inode,
534 struct buffer_head *fe_bh,
535 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700536 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 struct ocfs2_alloc_context *meta_ac)
538{
539 int status, new_blocks, i;
540 u64 next_blkno, new_last_eb_blk;
541 struct buffer_head *bh;
542 struct buffer_head **new_eb_bhs = NULL;
543 struct ocfs2_dinode *fe;
544 struct ocfs2_extent_block *eb;
545 struct ocfs2_extent_list *eb_el;
546 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800547 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800548
549 mlog_entry_void();
550
Mark Fasheh328d5752007-06-18 10:48:04 -0700551 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800552
553 fe = (struct ocfs2_dinode *) fe_bh->b_data;
554
555 if (eb_bh) {
556 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
557 el = &eb->h_list;
558 } else
559 el = &fe->id2.i_list;
560
561 /* we never add a branch to a leaf. */
562 BUG_ON(!el->l_tree_depth);
563
564 new_blocks = le16_to_cpu(el->l_tree_depth);
565
566 /* allocate the number of new eb blocks we need */
567 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
568 GFP_KERNEL);
569 if (!new_eb_bhs) {
570 status = -ENOMEM;
571 mlog_errno(status);
572 goto bail;
573 }
574
575 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
576 meta_ac, new_eb_bhs);
577 if (status < 0) {
578 mlog_errno(status);
579 goto bail;
580 }
581
Mark Fasheh328d5752007-06-18 10:48:04 -0700582 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -0800583 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
584
Mark Fashehccd979b2005-12-15 14:31:24 -0800585 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
586 * linked with the rest of the tree.
587 * conversly, new_eb_bhs[0] is the new bottommost leaf.
588 *
589 * when we leave the loop, new_last_eb_blk will point to the
590 * newest leaf, and next_blkno will point to the topmost extent
591 * block. */
592 next_blkno = new_last_eb_blk = 0;
593 for(i = 0; i < new_blocks; i++) {
594 bh = new_eb_bhs[i];
595 eb = (struct ocfs2_extent_block *) bh->b_data;
596 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
597 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
598 status = -EIO;
599 goto bail;
600 }
601 eb_el = &eb->h_list;
602
603 status = ocfs2_journal_access(handle, inode, bh,
604 OCFS2_JOURNAL_ACCESS_CREATE);
605 if (status < 0) {
606 mlog_errno(status);
607 goto bail;
608 }
609
610 eb->h_next_leaf_blk = 0;
611 eb_el->l_tree_depth = cpu_to_le16(i);
612 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800613 /*
614 * This actually counts as an empty extent as
615 * c_clusters == 0
616 */
617 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -0800618 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -0800619 /*
620 * eb_el isn't always an interior node, but even leaf
621 * nodes want a zero'd flags and reserved field so
622 * this gets the whole 32 bits regardless of use.
623 */
624 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800625 if (!eb_el->l_tree_depth)
626 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
627
628 status = ocfs2_journal_dirty(handle, bh);
629 if (status < 0) {
630 mlog_errno(status);
631 goto bail;
632 }
633
634 next_blkno = le64_to_cpu(eb->h_blkno);
635 }
636
637 /* This is a bit hairy. We want to update up to three blocks
638 * here without leaving any of them in an inconsistent state
639 * in case of error. We don't have to worry about
640 * journal_dirty erroring as it won't unless we've aborted the
641 * handle (in which case we would never be here) so reserving
642 * the write with journal_access is all we need to do. */
Mark Fasheh328d5752007-06-18 10:48:04 -0700643 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800644 OCFS2_JOURNAL_ACCESS_WRITE);
645 if (status < 0) {
646 mlog_errno(status);
647 goto bail;
648 }
649 status = ocfs2_journal_access(handle, inode, fe_bh,
650 OCFS2_JOURNAL_ACCESS_WRITE);
651 if (status < 0) {
652 mlog_errno(status);
653 goto bail;
654 }
655 if (eb_bh) {
656 status = ocfs2_journal_access(handle, inode, eb_bh,
657 OCFS2_JOURNAL_ACCESS_WRITE);
658 if (status < 0) {
659 mlog_errno(status);
660 goto bail;
661 }
662 }
663
664 /* Link the new branch into the rest of the tree (el will
665 * either be on the fe, or the extent block passed in. */
666 i = le16_to_cpu(el->l_next_free_rec);
667 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800668 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -0800669 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800670 le16_add_cpu(&el->l_next_free_rec, 1);
671
672 /* fe needs a new last extent block pointer, as does the
673 * next_leaf on the previously last-extent-block. */
674 fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
675
Mark Fasheh328d5752007-06-18 10:48:04 -0700676 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800677 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
678
Mark Fasheh328d5752007-06-18 10:48:04 -0700679 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800680 if (status < 0)
681 mlog_errno(status);
682 status = ocfs2_journal_dirty(handle, fe_bh);
683 if (status < 0)
684 mlog_errno(status);
685 if (eb_bh) {
686 status = ocfs2_journal_dirty(handle, eb_bh);
687 if (status < 0)
688 mlog_errno(status);
689 }
690
Mark Fasheh328d5752007-06-18 10:48:04 -0700691 /*
692 * Some callers want to track the rightmost leaf so pass it
693 * back here.
694 */
695 brelse(*last_eb_bh);
696 get_bh(new_eb_bhs[0]);
697 *last_eb_bh = new_eb_bhs[0];
698
Mark Fashehccd979b2005-12-15 14:31:24 -0800699 status = 0;
700bail:
701 if (new_eb_bhs) {
702 for (i = 0; i < new_blocks; i++)
703 if (new_eb_bhs[i])
704 brelse(new_eb_bhs[i]);
705 kfree(new_eb_bhs);
706 }
707
708 mlog_exit(status);
709 return status;
710}
711
712/*
713 * adds another level to the allocation tree.
714 * returns back the new extent block so you can add a branch to it
715 * after this call.
716 */
717static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700718 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800719 struct inode *inode,
720 struct buffer_head *fe_bh,
721 struct ocfs2_alloc_context *meta_ac,
722 struct buffer_head **ret_new_eb_bh)
723{
724 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -0800725 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -0800726 struct buffer_head *new_eb_bh = NULL;
727 struct ocfs2_dinode *fe;
728 struct ocfs2_extent_block *eb;
729 struct ocfs2_extent_list *fe_el;
730 struct ocfs2_extent_list *eb_el;
731
732 mlog_entry_void();
733
734 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
735 &new_eb_bh);
736 if (status < 0) {
737 mlog_errno(status);
738 goto bail;
739 }
740
741 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
742 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
743 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
744 status = -EIO;
745 goto bail;
746 }
747
748 eb_el = &eb->h_list;
749 fe = (struct ocfs2_dinode *) fe_bh->b_data;
750 fe_el = &fe->id2.i_list;
751
752 status = ocfs2_journal_access(handle, inode, new_eb_bh,
753 OCFS2_JOURNAL_ACCESS_CREATE);
754 if (status < 0) {
755 mlog_errno(status);
756 goto bail;
757 }
758
759 /* copy the fe data into the new extent block */
760 eb_el->l_tree_depth = fe_el->l_tree_depth;
761 eb_el->l_next_free_rec = fe_el->l_next_free_rec;
Mark Fashehe48edee2007-03-07 16:46:57 -0800762 for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
763 eb_el->l_recs[i] = fe_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -0800764
765 status = ocfs2_journal_dirty(handle, new_eb_bh);
766 if (status < 0) {
767 mlog_errno(status);
768 goto bail;
769 }
770
771 status = ocfs2_journal_access(handle, inode, fe_bh,
772 OCFS2_JOURNAL_ACCESS_WRITE);
773 if (status < 0) {
774 mlog_errno(status);
775 goto bail;
776 }
777
Mark Fashehdcd05382007-01-16 11:32:23 -0800778 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
779
Mark Fashehccd979b2005-12-15 14:31:24 -0800780 /* update fe now */
781 le16_add_cpu(&fe_el->l_tree_depth, 1);
782 fe_el->l_recs[0].e_cpos = 0;
783 fe_el->l_recs[0].e_blkno = eb->h_blkno;
Mark Fashehe48edee2007-03-07 16:46:57 -0800784 fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
785 for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
786 memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
Mark Fashehccd979b2005-12-15 14:31:24 -0800787 fe_el->l_next_free_rec = cpu_to_le16(1);
788
789 /* If this is our 1st tree depth shift, then last_eb_blk
790 * becomes the allocated extent block */
791 if (fe_el->l_tree_depth == cpu_to_le16(1))
792 fe->i_last_eb_blk = eb->h_blkno;
793
794 status = ocfs2_journal_dirty(handle, fe_bh);
795 if (status < 0) {
796 mlog_errno(status);
797 goto bail;
798 }
799
800 *ret_new_eb_bh = new_eb_bh;
801 new_eb_bh = NULL;
802 status = 0;
803bail:
804 if (new_eb_bh)
805 brelse(new_eb_bh);
806
807 mlog_exit(status);
808 return status;
809}
810
811/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800812 * Should only be called when there is no space left in any of the
813 * leaf nodes. What we want to do is find the lowest tree depth
814 * non-leaf extent block with room for new records. There are three
815 * valid results of this search:
816 *
817 * 1) a lowest extent block is found, then we pass it back in
818 * *lowest_eb_bh and return '0'
819 *
820 * 2) the search fails to find anything, but the dinode has room. We
821 * pass NULL back in *lowest_eb_bh, but still return '0'
822 *
823 * 3) the search fails to find anything AND the dinode is full, in
824 * which case we return > 0
825 *
826 * return status < 0 indicates an error.
827 */
828static int ocfs2_find_branch_target(struct ocfs2_super *osb,
829 struct inode *inode,
830 struct buffer_head *fe_bh,
831 struct buffer_head **target_bh)
832{
833 int status = 0, i;
834 u64 blkno;
835 struct ocfs2_dinode *fe;
836 struct ocfs2_extent_block *eb;
837 struct ocfs2_extent_list *el;
838 struct buffer_head *bh = NULL;
839 struct buffer_head *lowest_bh = NULL;
840
841 mlog_entry_void();
842
843 *target_bh = NULL;
844
845 fe = (struct ocfs2_dinode *) fe_bh->b_data;
846 el = &fe->id2.i_list;
847
848 while(le16_to_cpu(el->l_tree_depth) > 1) {
849 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb0697052006-03-03 10:24:33 -0800850 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 "extent list (next_free_rec == 0)",
Mark Fashehb0697052006-03-03 10:24:33 -0800852 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 status = -EIO;
854 goto bail;
855 }
856 i = le16_to_cpu(el->l_next_free_rec) - 1;
857 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
858 if (!blkno) {
Mark Fashehb0697052006-03-03 10:24:33 -0800859 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -0800860 "list where extent # %d has no physical "
861 "block start",
Mark Fashehb0697052006-03-03 10:24:33 -0800862 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -0800863 status = -EIO;
864 goto bail;
865 }
866
867 if (bh) {
868 brelse(bh);
869 bh = NULL;
870 }
871
872 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
873 inode);
874 if (status < 0) {
875 mlog_errno(status);
876 goto bail;
877 }
878
879 eb = (struct ocfs2_extent_block *) bh->b_data;
880 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
881 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
882 status = -EIO;
883 goto bail;
884 }
885 el = &eb->h_list;
886
887 if (le16_to_cpu(el->l_next_free_rec) <
888 le16_to_cpu(el->l_count)) {
889 if (lowest_bh)
890 brelse(lowest_bh);
891 lowest_bh = bh;
892 get_bh(lowest_bh);
893 }
894 }
895
896 /* If we didn't find one and the fe doesn't have any room,
897 * then return '1' */
898 if (!lowest_bh
899 && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
900 status = 1;
901
902 *target_bh = lowest_bh;
903bail:
904 if (bh)
905 brelse(bh);
906
907 mlog_exit(status);
908 return status;
909}
910
Mark Fashehe48edee2007-03-07 16:46:57 -0800911/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -0700912 * Grow a b-tree so that it has more records.
913 *
914 * We might shift the tree depth in which case existing paths should
915 * be considered invalid.
916 *
917 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -0700918 *
919 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -0700920 */
921static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
922 struct buffer_head *di_bh, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -0700923 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -0700924 struct ocfs2_alloc_context *meta_ac)
925{
926 int ret, shift;
927 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
928 int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
929 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
930 struct buffer_head *bh = NULL;
931
932 BUG_ON(meta_ac == NULL);
933
934 shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
935 if (shift < 0) {
936 ret = shift;
937 mlog_errno(ret);
938 goto out;
939 }
940
941 /* We traveled all the way to the bottom of the allocation tree
942 * and didn't find room for any more extents - we need to add
943 * another tree level */
944 if (shift) {
945 BUG_ON(bh);
946 mlog(0, "need to shift tree depth (current = %d)\n", depth);
947
948 /* ocfs2_shift_tree_depth will return us a buffer with
949 * the new extent block (so we can pass that to
950 * ocfs2_add_branch). */
951 ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
952 meta_ac, &bh);
953 if (ret < 0) {
954 mlog_errno(ret);
955 goto out;
956 }
957 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -0700958 if (depth == 1) {
959 /*
960 * Special case: we have room now if we shifted from
961 * tree_depth 0, so no more work needs to be done.
962 *
963 * We won't be calling add_branch, so pass
964 * back *last_eb_bh as the new leaf. At depth
965 * zero, it should always be null so there's
966 * no reason to brelse.
967 */
968 BUG_ON(*last_eb_bh);
969 get_bh(bh);
970 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -0700971 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -0700972 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -0700973 }
974
975 /* call ocfs2_add_branch to add the final part of the tree with
976 * the new data. */
977 mlog(0, "add branch. bh = %p\n", bh);
978 ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
979 meta_ac);
980 if (ret < 0) {
981 mlog_errno(ret);
982 goto out;
983 }
984
985out:
986 if (final_depth)
987 *final_depth = depth;
988 brelse(bh);
989 return ret;
990}
991
992/*
Mark Fashehe48edee2007-03-07 16:46:57 -0800993 * This is only valid for leaf nodes, which are the only ones that can
994 * have empty extents anyway.
995 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800996static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
997{
Mark Fashehe48edee2007-03-07 16:46:57 -0800998 return !rec->e_leaf_clusters;
Mark Fashehdcd05382007-01-16 11:32:23 -0800999}
1000
1001/*
1002 * This function will discard the rightmost extent record.
1003 */
1004static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1005{
1006 int next_free = le16_to_cpu(el->l_next_free_rec);
1007 int count = le16_to_cpu(el->l_count);
1008 unsigned int num_bytes;
1009
1010 BUG_ON(!next_free);
1011 /* This will cause us to go off the end of our extent list. */
1012 BUG_ON(next_free >= count);
1013
1014 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1015
1016 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1017}
1018
1019static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1020 struct ocfs2_extent_rec *insert_rec)
1021{
1022 int i, insert_index, next_free, has_empty, num_bytes;
1023 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1024 struct ocfs2_extent_rec *rec;
1025
1026 next_free = le16_to_cpu(el->l_next_free_rec);
1027 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1028
1029 BUG_ON(!next_free);
1030
1031 /* The tree code before us didn't allow enough room in the leaf. */
1032 if (el->l_next_free_rec == el->l_count && !has_empty)
1033 BUG();
1034
1035 /*
1036 * The easiest way to approach this is to just remove the
1037 * empty extent and temporarily decrement next_free.
1038 */
1039 if (has_empty) {
1040 /*
1041 * If next_free was 1 (only an empty extent), this
1042 * loop won't execute, which is fine. We still want
1043 * the decrement above to happen.
1044 */
1045 for(i = 0; i < (next_free - 1); i++)
1046 el->l_recs[i] = el->l_recs[i+1];
1047
1048 next_free--;
1049 }
1050
1051 /*
1052 * Figure out what the new record index should be.
1053 */
1054 for(i = 0; i < next_free; i++) {
1055 rec = &el->l_recs[i];
1056
1057 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1058 break;
1059 }
1060 insert_index = i;
1061
1062 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1063 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1064
1065 BUG_ON(insert_index < 0);
1066 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1067 BUG_ON(insert_index > next_free);
1068
1069 /*
1070 * No need to memmove if we're just adding to the tail.
1071 */
1072 if (insert_index != next_free) {
1073 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1074
1075 num_bytes = next_free - insert_index;
1076 num_bytes *= sizeof(struct ocfs2_extent_rec);
1077 memmove(&el->l_recs[insert_index + 1],
1078 &el->l_recs[insert_index],
1079 num_bytes);
1080 }
1081
1082 /*
1083 * Either we had an empty extent, and need to re-increment or
1084 * there was no empty extent on a non full rightmost leaf node,
1085 * in which case we still need to increment.
1086 */
1087 next_free++;
1088 el->l_next_free_rec = cpu_to_le16(next_free);
1089 /*
1090 * Make sure none of the math above just messed up our tree.
1091 */
1092 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1093
1094 el->l_recs[insert_index] = *insert_rec;
1095
1096}
1097
Mark Fasheh328d5752007-06-18 10:48:04 -07001098static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1099{
1100 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1101
1102 BUG_ON(num_recs == 0);
1103
1104 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1105 num_recs--;
1106 size = num_recs * sizeof(struct ocfs2_extent_rec);
1107 memmove(&el->l_recs[0], &el->l_recs[1], size);
1108 memset(&el->l_recs[num_recs], 0,
1109 sizeof(struct ocfs2_extent_rec));
1110 el->l_next_free_rec = cpu_to_le16(num_recs);
1111 }
1112}
1113
Mark Fashehdcd05382007-01-16 11:32:23 -08001114/*
1115 * Create an empty extent record .
1116 *
1117 * l_next_free_rec may be updated.
1118 *
1119 * If an empty extent already exists do nothing.
1120 */
1121static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1122{
1123 int next_free = le16_to_cpu(el->l_next_free_rec);
1124
Mark Fashehe48edee2007-03-07 16:46:57 -08001125 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1126
Mark Fashehdcd05382007-01-16 11:32:23 -08001127 if (next_free == 0)
1128 goto set_and_inc;
1129
1130 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1131 return;
1132
1133 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1134 "Asked to create an empty extent in a full list:\n"
1135 "count = %u, tree depth = %u",
1136 le16_to_cpu(el->l_count),
1137 le16_to_cpu(el->l_tree_depth));
1138
1139 ocfs2_shift_records_right(el);
1140
1141set_and_inc:
1142 le16_add_cpu(&el->l_next_free_rec, 1);
1143 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1144}
1145
1146/*
1147 * For a rotation which involves two leaf nodes, the "root node" is
1148 * the lowest level tree node which contains a path to both leafs. This
1149 * resulting set of information can be used to form a complete "subtree"
1150 *
1151 * This function is passed two full paths from the dinode down to a
1152 * pair of adjacent leaves. It's task is to figure out which path
1153 * index contains the subtree root - this can be the root index itself
1154 * in a worst-case rotation.
1155 *
1156 * The array index of the subtree root is passed back.
1157 */
1158static int ocfs2_find_subtree_root(struct inode *inode,
1159 struct ocfs2_path *left,
1160 struct ocfs2_path *right)
1161{
1162 int i = 0;
1163
1164 /*
1165 * Check that the caller passed in two paths from the same tree.
1166 */
1167 BUG_ON(path_root_bh(left) != path_root_bh(right));
1168
1169 do {
1170 i++;
1171
1172 /*
1173 * The caller didn't pass two adjacent paths.
1174 */
1175 mlog_bug_on_msg(i > left->p_tree_depth,
1176 "Inode %lu, left depth %u, right depth %u\n"
1177 "left leaf blk %llu, right leaf blk %llu\n",
1178 inode->i_ino, left->p_tree_depth,
1179 right->p_tree_depth,
1180 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1181 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1182 } while (left->p_node[i].bh->b_blocknr ==
1183 right->p_node[i].bh->b_blocknr);
1184
1185 return i - 1;
1186}
1187
1188typedef void (path_insert_t)(void *, struct buffer_head *);
1189
1190/*
1191 * Traverse a btree path in search of cpos, starting at root_el.
1192 *
1193 * This code can be called with a cpos larger than the tree, in which
1194 * case it will return the rightmost path.
1195 */
1196static int __ocfs2_find_path(struct inode *inode,
1197 struct ocfs2_extent_list *root_el, u32 cpos,
1198 path_insert_t *func, void *data)
1199{
1200 int i, ret = 0;
1201 u32 range;
1202 u64 blkno;
1203 struct buffer_head *bh = NULL;
1204 struct ocfs2_extent_block *eb;
1205 struct ocfs2_extent_list *el;
1206 struct ocfs2_extent_rec *rec;
1207 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1208
1209 el = root_el;
1210 while (el->l_tree_depth) {
1211 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1212 ocfs2_error(inode->i_sb,
1213 "Inode %llu has empty extent list at "
1214 "depth %u\n",
1215 (unsigned long long)oi->ip_blkno,
1216 le16_to_cpu(el->l_tree_depth));
1217 ret = -EROFS;
1218 goto out;
1219
1220 }
1221
1222 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1223 rec = &el->l_recs[i];
1224
1225 /*
1226 * In the case that cpos is off the allocation
1227 * tree, this should just wind up returning the
1228 * rightmost record.
1229 */
1230 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001231 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001232 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1233 break;
1234 }
1235
1236 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1237 if (blkno == 0) {
1238 ocfs2_error(inode->i_sb,
1239 "Inode %llu has bad blkno in extent list "
1240 "at depth %u (index %d)\n",
1241 (unsigned long long)oi->ip_blkno,
1242 le16_to_cpu(el->l_tree_depth), i);
1243 ret = -EROFS;
1244 goto out;
1245 }
1246
1247 brelse(bh);
1248 bh = NULL;
1249 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1250 &bh, OCFS2_BH_CACHED, inode);
1251 if (ret) {
1252 mlog_errno(ret);
1253 goto out;
1254 }
1255
1256 eb = (struct ocfs2_extent_block *) bh->b_data;
1257 el = &eb->h_list;
1258 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1259 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1260 ret = -EIO;
1261 goto out;
1262 }
1263
1264 if (le16_to_cpu(el->l_next_free_rec) >
1265 le16_to_cpu(el->l_count)) {
1266 ocfs2_error(inode->i_sb,
1267 "Inode %llu has bad count in extent list "
1268 "at block %llu (next free=%u, count=%u)\n",
1269 (unsigned long long)oi->ip_blkno,
1270 (unsigned long long)bh->b_blocknr,
1271 le16_to_cpu(el->l_next_free_rec),
1272 le16_to_cpu(el->l_count));
1273 ret = -EROFS;
1274 goto out;
1275 }
1276
1277 if (func)
1278 func(data, bh);
1279 }
1280
1281out:
1282 /*
1283 * Catch any trailing bh that the loop didn't handle.
1284 */
1285 brelse(bh);
1286
1287 return ret;
1288}
1289
1290/*
1291 * Given an initialized path (that is, it has a valid root extent
1292 * list), this function will traverse the btree in search of the path
1293 * which would contain cpos.
1294 *
1295 * The path traveled is recorded in the path structure.
1296 *
1297 * Note that this will not do any comparisons on leaf node extent
1298 * records, so it will work fine in the case that we just added a tree
1299 * branch.
1300 */
1301struct find_path_data {
1302 int index;
1303 struct ocfs2_path *path;
1304};
1305static void find_path_ins(void *data, struct buffer_head *bh)
1306{
1307 struct find_path_data *fp = data;
1308
1309 get_bh(bh);
1310 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1311 fp->index++;
1312}
1313static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1314 u32 cpos)
1315{
1316 struct find_path_data data;
1317
1318 data.index = 1;
1319 data.path = path;
1320 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1321 find_path_ins, &data);
1322}
1323
1324static void find_leaf_ins(void *data, struct buffer_head *bh)
1325{
1326 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1327 struct ocfs2_extent_list *el = &eb->h_list;
1328 struct buffer_head **ret = data;
1329
1330 /* We want to retain only the leaf block. */
1331 if (le16_to_cpu(el->l_tree_depth) == 0) {
1332 get_bh(bh);
1333 *ret = bh;
1334 }
1335}
1336/*
1337 * Find the leaf block in the tree which would contain cpos. No
1338 * checking of the actual leaf is done.
1339 *
1340 * Some paths want to call this instead of allocating a path structure
1341 * and calling ocfs2_find_path().
1342 *
1343 * This function doesn't handle non btree extent lists.
1344 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001345int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1346 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001347{
1348 int ret;
1349 struct buffer_head *bh = NULL;
1350
1351 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1352 if (ret) {
1353 mlog_errno(ret);
1354 goto out;
1355 }
1356
1357 *leaf_bh = bh;
1358out:
1359 return ret;
1360}
1361
1362/*
1363 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1364 *
1365 * Basically, we've moved stuff around at the bottom of the tree and
1366 * we need to fix up the extent records above the changes to reflect
1367 * the new changes.
1368 *
1369 * left_rec: the record on the left.
1370 * left_child_el: is the child list pointed to by left_rec
1371 * right_rec: the record to the right of left_rec
1372 * right_child_el: is the child list pointed to by right_rec
1373 *
1374 * By definition, this only works on interior nodes.
1375 */
1376static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1377 struct ocfs2_extent_list *left_child_el,
1378 struct ocfs2_extent_rec *right_rec,
1379 struct ocfs2_extent_list *right_child_el)
1380{
1381 u32 left_clusters, right_end;
1382
1383 /*
1384 * Interior nodes never have holes. Their cpos is the cpos of
1385 * the leftmost record in their child list. Their cluster
1386 * count covers the full theoretical range of their child list
1387 * - the range between their cpos and the cpos of the record
1388 * immediately to their right.
1389 */
1390 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001391 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1392 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1393 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1394 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001395 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001396 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001397
1398 /*
1399 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001400 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001401 * updating e_cpos to keep the same highest cluster count.
1402 */
1403 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001404 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001405
1406 right_rec->e_cpos = left_rec->e_cpos;
1407 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1408
1409 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001410 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001411}
1412
1413/*
1414 * Adjust the adjacent root node records involved in a
1415 * rotation. left_el_blkno is passed in as a key so that we can easily
1416 * find it's index in the root list.
1417 */
1418static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1419 struct ocfs2_extent_list *left_el,
1420 struct ocfs2_extent_list *right_el,
1421 u64 left_el_blkno)
1422{
1423 int i;
1424
1425 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1426 le16_to_cpu(left_el->l_tree_depth));
1427
1428 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1429 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1430 break;
1431 }
1432
1433 /*
1434 * The path walking code should have never returned a root and
1435 * two paths which are not adjacent.
1436 */
1437 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1438
1439 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1440 &root_el->l_recs[i + 1], right_el);
1441}
1442
1443/*
1444 * We've changed a leaf block (in right_path) and need to reflect that
1445 * change back up the subtree.
1446 *
1447 * This happens in multiple places:
1448 * - When we've moved an extent record from the left path leaf to the right
1449 * path leaf to make room for an empty extent in the left path leaf.
1450 * - When our insert into the right path leaf is at the leftmost edge
1451 * and requires an update of the path immediately to it's left. This
1452 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001453 * - When we've adjusted the last extent record in the left path leaf and the
1454 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001455 */
1456static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1457 struct ocfs2_path *left_path,
1458 struct ocfs2_path *right_path,
1459 int subtree_index)
1460{
1461 int ret, i, idx;
1462 struct ocfs2_extent_list *el, *left_el, *right_el;
1463 struct ocfs2_extent_rec *left_rec, *right_rec;
1464 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1465
1466 /*
1467 * Update the counts and position values within all the
1468 * interior nodes to reflect the leaf rotation we just did.
1469 *
1470 * The root node is handled below the loop.
1471 *
1472 * We begin the loop with right_el and left_el pointing to the
1473 * leaf lists and work our way up.
1474 *
1475 * NOTE: within this loop, left_el and right_el always refer
1476 * to the *child* lists.
1477 */
1478 left_el = path_leaf_el(left_path);
1479 right_el = path_leaf_el(right_path);
1480 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1481 mlog(0, "Adjust records at index %u\n", i);
1482
1483 /*
1484 * One nice property of knowing that all of these
1485 * nodes are below the root is that we only deal with
1486 * the leftmost right node record and the rightmost
1487 * left node record.
1488 */
1489 el = left_path->p_node[i].el;
1490 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1491 left_rec = &el->l_recs[idx];
1492
1493 el = right_path->p_node[i].el;
1494 right_rec = &el->l_recs[0];
1495
1496 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1497 right_el);
1498
1499 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1500 if (ret)
1501 mlog_errno(ret);
1502
1503 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1504 if (ret)
1505 mlog_errno(ret);
1506
1507 /*
1508 * Setup our list pointers now so that the current
1509 * parents become children in the next iteration.
1510 */
1511 left_el = left_path->p_node[i].el;
1512 right_el = right_path->p_node[i].el;
1513 }
1514
1515 /*
1516 * At the root node, adjust the two adjacent records which
1517 * begin our path to the leaves.
1518 */
1519
1520 el = left_path->p_node[subtree_index].el;
1521 left_el = left_path->p_node[subtree_index + 1].el;
1522 right_el = right_path->p_node[subtree_index + 1].el;
1523
1524 ocfs2_adjust_root_records(el, left_el, right_el,
1525 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1526
1527 root_bh = left_path->p_node[subtree_index].bh;
1528
1529 ret = ocfs2_journal_dirty(handle, root_bh);
1530 if (ret)
1531 mlog_errno(ret);
1532}
1533
1534static int ocfs2_rotate_subtree_right(struct inode *inode,
1535 handle_t *handle,
1536 struct ocfs2_path *left_path,
1537 struct ocfs2_path *right_path,
1538 int subtree_index)
1539{
1540 int ret, i;
1541 struct buffer_head *right_leaf_bh;
1542 struct buffer_head *left_leaf_bh = NULL;
1543 struct buffer_head *root_bh;
1544 struct ocfs2_extent_list *right_el, *left_el;
1545 struct ocfs2_extent_rec move_rec;
1546
1547 left_leaf_bh = path_leaf_bh(left_path);
1548 left_el = path_leaf_el(left_path);
1549
1550 if (left_el->l_next_free_rec != left_el->l_count) {
1551 ocfs2_error(inode->i_sb,
1552 "Inode %llu has non-full interior leaf node %llu"
1553 "(next free = %u)",
1554 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1555 (unsigned long long)left_leaf_bh->b_blocknr,
1556 le16_to_cpu(left_el->l_next_free_rec));
1557 return -EROFS;
1558 }
1559
1560 /*
1561 * This extent block may already have an empty record, so we
1562 * return early if so.
1563 */
1564 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1565 return 0;
1566
1567 root_bh = left_path->p_node[subtree_index].bh;
1568 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1569
1570 ret = ocfs2_journal_access(handle, inode, root_bh,
1571 OCFS2_JOURNAL_ACCESS_WRITE);
1572 if (ret) {
1573 mlog_errno(ret);
1574 goto out;
1575 }
1576
1577 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1578 ret = ocfs2_journal_access(handle, inode,
1579 right_path->p_node[i].bh,
1580 OCFS2_JOURNAL_ACCESS_WRITE);
1581 if (ret) {
1582 mlog_errno(ret);
1583 goto out;
1584 }
1585
1586 ret = ocfs2_journal_access(handle, inode,
1587 left_path->p_node[i].bh,
1588 OCFS2_JOURNAL_ACCESS_WRITE);
1589 if (ret) {
1590 mlog_errno(ret);
1591 goto out;
1592 }
1593 }
1594
1595 right_leaf_bh = path_leaf_bh(right_path);
1596 right_el = path_leaf_el(right_path);
1597
1598 /* This is a code error, not a disk corruption. */
1599 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1600 "because rightmost leaf block %llu is empty\n",
1601 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1602 (unsigned long long)right_leaf_bh->b_blocknr);
1603
1604 ocfs2_create_empty_extent(right_el);
1605
1606 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1607 if (ret) {
1608 mlog_errno(ret);
1609 goto out;
1610 }
1611
1612 /* Do the copy now. */
1613 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1614 move_rec = left_el->l_recs[i];
1615 right_el->l_recs[0] = move_rec;
1616
1617 /*
1618 * Clear out the record we just copied and shift everything
1619 * over, leaving an empty extent in the left leaf.
1620 *
1621 * We temporarily subtract from next_free_rec so that the
1622 * shift will lose the tail record (which is now defunct).
1623 */
1624 le16_add_cpu(&left_el->l_next_free_rec, -1);
1625 ocfs2_shift_records_right(left_el);
1626 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1627 le16_add_cpu(&left_el->l_next_free_rec, 1);
1628
1629 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1630 if (ret) {
1631 mlog_errno(ret);
1632 goto out;
1633 }
1634
1635 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1636 subtree_index);
1637
1638out:
1639 return ret;
1640}
1641
1642/*
1643 * Given a full path, determine what cpos value would return us a path
1644 * containing the leaf immediately to the left of the current one.
1645 *
1646 * Will return zero if the path passed in is already the leftmost path.
1647 */
1648static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1649 struct ocfs2_path *path, u32 *cpos)
1650{
1651 int i, j, ret = 0;
1652 u64 blkno;
1653 struct ocfs2_extent_list *el;
1654
Mark Fashehe48edee2007-03-07 16:46:57 -08001655 BUG_ON(path->p_tree_depth == 0);
1656
Mark Fashehdcd05382007-01-16 11:32:23 -08001657 *cpos = 0;
1658
1659 blkno = path_leaf_bh(path)->b_blocknr;
1660
1661 /* Start at the tree node just above the leaf and work our way up. */
1662 i = path->p_tree_depth - 1;
1663 while (i >= 0) {
1664 el = path->p_node[i].el;
1665
1666 /*
1667 * Find the extent record just before the one in our
1668 * path.
1669 */
1670 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1671 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1672 if (j == 0) {
1673 if (i == 0) {
1674 /*
1675 * We've determined that the
1676 * path specified is already
1677 * the leftmost one - return a
1678 * cpos of zero.
1679 */
1680 goto out;
1681 }
1682 /*
1683 * The leftmost record points to our
1684 * leaf - we need to travel up the
1685 * tree one level.
1686 */
1687 goto next_node;
1688 }
1689
1690 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001691 *cpos = *cpos + ocfs2_rec_clusters(el,
1692 &el->l_recs[j - 1]);
1693 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08001694 goto out;
1695 }
1696 }
1697
1698 /*
1699 * If we got here, we never found a valid node where
1700 * the tree indicated one should be.
1701 */
1702 ocfs2_error(sb,
1703 "Invalid extent tree at extent block %llu\n",
1704 (unsigned long long)blkno);
1705 ret = -EROFS;
1706 goto out;
1707
1708next_node:
1709 blkno = path->p_node[i].bh->b_blocknr;
1710 i--;
1711 }
1712
1713out:
1714 return ret;
1715}
1716
Mark Fasheh328d5752007-06-18 10:48:04 -07001717/*
1718 * Extend the transaction by enough credits to complete the rotation,
1719 * and still leave at least the original number of credits allocated
1720 * to this transaction.
1721 */
Mark Fashehdcd05382007-01-16 11:32:23 -08001722static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001723 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08001724 struct ocfs2_path *path)
1725{
Mark Fasheh328d5752007-06-18 10:48:04 -07001726 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08001727
1728 if (handle->h_buffer_credits < credits)
1729 return ocfs2_extend_trans(handle, credits);
1730
1731 return 0;
1732}
1733
1734/*
1735 * Trap the case where we're inserting into the theoretical range past
1736 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1737 * whose cpos is less than ours into the right leaf.
1738 *
1739 * It's only necessary to look at the rightmost record of the left
1740 * leaf because the logic that calls us should ensure that the
1741 * theoretical ranges in the path components above the leaves are
1742 * correct.
1743 */
1744static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1745 u32 insert_cpos)
1746{
1747 struct ocfs2_extent_list *left_el;
1748 struct ocfs2_extent_rec *rec;
1749 int next_free;
1750
1751 left_el = path_leaf_el(left_path);
1752 next_free = le16_to_cpu(left_el->l_next_free_rec);
1753 rec = &left_el->l_recs[next_free - 1];
1754
1755 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1756 return 1;
1757 return 0;
1758}
1759
Mark Fasheh328d5752007-06-18 10:48:04 -07001760static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
1761{
1762 int next_free = le16_to_cpu(el->l_next_free_rec);
1763 unsigned int range;
1764 struct ocfs2_extent_rec *rec;
1765
1766 if (next_free == 0)
1767 return 0;
1768
1769 rec = &el->l_recs[0];
1770 if (ocfs2_is_empty_extent(rec)) {
1771 /* Empty list. */
1772 if (next_free == 1)
1773 return 0;
1774 rec = &el->l_recs[1];
1775 }
1776
1777 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1778 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1779 return 1;
1780 return 0;
1781}
1782
Mark Fashehdcd05382007-01-16 11:32:23 -08001783/*
1784 * Rotate all the records in a btree right one record, starting at insert_cpos.
1785 *
1786 * The path to the rightmost leaf should be passed in.
1787 *
1788 * The array is assumed to be large enough to hold an entire path (tree depth).
1789 *
1790 * Upon succesful return from this function:
1791 *
1792 * - The 'right_path' array will contain a path to the leaf block
1793 * whose range contains e_cpos.
1794 * - That leaf block will have a single empty extent in list index 0.
1795 * - In the case that the rotation requires a post-insert update,
1796 * *ret_left_path will contain a valid path which can be passed to
1797 * ocfs2_insert_path().
1798 */
1799static int ocfs2_rotate_tree_right(struct inode *inode,
1800 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07001801 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08001802 u32 insert_cpos,
1803 struct ocfs2_path *right_path,
1804 struct ocfs2_path **ret_left_path)
1805{
Mark Fasheh328d5752007-06-18 10:48:04 -07001806 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08001807 u32 cpos;
1808 struct ocfs2_path *left_path = NULL;
1809
1810 *ret_left_path = NULL;
1811
1812 left_path = ocfs2_new_path(path_root_bh(right_path),
1813 path_root_el(right_path));
1814 if (!left_path) {
1815 ret = -ENOMEM;
1816 mlog_errno(ret);
1817 goto out;
1818 }
1819
1820 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
1821 if (ret) {
1822 mlog_errno(ret);
1823 goto out;
1824 }
1825
1826 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
1827
1828 /*
1829 * What we want to do here is:
1830 *
1831 * 1) Start with the rightmost path.
1832 *
1833 * 2) Determine a path to the leaf block directly to the left
1834 * of that leaf.
1835 *
1836 * 3) Determine the 'subtree root' - the lowest level tree node
1837 * which contains a path to both leaves.
1838 *
1839 * 4) Rotate the subtree.
1840 *
1841 * 5) Find the next subtree by considering the left path to be
1842 * the new right path.
1843 *
1844 * The check at the top of this while loop also accepts
1845 * insert_cpos == cpos because cpos is only a _theoretical_
1846 * value to get us the left path - insert_cpos might very well
1847 * be filling that hole.
1848 *
1849 * Stop at a cpos of '0' because we either started at the
1850 * leftmost branch (i.e., a tree with one branch and a
1851 * rotation inside of it), or we've gone as far as we can in
1852 * rotating subtrees.
1853 */
1854 while (cpos && insert_cpos <= cpos) {
1855 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
1856 insert_cpos, cpos);
1857
1858 ret = ocfs2_find_path(inode, left_path, cpos);
1859 if (ret) {
1860 mlog_errno(ret);
1861 goto out;
1862 }
1863
1864 mlog_bug_on_msg(path_leaf_bh(left_path) ==
1865 path_leaf_bh(right_path),
1866 "Inode %lu: error during insert of %u "
1867 "(left path cpos %u) results in two identical "
1868 "paths ending at %llu\n",
1869 inode->i_ino, insert_cpos, cpos,
1870 (unsigned long long)
1871 path_leaf_bh(left_path)->b_blocknr);
1872
Mark Fasheh328d5752007-06-18 10:48:04 -07001873 if (split == SPLIT_NONE &&
1874 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08001875 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08001876
1877 /*
1878 * We've rotated the tree as much as we
1879 * should. The rest is up to
1880 * ocfs2_insert_path() to complete, after the
1881 * record insertion. We indicate this
1882 * situation by returning the left path.
1883 *
1884 * The reason we don't adjust the records here
1885 * before the record insert is that an error
1886 * later might break the rule where a parent
1887 * record e_cpos will reflect the actual
1888 * e_cpos of the 1st nonempty record of the
1889 * child list.
1890 */
1891 *ret_left_path = left_path;
1892 goto out_ret_path;
1893 }
1894
1895 start = ocfs2_find_subtree_root(inode, left_path, right_path);
1896
1897 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
1898 start,
1899 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
1900 right_path->p_tree_depth);
1901
1902 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07001903 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08001904 if (ret) {
1905 mlog_errno(ret);
1906 goto out;
1907 }
1908
1909 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
1910 right_path, start);
1911 if (ret) {
1912 mlog_errno(ret);
1913 goto out;
1914 }
1915
Mark Fasheh328d5752007-06-18 10:48:04 -07001916 if (split != SPLIT_NONE &&
1917 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
1918 insert_cpos)) {
1919 /*
1920 * A rotate moves the rightmost left leaf
1921 * record over to the leftmost right leaf
1922 * slot. If we're doing an extent split
1923 * instead of a real insert, then we have to
1924 * check that the extent to be split wasn't
1925 * just moved over. If it was, then we can
1926 * exit here, passing left_path back -
1927 * ocfs2_split_extent() is smart enough to
1928 * search both leaves.
1929 */
1930 *ret_left_path = left_path;
1931 goto out_ret_path;
1932 }
1933
Mark Fashehdcd05382007-01-16 11:32:23 -08001934 /*
1935 * There is no need to re-read the next right path
1936 * as we know that it'll be our current left
1937 * path. Optimize by copying values instead.
1938 */
1939 ocfs2_mv_path(right_path, left_path);
1940
1941 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
1942 &cpos);
1943 if (ret) {
1944 mlog_errno(ret);
1945 goto out;
1946 }
1947 }
1948
1949out:
1950 ocfs2_free_path(left_path);
1951
1952out_ret_path:
1953 return ret;
1954}
1955
Mark Fasheh328d5752007-06-18 10:48:04 -07001956static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
1957 struct ocfs2_path *path)
1958{
1959 int i, idx;
1960 struct ocfs2_extent_rec *rec;
1961 struct ocfs2_extent_list *el;
1962 struct ocfs2_extent_block *eb;
1963 u32 range;
1964
1965 /* Path should always be rightmost. */
1966 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
1967 BUG_ON(eb->h_next_leaf_blk != 0ULL);
1968
1969 el = &eb->h_list;
1970 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
1971 idx = le16_to_cpu(el->l_next_free_rec) - 1;
1972 rec = &el->l_recs[idx];
1973 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
1974
1975 for (i = 0; i < path->p_tree_depth; i++) {
1976 el = path->p_node[i].el;
1977 idx = le16_to_cpu(el->l_next_free_rec) - 1;
1978 rec = &el->l_recs[idx];
1979
1980 rec->e_int_clusters = cpu_to_le32(range);
1981 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
1982
1983 ocfs2_journal_dirty(handle, path->p_node[i].bh);
1984 }
1985}
1986
1987static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
1988 struct ocfs2_cached_dealloc_ctxt *dealloc,
1989 struct ocfs2_path *path, int unlink_start)
1990{
1991 int ret, i;
1992 struct ocfs2_extent_block *eb;
1993 struct ocfs2_extent_list *el;
1994 struct buffer_head *bh;
1995
1996 for(i = unlink_start; i < path_num_items(path); i++) {
1997 bh = path->p_node[i].bh;
1998
1999 eb = (struct ocfs2_extent_block *)bh->b_data;
2000 /*
2001 * Not all nodes might have had their final count
2002 * decremented by the caller - handle this here.
2003 */
2004 el = &eb->h_list;
2005 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2006 mlog(ML_ERROR,
2007 "Inode %llu, attempted to remove extent block "
2008 "%llu with %u records\n",
2009 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2010 (unsigned long long)le64_to_cpu(eb->h_blkno),
2011 le16_to_cpu(el->l_next_free_rec));
2012
2013 ocfs2_journal_dirty(handle, bh);
2014 ocfs2_remove_from_cache(inode, bh);
2015 continue;
2016 }
2017
2018 el->l_next_free_rec = 0;
2019 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2020
2021 ocfs2_journal_dirty(handle, bh);
2022
2023 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2024 if (ret)
2025 mlog_errno(ret);
2026
2027 ocfs2_remove_from_cache(inode, bh);
2028 }
2029}
2030
2031static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2032 struct ocfs2_path *left_path,
2033 struct ocfs2_path *right_path,
2034 int subtree_index,
2035 struct ocfs2_cached_dealloc_ctxt *dealloc)
2036{
2037 int i;
2038 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2039 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2040 struct ocfs2_extent_list *el;
2041 struct ocfs2_extent_block *eb;
2042
2043 el = path_leaf_el(left_path);
2044
2045 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2046
2047 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2048 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2049 break;
2050
2051 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2052
2053 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2054 le16_add_cpu(&root_el->l_next_free_rec, -1);
2055
2056 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2057 eb->h_next_leaf_blk = 0;
2058
2059 ocfs2_journal_dirty(handle, root_bh);
2060 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2061
2062 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2063 subtree_index + 1);
2064}
2065
2066static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2067 struct ocfs2_path *left_path,
2068 struct ocfs2_path *right_path,
2069 int subtree_index,
2070 struct ocfs2_cached_dealloc_ctxt *dealloc,
2071 int *deleted)
2072{
2073 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2074 struct buffer_head *root_bh, *di_bh = path_root_bh(right_path);
2075 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2076 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2077 struct ocfs2_extent_block *eb;
2078
2079 *deleted = 0;
2080
2081 right_leaf_el = path_leaf_el(right_path);
2082 left_leaf_el = path_leaf_el(left_path);
2083 root_bh = left_path->p_node[subtree_index].bh;
2084 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2085
2086 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2087 return 0;
2088
2089 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2090 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2091 /*
2092 * It's legal for us to proceed if the right leaf is
2093 * the rightmost one and it has an empty extent. There
2094 * are two cases to handle - whether the leaf will be
2095 * empty after removal or not. If the leaf isn't empty
2096 * then just remove the empty extent up front. The
2097 * next block will handle empty leaves by flagging
2098 * them for unlink.
2099 *
2100 * Non rightmost leaves will throw -EAGAIN and the
2101 * caller can manually move the subtree and retry.
2102 */
2103
2104 if (eb->h_next_leaf_blk != 0ULL)
2105 return -EAGAIN;
2106
2107 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2108 ret = ocfs2_journal_access(handle, inode,
2109 path_leaf_bh(right_path),
2110 OCFS2_JOURNAL_ACCESS_WRITE);
2111 if (ret) {
2112 mlog_errno(ret);
2113 goto out;
2114 }
2115
2116 ocfs2_remove_empty_extent(right_leaf_el);
2117 } else
2118 right_has_empty = 1;
2119 }
2120
2121 if (eb->h_next_leaf_blk == 0ULL &&
2122 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2123 /*
2124 * We have to update i_last_eb_blk during the meta
2125 * data delete.
2126 */
2127 ret = ocfs2_journal_access(handle, inode, di_bh,
2128 OCFS2_JOURNAL_ACCESS_WRITE);
2129 if (ret) {
2130 mlog_errno(ret);
2131 goto out;
2132 }
2133
2134 del_right_subtree = 1;
2135 }
2136
2137 /*
2138 * Getting here with an empty extent in the right path implies
2139 * that it's the rightmost path and will be deleted.
2140 */
2141 BUG_ON(right_has_empty && !del_right_subtree);
2142
2143 ret = ocfs2_journal_access(handle, inode, root_bh,
2144 OCFS2_JOURNAL_ACCESS_WRITE);
2145 if (ret) {
2146 mlog_errno(ret);
2147 goto out;
2148 }
2149
2150 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2151 ret = ocfs2_journal_access(handle, inode,
2152 right_path->p_node[i].bh,
2153 OCFS2_JOURNAL_ACCESS_WRITE);
2154 if (ret) {
2155 mlog_errno(ret);
2156 goto out;
2157 }
2158
2159 ret = ocfs2_journal_access(handle, inode,
2160 left_path->p_node[i].bh,
2161 OCFS2_JOURNAL_ACCESS_WRITE);
2162 if (ret) {
2163 mlog_errno(ret);
2164 goto out;
2165 }
2166 }
2167
2168 if (!right_has_empty) {
2169 /*
2170 * Only do this if we're moving a real
2171 * record. Otherwise, the action is delayed until
2172 * after removal of the right path in which case we
2173 * can do a simple shift to remove the empty extent.
2174 */
2175 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2176 memset(&right_leaf_el->l_recs[0], 0,
2177 sizeof(struct ocfs2_extent_rec));
2178 }
2179 if (eb->h_next_leaf_blk == 0ULL) {
2180 /*
2181 * Move recs over to get rid of empty extent, decrease
2182 * next_free. This is allowed to remove the last
2183 * extent in our leaf (setting l_next_free_rec to
2184 * zero) - the delete code below won't care.
2185 */
2186 ocfs2_remove_empty_extent(right_leaf_el);
2187 }
2188
2189 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2190 if (ret)
2191 mlog_errno(ret);
2192 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2193 if (ret)
2194 mlog_errno(ret);
2195
2196 if (del_right_subtree) {
2197 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2198 subtree_index, dealloc);
2199 ocfs2_update_edge_lengths(inode, handle, left_path);
2200
2201 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2202 di->i_last_eb_blk = eb->h_blkno;
2203
2204 /*
2205 * Removal of the extent in the left leaf was skipped
2206 * above so we could delete the right path
2207 * 1st.
2208 */
2209 if (right_has_empty)
2210 ocfs2_remove_empty_extent(left_leaf_el);
2211
2212 ret = ocfs2_journal_dirty(handle, di_bh);
2213 if (ret)
2214 mlog_errno(ret);
2215
2216 *deleted = 1;
2217 } else
2218 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2219 subtree_index);
2220
2221out:
2222 return ret;
2223}
2224
2225/*
2226 * Given a full path, determine what cpos value would return us a path
2227 * containing the leaf immediately to the right of the current one.
2228 *
2229 * Will return zero if the path passed in is already the rightmost path.
2230 *
2231 * This looks similar, but is subtly different to
2232 * ocfs2_find_cpos_for_left_leaf().
2233 */
2234static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2235 struct ocfs2_path *path, u32 *cpos)
2236{
2237 int i, j, ret = 0;
2238 u64 blkno;
2239 struct ocfs2_extent_list *el;
2240
2241 *cpos = 0;
2242
2243 if (path->p_tree_depth == 0)
2244 return 0;
2245
2246 blkno = path_leaf_bh(path)->b_blocknr;
2247
2248 /* Start at the tree node just above the leaf and work our way up. */
2249 i = path->p_tree_depth - 1;
2250 while (i >= 0) {
2251 int next_free;
2252
2253 el = path->p_node[i].el;
2254
2255 /*
2256 * Find the extent record just after the one in our
2257 * path.
2258 */
2259 next_free = le16_to_cpu(el->l_next_free_rec);
2260 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2261 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2262 if (j == (next_free - 1)) {
2263 if (i == 0) {
2264 /*
2265 * We've determined that the
2266 * path specified is already
2267 * the rightmost one - return a
2268 * cpos of zero.
2269 */
2270 goto out;
2271 }
2272 /*
2273 * The rightmost record points to our
2274 * leaf - we need to travel up the
2275 * tree one level.
2276 */
2277 goto next_node;
2278 }
2279
2280 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2281 goto out;
2282 }
2283 }
2284
2285 /*
2286 * If we got here, we never found a valid node where
2287 * the tree indicated one should be.
2288 */
2289 ocfs2_error(sb,
2290 "Invalid extent tree at extent block %llu\n",
2291 (unsigned long long)blkno);
2292 ret = -EROFS;
2293 goto out;
2294
2295next_node:
2296 blkno = path->p_node[i].bh->b_blocknr;
2297 i--;
2298 }
2299
2300out:
2301 return ret;
2302}
2303
2304static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2305 handle_t *handle,
2306 struct buffer_head *bh,
2307 struct ocfs2_extent_list *el)
2308{
2309 int ret;
2310
2311 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2312 return 0;
2313
2314 ret = ocfs2_journal_access(handle, inode, bh,
2315 OCFS2_JOURNAL_ACCESS_WRITE);
2316 if (ret) {
2317 mlog_errno(ret);
2318 goto out;
2319 }
2320
2321 ocfs2_remove_empty_extent(el);
2322
2323 ret = ocfs2_journal_dirty(handle, bh);
2324 if (ret)
2325 mlog_errno(ret);
2326
2327out:
2328 return ret;
2329}
2330
2331static int __ocfs2_rotate_tree_left(struct inode *inode,
2332 handle_t *handle, int orig_credits,
2333 struct ocfs2_path *path,
2334 struct ocfs2_cached_dealloc_ctxt *dealloc,
2335 struct ocfs2_path **empty_extent_path)
2336{
2337 int ret, subtree_root, deleted;
2338 u32 right_cpos;
2339 struct ocfs2_path *left_path = NULL;
2340 struct ocfs2_path *right_path = NULL;
2341
2342 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2343
2344 *empty_extent_path = NULL;
2345
2346 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2347 &right_cpos);
2348 if (ret) {
2349 mlog_errno(ret);
2350 goto out;
2351 }
2352
2353 left_path = ocfs2_new_path(path_root_bh(path),
2354 path_root_el(path));
2355 if (!left_path) {
2356 ret = -ENOMEM;
2357 mlog_errno(ret);
2358 goto out;
2359 }
2360
2361 ocfs2_cp_path(left_path, path);
2362
2363 right_path = ocfs2_new_path(path_root_bh(path),
2364 path_root_el(path));
2365 if (!right_path) {
2366 ret = -ENOMEM;
2367 mlog_errno(ret);
2368 goto out;
2369 }
2370
2371 while (right_cpos) {
2372 ret = ocfs2_find_path(inode, right_path, right_cpos);
2373 if (ret) {
2374 mlog_errno(ret);
2375 goto out;
2376 }
2377
2378 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2379 right_path);
2380
2381 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2382 subtree_root,
2383 (unsigned long long)
2384 right_path->p_node[subtree_root].bh->b_blocknr,
2385 right_path->p_tree_depth);
2386
2387 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2388 orig_credits, left_path);
2389 if (ret) {
2390 mlog_errno(ret);
2391 goto out;
2392 }
2393
Mark Fashehe8aed342007-12-03 16:43:01 -08002394 /*
2395 * Caller might still want to make changes to the
2396 * tree root, so re-add it to the journal here.
2397 */
2398 ret = ocfs2_journal_access(handle, inode,
2399 path_root_bh(left_path),
2400 OCFS2_JOURNAL_ACCESS_WRITE);
2401 if (ret) {
2402 mlog_errno(ret);
2403 goto out;
2404 }
2405
Mark Fasheh328d5752007-06-18 10:48:04 -07002406 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2407 right_path, subtree_root,
2408 dealloc, &deleted);
2409 if (ret == -EAGAIN) {
2410 /*
2411 * The rotation has to temporarily stop due to
2412 * the right subtree having an empty
2413 * extent. Pass it back to the caller for a
2414 * fixup.
2415 */
2416 *empty_extent_path = right_path;
2417 right_path = NULL;
2418 goto out;
2419 }
2420 if (ret) {
2421 mlog_errno(ret);
2422 goto out;
2423 }
2424
2425 /*
2426 * The subtree rotate might have removed records on
2427 * the rightmost edge. If so, then rotation is
2428 * complete.
2429 */
2430 if (deleted)
2431 break;
2432
2433 ocfs2_mv_path(left_path, right_path);
2434
2435 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2436 &right_cpos);
2437 if (ret) {
2438 mlog_errno(ret);
2439 goto out;
2440 }
2441 }
2442
2443out:
2444 ocfs2_free_path(right_path);
2445 ocfs2_free_path(left_path);
2446
2447 return ret;
2448}
2449
2450static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2451 struct ocfs2_path *path,
2452 struct ocfs2_cached_dealloc_ctxt *dealloc)
2453{
2454 int ret, subtree_index;
2455 u32 cpos;
2456 struct ocfs2_path *left_path = NULL;
2457 struct ocfs2_dinode *di;
2458 struct ocfs2_extent_block *eb;
2459 struct ocfs2_extent_list *el;
2460
2461 /*
2462 * XXX: This code assumes that the root is an inode, which is
2463 * true for now but may change as tree code gets generic.
2464 */
2465 di = (struct ocfs2_dinode *)path_root_bh(path)->b_data;
2466 if (!OCFS2_IS_VALID_DINODE(di)) {
2467 ret = -EIO;
2468 ocfs2_error(inode->i_sb,
2469 "Inode %llu has invalid path root",
2470 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2471 goto out;
2472 }
2473
2474 /*
2475 * There's two ways we handle this depending on
2476 * whether path is the only existing one.
2477 */
2478 ret = ocfs2_extend_rotate_transaction(handle, 0,
2479 handle->h_buffer_credits,
2480 path);
2481 if (ret) {
2482 mlog_errno(ret);
2483 goto out;
2484 }
2485
2486 ret = ocfs2_journal_access_path(inode, handle, path);
2487 if (ret) {
2488 mlog_errno(ret);
2489 goto out;
2490 }
2491
2492 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2493 if (ret) {
2494 mlog_errno(ret);
2495 goto out;
2496 }
2497
2498 if (cpos) {
2499 /*
2500 * We have a path to the left of this one - it needs
2501 * an update too.
2502 */
2503 left_path = ocfs2_new_path(path_root_bh(path),
2504 path_root_el(path));
2505 if (!left_path) {
2506 ret = -ENOMEM;
2507 mlog_errno(ret);
2508 goto out;
2509 }
2510
2511 ret = ocfs2_find_path(inode, left_path, cpos);
2512 if (ret) {
2513 mlog_errno(ret);
2514 goto out;
2515 }
2516
2517 ret = ocfs2_journal_access_path(inode, handle, left_path);
2518 if (ret) {
2519 mlog_errno(ret);
2520 goto out;
2521 }
2522
2523 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2524
2525 ocfs2_unlink_subtree(inode, handle, left_path, path,
2526 subtree_index, dealloc);
2527 ocfs2_update_edge_lengths(inode, handle, left_path);
2528
2529 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2530 di->i_last_eb_blk = eb->h_blkno;
2531 } else {
2532 /*
2533 * 'path' is also the leftmost path which
2534 * means it must be the only one. This gets
2535 * handled differently because we want to
2536 * revert the inode back to having extents
2537 * in-line.
2538 */
2539 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2540
2541 el = &di->id2.i_list;
2542 el->l_tree_depth = 0;
2543 el->l_next_free_rec = 0;
2544 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2545
2546 di->i_last_eb_blk = 0;
2547 }
2548
2549 ocfs2_journal_dirty(handle, path_root_bh(path));
2550
2551out:
2552 ocfs2_free_path(left_path);
2553 return ret;
2554}
2555
2556/*
2557 * Left rotation of btree records.
2558 *
2559 * In many ways, this is (unsurprisingly) the opposite of right
2560 * rotation. We start at some non-rightmost path containing an empty
2561 * extent in the leaf block. The code works its way to the rightmost
2562 * path by rotating records to the left in every subtree.
2563 *
2564 * This is used by any code which reduces the number of extent records
2565 * in a leaf. After removal, an empty record should be placed in the
2566 * leftmost list position.
2567 *
2568 * This won't handle a length update of the rightmost path records if
2569 * the rightmost tree leaf record is removed so the caller is
2570 * responsible for detecting and correcting that.
2571 */
2572static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2573 struct ocfs2_path *path,
2574 struct ocfs2_cached_dealloc_ctxt *dealloc)
2575{
2576 int ret, orig_credits = handle->h_buffer_credits;
2577 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2578 struct ocfs2_extent_block *eb;
2579 struct ocfs2_extent_list *el;
2580
2581 el = path_leaf_el(path);
2582 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2583 return 0;
2584
2585 if (path->p_tree_depth == 0) {
2586rightmost_no_delete:
2587 /*
2588 * In-inode extents. This is trivially handled, so do
2589 * it up front.
2590 */
2591 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2592 path_leaf_bh(path),
2593 path_leaf_el(path));
2594 if (ret)
2595 mlog_errno(ret);
2596 goto out;
2597 }
2598
2599 /*
2600 * Handle rightmost branch now. There's several cases:
2601 * 1) simple rotation leaving records in there. That's trivial.
2602 * 2) rotation requiring a branch delete - there's no more
2603 * records left. Two cases of this:
2604 * a) There are branches to the left.
2605 * b) This is also the leftmost (the only) branch.
2606 *
2607 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2608 * 2a) we need the left branch so that we can update it with the unlink
2609 * 2b) we need to bring the inode back to inline extents.
2610 */
2611
2612 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2613 el = &eb->h_list;
2614 if (eb->h_next_leaf_blk == 0) {
2615 /*
2616 * This gets a bit tricky if we're going to delete the
2617 * rightmost path. Get the other cases out of the way
2618 * 1st.
2619 */
2620 if (le16_to_cpu(el->l_next_free_rec) > 1)
2621 goto rightmost_no_delete;
2622
2623 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2624 ret = -EIO;
2625 ocfs2_error(inode->i_sb,
2626 "Inode %llu has empty extent block at %llu",
2627 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2628 (unsigned long long)le64_to_cpu(eb->h_blkno));
2629 goto out;
2630 }
2631
2632 /*
2633 * XXX: The caller can not trust "path" any more after
2634 * this as it will have been deleted. What do we do?
2635 *
2636 * In theory the rotate-for-merge code will never get
2637 * here because it'll always ask for a rotate in a
2638 * nonempty list.
2639 */
2640
2641 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2642 dealloc);
2643 if (ret)
2644 mlog_errno(ret);
2645 goto out;
2646 }
2647
2648 /*
2649 * Now we can loop, remembering the path we get from -EAGAIN
2650 * and restarting from there.
2651 */
2652try_rotate:
2653 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2654 dealloc, &restart_path);
2655 if (ret && ret != -EAGAIN) {
2656 mlog_errno(ret);
2657 goto out;
2658 }
2659
2660 while (ret == -EAGAIN) {
2661 tmp_path = restart_path;
2662 restart_path = NULL;
2663
2664 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2665 tmp_path, dealloc,
2666 &restart_path);
2667 if (ret && ret != -EAGAIN) {
2668 mlog_errno(ret);
2669 goto out;
2670 }
2671
2672 ocfs2_free_path(tmp_path);
2673 tmp_path = NULL;
2674
2675 if (ret == 0)
2676 goto try_rotate;
2677 }
2678
2679out:
2680 ocfs2_free_path(tmp_path);
2681 ocfs2_free_path(restart_path);
2682 return ret;
2683}
2684
2685static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2686 int index)
2687{
2688 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2689 unsigned int size;
2690
2691 if (rec->e_leaf_clusters == 0) {
2692 /*
2693 * We consumed all of the merged-from record. An empty
2694 * extent cannot exist anywhere but the 1st array
2695 * position, so move things over if the merged-from
2696 * record doesn't occupy that position.
2697 *
2698 * This creates a new empty extent so the caller
2699 * should be smart enough to have removed any existing
2700 * ones.
2701 */
2702 if (index > 0) {
2703 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2704 size = index * sizeof(struct ocfs2_extent_rec);
2705 memmove(&el->l_recs[1], &el->l_recs[0], size);
2706 }
2707
2708 /*
2709 * Always memset - the caller doesn't check whether it
2710 * created an empty extent, so there could be junk in
2711 * the other fields.
2712 */
2713 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2714 }
2715}
2716
Tao Ma677b9752008-01-30 14:21:05 +08002717static int ocfs2_get_right_path(struct inode *inode,
2718 struct ocfs2_path *left_path,
2719 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002720{
2721 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08002722 u32 right_cpos;
2723 struct ocfs2_path *right_path = NULL;
2724 struct ocfs2_extent_list *left_el;
2725
2726 *ret_right_path = NULL;
2727
2728 /* This function shouldn't be called for non-trees. */
2729 BUG_ON(left_path->p_tree_depth == 0);
2730
2731 left_el = path_leaf_el(left_path);
2732 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2733
2734 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2735 &right_cpos);
2736 if (ret) {
2737 mlog_errno(ret);
2738 goto out;
2739 }
2740
2741 /* This function shouldn't be called for the rightmost leaf. */
2742 BUG_ON(right_cpos == 0);
2743
2744 right_path = ocfs2_new_path(path_root_bh(left_path),
2745 path_root_el(left_path));
2746 if (!right_path) {
2747 ret = -ENOMEM;
2748 mlog_errno(ret);
2749 goto out;
2750 }
2751
2752 ret = ocfs2_find_path(inode, right_path, right_cpos);
2753 if (ret) {
2754 mlog_errno(ret);
2755 goto out;
2756 }
2757
2758 *ret_right_path = right_path;
2759out:
2760 if (ret)
2761 ocfs2_free_path(right_path);
2762 return ret;
2763}
2764
2765/*
2766 * Remove split_rec clusters from the record at index and merge them
2767 * onto the beginning of the record "next" to it.
2768 * For index < l_count - 1, the next means the extent rec at index + 1.
2769 * For index == l_count - 1, the "next" means the 1st extent rec of the
2770 * next extent block.
2771 */
2772static int ocfs2_merge_rec_right(struct inode *inode,
2773 struct ocfs2_path *left_path,
2774 handle_t *handle,
2775 struct ocfs2_extent_rec *split_rec,
2776 int index)
2777{
2778 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07002779 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2780 struct ocfs2_extent_rec *left_rec;
2781 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08002782 struct ocfs2_extent_list *right_el;
2783 struct ocfs2_path *right_path = NULL;
2784 int subtree_index = 0;
2785 struct ocfs2_extent_list *el = path_leaf_el(left_path);
2786 struct buffer_head *bh = path_leaf_bh(left_path);
2787 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002788
2789 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07002790 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08002791
2792 if (index == le16_to_cpu(el->l_next_free_rec - 1) &&
2793 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
2794 /* we meet with a cross extent block merge. */
2795 ret = ocfs2_get_right_path(inode, left_path, &right_path);
2796 if (ret) {
2797 mlog_errno(ret);
2798 goto out;
2799 }
2800
2801 right_el = path_leaf_el(right_path);
2802 next_free = le16_to_cpu(right_el->l_next_free_rec);
2803 BUG_ON(next_free <= 0);
2804 right_rec = &right_el->l_recs[0];
2805 if (ocfs2_is_empty_extent(right_rec)) {
2806 BUG_ON(le16_to_cpu(next_free) <= 1);
2807 right_rec = &right_el->l_recs[1];
2808 }
2809
2810 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
2811 le16_to_cpu(left_rec->e_leaf_clusters) !=
2812 le32_to_cpu(right_rec->e_cpos));
2813
2814 subtree_index = ocfs2_find_subtree_root(inode,
2815 left_path, right_path);
2816
2817 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
2818 handle->h_buffer_credits,
2819 right_path);
2820 if (ret) {
2821 mlog_errno(ret);
2822 goto out;
2823 }
2824
2825 root_bh = left_path->p_node[subtree_index].bh;
2826 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2827
2828 ret = ocfs2_journal_access(handle, inode, root_bh,
2829 OCFS2_JOURNAL_ACCESS_WRITE);
2830 if (ret) {
2831 mlog_errno(ret);
2832 goto out;
2833 }
2834
2835 for (i = subtree_index + 1;
2836 i < path_num_items(right_path); i++) {
2837 ret = ocfs2_journal_access(handle, inode,
2838 right_path->p_node[i].bh,
2839 OCFS2_JOURNAL_ACCESS_WRITE);
2840 if (ret) {
2841 mlog_errno(ret);
2842 goto out;
2843 }
2844
2845 ret = ocfs2_journal_access(handle, inode,
2846 left_path->p_node[i].bh,
2847 OCFS2_JOURNAL_ACCESS_WRITE);
2848 if (ret) {
2849 mlog_errno(ret);
2850 goto out;
2851 }
2852 }
2853
2854 } else {
2855 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
2856 right_rec = &el->l_recs[index + 1];
2857 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002858
2859 ret = ocfs2_journal_access(handle, inode, bh,
2860 OCFS2_JOURNAL_ACCESS_WRITE);
2861 if (ret) {
2862 mlog_errno(ret);
2863 goto out;
2864 }
2865
2866 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
2867
2868 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
2869 le64_add_cpu(&right_rec->e_blkno,
2870 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
2871 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
2872
2873 ocfs2_cleanup_merge(el, index);
2874
2875 ret = ocfs2_journal_dirty(handle, bh);
2876 if (ret)
2877 mlog_errno(ret);
2878
Tao Ma677b9752008-01-30 14:21:05 +08002879 if (right_path) {
2880 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2881 if (ret)
2882 mlog_errno(ret);
2883
2884 ocfs2_complete_edge_insert(inode, handle, left_path,
2885 right_path, subtree_index);
2886 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002887out:
Tao Ma677b9752008-01-30 14:21:05 +08002888 if (right_path)
2889 ocfs2_free_path(right_path);
2890 return ret;
2891}
2892
2893static int ocfs2_get_left_path(struct inode *inode,
2894 struct ocfs2_path *right_path,
2895 struct ocfs2_path **ret_left_path)
2896{
2897 int ret;
2898 u32 left_cpos;
2899 struct ocfs2_path *left_path = NULL;
2900
2901 *ret_left_path = NULL;
2902
2903 /* This function shouldn't be called for non-trees. */
2904 BUG_ON(right_path->p_tree_depth == 0);
2905
2906 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
2907 right_path, &left_cpos);
2908 if (ret) {
2909 mlog_errno(ret);
2910 goto out;
2911 }
2912
2913 /* This function shouldn't be called for the leftmost leaf. */
2914 BUG_ON(left_cpos == 0);
2915
2916 left_path = ocfs2_new_path(path_root_bh(right_path),
2917 path_root_el(right_path));
2918 if (!left_path) {
2919 ret = -ENOMEM;
2920 mlog_errno(ret);
2921 goto out;
2922 }
2923
2924 ret = ocfs2_find_path(inode, left_path, left_cpos);
2925 if (ret) {
2926 mlog_errno(ret);
2927 goto out;
2928 }
2929
2930 *ret_left_path = left_path;
2931out:
2932 if (ret)
2933 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002934 return ret;
2935}
2936
2937/*
2938 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08002939 * onto the tail of the record "before" it.
2940 * For index > 0, the "before" means the extent rec at index - 1.
2941 *
2942 * For index == 0, the "before" means the last record of the previous
2943 * extent block. And there is also a situation that we may need to
2944 * remove the rightmost leaf extent block in the right_path and change
2945 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07002946 */
Tao Ma677b9752008-01-30 14:21:05 +08002947static int ocfs2_merge_rec_left(struct inode *inode,
2948 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002949 handle_t *handle,
2950 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08002951 struct ocfs2_cached_dealloc_ctxt *dealloc,
2952 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07002953{
Tao Ma677b9752008-01-30 14:21:05 +08002954 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07002955 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2956 struct ocfs2_extent_rec *left_rec;
2957 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08002958 struct ocfs2_extent_list *el = path_leaf_el(right_path);
2959 struct buffer_head *bh = path_leaf_bh(right_path);
2960 struct buffer_head *root_bh = NULL;
2961 struct ocfs2_path *left_path = NULL;
2962 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002963
Tao Ma677b9752008-01-30 14:21:05 +08002964 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002965
Mark Fasheh328d5752007-06-18 10:48:04 -07002966 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08002967 if (index == 0) {
2968 /* we meet with a cross extent block merge. */
2969 ret = ocfs2_get_left_path(inode, right_path, &left_path);
2970 if (ret) {
2971 mlog_errno(ret);
2972 goto out;
2973 }
2974
2975 left_el = path_leaf_el(left_path);
2976 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
2977 le16_to_cpu(left_el->l_count));
2978
2979 left_rec = &left_el->l_recs[
2980 le16_to_cpu(left_el->l_next_free_rec) - 1];
2981 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
2982 le16_to_cpu(left_rec->e_leaf_clusters) !=
2983 le32_to_cpu(split_rec->e_cpos));
2984
2985 subtree_index = ocfs2_find_subtree_root(inode,
2986 left_path, right_path);
2987
2988 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
2989 handle->h_buffer_credits,
2990 left_path);
2991 if (ret) {
2992 mlog_errno(ret);
2993 goto out;
2994 }
2995
2996 root_bh = left_path->p_node[subtree_index].bh;
2997 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2998
2999 ret = ocfs2_journal_access(handle, inode, root_bh,
3000 OCFS2_JOURNAL_ACCESS_WRITE);
3001 if (ret) {
3002 mlog_errno(ret);
3003 goto out;
3004 }
3005
3006 for (i = subtree_index + 1;
3007 i < path_num_items(right_path); i++) {
3008 ret = ocfs2_journal_access(handle, inode,
3009 right_path->p_node[i].bh,
3010 OCFS2_JOURNAL_ACCESS_WRITE);
3011 if (ret) {
3012 mlog_errno(ret);
3013 goto out;
3014 }
3015
3016 ret = ocfs2_journal_access(handle, inode,
3017 left_path->p_node[i].bh,
3018 OCFS2_JOURNAL_ACCESS_WRITE);
3019 if (ret) {
3020 mlog_errno(ret);
3021 goto out;
3022 }
3023 }
3024 } else {
3025 left_rec = &el->l_recs[index - 1];
3026 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3027 has_empty_extent = 1;
3028 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003029
3030 ret = ocfs2_journal_access(handle, inode, bh,
3031 OCFS2_JOURNAL_ACCESS_WRITE);
3032 if (ret) {
3033 mlog_errno(ret);
3034 goto out;
3035 }
3036
3037 if (has_empty_extent && index == 1) {
3038 /*
3039 * The easy case - we can just plop the record right in.
3040 */
3041 *left_rec = *split_rec;
3042
3043 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003044 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003045 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003046
3047 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3048 le64_add_cpu(&right_rec->e_blkno,
3049 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3050 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3051
3052 ocfs2_cleanup_merge(el, index);
3053
3054 ret = ocfs2_journal_dirty(handle, bh);
3055 if (ret)
3056 mlog_errno(ret);
3057
Tao Ma677b9752008-01-30 14:21:05 +08003058 if (left_path) {
3059 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3060 if (ret)
3061 mlog_errno(ret);
3062
3063 /*
3064 * In the situation that the right_rec is empty and the extent
3065 * block is empty also, ocfs2_complete_edge_insert can't handle
3066 * it and we need to delete the right extent block.
3067 */
3068 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3069 le16_to_cpu(el->l_next_free_rec) == 1) {
3070
3071 ret = ocfs2_remove_rightmost_path(inode, handle,
3072 right_path, dealloc);
3073 if (ret) {
3074 mlog_errno(ret);
3075 goto out;
3076 }
3077
3078 /* Now the rightmost extent block has been deleted.
3079 * So we use the new rightmost path.
3080 */
3081 ocfs2_mv_path(right_path, left_path);
3082 left_path = NULL;
3083 } else
3084 ocfs2_complete_edge_insert(inode, handle, left_path,
3085 right_path, subtree_index);
3086 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003087out:
Tao Ma677b9752008-01-30 14:21:05 +08003088 if (left_path)
3089 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003090 return ret;
3091}
3092
3093static int ocfs2_try_to_merge_extent(struct inode *inode,
3094 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003095 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003096 int split_index,
3097 struct ocfs2_extent_rec *split_rec,
3098 struct ocfs2_cached_dealloc_ctxt *dealloc,
3099 struct ocfs2_merge_ctxt *ctxt)
3100
3101{
Tao Mao518d7262007-08-28 17:25:35 -07003102 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003103 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003104 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3105
3106 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3107
Tao Mao518d7262007-08-28 17:25:35 -07003108 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3109 /*
3110 * The merge code will need to create an empty
3111 * extent to take the place of the newly
3112 * emptied slot. Remove any pre-existing empty
3113 * extents - having more than one in a leaf is
3114 * illegal.
3115 */
Tao Ma677b9752008-01-30 14:21:05 +08003116 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mao518d7262007-08-28 17:25:35 -07003117 dealloc);
3118 if (ret) {
3119 mlog_errno(ret);
3120 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003121 }
Tao Mao518d7262007-08-28 17:25:35 -07003122 split_index--;
3123 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003124 }
3125
3126 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3127 /*
3128 * Left-right contig implies this.
3129 */
3130 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003131
3132 /*
3133 * Since the leftright insert always covers the entire
3134 * extent, this call will delete the insert record
3135 * entirely, resulting in an empty extent record added to
3136 * the extent block.
3137 *
3138 * Since the adding of an empty extent shifts
3139 * everything back to the right, there's no need to
3140 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003141 *
3142 * When the split_index is zero, we need to merge it to the
3143 * prevoius extent block. It is more efficient and easier
3144 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003145 */
Tao Ma677b9752008-01-30 14:21:05 +08003146 ret = ocfs2_merge_rec_right(inode, path,
3147 handle, split_rec,
3148 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003149 if (ret) {
3150 mlog_errno(ret);
3151 goto out;
3152 }
3153
3154 /*
3155 * We can only get this from logic error above.
3156 */
3157 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3158
Tao Ma677b9752008-01-30 14:21:05 +08003159 /* The merge left us with an empty extent, remove it. */
3160 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003161 if (ret) {
3162 mlog_errno(ret);
3163 goto out;
3164 }
Tao Ma677b9752008-01-30 14:21:05 +08003165
Mark Fasheh328d5752007-06-18 10:48:04 -07003166 rec = &el->l_recs[split_index];
3167
3168 /*
3169 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003170 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003171 */
Tao Ma677b9752008-01-30 14:21:05 +08003172 ret = ocfs2_merge_rec_left(inode, path,
3173 handle, rec,
3174 dealloc,
3175 split_index);
3176
Mark Fasheh328d5752007-06-18 10:48:04 -07003177 if (ret) {
3178 mlog_errno(ret);
3179 goto out;
3180 }
3181
Tao Ma677b9752008-01-30 14:21:05 +08003182 ret = ocfs2_rotate_tree_left(inode, handle, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003183 dealloc);
3184 /*
3185 * Error from this last rotate is not critical, so
3186 * print but don't bubble it up.
3187 */
3188 if (ret)
3189 mlog_errno(ret);
3190 ret = 0;
3191 } else {
3192 /*
3193 * Merge a record to the left or right.
3194 *
3195 * 'contig_type' is relative to the existing record,
3196 * so for example, if we're "right contig", it's to
3197 * the record on the left (hence the left merge).
3198 */
3199 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3200 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003201 path,
3202 handle, split_rec,
3203 dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003204 split_index);
3205 if (ret) {
3206 mlog_errno(ret);
3207 goto out;
3208 }
3209 } else {
3210 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003211 path,
3212 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003213 split_index);
3214 if (ret) {
3215 mlog_errno(ret);
3216 goto out;
3217 }
3218 }
3219
3220 if (ctxt->c_split_covers_rec) {
3221 /*
3222 * The merge may have left an empty extent in
3223 * our leaf. Try to rotate it away.
3224 */
Tao Ma677b9752008-01-30 14:21:05 +08003225 ret = ocfs2_rotate_tree_left(inode, handle, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003226 dealloc);
3227 if (ret)
3228 mlog_errno(ret);
3229 ret = 0;
3230 }
3231 }
3232
3233out:
3234 return ret;
3235}
3236
3237static void ocfs2_subtract_from_rec(struct super_block *sb,
3238 enum ocfs2_split_type split,
3239 struct ocfs2_extent_rec *rec,
3240 struct ocfs2_extent_rec *split_rec)
3241{
3242 u64 len_blocks;
3243
3244 len_blocks = ocfs2_clusters_to_blocks(sb,
3245 le16_to_cpu(split_rec->e_leaf_clusters));
3246
3247 if (split == SPLIT_LEFT) {
3248 /*
3249 * Region is on the left edge of the existing
3250 * record.
3251 */
3252 le32_add_cpu(&rec->e_cpos,
3253 le16_to_cpu(split_rec->e_leaf_clusters));
3254 le64_add_cpu(&rec->e_blkno, len_blocks);
3255 le16_add_cpu(&rec->e_leaf_clusters,
3256 -le16_to_cpu(split_rec->e_leaf_clusters));
3257 } else {
3258 /*
3259 * Region is on the right edge of the existing
3260 * record.
3261 */
3262 le16_add_cpu(&rec->e_leaf_clusters,
3263 -le16_to_cpu(split_rec->e_leaf_clusters));
3264 }
3265}
3266
Mark Fashehdcd05382007-01-16 11:32:23 -08003267/*
3268 * Do the final bits of extent record insertion at the target leaf
3269 * list. If this leaf is part of an allocation tree, it is assumed
3270 * that the tree above has been prepared.
3271 */
3272static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3273 struct ocfs2_extent_list *el,
3274 struct ocfs2_insert_type *insert,
3275 struct inode *inode)
3276{
3277 int i = insert->ins_contig_index;
3278 unsigned int range;
3279 struct ocfs2_extent_rec *rec;
3280
Mark Fashehe48edee2007-03-07 16:46:57 -08003281 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003282
Mark Fasheh328d5752007-06-18 10:48:04 -07003283 if (insert->ins_split != SPLIT_NONE) {
3284 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3285 BUG_ON(i == -1);
3286 rec = &el->l_recs[i];
3287 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3288 insert_rec);
3289 goto rotate;
3290 }
3291
Mark Fashehdcd05382007-01-16 11:32:23 -08003292 /*
3293 * Contiguous insert - either left or right.
3294 */
3295 if (insert->ins_contig != CONTIG_NONE) {
3296 rec = &el->l_recs[i];
3297 if (insert->ins_contig == CONTIG_LEFT) {
3298 rec->e_blkno = insert_rec->e_blkno;
3299 rec->e_cpos = insert_rec->e_cpos;
3300 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003301 le16_add_cpu(&rec->e_leaf_clusters,
3302 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003303 return;
3304 }
3305
3306 /*
3307 * Handle insert into an empty leaf.
3308 */
3309 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3310 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3311 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3312 el->l_recs[0] = *insert_rec;
3313 el->l_next_free_rec = cpu_to_le16(1);
3314 return;
3315 }
3316
3317 /*
3318 * Appending insert.
3319 */
3320 if (insert->ins_appending == APPEND_TAIL) {
3321 i = le16_to_cpu(el->l_next_free_rec) - 1;
3322 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003323 range = le32_to_cpu(rec->e_cpos)
3324 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003325 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3326
3327 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3328 le16_to_cpu(el->l_count),
3329 "inode %lu, depth %u, count %u, next free %u, "
3330 "rec.cpos %u, rec.clusters %u, "
3331 "insert.cpos %u, insert.clusters %u\n",
3332 inode->i_ino,
3333 le16_to_cpu(el->l_tree_depth),
3334 le16_to_cpu(el->l_count),
3335 le16_to_cpu(el->l_next_free_rec),
3336 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003337 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003338 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003339 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003340 i++;
3341 el->l_recs[i] = *insert_rec;
3342 le16_add_cpu(&el->l_next_free_rec, 1);
3343 return;
3344 }
3345
Mark Fasheh328d5752007-06-18 10:48:04 -07003346rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003347 /*
3348 * Ok, we have to rotate.
3349 *
3350 * At this point, it is safe to assume that inserting into an
3351 * empty leaf and appending to a leaf have both been handled
3352 * above.
3353 *
3354 * This leaf needs to have space, either by the empty 1st
3355 * extent record, or by virtue of an l_next_rec < l_count.
3356 */
3357 ocfs2_rotate_leaf(el, insert_rec);
3358}
3359
3360static inline void ocfs2_update_dinode_clusters(struct inode *inode,
3361 struct ocfs2_dinode *di,
3362 u32 clusters)
3363{
3364 le32_add_cpu(&di->i_clusters, clusters);
3365 spin_lock(&OCFS2_I(inode)->ip_lock);
3366 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
3367 spin_unlock(&OCFS2_I(inode)->ip_lock);
3368}
3369
Mark Fasheh328d5752007-06-18 10:48:04 -07003370static void ocfs2_adjust_rightmost_records(struct inode *inode,
3371 handle_t *handle,
3372 struct ocfs2_path *path,
3373 struct ocfs2_extent_rec *insert_rec)
3374{
3375 int ret, i, next_free;
3376 struct buffer_head *bh;
3377 struct ocfs2_extent_list *el;
3378 struct ocfs2_extent_rec *rec;
3379
3380 /*
3381 * Update everything except the leaf block.
3382 */
3383 for (i = 0; i < path->p_tree_depth; i++) {
3384 bh = path->p_node[i].bh;
3385 el = path->p_node[i].el;
3386
3387 next_free = le16_to_cpu(el->l_next_free_rec);
3388 if (next_free == 0) {
3389 ocfs2_error(inode->i_sb,
3390 "Dinode %llu has a bad extent list",
3391 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3392 ret = -EIO;
3393 return;
3394 }
3395
3396 rec = &el->l_recs[next_free - 1];
3397
3398 rec->e_int_clusters = insert_rec->e_cpos;
3399 le32_add_cpu(&rec->e_int_clusters,
3400 le16_to_cpu(insert_rec->e_leaf_clusters));
3401 le32_add_cpu(&rec->e_int_clusters,
3402 -le32_to_cpu(rec->e_cpos));
3403
3404 ret = ocfs2_journal_dirty(handle, bh);
3405 if (ret)
3406 mlog_errno(ret);
3407
3408 }
3409}
3410
Mark Fashehdcd05382007-01-16 11:32:23 -08003411static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3412 struct ocfs2_extent_rec *insert_rec,
3413 struct ocfs2_path *right_path,
3414 struct ocfs2_path **ret_left_path)
3415{
Mark Fasheh328d5752007-06-18 10:48:04 -07003416 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003417 struct ocfs2_extent_list *el;
3418 struct ocfs2_path *left_path = NULL;
3419
3420 *ret_left_path = NULL;
3421
3422 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003423 * This shouldn't happen for non-trees. The extent rec cluster
3424 * count manipulation below only works for interior nodes.
3425 */
3426 BUG_ON(right_path->p_tree_depth == 0);
3427
3428 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003429 * If our appending insert is at the leftmost edge of a leaf,
3430 * then we might need to update the rightmost records of the
3431 * neighboring path.
3432 */
3433 el = path_leaf_el(right_path);
3434 next_free = le16_to_cpu(el->l_next_free_rec);
3435 if (next_free == 0 ||
3436 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3437 u32 left_cpos;
3438
3439 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3440 &left_cpos);
3441 if (ret) {
3442 mlog_errno(ret);
3443 goto out;
3444 }
3445
3446 mlog(0, "Append may need a left path update. cpos: %u, "
3447 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3448 left_cpos);
3449
3450 /*
3451 * No need to worry if the append is already in the
3452 * leftmost leaf.
3453 */
3454 if (left_cpos) {
3455 left_path = ocfs2_new_path(path_root_bh(right_path),
3456 path_root_el(right_path));
3457 if (!left_path) {
3458 ret = -ENOMEM;
3459 mlog_errno(ret);
3460 goto out;
3461 }
3462
3463 ret = ocfs2_find_path(inode, left_path, left_cpos);
3464 if (ret) {
3465 mlog_errno(ret);
3466 goto out;
3467 }
3468
3469 /*
3470 * ocfs2_insert_path() will pass the left_path to the
3471 * journal for us.
3472 */
3473 }
3474 }
3475
3476 ret = ocfs2_journal_access_path(inode, handle, right_path);
3477 if (ret) {
3478 mlog_errno(ret);
3479 goto out;
3480 }
3481
Mark Fasheh328d5752007-06-18 10:48:04 -07003482 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003483
3484 *ret_left_path = left_path;
3485 ret = 0;
3486out:
3487 if (ret != 0)
3488 ocfs2_free_path(left_path);
3489
3490 return ret;
3491}
3492
Mark Fasheh328d5752007-06-18 10:48:04 -07003493static void ocfs2_split_record(struct inode *inode,
3494 struct ocfs2_path *left_path,
3495 struct ocfs2_path *right_path,
3496 struct ocfs2_extent_rec *split_rec,
3497 enum ocfs2_split_type split)
3498{
3499 int index;
3500 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3501 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3502 struct ocfs2_extent_rec *rec, *tmprec;
3503
3504 right_el = path_leaf_el(right_path);;
3505 if (left_path)
3506 left_el = path_leaf_el(left_path);
3507
3508 el = right_el;
3509 insert_el = right_el;
3510 index = ocfs2_search_extent_list(el, cpos);
3511 if (index != -1) {
3512 if (index == 0 && left_path) {
3513 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3514
3515 /*
3516 * This typically means that the record
3517 * started in the left path but moved to the
3518 * right as a result of rotation. We either
3519 * move the existing record to the left, or we
3520 * do the later insert there.
3521 *
3522 * In this case, the left path should always
3523 * exist as the rotate code will have passed
3524 * it back for a post-insert update.
3525 */
3526
3527 if (split == SPLIT_LEFT) {
3528 /*
3529 * It's a left split. Since we know
3530 * that the rotate code gave us an
3531 * empty extent in the left path, we
3532 * can just do the insert there.
3533 */
3534 insert_el = left_el;
3535 } else {
3536 /*
3537 * Right split - we have to move the
3538 * existing record over to the left
3539 * leaf. The insert will be into the
3540 * newly created empty extent in the
3541 * right leaf.
3542 */
3543 tmprec = &right_el->l_recs[index];
3544 ocfs2_rotate_leaf(left_el, tmprec);
3545 el = left_el;
3546
3547 memset(tmprec, 0, sizeof(*tmprec));
3548 index = ocfs2_search_extent_list(left_el, cpos);
3549 BUG_ON(index == -1);
3550 }
3551 }
3552 } else {
3553 BUG_ON(!left_path);
3554 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3555 /*
3556 * Left path is easy - we can just allow the insert to
3557 * happen.
3558 */
3559 el = left_el;
3560 insert_el = left_el;
3561 index = ocfs2_search_extent_list(el, cpos);
3562 BUG_ON(index == -1);
3563 }
3564
3565 rec = &el->l_recs[index];
3566 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3567 ocfs2_rotate_leaf(insert_el, split_rec);
3568}
3569
Mark Fashehdcd05382007-01-16 11:32:23 -08003570/*
3571 * This function only does inserts on an allocation b-tree. For dinode
3572 * lists, ocfs2_insert_at_leaf() is called directly.
3573 *
3574 * right_path is the path we want to do the actual insert
3575 * in. left_path should only be passed in if we need to update that
3576 * portion of the tree after an edge insert.
3577 */
3578static int ocfs2_insert_path(struct inode *inode,
3579 handle_t *handle,
3580 struct ocfs2_path *left_path,
3581 struct ocfs2_path *right_path,
3582 struct ocfs2_extent_rec *insert_rec,
3583 struct ocfs2_insert_type *insert)
3584{
3585 int ret, subtree_index;
3586 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003587
Mark Fashehdcd05382007-01-16 11:32:23 -08003588 if (left_path) {
3589 int credits = handle->h_buffer_credits;
3590
3591 /*
3592 * There's a chance that left_path got passed back to
3593 * us without being accounted for in the
3594 * journal. Extend our transaction here to be sure we
3595 * can change those blocks.
3596 */
3597 credits += left_path->p_tree_depth;
3598
3599 ret = ocfs2_extend_trans(handle, credits);
3600 if (ret < 0) {
3601 mlog_errno(ret);
3602 goto out;
3603 }
3604
3605 ret = ocfs2_journal_access_path(inode, handle, left_path);
3606 if (ret < 0) {
3607 mlog_errno(ret);
3608 goto out;
3609 }
3610 }
3611
Mark Fashehe8aed342007-12-03 16:43:01 -08003612 /*
3613 * Pass both paths to the journal. The majority of inserts
3614 * will be touching all components anyway.
3615 */
3616 ret = ocfs2_journal_access_path(inode, handle, right_path);
3617 if (ret < 0) {
3618 mlog_errno(ret);
3619 goto out;
3620 }
3621
Mark Fasheh328d5752007-06-18 10:48:04 -07003622 if (insert->ins_split != SPLIT_NONE) {
3623 /*
3624 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003625 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003626 * function sort it all out.
3627 */
3628 ocfs2_split_record(inode, left_path, right_path,
3629 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08003630
3631 /*
3632 * Split might have modified either leaf and we don't
3633 * have a guarantee that the later edge insert will
3634 * dirty this for us.
3635 */
3636 if (left_path)
3637 ret = ocfs2_journal_dirty(handle,
3638 path_leaf_bh(left_path));
3639 if (ret)
3640 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07003641 } else
3642 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3643 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08003644
Mark Fashehdcd05382007-01-16 11:32:23 -08003645 ret = ocfs2_journal_dirty(handle, leaf_bh);
3646 if (ret)
3647 mlog_errno(ret);
3648
3649 if (left_path) {
3650 /*
3651 * The rotate code has indicated that we need to fix
3652 * up portions of the tree after the insert.
3653 *
3654 * XXX: Should we extend the transaction here?
3655 */
3656 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3657 right_path);
3658 ocfs2_complete_edge_insert(inode, handle, left_path,
3659 right_path, subtree_index);
3660 }
3661
3662 ret = 0;
3663out:
3664 return ret;
3665}
3666
3667static int ocfs2_do_insert_extent(struct inode *inode,
3668 handle_t *handle,
3669 struct buffer_head *di_bh,
3670 struct ocfs2_extent_rec *insert_rec,
3671 struct ocfs2_insert_type *type)
3672{
3673 int ret, rotate = 0;
3674 u32 cpos;
3675 struct ocfs2_path *right_path = NULL;
3676 struct ocfs2_path *left_path = NULL;
3677 struct ocfs2_dinode *di;
3678 struct ocfs2_extent_list *el;
3679
3680 di = (struct ocfs2_dinode *) di_bh->b_data;
3681 el = &di->id2.i_list;
3682
3683 ret = ocfs2_journal_access(handle, inode, di_bh,
3684 OCFS2_JOURNAL_ACCESS_WRITE);
3685 if (ret) {
3686 mlog_errno(ret);
3687 goto out;
3688 }
3689
3690 if (le16_to_cpu(el->l_tree_depth) == 0) {
3691 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3692 goto out_update_clusters;
3693 }
3694
3695 right_path = ocfs2_new_inode_path(di_bh);
3696 if (!right_path) {
3697 ret = -ENOMEM;
3698 mlog_errno(ret);
3699 goto out;
3700 }
3701
3702 /*
3703 * Determine the path to start with. Rotations need the
3704 * rightmost path, everything else can go directly to the
3705 * target leaf.
3706 */
3707 cpos = le32_to_cpu(insert_rec->e_cpos);
3708 if (type->ins_appending == APPEND_NONE &&
3709 type->ins_contig == CONTIG_NONE) {
3710 rotate = 1;
3711 cpos = UINT_MAX;
3712 }
3713
3714 ret = ocfs2_find_path(inode, right_path, cpos);
3715 if (ret) {
3716 mlog_errno(ret);
3717 goto out;
3718 }
3719
3720 /*
3721 * Rotations and appends need special treatment - they modify
3722 * parts of the tree's above them.
3723 *
3724 * Both might pass back a path immediate to the left of the
3725 * one being inserted to. This will be cause
3726 * ocfs2_insert_path() to modify the rightmost records of
3727 * left_path to account for an edge insert.
3728 *
3729 * XXX: When modifying this code, keep in mind that an insert
3730 * can wind up skipping both of these two special cases...
3731 */
3732 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07003733 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08003734 le32_to_cpu(insert_rec->e_cpos),
3735 right_path, &left_path);
3736 if (ret) {
3737 mlog_errno(ret);
3738 goto out;
3739 }
Mark Fashehe8aed342007-12-03 16:43:01 -08003740
3741 /*
3742 * ocfs2_rotate_tree_right() might have extended the
3743 * transaction without re-journaling our tree root.
3744 */
3745 ret = ocfs2_journal_access(handle, inode, di_bh,
3746 OCFS2_JOURNAL_ACCESS_WRITE);
3747 if (ret) {
3748 mlog_errno(ret);
3749 goto out;
3750 }
Mark Fashehdcd05382007-01-16 11:32:23 -08003751 } else if (type->ins_appending == APPEND_TAIL
3752 && type->ins_contig != CONTIG_LEFT) {
3753 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3754 right_path, &left_path);
3755 if (ret) {
3756 mlog_errno(ret);
3757 goto out;
3758 }
3759 }
3760
3761 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3762 insert_rec, type);
3763 if (ret) {
3764 mlog_errno(ret);
3765 goto out;
3766 }
3767
3768out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07003769 if (type->ins_split == SPLIT_NONE)
3770 ocfs2_update_dinode_clusters(inode, di,
3771 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003772
3773 ret = ocfs2_journal_dirty(handle, di_bh);
3774 if (ret)
3775 mlog_errno(ret);
3776
3777out:
3778 ocfs2_free_path(left_path);
3779 ocfs2_free_path(right_path);
3780
3781 return ret;
3782}
3783
Mark Fasheh328d5752007-06-18 10:48:04 -07003784static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08003785ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003786 struct ocfs2_extent_list *el, int index,
3787 struct ocfs2_extent_rec *split_rec)
3788{
Tao Maad5a4d72008-01-30 14:21:32 +08003789 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07003790 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08003791 u32 left_cpos, right_cpos;
3792 struct ocfs2_extent_rec *rec = NULL;
3793 struct ocfs2_extent_list *new_el;
3794 struct ocfs2_path *left_path = NULL, *right_path = NULL;
3795 struct buffer_head *bh;
3796 struct ocfs2_extent_block *eb;
3797
3798 if (index > 0) {
3799 rec = &el->l_recs[index - 1];
3800 } else if (path->p_tree_depth > 0) {
3801 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3802 path, &left_cpos);
3803 if (status)
3804 goto out;
3805
3806 if (left_cpos != 0) {
3807 left_path = ocfs2_new_path(path_root_bh(path),
3808 path_root_el(path));
3809 if (!left_path)
3810 goto out;
3811
3812 status = ocfs2_find_path(inode, left_path, left_cpos);
3813 if (status)
3814 goto out;
3815
3816 new_el = path_leaf_el(left_path);
3817
3818 if (le16_to_cpu(new_el->l_next_free_rec) !=
3819 le16_to_cpu(new_el->l_count)) {
3820 bh = path_leaf_bh(left_path);
3821 eb = (struct ocfs2_extent_block *)bh->b_data;
3822 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
3823 eb);
3824 goto out;
3825 }
3826 rec = &new_el->l_recs[
3827 le16_to_cpu(new_el->l_next_free_rec) - 1];
3828 }
3829 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003830
3831 /*
3832 * We're careful to check for an empty extent record here -
3833 * the merge code will know what to do if it sees one.
3834 */
Tao Maad5a4d72008-01-30 14:21:32 +08003835 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07003836 if (index == 1 && ocfs2_is_empty_extent(rec)) {
3837 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
3838 ret = CONTIG_RIGHT;
3839 } else {
3840 ret = ocfs2_extent_contig(inode, rec, split_rec);
3841 }
3842 }
3843
Tao Maad5a4d72008-01-30 14:21:32 +08003844 rec = NULL;
3845 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
3846 rec = &el->l_recs[index + 1];
3847 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
3848 path->p_tree_depth > 0) {
3849 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
3850 path, &right_cpos);
3851 if (status)
3852 goto out;
3853
3854 if (right_cpos == 0)
3855 goto out;
3856
3857 right_path = ocfs2_new_path(path_root_bh(path),
3858 path_root_el(path));
3859 if (!right_path)
3860 goto out;
3861
3862 status = ocfs2_find_path(inode, right_path, right_cpos);
3863 if (status)
3864 goto out;
3865
3866 new_el = path_leaf_el(right_path);
3867 rec = &new_el->l_recs[0];
3868 if (ocfs2_is_empty_extent(rec)) {
3869 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
3870 bh = path_leaf_bh(right_path);
3871 eb = (struct ocfs2_extent_block *)bh->b_data;
3872 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
3873 eb);
3874 goto out;
3875 }
3876 rec = &new_el->l_recs[1];
3877 }
3878 }
3879
3880 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07003881 enum ocfs2_contig_type contig_type;
3882
Mark Fasheh328d5752007-06-18 10:48:04 -07003883 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
3884
3885 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
3886 ret = CONTIG_LEFTRIGHT;
3887 else if (ret == CONTIG_NONE)
3888 ret = contig_type;
3889 }
3890
Tao Maad5a4d72008-01-30 14:21:32 +08003891out:
3892 if (left_path)
3893 ocfs2_free_path(left_path);
3894 if (right_path)
3895 ocfs2_free_path(right_path);
3896
Mark Fasheh328d5752007-06-18 10:48:04 -07003897 return ret;
3898}
3899
Mark Fashehdcd05382007-01-16 11:32:23 -08003900static void ocfs2_figure_contig_type(struct inode *inode,
3901 struct ocfs2_insert_type *insert,
3902 struct ocfs2_extent_list *el,
3903 struct ocfs2_extent_rec *insert_rec)
3904{
3905 int i;
3906 enum ocfs2_contig_type contig_type = CONTIG_NONE;
3907
Mark Fashehe48edee2007-03-07 16:46:57 -08003908 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3909
Mark Fashehdcd05382007-01-16 11:32:23 -08003910 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
3911 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
3912 insert_rec);
3913 if (contig_type != CONTIG_NONE) {
3914 insert->ins_contig_index = i;
3915 break;
3916 }
3917 }
3918 insert->ins_contig = contig_type;
3919}
3920
3921/*
3922 * This should only be called against the righmost leaf extent list.
3923 *
3924 * ocfs2_figure_appending_type() will figure out whether we'll have to
3925 * insert at the tail of the rightmost leaf.
3926 *
3927 * This should also work against the dinode list for tree's with 0
3928 * depth. If we consider the dinode list to be the rightmost leaf node
3929 * then the logic here makes sense.
3930 */
3931static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
3932 struct ocfs2_extent_list *el,
3933 struct ocfs2_extent_rec *insert_rec)
3934{
3935 int i;
3936 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
3937 struct ocfs2_extent_rec *rec;
3938
3939 insert->ins_appending = APPEND_NONE;
3940
Mark Fashehe48edee2007-03-07 16:46:57 -08003941 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003942
3943 if (!el->l_next_free_rec)
3944 goto set_tail_append;
3945
3946 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3947 /* Were all records empty? */
3948 if (le16_to_cpu(el->l_next_free_rec) == 1)
3949 goto set_tail_append;
3950 }
3951
3952 i = le16_to_cpu(el->l_next_free_rec) - 1;
3953 rec = &el->l_recs[i];
3954
Mark Fashehe48edee2007-03-07 16:46:57 -08003955 if (cpos >=
3956 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08003957 goto set_tail_append;
3958
3959 return;
3960
3961set_tail_append:
3962 insert->ins_appending = APPEND_TAIL;
3963}
3964
3965/*
3966 * Helper function called at the begining of an insert.
3967 *
3968 * This computes a few things that are commonly used in the process of
3969 * inserting into the btree:
3970 * - Whether the new extent is contiguous with an existing one.
3971 * - The current tree depth.
3972 * - Whether the insert is an appending one.
3973 * - The total # of free records in the tree.
3974 *
3975 * All of the information is stored on the ocfs2_insert_type
3976 * structure.
3977 */
3978static int ocfs2_figure_insert_type(struct inode *inode,
3979 struct buffer_head *di_bh,
3980 struct buffer_head **last_eb_bh,
3981 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07003982 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08003983 struct ocfs2_insert_type *insert)
3984{
3985 int ret;
3986 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3987 struct ocfs2_extent_block *eb;
3988 struct ocfs2_extent_list *el;
3989 struct ocfs2_path *path = NULL;
3990 struct buffer_head *bh = NULL;
3991
Mark Fasheh328d5752007-06-18 10:48:04 -07003992 insert->ins_split = SPLIT_NONE;
3993
Mark Fashehdcd05382007-01-16 11:32:23 -08003994 el = &di->id2.i_list;
3995 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
3996
3997 if (el->l_tree_depth) {
3998 /*
3999 * If we have tree depth, we read in the
4000 * rightmost extent block ahead of time as
4001 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4002 * may want it later.
4003 */
4004 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4005 le64_to_cpu(di->i_last_eb_blk), &bh,
4006 OCFS2_BH_CACHED, inode);
4007 if (ret) {
4008 mlog_exit(ret);
4009 goto out;
4010 }
4011 eb = (struct ocfs2_extent_block *) bh->b_data;
4012 el = &eb->h_list;
4013 }
4014
4015 /*
4016 * Unless we have a contiguous insert, we'll need to know if
4017 * there is room left in our allocation tree for another
4018 * extent record.
4019 *
4020 * XXX: This test is simplistic, we can search for empty
4021 * extent records too.
4022 */
Tao Maoc77534f2007-08-28 17:22:33 -07004023 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004024 le16_to_cpu(el->l_next_free_rec);
4025
4026 if (!insert->ins_tree_depth) {
4027 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
4028 ocfs2_figure_appending_type(insert, el, insert_rec);
4029 return 0;
4030 }
4031
4032 path = ocfs2_new_inode_path(di_bh);
4033 if (!path) {
4034 ret = -ENOMEM;
4035 mlog_errno(ret);
4036 goto out;
4037 }
4038
4039 /*
4040 * In the case that we're inserting past what the tree
4041 * currently accounts for, ocfs2_find_path() will return for
4042 * us the rightmost tree path. This is accounted for below in
4043 * the appending code.
4044 */
4045 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4046 if (ret) {
4047 mlog_errno(ret);
4048 goto out;
4049 }
4050
4051 el = path_leaf_el(path);
4052
4053 /*
4054 * Now that we have the path, there's two things we want to determine:
4055 * 1) Contiguousness (also set contig_index if this is so)
4056 *
4057 * 2) Are we doing an append? We can trivially break this up
4058 * into two types of appends: simple record append, or a
4059 * rotate inside the tail leaf.
4060 */
4061 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
4062
4063 /*
4064 * The insert code isn't quite ready to deal with all cases of
4065 * left contiguousness. Specifically, if it's an insert into
4066 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004067 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004068 * left. For now, just catch that case and fool the layers
4069 * above us. This works just fine for tree_depth == 0, which
4070 * is why we allow that above.
4071 */
4072 if (insert->ins_contig == CONTIG_LEFT &&
4073 insert->ins_contig_index == 0)
4074 insert->ins_contig = CONTIG_NONE;
4075
4076 /*
4077 * Ok, so we can simply compare against last_eb to figure out
4078 * whether the path doesn't exist. This will only happen in
4079 * the case that we're doing a tail append, so maybe we can
4080 * take advantage of that information somehow.
4081 */
4082 if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
4083 /*
4084 * Ok, ocfs2_find_path() returned us the rightmost
4085 * tree path. This might be an appending insert. There are
4086 * two cases:
4087 * 1) We're doing a true append at the tail:
4088 * -This might even be off the end of the leaf
4089 * 2) We're "appending" by rotating in the tail
4090 */
4091 ocfs2_figure_appending_type(insert, el, insert_rec);
4092 }
4093
4094out:
4095 ocfs2_free_path(path);
4096
4097 if (ret == 0)
4098 *last_eb_bh = bh;
4099 else
4100 brelse(bh);
4101 return ret;
4102}
4103
4104/*
4105 * Insert an extent into an inode btree.
4106 *
4107 * The caller needs to update fe->i_clusters
4108 */
Mark Fashehccd979b2005-12-15 14:31:24 -08004109int ocfs2_insert_extent(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07004110 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08004111 struct inode *inode,
4112 struct buffer_head *fe_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08004113 u32 cpos,
Mark Fashehccd979b2005-12-15 14:31:24 -08004114 u64 start_blk,
4115 u32 new_clusters,
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004116 u8 flags,
Mark Fashehccd979b2005-12-15 14:31:24 -08004117 struct ocfs2_alloc_context *meta_ac)
4118{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004119 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004120 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004121 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004122 struct ocfs2_insert_type insert = {0, };
4123 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004124
Mark Fasheh1afc32b2007-09-07 14:46:51 -07004125 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4126
Mark Fashehdcd05382007-01-16 11:32:23 -08004127 mlog(0, "add %u clusters at position %u to inode %llu\n",
4128 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004129
Mark Fashehdcd05382007-01-16 11:32:23 -08004130 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4131 (OCFS2_I(inode)->ip_clusters != cpos),
4132 "Device %s, asking for sparse allocation: inode %llu, "
4133 "cpos %u, clusters %u\n",
4134 osb->dev_str,
4135 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4136 OCFS2_I(inode)->ip_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08004137
Mark Fashehe48edee2007-03-07 16:46:57 -08004138 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004139 rec.e_cpos = cpu_to_le32(cpos);
4140 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004141 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004142 rec.e_flags = flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08004143
Mark Fashehdcd05382007-01-16 11:32:23 -08004144 status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004145 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004146 if (status < 0) {
4147 mlog_errno(status);
4148 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004149 }
4150
Mark Fashehdcd05382007-01-16 11:32:23 -08004151 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4152 "Insert.contig_index: %d, Insert.free_records: %d, "
4153 "Insert.tree_depth: %d\n",
4154 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004155 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004156
Tao Maoc77534f2007-08-28 17:22:33 -07004157 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004158 status = ocfs2_grow_tree(inode, handle, fe_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07004159 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004160 meta_ac);
4161 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004162 mlog_errno(status);
4163 goto bail;
4164 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004165 }
4166
Mark Fashehdcd05382007-01-16 11:32:23 -08004167 /* Finally, we can add clusters. This might rotate the tree for us. */
4168 status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004169 if (status < 0)
4170 mlog_errno(status);
Mark Fasheh83418972007-04-23 18:53:12 -07004171 else
4172 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004173
4174bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08004175 if (last_eb_bh)
4176 brelse(last_eb_bh);
4177
4178 mlog_exit(status);
4179 return status;
4180}
4181
Mark Fasheh328d5752007-06-18 10:48:04 -07004182static void ocfs2_make_right_split_rec(struct super_block *sb,
4183 struct ocfs2_extent_rec *split_rec,
4184 u32 cpos,
4185 struct ocfs2_extent_rec *rec)
4186{
4187 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4188 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4189
4190 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4191
4192 split_rec->e_cpos = cpu_to_le32(cpos);
4193 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4194
4195 split_rec->e_blkno = rec->e_blkno;
4196 le64_add_cpu(&split_rec->e_blkno,
4197 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4198
4199 split_rec->e_flags = rec->e_flags;
4200}
4201
4202static int ocfs2_split_and_insert(struct inode *inode,
4203 handle_t *handle,
4204 struct ocfs2_path *path,
4205 struct buffer_head *di_bh,
4206 struct buffer_head **last_eb_bh,
4207 int split_index,
4208 struct ocfs2_extent_rec *orig_split_rec,
4209 struct ocfs2_alloc_context *meta_ac)
4210{
4211 int ret = 0, depth;
4212 unsigned int insert_range, rec_range, do_leftright = 0;
4213 struct ocfs2_extent_rec tmprec;
4214 struct ocfs2_extent_list *rightmost_el;
4215 struct ocfs2_extent_rec rec;
4216 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4217 struct ocfs2_insert_type insert;
4218 struct ocfs2_extent_block *eb;
4219 struct ocfs2_dinode *di;
4220
4221leftright:
4222 /*
4223 * Store a copy of the record on the stack - it might move
4224 * around as the tree is manipulated below.
4225 */
4226 rec = path_leaf_el(path)->l_recs[split_index];
4227
4228 di = (struct ocfs2_dinode *)di_bh->b_data;
4229 rightmost_el = &di->id2.i_list;
4230
4231 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4232 if (depth) {
4233 BUG_ON(!(*last_eb_bh));
4234 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4235 rightmost_el = &eb->h_list;
4236 }
4237
4238 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4239 le16_to_cpu(rightmost_el->l_count)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004240 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh,
4241 meta_ac);
4242 if (ret) {
4243 mlog_errno(ret);
4244 goto out;
4245 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004246 }
4247
4248 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4249 insert.ins_appending = APPEND_NONE;
4250 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004251 insert.ins_tree_depth = depth;
4252
4253 insert_range = le32_to_cpu(split_rec.e_cpos) +
4254 le16_to_cpu(split_rec.e_leaf_clusters);
4255 rec_range = le32_to_cpu(rec.e_cpos) +
4256 le16_to_cpu(rec.e_leaf_clusters);
4257
4258 if (split_rec.e_cpos == rec.e_cpos) {
4259 insert.ins_split = SPLIT_LEFT;
4260 } else if (insert_range == rec_range) {
4261 insert.ins_split = SPLIT_RIGHT;
4262 } else {
4263 /*
4264 * Left/right split. We fake this as a right split
4265 * first and then make a second pass as a left split.
4266 */
4267 insert.ins_split = SPLIT_RIGHT;
4268
4269 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4270 &rec);
4271
4272 split_rec = tmprec;
4273
4274 BUG_ON(do_leftright);
4275 do_leftright = 1;
4276 }
4277
4278 ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec,
4279 &insert);
4280 if (ret) {
4281 mlog_errno(ret);
4282 goto out;
4283 }
4284
4285 if (do_leftright == 1) {
4286 u32 cpos;
4287 struct ocfs2_extent_list *el;
4288
4289 do_leftright++;
4290 split_rec = *orig_split_rec;
4291
4292 ocfs2_reinit_path(path, 1);
4293
4294 cpos = le32_to_cpu(split_rec.e_cpos);
4295 ret = ocfs2_find_path(inode, path, cpos);
4296 if (ret) {
4297 mlog_errno(ret);
4298 goto out;
4299 }
4300
4301 el = path_leaf_el(path);
4302 split_index = ocfs2_search_extent_list(el, cpos);
4303 goto leftright;
4304 }
4305out:
4306
4307 return ret;
4308}
4309
4310/*
4311 * Mark part or all of the extent record at split_index in the leaf
4312 * pointed to by path as written. This removes the unwritten
4313 * extent flag.
4314 *
4315 * Care is taken to handle contiguousness so as to not grow the tree.
4316 *
4317 * meta_ac is not strictly necessary - we only truly need it if growth
4318 * of the tree is required. All other cases will degrade into a less
4319 * optimal tree layout.
4320 *
4321 * last_eb_bh should be the rightmost leaf block for any inode with a
4322 * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call.
4323 *
4324 * This code is optimized for readability - several passes might be
4325 * made over certain portions of the tree. All of those blocks will
4326 * have been brought into cache (and pinned via the journal), so the
4327 * extra overhead is not expressed in terms of disk reads.
4328 */
4329static int __ocfs2_mark_extent_written(struct inode *inode,
4330 struct buffer_head *di_bh,
4331 handle_t *handle,
4332 struct ocfs2_path *path,
4333 int split_index,
4334 struct ocfs2_extent_rec *split_rec,
4335 struct ocfs2_alloc_context *meta_ac,
4336 struct ocfs2_cached_dealloc_ctxt *dealloc)
4337{
4338 int ret = 0;
4339 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004340 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004341 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4342 struct ocfs2_merge_ctxt ctxt;
4343 struct ocfs2_extent_list *rightmost_el;
4344
Roel Kluin3cf0c502007-10-27 00:20:36 +02004345 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004346 ret = -EIO;
4347 mlog_errno(ret);
4348 goto out;
4349 }
4350
4351 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4352 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4353 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4354 ret = -EIO;
4355 mlog_errno(ret);
4356 goto out;
4357 }
4358
Tao Maad5a4d72008-01-30 14:21:32 +08004359 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004360 split_index,
4361 split_rec);
4362
4363 /*
4364 * The core merge / split code wants to know how much room is
4365 * left in this inodes allocation tree, so we pass the
4366 * rightmost extent list.
4367 */
4368 if (path->p_tree_depth) {
4369 struct ocfs2_extent_block *eb;
4370 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4371
4372 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4373 le64_to_cpu(di->i_last_eb_blk),
4374 &last_eb_bh, OCFS2_BH_CACHED, inode);
4375 if (ret) {
4376 mlog_exit(ret);
4377 goto out;
4378 }
4379
4380 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4381 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4382 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4383 ret = -EROFS;
4384 goto out;
4385 }
4386
4387 rightmost_el = &eb->h_list;
4388 } else
4389 rightmost_el = path_root_el(path);
4390
Mark Fasheh328d5752007-06-18 10:48:04 -07004391 if (rec->e_cpos == split_rec->e_cpos &&
4392 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4393 ctxt.c_split_covers_rec = 1;
4394 else
4395 ctxt.c_split_covers_rec = 0;
4396
4397 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4398
Mark Fasheh015452b2007-09-12 10:21:22 -07004399 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4400 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4401 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004402
4403 if (ctxt.c_contig_type == CONTIG_NONE) {
4404 if (ctxt.c_split_covers_rec)
4405 el->l_recs[split_index] = *split_rec;
4406 else
4407 ret = ocfs2_split_and_insert(inode, handle, path, di_bh,
4408 &last_eb_bh, split_index,
4409 split_rec, meta_ac);
4410 if (ret)
4411 mlog_errno(ret);
4412 } else {
4413 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4414 split_index, split_rec,
4415 dealloc, &ctxt);
4416 if (ret)
4417 mlog_errno(ret);
4418 }
4419
Mark Fasheh328d5752007-06-18 10:48:04 -07004420out:
4421 brelse(last_eb_bh);
4422 return ret;
4423}
4424
4425/*
4426 * Mark the already-existing extent at cpos as written for len clusters.
4427 *
4428 * If the existing extent is larger than the request, initiate a
4429 * split. An attempt will be made at merging with adjacent extents.
4430 *
4431 * The caller is responsible for passing down meta_ac if we'll need it.
4432 */
4433int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
4434 handle_t *handle, u32 cpos, u32 len, u32 phys,
4435 struct ocfs2_alloc_context *meta_ac,
4436 struct ocfs2_cached_dealloc_ctxt *dealloc)
4437{
4438 int ret, index;
4439 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4440 struct ocfs2_extent_rec split_rec;
4441 struct ocfs2_path *left_path = NULL;
4442 struct ocfs2_extent_list *el;
4443
4444 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4445 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4446
4447 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4448 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4449 "that are being written to, but the feature bit "
4450 "is not set in the super block.",
4451 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4452 ret = -EROFS;
4453 goto out;
4454 }
4455
4456 /*
4457 * XXX: This should be fixed up so that we just re-insert the
4458 * next extent records.
4459 */
4460 ocfs2_extent_map_trunc(inode, 0);
4461
4462 left_path = ocfs2_new_inode_path(di_bh);
4463 if (!left_path) {
4464 ret = -ENOMEM;
4465 mlog_errno(ret);
4466 goto out;
4467 }
4468
4469 ret = ocfs2_find_path(inode, left_path, cpos);
4470 if (ret) {
4471 mlog_errno(ret);
4472 goto out;
4473 }
4474 el = path_leaf_el(left_path);
4475
4476 index = ocfs2_search_extent_list(el, cpos);
4477 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4478 ocfs2_error(inode->i_sb,
4479 "Inode %llu has an extent at cpos %u which can no "
4480 "longer be found.\n",
4481 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4482 ret = -EROFS;
4483 goto out;
4484 }
4485
4486 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4487 split_rec.e_cpos = cpu_to_le32(cpos);
4488 split_rec.e_leaf_clusters = cpu_to_le16(len);
4489 split_rec.e_blkno = cpu_to_le64(start_blkno);
4490 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4491 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4492
4493 ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path,
4494 index, &split_rec, meta_ac, dealloc);
4495 if (ret)
4496 mlog_errno(ret);
4497
4498out:
4499 ocfs2_free_path(left_path);
4500 return ret;
4501}
4502
Mark Fashehd0c7d702007-07-03 13:27:22 -07004503static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4504 handle_t *handle, struct ocfs2_path *path,
4505 int index, u32 new_range,
4506 struct ocfs2_alloc_context *meta_ac)
4507{
4508 int ret, depth, credits = handle->h_buffer_credits;
4509 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4510 struct buffer_head *last_eb_bh = NULL;
4511 struct ocfs2_extent_block *eb;
4512 struct ocfs2_extent_list *rightmost_el, *el;
4513 struct ocfs2_extent_rec split_rec;
4514 struct ocfs2_extent_rec *rec;
4515 struct ocfs2_insert_type insert;
4516
4517 /*
4518 * Setup the record to split before we grow the tree.
4519 */
4520 el = path_leaf_el(path);
4521 rec = &el->l_recs[index];
4522 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4523
4524 depth = path->p_tree_depth;
4525 if (depth > 0) {
4526 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4527 le64_to_cpu(di->i_last_eb_blk),
4528 &last_eb_bh, OCFS2_BH_CACHED, inode);
4529 if (ret < 0) {
4530 mlog_errno(ret);
4531 goto out;
4532 }
4533
4534 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4535 rightmost_el = &eb->h_list;
4536 } else
4537 rightmost_el = path_leaf_el(path);
4538
4539 credits += path->p_tree_depth + ocfs2_extend_meta_needed(di);
4540 ret = ocfs2_extend_trans(handle, credits);
4541 if (ret) {
4542 mlog_errno(ret);
4543 goto out;
4544 }
4545
4546 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4547 le16_to_cpu(rightmost_el->l_count)) {
Mark Fashehd0c7d702007-07-03 13:27:22 -07004548 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh,
4549 meta_ac);
4550 if (ret) {
4551 mlog_errno(ret);
4552 goto out;
4553 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07004554 }
4555
4556 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4557 insert.ins_appending = APPEND_NONE;
4558 insert.ins_contig = CONTIG_NONE;
4559 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004560 insert.ins_tree_depth = depth;
4561
4562 ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert);
4563 if (ret)
4564 mlog_errno(ret);
4565
4566out:
4567 brelse(last_eb_bh);
4568 return ret;
4569}
4570
4571static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4572 struct ocfs2_path *path, int index,
4573 struct ocfs2_cached_dealloc_ctxt *dealloc,
4574 u32 cpos, u32 len)
4575{
4576 int ret;
4577 u32 left_cpos, rec_range, trunc_range;
4578 int wants_rotate = 0, is_rightmost_tree_rec = 0;
4579 struct super_block *sb = inode->i_sb;
4580 struct ocfs2_path *left_path = NULL;
4581 struct ocfs2_extent_list *el = path_leaf_el(path);
4582 struct ocfs2_extent_rec *rec;
4583 struct ocfs2_extent_block *eb;
4584
4585 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
4586 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4587 if (ret) {
4588 mlog_errno(ret);
4589 goto out;
4590 }
4591
4592 index--;
4593 }
4594
4595 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
4596 path->p_tree_depth) {
4597 /*
4598 * Check whether this is the rightmost tree record. If
4599 * we remove all of this record or part of its right
4600 * edge then an update of the record lengths above it
4601 * will be required.
4602 */
4603 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
4604 if (eb->h_next_leaf_blk == 0)
4605 is_rightmost_tree_rec = 1;
4606 }
4607
4608 rec = &el->l_recs[index];
4609 if (index == 0 && path->p_tree_depth &&
4610 le32_to_cpu(rec->e_cpos) == cpos) {
4611 /*
4612 * Changing the leftmost offset (via partial or whole
4613 * record truncate) of an interior (or rightmost) path
4614 * means we have to update the subtree that is formed
4615 * by this leaf and the one to it's left.
4616 *
4617 * There are two cases we can skip:
4618 * 1) Path is the leftmost one in our inode tree.
4619 * 2) The leaf is rightmost and will be empty after
4620 * we remove the extent record - the rotate code
4621 * knows how to update the newly formed edge.
4622 */
4623
4624 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
4625 &left_cpos);
4626 if (ret) {
4627 mlog_errno(ret);
4628 goto out;
4629 }
4630
4631 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
4632 left_path = ocfs2_new_path(path_root_bh(path),
4633 path_root_el(path));
4634 if (!left_path) {
4635 ret = -ENOMEM;
4636 mlog_errno(ret);
4637 goto out;
4638 }
4639
4640 ret = ocfs2_find_path(inode, left_path, left_cpos);
4641 if (ret) {
4642 mlog_errno(ret);
4643 goto out;
4644 }
4645 }
4646 }
4647
4648 ret = ocfs2_extend_rotate_transaction(handle, 0,
4649 handle->h_buffer_credits,
4650 path);
4651 if (ret) {
4652 mlog_errno(ret);
4653 goto out;
4654 }
4655
4656 ret = ocfs2_journal_access_path(inode, handle, path);
4657 if (ret) {
4658 mlog_errno(ret);
4659 goto out;
4660 }
4661
4662 ret = ocfs2_journal_access_path(inode, handle, left_path);
4663 if (ret) {
4664 mlog_errno(ret);
4665 goto out;
4666 }
4667
4668 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4669 trunc_range = cpos + len;
4670
4671 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
4672 int next_free;
4673
4674 memset(rec, 0, sizeof(*rec));
4675 ocfs2_cleanup_merge(el, index);
4676 wants_rotate = 1;
4677
4678 next_free = le16_to_cpu(el->l_next_free_rec);
4679 if (is_rightmost_tree_rec && next_free > 1) {
4680 /*
4681 * We skip the edge update if this path will
4682 * be deleted by the rotate code.
4683 */
4684 rec = &el->l_recs[next_free - 1];
4685 ocfs2_adjust_rightmost_records(inode, handle, path,
4686 rec);
4687 }
4688 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
4689 /* Remove leftmost portion of the record. */
4690 le32_add_cpu(&rec->e_cpos, len);
4691 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
4692 le16_add_cpu(&rec->e_leaf_clusters, -len);
4693 } else if (rec_range == trunc_range) {
4694 /* Remove rightmost portion of the record */
4695 le16_add_cpu(&rec->e_leaf_clusters, -len);
4696 if (is_rightmost_tree_rec)
4697 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
4698 } else {
4699 /* Caller should have trapped this. */
4700 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
4701 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
4702 le32_to_cpu(rec->e_cpos),
4703 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
4704 BUG();
4705 }
4706
4707 if (left_path) {
4708 int subtree_index;
4709
4710 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
4711 ocfs2_complete_edge_insert(inode, handle, left_path, path,
4712 subtree_index);
4713 }
4714
4715 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4716
4717 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4718 if (ret) {
4719 mlog_errno(ret);
4720 goto out;
4721 }
4722
4723out:
4724 ocfs2_free_path(left_path);
4725 return ret;
4726}
4727
Mark Fasheh063c4562007-07-03 13:34:11 -07004728int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
4729 u32 cpos, u32 len, handle_t *handle,
4730 struct ocfs2_alloc_context *meta_ac,
4731 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07004732{
4733 int ret, index;
4734 u32 rec_range, trunc_range;
4735 struct ocfs2_extent_rec *rec;
4736 struct ocfs2_extent_list *el;
4737 struct ocfs2_path *path;
4738
4739 ocfs2_extent_map_trunc(inode, 0);
4740
4741 path = ocfs2_new_inode_path(di_bh);
4742 if (!path) {
4743 ret = -ENOMEM;
4744 mlog_errno(ret);
4745 goto out;
4746 }
4747
4748 ret = ocfs2_find_path(inode, path, cpos);
4749 if (ret) {
4750 mlog_errno(ret);
4751 goto out;
4752 }
4753
4754 el = path_leaf_el(path);
4755 index = ocfs2_search_extent_list(el, cpos);
4756 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4757 ocfs2_error(inode->i_sb,
4758 "Inode %llu has an extent at cpos %u which can no "
4759 "longer be found.\n",
4760 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4761 ret = -EROFS;
4762 goto out;
4763 }
4764
4765 /*
4766 * We have 3 cases of extent removal:
4767 * 1) Range covers the entire extent rec
4768 * 2) Range begins or ends on one edge of the extent rec
4769 * 3) Range is in the middle of the extent rec (no shared edges)
4770 *
4771 * For case 1 we remove the extent rec and left rotate to
4772 * fill the hole.
4773 *
4774 * For case 2 we just shrink the existing extent rec, with a
4775 * tree update if the shrinking edge is also the edge of an
4776 * extent block.
4777 *
4778 * For case 3 we do a right split to turn the extent rec into
4779 * something case 2 can handle.
4780 */
4781 rec = &el->l_recs[index];
4782 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4783 trunc_range = cpos + len;
4784
4785 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
4786
4787 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
4788 "(cpos %u, len %u)\n",
4789 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
4790 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
4791
4792 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
4793 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4794 cpos, len);
4795 if (ret) {
4796 mlog_errno(ret);
4797 goto out;
4798 }
4799 } else {
4800 ret = ocfs2_split_tree(inode, di_bh, handle, path, index,
4801 trunc_range, meta_ac);
4802 if (ret) {
4803 mlog_errno(ret);
4804 goto out;
4805 }
4806
4807 /*
4808 * The split could have manipulated the tree enough to
4809 * move the record location, so we have to look for it again.
4810 */
4811 ocfs2_reinit_path(path, 1);
4812
4813 ret = ocfs2_find_path(inode, path, cpos);
4814 if (ret) {
4815 mlog_errno(ret);
4816 goto out;
4817 }
4818
4819 el = path_leaf_el(path);
4820 index = ocfs2_search_extent_list(el, cpos);
4821 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4822 ocfs2_error(inode->i_sb,
4823 "Inode %llu: split at cpos %u lost record.",
4824 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4825 cpos);
4826 ret = -EROFS;
4827 goto out;
4828 }
4829
4830 /*
4831 * Double check our values here. If anything is fishy,
4832 * it's easier to catch it at the top level.
4833 */
4834 rec = &el->l_recs[index];
4835 rec_range = le32_to_cpu(rec->e_cpos) +
4836 ocfs2_rec_clusters(el, rec);
4837 if (rec_range != trunc_range) {
4838 ocfs2_error(inode->i_sb,
4839 "Inode %llu: error after split at cpos %u"
4840 "trunc len %u, existing record is (%u,%u)",
4841 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4842 cpos, len, le32_to_cpu(rec->e_cpos),
4843 ocfs2_rec_clusters(el, rec));
4844 ret = -EROFS;
4845 goto out;
4846 }
4847
4848 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4849 cpos, len);
4850 if (ret) {
4851 mlog_errno(ret);
4852 goto out;
4853 }
4854 }
4855
4856out:
4857 ocfs2_free_path(path);
4858 return ret;
4859}
4860
Mark Fasheh063c4562007-07-03 13:34:11 -07004861int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08004862{
4863 struct buffer_head *tl_bh = osb->osb_tl_bh;
4864 struct ocfs2_dinode *di;
4865 struct ocfs2_truncate_log *tl;
4866
4867 di = (struct ocfs2_dinode *) tl_bh->b_data;
4868 tl = &di->id2.i_dealloc;
4869
4870 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
4871 "slot %d, invalid truncate log parameters: used = "
4872 "%u, count = %u\n", osb->slot_num,
4873 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
4874 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
4875}
4876
4877static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
4878 unsigned int new_start)
4879{
4880 unsigned int tail_index;
4881 unsigned int current_tail;
4882
4883 /* No records, nothing to coalesce */
4884 if (!le16_to_cpu(tl->tl_used))
4885 return 0;
4886
4887 tail_index = le16_to_cpu(tl->tl_used) - 1;
4888 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
4889 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
4890
4891 return current_tail == new_start;
4892}
4893
Mark Fasheh063c4562007-07-03 13:34:11 -07004894int ocfs2_truncate_log_append(struct ocfs2_super *osb,
4895 handle_t *handle,
4896 u64 start_blk,
4897 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08004898{
4899 int status, index;
4900 unsigned int start_cluster, tl_count;
4901 struct inode *tl_inode = osb->osb_tl_inode;
4902 struct buffer_head *tl_bh = osb->osb_tl_bh;
4903 struct ocfs2_dinode *di;
4904 struct ocfs2_truncate_log *tl;
4905
Mark Fashehb0697052006-03-03 10:24:33 -08004906 mlog_entry("start_blk = %llu, num_clusters = %u\n",
4907 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08004908
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004909 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08004910
4911 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
4912
4913 di = (struct ocfs2_dinode *) tl_bh->b_data;
4914 tl = &di->id2.i_dealloc;
4915 if (!OCFS2_IS_VALID_DINODE(di)) {
4916 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
4917 status = -EIO;
4918 goto bail;
4919 }
4920
4921 tl_count = le16_to_cpu(tl->tl_count);
4922 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
4923 tl_count == 0,
Mark Fashehb0697052006-03-03 10:24:33 -08004924 "Truncate record count on #%llu invalid "
4925 "wanted %u, actual %u\n",
4926 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08004927 ocfs2_truncate_recs_per_inode(osb->sb),
4928 le16_to_cpu(tl->tl_count));
4929
4930 /* Caller should have known to flush before calling us. */
4931 index = le16_to_cpu(tl->tl_used);
4932 if (index >= tl_count) {
4933 status = -ENOSPC;
4934 mlog_errno(status);
4935 goto bail;
4936 }
4937
4938 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
4939 OCFS2_JOURNAL_ACCESS_WRITE);
4940 if (status < 0) {
4941 mlog_errno(status);
4942 goto bail;
4943 }
4944
4945 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb0697052006-03-03 10:24:33 -08004946 "%llu (index = %d)\n", num_clusters, start_cluster,
4947 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08004948
4949 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
4950 /*
4951 * Move index back to the record we are coalescing with.
4952 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
4953 */
4954 index--;
4955
4956 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
4957 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
4958 index, le32_to_cpu(tl->tl_recs[index].t_start),
4959 num_clusters);
4960 } else {
4961 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
4962 tl->tl_used = cpu_to_le16(index + 1);
4963 }
4964 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
4965
4966 status = ocfs2_journal_dirty(handle, tl_bh);
4967 if (status < 0) {
4968 mlog_errno(status);
4969 goto bail;
4970 }
4971
4972bail:
4973 mlog_exit(status);
4974 return status;
4975}
4976
4977static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07004978 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08004979 struct inode *data_alloc_inode,
4980 struct buffer_head *data_alloc_bh)
4981{
4982 int status = 0;
4983 int i;
4984 unsigned int num_clusters;
4985 u64 start_blk;
4986 struct ocfs2_truncate_rec rec;
4987 struct ocfs2_dinode *di;
4988 struct ocfs2_truncate_log *tl;
4989 struct inode *tl_inode = osb->osb_tl_inode;
4990 struct buffer_head *tl_bh = osb->osb_tl_bh;
4991
4992 mlog_entry_void();
4993
4994 di = (struct ocfs2_dinode *) tl_bh->b_data;
4995 tl = &di->id2.i_dealloc;
4996 i = le16_to_cpu(tl->tl_used) - 1;
4997 while (i >= 0) {
4998 /* Caller has given us at least enough credits to
4999 * update the truncate log dinode */
5000 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5001 OCFS2_JOURNAL_ACCESS_WRITE);
5002 if (status < 0) {
5003 mlog_errno(status);
5004 goto bail;
5005 }
5006
5007 tl->tl_used = cpu_to_le16(i);
5008
5009 status = ocfs2_journal_dirty(handle, tl_bh);
5010 if (status < 0) {
5011 mlog_errno(status);
5012 goto bail;
5013 }
5014
5015 /* TODO: Perhaps we can calculate the bulk of the
5016 * credits up front rather than extending like
5017 * this. */
5018 status = ocfs2_extend_trans(handle,
5019 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5020 if (status < 0) {
5021 mlog_errno(status);
5022 goto bail;
5023 }
5024
5025 rec = tl->tl_recs[i];
5026 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5027 le32_to_cpu(rec.t_start));
5028 num_clusters = le32_to_cpu(rec.t_clusters);
5029
5030 /* if start_blk is not set, we ignore the record as
5031 * invalid. */
5032 if (start_blk) {
5033 mlog(0, "free record %d, start = %u, clusters = %u\n",
5034 i, le32_to_cpu(rec.t_start), num_clusters);
5035
5036 status = ocfs2_free_clusters(handle, data_alloc_inode,
5037 data_alloc_bh, start_blk,
5038 num_clusters);
5039 if (status < 0) {
5040 mlog_errno(status);
5041 goto bail;
5042 }
5043 }
5044 i--;
5045 }
5046
5047bail:
5048 mlog_exit(status);
5049 return status;
5050}
5051
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005052/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005053int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005054{
5055 int status;
5056 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005057 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005058 struct inode *tl_inode = osb->osb_tl_inode;
5059 struct inode *data_alloc_inode = NULL;
5060 struct buffer_head *tl_bh = osb->osb_tl_bh;
5061 struct buffer_head *data_alloc_bh = NULL;
5062 struct ocfs2_dinode *di;
5063 struct ocfs2_truncate_log *tl;
5064
5065 mlog_entry_void();
5066
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005067 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005068
5069 di = (struct ocfs2_dinode *) tl_bh->b_data;
5070 tl = &di->id2.i_dealloc;
5071 if (!OCFS2_IS_VALID_DINODE(di)) {
5072 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5073 status = -EIO;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005074 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005075 }
5076
5077 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb0697052006-03-03 10:24:33 -08005078 mlog(0, "Flush %u records from truncate log #%llu\n",
5079 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005080 if (!num_to_flush) {
5081 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005082 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005083 }
5084
5085 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5086 GLOBAL_BITMAP_SYSTEM_INODE,
5087 OCFS2_INVALID_SLOT);
5088 if (!data_alloc_inode) {
5089 status = -EINVAL;
5090 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005091 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005092 }
5093
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005094 mutex_lock(&data_alloc_inode->i_mutex);
5095
Mark Fashehe63aecb62007-10-18 15:30:42 -07005096 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005097 if (status < 0) {
5098 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005099 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005100 }
5101
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005102 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005103 if (IS_ERR(handle)) {
5104 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005105 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005106 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005107 }
5108
5109 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5110 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005111 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005112 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005113
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005114 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005115
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005116out_unlock:
5117 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005118 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005119
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005120out_mutex:
5121 mutex_unlock(&data_alloc_inode->i_mutex);
5122 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005123
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005124out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005125 mlog_exit(status);
5126 return status;
5127}
5128
5129int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5130{
5131 int status;
5132 struct inode *tl_inode = osb->osb_tl_inode;
5133
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005134 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005135 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005136 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005137
5138 return status;
5139}
5140
David Howellsc4028952006-11-22 14:57:56 +00005141static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005142{
5143 int status;
David Howellsc4028952006-11-22 14:57:56 +00005144 struct ocfs2_super *osb =
5145 container_of(work, struct ocfs2_super,
5146 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005147
5148 mlog_entry_void();
5149
5150 status = ocfs2_flush_truncate_log(osb);
5151 if (status < 0)
5152 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005153 else
5154 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005155
5156 mlog_exit(status);
5157}
5158
5159#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5160void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5161 int cancel)
5162{
5163 if (osb->osb_tl_inode) {
5164 /* We want to push off log flushes while truncates are
5165 * still running. */
5166 if (cancel)
5167 cancel_delayed_work(&osb->osb_truncate_log_wq);
5168
5169 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5170 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5171 }
5172}
5173
5174static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5175 int slot_num,
5176 struct inode **tl_inode,
5177 struct buffer_head **tl_bh)
5178{
5179 int status;
5180 struct inode *inode = NULL;
5181 struct buffer_head *bh = NULL;
5182
5183 inode = ocfs2_get_system_file_inode(osb,
5184 TRUNCATE_LOG_SYSTEM_INODE,
5185 slot_num);
5186 if (!inode) {
5187 status = -EINVAL;
5188 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5189 goto bail;
5190 }
5191
5192 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5193 OCFS2_BH_CACHED, inode);
5194 if (status < 0) {
5195 iput(inode);
5196 mlog_errno(status);
5197 goto bail;
5198 }
5199
5200 *tl_inode = inode;
5201 *tl_bh = bh;
5202bail:
5203 mlog_exit(status);
5204 return status;
5205}
5206
5207/* called during the 1st stage of node recovery. we stamp a clean
5208 * truncate log and pass back a copy for processing later. if the
5209 * truncate log does not require processing, a *tl_copy is set to
5210 * NULL. */
5211int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5212 int slot_num,
5213 struct ocfs2_dinode **tl_copy)
5214{
5215 int status;
5216 struct inode *tl_inode = NULL;
5217 struct buffer_head *tl_bh = NULL;
5218 struct ocfs2_dinode *di;
5219 struct ocfs2_truncate_log *tl;
5220
5221 *tl_copy = NULL;
5222
5223 mlog(0, "recover truncate log from slot %d\n", slot_num);
5224
5225 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5226 if (status < 0) {
5227 mlog_errno(status);
5228 goto bail;
5229 }
5230
5231 di = (struct ocfs2_dinode *) tl_bh->b_data;
5232 tl = &di->id2.i_dealloc;
5233 if (!OCFS2_IS_VALID_DINODE(di)) {
5234 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5235 status = -EIO;
5236 goto bail;
5237 }
5238
5239 if (le16_to_cpu(tl->tl_used)) {
5240 mlog(0, "We'll have %u logs to recover\n",
5241 le16_to_cpu(tl->tl_used));
5242
5243 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5244 if (!(*tl_copy)) {
5245 status = -ENOMEM;
5246 mlog_errno(status);
5247 goto bail;
5248 }
5249
5250 /* Assuming the write-out below goes well, this copy
5251 * will be passed back to recovery for processing. */
5252 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5253
5254 /* All we need to do to clear the truncate log is set
5255 * tl_used. */
5256 tl->tl_used = 0;
5257
5258 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5259 if (status < 0) {
5260 mlog_errno(status);
5261 goto bail;
5262 }
5263 }
5264
5265bail:
5266 if (tl_inode)
5267 iput(tl_inode);
5268 if (tl_bh)
5269 brelse(tl_bh);
5270
5271 if (status < 0 && (*tl_copy)) {
5272 kfree(*tl_copy);
5273 *tl_copy = NULL;
5274 }
5275
5276 mlog_exit(status);
5277 return status;
5278}
5279
5280int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5281 struct ocfs2_dinode *tl_copy)
5282{
5283 int status = 0;
5284 int i;
5285 unsigned int clusters, num_recs, start_cluster;
5286 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005287 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005288 struct inode *tl_inode = osb->osb_tl_inode;
5289 struct ocfs2_truncate_log *tl;
5290
5291 mlog_entry_void();
5292
5293 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5294 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5295 return -EINVAL;
5296 }
5297
5298 tl = &tl_copy->id2.i_dealloc;
5299 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb0697052006-03-03 10:24:33 -08005300 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07005301 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08005302
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005303 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005304 for(i = 0; i < num_recs; i++) {
5305 if (ocfs2_truncate_log_needs_flush(osb)) {
5306 status = __ocfs2_flush_truncate_log(osb);
5307 if (status < 0) {
5308 mlog_errno(status);
5309 goto bail_up;
5310 }
5311 }
5312
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005313 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005314 if (IS_ERR(handle)) {
5315 status = PTR_ERR(handle);
5316 mlog_errno(status);
5317 goto bail_up;
5318 }
5319
5320 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5321 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5322 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5323
5324 status = ocfs2_truncate_log_append(osb, handle,
5325 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005326 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005327 if (status < 0) {
5328 mlog_errno(status);
5329 goto bail_up;
5330 }
5331 }
5332
5333bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005334 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005335
5336 mlog_exit(status);
5337 return status;
5338}
5339
5340void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5341{
5342 int status;
5343 struct inode *tl_inode = osb->osb_tl_inode;
5344
5345 mlog_entry_void();
5346
5347 if (tl_inode) {
5348 cancel_delayed_work(&osb->osb_truncate_log_wq);
5349 flush_workqueue(ocfs2_wq);
5350
5351 status = ocfs2_flush_truncate_log(osb);
5352 if (status < 0)
5353 mlog_errno(status);
5354
5355 brelse(osb->osb_tl_bh);
5356 iput(osb->osb_tl_inode);
5357 }
5358
5359 mlog_exit_void();
5360}
5361
5362int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5363{
5364 int status;
5365 struct inode *tl_inode = NULL;
5366 struct buffer_head *tl_bh = NULL;
5367
5368 mlog_entry_void();
5369
5370 status = ocfs2_get_truncate_log_info(osb,
5371 osb->slot_num,
5372 &tl_inode,
5373 &tl_bh);
5374 if (status < 0)
5375 mlog_errno(status);
5376
5377 /* ocfs2_truncate_log_shutdown keys on the existence of
5378 * osb->osb_tl_inode so we don't set any of the osb variables
5379 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00005380 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5381 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08005382 osb->osb_tl_bh = tl_bh;
5383 osb->osb_tl_inode = tl_inode;
5384
5385 mlog_exit(status);
5386 return status;
5387}
5388
Mark Fasheh2b604352007-06-22 15:45:27 -07005389/*
5390 * Delayed de-allocation of suballocator blocks.
5391 *
5392 * Some sets of block de-allocations might involve multiple suballocator inodes.
5393 *
5394 * The locking for this can get extremely complicated, especially when
5395 * the suballocator inodes to delete from aren't known until deep
5396 * within an unrelated codepath.
5397 *
5398 * ocfs2_extent_block structures are a good example of this - an inode
5399 * btree could have been grown by any number of nodes each allocating
5400 * out of their own suballoc inode.
5401 *
5402 * These structures allow the delay of block de-allocation until a
5403 * later time, when locking of multiple cluster inodes won't cause
5404 * deadlock.
5405 */
5406
5407/*
5408 * Describes a single block free from a suballocator
5409 */
5410struct ocfs2_cached_block_free {
5411 struct ocfs2_cached_block_free *free_next;
5412 u64 free_blk;
5413 unsigned int free_bit;
5414};
5415
5416struct ocfs2_per_slot_free_list {
5417 struct ocfs2_per_slot_free_list *f_next_suballocator;
5418 int f_inode_type;
5419 int f_slot;
5420 struct ocfs2_cached_block_free *f_first;
5421};
5422
5423static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5424 int sysfile_type,
5425 int slot,
5426 struct ocfs2_cached_block_free *head)
5427{
5428 int ret;
5429 u64 bg_blkno;
5430 handle_t *handle;
5431 struct inode *inode;
5432 struct buffer_head *di_bh = NULL;
5433 struct ocfs2_cached_block_free *tmp;
5434
5435 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5436 if (!inode) {
5437 ret = -EINVAL;
5438 mlog_errno(ret);
5439 goto out;
5440 }
5441
5442 mutex_lock(&inode->i_mutex);
5443
Mark Fashehe63aecb62007-10-18 15:30:42 -07005444 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005445 if (ret) {
5446 mlog_errno(ret);
5447 goto out_mutex;
5448 }
5449
5450 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5451 if (IS_ERR(handle)) {
5452 ret = PTR_ERR(handle);
5453 mlog_errno(ret);
5454 goto out_unlock;
5455 }
5456
5457 while (head) {
5458 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5459 head->free_bit);
5460 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5461 head->free_bit, (unsigned long long)head->free_blk);
5462
5463 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5464 head->free_bit, bg_blkno, 1);
5465 if (ret) {
5466 mlog_errno(ret);
5467 goto out_journal;
5468 }
5469
5470 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5471 if (ret) {
5472 mlog_errno(ret);
5473 goto out_journal;
5474 }
5475
5476 tmp = head;
5477 head = head->free_next;
5478 kfree(tmp);
5479 }
5480
5481out_journal:
5482 ocfs2_commit_trans(osb, handle);
5483
5484out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07005485 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005486 brelse(di_bh);
5487out_mutex:
5488 mutex_unlock(&inode->i_mutex);
5489 iput(inode);
5490out:
5491 while(head) {
5492 /* Premature exit may have left some dangling items. */
5493 tmp = head;
5494 head = head->free_next;
5495 kfree(tmp);
5496 }
5497
5498 return ret;
5499}
5500
5501int ocfs2_run_deallocs(struct ocfs2_super *osb,
5502 struct ocfs2_cached_dealloc_ctxt *ctxt)
5503{
5504 int ret = 0, ret2;
5505 struct ocfs2_per_slot_free_list *fl;
5506
5507 if (!ctxt)
5508 return 0;
5509
5510 while (ctxt->c_first_suballocator) {
5511 fl = ctxt->c_first_suballocator;
5512
5513 if (fl->f_first) {
5514 mlog(0, "Free items: (type %u, slot %d)\n",
5515 fl->f_inode_type, fl->f_slot);
5516 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5517 fl->f_slot, fl->f_first);
5518 if (ret2)
5519 mlog_errno(ret2);
5520 if (!ret)
5521 ret = ret2;
5522 }
5523
5524 ctxt->c_first_suballocator = fl->f_next_suballocator;
5525 kfree(fl);
5526 }
5527
5528 return ret;
5529}
5530
5531static struct ocfs2_per_slot_free_list *
5532ocfs2_find_per_slot_free_list(int type,
5533 int slot,
5534 struct ocfs2_cached_dealloc_ctxt *ctxt)
5535{
5536 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5537
5538 while (fl) {
5539 if (fl->f_inode_type == type && fl->f_slot == slot)
5540 return fl;
5541
5542 fl = fl->f_next_suballocator;
5543 }
5544
5545 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5546 if (fl) {
5547 fl->f_inode_type = type;
5548 fl->f_slot = slot;
5549 fl->f_first = NULL;
5550 fl->f_next_suballocator = ctxt->c_first_suballocator;
5551
5552 ctxt->c_first_suballocator = fl;
5553 }
5554 return fl;
5555}
5556
5557static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
5558 int type, int slot, u64 blkno,
5559 unsigned int bit)
5560{
5561 int ret;
5562 struct ocfs2_per_slot_free_list *fl;
5563 struct ocfs2_cached_block_free *item;
5564
5565 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
5566 if (fl == NULL) {
5567 ret = -ENOMEM;
5568 mlog_errno(ret);
5569 goto out;
5570 }
5571
5572 item = kmalloc(sizeof(*item), GFP_NOFS);
5573 if (item == NULL) {
5574 ret = -ENOMEM;
5575 mlog_errno(ret);
5576 goto out;
5577 }
5578
5579 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
5580 type, slot, bit, (unsigned long long)blkno);
5581
5582 item->free_blk = blkno;
5583 item->free_bit = bit;
5584 item->free_next = fl->f_first;
5585
5586 fl->f_first = item;
5587
5588 ret = 0;
5589out:
5590 return ret;
5591}
5592
Mark Fasheh59a5e412007-06-22 15:52:36 -07005593static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
5594 struct ocfs2_extent_block *eb)
5595{
5596 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
5597 le16_to_cpu(eb->h_suballoc_slot),
5598 le64_to_cpu(eb->h_blkno),
5599 le16_to_cpu(eb->h_suballoc_bit));
5600}
5601
Mark Fashehccd979b2005-12-15 14:31:24 -08005602/* This function will figure out whether the currently last extent
5603 * block will be deleted, and if it will, what the new last extent
5604 * block will be so we can update his h_next_leaf_blk field, as well
5605 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08005606static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005607 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08005608 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08005609 struct buffer_head **new_last_eb)
5610{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005611 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08005612 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005613 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08005614 struct ocfs2_extent_block *eb;
5615 struct ocfs2_extent_list *el;
5616 struct buffer_head *bh = NULL;
5617
5618 *new_last_eb = NULL;
5619
Mark Fashehccd979b2005-12-15 14:31:24 -08005620 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08005621 if (!path->p_tree_depth)
5622 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005623
5624 /* trunc to zero special case - this makes tree_depth = 0
5625 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005626 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08005627 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005628
Mark Fashehdcd05382007-01-16 11:32:23 -08005629 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08005630 BUG_ON(!el->l_next_free_rec);
5631
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005632 /*
5633 * Make sure that this extent list will actually be empty
5634 * after we clear away the data. We can shortcut out if
5635 * there's more than one non-empty extent in the
5636 * list. Otherwise, a check of the remaining extent is
5637 * necessary.
5638 */
5639 next_free = le16_to_cpu(el->l_next_free_rec);
5640 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08005641 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005642 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08005643 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005644
5645 /* We may have a valid extent in index 1, check it. */
5646 if (next_free == 2)
5647 rec = &el->l_recs[1];
5648
5649 /*
5650 * Fall through - no more nonempty extents, so we want
5651 * to delete this leaf.
5652 */
5653 } else {
5654 if (next_free > 1)
5655 goto out;
5656
5657 rec = &el->l_recs[0];
5658 }
5659
5660 if (rec) {
5661 /*
5662 * Check it we'll only be trimming off the end of this
5663 * cluster.
5664 */
Mark Fashehe48edee2007-03-07 16:46:57 -08005665 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005666 goto out;
5667 }
Mark Fashehccd979b2005-12-15 14:31:24 -08005668
Mark Fashehdcd05382007-01-16 11:32:23 -08005669 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
5670 if (ret) {
5671 mlog_errno(ret);
5672 goto out;
5673 }
Mark Fashehccd979b2005-12-15 14:31:24 -08005674
Mark Fashehdcd05382007-01-16 11:32:23 -08005675 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
5676 if (ret) {
5677 mlog_errno(ret);
5678 goto out;
5679 }
Mark Fashehccd979b2005-12-15 14:31:24 -08005680
Mark Fashehdcd05382007-01-16 11:32:23 -08005681 eb = (struct ocfs2_extent_block *) bh->b_data;
5682 el = &eb->h_list;
5683 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
5684 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
5685 ret = -EROFS;
5686 goto out;
5687 }
Mark Fashehccd979b2005-12-15 14:31:24 -08005688
5689 *new_last_eb = bh;
5690 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08005691 mlog(0, "returning block %llu, (cpos: %u)\n",
5692 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
5693out:
5694 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005695
Mark Fashehdcd05382007-01-16 11:32:23 -08005696 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08005697}
5698
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005699/*
5700 * Trim some clusters off the rightmost edge of a tree. Only called
5701 * during truncate.
5702 *
5703 * The caller needs to:
5704 * - start journaling of each path component.
5705 * - compute and fully set up any new last ext block
5706 */
5707static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
5708 handle_t *handle, struct ocfs2_truncate_context *tc,
5709 u32 clusters_to_del, u64 *delete_start)
5710{
5711 int ret, i, index = path->p_tree_depth;
5712 u32 new_edge = 0;
5713 u64 deleted_eb = 0;
5714 struct buffer_head *bh;
5715 struct ocfs2_extent_list *el;
5716 struct ocfs2_extent_rec *rec;
5717
5718 *delete_start = 0;
5719
5720 while (index >= 0) {
5721 bh = path->p_node[index].bh;
5722 el = path->p_node[index].el;
5723
5724 mlog(0, "traveling tree (index = %d, block = %llu)\n",
5725 index, (unsigned long long)bh->b_blocknr);
5726
5727 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
5728
5729 if (index !=
5730 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
5731 ocfs2_error(inode->i_sb,
5732 "Inode %lu has invalid ext. block %llu",
5733 inode->i_ino,
5734 (unsigned long long)bh->b_blocknr);
5735 ret = -EROFS;
5736 goto out;
5737 }
5738
5739find_tail_record:
5740 i = le16_to_cpu(el->l_next_free_rec) - 1;
5741 rec = &el->l_recs[i];
5742
5743 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
5744 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08005745 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005746 (unsigned long long)le64_to_cpu(rec->e_blkno),
5747 le16_to_cpu(el->l_next_free_rec));
5748
Mark Fashehe48edee2007-03-07 16:46:57 -08005749 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005750
5751 if (le16_to_cpu(el->l_tree_depth) == 0) {
5752 /*
5753 * If the leaf block contains a single empty
5754 * extent and no records, we can just remove
5755 * the block.
5756 */
5757 if (i == 0 && ocfs2_is_empty_extent(rec)) {
5758 memset(rec, 0,
5759 sizeof(struct ocfs2_extent_rec));
5760 el->l_next_free_rec = cpu_to_le16(0);
5761
5762 goto delete;
5763 }
5764
5765 /*
5766 * Remove any empty extents by shifting things
5767 * left. That should make life much easier on
5768 * the code below. This condition is rare
5769 * enough that we shouldn't see a performance
5770 * hit.
5771 */
5772 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
5773 le16_add_cpu(&el->l_next_free_rec, -1);
5774
5775 for(i = 0;
5776 i < le16_to_cpu(el->l_next_free_rec); i++)
5777 el->l_recs[i] = el->l_recs[i + 1];
5778
5779 memset(&el->l_recs[i], 0,
5780 sizeof(struct ocfs2_extent_rec));
5781
5782 /*
5783 * We've modified our extent list. The
5784 * simplest way to handle this change
5785 * is to being the search from the
5786 * start again.
5787 */
5788 goto find_tail_record;
5789 }
5790
Mark Fashehe48edee2007-03-07 16:46:57 -08005791 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005792
5793 /*
5794 * We'll use "new_edge" on our way back up the
5795 * tree to know what our rightmost cpos is.
5796 */
Mark Fashehe48edee2007-03-07 16:46:57 -08005797 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005798 new_edge += le32_to_cpu(rec->e_cpos);
5799
5800 /*
5801 * The caller will use this to delete data blocks.
5802 */
5803 *delete_start = le64_to_cpu(rec->e_blkno)
5804 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08005805 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005806
5807 /*
5808 * If it's now empty, remove this record.
5809 */
Mark Fashehe48edee2007-03-07 16:46:57 -08005810 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005811 memset(rec, 0,
5812 sizeof(struct ocfs2_extent_rec));
5813 le16_add_cpu(&el->l_next_free_rec, -1);
5814 }
5815 } else {
5816 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
5817 memset(rec, 0,
5818 sizeof(struct ocfs2_extent_rec));
5819 le16_add_cpu(&el->l_next_free_rec, -1);
5820
5821 goto delete;
5822 }
5823
5824 /* Can this actually happen? */
5825 if (le16_to_cpu(el->l_next_free_rec) == 0)
5826 goto delete;
5827
5828 /*
5829 * We never actually deleted any clusters
5830 * because our leaf was empty. There's no
5831 * reason to adjust the rightmost edge then.
5832 */
5833 if (new_edge == 0)
5834 goto delete;
5835
Mark Fashehe48edee2007-03-07 16:46:57 -08005836 rec->e_int_clusters = cpu_to_le32(new_edge);
5837 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005838 -le32_to_cpu(rec->e_cpos));
5839
5840 /*
5841 * A deleted child record should have been
5842 * caught above.
5843 */
Mark Fashehe48edee2007-03-07 16:46:57 -08005844 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005845 }
5846
5847delete:
5848 ret = ocfs2_journal_dirty(handle, bh);
5849 if (ret) {
5850 mlog_errno(ret);
5851 goto out;
5852 }
5853
5854 mlog(0, "extent list container %llu, after: record %d: "
5855 "(%u, %u, %llu), next = %u.\n",
5856 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08005857 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005858 (unsigned long long)le64_to_cpu(rec->e_blkno),
5859 le16_to_cpu(el->l_next_free_rec));
5860
5861 /*
5862 * We must be careful to only attempt delete of an
5863 * extent block (and not the root inode block).
5864 */
5865 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
5866 struct ocfs2_extent_block *eb =
5867 (struct ocfs2_extent_block *)bh->b_data;
5868
5869 /*
5870 * Save this for use when processing the
5871 * parent block.
5872 */
5873 deleted_eb = le64_to_cpu(eb->h_blkno);
5874
5875 mlog(0, "deleting this extent block.\n");
5876
5877 ocfs2_remove_from_cache(inode, bh);
5878
Mark Fashehe48edee2007-03-07 16:46:57 -08005879 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005880 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
5881 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
5882
Mark Fasheh59a5e412007-06-22 15:52:36 -07005883 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
5884 /* An error here is not fatal. */
5885 if (ret < 0)
5886 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005887 } else {
5888 deleted_eb = 0;
5889 }
5890
5891 index--;
5892 }
5893
5894 ret = 0;
5895out:
5896 return ret;
5897}
5898
Mark Fashehccd979b2005-12-15 14:31:24 -08005899static int ocfs2_do_truncate(struct ocfs2_super *osb,
5900 unsigned int clusters_to_del,
5901 struct inode *inode,
5902 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005903 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08005904 struct ocfs2_truncate_context *tc,
5905 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08005906{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005907 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08005908 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08005909 struct ocfs2_extent_block *last_eb = NULL;
5910 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08005911 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08005912 u64 delete_blk = 0;
5913
5914 fe = (struct ocfs2_dinode *) fe_bh->b_data;
5915
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005916 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08005917 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005918 if (status < 0) {
5919 mlog_errno(status);
5920 goto bail;
5921 }
Mark Fashehccd979b2005-12-15 14:31:24 -08005922
Mark Fashehdcd05382007-01-16 11:32:23 -08005923 /*
5924 * Each component will be touched, so we might as well journal
5925 * here to avoid having to handle errors later.
5926 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005927 status = ocfs2_journal_access_path(inode, handle, path);
5928 if (status < 0) {
5929 mlog_errno(status);
5930 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08005931 }
5932
5933 if (last_eb_bh) {
5934 status = ocfs2_journal_access(handle, inode, last_eb_bh,
5935 OCFS2_JOURNAL_ACCESS_WRITE);
5936 if (status < 0) {
5937 mlog_errno(status);
5938 goto bail;
5939 }
5940
5941 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5942 }
5943
5944 el = &(fe->id2.i_list);
5945
5946 /*
5947 * Lower levels depend on this never happening, but it's best
5948 * to check it up here before changing the tree.
5949 */
Mark Fashehe48edee2007-03-07 16:46:57 -08005950 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08005951 ocfs2_error(inode->i_sb,
5952 "Inode %lu has an empty extent record, depth %u\n",
5953 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005954 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08005955 goto bail;
5956 }
Mark Fashehccd979b2005-12-15 14:31:24 -08005957
5958 spin_lock(&OCFS2_I(inode)->ip_lock);
5959 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
5960 clusters_to_del;
5961 spin_unlock(&OCFS2_I(inode)->ip_lock);
5962 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07005963 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005964
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005965 status = ocfs2_trim_tree(inode, path, handle, tc,
5966 clusters_to_del, &delete_blk);
5967 if (status) {
5968 mlog_errno(status);
5969 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08005970 }
5971
Mark Fashehdcd05382007-01-16 11:32:23 -08005972 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08005973 /* trunc to zero is a special case. */
5974 el->l_tree_depth = 0;
5975 fe->i_last_eb_blk = 0;
5976 } else if (last_eb)
5977 fe->i_last_eb_blk = last_eb->h_blkno;
5978
5979 status = ocfs2_journal_dirty(handle, fe_bh);
5980 if (status < 0) {
5981 mlog_errno(status);
5982 goto bail;
5983 }
5984
5985 if (last_eb) {
5986 /* If there will be a new last extent block, then by
5987 * definition, there cannot be any leaves to the right of
5988 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08005989 last_eb->h_next_leaf_blk = 0;
5990 status = ocfs2_journal_dirty(handle, last_eb_bh);
5991 if (status < 0) {
5992 mlog_errno(status);
5993 goto bail;
5994 }
5995 }
5996
Mark Fasheh3a0782d2007-01-17 12:53:31 -08005997 if (delete_blk) {
5998 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
5999 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006000 if (status < 0) {
6001 mlog_errno(status);
6002 goto bail;
6003 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006004 }
6005 status = 0;
6006bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006007
Mark Fashehccd979b2005-12-15 14:31:24 -08006008 mlog_exit(status);
6009 return status;
6010}
6011
Mark Fasheh60b11392007-02-16 11:46:50 -08006012static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6013{
6014 set_buffer_uptodate(bh);
6015 mark_buffer_dirty(bh);
6016 return 0;
6017}
6018
6019static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6020{
6021 set_buffer_uptodate(bh);
6022 mark_buffer_dirty(bh);
6023 return ocfs2_journal_dirty_data(handle, bh);
6024}
6025
Mark Fasheh1d410a62007-09-07 14:20:45 -07006026static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6027 unsigned int from, unsigned int to,
6028 struct page *page, int zero, u64 *phys)
6029{
6030 int ret, partial = 0;
6031
6032 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6033 if (ret)
6034 mlog_errno(ret);
6035
6036 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006037 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006038
6039 /*
6040 * Need to set the buffers we zero'd into uptodate
6041 * here if they aren't - ocfs2_map_page_blocks()
6042 * might've skipped some
6043 */
6044 if (ocfs2_should_order_data(inode)) {
6045 ret = walk_page_buffers(handle,
6046 page_buffers(page),
6047 from, to, &partial,
6048 ocfs2_ordered_zero_func);
6049 if (ret < 0)
6050 mlog_errno(ret);
6051 } else {
6052 ret = walk_page_buffers(handle, page_buffers(page),
6053 from, to, &partial,
6054 ocfs2_writeback_zero_func);
6055 if (ret < 0)
6056 mlog_errno(ret);
6057 }
6058
6059 if (!partial)
6060 SetPageUptodate(page);
6061
6062 flush_dcache_page(page);
6063}
6064
Mark Fasheh35edec12007-07-06 14:41:18 -07006065static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6066 loff_t end, struct page **pages,
6067 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006068{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006069 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006070 struct page *page;
6071 unsigned int from, to = PAGE_CACHE_SIZE;
6072 struct super_block *sb = inode->i_sb;
6073
6074 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6075
6076 if (numpages == 0)
6077 goto out;
6078
Mark Fasheh35edec12007-07-06 14:41:18 -07006079 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006080 for(i = 0; i < numpages; i++) {
6081 page = pages[i];
6082
Mark Fasheh35edec12007-07-06 14:41:18 -07006083 from = start & (PAGE_CACHE_SIZE - 1);
6084 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6085 to = end & (PAGE_CACHE_SIZE - 1);
6086
Mark Fasheh60b11392007-02-16 11:46:50 -08006087 BUG_ON(from > PAGE_CACHE_SIZE);
6088 BUG_ON(to > PAGE_CACHE_SIZE);
6089
Mark Fasheh1d410a62007-09-07 14:20:45 -07006090 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6091 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006092
Mark Fasheh35edec12007-07-06 14:41:18 -07006093 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006094 }
6095out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006096 if (pages)
6097 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006098}
6099
Mark Fasheh35edec12007-07-06 14:41:18 -07006100static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006101 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006102{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006103 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006104 struct super_block *sb = inode->i_sb;
6105 struct address_space *mapping = inode->i_mapping;
6106 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006107 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006108
Mark Fasheh35edec12007-07-06 14:41:18 -07006109 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006110
Mark Fasheh35edec12007-07-06 14:41:18 -07006111 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6112 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6113
Mark Fasheh1d410a62007-09-07 14:20:45 -07006114 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006115 last_page_bytes = PAGE_ALIGN(end);
6116 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006117 do {
6118 pages[numpages] = grab_cache_page(mapping, index);
6119 if (!pages[numpages]) {
6120 ret = -ENOMEM;
6121 mlog_errno(ret);
6122 goto out;
6123 }
6124
6125 numpages++;
6126 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006127 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006128
6129out:
6130 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006131 if (pages)
6132 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006133 numpages = 0;
6134 }
6135
6136 *num = numpages;
6137
6138 return ret;
6139}
6140
6141/*
6142 * Zero the area past i_size but still within an allocated
6143 * cluster. This avoids exposing nonzero data on subsequent file
6144 * extends.
6145 *
6146 * We need to call this before i_size is updated on the inode because
6147 * otherwise block_write_full_page() will skip writeout of pages past
6148 * i_size. The new_i_size parameter is passed for this reason.
6149 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006150int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6151 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006152{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006153 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006154 struct page **pages = NULL;
6155 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006156 unsigned int ext_flags;
6157 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006158
6159 /*
6160 * File systems which don't support sparse files zero on every
6161 * extend.
6162 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006163 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006164 return 0;
6165
Mark Fasheh1d410a62007-09-07 14:20:45 -07006166 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006167 sizeof(struct page *), GFP_NOFS);
6168 if (pages == NULL) {
6169 ret = -ENOMEM;
6170 mlog_errno(ret);
6171 goto out;
6172 }
6173
Mark Fasheh1d410a62007-09-07 14:20:45 -07006174 if (range_start == range_end)
6175 goto out;
6176
6177 ret = ocfs2_extent_map_get_blocks(inode,
6178 range_start >> sb->s_blocksize_bits,
6179 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006180 if (ret) {
6181 mlog_errno(ret);
6182 goto out;
6183 }
6184
Mark Fasheh1d410a62007-09-07 14:20:45 -07006185 /*
6186 * Tail is a hole, or is marked unwritten. In either case, we
6187 * can count on read and write to return/push zero's.
6188 */
6189 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006190 goto out;
6191
Mark Fasheh1d410a62007-09-07 14:20:45 -07006192 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6193 &numpages);
6194 if (ret) {
6195 mlog_errno(ret);
6196 goto out;
6197 }
6198
Mark Fasheh35edec12007-07-06 14:41:18 -07006199 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6200 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006201
6202 /*
6203 * Initiate writeout of the pages we zero'd here. We don't
6204 * wait on them - the truncate_inode_pages() call later will
6205 * do that for us.
6206 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006207 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6208 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006209 if (ret)
6210 mlog_errno(ret);
6211
6212out:
6213 if (pages)
6214 kfree(pages);
6215
6216 return ret;
6217}
6218
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006219static void ocfs2_zero_dinode_id2(struct inode *inode, struct ocfs2_dinode *di)
6220{
6221 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6222
6223 memset(&di->id2, 0, blocksize - offsetof(struct ocfs2_dinode, id2));
6224}
6225
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006226void ocfs2_dinode_new_extent_list(struct inode *inode,
6227 struct ocfs2_dinode *di)
6228{
6229 ocfs2_zero_dinode_id2(inode, di);
6230 di->id2.i_list.l_tree_depth = 0;
6231 di->id2.i_list.l_next_free_rec = 0;
6232 di->id2.i_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(inode->i_sb));
6233}
6234
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006235void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6236{
6237 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6238 struct ocfs2_inline_data *idata = &di->id2.i_data;
6239
6240 spin_lock(&oi->ip_lock);
6241 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6242 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6243 spin_unlock(&oi->ip_lock);
6244
6245 /*
6246 * We clear the entire i_data structure here so that all
6247 * fields can be properly initialized.
6248 */
6249 ocfs2_zero_dinode_id2(inode, di);
6250
6251 idata->id_count = cpu_to_le16(ocfs2_max_inline_data(inode->i_sb));
6252}
6253
6254int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6255 struct buffer_head *di_bh)
6256{
6257 int ret, i, has_data, num_pages = 0;
6258 handle_t *handle;
6259 u64 uninitialized_var(block);
6260 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6261 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6262 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006263 struct ocfs2_alloc_context *data_ac = NULL;
6264 struct page **pages = NULL;
6265 loff_t end = osb->s_clustersize;
6266
6267 has_data = i_size_read(inode) ? 1 : 0;
6268
6269 if (has_data) {
6270 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6271 sizeof(struct page *), GFP_NOFS);
6272 if (pages == NULL) {
6273 ret = -ENOMEM;
6274 mlog_errno(ret);
6275 goto out;
6276 }
6277
6278 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6279 if (ret) {
6280 mlog_errno(ret);
6281 goto out;
6282 }
6283 }
6284
6285 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6286 if (IS_ERR(handle)) {
6287 ret = PTR_ERR(handle);
6288 mlog_errno(ret);
6289 goto out_unlock;
6290 }
6291
6292 ret = ocfs2_journal_access(handle, inode, di_bh,
6293 OCFS2_JOURNAL_ACCESS_WRITE);
6294 if (ret) {
6295 mlog_errno(ret);
6296 goto out_commit;
6297 }
6298
6299 if (has_data) {
6300 u32 bit_off, num;
6301 unsigned int page_end;
6302 u64 phys;
6303
6304 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6305 &num);
6306 if (ret) {
6307 mlog_errno(ret);
6308 goto out_commit;
6309 }
6310
6311 /*
6312 * Save two copies, one for insert, and one that can
6313 * be changed by ocfs2_map_and_dirty_page() below.
6314 */
6315 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6316
6317 /*
6318 * Non sparse file systems zero on extend, so no need
6319 * to do that now.
6320 */
6321 if (!ocfs2_sparse_alloc(osb) &&
6322 PAGE_CACHE_SIZE < osb->s_clustersize)
6323 end = PAGE_CACHE_SIZE;
6324
6325 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6326 if (ret) {
6327 mlog_errno(ret);
6328 goto out_commit;
6329 }
6330
6331 /*
6332 * This should populate the 1st page for us and mark
6333 * it up to date.
6334 */
6335 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6336 if (ret) {
6337 mlog_errno(ret);
6338 goto out_commit;
6339 }
6340
6341 page_end = PAGE_CACHE_SIZE;
6342 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6343 page_end = osb->s_clustersize;
6344
6345 for (i = 0; i < num_pages; i++)
6346 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6347 pages[i], i > 0, &phys);
6348 }
6349
6350 spin_lock(&oi->ip_lock);
6351 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6352 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6353 spin_unlock(&oi->ip_lock);
6354
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006355 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006356
6357 ocfs2_journal_dirty(handle, di_bh);
6358
6359 if (has_data) {
6360 /*
6361 * An error at this point should be extremely rare. If
6362 * this proves to be false, we could always re-build
6363 * the in-inode data from our pages.
6364 */
6365 ret = ocfs2_insert_extent(osb, handle, inode, di_bh,
6366 0, block, 1, 0, NULL);
6367 if (ret) {
6368 mlog_errno(ret);
6369 goto out_commit;
6370 }
6371
6372 inode->i_blocks = ocfs2_inode_sector_count(inode);
6373 }
6374
6375out_commit:
6376 ocfs2_commit_trans(osb, handle);
6377
6378out_unlock:
6379 if (data_ac)
6380 ocfs2_free_alloc_context(data_ac);
6381
6382out:
6383 if (pages) {
6384 ocfs2_unlock_and_free_pages(pages, num_pages);
6385 kfree(pages);
6386 }
6387
6388 return ret;
6389}
6390
Mark Fashehccd979b2005-12-15 14:31:24 -08006391/*
6392 * It is expected, that by the time you call this function,
6393 * inode->i_size and fe->i_size have been adjusted.
6394 *
6395 * WARNING: This will kfree the truncate context
6396 */
6397int ocfs2_commit_truncate(struct ocfs2_super *osb,
6398 struct inode *inode,
6399 struct buffer_head *fe_bh,
6400 struct ocfs2_truncate_context *tc)
6401{
6402 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006403 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08006404 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006405 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006406 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08006407 struct ocfs2_path *path = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006408
6409 mlog_entry_void();
6410
Mark Fashehdcd05382007-01-16 11:32:23 -08006411 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08006412 i_size_read(inode));
6413
Mark Fashehdcd05382007-01-16 11:32:23 -08006414 path = ocfs2_new_inode_path(fe_bh);
6415 if (!path) {
6416 status = -ENOMEM;
6417 mlog_errno(status);
6418 goto bail;
6419 }
Mark Fasheh83418972007-04-23 18:53:12 -07006420
6421 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6422
Mark Fashehccd979b2005-12-15 14:31:24 -08006423start:
Mark Fashehdcd05382007-01-16 11:32:23 -08006424 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006425 * Check that we still have allocation to delete.
6426 */
6427 if (OCFS2_I(inode)->ip_clusters == 0) {
6428 status = 0;
6429 goto bail;
6430 }
6431
6432 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08006433 * Truncate always works against the rightmost tree branch.
6434 */
6435 status = ocfs2_find_path(inode, path, UINT_MAX);
6436 if (status) {
6437 mlog_errno(status);
6438 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006439 }
6440
Mark Fashehdcd05382007-01-16 11:32:23 -08006441 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6442 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6443
6444 /*
6445 * By now, el will point to the extent list on the bottom most
6446 * portion of this tree. Only the tail record is considered in
6447 * each pass.
6448 *
6449 * We handle the following cases, in order:
6450 * - empty extent: delete the remaining branch
6451 * - remove the entire record
6452 * - remove a partial record
6453 * - no record needs to be removed (truncate has completed)
6454 */
6455 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006456 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6457 ocfs2_error(inode->i_sb,
6458 "Inode %llu has empty extent block at %llu\n",
6459 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6460 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6461 status = -EROFS;
6462 goto bail;
6463 }
6464
Mark Fashehccd979b2005-12-15 14:31:24 -08006465 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08006466 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08006467 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006468 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6469 clusters_to_del = 0;
6470 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006471 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006472 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006473 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08006474 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08006475 new_highest_cpos;
6476 } else {
6477 status = 0;
6478 goto bail;
6479 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006480
Mark Fashehdcd05382007-01-16 11:32:23 -08006481 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6482 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6483
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006484 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006485 tl_sem = 1;
6486 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6487 * record is free for use. If there isn't any, we flush to get
6488 * an empty truncate log. */
6489 if (ocfs2_truncate_log_needs_flush(osb)) {
6490 status = __ocfs2_flush_truncate_log(osb);
6491 if (status < 0) {
6492 mlog_errno(status);
6493 goto bail;
6494 }
6495 }
6496
6497 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006498 (struct ocfs2_dinode *)fe_bh->b_data,
6499 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006500 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08006501 if (IS_ERR(handle)) {
6502 status = PTR_ERR(handle);
6503 handle = NULL;
6504 mlog_errno(status);
6505 goto bail;
6506 }
6507
Mark Fashehdcd05382007-01-16 11:32:23 -08006508 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6509 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006510 if (status < 0) {
6511 mlog_errno(status);
6512 goto bail;
6513 }
6514
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006515 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006516 tl_sem = 0;
6517
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006518 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006519 handle = NULL;
6520
Mark Fashehdcd05382007-01-16 11:32:23 -08006521 ocfs2_reinit_path(path, 1);
6522
6523 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006524 * The check above will catch the case where we've truncated
6525 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08006526 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006527 goto start;
6528
Mark Fashehccd979b2005-12-15 14:31:24 -08006529bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08006530
6531 ocfs2_schedule_truncate_log_flush(osb, 1);
6532
6533 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006534 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006535
6536 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006537 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006538
Mark Fasheh59a5e412007-06-22 15:52:36 -07006539 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
6540
Mark Fashehdcd05382007-01-16 11:32:23 -08006541 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006542
6543 /* This will drop the ext_alloc cluster lock for us */
6544 ocfs2_free_truncate_context(tc);
6545
6546 mlog_exit(status);
6547 return status;
6548}
6549
Mark Fashehccd979b2005-12-15 14:31:24 -08006550/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07006551 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08006552 */
6553int ocfs2_prepare_truncate(struct ocfs2_super *osb,
6554 struct inode *inode,
6555 struct buffer_head *fe_bh,
6556 struct ocfs2_truncate_context **tc)
6557{
Mark Fasheh59a5e412007-06-22 15:52:36 -07006558 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006559 unsigned int new_i_clusters;
6560 struct ocfs2_dinode *fe;
6561 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08006562 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006563
6564 mlog_entry_void();
6565
6566 *tc = NULL;
6567
6568 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
6569 i_size_read(inode));
6570 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6571
6572 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006573 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
6574 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08006575
Robert P. J. Daycd861282006-12-13 00:34:52 -08006576 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08006577 if (!(*tc)) {
6578 status = -ENOMEM;
6579 mlog_errno(status);
6580 goto bail;
6581 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07006582 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08006583
Mark Fashehccd979b2005-12-15 14:31:24 -08006584 if (fe->id2.i_list.l_tree_depth) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006585 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
6586 &last_eb_bh, OCFS2_BH_CACHED, inode);
6587 if (status < 0) {
6588 mlog_errno(status);
6589 goto bail;
6590 }
6591 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6592 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6593 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6594
6595 brelse(last_eb_bh);
6596 status = -EIO;
6597 goto bail;
6598 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006599 }
6600
6601 (*tc)->tc_last_eb_bh = last_eb_bh;
6602
Mark Fashehccd979b2005-12-15 14:31:24 -08006603 status = 0;
6604bail:
6605 if (status < 0) {
6606 if (*tc)
6607 ocfs2_free_truncate_context(*tc);
6608 *tc = NULL;
6609 }
6610 mlog_exit_void();
6611 return status;
6612}
6613
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006614/*
6615 * 'start' is inclusive, 'end' is not.
6616 */
6617int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
6618 unsigned int start, unsigned int end, int trunc)
6619{
6620 int ret;
6621 unsigned int numbytes;
6622 handle_t *handle;
6623 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6624 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6625 struct ocfs2_inline_data *idata = &di->id2.i_data;
6626
6627 if (end > i_size_read(inode))
6628 end = i_size_read(inode);
6629
6630 BUG_ON(start >= end);
6631
6632 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
6633 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
6634 !ocfs2_supports_inline_data(osb)) {
6635 ocfs2_error(inode->i_sb,
6636 "Inline data flags for inode %llu don't agree! "
6637 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
6638 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6639 le16_to_cpu(di->i_dyn_features),
6640 OCFS2_I(inode)->ip_dyn_features,
6641 osb->s_feature_incompat);
6642 ret = -EROFS;
6643 goto out;
6644 }
6645
6646 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
6647 if (IS_ERR(handle)) {
6648 ret = PTR_ERR(handle);
6649 mlog_errno(ret);
6650 goto out;
6651 }
6652
6653 ret = ocfs2_journal_access(handle, inode, di_bh,
6654 OCFS2_JOURNAL_ACCESS_WRITE);
6655 if (ret) {
6656 mlog_errno(ret);
6657 goto out_commit;
6658 }
6659
6660 numbytes = end - start;
6661 memset(idata->id_data + start, 0, numbytes);
6662
6663 /*
6664 * No need to worry about the data page here - it's been
6665 * truncated already and inline data doesn't need it for
6666 * pushing zero's to disk, so we'll let readpage pick it up
6667 * later.
6668 */
6669 if (trunc) {
6670 i_size_write(inode, start);
6671 di->i_size = cpu_to_le64(start);
6672 }
6673
6674 inode->i_blocks = ocfs2_inode_sector_count(inode);
6675 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
6676
6677 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
6678 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
6679
6680 ocfs2_journal_dirty(handle, di_bh);
6681
6682out_commit:
6683 ocfs2_commit_trans(osb, handle);
6684
6685out:
6686 return ret;
6687}
6688
Mark Fashehccd979b2005-12-15 14:31:24 -08006689static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
6690{
Mark Fasheh59a5e412007-06-22 15:52:36 -07006691 /*
6692 * The caller is responsible for completing deallocation
6693 * before freeing the context.
6694 */
6695 if (tc->tc_dealloc.c_first_suballocator != NULL)
6696 mlog(ML_NOTICE,
6697 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08006698
6699 if (tc->tc_last_eb_bh)
6700 brelse(tc->tc_last_eb_bh);
6701
6702 kfree(tc);
6703}