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