ocfs2: Pass ocfs2_extent_tree to ocfs2_unlink_path()
[safe/jmp/linux-2.6] / fs / ocfs2 / alloc.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * alloc.c
5  *
6  * Extent allocs and frees
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  */
25
26 #include <linux/fs.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
31 #include <linux/quotaops.h>
32
33 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
34 #include <cluster/masklog.h>
35
36 #include "ocfs2.h"
37
38 #include "alloc.h"
39 #include "aops.h"
40 #include "blockcheck.h"
41 #include "dlmglue.h"
42 #include "extent_map.h"
43 #include "inode.h"
44 #include "journal.h"
45 #include "localalloc.h"
46 #include "suballoc.h"
47 #include "sysfile.h"
48 #include "file.h"
49 #include "super.h"
50 #include "uptodate.h"
51 #include "xattr.h"
52
53 #include "buffer_head_io.h"
54
55
56 /*
57  * Operations for a specific extent tree type.
58  *
59  * To implement an on-disk btree (extent tree) type in ocfs2, add
60  * an ocfs2_extent_tree_operations structure and the matching
61  * ocfs2_init_<thingy>_extent_tree() function.  That's pretty much it
62  * for the allocation portion of the extent tree.
63  */
64 struct ocfs2_extent_tree_operations {
65         /*
66          * last_eb_blk is the block number of the right most leaf extent
67          * block.  Most on-disk structures containing an extent tree store
68          * this value for fast access.  The ->eo_set_last_eb_blk() and
69          * ->eo_get_last_eb_blk() operations access this value.  They are
70          *  both required.
71          */
72         void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
73                                    u64 blkno);
74         u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
75
76         /*
77          * The on-disk structure usually keeps track of how many total
78          * clusters are stored in this extent tree.  This function updates
79          * that value.  new_clusters is the delta, and must be
80          * added to the total.  Required.
81          */
82         void (*eo_update_clusters)(struct inode *inode,
83                                    struct ocfs2_extent_tree *et,
84                                    u32 new_clusters);
85
86         /*
87          * If ->eo_insert_check() exists, it is called before rec is
88          * inserted into the extent tree.  It is optional.
89          */
90         int (*eo_insert_check)(struct inode *inode,
91                                struct ocfs2_extent_tree *et,
92                                struct ocfs2_extent_rec *rec);
93         int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
94
95         /*
96          * --------------------------------------------------------------
97          * The remaining are internal to ocfs2_extent_tree and don't have
98          * accessor functions
99          */
100
101         /*
102          * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
103          * It is required.
104          */
105         void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
106
107         /*
108          * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
109          * it exists.  If it does not, et->et_max_leaf_clusters is set
110          * to 0 (unlimited).  Optional.
111          */
112         void (*eo_fill_max_leaf_clusters)(struct inode *inode,
113                                           struct ocfs2_extent_tree *et);
114 };
115
116
117 /*
118  * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
119  * in the methods.
120  */
121 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
122 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
123                                          u64 blkno);
124 static void ocfs2_dinode_update_clusters(struct inode *inode,
125                                          struct ocfs2_extent_tree *et,
126                                          u32 clusters);
127 static int ocfs2_dinode_insert_check(struct inode *inode,
128                                      struct ocfs2_extent_tree *et,
129                                      struct ocfs2_extent_rec *rec);
130 static int ocfs2_dinode_sanity_check(struct inode *inode,
131                                      struct ocfs2_extent_tree *et);
132 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
133 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
134         .eo_set_last_eb_blk     = ocfs2_dinode_set_last_eb_blk,
135         .eo_get_last_eb_blk     = ocfs2_dinode_get_last_eb_blk,
136         .eo_update_clusters     = ocfs2_dinode_update_clusters,
137         .eo_insert_check        = ocfs2_dinode_insert_check,
138         .eo_sanity_check        = ocfs2_dinode_sanity_check,
139         .eo_fill_root_el        = ocfs2_dinode_fill_root_el,
140 };
141
142 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
143                                          u64 blkno)
144 {
145         struct ocfs2_dinode *di = et->et_object;
146
147         BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
148         di->i_last_eb_blk = cpu_to_le64(blkno);
149 }
150
151 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
152 {
153         struct ocfs2_dinode *di = et->et_object;
154
155         BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
156         return le64_to_cpu(di->i_last_eb_blk);
157 }
158
159 static void ocfs2_dinode_update_clusters(struct inode *inode,
160                                          struct ocfs2_extent_tree *et,
161                                          u32 clusters)
162 {
163         struct ocfs2_dinode *di = et->et_object;
164
165         le32_add_cpu(&di->i_clusters, clusters);
166         spin_lock(&OCFS2_I(inode)->ip_lock);
167         OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
168         spin_unlock(&OCFS2_I(inode)->ip_lock);
169 }
170
171 static int ocfs2_dinode_insert_check(struct inode *inode,
172                                      struct ocfs2_extent_tree *et,
173                                      struct ocfs2_extent_rec *rec)
174 {
175         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
176
177         BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
178         mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
179                         (OCFS2_I(inode)->ip_clusters !=
180                          le32_to_cpu(rec->e_cpos)),
181                         "Device %s, asking for sparse allocation: inode %llu, "
182                         "cpos %u, clusters %u\n",
183                         osb->dev_str,
184                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
185                         rec->e_cpos,
186                         OCFS2_I(inode)->ip_clusters);
187
188         return 0;
189 }
190
191 static int ocfs2_dinode_sanity_check(struct inode *inode,
192                                      struct ocfs2_extent_tree *et)
193 {
194         struct ocfs2_dinode *di = et->et_object;
195
196         BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
197         BUG_ON(!OCFS2_IS_VALID_DINODE(di));
198
199         return 0;
200 }
201
202 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
203 {
204         struct ocfs2_dinode *di = et->et_object;
205
206         et->et_root_el = &di->id2.i_list;
207 }
208
209
210 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
211 {
212         struct ocfs2_xattr_value_buf *vb = et->et_object;
213
214         et->et_root_el = &vb->vb_xv->xr_list;
215 }
216
217 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
218                                               u64 blkno)
219 {
220         struct ocfs2_xattr_value_buf *vb = et->et_object;
221
222         vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
223 }
224
225 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
226 {
227         struct ocfs2_xattr_value_buf *vb = et->et_object;
228
229         return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
230 }
231
232 static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233                                               struct ocfs2_extent_tree *et,
234                                               u32 clusters)
235 {
236         struct ocfs2_xattr_value_buf *vb = et->et_object;
237
238         le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
239 }
240
241 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
242         .eo_set_last_eb_blk     = ocfs2_xattr_value_set_last_eb_blk,
243         .eo_get_last_eb_blk     = ocfs2_xattr_value_get_last_eb_blk,
244         .eo_update_clusters     = ocfs2_xattr_value_update_clusters,
245         .eo_fill_root_el        = ocfs2_xattr_value_fill_root_el,
246 };
247
248 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
249 {
250         struct ocfs2_xattr_block *xb = et->et_object;
251
252         et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
253 }
254
255 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
256                                                     struct ocfs2_extent_tree *et)
257 {
258         et->et_max_leaf_clusters =
259                 ocfs2_clusters_for_bytes(inode->i_sb,
260                                          OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
261 }
262
263 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
264                                              u64 blkno)
265 {
266         struct ocfs2_xattr_block *xb = et->et_object;
267         struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
268
269         xt->xt_last_eb_blk = cpu_to_le64(blkno);
270 }
271
272 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
273 {
274         struct ocfs2_xattr_block *xb = et->et_object;
275         struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
276
277         return le64_to_cpu(xt->xt_last_eb_blk);
278 }
279
280 static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
281                                              struct ocfs2_extent_tree *et,
282                                              u32 clusters)
283 {
284         struct ocfs2_xattr_block *xb = et->et_object;
285
286         le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
287 }
288
289 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
290         .eo_set_last_eb_blk     = ocfs2_xattr_tree_set_last_eb_blk,
291         .eo_get_last_eb_blk     = ocfs2_xattr_tree_get_last_eb_blk,
292         .eo_update_clusters     = ocfs2_xattr_tree_update_clusters,
293         .eo_fill_root_el        = ocfs2_xattr_tree_fill_root_el,
294         .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
295 };
296
297 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
298                                           u64 blkno)
299 {
300         struct ocfs2_dx_root_block *dx_root = et->et_object;
301
302         dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
303 }
304
305 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
306 {
307         struct ocfs2_dx_root_block *dx_root = et->et_object;
308
309         return le64_to_cpu(dx_root->dr_last_eb_blk);
310 }
311
312 static void ocfs2_dx_root_update_clusters(struct inode *inode,
313                                           struct ocfs2_extent_tree *et,
314                                           u32 clusters)
315 {
316         struct ocfs2_dx_root_block *dx_root = et->et_object;
317
318         le32_add_cpu(&dx_root->dr_clusters, clusters);
319 }
320
321 static int ocfs2_dx_root_sanity_check(struct inode *inode,
322                                       struct ocfs2_extent_tree *et)
323 {
324         struct ocfs2_dx_root_block *dx_root = et->et_object;
325
326         BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
327
328         return 0;
329 }
330
331 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
332 {
333         struct ocfs2_dx_root_block *dx_root = et->et_object;
334
335         et->et_root_el = &dx_root->dr_list;
336 }
337
338 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
339         .eo_set_last_eb_blk     = ocfs2_dx_root_set_last_eb_blk,
340         .eo_get_last_eb_blk     = ocfs2_dx_root_get_last_eb_blk,
341         .eo_update_clusters     = ocfs2_dx_root_update_clusters,
342         .eo_sanity_check        = ocfs2_dx_root_sanity_check,
343         .eo_fill_root_el        = ocfs2_dx_root_fill_root_el,
344 };
345
346 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
347                                      struct inode *inode,
348                                      struct buffer_head *bh,
349                                      ocfs2_journal_access_func access,
350                                      void *obj,
351                                      struct ocfs2_extent_tree_operations *ops)
352 {
353         et->et_ops = ops;
354         et->et_root_bh = bh;
355         et->et_ci = INODE_CACHE(inode);
356         et->et_root_journal_access = access;
357         if (!obj)
358                 obj = (void *)bh->b_data;
359         et->et_object = obj;
360
361         et->et_ops->eo_fill_root_el(et);
362         if (!et->et_ops->eo_fill_max_leaf_clusters)
363                 et->et_max_leaf_clusters = 0;
364         else
365                 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
366 }
367
368 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
369                                    struct inode *inode,
370                                    struct buffer_head *bh)
371 {
372         __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
373                                  NULL, &ocfs2_dinode_et_ops);
374 }
375
376 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
377                                        struct inode *inode,
378                                        struct buffer_head *bh)
379 {
380         __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
381                                  NULL, &ocfs2_xattr_tree_et_ops);
382 }
383
384 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
385                                         struct inode *inode,
386                                         struct ocfs2_xattr_value_buf *vb)
387 {
388         __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
389                                  &ocfs2_xattr_value_et_ops);
390 }
391
392 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
393                                     struct inode *inode,
394                                     struct buffer_head *bh)
395 {
396         __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
397                                  NULL, &ocfs2_dx_root_et_ops);
398 }
399
400 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
401                                             u64 new_last_eb_blk)
402 {
403         et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
404 }
405
406 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
407 {
408         return et->et_ops->eo_get_last_eb_blk(et);
409 }
410
411 static inline void ocfs2_et_update_clusters(struct inode *inode,
412                                             struct ocfs2_extent_tree *et,
413                                             u32 clusters)
414 {
415         et->et_ops->eo_update_clusters(inode, et, clusters);
416 }
417
418 static inline int ocfs2_et_root_journal_access(handle_t *handle,
419                                                struct ocfs2_extent_tree *et,
420                                                int type)
421 {
422         return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
423                                           type);
424 }
425
426 static inline int ocfs2_et_insert_check(struct inode *inode,
427                                         struct ocfs2_extent_tree *et,
428                                         struct ocfs2_extent_rec *rec)
429 {
430         int ret = 0;
431
432         if (et->et_ops->eo_insert_check)
433                 ret = et->et_ops->eo_insert_check(inode, et, rec);
434         return ret;
435 }
436
437 static inline int ocfs2_et_sanity_check(struct inode *inode,
438                                         struct ocfs2_extent_tree *et)
439 {
440         int ret = 0;
441
442         if (et->et_ops->eo_sanity_check)
443                 ret = et->et_ops->eo_sanity_check(inode, et);
444         return ret;
445 }
446
447 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
448 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
449                                          struct ocfs2_extent_block *eb);
450
451 /*
452  * Structures which describe a path through a btree, and functions to
453  * manipulate them.
454  *
455  * The idea here is to be as generic as possible with the tree
456  * manipulation code.
457  */
458 struct ocfs2_path_item {
459         struct buffer_head              *bh;
460         struct ocfs2_extent_list        *el;
461 };
462
463 #define OCFS2_MAX_PATH_DEPTH    5
464
465 struct ocfs2_path {
466         int                             p_tree_depth;
467         ocfs2_journal_access_func       p_root_access;
468         struct ocfs2_path_item          p_node[OCFS2_MAX_PATH_DEPTH];
469 };
470
471 #define path_root_bh(_path) ((_path)->p_node[0].bh)
472 #define path_root_el(_path) ((_path)->p_node[0].el)
473 #define path_root_access(_path)((_path)->p_root_access)
474 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
475 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
476 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
477
478 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
479                            struct ocfs2_path *path, u32 cpos);
480 static void ocfs2_adjust_rightmost_records(struct inode *inode,
481                                            handle_t *handle,
482                                            struct ocfs2_path *path,
483                                            struct ocfs2_extent_rec *insert_rec);
484 /*
485  * Reset the actual path elements so that we can re-use the structure
486  * to build another path. Generally, this involves freeing the buffer
487  * heads.
488  */
489 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
490 {
491         int i, start = 0, depth = 0;
492         struct ocfs2_path_item *node;
493
494         if (keep_root)
495                 start = 1;
496
497         for(i = start; i < path_num_items(path); i++) {
498                 node = &path->p_node[i];
499
500                 brelse(node->bh);
501                 node->bh = NULL;
502                 node->el = NULL;
503         }
504
505         /*
506          * Tree depth may change during truncate, or insert. If we're
507          * keeping the root extent list, then make sure that our path
508          * structure reflects the proper depth.
509          */
510         if (keep_root)
511                 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
512         else
513                 path_root_access(path) = NULL;
514
515         path->p_tree_depth = depth;
516 }
517
518 static void ocfs2_free_path(struct ocfs2_path *path)
519 {
520         if (path) {
521                 ocfs2_reinit_path(path, 0);
522                 kfree(path);
523         }
524 }
525
526 /*
527  * All the elements of src into dest. After this call, src could be freed
528  * without affecting dest.
529  *
530  * Both paths should have the same root. Any non-root elements of dest
531  * will be freed.
532  */
533 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
534 {
535         int i;
536
537         BUG_ON(path_root_bh(dest) != path_root_bh(src));
538         BUG_ON(path_root_el(dest) != path_root_el(src));
539         BUG_ON(path_root_access(dest) != path_root_access(src));
540
541         ocfs2_reinit_path(dest, 1);
542
543         for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
544                 dest->p_node[i].bh = src->p_node[i].bh;
545                 dest->p_node[i].el = src->p_node[i].el;
546
547                 if (dest->p_node[i].bh)
548                         get_bh(dest->p_node[i].bh);
549         }
550 }
551
552 /*
553  * Make the *dest path the same as src and re-initialize src path to
554  * have a root only.
555  */
556 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
557 {
558         int i;
559
560         BUG_ON(path_root_bh(dest) != path_root_bh(src));
561         BUG_ON(path_root_access(dest) != path_root_access(src));
562
563         for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
564                 brelse(dest->p_node[i].bh);
565
566                 dest->p_node[i].bh = src->p_node[i].bh;
567                 dest->p_node[i].el = src->p_node[i].el;
568
569                 src->p_node[i].bh = NULL;
570                 src->p_node[i].el = NULL;
571         }
572 }
573
574 /*
575  * Insert an extent block at given index.
576  *
577  * This will not take an additional reference on eb_bh.
578  */
579 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
580                                         struct buffer_head *eb_bh)
581 {
582         struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
583
584         /*
585          * Right now, no root bh is an extent block, so this helps
586          * catch code errors with dinode trees. The assertion can be
587          * safely removed if we ever need to insert extent block
588          * structures at the root.
589          */
590         BUG_ON(index == 0);
591
592         path->p_node[index].bh = eb_bh;
593         path->p_node[index].el = &eb->h_list;
594 }
595
596 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
597                                          struct ocfs2_extent_list *root_el,
598                                          ocfs2_journal_access_func access)
599 {
600         struct ocfs2_path *path;
601
602         BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
603
604         path = kzalloc(sizeof(*path), GFP_NOFS);
605         if (path) {
606                 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
607                 get_bh(root_bh);
608                 path_root_bh(path) = root_bh;
609                 path_root_el(path) = root_el;
610                 path_root_access(path) = access;
611         }
612
613         return path;
614 }
615
616 static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
617 {
618         return ocfs2_new_path(path_root_bh(path), path_root_el(path),
619                               path_root_access(path));
620 }
621
622 static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
623 {
624         return ocfs2_new_path(et->et_root_bh, et->et_root_el,
625                               et->et_root_journal_access);
626 }
627
628 /*
629  * Journal the buffer at depth idx.  All idx>0 are extent_blocks,
630  * otherwise it's the root_access function.
631  *
632  * I don't like the way this function's name looks next to
633  * ocfs2_journal_access_path(), but I don't have a better one.
634  */
635 static int ocfs2_path_bh_journal_access(handle_t *handle,
636                                         struct ocfs2_caching_info *ci,
637                                         struct ocfs2_path *path,
638                                         int idx)
639 {
640         ocfs2_journal_access_func access = path_root_access(path);
641
642         if (!access)
643                 access = ocfs2_journal_access;
644
645         if (idx)
646                 access = ocfs2_journal_access_eb;
647
648         return access(handle, ci, path->p_node[idx].bh,
649                       OCFS2_JOURNAL_ACCESS_WRITE);
650 }
651
652 /*
653  * Convenience function to journal all components in a path.
654  */
655 static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
656                                      handle_t *handle,
657                                      struct ocfs2_path *path)
658 {
659         int i, ret = 0;
660
661         if (!path)
662                 goto out;
663
664         for(i = 0; i < path_num_items(path); i++) {
665                 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
666                 if (ret < 0) {
667                         mlog_errno(ret);
668                         goto out;
669                 }
670         }
671
672 out:
673         return ret;
674 }
675
676 /*
677  * Return the index of the extent record which contains cluster #v_cluster.
678  * -1 is returned if it was not found.
679  *
680  * Should work fine on interior and exterior nodes.
681  */
682 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
683 {
684         int ret = -1;
685         int i;
686         struct ocfs2_extent_rec *rec;
687         u32 rec_end, rec_start, clusters;
688
689         for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
690                 rec = &el->l_recs[i];
691
692                 rec_start = le32_to_cpu(rec->e_cpos);
693                 clusters = ocfs2_rec_clusters(el, rec);
694
695                 rec_end = rec_start + clusters;
696
697                 if (v_cluster >= rec_start && v_cluster < rec_end) {
698                         ret = i;
699                         break;
700                 }
701         }
702
703         return ret;
704 }
705
706 enum ocfs2_contig_type {
707         CONTIG_NONE = 0,
708         CONTIG_LEFT,
709         CONTIG_RIGHT,
710         CONTIG_LEFTRIGHT,
711 };
712
713
714 /*
715  * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
716  * ocfs2_extent_contig only work properly against leaf nodes!
717  */
718 static int ocfs2_block_extent_contig(struct super_block *sb,
719                                      struct ocfs2_extent_rec *ext,
720                                      u64 blkno)
721 {
722         u64 blk_end = le64_to_cpu(ext->e_blkno);
723
724         blk_end += ocfs2_clusters_to_blocks(sb,
725                                     le16_to_cpu(ext->e_leaf_clusters));
726
727         return blkno == blk_end;
728 }
729
730 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
731                                   struct ocfs2_extent_rec *right)
732 {
733         u32 left_range;
734
735         left_range = le32_to_cpu(left->e_cpos) +
736                 le16_to_cpu(left->e_leaf_clusters);
737
738         return (left_range == le32_to_cpu(right->e_cpos));
739 }
740
741 static enum ocfs2_contig_type
742         ocfs2_extent_contig(struct inode *inode,
743                             struct ocfs2_extent_rec *ext,
744                             struct ocfs2_extent_rec *insert_rec)
745 {
746         u64 blkno = le64_to_cpu(insert_rec->e_blkno);
747
748         /*
749          * Refuse to coalesce extent records with different flag
750          * fields - we don't want to mix unwritten extents with user
751          * data.
752          */
753         if (ext->e_flags != insert_rec->e_flags)
754                 return CONTIG_NONE;
755
756         if (ocfs2_extents_adjacent(ext, insert_rec) &&
757             ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
758                         return CONTIG_RIGHT;
759
760         blkno = le64_to_cpu(ext->e_blkno);
761         if (ocfs2_extents_adjacent(insert_rec, ext) &&
762             ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
763                 return CONTIG_LEFT;
764
765         return CONTIG_NONE;
766 }
767
768 /*
769  * NOTE: We can have pretty much any combination of contiguousness and
770  * appending.
771  *
772  * The usefulness of APPEND_TAIL is more in that it lets us know that
773  * we'll have to update the path to that leaf.
774  */
775 enum ocfs2_append_type {
776         APPEND_NONE = 0,
777         APPEND_TAIL,
778 };
779
780 enum ocfs2_split_type {
781         SPLIT_NONE = 0,
782         SPLIT_LEFT,
783         SPLIT_RIGHT,
784 };
785
786 struct ocfs2_insert_type {
787         enum ocfs2_split_type   ins_split;
788         enum ocfs2_append_type  ins_appending;
789         enum ocfs2_contig_type  ins_contig;
790         int                     ins_contig_index;
791         int                     ins_tree_depth;
792 };
793
794 struct ocfs2_merge_ctxt {
795         enum ocfs2_contig_type  c_contig_type;
796         int                     c_has_empty_extent;
797         int                     c_split_covers_rec;
798 };
799
800 static int ocfs2_validate_extent_block(struct super_block *sb,
801                                        struct buffer_head *bh)
802 {
803         int rc;
804         struct ocfs2_extent_block *eb =
805                 (struct ocfs2_extent_block *)bh->b_data;
806
807         mlog(0, "Validating extent block %llu\n",
808              (unsigned long long)bh->b_blocknr);
809
810         BUG_ON(!buffer_uptodate(bh));
811
812         /*
813          * If the ecc fails, we return the error but otherwise
814          * leave the filesystem running.  We know any error is
815          * local to this block.
816          */
817         rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
818         if (rc) {
819                 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
820                      (unsigned long long)bh->b_blocknr);
821                 return rc;
822         }
823
824         /*
825          * Errors after here are fatal.
826          */
827
828         if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
829                 ocfs2_error(sb,
830                             "Extent block #%llu has bad signature %.*s",
831                             (unsigned long long)bh->b_blocknr, 7,
832                             eb->h_signature);
833                 return -EINVAL;
834         }
835
836         if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
837                 ocfs2_error(sb,
838                             "Extent block #%llu has an invalid h_blkno "
839                             "of %llu",
840                             (unsigned long long)bh->b_blocknr,
841                             (unsigned long long)le64_to_cpu(eb->h_blkno));
842                 return -EINVAL;
843         }
844
845         if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
846                 ocfs2_error(sb,
847                             "Extent block #%llu has an invalid "
848                             "h_fs_generation of #%u",
849                             (unsigned long long)bh->b_blocknr,
850                             le32_to_cpu(eb->h_fs_generation));
851                 return -EINVAL;
852         }
853
854         return 0;
855 }
856
857 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
858                             struct buffer_head **bh)
859 {
860         int rc;
861         struct buffer_head *tmp = *bh;
862
863         rc = ocfs2_read_block(ci, eb_blkno, &tmp,
864                               ocfs2_validate_extent_block);
865
866         /* If ocfs2_read_block() got us a new bh, pass it up. */
867         if (!rc && !*bh)
868                 *bh = tmp;
869
870         return rc;
871 }
872
873
874 /*
875  * How many free extents have we got before we need more meta data?
876  */
877 int ocfs2_num_free_extents(struct ocfs2_super *osb,
878                            struct ocfs2_extent_tree *et)
879 {
880         int retval;
881         struct ocfs2_extent_list *el = NULL;
882         struct ocfs2_extent_block *eb;
883         struct buffer_head *eb_bh = NULL;
884         u64 last_eb_blk = 0;
885
886         mlog_entry_void();
887
888         el = et->et_root_el;
889         last_eb_blk = ocfs2_et_get_last_eb_blk(et);
890
891         if (last_eb_blk) {
892                 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
893                                                  &eb_bh);
894                 if (retval < 0) {
895                         mlog_errno(retval);
896                         goto bail;
897                 }
898                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
899                 el = &eb->h_list;
900         }
901
902         BUG_ON(el->l_tree_depth != 0);
903
904         retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
905 bail:
906         brelse(eb_bh);
907
908         mlog_exit(retval);
909         return retval;
910 }
911
912 /* expects array to already be allocated
913  *
914  * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
915  * l_count for you
916  */
917 static int ocfs2_create_new_meta_bhs(handle_t *handle,
918                                      struct ocfs2_extent_tree *et,
919                                      int wanted,
920                                      struct ocfs2_alloc_context *meta_ac,
921                                      struct buffer_head *bhs[])
922 {
923         int count, status, i;
924         u16 suballoc_bit_start;
925         u32 num_got;
926         u64 first_blkno;
927         struct ocfs2_super *osb =
928                 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
929         struct ocfs2_extent_block *eb;
930
931         mlog_entry_void();
932
933         count = 0;
934         while (count < wanted) {
935                 status = ocfs2_claim_metadata(osb,
936                                               handle,
937                                               meta_ac,
938                                               wanted - count,
939                                               &suballoc_bit_start,
940                                               &num_got,
941                                               &first_blkno);
942                 if (status < 0) {
943                         mlog_errno(status);
944                         goto bail;
945                 }
946
947                 for(i = count;  i < (num_got + count); i++) {
948                         bhs[i] = sb_getblk(osb->sb, first_blkno);
949                         if (bhs[i] == NULL) {
950                                 status = -EIO;
951                                 mlog_errno(status);
952                                 goto bail;
953                         }
954                         ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
955
956                         status = ocfs2_journal_access_eb(handle, et->et_ci,
957                                                          bhs[i],
958                                                          OCFS2_JOURNAL_ACCESS_CREATE);
959                         if (status < 0) {
960                                 mlog_errno(status);
961                                 goto bail;
962                         }
963
964                         memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
965                         eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
966                         /* Ok, setup the minimal stuff here. */
967                         strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
968                         eb->h_blkno = cpu_to_le64(first_blkno);
969                         eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
970                         eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
971                         eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
972                         eb->h_list.l_count =
973                                 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
974
975                         suballoc_bit_start++;
976                         first_blkno++;
977
978                         /* We'll also be dirtied by the caller, so
979                          * this isn't absolutely necessary. */
980                         status = ocfs2_journal_dirty(handle, bhs[i]);
981                         if (status < 0) {
982                                 mlog_errno(status);
983                                 goto bail;
984                         }
985                 }
986
987                 count += num_got;
988         }
989
990         status = 0;
991 bail:
992         if (status < 0) {
993                 for(i = 0; i < wanted; i++) {
994                         brelse(bhs[i]);
995                         bhs[i] = NULL;
996                 }
997         }
998         mlog_exit(status);
999         return status;
1000 }
1001
1002 /*
1003  * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1004  *
1005  * Returns the sum of the rightmost extent rec logical offset and
1006  * cluster count.
1007  *
1008  * ocfs2_add_branch() uses this to determine what logical cluster
1009  * value should be populated into the leftmost new branch records.
1010  *
1011  * ocfs2_shift_tree_depth() uses this to determine the # clusters
1012  * value for the new topmost tree record.
1013  */
1014 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
1015 {
1016         int i;
1017
1018         i = le16_to_cpu(el->l_next_free_rec) - 1;
1019
1020         return le32_to_cpu(el->l_recs[i].e_cpos) +
1021                 ocfs2_rec_clusters(el, &el->l_recs[i]);
1022 }
1023
1024 /*
1025  * Change range of the branches in the right most path according to the leaf
1026  * extent block's rightmost record.
1027  */
1028 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1029                                          struct inode *inode,
1030                                          struct ocfs2_extent_tree *et)
1031 {
1032         int status;
1033         struct ocfs2_path *path = NULL;
1034         struct ocfs2_extent_list *el;
1035         struct ocfs2_extent_rec *rec;
1036
1037         path = ocfs2_new_path_from_et(et);
1038         if (!path) {
1039                 status = -ENOMEM;
1040                 return status;
1041         }
1042
1043         status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1044         if (status < 0) {
1045                 mlog_errno(status);
1046                 goto out;
1047         }
1048
1049         status = ocfs2_extend_trans(handle, path_num_items(path) +
1050                                     handle->h_buffer_credits);
1051         if (status < 0) {
1052                 mlog_errno(status);
1053                 goto out;
1054         }
1055
1056         status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
1057         if (status < 0) {
1058                 mlog_errno(status);
1059                 goto out;
1060         }
1061
1062         el = path_leaf_el(path);
1063         rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1064
1065         ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1066
1067 out:
1068         ocfs2_free_path(path);
1069         return status;
1070 }
1071
1072 /*
1073  * Add an entire tree branch to our inode. eb_bh is the extent block
1074  * to start at, if we don't want to start the branch at the dinode
1075  * structure.
1076  *
1077  * last_eb_bh is required as we have to update it's next_leaf pointer
1078  * for the new last extent block.
1079  *
1080  * the new branch will be 'empty' in the sense that every block will
1081  * contain a single record with cluster count == 0.
1082  */
1083 static int ocfs2_add_branch(struct ocfs2_super *osb,
1084                             handle_t *handle,
1085                             struct inode *inode,
1086                             struct ocfs2_extent_tree *et,
1087                             struct buffer_head *eb_bh,
1088                             struct buffer_head **last_eb_bh,
1089                             struct ocfs2_alloc_context *meta_ac)
1090 {
1091         int status, new_blocks, i;
1092         u64 next_blkno, new_last_eb_blk;
1093         struct buffer_head *bh;
1094         struct buffer_head **new_eb_bhs = NULL;
1095         struct ocfs2_extent_block *eb;
1096         struct ocfs2_extent_list  *eb_el;
1097         struct ocfs2_extent_list  *el;
1098         u32 new_cpos, root_end;
1099
1100         mlog_entry_void();
1101
1102         BUG_ON(!last_eb_bh || !*last_eb_bh);
1103
1104         if (eb_bh) {
1105                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1106                 el = &eb->h_list;
1107         } else
1108                 el = et->et_root_el;
1109
1110         /* we never add a branch to a leaf. */
1111         BUG_ON(!el->l_tree_depth);
1112
1113         new_blocks = le16_to_cpu(el->l_tree_depth);
1114
1115         eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1116         new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1117         root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1118
1119         /*
1120          * If there is a gap before the root end and the real end
1121          * of the righmost leaf block, we need to remove the gap
1122          * between new_cpos and root_end first so that the tree
1123          * is consistent after we add a new branch(it will start
1124          * from new_cpos).
1125          */
1126         if (root_end > new_cpos) {
1127                 mlog(0, "adjust the cluster end from %u to %u\n",
1128                      root_end, new_cpos);
1129                 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1130                 if (status) {
1131                         mlog_errno(status);
1132                         goto bail;
1133                 }
1134         }
1135
1136         /* allocate the number of new eb blocks we need */
1137         new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1138                              GFP_KERNEL);
1139         if (!new_eb_bhs) {
1140                 status = -ENOMEM;
1141                 mlog_errno(status);
1142                 goto bail;
1143         }
1144
1145         status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1146                                            meta_ac, new_eb_bhs);
1147         if (status < 0) {
1148                 mlog_errno(status);
1149                 goto bail;
1150         }
1151
1152         /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1153          * linked with the rest of the tree.
1154          * conversly, new_eb_bhs[0] is the new bottommost leaf.
1155          *
1156          * when we leave the loop, new_last_eb_blk will point to the
1157          * newest leaf, and next_blkno will point to the topmost extent
1158          * block. */
1159         next_blkno = new_last_eb_blk = 0;
1160         for(i = 0; i < new_blocks; i++) {
1161                 bh = new_eb_bhs[i];
1162                 eb = (struct ocfs2_extent_block *) bh->b_data;
1163                 /* ocfs2_create_new_meta_bhs() should create it right! */
1164                 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1165                 eb_el = &eb->h_list;
1166
1167                 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh,
1168                                                  OCFS2_JOURNAL_ACCESS_CREATE);
1169                 if (status < 0) {
1170                         mlog_errno(status);
1171                         goto bail;
1172                 }
1173
1174                 eb->h_next_leaf_blk = 0;
1175                 eb_el->l_tree_depth = cpu_to_le16(i);
1176                 eb_el->l_next_free_rec = cpu_to_le16(1);
1177                 /*
1178                  * This actually counts as an empty extent as
1179                  * c_clusters == 0
1180                  */
1181                 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1182                 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1183                 /*
1184                  * eb_el isn't always an interior node, but even leaf
1185                  * nodes want a zero'd flags and reserved field so
1186                  * this gets the whole 32 bits regardless of use.
1187                  */
1188                 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1189                 if (!eb_el->l_tree_depth)
1190                         new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1191
1192                 status = ocfs2_journal_dirty(handle, bh);
1193                 if (status < 0) {
1194                         mlog_errno(status);
1195                         goto bail;
1196                 }
1197
1198                 next_blkno = le64_to_cpu(eb->h_blkno);
1199         }
1200
1201         /* This is a bit hairy. We want to update up to three blocks
1202          * here without leaving any of them in an inconsistent state
1203          * in case of error. We don't have to worry about
1204          * journal_dirty erroring as it won't unless we've aborted the
1205          * handle (in which case we would never be here) so reserving
1206          * the write with journal_access is all we need to do. */
1207         status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh,
1208                                          OCFS2_JOURNAL_ACCESS_WRITE);
1209         if (status < 0) {
1210                 mlog_errno(status);
1211                 goto bail;
1212         }
1213         status = ocfs2_et_root_journal_access(handle, et,
1214                                               OCFS2_JOURNAL_ACCESS_WRITE);
1215         if (status < 0) {
1216                 mlog_errno(status);
1217                 goto bail;
1218         }
1219         if (eb_bh) {
1220                 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh,
1221                                                  OCFS2_JOURNAL_ACCESS_WRITE);
1222                 if (status < 0) {
1223                         mlog_errno(status);
1224                         goto bail;
1225                 }
1226         }
1227
1228         /* Link the new branch into the rest of the tree (el will
1229          * either be on the root_bh, or the extent block passed in. */
1230         i = le16_to_cpu(el->l_next_free_rec);
1231         el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1232         el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1233         el->l_recs[i].e_int_clusters = 0;
1234         le16_add_cpu(&el->l_next_free_rec, 1);
1235
1236         /* fe needs a new last extent block pointer, as does the
1237          * next_leaf on the previously last-extent-block. */
1238         ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1239
1240         eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1241         eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1242
1243         status = ocfs2_journal_dirty(handle, *last_eb_bh);
1244         if (status < 0)
1245                 mlog_errno(status);
1246         status = ocfs2_journal_dirty(handle, et->et_root_bh);
1247         if (status < 0)
1248                 mlog_errno(status);
1249         if (eb_bh) {
1250                 status = ocfs2_journal_dirty(handle, eb_bh);
1251                 if (status < 0)
1252                         mlog_errno(status);
1253         }
1254
1255         /*
1256          * Some callers want to track the rightmost leaf so pass it
1257          * back here.
1258          */
1259         brelse(*last_eb_bh);
1260         get_bh(new_eb_bhs[0]);
1261         *last_eb_bh = new_eb_bhs[0];
1262
1263         status = 0;
1264 bail:
1265         if (new_eb_bhs) {
1266                 for (i = 0; i < new_blocks; i++)
1267                         brelse(new_eb_bhs[i]);
1268                 kfree(new_eb_bhs);
1269         }
1270
1271         mlog_exit(status);
1272         return status;
1273 }
1274
1275 /*
1276  * adds another level to the allocation tree.
1277  * returns back the new extent block so you can add a branch to it
1278  * after this call.
1279  */
1280 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
1281                                   handle_t *handle,
1282                                   struct inode *inode,
1283                                   struct ocfs2_extent_tree *et,
1284                                   struct ocfs2_alloc_context *meta_ac,
1285                                   struct buffer_head **ret_new_eb_bh)
1286 {
1287         int status, i;
1288         u32 new_clusters;
1289         struct buffer_head *new_eb_bh = NULL;
1290         struct ocfs2_extent_block *eb;
1291         struct ocfs2_extent_list  *root_el;
1292         struct ocfs2_extent_list  *eb_el;
1293
1294         mlog_entry_void();
1295
1296         status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1297                                            &new_eb_bh);
1298         if (status < 0) {
1299                 mlog_errno(status);
1300                 goto bail;
1301         }
1302
1303         eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1304         /* ocfs2_create_new_meta_bhs() should create it right! */
1305         BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1306
1307         eb_el = &eb->h_list;
1308         root_el = et->et_root_el;
1309
1310         status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh,
1311                                          OCFS2_JOURNAL_ACCESS_CREATE);
1312         if (status < 0) {
1313                 mlog_errno(status);
1314                 goto bail;
1315         }
1316
1317         /* copy the root extent list data into the new extent block */
1318         eb_el->l_tree_depth = root_el->l_tree_depth;
1319         eb_el->l_next_free_rec = root_el->l_next_free_rec;
1320         for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1321                 eb_el->l_recs[i] = root_el->l_recs[i];
1322
1323         status = ocfs2_journal_dirty(handle, new_eb_bh);
1324         if (status < 0) {
1325                 mlog_errno(status);
1326                 goto bail;
1327         }
1328
1329         status = ocfs2_et_root_journal_access(handle, et,
1330                                               OCFS2_JOURNAL_ACCESS_WRITE);
1331         if (status < 0) {
1332                 mlog_errno(status);
1333                 goto bail;
1334         }
1335
1336         new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1337
1338         /* update root_bh now */
1339         le16_add_cpu(&root_el->l_tree_depth, 1);
1340         root_el->l_recs[0].e_cpos = 0;
1341         root_el->l_recs[0].e_blkno = eb->h_blkno;
1342         root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1343         for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1344                 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1345         root_el->l_next_free_rec = cpu_to_le16(1);
1346
1347         /* If this is our 1st tree depth shift, then last_eb_blk
1348          * becomes the allocated extent block */
1349         if (root_el->l_tree_depth == cpu_to_le16(1))
1350                 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1351
1352         status = ocfs2_journal_dirty(handle, et->et_root_bh);
1353         if (status < 0) {
1354                 mlog_errno(status);
1355                 goto bail;
1356         }
1357
1358         *ret_new_eb_bh = new_eb_bh;
1359         new_eb_bh = NULL;
1360         status = 0;
1361 bail:
1362         brelse(new_eb_bh);
1363
1364         mlog_exit(status);
1365         return status;
1366 }
1367
1368 /*
1369  * Should only be called when there is no space left in any of the
1370  * leaf nodes. What we want to do is find the lowest tree depth
1371  * non-leaf extent block with room for new records. There are three
1372  * valid results of this search:
1373  *
1374  * 1) a lowest extent block is found, then we pass it back in
1375  *    *lowest_eb_bh and return '0'
1376  *
1377  * 2) the search fails to find anything, but the root_el has room. We
1378  *    pass NULL back in *lowest_eb_bh, but still return '0'
1379  *
1380  * 3) the search fails to find anything AND the root_el is full, in
1381  *    which case we return > 0
1382  *
1383  * return status < 0 indicates an error.
1384  */
1385 static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1386                                     struct ocfs2_extent_tree *et,
1387                                     struct buffer_head **target_bh)
1388 {
1389         int status = 0, i;
1390         u64 blkno;
1391         struct ocfs2_extent_block *eb;
1392         struct ocfs2_extent_list  *el;
1393         struct buffer_head *bh = NULL;
1394         struct buffer_head *lowest_bh = NULL;
1395
1396         mlog_entry_void();
1397
1398         *target_bh = NULL;
1399
1400         el = et->et_root_el;
1401
1402         while(le16_to_cpu(el->l_tree_depth) > 1) {
1403                 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1404                         ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1405                                     "Owner %llu has empty "
1406                                     "extent list (next_free_rec == 0)",
1407                                     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1408                         status = -EIO;
1409                         goto bail;
1410                 }
1411                 i = le16_to_cpu(el->l_next_free_rec) - 1;
1412                 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1413                 if (!blkno) {
1414                         ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1415                                     "Owner %llu has extent "
1416                                     "list where extent # %d has no physical "
1417                                     "block start",
1418                                     (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1419                         status = -EIO;
1420                         goto bail;
1421                 }
1422
1423                 brelse(bh);
1424                 bh = NULL;
1425
1426                 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1427                 if (status < 0) {
1428                         mlog_errno(status);
1429                         goto bail;
1430                 }
1431
1432                 eb = (struct ocfs2_extent_block *) bh->b_data;
1433                 el = &eb->h_list;
1434
1435                 if (le16_to_cpu(el->l_next_free_rec) <
1436                     le16_to_cpu(el->l_count)) {
1437                         brelse(lowest_bh);
1438                         lowest_bh = bh;
1439                         get_bh(lowest_bh);
1440                 }
1441         }
1442
1443         /* If we didn't find one and the fe doesn't have any room,
1444          * then return '1' */
1445         el = et->et_root_el;
1446         if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1447                 status = 1;
1448
1449         *target_bh = lowest_bh;
1450 bail:
1451         brelse(bh);
1452
1453         mlog_exit(status);
1454         return status;
1455 }
1456
1457 /*
1458  * Grow a b-tree so that it has more records.
1459  *
1460  * We might shift the tree depth in which case existing paths should
1461  * be considered invalid.
1462  *
1463  * Tree depth after the grow is returned via *final_depth.
1464  *
1465  * *last_eb_bh will be updated by ocfs2_add_branch().
1466  */
1467 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
1468                            struct ocfs2_extent_tree *et, int *final_depth,
1469                            struct buffer_head **last_eb_bh,
1470                            struct ocfs2_alloc_context *meta_ac)
1471 {
1472         int ret, shift;
1473         struct ocfs2_extent_list *el = et->et_root_el;
1474         int depth = le16_to_cpu(el->l_tree_depth);
1475         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1476         struct buffer_head *bh = NULL;
1477
1478         BUG_ON(meta_ac == NULL);
1479
1480         shift = ocfs2_find_branch_target(osb, et, &bh);
1481         if (shift < 0) {
1482                 ret = shift;
1483                 mlog_errno(ret);
1484                 goto out;
1485         }
1486
1487         /* We traveled all the way to the bottom of the allocation tree
1488          * and didn't find room for any more extents - we need to add
1489          * another tree level */
1490         if (shift) {
1491                 BUG_ON(bh);
1492                 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1493
1494                 /* ocfs2_shift_tree_depth will return us a buffer with
1495                  * the new extent block (so we can pass that to
1496                  * ocfs2_add_branch). */
1497                 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
1498                                              meta_ac, &bh);
1499                 if (ret < 0) {
1500                         mlog_errno(ret);
1501                         goto out;
1502                 }
1503                 depth++;
1504                 if (depth == 1) {
1505                         /*
1506                          * Special case: we have room now if we shifted from
1507                          * tree_depth 0, so no more work needs to be done.
1508                          *
1509                          * We won't be calling add_branch, so pass
1510                          * back *last_eb_bh as the new leaf. At depth
1511                          * zero, it should always be null so there's
1512                          * no reason to brelse.
1513                          */
1514                         BUG_ON(*last_eb_bh);
1515                         get_bh(bh);
1516                         *last_eb_bh = bh;
1517                         goto out;
1518                 }
1519         }
1520
1521         /* call ocfs2_add_branch to add the final part of the tree with
1522          * the new data. */
1523         mlog(0, "add branch. bh = %p\n", bh);
1524         ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
1525                                meta_ac);
1526         if (ret < 0) {
1527                 mlog_errno(ret);
1528                 goto out;
1529         }
1530
1531 out:
1532         if (final_depth)
1533                 *final_depth = depth;
1534         brelse(bh);
1535         return ret;
1536 }
1537
1538 /*
1539  * This function will discard the rightmost extent record.
1540  */
1541 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1542 {
1543         int next_free = le16_to_cpu(el->l_next_free_rec);
1544         int count = le16_to_cpu(el->l_count);
1545         unsigned int num_bytes;
1546
1547         BUG_ON(!next_free);
1548         /* This will cause us to go off the end of our extent list. */
1549         BUG_ON(next_free >= count);
1550
1551         num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1552
1553         memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1554 }
1555
1556 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1557                               struct ocfs2_extent_rec *insert_rec)
1558 {
1559         int i, insert_index, next_free, has_empty, num_bytes;
1560         u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1561         struct ocfs2_extent_rec *rec;
1562
1563         next_free = le16_to_cpu(el->l_next_free_rec);
1564         has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1565
1566         BUG_ON(!next_free);
1567
1568         /* The tree code before us didn't allow enough room in the leaf. */
1569         BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1570
1571         /*
1572          * The easiest way to approach this is to just remove the
1573          * empty extent and temporarily decrement next_free.
1574          */
1575         if (has_empty) {
1576                 /*
1577                  * If next_free was 1 (only an empty extent), this
1578                  * loop won't execute, which is fine. We still want
1579                  * the decrement above to happen.
1580                  */
1581                 for(i = 0; i < (next_free - 1); i++)
1582                         el->l_recs[i] = el->l_recs[i+1];
1583
1584                 next_free--;
1585         }
1586
1587         /*
1588          * Figure out what the new record index should be.
1589          */
1590         for(i = 0; i < next_free; i++) {
1591                 rec = &el->l_recs[i];
1592
1593                 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1594                         break;
1595         }
1596         insert_index = i;
1597
1598         mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1599              insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1600
1601         BUG_ON(insert_index < 0);
1602         BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1603         BUG_ON(insert_index > next_free);
1604
1605         /*
1606          * No need to memmove if we're just adding to the tail.
1607          */
1608         if (insert_index != next_free) {
1609                 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1610
1611                 num_bytes = next_free - insert_index;
1612                 num_bytes *= sizeof(struct ocfs2_extent_rec);
1613                 memmove(&el->l_recs[insert_index + 1],
1614                         &el->l_recs[insert_index],
1615                         num_bytes);
1616         }
1617
1618         /*
1619          * Either we had an empty extent, and need to re-increment or
1620          * there was no empty extent on a non full rightmost leaf node,
1621          * in which case we still need to increment.
1622          */
1623         next_free++;
1624         el->l_next_free_rec = cpu_to_le16(next_free);
1625         /*
1626          * Make sure none of the math above just messed up our tree.
1627          */
1628         BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1629
1630         el->l_recs[insert_index] = *insert_rec;
1631
1632 }
1633
1634 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1635 {
1636         int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1637
1638         BUG_ON(num_recs == 0);
1639
1640         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1641                 num_recs--;
1642                 size = num_recs * sizeof(struct ocfs2_extent_rec);
1643                 memmove(&el->l_recs[0], &el->l_recs[1], size);
1644                 memset(&el->l_recs[num_recs], 0,
1645                        sizeof(struct ocfs2_extent_rec));
1646                 el->l_next_free_rec = cpu_to_le16(num_recs);
1647         }
1648 }
1649
1650 /*
1651  * Create an empty extent record .
1652  *
1653  * l_next_free_rec may be updated.
1654  *
1655  * If an empty extent already exists do nothing.
1656  */
1657 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1658 {
1659         int next_free = le16_to_cpu(el->l_next_free_rec);
1660
1661         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1662
1663         if (next_free == 0)
1664                 goto set_and_inc;
1665
1666         if (ocfs2_is_empty_extent(&el->l_recs[0]))
1667                 return;
1668
1669         mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1670                         "Asked to create an empty extent in a full list:\n"
1671                         "count = %u, tree depth = %u",
1672                         le16_to_cpu(el->l_count),
1673                         le16_to_cpu(el->l_tree_depth));
1674
1675         ocfs2_shift_records_right(el);
1676
1677 set_and_inc:
1678         le16_add_cpu(&el->l_next_free_rec, 1);
1679         memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1680 }
1681
1682 /*
1683  * For a rotation which involves two leaf nodes, the "root node" is
1684  * the lowest level tree node which contains a path to both leafs. This
1685  * resulting set of information can be used to form a complete "subtree"
1686  *
1687  * This function is passed two full paths from the dinode down to a
1688  * pair of adjacent leaves. It's task is to figure out which path
1689  * index contains the subtree root - this can be the root index itself
1690  * in a worst-case rotation.
1691  *
1692  * The array index of the subtree root is passed back.
1693  */
1694 static int ocfs2_find_subtree_root(struct inode *inode,
1695                                    struct ocfs2_path *left,
1696                                    struct ocfs2_path *right)
1697 {
1698         int i = 0;
1699
1700         /*
1701          * Check that the caller passed in two paths from the same tree.
1702          */
1703         BUG_ON(path_root_bh(left) != path_root_bh(right));
1704
1705         do {
1706                 i++;
1707
1708                 /*
1709                  * The caller didn't pass two adjacent paths.
1710                  */
1711                 mlog_bug_on_msg(i > left->p_tree_depth,
1712                                 "Inode %lu, left depth %u, right depth %u\n"
1713                                 "left leaf blk %llu, right leaf blk %llu\n",
1714                                 inode->i_ino, left->p_tree_depth,
1715                                 right->p_tree_depth,
1716                                 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1717                                 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1718         } while (left->p_node[i].bh->b_blocknr ==
1719                  right->p_node[i].bh->b_blocknr);
1720
1721         return i - 1;
1722 }
1723
1724 typedef void (path_insert_t)(void *, struct buffer_head *);
1725
1726 /*
1727  * Traverse a btree path in search of cpos, starting at root_el.
1728  *
1729  * This code can be called with a cpos larger than the tree, in which
1730  * case it will return the rightmost path.
1731  */
1732 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1733                              struct ocfs2_extent_list *root_el, u32 cpos,
1734                              path_insert_t *func, void *data)
1735 {
1736         int i, ret = 0;
1737         u32 range;
1738         u64 blkno;
1739         struct buffer_head *bh = NULL;
1740         struct ocfs2_extent_block *eb;
1741         struct ocfs2_extent_list *el;
1742         struct ocfs2_extent_rec *rec;
1743
1744         el = root_el;
1745         while (el->l_tree_depth) {
1746                 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1747                         ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1748                                     "Owner %llu has empty extent list at "
1749                                     "depth %u\n",
1750                                     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1751                                     le16_to_cpu(el->l_tree_depth));
1752                         ret = -EROFS;
1753                         goto out;
1754
1755                 }
1756
1757                 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1758                         rec = &el->l_recs[i];
1759
1760                         /*
1761                          * In the case that cpos is off the allocation
1762                          * tree, this should just wind up returning the
1763                          * rightmost record.
1764                          */
1765                         range = le32_to_cpu(rec->e_cpos) +
1766                                 ocfs2_rec_clusters(el, rec);
1767                         if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1768                             break;
1769                 }
1770
1771                 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1772                 if (blkno == 0) {
1773                         ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1774                                     "Owner %llu has bad blkno in extent list "
1775                                     "at depth %u (index %d)\n",
1776                                     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1777                                     le16_to_cpu(el->l_tree_depth), i);
1778                         ret = -EROFS;
1779                         goto out;
1780                 }
1781
1782                 brelse(bh);
1783                 bh = NULL;
1784                 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1785                 if (ret) {
1786                         mlog_errno(ret);
1787                         goto out;
1788                 }
1789
1790                 eb = (struct ocfs2_extent_block *) bh->b_data;
1791                 el = &eb->h_list;
1792
1793                 if (le16_to_cpu(el->l_next_free_rec) >
1794                     le16_to_cpu(el->l_count)) {
1795                         ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1796                                     "Owner %llu has bad count in extent list "
1797                                     "at block %llu (next free=%u, count=%u)\n",
1798                                     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1799                                     (unsigned long long)bh->b_blocknr,
1800                                     le16_to_cpu(el->l_next_free_rec),
1801                                     le16_to_cpu(el->l_count));
1802                         ret = -EROFS;
1803                         goto out;
1804                 }
1805
1806                 if (func)
1807                         func(data, bh);
1808         }
1809
1810 out:
1811         /*
1812          * Catch any trailing bh that the loop didn't handle.
1813          */
1814         brelse(bh);
1815
1816         return ret;
1817 }
1818
1819 /*
1820  * Given an initialized path (that is, it has a valid root extent
1821  * list), this function will traverse the btree in search of the path
1822  * which would contain cpos.
1823  *
1824  * The path traveled is recorded in the path structure.
1825  *
1826  * Note that this will not do any comparisons on leaf node extent
1827  * records, so it will work fine in the case that we just added a tree
1828  * branch.
1829  */
1830 struct find_path_data {
1831         int index;
1832         struct ocfs2_path *path;
1833 };
1834 static void find_path_ins(void *data, struct buffer_head *bh)
1835 {
1836         struct find_path_data *fp = data;
1837
1838         get_bh(bh);
1839         ocfs2_path_insert_eb(fp->path, fp->index, bh);
1840         fp->index++;
1841 }
1842 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1843                            struct ocfs2_path *path, u32 cpos)
1844 {
1845         struct find_path_data data;
1846
1847         data.index = 1;
1848         data.path = path;
1849         return __ocfs2_find_path(ci, path_root_el(path), cpos,
1850                                  find_path_ins, &data);
1851 }
1852
1853 static void find_leaf_ins(void *data, struct buffer_head *bh)
1854 {
1855         struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1856         struct ocfs2_extent_list *el = &eb->h_list;
1857         struct buffer_head **ret = data;
1858
1859         /* We want to retain only the leaf block. */
1860         if (le16_to_cpu(el->l_tree_depth) == 0) {
1861                 get_bh(bh);
1862                 *ret = bh;
1863         }
1864 }
1865 /*
1866  * Find the leaf block in the tree which would contain cpos. No
1867  * checking of the actual leaf is done.
1868  *
1869  * Some paths want to call this instead of allocating a path structure
1870  * and calling ocfs2_find_path().
1871  *
1872  * This function doesn't handle non btree extent lists.
1873  */
1874 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1875                     struct ocfs2_extent_list *root_el, u32 cpos,
1876                     struct buffer_head **leaf_bh)
1877 {
1878         int ret;
1879         struct buffer_head *bh = NULL;
1880
1881         ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1882         if (ret) {
1883                 mlog_errno(ret);
1884                 goto out;
1885         }
1886
1887         *leaf_bh = bh;
1888 out:
1889         return ret;
1890 }
1891
1892 /*
1893  * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1894  *
1895  * Basically, we've moved stuff around at the bottom of the tree and
1896  * we need to fix up the extent records above the changes to reflect
1897  * the new changes.
1898  *
1899  * left_rec: the record on the left.
1900  * left_child_el: is the child list pointed to by left_rec
1901  * right_rec: the record to the right of left_rec
1902  * right_child_el: is the child list pointed to by right_rec
1903  *
1904  * By definition, this only works on interior nodes.
1905  */
1906 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1907                                   struct ocfs2_extent_list *left_child_el,
1908                                   struct ocfs2_extent_rec *right_rec,
1909                                   struct ocfs2_extent_list *right_child_el)
1910 {
1911         u32 left_clusters, right_end;
1912
1913         /*
1914          * Interior nodes never have holes. Their cpos is the cpos of
1915          * the leftmost record in their child list. Their cluster
1916          * count covers the full theoretical range of their child list
1917          * - the range between their cpos and the cpos of the record
1918          * immediately to their right.
1919          */
1920         left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1921         if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1922                 BUG_ON(right_child_el->l_tree_depth);
1923                 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1924                 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1925         }
1926         left_clusters -= le32_to_cpu(left_rec->e_cpos);
1927         left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1928
1929         /*
1930          * Calculate the rightmost cluster count boundary before
1931          * moving cpos - we will need to adjust clusters after
1932          * updating e_cpos to keep the same highest cluster count.
1933          */
1934         right_end = le32_to_cpu(right_rec->e_cpos);
1935         right_end += le32_to_cpu(right_rec->e_int_clusters);
1936
1937         right_rec->e_cpos = left_rec->e_cpos;
1938         le32_add_cpu(&right_rec->e_cpos, left_clusters);
1939
1940         right_end -= le32_to_cpu(right_rec->e_cpos);
1941         right_rec->e_int_clusters = cpu_to_le32(right_end);
1942 }
1943
1944 /*
1945  * Adjust the adjacent root node records involved in a
1946  * rotation. left_el_blkno is passed in as a key so that we can easily
1947  * find it's index in the root list.
1948  */
1949 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1950                                       struct ocfs2_extent_list *left_el,
1951                                       struct ocfs2_extent_list *right_el,
1952                                       u64 left_el_blkno)
1953 {
1954         int i;
1955
1956         BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1957                le16_to_cpu(left_el->l_tree_depth));
1958
1959         for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1960                 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1961                         break;
1962         }
1963
1964         /*
1965          * The path walking code should have never returned a root and
1966          * two paths which are not adjacent.
1967          */
1968         BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1969
1970         ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1971                                       &root_el->l_recs[i + 1], right_el);
1972 }
1973
1974 /*
1975  * We've changed a leaf block (in right_path) and need to reflect that
1976  * change back up the subtree.
1977  *
1978  * This happens in multiple places:
1979  *   - When we've moved an extent record from the left path leaf to the right
1980  *     path leaf to make room for an empty extent in the left path leaf.
1981  *   - When our insert into the right path leaf is at the leftmost edge
1982  *     and requires an update of the path immediately to it's left. This
1983  *     can occur at the end of some types of rotation and appending inserts.
1984  *   - When we've adjusted the last extent record in the left path leaf and the
1985  *     1st extent record in the right path leaf during cross extent block merge.
1986  */
1987 static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1988                                        struct ocfs2_path *left_path,
1989                                        struct ocfs2_path *right_path,
1990                                        int subtree_index)
1991 {
1992         int ret, i, idx;
1993         struct ocfs2_extent_list *el, *left_el, *right_el;
1994         struct ocfs2_extent_rec *left_rec, *right_rec;
1995         struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1996
1997         /*
1998          * Update the counts and position values within all the
1999          * interior nodes to reflect the leaf rotation we just did.
2000          *
2001          * The root node is handled below the loop.
2002          *
2003          * We begin the loop with right_el and left_el pointing to the
2004          * leaf lists and work our way up.
2005          *
2006          * NOTE: within this loop, left_el and right_el always refer
2007          * to the *child* lists.
2008          */
2009         left_el = path_leaf_el(left_path);
2010         right_el = path_leaf_el(right_path);
2011         for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2012                 mlog(0, "Adjust records at index %u\n", i);
2013
2014                 /*
2015                  * One nice property of knowing that all of these
2016                  * nodes are below the root is that we only deal with
2017                  * the leftmost right node record and the rightmost
2018                  * left node record.
2019                  */
2020                 el = left_path->p_node[i].el;
2021                 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2022                 left_rec = &el->l_recs[idx];
2023
2024                 el = right_path->p_node[i].el;
2025                 right_rec = &el->l_recs[0];
2026
2027                 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2028                                               right_el);
2029
2030                 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2031                 if (ret)
2032                         mlog_errno(ret);
2033
2034                 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2035                 if (ret)
2036                         mlog_errno(ret);
2037
2038                 /*
2039                  * Setup our list pointers now so that the current
2040                  * parents become children in the next iteration.
2041                  */
2042                 left_el = left_path->p_node[i].el;
2043                 right_el = right_path->p_node[i].el;
2044         }
2045
2046         /*
2047          * At the root node, adjust the two adjacent records which
2048          * begin our path to the leaves.
2049          */
2050
2051         el = left_path->p_node[subtree_index].el;
2052         left_el = left_path->p_node[subtree_index + 1].el;
2053         right_el = right_path->p_node[subtree_index + 1].el;
2054
2055         ocfs2_adjust_root_records(el, left_el, right_el,
2056                                   left_path->p_node[subtree_index + 1].bh->b_blocknr);
2057
2058         root_bh = left_path->p_node[subtree_index].bh;
2059
2060         ret = ocfs2_journal_dirty(handle, root_bh);
2061         if (ret)
2062                 mlog_errno(ret);
2063 }
2064
2065 static int ocfs2_rotate_subtree_right(struct inode *inode,
2066                                       handle_t *handle,
2067                                       struct ocfs2_path *left_path,
2068                                       struct ocfs2_path *right_path,
2069                                       int subtree_index)
2070 {
2071         int ret, i;
2072         struct buffer_head *right_leaf_bh;
2073         struct buffer_head *left_leaf_bh = NULL;
2074         struct buffer_head *root_bh;
2075         struct ocfs2_extent_list *right_el, *left_el;
2076         struct ocfs2_extent_rec move_rec;
2077
2078         left_leaf_bh = path_leaf_bh(left_path);
2079         left_el = path_leaf_el(left_path);
2080
2081         if (left_el->l_next_free_rec != left_el->l_count) {
2082                 ocfs2_error(inode->i_sb,
2083                             "Inode %llu has non-full interior leaf node %llu"
2084                             "(next free = %u)",
2085                             (unsigned long long)OCFS2_I(inode)->ip_blkno,
2086                             (unsigned long long)left_leaf_bh->b_blocknr,
2087                             le16_to_cpu(left_el->l_next_free_rec));
2088                 return -EROFS;
2089         }
2090
2091         /*
2092          * This extent block may already have an empty record, so we
2093          * return early if so.
2094          */
2095         if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2096                 return 0;
2097
2098         root_bh = left_path->p_node[subtree_index].bh;
2099         BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2100
2101         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
2102                                            subtree_index);
2103         if (ret) {
2104                 mlog_errno(ret);
2105                 goto out;
2106         }
2107
2108         for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2109                 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2110                                                    right_path, i);
2111                 if (ret) {
2112                         mlog_errno(ret);
2113                         goto out;
2114                 }
2115
2116                 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2117                                                    left_path, i);
2118                 if (ret) {
2119                         mlog_errno(ret);
2120                         goto out;
2121                 }
2122         }
2123
2124         right_leaf_bh = path_leaf_bh(right_path);
2125         right_el = path_leaf_el(right_path);
2126
2127         /* This is a code error, not a disk corruption. */
2128         mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2129                         "because rightmost leaf block %llu is empty\n",
2130                         (unsigned long long)OCFS2_I(inode)->ip_blkno,
2131                         (unsigned long long)right_leaf_bh->b_blocknr);
2132
2133         ocfs2_create_empty_extent(right_el);
2134
2135         ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2136         if (ret) {
2137                 mlog_errno(ret);
2138                 goto out;
2139         }
2140
2141         /* Do the copy now. */
2142         i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2143         move_rec = left_el->l_recs[i];
2144         right_el->l_recs[0] = move_rec;
2145
2146         /*
2147          * Clear out the record we just copied and shift everything
2148          * over, leaving an empty extent in the left leaf.
2149          *
2150          * We temporarily subtract from next_free_rec so that the
2151          * shift will lose the tail record (which is now defunct).
2152          */
2153         le16_add_cpu(&left_el->l_next_free_rec, -1);
2154         ocfs2_shift_records_right(left_el);
2155         memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2156         le16_add_cpu(&left_el->l_next_free_rec, 1);
2157
2158         ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2159         if (ret) {
2160                 mlog_errno(ret);
2161                 goto out;
2162         }
2163
2164         ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2165                                 subtree_index);
2166
2167 out:
2168         return ret;
2169 }
2170
2171 /*
2172  * Given a full path, determine what cpos value would return us a path
2173  * containing the leaf immediately to the left of the current one.
2174  *
2175  * Will return zero if the path passed in is already the leftmost path.
2176  */
2177 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2178                                          struct ocfs2_path *path, u32 *cpos)
2179 {
2180         int i, j, ret = 0;
2181         u64 blkno;
2182         struct ocfs2_extent_list *el;
2183
2184         BUG_ON(path->p_tree_depth == 0);
2185
2186         *cpos = 0;
2187
2188         blkno = path_leaf_bh(path)->b_blocknr;
2189
2190         /* Start at the tree node just above the leaf and work our way up. */
2191         i = path->p_tree_depth - 1;
2192         while (i >= 0) {
2193                 el = path->p_node[i].el;
2194
2195                 /*
2196                  * Find the extent record just before the one in our
2197                  * path.
2198                  */
2199                 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2200                         if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2201                                 if (j == 0) {
2202                                         if (i == 0) {
2203                                                 /*
2204                                                  * We've determined that the
2205                                                  * path specified is already
2206                                                  * the leftmost one - return a
2207                                                  * cpos of zero.
2208                                                  */
2209                                                 goto out;
2210                                         }
2211                                         /*
2212                                          * The leftmost record points to our
2213                                          * leaf - we need to travel up the
2214                                          * tree one level.
2215                                          */
2216                                         goto next_node;
2217                                 }
2218
2219                                 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2220                                 *cpos = *cpos + ocfs2_rec_clusters(el,
2221                                                            &el->l_recs[j - 1]);
2222                                 *cpos = *cpos - 1;
2223                                 goto out;
2224                         }
2225                 }
2226
2227                 /*
2228                  * If we got here, we never found a valid node where
2229                  * the tree indicated one should be.
2230                  */
2231                 ocfs2_error(sb,
2232                             "Invalid extent tree at extent block %llu\n",
2233                             (unsigned long long)blkno);
2234                 ret = -EROFS;
2235                 goto out;
2236
2237 next_node:
2238                 blkno = path->p_node[i].bh->b_blocknr;
2239                 i--;
2240         }
2241
2242 out:
2243         return ret;
2244 }
2245
2246 /*
2247  * Extend the transaction by enough credits to complete the rotation,
2248  * and still leave at least the original number of credits allocated
2249  * to this transaction.
2250  */
2251 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2252                                            int op_credits,
2253                                            struct ocfs2_path *path)
2254 {
2255         int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2256
2257         if (handle->h_buffer_credits < credits)
2258                 return ocfs2_extend_trans(handle, credits);
2259
2260         return 0;
2261 }
2262
2263 /*
2264  * Trap the case where we're inserting into the theoretical range past
2265  * the _actual_ left leaf range. Otherwise, we'll rotate a record
2266  * whose cpos is less than ours into the right leaf.
2267  *
2268  * It's only necessary to look at the rightmost record of the left
2269  * leaf because the logic that calls us should ensure that the
2270  * theoretical ranges in the path components above the leaves are
2271  * correct.
2272  */
2273 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2274                                                  u32 insert_cpos)
2275 {
2276         struct ocfs2_extent_list *left_el;
2277         struct ocfs2_extent_rec *rec;
2278         int next_free;
2279
2280         left_el = path_leaf_el(left_path);
2281         next_free = le16_to_cpu(left_el->l_next_free_rec);
2282         rec = &left_el->l_recs[next_free - 1];
2283
2284         if (insert_cpos > le32_to_cpu(rec->e_cpos))
2285                 return 1;
2286         return 0;
2287 }
2288
2289 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2290 {
2291         int next_free = le16_to_cpu(el->l_next_free_rec);
2292         unsigned int range;
2293         struct ocfs2_extent_rec *rec;
2294
2295         if (next_free == 0)
2296                 return 0;
2297
2298         rec = &el->l_recs[0];
2299         if (ocfs2_is_empty_extent(rec)) {
2300                 /* Empty list. */
2301                 if (next_free == 1)
2302                         return 0;
2303                 rec = &el->l_recs[1];
2304         }
2305
2306         range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2307         if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2308                 return 1;
2309         return 0;
2310 }
2311
2312 /*
2313  * Rotate all the records in a btree right one record, starting at insert_cpos.
2314  *
2315  * The path to the rightmost leaf should be passed in.
2316  *
2317  * The array is assumed to be large enough to hold an entire path (tree depth).
2318  *
2319  * Upon succesful return from this function:
2320  *
2321  * - The 'right_path' array will contain a path to the leaf block
2322  *   whose range contains e_cpos.
2323  * - That leaf block will have a single empty extent in list index 0.
2324  * - In the case that the rotation requires a post-insert update,
2325  *   *ret_left_path will contain a valid path which can be passed to
2326  *   ocfs2_insert_path().
2327  */
2328 static int ocfs2_rotate_tree_right(struct inode *inode,
2329                                    handle_t *handle,
2330                                    enum ocfs2_split_type split,
2331                                    u32 insert_cpos,
2332                                    struct ocfs2_path *right_path,
2333                                    struct ocfs2_path **ret_left_path)
2334 {
2335         int ret, start, orig_credits = handle->h_buffer_credits;
2336         u32 cpos;
2337         struct ocfs2_path *left_path = NULL;
2338
2339         *ret_left_path = NULL;
2340
2341         left_path = ocfs2_new_path_from_path(right_path);
2342         if (!left_path) {
2343                 ret = -ENOMEM;
2344                 mlog_errno(ret);
2345                 goto out;
2346         }
2347
2348         ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2349         if (ret) {
2350                 mlog_errno(ret);
2351                 goto out;
2352         }
2353
2354         mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2355
2356         /*
2357          * What we want to do here is:
2358          *
2359          * 1) Start with the rightmost path.
2360          *
2361          * 2) Determine a path to the leaf block directly to the left
2362          *    of that leaf.
2363          *
2364          * 3) Determine the 'subtree root' - the lowest level tree node
2365          *    which contains a path to both leaves.
2366          *
2367          * 4) Rotate the subtree.
2368          *
2369          * 5) Find the next subtree by considering the left path to be
2370          *    the new right path.
2371          *
2372          * The check at the top of this while loop also accepts
2373          * insert_cpos == cpos because cpos is only a _theoretical_
2374          * value to get us the left path - insert_cpos might very well
2375          * be filling that hole.
2376          *
2377          * Stop at a cpos of '0' because we either started at the
2378          * leftmost branch (i.e., a tree with one branch and a
2379          * rotation inside of it), or we've gone as far as we can in
2380          * rotating subtrees.
2381          */
2382         while (cpos && insert_cpos <= cpos) {
2383                 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2384                      insert_cpos, cpos);
2385
2386                 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, cpos);
2387                 if (ret) {
2388                         mlog_errno(ret);
2389                         goto out;
2390                 }
2391
2392                 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2393                                 path_leaf_bh(right_path),
2394                                 "Inode %lu: error during insert of %u "
2395                                 "(left path cpos %u) results in two identical "
2396                                 "paths ending at %llu\n",
2397                                 inode->i_ino, insert_cpos, cpos,
2398                                 (unsigned long long)
2399                                 path_leaf_bh(left_path)->b_blocknr);
2400
2401                 if (split == SPLIT_NONE &&
2402                     ocfs2_rotate_requires_path_adjustment(left_path,
2403                                                           insert_cpos)) {
2404
2405                         /*
2406                          * We've rotated the tree as much as we
2407                          * should. The rest is up to
2408                          * ocfs2_insert_path() to complete, after the
2409                          * record insertion. We indicate this
2410                          * situation by returning the left path.
2411                          *
2412                          * The reason we don't adjust the records here
2413                          * before the record insert is that an error
2414                          * later might break the rule where a parent
2415                          * record e_cpos will reflect the actual
2416                          * e_cpos of the 1st nonempty record of the
2417                          * child list.
2418                          */
2419                         *ret_left_path = left_path;
2420                         goto out_ret_path;
2421                 }
2422
2423                 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2424
2425                 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2426                      start,
2427                      (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2428                      right_path->p_tree_depth);
2429
2430                 ret = ocfs2_extend_rotate_transaction(handle, start,
2431                                                       orig_credits, right_path);
2432                 if (ret) {
2433                         mlog_errno(ret);
2434                         goto out;
2435                 }
2436
2437                 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2438                                                  right_path, start);
2439                 if (ret) {
2440                         mlog_errno(ret);
2441                         goto out;
2442                 }
2443
2444                 if (split != SPLIT_NONE &&
2445                     ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2446                                                 insert_cpos)) {
2447                         /*
2448                          * A rotate moves the rightmost left leaf
2449                          * record over to the leftmost right leaf
2450                          * slot. If we're doing an extent split
2451                          * instead of a real insert, then we have to
2452                          * check that the extent to be split wasn't
2453                          * just moved over. If it was, then we can
2454                          * exit here, passing left_path back -
2455                          * ocfs2_split_extent() is smart enough to
2456                          * search both leaves.
2457                          */
2458                         *ret_left_path = left_path;
2459                         goto out_ret_path;
2460                 }
2461
2462                 /*
2463                  * There is no need to re-read the next right path
2464                  * as we know that it'll be our current left
2465                  * path. Optimize by copying values instead.
2466                  */
2467                 ocfs2_mv_path(right_path, left_path);
2468
2469                 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2470                                                     &cpos);
2471                 if (ret) {
2472                         mlog_errno(ret);
2473                         goto out;
2474                 }
2475         }
2476
2477 out:
2478         ocfs2_free_path(left_path);
2479
2480 out_ret_path:
2481         return ret;
2482 }
2483
2484 static int ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2485                                      int subtree_index, struct ocfs2_path *path)
2486 {
2487         int i, idx, ret;
2488         struct ocfs2_extent_rec *rec;
2489         struct ocfs2_extent_list *el;
2490         struct ocfs2_extent_block *eb;
2491         u32 range;
2492
2493         /*
2494          * In normal tree rotation process, we will never touch the
2495          * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2496          * doesn't reserve the credits for them either.
2497          *
2498          * But we do have a special case here which will update the rightmost
2499          * records for all the bh in the path.
2500          * So we have to allocate extra credits and access them.
2501          */
2502         ret = ocfs2_extend_trans(handle,
2503                                  handle->h_buffer_credits + subtree_index);
2504         if (ret) {
2505                 mlog_errno(ret);
2506                 goto out;
2507         }
2508
2509         ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
2510         if (ret) {
2511                 mlog_errno(ret);
2512                 goto out;
2513         }
2514
2515         /* Path should always be rightmost. */
2516         eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2517         BUG_ON(eb->h_next_leaf_blk != 0ULL);
2518
2519         el = &eb->h_list;
2520         BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2521         idx = le16_to_cpu(el->l_next_free_rec) - 1;
2522         rec = &el->l_recs[idx];
2523         range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2524
2525         for (i = 0; i < path->p_tree_depth; i++) {
2526                 el = path->p_node[i].el;
2527                 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2528                 rec = &el->l_recs[idx];
2529
2530                 rec->e_int_clusters = cpu_to_le32(range);
2531                 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2532
2533                 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2534         }
2535 out:
2536         return ret;
2537 }
2538
2539 static void ocfs2_unlink_path(handle_t *handle,
2540                               struct ocfs2_extent_tree *et,
2541                               struct ocfs2_cached_dealloc_ctxt *dealloc,
2542                               struct ocfs2_path *path, int unlink_start)
2543 {
2544         int ret, i;
2545         struct ocfs2_extent_block *eb;
2546         struct ocfs2_extent_list *el;
2547         struct buffer_head *bh;
2548
2549         for(i = unlink_start; i < path_num_items(path); i++) {
2550                 bh = path->p_node[i].bh;
2551
2552                 eb = (struct ocfs2_extent_block *)bh->b_data;
2553                 /*
2554                  * Not all nodes might have had their final count
2555                  * decremented by the caller - handle this here.
2556                  */
2557                 el = &eb->h_list;
2558                 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2559                         mlog(ML_ERROR,
2560                              "Inode %llu, attempted to remove extent block "
2561                              "%llu with %u records\n",
2562                              (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2563                              (unsigned long long)le64_to_cpu(eb->h_blkno),
2564                              le16_to_cpu(el->l_next_free_rec));
2565
2566                         ocfs2_journal_dirty(handle, bh);
2567                         ocfs2_remove_from_cache(et->et_ci, bh);
2568                         continue;
2569                 }
2570
2571                 el->l_next_free_rec = 0;
2572                 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2573
2574                 ocfs2_journal_dirty(handle, bh);
2575
2576                 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2577                 if (ret)
2578                         mlog_errno(ret);
2579
2580                 ocfs2_remove_from_cache(et->et_ci, bh);
2581         }
2582 }
2583
2584 static void ocfs2_unlink_subtree(handle_t *handle,
2585                                  struct ocfs2_extent_tree *et,
2586                                  struct ocfs2_path *left_path,
2587                                  struct ocfs2_path *right_path,
2588                                  int subtree_index,
2589                                  struct ocfs2_cached_dealloc_ctxt *dealloc)
2590 {
2591         int i;
2592         struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2593         struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2594         struct ocfs2_extent_list *el;
2595         struct ocfs2_extent_block *eb;
2596
2597         el = path_leaf_el(left_path);
2598
2599         eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2600
2601         for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2602                 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2603                         break;
2604
2605         BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2606
2607         memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2608         le16_add_cpu(&root_el->l_next_free_rec, -1);
2609
2610         eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2611         eb->h_next_leaf_blk = 0;
2612
2613         ocfs2_journal_dirty(handle, root_bh);
2614         ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2615
2616         ocfs2_unlink_path(handle, et, dealloc, right_path,
2617                           subtree_index + 1);
2618 }
2619
2620 static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2621                                      struct ocfs2_path *left_path,
2622                                      struct ocfs2_path *right_path,
2623                                      int subtree_index,
2624                                      struct ocfs2_cached_dealloc_ctxt *dealloc,
2625                                      int *deleted,
2626                                      struct ocfs2_extent_tree *et)
2627 {
2628         int ret, i, del_right_subtree = 0, right_has_empty = 0;
2629         struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2630         struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2631         struct ocfs2_extent_block *eb;
2632
2633         *deleted = 0;
2634
2635         right_leaf_el = path_leaf_el(right_path);
2636         left_leaf_el = path_leaf_el(left_path);
2637         root_bh = left_path->p_node[subtree_index].bh;
2638         BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2639
2640         if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2641                 return 0;
2642
2643         eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2644         if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2645                 /*
2646                  * It's legal for us to proceed if the right leaf is
2647                  * the rightmost one and it has an empty extent. There
2648                  * are two cases to handle - whether the leaf will be
2649                  * empty after removal or not. If the leaf isn't empty
2650                  * then just remove the empty extent up front. The
2651                  * next block will handle empty leaves by flagging
2652                  * them for unlink.
2653                  *
2654                  * Non rightmost leaves will throw -EAGAIN and the
2655                  * caller can manually move the subtree and retry.
2656                  */
2657
2658                 if (eb->h_next_leaf_blk != 0ULL)
2659                         return -EAGAIN;
2660
2661                 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2662                         ret = ocfs2_journal_access_eb(handle, INODE_CACHE(inode),
2663                                                       path_leaf_bh(right_path),
2664                                                       OCFS2_JOURNAL_ACCESS_WRITE);
2665                         if (ret) {
2666                                 mlog_errno(ret);
2667                                 goto out;
2668                         }
2669
2670                         ocfs2_remove_empty_extent(right_leaf_el);
2671                 } else
2672                         right_has_empty = 1;
2673         }
2674
2675         if (eb->h_next_leaf_blk == 0ULL &&
2676             le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2677                 /*
2678                  * We have to update i_last_eb_blk during the meta
2679                  * data delete.
2680                  */
2681                 ret = ocfs2_et_root_journal_access(handle, et,
2682                                                    OCFS2_JOURNAL_ACCESS_WRITE);
2683                 if (ret) {
2684                         mlog_errno(ret);
2685                         goto out;
2686                 }
2687
2688                 del_right_subtree = 1;
2689         }
2690
2691         /*
2692          * Getting here with an empty extent in the right path implies
2693          * that it's the rightmost path and will be deleted.
2694          */
2695         BUG_ON(right_has_empty && !del_right_subtree);
2696
2697         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
2698                                            subtree_index);
2699         if (ret) {
2700                 mlog_errno(ret);
2701                 goto out;
2702         }
2703
2704         for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2705                 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2706                                                    right_path, i);
2707                 if (ret) {
2708                         mlog_errno(ret);
2709                         goto out;
2710                 }
2711
2712                 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2713                                                    left_path, i);
2714                 if (ret) {
2715                         mlog_errno(ret);
2716                         goto out;
2717                 }
2718         }
2719
2720         if (!right_has_empty) {
2721                 /*
2722                  * Only do this if we're moving a real
2723                  * record. Otherwise, the action is delayed until
2724                  * after removal of the right path in which case we
2725                  * can do a simple shift to remove the empty extent.
2726                  */
2727                 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2728                 memset(&right_leaf_el->l_recs[0], 0,
2729                        sizeof(struct ocfs2_extent_rec));
2730         }
2731         if (eb->h_next_leaf_blk == 0ULL) {
2732                 /*
2733                  * Move recs over to get rid of empty extent, decrease
2734                  * next_free. This is allowed to remove the last
2735                  * extent in our leaf (setting l_next_free_rec to
2736                  * zero) - the delete code below won't care.
2737                  */
2738                 ocfs2_remove_empty_extent(right_leaf_el);
2739         }
2740
2741         ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2742         if (ret)
2743                 mlog_errno(ret);
2744         ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2745         if (ret)
2746                 mlog_errno(ret);
2747
2748         if (del_right_subtree) {
2749                 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2750                                      subtree_index, dealloc);
2751                 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
2752                                                 left_path);
2753                 if (ret) {
2754                         mlog_errno(ret);
2755                         goto out;
2756                 }
2757
2758                 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2759                 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2760
2761                 /*
2762                  * Removal of the extent in the left leaf was skipped
2763                  * above so we could delete the right path
2764                  * 1st.
2765                  */
2766                 if (right_has_empty)
2767                         ocfs2_remove_empty_extent(left_leaf_el);
2768
2769                 ret = ocfs2_journal_dirty(handle, et_root_bh);
2770                 if (ret)
2771                         mlog_errno(ret);
2772
2773                 *deleted = 1;
2774         } else
2775                 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2776                                            subtree_index);
2777
2778 out:
2779         return ret;
2780 }
2781
2782 /*
2783  * Given a full path, determine what cpos value would return us a path
2784  * containing the leaf immediately to the right of the current one.
2785  *
2786  * Will return zero if the path passed in is already the rightmost path.
2787  *
2788  * This looks similar, but is subtly different to
2789  * ocfs2_find_cpos_for_left_leaf().
2790  */
2791 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2792                                           struct ocfs2_path *path, u32 *cpos)
2793 {
2794         int i, j, ret = 0;
2795         u64 blkno;
2796         struct ocfs2_extent_list *el;
2797
2798         *cpos = 0;
2799
2800         if (path->p_tree_depth == 0)
2801                 return 0;
2802
2803         blkno = path_leaf_bh(path)->b_blocknr;
2804
2805         /* Start at the tree node just above the leaf and work our way up. */
2806         i = path->p_tree_depth - 1;
2807         while (i >= 0) {
2808                 int next_free;
2809
2810                 el = path->p_node[i].el;
2811
2812                 /*
2813                  * Find the extent record just after the one in our
2814                  * path.
2815                  */
2816                 next_free = le16_to_cpu(el->l_next_free_rec);
2817                 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2818                         if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2819                                 if (j == (next_free - 1)) {
2820                                         if (i == 0) {
2821                                                 /*
2822                                                  * We've determined that the
2823                                                  * path specified is already
2824                                                  * the rightmost one - return a
2825                                                  * cpos of zero.
2826                                                  */
2827                                                 goto out;
2828                                         }
2829                                         /*
2830                                          * The rightmost record points to our
2831                                          * leaf - we need to travel up the
2832                                          * tree one level.
2833                                          */
2834                                         goto next_node;
2835                                 }
2836
2837                                 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2838                                 goto out;
2839                         }
2840                 }
2841
2842                 /*
2843                  * If we got here, we never found a valid node where
2844                  * the tree indicated one should be.
2845                  */
2846                 ocfs2_error(sb,
2847                             "Invalid extent tree at extent block %llu\n",
2848                             (unsigned long long)blkno);
2849                 ret = -EROFS;
2850                 goto out;
2851
2852 next_node:
2853                 blkno = path->p_node[i].bh->b_blocknr;
2854                 i--;
2855         }
2856
2857 out:
2858         return ret;
2859 }
2860
2861 static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2862                                             handle_t *handle,
2863                                             struct ocfs2_path *path)
2864 {
2865         int ret;
2866         struct buffer_head *bh = path_leaf_bh(path);
2867         struct ocfs2_extent_list *el = path_leaf_el(path);
2868
2869         if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2870                 return 0;
2871
2872         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
2873                                            path_num_items(path) - 1);
2874         if (ret) {
2875                 mlog_errno(ret);
2876                 goto out;
2877         }
2878
2879         ocfs2_remove_empty_extent(el);
2880
2881         ret = ocfs2_journal_dirty(handle, bh);
2882         if (ret)
2883                 mlog_errno(ret);
2884
2885 out:
2886         return ret;
2887 }
2888
2889 static int __ocfs2_rotate_tree_left(struct inode *inode,
2890                                     handle_t *handle, int orig_credits,
2891                                     struct ocfs2_path *path,
2892                                     struct ocfs2_cached_dealloc_ctxt *dealloc,
2893                                     struct ocfs2_path **empty_extent_path,
2894                                     struct ocfs2_extent_tree *et)
2895 {
2896         int ret, subtree_root, deleted;
2897         u32 right_cpos;
2898         struct ocfs2_path *left_path = NULL;
2899         struct ocfs2_path *right_path = NULL;
2900
2901         BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2902
2903         *empty_extent_path = NULL;
2904
2905         ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2906                                              &right_cpos);
2907         if (ret) {
2908                 mlog_errno(ret);
2909                 goto out;
2910         }
2911
2912         left_path = ocfs2_new_path_from_path(path);
2913         if (!left_path) {
2914                 ret = -ENOMEM;
2915                 mlog_errno(ret);
2916                 goto out;
2917         }
2918
2919         ocfs2_cp_path(left_path, path);
2920
2921         right_path = ocfs2_new_path_from_path(path);
2922         if (!right_path) {
2923                 ret = -ENOMEM;
2924                 mlog_errno(ret);
2925                 goto out;
2926         }
2927
2928         while (right_cpos) {
2929                 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2930                 if (ret) {
2931                         mlog_errno(ret);
2932                         goto out;
2933                 }
2934
2935                 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2936                                                        right_path);
2937
2938                 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2939                      subtree_root,
2940                      (unsigned long long)
2941                      right_path->p_node[subtree_root].bh->b_blocknr,
2942                      right_path->p_tree_depth);
2943
2944                 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2945                                                       orig_credits, left_path);
2946                 if (ret) {
2947                         mlog_errno(ret);
2948                         goto out;
2949                 }
2950
2951                 /*
2952                  * Caller might still want to make changes to the
2953                  * tree root, so re-add it to the journal here.
2954                  */
2955                 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
2956                                                    left_path, 0);
2957                 if (ret) {
2958                         mlog_errno(ret);
2959                         goto out;
2960                 }
2961
2962                 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2963                                                 right_path, subtree_root,
2964                                                 dealloc, &deleted, et);
2965                 if (ret == -EAGAIN) {
2966                         /*
2967                          * The rotation has to temporarily stop due to
2968                          * the right subtree having an empty
2969                          * extent. Pass it back to the caller for a
2970                          * fixup.
2971                          */
2972                         *empty_extent_path = right_path;
2973                         right_path = NULL;
2974                         goto out;
2975                 }
2976                 if (ret) {
2977                         mlog_errno(ret);
2978                         goto out;
2979                 }
2980
2981                 /*
2982                  * The subtree rotate might have removed records on
2983                  * the rightmost edge. If so, then rotation is
2984                  * complete.
2985                  */
2986                 if (deleted)
2987                         break;
2988
2989                 ocfs2_mv_path(left_path, right_path);
2990
2991                 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2992                                                      &right_cpos);
2993                 if (ret) {
2994                         mlog_errno(ret);
2995                         goto out;
2996                 }
2997         }
2998
2999 out:
3000         ocfs2_free_path(right_path);
3001         ocfs2_free_path(left_path);
3002
3003         return ret;
3004 }
3005
3006 static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
3007                                 struct ocfs2_path *path,
3008                                 struct ocfs2_cached_dealloc_ctxt *dealloc,
3009                                 struct ocfs2_extent_tree *et)
3010 {
3011         int ret, subtree_index;
3012         u32 cpos;
3013         struct ocfs2_path *left_path = NULL;
3014         struct ocfs2_extent_block *eb;
3015         struct ocfs2_extent_list *el;
3016
3017
3018         ret = ocfs2_et_sanity_check(inode, et);
3019         if (ret)
3020                 goto out;
3021         /*
3022          * There's two ways we handle this depending on
3023          * whether path is the only existing one.
3024          */
3025         ret = ocfs2_extend_rotate_transaction(handle, 0,
3026                                               handle->h_buffer_credits,
3027                                               path);
3028         if (ret) {
3029                 mlog_errno(ret);
3030                 goto out;
3031         }
3032
3033         ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3034         if (ret) {
3035                 mlog_errno(ret);
3036                 goto out;
3037         }
3038
3039         ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3040                                             path, &cpos);
3041         if (ret) {
3042                 mlog_errno(ret);
3043                 goto out;
3044         }
3045
3046         if (cpos) {
3047                 /*
3048                  * We have a path to the left of this one - it needs
3049                  * an update too.
3050                  */
3051                 left_path = ocfs2_new_path_from_path(path);
3052                 if (!left_path) {
3053                         ret = -ENOMEM;
3054                         mlog_errno(ret);
3055                         goto out;
3056                 }
3057
3058                 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3059                 if (ret) {
3060                         mlog_errno(ret);
3061                         goto out;
3062                 }
3063
3064                 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3065                 if (ret) {
3066                         mlog_errno(ret);
3067                         goto out;
3068                 }
3069
3070                 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
3071
3072                 ocfs2_unlink_subtree(handle, et, left_path, path,
3073                                      subtree_index, dealloc);
3074                 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
3075                                                 left_path);
3076                 if (ret) {
3077                         mlog_errno(ret);
3078                         goto out;
3079                 }
3080
3081                 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3082                 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3083         } else {
3084                 /*
3085                  * 'path' is also the leftmost path which
3086                  * means it must be the only one. This gets
3087                  * handled differently because we want to
3088                  * revert the inode back to having extents
3089                  * in-line.
3090                  */
3091                 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3092
3093                 el = et->et_root_el;
3094                 el->l_tree_depth = 0;
3095                 el->l_next_free_rec = 0;
3096                 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3097
3098                 ocfs2_et_set_last_eb_blk(et, 0);
3099         }
3100
3101         ocfs2_journal_dirty(handle, path_root_bh(path));
3102
3103 out:
3104         ocfs2_free_path(left_path);
3105         return ret;
3106 }
3107
3108 /*
3109  * Left rotation of btree records.
3110  *
3111  * In many ways, this is (unsurprisingly) the opposite of right
3112  * rotation. We start at some non-rightmost path containing an empty
3113  * extent in the leaf block. The code works its way to the rightmost
3114  * path by rotating records to the left in every subtree.
3115  *
3116  * This is used by any code which reduces the number of extent records
3117  * in a leaf. After removal, an empty record should be placed in the
3118  * leftmost list position.
3119  *
3120  * This won't handle a length update of the rightmost path records if
3121  * the rightmost tree leaf record is removed so the caller is
3122  * responsible for detecting and correcting that.
3123  */
3124 static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
3125                                   struct ocfs2_path *path,
3126                                   struct ocfs2_cached_dealloc_ctxt *dealloc,
3127                                   struct ocfs2_extent_tree *et)
3128 {
3129         int ret, orig_credits = handle->h_buffer_credits;
3130         struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3131         struct ocfs2_extent_block *eb;
3132         struct ocfs2_extent_list *el;
3133
3134         el = path_leaf_el(path);
3135         if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3136                 return 0;
3137
3138         if (path->p_tree_depth == 0) {
3139 rightmost_no_delete:
3140                 /*
3141                  * Inline extents. This is trivially handled, so do
3142                  * it up front.
3143                  */
3144                 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
3145                                                        path);
3146                 if (ret)
3147                         mlog_errno(ret);
3148                 goto out;
3149         }
3150
3151         /*
3152          * Handle rightmost branch now. There's several cases:
3153          *  1) simple rotation leaving records in there. That's trivial.
3154          *  2) rotation requiring a branch delete - there's no more
3155          *     records left. Two cases of this:
3156          *     a) There are branches to the left.
3157          *     b) This is also the leftmost (the only) branch.
3158          *
3159          *  1) is handled via ocfs2_rotate_rightmost_leaf_left()
3160          *  2a) we need the left branch so that we can update it with the unlink
3161          *  2b) we need to bring the inode back to inline extents.
3162          */
3163
3164         eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3165         el = &eb->h_list;
3166         if (eb->h_next_leaf_blk == 0) {
3167                 /*
3168                  * This gets a bit tricky if we're going to delete the
3169                  * rightmost path. Get the other cases out of the way
3170                  * 1st.
3171                  */
3172                 if (le16_to_cpu(el->l_next_free_rec) > 1)
3173                         goto rightmost_no_delete;
3174
3175                 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3176                         ret = -EIO;
3177                         ocfs2_error(inode->i_sb,
3178                                     "Inode %llu has empty extent block at %llu",
3179                                     (unsigned long long)OCFS2_I(inode)->ip_blkno,
3180                                     (unsigned long long)le64_to_cpu(eb->h_blkno));
3181                         goto out;
3182                 }
3183
3184                 /*
3185                  * XXX: The caller can not trust "path" any more after
3186                  * this as it will have been deleted. What do we do?
3187                  *
3188                  * In theory the rotate-for-merge code will never get
3189                  * here because it'll always ask for a rotate in a
3190                  * nonempty list.
3191                  */
3192
3193                 ret = ocfs2_remove_rightmost_path(inode, handle, path,
3194                                                   dealloc, et);
3195                 if (ret)
3196                         mlog_errno(ret);
3197                 goto out;
3198         }
3199
3200         /*
3201          * Now we can loop, remembering the path we get from -EAGAIN
3202          * and restarting from there.
3203          */
3204 try_rotate:
3205         ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
3206                                        dealloc, &restart_path, et);
3207         if (ret && ret != -EAGAIN) {
3208                 mlog_errno(ret);
3209                 goto out;
3210         }
3211
3212         while (ret == -EAGAIN) {
3213                 tmp_path = restart_path;
3214                 restart_path = NULL;
3215
3216                 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3217                                                tmp_path, dealloc,
3218                                                &restart_path, et);
3219                 if (ret && ret != -EAGAIN) {
3220                         mlog_errno(ret);
3221                         goto out;
3222                 }
3223
3224                 ocfs2_free_path(tmp_path);
3225                 tmp_path = NULL;
3226
3227                 if (ret == 0)
3228                         goto try_rotate;
3229         }
3230
3231 out:
3232         ocfs2_free_path(tmp_path);
3233         ocfs2_free_path(restart_path);
3234         return ret;
3235 }
3236
3237 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3238                                 int index)
3239 {
3240         struct ocfs2_extent_rec *rec = &el->l_recs[index];
3241         unsigned int size;
3242
3243         if (rec->e_leaf_clusters == 0) {
3244                 /*
3245                  * We consumed all of the merged-from record. An empty
3246                  * extent cannot exist anywhere but the 1st array
3247                  * position, so move things over if the merged-from
3248                  * record doesn't occupy that position.
3249                  *
3250                  * This creates a new empty extent so the caller
3251                  * should be smart enough to have removed any existing
3252                  * ones.
3253                  */
3254                 if (index > 0) {
3255                         BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3256                         size = index * sizeof(struct ocfs2_extent_rec);
3257                         memmove(&el->l_recs[1], &el->l_recs[0], size);
3258                 }
3259
3260                 /*
3261                  * Always memset - the caller doesn't check whether it
3262                  * created an empty extent, so there could be junk in
3263                  * the other fields.
3264                  */
3265                 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3266         }
3267 }
3268
3269 static int ocfs2_get_right_path(struct inode *inode,
3270                                 struct ocfs2_path *left_path,
3271                                 struct ocfs2_path **ret_right_path)
3272 {
3273         int ret;
3274         u32 right_cpos;
3275         struct ocfs2_path *right_path = NULL;
3276         struct ocfs2_extent_list *left_el;
3277
3278         *ret_right_path = NULL;
3279
3280         /* This function shouldn't be called for non-trees. */
3281         BUG_ON(left_path->p_tree_depth == 0);
3282
3283         left_el = path_leaf_el(left_path);
3284         BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3285
3286         ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3287                                              &right_cpos);
3288         if (ret) {
3289                 mlog_errno(ret);
3290                 goto out;
3291         }
3292
3293         /* This function shouldn't be called for the rightmost leaf. */
3294         BUG_ON(right_cpos == 0);
3295
3296         right_path = ocfs2_new_path_from_path(left_path);
3297         if (!right_path) {
3298                 ret = -ENOMEM;
3299                 mlog_errno(ret);
3300                 goto out;
3301         }
3302
3303         ret = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
3304         if (ret) {
3305                 mlog_errno(ret);
3306                 goto out;
3307         }
3308
3309         *ret_right_path = right_path;
3310 out:
3311         if (ret)
3312                 ocfs2_free_path(right_path);
3313         return ret;
3314 }
3315
3316 /*
3317  * Remove split_rec clusters from the record at index and merge them
3318  * onto the beginning of the record "next" to it.
3319  * For index < l_count - 1, the next means the extent rec at index + 1.
3320  * For index == l_count - 1, the "next" means the 1st extent rec of the
3321  * next extent block.
3322  */
3323 static int ocfs2_merge_rec_right(struct inode *inode,
3324                                  struct ocfs2_path *left_path,
3325                                  handle_t *handle,
3326                                  struct ocfs2_extent_rec *split_rec,
3327                                  int index)
3328 {
3329         int ret, next_free, i;
3330         unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3331         struct ocfs2_extent_rec *left_rec;
3332         struct ocfs2_extent_rec *right_rec;
3333         struct ocfs2_extent_list *right_el;
3334         struct ocfs2_path *right_path = NULL;
3335         int subtree_index = 0;
3336         struct ocfs2_extent_list *el = path_leaf_el(left_path);
3337         struct buffer_head *bh = path_leaf_bh(left_path);
3338         struct buffer_head *root_bh = NULL;
3339
3340         BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3341         left_rec = &el->l_recs[index];
3342
3343         if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3344             le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3345                 /* we meet with a cross extent block merge. */
3346                 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3347                 if (ret) {
3348                         mlog_errno(ret);
3349                         goto out;
3350                 }
3351
3352                 right_el = path_leaf_el(right_path);
3353                 next_free = le16_to_cpu(right_el->l_next_free_rec);
3354                 BUG_ON(next_free <= 0);
3355                 right_rec = &right_el->l_recs[0];
3356                 if (ocfs2_is_empty_extent(right_rec)) {
3357                         BUG_ON(next_free <= 1);
3358                         right_rec = &right_el->l_recs[1];
3359                 }
3360
3361                 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3362                        le16_to_cpu(left_rec->e_leaf_clusters) !=
3363                        le32_to_cpu(right_rec->e_cpos));
3364
3365                 subtree_index = ocfs2_find_subtree_root(inode,
3366                                                         left_path, right_path);
3367
3368                 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3369                                                       handle->h_buffer_credits,
3370                                                       right_path);
3371                 if (ret) {
3372                         mlog_errno(ret);
3373                         goto out;
3374                 }
3375
3376                 root_bh = left_path->p_node[subtree_index].bh;
3377                 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3378
3379                 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3380                                                    subtree_index);
3381                 if (ret) {
3382                         mlog_errno(ret);
3383                         goto out;
3384                 }
3385
3386                 for (i = subtree_index + 1;
3387                      i < path_num_items(right_path); i++) {
3388                         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3389                                                            right_path, i);
3390                         if (ret) {
3391                                 mlog_errno(ret);
3392                                 goto out;
3393                         }
3394
3395                         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3396                                                            left_path, i);
3397                         if (ret) {
3398                                 mlog_errno(ret);
3399                                 goto out;
3400                         }
3401                 }
3402
3403         } else {
3404                 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3405                 right_rec = &el->l_recs[index + 1];
3406         }
3407
3408         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), left_path,
3409                                            path_num_items(left_path) - 1);
3410         if (ret) {
3411                 mlog_errno(ret);
3412                 goto out;
3413         }
3414
3415         le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3416
3417         le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3418         le64_add_cpu(&right_rec->e_blkno,
3419                      -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3420         le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3421
3422         ocfs2_cleanup_merge(el, index);
3423
3424         ret = ocfs2_journal_dirty(handle, bh);
3425         if (ret)
3426                 mlog_errno(ret);
3427
3428         if (right_path) {
3429                 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3430                 if (ret)
3431                         mlog_errno(ret);
3432
3433                 ocfs2_complete_edge_insert(inode, handle, left_path,
3434                                            right_path, subtree_index);
3435         }
3436 out:
3437         if (right_path)
3438                 ocfs2_free_path(right_path);
3439         return ret;
3440 }
3441
3442 static int ocfs2_get_left_path(struct inode *inode,
3443                                struct ocfs2_path *right_path,
3444                                struct ocfs2_path **ret_left_path)
3445 {
3446         int ret;
3447         u32 left_cpos;
3448         struct ocfs2_path *left_path = NULL;
3449
3450         *ret_left_path = NULL;
3451
3452         /* This function shouldn't be called for non-trees. */
3453         BUG_ON(right_path->p_tree_depth == 0);
3454
3455         ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3456                                             right_path, &left_cpos);
3457         if (ret) {
3458                 mlog_errno(ret);
3459                 goto out;
3460         }
3461
3462         /* This function shouldn't be called for the leftmost leaf. */
3463         BUG_ON(left_cpos == 0);
3464
3465         left_path = ocfs2_new_path_from_path(right_path);
3466         if (!left_path) {
3467                 ret = -ENOMEM;
3468                 mlog_errno(ret);
3469                 goto out;
3470         }
3471
3472         ret = ocfs2_find_path(INODE_CACHE(inode), left_path, left_cpos);
3473         if (ret) {
3474                 mlog_errno(ret);
3475                 goto out;
3476         }
3477
3478         *ret_left_path = left_path;
3479 out:
3480         if (ret)
3481                 ocfs2_free_path(left_path);
3482         return ret;
3483 }
3484
3485 /*
3486  * Remove split_rec clusters from the record at index and merge them
3487  * onto the tail of the record "before" it.
3488  * For index > 0, the "before" means the extent rec at index - 1.
3489  *
3490  * For index == 0, the "before" means the last record of the previous
3491  * extent block. And there is also a situation that we may need to
3492  * remove the rightmost leaf extent block in the right_path and change
3493  * the right path to indicate the new rightmost path.
3494  */
3495 static int ocfs2_merge_rec_left(struct inode *inode,
3496                                 struct ocfs2_path *right_path,
3497                                 handle_t *handle,
3498                                 struct ocfs2_extent_rec *split_rec,
3499                                 struct ocfs2_cached_dealloc_ctxt *dealloc,
3500                                 struct ocfs2_extent_tree *et,
3501                                 int index)
3502 {
3503         int ret, i, subtree_index = 0, has_empty_extent = 0;
3504         unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3505         struct ocfs2_extent_rec *left_rec;
3506         struct ocfs2_extent_rec *right_rec;
3507         struct ocfs2_extent_list *el = path_leaf_el(right_path);
3508         struct buffer_head *bh = path_leaf_bh(right_path);
3509         struct buffer_head *root_bh = NULL;
3510         struct ocfs2_path *left_path = NULL;
3511         struct ocfs2_extent_list *left_el;
3512
3513         BUG_ON(index < 0);
3514
3515         right_rec = &el->l_recs[index];
3516         if (index == 0) {
3517                 /* we meet with a cross extent block merge. */
3518                 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3519                 if (ret) {
3520                         mlog_errno(ret);
3521                         goto out;
3522                 }
3523
3524                 left_el = path_leaf_el(left_path);
3525                 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3526                        le16_to_cpu(left_el->l_count));
3527
3528                 left_rec = &left_el->l_recs[
3529                                 le16_to_cpu(left_el->l_next_free_rec) - 1];
3530                 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3531                        le16_to_cpu(left_rec->e_leaf_clusters) !=
3532                        le32_to_cpu(split_rec->e_cpos));
3533
3534                 subtree_index = ocfs2_find_subtree_root(inode,
3535                                                         left_path, right_path);
3536
3537                 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3538                                                       handle->h_buffer_credits,
3539                                                       left_path);
3540                 if (ret) {
3541                         mlog_errno(ret);
3542                         goto out;
3543                 }
3544
3545                 root_bh = left_path->p_node[subtree_index].bh;
3546                 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3547
3548                 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3549                                                    subtree_index);
3550                 if (ret) {
3551                         mlog_errno(ret);
3552                         goto out;
3553                 }
3554
3555                 for (i = subtree_index + 1;
3556                      i < path_num_items(right_path); i++) {
3557                         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3558                                                            right_path, i);
3559                         if (ret) {
3560                                 mlog_errno(ret);
3561                                 goto out;
3562                         }
3563
3564                         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
3565                                                            left_path, i);
3566                         if (ret) {
3567                                 mlog_errno(ret);
3568                                 goto out;
3569                         }
3570                 }
3571         } else {
3572                 left_rec = &el->l_recs[index - 1];
3573                 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3574                         has_empty_extent = 1;
3575         }
3576
3577         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
3578                                            path_num_items(right_path) - 1);
3579         if (ret) {
3580                 mlog_errno(ret);
3581                 goto out;
3582         }
3583
3584         if (has_empty_extent && index == 1) {
3585                 /*
3586                  * The easy case - we can just plop the record right in.
3587                  */
3588                 *left_rec = *split_rec;
3589
3590                 has_empty_extent = 0;
3591         } else
3592                 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3593
3594         le32_add_cpu(&right_rec->e_cpos, split_clusters);
3595         le64_add_cpu(&right_rec->e_blkno,
3596                      ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3597         le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3598
3599         ocfs2_cleanup_merge(el, index);
3600
3601         ret = ocfs2_journal_dirty(handle, bh);
3602         if (ret)
3603                 mlog_errno(ret);
3604
3605         if (left_path) {
3606                 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3607                 if (ret)
3608                         mlog_errno(ret);
3609
3610                 /*
3611                  * In the situation that the right_rec is empty and the extent
3612                  * block is empty also,  ocfs2_complete_edge_insert can't handle
3613                  * it and we need to delete the right extent block.
3614                  */
3615                 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3616                     le16_to_cpu(el->l_next_free_rec) == 1) {
3617
3618                         ret = ocfs2_remove_rightmost_path(inode, handle,
3619                                                           right_path,
3620                                                           dealloc, et);
3621                         if (ret) {
3622                                 mlog_errno(ret);
3623                                 goto out;
3624                         }
3625
3626                         /* Now the rightmost extent block has been deleted.
3627                          * So we use the new rightmost path.
3628                          */
3629                         ocfs2_mv_path(right_path, left_path);
3630                         left_path = NULL;
3631                 } else
3632                         ocfs2_complete_edge_insert(inode, handle, left_path,
3633                                                    right_path, subtree_index);
3634         }
3635 out:
3636         if (left_path)
3637                 ocfs2_free_path(left_path);
3638         return ret;
3639 }
3640
3641 static int ocfs2_try_to_merge_extent(struct inode *inode,
3642                                      handle_t *handle,
3643                                      struct ocfs2_path *path,
3644                                      int split_index,
3645                                      struct ocfs2_extent_rec *split_rec,
3646                                      struct ocfs2_cached_dealloc_ctxt *dealloc,
3647                                      struct ocfs2_merge_ctxt *ctxt,
3648                                      struct ocfs2_extent_tree *et)
3649
3650 {
3651         int ret = 0;
3652         struct ocfs2_extent_list *el = path_leaf_el(path);
3653         struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3654
3655         BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3656
3657         if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3658                 /*
3659                  * The merge code will need to create an empty
3660                  * extent to take the place of the newly
3661                  * emptied slot. Remove any pre-existing empty
3662                  * extents - having more than one in a leaf is
3663                  * illegal.
3664                  */
3665                 ret = ocfs2_rotate_tree_left(inode, handle, path,
3666                                              dealloc, et);
3667                 if (ret) {
3668                         mlog_errno(ret);
3669                         goto out;
3670                 }
3671                 split_index--;
3672                 rec = &el->l_recs[split_index];
3673         }
3674
3675         if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3676                 /*
3677                  * Left-right contig implies this.
3678                  */
3679                 BUG_ON(!ctxt->c_split_covers_rec);
3680
3681                 /*
3682                  * Since the leftright insert always covers the entire
3683                  * extent, this call will delete the insert record
3684                  * entirely, resulting in an empty extent record added to
3685                  * the extent block.
3686                  *
3687                  * Since the adding of an empty extent shifts
3688                  * everything back to the right, there's no need to
3689                  * update split_index here.
3690                  *
3691                  * When the split_index is zero, we need to merge it to the
3692                  * prevoius extent block. It is more efficient and easier
3693                  * if we do merge_right first and merge_left later.
3694                  */
3695                 ret = ocfs2_merge_rec_right(inode, path,
3696                                             handle, split_rec,
3697                                             split_index);
3698                 if (ret) {
3699                         mlog_errno(ret);
3700                         goto out;
3701                 }
3702
3703                 /*
3704                  * We can only get this from logic error above.
3705                  */
3706                 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3707
3708                 /* The merge left us with an empty extent, remove it. */
3709                 ret = ocfs2_rotate_tree_left(inode, handle, path,
3710                                              dealloc, et);
3711                 if (ret) {
3712                         mlog_errno(ret);
3713                         goto out;
3714                 }
3715
3716                 rec = &el->l_recs[split_index];
3717
3718                 /*
3719                  * Note that we don't pass split_rec here on purpose -
3720                  * we've merged it into the rec already.
3721                  */
3722                 ret = ocfs2_merge_rec_left(inode, path,
3723                                            handle, rec,
3724                                            dealloc, et,
3725                                            split_index);
3726
3727                 if (ret) {
3728                         mlog_errno(ret);
3729                         goto out;
3730                 }
3731
3732                 ret = ocfs2_rotate_tree_left(inode, handle, path,
3733                                              dealloc, et);
3734                 /*
3735                  * Error from this last rotate is not critical, so
3736                  * print but don't bubble it up.
3737                  */
3738                 if (ret)
3739                         mlog_errno(ret);
3740                 ret = 0;
3741         } else {
3742                 /*
3743                  * Merge a record to the left or right.
3744                  *
3745                  * 'contig_type' is relative to the existing record,
3746                  * so for example, if we're "right contig", it's to
3747                  * the record on the left (hence the left merge).
3748                  */
3749                 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3750                         ret = ocfs2_merge_rec_left(inode,
3751                                                    path,
3752                                                    handle, split_rec,
3753                                                    dealloc, et,
3754                                                    split_index);
3755                         if (ret) {
3756                                 mlog_errno(ret);
3757                                 goto out;
3758                         }
3759                 } else {
3760                         ret = ocfs2_merge_rec_right(inode,
3761                                                     path,
3762                                                     handle, split_rec,
3763                                                     split_index);
3764                         if (ret) {
3765                                 mlog_errno(ret);
3766                                 goto out;
3767                         }
3768                 }
3769
3770                 if (ctxt->c_split_covers_rec) {
3771                         /*
3772                          * The merge may have left an empty extent in
3773                          * our leaf. Try to rotate it away.
3774                          */
3775                         ret = ocfs2_rotate_tree_left(inode, handle, path,
3776                                                      dealloc, et);
3777                         if (ret)
3778                                 mlog_errno(ret);
3779                         ret = 0;
3780                 }
3781         }
3782
3783 out:
3784         return ret;
3785 }
3786
3787 static void ocfs2_subtract_from_rec(struct super_block *sb,
3788                                     enum ocfs2_split_type split,
3789                                     struct ocfs2_extent_rec *rec,
3790                                     struct ocfs2_extent_rec *split_rec)
3791 {
3792         u64 len_blocks;
3793
3794         len_blocks = ocfs2_clusters_to_blocks(sb,
3795                                 le16_to_cpu(split_rec->e_leaf_clusters));
3796
3797         if (split == SPLIT_LEFT) {
3798                 /*
3799                  * Region is on the left edge of the existing
3800                  * record.
3801                  */
3802                 le32_add_cpu(&rec->e_cpos,
3803                              le16_to_cpu(split_rec->e_leaf_clusters));
3804                 le64_add_cpu(&rec->e_blkno, len_blocks);
3805                 le16_add_cpu(&rec->e_leaf_clusters,
3806                              -le16_to_cpu(split_rec->e_leaf_clusters));
3807         } else {
3808                 /*
3809                  * Region is on the right edge of the existing
3810                  * record.
3811                  */
3812                 le16_add_cpu(&rec->e_leaf_clusters,
3813                              -le16_to_cpu(split_rec->e_leaf_clusters));
3814         }
3815 }
3816
3817 /*
3818  * Do the final bits of extent record insertion at the target leaf
3819  * list. If this leaf is part of an allocation tree, it is assumed
3820  * that the tree above has been prepared.
3821  */
3822 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3823                                  struct ocfs2_extent_list *el,
3824                                  struct ocfs2_insert_type *insert,
3825                                  struct inode *inode)
3826 {
3827         int i = insert->ins_contig_index;
3828         unsigned int range;
3829         struct ocfs2_extent_rec *rec;
3830
3831         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3832
3833         if (insert->ins_split != SPLIT_NONE) {
3834                 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3835                 BUG_ON(i == -1);
3836                 rec = &el->l_recs[i];
3837                 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3838                                         insert_rec);
3839                 goto rotate;
3840         }
3841
3842         /*
3843          * Contiguous insert - either left or right.
3844          */
3845         if (insert->ins_contig != CONTIG_NONE) {
3846                 rec = &el->l_recs[i];
3847                 if (insert->ins_contig == CONTIG_LEFT) {
3848                         rec->e_blkno = insert_rec->e_blkno;
3849                         rec->e_cpos = insert_rec->e_cpos;
3850                 }
3851                 le16_add_cpu(&rec->e_leaf_clusters,
3852                              le16_to_cpu(insert_rec->e_leaf_clusters));
3853                 return;
3854         }
3855
3856         /*
3857          * Handle insert into an empty leaf.
3858          */
3859         if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3860             ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3861              ocfs2_is_empty_extent(&el->l_recs[0]))) {
3862                 el->l_recs[0] = *insert_rec;
3863                 el->l_next_free_rec = cpu_to_le16(1);
3864                 return;
3865         }
3866
3867         /*
3868          * Appending insert.
3869          */
3870         if (insert->ins_appending == APPEND_TAIL) {
3871                 i = le16_to_cpu(el->l_next_free_rec) - 1;
3872                 rec = &el->l_recs[i];
3873                 range = le32_to_cpu(rec->e_cpos)
3874                         + le16_to_cpu(rec->e_leaf_clusters);
3875                 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3876
3877                 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3878                                 le16_to_cpu(el->l_count),
3879                                 "inode %lu, depth %u, count %u, next free %u, "
3880                                 "rec.cpos %u, rec.clusters %u, "
3881                                 "insert.cpos %u, insert.clusters %u\n",
3882                                 inode->i_ino,
3883                                 le16_to_cpu(el->l_tree_depth),
3884                                 le16_to_cpu(el->l_count),
3885                                 le16_to_cpu(el->l_next_free_rec),
3886                                 le32_to_cpu(el->l_recs[i].e_cpos),
3887                                 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3888                                 le32_to_cpu(insert_rec->e_cpos),
3889                                 le16_to_cpu(insert_rec->e_leaf_clusters));
3890                 i++;
3891                 el->l_recs[i] = *insert_rec;
3892                 le16_add_cpu(&el->l_next_free_rec, 1);
3893                 return;
3894         }
3895
3896 rotate:
3897         /*
3898          * Ok, we have to rotate.
3899          *
3900          * At this point, it is safe to assume that inserting into an
3901          * empty leaf and appending to a leaf have both been handled
3902          * above.
3903          *
3904          * This leaf needs to have space, either by the empty 1st
3905          * extent record, or by virtue of an l_next_rec < l_count.
3906          */
3907         ocfs2_rotate_leaf(el, insert_rec);
3908 }
3909
3910 static void ocfs2_adjust_rightmost_records(struct inode *inode,
3911                                            handle_t *handle,
3912                                            struct ocfs2_path *path,
3913                                            struct ocfs2_extent_rec *insert_rec)
3914 {
3915         int ret, i, next_free;
3916         struct buffer_head *bh;
3917         struct ocfs2_extent_list *el;
3918         struct ocfs2_extent_rec *rec;
3919
3920         /*
3921          * Update everything except the leaf block.
3922          */
3923         for (i = 0; i < path->p_tree_depth; i++) {
3924                 bh = path->p_node[i].bh;
3925                 el = path->p_node[i].el;
3926
3927                 next_free = le16_to_cpu(el->l_next_free_rec);
3928                 if (next_free == 0) {
3929                         ocfs2_error(inode->i_sb,
3930                                     "Dinode %llu has a bad extent list",
3931                                     (unsigned long long)OCFS2_I(inode)->ip_blkno);
3932                         ret = -EIO;
3933                         return;
3934                 }
3935
3936                 rec = &el->l_recs[next_free - 1];
3937
3938                 rec->e_int_clusters = insert_rec->e_cpos;
3939                 le32_add_cpu(&rec->e_int_clusters,
3940                              le16_to_cpu(insert_rec->e_leaf_clusters));
3941                 le32_add_cpu(&rec->e_int_clusters,
3942                              -le32_to_cpu(rec->e_cpos));
3943
3944                 ret = ocfs2_journal_dirty(handle, bh);
3945                 if (ret)
3946                         mlog_errno(ret);
3947
3948         }
3949 }
3950
3951 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3952                                     struct ocfs2_extent_rec *insert_rec,
3953                                     struct ocfs2_path *right_path,
3954                                     struct ocfs2_path **ret_left_path)
3955 {
3956         int ret, next_free;
3957         struct ocfs2_extent_list *el;
3958         struct ocfs2_path *left_path = NULL;
3959
3960         *ret_left_path = NULL;
3961
3962         /*
3963          * This shouldn't happen for non-trees. The extent rec cluster
3964          * count manipulation below only works for interior nodes.
3965          */
3966         BUG_ON(right_path->p_tree_depth == 0);
3967
3968         /*
3969          * If our appending insert is at the leftmost edge of a leaf,
3970          * then we might need to update the rightmost records of the
3971          * neighboring path.
3972          */
3973         el = path_leaf_el(right_path);
3974         next_free = le16_to_cpu(el->l_next_free_rec);
3975         if (next_free == 0 ||
3976             (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3977                 u32 left_cpos;
3978
3979                 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3980                                                     &left_cpos);
3981                 if (ret) {
3982                         mlog_errno(ret);
3983                         goto out;
3984                 }
3985
3986                 mlog(0, "Append may need a left path update. cpos: %u, "
3987                      "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3988                      left_cpos);
3989
3990                 /*
3991                  * No need to worry if the append is already in the
3992                  * leftmost leaf.
3993                  */
3994                 if (left_cpos) {
3995                         left_path = ocfs2_new_path_from_path(right_path);
3996                         if (!left_path) {
3997                                 ret = -ENOMEM;
3998                                 mlog_errno(ret);
3999                                 goto out;
4000                         }
4001
4002                         ret = ocfs2_find_path(INODE_CACHE(inode), left_path,
4003                                               left_cpos);
4004                         if (ret) {
4005                                 mlog_errno(ret);
4006                                 goto out;
4007                         }
4008
4009                         /*
4010                          * ocfs2_insert_path() will pass the left_path to the
4011                          * journal for us.
4012                          */
4013                 }
4014         }
4015
4016         ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
4017         if (ret) {
4018                 mlog_errno(ret);
4019                 goto out;
4020         }
4021
4022         ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
4023
4024         *ret_left_path = left_path;
4025         ret = 0;
4026 out:
4027         if (ret != 0)
4028                 ocfs2_free_path(left_path);
4029
4030         return ret;
4031 }
4032
4033 static void ocfs2_split_record(struct inode *inode,
4034                                struct ocfs2_path *left_path,
4035                                struct ocfs2_path *right_path,
4036                                struct ocfs2_extent_rec *split_rec,
4037                                enum ocfs2_split_type split)
4038 {
4039         int index;
4040         u32 cpos = le32_to_cpu(split_rec->e_cpos);
4041         struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4042         struct ocfs2_extent_rec *rec, *tmprec;
4043
4044         right_el = path_leaf_el(right_path);
4045         if (left_path)
4046                 left_el = path_leaf_el(left_path);
4047
4048         el = right_el;
4049         insert_el = right_el;
4050         index = ocfs2_search_extent_list(el, cpos);
4051         if (index != -1) {
4052                 if (index == 0 && left_path) {
4053                         BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4054
4055                         /*
4056                          * This typically means that the record
4057                          * started in the left path but moved to the
4058                          * right as a result of rotation. We either
4059                          * move the existing record to the left, or we
4060                          * do the later insert there.
4061                          *
4062                          * In this case, the left path should always
4063                          * exist as the rotate code will have passed
4064                          * it back for a post-insert update.
4065                          */
4066
4067                         if (split == SPLIT_LEFT) {
4068                                 /*
4069                                  * It's a left split. Since we know
4070                                  * that the rotate code gave us an
4071                                  * empty extent in the left path, we
4072                                  * can just do the insert there.
4073                                  */
4074                                 insert_el = left_el;
4075                         } else {
4076                                 /*
4077                                  * Right split - we have to move the
4078                                  * existing record over to the left
4079                                  * leaf. The insert will be into the
4080                                  * newly created empty extent in the
4081                                  * right leaf.
4082                                  */
4083                                 tmprec = &right_el->l_recs[index];
4084                                 ocfs2_rotate_leaf(left_el, tmprec);
4085                                 el = left_el;
4086
4087                                 memset(tmprec, 0, sizeof(*tmprec));
4088                                 index = ocfs2_search_extent_list(left_el, cpos);
4089                                 BUG_ON(index == -1);
4090                         }
4091                 }
4092         } else {
4093                 BUG_ON(!left_path);
4094                 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4095                 /*
4096                  * Left path is easy - we can just allow the insert to
4097                  * happen.
4098                  */
4099                 el = left_el;
4100                 insert_el = left_el;
4101                 index = ocfs2_search_extent_list(el, cpos);
4102                 BUG_ON(index == -1);
4103         }
4104
4105         rec = &el->l_recs[index];
4106         ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4107         ocfs2_rotate_leaf(insert_el, split_rec);
4108 }
4109
4110 /*
4111  * This function only does inserts on an allocation b-tree. For tree
4112  * depth = 0, ocfs2_insert_at_leaf() is called directly.
4113  *
4114  * right_path is the path we want to do the actual insert
4115  * in. left_path should only be passed in if we need to update that
4116  * portion of the tree after an edge insert.
4117  */
4118 static int ocfs2_insert_path(struct inode *inode,
4119                              handle_t *handle,
4120                              struct ocfs2_path *left_path,
4121                              struct ocfs2_path *right_path,
4122                              struct ocfs2_extent_rec *insert_rec,
4123                              struct ocfs2_insert_type *insert)
4124 {
4125         int ret, subtree_index;
4126         struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4127
4128         if (left_path) {
4129                 int credits = handle->h_buffer_credits;
4130
4131                 /*
4132                  * There's a chance that left_path got passed back to
4133                  * us without being accounted for in the
4134                  * journal. Extend our transaction here to be sure we
4135                  * can change those blocks.
4136                  */
4137                 credits += left_path->p_tree_depth;
4138
4139                 ret = ocfs2_extend_trans(handle, credits);
4140                 if (ret < 0) {
4141                         mlog_errno(ret);
4142                         goto out;
4143                 }
4144
4145                 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, left_path);
4146                 if (ret < 0) {
4147                         mlog_errno(ret);
4148                         goto out;
4149                 }
4150         }
4151
4152         /*
4153          * Pass both paths to the journal. The majority of inserts
4154          * will be touching all components anyway.
4155          */
4156         ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
4157         if (ret < 0) {
4158                 mlog_errno(ret);
4159                 goto out;
4160         }
4161
4162         if (insert->ins_split != SPLIT_NONE) {
4163                 /*
4164                  * We could call ocfs2_insert_at_leaf() for some types
4165                  * of splits, but it's easier to just let one separate
4166                  * function sort it all out.
4167                  */
4168                 ocfs2_split_record(inode, left_path, right_path,
4169                                    insert_rec, insert->ins_split);
4170
4171                 /*
4172                  * Split might have modified either leaf and we don't
4173                  * have a guarantee that the later edge insert will
4174                  * dirty this for us.
4175                  */
4176                 if (left_path)
4177                         ret = ocfs2_journal_dirty(handle,
4178                                                   path_leaf_bh(left_path));
4179                         if (ret)
4180                                 mlog_errno(ret);
4181         } else
4182                 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4183                                      insert, inode);
4184
4185         ret = ocfs2_journal_dirty(handle, leaf_bh);
4186         if (ret)
4187                 mlog_errno(ret);
4188
4189         if (left_path) {
4190                 /*
4191                  * The rotate code has indicated that we need to fix
4192                  * up portions of the tree after the insert.
4193                  *
4194                  * XXX: Should we extend the transaction here?
4195                  */
4196                 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4197                                                         right_path);
4198                 ocfs2_complete_edge_insert(inode, handle, left_path,
4199                                            right_path, subtree_index);
4200         }
4201
4202         ret = 0;
4203 out:
4204         return ret;
4205 }
4206
4207 static int ocfs2_do_insert_extent(struct inode *inode,
4208                                   handle_t *handle,
4209                                   struct ocfs2_extent_tree *et,
4210                                   struct ocfs2_extent_rec *insert_rec,
4211                                   struct ocfs2_insert_type *type)
4212 {
4213         int ret, rotate = 0;
4214         u32 cpos;
4215         struct ocfs2_path *right_path = NULL;
4216         struct ocfs2_path *left_path = NULL;
4217         struct ocfs2_extent_list *el;
4218
4219         el = et->et_root_el;
4220
4221         ret = ocfs2_et_root_journal_access(handle, et,
4222                                            OCFS2_JOURNAL_ACCESS_WRITE);
4223         if (ret) {
4224                 mlog_errno(ret);
4225                 goto out;
4226         }
4227
4228         if (le16_to_cpu(el->l_tree_depth) == 0) {
4229                 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4230                 goto out_update_clusters;
4231         }
4232
4233         right_path = ocfs2_new_path_from_et(et);
4234         if (!right_path) {
4235                 ret = -ENOMEM;
4236                 mlog_errno(ret);
4237                 goto out;
4238         }
4239
4240         /*
4241          * Determine the path to start with. Rotations need the
4242          * rightmost path, everything else can go directly to the
4243          * target leaf.
4244          */
4245         cpos = le32_to_cpu(insert_rec->e_cpos);
4246         if (type->ins_appending == APPEND_NONE &&
4247             type->ins_contig == CONTIG_NONE) {
4248                 rotate = 1;
4249                 cpos = UINT_MAX;
4250         }
4251
4252         ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4253         if (ret) {
4254                 mlog_errno(ret);
4255                 goto out;
4256         }
4257
4258         /*
4259          * Rotations and appends need special treatment - they modify
4260          * parts of the tree's above them.
4261          *
4262          * Both might pass back a path immediate to the left of the
4263          * one being inserted to. This will be cause
4264          * ocfs2_insert_path() to modify the rightmost records of
4265          * left_path to account for an edge insert.
4266          *
4267          * XXX: When modifying this code, keep in mind that an insert
4268          * can wind up skipping both of these two special cases...
4269          */
4270         if (rotate) {
4271                 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
4272                                               le32_to_cpu(insert_rec->e_cpos),
4273                                               right_path, &left_path);
4274                 if (ret) {
4275                         mlog_errno(ret);
4276                         goto out;
4277                 }
4278
4279                 /*
4280                  * ocfs2_rotate_tree_right() might have extended the
4281                  * transaction without re-journaling our tree root.
4282                  */
4283                 ret = ocfs2_et_root_journal_access(handle, et,
4284                                                    OCFS2_JOURNAL_ACCESS_WRITE);
4285                 if (ret) {
4286                         mlog_errno(ret);
4287                         goto out;
4288                 }
4289         } else if (type->ins_appending == APPEND_TAIL
4290                    && type->ins_contig != CONTIG_LEFT) {
4291                 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4292                                                right_path, &left_path);
4293                 if (ret) {
4294                         mlog_errno(ret);
4295                         goto out;
4296                 }
4297         }
4298
4299         ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4300                                 insert_rec, type);
4301         if (ret) {
4302                 mlog_errno(ret);
4303                 goto out;
4304         }
4305
4306 out_update_clusters:
4307         if (type->ins_split == SPLIT_NONE)
4308                 ocfs2_et_update_clusters(inode, et,
4309                                          le16_to_cpu(insert_rec->e_leaf_clusters));
4310
4311         ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4312         if (ret)
4313                 mlog_errno(ret);
4314
4315 out:
4316         ocfs2_free_path(left_path);
4317         ocfs2_free_path(right_path);
4318
4319         return ret;
4320 }
4321
4322 static enum ocfs2_contig_type
4323 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
4324                                struct ocfs2_extent_list *el, int index,
4325                                struct ocfs2_extent_rec *split_rec)
4326 {
4327         int status;
4328         enum ocfs2_contig_type ret = CONTIG_NONE;
4329         u32 left_cpos, right_cpos;
4330         struct ocfs2_extent_rec *rec = NULL;
4331         struct ocfs2_extent_list *new_el;
4332         struct ocfs2_path *left_path = NULL, *right_path = NULL;
4333         struct buffer_head *bh;
4334         struct ocfs2_extent_block *eb;
4335
4336         if (index > 0) {
4337                 rec = &el->l_recs[index - 1];
4338         } else if (path->p_tree_depth > 0) {
4339                 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4340                                                        path, &left_cpos);
4341                 if (status)
4342                         goto out;
4343
4344                 if (left_cpos != 0) {
4345                         left_path = ocfs2_new_path_from_path(path);
4346                         if (!left_path)
4347                                 goto out;
4348
4349                         status = ocfs2_find_path(INODE_CACHE(inode),
4350                                                  left_path, left_cpos);
4351                         if (status)
4352                                 goto out;
4353
4354                         new_el = path_leaf_el(left_path);
4355
4356                         if (le16_to_cpu(new_el->l_next_free_rec) !=
4357                             le16_to_cpu(new_el->l_count)) {
4358                                 bh = path_leaf_bh(left_path);
4359                                 eb = (struct ocfs2_extent_block *)bh->b_data;
4360                                 ocfs2_error(inode->i_sb,
4361                                             "Extent block #%llu has an "
4362                                             "invalid l_next_free_rec of "
4363                                             "%d.  It should have "
4364                                             "matched the l_count of %d",
4365                                             (unsigned long long)le64_to_cpu(eb->h_blkno),
4366                                             le16_to_cpu(new_el->l_next_free_rec),
4367                                             le16_to_cpu(new_el->l_count));
4368                                 status = -EINVAL;
4369                                 goto out;
4370                         }
4371                         rec = &new_el->l_recs[
4372                                 le16_to_cpu(new_el->l_next_free_rec) - 1];
4373                 }
4374         }
4375
4376         /*
4377          * We're careful to check for an empty extent record here -
4378          * the merge code will know what to do if it sees one.
4379          */
4380         if (rec) {
4381                 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4382                         if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4383                                 ret = CONTIG_RIGHT;
4384                 } else {
4385                         ret = ocfs2_extent_contig(inode, rec, split_rec);
4386                 }
4387         }
4388
4389         rec = NULL;
4390         if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4391                 rec = &el->l_recs[index + 1];
4392         else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4393                  path->p_tree_depth > 0) {
4394                 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4395                                                         path, &right_cpos);
4396                 if (status)
4397                         goto out;
4398
4399                 if (right_cpos == 0)
4400                         goto out;
4401
4402                 right_path = ocfs2_new_path_from_path(path);
4403                 if (!right_path)
4404                         goto out;
4405
4406                 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
4407                 if (status)
4408                         goto out;
4409
4410                 new_el = path_leaf_el(right_path);
4411                 rec = &new_el->l_recs[0];
4412                 if (ocfs2_is_empty_extent(rec)) {
4413                         if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4414                                 bh = path_leaf_bh(right_path);
4415                                 eb = (struct ocfs2_extent_block *)bh->b_data;
4416                                 ocfs2_error(inode->i_sb,
4417                                             "Extent block #%llu has an "
4418                                             "invalid l_next_free_rec of %d",
4419                                             (unsigned long long)le64_to_cpu(eb->h_blkno),
4420                                             le16_to_cpu(new_el->l_next_free_rec));
4421                                 status = -EINVAL;
4422                                 goto out;
4423                         }
4424                         rec = &new_el->l_recs[1];
4425                 }
4426         }
4427
4428         if (rec) {
4429                 enum ocfs2_contig_type contig_type;
4430
4431                 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4432
4433                 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4434                         ret = CONTIG_LEFTRIGHT;
4435                 else if (ret == CONTIG_NONE)
4436                         ret = contig_type;
4437         }
4438
4439 out:
4440         if (left_path)
4441                 ocfs2_free_path(left_path);
4442         if (right_path)
4443                 ocfs2_free_path(right_path);
4444
4445         return ret;
4446 }
4447
4448 static void ocfs2_figure_contig_type(struct inode *inode,
4449                                      struct ocfs2_insert_type *insert,
4450                                      struct ocfs2_extent_list *el,
4451                                      struct ocfs2_extent_rec *insert_rec,
4452                                      struct ocfs2_extent_tree *et)
4453 {
4454         int i;
4455         enum ocfs2_contig_type contig_type = CONTIG_NONE;
4456
4457         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4458
4459         for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4460                 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4461                                                   insert_rec);
4462                 if (contig_type != CONTIG_NONE) {
4463                         insert->ins_contig_index = i;
4464                         break;
4465                 }
4466         }
4467         insert->ins_contig = contig_type;
4468
4469         if (insert->ins_contig != CONTIG_NONE) {
4470                 struct ocfs2_extent_rec *rec =
4471                                 &el->l_recs[insert->ins_contig_index];
4472                 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4473                                    le16_to_cpu(insert_rec->e_leaf_clusters);
4474
4475                 /*
4476                  * Caller might want us to limit the size of extents, don't
4477                  * calculate contiguousness if we might exceed that limit.
4478                  */
4479                 if (et->et_max_leaf_clusters &&
4480                     (len > et->et_max_leaf_clusters))
4481                         insert->ins_contig = CONTIG_NONE;
4482         }
4483 }
4484
4485 /*
4486  * This should only be called against the righmost leaf extent list.
4487  *
4488  * ocfs2_figure_appending_type() will figure out whether we'll have to
4489  * insert at the tail of the rightmost leaf.
4490  *
4491  * This should also work against the root extent list for tree's with 0
4492  * depth. If we consider the root extent list to be the rightmost leaf node
4493  * then the logic here makes sense.
4494  */
4495 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4496                                         struct ocfs2_extent_list *el,
4497                                         struct ocfs2_extent_rec *insert_rec)
4498 {
4499         int i;
4500         u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4501         struct ocfs2_extent_rec *rec;
4502
4503         insert->ins_appending = APPEND_NONE;
4504
4505         BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4506
4507         if (!el->l_next_free_rec)
4508                 goto set_tail_append;
4509
4510         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4511                 /* Were all records empty? */
4512                 if (le16_to_cpu(el->l_next_free_rec) == 1)
4513                         goto set_tail_append;
4514         }
4515
4516         i = le16_to_cpu(el->l_next_free_rec) - 1;
4517         rec = &el->l_recs[i];
4518
4519         if (cpos >=
4520             (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4521                 goto set_tail_append;
4522
4523         return;
4524
4525 set_tail_append:
4526         insert->ins_appending = APPEND_TAIL;
4527 }
4528
4529 /*
4530  * Helper function called at the begining of an insert.
4531  *
4532  * This computes a few things that are commonly used in the process of
4533  * inserting into the btree:
4534  *   - Whether the new extent is contiguous with an existing one.
4535  *   - The current tree depth.
4536  *   - Whether the insert is an appending one.
4537  *   - The total # of free records in the tree.
4538  *
4539  * All of the information is stored on the ocfs2_insert_type
4540  * structure.
4541  */
4542 static int ocfs2_figure_insert_type(struct inode *inode,
4543                                     struct ocfs2_extent_tree *et,
4544                                     struct buffer_head **last_eb_bh,
4545                                     struct ocfs2_extent_rec *insert_rec,
4546                                     int *free_records,
4547                                     struct ocfs2_insert_type *insert)
4548 {
4549         int ret;
4550         struct ocfs2_extent_block *eb;
4551         struct ocfs2_extent_list *el;
4552         struct ocfs2_path *path = NULL;
4553         struct buffer_head *bh = NULL;
4554
4555         insert->ins_split = SPLIT_NONE;
4556
4557         el = et->et_root_el;
4558         insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4559
4560         if (el->l_tree_depth) {
4561                 /*
4562                  * If we have tree depth, we read in the
4563                  * rightmost extent block ahead of time as
4564                  * ocfs2_figure_insert_type() and ocfs2_add_branch()
4565                  * may want it later.
4566                  */
4567                 ret = ocfs2_read_extent_block(et->et_ci,
4568                                               ocfs2_et_get_last_eb_blk(et),
4569                                               &bh);
4570                 if (ret) {
4571                         mlog_exit(ret);
4572                         goto out;
4573                 }
4574                 eb = (struct ocfs2_extent_block *) bh->b_data;
4575                 el = &eb->h_list;
4576         }
4577
4578         /*
4579          * Unless we have a contiguous insert, we'll need to know if
4580          * there is room left in our allocation tree for another
4581          * extent record.
4582          *
4583          * XXX: This test is simplistic, we can search for empty
4584          * extent records too.
4585          */
4586         *free_records = le16_to_cpu(el->l_count) -
4587                 le16_to_cpu(el->l_next_free_rec);
4588
4589         if (!insert->ins_tree_depth) {
4590                 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4591                 ocfs2_figure_appending_type(insert, el, insert_rec);
4592                 return 0;
4593         }
4594
4595         path = ocfs2_new_path_from_et(et);
4596         if (!path) {
4597                 ret = -ENOMEM;
4598                 mlog_errno(ret);
4599                 goto out;
4600         }
4601
4602         /*
4603          * In the case that we're inserting past what the tree
4604          * currently accounts for, ocfs2_find_path() will return for
4605          * us the rightmost tree path. This is accounted for below in
4606          * the appending code.
4607          */
4608         ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4609         if (ret) {
4610                 mlog_errno(ret);
4611                 goto out;
4612         }
4613
4614         el = path_leaf_el(path);
4615
4616         /*
4617          * Now that we have the path, there's two things we want to determine:
4618          * 1) Contiguousness (also set contig_index if this is so)
4619          *
4620          * 2) Are we doing an append? We can trivially break this up
4621          *     into two types of appends: simple record append, or a
4622          *     rotate inside the tail leaf.
4623          */
4624         ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
4625
4626         /*
4627          * The insert code isn't quite ready to deal with all cases of
4628          * left contiguousness. Specifically, if it's an insert into
4629          * the 1st record in a leaf, it will require the adjustment of
4630          * cluster count on the last record of the path directly to it's
4631          * left. For now, just catch that case and fool the layers
4632          * above us. This works just fine for tree_depth == 0, which
4633          * is why we allow that above.
4634          */
4635         if (insert->ins_contig == CONTIG_LEFT &&
4636             insert->ins_contig_index == 0)
4637                 insert->ins_contig = CONTIG_NONE;
4638
4639         /*
4640          * Ok, so we can simply compare against last_eb to figure out
4641          * whether the path doesn't exist. This will only happen in
4642          * the case that we're doing a tail append, so maybe we can
4643          * take advantage of that information somehow.
4644          */
4645         if (ocfs2_et_get_last_eb_blk(et) ==
4646             path_leaf_bh(path)->b_blocknr) {
4647                 /*
4648                  * Ok, ocfs2_find_path() returned us the rightmost
4649                  * tree path. This might be an appending insert. There are
4650                  * two cases:
4651                  *    1) We're doing a true append at the tail:
4652                  *      -This might even be off the end of the leaf
4653                  *    2) We're "appending" by rotating in the tail
4654                  */
4655                 ocfs2_figure_appending_type(insert, el, insert_rec);
4656         }
4657
4658 out:
4659         ocfs2_free_path(path);
4660
4661         if (ret == 0)
4662                 *last_eb_bh = bh;
4663         else
4664                 brelse(bh);
4665         return ret;
4666 }
4667
4668 /*
4669  * Insert an extent into an inode btree.
4670  *
4671  * The caller needs to update fe->i_clusters
4672  */
4673 int ocfs2_insert_extent(struct ocfs2_super *osb,
4674                         handle_t *handle,
4675                         struct inode *inode,
4676                         struct ocfs2_extent_tree *et,
4677                         u32 cpos,
4678                         u64 start_blk,
4679                         u32 new_clusters,
4680                         u8 flags,
4681                         struct ocfs2_alloc_context *meta_ac)
4682 {
4683         int status;
4684         int uninitialized_var(free_records);
4685         struct buffer_head *last_eb_bh = NULL;
4686         struct ocfs2_insert_type insert = {0, };
4687         struct ocfs2_extent_rec rec;
4688
4689         mlog(0, "add %u clusters at position %u to inode %llu\n",
4690              new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4691
4692         memset(&rec, 0, sizeof(rec));
4693         rec.e_cpos = cpu_to_le32(cpos);
4694         rec.e_blkno = cpu_to_le64(start_blk);
4695         rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4696         rec.e_flags = flags;
4697         status = ocfs2_et_insert_check(inode, et, &rec);
4698         if (status) {
4699                 mlog_errno(status);
4700                 goto bail;
4701         }
4702
4703         status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
4704                                           &free_records, &insert);
4705         if (status < 0) {
4706                 mlog_errno(status);
4707                 goto bail;
4708         }
4709
4710         mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4711              "Insert.contig_index: %d, Insert.free_records: %d, "
4712              "Insert.tree_depth: %d\n",
4713              insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4714              free_records, insert.ins_tree_depth);
4715
4716         if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4717                 status = ocfs2_grow_tree(inode, handle, et,
4718                                          &insert.ins_tree_depth, &last_eb_bh,
4719                                          meta_ac);
4720                 if (status) {
4721                         mlog_errno(status);
4722                         goto bail;
4723                 }
4724         }
4725
4726         /* Finally, we can add clusters. This might rotate the tree for us. */
4727         status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
4728         if (status < 0)
4729                 mlog_errno(status);
4730         else if (et->et_ops == &ocfs2_dinode_et_ops)
4731                 ocfs2_extent_map_insert_rec(inode, &rec);
4732
4733 bail:
4734         brelse(last_eb_bh);
4735
4736         mlog_exit(status);
4737         return status;
4738 }
4739
4740 /*
4741  * Allcate and add clusters into the extent b-tree.
4742  * The new clusters(clusters_to_add) will be inserted at logical_offset.
4743  * The extent b-tree's root is specified by et, and
4744  * it is not limited to the file storage. Any extent tree can use this
4745  * function if it implements the proper ocfs2_extent_tree.
4746  */
4747 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4748                                 struct inode *inode,
4749                                 u32 *logical_offset,
4750                                 u32 clusters_to_add,
4751                                 int mark_unwritten,
4752                                 struct ocfs2_extent_tree *et,
4753                                 handle_t *handle,
4754                                 struct ocfs2_alloc_context *data_ac,
4755                                 struct ocfs2_alloc_context *meta_ac,
4756                                 enum ocfs2_alloc_restarted *reason_ret)
4757 {
4758         int status = 0;
4759         int free_extents;
4760         enum ocfs2_alloc_restarted reason = RESTART_NONE;
4761         u32 bit_off, num_bits;
4762         u64 block;
4763         u8 flags = 0;
4764
4765         BUG_ON(!clusters_to_add);
4766
4767         if (mark_unwritten)
4768                 flags = OCFS2_EXT_UNWRITTEN;
4769
4770         free_extents = ocfs2_num_free_extents(osb, et);
4771         if (free_extents < 0) {
4772                 status = free_extents;
4773                 mlog_errno(status);
4774                 goto leave;
4775         }
4776
4777         /* there are two cases which could cause us to EAGAIN in the
4778          * we-need-more-metadata case:
4779          * 1) we haven't reserved *any*
4780          * 2) we are so fragmented, we've needed to add metadata too
4781          *    many times. */
4782         if (!free_extents && !meta_ac) {
4783                 mlog(0, "we haven't reserved any metadata!\n");
4784                 status = -EAGAIN;
4785                 reason = RESTART_META;
4786                 goto leave;
4787         } else if ((!free_extents)
4788                    && (ocfs2_alloc_context_bits_left(meta_ac)
4789                        < ocfs2_extend_meta_needed(et->et_root_el))) {
4790                 mlog(0, "filesystem is really fragmented...\n");
4791                 status = -EAGAIN;
4792                 reason = RESTART_META;
4793                 goto leave;
4794         }
4795
4796         status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4797                                         clusters_to_add, &bit_off, &num_bits);
4798         if (status < 0) {
4799                 if (status != -ENOSPC)
4800                         mlog_errno(status);
4801                 goto leave;
4802         }
4803
4804         BUG_ON(num_bits > clusters_to_add);
4805
4806         /* reserve our write early -- insert_extent may update the tree root */
4807         status = ocfs2_et_root_journal_access(handle, et,
4808                                               OCFS2_JOURNAL_ACCESS_WRITE);
4809         if (status < 0) {
4810                 mlog_errno(status);
4811                 goto leave;
4812         }
4813
4814         block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4815         mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4816              num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4817         status = ocfs2_insert_extent(osb, handle, inode, et,
4818                                      *logical_offset, block,
4819                                      num_bits, flags, meta_ac);
4820         if (status < 0) {
4821                 mlog_errno(status);
4822                 goto leave;
4823         }
4824
4825         status = ocfs2_journal_dirty(handle, et->et_root_bh);
4826         if (status < 0) {
4827                 mlog_errno(status);
4828                 goto leave;
4829         }
4830
4831         clusters_to_add -= num_bits;
4832         *logical_offset += num_bits;
4833
4834         if (clusters_to_add) {
4835                 mlog(0, "need to alloc once more, wanted = %u\n",
4836                      clusters_to_add);
4837                 status = -EAGAIN;
4838                 reason = RESTART_TRANS;
4839         }
4840
4841 leave:
4842         mlog_exit(status);
4843         if (reason_ret)
4844                 *reason_ret = reason;
4845         return status;
4846 }
4847
4848 static void ocfs2_make_right_split_rec(struct super_block *sb,
4849                                        struct ocfs2_extent_rec *split_rec,
4850                                        u32 cpos,
4851                                        struct ocfs2_extent_rec *rec)
4852 {
4853         u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4854         u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4855
4856         memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4857
4858         split_rec->e_cpos = cpu_to_le32(cpos);
4859         split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4860
4861         split_rec->e_blkno = rec->e_blkno;
4862         le64_add_cpu(&split_rec->e_blkno,
4863                      ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4864
4865         split_rec->e_flags = rec->e_flags;
4866 }
4867
4868 static int ocfs2_split_and_insert(struct inode *inode,
4869                                   handle_t *handle,
4870                                   struct ocfs2_path *path,
4871                                   struct ocfs2_extent_tree *et,
4872                                   struct buffer_head **last_eb_bh,
4873                                   int split_index,
4874                                   struct ocfs2_extent_rec *orig_split_rec,
4875                                   struct ocfs2_alloc_context *meta_ac)
4876 {
4877         int ret = 0, depth;
4878         unsigned int insert_range, rec_range, do_leftright = 0;
4879         struct ocfs2_extent_rec tmprec;
4880         struct ocfs2_extent_list *rightmost_el;
4881         struct ocfs2_extent_rec rec;
4882         struct ocfs2_extent_rec split_rec = *orig_split_rec;
4883         struct ocfs2_insert_type insert;
4884         struct ocfs2_extent_block *eb;
4885
4886 leftright:
4887         /*
4888          * Store a copy of the record on the stack - it might move
4889          * around as the tree is manipulated below.
4890          */
4891         rec = path_leaf_el(path)->l_recs[split_index];
4892
4893         rightmost_el = et->et_root_el;
4894
4895         depth = le16_to_cpu(rightmost_el->l_tree_depth);
4896         if (depth) {
4897                 BUG_ON(!(*last_eb_bh));
4898                 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4899                 rightmost_el = &eb->h_list;
4900         }
4901
4902         if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4903             le16_to_cpu(rightmost_el->l_count)) {
4904                 ret = ocfs2_grow_tree(inode, handle, et,
4905                                       &depth, last_eb_bh, meta_ac);
4906                 if (ret) {
4907                         mlog_errno(ret);
4908                         goto out;
4909                 }
4910         }
4911
4912         memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4913         insert.ins_appending = APPEND_NONE;
4914         insert.ins_contig = CONTIG_NONE;
4915         insert.ins_tree_depth = depth;
4916
4917         insert_range = le32_to_cpu(split_rec.e_cpos) +
4918                 le16_to_cpu(split_rec.e_leaf_clusters);
4919         rec_range = le32_to_cpu(rec.e_cpos) +
4920                 le16_to_cpu(rec.e_leaf_clusters);
4921
4922         if (split_rec.e_cpos == rec.e_cpos) {
4923                 insert.ins_split = SPLIT_LEFT;
4924         } else if (insert_range == rec_range) {
4925                 insert.ins_split = SPLIT_RIGHT;
4926         } else {
4927                 /*
4928                  * Left/right split. We fake this as a right split
4929                  * first and then make a second pass as a left split.
4930                  */
4931                 insert.ins_split = SPLIT_RIGHT;
4932
4933                 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4934                                            &rec);
4935
4936                 split_rec = tmprec;
4937
4938                 BUG_ON(do_leftright);
4939                 do_leftright = 1;
4940         }
4941
4942         ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
4943         if (ret) {
4944                 mlog_errno(ret);
4945                 goto out;
4946         }
4947
4948         if (do_leftright == 1) {
4949                 u32 cpos;
4950                 struct ocfs2_extent_list *el;
4951
4952                 do_leftright++;
4953                 split_rec = *orig_split_rec;
4954
4955                 ocfs2_reinit_path(path, 1);
4956
4957                 cpos = le32_to_cpu(split_rec.e_cpos);
4958                 ret = ocfs2_find_path(et->et_ci, path, cpos);
4959                 if (ret) {
4960                         mlog_errno(ret);
4961                         goto out;
4962                 }
4963
4964                 el = path_leaf_el(path);
4965                 split_index = ocfs2_search_extent_list(el, cpos);
4966                 goto leftright;
4967         }
4968 out:
4969
4970         return ret;
4971 }
4972
4973 static int ocfs2_replace_extent_rec(struct inode *inode,
4974                                     handle_t *handle,
4975                                     struct ocfs2_path *path,
4976                                     struct ocfs2_extent_list *el,
4977                                     int split_index,
4978                                     struct ocfs2_extent_rec *split_rec)
4979 {
4980         int ret;
4981
4982         ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
4983                                            path_num_items(path) - 1);
4984         if (ret) {
4985                 mlog_errno(ret);
4986                 goto out;
4987         }
4988
4989         el->l_recs[split_index] = *split_rec;
4990
4991         ocfs2_journal_dirty(handle, path_leaf_bh(path));
4992 out:
4993         return ret;
4994 }
4995
4996 /*
4997  * Mark part or all of the extent record at split_index in the leaf
4998  * pointed to by path as written. This removes the unwritten
4999  * extent flag.
5000  *
5001  * Care is taken to handle contiguousness so as to not grow the tree.
5002  *
5003  * meta_ac is not strictly necessary - we only truly need it if growth
5004  * of the tree is required. All other cases will degrade into a less
5005  * optimal tree layout.
5006  *
5007  * last_eb_bh should be the rightmost leaf block for any extent
5008  * btree. Since a split may grow the tree or a merge might shrink it,
5009  * the caller cannot trust the contents of that buffer after this call.
5010  *
5011  * This code is optimized for readability - several passes might be
5012  * made over certain portions of the tree. All of those blocks will
5013  * have been brought into cache (and pinned via the journal), so the
5014  * extra overhead is not expressed in terms of disk reads.
5015  */
5016 static int __ocfs2_mark_extent_written(struct inode *inode,
5017                                        struct ocfs2_extent_tree *et,
5018                                        handle_t *handle,
5019                                        struct ocfs2_path *path,
5020                                        int split_index,
5021                                        struct ocfs2_extent_rec *split_rec,
5022                                        struct ocfs2_alloc_context *meta_ac,
5023                                        struct ocfs2_cached_dealloc_ctxt *dealloc)
5024 {
5025         int ret = 0;
5026         struct ocfs2_extent_list *el = path_leaf_el(path);
5027         struct buffer_head *last_eb_bh = NULL;
5028         struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5029         struct ocfs2_merge_ctxt ctxt;
5030         struct ocfs2_extent_list *rightmost_el;
5031
5032         if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
5033                 ret = -EIO;
5034                 mlog_errno(ret);
5035                 goto out;
5036         }
5037
5038         if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5039             ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5040              (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5041                 ret = -EIO;
5042                 mlog_errno(ret);
5043                 goto out;
5044         }
5045
5046         ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
5047                                                             split_index,
5048                                                             split_rec);
5049
5050         /*
5051          * The core merge / split code wants to know how much room is
5052          * left in this inodes allocation tree, so we pass the
5053          * rightmost extent list.
5054          */
5055         if (path->p_tree_depth) {
5056                 struct ocfs2_extent_block *eb;
5057
5058                 ret = ocfs2_read_extent_block(et->et_ci,
5059                                               ocfs2_et_get_last_eb_blk(et),
5060                                               &last_eb_bh);
5061                 if (ret) {
5062                         mlog_exit(ret);
5063                         goto out;
5064                 }
5065
5066                 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5067                 rightmost_el = &eb->h_list;
5068         } else
5069                 rightmost_el = path_root_el(path);
5070
5071         if (rec->e_cpos == split_rec->e_cpos &&
5072             rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5073                 ctxt.c_split_covers_rec = 1;
5074         else
5075                 ctxt.c_split_covers_rec = 0;
5076
5077         ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5078
5079         mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5080              split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5081              ctxt.c_split_covers_rec);
5082
5083         if (ctxt.c_contig_type == CONTIG_NONE) {
5084                 if (ctxt.c_split_covers_rec)
5085                         ret = ocfs2_replace_extent_rec(inode, handle,
5086                                                        path, el,
5087                                                        split_index, split_rec);
5088                 else
5089                         ret = ocfs2_split_and_insert(inode, handle, path, et,
5090                                                      &last_eb_bh, split_index,
5091                                                      split_rec, meta_ac);
5092                 if (ret)
5093                         mlog_errno(ret);
5094         } else {
5095                 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5096                                                 split_index, split_rec,
5097                                                 dealloc, &ctxt, et);
5098                 if (ret)
5099                         mlog_errno(ret);
5100         }
5101
5102 out:
5103         brelse(last_eb_bh);
5104         return ret;
5105 }
5106
5107 /*
5108  * Mark the already-existing extent at cpos as written for len clusters.
5109  *
5110  * If the existing extent is larger than the request, initiate a
5111  * split. An attempt will be made at merging with adjacent extents.
5112  *
5113  * The caller is responsible for passing down meta_ac if we'll need it.
5114  */
5115 int ocfs2_mark_extent_written(struct inode *inode,
5116                               struct ocfs2_extent_tree *et,
5117                               handle_t *handle, u32 cpos, u32 len, u32 phys,
5118                               struct ocfs2_alloc_context *meta_ac,
5119                               struct ocfs2_cached_dealloc_ctxt *dealloc)
5120 {
5121         int ret, index;
5122         u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5123         struct ocfs2_extent_rec split_rec;
5124         struct ocfs2_path *left_path = NULL;
5125         struct ocfs2_extent_list *el;
5126
5127         mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5128              inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5129
5130         if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5131                 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5132                             "that are being written to, but the feature bit "
5133                             "is not set in the super block.",
5134                             (unsigned long long)OCFS2_I(inode)->ip_blkno);
5135                 ret = -EROFS;
5136                 goto out;
5137         }
5138
5139         /*
5140          * XXX: This should be fixed up so that we just re-insert the
5141          * next extent records.
5142          *
5143          * XXX: This is a hack on the extent tree, maybe it should be
5144          * an op?
5145          */
5146         if (et->et_ops == &ocfs2_dinode_et_ops)
5147                 ocfs2_extent_map_trunc(inode, 0);
5148
5149         left_path = ocfs2_new_path_from_et(et);
5150         if (!left_path) {
5151                 ret = -ENOMEM;
5152                 mlog_errno(ret);
5153                 goto out;
5154         }
5155
5156         ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5157         if (ret) {
5158                 mlog_errno(ret);
5159                 goto out;
5160         }
5161         el = path_leaf_el(left_path);
5162
5163         index = ocfs2_search_extent_list(el, cpos);
5164         if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5165                 ocfs2_error(inode->i_sb,
5166                             "Inode %llu has an extent at cpos %u which can no "
5167                             "longer be found.\n",
5168                             (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5169                 ret = -EROFS;
5170                 goto out;
5171         }
5172
5173         memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5174         split_rec.e_cpos = cpu_to_le32(cpos);
5175         split_rec.e_leaf_clusters = cpu_to_le16(len);
5176         split_rec.e_blkno = cpu_to_le64(start_blkno);
5177         split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5178         split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5179
5180         ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
5181                                           index, &split_rec, meta_ac,
5182                                           dealloc);
5183         if (ret)
5184                 mlog_errno(ret);
5185
5186 out:
5187         ocfs2_free_path(left_path);
5188         return ret;
5189 }
5190
5191 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
5192                             handle_t *handle, struct ocfs2_path *path,
5193                             int index, u32 new_range,
5194                             struct ocfs2_alloc_context *meta_ac)
5195 {
5196         int ret, depth, credits = handle->h_buffer_credits;
5197         struct buffer_head *last_eb_bh = NULL;
5198         struct ocfs2_extent_block *eb;
5199         struct ocfs2_extent_list *rightmost_el, *el;
5200         struct ocfs2_extent_rec split_rec;
5201         struct ocfs2_extent_rec *rec;
5202         struct ocfs2_insert_type insert;
5203
5204         /*
5205          * Setup the record to split before we grow the tree.
5206          */
5207         el = path_leaf_el(path);
5208         rec = &el->l_recs[index];
5209         ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5210
5211         depth = path->p_tree_depth;
5212         if (depth > 0) {
5213                 ret = ocfs2_read_extent_block(et->et_ci,
5214                                               ocfs2_et_get_last_eb_blk(et),
5215                                               &last_eb_bh);
5216                 if (ret < 0) {
5217                         mlog_errno(ret);
5218                         goto out;
5219                 }
5220
5221                 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5222                 rightmost_el = &eb->h_list;
5223         } else
5224                 rightmost_el = path_leaf_el(path);
5225
5226         credits += path->p_tree_depth +
5227                    ocfs2_extend_meta_needed(et->et_root_el);
5228         ret = ocfs2_extend_trans(handle, credits);
5229         if (ret) {
5230                 mlog_errno(ret);
5231                 goto out;
5232         }
5233
5234         if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5235             le16_to_cpu(rightmost_el->l_count)) {
5236                 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
5237                                       meta_ac);
5238                 if (ret) {
5239                         mlog_errno(ret);
5240                         goto out;
5241                 }
5242         }
5243
5244         memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5245         insert.ins_appending = APPEND_NONE;
5246         insert.ins_contig = CONTIG_NONE;
5247         insert.ins_split = SPLIT_RIGHT;
5248         insert.ins_tree_depth = depth;
5249
5250         ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
5251         if (ret)
5252                 mlog_errno(ret);
5253
5254 out:
5255         brelse(last_eb_bh);
5256         return ret;
5257 }
5258
5259 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5260                               struct ocfs2_path *path, int index,
5261                               struct ocfs2_cached_dealloc_ctxt *dealloc,
5262                               u32 cpos, u32 len,
5263                               struct ocfs2_extent_tree *et)
5264 {
5265         int ret;
5266         u32 left_cpos, rec_range, trunc_range;
5267         int wants_rotate = 0, is_rightmost_tree_rec = 0;
5268         struct super_block *sb = inode->i_sb;
5269         struct ocfs2_path *left_path = NULL;
5270         struct ocfs2_extent_list *el = path_leaf_el(path);
5271         struct ocfs2_extent_rec *rec;
5272         struct ocfs2_extent_block *eb;
5273
5274         if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5275                 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5276                 if (ret) {
5277                         mlog_errno(ret);
5278                         goto out;
5279                 }
5280
5281                 index--;
5282         }
5283
5284         if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5285             path->p_tree_depth) {
5286                 /*
5287                  * Check whether this is the rightmost tree record. If
5288                  * we remove all of this record or part of its right
5289                  * edge then an update of the record lengths above it
5290                  * will be required.
5291                  */
5292                 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5293                 if (eb->h_next_leaf_blk == 0)
5294                         is_rightmost_tree_rec = 1;
5295         }
5296
5297         rec = &el->l_recs[index];
5298         if (index == 0 && path->p_tree_depth &&
5299             le32_to_cpu(rec->e_cpos) == cpos) {
5300                 /*
5301                  * Changing the leftmost offset (via partial or whole
5302                  * record truncate) of an interior (or rightmost) path
5303                  * means we have to update the subtree that is formed
5304                  * by this leaf and the one to it's left.
5305                  *
5306                  * There are two cases we can skip:
5307                  *   1) Path is the leftmost one in our inode tree.
5308                  *   2) The leaf is rightmost and will be empty after
5309                  *      we remove the extent record - the rotate code
5310                  *      knows how to update the newly formed edge.
5311                  */
5312
5313                 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5314                                                     &left_cpos);
5315                 if (ret) {
5316                         mlog_errno(ret);
5317                         goto out;
5318                 }
5319
5320                 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5321                         left_path = ocfs2_new_path_from_path(path);
5322                         if (!left_path) {
5323                                 ret = -ENOMEM;
5324                                 mlog_errno(ret);
5325                                 goto out;
5326                         }
5327
5328                         ret = ocfs2_find_path(et->et_ci, left_path,
5329                                               left_cpos);
5330                         if (ret) {
5331                                 mlog_errno(ret);
5332                                 goto out;
5333                         }
5334                 }
5335         }
5336
5337         ret = ocfs2_extend_rotate_transaction(handle, 0,
5338                                               handle->h_buffer_credits,
5339                                               path);
5340         if (ret) {
5341                 mlog_errno(ret);
5342                 goto out;
5343         }
5344
5345         ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5346         if (ret) {
5347                 mlog_errno(ret);
5348                 goto out;
5349         }
5350
5351         ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5352         if (ret) {
5353                 mlog_errno(ret);
5354                 goto out;
5355         }
5356
5357         rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5358         trunc_range = cpos + len;
5359
5360         if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5361                 int next_free;
5362
5363                 memset(rec, 0, sizeof(*rec));
5364                 ocfs2_cleanup_merge(el, index);
5365                 wants_rotate = 1;
5366
5367                 next_free = le16_to_cpu(el->l_next_free_rec);
5368                 if (is_rightmost_tree_rec && next_free > 1) {
5369                         /*
5370                          * We skip the edge update if this path will
5371                          * be deleted by the rotate code.
5372                          */
5373                         rec = &el->l_recs[next_free - 1];
5374                         ocfs2_adjust_rightmost_records(inode, handle, path,
5375                                                        rec);
5376                 }
5377         } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5378                 /* Remove leftmost portion of the record. */
5379                 le32_add_cpu(&rec->e_cpos, len);
5380                 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5381                 le16_add_cpu(&rec->e_leaf_clusters, -len);
5382         } else if (rec_range == trunc_range) {
5383                 /* Remove rightmost portion of the record */
5384                 le16_add_cpu(&rec->e_leaf_clusters, -len);
5385                 if (is_rightmost_tree_rec)
5386                         ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5387         } else {
5388                 /* Caller should have trapped this. */
5389                 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5390                      "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5391                      le32_to_cpu(rec->e_cpos),
5392                      le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5393                 BUG();
5394         }
5395
5396         if (left_path) {
5397                 int subtree_index;
5398
5399                 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5400                 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5401                                            subtree_index);
5402         }
5403
5404         ocfs2_journal_dirty(handle, path_leaf_bh(path));
5405
5406         ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
5407         if (ret) {
5408                 mlog_errno(ret);
5409                 goto out;
5410         }
5411
5412 out:
5413         ocfs2_free_path(left_path);
5414         return ret;
5415 }
5416
5417 int ocfs2_remove_extent(struct inode *inode,
5418                         struct ocfs2_extent_tree *et,
5419                         u32 cpos, u32 len, handle_t *handle,
5420                         struct ocfs2_alloc_context *meta_ac,
5421                         struct ocfs2_cached_dealloc_ctxt *dealloc)
5422 {
5423         int ret, index;
5424         u32 rec_range, trunc_range;
5425         struct ocfs2_extent_rec *rec;
5426         struct ocfs2_extent_list *el;
5427         struct ocfs2_path *path = NULL;
5428
5429         ocfs2_extent_map_trunc(inode, 0);
5430
5431         path = ocfs2_new_path_from_et(et);
5432         if (!path) {
5433                 ret = -ENOMEM;
5434                 mlog_errno(ret);
5435                 goto out;
5436         }
5437
5438         ret = ocfs2_find_path(et->et_ci, path, cpos);
5439         if (ret) {
5440                 mlog_errno(ret);
5441                 goto out;
5442         }
5443
5444         el = path_leaf_el(path);
5445         index = ocfs2_search_extent_list(el, cpos);
5446         if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5447                 ocfs2_error(inode->i_sb,
5448                             "Inode %llu has an extent at cpos %u which can no "
5449                             "longer be found.\n",
5450                             (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5451                 ret = -EROFS;
5452                 goto out;
5453         }
5454
5455         /*
5456          * We have 3 cases of extent removal:
5457          *   1) Range covers the entire extent rec
5458          *   2) Range begins or ends on one edge of the extent rec
5459          *   3) Range is in the middle of the extent rec (no shared edges)
5460          *
5461          * For case 1 we remove the extent rec and left rotate to
5462          * fill the hole.
5463          *
5464          * For case 2 we just shrink the existing extent rec, with a
5465          * tree update if the shrinking edge is also the edge of an
5466          * extent block.
5467          *
5468          * For case 3 we do a right split to turn the extent rec into
5469          * something case 2 can handle.
5470          */
5471         rec = &el->l_recs[index];
5472         rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5473         trunc_range = cpos + len;
5474
5475         BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5476
5477         mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5478              "(cpos %u, len %u)\n",
5479              (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5480              le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5481
5482         if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5483                 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5484                                          cpos, len, et);
5485                 if (ret) {
5486                         mlog_errno(ret);
5487                         goto out;
5488                 }
5489         } else {
5490                 ret = ocfs2_split_tree(inode, et, handle, path, index,
5491                                        trunc_range, meta_ac);
5492                 if (ret) {
5493                         mlog_errno(ret);
5494                         goto out;
5495                 }
5496
5497                 /*
5498                  * The split could have manipulated the tree enough to
5499                  * move the record location, so we have to look for it again.
5500                  */
5501                 ocfs2_reinit_path(path, 1);
5502
5503                 ret = ocfs2_find_path(et->et_ci, path, cpos);
5504                 if (ret) {
5505                         mlog_errno(ret);
5506                         goto out;
5507                 }
5508
5509                 el = path_leaf_el(path);
5510                 index = ocfs2_search_extent_list(el, cpos);
5511                 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5512                         ocfs2_error(inode->i_sb,
5513                                     "Inode %llu: split at cpos %u lost record.",
5514                                     (unsigned long long)OCFS2_I(inode)->ip_blkno,
5515                                     cpos);
5516                         ret = -EROFS;
5517                         goto out;
5518                 }
5519
5520                 /*
5521                  * Double check our values here. If anything is fishy,
5522                  * it's easier to catch it at the top level.
5523                  */
5524                 rec = &el->l_recs[index];
5525                 rec_range = le32_to_cpu(rec->e_cpos) +
5526                         ocfs2_rec_clusters(el, rec);
5527                 if (rec_range != trunc_range) {
5528                         ocfs2_error(inode->i_sb,
5529                                     "Inode %llu: error after split at cpos %u"
5530                                     "trunc len %u, existing record is (%u,%u)",
5531                                     (unsigned long long)OCFS2_I(inode)->ip_blkno,
5532                                     cpos, len, le32_to_cpu(rec->e_cpos),
5533                                     ocfs2_rec_clusters(el, rec));
5534                         ret = -EROFS;
5535                         goto out;
5536                 }
5537
5538                 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
5539                                          cpos, len, et);
5540                 if (ret) {
5541                         mlog_errno(ret);
5542                         goto out;
5543                 }
5544         }
5545
5546 out:
5547         ocfs2_free_path(path);
5548         return ret;
5549 }
5550
5551 int ocfs2_remove_btree_range(struct inode *inode,
5552                              struct ocfs2_extent_tree *et,
5553                              u32 cpos, u32 phys_cpos, u32 len,
5554                              struct ocfs2_cached_dealloc_ctxt *dealloc)
5555 {
5556         int ret;
5557         u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5558         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5559         struct inode *tl_inode = osb->osb_tl_inode;
5560         handle_t *handle;
5561         struct ocfs2_alloc_context *meta_ac = NULL;
5562
5563         ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5564         if (ret) {
5565                 mlog_errno(ret);
5566                 return ret;
5567         }
5568
5569         mutex_lock(&tl_inode->i_mutex);
5570
5571         if (ocfs2_truncate_log_needs_flush(osb)) {
5572                 ret = __ocfs2_flush_truncate_log(osb);
5573                 if (ret < 0) {
5574                         mlog_errno(ret);
5575                         goto out;
5576                 }
5577         }
5578
5579         handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5580         if (IS_ERR(handle)) {
5581                 ret = PTR_ERR(handle);
5582                 mlog_errno(ret);
5583                 goto out;
5584         }
5585
5586         ret = ocfs2_et_root_journal_access(handle, et,
5587                                            OCFS2_JOURNAL_ACCESS_WRITE);
5588         if (ret) {
5589                 mlog_errno(ret);
5590                 goto out;
5591         }
5592
5593         vfs_dq_free_space_nodirty(inode,
5594                                   ocfs2_clusters_to_bytes(inode->i_sb, len));
5595
5596         ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5597                                   dealloc);
5598         if (ret) {
5599                 mlog_errno(ret);
5600                 goto out_commit;
5601         }
5602
5603         ocfs2_et_update_clusters(inode, et, -len);
5604
5605         ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5606         if (ret) {
5607                 mlog_errno(ret);
5608                 goto out_commit;
5609         }
5610
5611         ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5612         if (ret)
5613                 mlog_errno(ret);
5614
5615 out_commit:
5616         ocfs2_commit_trans(osb, handle);
5617 out:
5618         mutex_unlock(&tl_inode->i_mutex);
5619
5620         if (meta_ac)
5621                 ocfs2_free_alloc_context(meta_ac);
5622
5623         return ret;
5624 }
5625
5626 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5627 {
5628         struct buffer_head *tl_bh = osb->osb_tl_bh;
5629         struct ocfs2_dinode *di;
5630         struct ocfs2_truncate_log *tl;
5631
5632         di = (struct ocfs2_dinode *) tl_bh->b_data;
5633         tl = &di->id2.i_dealloc;
5634
5635         mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5636                         "slot %d, invalid truncate log parameters: used = "
5637                         "%u, count = %u\n", osb->slot_num,
5638                         le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5639         return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5640 }
5641
5642 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5643                                            unsigned int new_start)
5644 {
5645         unsigned int tail_index;
5646         unsigned int current_tail;
5647
5648         /* No records, nothing to coalesce */
5649         if (!le16_to_cpu(tl->tl_used))
5650                 return 0;
5651
5652         tail_index = le16_to_cpu(tl->tl_used) - 1;
5653         current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5654         current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5655
5656         return current_tail == new_start;
5657 }
5658
5659 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5660                               handle_t *handle,
5661                               u64 start_blk,
5662                               unsigned int num_clusters)
5663 {
5664         int status, index;
5665         unsigned int start_cluster, tl_count;
5666         struct inode *tl_inode = osb->osb_tl_inode;
5667         struct buffer_head *tl_bh = osb->osb_tl_bh;
5668         struct ocfs2_dinode *di;
5669         struct ocfs2_truncate_log *tl;
5670
5671         mlog_entry("start_blk = %llu, num_clusters = %u\n",
5672                    (unsigned long long)start_blk, num_clusters);
5673
5674         BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5675
5676         start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5677
5678         di = (struct ocfs2_dinode *) tl_bh->b_data;
5679
5680         /* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
5681          * by the underlying call to ocfs2_read_inode_block(), so any
5682          * corruption is a code bug */
5683         BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5684
5685         tl = &di->id2.i_dealloc;
5686         tl_count = le16_to_cpu(tl->tl_count);
5687         mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5688                         tl_count == 0,
5689                         "Truncate record count on #%llu invalid "
5690                         "wanted %u, actual %u\n",
5691                         (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5692                         ocfs2_truncate_recs_per_inode(osb->sb),
5693                         le16_to_cpu(tl->tl_count));
5694
5695         /* Caller should have known to flush before calling us. */
5696         index = le16_to_cpu(tl->tl_used);
5697         if (index >= tl_count) {
5698                 status = -ENOSPC;
5699                 mlog_errno(status);
5700                 goto bail;
5701         }
5702
5703         status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5704                                          OCFS2_JOURNAL_ACCESS_WRITE);
5705         if (status < 0) {
5706                 mlog_errno(status);
5707                 goto bail;
5708         }
5709
5710         mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5711              "%llu (index = %d)\n", num_clusters, start_cluster,
5712              (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5713
5714         if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5715                 /*
5716                  * Move index back to the record we are coalescing with.
5717                  * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5718                  */
5719                 index--;
5720
5721                 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5722                 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5723                      index, le32_to_cpu(tl->tl_recs[index].t_start),
5724                      num_clusters);
5725         } else {
5726                 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5727                 tl->tl_used = cpu_to_le16(index + 1);
5728         }
5729         tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5730
5731         status = ocfs2_journal_dirty(handle, tl_bh);
5732         if (status < 0) {
5733                 mlog_errno(status);
5734                 goto bail;
5735         }
5736
5737 bail:
5738         mlog_exit(status);
5739         return status;
5740 }
5741
5742 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5743                                          handle_t *handle,
5744                                          struct inode *data_alloc_inode,
5745                                          struct buffer_head *data_alloc_bh)
5746 {
5747         int status = 0;
5748         int i;
5749         unsigned int num_clusters;
5750         u64 start_blk;
5751         struct ocfs2_truncate_rec rec;
5752         struct ocfs2_dinode *di;
5753         struct ocfs2_truncate_log *tl;
5754         struct inode *tl_inode = osb->osb_tl_inode;
5755         struct buffer_head *tl_bh = osb->osb_tl_bh;
5756
5757         mlog_entry_void();
5758
5759         di = (struct ocfs2_dinode *) tl_bh->b_data;
5760         tl = &di->id2.i_dealloc;
5761         i = le16_to_cpu(tl->tl_used) - 1;
5762         while (i >= 0) {
5763                 /* Caller has given us at least enough credits to
5764                  * update the truncate log dinode */
5765                 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5766                                                  OCFS2_JOURNAL_ACCESS_WRITE);
5767                 if (status < 0) {
5768                         mlog_errno(status);
5769                         goto bail;
5770                 }
5771
5772                 tl->tl_used = cpu_to_le16(i);
5773
5774                 status = ocfs2_journal_dirty(handle, tl_bh);
5775                 if (status < 0) {
5776                         mlog_errno(status);
5777                         goto bail;
5778                 }
5779
5780                 /* TODO: Perhaps we can calculate the bulk of the
5781                  * credits up front rather than extending like
5782                  * this. */
5783                 status = ocfs2_extend_trans(handle,
5784                                             OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5785                 if (status < 0) {
5786                         mlog_errno(status);
5787                         goto bail;
5788                 }
5789
5790                 rec = tl->tl_recs[i];
5791                 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5792                                                     le32_to_cpu(rec.t_start));
5793                 num_clusters = le32_to_cpu(rec.t_clusters);
5794
5795                 /* if start_blk is not set, we ignore the record as
5796                  * invalid. */
5797                 if (start_blk) {
5798                         mlog(0, "free record %d, start = %u, clusters = %u\n",
5799                              i, le32_to_cpu(rec.t_start), num_clusters);
5800
5801                         status = ocfs2_free_clusters(handle, data_alloc_inode,
5802                                                      data_alloc_bh, start_blk,
5803                                                      num_clusters);
5804                         if (status < 0) {
5805                                 mlog_errno(status);
5806                                 goto bail;
5807                         }
5808                 }
5809                 i--;
5810         }
5811
5812 bail:
5813         mlog_exit(status);
5814         return status;
5815 }
5816
5817 /* Expects you to already be holding tl_inode->i_mutex */
5818 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5819 {
5820         int status;
5821         unsigned int num_to_flush;
5822         handle_t *handle;
5823         struct inode *tl_inode = osb->osb_tl_inode;
5824         struct inode *data_alloc_inode = NULL;
5825         struct buffer_head *tl_bh = osb->osb_tl_bh;
5826         struct buffer_head *data_alloc_bh = NULL;
5827         struct ocfs2_dinode *di;
5828         struct ocfs2_truncate_log *tl;
5829
5830         mlog_entry_void();
5831
5832         BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5833
5834         di = (struct ocfs2_dinode *) tl_bh->b_data;
5835
5836         /* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
5837          * by the underlying call to ocfs2_read_inode_block(), so any
5838          * corruption is a code bug */
5839         BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5840
5841         tl = &di->id2.i_dealloc;
5842         num_to_flush = le16_to_cpu(tl->tl_used);
5843         mlog(0, "Flush %u records from truncate log #%llu\n",
5844              num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5845         if (!num_to_flush) {
5846                 status = 0;
5847                 goto out;
5848         }
5849
5850         data_alloc_inode = ocfs2_get_system_file_inode(osb,
5851                                                        GLOBAL_BITMAP_SYSTEM_INODE,
5852                                                        OCFS2_INVALID_SLOT);
5853         if (!data_alloc_inode) {
5854                 status = -EINVAL;
5855                 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5856                 goto out;
5857         }
5858
5859         mutex_lock(&data_alloc_inode->i_mutex);
5860
5861         status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5862         if (status < 0) {
5863                 mlog_errno(status);
5864                 goto out_mutex;
5865         }
5866
5867         handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5868         if (IS_ERR(handle)) {
5869                 status = PTR_ERR(handle);
5870                 mlog_errno(status);
5871                 goto out_unlock;
5872         }
5873
5874         status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5875                                                data_alloc_bh);
5876         if (status < 0)
5877                 mlog_errno(status);
5878
5879         ocfs2_commit_trans(osb, handle);
5880
5881 out_unlock:
5882         brelse(data_alloc_bh);
5883         ocfs2_inode_unlock(data_alloc_inode, 1);
5884
5885 out_mutex:
5886         mutex_unlock(&data_alloc_inode->i_mutex);
5887         iput(data_alloc_inode);
5888
5889 out:
5890         mlog_exit(status);
5891         return status;
5892 }
5893
5894 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5895 {
5896         int status;
5897         struct inode *tl_inode = osb->osb_tl_inode;
5898
5899         mutex_lock(&tl_inode->i_mutex);
5900         status = __ocfs2_flush_truncate_log(osb);
5901         mutex_unlock(&tl_inode->i_mutex);
5902
5903         return status;
5904 }
5905
5906 static void ocfs2_truncate_log_worker(struct work_struct *work)
5907 {
5908         int status;
5909         struct ocfs2_super *osb =
5910                 container_of(work, struct ocfs2_super,
5911                              osb_truncate_log_wq.work);
5912
5913         mlog_entry_void();
5914
5915         status = ocfs2_flush_truncate_log(osb);
5916         if (status < 0)
5917                 mlog_errno(status);
5918         else
5919                 ocfs2_init_inode_steal_slot(osb);
5920
5921         mlog_exit(status);
5922 }
5923
5924 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5925 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5926                                        int cancel)
5927 {
5928         if (osb->osb_tl_inode) {
5929                 /* We want to push off log flushes while truncates are
5930                  * still running. */
5931                 if (cancel)
5932                         cancel_delayed_work(&osb->osb_truncate_log_wq);
5933
5934                 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5935                                    OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5936         }
5937 }
5938
5939 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5940                                        int slot_num,
5941                                        struct inode **tl_inode,
5942                                        struct buffer_head **tl_bh)
5943 {
5944         int status;
5945         struct inode *inode = NULL;
5946         struct buffer_head *bh = NULL;
5947
5948         inode = ocfs2_get_system_file_inode(osb,
5949                                            TRUNCATE_LOG_SYSTEM_INODE,
5950                                            slot_num);
5951         if (!inode) {
5952                 status = -EINVAL;
5953                 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5954                 goto bail;
5955         }
5956
5957         status = ocfs2_read_inode_block(inode, &bh);
5958         if (status < 0) {
5959                 iput(inode);
5960                 mlog_errno(status);
5961                 goto bail;
5962         }
5963
5964         *tl_inode = inode;
5965         *tl_bh    = bh;
5966 bail:
5967         mlog_exit(status);
5968         return status;
5969 }
5970
5971 /* called during the 1st stage of node recovery. we stamp a clean
5972  * truncate log and pass back a copy for processing later. if the
5973  * truncate log does not require processing, a *tl_copy is set to
5974  * NULL. */
5975 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5976                                       int slot_num,
5977                                       struct ocfs2_dinode **tl_copy)
5978 {
5979         int status;
5980         struct inode *tl_inode = NULL;
5981         struct buffer_head *tl_bh = NULL;
5982         struct ocfs2_dinode *di;
5983         struct ocfs2_truncate_log *tl;
5984
5985         *tl_copy = NULL;
5986
5987         mlog(0, "recover truncate log from slot %d\n", slot_num);
5988
5989         status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5990         if (status < 0) {
5991                 mlog_errno(status);
5992                 goto bail;
5993         }
5994
5995         di = (struct ocfs2_dinode *) tl_bh->b_data;
5996
5997         /* tl_bh is loaded from ocfs2_get_truncate_log_info().  It's
5998          * validated by the underlying call to ocfs2_read_inode_block(),
5999          * so any corruption is a code bug */
6000         BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6001
6002         tl = &di->id2.i_dealloc;
6003         if (le16_to_cpu(tl->tl_used)) {
6004                 mlog(0, "We'll have %u logs to recover\n",
6005                      le16_to_cpu(tl->tl_used));
6006
6007                 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6008                 if (!(*tl_copy)) {
6009                         status = -ENOMEM;
6010                         mlog_errno(status);
6011                         goto bail;
6012                 }
6013
6014                 /* Assuming the write-out below goes well, this copy
6015                  * will be passed back to recovery for processing. */
6016                 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6017
6018                 /* All we need to do to clear the truncate log is set
6019                  * tl_used. */
6020                 tl->tl_used = 0;
6021
6022                 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6023                 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6024                 if (status < 0) {
6025                         mlog_errno(status);
6026                         goto bail;
6027                 }
6028         }
6029
6030 bail:
6031         if (tl_inode)
6032                 iput(tl_inode);
6033         brelse(tl_bh);
6034
6035         if (status < 0 && (*tl_copy)) {
6036                 kfree(*tl_copy);
6037                 *tl_copy = NULL;
6038         }
6039
6040         mlog_exit(status);
6041         return status;
6042 }
6043
6044 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6045                                          struct ocfs2_dinode *tl_copy)
6046 {
6047         int status = 0;
6048         int i;
6049         unsigned int clusters, num_recs, start_cluster;
6050         u64 start_blk;
6051         handle_t *handle;
6052         struct inode *tl_inode = osb->osb_tl_inode;
6053         struct ocfs2_truncate_log *tl;
6054
6055         mlog_entry_void();
6056
6057         if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6058                 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6059                 return -EINVAL;
6060         }
6061
6062         tl = &tl_copy->id2.i_dealloc;
6063         num_recs = le16_to_cpu(tl->tl_used);
6064         mlog(0, "cleanup %u records from %llu\n", num_recs,
6065              (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6066
6067         mutex_lock(&tl_inode->i_mutex);
6068         for(i = 0; i < num_recs; i++) {
6069                 if (ocfs2_truncate_log_needs_flush(osb)) {
6070                         status = __ocfs2_flush_truncate_log(osb);
6071                         if (status < 0) {
6072                                 mlog_errno(status);
6073                                 goto bail_up;
6074                         }
6075                 }
6076
6077                 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6078                 if (IS_ERR(handle)) {
6079                         status = PTR_ERR(handle);
6080                         mlog_errno(status);
6081                         goto bail_up;
6082                 }
6083
6084                 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6085                 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6086                 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6087
6088                 status = ocfs2_truncate_log_append(osb, handle,
6089                                                    start_blk, clusters);
6090                 ocfs2_commit_trans(osb, handle);
6091                 if (status < 0) {
6092                         mlog_errno(status);
6093                         goto bail_up;
6094                 }
6095         }
6096
6097 bail_up:
6098         mutex_unlock(&tl_inode->i_mutex);
6099
6100         mlog_exit(status);
6101         return status;
6102 }
6103
6104 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6105 {
6106         int status;
6107         struct inode *tl_inode = osb->osb_tl_inode;
6108
6109         mlog_entry_void();
6110
6111         if (tl_inode) {
6112                 cancel_delayed_work(&osb->osb_truncate_log_wq);
6113                 flush_workqueue(ocfs2_wq);
6114
6115                 status = ocfs2_flush_truncate_log(osb);
6116                 if (status < 0)
6117                         mlog_errno(status);
6118
6119                 brelse(osb->osb_tl_bh);
6120                 iput(osb->osb_tl_inode);
6121         }
6122
6123         mlog_exit_void();
6124 }
6125
6126 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6127 {
6128         int status;
6129         struct inode *tl_inode = NULL;
6130         struct buffer_head *tl_bh = NULL;
6131
6132         mlog_entry_void();
6133
6134         status = ocfs2_get_truncate_log_info(osb,
6135                                              osb->slot_num,
6136                                              &tl_inode,
6137                                              &tl_bh);
6138         if (status < 0)
6139                 mlog_errno(status);
6140
6141         /* ocfs2_truncate_log_shutdown keys on the existence of
6142          * osb->osb_tl_inode so we don't set any of the osb variables
6143          * until we're sure all is well. */
6144         INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6145                           ocfs2_truncate_log_worker);
6146         osb->osb_tl_bh    = tl_bh;
6147         osb->osb_tl_inode = tl_inode;
6148
6149         mlog_exit(status);
6150         return status;
6151 }
6152
6153 /*
6154  * Delayed de-allocation of suballocator blocks.
6155  *
6156  * Some sets of block de-allocations might involve multiple suballocator inodes.
6157  *
6158  * The locking for this can get extremely complicated, especially when
6159  * the suballocator inodes to delete from aren't known until deep
6160  * within an unrelated codepath.
6161  *
6162  * ocfs2_extent_block structures are a good example of this - an inode
6163  * btree could have been grown by any number of nodes each allocating
6164  * out of their own suballoc inode.
6165  *
6166  * These structures allow the delay of block de-allocation until a
6167  * later time, when locking of multiple cluster inodes won't cause
6168  * deadlock.
6169  */
6170
6171 /*
6172  * Describe a single bit freed from a suballocator.  For the block
6173  * suballocators, it represents one block.  For the global cluster
6174  * allocator, it represents some clusters and free_bit indicates
6175  * clusters number.
6176  */
6177 struct ocfs2_cached_block_free {
6178         struct ocfs2_cached_block_free          *free_next;
6179         u64                                     free_blk;
6180         unsigned int                            free_bit;
6181 };
6182
6183 struct ocfs2_per_slot_free_list {
6184         struct ocfs2_per_slot_free_list         *f_next_suballocator;
6185         int                                     f_inode_type;
6186         int                                     f_slot;
6187         struct ocfs2_cached_block_free          *f_first;
6188 };
6189
6190 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6191                                     int sysfile_type,
6192                                     int slot,
6193                                     struct ocfs2_cached_block_free *head)
6194 {
6195         int ret;
6196         u64 bg_blkno;
6197         handle_t *handle;
6198         struct inode *inode;
6199         struct buffer_head *di_bh = NULL;
6200         struct ocfs2_cached_block_free *tmp;
6201
6202         inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6203         if (!inode) {
6204                 ret = -EINVAL;
6205                 mlog_errno(ret);
6206                 goto out;
6207         }
6208
6209         mutex_lock(&inode->i_mutex);
6210
6211         ret = ocfs2_inode_lock(inode, &di_bh, 1);
6212         if (ret) {
6213                 mlog_errno(ret);
6214                 goto out_mutex;
6215         }
6216
6217         handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6218         if (IS_ERR(handle)) {
6219                 ret = PTR_ERR(handle);
6220                 mlog_errno(ret);
6221                 goto out_unlock;
6222         }
6223
6224         while (head) {
6225                 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6226                                                       head->free_bit);
6227                 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6228                      head->free_bit, (unsigned long long)head->free_blk);
6229
6230                 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6231                                                head->free_bit, bg_blkno, 1);
6232                 if (ret) {
6233                         mlog_errno(ret);
6234                         goto out_journal;
6235                 }
6236
6237                 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6238                 if (ret) {
6239                         mlog_errno(ret);
6240                         goto out_journal;
6241                 }
6242
6243                 tmp = head;
6244                 head = head->free_next;
6245                 kfree(tmp);
6246         }
6247
6248 out_journal:
6249         ocfs2_commit_trans(osb, handle);
6250
6251 out_unlock:
6252         ocfs2_inode_unlock(inode, 1);
6253         brelse(di_bh);
6254 out_mutex:
6255         mutex_unlock(&inode->i_mutex);
6256         iput(inode);
6257 out:
6258         while(head) {
6259                 /* Premature exit may have left some dangling items. */
6260                 tmp = head;
6261                 head = head->free_next;
6262                 kfree(tmp);
6263         }
6264
6265         return ret;
6266 }
6267
6268 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6269                                 u64 blkno, unsigned int bit)
6270 {
6271         int ret = 0;
6272         struct ocfs2_cached_block_free *item;
6273
6274         item = kmalloc(sizeof(*item), GFP_NOFS);
6275         if (item == NULL) {
6276                 ret = -ENOMEM;
6277                 mlog_errno(ret);
6278                 return ret;
6279         }
6280
6281         mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6282              bit, (unsigned long long)blkno);
6283
6284         item->free_blk = blkno;
6285         item->free_bit = bit;
6286         item->free_next = ctxt->c_global_allocator;
6287
6288         ctxt->c_global_allocator = item;
6289         return ret;
6290 }
6291
6292 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6293                                       struct ocfs2_cached_block_free *head)
6294 {
6295         struct ocfs2_cached_block_free *tmp;
6296         struct inode *tl_inode = osb->osb_tl_inode;
6297         handle_t *handle;
6298         int ret = 0;
6299
6300         mutex_lock(&tl_inode->i_mutex);
6301
6302         while (head) {
6303                 if (ocfs2_truncate_log_needs_flush(osb)) {
6304                         ret = __ocfs2_flush_truncate_log(osb);
6305                         if (ret < 0) {
6306                                 mlog_errno(ret);
6307                                 break;
6308                         }
6309                 }
6310
6311                 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6312                 if (IS_ERR(handle)) {
6313                         ret = PTR_ERR(handle);
6314                         mlog_errno(ret);
6315                         break;
6316                 }
6317
6318                 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6319                                                 head->free_bit);
6320
6321                 ocfs2_commit_trans(osb, handle);
6322                 tmp = head;
6323                 head = head->free_next;
6324                 kfree(tmp);
6325
6326                 if (ret < 0) {
6327                         mlog_errno(ret);
6328                         break;
6329                 }
6330         }
6331
6332         mutex_unlock(&tl_inode->i_mutex);
6333
6334         while (head) {
6335                 /* Premature exit may have left some dangling items. */
6336                 tmp = head;
6337                 head = head->free_next;
6338                 kfree(tmp);
6339         }
6340
6341         return ret;
6342 }
6343
6344 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6345                        struct ocfs2_cached_dealloc_ctxt *ctxt)
6346 {
6347         int ret = 0, ret2;
6348         struct ocfs2_per_slot_free_list *fl;
6349
6350         if (!ctxt)
6351                 return 0;
6352
6353         while (ctxt->c_first_suballocator) {
6354                 fl = ctxt->c_first_suballocator;
6355
6356                 if (fl->f_first) {
6357                         mlog(0, "Free items: (type %u, slot %d)\n",
6358                              fl->f_inode_type, fl->f_slot);
6359                         ret2 = ocfs2_free_cached_blocks(osb,
6360                                                         fl->f_inode_type,
6361                                                         fl->f_slot,
6362                                                         fl->f_first);
6363                         if (ret2)
6364                                 mlog_errno(ret2);
6365                         if (!ret)
6366                                 ret = ret2;
6367                 }
6368
6369                 ctxt->c_first_suballocator = fl->f_next_suballocator;
6370                 kfree(fl);
6371         }
6372
6373         if (ctxt->c_global_allocator) {
6374                 ret2 = ocfs2_free_cached_clusters(osb,
6375                                                   ctxt->c_global_allocator);
6376                 if (ret2)
6377                         mlog_errno(ret2);
6378                 if (!ret)
6379                         ret = ret2;
6380
6381                 ctxt->c_global_allocator = NULL;
6382         }
6383
6384         return ret;
6385 }
6386
6387 static struct ocfs2_per_slot_free_list *
6388 ocfs2_find_per_slot_free_list(int type,
6389                               int slot,
6390                               struct ocfs2_cached_dealloc_ctxt *ctxt)
6391 {
6392         struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6393
6394         while (fl) {
6395                 if (fl->f_inode_type == type && fl->f_slot == slot)
6396                         return fl;
6397
6398                 fl = fl->f_next_suballocator;
6399         }
6400
6401         fl = kmalloc(sizeof(*fl), GFP_NOFS);
6402         if (fl) {
6403                 fl->f_inode_type = type;
6404                 fl->f_slot = slot;
6405                 fl->f_first = NULL;
6406                 fl->f_next_suballocator = ctxt->c_first_suballocator;
6407
6408                 ctxt->c_first_suballocator = fl;
6409         }
6410         return fl;
6411 }
6412
6413 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6414                                      int type, int slot, u64 blkno,
6415                                      unsigned int bit)
6416 {
6417         int ret;
6418         struct ocfs2_per_slot_free_list *fl;
6419         struct ocfs2_cached_block_free *item;
6420
6421         fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6422         if (fl == NULL) {
6423                 ret = -ENOMEM;
6424                 mlog_errno(ret);
6425                 goto out;
6426         }
6427
6428         item = kmalloc(sizeof(*item), GFP_NOFS);
6429         if (item == NULL) {
6430                 ret = -ENOMEM;
6431                 mlog_errno(ret);
6432                 goto out;
6433         }
6434
6435         mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6436              type, slot, bit, (unsigned long long)blkno);
6437
6438         item->free_blk = blkno;
6439         item->free_bit = bit;
6440         item->free_next = fl->f_first;
6441
6442         fl->f_first = item;
6443
6444         ret = 0;
6445 out:
6446         return ret;
6447 }
6448
6449 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6450                                          struct ocfs2_extent_block *eb)
6451 {
6452         return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6453                                          le16_to_cpu(eb->h_suballoc_slot),
6454                                          le64_to_cpu(eb->h_blkno),
6455                                          le16_to_cpu(eb->h_suballoc_bit));
6456 }
6457
6458 /* This function will figure out whether the currently last extent
6459  * block will be deleted, and if it will, what the new last extent
6460  * block will be so we can update his h_next_leaf_blk field, as well
6461  * as the dinodes i_last_eb_blk */
6462 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6463                                        unsigned int clusters_to_del,
6464                                        struct ocfs2_path *path,
6465                                        struct buffer_head **new_last_eb)
6466 {
6467         int next_free, ret = 0;
6468         u32 cpos;
6469         struct ocfs2_extent_rec *rec;
6470         struct ocfs2_extent_block *eb;
6471         struct ocfs2_extent_list *el;
6472         struct buffer_head *bh = NULL;
6473
6474         *new_last_eb = NULL;
6475
6476         /* we have no tree, so of course, no last_eb. */
6477         if (!path->p_tree_depth)
6478                 goto out;
6479
6480         /* trunc to zero special case - this makes tree_depth = 0
6481          * regardless of what it is.  */
6482         if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6483                 goto out;
6484
6485         el = path_leaf_el(path);
6486         BUG_ON(!el->l_next_free_rec);
6487
6488         /*
6489          * Make sure that this extent list will actually be empty
6490          * after we clear away the data. We can shortcut out if
6491          * there's more than one non-empty extent in the
6492          * list. Otherwise, a check of the remaining extent is
6493          * necessary.
6494          */
6495         next_free = le16_to_cpu(el->l_next_free_rec);
6496         rec = NULL;
6497         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6498                 if (next_free > 2)
6499                         goto out;
6500
6501                 /* We may have a valid extent in index 1, check it. */
6502                 if (next_free == 2)
6503                         rec = &el->l_recs[1];
6504
6505                 /*
6506                  * Fall through - no more nonempty extents, so we want
6507                  * to delete this leaf.
6508                  */
6509         } else {
6510                 if (next_free > 1)
6511                         goto out;
6512
6513                 rec = &el->l_recs[0];
6514         }
6515
6516         if (rec) {
6517                 /*
6518                  * Check it we'll only be trimming off the end of this
6519                  * cluster.
6520                  */
6521                 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6522                         goto out;
6523         }
6524
6525         ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6526         if (ret) {
6527                 mlog_errno(ret);
6528                 goto out;
6529         }
6530
6531         ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
6532         if (ret) {
6533                 mlog_errno(ret);
6534                 goto out;
6535         }
6536
6537         eb = (struct ocfs2_extent_block *) bh->b_data;
6538         el = &eb->h_list;
6539
6540         /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6541          * Any corruption is a code bug. */
6542         BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6543
6544         *new_last_eb = bh;
6545         get_bh(*new_last_eb);
6546         mlog(0, "returning block %llu, (cpos: %u)\n",
6547              (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6548 out:
6549         brelse(bh);
6550
6551         return ret;
6552 }
6553
6554 /*
6555  * Trim some clusters off the rightmost edge of a tree. Only called
6556  * during truncate.
6557  *
6558  * The caller needs to:
6559  *   - start journaling of each path component.
6560  *   - compute and fully set up any new last ext block
6561  */
6562 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6563                            handle_t *handle, struct ocfs2_truncate_context *tc,
6564                            u32 clusters_to_del, u64 *delete_start)
6565 {
6566         int ret, i, index = path->p_tree_depth;
6567         u32 new_edge = 0;
6568         u64 deleted_eb = 0;
6569         struct buffer_head *bh;
6570         struct ocfs2_extent_list *el;
6571         struct ocfs2_extent_rec *rec;
6572
6573         *delete_start = 0;
6574
6575         while (index >= 0) {
6576                 bh = path->p_node[index].bh;
6577                 el = path->p_node[index].el;
6578
6579                 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6580                      index,  (unsigned long long)bh->b_blocknr);
6581
6582                 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6583
6584                 if (index !=
6585                     (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6586                         ocfs2_error(inode->i_sb,
6587                                     "Inode %lu has invalid ext. block %llu",
6588                                     inode->i_ino,
6589                                     (unsigned long long)bh->b_blocknr);
6590                         ret = -EROFS;
6591                         goto out;
6592                 }
6593
6594 find_tail_record:
6595                 i = le16_to_cpu(el->l_next_free_rec) - 1;
6596                 rec = &el->l_recs[i];
6597
6598                 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6599                      "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6600                      ocfs2_rec_clusters(el, rec),
6601                      (unsigned long long)le64_to_cpu(rec->e_blkno),
6602                      le16_to_cpu(el->l_next_free_rec));
6603
6604                 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6605
6606                 if (le16_to_cpu(el->l_tree_depth) == 0) {
6607                         /*
6608                          * If the leaf block contains a single empty
6609                          * extent and no records, we can just remove
6610                          * the block.
6611                          */
6612                         if (i == 0 && ocfs2_is_empty_extent(rec)) {
6613                                 memset(rec, 0,
6614                                        sizeof(struct ocfs2_extent_rec));
6615                                 el->l_next_free_rec = cpu_to_le16(0);
6616
6617                                 goto delete;
6618                         }
6619
6620                         /*
6621                          * Remove any empty extents by shifting things
6622                          * left. That should make life much easier on
6623                          * the code below. This condition is rare
6624                          * enough that we shouldn't see a performance
6625                          * hit.
6626                          */
6627                         if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6628                                 le16_add_cpu(&el->l_next_free_rec, -1);
6629
6630                                 for(i = 0;
6631                                     i < le16_to_cpu(el->l_next_free_rec); i++)
6632                                         el->l_recs[i] = el->l_recs[i + 1];
6633
6634                                 memset(&el->l_recs[i], 0,
6635                                        sizeof(struct ocfs2_extent_rec));
6636
6637                                 /*
6638                                  * We've modified our extent list. The
6639                                  * simplest way to handle this change
6640                                  * is to being the search from the
6641                                  * start again.
6642                                  */
6643                                 goto find_tail_record;
6644                         }
6645
6646                         le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6647
6648                         /*
6649                          * We'll use "new_edge" on our way back up the
6650                          * tree to know what our rightmost cpos is.
6651                          */
6652                         new_edge = le16_to_cpu(rec->e_leaf_clusters);
6653                         new_edge += le32_to_cpu(rec->e_cpos);
6654
6655                         /*
6656                          * The caller will use this to delete data blocks.
6657                          */
6658                         *delete_start = le64_to_cpu(rec->e_blkno)
6659                                 + ocfs2_clusters_to_blocks(inode->i_sb,
6660                                         le16_to_cpu(rec->e_leaf_clusters));
6661
6662                         /*
6663                          * If it's now empty, remove this record.
6664                          */
6665                         if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6666                                 memset(rec, 0,
6667                                        sizeof(struct ocfs2_extent_rec));
6668                                 le16_add_cpu(&el->l_next_free_rec, -1);
6669                         }
6670                 } else {
6671                         if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6672                                 memset(rec, 0,
6673                                        sizeof(struct ocfs2_extent_rec));
6674                                 le16_add_cpu(&el->l_next_free_rec, -1);
6675
6676                                 goto delete;
6677                         }
6678
6679                         /* Can this actually happen? */
6680                         if (le16_to_cpu(el->l_next_free_rec) == 0)
6681                                 goto delete;
6682
6683                         /*
6684                          * We never actually deleted any clusters
6685                          * because our leaf was empty. There's no
6686                          * reason to adjust the rightmost edge then.
6687                          */
6688                         if (new_edge == 0)
6689                                 goto delete;
6690
6691                         rec->e_int_clusters = cpu_to_le32(new_edge);
6692                         le32_add_cpu(&rec->e_int_clusters,
6693                                      -le32_to_cpu(rec->e_cpos));
6694
6695                          /*
6696                           * A deleted child record should have been
6697                           * caught above.
6698                           */
6699                          BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6700                 }
6701
6702 delete:
6703                 ret = ocfs2_journal_dirty(handle, bh);
6704                 if (ret) {
6705                         mlog_errno(ret);
6706                         goto out;
6707                 }
6708
6709                 mlog(0, "extent list container %llu, after: record %d: "
6710                      "(%u, %u, %llu), next = %u.\n",
6711                      (unsigned long long)bh->b_blocknr, i,
6712                      le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6713                      (unsigned long long)le64_to_cpu(rec->e_blkno),
6714                      le16_to_cpu(el->l_next_free_rec));
6715
6716                 /*
6717                  * We must be careful to only attempt delete of an
6718                  * extent block (and not the root inode block).
6719                  */
6720                 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6721                         struct ocfs2_extent_block *eb =
6722                                 (struct ocfs2_extent_block *)bh->b_data;
6723
6724                         /*
6725                          * Save this for use when processing the
6726                          * parent block.
6727                          */
6728                         deleted_eb = le64_to_cpu(eb->h_blkno);
6729
6730                         mlog(0, "deleting this extent block.\n");
6731
6732                         ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
6733
6734                         BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6735                         BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6736                         BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6737
6738                         ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6739                         /* An error here is not fatal. */
6740                         if (ret < 0)
6741                                 mlog_errno(ret);
6742                 } else {
6743                         deleted_eb = 0;
6744                 }
6745
6746                 index--;
6747         }
6748
6749         ret = 0;
6750 out:
6751         return ret;
6752 }
6753
6754 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6755                              unsigned int clusters_to_del,
6756                              struct inode *inode,
6757                              struct buffer_head *fe_bh,
6758                              handle_t *handle,
6759                              struct ocfs2_truncate_context *tc,
6760                              struct ocfs2_path *path)
6761 {
6762         int status;
6763         struct ocfs2_dinode *fe;
6764         struct ocfs2_extent_block *last_eb = NULL;
6765         struct ocfs2_extent_list *el;
6766         struct buffer_head *last_eb_bh = NULL;
6767         u64 delete_blk = 0;
6768
6769         fe = (struct ocfs2_dinode *) fe_bh->b_data;
6770
6771         status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6772                                              path, &last_eb_bh);
6773         if (status < 0) {
6774                 mlog_errno(status);
6775                 goto bail;
6776         }
6777
6778         /*
6779          * Each component will be touched, so we might as well journal
6780          * here to avoid having to handle errors later.
6781          */
6782         status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
6783         if (status < 0) {
6784                 mlog_errno(status);
6785                 goto bail;
6786         }
6787
6788         if (last_eb_bh) {
6789                 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
6790                                                  OCFS2_JOURNAL_ACCESS_WRITE);
6791                 if (status < 0) {
6792                         mlog_errno(status);
6793                         goto bail;
6794                 }
6795
6796                 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6797         }
6798
6799         el = &(fe->id2.i_list);
6800
6801         /*
6802          * Lower levels depend on this never happening, but it's best
6803          * to check it up here before changing the tree.
6804          */
6805         if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6806                 ocfs2_error(inode->i_sb,
6807                             "Inode %lu has an empty extent record, depth %u\n",
6808                             inode->i_ino, le16_to_cpu(el->l_tree_depth));
6809                 status = -EROFS;
6810                 goto bail;
6811         }
6812
6813         vfs_dq_free_space_nodirty(inode,
6814                         ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6815         spin_lock(&OCFS2_I(inode)->ip_lock);
6816         OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6817                                       clusters_to_del;
6818         spin_unlock(&OCFS2_I(inode)->ip_lock);
6819         le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6820         inode->i_blocks = ocfs2_inode_sector_count(inode);
6821
6822         status = ocfs2_trim_tree(inode, path, handle, tc,
6823                                  clusters_to_del, &delete_blk);
6824         if (status) {
6825                 mlog_errno(status);
6826                 goto bail;
6827         }
6828
6829         if (le32_to_cpu(fe->i_clusters) == 0) {
6830                 /* trunc to zero is a special case. */
6831                 el->l_tree_depth = 0;
6832                 fe->i_last_eb_blk = 0;
6833         } else if (last_eb)
6834                 fe->i_last_eb_blk = last_eb->h_blkno;
6835
6836         status = ocfs2_journal_dirty(handle, fe_bh);
6837         if (status < 0) {
6838                 mlog_errno(status);
6839                 goto bail;
6840         }
6841
6842         if (last_eb) {
6843                 /* If there will be a new last extent block, then by
6844                  * definition, there cannot be any leaves to the right of
6845                  * him. */
6846                 last_eb->h_next_leaf_blk = 0;
6847                 status = ocfs2_journal_dirty(handle, last_eb_bh);
6848                 if (status < 0) {
6849                         mlog_errno(status);
6850                         goto bail;
6851                 }
6852         }
6853
6854         if (delete_blk) {
6855                 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6856                                                    clusters_to_del);
6857                 if (status < 0) {
6858                         mlog_errno(status);
6859                         goto bail;
6860                 }
6861         }
6862         status = 0;
6863 bail:
6864         brelse(last_eb_bh);
6865         mlog_exit(status);
6866         return status;
6867 }
6868
6869 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6870 {
6871         set_buffer_uptodate(bh);
6872         mark_buffer_dirty(bh);
6873         return 0;
6874 }
6875
6876 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6877                                      unsigned int from, unsigned int to,
6878                                      struct page *page, int zero, u64 *phys)
6879 {
6880         int ret, partial = 0;
6881
6882         ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6883         if (ret)
6884                 mlog_errno(ret);
6885
6886         if (zero)
6887                 zero_user_segment(page, from, to);
6888
6889         /*
6890          * Need to set the buffers we zero'd into uptodate
6891          * here if they aren't - ocfs2_map_page_blocks()
6892          * might've skipped some
6893          */
6894         ret = walk_page_buffers(handle, page_buffers(page),
6895                                 from, to, &partial,
6896                                 ocfs2_zero_func);
6897         if (ret < 0)
6898                 mlog_errno(ret);
6899         else if (ocfs2_should_order_data(inode)) {
6900                 ret = ocfs2_jbd2_file_inode(handle, inode);
6901                 if (ret < 0)
6902                         mlog_errno(ret);
6903         }
6904
6905         if (!partial)
6906                 SetPageUptodate(page);
6907
6908         flush_dcache_page(page);
6909 }
6910
6911 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6912                                      loff_t end, struct page **pages,
6913                                      int numpages, u64 phys, handle_t *handle)
6914 {
6915         int i;
6916         struct page *page;
6917         unsigned int from, to = PAGE_CACHE_SIZE;
6918         struct super_block *sb = inode->i_sb;
6919
6920         BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6921
6922         if (numpages == 0)
6923                 goto out;
6924
6925         to = PAGE_CACHE_SIZE;
6926         for(i = 0; i < numpages; i++) {
6927                 page = pages[i];
6928
6929                 from = start & (PAGE_CACHE_SIZE - 1);
6930                 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6931                         to = end & (PAGE_CACHE_SIZE - 1);
6932
6933                 BUG_ON(from > PAGE_CACHE_SIZE);
6934                 BUG_ON(to > PAGE_CACHE_SIZE);
6935
6936                 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6937                                          &phys);
6938
6939                 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6940         }
6941 out:
6942         if (pages)
6943                 ocfs2_unlock_and_free_pages(pages, numpages);
6944 }
6945
6946 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6947                                 struct page **pages, int *num)
6948 {
6949         int numpages, ret = 0;
6950         struct super_block *sb = inode->i_sb;
6951         struct address_space *mapping = inode->i_mapping;
6952         unsigned long index;
6953         loff_t last_page_bytes;
6954
6955         BUG_ON(start > end);
6956
6957         BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6958                (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6959
6960         numpages = 0;
6961         last_page_bytes = PAGE_ALIGN(end);
6962         index = start >> PAGE_CACHE_SHIFT;
6963         do {
6964                 pages[numpages] = grab_cache_page(mapping, index);
6965                 if (!pages[numpages]) {
6966                         ret = -ENOMEM;
6967                         mlog_errno(ret);
6968                         goto out;
6969                 }
6970
6971                 numpages++;
6972                 index++;
6973         } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6974
6975 out:
6976         if (ret != 0) {
6977                 if (pages)
6978                         ocfs2_unlock_and_free_pages(pages, numpages);
6979                 numpages = 0;
6980         }
6981
6982         *num = numpages;
6983
6984         return ret;
6985 }
6986
6987 /*
6988  * Zero the area past i_size but still within an allocated
6989  * cluster. This avoids exposing nonzero data on subsequent file
6990  * extends.
6991  *
6992  * We need to call this before i_size is updated on the inode because
6993  * otherwise block_write_full_page() will skip writeout of pages past
6994  * i_size. The new_i_size parameter is passed for this reason.
6995  */
6996 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6997                                   u64 range_start, u64 range_end)
6998 {
6999         int ret = 0, numpages;
7000         struct page **pages = NULL;
7001         u64 phys;
7002         unsigned int ext_flags;
7003         struct super_block *sb = inode->i_sb;
7004
7005         /*
7006          * File systems which don't support sparse files zero on every
7007          * extend.
7008          */
7009         if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
7010                 return 0;
7011
7012         pages = kcalloc(ocfs2_pages_per_cluster(sb),
7013                         sizeof(struct page *), GFP_NOFS);
7014         if (pages == NULL) {
7015                 ret = -ENOMEM;
7016                 mlog_errno(ret);
7017                 goto out;
7018         }
7019
7020         if (range_start == range_end)
7021                 goto out;
7022
7023         ret = ocfs2_extent_map_get_blocks(inode,
7024                                           range_start >> sb->s_blocksize_bits,
7025                                           &phys, NULL, &ext_flags);
7026         if (ret) {
7027                 mlog_errno(ret);
7028                 goto out;
7029         }
7030
7031         /*
7032          * Tail is a hole, or is marked unwritten. In either case, we
7033          * can count on read and write to return/push zero's.
7034          */
7035         if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
7036                 goto out;
7037
7038         ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7039                                    &numpages);
7040         if (ret) {
7041                 mlog_errno(ret);
7042                 goto out;
7043         }
7044
7045         ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7046                                  numpages, phys, handle);
7047
7048         /*
7049          * Initiate writeout of the pages we zero'd here. We don't
7050          * wait on them - the truncate_inode_pages() call later will
7051          * do that for us.
7052          */
7053         ret = do_sync_mapping_range(inode->i_mapping, range_start,
7054                                     range_end - 1, SYNC_FILE_RANGE_WRITE);
7055         if (ret)
7056                 mlog_errno(ret);
7057
7058 out:
7059         if (pages)
7060                 kfree(pages);
7061
7062         return ret;
7063 }
7064
7065 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7066                                              struct ocfs2_dinode *di)
7067 {
7068         unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
7069         unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
7070
7071         if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7072                 memset(&di->id2, 0, blocksize -
7073                                     offsetof(struct ocfs2_dinode, id2) -
7074                                     xattrsize);
7075         else
7076                 memset(&di->id2, 0, blocksize -
7077                                     offsetof(struct ocfs2_dinode, id2));
7078 }
7079
7080 void ocfs2_dinode_new_extent_list(struct inode *inode,
7081                                   struct ocfs2_dinode *di)
7082 {
7083         ocfs2_zero_dinode_id2_with_xattr(inode, di);
7084         di->id2.i_list.l_tree_depth = 0;
7085         di->id2.i_list.l_next_free_rec = 0;
7086         di->id2.i_list.l_count = cpu_to_le16(
7087                 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
7088 }
7089
7090 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7091 {
7092         struct ocfs2_inode_info *oi = OCFS2_I(inode);
7093         struct ocfs2_inline_data *idata = &di->id2.i_data;
7094
7095         spin_lock(&oi->ip_lock);
7096         oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7097         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7098         spin_unlock(&oi->ip_lock);
7099
7100         /*
7101          * We clear the entire i_data structure here so that all
7102          * fields can be properly initialized.
7103          */
7104         ocfs2_zero_dinode_id2_with_xattr(inode, di);
7105
7106         idata->id_count = cpu_to_le16(
7107                         ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
7108 }
7109
7110 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7111                                          struct buffer_head *di_bh)
7112 {
7113         int ret, i, has_data, num_pages = 0;
7114         handle_t *handle;
7115         u64 uninitialized_var(block);
7116         struct ocfs2_inode_info *oi = OCFS2_I(inode);
7117         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7118         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7119         struct ocfs2_alloc_context *data_ac = NULL;
7120         struct page **pages = NULL;
7121         loff_t end = osb->s_clustersize;
7122         struct ocfs2_extent_tree et;
7123         int did_quota = 0;
7124
7125         has_data = i_size_read(inode) ? 1 : 0;
7126
7127         if (has_data) {
7128                 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7129                                 sizeof(struct page *), GFP_NOFS);
7130                 if (pages == NULL) {
7131                         ret = -ENOMEM;
7132                         mlog_errno(ret);
7133                         goto out;
7134                 }
7135
7136                 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7137                 if (ret) {
7138                         mlog_errno(ret);
7139                         goto out;
7140                 }
7141         }
7142
7143         handle = ocfs2_start_trans(osb,
7144                                    ocfs2_inline_to_extents_credits(osb->sb));
7145         if (IS_ERR(handle)) {
7146                 ret = PTR_ERR(handle);
7147                 mlog_errno(ret);
7148                 goto out_unlock;
7149         }
7150
7151         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7152                                       OCFS2_JOURNAL_ACCESS_WRITE);
7153         if (ret) {
7154                 mlog_errno(ret);
7155                 goto out_commit;
7156         }
7157
7158         if (has_data) {
7159                 u32 bit_off, num;
7160                 unsigned int page_end;
7161                 u64 phys;
7162
7163                 if (vfs_dq_alloc_space_nodirty(inode,
7164                                        ocfs2_clusters_to_bytes(osb->sb, 1))) {
7165                         ret = -EDQUOT;
7166                         goto out_commit;
7167                 }
7168                 did_quota = 1;
7169
7170                 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7171                                            &num);
7172                 if (ret) {
7173                         mlog_errno(ret);
7174                         goto out_commit;
7175                 }
7176
7177                 /*
7178                  * Save two copies, one for insert, and one that can
7179                  * be changed by ocfs2_map_and_dirty_page() below.
7180                  */
7181                 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7182
7183                 /*
7184                  * Non sparse file systems zero on extend, so no need
7185                  * to do that now.
7186                  */
7187                 if (!ocfs2_sparse_alloc(osb) &&
7188                     PAGE_CACHE_SIZE < osb->s_clustersize)
7189                         end = PAGE_CACHE_SIZE;
7190
7191                 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7192                 if (ret) {
7193                         mlog_errno(ret);
7194                         goto out_commit;
7195                 }
7196
7197                 /*
7198                  * This should populate the 1st page for us and mark
7199                  * it up to date.
7200                  */
7201                 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7202                 if (ret) {
7203                         mlog_errno(ret);
7204                         goto out_commit;
7205                 }
7206
7207                 page_end = PAGE_CACHE_SIZE;
7208                 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7209                         page_end = osb->s_clustersize;
7210
7211                 for (i = 0; i < num_pages; i++)
7212                         ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7213                                                  pages[i], i > 0, &phys);
7214         }
7215
7216         spin_lock(&oi->ip_lock);
7217         oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7218         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7219         spin_unlock(&oi->ip_lock);
7220
7221         ocfs2_dinode_new_extent_list(inode, di);
7222
7223         ocfs2_journal_dirty(handle, di_bh);
7224
7225         if (has_data) {
7226                 /*
7227                  * An error at this point should be extremely rare. If
7228                  * this proves to be false, we could always re-build
7229                  * the in-inode data from our pages.
7230                  */
7231                 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
7232                 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7233                                           0, block, 1, 0, NULL);
7234                 if (ret) {
7235                         mlog_errno(ret);
7236                         goto out_commit;
7237                 }
7238
7239                 inode->i_blocks = ocfs2_inode_sector_count(inode);
7240         }
7241
7242 out_commit:
7243         if (ret < 0 && did_quota)
7244                 vfs_dq_free_space_nodirty(inode,
7245                                           ocfs2_clusters_to_bytes(osb->sb, 1));
7246
7247         ocfs2_commit_trans(osb, handle);
7248
7249 out_unlock:
7250         if (data_ac)
7251                 ocfs2_free_alloc_context(data_ac);
7252
7253 out:
7254         if (pages) {
7255                 ocfs2_unlock_and_free_pages(pages, num_pages);
7256                 kfree(pages);
7257         }
7258
7259         return ret;
7260 }
7261
7262 /*
7263  * It is expected, that by the time you call this function,
7264  * inode->i_size and fe->i_size have been adjusted.
7265  *
7266  * WARNING: This will kfree the truncate context
7267  */
7268 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7269                           struct inode *inode,
7270                           struct buffer_head *fe_bh,
7271                           struct ocfs2_truncate_context *tc)
7272 {
7273         int status, i, credits, tl_sem = 0;
7274         u32 clusters_to_del, new_highest_cpos, range;
7275         struct ocfs2_extent_list *el;
7276         handle_t *handle = NULL;
7277         struct inode *tl_inode = osb->osb_tl_inode;
7278         struct ocfs2_path *path = NULL;
7279         struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7280
7281         mlog_entry_void();
7282
7283         new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7284                                                      i_size_read(inode));
7285
7286         path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7287                               ocfs2_journal_access_di);
7288         if (!path) {
7289                 status = -ENOMEM;
7290                 mlog_errno(status);
7291                 goto bail;
7292         }
7293
7294         ocfs2_extent_map_trunc(inode, new_highest_cpos);
7295
7296 start:
7297         /*
7298          * Check that we still have allocation to delete.
7299          */
7300         if (OCFS2_I(inode)->ip_clusters == 0) {
7301                 status = 0;
7302                 goto bail;
7303         }
7304
7305         /*
7306          * Truncate always works against the rightmost tree branch.
7307          */
7308         status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7309         if (status) {
7310                 mlog_errno(status);
7311                 goto bail;
7312         }
7313
7314         mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7315              OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7316
7317         /*
7318          * By now, el will point to the extent list on the bottom most
7319          * portion of this tree. Only the tail record is considered in
7320          * each pass.
7321          *
7322          * We handle the following cases, in order:
7323          * - empty extent: delete the remaining branch
7324          * - remove the entire record
7325          * - remove a partial record
7326          * - no record needs to be removed (truncate has completed)
7327          */
7328         el = path_leaf_el(path);
7329         if (le16_to_cpu(el->l_next_free_rec) == 0) {
7330                 ocfs2_error(inode->i_sb,
7331                             "Inode %llu has empty extent block at %llu\n",
7332                             (unsigned long long)OCFS2_I(inode)->ip_blkno,
7333                             (unsigned long long)path_leaf_bh(path)->b_blocknr);
7334                 status = -EROFS;
7335                 goto bail;
7336         }
7337
7338         i = le16_to_cpu(el->l_next_free_rec) - 1;
7339         range = le32_to_cpu(el->l_recs[i].e_cpos) +
7340                 ocfs2_rec_clusters(el, &el->l_recs[i]);
7341         if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7342                 clusters_to_del = 0;
7343         } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7344                 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7345         } else if (range > new_highest_cpos) {
7346                 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7347                                    le32_to_cpu(el->l_recs[i].e_cpos)) -
7348                                   new_highest_cpos;
7349         } else {
7350                 status = 0;
7351                 goto bail;
7352         }
7353
7354         mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7355              clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7356
7357         mutex_lock(&tl_inode->i_mutex);
7358         tl_sem = 1;
7359         /* ocfs2_truncate_log_needs_flush guarantees us at least one
7360          * record is free for use. If there isn't any, we flush to get
7361          * an empty truncate log.  */
7362         if (ocfs2_truncate_log_needs_flush(osb)) {
7363                 status = __ocfs2_flush_truncate_log(osb);
7364                 if (status < 0) {
7365                         mlog_errno(status);
7366                         goto bail;
7367                 }
7368         }
7369
7370         credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7371                                                 (struct ocfs2_dinode *)fe_bh->b_data,
7372                                                 el);
7373         handle = ocfs2_start_trans(osb, credits);
7374         if (IS_ERR(handle)) {
7375                 status = PTR_ERR(handle);
7376                 handle = NULL;
7377                 mlog_errno(status);
7378                 goto bail;
7379         }
7380
7381         status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7382                                    tc, path);
7383         if (status < 0) {
7384                 mlog_errno(status);
7385                 goto bail;
7386         }
7387
7388         mutex_unlock(&tl_inode->i_mutex);
7389         tl_sem = 0;
7390
7391         ocfs2_commit_trans(osb, handle);
7392         handle = NULL;
7393
7394         ocfs2_reinit_path(path, 1);
7395
7396         /*
7397          * The check above will catch the case where we've truncated
7398          * away all allocation.
7399          */
7400         goto start;
7401
7402 bail:
7403
7404         ocfs2_schedule_truncate_log_flush(osb, 1);
7405
7406         if (tl_sem)
7407                 mutex_unlock(&tl_inode->i_mutex);
7408
7409         if (handle)
7410                 ocfs2_commit_trans(osb, handle);
7411
7412         ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7413
7414         ocfs2_free_path(path);
7415
7416         /* This will drop the ext_alloc cluster lock for us */
7417         ocfs2_free_truncate_context(tc);
7418
7419         mlog_exit(status);
7420         return status;
7421 }
7422
7423 /*
7424  * Expects the inode to already be locked.
7425  */
7426 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7427                            struct inode *inode,
7428                            struct buffer_head *fe_bh,
7429                            struct ocfs2_truncate_context **tc)
7430 {
7431         int status;
7432         unsigned int new_i_clusters;
7433         struct ocfs2_dinode *fe;
7434         struct ocfs2_extent_block *eb;
7435         struct buffer_head *last_eb_bh = NULL;
7436
7437         mlog_entry_void();
7438
7439         *tc = NULL;
7440
7441         new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7442                                                   i_size_read(inode));
7443         fe = (struct ocfs2_dinode *) fe_bh->b_data;
7444
7445         mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7446              "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7447              (unsigned long long)le64_to_cpu(fe->i_size));
7448
7449         *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7450         if (!(*tc)) {
7451                 status = -ENOMEM;
7452                 mlog_errno(status);
7453                 goto bail;
7454         }
7455         ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7456
7457         if (fe->id2.i_list.l_tree_depth) {
7458                 status = ocfs2_read_extent_block(INODE_CACHE(inode),
7459                                                  le64_to_cpu(fe->i_last_eb_blk),
7460                                                  &last_eb_bh);
7461                 if (status < 0) {
7462                         mlog_errno(status);
7463                         goto bail;
7464                 }
7465                 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7466         }
7467
7468         (*tc)->tc_last_eb_bh = last_eb_bh;
7469
7470         status = 0;
7471 bail:
7472         if (status < 0) {
7473                 if (*tc)
7474                         ocfs2_free_truncate_context(*tc);
7475                 *tc = NULL;
7476         }
7477         mlog_exit_void();
7478         return status;
7479 }
7480
7481 /*
7482  * 'start' is inclusive, 'end' is not.
7483  */
7484 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7485                           unsigned int start, unsigned int end, int trunc)
7486 {
7487         int ret;
7488         unsigned int numbytes;
7489         handle_t *handle;
7490         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7491         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7492         struct ocfs2_inline_data *idata = &di->id2.i_data;
7493
7494         if (end > i_size_read(inode))
7495                 end = i_size_read(inode);
7496
7497         BUG_ON(start >= end);
7498
7499         if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7500             !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7501             !ocfs2_supports_inline_data(osb)) {
7502                 ocfs2_error(inode->i_sb,
7503                             "Inline data flags for inode %llu don't agree! "
7504                             "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7505                             (unsigned long long)OCFS2_I(inode)->ip_blkno,
7506                             le16_to_cpu(di->i_dyn_features),
7507                             OCFS2_I(inode)->ip_dyn_features,
7508                             osb->s_feature_incompat);
7509                 ret = -EROFS;
7510                 goto out;
7511         }
7512
7513         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7514         if (IS_ERR(handle)) {
7515                 ret = PTR_ERR(handle);
7516                 mlog_errno(ret);
7517                 goto out;
7518         }
7519
7520         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7521                                       OCFS2_JOURNAL_ACCESS_WRITE);
7522         if (ret) {
7523                 mlog_errno(ret);
7524                 goto out_commit;
7525         }
7526
7527         numbytes = end - start;
7528         memset(idata->id_data + start, 0, numbytes);
7529
7530         /*
7531          * No need to worry about the data page here - it's been
7532          * truncated already and inline data doesn't need it for
7533          * pushing zero's to disk, so we'll let readpage pick it up
7534          * later.
7535          */
7536         if (trunc) {
7537                 i_size_write(inode, start);
7538                 di->i_size = cpu_to_le64(start);
7539         }
7540
7541         inode->i_blocks = ocfs2_inode_sector_count(inode);
7542         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7543
7544         di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7545         di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7546
7547         ocfs2_journal_dirty(handle, di_bh);
7548
7549 out_commit:
7550         ocfs2_commit_trans(osb, handle);
7551
7552 out:
7553         return ret;
7554 }
7555
7556 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7557 {
7558         /*
7559          * The caller is responsible for completing deallocation
7560          * before freeing the context.
7561          */
7562         if (tc->tc_dealloc.c_first_suballocator != NULL)
7563                 mlog(ML_NOTICE,
7564                      "Truncate completion has non-empty dealloc context\n");
7565
7566         brelse(tc->tc_last_eb_bh);
7567
7568         kfree(tc);
7569 }