Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[linux-2.6] / fs / ocfs2 / alloc.c
1 /* -*- 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>
30 #include <linux/swap.h>
31
32 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
33 #include <cluster/masklog.h>
34
35 #include "ocfs2.h"
36
37 #include "alloc.h"
38 #include "aops.h"
39 #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
52 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
53 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
54                                          struct ocfs2_extent_block *eb);
55
56 /*
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  */
63 struct ocfs2_path_item {
64         struct buffer_head              *bh;
65         struct ocfs2_extent_list        *el;
66 };
67
68 #define OCFS2_MAX_PATH_DEPTH    5
69
70 struct 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  */
86 static 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
113 static void ocfs2_free_path(struct ocfs2_path *path)
114 {
115         if (path) {
116                 ocfs2_reinit_path(path, 0);
117                 kfree(path);
118         }
119 }
120
121 /*
122  * 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  */
128 static 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 /*
147  * Make the *dest path the same as src and re-initialize src path to
148  * have a root only.
149  */
150 static 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  */
172 static 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
189 static 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  */
210 static 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  */
221 static 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
238 out:
239         return ret;
240 }
241
242 /*
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  */
248 int 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
272 enum ocfs2_contig_type {
273         CONTIG_NONE = 0,
274         CONTIG_LEFT,
275         CONTIG_RIGHT,
276         CONTIG_LEFTRIGHT,
277 };
278
279
280 /*
281  * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
282  * ocfs2_extent_contig only work properly against leaf nodes!
283  */
284 static int ocfs2_block_extent_contig(struct super_block *sb,
285                                      struct ocfs2_extent_rec *ext,
286                                      u64 blkno)
287 {
288         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;
294 }
295
296 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
297                                   struct ocfs2_extent_rec *right)
298 {
299         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));
305 }
306
307 static 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
314         /*
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
322         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  */
341 enum ocfs2_append_type {
342         APPEND_NONE = 0,
343         APPEND_TAIL,
344 };
345
346 enum ocfs2_split_type {
347         SPLIT_NONE = 0,
348         SPLIT_LEFT,
349         SPLIT_RIGHT,
350 };
351
352 struct ocfs2_insert_type {
353         enum ocfs2_split_type   ins_split;
354         enum ocfs2_append_type  ins_appending;
355         enum ocfs2_contig_type  ins_contig;
356         int                     ins_contig_index;
357         int                     ins_free_records;
358         int                     ins_tree_depth;
359 };
360
361 struct ocfs2_merge_ctxt {
362         enum ocfs2_contig_type  c_contig_type;
363         int                     c_has_empty_extent;
364         int                     c_split_covers_rec;
365         int                     c_used_tail_recs;
366 };
367
368 /*
369  * How many free extents have we got before we need more meta data?
370  */
371 int ocfs2_num_free_extents(struct ocfs2_super *osb,
372                            struct inode *inode,
373                            struct ocfs2_dinode *fe)
374 {
375         int retval;
376         struct ocfs2_extent_list *el;
377         struct ocfs2_extent_block *eb;
378         struct buffer_head *eb_bh = NULL;
379
380         mlog_entry_void();
381
382         if (!OCFS2_IS_VALID_DINODE(fe)) {
383                 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
384                 retval = -EIO;
385                 goto bail;
386         }
387
388         if (fe->i_last_eb_blk) {
389                 retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
390                                           &eb_bh, OCFS2_BH_CACHED, inode);
391                 if (retval < 0) {
392                         mlog_errno(retval);
393                         goto bail;
394                 }
395                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
396                 el = &eb->h_list;
397         } else
398                 el = &fe->id2.i_list;
399
400         BUG_ON(el->l_tree_depth != 0);
401
402         retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
403 bail:
404         if (eb_bh)
405                 brelse(eb_bh);
406
407         mlog_exit(retval);
408         return retval;
409 }
410
411 /* expects array to already be allocated
412  *
413  * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
414  * l_count for you
415  */
416 static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
417                                      handle_t *handle,
418                                      struct inode *inode,
419                                      int wanted,
420                                      struct ocfs2_alloc_context *meta_ac,
421                                      struct buffer_head *bhs[])
422 {
423         int count, status, i;
424         u16 suballoc_bit_start;
425         u32 num_got;
426         u64 first_blkno;
427         struct ocfs2_extent_block *eb;
428
429         mlog_entry_void();
430
431         count = 0;
432         while (count < wanted) {
433                 status = ocfs2_claim_metadata(osb,
434                                               handle,
435                                               meta_ac,
436                                               wanted - count,
437                                               &suballoc_bit_start,
438                                               &num_got,
439                                               &first_blkno);
440                 if (status < 0) {
441                         mlog_errno(status);
442                         goto bail;
443                 }
444
445                 for(i = count;  i < (num_got + count); i++) {
446                         bhs[i] = sb_getblk(osb->sb, first_blkno);
447                         if (bhs[i] == NULL) {
448                                 status = -EIO;
449                                 mlog_errno(status);
450                                 goto bail;
451                         }
452                         ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
453
454                         status = ocfs2_journal_access(handle, inode, bhs[i],
455                                                       OCFS2_JOURNAL_ACCESS_CREATE);
456                         if (status < 0) {
457                                 mlog_errno(status);
458                                 goto bail;
459                         }
460
461                         memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
462                         eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
463                         /* Ok, setup the minimal stuff here. */
464                         strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
465                         eb->h_blkno = cpu_to_le64(first_blkno);
466                         eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
467                         eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
468                         eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
469                         eb->h_list.l_count =
470                                 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
471
472                         suballoc_bit_start++;
473                         first_blkno++;
474
475                         /* We'll also be dirtied by the caller, so
476                          * this isn't absolutely necessary. */
477                         status = ocfs2_journal_dirty(handle, bhs[i]);
478                         if (status < 0) {
479                                 mlog_errno(status);
480                                 goto bail;
481                         }
482                 }
483
484                 count += num_got;
485         }
486
487         status = 0;
488 bail:
489         if (status < 0) {
490                 for(i = 0; i < wanted; i++) {
491                         if (bhs[i])
492                                 brelse(bhs[i]);
493                         bhs[i] = NULL;
494                 }
495         }
496         mlog_exit(status);
497         return status;
498 }
499
500 /*
501  * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
502  *
503  * Returns the sum of the rightmost extent rec logical offset and
504  * cluster count.
505  *
506  * ocfs2_add_branch() uses this to determine what logical cluster
507  * value should be populated into the leftmost new branch records.
508  *
509  * ocfs2_shift_tree_depth() uses this to determine the # clusters
510  * value for the new topmost tree record.
511  */
512 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
513 {
514         int i;
515
516         i = le16_to_cpu(el->l_next_free_rec) - 1;
517
518         return le32_to_cpu(el->l_recs[i].e_cpos) +
519                 ocfs2_rec_clusters(el, &el->l_recs[i]);
520 }
521
522 /*
523  * Add an entire tree branch to our inode. eb_bh is the extent block
524  * to start at, if we don't want to start the branch at the dinode
525  * structure.
526  *
527  * last_eb_bh is required as we have to update it's next_leaf pointer
528  * for the new last extent block.
529  *
530  * the new branch will be 'empty' in the sense that every block will
531  * contain a single record with cluster count == 0.
532  */
533 static int ocfs2_add_branch(struct ocfs2_super *osb,
534                             handle_t *handle,
535                             struct inode *inode,
536                             struct buffer_head *fe_bh,
537                             struct buffer_head *eb_bh,
538                             struct buffer_head **last_eb_bh,
539                             struct ocfs2_alloc_context *meta_ac)
540 {
541         int status, new_blocks, i;
542         u64 next_blkno, new_last_eb_blk;
543         struct buffer_head *bh;
544         struct buffer_head **new_eb_bhs = NULL;
545         struct ocfs2_dinode *fe;
546         struct ocfs2_extent_block *eb;
547         struct ocfs2_extent_list  *eb_el;
548         struct ocfs2_extent_list  *el;
549         u32 new_cpos;
550
551         mlog_entry_void();
552
553         BUG_ON(!last_eb_bh || !*last_eb_bh);
554
555         fe = (struct ocfs2_dinode *) fe_bh->b_data;
556
557         if (eb_bh) {
558                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
559                 el = &eb->h_list;
560         } else
561                 el = &fe->id2.i_list;
562
563         /* we never add a branch to a leaf. */
564         BUG_ON(!el->l_tree_depth);
565
566         new_blocks = le16_to_cpu(el->l_tree_depth);
567
568         /* allocate the number of new eb blocks we need */
569         new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
570                              GFP_KERNEL);
571         if (!new_eb_bhs) {
572                 status = -ENOMEM;
573                 mlog_errno(status);
574                 goto bail;
575         }
576
577         status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
578                                            meta_ac, new_eb_bhs);
579         if (status < 0) {
580                 mlog_errno(status);
581                 goto bail;
582         }
583
584         eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
585         new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
586
587         /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
588          * linked with the rest of the tree.
589          * conversly, new_eb_bhs[0] is the new bottommost leaf.
590          *
591          * when we leave the loop, new_last_eb_blk will point to the
592          * newest leaf, and next_blkno will point to the topmost extent
593          * block. */
594         next_blkno = new_last_eb_blk = 0;
595         for(i = 0; i < new_blocks; i++) {
596                 bh = new_eb_bhs[i];
597                 eb = (struct ocfs2_extent_block *) bh->b_data;
598                 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
599                         OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
600                         status = -EIO;
601                         goto bail;
602                 }
603                 eb_el = &eb->h_list;
604
605                 status = ocfs2_journal_access(handle, inode, bh,
606                                               OCFS2_JOURNAL_ACCESS_CREATE);
607                 if (status < 0) {
608                         mlog_errno(status);
609                         goto bail;
610                 }
611
612                 eb->h_next_leaf_blk = 0;
613                 eb_el->l_tree_depth = cpu_to_le16(i);
614                 eb_el->l_next_free_rec = cpu_to_le16(1);
615                 /*
616                  * This actually counts as an empty extent as
617                  * c_clusters == 0
618                  */
619                 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
620                 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
621                 /*
622                  * eb_el isn't always an interior node, but even leaf
623                  * nodes want a zero'd flags and reserved field so
624                  * this gets the whole 32 bits regardless of use.
625                  */
626                 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
627                 if (!eb_el->l_tree_depth)
628                         new_last_eb_blk = le64_to_cpu(eb->h_blkno);
629
630                 status = ocfs2_journal_dirty(handle, bh);
631                 if (status < 0) {
632                         mlog_errno(status);
633                         goto bail;
634                 }
635
636                 next_blkno = le64_to_cpu(eb->h_blkno);
637         }
638
639         /* This is a bit hairy. We want to update up to three blocks
640          * here without leaving any of them in an inconsistent state
641          * in case of error. We don't have to worry about
642          * journal_dirty erroring as it won't unless we've aborted the
643          * handle (in which case we would never be here) so reserving
644          * the write with journal_access is all we need to do. */
645         status = ocfs2_journal_access(handle, inode, *last_eb_bh,
646                                       OCFS2_JOURNAL_ACCESS_WRITE);
647         if (status < 0) {
648                 mlog_errno(status);
649                 goto bail;
650         }
651         status = ocfs2_journal_access(handle, inode, fe_bh,
652                                       OCFS2_JOURNAL_ACCESS_WRITE);
653         if (status < 0) {
654                 mlog_errno(status);
655                 goto bail;
656         }
657         if (eb_bh) {
658                 status = ocfs2_journal_access(handle, inode, eb_bh,
659                                               OCFS2_JOURNAL_ACCESS_WRITE);
660                 if (status < 0) {
661                         mlog_errno(status);
662                         goto bail;
663                 }
664         }
665
666         /* Link the new branch into the rest of the tree (el will
667          * either be on the fe, or the extent block passed in. */
668         i = le16_to_cpu(el->l_next_free_rec);
669         el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
670         el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
671         el->l_recs[i].e_int_clusters = 0;
672         le16_add_cpu(&el->l_next_free_rec, 1);
673
674         /* fe needs a new last extent block pointer, as does the
675          * next_leaf on the previously last-extent-block. */
676         fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
677
678         eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
679         eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
680
681         status = ocfs2_journal_dirty(handle, *last_eb_bh);
682         if (status < 0)
683                 mlog_errno(status);
684         status = ocfs2_journal_dirty(handle, fe_bh);
685         if (status < 0)
686                 mlog_errno(status);
687         if (eb_bh) {
688                 status = ocfs2_journal_dirty(handle, eb_bh);
689                 if (status < 0)
690                         mlog_errno(status);
691         }
692
693         /*
694          * Some callers want to track the rightmost leaf so pass it
695          * back here.
696          */
697         brelse(*last_eb_bh);
698         get_bh(new_eb_bhs[0]);
699         *last_eb_bh = new_eb_bhs[0];
700
701         status = 0;
702 bail:
703         if (new_eb_bhs) {
704                 for (i = 0; i < new_blocks; i++)
705                         if (new_eb_bhs[i])
706                                 brelse(new_eb_bhs[i]);
707                 kfree(new_eb_bhs);
708         }
709
710         mlog_exit(status);
711         return status;
712 }
713
714 /*
715  * adds another level to the allocation tree.
716  * returns back the new extent block so you can add a branch to it
717  * after this call.
718  */
719 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
720                                   handle_t *handle,
721                                   struct inode *inode,
722                                   struct buffer_head *fe_bh,
723                                   struct ocfs2_alloc_context *meta_ac,
724                                   struct buffer_head **ret_new_eb_bh)
725 {
726         int status, i;
727         u32 new_clusters;
728         struct buffer_head *new_eb_bh = NULL;
729         struct ocfs2_dinode *fe;
730         struct ocfs2_extent_block *eb;
731         struct ocfs2_extent_list  *fe_el;
732         struct ocfs2_extent_list  *eb_el;
733
734         mlog_entry_void();
735
736         status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
737                                            &new_eb_bh);
738         if (status < 0) {
739                 mlog_errno(status);
740                 goto bail;
741         }
742
743         eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
744         if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
745                 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
746                 status = -EIO;
747                 goto bail;
748         }
749
750         eb_el = &eb->h_list;
751         fe = (struct ocfs2_dinode *) fe_bh->b_data;
752         fe_el = &fe->id2.i_list;
753
754         status = ocfs2_journal_access(handle, inode, new_eb_bh,
755                                       OCFS2_JOURNAL_ACCESS_CREATE);
756         if (status < 0) {
757                 mlog_errno(status);
758                 goto bail;
759         }
760
761         /* copy the fe data into the new extent block */
762         eb_el->l_tree_depth = fe_el->l_tree_depth;
763         eb_el->l_next_free_rec = fe_el->l_next_free_rec;
764         for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
765                 eb_el->l_recs[i] = fe_el->l_recs[i];
766
767         status = ocfs2_journal_dirty(handle, new_eb_bh);
768         if (status < 0) {
769                 mlog_errno(status);
770                 goto bail;
771         }
772
773         status = ocfs2_journal_access(handle, inode, fe_bh,
774                                       OCFS2_JOURNAL_ACCESS_WRITE);
775         if (status < 0) {
776                 mlog_errno(status);
777                 goto bail;
778         }
779
780         new_clusters = ocfs2_sum_rightmost_rec(eb_el);
781
782         /* update fe now */
783         le16_add_cpu(&fe_el->l_tree_depth, 1);
784         fe_el->l_recs[0].e_cpos = 0;
785         fe_el->l_recs[0].e_blkno = eb->h_blkno;
786         fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
787         for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
788                 memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
789         fe_el->l_next_free_rec = cpu_to_le16(1);
790
791         /* If this is our 1st tree depth shift, then last_eb_blk
792          * becomes the allocated extent block */
793         if (fe_el->l_tree_depth == cpu_to_le16(1))
794                 fe->i_last_eb_blk = eb->h_blkno;
795
796         status = ocfs2_journal_dirty(handle, fe_bh);
797         if (status < 0) {
798                 mlog_errno(status);
799                 goto bail;
800         }
801
802         *ret_new_eb_bh = new_eb_bh;
803         new_eb_bh = NULL;
804         status = 0;
805 bail:
806         if (new_eb_bh)
807                 brelse(new_eb_bh);
808
809         mlog_exit(status);
810         return status;
811 }
812
813 /*
814  * Should only be called when there is no space left in any of the
815  * leaf nodes. What we want to do is find the lowest tree depth
816  * non-leaf extent block with room for new records. There are three
817  * valid results of this search:
818  *
819  * 1) a lowest extent block is found, then we pass it back in
820  *    *lowest_eb_bh and return '0'
821  *
822  * 2) the search fails to find anything, but the dinode has room. We
823  *    pass NULL back in *lowest_eb_bh, but still return '0'
824  *
825  * 3) the search fails to find anything AND the dinode is full, in
826  *    which case we return > 0
827  *
828  * return status < 0 indicates an error.
829  */
830 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
831                                     struct inode *inode,
832                                     struct buffer_head *fe_bh,
833                                     struct buffer_head **target_bh)
834 {
835         int status = 0, i;
836         u64 blkno;
837         struct ocfs2_dinode *fe;
838         struct ocfs2_extent_block *eb;
839         struct ocfs2_extent_list  *el;
840         struct buffer_head *bh = NULL;
841         struct buffer_head *lowest_bh = NULL;
842
843         mlog_entry_void();
844
845         *target_bh = NULL;
846
847         fe = (struct ocfs2_dinode *) fe_bh->b_data;
848         el = &fe->id2.i_list;
849
850         while(le16_to_cpu(el->l_tree_depth) > 1) {
851                 if (le16_to_cpu(el->l_next_free_rec) == 0) {
852                         ocfs2_error(inode->i_sb, "Dinode %llu has empty "
853                                     "extent list (next_free_rec == 0)",
854                                     (unsigned long long)OCFS2_I(inode)->ip_blkno);
855                         status = -EIO;
856                         goto bail;
857                 }
858                 i = le16_to_cpu(el->l_next_free_rec) - 1;
859                 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
860                 if (!blkno) {
861                         ocfs2_error(inode->i_sb, "Dinode %llu has extent "
862                                     "list where extent # %d has no physical "
863                                     "block start",
864                                     (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
865                         status = -EIO;
866                         goto bail;
867                 }
868
869                 if (bh) {
870                         brelse(bh);
871                         bh = NULL;
872                 }
873
874                 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
875                                           inode);
876                 if (status < 0) {
877                         mlog_errno(status);
878                         goto bail;
879                 }
880
881                 eb = (struct ocfs2_extent_block *) bh->b_data;
882                 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
883                         OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
884                         status = -EIO;
885                         goto bail;
886                 }
887                 el = &eb->h_list;
888
889                 if (le16_to_cpu(el->l_next_free_rec) <
890                     le16_to_cpu(el->l_count)) {
891                         if (lowest_bh)
892                                 brelse(lowest_bh);
893                         lowest_bh = bh;
894                         get_bh(lowest_bh);
895                 }
896         }
897
898         /* If we didn't find one and the fe doesn't have any room,
899          * then return '1' */
900         if (!lowest_bh
901             && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
902                 status = 1;
903
904         *target_bh = lowest_bh;
905 bail:
906         if (bh)
907                 brelse(bh);
908
909         mlog_exit(status);
910         return status;
911 }
912
913 /*
914  * Grow a b-tree so that it has more records.
915  *
916  * We might shift the tree depth in which case existing paths should
917  * be considered invalid.
918  *
919  * Tree depth after the grow is returned via *final_depth.
920  *
921  * *last_eb_bh will be updated by ocfs2_add_branch().
922  */
923 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
924                            struct buffer_head *di_bh, int *final_depth,
925                            struct buffer_head **last_eb_bh,
926                            struct ocfs2_alloc_context *meta_ac)
927 {
928         int ret, shift;
929         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
930         int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
931         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
932         struct buffer_head *bh = NULL;
933
934         BUG_ON(meta_ac == NULL);
935
936         shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
937         if (shift < 0) {
938                 ret = shift;
939                 mlog_errno(ret);
940                 goto out;
941         }
942
943         /* We traveled all the way to the bottom of the allocation tree
944          * and didn't find room for any more extents - we need to add
945          * another tree level */
946         if (shift) {
947                 BUG_ON(bh);
948                 mlog(0, "need to shift tree depth (current = %d)\n", depth);
949
950                 /* ocfs2_shift_tree_depth will return us a buffer with
951                  * the new extent block (so we can pass that to
952                  * ocfs2_add_branch). */
953                 ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
954                                              meta_ac, &bh);
955                 if (ret < 0) {
956                         mlog_errno(ret);
957                         goto out;
958                 }
959                 depth++;
960                 if (depth == 1) {
961                         /*
962                          * Special case: we have room now if we shifted from
963                          * tree_depth 0, so no more work needs to be done.
964                          *
965                          * We won't be calling add_branch, so pass
966                          * back *last_eb_bh as the new leaf. At depth
967                          * zero, it should always be null so there's
968                          * no reason to brelse.
969                          */
970                         BUG_ON(*last_eb_bh);
971                         get_bh(bh);
972                         *last_eb_bh = bh;
973                         goto out;
974                 }
975         }
976
977         /* call ocfs2_add_branch to add the final part of the tree with
978          * the new data. */
979         mlog(0, "add branch. bh = %p\n", bh);
980         ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
981                                meta_ac);
982         if (ret < 0) {
983                 mlog_errno(ret);
984                 goto out;
985         }
986
987 out:
988         if (final_depth)
989                 *final_depth = depth;
990         brelse(bh);
991         return ret;
992 }
993
994 /*
995  * This is only valid for leaf nodes, which are the only ones that can
996  * have empty extents anyway.
997  */
998 static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
999 {
1000         return !rec->e_leaf_clusters;
1001 }
1002
1003 /*
1004  * This function will discard the rightmost extent record.
1005  */
1006 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1007 {
1008         int next_free = le16_to_cpu(el->l_next_free_rec);
1009         int count = le16_to_cpu(el->l_count);
1010         unsigned int num_bytes;
1011
1012         BUG_ON(!next_free);
1013         /* This will cause us to go off the end of our extent list. */
1014         BUG_ON(next_free >= count);
1015
1016         num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1017
1018         memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1019 }
1020
1021 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1022                               struct ocfs2_extent_rec *insert_rec)
1023 {
1024         int i, insert_index, next_free, has_empty, num_bytes;
1025         u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1026         struct ocfs2_extent_rec *rec;
1027
1028         next_free = le16_to_cpu(el->l_next_free_rec);
1029         has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1030
1031         BUG_ON(!next_free);
1032
1033         /* The tree code before us didn't allow enough room in the leaf. */
1034         if (el->l_next_free_rec == el->l_count && !has_empty)
1035                 BUG();
1036
1037         /*
1038          * The easiest way to approach this is to just remove the
1039          * empty extent and temporarily decrement next_free.
1040          */
1041         if (has_empty) {
1042                 /*
1043                  * If next_free was 1 (only an empty extent), this
1044                  * loop won't execute, which is fine. We still want
1045                  * the decrement above to happen.
1046                  */
1047                 for(i = 0; i < (next_free - 1); i++)
1048                         el->l_recs[i] = el->l_recs[i+1];
1049
1050                 next_free--;
1051         }
1052
1053         /*
1054          * Figure out what the new record index should be.
1055          */
1056         for(i = 0; i < next_free; i++) {
1057                 rec = &el->l_recs[i];
1058
1059                 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1060                         break;
1061         }
1062         insert_index = i;
1063
1064         mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1065              insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1066
1067         BUG_ON(insert_index < 0);
1068         BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1069         BUG_ON(insert_index > next_free);
1070
1071         /*
1072          * No need to memmove if we're just adding to the tail.
1073          */
1074         if (insert_index != next_free) {
1075                 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1076
1077                 num_bytes = next_free - insert_index;
1078                 num_bytes *= sizeof(struct ocfs2_extent_rec);
1079                 memmove(&el->l_recs[insert_index + 1],
1080                         &el->l_recs[insert_index],
1081                         num_bytes);
1082         }
1083
1084         /*
1085          * Either we had an empty extent, and need to re-increment or
1086          * there was no empty extent on a non full rightmost leaf node,
1087          * in which case we still need to increment.
1088          */
1089         next_free++;
1090         el->l_next_free_rec = cpu_to_le16(next_free);
1091         /*
1092          * Make sure none of the math above just messed up our tree.
1093          */
1094         BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1095
1096         el->l_recs[insert_index] = *insert_rec;
1097
1098 }
1099
1100 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1101 {
1102         int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1103
1104         BUG_ON(num_recs == 0);
1105
1106         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1107                 num_recs--;
1108                 size = num_recs * sizeof(struct ocfs2_extent_rec);
1109                 memmove(&el->l_recs[0], &el->l_recs[1], size);
1110                 memset(&el->l_recs[num_recs], 0,
1111                        sizeof(struct ocfs2_extent_rec));
1112                 el->l_next_free_rec = cpu_to_le16(num_recs);
1113         }
1114 }
1115
1116 /*
1117  * Create an empty extent record .
1118  *
1119  * l_next_free_rec may be updated.
1120  *
1121  * If an empty extent already exists do nothing.
1122  */
1123 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1124 {
1125         int next_free = le16_to_cpu(el->l_next_free_rec);
1126
1127         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1128
1129         if (next_free == 0)
1130                 goto set_and_inc;
1131
1132         if (ocfs2_is_empty_extent(&el->l_recs[0]))
1133                 return;
1134
1135         mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1136                         "Asked to create an empty extent in a full list:\n"
1137                         "count = %u, tree depth = %u",
1138                         le16_to_cpu(el->l_count),
1139                         le16_to_cpu(el->l_tree_depth));
1140
1141         ocfs2_shift_records_right(el);
1142
1143 set_and_inc:
1144         le16_add_cpu(&el->l_next_free_rec, 1);
1145         memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1146 }
1147
1148 /*
1149  * For a rotation which involves two leaf nodes, the "root node" is
1150  * the lowest level tree node which contains a path to both leafs. This
1151  * resulting set of information can be used to form a complete "subtree"
1152  *
1153  * This function is passed two full paths from the dinode down to a
1154  * pair of adjacent leaves. It's task is to figure out which path
1155  * index contains the subtree root - this can be the root index itself
1156  * in a worst-case rotation.
1157  *
1158  * The array index of the subtree root is passed back.
1159  */
1160 static int ocfs2_find_subtree_root(struct inode *inode,
1161                                    struct ocfs2_path *left,
1162                                    struct ocfs2_path *right)
1163 {
1164         int i = 0;
1165
1166         /*
1167          * Check that the caller passed in two paths from the same tree.
1168          */
1169         BUG_ON(path_root_bh(left) != path_root_bh(right));
1170
1171         do {
1172                 i++;
1173
1174                 /*
1175                  * The caller didn't pass two adjacent paths.
1176                  */
1177                 mlog_bug_on_msg(i > left->p_tree_depth,
1178                                 "Inode %lu, left depth %u, right depth %u\n"
1179                                 "left leaf blk %llu, right leaf blk %llu\n",
1180                                 inode->i_ino, left->p_tree_depth,
1181                                 right->p_tree_depth,
1182                                 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1183                                 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1184         } while (left->p_node[i].bh->b_blocknr ==
1185                  right->p_node[i].bh->b_blocknr);
1186
1187         return i - 1;
1188 }
1189
1190 typedef void (path_insert_t)(void *, struct buffer_head *);
1191
1192 /*
1193  * Traverse a btree path in search of cpos, starting at root_el.
1194  *
1195  * This code can be called with a cpos larger than the tree, in which
1196  * case it will return the rightmost path.
1197  */
1198 static int __ocfs2_find_path(struct inode *inode,
1199                              struct ocfs2_extent_list *root_el, u32 cpos,
1200                              path_insert_t *func, void *data)
1201 {
1202         int i, ret = 0;
1203         u32 range;
1204         u64 blkno;
1205         struct buffer_head *bh = NULL;
1206         struct ocfs2_extent_block *eb;
1207         struct ocfs2_extent_list *el;
1208         struct ocfs2_extent_rec *rec;
1209         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1210
1211         el = root_el;
1212         while (el->l_tree_depth) {
1213                 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1214                         ocfs2_error(inode->i_sb,
1215                                     "Inode %llu has empty extent list at "
1216                                     "depth %u\n",
1217                                     (unsigned long long)oi->ip_blkno,
1218                                     le16_to_cpu(el->l_tree_depth));
1219                         ret = -EROFS;
1220                         goto out;
1221
1222                 }
1223
1224                 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1225                         rec = &el->l_recs[i];
1226
1227                         /*
1228                          * In the case that cpos is off the allocation
1229                          * tree, this should just wind up returning the
1230                          * rightmost record.
1231                          */
1232                         range = le32_to_cpu(rec->e_cpos) +
1233                                 ocfs2_rec_clusters(el, rec);
1234                         if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1235                             break;
1236                 }
1237
1238                 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1239                 if (blkno == 0) {
1240                         ocfs2_error(inode->i_sb,
1241                                     "Inode %llu has bad blkno in extent list "
1242                                     "at depth %u (index %d)\n",
1243                                     (unsigned long long)oi->ip_blkno,
1244                                     le16_to_cpu(el->l_tree_depth), i);
1245                         ret = -EROFS;
1246                         goto out;
1247                 }
1248
1249                 brelse(bh);
1250                 bh = NULL;
1251                 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1252                                        &bh, OCFS2_BH_CACHED, inode);
1253                 if (ret) {
1254                         mlog_errno(ret);
1255                         goto out;
1256                 }
1257
1258                 eb = (struct ocfs2_extent_block *) bh->b_data;
1259                 el = &eb->h_list;
1260                 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1261                         OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1262                         ret = -EIO;
1263                         goto out;
1264                 }
1265
1266                 if (le16_to_cpu(el->l_next_free_rec) >
1267                     le16_to_cpu(el->l_count)) {
1268                         ocfs2_error(inode->i_sb,
1269                                     "Inode %llu has bad count in extent list "
1270                                     "at block %llu (next free=%u, count=%u)\n",
1271                                     (unsigned long long)oi->ip_blkno,
1272                                     (unsigned long long)bh->b_blocknr,
1273                                     le16_to_cpu(el->l_next_free_rec),
1274                                     le16_to_cpu(el->l_count));
1275                         ret = -EROFS;
1276                         goto out;
1277                 }
1278
1279                 if (func)
1280                         func(data, bh);
1281         }
1282
1283 out:
1284         /*
1285          * Catch any trailing bh that the loop didn't handle.
1286          */
1287         brelse(bh);
1288
1289         return ret;
1290 }
1291
1292 /*
1293  * Given an initialized path (that is, it has a valid root extent
1294  * list), this function will traverse the btree in search of the path
1295  * which would contain cpos.
1296  *
1297  * The path traveled is recorded in the path structure.
1298  *
1299  * Note that this will not do any comparisons on leaf node extent
1300  * records, so it will work fine in the case that we just added a tree
1301  * branch.
1302  */
1303 struct find_path_data {
1304         int index;
1305         struct ocfs2_path *path;
1306 };
1307 static void find_path_ins(void *data, struct buffer_head *bh)
1308 {
1309         struct find_path_data *fp = data;
1310
1311         get_bh(bh);
1312         ocfs2_path_insert_eb(fp->path, fp->index, bh);
1313         fp->index++;
1314 }
1315 static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1316                            u32 cpos)
1317 {
1318         struct find_path_data data;
1319
1320         data.index = 1;
1321         data.path = path;
1322         return __ocfs2_find_path(inode, path_root_el(path), cpos,
1323                                  find_path_ins, &data);
1324 }
1325
1326 static void find_leaf_ins(void *data, struct buffer_head *bh)
1327 {
1328         struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1329         struct ocfs2_extent_list *el = &eb->h_list;
1330         struct buffer_head **ret = data;
1331
1332         /* We want to retain only the leaf block. */
1333         if (le16_to_cpu(el->l_tree_depth) == 0) {
1334                 get_bh(bh);
1335                 *ret = bh;
1336         }
1337 }
1338 /*
1339  * Find the leaf block in the tree which would contain cpos. No
1340  * checking of the actual leaf is done.
1341  *
1342  * Some paths want to call this instead of allocating a path structure
1343  * and calling ocfs2_find_path().
1344  *
1345  * This function doesn't handle non btree extent lists.
1346  */
1347 int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1348                     u32 cpos, struct buffer_head **leaf_bh)
1349 {
1350         int ret;
1351         struct buffer_head *bh = NULL;
1352
1353         ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1354         if (ret) {
1355                 mlog_errno(ret);
1356                 goto out;
1357         }
1358
1359         *leaf_bh = bh;
1360 out:
1361         return ret;
1362 }
1363
1364 /*
1365  * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1366  *
1367  * Basically, we've moved stuff around at the bottom of the tree and
1368  * we need to fix up the extent records above the changes to reflect
1369  * the new changes.
1370  *
1371  * left_rec: the record on the left.
1372  * left_child_el: is the child list pointed to by left_rec
1373  * right_rec: the record to the right of left_rec
1374  * right_child_el: is the child list pointed to by right_rec
1375  *
1376  * By definition, this only works on interior nodes.
1377  */
1378 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1379                                   struct ocfs2_extent_list *left_child_el,
1380                                   struct ocfs2_extent_rec *right_rec,
1381                                   struct ocfs2_extent_list *right_child_el)
1382 {
1383         u32 left_clusters, right_end;
1384
1385         /*
1386          * Interior nodes never have holes. Their cpos is the cpos of
1387          * the leftmost record in their child list. Their cluster
1388          * count covers the full theoretical range of their child list
1389          * - the range between their cpos and the cpos of the record
1390          * immediately to their right.
1391          */
1392         left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1393         if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1394                 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1395                 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1396         }
1397         left_clusters -= le32_to_cpu(left_rec->e_cpos);
1398         left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1399
1400         /*
1401          * Calculate the rightmost cluster count boundary before
1402          * moving cpos - we will need to adjust clusters after
1403          * updating e_cpos to keep the same highest cluster count.
1404          */
1405         right_end = le32_to_cpu(right_rec->e_cpos);
1406         right_end += le32_to_cpu(right_rec->e_int_clusters);
1407
1408         right_rec->e_cpos = left_rec->e_cpos;
1409         le32_add_cpu(&right_rec->e_cpos, left_clusters);
1410
1411         right_end -= le32_to_cpu(right_rec->e_cpos);
1412         right_rec->e_int_clusters = cpu_to_le32(right_end);
1413 }
1414
1415 /*
1416  * Adjust the adjacent root node records involved in a
1417  * rotation. left_el_blkno is passed in as a key so that we can easily
1418  * find it's index in the root list.
1419  */
1420 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1421                                       struct ocfs2_extent_list *left_el,
1422                                       struct ocfs2_extent_list *right_el,
1423                                       u64 left_el_blkno)
1424 {
1425         int i;
1426
1427         BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1428                le16_to_cpu(left_el->l_tree_depth));
1429
1430         for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1431                 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1432                         break;
1433         }
1434
1435         /*
1436          * The path walking code should have never returned a root and
1437          * two paths which are not adjacent.
1438          */
1439         BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1440
1441         ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1442                                       &root_el->l_recs[i + 1], right_el);
1443 }
1444
1445 /*
1446  * We've changed a leaf block (in right_path) and need to reflect that
1447  * change back up the subtree.
1448  *
1449  * This happens in multiple places:
1450  *   - When we've moved an extent record from the left path leaf to the right
1451  *     path leaf to make room for an empty extent in the left path leaf.
1452  *   - When our insert into the right path leaf is at the leftmost edge
1453  *     and requires an update of the path immediately to it's left. This
1454  *     can occur at the end of some types of rotation and appending inserts.
1455  */
1456 static 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
1534 static 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
1638 out:
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  */
1648 static 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
1655         BUG_ON(path->p_tree_depth == 0);
1656
1657         *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);
1691                                 *cpos = *cpos + ocfs2_rec_clusters(el,
1692                                                            &el->l_recs[j - 1]);
1693                                 *cpos = *cpos - 1;
1694                                 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
1708 next_node:
1709                 blkno = path->p_node[i].bh->b_blocknr;
1710                 i--;
1711         }
1712
1713 out:
1714         return ret;
1715 }
1716
1717 /*
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  */
1722 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
1723                                            int op_credits,
1724                                            struct ocfs2_path *path)
1725 {
1726         int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
1727
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  */
1744 static 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
1760 static 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
1783 /*
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  */
1799 static int ocfs2_rotate_tree_right(struct inode *inode,
1800                                    handle_t *handle,
1801                                    enum ocfs2_split_type split,
1802                                    u32 insert_cpos,
1803                                    struct ocfs2_path *right_path,
1804                                    struct ocfs2_path **ret_left_path)
1805 {
1806         int ret, start, orig_credits = handle->h_buffer_credits;
1807         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
1873                 if (split == SPLIT_NONE &&
1874                     ocfs2_rotate_requires_path_adjustment(left_path,
1875                                                           insert_cpos)) {
1876
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,
1903                                                       orig_credits, right_path);
1904                 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
1916                 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
1934                 /*
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
1949 out:
1950         ocfs2_free_path(left_path);
1951
1952 out_ret_path:
1953         return ret;
1954 }
1955
1956 static 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
1987 static 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
2031 static 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
2066 static 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
2221 out:
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  */
2234 static 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
2295 next_node:
2296                 blkno = path->p_node[i].bh->b_blocknr;
2297                 i--;
2298         }
2299
2300 out:
2301         return ret;
2302 }
2303
2304 static 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
2327 out:
2328         return ret;
2329 }
2330
2331 static 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
2394                 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2395                                                 right_path, subtree_root,
2396                                                 dealloc, &deleted);
2397                 if (ret == -EAGAIN) {
2398                         /*
2399                          * The rotation has to temporarily stop due to
2400                          * the right subtree having an empty
2401                          * extent. Pass it back to the caller for a
2402                          * fixup.
2403                          */
2404                         *empty_extent_path = right_path;
2405                         right_path = NULL;
2406                         goto out;
2407                 }
2408                 if (ret) {
2409                         mlog_errno(ret);
2410                         goto out;
2411                 }
2412
2413                 /*
2414                  * The subtree rotate might have removed records on
2415                  * the rightmost edge. If so, then rotation is
2416                  * complete.
2417                  */
2418                 if (deleted)
2419                         break;
2420
2421                 ocfs2_mv_path(left_path, right_path);
2422
2423                 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2424                                                      &right_cpos);
2425                 if (ret) {
2426                         mlog_errno(ret);
2427                         goto out;
2428                 }
2429         }
2430
2431 out:
2432         ocfs2_free_path(right_path);
2433         ocfs2_free_path(left_path);
2434
2435         return ret;
2436 }
2437
2438 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
2439                                        struct ocfs2_path *path,
2440                                        struct ocfs2_cached_dealloc_ctxt *dealloc)
2441 {
2442         int ret, subtree_index;
2443         u32 cpos;
2444         struct ocfs2_path *left_path = NULL;
2445         struct ocfs2_dinode *di;
2446         struct ocfs2_extent_block *eb;
2447         struct ocfs2_extent_list *el;
2448
2449         /*
2450          * XXX: This code assumes that the root is an inode, which is
2451          * true for now but may change as tree code gets generic.
2452          */
2453         di = (struct ocfs2_dinode *)path_root_bh(path)->b_data;
2454         if (!OCFS2_IS_VALID_DINODE(di)) {
2455                 ret = -EIO;
2456                 ocfs2_error(inode->i_sb,
2457                             "Inode %llu has invalid path root",
2458                             (unsigned long long)OCFS2_I(inode)->ip_blkno);
2459                 goto out;
2460         }
2461
2462         /*
2463          * There's two ways we handle this depending on
2464          * whether path is the only existing one.
2465          */
2466         ret = ocfs2_extend_rotate_transaction(handle, 0,
2467                                               handle->h_buffer_credits,
2468                                               path);
2469         if (ret) {
2470                 mlog_errno(ret);
2471                 goto out;
2472         }
2473
2474         ret = ocfs2_journal_access_path(inode, handle, path);
2475         if (ret) {
2476                 mlog_errno(ret);
2477                 goto out;
2478         }
2479
2480         ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2481         if (ret) {
2482                 mlog_errno(ret);
2483                 goto out;
2484         }
2485
2486         if (cpos) {
2487                 /*
2488                  * We have a path to the left of this one - it needs
2489                  * an update too.
2490                  */
2491                 left_path = ocfs2_new_path(path_root_bh(path),
2492                                            path_root_el(path));
2493                 if (!left_path) {
2494                         ret = -ENOMEM;
2495                         mlog_errno(ret);
2496                         goto out;
2497                 }
2498
2499                 ret = ocfs2_find_path(inode, left_path, cpos);
2500                 if (ret) {
2501                         mlog_errno(ret);
2502                         goto out;
2503                 }
2504
2505                 ret = ocfs2_journal_access_path(inode, handle, left_path);
2506                 if (ret) {
2507                         mlog_errno(ret);
2508                         goto out;
2509                 }
2510
2511                 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2512
2513                 ocfs2_unlink_subtree(inode, handle, left_path, path,
2514                                      subtree_index, dealloc);
2515                 ocfs2_update_edge_lengths(inode, handle, left_path);
2516
2517                 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2518                 di->i_last_eb_blk = eb->h_blkno;
2519         } else {
2520                 /*
2521                  * 'path' is also the leftmost path which
2522                  * means it must be the only one. This gets
2523                  * handled differently because we want to
2524                  * revert the inode back to having extents
2525                  * in-line.
2526                  */
2527                 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2528
2529                 el = &di->id2.i_list;
2530                 el->l_tree_depth = 0;
2531                 el->l_next_free_rec = 0;
2532                 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2533
2534                 di->i_last_eb_blk = 0;
2535         }
2536
2537         ocfs2_journal_dirty(handle, path_root_bh(path));
2538
2539 out:
2540         ocfs2_free_path(left_path);
2541         return ret;
2542 }
2543
2544 /*
2545  * Left rotation of btree records.
2546  *
2547  * In many ways, this is (unsurprisingly) the opposite of right
2548  * rotation. We start at some non-rightmost path containing an empty
2549  * extent in the leaf block. The code works its way to the rightmost
2550  * path by rotating records to the left in every subtree.
2551  *
2552  * This is used by any code which reduces the number of extent records
2553  * in a leaf. After removal, an empty record should be placed in the
2554  * leftmost list position.
2555  *
2556  * This won't handle a length update of the rightmost path records if
2557  * the rightmost tree leaf record is removed so the caller is
2558  * responsible for detecting and correcting that.
2559  */
2560 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2561                                   struct ocfs2_path *path,
2562                                   struct ocfs2_cached_dealloc_ctxt *dealloc)
2563 {
2564         int ret, orig_credits = handle->h_buffer_credits;
2565         struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2566         struct ocfs2_extent_block *eb;
2567         struct ocfs2_extent_list *el;
2568
2569         el = path_leaf_el(path);
2570         if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2571                 return 0;
2572
2573         if (path->p_tree_depth == 0) {
2574 rightmost_no_delete:
2575                 /*
2576                  * In-inode extents. This is trivially handled, so do
2577                  * it up front.
2578                  */
2579                 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2580                                                        path_leaf_bh(path),
2581                                                        path_leaf_el(path));
2582                 if (ret)
2583                         mlog_errno(ret);
2584                 goto out;
2585         }
2586
2587         /*
2588          * Handle rightmost branch now. There's several cases:
2589          *  1) simple rotation leaving records in there. That's trivial.
2590          *  2) rotation requiring a branch delete - there's no more
2591          *     records left. Two cases of this:
2592          *     a) There are branches to the left.
2593          *     b) This is also the leftmost (the only) branch.
2594          *
2595          *  1) is handled via ocfs2_rotate_rightmost_leaf_left()
2596          *  2a) we need the left branch so that we can update it with the unlink
2597          *  2b) we need to bring the inode back to inline extents.
2598          */
2599
2600         eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2601         el = &eb->h_list;
2602         if (eb->h_next_leaf_blk == 0) {
2603                 /*
2604                  * This gets a bit tricky if we're going to delete the
2605                  * rightmost path. Get the other cases out of the way
2606                  * 1st.
2607                  */
2608                 if (le16_to_cpu(el->l_next_free_rec) > 1)
2609                         goto rightmost_no_delete;
2610
2611                 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2612                         ret = -EIO;
2613                         ocfs2_error(inode->i_sb,
2614                                     "Inode %llu has empty extent block at %llu",
2615                                     (unsigned long long)OCFS2_I(inode)->ip_blkno,
2616                                     (unsigned long long)le64_to_cpu(eb->h_blkno));
2617                         goto out;
2618                 }
2619
2620                 /*
2621                  * XXX: The caller can not trust "path" any more after
2622                  * this as it will have been deleted. What do we do?
2623                  *
2624                  * In theory the rotate-for-merge code will never get
2625                  * here because it'll always ask for a rotate in a
2626                  * nonempty list.
2627                  */
2628
2629                 ret = ocfs2_remove_rightmost_path(inode, handle, path,
2630                                                   dealloc);
2631                 if (ret)
2632                         mlog_errno(ret);
2633                 goto out;
2634         }
2635
2636         /*
2637          * Now we can loop, remembering the path we get from -EAGAIN
2638          * and restarting from there.
2639          */
2640 try_rotate:
2641         ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
2642                                        dealloc, &restart_path);
2643         if (ret && ret != -EAGAIN) {
2644                 mlog_errno(ret);
2645                 goto out;
2646         }
2647
2648         while (ret == -EAGAIN) {
2649                 tmp_path = restart_path;
2650                 restart_path = NULL;
2651
2652                 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2653                                                tmp_path, dealloc,
2654                                                &restart_path);
2655                 if (ret && ret != -EAGAIN) {
2656                         mlog_errno(ret);
2657                         goto out;
2658                 }
2659
2660                 ocfs2_free_path(tmp_path);
2661                 tmp_path = NULL;
2662
2663                 if (ret == 0)
2664                         goto try_rotate;
2665         }
2666
2667 out:
2668         ocfs2_free_path(tmp_path);
2669         ocfs2_free_path(restart_path);
2670         return ret;
2671 }
2672
2673 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2674                                 int index)
2675 {
2676         struct ocfs2_extent_rec *rec = &el->l_recs[index];
2677         unsigned int size;
2678
2679         if (rec->e_leaf_clusters == 0) {
2680                 /*
2681                  * We consumed all of the merged-from record. An empty
2682                  * extent cannot exist anywhere but the 1st array
2683                  * position, so move things over if the merged-from
2684                  * record doesn't occupy that position.
2685                  *
2686                  * This creates a new empty extent so the caller
2687                  * should be smart enough to have removed any existing
2688                  * ones.
2689                  */
2690                 if (index > 0) {
2691                         BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2692                         size = index * sizeof(struct ocfs2_extent_rec);
2693                         memmove(&el->l_recs[1], &el->l_recs[0], size);
2694                 }
2695
2696                 /*
2697                  * Always memset - the caller doesn't check whether it
2698                  * created an empty extent, so there could be junk in
2699                  * the other fields.
2700                  */
2701                 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2702         }
2703 }
2704
2705 /*
2706  * Remove split_rec clusters from the record at index and merge them
2707  * onto the beginning of the record at index + 1.
2708  */
2709 static int ocfs2_merge_rec_right(struct inode *inode, struct buffer_head *bh,
2710                                 handle_t *handle,
2711                                 struct ocfs2_extent_rec *split_rec,
2712                                 struct ocfs2_extent_list *el, int index)
2713 {
2714         int ret;
2715         unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2716         struct ocfs2_extent_rec *left_rec;
2717         struct ocfs2_extent_rec *right_rec;
2718
2719         BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
2720
2721         left_rec = &el->l_recs[index];
2722         right_rec = &el->l_recs[index + 1];
2723
2724         ret = ocfs2_journal_access(handle, inode, bh,
2725                                    OCFS2_JOURNAL_ACCESS_WRITE);
2726         if (ret) {
2727                 mlog_errno(ret);
2728                 goto out;
2729         }
2730
2731         le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
2732
2733         le32_add_cpu(&right_rec->e_cpos, -split_clusters);
2734         le64_add_cpu(&right_rec->e_blkno,
2735                      -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
2736         le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
2737
2738         ocfs2_cleanup_merge(el, index);
2739
2740         ret = ocfs2_journal_dirty(handle, bh);
2741         if (ret)
2742                 mlog_errno(ret);
2743
2744 out:
2745         return ret;
2746 }
2747
2748 /*
2749  * Remove split_rec clusters from the record at index and merge them
2750  * onto the tail of the record at index - 1.
2751  */
2752 static int ocfs2_merge_rec_left(struct inode *inode, struct buffer_head *bh,
2753                                 handle_t *handle,
2754                                 struct ocfs2_extent_rec *split_rec,
2755                                 struct ocfs2_extent_list *el, int index)
2756 {
2757         int ret, has_empty_extent = 0;
2758         unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
2759         struct ocfs2_extent_rec *left_rec;
2760         struct ocfs2_extent_rec *right_rec;
2761
2762         BUG_ON(index <= 0);
2763
2764         left_rec = &el->l_recs[index - 1];
2765         right_rec = &el->l_recs[index];
2766         if (ocfs2_is_empty_extent(&el->l_recs[0]))
2767                 has_empty_extent = 1;
2768
2769         ret = ocfs2_journal_access(handle, inode, bh,
2770                                    OCFS2_JOURNAL_ACCESS_WRITE);
2771         if (ret) {
2772                 mlog_errno(ret);
2773                 goto out;
2774         }
2775
2776         if (has_empty_extent && index == 1) {
2777                 /*
2778                  * The easy case - we can just plop the record right in.
2779                  */
2780                 *left_rec = *split_rec;
2781
2782                 has_empty_extent = 0;
2783         } else {
2784                 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
2785         }
2786
2787         le32_add_cpu(&right_rec->e_cpos, split_clusters);
2788         le64_add_cpu(&right_rec->e_blkno,
2789                      ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
2790         le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
2791
2792         ocfs2_cleanup_merge(el, index);
2793
2794         ret = ocfs2_journal_dirty(handle, bh);
2795         if (ret)
2796                 mlog_errno(ret);
2797
2798 out:
2799         return ret;
2800 }
2801
2802 static int ocfs2_try_to_merge_extent(struct inode *inode,
2803                                      handle_t *handle,
2804                                      struct ocfs2_path *left_path,
2805                                      int split_index,
2806                                      struct ocfs2_extent_rec *split_rec,
2807                                      struct ocfs2_cached_dealloc_ctxt *dealloc,
2808                                      struct ocfs2_merge_ctxt *ctxt)
2809
2810 {
2811         int ret = 0, delete_tail_recs = 0;
2812         struct ocfs2_extent_list *el = path_leaf_el(left_path);
2813         struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
2814
2815         BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
2816
2817         if (ctxt->c_split_covers_rec) {
2818                 delete_tail_recs++;
2819
2820                 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT ||
2821                     ctxt->c_has_empty_extent)
2822                         delete_tail_recs++;
2823
2824                 if (ctxt->c_has_empty_extent) {
2825                         /*
2826                          * The merge code will need to create an empty
2827                          * extent to take the place of the newly
2828                          * emptied slot. Remove any pre-existing empty
2829                          * extents - having more than one in a leaf is
2830                          * illegal.
2831                          */
2832                         ret = ocfs2_rotate_tree_left(inode, handle, left_path,
2833                                                      dealloc);
2834                         if (ret) {
2835                                 mlog_errno(ret);
2836                                 goto out;
2837                         }
2838                         split_index--;
2839                         rec = &el->l_recs[split_index];
2840                 }
2841         }
2842
2843         if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
2844                 /*
2845                  * Left-right contig implies this.
2846                  */
2847                 BUG_ON(!ctxt->c_split_covers_rec);
2848                 BUG_ON(split_index == 0);
2849
2850                 /*
2851                  * Since the leftright insert always covers the entire
2852                  * extent, this call will delete the insert record
2853                  * entirely, resulting in an empty extent record added to
2854                  * the extent block.
2855                  *
2856                  * Since the adding of an empty extent shifts
2857                  * everything back to the right, there's no need to
2858                  * update split_index here.
2859                  */
2860                 ret = ocfs2_merge_rec_left(inode, path_leaf_bh(left_path),
2861                                            handle, split_rec, el, split_index);
2862                 if (ret) {
2863                         mlog_errno(ret);
2864                         goto out;
2865                 }
2866
2867                 /*
2868                  * We can only get this from logic error above.
2869                  */
2870                 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
2871
2872                 /*
2873                  * The left merge left us with an empty extent, remove
2874                  * it.
2875                  */
2876                 ret = ocfs2_rotate_tree_left(inode, handle, left_path, dealloc);
2877                 if (ret) {
2878                         mlog_errno(ret);
2879                         goto out;
2880                 }
2881                 split_index--;
2882                 rec = &el->l_recs[split_index];
2883
2884                 /*
2885                  * Note that we don't pass split_rec here on purpose -
2886                  * we've merged it into the left side.
2887                  */
2888                 ret = ocfs2_merge_rec_right(inode, path_leaf_bh(left_path),
2889                                             handle, rec, el, split_index);
2890                 if (ret) {
2891                         mlog_errno(ret);
2892                         goto out;
2893                 }
2894
2895                 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
2896
2897                 ret = ocfs2_rotate_tree_left(inode, handle, left_path,
2898                                              dealloc);
2899                 /*
2900                  * Error from this last rotate is not critical, so
2901                  * print but don't bubble it up.
2902                  */
2903                 if (ret)
2904                         mlog_errno(ret);
2905                 ret = 0;
2906         } else {
2907                 /*
2908                  * Merge a record to the left or right.
2909                  *
2910                  * 'contig_type' is relative to the existing record,
2911                  * so for example, if we're "right contig", it's to
2912                  * the record on the left (hence the left merge).
2913                  */
2914                 if (ctxt->c_contig_type == CONTIG_RIGHT) {
2915                         ret = ocfs2_merge_rec_left(inode,
2916                                                    path_leaf_bh(left_path),
2917                                                    handle, split_rec, el,
2918                                                    split_index);
2919                         if (ret) {
2920                                 mlog_errno(ret);
2921                                 goto out;
2922                         }
2923                 } else {
2924                         ret = ocfs2_merge_rec_right(inode,
2925                                                     path_leaf_bh(left_path),
2926                                                     handle, split_rec, el,
2927                                                     split_index);
2928                         if (ret) {
2929                                 mlog_errno(ret);
2930                                 goto out;
2931                         }
2932                 }
2933
2934                 if (ctxt->c_split_covers_rec) {
2935                         /*
2936                          * The merge may have left an empty extent in
2937                          * our leaf. Try to rotate it away.
2938                          */
2939                         ret = ocfs2_rotate_tree_left(inode, handle, left_path,
2940                                                      dealloc);
2941                         if (ret)
2942                                 mlog_errno(ret);
2943                         ret = 0;
2944                 }
2945         }
2946
2947 out:
2948         return ret;
2949 }
2950
2951 static void ocfs2_subtract_from_rec(struct super_block *sb,
2952                                     enum ocfs2_split_type split,
2953                                     struct ocfs2_extent_rec *rec,
2954                                     struct ocfs2_extent_rec *split_rec)
2955 {
2956         u64 len_blocks;
2957
2958         len_blocks = ocfs2_clusters_to_blocks(sb,
2959                                 le16_to_cpu(split_rec->e_leaf_clusters));
2960
2961         if (split == SPLIT_LEFT) {
2962                 /*
2963                  * Region is on the left edge of the existing
2964                  * record.
2965                  */
2966                 le32_add_cpu(&rec->e_cpos,
2967                              le16_to_cpu(split_rec->e_leaf_clusters));
2968                 le64_add_cpu(&rec->e_blkno, len_blocks);
2969                 le16_add_cpu(&rec->e_leaf_clusters,
2970                              -le16_to_cpu(split_rec->e_leaf_clusters));
2971         } else {
2972                 /*
2973                  * Region is on the right edge of the existing
2974                  * record.
2975                  */
2976                 le16_add_cpu(&rec->e_leaf_clusters,
2977                              -le16_to_cpu(split_rec->e_leaf_clusters));
2978         }
2979 }
2980
2981 /*
2982  * Do the final bits of extent record insertion at the target leaf
2983  * list. If this leaf is part of an allocation tree, it is assumed
2984  * that the tree above has been prepared.
2985  */
2986 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
2987                                  struct ocfs2_extent_list *el,
2988                                  struct ocfs2_insert_type *insert,
2989                                  struct inode *inode)
2990 {
2991         int i = insert->ins_contig_index;
2992         unsigned int range;
2993         struct ocfs2_extent_rec *rec;
2994
2995         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
2996
2997         if (insert->ins_split != SPLIT_NONE) {
2998                 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
2999                 BUG_ON(i == -1);
3000                 rec = &el->l_recs[i];
3001                 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3002                                         insert_rec);
3003                 goto rotate;
3004         }
3005
3006         /*
3007          * Contiguous insert - either left or right.
3008          */
3009         if (insert->ins_contig != CONTIG_NONE) {
3010                 rec = &el->l_recs[i];
3011                 if (insert->ins_contig == CONTIG_LEFT) {
3012                         rec->e_blkno = insert_rec->e_blkno;
3013                         rec->e_cpos = insert_rec->e_cpos;
3014                 }
3015                 le16_add_cpu(&rec->e_leaf_clusters,
3016                              le16_to_cpu(insert_rec->e_leaf_clusters));
3017                 return;
3018         }
3019
3020         /*
3021          * Handle insert into an empty leaf.
3022          */
3023         if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3024             ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3025              ocfs2_is_empty_extent(&el->l_recs[0]))) {
3026                 el->l_recs[0] = *insert_rec;
3027                 el->l_next_free_rec = cpu_to_le16(1);
3028                 return;
3029         }
3030
3031         /*
3032          * Appending insert.
3033          */
3034         if (insert->ins_appending == APPEND_TAIL) {
3035                 i = le16_to_cpu(el->l_next_free_rec) - 1;
3036                 rec = &el->l_recs[i];
3037                 range = le32_to_cpu(rec->e_cpos)
3038                         + le16_to_cpu(rec->e_leaf_clusters);
3039                 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3040
3041                 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3042                                 le16_to_cpu(el->l_count),
3043                                 "inode %lu, depth %u, count %u, next free %u, "
3044                                 "rec.cpos %u, rec.clusters %u, "
3045                                 "insert.cpos %u, insert.clusters %u\n",
3046                                 inode->i_ino,
3047                                 le16_to_cpu(el->l_tree_depth),
3048                                 le16_to_cpu(el->l_count),
3049                                 le16_to_cpu(el->l_next_free_rec),
3050                                 le32_to_cpu(el->l_recs[i].e_cpos),
3051                                 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3052                                 le32_to_cpu(insert_rec->e_cpos),
3053                                 le16_to_cpu(insert_rec->e_leaf_clusters));
3054                 i++;
3055                 el->l_recs[i] = *insert_rec;
3056                 le16_add_cpu(&el->l_next_free_rec, 1);
3057                 return;
3058         }
3059
3060 rotate:
3061         /*
3062          * Ok, we have to rotate.
3063          *
3064          * At this point, it is safe to assume that inserting into an
3065          * empty leaf and appending to a leaf have both been handled
3066          * above.
3067          *
3068          * This leaf needs to have space, either by the empty 1st
3069          * extent record, or by virtue of an l_next_rec < l_count.
3070          */
3071         ocfs2_rotate_leaf(el, insert_rec);
3072 }
3073
3074 static inline void ocfs2_update_dinode_clusters(struct inode *inode,
3075                                                 struct ocfs2_dinode *di,
3076                                                 u32 clusters)
3077 {
3078         le32_add_cpu(&di->i_clusters, clusters);
3079         spin_lock(&OCFS2_I(inode)->ip_lock);
3080         OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
3081         spin_unlock(&OCFS2_I(inode)->ip_lock);
3082 }
3083
3084 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3085                                            handle_t *handle,
3086                                            struct ocfs2_path *path,
3087                                            struct ocfs2_extent_rec *insert_rec)
3088 {
3089         int ret, i, next_free;
3090         struct buffer_head *bh;
3091         struct ocfs2_extent_list *el;
3092         struct ocfs2_extent_rec *rec;
3093
3094         /*
3095          * Update everything except the leaf block.
3096          */
3097         for (i = 0; i < path->p_tree_depth; i++) {
3098                 bh = path->p_node[i].bh;
3099                 el = path->p_node[i].el;
3100
3101                 next_free = le16_to_cpu(el->l_next_free_rec);
3102                 if (next_free == 0) {
3103                         ocfs2_error(inode->i_sb,
3104                                     "Dinode %llu has a bad extent list",
3105                                     (unsigned long long)OCFS2_I(inode)->ip_blkno);
3106                         ret = -EIO;
3107                         return;
3108                 }
3109
3110                 rec = &el->l_recs[next_free - 1];
3111
3112                 rec->e_int_clusters = insert_rec->e_cpos;
3113                 le32_add_cpu(&rec->e_int_clusters,
3114                              le16_to_cpu(insert_rec->e_leaf_clusters));
3115                 le32_add_cpu(&rec->e_int_clusters,
3116                              -le32_to_cpu(rec->e_cpos));
3117
3118                 ret = ocfs2_journal_dirty(handle, bh);
3119                 if (ret)
3120                         mlog_errno(ret);
3121
3122         }
3123 }
3124
3125 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3126                                     struct ocfs2_extent_rec *insert_rec,
3127                                     struct ocfs2_path *right_path,
3128                                     struct ocfs2_path **ret_left_path)
3129 {
3130         int ret, next_free;
3131         struct ocfs2_extent_list *el;
3132         struct ocfs2_path *left_path = NULL;
3133
3134         *ret_left_path = NULL;
3135
3136         /*
3137          * This shouldn't happen for non-trees. The extent rec cluster
3138          * count manipulation below only works for interior nodes.
3139          */
3140         BUG_ON(right_path->p_tree_depth == 0);
3141
3142         /*
3143          * If our appending insert is at the leftmost edge of a leaf,
3144          * then we might need to update the rightmost records of the
3145          * neighboring path.
3146          */
3147         el = path_leaf_el(right_path);
3148         next_free = le16_to_cpu(el->l_next_free_rec);
3149         if (next_free == 0 ||
3150             (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3151                 u32 left_cpos;
3152
3153                 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3154                                                     &left_cpos);
3155                 if (ret) {
3156                         mlog_errno(ret);
3157                         goto out;
3158                 }
3159
3160                 mlog(0, "Append may need a left path update. cpos: %u, "
3161                      "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3162                      left_cpos);
3163
3164                 /*
3165                  * No need to worry if the append is already in the
3166                  * leftmost leaf.
3167                  */
3168                 if (left_cpos) {
3169                         left_path = ocfs2_new_path(path_root_bh(right_path),
3170                                                    path_root_el(right_path));
3171                         if (!left_path) {
3172                                 ret = -ENOMEM;
3173                                 mlog_errno(ret);
3174                                 goto out;
3175                         }
3176
3177                         ret = ocfs2_find_path(inode, left_path, left_cpos);
3178                         if (ret) {
3179                                 mlog_errno(ret);
3180                                 goto out;
3181                         }
3182
3183                         /*
3184                          * ocfs2_insert_path() will pass the left_path to the
3185                          * journal for us.
3186                          */
3187                 }
3188         }
3189
3190         ret = ocfs2_journal_access_path(inode, handle, right_path);
3191         if (ret) {
3192                 mlog_errno(ret);
3193                 goto out;
3194         }
3195
3196         ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
3197
3198         *ret_left_path = left_path;
3199         ret = 0;
3200 out:
3201         if (ret != 0)
3202                 ocfs2_free_path(left_path);
3203
3204         return ret;
3205 }
3206
3207 static void ocfs2_split_record(struct inode *inode,
3208                                struct ocfs2_path *left_path,
3209                                struct ocfs2_path *right_path,
3210                                struct ocfs2_extent_rec *split_rec,
3211                                enum ocfs2_split_type split)
3212 {
3213         int index;
3214         u32 cpos = le32_to_cpu(split_rec->e_cpos);
3215         struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3216         struct ocfs2_extent_rec *rec, *tmprec;
3217
3218         right_el = path_leaf_el(right_path);;
3219         if (left_path)
3220                 left_el = path_leaf_el(left_path);
3221
3222         el = right_el;
3223         insert_el = right_el;
3224         index = ocfs2_search_extent_list(el, cpos);
3225         if (index != -1) {
3226                 if (index == 0 && left_path) {
3227                         BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3228
3229                         /*
3230                          * This typically means that the record
3231                          * started in the left path but moved to the
3232                          * right as a result of rotation. We either
3233                          * move the existing record to the left, or we
3234                          * do the later insert there.
3235                          *
3236                          * In this case, the left path should always
3237                          * exist as the rotate code will have passed
3238                          * it back for a post-insert update.
3239                          */
3240
3241                         if (split == SPLIT_LEFT) {
3242                                 /*
3243                                  * It's a left split. Since we know
3244                                  * that the rotate code gave us an
3245                                  * empty extent in the left path, we
3246                                  * can just do the insert there.
3247                                  */
3248                                 insert_el = left_el;
3249                         } else {
3250                                 /*
3251                                  * Right split - we have to move the
3252                                  * existing record over to the left
3253                                  * leaf. The insert will be into the
3254                                  * newly created empty extent in the
3255                                  * right leaf.
3256                                  */
3257                                 tmprec = &right_el->l_recs[index];
3258                                 ocfs2_rotate_leaf(left_el, tmprec);
3259                                 el = left_el;
3260
3261                                 memset(tmprec, 0, sizeof(*tmprec));
3262                                 index = ocfs2_search_extent_list(left_el, cpos);
3263                                 BUG_ON(index == -1);
3264                         }
3265                 }
3266         } else {
3267                 BUG_ON(!left_path);
3268                 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3269                 /*
3270                  * Left path is easy - we can just allow the insert to
3271                  * happen.
3272                  */
3273                 el = left_el;
3274                 insert_el = left_el;
3275                 index = ocfs2_search_extent_list(el, cpos);
3276                 BUG_ON(index == -1);
3277         }
3278
3279         rec = &el->l_recs[index];
3280         ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3281         ocfs2_rotate_leaf(insert_el, split_rec);
3282 }
3283
3284 /*
3285  * This function only does inserts on an allocation b-tree. For dinode
3286  * lists, ocfs2_insert_at_leaf() is called directly.
3287  *
3288  * right_path is the path we want to do the actual insert
3289  * in. left_path should only be passed in if we need to update that
3290  * portion of the tree after an edge insert.
3291  */
3292 static int ocfs2_insert_path(struct inode *inode,
3293                              handle_t *handle,
3294                              struct ocfs2_path *left_path,
3295                              struct ocfs2_path *right_path,
3296                              struct ocfs2_extent_rec *insert_rec,
3297                              struct ocfs2_insert_type *insert)
3298 {
3299         int ret, subtree_index;
3300         struct buffer_head *leaf_bh = path_leaf_bh(right_path);
3301
3302         /*
3303          * Pass both paths to the journal. The majority of inserts
3304          * will be touching all components anyway.
3305          */
3306         ret = ocfs2_journal_access_path(inode, handle, right_path);
3307         if (ret < 0) {
3308                 mlog_errno(ret);
3309                 goto out;
3310         }
3311
3312         if (left_path) {
3313                 int credits = handle->h_buffer_credits;
3314
3315                 /*
3316                  * There's a chance that left_path got passed back to
3317                  * us without being accounted for in the
3318                  * journal. Extend our transaction here to be sure we
3319                  * can change those blocks.
3320                  */
3321                 credits += left_path->p_tree_depth;
3322
3323                 ret = ocfs2_extend_trans(handle, credits);
3324                 if (ret < 0) {
3325                         mlog_errno(ret);
3326                         goto out;
3327                 }
3328
3329                 ret = ocfs2_journal_access_path(inode, handle, left_path);
3330                 if (ret < 0) {
3331                         mlog_errno(ret);
3332                         goto out;
3333                 }
3334         }
3335
3336         if (insert->ins_split != SPLIT_NONE) {
3337                 /*
3338                  * We could call ocfs2_insert_at_leaf() for some types
3339                  * of splits, but it's easier to just let one seperate
3340                  * function sort it all out.
3341                  */
3342                 ocfs2_split_record(inode, left_path, right_path,
3343                                    insert_rec, insert->ins_split);
3344         } else
3345                 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3346                                      insert, inode);
3347
3348         ret = ocfs2_journal_dirty(handle, leaf_bh);
3349         if (ret)
3350                 mlog_errno(ret);
3351
3352         if (left_path) {
3353                 /*
3354                  * The rotate code has indicated that we need to fix
3355                  * up portions of the tree after the insert.
3356                  *
3357                  * XXX: Should we extend the transaction here?
3358                  */
3359                 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3360                                                         right_path);
3361                 ocfs2_complete_edge_insert(inode, handle, left_path,
3362                                            right_path, subtree_index);
3363         }
3364
3365         ret = 0;
3366 out:
3367         return ret;
3368 }
3369
3370 static int ocfs2_do_insert_extent(struct inode *inode,
3371                                   handle_t *handle,
3372                                   struct buffer_head *di_bh,
3373                                   struct ocfs2_extent_rec *insert_rec,
3374                                   struct ocfs2_insert_type *type)
3375 {
3376         int ret, rotate = 0;
3377         u32 cpos;
3378         struct ocfs2_path *right_path = NULL;
3379         struct ocfs2_path *left_path = NULL;
3380         struct ocfs2_dinode *di;
3381         struct ocfs2_extent_list *el;
3382
3383         di = (struct ocfs2_dinode *) di_bh->b_data;
3384         el = &di->id2.i_list;
3385
3386         ret = ocfs2_journal_access(handle, inode, di_bh,
3387                                    OCFS2_JOURNAL_ACCESS_WRITE);
3388         if (ret) {
3389                 mlog_errno(ret);
3390                 goto out;
3391         }
3392
3393         if (le16_to_cpu(el->l_tree_depth) == 0) {
3394                 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3395                 goto out_update_clusters;
3396         }
3397
3398         right_path = ocfs2_new_inode_path(di_bh);
3399         if (!right_path) {
3400                 ret = -ENOMEM;
3401                 mlog_errno(ret);
3402                 goto out;
3403         }
3404
3405         /*
3406          * Determine the path to start with. Rotations need the
3407          * rightmost path, everything else can go directly to the
3408          * target leaf.
3409          */
3410         cpos = le32_to_cpu(insert_rec->e_cpos);
3411         if (type->ins_appending == APPEND_NONE &&
3412             type->ins_contig == CONTIG_NONE) {
3413                 rotate = 1;
3414                 cpos = UINT_MAX;
3415         }
3416
3417         ret = ocfs2_find_path(inode, right_path, cpos);
3418         if (ret) {
3419                 mlog_errno(ret);
3420                 goto out;
3421         }
3422
3423         /*
3424          * Rotations and appends need special treatment - they modify
3425          * parts of the tree's above them.
3426          *
3427          * Both might pass back a path immediate to the left of the
3428          * one being inserted to. This will be cause
3429          * ocfs2_insert_path() to modify the rightmost records of
3430          * left_path to account for an edge insert.
3431          *
3432          * XXX: When modifying this code, keep in mind that an insert
3433          * can wind up skipping both of these two special cases...
3434          */
3435         if (rotate) {
3436                 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
3437                                               le32_to_cpu(insert_rec->e_cpos),
3438                                               right_path, &left_path);
3439                 if (ret) {
3440                         mlog_errno(ret);
3441                         goto out;
3442                 }
3443         } else if (type->ins_appending == APPEND_TAIL
3444                    && type->ins_contig != CONTIG_LEFT) {
3445                 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3446                                                right_path, &left_path);
3447                 if (ret) {
3448                         mlog_errno(ret);
3449                         goto out;
3450                 }
3451         }
3452
3453         ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3454                                 insert_rec, type);
3455         if (ret) {
3456                 mlog_errno(ret);
3457                 goto out;
3458         }
3459
3460 out_update_clusters:
3461         if (type->ins_split == SPLIT_NONE)
3462                 ocfs2_update_dinode_clusters(inode, di,
3463                                              le16_to_cpu(insert_rec->e_leaf_clusters));
3464
3465         ret = ocfs2_journal_dirty(handle, di_bh);
3466         if (ret)
3467                 mlog_errno(ret);
3468
3469 out:
3470         ocfs2_free_path(left_path);
3471         ocfs2_free_path(right_path);
3472
3473         return ret;
3474 }
3475
3476 static enum ocfs2_contig_type
3477 ocfs2_figure_merge_contig_type(struct inode *inode,
3478                                struct ocfs2_extent_list *el, int index,
3479                                struct ocfs2_extent_rec *split_rec)
3480 {
3481         struct ocfs2_extent_rec *rec;
3482         enum ocfs2_contig_type ret = CONTIG_NONE;
3483
3484         /*
3485          * We're careful to check for an empty extent record here -
3486          * the merge code will know what to do if it sees one.
3487          */
3488
3489         if (index > 0) {
3490                 rec = &el->l_recs[index - 1];
3491                 if (index == 1 && ocfs2_is_empty_extent(rec)) {
3492                         if (split_rec->e_cpos == el->l_recs[index].e_cpos)
3493                                 ret = CONTIG_RIGHT;
3494                 } else {
3495                         ret = ocfs2_extent_contig(inode, rec, split_rec);
3496                 }
3497         }
3498
3499         if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) {
3500                 enum ocfs2_contig_type contig_type;
3501
3502                 rec = &el->l_recs[index + 1];
3503                 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
3504
3505                 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
3506                         ret = CONTIG_LEFTRIGHT;
3507                 else if (ret == CONTIG_NONE)
3508                         ret = contig_type;
3509         }
3510
3511         return ret;
3512 }
3513
3514 static void ocfs2_figure_contig_type(struct inode *inode,
3515                                      struct ocfs2_insert_type *insert,
3516                                      struct ocfs2_extent_list *el,
3517                                      struct ocfs2_extent_rec *insert_rec)
3518 {
3519         int i;
3520         enum ocfs2_contig_type contig_type = CONTIG_NONE;
3521
3522         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3523
3524         for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
3525                 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
3526                                                   insert_rec);
3527                 if (contig_type != CONTIG_NONE) {
3528                         insert->ins_contig_index = i;
3529                         break;
3530                 }
3531         }
3532         insert->ins_contig = contig_type;
3533 }
3534
3535 /*
3536  * This should only be called against the righmost leaf extent list.
3537  *
3538  * ocfs2_figure_appending_type() will figure out whether we'll have to
3539  * insert at the tail of the rightmost leaf.
3540  *
3541  * This should also work against the dinode list for tree's with 0
3542  * depth. If we consider the dinode list to be the rightmost leaf node
3543  * then the logic here makes sense.
3544  */
3545 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
3546                                         struct ocfs2_extent_list *el,
3547                                         struct ocfs2_extent_rec *insert_rec)
3548 {
3549         int i;
3550         u32 cpos = le32_to_cpu(insert_rec->e_cpos);
3551         struct ocfs2_extent_rec *rec;
3552
3553         insert->ins_appending = APPEND_NONE;
3554
3555         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3556
3557         if (!el->l_next_free_rec)
3558                 goto set_tail_append;
3559
3560         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
3561                 /* Were all records empty? */
3562                 if (le16_to_cpu(el->l_next_free_rec) == 1)
3563                         goto set_tail_append;
3564         }
3565
3566         i = le16_to_cpu(el->l_next_free_rec) - 1;
3567         rec = &el->l_recs[i];
3568
3569         if (cpos >=
3570             (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
3571                 goto set_tail_append;
3572
3573         return;
3574
3575 set_tail_append:
3576         insert->ins_appending = APPEND_TAIL;
3577 }
3578
3579 /*
3580  * Helper function called at the begining of an insert.
3581  *
3582  * This computes a few things that are commonly used in the process of
3583  * inserting into the btree:
3584  *   - Whether the new extent is contiguous with an existing one.
3585  *   - The current tree depth.
3586  *   - Whether the insert is an appending one.
3587  *   - The total # of free records in the tree.
3588  *
3589  * All of the information is stored on the ocfs2_insert_type
3590  * structure.
3591  */
3592 static int ocfs2_figure_insert_type(struct inode *inode,
3593                                     struct buffer_head *di_bh,
3594                                     struct buffer_head **last_eb_bh,
3595                                     struct ocfs2_extent_rec *insert_rec,
3596                                     struct ocfs2_insert_type *insert)
3597 {
3598         int ret;
3599         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3600         struct ocfs2_extent_block *eb;
3601         struct ocfs2_extent_list *el;
3602         struct ocfs2_path *path = NULL;
3603         struct buffer_head *bh = NULL;
3604
3605         insert->ins_split = SPLIT_NONE;
3606
3607         el = &di->id2.i_list;
3608         insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
3609
3610         if (el->l_tree_depth) {
3611                 /*
3612                  * If we have tree depth, we read in the
3613                  * rightmost extent block ahead of time as
3614                  * ocfs2_figure_insert_type() and ocfs2_add_branch()
3615                  * may want it later.
3616                  */
3617                 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
3618                                        le64_to_cpu(di->i_last_eb_blk), &bh,
3619                                        OCFS2_BH_CACHED, inode);
3620                 if (ret) {
3621                         mlog_exit(ret);
3622                         goto out;
3623                 }
3624                 eb = (struct ocfs2_extent_block *) bh->b_data;
3625                 el = &eb->h_list;
3626         }
3627
3628         /*
3629          * Unless we have a contiguous insert, we'll need to know if
3630          * there is room left in our allocation tree for another
3631          * extent record.
3632          *
3633          * XXX: This test is simplistic, we can search for empty
3634          * extent records too.
3635          */
3636         insert->ins_free_records = le16_to_cpu(el->l_count) -
3637                 le16_to_cpu(el->l_next_free_rec);
3638
3639         if (!insert->ins_tree_depth) {
3640                 ocfs2_figure_contig_type(inode, insert, el, insert_rec);
3641                 ocfs2_figure_appending_type(insert, el, insert_rec);
3642                 return 0;
3643         }
3644
3645         path = ocfs2_new_inode_path(di_bh);
3646         if (!path) {
3647                 ret = -ENOMEM;
3648                 mlog_errno(ret);
3649                 goto out;
3650         }
3651
3652         /*
3653          * In the case that we're inserting past what the tree
3654          * currently accounts for, ocfs2_find_path() will return for
3655          * us the rightmost tree path. This is accounted for below in
3656          * the appending code.
3657          */
3658         ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
3659         if (ret) {
3660                 mlog_errno(ret);
3661                 goto out;
3662         }
3663
3664         el = path_leaf_el(path);
3665
3666         /*
3667          * Now that we have the path, there's two things we want to determine:
3668          * 1) Contiguousness (also set contig_index if this is so)
3669          *
3670          * 2) Are we doing an append? We can trivially break this up
3671          *     into two types of appends: simple record append, or a
3672          *     rotate inside the tail leaf.
3673          */
3674         ocfs2_figure_contig_type(inode, insert, el, insert_rec);
3675
3676         /*
3677          * The insert code isn't quite ready to deal with all cases of
3678          * left contiguousness. Specifically, if it's an insert into
3679          * the 1st record in a leaf, it will require the adjustment of
3680          * cluster count on the last record of the path directly to it's
3681          * left. For now, just catch that case and fool the layers
3682          * above us. This works just fine for tree_depth == 0, which
3683          * is why we allow that above.
3684          */
3685         if (insert->ins_contig == CONTIG_LEFT &&
3686             insert->ins_contig_index == 0)
3687                 insert->ins_contig = CONTIG_NONE;
3688
3689         /*
3690          * Ok, so we can simply compare against last_eb to figure out
3691          * whether the path doesn't exist. This will only happen in
3692          * the case that we're doing a tail append, so maybe we can
3693          * take advantage of that information somehow.
3694          */
3695         if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
3696                 /*
3697                  * Ok, ocfs2_find_path() returned us the rightmost
3698                  * tree path. This might be an appending insert. There are
3699                  * two cases:
3700                  *    1) We're doing a true append at the tail:
3701                  *      -This might even be off the end of the leaf
3702                  *    2) We're "appending" by rotating in the tail
3703                  */
3704                 ocfs2_figure_appending_type(insert, el, insert_rec);
3705         }
3706
3707 out:
3708         ocfs2_free_path(path);
3709
3710         if (ret == 0)
3711                 *last_eb_bh = bh;
3712         else
3713                 brelse(bh);
3714         return ret;
3715 }
3716
3717 /*
3718  * Insert an extent into an inode btree.
3719  *
3720  * The caller needs to update fe->i_clusters
3721  */
3722 int ocfs2_insert_extent(struct ocfs2_super *osb,
3723                         handle_t *handle,
3724                         struct inode *inode,
3725                         struct buffer_head *fe_bh,
3726                         u32 cpos,
3727                         u64 start_blk,
3728                         u32 new_clusters,
3729                         u8 flags,
3730                         struct ocfs2_alloc_context *meta_ac)
3731 {
3732         int status;
3733         struct buffer_head *last_eb_bh = NULL;
3734         struct ocfs2_insert_type insert = {0, };
3735         struct ocfs2_extent_rec rec;
3736
3737         mlog(0, "add %u clusters at position %u to inode %llu\n",
3738              new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3739
3740         mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
3741                         (OCFS2_I(inode)->ip_clusters != cpos),
3742                         "Device %s, asking for sparse allocation: inode %llu, "
3743                         "cpos %u, clusters %u\n",
3744                         osb->dev_str,
3745                         (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
3746                         OCFS2_I(inode)->ip_clusters);
3747
3748         memset(&rec, 0, sizeof(rec));
3749         rec.e_cpos = cpu_to_le32(cpos);
3750         rec.e_blkno = cpu_to_le64(start_blk);
3751         rec.e_leaf_clusters = cpu_to_le16(new_clusters);
3752         rec.e_flags = flags;
3753
3754         status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
3755                                           &insert);
3756         if (status < 0) {
3757                 mlog_errno(status);
3758                 goto bail;
3759         }
3760
3761         mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
3762              "Insert.contig_index: %d, Insert.free_records: %d, "
3763              "Insert.tree_depth: %d\n",
3764              insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
3765              insert.ins_free_records, insert.ins_tree_depth);
3766
3767         if (insert.ins_contig == CONTIG_NONE && insert.ins_free_records == 0) {
3768                 status = ocfs2_grow_tree(inode, handle, fe_bh,
3769                                          &insert.ins_tree_depth, &last_eb_bh,
3770                                          meta_ac);
3771                 if (status) {
3772                         mlog_errno(status);
3773                         goto bail;
3774                 }
3775         }
3776
3777         /* Finally, we can add clusters. This might rotate the tree for us. */
3778         status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
3779         if (status < 0)
3780                 mlog_errno(status);
3781         else
3782                 ocfs2_extent_map_insert_rec(inode, &rec);
3783
3784 bail:
3785         if (last_eb_bh)
3786                 brelse(last_eb_bh);
3787
3788         mlog_exit(status);
3789         return status;
3790 }
3791
3792 static void ocfs2_make_right_split_rec(struct super_block *sb,
3793                                        struct ocfs2_extent_rec *split_rec,
3794                                        u32 cpos,
3795                                        struct ocfs2_extent_rec *rec)
3796 {
3797         u32 rec_cpos = le32_to_cpu(rec->e_cpos);
3798         u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
3799
3800         memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
3801
3802         split_rec->e_cpos = cpu_to_le32(cpos);
3803         split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
3804
3805         split_rec->e_blkno = rec->e_blkno;
3806         le64_add_cpu(&split_rec->e_blkno,
3807                      ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
3808
3809         split_rec->e_flags = rec->e_flags;
3810 }
3811
3812 static int ocfs2_split_and_insert(struct inode *inode,
3813                                   handle_t *handle,
3814                                   struct ocfs2_path *path,
3815                                   struct buffer_head *di_bh,
3816                                   struct buffer_head **last_eb_bh,
3817                                   int split_index,
3818                                   struct ocfs2_extent_rec *orig_split_rec,
3819                                   struct ocfs2_alloc_context *meta_ac)
3820 {
3821         int ret = 0, depth;
3822         unsigned int insert_range, rec_range, do_leftright = 0;
3823         struct ocfs2_extent_rec tmprec;
3824         struct ocfs2_extent_list *rightmost_el;
3825         struct ocfs2_extent_rec rec;
3826         struct ocfs2_extent_rec split_rec = *orig_split_rec;
3827         struct ocfs2_insert_type insert;
3828         struct ocfs2_extent_block *eb;
3829         struct ocfs2_dinode *di;
3830
3831 leftright:
3832         /*
3833          * Store a copy of the record on the stack - it might move
3834          * around as the tree is manipulated below.
3835          */
3836         rec = path_leaf_el(path)->l_recs[split_index];
3837
3838         di = (struct ocfs2_dinode *)di_bh->b_data;
3839         rightmost_el = &di->id2.i_list;
3840
3841         depth = le16_to_cpu(rightmost_el->l_tree_depth);
3842         if (depth) {
3843                 BUG_ON(!(*last_eb_bh));
3844                 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
3845                 rightmost_el = &eb->h_list;
3846         }
3847
3848         if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
3849             le16_to_cpu(rightmost_el->l_count)) {
3850                 int old_depth = depth;
3851
3852                 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh,
3853                                       meta_ac);
3854                 if (ret) {
3855                         mlog_errno(ret);
3856                         goto out;
3857                 }
3858
3859                 if (old_depth != depth) {
3860                         eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
3861                         rightmost_el = &eb->h_list;
3862                 }
3863         }
3864
3865         memset(&insert, 0, sizeof(struct ocfs2_insert_type));
3866         insert.ins_appending = APPEND_NONE;
3867         insert.ins_contig = CONTIG_NONE;
3868         insert.ins_free_records = le16_to_cpu(rightmost_el->l_count)
3869                 - le16_to_cpu(rightmost_el->l_next_free_rec);
3870         insert.ins_tree_depth = depth;
3871
3872         insert_range = le32_to_cpu(split_rec.e_cpos) +
3873                 le16_to_cpu(split_rec.e_leaf_clusters);
3874         rec_range = le32_to_cpu(rec.e_cpos) +
3875                 le16_to_cpu(rec.e_leaf_clusters);
3876
3877         if (split_rec.e_cpos == rec.e_cpos) {
3878                 insert.ins_split = SPLIT_LEFT;
3879         } else if (insert_range == rec_range) {
3880                 insert.ins_split = SPLIT_RIGHT;
3881         } else {
3882                 /*
3883                  * Left/right split. We fake this as a right split
3884                  * first and then make a second pass as a left split.
3885                  */
3886                 insert.ins_split = SPLIT_RIGHT;
3887
3888                 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
3889                                            &rec);
3890
3891                 split_rec = tmprec;
3892
3893                 BUG_ON(do_leftright);
3894                 do_leftright = 1;
3895         }
3896
3897         ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec,
3898                                      &insert);
3899         if (ret) {
3900                 mlog_errno(ret);
3901                 goto out;
3902         }
3903
3904         if (do_leftright == 1) {
3905                 u32 cpos;
3906                 struct ocfs2_extent_list *el;
3907
3908                 do_leftright++;
3909                 split_rec = *orig_split_rec;
3910
3911                 ocfs2_reinit_path(path, 1);
3912
3913                 cpos = le32_to_cpu(split_rec.e_cpos);
3914                 ret = ocfs2_find_path(inode, path, cpos);
3915                 if (ret) {
3916                         mlog_errno(ret);
3917                         goto out;
3918                 }
3919
3920                 el = path_leaf_el(path);
3921                 split_index = ocfs2_search_extent_list(el, cpos);
3922                 goto leftright;
3923         }
3924 out:
3925
3926         return ret;
3927 }
3928
3929 /*
3930  * Mark part or all of the extent record at split_index in the leaf
3931  * pointed to by path as written. This removes the unwritten
3932  * extent flag.
3933  *
3934  * Care is taken to handle contiguousness so as to not grow the tree.
3935  *
3936  * meta_ac is not strictly necessary - we only truly need it if growth
3937  * of the tree is required. All other cases will degrade into a less
3938  * optimal tree layout.
3939  *
3940  * last_eb_bh should be the rightmost leaf block for any inode with a
3941  * 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.
3942  *
3943  * This code is optimized for readability - several passes might be
3944  * made over certain portions of the tree. All of those blocks will
3945  * have been brought into cache (and pinned via the journal), so the
3946  * extra overhead is not expressed in terms of disk reads.
3947  */
3948 static int __ocfs2_mark_extent_written(struct inode *inode,
3949                                        struct buffer_head *di_bh,
3950                                        handle_t *handle,
3951                                        struct ocfs2_path *path,
3952                                        int split_index,
3953                                        struct ocfs2_extent_rec *split_rec,
3954                                        struct ocfs2_alloc_context *meta_ac,
3955                                        struct ocfs2_cached_dealloc_ctxt *dealloc)
3956 {
3957         int ret = 0;
3958         struct ocfs2_extent_list *el = path_leaf_el(path);
3959         struct buffer_head *eb_bh, *last_eb_bh = NULL;
3960         struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3961         struct ocfs2_merge_ctxt ctxt;
3962         struct ocfs2_extent_list *rightmost_el;
3963
3964         if (!rec->e_flags & OCFS2_EXT_UNWRITTEN) {
3965                 ret = -EIO;
3966                 mlog_errno(ret);
3967                 goto out;
3968         }
3969
3970         if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
3971             ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
3972              (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
3973                 ret = -EIO;
3974                 mlog_errno(ret);
3975                 goto out;
3976         }
3977
3978         eb_bh = path_leaf_bh(path);
3979         ret = ocfs2_journal_access(handle, inode, eb_bh,
3980                                    OCFS2_JOURNAL_ACCESS_WRITE);
3981         if (ret) {
3982                 mlog_errno(ret);
3983                 goto out;
3984         }
3985
3986         ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, el,
3987                                                             split_index,
3988                                                             split_rec);
3989
3990         /*
3991          * The core merge / split code wants to know how much room is
3992          * left in this inodes allocation tree, so we pass the
3993          * rightmost extent list.
3994          */
3995         if (path->p_tree_depth) {
3996                 struct ocfs2_extent_block *eb;
3997                 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3998
3999                 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4000                                        le64_to_cpu(di->i_last_eb_blk),
4001                                        &last_eb_bh, OCFS2_BH_CACHED, inode);
4002                 if (ret) {
4003                         mlog_exit(ret);
4004                         goto out;
4005                 }
4006
4007                 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4008                 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4009                         OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4010                         ret = -EROFS;
4011                         goto out;
4012                 }
4013
4014                 rightmost_el = &eb->h_list;
4015         } else
4016                 rightmost_el = path_root_el(path);
4017
4018         ctxt.c_used_tail_recs = le16_to_cpu(rightmost_el->l_next_free_rec);
4019         if (ctxt.c_used_tail_recs > 0 &&
4020             ocfs2_is_empty_extent(&rightmost_el->l_recs[0]))
4021                 ctxt.c_used_tail_recs--;
4022
4023         if (rec->e_cpos == split_rec->e_cpos &&
4024             rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4025                 ctxt.c_split_covers_rec = 1;
4026         else
4027                 ctxt.c_split_covers_rec = 0;
4028
4029         ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4030
4031         mlog(0, "index: %d, contig: %u, used_tail_recs: %u, "
4032              "has_empty: %u, split_covers: %u\n", split_index,
4033              ctxt.c_contig_type, ctxt.c_used_tail_recs,
4034              ctxt.c_has_empty_extent, ctxt.c_split_covers_rec);
4035
4036         if (ctxt.c_contig_type == CONTIG_NONE) {
4037                 if (ctxt.c_split_covers_rec)
4038                         el->l_recs[split_index] = *split_rec;
4039                 else
4040                         ret = ocfs2_split_and_insert(inode, handle, path, di_bh,
4041                                                      &last_eb_bh, split_index,
4042                                                      split_rec, meta_ac);
4043                 if (ret)
4044                         mlog_errno(ret);
4045         } else {
4046                 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4047                                                 split_index, split_rec,
4048                                                 dealloc, &ctxt);
4049                 if (ret)
4050                         mlog_errno(ret);
4051         }
4052
4053         ocfs2_journal_dirty(handle, eb_bh);
4054
4055 out:
4056         brelse(last_eb_bh);
4057         return ret;
4058 }
4059
4060 /*
4061  * Mark the already-existing extent at cpos as written for len clusters.
4062  *
4063  * If the existing extent is larger than the request, initiate a
4064  * split. An attempt will be made at merging with adjacent extents.
4065  *
4066  * The caller is responsible for passing down meta_ac if we'll need it.
4067  */
4068 int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
4069                               handle_t *handle, u32 cpos, u32 len, u32 phys,
4070                               struct ocfs2_alloc_context *meta_ac,
4071                               struct ocfs2_cached_dealloc_ctxt *dealloc)
4072 {
4073         int ret, index;
4074         u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4075         struct ocfs2_extent_rec split_rec;
4076         struct ocfs2_path *left_path = NULL;
4077         struct ocfs2_extent_list *el;
4078
4079         mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4080              inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4081
4082         if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4083                 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4084                             "that are being written to, but the feature bit "
4085                             "is not set in the super block.",
4086                             (unsigned long long)OCFS2_I(inode)->ip_blkno);
4087                 ret = -EROFS;
4088                 goto out;
4089         }
4090
4091         /*
4092          * XXX: This should be fixed up so that we just re-insert the
4093          * next extent records.
4094          */
4095         ocfs2_extent_map_trunc(inode, 0);
4096
4097         left_path = ocfs2_new_inode_path(di_bh);
4098         if (!left_path) {
4099                 ret = -ENOMEM;
4100                 mlog_errno(ret);
4101                 goto out;
4102         }
4103
4104         ret = ocfs2_find_path(inode, left_path, cpos);
4105         if (ret) {
4106                 mlog_errno(ret);
4107                 goto out;
4108         }
4109         el = path_leaf_el(left_path);
4110
4111         index = ocfs2_search_extent_list(el, cpos);
4112         if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4113                 ocfs2_error(inode->i_sb,
4114                             "Inode %llu has an extent at cpos %u which can no "
4115                             "longer be found.\n",
4116                             (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4117                 ret = -EROFS;
4118                 goto out;
4119         }
4120
4121         memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4122         split_rec.e_cpos = cpu_to_le32(cpos);
4123         split_rec.e_leaf_clusters = cpu_to_le16(len);
4124         split_rec.e_blkno = cpu_to_le64(start_blkno);
4125         split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4126         split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4127
4128         ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path,
4129                                           index, &split_rec, meta_ac, dealloc);
4130         if (ret)
4131                 mlog_errno(ret);
4132
4133 out:
4134         ocfs2_free_path(left_path);
4135         return ret;
4136 }
4137
4138 static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
4139                             handle_t *handle, struct ocfs2_path *path,
4140                             int index, u32 new_range,
4141                             struct ocfs2_alloc_context *meta_ac)
4142 {
4143         int ret, depth, credits = handle->h_buffer_credits;
4144         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4145         struct buffer_head *last_eb_bh = NULL;
4146         struct ocfs2_extent_block *eb;
4147         struct ocfs2_extent_list *rightmost_el, *el;
4148         struct ocfs2_extent_rec split_rec;
4149         struct ocfs2_extent_rec *rec;
4150         struct ocfs2_insert_type insert;
4151
4152         /*
4153          * Setup the record to split before we grow the tree.
4154          */
4155         el = path_leaf_el(path);
4156         rec = &el->l_recs[index];
4157         ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4158
4159         depth = path->p_tree_depth;
4160         if (depth > 0) {
4161                 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
4162                                        le64_to_cpu(di->i_last_eb_blk),
4163                                        &last_eb_bh, OCFS2_BH_CACHED, inode);
4164                 if (ret < 0) {
4165                         mlog_errno(ret);
4166                         goto out;
4167                 }
4168
4169                 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4170                 rightmost_el = &eb->h_list;
4171         } else
4172                 rightmost_el = path_leaf_el(path);
4173
4174         credits += path->p_tree_depth + ocfs2_extend_meta_needed(di);
4175         ret = ocfs2_extend_trans(handle, credits);
4176         if (ret) {
4177                 mlog_errno(ret);
4178                 goto out;
4179         }
4180
4181         if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4182             le16_to_cpu(rightmost_el->l_count)) {
4183                 int old_depth = depth;
4184
4185                 ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh,
4186                                       meta_ac);
4187                 if (ret) {
4188                         mlog_errno(ret);
4189                         goto out;
4190                 }
4191
4192                 if (old_depth != depth) {
4193                         eb = (struct ocfs2_extent_block *)last_eb_bh->b_data;
4194                         rightmost_el = &eb->h_list;
4195                 }
4196         }
4197
4198         memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4199         insert.ins_appending = APPEND_NONE;
4200         insert.ins_contig = CONTIG_NONE;
4201         insert.ins_split = SPLIT_RIGHT;
4202         insert.ins_free_records = le16_to_cpu(rightmost_el->l_count)
4203                 - le16_to_cpu(rightmost_el->l_next_free_rec);
4204         insert.ins_tree_depth = depth;
4205
4206         ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert);
4207         if (ret)
4208                 mlog_errno(ret);
4209
4210 out:
4211         brelse(last_eb_bh);
4212         return ret;
4213 }
4214
4215 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4216                               struct ocfs2_path *path, int index,
4217                               struct ocfs2_cached_dealloc_ctxt *dealloc,
4218                               u32 cpos, u32 len)
4219 {
4220         int ret;
4221         u32 left_cpos, rec_range, trunc_range;
4222         int wants_rotate = 0, is_rightmost_tree_rec = 0;
4223         struct super_block *sb = inode->i_sb;
4224         struct ocfs2_path *left_path = NULL;
4225         struct ocfs2_extent_list *el = path_leaf_el(path);
4226         struct ocfs2_extent_rec *rec;
4227         struct ocfs2_extent_block *eb;
4228
4229         if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
4230                 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4231                 if (ret) {
4232                         mlog_errno(ret);
4233                         goto out;
4234                 }
4235
4236                 index--;
4237         }
4238
4239         if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
4240             path->p_tree_depth) {
4241                 /*
4242                  * Check whether this is the rightmost tree record. If
4243                  * we remove all of this record or part of its right
4244                  * edge then an update of the record lengths above it
4245                  * will be required.
4246                  */
4247                 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
4248                 if (eb->h_next_leaf_blk == 0)
4249                         is_rightmost_tree_rec = 1;
4250         }
4251
4252         rec = &el->l_recs[index];
4253         if (index == 0 && path->p_tree_depth &&
4254             le32_to_cpu(rec->e_cpos) == cpos) {
4255                 /*
4256                  * Changing the leftmost offset (via partial or whole
4257                  * record truncate) of an interior (or rightmost) path
4258                  * means we have to update the subtree that is formed
4259                  * by this leaf and the one to it's left.
4260                  *
4261                  * There are two cases we can skip:
4262                  *   1) Path is the leftmost one in our inode tree.
4263                  *   2) The leaf is rightmost and will be empty after
4264                  *      we remove the extent record - the rotate code
4265                  *      knows how to update the newly formed edge.
4266                  */
4267
4268                 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
4269                                                     &left_cpos);
4270                 if (ret) {
4271                         mlog_errno(ret);
4272                         goto out;
4273                 }
4274
4275                 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
4276                         left_path = ocfs2_new_path(path_root_bh(path),
4277                                                    path_root_el(path));
4278                         if (!left_path) {
4279                                 ret = -ENOMEM;
4280                                 mlog_errno(ret);
4281                                 goto out;
4282                         }
4283
4284                         ret = ocfs2_find_path(inode, left_path, left_cpos);
4285                         if (ret) {
4286                                 mlog_errno(ret);
4287                                 goto out;
4288                         }
4289                 }
4290         }
4291
4292         ret = ocfs2_extend_rotate_transaction(handle, 0,
4293                                               handle->h_buffer_credits,
4294                                               path);
4295         if (ret) {
4296                 mlog_errno(ret);
4297                 goto out;
4298         }
4299
4300         ret = ocfs2_journal_access_path(inode, handle, path);
4301         if (ret) {
4302                 mlog_errno(ret);
4303                 goto out;
4304         }
4305
4306         ret = ocfs2_journal_access_path(inode, handle, left_path);
4307         if (ret) {
4308                 mlog_errno(ret);
4309                 goto out;
4310         }
4311
4312         rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4313         trunc_range = cpos + len;
4314
4315         if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
4316                 int next_free;
4317
4318                 memset(rec, 0, sizeof(*rec));
4319                 ocfs2_cleanup_merge(el, index);
4320                 wants_rotate = 1;
4321
4322                 next_free = le16_to_cpu(el->l_next_free_rec);
4323                 if (is_rightmost_tree_rec && next_free > 1) {
4324                         /*
4325                          * We skip the edge update if this path will
4326                          * be deleted by the rotate code.
4327                          */
4328                         rec = &el->l_recs[next_free - 1];
4329                         ocfs2_adjust_rightmost_records(inode, handle, path,
4330                                                        rec);
4331                 }
4332         } else if (le32_to_cpu(rec->e_cpos) == cpos) {
4333                 /* Remove leftmost portion of the record. */
4334                 le32_add_cpu(&rec->e_cpos, len);
4335                 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
4336                 le16_add_cpu(&rec->e_leaf_clusters, -len);
4337         } else if (rec_range == trunc_range) {
4338                 /* Remove rightmost portion of the record */
4339                 le16_add_cpu(&rec->e_leaf_clusters, -len);
4340                 if (is_rightmost_tree_rec)
4341                         ocfs2_adjust_rightmost_records(inode, handle, path, rec);
4342         } else {
4343                 /* Caller should have trapped this. */
4344                 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
4345                      "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
4346                      le32_to_cpu(rec->e_cpos),
4347                      le16_to_cpu(rec->e_leaf_clusters), cpos, len);
4348                 BUG();
4349         }
4350
4351         if (left_path) {
4352                 int subtree_index;
4353
4354                 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
4355                 ocfs2_complete_edge_insert(inode, handle, left_path, path,
4356                                            subtree_index);
4357         }
4358
4359         ocfs2_journal_dirty(handle, path_leaf_bh(path));
4360
4361         ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
4362         if (ret) {
4363                 mlog_errno(ret);
4364                 goto out;
4365         }
4366
4367 out:
4368         ocfs2_free_path(left_path);
4369         return ret;
4370 }
4371
4372 int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
4373                         u32 cpos, u32 len, handle_t *handle,
4374                         struct ocfs2_alloc_context *meta_ac,
4375                         struct ocfs2_cached_dealloc_ctxt *dealloc)
4376 {
4377         int ret, index;
4378         u32 rec_range, trunc_range;
4379         struct ocfs2_extent_rec *rec;
4380         struct ocfs2_extent_list *el;
4381         struct ocfs2_path *path;
4382
4383         ocfs2_extent_map_trunc(inode, 0);
4384
4385         path = ocfs2_new_inode_path(di_bh);
4386         if (!path) {
4387                 ret = -ENOMEM;
4388                 mlog_errno(ret);
4389                 goto out;
4390         }
4391
4392         ret = ocfs2_find_path(inode, path, cpos);
4393         if (ret) {
4394                 mlog_errno(ret);
4395                 goto out;
4396         }
4397
4398         el = path_leaf_el(path);
4399         index = ocfs2_search_extent_list(el, cpos);
4400         if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4401                 ocfs2_error(inode->i_sb,
4402                             "Inode %llu has an extent at cpos %u which can no "
4403                             "longer be found.\n",
4404                             (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4405                 ret = -EROFS;
4406                 goto out;
4407         }
4408
4409         /*
4410          * We have 3 cases of extent removal:
4411          *   1) Range covers the entire extent rec
4412          *   2) Range begins or ends on one edge of the extent rec
4413          *   3) Range is in the middle of the extent rec (no shared edges)
4414          *
4415          * For case 1 we remove the extent rec and left rotate to
4416          * fill the hole.
4417          *
4418          * For case 2 we just shrink the existing extent rec, with a
4419          * tree update if the shrinking edge is also the edge of an
4420          * extent block.
4421          *
4422          * For case 3 we do a right split to turn the extent rec into
4423          * something case 2 can handle.
4424          */
4425         rec = &el->l_recs[index];
4426         rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
4427         trunc_range = cpos + len;
4428
4429         BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
4430
4431         mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
4432              "(cpos %u, len %u)\n",
4433              (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
4434              le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
4435
4436         if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
4437                 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4438                                          cpos, len);
4439                 if (ret) {
4440                         mlog_errno(ret);
4441                         goto out;
4442                 }
4443         } else {
4444                 ret = ocfs2_split_tree(inode, di_bh, handle, path, index,
4445                                        trunc_range, meta_ac);
4446                 if (ret) {
4447                         mlog_errno(ret);
4448                         goto out;
4449                 }
4450
4451                 /*
4452                  * The split could have manipulated the tree enough to
4453                  * move the record location, so we have to look for it again.
4454                  */
4455                 ocfs2_reinit_path(path, 1);
4456
4457                 ret = ocfs2_find_path(inode, path, cpos);
4458                 if (ret) {
4459                         mlog_errno(ret);
4460                         goto out;
4461                 }
4462
4463                 el = path_leaf_el(path);
4464                 index = ocfs2_search_extent_list(el, cpos);
4465                 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4466                         ocfs2_error(inode->i_sb,
4467                                     "Inode %llu: split at cpos %u lost record.",
4468                                     (unsigned long long)OCFS2_I(inode)->ip_blkno,
4469                                     cpos);
4470                         ret = -EROFS;
4471                         goto out;
4472                 }
4473
4474                 /*
4475                  * Double check our values here. If anything is fishy,
4476                  * it's easier to catch it at the top level.
4477                  */
4478                 rec = &el->l_recs[index];
4479                 rec_range = le32_to_cpu(rec->e_cpos) +
4480                         ocfs2_rec_clusters(el, rec);
4481                 if (rec_range != trunc_range) {
4482                         ocfs2_error(inode->i_sb,
4483                                     "Inode %llu: error after split at cpos %u"
4484                                     "trunc len %u, existing record is (%u,%u)",
4485                                     (unsigned long long)OCFS2_I(inode)->ip_blkno,
4486                                     cpos, len, le32_to_cpu(rec->e_cpos),
4487                                     ocfs2_rec_clusters(el, rec));
4488                         ret = -EROFS;
4489                         goto out;
4490                 }
4491
4492                 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
4493                                          cpos, len);
4494                 if (ret) {
4495                         mlog_errno(ret);
4496                         goto out;
4497                 }
4498         }
4499
4500 out:
4501         ocfs2_free_path(path);
4502         return ret;
4503 }
4504
4505 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
4506 {
4507         struct buffer_head *tl_bh = osb->osb_tl_bh;
4508         struct ocfs2_dinode *di;
4509         struct ocfs2_truncate_log *tl;
4510
4511         di = (struct ocfs2_dinode *) tl_bh->b_data;
4512         tl = &di->id2.i_dealloc;
4513
4514         mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
4515                         "slot %d, invalid truncate log parameters: used = "
4516                         "%u, count = %u\n", osb->slot_num,
4517                         le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
4518         return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
4519 }
4520
4521 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
4522                                            unsigned int new_start)
4523 {
4524         unsigned int tail_index;
4525         unsigned int current_tail;
4526
4527         /* No records, nothing to coalesce */
4528         if (!le16_to_cpu(tl->tl_used))
4529                 return 0;
4530
4531         tail_index = le16_to_cpu(tl->tl_used) - 1;
4532         current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
4533         current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
4534
4535         return current_tail == new_start;
4536 }
4537
4538 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
4539                               handle_t *handle,
4540                               u64 start_blk,
4541                               unsigned int num_clusters)
4542 {
4543         int status, index;
4544         unsigned int start_cluster, tl_count;
4545         struct inode *tl_inode = osb->osb_tl_inode;
4546         struct buffer_head *tl_bh = osb->osb_tl_bh;
4547         struct ocfs2_dinode *di;
4548         struct ocfs2_truncate_log *tl;
4549
4550         mlog_entry("start_blk = %llu, num_clusters = %u\n",
4551                    (unsigned long long)start_blk, num_clusters);
4552
4553         BUG_ON(mutex_trylock(&tl_inode->i_mutex));
4554
4555         start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
4556
4557         di = (struct ocfs2_dinode *) tl_bh->b_data;
4558         tl = &di->id2.i_dealloc;
4559         if (!OCFS2_IS_VALID_DINODE(di)) {
4560                 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
4561                 status = -EIO;
4562                 goto bail;
4563         }
4564
4565         tl_count = le16_to_cpu(tl->tl_count);
4566         mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
4567                         tl_count == 0,
4568                         "Truncate record count on #%llu invalid "
4569                         "wanted %u, actual %u\n",
4570                         (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
4571                         ocfs2_truncate_recs_per_inode(osb->sb),
4572                         le16_to_cpu(tl->tl_count));
4573
4574         /* Caller should have known to flush before calling us. */
4575         index = le16_to_cpu(tl->tl_used);
4576         if (index >= tl_count) {
4577                 status = -ENOSPC;
4578                 mlog_errno(status);
4579                 goto bail;
4580         }
4581
4582         status = ocfs2_journal_access(handle, tl_inode, tl_bh,
4583                                       OCFS2_JOURNAL_ACCESS_WRITE);
4584         if (status < 0) {
4585                 mlog_errno(status);
4586                 goto bail;
4587         }
4588
4589         mlog(0, "Log truncate of %u clusters starting at cluster %u to "
4590              "%llu (index = %d)\n", num_clusters, start_cluster,
4591              (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
4592
4593         if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
4594                 /*
4595                  * Move index back to the record we are coalescing with.
4596                  * ocfs2_truncate_log_can_coalesce() guarantees nonzero
4597                  */
4598                 index--;
4599
4600                 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
4601                 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
4602                      index, le32_to_cpu(tl->tl_recs[index].t_start),
4603                      num_clusters);
4604         } else {
4605                 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
4606                 tl->tl_used = cpu_to_le16(index + 1);
4607         }
4608         tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
4609
4610         status = ocfs2_journal_dirty(handle, tl_bh);
4611         if (status < 0) {
4612                 mlog_errno(status);
4613                 goto bail;
4614         }
4615
4616 bail:
4617         mlog_exit(status);
4618         return status;
4619 }
4620
4621 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
4622                                          handle_t *handle,
4623                                          struct inode *data_alloc_inode,
4624                                          struct buffer_head *data_alloc_bh)
4625 {
4626         int status = 0;
4627         int i;
4628         unsigned int num_clusters;
4629         u64 start_blk;
4630         struct ocfs2_truncate_rec rec;
4631         struct ocfs2_dinode *di;
4632         struct ocfs2_truncate_log *tl;
4633         struct inode *tl_inode = osb->osb_tl_inode;
4634         struct buffer_head *tl_bh = osb->osb_tl_bh;
4635
4636         mlog_entry_void();
4637
4638         di = (struct ocfs2_dinode *) tl_bh->b_data;
4639         tl = &di->id2.i_dealloc;
4640         i = le16_to_cpu(tl->tl_used) - 1;
4641         while (i >= 0) {
4642                 /* Caller has given us at least enough credits to
4643                  * update the truncate log dinode */
4644                 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
4645                                               OCFS2_JOURNAL_ACCESS_WRITE);
4646                 if (status < 0) {
4647                         mlog_errno(status);
4648                         goto bail;
4649                 }
4650
4651                 tl->tl_used = cpu_to_le16(i);
4652
4653                 status = ocfs2_journal_dirty(handle, tl_bh);
4654                 if (status < 0) {
4655                         mlog_errno(status);
4656                         goto bail;
4657                 }
4658
4659                 /* TODO: Perhaps we can calculate the bulk of the
4660                  * credits up front rather than extending like
4661                  * this. */
4662                 status = ocfs2_extend_trans(handle,
4663                                             OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
4664                 if (status < 0) {
4665                         mlog_errno(status);
4666                         goto bail;
4667                 }
4668
4669                 rec = tl->tl_recs[i];
4670                 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
4671                                                     le32_to_cpu(rec.t_start));
4672                 num_clusters = le32_to_cpu(rec.t_clusters);
4673
4674                 /* if start_blk is not set, we ignore the record as
4675                  * invalid. */
4676                 if (start_blk) {
4677                         mlog(0, "free record %d, start = %u, clusters = %u\n",
4678                              i, le32_to_cpu(rec.t_start), num_clusters);
4679
4680                         status = ocfs2_free_clusters(handle, data_alloc_inode,
4681                                                      data_alloc_bh, start_blk,
4682                                                      num_clusters);
4683                         if (status < 0) {
4684                                 mlog_errno(status);
4685                                 goto bail;
4686                         }
4687                 }
4688                 i--;
4689         }
4690
4691 bail:
4692         mlog_exit(status);
4693         return status;
4694 }
4695
4696 /* Expects you to already be holding tl_inode->i_mutex */
4697 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
4698 {
4699         int status;
4700         unsigned int num_to_flush;
4701         handle_t *handle;
4702         struct inode *tl_inode = osb->osb_tl_inode;
4703         struct inode *data_alloc_inode = NULL;
4704         struct buffer_head *tl_bh = osb->osb_tl_bh;
4705         struct buffer_head *data_alloc_bh = NULL;
4706         struct ocfs2_dinode *di;
4707         struct ocfs2_truncate_log *tl;
4708
4709         mlog_entry_void();
4710
4711         BUG_ON(mutex_trylock(&tl_inode->i_mutex));
4712
4713         di = (struct ocfs2_dinode *) tl_bh->b_data;
4714         tl = &di->id2.i_dealloc;
4715         if (!OCFS2_IS_VALID_DINODE(di)) {
4716                 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
4717                 status = -EIO;
4718                 goto out;
4719         }
4720
4721         num_to_flush = le16_to_cpu(tl->tl_used);
4722         mlog(0, "Flush %u records from truncate log #%llu\n",
4723              num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
4724         if (!num_to_flush) {
4725                 status = 0;
4726                 goto out;
4727         }
4728
4729         data_alloc_inode = ocfs2_get_system_file_inode(osb,
4730                                                        GLOBAL_BITMAP_SYSTEM_INODE,
4731                                                        OCFS2_INVALID_SLOT);
4732         if (!data_alloc_inode) {
4733                 status = -EINVAL;
4734                 mlog(ML_ERROR, "Could not get bitmap inode!\n");
4735                 goto out;
4736         }
4737
4738         mutex_lock(&data_alloc_inode->i_mutex);
4739
4740         status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
4741         if (status < 0) {
4742                 mlog_errno(status);
4743                 goto out_mutex;
4744         }
4745
4746         handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
4747         if (IS_ERR(handle)) {
4748                 status = PTR_ERR(handle);
4749                 mlog_errno(status);
4750                 goto out_unlock;
4751         }
4752
4753         status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
4754                                                data_alloc_bh);
4755         if (status < 0)
4756                 mlog_errno(status);
4757
4758         ocfs2_commit_trans(osb, handle);
4759
4760 out_unlock:
4761         brelse(data_alloc_bh);
4762         ocfs2_meta_unlock(data_alloc_inode, 1);
4763
4764 out_mutex:
4765         mutex_unlock(&data_alloc_inode->i_mutex);
4766         iput(data_alloc_inode);
4767
4768 out:
4769         mlog_exit(status);
4770         return status;
4771 }
4772
4773 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
4774 {
4775         int status;
4776         struct inode *tl_inode = osb->osb_tl_inode;
4777
4778         mutex_lock(&tl_inode->i_mutex);
4779         status = __ocfs2_flush_truncate_log(osb);
4780         mutex_unlock(&tl_inode->i_mutex);
4781
4782         return status;
4783 }
4784
4785 static void ocfs2_truncate_log_worker(struct work_struct *work)
4786 {
4787         int status;
4788         struct ocfs2_super *osb =
4789                 container_of(work, struct ocfs2_super,
4790                              osb_truncate_log_wq.work);
4791
4792         mlog_entry_void();
4793
4794         status = ocfs2_flush_truncate_log(osb);
4795         if (status < 0)
4796                 mlog_errno(status);
4797
4798         mlog_exit(status);
4799 }
4800
4801 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
4802 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
4803                                        int cancel)
4804 {
4805         if (osb->osb_tl_inode) {
4806                 /* We want to push off log flushes while truncates are
4807                  * still running. */
4808                 if (cancel)
4809                         cancel_delayed_work(&osb->osb_truncate_log_wq);
4810
4811                 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
4812                                    OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
4813         }
4814 }
4815
4816 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
4817                                        int slot_num,
4818                                        struct inode **tl_inode,
4819                                        struct buffer_head **tl_bh)
4820 {
4821         int status;
4822         struct inode *inode = NULL;
4823         struct buffer_head *bh = NULL;
4824
4825         inode = ocfs2_get_system_file_inode(osb,
4826                                            TRUNCATE_LOG_SYSTEM_INODE,
4827                                            slot_num);
4828         if (!inode) {
4829                 status = -EINVAL;
4830                 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
4831                 goto bail;
4832         }
4833
4834         status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
4835                                   OCFS2_BH_CACHED, inode);
4836         if (status < 0) {
4837                 iput(inode);
4838                 mlog_errno(status);
4839                 goto bail;
4840         }
4841
4842         *tl_inode = inode;
4843         *tl_bh    = bh;
4844 bail:
4845         mlog_exit(status);
4846         return status;
4847 }
4848
4849 /* called during the 1st stage of node recovery. we stamp a clean
4850  * truncate log and pass back a copy for processing later. if the
4851  * truncate log does not require processing, a *tl_copy is set to
4852  * NULL. */
4853 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
4854                                       int slot_num,
4855                                       struct ocfs2_dinode **tl_copy)
4856 {
4857         int status;
4858         struct inode *tl_inode = NULL;
4859         struct buffer_head *tl_bh = NULL;
4860         struct ocfs2_dinode *di;
4861         struct ocfs2_truncate_log *tl;
4862
4863         *tl_copy = NULL;
4864
4865         mlog(0, "recover truncate log from slot %d\n", slot_num);
4866
4867         status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
4868         if (status < 0) {
4869                 mlog_errno(status);
4870                 goto bail;
4871         }
4872
4873         di = (struct ocfs2_dinode *) tl_bh->b_data;
4874         tl = &di->id2.i_dealloc;
4875         if (!OCFS2_IS_VALID_DINODE(di)) {
4876                 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
4877                 status = -EIO;
4878                 goto bail;
4879         }
4880
4881         if (le16_to_cpu(tl->tl_used)) {
4882                 mlog(0, "We'll have %u logs to recover\n",
4883                      le16_to_cpu(tl->tl_used));
4884
4885                 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
4886                 if (!(*tl_copy)) {
4887                         status = -ENOMEM;
4888                         mlog_errno(status);
4889                         goto bail;
4890                 }
4891
4892                 /* Assuming the write-out below goes well, this copy
4893                  * will be passed back to recovery for processing. */
4894                 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
4895
4896                 /* All we need to do to clear the truncate log is set
4897                  * tl_used. */
4898                 tl->tl_used = 0;
4899
4900                 status = ocfs2_write_block(osb, tl_bh, tl_inode);
4901                 if (status < 0) {
4902                         mlog_errno(status);
4903                         goto bail;
4904                 }
4905         }
4906
4907 bail:
4908         if (tl_inode)
4909                 iput(tl_inode);
4910         if (tl_bh)
4911                 brelse(tl_bh);
4912
4913         if (status < 0 && (*tl_copy)) {
4914                 kfree(*tl_copy);
4915                 *tl_copy = NULL;
4916         }
4917
4918         mlog_exit(status);
4919         return status;
4920 }
4921
4922 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
4923                                          struct ocfs2_dinode *tl_copy)
4924 {
4925         int status = 0;
4926         int i;
4927         unsigned int clusters, num_recs, start_cluster;
4928         u64 start_blk;
4929         handle_t *handle;
4930         struct inode *tl_inode = osb->osb_tl_inode;
4931         struct ocfs2_truncate_log *tl;
4932
4933         mlog_entry_void();
4934
4935         if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
4936                 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
4937                 return -EINVAL;
4938         }
4939
4940         tl = &tl_copy->id2.i_dealloc;
4941         num_recs = le16_to_cpu(tl->tl_used);
4942         mlog(0, "cleanup %u records from %llu\n", num_recs,
4943              (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
4944
4945         mutex_lock(&tl_inode->i_mutex);
4946         for(i = 0; i < num_recs; i++) {
4947                 if (ocfs2_truncate_log_needs_flush(osb)) {
4948                         status = __ocfs2_flush_truncate_log(osb);
4949                         if (status < 0) {
4950                                 mlog_errno(status);
4951                                 goto bail_up;
4952                         }
4953                 }
4954
4955                 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
4956                 if (IS_ERR(handle)) {
4957                         status = PTR_ERR(handle);
4958                         mlog_errno(status);
4959                         goto bail_up;
4960                 }
4961
4962                 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
4963                 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
4964                 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
4965
4966                 status = ocfs2_truncate_log_append(osb, handle,
4967                                                    start_blk, clusters);
4968                 ocfs2_commit_trans(osb, handle);
4969                 if (status < 0) {
4970                         mlog_errno(status);
4971                         goto bail_up;
4972                 }
4973         }
4974
4975 bail_up:
4976         mutex_unlock(&tl_inode->i_mutex);
4977
4978         mlog_exit(status);
4979         return status;
4980 }
4981
4982 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
4983 {
4984         int status;
4985         struct inode *tl_inode = osb->osb_tl_inode;
4986
4987         mlog_entry_void();
4988
4989         if (tl_inode) {
4990                 cancel_delayed_work(&osb->osb_truncate_log_wq);
4991                 flush_workqueue(ocfs2_wq);
4992
4993                 status = ocfs2_flush_truncate_log(osb);
4994                 if (status < 0)
4995                         mlog_errno(status);
4996
4997                 brelse(osb->osb_tl_bh);
4998                 iput(osb->osb_tl_inode);
4999         }
5000
5001         mlog_exit_void();
5002 }
5003
5004 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5005 {
5006         int status;
5007         struct inode *tl_inode = NULL;
5008         struct buffer_head *tl_bh = NULL;
5009
5010         mlog_entry_void();
5011
5012         status = ocfs2_get_truncate_log_info(osb,
5013                                              osb->slot_num,
5014                                              &tl_inode,
5015                                              &tl_bh);
5016         if (status < 0)
5017                 mlog_errno(status);
5018
5019         /* ocfs2_truncate_log_shutdown keys on the existence of
5020          * osb->osb_tl_inode so we don't set any of the osb variables
5021          * until we're sure all is well. */
5022         INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5023                           ocfs2_truncate_log_worker);
5024         osb->osb_tl_bh    = tl_bh;
5025         osb->osb_tl_inode = tl_inode;
5026
5027         mlog_exit(status);
5028         return status;
5029 }
5030
5031 /*
5032  * Delayed de-allocation of suballocator blocks.
5033  *
5034  * Some sets of block de-allocations might involve multiple suballocator inodes.
5035  *
5036  * The locking for this can get extremely complicated, especially when
5037  * the suballocator inodes to delete from aren't known until deep
5038  * within an unrelated codepath.
5039  *
5040  * ocfs2_extent_block structures are a good example of this - an inode
5041  * btree could have been grown by any number of nodes each allocating
5042  * out of their own suballoc inode.
5043  *
5044  * These structures allow the delay of block de-allocation until a
5045  * later time, when locking of multiple cluster inodes won't cause
5046  * deadlock.
5047  */
5048
5049 /*
5050  * Describes a single block free from a suballocator
5051  */
5052 struct ocfs2_cached_block_free {
5053         struct ocfs2_cached_block_free          *free_next;
5054         u64                                     free_blk;
5055         unsigned int                            free_bit;
5056 };
5057
5058 struct ocfs2_per_slot_free_list {
5059         struct ocfs2_per_slot_free_list         *f_next_suballocator;
5060         int                                     f_inode_type;
5061         int                                     f_slot;
5062         struct ocfs2_cached_block_free          *f_first;
5063 };
5064
5065 static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5066                                    int sysfile_type,
5067                                    int slot,
5068                                    struct ocfs2_cached_block_free *head)
5069 {
5070         int ret;
5071         u64 bg_blkno;
5072         handle_t *handle;
5073         struct inode *inode;
5074         struct buffer_head *di_bh = NULL;
5075         struct ocfs2_cached_block_free *tmp;
5076
5077         inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5078         if (!inode) {
5079                 ret = -EINVAL;
5080                 mlog_errno(ret);
5081                 goto out;
5082         }
5083
5084         mutex_lock(&inode->i_mutex);
5085
5086         ret = ocfs2_meta_lock(inode, &di_bh, 1);
5087         if (ret) {
5088                 mlog_errno(ret);
5089                 goto out_mutex;
5090         }
5091
5092         handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5093         if (IS_ERR(handle)) {
5094                 ret = PTR_ERR(handle);
5095                 mlog_errno(ret);
5096                 goto out_unlock;
5097         }
5098
5099         while (head) {
5100                 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5101                                                       head->free_bit);
5102                 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5103                      head->free_bit, (unsigned long long)head->free_blk);
5104
5105                 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5106                                                head->free_bit, bg_blkno, 1);
5107                 if (ret) {
5108                         mlog_errno(ret);
5109                         goto out_journal;
5110                 }
5111
5112                 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5113                 if (ret) {
5114                         mlog_errno(ret);
5115                         goto out_journal;
5116                 }
5117
5118                 tmp = head;
5119                 head = head->free_next;
5120                 kfree(tmp);
5121         }
5122
5123 out_journal:
5124         ocfs2_commit_trans(osb, handle);
5125
5126 out_unlock:
5127         ocfs2_meta_unlock(inode, 1);
5128         brelse(di_bh);
5129 out_mutex:
5130         mutex_unlock(&inode->i_mutex);
5131         iput(inode);
5132 out:
5133         while(head) {
5134                 /* Premature exit may have left some dangling items. */
5135                 tmp = head;
5136                 head = head->free_next;
5137                 kfree(tmp);
5138         }
5139
5140         return ret;
5141 }
5142
5143 int ocfs2_run_deallocs(struct ocfs2_super *osb,
5144                        struct ocfs2_cached_dealloc_ctxt *ctxt)
5145 {
5146         int ret = 0, ret2;
5147         struct ocfs2_per_slot_free_list *fl;
5148
5149         if (!ctxt)
5150                 return 0;
5151
5152         while (ctxt->c_first_suballocator) {
5153                 fl = ctxt->c_first_suballocator;
5154
5155                 if (fl->f_first) {
5156                         mlog(0, "Free items: (type %u, slot %d)\n",
5157                              fl->f_inode_type, fl->f_slot);
5158                         ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5159                                                        fl->f_slot, fl->f_first);
5160                         if (ret2)
5161                                 mlog_errno(ret2);
5162                         if (!ret)
5163                                 ret = ret2;
5164                 }
5165
5166                 ctxt->c_first_suballocator = fl->f_next_suballocator;
5167                 kfree(fl);
5168         }
5169
5170         return ret;
5171 }
5172
5173 static struct ocfs2_per_slot_free_list *
5174 ocfs2_find_per_slot_free_list(int type,
5175                               int slot,
5176                               struct ocfs2_cached_dealloc_ctxt *ctxt)
5177 {
5178         struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5179
5180         while (fl) {
5181                 if (fl->f_inode_type == type && fl->f_slot == slot)
5182                         return fl;
5183
5184                 fl = fl->f_next_suballocator;
5185         }
5186
5187         fl = kmalloc(sizeof(*fl), GFP_NOFS);
5188         if (fl) {
5189                 fl->f_inode_type = type;
5190                 fl->f_slot = slot;
5191                 fl->f_first = NULL;
5192                 fl->f_next_suballocator = ctxt->c_first_suballocator;
5193
5194                 ctxt->c_first_suballocator = fl;
5195         }
5196         return fl;
5197 }
5198
5199 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
5200                                      int type, int slot, u64 blkno,
5201                                      unsigned int bit)
5202 {
5203         int ret;
5204         struct ocfs2_per_slot_free_list *fl;
5205         struct ocfs2_cached_block_free *item;
5206
5207         fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
5208         if (fl == NULL) {
5209                 ret = -ENOMEM;
5210                 mlog_errno(ret);
5211                 goto out;
5212         }
5213
5214         item = kmalloc(sizeof(*item), GFP_NOFS);
5215         if (item == NULL) {
5216                 ret = -ENOMEM;
5217                 mlog_errno(ret);
5218                 goto out;
5219         }
5220
5221         mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
5222              type, slot, bit, (unsigned long long)blkno);
5223
5224         item->free_blk = blkno;
5225         item->free_bit = bit;
5226         item->free_next = fl->f_first;
5227
5228         fl->f_first = item;
5229
5230         ret = 0;
5231 out:
5232         return ret;
5233 }
5234
5235 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
5236                                          struct ocfs2_extent_block *eb)
5237 {
5238         return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
5239                                          le16_to_cpu(eb->h_suballoc_slot),
5240                                          le64_to_cpu(eb->h_blkno),
5241                                          le16_to_cpu(eb->h_suballoc_bit));
5242 }
5243
5244 /* This function will figure out whether the currently last extent
5245  * block will be deleted, and if it will, what the new last extent
5246  * block will be so we can update his h_next_leaf_blk field, as well
5247  * as the dinodes i_last_eb_blk */
5248 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
5249                                        unsigned int clusters_to_del,
5250                                        struct ocfs2_path *path,
5251                                        struct buffer_head **new_last_eb)
5252 {
5253         int next_free, ret = 0;
5254         u32 cpos;
5255         struct ocfs2_extent_rec *rec;
5256         struct ocfs2_extent_block *eb;
5257         struct ocfs2_extent_list *el;
5258         struct buffer_head *bh = NULL;
5259
5260         *new_last_eb = NULL;
5261
5262         /* we have no tree, so of course, no last_eb. */
5263         if (!path->p_tree_depth)
5264                 goto out;
5265
5266         /* trunc to zero special case - this makes tree_depth = 0
5267          * regardless of what it is.  */
5268         if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
5269                 goto out;
5270
5271         el = path_leaf_el(path);
5272         BUG_ON(!el->l_next_free_rec);
5273
5274         /*
5275          * Make sure that this extent list will actually be empty
5276          * after we clear away the data. We can shortcut out if
5277          * there's more than one non-empty extent in the
5278          * list. Otherwise, a check of the remaining extent is
5279          * necessary.
5280          */
5281         next_free = le16_to_cpu(el->l_next_free_rec);
5282         rec = NULL;
5283         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
5284                 if (next_free > 2)
5285                         goto out;
5286
5287                 /* We may have a valid extent in index 1, check it. */
5288                 if (next_free == 2)
5289                         rec = &el->l_recs[1];
5290
5291                 /*
5292                  * Fall through - no more nonempty extents, so we want
5293                  * to delete this leaf.
5294                  */
5295         } else {
5296                 if (next_free > 1)
5297                         goto out;
5298
5299                 rec = &el->l_recs[0];
5300         }
5301
5302         if (rec) {
5303                 /*
5304                  * Check it we'll only be trimming off the end of this
5305                  * cluster.
5306                  */
5307                 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
5308                         goto out;
5309         }
5310
5311         ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
5312         if (ret) {
5313                 mlog_errno(ret);
5314                 goto out;
5315         }
5316
5317         ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
5318         if (ret) {
5319                 mlog_errno(ret);
5320                 goto out;
5321         }
5322
5323         eb = (struct ocfs2_extent_block *) bh->b_data;
5324         el = &eb->h_list;
5325         if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
5326                 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
5327                 ret = -EROFS;
5328                 goto out;
5329         }
5330
5331         *new_last_eb = bh;
5332         get_bh(*new_last_eb);
5333         mlog(0, "returning block %llu, (cpos: %u)\n",
5334              (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
5335 out:
5336         brelse(bh);
5337
5338         return ret;
5339 }
5340
5341 /*
5342  * Trim some clusters off the rightmost edge of a tree. Only called
5343  * during truncate.
5344  *
5345  * The caller needs to:
5346  *   - start journaling of each path component.
5347  *   - compute and fully set up any new last ext block
5348  */
5349 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
5350                            handle_t *handle, struct ocfs2_truncate_context *tc,
5351                            u32 clusters_to_del, u64 *delete_start)
5352 {
5353         int ret, i, index = path->p_tree_depth;
5354         u32 new_edge = 0;
5355         u64 deleted_eb = 0;
5356         struct buffer_head *bh;
5357         struct ocfs2_extent_list *el;
5358         struct ocfs2_extent_rec *rec;
5359
5360         *delete_start = 0;
5361
5362         while (index >= 0) {
5363                 bh = path->p_node[index].bh;
5364                 el = path->p_node[index].el;
5365
5366                 mlog(0, "traveling tree (index = %d, block = %llu)\n",
5367                      index,  (unsigned long long)bh->b_blocknr);
5368
5369                 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
5370
5371                 if (index !=
5372                     (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
5373                         ocfs2_error(inode->i_sb,
5374                                     "Inode %lu has invalid ext. block %llu",
5375                                     inode->i_ino,
5376                                     (unsigned long long)bh->b_blocknr);
5377                         ret = -EROFS;
5378                         goto out;
5379                 }
5380
5381 find_tail_record:
5382                 i = le16_to_cpu(el->l_next_free_rec) - 1;
5383                 rec = &el->l_recs[i];
5384
5385                 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
5386                      "next = %u\n", i, le32_to_cpu(rec->e_cpos),
5387                      ocfs2_rec_clusters(el, rec),
5388                      (unsigned long long)le64_to_cpu(rec->e_blkno),
5389                      le16_to_cpu(el->l_next_free_rec));
5390
5391                 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
5392
5393                 if (le16_to_cpu(el->l_tree_depth) == 0) {
5394                         /*
5395                          * If the leaf block contains a single empty
5396                          * extent and no records, we can just remove
5397                          * the block.
5398                          */
5399                         if (i == 0 && ocfs2_is_empty_extent(rec)) {
5400                                 memset(rec, 0,
5401                                        sizeof(struct ocfs2_extent_rec));
5402                                 el->l_next_free_rec = cpu_to_le16(0);
5403
5404                                 goto delete;
5405                         }
5406
5407                         /*
5408                          * Remove any empty extents by shifting things
5409                          * left. That should make life much easier on
5410                          * the code below. This condition is rare
5411                          * enough that we shouldn't see a performance
5412                          * hit.
5413                          */
5414                         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
5415                                 le16_add_cpu(&el->l_next_free_rec, -1);
5416
5417                                 for(i = 0;
5418                                     i < le16_to_cpu(el->l_next_free_rec); i++)
5419                                         el->l_recs[i] = el->l_recs[i + 1];
5420
5421                                 memset(&el->l_recs[i], 0,
5422                                        sizeof(struct ocfs2_extent_rec));
5423
5424                                 /*
5425                                  * We've modified our extent list. The
5426                                  * simplest way to handle this change
5427                                  * is to being the search from the
5428                                  * start again.
5429                                  */
5430                                 goto find_tail_record;
5431                         }
5432
5433                         le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
5434
5435                         /*
5436                          * We'll use "new_edge" on our way back up the
5437                          * tree to know what our rightmost cpos is.
5438                          */
5439                         new_edge = le16_to_cpu(rec->e_leaf_clusters);
5440                         new_edge += le32_to_cpu(rec->e_cpos);
5441
5442                         /*
5443                          * The caller will use this to delete data blocks.
5444                          */
5445                         *delete_start = le64_to_cpu(rec->e_blkno)
5446                                 + ocfs2_clusters_to_blocks(inode->i_sb,
5447                                         le16_to_cpu(rec->e_leaf_clusters));
5448
5449                         /*
5450                          * If it's now empty, remove this record.
5451                          */
5452                         if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
5453                                 memset(rec, 0,
5454                                        sizeof(struct ocfs2_extent_rec));
5455                                 le16_add_cpu(&el->l_next_free_rec, -1);
5456                         }
5457                 } else {
5458                         if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
5459                                 memset(rec, 0,
5460                                        sizeof(struct ocfs2_extent_rec));
5461                                 le16_add_cpu(&el->l_next_free_rec, -1);
5462
5463                                 goto delete;
5464                         }
5465
5466                         /* Can this actually happen? */
5467                         if (le16_to_cpu(el->l_next_free_rec) == 0)
5468                                 goto delete;
5469
5470                         /*
5471                          * We never actually deleted any clusters
5472                          * because our leaf was empty. There's no
5473                          * reason to adjust the rightmost edge then.
5474                          */
5475                         if (new_edge == 0)
5476                                 goto delete;
5477
5478                         rec->e_int_clusters = cpu_to_le32(new_edge);
5479                         le32_add_cpu(&rec->e_int_clusters,
5480                                      -le32_to_cpu(rec->e_cpos));
5481
5482                          /*
5483                           * A deleted child record should have been
5484                           * caught above.
5485                           */
5486                          BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
5487                 }
5488
5489 delete:
5490                 ret = ocfs2_journal_dirty(handle, bh);
5491                 if (ret) {
5492                         mlog_errno(ret);
5493                         goto out;
5494                 }
5495
5496                 mlog(0, "extent list container %llu, after: record %d: "
5497                      "(%u, %u, %llu), next = %u.\n",
5498                      (unsigned long long)bh->b_blocknr, i,
5499                      le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
5500                      (unsigned long long)le64_to_cpu(rec->e_blkno),
5501                      le16_to_cpu(el->l_next_free_rec));
5502
5503                 /*
5504                  * We must be careful to only attempt delete of an
5505                  * extent block (and not the root inode block).
5506                  */
5507                 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
5508                         struct ocfs2_extent_block *eb =
5509                                 (struct ocfs2_extent_block *)bh->b_data;
5510
5511                         /*
5512                          * Save this for use when processing the
5513                          * parent block.
5514                          */
5515                         deleted_eb = le64_to_cpu(eb->h_blkno);
5516
5517                         mlog(0, "deleting this extent block.\n");
5518
5519                         ocfs2_remove_from_cache(inode, bh);
5520
5521                         BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
5522                         BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
5523                         BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
5524
5525                         ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
5526                         /* An error here is not fatal. */
5527                         if (ret < 0)
5528                                 mlog_errno(ret);
5529                 } else {
5530                         deleted_eb = 0;
5531                 }
5532
5533                 index--;
5534         }
5535
5536         ret = 0;
5537 out:
5538         return ret;
5539 }
5540
5541 static int ocfs2_do_truncate(struct ocfs2_super *osb,
5542                              unsigned int clusters_to_del,
5543                              struct inode *inode,
5544                              struct buffer_head *fe_bh,
5545                              handle_t *handle,
5546                              struct ocfs2_truncate_context *tc,
5547                              struct ocfs2_path *path)
5548 {
5549         int status;
5550         struct ocfs2_dinode *fe;
5551         struct ocfs2_extent_block *last_eb = NULL;
5552         struct ocfs2_extent_list *el;
5553         struct buffer_head *last_eb_bh = NULL;
5554         u64 delete_blk = 0;
5555
5556         fe = (struct ocfs2_dinode *) fe_bh->b_data;
5557
5558         status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
5559                                              path, &last_eb_bh);
5560         if (status < 0) {
5561                 mlog_errno(status);
5562                 goto bail;
5563         }
5564
5565         /*
5566          * Each component will be touched, so we might as well journal
5567          * here to avoid having to handle errors later.
5568          */
5569         status = ocfs2_journal_access_path(inode, handle, path);
5570         if (status < 0) {
5571                 mlog_errno(status);
5572                 goto bail;
5573         }
5574
5575         if (last_eb_bh) {
5576                 status = ocfs2_journal_access(handle, inode, last_eb_bh,
5577                                               OCFS2_JOURNAL_ACCESS_WRITE);
5578                 if (status < 0) {
5579                         mlog_errno(status);
5580                         goto bail;
5581                 }
5582
5583                 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5584         }
5585
5586         el = &(fe->id2.i_list);
5587
5588         /*
5589          * Lower levels depend on this never happening, but it's best
5590          * to check it up here before changing the tree.
5591          */
5592         if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
5593                 ocfs2_error(inode->i_sb,
5594                             "Inode %lu has an empty extent record, depth %u\n",
5595                             inode->i_ino, le16_to_cpu(el->l_tree_depth));
5596                 status = -EROFS;
5597                 goto bail;
5598         }
5599
5600         spin_lock(&OCFS2_I(inode)->ip_lock);
5601         OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
5602                                       clusters_to_del;
5603         spin_unlock(&OCFS2_I(inode)->ip_lock);
5604         le32_add_cpu(&fe->i_clusters, -clusters_to_del);
5605
5606         status = ocfs2_trim_tree(inode, path, handle, tc,
5607                                  clusters_to_del, &delete_blk);
5608         if (status) {
5609                 mlog_errno(status);
5610                 goto bail;
5611         }
5612
5613         if (le32_to_cpu(fe->i_clusters) == 0) {
5614                 /* trunc to zero is a special case. */
5615                 el->l_tree_depth = 0;
5616                 fe->i_last_eb_blk = 0;
5617         } else if (last_eb)
5618                 fe->i_last_eb_blk = last_eb->h_blkno;
5619
5620         status = ocfs2_journal_dirty(handle, fe_bh);
5621         if (status < 0) {
5622                 mlog_errno(status);
5623                 goto bail;
5624         }
5625
5626         if (last_eb) {
5627                 /* If there will be a new last extent block, then by
5628                  * definition, there cannot be any leaves to the right of
5629                  * him. */
5630                 last_eb->h_next_leaf_blk = 0;
5631                 status = ocfs2_journal_dirty(handle, last_eb_bh);
5632                 if (status < 0) {
5633                         mlog_errno(status);
5634                         goto bail;
5635                 }
5636         }
5637
5638         if (delete_blk) {
5639                 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
5640                                                    clusters_to_del);
5641                 if (status < 0) {
5642                         mlog_errno(status);
5643                         goto bail;
5644                 }
5645         }
5646         status = 0;
5647 bail:
5648
5649         mlog_exit(status);
5650         return status;
5651 }
5652
5653 static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
5654 {
5655         set_buffer_uptodate(bh);
5656         mark_buffer_dirty(bh);
5657         return 0;
5658 }
5659
5660 static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
5661 {
5662         set_buffer_uptodate(bh);
5663         mark_buffer_dirty(bh);
5664         return ocfs2_journal_dirty_data(handle, bh);
5665 }
5666
5667 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
5668                                      loff_t end, struct page **pages,
5669                                      int numpages, u64 phys, handle_t *handle)
5670 {
5671         int i, ret, partial = 0;
5672         void *kaddr;
5673         struct page *page;
5674         unsigned int from, to = PAGE_CACHE_SIZE;
5675         struct super_block *sb = inode->i_sb;
5676
5677         BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
5678
5679         if (numpages == 0)
5680                 goto out;
5681
5682         to = PAGE_CACHE_SIZE;
5683         for(i = 0; i < numpages; i++) {
5684                 page = pages[i];
5685
5686                 from = start & (PAGE_CACHE_SIZE - 1);
5687                 if ((end >> PAGE_CACHE_SHIFT) == page->index)
5688                         to = end & (PAGE_CACHE_SIZE - 1);
5689
5690                 BUG_ON(from > PAGE_CACHE_SIZE);
5691                 BUG_ON(to > PAGE_CACHE_SIZE);
5692
5693                 ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
5694                 if (ret)
5695                         mlog_errno(ret);
5696
5697                 kaddr = kmap_atomic(page, KM_USER0);
5698                 memset(kaddr + from, 0, to - from);
5699                 kunmap_atomic(kaddr, KM_USER0);
5700
5701                 /*
5702                  * Need to set the buffers we zero'd into uptodate
5703                  * here if they aren't - ocfs2_map_page_blocks()
5704                  * might've skipped some
5705                  */
5706                 if (ocfs2_should_order_data(inode)) {
5707                         ret = walk_page_buffers(handle,
5708                                                 page_buffers(page),
5709                                                 from, to, &partial,
5710                                                 ocfs2_ordered_zero_func);
5711                         if (ret < 0)
5712                                 mlog_errno(ret);
5713                 } else {
5714                         ret = walk_page_buffers(handle, page_buffers(page),
5715                                                 from, to, &partial,
5716                                                 ocfs2_writeback_zero_func);
5717                         if (ret < 0)
5718                                 mlog_errno(ret);
5719                 }
5720
5721                 if (!partial)
5722                         SetPageUptodate(page);
5723
5724                 flush_dcache_page(page);
5725
5726                 start = (page->index + 1) << PAGE_CACHE_SHIFT;
5727         }
5728 out:
5729         if (pages) {
5730                 for (i = 0; i < numpages; i++) {
5731                         page = pages[i];
5732                         unlock_page(page);
5733                         mark_page_accessed(page);
5734                         page_cache_release(page);
5735                 }
5736         }
5737 }
5738
5739 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
5740                                 struct page **pages, int *num, u64 *phys)
5741 {
5742         int i, numpages = 0, ret = 0;
5743         unsigned int ext_flags;
5744         struct super_block *sb = inode->i_sb;
5745         struct address_space *mapping = inode->i_mapping;
5746         unsigned long index;
5747         loff_t last_page_bytes;
5748
5749         BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
5750         BUG_ON(start > end);
5751
5752         if (start == end)
5753                 goto out;
5754
5755         BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
5756                (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
5757
5758         ret = ocfs2_extent_map_get_blocks(inode, start >> sb->s_blocksize_bits,
5759                                           phys, NULL, &ext_flags);
5760         if (ret) {
5761                 mlog_errno(ret);
5762                 goto out;
5763         }
5764
5765         /* Tail is a hole. */
5766         if (*phys == 0)
5767                 goto out;
5768
5769         /* Tail is marked as unwritten, we can count on write to zero
5770          * in that case. */
5771         if (ext_flags & OCFS2_EXT_UNWRITTEN)
5772                 goto out;
5773
5774         last_page_bytes = PAGE_ALIGN(end);
5775         index = start >> PAGE_CACHE_SHIFT;
5776         do {
5777                 pages[numpages] = grab_cache_page(mapping, index);
5778                 if (!pages[numpages]) {
5779                         ret = -ENOMEM;
5780                         mlog_errno(ret);
5781                         goto out;
5782                 }
5783
5784                 numpages++;
5785                 index++;
5786         } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
5787
5788 out:
5789         if (ret != 0) {
5790                 if (pages) {
5791                         for (i = 0; i < numpages; i++) {
5792                                 if (pages[i]) {
5793                                         unlock_page(pages[i]);
5794                                         page_cache_release(pages[i]);
5795                                 }
5796                         }
5797                 }
5798                 numpages = 0;
5799         }
5800
5801         *num = numpages;
5802
5803         return ret;
5804 }
5805
5806 /*
5807  * Zero the area past i_size but still within an allocated
5808  * cluster. This avoids exposing nonzero data on subsequent file
5809  * extends.
5810  *
5811  * We need to call this before i_size is updated on the inode because
5812  * otherwise block_write_full_page() will skip writeout of pages past
5813  * i_size. The new_i_size parameter is passed for this reason.
5814  */
5815 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
5816                                   u64 range_start, u64 range_end)
5817 {
5818         int ret, numpages;
5819         struct page **pages = NULL;
5820         u64 phys;
5821
5822         /*
5823          * File systems which don't support sparse files zero on every
5824          * extend.
5825          */
5826         if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
5827                 return 0;
5828
5829         pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
5830                         sizeof(struct page *), GFP_NOFS);
5831         if (pages == NULL) {
5832                 ret = -ENOMEM;
5833                 mlog_errno(ret);
5834                 goto out;
5835         }
5836
5837         ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
5838                                    &numpages, &phys);
5839         if (ret) {
5840                 mlog_errno(ret);
5841                 goto out;
5842         }
5843
5844         if (numpages == 0)
5845                 goto out;
5846
5847         ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
5848                                  numpages, phys, handle);
5849
5850         /*
5851          * Initiate writeout of the pages we zero'd here. We don't
5852          * wait on them - the truncate_inode_pages() call later will
5853          * do that for us.
5854          */
5855         ret = do_sync_mapping_range(inode->i_mapping, range_start,
5856                                     range_end - 1, SYNC_FILE_RANGE_WRITE);
5857         if (ret)
5858                 mlog_errno(ret);
5859
5860 out:
5861         if (pages)
5862                 kfree(pages);
5863
5864         return ret;
5865 }
5866
5867 /*
5868  * It is expected, that by the time you call this function,
5869  * inode->i_size and fe->i_size have been adjusted.
5870  *
5871  * WARNING: This will kfree the truncate context
5872  */
5873 int ocfs2_commit_truncate(struct ocfs2_super *osb,
5874                           struct inode *inode,
5875                           struct buffer_head *fe_bh,
5876                           struct ocfs2_truncate_context *tc)
5877 {
5878         int status, i, credits, tl_sem = 0;
5879         u32 clusters_to_del, new_highest_cpos, range;
5880         struct ocfs2_extent_list *el;
5881         handle_t *handle = NULL;
5882         struct inode *tl_inode = osb->osb_tl_inode;
5883         struct ocfs2_path *path = NULL;
5884
5885         mlog_entry_void();
5886
5887         new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
5888                                                      i_size_read(inode));
5889
5890         path = ocfs2_new_inode_path(fe_bh);
5891         if (!path) {
5892                 status = -ENOMEM;
5893                 mlog_errno(status);
5894                 goto bail;
5895         }
5896
5897         ocfs2_extent_map_trunc(inode, new_highest_cpos);
5898
5899 start:
5900         /*
5901          * Check that we still have allocation to delete.
5902          */
5903         if (OCFS2_I(inode)->ip_clusters == 0) {
5904                 status = 0;
5905                 goto bail;
5906         }
5907
5908         /*
5909          * Truncate always works against the rightmost tree branch.
5910          */
5911         status = ocfs2_find_path(inode, path, UINT_MAX);
5912         if (status) {
5913                 mlog_errno(status);
5914                 goto bail;
5915         }
5916
5917         mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
5918              OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
5919
5920         /*
5921          * By now, el will point to the extent list on the bottom most
5922          * portion of this tree. Only the tail record is considered in
5923          * each pass.
5924          *
5925          * We handle the following cases, in order:
5926          * - empty extent: delete the remaining branch
5927          * - remove the entire record
5928          * - remove a partial record
5929          * - no record needs to be removed (truncate has completed)
5930          */
5931         el = path_leaf_el(path);
5932         if (le16_to_cpu(el->l_next_free_rec) == 0) {
5933                 ocfs2_error(inode->i_sb,
5934                             "Inode %llu has empty extent block at %llu\n",
5935                             (unsigned long long)OCFS2_I(inode)->ip_blkno,
5936                             (unsigned long long)path_leaf_bh(path)->b_blocknr);
5937                 status = -EROFS;
5938                 goto bail;
5939         }
5940
5941         i = le16_to_cpu(el->l_next_free_rec) - 1;
5942         range = le32_to_cpu(el->l_recs[i].e_cpos) +
5943                 ocfs2_rec_clusters(el, &el->l_recs[i]);
5944         if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
5945                 clusters_to_del = 0;
5946         } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
5947                 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
5948         } else if (range > new_highest_cpos) {
5949                 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
5950                                    le32_to_cpu(el->l_recs[i].e_cpos)) -
5951                                   new_highest_cpos;
5952         } else {
5953                 status = 0;
5954                 goto bail;
5955         }
5956
5957         mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
5958              clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
5959
5960         BUG_ON(clusters_to_del == 0);
5961
5962         mutex_lock(&tl_inode->i_mutex);
5963         tl_sem = 1;
5964         /* ocfs2_truncate_log_needs_flush guarantees us at least one
5965          * record is free for use. If there isn't any, we flush to get
5966          * an empty truncate log.  */
5967         if (ocfs2_truncate_log_needs_flush(osb)) {
5968                 status = __ocfs2_flush_truncate_log(osb);
5969                 if (status < 0) {
5970                         mlog_errno(status);
5971                         goto bail;
5972                 }
5973         }
5974
5975         credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
5976                                                 (struct ocfs2_dinode *)fe_bh->b_data,
5977                                                 el);
5978         handle = ocfs2_start_trans(osb, credits);
5979         if (IS_ERR(handle)) {
5980                 status = PTR_ERR(handle);
5981                 handle = NULL;
5982                 mlog_errno(status);
5983                 goto bail;
5984         }
5985
5986         status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
5987                                    tc, path);
5988         if (status < 0) {
5989                 mlog_errno(status);
5990                 goto bail;
5991         }
5992
5993         mutex_unlock(&tl_inode->i_mutex);
5994         tl_sem = 0;
5995
5996         ocfs2_commit_trans(osb, handle);
5997         handle = NULL;
5998
5999         ocfs2_reinit_path(path, 1);
6000
6001         /*
6002          * The check above will catch the case where we've truncated
6003          * away all allocation.
6004          */
6005         goto start;
6006
6007 bail:
6008
6009         ocfs2_schedule_truncate_log_flush(osb, 1);
6010
6011         if (tl_sem)
6012                 mutex_unlock(&tl_inode->i_mutex);
6013
6014         if (handle)
6015                 ocfs2_commit_trans(osb, handle);
6016
6017         ocfs2_run_deallocs(osb, &tc->tc_dealloc);
6018
6019         ocfs2_free_path(path);
6020
6021         /* This will drop the ext_alloc cluster lock for us */
6022         ocfs2_free_truncate_context(tc);
6023
6024         mlog_exit(status);
6025         return status;
6026 }
6027
6028 /*
6029  * Expects the inode to already be locked.
6030  */
6031 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
6032                            struct inode *inode,
6033                            struct buffer_head *fe_bh,
6034                            struct ocfs2_truncate_context **tc)
6035 {
6036         int status;
6037         unsigned int new_i_clusters;
6038         struct ocfs2_dinode *fe;
6039         struct ocfs2_extent_block *eb;
6040         struct buffer_head *last_eb_bh = NULL;
6041
6042         mlog_entry_void();
6043
6044         *tc = NULL;
6045
6046         new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
6047                                                   i_size_read(inode));
6048         fe = (struct ocfs2_dinode *) fe_bh->b_data;
6049
6050         mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
6051              "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
6052              (unsigned long long)le64_to_cpu(fe->i_size));
6053
6054         *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
6055         if (!(*tc)) {
6056                 status = -ENOMEM;
6057                 mlog_errno(status);
6058                 goto bail;
6059         }
6060         ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
6061
6062         if (fe->id2.i_list.l_tree_depth) {
6063                 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
6064                                           &last_eb_bh, OCFS2_BH_CACHED, inode);
6065                 if (status < 0) {
6066                         mlog_errno(status);
6067                         goto bail;
6068                 }
6069                 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6070                 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6071                         OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6072
6073                         brelse(last_eb_bh);
6074                         status = -EIO;
6075                         goto bail;
6076                 }
6077         }
6078
6079         (*tc)->tc_last_eb_bh = last_eb_bh;
6080
6081         status = 0;
6082 bail:
6083         if (status < 0) {
6084                 if (*tc)
6085                         ocfs2_free_truncate_context(*tc);
6086                 *tc = NULL;
6087         }
6088         mlog_exit_void();
6089         return status;
6090 }
6091
6092 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
6093 {
6094         /*
6095          * The caller is responsible for completing deallocation
6096          * before freeing the context.
6097          */
6098         if (tc->tc_dealloc.c_first_suballocator != NULL)
6099                 mlog(ML_NOTICE,
6100                      "Truncate completion has non-empty dealloc context\n");
6101
6102         if (tc->tc_last_eb_bh)
6103                 brelse(tc->tc_last_eb_bh);
6104
6105         kfree(tc);
6106 }