~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ocfs2/refcounttree.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/ocfs2/refcounttree.c (Version linux-6.12-rc7) and /fs/ocfs2/refcounttree.c (Version linux-3.10.108)


  1 // SPDX-License-Identifier: GPL-2.0-only       !!   1 /* -*- mode: c; c-basic-offset: 8; -*-
  2 /*                                             !!   2  * vim: noexpandtab sw=8 ts=8 sts=0:
                                                   >>   3  *
  3  * refcounttree.c                                   4  * refcounttree.c
  4  *                                                  5  *
  5  * Copyright (C) 2009 Oracle.  All rights rese      6  * Copyright (C) 2009 Oracle.  All rights reserved.
                                                   >>   7  *
                                                   >>   8  * This program is free software; you can redistribute it and/or
                                                   >>   9  * modify it under the terms of the GNU General Public
                                                   >>  10  * License version 2 as published by the Free Software Foundation.
                                                   >>  11  *
                                                   >>  12  * This program is distributed in the hope that it will be useful,
                                                   >>  13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                                                   >>  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
                                                   >>  15  * General Public License for more details.
  6  */                                                16  */
  7                                                    17 
  8 #include <linux/sort.h>                            18 #include <linux/sort.h>
  9 #include <cluster/masklog.h>                       19 #include <cluster/masklog.h>
 10 #include "ocfs2.h"                                 20 #include "ocfs2.h"
 11 #include "inode.h"                                 21 #include "inode.h"
 12 #include "alloc.h"                                 22 #include "alloc.h"
 13 #include "suballoc.h"                              23 #include "suballoc.h"
 14 #include "journal.h"                               24 #include "journal.h"
 15 #include "uptodate.h"                              25 #include "uptodate.h"
 16 #include "super.h"                                 26 #include "super.h"
 17 #include "buffer_head_io.h"                        27 #include "buffer_head_io.h"
 18 #include "blockcheck.h"                            28 #include "blockcheck.h"
 19 #include "refcounttree.h"                          29 #include "refcounttree.h"
 20 #include "sysfile.h"                               30 #include "sysfile.h"
 21 #include "dlmglue.h"                               31 #include "dlmglue.h"
 22 #include "extent_map.h"                            32 #include "extent_map.h"
 23 #include "aops.h"                                  33 #include "aops.h"
 24 #include "xattr.h"                                 34 #include "xattr.h"
 25 #include "namei.h"                                 35 #include "namei.h"
 26 #include "ocfs2_trace.h"                           36 #include "ocfs2_trace.h"
 27 #include "file.h"                              << 
 28 #include "symlink.h"                           << 
 29                                                    37 
 30 #include <linux/bio.h>                             38 #include <linux/bio.h>
 31 #include <linux/blkdev.h>                          39 #include <linux/blkdev.h>
 32 #include <linux/slab.h>                            40 #include <linux/slab.h>
 33 #include <linux/writeback.h>                       41 #include <linux/writeback.h>
 34 #include <linux/pagevec.h>                         42 #include <linux/pagevec.h>
 35 #include <linux/swap.h>                            43 #include <linux/swap.h>
 36 #include <linux/security.h>                        44 #include <linux/security.h>
 37 #include <linux/fsnotify.h>                        45 #include <linux/fsnotify.h>
 38 #include <linux/quotaops.h>                        46 #include <linux/quotaops.h>
 39 #include <linux/namei.h>                           47 #include <linux/namei.h>
 40 #include <linux/mount.h>                           48 #include <linux/mount.h>
 41 #include <linux/posix_acl.h>                   << 
 42                                                    49 
 43 struct ocfs2_cow_context {                         50 struct ocfs2_cow_context {
 44         struct inode *inode;                       51         struct inode *inode;
                                                   >>  52         struct file *file;
 45         u32 cow_start;                             53         u32 cow_start;
 46         u32 cow_len;                               54         u32 cow_len;
 47         struct ocfs2_extent_tree data_et;          55         struct ocfs2_extent_tree data_et;
 48         struct ocfs2_refcount_tree *ref_tree;      56         struct ocfs2_refcount_tree *ref_tree;
 49         struct buffer_head *ref_root_bh;           57         struct buffer_head *ref_root_bh;
 50         struct ocfs2_alloc_context *meta_ac;       58         struct ocfs2_alloc_context *meta_ac;
 51         struct ocfs2_alloc_context *data_ac;       59         struct ocfs2_alloc_context *data_ac;
 52         struct ocfs2_cached_dealloc_ctxt deall     60         struct ocfs2_cached_dealloc_ctxt dealloc;
 53         void *cow_object;                          61         void *cow_object;
 54         struct ocfs2_post_refcount *post_refco     62         struct ocfs2_post_refcount *post_refcount;
 55         int extra_credits;                         63         int extra_credits;
 56         int (*get_clusters)(struct ocfs2_cow_c     64         int (*get_clusters)(struct ocfs2_cow_context *context,
 57                             u32 v_cluster, u32     65                             u32 v_cluster, u32 *p_cluster,
 58                             u32 *num_clusters,     66                             u32 *num_clusters,
 59                             unsigned int *exte     67                             unsigned int *extent_flags);
 60         int (*cow_duplicate_clusters)(handle_t     68         int (*cow_duplicate_clusters)(handle_t *handle,
 61                                       struct i !!  69                                       struct file *file,
 62                                       u32 cpos     70                                       u32 cpos, u32 old_cluster,
 63                                       u32 new_     71                                       u32 new_cluster, u32 new_len);
 64 };                                                 72 };
 65                                                    73 
 66 static inline struct ocfs2_refcount_tree *         74 static inline struct ocfs2_refcount_tree *
 67 cache_info_to_refcount(struct ocfs2_caching_in     75 cache_info_to_refcount(struct ocfs2_caching_info *ci)
 68 {                                                  76 {
 69         return container_of(ci, struct ocfs2_r     77         return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
 70 }                                                  78 }
 71                                                    79 
 72 static int ocfs2_validate_refcount_block(struc     80 static int ocfs2_validate_refcount_block(struct super_block *sb,
 73                                          struc     81                                          struct buffer_head *bh)
 74 {                                                  82 {
 75         int rc;                                    83         int rc;
 76         struct ocfs2_refcount_block *rb =          84         struct ocfs2_refcount_block *rb =
 77                 (struct ocfs2_refcount_block *     85                 (struct ocfs2_refcount_block *)bh->b_data;
 78                                                    86 
 79         trace_ocfs2_validate_refcount_block((u     87         trace_ocfs2_validate_refcount_block((unsigned long long)bh->b_blocknr);
 80                                                    88 
 81         BUG_ON(!buffer_uptodate(bh));              89         BUG_ON(!buffer_uptodate(bh));
 82                                                    90 
 83         /*                                         91         /*
 84          * If the ecc fails, we return the err     92          * If the ecc fails, we return the error but otherwise
 85          * leave the filesystem running.  We k     93          * leave the filesystem running.  We know any error is
 86          * local to this block.                    94          * local to this block.
 87          */                                        95          */
 88         rc = ocfs2_validate_meta_ecc(sb, bh->b     96         rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
 89         if (rc) {                                  97         if (rc) {
 90                 mlog(ML_ERROR, "Checksum faile     98                 mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
 91                      (unsigned long long)bh->b     99                      (unsigned long long)bh->b_blocknr);
 92                 return rc;                        100                 return rc;
 93         }                                         101         }
 94                                                   102 
 95                                                   103 
 96         if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)    104         if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
 97                 rc = ocfs2_error(sb,           !! 105                 ocfs2_error(sb,
 98                                  "Refcount blo !! 106                             "Refcount block #%llu has bad signature %.*s",
 99                                  (unsigned lon !! 107                             (unsigned long long)bh->b_blocknr, 7,
100                                  rb->rf_signat !! 108                             rb->rf_signature);
101                 goto out;                      !! 109                 return -EINVAL;
102         }                                         110         }
103                                                   111 
104         if (le64_to_cpu(rb->rf_blkno) != bh->b    112         if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
105                 rc = ocfs2_error(sb,           !! 113                 ocfs2_error(sb,
106                                  "Refcount blo !! 114                             "Refcount block #%llu has an invalid rf_blkno "
107                                  (unsigned lon !! 115                             "of %llu",
108                                  (unsigned lon !! 116                             (unsigned long long)bh->b_blocknr,
109                 goto out;                      !! 117                             (unsigned long long)le64_to_cpu(rb->rf_blkno));
                                                   >> 118                 return -EINVAL;
110         }                                         119         }
111                                                   120 
112         if (le32_to_cpu(rb->rf_fs_generation)     121         if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
113                 rc = ocfs2_error(sb,           !! 122                 ocfs2_error(sb,
114                                  "Refcount blo !! 123                             "Refcount block #%llu has an invalid "
115                                  (unsigned lon !! 124                             "rf_fs_generation of #%u",
116                                  le32_to_cpu(r !! 125                             (unsigned long long)bh->b_blocknr,
117                 goto out;                      !! 126                             le32_to_cpu(rb->rf_fs_generation));
                                                   >> 127                 return -EINVAL;
118         }                                         128         }
119 out:                                           !! 129 
120         return rc;                             !! 130         return 0;
121 }                                                 131 }
122                                                   132 
123 static int ocfs2_read_refcount_block(struct oc    133 static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
124                                      u64 rb_bl    134                                      u64 rb_blkno,
125                                      struct bu    135                                      struct buffer_head **bh)
126 {                                                 136 {
127         int rc;                                   137         int rc;
128         struct buffer_head *tmp = *bh;            138         struct buffer_head *tmp = *bh;
129                                                   139 
130         rc = ocfs2_read_block(ci, rb_blkno, &t    140         rc = ocfs2_read_block(ci, rb_blkno, &tmp,
131                               ocfs2_validate_r    141                               ocfs2_validate_refcount_block);
132                                                   142 
133         /* If ocfs2_read_block() got us a new     143         /* If ocfs2_read_block() got us a new bh, pass it up. */
134         if (!rc && !*bh)                          144         if (!rc && !*bh)
135                 *bh = tmp;                        145                 *bh = tmp;
136                                                   146 
137         return rc;                                147         return rc;
138 }                                                 148 }
139                                                   149 
140 static u64 ocfs2_refcount_cache_owner(struct o    150 static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
141 {                                                 151 {
142         struct ocfs2_refcount_tree *rf = cache    152         struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
143                                                   153 
144         return rf->rf_blkno;                      154         return rf->rf_blkno;
145 }                                                 155 }
146                                                   156 
147 static struct super_block *                       157 static struct super_block *
148 ocfs2_refcount_cache_get_super(struct ocfs2_ca    158 ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
149 {                                                 159 {
150         struct ocfs2_refcount_tree *rf = cache    160         struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
151                                                   161 
152         return rf->rf_sb;                         162         return rf->rf_sb;
153 }                                                 163 }
154                                                   164 
155 static void ocfs2_refcount_cache_lock(struct o    165 static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
156 __acquires(&rf->rf_lock)                       << 
157 {                                                 166 {
158         struct ocfs2_refcount_tree *rf = cache    167         struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
159                                                   168 
160         spin_lock(&rf->rf_lock);                  169         spin_lock(&rf->rf_lock);
161 }                                                 170 }
162                                                   171 
163 static void ocfs2_refcount_cache_unlock(struct    172 static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
164 __releases(&rf->rf_lock)                       << 
165 {                                                 173 {
166         struct ocfs2_refcount_tree *rf = cache    174         struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
167                                                   175 
168         spin_unlock(&rf->rf_lock);                176         spin_unlock(&rf->rf_lock);
169 }                                                 177 }
170                                                   178 
171 static void ocfs2_refcount_cache_io_lock(struc    179 static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
172 {                                                 180 {
173         struct ocfs2_refcount_tree *rf = cache    181         struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
174                                                   182 
175         mutex_lock(&rf->rf_io_mutex);             183         mutex_lock(&rf->rf_io_mutex);
176 }                                                 184 }
177                                                   185 
178 static void ocfs2_refcount_cache_io_unlock(str    186 static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
179 {                                                 187 {
180         struct ocfs2_refcount_tree *rf = cache    188         struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
181                                                   189 
182         mutex_unlock(&rf->rf_io_mutex);           190         mutex_unlock(&rf->rf_io_mutex);
183 }                                                 191 }
184                                                   192 
185 static const struct ocfs2_caching_operations o    193 static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
186         .co_owner               = ocfs2_refcou    194         .co_owner               = ocfs2_refcount_cache_owner,
187         .co_get_super           = ocfs2_refcou    195         .co_get_super           = ocfs2_refcount_cache_get_super,
188         .co_cache_lock          = ocfs2_refcou    196         .co_cache_lock          = ocfs2_refcount_cache_lock,
189         .co_cache_unlock        = ocfs2_refcou    197         .co_cache_unlock        = ocfs2_refcount_cache_unlock,
190         .co_io_lock             = ocfs2_refcou    198         .co_io_lock             = ocfs2_refcount_cache_io_lock,
191         .co_io_unlock           = ocfs2_refcou    199         .co_io_unlock           = ocfs2_refcount_cache_io_unlock,
192 };                                                200 };
193                                                   201 
194 static struct ocfs2_refcount_tree *               202 static struct ocfs2_refcount_tree *
195 ocfs2_find_refcount_tree(struct ocfs2_super *o    203 ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
196 {                                                 204 {
197         struct rb_node *n = osb->osb_rf_lock_t    205         struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
198         struct ocfs2_refcount_tree *tree = NUL    206         struct ocfs2_refcount_tree *tree = NULL;
199                                                   207 
200         while (n) {                               208         while (n) {
201                 tree = rb_entry(n, struct ocfs    209                 tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
202                                                   210 
203                 if (blkno < tree->rf_blkno)       211                 if (blkno < tree->rf_blkno)
204                         n = n->rb_left;           212                         n = n->rb_left;
205                 else if (blkno > tree->rf_blkn    213                 else if (blkno > tree->rf_blkno)
206                         n = n->rb_right;          214                         n = n->rb_right;
207                 else                              215                 else
208                         return tree;              216                         return tree;
209         }                                         217         }
210                                                   218 
211         return NULL;                              219         return NULL;
212 }                                                 220 }
213                                                   221 
214 /* osb_lock is already locked. */                 222 /* osb_lock is already locked. */
215 static void ocfs2_insert_refcount_tree(struct     223 static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
216                                        struct     224                                        struct ocfs2_refcount_tree *new)
217 {                                                 225 {
218         u64 rf_blkno = new->rf_blkno;             226         u64 rf_blkno = new->rf_blkno;
219         struct rb_node *parent = NULL;            227         struct rb_node *parent = NULL;
220         struct rb_node **p = &osb->osb_rf_lock    228         struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
221         struct ocfs2_refcount_tree *tmp;          229         struct ocfs2_refcount_tree *tmp;
222                                                   230 
223         while (*p) {                              231         while (*p) {
224                 parent = *p;                      232                 parent = *p;
225                                                   233 
226                 tmp = rb_entry(parent, struct     234                 tmp = rb_entry(parent, struct ocfs2_refcount_tree,
227                                rf_node);          235                                rf_node);
228                                                   236 
229                 if (rf_blkno < tmp->rf_blkno)     237                 if (rf_blkno < tmp->rf_blkno)
230                         p = &(*p)->rb_left;       238                         p = &(*p)->rb_left;
231                 else if (rf_blkno > tmp->rf_bl    239                 else if (rf_blkno > tmp->rf_blkno)
232                         p = &(*p)->rb_right;      240                         p = &(*p)->rb_right;
233                 else {                            241                 else {
234                         /* This should never h    242                         /* This should never happen! */
235                         mlog(ML_ERROR, "Duplic    243                         mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
236                              (unsigned long lo    244                              (unsigned long long)rf_blkno);
237                         BUG();                    245                         BUG();
238                 }                                 246                 }
239         }                                         247         }
240                                                   248 
241         rb_link_node(&new->rf_node, parent, p)    249         rb_link_node(&new->rf_node, parent, p);
242         rb_insert_color(&new->rf_node, &osb->o    250         rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
243 }                                                 251 }
244                                                   252 
245 static void ocfs2_free_refcount_tree(struct oc    253 static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
246 {                                                 254 {
247         ocfs2_metadata_cache_exit(&tree->rf_ci    255         ocfs2_metadata_cache_exit(&tree->rf_ci);
248         ocfs2_simple_drop_lockres(OCFS2_SB(tre    256         ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
249         ocfs2_lock_res_free(&tree->rf_lockres)    257         ocfs2_lock_res_free(&tree->rf_lockres);
250         kfree(tree);                              258         kfree(tree);
251 }                                                 259 }
252                                                   260 
253 static inline void                                261 static inline void
254 ocfs2_erase_refcount_tree_from_list_no_lock(st    262 ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
255                                         struct    263                                         struct ocfs2_refcount_tree *tree)
256 {                                                 264 {
257         rb_erase(&tree->rf_node, &osb->osb_rf_    265         rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
258         if (osb->osb_ref_tree_lru && osb->osb_    266         if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
259                 osb->osb_ref_tree_lru = NULL;     267                 osb->osb_ref_tree_lru = NULL;
260 }                                                 268 }
261                                                   269 
262 static void ocfs2_erase_refcount_tree_from_lis    270 static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
263                                         struct    271                                         struct ocfs2_refcount_tree *tree)
264 {                                                 272 {
265         spin_lock(&osb->osb_lock);                273         spin_lock(&osb->osb_lock);
266         ocfs2_erase_refcount_tree_from_list_no    274         ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
267         spin_unlock(&osb->osb_lock);              275         spin_unlock(&osb->osb_lock);
268 }                                                 276 }
269                                                   277 
270 static void ocfs2_kref_remove_refcount_tree(st    278 static void ocfs2_kref_remove_refcount_tree(struct kref *kref)
271 {                                                 279 {
272         struct ocfs2_refcount_tree *tree =        280         struct ocfs2_refcount_tree *tree =
273                 container_of(kref, struct ocfs    281                 container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
274                                                   282 
275         ocfs2_free_refcount_tree(tree);           283         ocfs2_free_refcount_tree(tree);
276 }                                                 284 }
277                                                   285 
278 static inline void                                286 static inline void
279 ocfs2_refcount_tree_get(struct ocfs2_refcount_    287 ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
280 {                                                 288 {
281         kref_get(&tree->rf_getcnt);               289         kref_get(&tree->rf_getcnt);
282 }                                                 290 }
283                                                   291 
284 static inline void                                292 static inline void
285 ocfs2_refcount_tree_put(struct ocfs2_refcount_    293 ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
286 {                                                 294 {
287         kref_put(&tree->rf_getcnt, ocfs2_kref_    295         kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
288 }                                                 296 }
289                                                   297 
290 static inline void ocfs2_init_refcount_tree_ci    298 static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
291                                                   299                                                struct super_block *sb)
292 {                                                 300 {
293         ocfs2_metadata_cache_init(&new->rf_ci,    301         ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
294         mutex_init(&new->rf_io_mutex);            302         mutex_init(&new->rf_io_mutex);
295         new->rf_sb = sb;                          303         new->rf_sb = sb;
296         spin_lock_init(&new->rf_lock);            304         spin_lock_init(&new->rf_lock);
297 }                                                 305 }
298                                                   306 
299 static inline void ocfs2_init_refcount_tree_lo    307 static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
300                                         struct    308                                         struct ocfs2_refcount_tree *new,
301                                         u64 rf    309                                         u64 rf_blkno, u32 generation)
302 {                                                 310 {
303         init_rwsem(&new->rf_sem);                 311         init_rwsem(&new->rf_sem);
304         ocfs2_refcount_lock_res_init(&new->rf_    312         ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
305                                      rf_blkno,    313                                      rf_blkno, generation);
306 }                                                 314 }
307                                                   315 
308 static struct ocfs2_refcount_tree*                316 static struct ocfs2_refcount_tree*
309 ocfs2_allocate_refcount_tree(struct ocfs2_supe    317 ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
310 {                                                 318 {
311         struct ocfs2_refcount_tree *new;          319         struct ocfs2_refcount_tree *new;
312                                                   320 
313         new = kzalloc(sizeof(struct ocfs2_refc    321         new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
314         if (!new)                                 322         if (!new)
315                 return NULL;                      323                 return NULL;
316                                                   324 
317         new->rf_blkno = rf_blkno;                 325         new->rf_blkno = rf_blkno;
318         kref_init(&new->rf_getcnt);               326         kref_init(&new->rf_getcnt);
319         ocfs2_init_refcount_tree_ci(new, osb->    327         ocfs2_init_refcount_tree_ci(new, osb->sb);
320                                                   328 
321         return new;                               329         return new;
322 }                                                 330 }
323                                                   331 
324 static int ocfs2_get_refcount_tree(struct ocfs    332 static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
325                                    struct ocfs    333                                    struct ocfs2_refcount_tree **ret_tree)
326 {                                                 334 {
327         int ret = 0;                              335         int ret = 0;
328         struct ocfs2_refcount_tree *tree, *new    336         struct ocfs2_refcount_tree *tree, *new = NULL;
329         struct buffer_head *ref_root_bh = NULL    337         struct buffer_head *ref_root_bh = NULL;
330         struct ocfs2_refcount_block *ref_rb;      338         struct ocfs2_refcount_block *ref_rb;
331                                                   339 
332         spin_lock(&osb->osb_lock);                340         spin_lock(&osb->osb_lock);
333         if (osb->osb_ref_tree_lru &&              341         if (osb->osb_ref_tree_lru &&
334             osb->osb_ref_tree_lru->rf_blkno ==    342             osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
335                 tree = osb->osb_ref_tree_lru;     343                 tree = osb->osb_ref_tree_lru;
336         else                                      344         else
337                 tree = ocfs2_find_refcount_tre    345                 tree = ocfs2_find_refcount_tree(osb, rf_blkno);
338         if (tree)                                 346         if (tree)
339                 goto out;                         347                 goto out;
340                                                   348 
341         spin_unlock(&osb->osb_lock);              349         spin_unlock(&osb->osb_lock);
342                                                   350 
343         new = ocfs2_allocate_refcount_tree(osb    351         new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
344         if (!new) {                               352         if (!new) {
345                 ret = -ENOMEM;                    353                 ret = -ENOMEM;
346                 mlog_errno(ret);                  354                 mlog_errno(ret);
347                 return ret;                       355                 return ret;
348         }                                         356         }
349         /*                                        357         /*
350          * We need the generation to create th    358          * We need the generation to create the refcount tree lock and since
351          * it isn't changed during the tree mo    359          * it isn't changed during the tree modification, we are safe here to
352          * read without protection.               360          * read without protection.
353          * We also have to purge the cache aft    361          * We also have to purge the cache after we create the lock since the
354          * refcount block may have the stale d    362          * refcount block may have the stale data. It can only be trusted when
355          * we hold the refcount lock.             363          * we hold the refcount lock.
356          */                                       364          */
357         ret = ocfs2_read_refcount_block(&new->    365         ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
358         if (ret) {                                366         if (ret) {
359                 mlog_errno(ret);                  367                 mlog_errno(ret);
360                 ocfs2_metadata_cache_exit(&new    368                 ocfs2_metadata_cache_exit(&new->rf_ci);
361                 kfree(new);                       369                 kfree(new);
362                 return ret;                       370                 return ret;
363         }                                         371         }
364                                                   372 
365         ref_rb = (struct ocfs2_refcount_block     373         ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
366         new->rf_generation = le32_to_cpu(ref_r    374         new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
367         ocfs2_init_refcount_tree_lock(osb, new    375         ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
368                                       new->rf_    376                                       new->rf_generation);
369         ocfs2_metadata_cache_purge(&new->rf_ci    377         ocfs2_metadata_cache_purge(&new->rf_ci);
370                                                   378 
371         spin_lock(&osb->osb_lock);                379         spin_lock(&osb->osb_lock);
372         tree = ocfs2_find_refcount_tree(osb, r    380         tree = ocfs2_find_refcount_tree(osb, rf_blkno);
373         if (tree)                                 381         if (tree)
374                 goto out;                         382                 goto out;
375                                                   383 
376         ocfs2_insert_refcount_tree(osb, new);     384         ocfs2_insert_refcount_tree(osb, new);
377                                                   385 
378         tree = new;                               386         tree = new;
379         new = NULL;                               387         new = NULL;
380                                                   388 
381 out:                                              389 out:
382         *ret_tree = tree;                         390         *ret_tree = tree;
383                                                   391 
384         osb->osb_ref_tree_lru = tree;             392         osb->osb_ref_tree_lru = tree;
385                                                   393 
386         spin_unlock(&osb->osb_lock);              394         spin_unlock(&osb->osb_lock);
387                                                   395 
388         if (new)                                  396         if (new)
389                 ocfs2_free_refcount_tree(new);    397                 ocfs2_free_refcount_tree(new);
390                                                   398 
391         brelse(ref_root_bh);                      399         brelse(ref_root_bh);
392         return ret;                               400         return ret;
393 }                                                 401 }
394                                                   402 
395 static int ocfs2_get_refcount_block(struct ino    403 static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
396 {                                                 404 {
397         int ret;                                  405         int ret;
398         struct buffer_head *di_bh = NULL;         406         struct buffer_head *di_bh = NULL;
399         struct ocfs2_dinode *di;                  407         struct ocfs2_dinode *di;
400                                                   408 
401         ret = ocfs2_read_inode_block(inode, &d    409         ret = ocfs2_read_inode_block(inode, &di_bh);
402         if (ret) {                                410         if (ret) {
403                 mlog_errno(ret);                  411                 mlog_errno(ret);
404                 goto out;                         412                 goto out;
405         }                                         413         }
406                                                   414 
407         BUG_ON(!ocfs2_is_refcount_inode(inode) !! 415         BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
408                                                   416 
409         di = (struct ocfs2_dinode *)di_bh->b_d    417         di = (struct ocfs2_dinode *)di_bh->b_data;
410         *ref_blkno = le64_to_cpu(di->i_refcoun    418         *ref_blkno = le64_to_cpu(di->i_refcount_loc);
411         brelse(di_bh);                            419         brelse(di_bh);
412 out:                                              420 out:
413         return ret;                               421         return ret;
414 }                                                 422 }
415                                                   423 
416 static int __ocfs2_lock_refcount_tree(struct o    424 static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
417                                       struct o    425                                       struct ocfs2_refcount_tree *tree, int rw)
418 {                                                 426 {
419         int ret;                                  427         int ret;
420                                                   428 
421         ret = ocfs2_refcount_lock(tree, rw);      429         ret = ocfs2_refcount_lock(tree, rw);
422         if (ret) {                                430         if (ret) {
423                 mlog_errno(ret);                  431                 mlog_errno(ret);
424                 goto out;                         432                 goto out;
425         }                                         433         }
426                                                   434 
427         if (rw)                                   435         if (rw)
428                 down_write(&tree->rf_sem);        436                 down_write(&tree->rf_sem);
429         else                                      437         else
430                 down_read(&tree->rf_sem);         438                 down_read(&tree->rf_sem);
431                                                   439 
432 out:                                              440 out:
433         return ret;                               441         return ret;
434 }                                                 442 }
435                                                   443 
436 /*                                                444 /*
437  * Lock the refcount tree pointed by ref_blkno    445  * Lock the refcount tree pointed by ref_blkno and return the tree.
438  * In most case, we lock the tree and read the    446  * In most case, we lock the tree and read the refcount block.
439  * So read it here if the caller really needs     447  * So read it here if the caller really needs it.
440  *                                                448  *
441  * If the tree has been re-created by other no    449  * If the tree has been re-created by other node, it will free the
442  * old one and re-create it.                      450  * old one and re-create it.
443  */                                               451  */
444 int ocfs2_lock_refcount_tree(struct ocfs2_supe    452 int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
445                              u64 ref_blkno, in    453                              u64 ref_blkno, int rw,
446                              struct ocfs2_refc    454                              struct ocfs2_refcount_tree **ret_tree,
447                              struct buffer_hea    455                              struct buffer_head **ref_bh)
448 {                                                 456 {
449         int ret, delete_tree = 0;                 457         int ret, delete_tree = 0;
450         struct ocfs2_refcount_tree *tree = NUL    458         struct ocfs2_refcount_tree *tree = NULL;
451         struct buffer_head *ref_root_bh = NULL    459         struct buffer_head *ref_root_bh = NULL;
452         struct ocfs2_refcount_block *rb;          460         struct ocfs2_refcount_block *rb;
453                                                   461 
454 again:                                            462 again:
455         ret = ocfs2_get_refcount_tree(osb, ref    463         ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
456         if (ret) {                                464         if (ret) {
457                 mlog_errno(ret);                  465                 mlog_errno(ret);
458                 return ret;                       466                 return ret;
459         }                                         467         }
460                                                   468 
461         ocfs2_refcount_tree_get(tree);            469         ocfs2_refcount_tree_get(tree);
462                                                   470 
463         ret = __ocfs2_lock_refcount_tree(osb,     471         ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
464         if (ret) {                                472         if (ret) {
465                 mlog_errno(ret);                  473                 mlog_errno(ret);
466                 ocfs2_refcount_tree_put(tree);    474                 ocfs2_refcount_tree_put(tree);
467                 goto out;                         475                 goto out;
468         }                                         476         }
469                                                   477 
470         ret = ocfs2_read_refcount_block(&tree-    478         ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
471                                         &ref_r    479                                         &ref_root_bh);
472         if (ret) {                                480         if (ret) {
473                 mlog_errno(ret);                  481                 mlog_errno(ret);
474                 ocfs2_unlock_refcount_tree(osb    482                 ocfs2_unlock_refcount_tree(osb, tree, rw);
                                                   >> 483                 ocfs2_refcount_tree_put(tree);
475                 goto out;                         484                 goto out;
476         }                                         485         }
477                                                   486 
478         rb = (struct ocfs2_refcount_block *)re    487         rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
479         /*                                        488         /*
480          * If the refcount block has been free    489          * If the refcount block has been freed and re-created, we may need
481          * to recreate the refcount tree also.    490          * to recreate the refcount tree also.
482          *                                        491          *
483          * Here we just remove the tree from t    492          * Here we just remove the tree from the rb-tree, and the last
484          * kref holder will unlock and delete     493          * kref holder will unlock and delete this refcount_tree.
485          * Then we goto "again" and ocfs2_get_    494          * Then we goto "again" and ocfs2_get_refcount_tree will create
486          * the new refcount tree for us.          495          * the new refcount tree for us.
487          */                                       496          */
488         if (tree->rf_generation != le32_to_cpu    497         if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
489                 if (!tree->rf_removed) {          498                 if (!tree->rf_removed) {
490                         ocfs2_erase_refcount_t    499                         ocfs2_erase_refcount_tree_from_list(osb, tree);
491                         tree->rf_removed = 1;     500                         tree->rf_removed = 1;
492                         delete_tree = 1;          501                         delete_tree = 1;
493                 }                                 502                 }
494                                                   503 
495                 ocfs2_unlock_refcount_tree(osb    504                 ocfs2_unlock_refcount_tree(osb, tree, rw);
496                 /*                                505                 /*
497                  * We get an extra reference w    506                  * We get an extra reference when we create the refcount
498                  * tree, so another put will d    507                  * tree, so another put will destroy it.
499                  */                               508                  */
500                 if (delete_tree)                  509                 if (delete_tree)
501                         ocfs2_refcount_tree_pu    510                         ocfs2_refcount_tree_put(tree);
502                 brelse(ref_root_bh);              511                 brelse(ref_root_bh);
503                 ref_root_bh = NULL;               512                 ref_root_bh = NULL;
504                 goto again;                       513                 goto again;
505         }                                         514         }
506                                                   515 
507         *ret_tree = tree;                         516         *ret_tree = tree;
508         if (ref_bh) {                             517         if (ref_bh) {
509                 *ref_bh = ref_root_bh;            518                 *ref_bh = ref_root_bh;
510                 ref_root_bh = NULL;               519                 ref_root_bh = NULL;
511         }                                         520         }
512 out:                                              521 out:
513         brelse(ref_root_bh);                      522         brelse(ref_root_bh);
514         return ret;                               523         return ret;
515 }                                                 524 }
516                                                   525 
517 void ocfs2_unlock_refcount_tree(struct ocfs2_s    526 void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
518                                 struct ocfs2_r    527                                 struct ocfs2_refcount_tree *tree, int rw)
519 {                                                 528 {
520         if (rw)                                   529         if (rw)
521                 up_write(&tree->rf_sem);          530                 up_write(&tree->rf_sem);
522         else                                      531         else
523                 up_read(&tree->rf_sem);           532                 up_read(&tree->rf_sem);
524                                                   533 
525         ocfs2_refcount_unlock(tree, rw);          534         ocfs2_refcount_unlock(tree, rw);
526         ocfs2_refcount_tree_put(tree);            535         ocfs2_refcount_tree_put(tree);
527 }                                                 536 }
528                                                   537 
529 void ocfs2_purge_refcount_trees(struct ocfs2_s    538 void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
530 {                                                 539 {
531         struct rb_node *node;                     540         struct rb_node *node;
532         struct ocfs2_refcount_tree *tree;         541         struct ocfs2_refcount_tree *tree;
533         struct rb_root *root = &osb->osb_rf_lo    542         struct rb_root *root = &osb->osb_rf_lock_tree;
534                                                   543 
535         while ((node = rb_last(root)) != NULL)    544         while ((node = rb_last(root)) != NULL) {
536                 tree = rb_entry(node, struct o    545                 tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
537                                                   546 
538                 trace_ocfs2_purge_refcount_tre    547                 trace_ocfs2_purge_refcount_trees(
539                                 (unsigned long    548                                 (unsigned long long) tree->rf_blkno);
540                                                   549 
541                 rb_erase(&tree->rf_node, root)    550                 rb_erase(&tree->rf_node, root);
542                 ocfs2_free_refcount_tree(tree)    551                 ocfs2_free_refcount_tree(tree);
543         }                                         552         }
544 }                                                 553 }
545                                                   554 
546 /*                                                555 /*
547  * Create a refcount tree for an inode.           556  * Create a refcount tree for an inode.
548  * We take for granted that the inode is alrea    557  * We take for granted that the inode is already locked.
549  */                                               558  */
550 static int ocfs2_create_refcount_tree(struct i    559 static int ocfs2_create_refcount_tree(struct inode *inode,
551                                       struct b    560                                       struct buffer_head *di_bh)
552 {                                                 561 {
553         int ret;                                  562         int ret;
554         handle_t *handle = NULL;                  563         handle_t *handle = NULL;
555         struct ocfs2_alloc_context *meta_ac =     564         struct ocfs2_alloc_context *meta_ac = NULL;
556         struct ocfs2_dinode *di = (struct ocfs    565         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
557         struct ocfs2_inode_info *oi = OCFS2_I(    566         struct ocfs2_inode_info *oi = OCFS2_I(inode);
558         struct ocfs2_super *osb = OCFS2_SB(ino    567         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
559         struct buffer_head *new_bh = NULL;        568         struct buffer_head *new_bh = NULL;
560         struct ocfs2_refcount_block *rb;          569         struct ocfs2_refcount_block *rb;
561         struct ocfs2_refcount_tree *new_tree =    570         struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
562         u16 suballoc_bit_start;                   571         u16 suballoc_bit_start;
563         u32 num_got;                              572         u32 num_got;
564         u64 suballoc_loc, first_blkno;            573         u64 suballoc_loc, first_blkno;
565                                                   574 
566         BUG_ON(ocfs2_is_refcount_inode(inode)) !! 575         BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
567                                                   576 
568         trace_ocfs2_create_refcount_tree(         577         trace_ocfs2_create_refcount_tree(
569                 (unsigned long long)oi->ip_blk !! 578                 (unsigned long long)OCFS2_I(inode)->ip_blkno);
570                                                   579 
571         ret = ocfs2_reserve_new_metadata_block    580         ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
572         if (ret) {                                581         if (ret) {
573                 mlog_errno(ret);                  582                 mlog_errno(ret);
574                 goto out;                         583                 goto out;
575         }                                         584         }
576                                                   585 
577         handle = ocfs2_start_trans(osb, OCFS2_    586         handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
578         if (IS_ERR(handle)) {                     587         if (IS_ERR(handle)) {
579                 ret = PTR_ERR(handle);            588                 ret = PTR_ERR(handle);
580                 mlog_errno(ret);                  589                 mlog_errno(ret);
581                 goto out;                         590                 goto out;
582         }                                         591         }
583                                                   592 
584         ret = ocfs2_journal_access_di(handle,     593         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
585                                       OCFS2_JO    594                                       OCFS2_JOURNAL_ACCESS_WRITE);
586         if (ret) {                                595         if (ret) {
587                 mlog_errno(ret);                  596                 mlog_errno(ret);
588                 goto out_commit;                  597                 goto out_commit;
589         }                                         598         }
590                                                   599 
591         ret = ocfs2_claim_metadata(handle, met    600         ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
592                                    &suballoc_b    601                                    &suballoc_bit_start, &num_got,
593                                    &first_blkn    602                                    &first_blkno);
594         if (ret) {                                603         if (ret) {
595                 mlog_errno(ret);                  604                 mlog_errno(ret);
596                 goto out_commit;                  605                 goto out_commit;
597         }                                         606         }
598                                                   607 
599         new_tree = ocfs2_allocate_refcount_tre    608         new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
600         if (!new_tree) {                          609         if (!new_tree) {
601                 ret = -ENOMEM;                    610                 ret = -ENOMEM;
602                 mlog_errno(ret);                  611                 mlog_errno(ret);
603                 goto out_commit;                  612                 goto out_commit;
604         }                                         613         }
605                                                   614 
606         new_bh = sb_getblk(inode->i_sb, first_    615         new_bh = sb_getblk(inode->i_sb, first_blkno);
607         if (!new_bh) {                         << 
608                 ret = -ENOMEM;                 << 
609                 mlog_errno(ret);               << 
610                 goto out_commit;               << 
611         }                                      << 
612         ocfs2_set_new_buffer_uptodate(&new_tre    616         ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
613                                                   617 
614         ret = ocfs2_journal_access_rb(handle,     618         ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
615                                       OCFS2_JO    619                                       OCFS2_JOURNAL_ACCESS_CREATE);
616         if (ret) {                                620         if (ret) {
617                 mlog_errno(ret);                  621                 mlog_errno(ret);
618                 goto out_commit;                  622                 goto out_commit;
619         }                                         623         }
620                                                   624 
621         /* Initialize ocfs2_refcount_block. */    625         /* Initialize ocfs2_refcount_block. */
622         rb = (struct ocfs2_refcount_block *)ne    626         rb = (struct ocfs2_refcount_block *)new_bh->b_data;
623         memset(rb, 0, inode->i_sb->s_blocksize    627         memset(rb, 0, inode->i_sb->s_blocksize);
624         strcpy((void *)rb, OCFS2_REFCOUNT_BLOC    628         strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
625         rb->rf_suballoc_slot = cpu_to_le16(met    629         rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
626         rb->rf_suballoc_loc = cpu_to_le64(suba    630         rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
627         rb->rf_suballoc_bit = cpu_to_le16(suba    631         rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
628         rb->rf_fs_generation = cpu_to_le32(osb    632         rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
629         rb->rf_blkno = cpu_to_le64(first_blkno    633         rb->rf_blkno = cpu_to_le64(first_blkno);
630         rb->rf_count = cpu_to_le32(1);            634         rb->rf_count = cpu_to_le32(1);
631         rb->rf_records.rl_count =                 635         rb->rf_records.rl_count =
632                         cpu_to_le16(ocfs2_refc    636                         cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
633         spin_lock(&osb->osb_lock);                637         spin_lock(&osb->osb_lock);
634         rb->rf_generation = cpu_to_le32(osb->s !! 638         rb->rf_generation = osb->s_next_generation++;
635         spin_unlock(&osb->osb_lock);              639         spin_unlock(&osb->osb_lock);
636                                                   640 
637         ocfs2_journal_dirty(handle, new_bh);      641         ocfs2_journal_dirty(handle, new_bh);
638                                                   642 
639         spin_lock(&oi->ip_lock);                  643         spin_lock(&oi->ip_lock);
640         oi->ip_dyn_features |= OCFS2_HAS_REFCO    644         oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
641         di->i_dyn_features = cpu_to_le16(oi->i    645         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
642         di->i_refcount_loc = cpu_to_le64(first    646         di->i_refcount_loc = cpu_to_le64(first_blkno);
643         spin_unlock(&oi->ip_lock);                647         spin_unlock(&oi->ip_lock);
644                                                   648 
645         trace_ocfs2_create_refcount_tree_blkno    649         trace_ocfs2_create_refcount_tree_blkno((unsigned long long)first_blkno);
646                                                   650 
647         ocfs2_journal_dirty(handle, di_bh);       651         ocfs2_journal_dirty(handle, di_bh);
648                                                   652 
649         /*                                        653         /*
650          * We have to init the tree lock here     654          * We have to init the tree lock here since it will use
651          * the generation number to create it.    655          * the generation number to create it.
652          */                                       656          */
653         new_tree->rf_generation = le32_to_cpu(    657         new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
654         ocfs2_init_refcount_tree_lock(osb, new    658         ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
655                                       new_tree    659                                       new_tree->rf_generation);
656                                                   660 
657         spin_lock(&osb->osb_lock);                661         spin_lock(&osb->osb_lock);
658         tree = ocfs2_find_refcount_tree(osb, f    662         tree = ocfs2_find_refcount_tree(osb, first_blkno);
659                                                   663 
660         /*                                        664         /*
661          * We've just created a new refcount t    665          * We've just created a new refcount tree in this block.  If
662          * we found a refcount tree on the ocf    666          * we found a refcount tree on the ocfs2_super, it must be
663          * one we just deleted.  We free the o    667          * one we just deleted.  We free the old tree before
664          * inserting the new tree.                668          * inserting the new tree.
665          */                                       669          */
666         BUG_ON(tree && tree->rf_generation ==     670         BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
667         if (tree)                                 671         if (tree)
668                 ocfs2_erase_refcount_tree_from    672                 ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
669         ocfs2_insert_refcount_tree(osb, new_tr    673         ocfs2_insert_refcount_tree(osb, new_tree);
670         spin_unlock(&osb->osb_lock);              674         spin_unlock(&osb->osb_lock);
671         new_tree = NULL;                          675         new_tree = NULL;
672         if (tree)                                 676         if (tree)
673                 ocfs2_refcount_tree_put(tree);    677                 ocfs2_refcount_tree_put(tree);
674                                                   678 
675 out_commit:                                       679 out_commit:
676         ocfs2_commit_trans(osb, handle);          680         ocfs2_commit_trans(osb, handle);
677                                                   681 
678 out:                                              682 out:
679         if (new_tree) {                           683         if (new_tree) {
680                 ocfs2_metadata_cache_exit(&new    684                 ocfs2_metadata_cache_exit(&new_tree->rf_ci);
681                 kfree(new_tree);                  685                 kfree(new_tree);
682         }                                         686         }
683                                                   687 
684         brelse(new_bh);                           688         brelse(new_bh);
685         if (meta_ac)                              689         if (meta_ac)
686                 ocfs2_free_alloc_context(meta_    690                 ocfs2_free_alloc_context(meta_ac);
687                                                   691 
688         return ret;                               692         return ret;
689 }                                                 693 }
690                                                   694 
691 static int ocfs2_set_refcount_tree(struct inod    695 static int ocfs2_set_refcount_tree(struct inode *inode,
692                                    struct buff    696                                    struct buffer_head *di_bh,
693                                    u64 refcoun    697                                    u64 refcount_loc)
694 {                                                 698 {
695         int ret;                                  699         int ret;
696         handle_t *handle = NULL;                  700         handle_t *handle = NULL;
697         struct ocfs2_dinode *di = (struct ocfs    701         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
698         struct ocfs2_inode_info *oi = OCFS2_I(    702         struct ocfs2_inode_info *oi = OCFS2_I(inode);
699         struct ocfs2_super *osb = OCFS2_SB(ino    703         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
700         struct buffer_head *ref_root_bh = NULL    704         struct buffer_head *ref_root_bh = NULL;
701         struct ocfs2_refcount_block *rb;          705         struct ocfs2_refcount_block *rb;
702         struct ocfs2_refcount_tree *ref_tree;     706         struct ocfs2_refcount_tree *ref_tree;
703                                                   707 
704         BUG_ON(ocfs2_is_refcount_inode(inode)) !! 708         BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
705                                                   709 
706         ret = ocfs2_lock_refcount_tree(osb, re    710         ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
707                                        &ref_tr    711                                        &ref_tree, &ref_root_bh);
708         if (ret) {                                712         if (ret) {
709                 mlog_errno(ret);                  713                 mlog_errno(ret);
710                 return ret;                       714                 return ret;
711         }                                         715         }
712                                                   716 
713         handle = ocfs2_start_trans(osb, OCFS2_    717         handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
714         if (IS_ERR(handle)) {                     718         if (IS_ERR(handle)) {
715                 ret = PTR_ERR(handle);            719                 ret = PTR_ERR(handle);
716                 mlog_errno(ret);                  720                 mlog_errno(ret);
717                 goto out;                         721                 goto out;
718         }                                         722         }
719                                                   723 
720         ret = ocfs2_journal_access_di(handle,     724         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
721                                       OCFS2_JO    725                                       OCFS2_JOURNAL_ACCESS_WRITE);
722         if (ret) {                                726         if (ret) {
723                 mlog_errno(ret);                  727                 mlog_errno(ret);
724                 goto out_commit;                  728                 goto out_commit;
725         }                                         729         }
726                                                   730 
727         ret = ocfs2_journal_access_rb(handle,     731         ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
728                                       OCFS2_JO    732                                       OCFS2_JOURNAL_ACCESS_WRITE);
729         if (ret) {                                733         if (ret) {
730                 mlog_errno(ret);                  734                 mlog_errno(ret);
731                 goto out_commit;                  735                 goto out_commit;
732         }                                         736         }
733                                                   737 
734         rb = (struct ocfs2_refcount_block *)re    738         rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
735         le32_add_cpu(&rb->rf_count, 1);           739         le32_add_cpu(&rb->rf_count, 1);
736                                                   740 
737         ocfs2_journal_dirty(handle, ref_root_b    741         ocfs2_journal_dirty(handle, ref_root_bh);
738                                                   742 
739         spin_lock(&oi->ip_lock);                  743         spin_lock(&oi->ip_lock);
740         oi->ip_dyn_features |= OCFS2_HAS_REFCO    744         oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
741         di->i_dyn_features = cpu_to_le16(oi->i    745         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
742         di->i_refcount_loc = cpu_to_le64(refco    746         di->i_refcount_loc = cpu_to_le64(refcount_loc);
743         spin_unlock(&oi->ip_lock);                747         spin_unlock(&oi->ip_lock);
744         ocfs2_journal_dirty(handle, di_bh);       748         ocfs2_journal_dirty(handle, di_bh);
745                                                   749 
746 out_commit:                                       750 out_commit:
747         ocfs2_commit_trans(osb, handle);          751         ocfs2_commit_trans(osb, handle);
748 out:                                              752 out:
749         ocfs2_unlock_refcount_tree(osb, ref_tr    753         ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
750         brelse(ref_root_bh);                      754         brelse(ref_root_bh);
751                                                   755 
752         return ret;                               756         return ret;
753 }                                                 757 }
754                                                   758 
755 int ocfs2_remove_refcount_tree(struct inode *i    759 int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
756 {                                                 760 {
757         int ret, delete_tree = 0;                 761         int ret, delete_tree = 0;
758         handle_t *handle = NULL;                  762         handle_t *handle = NULL;
759         struct ocfs2_dinode *di = (struct ocfs    763         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
760         struct ocfs2_inode_info *oi = OCFS2_I(    764         struct ocfs2_inode_info *oi = OCFS2_I(inode);
761         struct ocfs2_super *osb = OCFS2_SB(ino    765         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
762         struct ocfs2_refcount_block *rb;          766         struct ocfs2_refcount_block *rb;
763         struct inode *alloc_inode = NULL;         767         struct inode *alloc_inode = NULL;
764         struct buffer_head *alloc_bh = NULL;      768         struct buffer_head *alloc_bh = NULL;
765         struct buffer_head *blk_bh = NULL;        769         struct buffer_head *blk_bh = NULL;
766         struct ocfs2_refcount_tree *ref_tree;     770         struct ocfs2_refcount_tree *ref_tree;
767         int credits = OCFS2_REFCOUNT_TREE_REMO    771         int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
768         u64 blk = 0, bg_blkno = 0, ref_blkno =    772         u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
769         u16 bit = 0;                              773         u16 bit = 0;
770                                                   774 
771         if (!ocfs2_is_refcount_inode(inode))   !! 775         if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL))
772                 return 0;                         776                 return 0;
773                                                   777 
774         BUG_ON(!ref_blkno);                       778         BUG_ON(!ref_blkno);
775         ret = ocfs2_lock_refcount_tree(osb, re    779         ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
776         if (ret) {                                780         if (ret) {
777                 mlog_errno(ret);                  781                 mlog_errno(ret);
778                 return ret;                       782                 return ret;
779         }                                         783         }
780                                                   784 
781         rb = (struct ocfs2_refcount_block *)bl    785         rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
782                                                   786 
783         /*                                        787         /*
784          * If we are the last user, we need to    788          * If we are the last user, we need to free the block.
785          * So lock the allocator ahead.           789          * So lock the allocator ahead.
786          */                                       790          */
787         if (le32_to_cpu(rb->rf_count) == 1) {     791         if (le32_to_cpu(rb->rf_count) == 1) {
788                 blk = le64_to_cpu(rb->rf_blkno    792                 blk = le64_to_cpu(rb->rf_blkno);
789                 bit = le16_to_cpu(rb->rf_subal    793                 bit = le16_to_cpu(rb->rf_suballoc_bit);
790                 if (rb->rf_suballoc_loc)          794                 if (rb->rf_suballoc_loc)
791                         bg_blkno = le64_to_cpu    795                         bg_blkno = le64_to_cpu(rb->rf_suballoc_loc);
792                 else                              796                 else
793                         bg_blkno = ocfs2_which    797                         bg_blkno = ocfs2_which_suballoc_group(blk, bit);
794                                                   798 
795                 alloc_inode = ocfs2_get_system    799                 alloc_inode = ocfs2_get_system_file_inode(osb,
796                                         EXTENT    800                                         EXTENT_ALLOC_SYSTEM_INODE,
797                                         le16_t    801                                         le16_to_cpu(rb->rf_suballoc_slot));
798                 if (!alloc_inode) {               802                 if (!alloc_inode) {
799                         ret = -ENOMEM;            803                         ret = -ENOMEM;
800                         mlog_errno(ret);          804                         mlog_errno(ret);
801                         goto out;                 805                         goto out;
802                 }                                 806                 }
803                 inode_lock(alloc_inode);       !! 807                 mutex_lock(&alloc_inode->i_mutex);
804                                                   808 
805                 ret = ocfs2_inode_lock(alloc_i    809                 ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
806                 if (ret) {                        810                 if (ret) {
807                         mlog_errno(ret);          811                         mlog_errno(ret);
808                         goto out_mutex;           812                         goto out_mutex;
809                 }                                 813                 }
810                                                   814 
811                 credits += OCFS2_SUBALLOC_FREE    815                 credits += OCFS2_SUBALLOC_FREE;
812         }                                         816         }
813                                                   817 
814         handle = ocfs2_start_trans(osb, credit    818         handle = ocfs2_start_trans(osb, credits);
815         if (IS_ERR(handle)) {                     819         if (IS_ERR(handle)) {
816                 ret = PTR_ERR(handle);            820                 ret = PTR_ERR(handle);
817                 mlog_errno(ret);                  821                 mlog_errno(ret);
818                 goto out_unlock;                  822                 goto out_unlock;
819         }                                         823         }
820                                                   824 
821         ret = ocfs2_journal_access_di(handle,     825         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
822                                       OCFS2_JO    826                                       OCFS2_JOURNAL_ACCESS_WRITE);
823         if (ret) {                                827         if (ret) {
824                 mlog_errno(ret);                  828                 mlog_errno(ret);
825                 goto out_commit;                  829                 goto out_commit;
826         }                                         830         }
827                                                   831 
828         ret = ocfs2_journal_access_rb(handle,     832         ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
829                                       OCFS2_JO    833                                       OCFS2_JOURNAL_ACCESS_WRITE);
830         if (ret) {                                834         if (ret) {
831                 mlog_errno(ret);                  835                 mlog_errno(ret);
832                 goto out_commit;                  836                 goto out_commit;
833         }                                         837         }
834                                                   838 
835         spin_lock(&oi->ip_lock);                  839         spin_lock(&oi->ip_lock);
836         oi->ip_dyn_features &= ~OCFS2_HAS_REFC    840         oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
837         di->i_dyn_features = cpu_to_le16(oi->i    841         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
838         di->i_refcount_loc = 0;                   842         di->i_refcount_loc = 0;
839         spin_unlock(&oi->ip_lock);                843         spin_unlock(&oi->ip_lock);
840         ocfs2_journal_dirty(handle, di_bh);       844         ocfs2_journal_dirty(handle, di_bh);
841                                                   845 
842         le32_add_cpu(&rb->rf_count , -1);         846         le32_add_cpu(&rb->rf_count , -1);
843         ocfs2_journal_dirty(handle, blk_bh);      847         ocfs2_journal_dirty(handle, blk_bh);
844                                                   848 
845         if (!rb->rf_count) {                      849         if (!rb->rf_count) {
846                 delete_tree = 1;                  850                 delete_tree = 1;
847                 ocfs2_erase_refcount_tree_from    851                 ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
848                 ret = ocfs2_free_suballoc_bits    852                 ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
849                                                   853                                                alloc_bh, bit, bg_blkno, 1);
850                 if (ret)                          854                 if (ret)
851                         mlog_errno(ret);          855                         mlog_errno(ret);
852         }                                         856         }
853                                                   857 
854 out_commit:                                       858 out_commit:
855         ocfs2_commit_trans(osb, handle);          859         ocfs2_commit_trans(osb, handle);
856 out_unlock:                                       860 out_unlock:
857         if (alloc_inode) {                        861         if (alloc_inode) {
858                 ocfs2_inode_unlock(alloc_inode    862                 ocfs2_inode_unlock(alloc_inode, 1);
859                 brelse(alloc_bh);                 863                 brelse(alloc_bh);
860         }                                         864         }
861 out_mutex:                                        865 out_mutex:
862         if (alloc_inode) {                        866         if (alloc_inode) {
863                 inode_unlock(alloc_inode);     !! 867                 mutex_unlock(&alloc_inode->i_mutex);
864                 iput(alloc_inode);                868                 iput(alloc_inode);
865         }                                         869         }
866 out:                                              870 out:
867         ocfs2_unlock_refcount_tree(osb, ref_tr    871         ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
868         if (delete_tree)                          872         if (delete_tree)
869                 ocfs2_refcount_tree_put(ref_tr    873                 ocfs2_refcount_tree_put(ref_tree);
870         brelse(blk_bh);                           874         brelse(blk_bh);
871                                                   875 
872         return ret;                               876         return ret;
873 }                                                 877 }
874                                                   878 
875 static void ocfs2_find_refcount_rec_in_rl(stru    879 static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
876                                           stru    880                                           struct buffer_head *ref_leaf_bh,
877                                           u64     881                                           u64 cpos, unsigned int len,
878                                           stru    882                                           struct ocfs2_refcount_rec *ret_rec,
879                                           int     883                                           int *index)
880 {                                                 884 {
881         int i = 0;                                885         int i = 0;
882         struct ocfs2_refcount_block *rb =         886         struct ocfs2_refcount_block *rb =
883                 (struct ocfs2_refcount_block *    887                 (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
884         struct ocfs2_refcount_rec *rec = NULL;    888         struct ocfs2_refcount_rec *rec = NULL;
885                                                   889 
886         for (; i < le16_to_cpu(rb->rf_records.    890         for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
887                 rec = &rb->rf_records.rl_recs[    891                 rec = &rb->rf_records.rl_recs[i];
888                                                   892 
889                 if (le64_to_cpu(rec->r_cpos) +    893                 if (le64_to_cpu(rec->r_cpos) +
890                     le32_to_cpu(rec->r_cluster    894                     le32_to_cpu(rec->r_clusters) <= cpos)
891                         continue;                 895                         continue;
892                 else if (le64_to_cpu(rec->r_cp    896                 else if (le64_to_cpu(rec->r_cpos) > cpos)
893                         break;                    897                         break;
894                                                   898 
895                 /* ok, cpos fail in this rec.     899                 /* ok, cpos fail in this rec. Just return. */
896                 if (ret_rec)                      900                 if (ret_rec)
897                         *ret_rec = *rec;          901                         *ret_rec = *rec;
898                 goto out;                         902                 goto out;
899         }                                         903         }
900                                                   904 
901         if (ret_rec) {                            905         if (ret_rec) {
902                 /* We meet with a hole here, s    906                 /* We meet with a hole here, so fake the rec. */
903                 ret_rec->r_cpos = cpu_to_le64(    907                 ret_rec->r_cpos = cpu_to_le64(cpos);
904                 ret_rec->r_refcount = 0;          908                 ret_rec->r_refcount = 0;
905                 if (i < le16_to_cpu(rb->rf_rec    909                 if (i < le16_to_cpu(rb->rf_records.rl_used) &&
906                     le64_to_cpu(rec->r_cpos) <    910                     le64_to_cpu(rec->r_cpos) < cpos + len)
907                         ret_rec->r_clusters =     911                         ret_rec->r_clusters =
908                                 cpu_to_le32(le    912                                 cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
909                 else                              913                 else
910                         ret_rec->r_clusters =     914                         ret_rec->r_clusters = cpu_to_le32(len);
911         }                                         915         }
912                                                   916 
913 out:                                              917 out:
914         *index = i;                               918         *index = i;
915 }                                                 919 }
916                                                   920 
917 /*                                                921 /*
918  * Try to remove refcount tree. The mechanism     922  * Try to remove refcount tree. The mechanism is:
919  * 1) Check whether i_clusters == 0, if no, ex    923  * 1) Check whether i_clusters == 0, if no, exit.
920  * 2) check whether we have i_xattr_loc in din    924  * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
921  * 3) Check whether we have inline xattr store    925  * 3) Check whether we have inline xattr stored outside, if yes, exit.
922  * 4) Remove the tree.                            926  * 4) Remove the tree.
923  */                                               927  */
924 int ocfs2_try_remove_refcount_tree(struct inod    928 int ocfs2_try_remove_refcount_tree(struct inode *inode,
925                                    struct buff    929                                    struct buffer_head *di_bh)
926 {                                                 930 {
927         int ret;                                  931         int ret;
928         struct ocfs2_inode_info *oi = OCFS2_I(    932         struct ocfs2_inode_info *oi = OCFS2_I(inode);
929         struct ocfs2_dinode *di = (struct ocfs    933         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
930                                                   934 
931         down_write(&oi->ip_xattr_sem);            935         down_write(&oi->ip_xattr_sem);
932         down_write(&oi->ip_alloc_sem);            936         down_write(&oi->ip_alloc_sem);
933                                                   937 
934         if (oi->ip_clusters)                      938         if (oi->ip_clusters)
935                 goto out;                         939                 goto out;
936                                                   940 
937         if ((oi->ip_dyn_features & OCFS2_HAS_X    941         if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
938                 goto out;                         942                 goto out;
939                                                   943 
940         if (oi->ip_dyn_features & OCFS2_INLINE    944         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
941             ocfs2_has_inline_xattr_value_outsi    945             ocfs2_has_inline_xattr_value_outside(inode, di))
942                 goto out;                         946                 goto out;
943                                                   947 
944         ret = ocfs2_remove_refcount_tree(inode    948         ret = ocfs2_remove_refcount_tree(inode, di_bh);
945         if (ret)                                  949         if (ret)
946                 mlog_errno(ret);                  950                 mlog_errno(ret);
947 out:                                              951 out:
948         up_write(&oi->ip_alloc_sem);              952         up_write(&oi->ip_alloc_sem);
949         up_write(&oi->ip_xattr_sem);              953         up_write(&oi->ip_xattr_sem);
950         return 0;                                 954         return 0;
951 }                                                 955 }
952                                                   956 
953 /*                                                957 /*
954  * Find the end range for a leaf refcount bloc    958  * Find the end range for a leaf refcount block indicated by
955  * el->l_recs[index].e_blkno.                     959  * el->l_recs[index].e_blkno.
956  */                                               960  */
957 static int ocfs2_get_refcount_cpos_end(struct     961 static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci,
958                                        struct     962                                        struct buffer_head *ref_root_bh,
959                                        struct     963                                        struct ocfs2_extent_block *eb,
960                                        struct     964                                        struct ocfs2_extent_list *el,
961                                        int ind    965                                        int index,  u32 *cpos_end)
962 {                                                 966 {
963         int ret, i, subtree_root;                 967         int ret, i, subtree_root;
964         u32 cpos;                                 968         u32 cpos;
965         u64 blkno;                                969         u64 blkno;
966         struct super_block *sb = ocfs2_metadat    970         struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
967         struct ocfs2_path *left_path = NULL, *    971         struct ocfs2_path *left_path = NULL, *right_path = NULL;
968         struct ocfs2_extent_tree et;              972         struct ocfs2_extent_tree et;
969         struct ocfs2_extent_list *tmp_el;         973         struct ocfs2_extent_list *tmp_el;
970                                                   974 
971         if (index < le16_to_cpu(el->l_next_fre    975         if (index < le16_to_cpu(el->l_next_free_rec) - 1) {
972                 /*                                976                 /*
973                  * We have a extent rec after     977                  * We have a extent rec after index, so just use the e_cpos
974                  * of the next extent rec.        978                  * of the next extent rec.
975                  */                               979                  */
976                 *cpos_end = le32_to_cpu(el->l_    980                 *cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos);
977                 return 0;                         981                 return 0;
978         }                                         982         }
979                                                   983 
980         if (!eb || !eb->h_next_leaf_blk) {     !! 984         if (!eb || (eb && !eb->h_next_leaf_blk)) {
981                 /*                                985                 /*
982                  * We are the last extent rec,    986                  * We are the last extent rec, so any high cpos should
983                  * be stored in this leaf refc    987                  * be stored in this leaf refcount block.
984                  */                               988                  */
985                 *cpos_end = UINT_MAX;             989                 *cpos_end = UINT_MAX;
986                 return 0;                         990                 return 0;
987         }                                         991         }
988                                                   992 
989         /*                                        993         /*
990          * If the extent block isn't the last     994          * If the extent block isn't the last one, we have to find
991          * the subtree root between this exten    995          * the subtree root between this extent block and the next
992          * leaf extent block and get the corre    996          * leaf extent block and get the corresponding e_cpos from
993          * the subroot. Otherwise we may corru    997          * the subroot. Otherwise we may corrupt the b-tree.
994          */                                       998          */
995         ocfs2_init_refcount_extent_tree(&et, c    999         ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
996                                                   1000 
997         left_path = ocfs2_new_path_from_et(&et    1001         left_path = ocfs2_new_path_from_et(&et);
998         if (!left_path) {                         1002         if (!left_path) {
999                 ret = -ENOMEM;                    1003                 ret = -ENOMEM;
1000                 mlog_errno(ret);                 1004                 mlog_errno(ret);
1001                 goto out;                        1005                 goto out;
1002         }                                        1006         }
1003                                                  1007 
1004         cpos = le32_to_cpu(eb->h_list.l_recs[    1008         cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos);
1005         ret = ocfs2_find_path(ci, left_path,     1009         ret = ocfs2_find_path(ci, left_path, cpos);
1006         if (ret) {                               1010         if (ret) {
1007                 mlog_errno(ret);                 1011                 mlog_errno(ret);
1008                 goto out;                        1012                 goto out;
1009         }                                        1013         }
1010                                                  1014 
1011         right_path = ocfs2_new_path_from_path    1015         right_path = ocfs2_new_path_from_path(left_path);
1012         if (!right_path) {                       1016         if (!right_path) {
1013                 ret = -ENOMEM;                   1017                 ret = -ENOMEM;
1014                 mlog_errno(ret);                 1018                 mlog_errno(ret);
1015                 goto out;                        1019                 goto out;
1016         }                                        1020         }
1017                                                  1021 
1018         ret = ocfs2_find_cpos_for_right_leaf(    1022         ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos);
1019         if (ret) {                               1023         if (ret) {
1020                 mlog_errno(ret);                 1024                 mlog_errno(ret);
1021                 goto out;                        1025                 goto out;
1022         }                                        1026         }
1023                                                  1027 
1024         ret = ocfs2_find_path(ci, right_path,    1028         ret = ocfs2_find_path(ci, right_path, cpos);
1025         if (ret) {                               1029         if (ret) {
1026                 mlog_errno(ret);                 1030                 mlog_errno(ret);
1027                 goto out;                        1031                 goto out;
1028         }                                        1032         }
1029                                                  1033 
1030         subtree_root = ocfs2_find_subtree_roo    1034         subtree_root = ocfs2_find_subtree_root(&et, left_path,
1031                                                  1035                                                right_path);
1032                                                  1036 
1033         tmp_el = left_path->p_node[subtree_ro    1037         tmp_el = left_path->p_node[subtree_root].el;
1034         blkno = left_path->p_node[subtree_roo    1038         blkno = left_path->p_node[subtree_root+1].bh->b_blocknr;
1035         for (i = 0; i < le16_to_cpu(tmp_el->l    1039         for (i = 0; i < le16_to_cpu(tmp_el->l_next_free_rec); i++) {
1036                 if (le64_to_cpu(tmp_el->l_rec    1040                 if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) {
1037                         *cpos_end = le32_to_c    1041                         *cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos);
1038                         break;                   1042                         break;
1039                 }                                1043                 }
1040         }                                        1044         }
1041                                                  1045 
1042         BUG_ON(i == le16_to_cpu(tmp_el->l_nex    1046         BUG_ON(i == le16_to_cpu(tmp_el->l_next_free_rec));
1043                                                  1047 
1044 out:                                             1048 out:
1045         ocfs2_free_path(left_path);              1049         ocfs2_free_path(left_path);
1046         ocfs2_free_path(right_path);             1050         ocfs2_free_path(right_path);
1047         return ret;                              1051         return ret;
1048 }                                                1052 }
1049                                                  1053 
1050 /*                                               1054 /*
1051  * Given a cpos and len, try to find the refc    1055  * Given a cpos and len, try to find the refcount record which contains cpos.
1052  * 1. If cpos can be found in one refcount re    1056  * 1. If cpos can be found in one refcount record, return the record.
1053  * 2. If cpos can't be found, return a fake r    1057  * 2. If cpos can't be found, return a fake record which start from cpos
1054  *    and end at a small value between cpos+l    1058  *    and end at a small value between cpos+len and start of the next record.
1055  *    This fake record has r_refcount = 0.       1059  *    This fake record has r_refcount = 0.
1056  */                                              1060  */
1057 static int ocfs2_get_refcount_rec(struct ocfs    1061 static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
1058                                   struct buff    1062                                   struct buffer_head *ref_root_bh,
1059                                   u64 cpos, u    1063                                   u64 cpos, unsigned int len,
1060                                   struct ocfs    1064                                   struct ocfs2_refcount_rec *ret_rec,
1061                                   int *index,    1065                                   int *index,
1062                                   struct buff    1066                                   struct buffer_head **ret_bh)
1063 {                                                1067 {
1064         int ret = 0, i, found;                   1068         int ret = 0, i, found;
1065         u32 low_cpos, cpos_end;               !! 1069         u32 low_cpos, uninitialized_var(cpos_end);
1066         struct ocfs2_extent_list *el;            1070         struct ocfs2_extent_list *el;
1067         struct ocfs2_extent_rec *rec = NULL;     1071         struct ocfs2_extent_rec *rec = NULL;
1068         struct ocfs2_extent_block *eb = NULL;    1072         struct ocfs2_extent_block *eb = NULL;
1069         struct buffer_head *eb_bh = NULL, *re    1073         struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
1070         struct super_block *sb = ocfs2_metada    1074         struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1071         struct ocfs2_refcount_block *rb =        1075         struct ocfs2_refcount_block *rb =
1072                         (struct ocfs2_refcoun    1076                         (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1073                                                  1077 
1074         if (!(le32_to_cpu(rb->rf_flags) & OCF    1078         if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
1075                 ocfs2_find_refcount_rec_in_rl    1079                 ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
1076                                                  1080                                               ret_rec, index);
1077                 *ret_bh = ref_root_bh;           1081                 *ret_bh = ref_root_bh;
1078                 get_bh(ref_root_bh);             1082                 get_bh(ref_root_bh);
1079                 return 0;                        1083                 return 0;
1080         }                                        1084         }
1081                                                  1085 
1082         el = &rb->rf_list;                       1086         el = &rb->rf_list;
1083         low_cpos = cpos & OCFS2_32BIT_POS_MAS    1087         low_cpos = cpos & OCFS2_32BIT_POS_MASK;
1084                                                  1088 
1085         if (el->l_tree_depth) {                  1089         if (el->l_tree_depth) {
1086                 ret = ocfs2_find_leaf(ci, el,    1090                 ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
1087                 if (ret) {                       1091                 if (ret) {
1088                         mlog_errno(ret);         1092                         mlog_errno(ret);
1089                         goto out;                1093                         goto out;
1090                 }                                1094                 }
1091                                                  1095 
1092                 eb = (struct ocfs2_extent_blo    1096                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1093                 el = &eb->h_list;                1097                 el = &eb->h_list;
1094                                                  1098 
1095                 if (el->l_tree_depth) {          1099                 if (el->l_tree_depth) {
1096                         ret = ocfs2_error(sb, !! 1100                         ocfs2_error(sb,
1097                                           "re !! 1101                         "refcount tree %llu has non zero tree "
1098                                           (un !! 1102                         "depth in leaf btree tree block %llu\n",
1099                                           (un !! 1103                         (unsigned long long)ocfs2_metadata_cache_owner(ci),
                                                   >> 1104                         (unsigned long long)eb_bh->b_blocknr);
                                                   >> 1105                         ret = -EROFS;
1100                         goto out;                1106                         goto out;
1101                 }                                1107                 }
1102         }                                        1108         }
1103                                                  1109 
1104         found = 0;                               1110         found = 0;
1105         for (i = le16_to_cpu(el->l_next_free_    1111         for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1106                 rec = &el->l_recs[i];            1112                 rec = &el->l_recs[i];
1107                                                  1113 
1108                 if (le32_to_cpu(rec->e_cpos)     1114                 if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
1109                         found = 1;               1115                         found = 1;
1110                         break;                   1116                         break;
1111                 }                                1117                 }
1112         }                                        1118         }
1113                                                  1119 
1114         if (found) {                             1120         if (found) {
1115                 ret = ocfs2_get_refcount_cpos    1121                 ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh,
1116                                                  1122                                                   eb, el, i, &cpos_end);
1117                 if (ret) {                       1123                 if (ret) {
1118                         mlog_errno(ret);         1124                         mlog_errno(ret);
1119                         goto out;                1125                         goto out;
1120                 }                                1126                 }
1121                                                  1127 
1122                 if (cpos_end < low_cpos + len    1128                 if (cpos_end < low_cpos + len)
1123                         len = cpos_end - low_    1129                         len = cpos_end - low_cpos;
1124         }                                        1130         }
1125                                                  1131 
1126         ret = ocfs2_read_refcount_block(ci, l    1132         ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
1127                                         &ref_    1133                                         &ref_leaf_bh);
1128         if (ret) {                               1134         if (ret) {
1129                 mlog_errno(ret);                 1135                 mlog_errno(ret);
1130                 goto out;                        1136                 goto out;
1131         }                                        1137         }
1132                                                  1138 
1133         ocfs2_find_refcount_rec_in_rl(ci, ref    1139         ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
1134                                       ret_rec    1140                                       ret_rec, index);
1135         *ret_bh = ref_leaf_bh;                   1141         *ret_bh = ref_leaf_bh;
1136 out:                                             1142 out:
1137         brelse(eb_bh);                           1143         brelse(eb_bh);
1138         return ret;                              1144         return ret;
1139 }                                                1145 }
1140                                                  1146 
1141 enum ocfs2_ref_rec_contig {                      1147 enum ocfs2_ref_rec_contig {
1142         REF_CONTIG_NONE = 0,                     1148         REF_CONTIG_NONE = 0,
1143         REF_CONTIG_LEFT,                         1149         REF_CONTIG_LEFT,
1144         REF_CONTIG_RIGHT,                        1150         REF_CONTIG_RIGHT,
1145         REF_CONTIG_LEFTRIGHT,                    1151         REF_CONTIG_LEFTRIGHT,
1146 };                                               1152 };
1147                                                  1153 
1148 static enum ocfs2_ref_rec_contig                 1154 static enum ocfs2_ref_rec_contig
1149         ocfs2_refcount_rec_adjacent(struct oc    1155         ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
1150                                     int index    1156                                     int index)
1151 {                                                1157 {
1152         if ((rb->rf_records.rl_recs[index].r_    1158         if ((rb->rf_records.rl_recs[index].r_refcount ==
1153             rb->rf_records.rl_recs[index + 1]    1159             rb->rf_records.rl_recs[index + 1].r_refcount) &&
1154             (le64_to_cpu(rb->rf_records.rl_re    1160             (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
1155             le32_to_cpu(rb->rf_records.rl_rec    1161             le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
1156             le64_to_cpu(rb->rf_records.rl_rec    1162             le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
1157                 return REF_CONTIG_RIGHT;         1163                 return REF_CONTIG_RIGHT;
1158                                                  1164 
1159         return REF_CONTIG_NONE;                  1165         return REF_CONTIG_NONE;
1160 }                                                1166 }
1161                                                  1167 
1162 static enum ocfs2_ref_rec_contig                 1168 static enum ocfs2_ref_rec_contig
1163         ocfs2_refcount_rec_contig(struct ocfs    1169         ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
1164                                   int index)     1170                                   int index)
1165 {                                                1171 {
1166         enum ocfs2_ref_rec_contig ret = REF_C    1172         enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
1167                                                  1173 
1168         if (index < le16_to_cpu(rb->rf_record    1174         if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
1169                 ret = ocfs2_refcount_rec_adja    1175                 ret = ocfs2_refcount_rec_adjacent(rb, index);
1170                                                  1176 
1171         if (index > 0) {                         1177         if (index > 0) {
1172                 enum ocfs2_ref_rec_contig tmp    1178                 enum ocfs2_ref_rec_contig tmp;
1173                                                  1179 
1174                 tmp = ocfs2_refcount_rec_adja    1180                 tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1175                                                  1181 
1176                 if (tmp == REF_CONTIG_RIGHT)     1182                 if (tmp == REF_CONTIG_RIGHT) {
1177                         if (ret == REF_CONTIG    1183                         if (ret == REF_CONTIG_RIGHT)
1178                                 ret = REF_CON    1184                                 ret = REF_CONTIG_LEFTRIGHT;
1179                         else                     1185                         else
1180                                 ret = REF_CON    1186                                 ret = REF_CONTIG_LEFT;
1181                 }                                1187                 }
1182         }                                        1188         }
1183                                                  1189 
1184         return ret;                              1190         return ret;
1185 }                                                1191 }
1186                                                  1192 
1187 static void ocfs2_rotate_refcount_rec_left(st    1193 static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
1188                                            in    1194                                            int index)
1189 {                                                1195 {
1190         BUG_ON(rb->rf_records.rl_recs[index].    1196         BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
1191                rb->rf_records.rl_recs[index+1    1197                rb->rf_records.rl_recs[index+1].r_refcount);
1192                                                  1198 
1193         le32_add_cpu(&rb->rf_records.rl_recs[    1199         le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
1194                      le32_to_cpu(rb->rf_recor    1200                      le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
1195                                                  1201 
1196         if (index < le16_to_cpu(rb->rf_record    1202         if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
1197                 memmove(&rb->rf_records.rl_re    1203                 memmove(&rb->rf_records.rl_recs[index + 1],
1198                         &rb->rf_records.rl_re    1204                         &rb->rf_records.rl_recs[index + 2],
1199                         sizeof(struct ocfs2_r    1205                         sizeof(struct ocfs2_refcount_rec) *
1200                         (le16_to_cpu(rb->rf_r    1206                         (le16_to_cpu(rb->rf_records.rl_used) - index - 2));
1201                                                  1207 
1202         memset(&rb->rf_records.rl_recs[le16_t    1208         memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
1203                0, sizeof(struct ocfs2_refcoun    1209                0, sizeof(struct ocfs2_refcount_rec));
1204         le16_add_cpu(&rb->rf_records.rl_used,    1210         le16_add_cpu(&rb->rf_records.rl_used, -1);
1205 }                                                1211 }
1206                                                  1212 
1207 /*                                               1213 /*
1208  * Merge the refcount rec if we are contiguou    1214  * Merge the refcount rec if we are contiguous with the adjacent recs.
1209  */                                              1215  */
1210 static void ocfs2_refcount_rec_merge(struct o    1216 static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
1211                                      int inde    1217                                      int index)
1212 {                                                1218 {
1213         enum ocfs2_ref_rec_contig contig =       1219         enum ocfs2_ref_rec_contig contig =
1214                                 ocfs2_refcoun    1220                                 ocfs2_refcount_rec_contig(rb, index);
1215                                                  1221 
1216         if (contig == REF_CONTIG_NONE)           1222         if (contig == REF_CONTIG_NONE)
1217                 return;                          1223                 return;
1218                                                  1224 
1219         if (contig == REF_CONTIG_LEFT || cont    1225         if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
1220                 BUG_ON(index == 0);              1226                 BUG_ON(index == 0);
1221                 index--;                         1227                 index--;
1222         }                                        1228         }
1223                                                  1229 
1224         ocfs2_rotate_refcount_rec_left(rb, in    1230         ocfs2_rotate_refcount_rec_left(rb, index);
1225                                                  1231 
1226         if (contig == REF_CONTIG_LEFTRIGHT)      1232         if (contig == REF_CONTIG_LEFTRIGHT)
1227                 ocfs2_rotate_refcount_rec_lef    1233                 ocfs2_rotate_refcount_rec_left(rb, index);
1228 }                                                1234 }
1229                                                  1235 
1230 /*                                               1236 /*
1231  * Change the refcount indexed by "index" in     1237  * Change the refcount indexed by "index" in ref_bh.
1232  * If refcount reaches 0, remove it.             1238  * If refcount reaches 0, remove it.
1233  */                                              1239  */
1234 static int ocfs2_change_refcount_rec(handle_t    1240 static int ocfs2_change_refcount_rec(handle_t *handle,
1235                                      struct o    1241                                      struct ocfs2_caching_info *ci,
1236                                      struct b    1242                                      struct buffer_head *ref_leaf_bh,
1237                                      int inde    1243                                      int index, int merge, int change)
1238 {                                                1244 {
1239         int ret;                                 1245         int ret;
1240         struct ocfs2_refcount_block *rb =        1246         struct ocfs2_refcount_block *rb =
1241                         (struct ocfs2_refcoun    1247                         (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1242         struct ocfs2_refcount_list *rl = &rb-    1248         struct ocfs2_refcount_list *rl = &rb->rf_records;
1243         struct ocfs2_refcount_rec *rec = &rl-    1249         struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
1244                                                  1250 
1245         ret = ocfs2_journal_access_rb(handle,    1251         ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1246                                       OCFS2_J    1252                                       OCFS2_JOURNAL_ACCESS_WRITE);
1247         if (ret) {                               1253         if (ret) {
1248                 mlog_errno(ret);                 1254                 mlog_errno(ret);
1249                 goto out;                        1255                 goto out;
1250         }                                        1256         }
1251                                                  1257 
1252         trace_ocfs2_change_refcount_rec(         1258         trace_ocfs2_change_refcount_rec(
1253                 (unsigned long long)ocfs2_met    1259                 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1254                 index, le32_to_cpu(rec->r_ref    1260                 index, le32_to_cpu(rec->r_refcount), change);
1255         le32_add_cpu(&rec->r_refcount, change    1261         le32_add_cpu(&rec->r_refcount, change);
1256                                                  1262 
1257         if (!rec->r_refcount) {                  1263         if (!rec->r_refcount) {
1258                 if (index != le16_to_cpu(rl->    1264                 if (index != le16_to_cpu(rl->rl_used) - 1) {
1259                         memmove(rec, rec + 1,    1265                         memmove(rec, rec + 1,
1260                                 (le16_to_cpu(    1266                                 (le16_to_cpu(rl->rl_used) - index - 1) *
1261                                 sizeof(struct    1267                                 sizeof(struct ocfs2_refcount_rec));
1262                         memset(&rl->rl_recs[l    1268                         memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
1263                                0, sizeof(stru    1269                                0, sizeof(struct ocfs2_refcount_rec));
1264                 }                                1270                 }
1265                                                  1271 
1266                 le16_add_cpu(&rl->rl_used, -1    1272                 le16_add_cpu(&rl->rl_used, -1);
1267         } else if (merge)                        1273         } else if (merge)
1268                 ocfs2_refcount_rec_merge(rb,     1274                 ocfs2_refcount_rec_merge(rb, index);
1269                                                  1275 
1270         ocfs2_journal_dirty(handle, ref_leaf_    1276         ocfs2_journal_dirty(handle, ref_leaf_bh);
1271 out:                                             1277 out:
1272         return ret;                              1278         return ret;
1273 }                                                1279 }
1274                                                  1280 
1275 static int ocfs2_expand_inline_ref_root(handl    1281 static int ocfs2_expand_inline_ref_root(handle_t *handle,
1276                                         struc    1282                                         struct ocfs2_caching_info *ci,
1277                                         struc    1283                                         struct buffer_head *ref_root_bh,
1278                                         struc    1284                                         struct buffer_head **ref_leaf_bh,
1279                                         struc    1285                                         struct ocfs2_alloc_context *meta_ac)
1280 {                                                1286 {
1281         int ret;                                 1287         int ret;
1282         u16 suballoc_bit_start;                  1288         u16 suballoc_bit_start;
1283         u32 num_got;                             1289         u32 num_got;
1284         u64 suballoc_loc, blkno;                 1290         u64 suballoc_loc, blkno;
1285         struct super_block *sb = ocfs2_metada    1291         struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1286         struct buffer_head *new_bh = NULL;       1292         struct buffer_head *new_bh = NULL;
1287         struct ocfs2_refcount_block *new_rb;     1293         struct ocfs2_refcount_block *new_rb;
1288         struct ocfs2_refcount_block *root_rb     1294         struct ocfs2_refcount_block *root_rb =
1289                         (struct ocfs2_refcoun    1295                         (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1290                                                  1296 
1291         ret = ocfs2_journal_access_rb(handle,    1297         ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1292                                       OCFS2_J    1298                                       OCFS2_JOURNAL_ACCESS_WRITE);
1293         if (ret) {                               1299         if (ret) {
1294                 mlog_errno(ret);                 1300                 mlog_errno(ret);
1295                 goto out;                        1301                 goto out;
1296         }                                        1302         }
1297                                                  1303 
1298         ret = ocfs2_claim_metadata(handle, me    1304         ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1299                                    &suballoc_    1305                                    &suballoc_bit_start, &num_got,
1300                                    &blkno);      1306                                    &blkno);
1301         if (ret) {                               1307         if (ret) {
1302                 mlog_errno(ret);                 1308                 mlog_errno(ret);
1303                 goto out;                        1309                 goto out;
1304         }                                        1310         }
1305                                                  1311 
1306         new_bh = sb_getblk(sb, blkno);           1312         new_bh = sb_getblk(sb, blkno);
1307         if (new_bh == NULL) {                    1313         if (new_bh == NULL) {
1308                 ret = -ENOMEM;                !! 1314                 ret = -EIO;
1309                 mlog_errno(ret);                 1315                 mlog_errno(ret);
1310                 goto out;                        1316                 goto out;
1311         }                                        1317         }
1312         ocfs2_set_new_buffer_uptodate(ci, new    1318         ocfs2_set_new_buffer_uptodate(ci, new_bh);
1313                                                  1319 
1314         ret = ocfs2_journal_access_rb(handle,    1320         ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1315                                       OCFS2_J    1321                                       OCFS2_JOURNAL_ACCESS_CREATE);
1316         if (ret) {                               1322         if (ret) {
1317                 mlog_errno(ret);                 1323                 mlog_errno(ret);
1318                 goto out;                        1324                 goto out;
1319         }                                        1325         }
1320                                                  1326 
1321         /*                                       1327         /*
1322          * Initialize ocfs2_refcount_block.      1328          * Initialize ocfs2_refcount_block.
1323          * It should contain the same informa    1329          * It should contain the same information as the old root.
1324          * so just memcpy it and change the c    1330          * so just memcpy it and change the corresponding field.
1325          */                                      1331          */
1326         memcpy(new_bh->b_data, ref_root_bh->b    1332         memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1327                                                  1333 
1328         new_rb = (struct ocfs2_refcount_block    1334         new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1329         new_rb->rf_suballoc_slot = cpu_to_le1    1335         new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1330         new_rb->rf_suballoc_loc = cpu_to_le64    1336         new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1331         new_rb->rf_suballoc_bit = cpu_to_le16    1337         new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1332         new_rb->rf_blkno = cpu_to_le64(blkno)    1338         new_rb->rf_blkno = cpu_to_le64(blkno);
1333         new_rb->rf_cpos = cpu_to_le32(0);        1339         new_rb->rf_cpos = cpu_to_le32(0);
1334         new_rb->rf_parent = cpu_to_le64(ref_r    1340         new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1335         new_rb->rf_flags = cpu_to_le32(OCFS2_    1341         new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1336         ocfs2_journal_dirty(handle, new_bh);     1342         ocfs2_journal_dirty(handle, new_bh);
1337                                                  1343 
1338         /* Now change the root. */               1344         /* Now change the root. */
1339         memset(&root_rb->rf_list, 0, sb->s_bl    1345         memset(&root_rb->rf_list, 0, sb->s_blocksize -
1340                offsetof(struct ocfs2_refcount    1346                offsetof(struct ocfs2_refcount_block, rf_list));
1341         root_rb->rf_list.l_count = cpu_to_le1    1347         root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
1342         root_rb->rf_clusters = cpu_to_le32(1)    1348         root_rb->rf_clusters = cpu_to_le32(1);
1343         root_rb->rf_list.l_next_free_rec = cp    1349         root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
1344         root_rb->rf_list.l_recs[0].e_blkno =     1350         root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
1345         root_rb->rf_list.l_recs[0].e_leaf_clu    1351         root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
1346         root_rb->rf_flags = cpu_to_le32(OCFS2    1352         root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
1347                                                  1353 
1348         ocfs2_journal_dirty(handle, ref_root_    1354         ocfs2_journal_dirty(handle, ref_root_bh);
1349                                                  1355 
1350         trace_ocfs2_expand_inline_ref_root((u    1356         trace_ocfs2_expand_inline_ref_root((unsigned long long)blkno,
1351                 le16_to_cpu(new_rb->rf_record    1357                 le16_to_cpu(new_rb->rf_records.rl_used));
1352                                                  1358 
1353         *ref_leaf_bh = new_bh;                   1359         *ref_leaf_bh = new_bh;
1354         new_bh = NULL;                           1360         new_bh = NULL;
1355 out:                                             1361 out:
1356         brelse(new_bh);                          1362         brelse(new_bh);
1357         return ret;                              1363         return ret;
1358 }                                                1364 }
1359                                                  1365 
1360 static int ocfs2_refcount_rec_no_intersect(st    1366 static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
1361                                            st    1367                                            struct ocfs2_refcount_rec *next)
1362 {                                                1368 {
1363         if (ocfs2_get_ref_rec_low_cpos(prev)     1369         if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
1364                 ocfs2_get_ref_rec_low_cpos(ne    1370                 ocfs2_get_ref_rec_low_cpos(next))
1365                 return 1;                        1371                 return 1;
1366                                                  1372 
1367         return 0;                                1373         return 0;
1368 }                                                1374 }
1369                                                  1375 
1370 static int cmp_refcount_rec_by_low_cpos(const    1376 static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
1371 {                                                1377 {
1372         const struct ocfs2_refcount_rec *l =     1378         const struct ocfs2_refcount_rec *l = a, *r = b;
1373         u32 l_cpos = ocfs2_get_ref_rec_low_cp    1379         u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1374         u32 r_cpos = ocfs2_get_ref_rec_low_cp    1380         u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1375                                                  1381 
1376         if (l_cpos > r_cpos)                     1382         if (l_cpos > r_cpos)
1377                 return 1;                        1383                 return 1;
1378         if (l_cpos < r_cpos)                     1384         if (l_cpos < r_cpos)
1379                 return -1;                       1385                 return -1;
1380         return 0;                                1386         return 0;
1381 }                                                1387 }
1382                                                  1388 
1383 static int cmp_refcount_rec_by_cpos(const voi    1389 static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
1384 {                                                1390 {
1385         const struct ocfs2_refcount_rec *l =     1391         const struct ocfs2_refcount_rec *l = a, *r = b;
1386         u64 l_cpos = le64_to_cpu(l->r_cpos);     1392         u64 l_cpos = le64_to_cpu(l->r_cpos);
1387         u64 r_cpos = le64_to_cpu(r->r_cpos);     1393         u64 r_cpos = le64_to_cpu(r->r_cpos);
1388                                                  1394 
1389         if (l_cpos > r_cpos)                     1395         if (l_cpos > r_cpos)
1390                 return 1;                        1396                 return 1;
1391         if (l_cpos < r_cpos)                     1397         if (l_cpos < r_cpos)
1392                 return -1;                       1398                 return -1;
1393         return 0;                                1399         return 0;
1394 }                                                1400 }
1395                                                  1401 
                                                   >> 1402 static void swap_refcount_rec(void *a, void *b, int size)
                                                   >> 1403 {
                                                   >> 1404         struct ocfs2_refcount_rec *l = a, *r = b, tmp;
                                                   >> 1405 
                                                   >> 1406         tmp = *(struct ocfs2_refcount_rec *)l;
                                                   >> 1407         *(struct ocfs2_refcount_rec *)l =
                                                   >> 1408                         *(struct ocfs2_refcount_rec *)r;
                                                   >> 1409         *(struct ocfs2_refcount_rec *)r = tmp;
                                                   >> 1410 }
                                                   >> 1411 
1396 /*                                               1412 /*
1397  * The refcount cpos are ordered by their 64b    1413  * The refcount cpos are ordered by their 64bit cpos,
1398  * But we will use the low 32 bit to be the e    1414  * But we will use the low 32 bit to be the e_cpos in the b-tree.
1399  * So we need to make sure that this pos isn'    1415  * So we need to make sure that this pos isn't intersected with others.
1400  *                                               1416  *
1401  * Note: The refcount block is already sorted    1417  * Note: The refcount block is already sorted by their low 32 bit cpos,
1402  *       So just try the middle pos first, an    1418  *       So just try the middle pos first, and we will exit when we find
1403  *       the good position.                      1419  *       the good position.
1404  */                                              1420  */
1405 static int ocfs2_find_refcount_split_pos(stru    1421 static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
1406                                          u32     1422                                          u32 *split_pos, int *split_index)
1407 {                                                1423 {
1408         int num_used = le16_to_cpu(rl->rl_use    1424         int num_used = le16_to_cpu(rl->rl_used);
1409         int delta, middle = num_used / 2;        1425         int delta, middle = num_used / 2;
1410                                                  1426 
1411         for (delta = 0; delta < middle; delta    1427         for (delta = 0; delta < middle; delta++) {
1412                 /* Let's check delta earlier     1428                 /* Let's check delta earlier than middle */
1413                 if (ocfs2_refcount_rec_no_int    1429                 if (ocfs2_refcount_rec_no_intersect(
1414                                         &rl->    1430                                         &rl->rl_recs[middle - delta - 1],
1415                                         &rl->    1431                                         &rl->rl_recs[middle - delta])) {
1416                         *split_index = middle    1432                         *split_index = middle - delta;
1417                         break;                   1433                         break;
1418                 }                                1434                 }
1419                                                  1435 
1420                 /* For even counts, don't wal    1436                 /* For even counts, don't walk off the end */
1421                 if ((middle + delta + 1) == n    1437                 if ((middle + delta + 1) == num_used)
1422                         continue;                1438                         continue;
1423                                                  1439 
1424                 /* Now try delta past middle     1440                 /* Now try delta past middle */
1425                 if (ocfs2_refcount_rec_no_int    1441                 if (ocfs2_refcount_rec_no_intersect(
1426                                         &rl->    1442                                         &rl->rl_recs[middle + delta],
1427                                         &rl->    1443                                         &rl->rl_recs[middle + delta + 1])) {
1428                         *split_index = middle    1444                         *split_index = middle + delta + 1;
1429                         break;                   1445                         break;
1430                 }                                1446                 }
1431         }                                        1447         }
1432                                                  1448 
1433         if (delta >= middle)                     1449         if (delta >= middle)
1434                 return -ENOSPC;                  1450                 return -ENOSPC;
1435                                                  1451 
1436         *split_pos = ocfs2_get_ref_rec_low_cp    1452         *split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
1437         return 0;                                1453         return 0;
1438 }                                                1454 }
1439                                                  1455 
1440 static int ocfs2_divide_leaf_refcount_block(s    1456 static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1441                                             s    1457                                             struct buffer_head *new_bh,
1442                                             u    1458                                             u32 *split_cpos)
1443 {                                                1459 {
1444         int split_index = 0, num_moved, ret;     1460         int split_index = 0, num_moved, ret;
1445         u32 cpos = 0;                            1461         u32 cpos = 0;
1446         struct ocfs2_refcount_block *rb =        1462         struct ocfs2_refcount_block *rb =
1447                         (struct ocfs2_refcoun    1463                         (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1448         struct ocfs2_refcount_list *rl = &rb-    1464         struct ocfs2_refcount_list *rl = &rb->rf_records;
1449         struct ocfs2_refcount_block *new_rb =    1465         struct ocfs2_refcount_block *new_rb =
1450                         (struct ocfs2_refcoun    1466                         (struct ocfs2_refcount_block *)new_bh->b_data;
1451         struct ocfs2_refcount_list *new_rl =     1467         struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
1452                                                  1468 
1453         trace_ocfs2_divide_leaf_refcount_bloc    1469         trace_ocfs2_divide_leaf_refcount_block(
1454                 (unsigned long long)ref_leaf_    1470                 (unsigned long long)ref_leaf_bh->b_blocknr,
1455                 le16_to_cpu(rl->rl_count), le    1471                 le16_to_cpu(rl->rl_count), le16_to_cpu(rl->rl_used));
1456                                                  1472 
1457         /*                                       1473         /*
1458          * XXX: Improvement later.               1474          * XXX: Improvement later.
1459          * If we know all the high 32 bit cpo    1475          * If we know all the high 32 bit cpos is the same, no need to sort.
1460          *                                       1476          *
1461          * In order to make the whole process    1477          * In order to make the whole process safe, we do:
1462          * 1. sort the entries by their low 3    1478          * 1. sort the entries by their low 32 bit cpos first so that we can
1463          *    find the split cpos easily.        1479          *    find the split cpos easily.
1464          * 2. call ocfs2_insert_extent to ins    1480          * 2. call ocfs2_insert_extent to insert the new refcount block.
1465          * 3. move the refcount rec to the ne    1481          * 3. move the refcount rec to the new block.
1466          * 4. sort the entries by their 64 bi    1482          * 4. sort the entries by their 64 bit cpos.
1467          * 5. dirty the new_rb and rb.           1483          * 5. dirty the new_rb and rb.
1468          */                                      1484          */
1469         sort(&rl->rl_recs, le16_to_cpu(rl->rl    1485         sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1470              sizeof(struct ocfs2_refcount_rec    1486              sizeof(struct ocfs2_refcount_rec),
1471              cmp_refcount_rec_by_low_cpos, NU !! 1487              cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1472                                                  1488 
1473         ret = ocfs2_find_refcount_split_pos(r    1489         ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1474         if (ret) {                               1490         if (ret) {
1475                 mlog_errno(ret);                 1491                 mlog_errno(ret);
1476                 return ret;                      1492                 return ret;
1477         }                                        1493         }
1478                                                  1494 
1479         new_rb->rf_cpos = cpu_to_le32(cpos);     1495         new_rb->rf_cpos = cpu_to_le32(cpos);
1480                                                  1496 
1481         /* move refcount records starting fro    1497         /* move refcount records starting from split_index to the new block. */
1482         num_moved = le16_to_cpu(rl->rl_used)     1498         num_moved = le16_to_cpu(rl->rl_used) - split_index;
1483         memcpy(new_rl->rl_recs, &rl->rl_recs[    1499         memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
1484                num_moved * sizeof(struct ocfs    1500                num_moved * sizeof(struct ocfs2_refcount_rec));
1485                                                  1501 
1486         /*ok, remove the entries we just move    1502         /*ok, remove the entries we just moved over to the other block. */
1487         memset(&rl->rl_recs[split_index], 0,     1503         memset(&rl->rl_recs[split_index], 0,
1488                num_moved * sizeof(struct ocfs    1504                num_moved * sizeof(struct ocfs2_refcount_rec));
1489                                                  1505 
1490         /* change old and new rl_used accordi    1506         /* change old and new rl_used accordingly. */
1491         le16_add_cpu(&rl->rl_used, -num_moved    1507         le16_add_cpu(&rl->rl_used, -num_moved);
1492         new_rl->rl_used = cpu_to_le16(num_mov    1508         new_rl->rl_used = cpu_to_le16(num_moved);
1493                                                  1509 
1494         sort(&rl->rl_recs, le16_to_cpu(rl->rl    1510         sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1495              sizeof(struct ocfs2_refcount_rec    1511              sizeof(struct ocfs2_refcount_rec),
1496              cmp_refcount_rec_by_cpos, NULL); !! 1512              cmp_refcount_rec_by_cpos, swap_refcount_rec);
1497                                                  1513 
1498         sort(&new_rl->rl_recs, le16_to_cpu(ne    1514         sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
1499              sizeof(struct ocfs2_refcount_rec    1515              sizeof(struct ocfs2_refcount_rec),
1500              cmp_refcount_rec_by_cpos, NULL); !! 1516              cmp_refcount_rec_by_cpos, swap_refcount_rec);
1501                                                  1517 
1502         *split_cpos = cpos;                      1518         *split_cpos = cpos;
1503         return 0;                                1519         return 0;
1504 }                                                1520 }
1505                                                  1521 
1506 static int ocfs2_new_leaf_refcount_block(hand    1522 static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1507                                          stru    1523                                          struct ocfs2_caching_info *ci,
1508                                          stru    1524                                          struct buffer_head *ref_root_bh,
1509                                          stru    1525                                          struct buffer_head *ref_leaf_bh,
1510                                          stru    1526                                          struct ocfs2_alloc_context *meta_ac)
1511 {                                                1527 {
1512         int ret;                                 1528         int ret;
1513         u16 suballoc_bit_start;                  1529         u16 suballoc_bit_start;
1514         u32 num_got, new_cpos;                   1530         u32 num_got, new_cpos;
1515         u64 suballoc_loc, blkno;                 1531         u64 suballoc_loc, blkno;
1516         struct super_block *sb = ocfs2_metada    1532         struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1517         struct ocfs2_refcount_block *root_rb     1533         struct ocfs2_refcount_block *root_rb =
1518                         (struct ocfs2_refcoun    1534                         (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1519         struct buffer_head *new_bh = NULL;       1535         struct buffer_head *new_bh = NULL;
1520         struct ocfs2_refcount_block *new_rb;     1536         struct ocfs2_refcount_block *new_rb;
1521         struct ocfs2_extent_tree ref_et;         1537         struct ocfs2_extent_tree ref_et;
1522                                                  1538 
1523         BUG_ON(!(le32_to_cpu(root_rb->rf_flag    1539         BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
1524                                                  1540 
1525         ret = ocfs2_journal_access_rb(handle,    1541         ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1526                                       OCFS2_J    1542                                       OCFS2_JOURNAL_ACCESS_WRITE);
1527         if (ret) {                               1543         if (ret) {
1528                 mlog_errno(ret);                 1544                 mlog_errno(ret);
1529                 goto out;                        1545                 goto out;
1530         }                                        1546         }
1531                                                  1547 
1532         ret = ocfs2_journal_access_rb(handle,    1548         ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1533                                       OCFS2_J    1549                                       OCFS2_JOURNAL_ACCESS_WRITE);
1534         if (ret) {                               1550         if (ret) {
1535                 mlog_errno(ret);                 1551                 mlog_errno(ret);
1536                 goto out;                        1552                 goto out;
1537         }                                        1553         }
1538                                                  1554 
1539         ret = ocfs2_claim_metadata(handle, me    1555         ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1540                                    &suballoc_    1556                                    &suballoc_bit_start, &num_got,
1541                                    &blkno);      1557                                    &blkno);
1542         if (ret) {                               1558         if (ret) {
1543                 mlog_errno(ret);                 1559                 mlog_errno(ret);
1544                 goto out;                        1560                 goto out;
1545         }                                        1561         }
1546                                                  1562 
1547         new_bh = sb_getblk(sb, blkno);           1563         new_bh = sb_getblk(sb, blkno);
1548         if (new_bh == NULL) {                    1564         if (new_bh == NULL) {
1549                 ret = -ENOMEM;                !! 1565                 ret = -EIO;
1550                 mlog_errno(ret);                 1566                 mlog_errno(ret);
1551                 goto out;                        1567                 goto out;
1552         }                                        1568         }
1553         ocfs2_set_new_buffer_uptodate(ci, new    1569         ocfs2_set_new_buffer_uptodate(ci, new_bh);
1554                                                  1570 
1555         ret = ocfs2_journal_access_rb(handle,    1571         ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1556                                       OCFS2_J    1572                                       OCFS2_JOURNAL_ACCESS_CREATE);
1557         if (ret) {                               1573         if (ret) {
1558                 mlog_errno(ret);                 1574                 mlog_errno(ret);
1559                 goto out;                        1575                 goto out;
1560         }                                        1576         }
1561                                                  1577 
1562         /* Initialize ocfs2_refcount_block. *    1578         /* Initialize ocfs2_refcount_block. */
1563         new_rb = (struct ocfs2_refcount_block    1579         new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1564         memset(new_rb, 0, sb->s_blocksize);      1580         memset(new_rb, 0, sb->s_blocksize);
1565         strcpy((void *)new_rb, OCFS2_REFCOUNT    1581         strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1566         new_rb->rf_suballoc_slot = cpu_to_le1    1582         new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1567         new_rb->rf_suballoc_loc = cpu_to_le64    1583         new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1568         new_rb->rf_suballoc_bit = cpu_to_le16    1584         new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1569         new_rb->rf_fs_generation = cpu_to_le3    1585         new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1570         new_rb->rf_blkno = cpu_to_le64(blkno)    1586         new_rb->rf_blkno = cpu_to_le64(blkno);
1571         new_rb->rf_parent = cpu_to_le64(ref_r    1587         new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1572         new_rb->rf_flags = cpu_to_le32(OCFS2_    1588         new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1573         new_rb->rf_records.rl_count =            1589         new_rb->rf_records.rl_count =
1574                                 cpu_to_le16(o    1590                                 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
1575         new_rb->rf_generation = root_rb->rf_g    1591         new_rb->rf_generation = root_rb->rf_generation;
1576                                                  1592 
1577         ret = ocfs2_divide_leaf_refcount_bloc    1593         ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1578         if (ret) {                               1594         if (ret) {
1579                 mlog_errno(ret);                 1595                 mlog_errno(ret);
1580                 goto out;                        1596                 goto out;
1581         }                                        1597         }
1582                                                  1598 
1583         ocfs2_journal_dirty(handle, ref_leaf_    1599         ocfs2_journal_dirty(handle, ref_leaf_bh);
1584         ocfs2_journal_dirty(handle, new_bh);     1600         ocfs2_journal_dirty(handle, new_bh);
1585                                                  1601 
1586         ocfs2_init_refcount_extent_tree(&ref_    1602         ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
1587                                                  1603 
1588         trace_ocfs2_new_leaf_refcount_block(     1604         trace_ocfs2_new_leaf_refcount_block(
1589                         (unsigned long long)n    1605                         (unsigned long long)new_bh->b_blocknr, new_cpos);
1590                                                  1606 
1591         /* Insert the new leaf block with the    1607         /* Insert the new leaf block with the specific offset cpos. */
1592         ret = ocfs2_insert_extent(handle, &re    1608         ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
1593                                   1, 0, meta_    1609                                   1, 0, meta_ac);
1594         if (ret)                                 1610         if (ret)
1595                 mlog_errno(ret);                 1611                 mlog_errno(ret);
1596                                                  1612 
1597 out:                                             1613 out:
1598         brelse(new_bh);                          1614         brelse(new_bh);
1599         return ret;                              1615         return ret;
1600 }                                                1616 }
1601                                                  1617 
1602 static int ocfs2_expand_refcount_tree(handle_    1618 static int ocfs2_expand_refcount_tree(handle_t *handle,
1603                                       struct     1619                                       struct ocfs2_caching_info *ci,
1604                                       struct     1620                                       struct buffer_head *ref_root_bh,
1605                                       struct     1621                                       struct buffer_head *ref_leaf_bh,
1606                                       struct     1622                                       struct ocfs2_alloc_context *meta_ac)
1607 {                                                1623 {
1608         int ret;                                 1624         int ret;
1609         struct buffer_head *expand_bh = NULL;    1625         struct buffer_head *expand_bh = NULL;
1610                                                  1626 
1611         if (ref_root_bh == ref_leaf_bh) {        1627         if (ref_root_bh == ref_leaf_bh) {
1612                 /*                               1628                 /*
1613                  * the old root bh hasn't bee    1629                  * the old root bh hasn't been expanded to a b-tree,
1614                  * so expand it first.           1630                  * so expand it first.
1615                  */                              1631                  */
1616                 ret = ocfs2_expand_inline_ref    1632                 ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1617                                                  1633                                                    &expand_bh, meta_ac);
1618                 if (ret) {                       1634                 if (ret) {
1619                         mlog_errno(ret);         1635                         mlog_errno(ret);
1620                         goto out;                1636                         goto out;
1621                 }                                1637                 }
1622         } else {                                 1638         } else {
1623                 expand_bh = ref_leaf_bh;         1639                 expand_bh = ref_leaf_bh;
1624                 get_bh(expand_bh);               1640                 get_bh(expand_bh);
1625         }                                        1641         }
1626                                                  1642 
1627                                                  1643 
1628         /* Now add a new refcount block into     1644         /* Now add a new refcount block into the tree.*/
1629         ret = ocfs2_new_leaf_refcount_block(h    1645         ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1630                                             e    1646                                             expand_bh, meta_ac);
1631         if (ret)                                 1647         if (ret)
1632                 mlog_errno(ret);                 1648                 mlog_errno(ret);
1633 out:                                             1649 out:
1634         brelse(expand_bh);                       1650         brelse(expand_bh);
1635         return ret;                              1651         return ret;
1636 }                                                1652 }
1637                                                  1653 
1638 /*                                               1654 /*
1639  * Adjust the extent rec in b-tree representi    1655  * Adjust the extent rec in b-tree representing ref_leaf_bh.
1640  *                                               1656  *
1641  * Only called when we have inserted a new re    1657  * Only called when we have inserted a new refcount rec at index 0
1642  * which means ocfs2_extent_rec.e_cpos may ne    1658  * which means ocfs2_extent_rec.e_cpos may need some change.
1643  */                                              1659  */
1644 static int ocfs2_adjust_refcount_rec(handle_t    1660 static int ocfs2_adjust_refcount_rec(handle_t *handle,
1645                                      struct o    1661                                      struct ocfs2_caching_info *ci,
1646                                      struct b    1662                                      struct buffer_head *ref_root_bh,
1647                                      struct b    1663                                      struct buffer_head *ref_leaf_bh,
1648                                      struct o    1664                                      struct ocfs2_refcount_rec *rec)
1649 {                                                1665 {
1650         int ret = 0, i;                          1666         int ret = 0, i;
1651         u32 new_cpos, old_cpos;                  1667         u32 new_cpos, old_cpos;
1652         struct ocfs2_path *path = NULL;          1668         struct ocfs2_path *path = NULL;
1653         struct ocfs2_extent_tree et;             1669         struct ocfs2_extent_tree et;
1654         struct ocfs2_refcount_block *rb =        1670         struct ocfs2_refcount_block *rb =
1655                 (struct ocfs2_refcount_block     1671                 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1656         struct ocfs2_extent_list *el;            1672         struct ocfs2_extent_list *el;
1657                                                  1673 
1658         if (!(le32_to_cpu(rb->rf_flags) & OCF    1674         if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
1659                 goto out;                        1675                 goto out;
1660                                                  1676 
1661         rb = (struct ocfs2_refcount_block *)r    1677         rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1662         old_cpos = le32_to_cpu(rb->rf_cpos);     1678         old_cpos = le32_to_cpu(rb->rf_cpos);
1663         new_cpos = le64_to_cpu(rec->r_cpos) &    1679         new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
1664         if (old_cpos <= new_cpos)                1680         if (old_cpos <= new_cpos)
1665                 goto out;                        1681                 goto out;
1666                                                  1682 
1667         ocfs2_init_refcount_extent_tree(&et,     1683         ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
1668                                                  1684 
1669         path = ocfs2_new_path_from_et(&et);      1685         path = ocfs2_new_path_from_et(&et);
1670         if (!path) {                             1686         if (!path) {
1671                 ret = -ENOMEM;                   1687                 ret = -ENOMEM;
1672                 mlog_errno(ret);                 1688                 mlog_errno(ret);
1673                 goto out;                        1689                 goto out;
1674         }                                        1690         }
1675                                                  1691 
1676         ret = ocfs2_find_path(ci, path, old_c    1692         ret = ocfs2_find_path(ci, path, old_cpos);
1677         if (ret) {                               1693         if (ret) {
1678                 mlog_errno(ret);                 1694                 mlog_errno(ret);
1679                 goto out;                        1695                 goto out;
1680         }                                        1696         }
1681                                                  1697 
1682         /*                                       1698         /*
1683          * 2 more credits, one for the leaf r    1699          * 2 more credits, one for the leaf refcount block, one for
1684          * the extent block contains the exte    1700          * the extent block contains the extent rec.
1685          */                                      1701          */
1686         ret = ocfs2_extend_trans(handle, 2);     1702         ret = ocfs2_extend_trans(handle, 2);
1687         if (ret < 0) {                           1703         if (ret < 0) {
1688                 mlog_errno(ret);                 1704                 mlog_errno(ret);
1689                 goto out;                        1705                 goto out;
1690         }                                        1706         }
1691                                                  1707 
1692         ret = ocfs2_journal_access_rb(handle,    1708         ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1693                                       OCFS2_J    1709                                       OCFS2_JOURNAL_ACCESS_WRITE);
1694         if (ret < 0) {                           1710         if (ret < 0) {
1695                 mlog_errno(ret);                 1711                 mlog_errno(ret);
1696                 goto out;                        1712                 goto out;
1697         }                                        1713         }
1698                                                  1714 
1699         ret = ocfs2_journal_access_eb(handle,    1715         ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
1700                                       OCFS2_J    1716                                       OCFS2_JOURNAL_ACCESS_WRITE);
1701         if (ret < 0) {                           1717         if (ret < 0) {
1702                 mlog_errno(ret);                 1718                 mlog_errno(ret);
1703                 goto out;                        1719                 goto out;
1704         }                                        1720         }
1705                                                  1721 
1706         /* change the leaf extent block first    1722         /* change the leaf extent block first. */
1707         el = path_leaf_el(path);                 1723         el = path_leaf_el(path);
1708                                                  1724 
1709         for (i = 0; i < le16_to_cpu(el->l_nex    1725         for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
1710                 if (le32_to_cpu(el->l_recs[i]    1726                 if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
1711                         break;                   1727                         break;
1712                                                  1728 
1713         BUG_ON(i == le16_to_cpu(el->l_next_fr    1729         BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
1714                                                  1730 
1715         el->l_recs[i].e_cpos = cpu_to_le32(ne    1731         el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1716                                                  1732 
1717         /* change the r_cpos in the leaf bloc    1733         /* change the r_cpos in the leaf block. */
1718         rb->rf_cpos = cpu_to_le32(new_cpos);     1734         rb->rf_cpos = cpu_to_le32(new_cpos);
1719                                                  1735 
1720         ocfs2_journal_dirty(handle, path_leaf    1736         ocfs2_journal_dirty(handle, path_leaf_bh(path));
1721         ocfs2_journal_dirty(handle, ref_leaf_    1737         ocfs2_journal_dirty(handle, ref_leaf_bh);
1722                                                  1738 
1723 out:                                             1739 out:
1724         ocfs2_free_path(path);                   1740         ocfs2_free_path(path);
1725         return ret;                              1741         return ret;
1726 }                                                1742 }
1727                                                  1743 
1728 static int ocfs2_insert_refcount_rec(handle_t    1744 static int ocfs2_insert_refcount_rec(handle_t *handle,
1729                                      struct o    1745                                      struct ocfs2_caching_info *ci,
1730                                      struct b    1746                                      struct buffer_head *ref_root_bh,
1731                                      struct b    1747                                      struct buffer_head *ref_leaf_bh,
1732                                      struct o    1748                                      struct ocfs2_refcount_rec *rec,
1733                                      int inde    1749                                      int index, int merge,
1734                                      struct o    1750                                      struct ocfs2_alloc_context *meta_ac)
1735 {                                                1751 {
1736         int ret;                                 1752         int ret;
1737         struct ocfs2_refcount_block *rb =        1753         struct ocfs2_refcount_block *rb =
1738                         (struct ocfs2_refcoun    1754                         (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1739         struct ocfs2_refcount_list *rf_list =    1755         struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1740         struct buffer_head *new_bh = NULL;       1756         struct buffer_head *new_bh = NULL;
1741                                                  1757 
1742         BUG_ON(le32_to_cpu(rb->rf_flags) & OC    1758         BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1743                                                  1759 
1744         if (rf_list->rl_used == rf_list->rl_c    1760         if (rf_list->rl_used == rf_list->rl_count) {
1745                 u64 cpos = le64_to_cpu(rec->r    1761                 u64 cpos = le64_to_cpu(rec->r_cpos);
1746                 u32 len = le32_to_cpu(rec->r_    1762                 u32 len = le32_to_cpu(rec->r_clusters);
1747                                                  1763 
1748                 ret = ocfs2_expand_refcount_t    1764                 ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1749                                                  1765                                                  ref_leaf_bh, meta_ac);
1750                 if (ret) {                       1766                 if (ret) {
1751                         mlog_errno(ret);         1767                         mlog_errno(ret);
1752                         goto out;                1768                         goto out;
1753                 }                                1769                 }
1754                                                  1770 
1755                 ret = ocfs2_get_refcount_rec(    1771                 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1756                                                  1772                                              cpos, len, NULL, &index,
1757                                                  1773                                              &new_bh);
1758                 if (ret) {                       1774                 if (ret) {
1759                         mlog_errno(ret);         1775                         mlog_errno(ret);
1760                         goto out;                1776                         goto out;
1761                 }                                1777                 }
1762                                                  1778 
1763                 ref_leaf_bh = new_bh;            1779                 ref_leaf_bh = new_bh;
1764                 rb = (struct ocfs2_refcount_b    1780                 rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1765                 rf_list = &rb->rf_records;       1781                 rf_list = &rb->rf_records;
1766         }                                        1782         }
1767                                                  1783 
1768         ret = ocfs2_journal_access_rb(handle,    1784         ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1769                                       OCFS2_J    1785                                       OCFS2_JOURNAL_ACCESS_WRITE);
1770         if (ret) {                               1786         if (ret) {
1771                 mlog_errno(ret);                 1787                 mlog_errno(ret);
1772                 goto out;                        1788                 goto out;
1773         }                                        1789         }
1774                                                  1790 
1775         if (index < le16_to_cpu(rf_list->rl_u    1791         if (index < le16_to_cpu(rf_list->rl_used))
1776                 memmove(&rf_list->rl_recs[ind    1792                 memmove(&rf_list->rl_recs[index + 1],
1777                         &rf_list->rl_recs[ind    1793                         &rf_list->rl_recs[index],
1778                         (le16_to_cpu(rf_list-    1794                         (le16_to_cpu(rf_list->rl_used) - index) *
1779                          sizeof(struct ocfs2_    1795                          sizeof(struct ocfs2_refcount_rec));
1780                                                  1796 
1781         trace_ocfs2_insert_refcount_rec(         1797         trace_ocfs2_insert_refcount_rec(
1782                 (unsigned long long)ref_leaf_    1798                 (unsigned long long)ref_leaf_bh->b_blocknr, index,
1783                 (unsigned long long)le64_to_c    1799                 (unsigned long long)le64_to_cpu(rec->r_cpos),
1784                 le32_to_cpu(rec->r_clusters),    1800                 le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount));
1785                                                  1801 
1786         rf_list->rl_recs[index] = *rec;          1802         rf_list->rl_recs[index] = *rec;
1787                                                  1803 
1788         le16_add_cpu(&rf_list->rl_used, 1);      1804         le16_add_cpu(&rf_list->rl_used, 1);
1789                                                  1805 
1790         if (merge)                               1806         if (merge)
1791                 ocfs2_refcount_rec_merge(rb,     1807                 ocfs2_refcount_rec_merge(rb, index);
1792                                                  1808 
1793         ocfs2_journal_dirty(handle, ref_leaf_    1809         ocfs2_journal_dirty(handle, ref_leaf_bh);
1794                                                  1810 
1795         if (index == 0) {                        1811         if (index == 0) {
1796                 ret = ocfs2_adjust_refcount_r    1812                 ret = ocfs2_adjust_refcount_rec(handle, ci,
1797                                                  1813                                                 ref_root_bh,
1798                                                  1814                                                 ref_leaf_bh, rec);
1799                 if (ret)                         1815                 if (ret)
1800                         mlog_errno(ret);         1816                         mlog_errno(ret);
1801         }                                        1817         }
1802 out:                                             1818 out:
1803         brelse(new_bh);                          1819         brelse(new_bh);
1804         return ret;                              1820         return ret;
1805 }                                                1821 }
1806                                                  1822 
1807 /*                                               1823 /*
1808  * Split the refcount_rec indexed by "index"     1824  * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1809  * This is much simple than our b-tree code.     1825  * This is much simple than our b-tree code.
1810  * split_rec is the new refcount rec we want     1826  * split_rec is the new refcount rec we want to insert.
1811  * If split_rec->r_refcount > 0, we are chang    1827  * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1812  * increase refcount or decrease a refcount t    1828  * increase refcount or decrease a refcount to non-zero).
1813  * If split_rec->r_refcount == 0, we are punc    1829  * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1814  * rec( in case we decrease a refcount to zer    1830  * rec( in case we decrease a refcount to zero).
1815  */                                              1831  */
1816 static int ocfs2_split_refcount_rec(handle_t     1832 static int ocfs2_split_refcount_rec(handle_t *handle,
1817                                     struct oc    1833                                     struct ocfs2_caching_info *ci,
1818                                     struct bu    1834                                     struct buffer_head *ref_root_bh,
1819                                     struct bu    1835                                     struct buffer_head *ref_leaf_bh,
1820                                     struct oc    1836                                     struct ocfs2_refcount_rec *split_rec,
1821                                     int index    1837                                     int index, int merge,
1822                                     struct oc    1838                                     struct ocfs2_alloc_context *meta_ac,
1823                                     struct oc    1839                                     struct ocfs2_cached_dealloc_ctxt *dealloc)
1824 {                                                1840 {
1825         int ret, recs_need;                      1841         int ret, recs_need;
1826         u32 len;                                 1842         u32 len;
1827         struct ocfs2_refcount_block *rb =        1843         struct ocfs2_refcount_block *rb =
1828                         (struct ocfs2_refcoun    1844                         (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1829         struct ocfs2_refcount_list *rf_list =    1845         struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1830         struct ocfs2_refcount_rec *orig_rec =    1846         struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
1831         struct ocfs2_refcount_rec *tail_rec =    1847         struct ocfs2_refcount_rec *tail_rec = NULL;
1832         struct buffer_head *new_bh = NULL;       1848         struct buffer_head *new_bh = NULL;
1833                                                  1849 
1834         BUG_ON(le32_to_cpu(rb->rf_flags) & OC    1850         BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1835                                                  1851 
1836         trace_ocfs2_split_refcount_rec(le64_t    1852         trace_ocfs2_split_refcount_rec(le64_to_cpu(orig_rec->r_cpos),
1837                 le32_to_cpu(orig_rec->r_clust    1853                 le32_to_cpu(orig_rec->r_clusters),
1838                 le32_to_cpu(orig_rec->r_refco    1854                 le32_to_cpu(orig_rec->r_refcount),
1839                 le64_to_cpu(split_rec->r_cpos    1855                 le64_to_cpu(split_rec->r_cpos),
1840                 le32_to_cpu(split_rec->r_clus    1856                 le32_to_cpu(split_rec->r_clusters),
1841                 le32_to_cpu(split_rec->r_refc    1857                 le32_to_cpu(split_rec->r_refcount));
1842                                                  1858 
1843         /*                                       1859         /*
1844          * If we just need to split the heade    1860          * If we just need to split the header or tail clusters,
1845          * no more recs are needed, just spli    1861          * no more recs are needed, just split is OK.
1846          * Otherwise we at least need one new    1862          * Otherwise we at least need one new recs.
1847          */                                      1863          */
1848         if (!split_rec->r_refcount &&            1864         if (!split_rec->r_refcount &&
1849             (split_rec->r_cpos == orig_rec->r    1865             (split_rec->r_cpos == orig_rec->r_cpos ||
1850              le64_to_cpu(split_rec->r_cpos) +    1866              le64_to_cpu(split_rec->r_cpos) +
1851              le32_to_cpu(split_rec->r_cluster    1867              le32_to_cpu(split_rec->r_clusters) ==
1852              le64_to_cpu(orig_rec->r_cpos) +     1868              le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1853                 recs_need = 0;                   1869                 recs_need = 0;
1854         else                                     1870         else
1855                 recs_need = 1;                   1871                 recs_need = 1;
1856                                                  1872 
1857         /*                                       1873         /*
1858          * We need one more rec if we split i    1874          * We need one more rec if we split in the middle and the new rec have
1859          * some refcount in it.                  1875          * some refcount in it.
1860          */                                      1876          */
1861         if (split_rec->r_refcount &&             1877         if (split_rec->r_refcount &&
1862             (split_rec->r_cpos != orig_rec->r    1878             (split_rec->r_cpos != orig_rec->r_cpos &&
1863              le64_to_cpu(split_rec->r_cpos) +    1879              le64_to_cpu(split_rec->r_cpos) +
1864              le32_to_cpu(split_rec->r_cluster    1880              le32_to_cpu(split_rec->r_clusters) !=
1865              le64_to_cpu(orig_rec->r_cpos) +     1881              le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1866                 recs_need++;                     1882                 recs_need++;
1867                                                  1883 
1868         /* If the leaf block don't have enoug    1884         /* If the leaf block don't have enough record, expand it. */
1869         if (le16_to_cpu(rf_list->rl_used) + r    1885         if (le16_to_cpu(rf_list->rl_used) + recs_need >
1870                                          le16    1886                                          le16_to_cpu(rf_list->rl_count)) {
1871                 struct ocfs2_refcount_rec tmp    1887                 struct ocfs2_refcount_rec tmp_rec;
1872                 u64 cpos = le64_to_cpu(orig_r    1888                 u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1873                 len = le32_to_cpu(orig_rec->r    1889                 len = le32_to_cpu(orig_rec->r_clusters);
1874                 ret = ocfs2_expand_refcount_t    1890                 ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1875                                                  1891                                                  ref_leaf_bh, meta_ac);
1876                 if (ret) {                       1892                 if (ret) {
1877                         mlog_errno(ret);         1893                         mlog_errno(ret);
1878                         goto out;                1894                         goto out;
1879                 }                                1895                 }
1880                                                  1896 
1881                 /*                               1897                 /*
1882                  * We have to re-get it since    1898                  * We have to re-get it since now cpos may be moved to
1883                  * another leaf block.           1899                  * another leaf block.
1884                  */                              1900                  */
1885                 ret = ocfs2_get_refcount_rec(    1901                 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1886                                                  1902                                              cpos, len, &tmp_rec, &index,
1887                                                  1903                                              &new_bh);
1888                 if (ret) {                       1904                 if (ret) {
1889                         mlog_errno(ret);         1905                         mlog_errno(ret);
1890                         goto out;                1906                         goto out;
1891                 }                                1907                 }
1892                                                  1908 
1893                 ref_leaf_bh = new_bh;            1909                 ref_leaf_bh = new_bh;
1894                 rb = (struct ocfs2_refcount_b    1910                 rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1895                 rf_list = &rb->rf_records;       1911                 rf_list = &rb->rf_records;
1896                 orig_rec = &rf_list->rl_recs[    1912                 orig_rec = &rf_list->rl_recs[index];
1897         }                                        1913         }
1898                                                  1914 
1899         ret = ocfs2_journal_access_rb(handle,    1915         ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1900                                       OCFS2_J    1916                                       OCFS2_JOURNAL_ACCESS_WRITE);
1901         if (ret) {                               1917         if (ret) {
1902                 mlog_errno(ret);                 1918                 mlog_errno(ret);
1903                 goto out;                        1919                 goto out;
1904         }                                        1920         }
1905                                                  1921 
1906         /*                                       1922         /*
1907          * We have calculated out how many ne    1923          * We have calculated out how many new records we need and store
1908          * in recs_need, so spare enough spac    1924          * in recs_need, so spare enough space first by moving the records
1909          * after "index" to the end.             1925          * after "index" to the end.
1910          */                                      1926          */
1911         if (index != le16_to_cpu(rf_list->rl_    1927         if (index != le16_to_cpu(rf_list->rl_used) - 1)
1912                 memmove(&rf_list->rl_recs[ind    1928                 memmove(&rf_list->rl_recs[index + 1 + recs_need],
1913                         &rf_list->rl_recs[ind    1929                         &rf_list->rl_recs[index + 1],
1914                         (le16_to_cpu(rf_list-    1930                         (le16_to_cpu(rf_list->rl_used) - index - 1) *
1915                          sizeof(struct ocfs2_    1931                          sizeof(struct ocfs2_refcount_rec));
1916                                                  1932 
1917         len = (le64_to_cpu(orig_rec->r_cpos)     1933         len = (le64_to_cpu(orig_rec->r_cpos) +
1918               le32_to_cpu(orig_rec->r_cluster    1934               le32_to_cpu(orig_rec->r_clusters)) -
1919               (le64_to_cpu(split_rec->r_cpos)    1935               (le64_to_cpu(split_rec->r_cpos) +
1920               le32_to_cpu(split_rec->r_cluste    1936               le32_to_cpu(split_rec->r_clusters));
1921                                                  1937 
1922         /*                                       1938         /*
1923          * If we have "len", the we will spli    1939          * If we have "len", the we will split in the tail and move it
1924          * to the end of the space we have ju    1940          * to the end of the space we have just spared.
1925          */                                      1941          */
1926         if (len) {                               1942         if (len) {
1927                 tail_rec = &rf_list->rl_recs[    1943                 tail_rec = &rf_list->rl_recs[index + recs_need];
1928                                                  1944 
1929                 memcpy(tail_rec, orig_rec, si    1945                 memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1930                 le64_add_cpu(&tail_rec->r_cpo    1946                 le64_add_cpu(&tail_rec->r_cpos,
1931                              le32_to_cpu(tail    1947                              le32_to_cpu(tail_rec->r_clusters) - len);
1932                 tail_rec->r_clusters = cpu_to    1948                 tail_rec->r_clusters = cpu_to_le32(len);
1933         }                                        1949         }
1934                                                  1950 
1935         /*                                       1951         /*
1936          * If the split pos isn't the same as    1952          * If the split pos isn't the same as the original one, we need to
1937          * split in the head.                    1953          * split in the head.
1938          *                                       1954          *
1939          * Note: We have the chance that spli    1955          * Note: We have the chance that split_rec.r_refcount = 0,
1940          * recs_need = 0 and len > 0, which m    1956          * recs_need = 0 and len > 0, which means we just cut the head from
1941          * the orig_rec and in that case we h    1957          * the orig_rec and in that case we have done some modification in
1942          * orig_rec above, so the check for r    1958          * orig_rec above, so the check for r_cpos is faked.
1943          */                                      1959          */
1944         if (split_rec->r_cpos != orig_rec->r_    1960         if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1945                 len = le64_to_cpu(split_rec->    1961                 len = le64_to_cpu(split_rec->r_cpos) -
1946                       le64_to_cpu(orig_rec->r    1962                       le64_to_cpu(orig_rec->r_cpos);
1947                 orig_rec->r_clusters = cpu_to    1963                 orig_rec->r_clusters = cpu_to_le32(len);
1948                 index++;                         1964                 index++;
1949         }                                        1965         }
1950                                                  1966 
1951         le16_add_cpu(&rf_list->rl_used, recs_    1967         le16_add_cpu(&rf_list->rl_used, recs_need);
1952                                                  1968 
1953         if (split_rec->r_refcount) {             1969         if (split_rec->r_refcount) {
1954                 rf_list->rl_recs[index] = *sp    1970                 rf_list->rl_recs[index] = *split_rec;
1955                 trace_ocfs2_split_refcount_re    1971                 trace_ocfs2_split_refcount_rec_insert(
1956                         (unsigned long long)r    1972                         (unsigned long long)ref_leaf_bh->b_blocknr, index,
1957                         (unsigned long long)l    1973                         (unsigned long long)le64_to_cpu(split_rec->r_cpos),
1958                         le32_to_cpu(split_rec    1974                         le32_to_cpu(split_rec->r_clusters),
1959                         le32_to_cpu(split_rec    1975                         le32_to_cpu(split_rec->r_refcount));
1960                                                  1976 
1961                 if (merge)                       1977                 if (merge)
1962                         ocfs2_refcount_rec_me    1978                         ocfs2_refcount_rec_merge(rb, index);
1963         }                                        1979         }
1964                                                  1980 
1965         ocfs2_journal_dirty(handle, ref_leaf_    1981         ocfs2_journal_dirty(handle, ref_leaf_bh);
1966                                                  1982 
1967 out:                                             1983 out:
1968         brelse(new_bh);                          1984         brelse(new_bh);
1969         return ret;                              1985         return ret;
1970 }                                                1986 }
1971                                                  1987 
1972 static int __ocfs2_increase_refcount(handle_t    1988 static int __ocfs2_increase_refcount(handle_t *handle,
1973                                      struct o    1989                                      struct ocfs2_caching_info *ci,
1974                                      struct b    1990                                      struct buffer_head *ref_root_bh,
1975                                      u64 cpos    1991                                      u64 cpos, u32 len, int merge,
1976                                      struct o    1992                                      struct ocfs2_alloc_context *meta_ac,
1977                                      struct o    1993                                      struct ocfs2_cached_dealloc_ctxt *dealloc)
1978 {                                                1994 {
1979         int ret = 0, index;                      1995         int ret = 0, index;
1980         struct buffer_head *ref_leaf_bh = NUL    1996         struct buffer_head *ref_leaf_bh = NULL;
1981         struct ocfs2_refcount_rec rec;           1997         struct ocfs2_refcount_rec rec;
1982         unsigned int set_len = 0;                1998         unsigned int set_len = 0;
1983                                                  1999 
1984         trace_ocfs2_increase_refcount_begin(     2000         trace_ocfs2_increase_refcount_begin(
1985              (unsigned long long)ocfs2_metada    2001              (unsigned long long)ocfs2_metadata_cache_owner(ci),
1986              (unsigned long long)cpos, len);     2002              (unsigned long long)cpos, len);
1987                                                  2003 
1988         while (len) {                            2004         while (len) {
1989                 ret = ocfs2_get_refcount_rec(    2005                 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1990                                                  2006                                              cpos, len, &rec, &index,
1991                                                  2007                                              &ref_leaf_bh);
1992                 if (ret) {                       2008                 if (ret) {
1993                         mlog_errno(ret);         2009                         mlog_errno(ret);
1994                         goto out;                2010                         goto out;
1995                 }                                2011                 }
1996                                                  2012 
1997                 set_len = le32_to_cpu(rec.r_c    2013                 set_len = le32_to_cpu(rec.r_clusters);
1998                                                  2014 
1999                 /*                               2015                 /*
2000                  * Here we may meet with 3 si    2016                  * Here we may meet with 3 situations:
2001                  *                               2017                  *
2002                  * 1. If we find an already e    2018                  * 1. If we find an already existing record, and the length
2003                  *    is the same, cool, we j    2019                  *    is the same, cool, we just need to increase the r_refcount
2004                  *    and it is OK.              2020                  *    and it is OK.
2005                  * 2. If we find a hole, just    2021                  * 2. If we find a hole, just insert it with r_refcount = 1.
2006                  * 3. If we are in the middle    2022                  * 3. If we are in the middle of one extent record, split
2007                  *    it.                        2023                  *    it.
2008                  */                              2024                  */
2009                 if (rec.r_refcount && le64_to    2025                 if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
2010                     set_len <= len) {            2026                     set_len <= len) {
2011                         trace_ocfs2_increase_    2027                         trace_ocfs2_increase_refcount_change(
2012                                 (unsigned lon    2028                                 (unsigned long long)cpos, set_len,
2013                                 le32_to_cpu(r    2029                                 le32_to_cpu(rec.r_refcount));
2014                         ret = ocfs2_change_re    2030                         ret = ocfs2_change_refcount_rec(handle, ci,
2015                                                  2031                                                         ref_leaf_bh, index,
2016                                                  2032                                                         merge, 1);
2017                         if (ret) {               2033                         if (ret) {
2018                                 mlog_errno(re    2034                                 mlog_errno(ret);
2019                                 goto out;        2035                                 goto out;
2020                         }                        2036                         }
2021                 } else if (!rec.r_refcount) {    2037                 } else if (!rec.r_refcount) {
2022                         rec.r_refcount = cpu_    2038                         rec.r_refcount = cpu_to_le32(1);
2023                                                  2039 
2024                         trace_ocfs2_increase_    2040                         trace_ocfs2_increase_refcount_insert(
2025                              (unsigned long l    2041                              (unsigned long long)le64_to_cpu(rec.r_cpos),
2026                              set_len);           2042                              set_len);
2027                         ret = ocfs2_insert_re    2043                         ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
2028                                                  2044                                                         ref_leaf_bh,
2029                                                  2045                                                         &rec, index,
2030                                                  2046                                                         merge, meta_ac);
2031                         if (ret) {               2047                         if (ret) {
2032                                 mlog_errno(re    2048                                 mlog_errno(ret);
2033                                 goto out;        2049                                 goto out;
2034                         }                        2050                         }
2035                 } else  {                        2051                 } else  {
2036                         set_len = min((u64)(c    2052                         set_len = min((u64)(cpos + len),
2037                                       le64_to    2053                                       le64_to_cpu(rec.r_cpos) + set_len) - cpos;
2038                         rec.r_cpos = cpu_to_l    2054                         rec.r_cpos = cpu_to_le64(cpos);
2039                         rec.r_clusters = cpu_    2055                         rec.r_clusters = cpu_to_le32(set_len);
2040                         le32_add_cpu(&rec.r_r    2056                         le32_add_cpu(&rec.r_refcount, 1);
2041                                                  2057 
2042                         trace_ocfs2_increase_    2058                         trace_ocfs2_increase_refcount_split(
2043                              (unsigned long l    2059                              (unsigned long long)le64_to_cpu(rec.r_cpos),
2044                              set_len, le32_to    2060                              set_len, le32_to_cpu(rec.r_refcount));
2045                         ret = ocfs2_split_ref    2061                         ret = ocfs2_split_refcount_rec(handle, ci,
2046                                                  2062                                                        ref_root_bh, ref_leaf_bh,
2047                                                  2063                                                        &rec, index, merge,
2048                                                  2064                                                        meta_ac, dealloc);
2049                         if (ret) {               2065                         if (ret) {
2050                                 mlog_errno(re    2066                                 mlog_errno(ret);
2051                                 goto out;        2067                                 goto out;
2052                         }                        2068                         }
2053                 }                                2069                 }
2054                                                  2070 
2055                 cpos += set_len;                 2071                 cpos += set_len;
2056                 len -= set_len;                  2072                 len -= set_len;
2057                 brelse(ref_leaf_bh);             2073                 brelse(ref_leaf_bh);
2058                 ref_leaf_bh = NULL;              2074                 ref_leaf_bh = NULL;
2059         }                                        2075         }
2060                                                  2076 
2061 out:                                             2077 out:
2062         brelse(ref_leaf_bh);                     2078         brelse(ref_leaf_bh);
2063         return ret;                              2079         return ret;
2064 }                                                2080 }
2065                                                  2081 
2066 static int ocfs2_remove_refcount_extent(handl    2082 static int ocfs2_remove_refcount_extent(handle_t *handle,
2067                                 struct ocfs2_    2083                                 struct ocfs2_caching_info *ci,
2068                                 struct buffer    2084                                 struct buffer_head *ref_root_bh,
2069                                 struct buffer    2085                                 struct buffer_head *ref_leaf_bh,
2070                                 struct ocfs2_    2086                                 struct ocfs2_alloc_context *meta_ac,
2071                                 struct ocfs2_    2087                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
2072 {                                                2088 {
2073         int ret;                                 2089         int ret;
2074         struct super_block *sb = ocfs2_metada    2090         struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2075         struct ocfs2_refcount_block *rb =        2091         struct ocfs2_refcount_block *rb =
2076                         (struct ocfs2_refcoun    2092                         (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2077         struct ocfs2_extent_tree et;             2093         struct ocfs2_extent_tree et;
2078                                                  2094 
2079         BUG_ON(rb->rf_records.rl_used);          2095         BUG_ON(rb->rf_records.rl_used);
2080                                                  2096 
2081         trace_ocfs2_remove_refcount_extent(      2097         trace_ocfs2_remove_refcount_extent(
2082                 (unsigned long long)ocfs2_met    2098                 (unsigned long long)ocfs2_metadata_cache_owner(ci),
2083                 (unsigned long long)ref_leaf_    2099                 (unsigned long long)ref_leaf_bh->b_blocknr,
2084                 le32_to_cpu(rb->rf_cpos));       2100                 le32_to_cpu(rb->rf_cpos));
2085                                                  2101 
2086         ocfs2_init_refcount_extent_tree(&et,     2102         ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2087         ret = ocfs2_remove_extent(handle, &et    2103         ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
2088                                   1, meta_ac,    2104                                   1, meta_ac, dealloc);
2089         if (ret) {                               2105         if (ret) {
2090                 mlog_errno(ret);                 2106                 mlog_errno(ret);
2091                 goto out;                        2107                 goto out;
2092         }                                        2108         }
2093                                                  2109 
2094         ocfs2_remove_from_cache(ci, ref_leaf_    2110         ocfs2_remove_from_cache(ci, ref_leaf_bh);
2095                                                  2111 
2096         /*                                       2112         /*
2097          * add the freed block to the dealloc    2113          * add the freed block to the dealloc so that it will be freed
2098          * when we run dealloc.                  2114          * when we run dealloc.
2099          */                                      2115          */
2100         ret = ocfs2_cache_block_dealloc(deall    2116         ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
2101                                         le16_    2117                                         le16_to_cpu(rb->rf_suballoc_slot),
2102                                         le64_    2118                                         le64_to_cpu(rb->rf_suballoc_loc),
2103                                         le64_    2119                                         le64_to_cpu(rb->rf_blkno),
2104                                         le16_    2120                                         le16_to_cpu(rb->rf_suballoc_bit));
2105         if (ret) {                               2121         if (ret) {
2106                 mlog_errno(ret);                 2122                 mlog_errno(ret);
2107                 goto out;                        2123                 goto out;
2108         }                                        2124         }
2109                                                  2125 
2110         ret = ocfs2_journal_access_rb(handle,    2126         ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
2111                                       OCFS2_J    2127                                       OCFS2_JOURNAL_ACCESS_WRITE);
2112         if (ret) {                               2128         if (ret) {
2113                 mlog_errno(ret);                 2129                 mlog_errno(ret);
2114                 goto out;                        2130                 goto out;
2115         }                                        2131         }
2116                                                  2132 
2117         rb = (struct ocfs2_refcount_block *)r    2133         rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2118                                                  2134 
2119         le32_add_cpu(&rb->rf_clusters, -1);      2135         le32_add_cpu(&rb->rf_clusters, -1);
2120                                                  2136 
2121         /*                                       2137         /*
2122          * check whether we need to restore t    2138          * check whether we need to restore the root refcount block if
2123          * there is no leaf extent block at a    2139          * there is no leaf extent block at atll.
2124          */                                      2140          */
2125         if (!rb->rf_list.l_next_free_rec) {      2141         if (!rb->rf_list.l_next_free_rec) {
2126                 BUG_ON(rb->rf_clusters);         2142                 BUG_ON(rb->rf_clusters);
2127                                                  2143 
2128                 trace_ocfs2_restore_refcount_    2144                 trace_ocfs2_restore_refcount_block(
2129                      (unsigned long long)ref_    2145                      (unsigned long long)ref_root_bh->b_blocknr);
2130                                                  2146 
2131                 rb->rf_flags = 0;                2147                 rb->rf_flags = 0;
2132                 rb->rf_parent = 0;               2148                 rb->rf_parent = 0;
2133                 rb->rf_cpos = 0;                 2149                 rb->rf_cpos = 0;
2134                 memset(&rb->rf_records, 0, sb    2150                 memset(&rb->rf_records, 0, sb->s_blocksize -
2135                        offsetof(struct ocfs2_    2151                        offsetof(struct ocfs2_refcount_block, rf_records));
2136                 rb->rf_records.rl_count =        2152                 rb->rf_records.rl_count =
2137                                 cpu_to_le16(o    2153                                 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
2138         }                                        2154         }
2139                                                  2155 
2140         ocfs2_journal_dirty(handle, ref_root_    2156         ocfs2_journal_dirty(handle, ref_root_bh);
2141                                                  2157 
2142 out:                                             2158 out:
2143         return ret;                              2159         return ret;
2144 }                                                2160 }
2145                                                  2161 
2146 int ocfs2_increase_refcount(handle_t *handle,    2162 int ocfs2_increase_refcount(handle_t *handle,
2147                             struct ocfs2_cach    2163                             struct ocfs2_caching_info *ci,
2148                             struct buffer_hea    2164                             struct buffer_head *ref_root_bh,
2149                             u64 cpos, u32 len    2165                             u64 cpos, u32 len,
2150                             struct ocfs2_allo    2166                             struct ocfs2_alloc_context *meta_ac,
2151                             struct ocfs2_cach    2167                             struct ocfs2_cached_dealloc_ctxt *dealloc)
2152 {                                                2168 {
2153         return __ocfs2_increase_refcount(hand    2169         return __ocfs2_increase_refcount(handle, ci, ref_root_bh,
2154                                          cpos    2170                                          cpos, len, 1,
2155                                          meta    2171                                          meta_ac, dealloc);
2156 }                                                2172 }
2157                                                  2173 
2158 static int ocfs2_decrease_refcount_rec(handle    2174 static int ocfs2_decrease_refcount_rec(handle_t *handle,
2159                                 struct ocfs2_    2175                                 struct ocfs2_caching_info *ci,
2160                                 struct buffer    2176                                 struct buffer_head *ref_root_bh,
2161                                 struct buffer    2177                                 struct buffer_head *ref_leaf_bh,
2162                                 int index, u6    2178                                 int index, u64 cpos, unsigned int len,
2163                                 struct ocfs2_    2179                                 struct ocfs2_alloc_context *meta_ac,
2164                                 struct ocfs2_    2180                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
2165 {                                                2181 {
2166         int ret;                                 2182         int ret;
2167         struct ocfs2_refcount_block *rb =        2183         struct ocfs2_refcount_block *rb =
2168                         (struct ocfs2_refcoun    2184                         (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2169         struct ocfs2_refcount_rec *rec = &rb-    2185         struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
2170                                                  2186 
2171         BUG_ON(cpos < le64_to_cpu(rec->r_cpos    2187         BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
2172         BUG_ON(cpos + len >                      2188         BUG_ON(cpos + len >
2173                le64_to_cpu(rec->r_cpos) + le3    2189                le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
2174                                                  2190 
2175         trace_ocfs2_decrease_refcount_rec(       2191         trace_ocfs2_decrease_refcount_rec(
2176                 (unsigned long long)ocfs2_met    2192                 (unsigned long long)ocfs2_metadata_cache_owner(ci),
2177                 (unsigned long long)cpos, len    2193                 (unsigned long long)cpos, len);
2178                                                  2194 
2179         if (cpos == le64_to_cpu(rec->r_cpos)     2195         if (cpos == le64_to_cpu(rec->r_cpos) &&
2180             len == le32_to_cpu(rec->r_cluster    2196             len == le32_to_cpu(rec->r_clusters))
2181                 ret = ocfs2_change_refcount_r    2197                 ret = ocfs2_change_refcount_rec(handle, ci,
2182                                                  2198                                                 ref_leaf_bh, index, 1, -1);
2183         else {                                   2199         else {
2184                 struct ocfs2_refcount_rec spl    2200                 struct ocfs2_refcount_rec split = *rec;
2185                 split.r_cpos = cpu_to_le64(cp    2201                 split.r_cpos = cpu_to_le64(cpos);
2186                 split.r_clusters = cpu_to_le3    2202                 split.r_clusters = cpu_to_le32(len);
2187                                                  2203 
2188                 le32_add_cpu(&split.r_refcoun    2204                 le32_add_cpu(&split.r_refcount, -1);
2189                                                  2205 
2190                 ret = ocfs2_split_refcount_re    2206                 ret = ocfs2_split_refcount_rec(handle, ci,
2191                                                  2207                                                ref_root_bh, ref_leaf_bh,
2192                                                  2208                                                &split, index, 1,
2193                                                  2209                                                meta_ac, dealloc);
2194         }                                        2210         }
2195                                                  2211 
2196         if (ret) {                               2212         if (ret) {
2197                 mlog_errno(ret);                 2213                 mlog_errno(ret);
2198                 goto out;                        2214                 goto out;
2199         }                                        2215         }
2200                                                  2216 
2201         /* Remove the leaf refcount block if     2217         /* Remove the leaf refcount block if it contains no refcount record. */
2202         if (!rb->rf_records.rl_used && ref_le    2218         if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
2203                 ret = ocfs2_remove_refcount_e    2219                 ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
2204                                                  2220                                                    ref_leaf_bh, meta_ac,
2205                                                  2221                                                    dealloc);
2206                 if (ret)                         2222                 if (ret)
2207                         mlog_errno(ret);         2223                         mlog_errno(ret);
2208         }                                        2224         }
2209                                                  2225 
2210 out:                                             2226 out:
2211         return ret;                              2227         return ret;
2212 }                                                2228 }
2213                                                  2229 
2214 static int __ocfs2_decrease_refcount(handle_t    2230 static int __ocfs2_decrease_refcount(handle_t *handle,
2215                                      struct o    2231                                      struct ocfs2_caching_info *ci,
2216                                      struct b    2232                                      struct buffer_head *ref_root_bh,
2217                                      u64 cpos    2233                                      u64 cpos, u32 len,
2218                                      struct o    2234                                      struct ocfs2_alloc_context *meta_ac,
2219                                      struct o    2235                                      struct ocfs2_cached_dealloc_ctxt *dealloc,
2220                                      int dele    2236                                      int delete)
2221 {                                                2237 {
2222         int ret = 0, index = 0;                  2238         int ret = 0, index = 0;
2223         struct ocfs2_refcount_rec rec;           2239         struct ocfs2_refcount_rec rec;
2224         unsigned int r_count = 0, r_len;         2240         unsigned int r_count = 0, r_len;
2225         struct super_block *sb = ocfs2_metada    2241         struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2226         struct buffer_head *ref_leaf_bh = NUL    2242         struct buffer_head *ref_leaf_bh = NULL;
2227                                                  2243 
2228         trace_ocfs2_decrease_refcount(           2244         trace_ocfs2_decrease_refcount(
2229                 (unsigned long long)ocfs2_met    2245                 (unsigned long long)ocfs2_metadata_cache_owner(ci),
2230                 (unsigned long long)cpos, len    2246                 (unsigned long long)cpos, len, delete);
2231                                                  2247 
2232         while (len) {                            2248         while (len) {
2233                 ret = ocfs2_get_refcount_rec(    2249                 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2234                                                  2250                                              cpos, len, &rec, &index,
2235                                                  2251                                              &ref_leaf_bh);
2236                 if (ret) {                       2252                 if (ret) {
2237                         mlog_errno(ret);         2253                         mlog_errno(ret);
2238                         goto out;                2254                         goto out;
2239                 }                                2255                 }
2240                                                  2256 
2241                 r_count = le32_to_cpu(rec.r_r    2257                 r_count = le32_to_cpu(rec.r_refcount);
2242                 BUG_ON(r_count == 0);            2258                 BUG_ON(r_count == 0);
2243                 if (!delete)                     2259                 if (!delete)
2244                         BUG_ON(r_count > 1);     2260                         BUG_ON(r_count > 1);
2245                                                  2261 
2246                 r_len = min((u64)(cpos + len)    2262                 r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
2247                               le32_to_cpu(rec    2263                               le32_to_cpu(rec.r_clusters)) - cpos;
2248                                                  2264 
2249                 ret = ocfs2_decrease_refcount    2265                 ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
2250                                                  2266                                                   ref_leaf_bh, index,
2251                                                  2267                                                   cpos, r_len,
2252                                                  2268                                                   meta_ac, dealloc);
2253                 if (ret) {                       2269                 if (ret) {
2254                         mlog_errno(ret);         2270                         mlog_errno(ret);
2255                         goto out;                2271                         goto out;
2256                 }                                2272                 }
2257                                                  2273 
2258                 if (le32_to_cpu(rec.r_refcoun    2274                 if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
2259                         ret = ocfs2_cache_clu    2275                         ret = ocfs2_cache_cluster_dealloc(dealloc,
2260                                           ocf    2276                                           ocfs2_clusters_to_blocks(sb, cpos),
2261                                                  2277                                                           r_len);
2262                         if (ret) {               2278                         if (ret) {
2263                                 mlog_errno(re    2279                                 mlog_errno(ret);
2264                                 goto out;        2280                                 goto out;
2265                         }                        2281                         }
2266                 }                                2282                 }
2267                                                  2283 
2268                 cpos += r_len;                   2284                 cpos += r_len;
2269                 len -= r_len;                    2285                 len -= r_len;
2270                 brelse(ref_leaf_bh);             2286                 brelse(ref_leaf_bh);
2271                 ref_leaf_bh = NULL;              2287                 ref_leaf_bh = NULL;
2272         }                                        2288         }
2273                                                  2289 
2274 out:                                             2290 out:
2275         brelse(ref_leaf_bh);                     2291         brelse(ref_leaf_bh);
2276         return ret;                              2292         return ret;
2277 }                                                2293 }
2278                                                  2294 
2279 /* Caller must hold refcount tree lock. */       2295 /* Caller must hold refcount tree lock. */
2280 int ocfs2_decrease_refcount(struct inode *ino    2296 int ocfs2_decrease_refcount(struct inode *inode,
2281                             handle_t *handle,    2297                             handle_t *handle, u32 cpos, u32 len,
2282                             struct ocfs2_allo    2298                             struct ocfs2_alloc_context *meta_ac,
2283                             struct ocfs2_cach    2299                             struct ocfs2_cached_dealloc_ctxt *dealloc,
2284                             int delete)          2300                             int delete)
2285 {                                                2301 {
2286         int ret;                                 2302         int ret;
2287         u64 ref_blkno;                           2303         u64 ref_blkno;
                                                   >> 2304         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2288         struct buffer_head *ref_root_bh = NUL    2305         struct buffer_head *ref_root_bh = NULL;
2289         struct ocfs2_refcount_tree *tree;        2306         struct ocfs2_refcount_tree *tree;
2290                                                  2307 
2291         BUG_ON(!ocfs2_is_refcount_inode(inode !! 2308         BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
2292                                                  2309 
2293         ret = ocfs2_get_refcount_block(inode,    2310         ret = ocfs2_get_refcount_block(inode, &ref_blkno);
2294         if (ret) {                               2311         if (ret) {
2295                 mlog_errno(ret);                 2312                 mlog_errno(ret);
2296                 goto out;                        2313                 goto out;
2297         }                                        2314         }
2298                                                  2315 
2299         ret = ocfs2_get_refcount_tree(OCFS2_S    2316         ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
2300         if (ret) {                               2317         if (ret) {
2301                 mlog_errno(ret);                 2318                 mlog_errno(ret);
2302                 goto out;                        2319                 goto out;
2303         }                                        2320         }
2304                                                  2321 
2305         ret = ocfs2_read_refcount_block(&tree    2322         ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
2306                                         &ref_    2323                                         &ref_root_bh);
2307         if (ret) {                               2324         if (ret) {
2308                 mlog_errno(ret);                 2325                 mlog_errno(ret);
2309                 goto out;                        2326                 goto out;
2310         }                                        2327         }
2311                                                  2328 
2312         ret = __ocfs2_decrease_refcount(handl    2329         ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
2313                                         cpos,    2330                                         cpos, len, meta_ac, dealloc, delete);
2314         if (ret)                                 2331         if (ret)
2315                 mlog_errno(ret);                 2332                 mlog_errno(ret);
2316 out:                                             2333 out:
2317         brelse(ref_root_bh);                     2334         brelse(ref_root_bh);
2318         return ret;                              2335         return ret;
2319 }                                                2336 }
2320                                                  2337 
2321 /*                                               2338 /*
2322  * Mark the already-existing extent at cpos a    2339  * Mark the already-existing extent at cpos as refcounted for len clusters.
2323  * This adds the refcount extent flag.           2340  * This adds the refcount extent flag.
2324  *                                               2341  *
2325  * If the existing extent is larger than the     2342  * If the existing extent is larger than the request, initiate a
2326  * split. An attempt will be made at merging     2343  * split. An attempt will be made at merging with adjacent extents.
2327  *                                               2344  *
2328  * The caller is responsible for passing down    2345  * The caller is responsible for passing down meta_ac if we'll need it.
2329  */                                              2346  */
2330 static int ocfs2_mark_extent_refcounted(struc    2347 static int ocfs2_mark_extent_refcounted(struct inode *inode,
2331                                 struct ocfs2_    2348                                 struct ocfs2_extent_tree *et,
2332                                 handle_t *han    2349                                 handle_t *handle, u32 cpos,
2333                                 u32 len, u32     2350                                 u32 len, u32 phys,
2334                                 struct ocfs2_    2351                                 struct ocfs2_alloc_context *meta_ac,
2335                                 struct ocfs2_    2352                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
2336 {                                                2353 {
2337         int ret;                                 2354         int ret;
2338                                                  2355 
2339         trace_ocfs2_mark_extent_refcounted(OC    2356         trace_ocfs2_mark_extent_refcounted(OCFS2_I(inode)->ip_blkno,
2340                                            cp    2357                                            cpos, len, phys);
2341                                                  2358 
2342         if (!ocfs2_refcount_tree(OCFS2_SB(ino    2359         if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2343                 ret = ocfs2_error(inode->i_sb !! 2360                 ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
2344                                   inode->i_in !! 2361                             "tree, but the feature bit is not set in the "
                                                   >> 2362                             "super block.", inode->i_ino);
                                                   >> 2363                 ret = -EROFS;
2345                 goto out;                        2364                 goto out;
2346         }                                        2365         }
2347                                                  2366 
2348         ret = ocfs2_change_extent_flag(handle    2367         ret = ocfs2_change_extent_flag(handle, et, cpos,
2349                                        len, p    2368                                        len, phys, meta_ac, dealloc,
2350                                        OCFS2_    2369                                        OCFS2_EXT_REFCOUNTED, 0);
2351         if (ret)                                 2370         if (ret)
2352                 mlog_errno(ret);                 2371                 mlog_errno(ret);
2353                                                  2372 
2354 out:                                             2373 out:
2355         return ret;                              2374         return ret;
2356 }                                                2375 }
2357                                                  2376 
2358 /*                                               2377 /*
2359  * Given some contiguous physical clusters, c    2378  * Given some contiguous physical clusters, calculate what we need
2360  * for modifying their refcount.                 2379  * for modifying their refcount.
2361  */                                              2380  */
2362 static int ocfs2_calc_refcount_meta_credits(s    2381 static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
2363                                             s    2382                                             struct ocfs2_caching_info *ci,
2364                                             s    2383                                             struct buffer_head *ref_root_bh,
2365                                             u    2384                                             u64 start_cpos,
2366                                             u    2385                                             u32 clusters,
2367                                             i    2386                                             int *meta_add,
2368                                             i    2387                                             int *credits)
2369 {                                                2388 {
2370         int ret = 0, index, ref_blocks = 0, r    2389         int ret = 0, index, ref_blocks = 0, recs_add = 0;
2371         u64 cpos = start_cpos;                   2390         u64 cpos = start_cpos;
2372         struct ocfs2_refcount_block *rb;         2391         struct ocfs2_refcount_block *rb;
2373         struct ocfs2_refcount_rec rec;           2392         struct ocfs2_refcount_rec rec;
2374         struct buffer_head *ref_leaf_bh = NUL    2393         struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
2375         u32 len;                                 2394         u32 len;
2376                                                  2395 
2377         while (clusters) {                       2396         while (clusters) {
2378                 ret = ocfs2_get_refcount_rec(    2397                 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2379                                                  2398                                              cpos, clusters, &rec,
2380                                                  2399                                              &index, &ref_leaf_bh);
2381                 if (ret) {                       2400                 if (ret) {
2382                         mlog_errno(ret);         2401                         mlog_errno(ret);
2383                         goto out;                2402                         goto out;
2384                 }                                2403                 }
2385                                                  2404 
2386                 if (ref_leaf_bh != prev_bh) {    2405                 if (ref_leaf_bh != prev_bh) {
2387                         /*                       2406                         /*
2388                          * Now we encounter a    2407                          * Now we encounter a new leaf block, so calculate
2389                          * whether we need to    2408                          * whether we need to extend the old leaf.
2390                          */                      2409                          */
2391                         if (prev_bh) {           2410                         if (prev_bh) {
2392                                 rb = (struct     2411                                 rb = (struct ocfs2_refcount_block *)
2393                                                  2412                                                         prev_bh->b_data;
2394                                                  2413 
2395                                 if (le16_to_c    2414                                 if (le16_to_cpu(rb->rf_records.rl_used) +
2396                                     recs_add     2415                                     recs_add >
2397                                     le16_to_c    2416                                     le16_to_cpu(rb->rf_records.rl_count))
2398                                         ref_b    2417                                         ref_blocks++;
2399                         }                        2418                         }
2400                                                  2419 
2401                         recs_add = 0;            2420                         recs_add = 0;
2402                         *credits += 1;           2421                         *credits += 1;
2403                         brelse(prev_bh);         2422                         brelse(prev_bh);
2404                         prev_bh = ref_leaf_bh    2423                         prev_bh = ref_leaf_bh;
2405                         get_bh(prev_bh);         2424                         get_bh(prev_bh);
2406                 }                                2425                 }
2407                                                  2426 
                                                   >> 2427                 rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
                                                   >> 2428 
2408                 trace_ocfs2_calc_refcount_met    2429                 trace_ocfs2_calc_refcount_meta_credits_iterate(
2409                                 recs_add, (un    2430                                 recs_add, (unsigned long long)cpos, clusters,
2410                                 (unsigned lon    2431                                 (unsigned long long)le64_to_cpu(rec.r_cpos),
2411                                 le32_to_cpu(r    2432                                 le32_to_cpu(rec.r_clusters),
2412                                 le32_to_cpu(r    2433                                 le32_to_cpu(rec.r_refcount), index);
2413                                                  2434 
2414                 len = min((u64)cpos + cluster    2435                 len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
2415                           le32_to_cpu(rec.r_c    2436                           le32_to_cpu(rec.r_clusters)) - cpos;
2416                 /*                               2437                 /*
2417                  * We record all the records     2438                  * We record all the records which will be inserted to the
2418                  * same refcount block, so th    2439                  * same refcount block, so that we can tell exactly whether
2419                  * we need a new refcount blo    2440                  * we need a new refcount block or not.
2420                  *                               2441                  *
2421                  * If we will insert a new on    2442                  * If we will insert a new one, this is easy and only happens
2422                  * during adding refcounted f    2443                  * during adding refcounted flag to the extent, so we don't
2423                  * have a chance of spliting.    2444                  * have a chance of spliting. We just need one record.
2424                  *                               2445                  *
2425                  * If the refcount rec alread    2446                  * If the refcount rec already exists, that would be a little
2426                  * complicated. we may have t    2447                  * complicated. we may have to:
2427                  * 1) split at the beginning     2448                  * 1) split at the beginning if the start pos isn't aligned.
2428                  *    we need 1 more record i    2449                  *    we need 1 more record in this case.
2429                  * 2) split int the end if th    2450                  * 2) split int the end if the end pos isn't aligned.
2430                  *    we need 1 more record i    2451                  *    we need 1 more record in this case.
2431                  * 3) split in the middle bec    2452                  * 3) split in the middle because of file system fragmentation.
2432                  *    we need 2 more records     2453                  *    we need 2 more records in this case(we can't detect this
2433                  *    beforehand, so always t    2454                  *    beforehand, so always think of the worst case).
2434                  */                              2455                  */
2435                 if (rec.r_refcount) {            2456                 if (rec.r_refcount) {
2436                         recs_add += 2;           2457                         recs_add += 2;
2437                         /* Check whether we n    2458                         /* Check whether we need a split at the beginning. */
2438                         if (cpos == start_cpo    2459                         if (cpos == start_cpos &&
2439                             cpos != le64_to_c    2460                             cpos != le64_to_cpu(rec.r_cpos))
2440                                 recs_add++;      2461                                 recs_add++;
2441                                                  2462 
2442                         /* Check whether we n    2463                         /* Check whether we need a split in the end. */
2443                         if (cpos + clusters <    2464                         if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
2444                             le32_to_cpu(rec.r    2465                             le32_to_cpu(rec.r_clusters))
2445                                 recs_add++;      2466                                 recs_add++;
2446                 } else                           2467                 } else
2447                         recs_add++;              2468                         recs_add++;
2448                                                  2469 
2449                 brelse(ref_leaf_bh);             2470                 brelse(ref_leaf_bh);
2450                 ref_leaf_bh = NULL;              2471                 ref_leaf_bh = NULL;
2451                 clusters -= len;                 2472                 clusters -= len;
2452                 cpos += len;                     2473                 cpos += len;
2453         }                                        2474         }
2454                                                  2475 
2455         if (prev_bh) {                           2476         if (prev_bh) {
2456                 rb = (struct ocfs2_refcount_b    2477                 rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
2457                                                  2478 
2458                 if (le16_to_cpu(rb->rf_record    2479                 if (le16_to_cpu(rb->rf_records.rl_used) + recs_add >
2459                     le16_to_cpu(rb->rf_record    2480                     le16_to_cpu(rb->rf_records.rl_count))
2460                         ref_blocks++;            2481                         ref_blocks++;
2461                                                  2482 
2462                 *credits += 1;                   2483                 *credits += 1;
2463         }                                        2484         }
2464                                                  2485 
2465         if (!ref_blocks)                         2486         if (!ref_blocks)
2466                 goto out;                        2487                 goto out;
2467                                                  2488 
2468         *meta_add += ref_blocks;                 2489         *meta_add += ref_blocks;
2469         *credits += ref_blocks;                  2490         *credits += ref_blocks;
2470                                                  2491 
2471         /*                                       2492         /*
2472          * So we may need ref_blocks to inser    2493          * So we may need ref_blocks to insert into the tree.
2473          * That also means we need to change     2494          * That also means we need to change the b-tree and add that number
2474          * of records since we never merge th    2495          * of records since we never merge them.
2475          * We need one more block for expansi    2496          * We need one more block for expansion since the new created leaf
2476          * block is also full and needs split    2497          * block is also full and needs split.
2477          */                                      2498          */
2478         rb = (struct ocfs2_refcount_block *)r    2499         rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2479         if (le32_to_cpu(rb->rf_flags) & OCFS2    2500         if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
2480                 struct ocfs2_extent_tree et;     2501                 struct ocfs2_extent_tree et;
2481                                                  2502 
2482                 ocfs2_init_refcount_extent_tr    2503                 ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2483                 *meta_add += ocfs2_extend_met    2504                 *meta_add += ocfs2_extend_meta_needed(et.et_root_el);
2484                 *credits += ocfs2_calc_extend    2505                 *credits += ocfs2_calc_extend_credits(sb,
2485                                               !! 2506                                                       et.et_root_el,
                                                   >> 2507                                                       ref_blocks);
2486         } else {                                 2508         } else {
2487                 *credits += OCFS2_EXPAND_REFC    2509                 *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
2488                 *meta_add += 1;                  2510                 *meta_add += 1;
2489         }                                        2511         }
2490                                                  2512 
2491 out:                                             2513 out:
2492                                                  2514 
2493         trace_ocfs2_calc_refcount_meta_credit    2515         trace_ocfs2_calc_refcount_meta_credits(
2494                 (unsigned long long)start_cpo    2516                 (unsigned long long)start_cpos, clusters,
2495                 *meta_add, *credits);            2517                 *meta_add, *credits);
2496         brelse(ref_leaf_bh);                     2518         brelse(ref_leaf_bh);
2497         brelse(prev_bh);                         2519         brelse(prev_bh);
2498         return ret;                              2520         return ret;
2499 }                                                2521 }
2500                                                  2522 
2501 /*                                               2523 /*
2502  * For refcount tree, we will decrease some c    2524  * For refcount tree, we will decrease some contiguous clusters
2503  * refcount count, so just go through it to s    2525  * refcount count, so just go through it to see how many blocks
2504  * we gonna touch and whether we need to crea    2526  * we gonna touch and whether we need to create new blocks.
2505  *                                               2527  *
2506  * Normally the refcount blocks store these r    2528  * Normally the refcount blocks store these refcount should be
2507  * contiguous also, so that we can get the nu    2529  * contiguous also, so that we can get the number easily.
2508  * We will at most add split 2 refcount recor    2530  * We will at most add split 2 refcount records and 2 more
2509  * refcount blocks, so just check it in a rou    2531  * refcount blocks, so just check it in a rough way.
2510  *                                               2532  *
2511  * Caller must hold refcount tree lock.          2533  * Caller must hold refcount tree lock.
2512  */                                              2534  */
2513 int ocfs2_prepare_refcount_change_for_del(str    2535 int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
2514                                           u64    2536                                           u64 refcount_loc,
2515                                           u64    2537                                           u64 phys_blkno,
2516                                           u32    2538                                           u32 clusters,
2517                                           int    2539                                           int *credits,
2518                                           int    2540                                           int *ref_blocks)
2519 {                                                2541 {
2520         int ret;                                 2542         int ret;
                                                   >> 2543         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2521         struct buffer_head *ref_root_bh = NUL    2544         struct buffer_head *ref_root_bh = NULL;
2522         struct ocfs2_refcount_tree *tree;        2545         struct ocfs2_refcount_tree *tree;
2523         u64 start_cpos = ocfs2_blocks_to_clus    2546         u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
2524                                                  2547 
2525         if (!ocfs2_refcount_tree(OCFS2_SB(ino    2548         if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2526                 ret = ocfs2_error(inode->i_sb !! 2549                 ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
2527                                   inode->i_in !! 2550                             "tree, but the feature bit is not set in the "
                                                   >> 2551                             "super block.", inode->i_ino);
                                                   >> 2552                 ret = -EROFS;
2528                 goto out;                        2553                 goto out;
2529         }                                        2554         }
2530                                                  2555 
2531         BUG_ON(!ocfs2_is_refcount_inode(inode !! 2556         BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
2532                                                  2557 
2533         ret = ocfs2_get_refcount_tree(OCFS2_S    2558         ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
2534                                       refcoun    2559                                       refcount_loc, &tree);
2535         if (ret) {                               2560         if (ret) {
2536                 mlog_errno(ret);                 2561                 mlog_errno(ret);
2537                 goto out;                        2562                 goto out;
2538         }                                        2563         }
2539                                                  2564 
2540         ret = ocfs2_read_refcount_block(&tree    2565         ret = ocfs2_read_refcount_block(&tree->rf_ci, refcount_loc,
2541                                         &ref_    2566                                         &ref_root_bh);
2542         if (ret) {                               2567         if (ret) {
2543                 mlog_errno(ret);                 2568                 mlog_errno(ret);
2544                 goto out;                        2569                 goto out;
2545         }                                        2570         }
2546                                                  2571 
2547         ret = ocfs2_calc_refcount_meta_credit    2572         ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
2548                                                  2573                                                &tree->rf_ci,
2549                                                  2574                                                ref_root_bh,
2550                                                  2575                                                start_cpos, clusters,
2551                                                  2576                                                ref_blocks, credits);
2552         if (ret) {                               2577         if (ret) {
2553                 mlog_errno(ret);                 2578                 mlog_errno(ret);
2554                 goto out;                        2579                 goto out;
2555         }                                        2580         }
2556                                                  2581 
2557         trace_ocfs2_prepare_refcount_change_f    2582         trace_ocfs2_prepare_refcount_change_for_del(*ref_blocks, *credits);
2558                                                  2583 
2559 out:                                             2584 out:
2560         brelse(ref_root_bh);                     2585         brelse(ref_root_bh);
2561         return ret;                              2586         return ret;
2562 }                                                2587 }
2563                                                  2588 
2564 #define MAX_CONTIG_BYTES        1048576          2589 #define MAX_CONTIG_BYTES        1048576
2565                                                  2590 
2566 static inline unsigned int ocfs2_cow_contig_c    2591 static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
2567 {                                                2592 {
2568         return ocfs2_clusters_for_bytes(sb, M    2593         return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
2569 }                                                2594 }
2570                                                  2595 
2571 static inline unsigned int ocfs2_cow_contig_m    2596 static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
2572 {                                                2597 {
2573         return ~(ocfs2_cow_contig_clusters(sb    2598         return ~(ocfs2_cow_contig_clusters(sb) - 1);
2574 }                                                2599 }
2575                                                  2600 
2576 /*                                               2601 /*
2577  * Given an extent that starts at 'start' and    2602  * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
2578  * find an offset (start + (n * contig_cluste    2603  * find an offset (start + (n * contig_clusters)) that is closest to cpos
2579  * while still being less than or equal to it    2604  * while still being less than or equal to it.
2580  *                                               2605  *
2581  * The goal is to break the extent at a multi    2606  * The goal is to break the extent at a multiple of contig_clusters.
2582  */                                              2607  */
2583 static inline unsigned int ocfs2_cow_align_st    2608 static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
2584                                                  2609                                                  unsigned int start,
2585                                                  2610                                                  unsigned int cpos)
2586 {                                                2611 {
2587         BUG_ON(start > cpos);                    2612         BUG_ON(start > cpos);
2588                                                  2613 
2589         return start + ((cpos - start) & ocfs    2614         return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
2590 }                                                2615 }
2591                                                  2616 
2592 /*                                               2617 /*
2593  * Given a cluster count of len, pad it out s    2618  * Given a cluster count of len, pad it out so that it is a multiple
2594  * of contig_clusters.                           2619  * of contig_clusters.
2595  */                                              2620  */
2596 static inline unsigned int ocfs2_cow_align_le    2621 static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
2597                                                  2622                                                   unsigned int len)
2598 {                                                2623 {
2599         unsigned int padded =                    2624         unsigned int padded =
2600                 (len + (ocfs2_cow_contig_clus    2625                 (len + (ocfs2_cow_contig_clusters(sb) - 1)) &
2601                 ocfs2_cow_contig_mask(sb);       2626                 ocfs2_cow_contig_mask(sb);
2602                                                  2627 
2603         /* Did we wrap? */                       2628         /* Did we wrap? */
2604         if (padded < len)                        2629         if (padded < len)
2605                 padded = UINT_MAX;               2630                 padded = UINT_MAX;
2606                                                  2631 
2607         return padded;                           2632         return padded;
2608 }                                                2633 }
2609                                                  2634 
2610 /*                                               2635 /*
2611  * Calculate out the start and number of virt !! 2636  * Calculate out the start and number of virtual clusters we need to to CoW.
2612  *                                               2637  *
2613  * cpos is vitual start cluster position we w    2638  * cpos is vitual start cluster position we want to do CoW in a
2614  * file and write_len is the cluster length.     2639  * file and write_len is the cluster length.
2615  * max_cpos is the place where we want to sto    2640  * max_cpos is the place where we want to stop CoW intentionally.
2616  *                                               2641  *
2617  * Normal we will start CoW from the beginnin    2642  * Normal we will start CoW from the beginning of extent record cotaining cpos.
2618  * We try to break up extents on boundaries o    2643  * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
2619  * get good I/O from the resulting extent tre    2644  * get good I/O from the resulting extent tree.
2620  */                                              2645  */
2621 static int ocfs2_refcount_cal_cow_clusters(st    2646 static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
2622                                            st    2647                                            struct ocfs2_extent_list *el,
2623                                            u3    2648                                            u32 cpos,
2624                                            u3    2649                                            u32 write_len,
2625                                            u3    2650                                            u32 max_cpos,
2626                                            u3    2651                                            u32 *cow_start,
2627                                            u3    2652                                            u32 *cow_len)
2628 {                                                2653 {
2629         int ret = 0;                             2654         int ret = 0;
2630         int tree_height = le16_to_cpu(el->l_t    2655         int tree_height = le16_to_cpu(el->l_tree_depth), i;
2631         struct buffer_head *eb_bh = NULL;        2656         struct buffer_head *eb_bh = NULL;
2632         struct ocfs2_extent_block *eb = NULL;    2657         struct ocfs2_extent_block *eb = NULL;
2633         struct ocfs2_extent_rec *rec;            2658         struct ocfs2_extent_rec *rec;
2634         unsigned int want_clusters, rec_end =    2659         unsigned int want_clusters, rec_end = 0;
2635         int contig_clusters = ocfs2_cow_conti    2660         int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
2636         int leaf_clusters;                       2661         int leaf_clusters;
2637                                                  2662 
2638         BUG_ON(cpos + write_len > max_cpos);     2663         BUG_ON(cpos + write_len > max_cpos);
2639                                                  2664 
2640         if (tree_height > 0) {                   2665         if (tree_height > 0) {
2641                 ret = ocfs2_find_leaf(INODE_C    2666                 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
2642                 if (ret) {                       2667                 if (ret) {
2643                         mlog_errno(ret);         2668                         mlog_errno(ret);
2644                         goto out;                2669                         goto out;
2645                 }                                2670                 }
2646                                                  2671 
2647                 eb = (struct ocfs2_extent_blo    2672                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2648                 el = &eb->h_list;                2673                 el = &eb->h_list;
2649                                                  2674 
2650                 if (el->l_tree_depth) {          2675                 if (el->l_tree_depth) {
2651                         ret = ocfs2_error(ino !! 2676                         ocfs2_error(inode->i_sb,
2652                                           "In !! 2677                                     "Inode %lu has non zero tree depth in "
2653                                           ino !! 2678                                     "leaf block %llu\n", inode->i_ino,
2654                                           (un !! 2679                                     (unsigned long long)eb_bh->b_blocknr);
                                                   >> 2680                         ret = -EROFS;
2655                         goto out;                2681                         goto out;
2656                 }                                2682                 }
2657         }                                        2683         }
2658                                                  2684 
2659         *cow_len = 0;                            2685         *cow_len = 0;
2660         for (i = 0; i < le16_to_cpu(el->l_nex    2686         for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2661                 rec = &el->l_recs[i];            2687                 rec = &el->l_recs[i];
2662                                                  2688 
2663                 if (ocfs2_is_empty_extent(rec    2689                 if (ocfs2_is_empty_extent(rec)) {
2664                         mlog_bug_on_msg(i !=     2690                         mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
2665                                         "inde    2691                                         "index %d\n", inode->i_ino, i);
2666                         continue;                2692                         continue;
2667                 }                                2693                 }
2668                                                  2694 
2669                 if (le32_to_cpu(rec->e_cpos)     2695                 if (le32_to_cpu(rec->e_cpos) +
2670                     le16_to_cpu(rec->e_leaf_c    2696                     le16_to_cpu(rec->e_leaf_clusters) <= cpos)
2671                         continue;                2697                         continue;
2672                                                  2698 
2673                 if (*cow_len == 0) {             2699                 if (*cow_len == 0) {
2674                         /*                       2700                         /*
2675                          * We should find a r    2701                          * We should find a refcounted record in the
2676                          * first pass.           2702                          * first pass.
2677                          */                      2703                          */
2678                         BUG_ON(!(rec->e_flags    2704                         BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
2679                         *cow_start = le32_to_    2705                         *cow_start = le32_to_cpu(rec->e_cpos);
2680                 }                                2706                 }
2681                                                  2707 
2682                 /*                               2708                 /*
2683                  * If we encounter a hole, a     2709                  * If we encounter a hole, a non-refcounted record or
2684                  * pass the max_cpos, stop th    2710                  * pass the max_cpos, stop the search.
2685                  */                              2711                  */
2686                 if ((!(rec->e_flags & OCFS2_E    2712                 if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
2687                     (*cow_len && rec_end != l    2713                     (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
2688                     (max_cpos <= le32_to_cpu(    2714                     (max_cpos <= le32_to_cpu(rec->e_cpos)))
2689                         break;                   2715                         break;
2690                                                  2716 
2691                 leaf_clusters = le16_to_cpu(r    2717                 leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
2692                 rec_end = le32_to_cpu(rec->e_    2718                 rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
2693                 if (rec_end > max_cpos) {        2719                 if (rec_end > max_cpos) {
2694                         rec_end = max_cpos;      2720                         rec_end = max_cpos;
2695                         leaf_clusters = rec_e    2721                         leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
2696                 }                                2722                 }
2697                                                  2723 
2698                 /*                               2724                 /*
2699                  * How many clusters do we ac    2725                  * How many clusters do we actually need from
2700                  * this extent?  First we see    2726                  * this extent?  First we see how many we actually
2701                  * need to complete the write    2727                  * need to complete the write.  If that's smaller
2702                  * than contig_clusters, we t    2728                  * than contig_clusters, we try for contig_clusters.
2703                  */                              2729                  */
2704                 if (!*cow_len)                   2730                 if (!*cow_len)
2705                         want_clusters = write    2731                         want_clusters = write_len;
2706                 else                             2732                 else
2707                         want_clusters = (cpos    2733                         want_clusters = (cpos + write_len) -
2708                                 (*cow_start +    2734                                 (*cow_start + *cow_len);
2709                 if (want_clusters < contig_cl    2735                 if (want_clusters < contig_clusters)
2710                         want_clusters = conti    2736                         want_clusters = contig_clusters;
2711                                                  2737 
2712                 /*                               2738                 /*
2713                  * If the write does not cove    2739                  * If the write does not cover the whole extent, we
2714                  * need to calculate how we'r    2740                  * need to calculate how we're going to split the extent.
2715                  * We try to do it on contig_    2741                  * We try to do it on contig_clusters boundaries.
2716                  *                               2742                  *
2717                  * Any extent smaller than co    2743                  * Any extent smaller than contig_clusters will be
2718                  * CoWed in its entirety.        2744                  * CoWed in its entirety.
2719                  */                              2745                  */
2720                 if (leaf_clusters <= contig_c    2746                 if (leaf_clusters <= contig_clusters)
2721                         *cow_len += leaf_clus    2747                         *cow_len += leaf_clusters;
2722                 else if (*cow_len || (*cow_st    2748                 else if (*cow_len || (*cow_start == cpos)) {
2723                         /*                       2749                         /*
2724                          * This extent needs     2750                          * This extent needs to be CoW'd from its
2725                          * beginning, so all     2751                          * beginning, so all we have to do is compute
2726                          * how many clusters     2752                          * how many clusters to grab.  We align
2727                          * want_clusters to t    2753                          * want_clusters to the edge of contig_clusters
2728                          * to get better I/O.    2754                          * to get better I/O.
2729                          */                      2755                          */
2730                         want_clusters = ocfs2    2756                         want_clusters = ocfs2_cow_align_length(inode->i_sb,
2731                                                  2757                                                                want_clusters);
2732                                                  2758 
2733                         if (leaf_clusters < w    2759                         if (leaf_clusters < want_clusters)
2734                                 *cow_len += l    2760                                 *cow_len += leaf_clusters;
2735                         else                     2761                         else
2736                                 *cow_len += w    2762                                 *cow_len += want_clusters;
2737                 } else if ((*cow_start + cont    2763                 } else if ((*cow_start + contig_clusters) >=
2738                            (cpos + write_len)    2764                            (cpos + write_len)) {
2739                         /*                       2765                         /*
2740                          * Breaking off conti    2766                          * Breaking off contig_clusters at the front
2741                          * of the extent will    2767                          * of the extent will cover our write.  That's
2742                          * easy.                 2768                          * easy.
2743                          */                      2769                          */
2744                         *cow_len = contig_clu    2770                         *cow_len = contig_clusters;
2745                 } else if ((rec_end - cpos) <    2771                 } else if ((rec_end - cpos) <= contig_clusters) {
2746                         /*                       2772                         /*
2747                          * Breaking off conti    2773                          * Breaking off contig_clusters at the tail of
2748                          * this extent will c    2774                          * this extent will cover cpos.
2749                          */                      2775                          */
2750                         *cow_start = rec_end     2776                         *cow_start = rec_end - contig_clusters;
2751                         *cow_len = contig_clu    2777                         *cow_len = contig_clusters;
2752                 } else if ((rec_end - cpos) <    2778                 } else if ((rec_end - cpos) <= want_clusters) {
2753                         /*                       2779                         /*
2754                          * While we can't fit    2780                          * While we can't fit the entire write in this
2755                          * extent, we know th    2781                          * extent, we know that the write goes from cpos
2756                          * to the end of the     2782                          * to the end of the extent.  Break that off.
2757                          * We try to break it    2783                          * We try to break it at some multiple of
2758                          * contig_clusters fr    2784                          * contig_clusters from the front of the extent.
2759                          * Failing that (ie,     2785                          * Failing that (ie, cpos is within
2760                          * contig_clusters of    2786                          * contig_clusters of the front), we'll CoW the
2761                          * entire extent.        2787                          * entire extent.
2762                          */                      2788                          */
2763                         *cow_start = ocfs2_co    2789                         *cow_start = ocfs2_cow_align_start(inode->i_sb,
2764                                                  2790                                                            *cow_start, cpos);
2765                         *cow_len = rec_end -     2791                         *cow_len = rec_end - *cow_start;
2766                 } else {                         2792                 } else {
2767                         /*                       2793                         /*
2768                          * Ok, the entire wri    2794                          * Ok, the entire write lives in the middle of
2769                          * this extent.  Let'    2795                          * this extent.  Let's try to slice the extent up
2770                          * nicely.  Optimally    2796                          * nicely.  Optimally, our CoW region starts at
2771                          * m*contig_clusters     2797                          * m*contig_clusters from the beginning of the
2772                          * extent and goes fo    2798                          * extent and goes for n*contig_clusters,
2773                          * covering the entir    2799                          * covering the entire write.
2774                          */                      2800                          */
2775                         *cow_start = ocfs2_co    2801                         *cow_start = ocfs2_cow_align_start(inode->i_sb,
2776                                                  2802                                                            *cow_start, cpos);
2777                                                  2803 
2778                         want_clusters = (cpos    2804                         want_clusters = (cpos + write_len) - *cow_start;
2779                         want_clusters = ocfs2    2805                         want_clusters = ocfs2_cow_align_length(inode->i_sb,
2780                                                  2806                                                                want_clusters);
2781                         if (*cow_start + want    2807                         if (*cow_start + want_clusters <= rec_end)
2782                                 *cow_len = wa    2808                                 *cow_len = want_clusters;
2783                         else                     2809                         else
2784                                 *cow_len = re    2810                                 *cow_len = rec_end - *cow_start;
2785                 }                                2811                 }
2786                                                  2812 
2787                 /* Have we covered our entire    2813                 /* Have we covered our entire write yet? */
2788                 if ((*cow_start + *cow_len) >    2814                 if ((*cow_start + *cow_len) >= (cpos + write_len))
2789                         break;                   2815                         break;
2790                                                  2816 
2791                 /*                               2817                 /*
2792                  * If we reach the end of the    2818                  * If we reach the end of the extent block and don't get enough
2793                  * clusters, continue with th    2819                  * clusters, continue with the next extent block if possible.
2794                  */                              2820                  */
2795                 if (i + 1 == le16_to_cpu(el->    2821                 if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
2796                     eb && eb->h_next_leaf_blk    2822                     eb && eb->h_next_leaf_blk) {
2797                         brelse(eb_bh);           2823                         brelse(eb_bh);
2798                         eb_bh = NULL;            2824                         eb_bh = NULL;
2799                                                  2825 
2800                         ret = ocfs2_read_exte    2826                         ret = ocfs2_read_extent_block(INODE_CACHE(inode),
2801                                                  2827                                                le64_to_cpu(eb->h_next_leaf_blk),
2802                                                  2828                                                &eb_bh);
2803                         if (ret) {               2829                         if (ret) {
2804                                 mlog_errno(re    2830                                 mlog_errno(ret);
2805                                 goto out;        2831                                 goto out;
2806                         }                        2832                         }
2807                                                  2833 
2808                         eb = (struct ocfs2_ex    2834                         eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2809                         el = &eb->h_list;        2835                         el = &eb->h_list;
2810                         i = -1;                  2836                         i = -1;
2811                 }                                2837                 }
2812         }                                        2838         }
2813                                                  2839 
2814 out:                                             2840 out:
2815         brelse(eb_bh);                           2841         brelse(eb_bh);
2816         return ret;                              2842         return ret;
2817 }                                                2843 }
2818                                                  2844 
2819 /*                                               2845 /*
2820  * Prepare meta_ac, data_ac and calculate cre    2846  * Prepare meta_ac, data_ac and calculate credits when we want to add some
2821  * num_clusters in data_tree "et" and change     2847  * num_clusters in data_tree "et" and change the refcount for the old
2822  * clusters(starting form p_cluster) in the r    2848  * clusters(starting form p_cluster) in the refcount tree.
2823  *                                               2849  *
2824  * Note:                                         2850  * Note:
2825  * 1. since we may split the old tree, so we     2851  * 1. since we may split the old tree, so we at most will need num_clusters + 2
2826  *    more new leaf records.                     2852  *    more new leaf records.
2827  * 2. In some case, we may not need to reserv    2853  * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
2828  *    just give data_ac = NULL.                  2854  *    just give data_ac = NULL.
2829  */                                              2855  */
2830 static int ocfs2_lock_refcount_allocators(str    2856 static int ocfs2_lock_refcount_allocators(struct super_block *sb,
2831                                         u32 p    2857                                         u32 p_cluster, u32 num_clusters,
2832                                         struc    2858                                         struct ocfs2_extent_tree *et,
2833                                         struc    2859                                         struct ocfs2_caching_info *ref_ci,
2834                                         struc    2860                                         struct buffer_head *ref_root_bh,
2835                                         struc    2861                                         struct ocfs2_alloc_context **meta_ac,
2836                                         struc    2862                                         struct ocfs2_alloc_context **data_ac,
2837                                         int *    2863                                         int *credits)
2838 {                                                2864 {
2839         int ret = 0, meta_add = 0;               2865         int ret = 0, meta_add = 0;
2840         int num_free_extents = ocfs2_num_free !! 2866         int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et);
2841                                                  2867 
2842         if (num_free_extents < 0) {              2868         if (num_free_extents < 0) {
2843                 ret = num_free_extents;          2869                 ret = num_free_extents;
2844                 mlog_errno(ret);                 2870                 mlog_errno(ret);
2845                 goto out;                        2871                 goto out;
2846         }                                        2872         }
2847                                                  2873 
2848         if (num_free_extents < num_clusters +    2874         if (num_free_extents < num_clusters + 2)
2849                 meta_add =                       2875                 meta_add =
2850                         ocfs2_extend_meta_nee    2876                         ocfs2_extend_meta_needed(et->et_root_el);
2851                                                  2877 
2852         *credits += ocfs2_calc_extend_credits !! 2878         *credits += ocfs2_calc_extend_credits(sb, et->et_root_el,
                                                   >> 2879                                               num_clusters + 2);
2853                                                  2880 
2854         ret = ocfs2_calc_refcount_meta_credit    2881         ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
2855                                                  2882                                                p_cluster, num_clusters,
2856                                                  2883                                                &meta_add, credits);
2857         if (ret) {                               2884         if (ret) {
2858                 mlog_errno(ret);                 2885                 mlog_errno(ret);
2859                 goto out;                        2886                 goto out;
2860         }                                        2887         }
2861                                                  2888 
2862         trace_ocfs2_lock_refcount_allocators(    2889         trace_ocfs2_lock_refcount_allocators(meta_add, *credits);
2863         ret = ocfs2_reserve_new_metadata_bloc    2890         ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
2864                                                  2891                                                 meta_ac);
2865         if (ret) {                               2892         if (ret) {
2866                 mlog_errno(ret);                 2893                 mlog_errno(ret);
2867                 goto out;                        2894                 goto out;
2868         }                                        2895         }
2869                                                  2896 
2870         if (data_ac) {                           2897         if (data_ac) {
2871                 ret = ocfs2_reserve_clusters(    2898                 ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
2872                                                  2899                                              data_ac);
2873                 if (ret)                         2900                 if (ret)
2874                         mlog_errno(ret);         2901                         mlog_errno(ret);
2875         }                                        2902         }
2876                                                  2903 
2877 out:                                             2904 out:
2878         if (ret) {                               2905         if (ret) {
2879                 if (*meta_ac) {                  2906                 if (*meta_ac) {
2880                         ocfs2_free_alloc_cont    2907                         ocfs2_free_alloc_context(*meta_ac);
2881                         *meta_ac = NULL;         2908                         *meta_ac = NULL;
2882                 }                                2909                 }
2883         }                                        2910         }
2884                                                  2911 
2885         return ret;                              2912         return ret;
2886 }                                                2913 }
2887                                                  2914 
2888 static int ocfs2_clear_cow_buffer(handle_t *h    2915 static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
2889 {                                                2916 {
2890         BUG_ON(buffer_dirty(bh));                2917         BUG_ON(buffer_dirty(bh));
2891                                                  2918 
2892         clear_buffer_mapped(bh);                 2919         clear_buffer_mapped(bh);
2893                                                  2920 
2894         return 0;                                2921         return 0;
2895 }                                                2922 }
2896                                                  2923 
2897 int ocfs2_duplicate_clusters_by_page(handle_t    2924 int ocfs2_duplicate_clusters_by_page(handle_t *handle,
2898                                      struct i !! 2925                                      struct file *file,
2899                                      u32 cpos    2926                                      u32 cpos, u32 old_cluster,
2900                                      u32 new_    2927                                      u32 new_cluster, u32 new_len)
2901 {                                                2928 {
2902         int ret = 0, partial;                    2929         int ret = 0, partial;
2903         struct super_block *sb = inode->i_sb; !! 2930         struct inode *inode = file_inode(file);
                                                   >> 2931         struct ocfs2_caching_info *ci = INODE_CACHE(inode);
                                                   >> 2932         struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2904         u64 new_block = ocfs2_clusters_to_blo    2933         u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
2905         struct page *page;                       2934         struct page *page;
2906         pgoff_t page_index;                      2935         pgoff_t page_index;
2907         unsigned int from, to;                !! 2936         unsigned int from, to, readahead_pages;
2908         loff_t offset, end, map_end;             2937         loff_t offset, end, map_end;
2909         struct address_space *mapping = inode    2938         struct address_space *mapping = inode->i_mapping;
2910                                                  2939 
2911         trace_ocfs2_duplicate_clusters_by_pag    2940         trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
2912                                                  2941                                                new_cluster, new_len);
2913                                                  2942 
                                                   >> 2943         readahead_pages =
                                                   >> 2944                 (ocfs2_cow_contig_clusters(sb) <<
                                                   >> 2945                  OCFS2_SB(sb)->s_clustersize_bits) >> PAGE_CACHE_SHIFT;
2914         offset = ((loff_t)cpos) << OCFS2_SB(s    2946         offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
2915         end = offset + (new_len << OCFS2_SB(s    2947         end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
2916         /*                                       2948         /*
2917          * We only duplicate pages until we r    2949          * We only duplicate pages until we reach the page contains i_size - 1.
2918          * So trim 'end' to i_size.              2950          * So trim 'end' to i_size.
2919          */                                      2951          */
2920         if (end > i_size_read(inode))            2952         if (end > i_size_read(inode))
2921                 end = i_size_read(inode);        2953                 end = i_size_read(inode);
2922                                                  2954 
2923         while (offset < end) {                   2955         while (offset < end) {
2924                 page_index = offset >> PAGE_S !! 2956                 page_index = offset >> PAGE_CACHE_SHIFT;
2925                 map_end = ((loff_t)page_index !! 2957                 map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
2926                 if (map_end > end)               2958                 if (map_end > end)
2927                         map_end = end;           2959                         map_end = end;
2928                                                  2960 
2929                 /* from, to is the offset wit    2961                 /* from, to is the offset within the page. */
2930                 from = offset & (PAGE_SIZE -  !! 2962                 from = offset & (PAGE_CACHE_SIZE - 1);
2931                 to = PAGE_SIZE;               !! 2963                 to = PAGE_CACHE_SIZE;
2932                 if (map_end & (PAGE_SIZE - 1) !! 2964                 if (map_end & (PAGE_CACHE_SIZE - 1))
2933                         to = map_end & (PAGE_ !! 2965                         to = map_end & (PAGE_CACHE_SIZE - 1);
2934                                                  2966 
2935 retry:                                        << 
2936                 page = find_or_create_page(ma    2967                 page = find_or_create_page(mapping, page_index, GFP_NOFS);
2937                 if (!page) {                  << 
2938                         ret = -ENOMEM;        << 
2939                         mlog_errno(ret);      << 
2940                         break;                << 
2941                 }                             << 
2942                                                  2968 
2943                 /*                               2969                 /*
2944                  * In case PAGE_SIZE <= CLUST !! 2970                  * In case PAGE_CACHE_SIZE <= CLUSTER_SIZE, This page
2945                  * page, so write it back.    !! 2971                  * can't be dirtied before we CoW it out.
2946                  */                              2972                  */
2947                 if (PAGE_SIZE <= OCFS2_SB(sb) !! 2973                 if (PAGE_CACHE_SIZE <= OCFS2_SB(sb)->s_clustersize)
2948                         if (PageDirty(page))  !! 2974                         BUG_ON(PageDirty(page));
2949                                 unlock_page(p !! 2975 
2950                                 put_page(page !! 2976                 if (PageReadahead(page)) {
2951                                               !! 2977                         page_cache_async_readahead(mapping,
2952                                 ret = filemap !! 2978                                                    &file->f_ra, file,
2953                                               !! 2979                                                    page, page_index,
2954                                 goto retry;   !! 2980                                                    readahead_pages);
2955                         }                     << 
2956                 }                                2981                 }
2957                                                  2982 
2958                 if (!PageUptodate(page)) {       2983                 if (!PageUptodate(page)) {
2959                         struct folio *folio = !! 2984                         ret = block_read_full_page(page, ocfs2_get_block);
2960                                               << 
2961                         ret = block_read_full << 
2962                         if (ret) {               2985                         if (ret) {
2963                                 mlog_errno(re    2986                                 mlog_errno(ret);
2964                                 goto unlock;     2987                                 goto unlock;
2965                         }                        2988                         }
2966                         folio_lock(folio);    !! 2989                         lock_page(page);
2967                 }                                2990                 }
2968                                                  2991 
2969                 if (page_has_buffers(page)) {    2992                 if (page_has_buffers(page)) {
2970                         ret = walk_page_buffe    2993                         ret = walk_page_buffers(handle, page_buffers(page),
2971                                                  2994                                                 from, to, &partial,
2972                                                  2995                                                 ocfs2_clear_cow_buffer);
2973                         if (ret) {               2996                         if (ret) {
2974                                 mlog_errno(re    2997                                 mlog_errno(ret);
2975                                 goto unlock;     2998                                 goto unlock;
2976                         }                        2999                         }
2977                 }                                3000                 }
2978                                                  3001 
2979                 ocfs2_map_and_dirty_page(inod !! 3002                 ocfs2_map_and_dirty_page(inode, handle, from, to,
2980                                          hand << 
2981                                          page    3003                                          page, 0, &new_block);
2982                 mark_page_accessed(page);        3004                 mark_page_accessed(page);
2983 unlock:                                          3005 unlock:
2984                 unlock_page(page);               3006                 unlock_page(page);
2985                 put_page(page);               !! 3007                 page_cache_release(page);
2986                 page = NULL;                     3008                 page = NULL;
2987                 offset = map_end;                3009                 offset = map_end;
2988                 if (ret)                         3010                 if (ret)
2989                         break;                   3011                         break;
2990         }                                        3012         }
2991                                                  3013 
2992         return ret;                              3014         return ret;
2993 }                                                3015 }
2994                                                  3016 
2995 int ocfs2_duplicate_clusters_by_jbd(handle_t     3017 int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
2996                                     struct in !! 3018                                     struct file *file,
2997                                     u32 cpos,    3019                                     u32 cpos, u32 old_cluster,
2998                                     u32 new_c    3020                                     u32 new_cluster, u32 new_len)
2999 {                                                3021 {
3000         int ret = 0;                             3022         int ret = 0;
                                                   >> 3023         struct inode *inode = file_inode(file);
3001         struct super_block *sb = inode->i_sb;    3024         struct super_block *sb = inode->i_sb;
3002         struct ocfs2_caching_info *ci = INODE    3025         struct ocfs2_caching_info *ci = INODE_CACHE(inode);
3003         int i, blocks = ocfs2_clusters_to_blo    3026         int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
3004         u64 old_block = ocfs2_clusters_to_blo    3027         u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
3005         u64 new_block = ocfs2_clusters_to_blo    3028         u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
3006         struct ocfs2_super *osb = OCFS2_SB(sb    3029         struct ocfs2_super *osb = OCFS2_SB(sb);
3007         struct buffer_head *old_bh = NULL;       3030         struct buffer_head *old_bh = NULL;
3008         struct buffer_head *new_bh = NULL;       3031         struct buffer_head *new_bh = NULL;
3009                                                  3032 
3010         trace_ocfs2_duplicate_clusters_by_pag    3033         trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
3011                                                  3034                                                new_cluster, new_len);
3012                                                  3035 
3013         for (i = 0; i < blocks; i++, old_bloc    3036         for (i = 0; i < blocks; i++, old_block++, new_block++) {
3014                 new_bh = sb_getblk(osb->sb, n    3037                 new_bh = sb_getblk(osb->sb, new_block);
3015                 if (new_bh == NULL) {            3038                 if (new_bh == NULL) {
3016                         ret = -ENOMEM;        !! 3039                         ret = -EIO;
3017                         mlog_errno(ret);         3040                         mlog_errno(ret);
3018                         break;                   3041                         break;
3019                 }                                3042                 }
3020                                                  3043 
3021                 ocfs2_set_new_buffer_uptodate    3044                 ocfs2_set_new_buffer_uptodate(ci, new_bh);
3022                                                  3045 
3023                 ret = ocfs2_read_block(ci, ol    3046                 ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
3024                 if (ret) {                       3047                 if (ret) {
3025                         mlog_errno(ret);         3048                         mlog_errno(ret);
3026                         break;                   3049                         break;
3027                 }                                3050                 }
3028                                                  3051 
3029                 ret = ocfs2_journal_access(ha    3052                 ret = ocfs2_journal_access(handle, ci, new_bh,
3030                                            OC    3053                                            OCFS2_JOURNAL_ACCESS_CREATE);
3031                 if (ret) {                       3054                 if (ret) {
3032                         mlog_errno(ret);         3055                         mlog_errno(ret);
3033                         break;                   3056                         break;
3034                 }                                3057                 }
3035                                                  3058 
3036                 memcpy(new_bh->b_data, old_bh    3059                 memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
3037                 ocfs2_journal_dirty(handle, n    3060                 ocfs2_journal_dirty(handle, new_bh);
3038                                                  3061 
3039                 brelse(new_bh);                  3062                 brelse(new_bh);
3040                 brelse(old_bh);                  3063                 brelse(old_bh);
3041                 new_bh = NULL;                   3064                 new_bh = NULL;
3042                 old_bh = NULL;                   3065                 old_bh = NULL;
3043         }                                        3066         }
3044                                                  3067 
3045         brelse(new_bh);                          3068         brelse(new_bh);
3046         brelse(old_bh);                          3069         brelse(old_bh);
3047         return ret;                              3070         return ret;
3048 }                                                3071 }
3049                                                  3072 
3050 static int ocfs2_clear_ext_refcount(handle_t     3073 static int ocfs2_clear_ext_refcount(handle_t *handle,
3051                                     struct oc    3074                                     struct ocfs2_extent_tree *et,
3052                                     u32 cpos,    3075                                     u32 cpos, u32 p_cluster, u32 len,
3053                                     unsigned     3076                                     unsigned int ext_flags,
3054                                     struct oc    3077                                     struct ocfs2_alloc_context *meta_ac,
3055                                     struct oc    3078                                     struct ocfs2_cached_dealloc_ctxt *dealloc)
3056 {                                                3079 {
3057         int ret, index;                          3080         int ret, index;
3058         struct ocfs2_extent_rec replace_rec;     3081         struct ocfs2_extent_rec replace_rec;
3059         struct ocfs2_path *path = NULL;          3082         struct ocfs2_path *path = NULL;
3060         struct ocfs2_extent_list *el;            3083         struct ocfs2_extent_list *el;
3061         struct super_block *sb = ocfs2_metada    3084         struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
3062         u64 ino = ocfs2_metadata_cache_owner(    3085         u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
3063                                                  3086 
3064         trace_ocfs2_clear_ext_refcount((unsig    3087         trace_ocfs2_clear_ext_refcount((unsigned long long)ino,
3065                                        cpos,     3088                                        cpos, len, p_cluster, ext_flags);
3066                                                  3089 
3067         memset(&replace_rec, 0, sizeof(replac    3090         memset(&replace_rec, 0, sizeof(replace_rec));
3068         replace_rec.e_cpos = cpu_to_le32(cpos    3091         replace_rec.e_cpos = cpu_to_le32(cpos);
3069         replace_rec.e_leaf_clusters = cpu_to_    3092         replace_rec.e_leaf_clusters = cpu_to_le16(len);
3070         replace_rec.e_blkno = cpu_to_le64(ocf    3093         replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
3071                                                  3094                                                                    p_cluster));
3072         replace_rec.e_flags = ext_flags;         3095         replace_rec.e_flags = ext_flags;
3073         replace_rec.e_flags &= ~OCFS2_EXT_REF    3096         replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
3074                                                  3097 
3075         path = ocfs2_new_path_from_et(et);       3098         path = ocfs2_new_path_from_et(et);
3076         if (!path) {                             3099         if (!path) {
3077                 ret = -ENOMEM;                   3100                 ret = -ENOMEM;
3078                 mlog_errno(ret);                 3101                 mlog_errno(ret);
3079                 goto out;                        3102                 goto out;
3080         }                                        3103         }
3081                                                  3104 
3082         ret = ocfs2_find_path(et->et_ci, path    3105         ret = ocfs2_find_path(et->et_ci, path, cpos);
3083         if (ret) {                               3106         if (ret) {
3084                 mlog_errno(ret);                 3107                 mlog_errno(ret);
3085                 goto out;                        3108                 goto out;
3086         }                                        3109         }
3087                                                  3110 
3088         el = path_leaf_el(path);                 3111         el = path_leaf_el(path);
3089                                                  3112 
3090         index = ocfs2_search_extent_list(el,     3113         index = ocfs2_search_extent_list(el, cpos);
3091         if (index == -1) {                    !! 3114         if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
3092                 ret = ocfs2_error(sb,         !! 3115                 ocfs2_error(sb,
3093                                   "Inode %llu !! 3116                             "Inode %llu has an extent at cpos %u which can no "
3094                                   (unsigned l !! 3117                             "longer be found.\n",
                                                   >> 3118                             (unsigned long long)ino, cpos);
                                                   >> 3119                 ret = -EROFS;
3095                 goto out;                        3120                 goto out;
3096         }                                        3121         }
3097                                                  3122 
3098         ret = ocfs2_split_extent(handle, et,     3123         ret = ocfs2_split_extent(handle, et, path, index,
3099                                  &replace_rec    3124                                  &replace_rec, meta_ac, dealloc);
3100         if (ret)                                 3125         if (ret)
3101                 mlog_errno(ret);                 3126                 mlog_errno(ret);
3102                                                  3127 
3103 out:                                             3128 out:
3104         ocfs2_free_path(path);                   3129         ocfs2_free_path(path);
3105         return ret;                              3130         return ret;
3106 }                                                3131 }
3107                                                  3132 
3108 static int ocfs2_replace_clusters(handle_t *h    3133 static int ocfs2_replace_clusters(handle_t *handle,
3109                                   struct ocfs    3134                                   struct ocfs2_cow_context *context,
3110                                   u32 cpos, u    3135                                   u32 cpos, u32 old,
3111                                   u32 new, u3    3136                                   u32 new, u32 len,
3112                                   unsigned in    3137                                   unsigned int ext_flags)
3113 {                                                3138 {
3114         int ret;                                 3139         int ret;
3115         struct ocfs2_caching_info *ci = conte    3140         struct ocfs2_caching_info *ci = context->data_et.et_ci;
3116         u64 ino = ocfs2_metadata_cache_owner(    3141         u64 ino = ocfs2_metadata_cache_owner(ci);
3117                                                  3142 
3118         trace_ocfs2_replace_clusters((unsigne    3143         trace_ocfs2_replace_clusters((unsigned long long)ino,
3119                                      cpos, ol    3144                                      cpos, old, new, len, ext_flags);
3120                                                  3145 
3121         /*If the old clusters is unwritten, n    3146         /*If the old clusters is unwritten, no need to duplicate. */
3122         if (!(ext_flags & OCFS2_EXT_UNWRITTEN    3147         if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
3123                 ret = context->cow_duplicate_ !! 3148                 ret = context->cow_duplicate_clusters(handle, context->file,
3124                                                  3149                                                       cpos, old, new, len);
3125                 if (ret) {                       3150                 if (ret) {
3126                         mlog_errno(ret);         3151                         mlog_errno(ret);
3127                         goto out;                3152                         goto out;
3128                 }                                3153                 }
3129         }                                        3154         }
3130                                                  3155 
3131         ret = ocfs2_clear_ext_refcount(handle    3156         ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
3132                                        cpos,     3157                                        cpos, new, len, ext_flags,
3133                                        contex    3158                                        context->meta_ac, &context->dealloc);
3134         if (ret)                                 3159         if (ret)
3135                 mlog_errno(ret);                 3160                 mlog_errno(ret);
3136 out:                                             3161 out:
3137         return ret;                              3162         return ret;
3138 }                                                3163 }
3139                                                  3164 
3140 int ocfs2_cow_sync_writeback(struct super_blo    3165 int ocfs2_cow_sync_writeback(struct super_block *sb,
3141                              struct inode *in    3166                              struct inode *inode,
3142                              u32 cpos, u32 nu    3167                              u32 cpos, u32 num_clusters)
3143 {                                                3168 {
3144         int ret;                              !! 3169         int ret = 0;
3145         loff_t start, end;                    !! 3170         loff_t offset, end, map_end;
                                                   >> 3171         pgoff_t page_index;
                                                   >> 3172         struct page *page;
3146                                                  3173 
3147         if (ocfs2_should_order_data(inode))      3174         if (ocfs2_should_order_data(inode))
3148                 return 0;                        3175                 return 0;
3149                                                  3176 
3150         start = ((loff_t)cpos) << OCFS2_SB(sb !! 3177         offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
3151         end = start + (num_clusters << OCFS2_ !! 3178         end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
3152                                                  3179 
3153         ret = filemap_write_and_wait_range(in !! 3180         ret = filemap_fdatawrite_range(inode->i_mapping,
3154         if (ret < 0)                          !! 3181                                        offset, end - 1);
                                                   >> 3182         if (ret < 0) {
3155                 mlog_errno(ret);                 3183                 mlog_errno(ret);
                                                   >> 3184                 return ret;
                                                   >> 3185         }
                                                   >> 3186 
                                                   >> 3187         while (offset < end) {
                                                   >> 3188                 page_index = offset >> PAGE_CACHE_SHIFT;
                                                   >> 3189                 map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
                                                   >> 3190                 if (map_end > end)
                                                   >> 3191                         map_end = end;
                                                   >> 3192 
                                                   >> 3193                 page = find_or_create_page(inode->i_mapping,
                                                   >> 3194                                            page_index, GFP_NOFS);
                                                   >> 3195                 BUG_ON(!page);
                                                   >> 3196 
                                                   >> 3197                 wait_on_page_writeback(page);
                                                   >> 3198                 if (PageError(page)) {
                                                   >> 3199                         ret = -EIO;
                                                   >> 3200                         mlog_errno(ret);
                                                   >> 3201                 } else
                                                   >> 3202                         mark_page_accessed(page);
                                                   >> 3203 
                                                   >> 3204                 unlock_page(page);
                                                   >> 3205                 page_cache_release(page);
                                                   >> 3206                 page = NULL;
                                                   >> 3207                 offset = map_end;
                                                   >> 3208                 if (ret)
                                                   >> 3209                         break;
                                                   >> 3210         }
3156                                                  3211 
3157         return ret;                              3212         return ret;
3158 }                                                3213 }
3159                                                  3214 
3160 static int ocfs2_di_get_clusters(struct ocfs2    3215 static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
3161                                  u32 v_cluste    3216                                  u32 v_cluster, u32 *p_cluster,
3162                                  u32 *num_clu    3217                                  u32 *num_clusters,
3163                                  unsigned int    3218                                  unsigned int *extent_flags)
3164 {                                                3219 {
3165         return ocfs2_get_clusters(context->in    3220         return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
3166                                   num_cluster    3221                                   num_clusters, extent_flags);
3167 }                                                3222 }
3168                                                  3223 
3169 static int ocfs2_make_clusters_writable(struc    3224 static int ocfs2_make_clusters_writable(struct super_block *sb,
3170                                         struc    3225                                         struct ocfs2_cow_context *context,
3171                                         u32 c    3226                                         u32 cpos, u32 p_cluster,
3172                                         u32 n    3227                                         u32 num_clusters, unsigned int e_flags)
3173 {                                                3228 {
3174         int ret, delete, index, credits =  0;    3229         int ret, delete, index, credits =  0;
3175         u32 new_bit, new_len, orig_num_cluste    3230         u32 new_bit, new_len, orig_num_clusters;
3176         unsigned int set_len;                    3231         unsigned int set_len;
3177         struct ocfs2_super *osb = OCFS2_SB(sb    3232         struct ocfs2_super *osb = OCFS2_SB(sb);
3178         handle_t *handle;                        3233         handle_t *handle;
3179         struct buffer_head *ref_leaf_bh = NUL    3234         struct buffer_head *ref_leaf_bh = NULL;
3180         struct ocfs2_caching_info *ref_ci = &    3235         struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
3181         struct ocfs2_refcount_rec rec;           3236         struct ocfs2_refcount_rec rec;
3182                                                  3237 
3183         trace_ocfs2_make_clusters_writable(cp    3238         trace_ocfs2_make_clusters_writable(cpos, p_cluster,
3184                                            nu    3239                                            num_clusters, e_flags);
3185                                                  3240 
3186         ret = ocfs2_lock_refcount_allocators(    3241         ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
3187                                                  3242                                              &context->data_et,
3188                                                  3243                                              ref_ci,
3189                                                  3244                                              context->ref_root_bh,
3190                                                  3245                                              &context->meta_ac,
3191                                                  3246                                              &context->data_ac, &credits);
3192         if (ret) {                               3247         if (ret) {
3193                 mlog_errno(ret);                 3248                 mlog_errno(ret);
3194                 return ret;                      3249                 return ret;
3195         }                                        3250         }
3196                                                  3251 
3197         if (context->post_refcount)              3252         if (context->post_refcount)
3198                 credits += context->post_refc    3253                 credits += context->post_refcount->credits;
3199                                                  3254 
3200         credits += context->extra_credits;       3255         credits += context->extra_credits;
3201         handle = ocfs2_start_trans(osb, credi    3256         handle = ocfs2_start_trans(osb, credits);
3202         if (IS_ERR(handle)) {                    3257         if (IS_ERR(handle)) {
3203                 ret = PTR_ERR(handle);           3258                 ret = PTR_ERR(handle);
3204                 mlog_errno(ret);                 3259                 mlog_errno(ret);
3205                 goto out;                        3260                 goto out;
3206         }                                        3261         }
3207                                                  3262 
3208         orig_num_clusters = num_clusters;        3263         orig_num_clusters = num_clusters;
3209                                                  3264 
3210         while (num_clusters) {                   3265         while (num_clusters) {
3211                 ret = ocfs2_get_refcount_rec(    3266                 ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
3212                                                  3267                                              p_cluster, num_clusters,
3213                                                  3268                                              &rec, &index, &ref_leaf_bh);
3214                 if (ret) {                       3269                 if (ret) {
3215                         mlog_errno(ret);         3270                         mlog_errno(ret);
3216                         goto out_commit;         3271                         goto out_commit;
3217                 }                                3272                 }
3218                                                  3273 
3219                 BUG_ON(!rec.r_refcount);         3274                 BUG_ON(!rec.r_refcount);
3220                 set_len = min((u64)p_cluster     3275                 set_len = min((u64)p_cluster + num_clusters,
3221                               le64_to_cpu(rec    3276                               le64_to_cpu(rec.r_cpos) +
3222                               le32_to_cpu(rec    3277                               le32_to_cpu(rec.r_clusters)) - p_cluster;
3223                                                  3278 
3224                 /*                               3279                 /*
3225                  * There are many different s    3280                  * There are many different situation here.
3226                  * 1. If refcount == 1, remov    3281                  * 1. If refcount == 1, remove the flag and don't COW.
3227                  * 2. If refcount > 1, alloca    3282                  * 2. If refcount > 1, allocate clusters.
3228                  *    Here we may not allocat    3283                  *    Here we may not allocate r_len once at a time, so continue
3229                  *    until we reach num_clus    3284                  *    until we reach num_clusters.
3230                  */                              3285                  */
3231                 if (le32_to_cpu(rec.r_refcoun    3286                 if (le32_to_cpu(rec.r_refcount) == 1) {
3232                         delete = 0;              3287                         delete = 0;
3233                         ret = ocfs2_clear_ext    3288                         ret = ocfs2_clear_ext_refcount(handle,
3234                                                  3289                                                        &context->data_et,
3235                                                  3290                                                        cpos, p_cluster,
3236                                                  3291                                                        set_len, e_flags,
3237                                                  3292                                                        context->meta_ac,
3238                                                  3293                                                        &context->dealloc);
3239                         if (ret) {               3294                         if (ret) {
3240                                 mlog_errno(re    3295                                 mlog_errno(ret);
3241                                 goto out_comm    3296                                 goto out_commit;
3242                         }                        3297                         }
3243                 } else {                         3298                 } else {
3244                         delete = 1;              3299                         delete = 1;
3245                                                  3300 
3246                         ret = __ocfs2_claim_c    3301                         ret = __ocfs2_claim_clusters(handle,
3247                                                  3302                                                      context->data_ac,
3248                                                  3303                                                      1, set_len,
3249                                                  3304                                                      &new_bit, &new_len);
3250                         if (ret) {               3305                         if (ret) {
3251                                 mlog_errno(re    3306                                 mlog_errno(ret);
3252                                 goto out_comm    3307                                 goto out_commit;
3253                         }                        3308                         }
3254                                                  3309 
3255                         ret = ocfs2_replace_c    3310                         ret = ocfs2_replace_clusters(handle, context,
3256                                                  3311                                                      cpos, p_cluster, new_bit,
3257                                                  3312                                                      new_len, e_flags);
3258                         if (ret) {               3313                         if (ret) {
3259                                 mlog_errno(re    3314                                 mlog_errno(ret);
3260                                 goto out_comm    3315                                 goto out_commit;
3261                         }                        3316                         }
3262                         set_len = new_len;       3317                         set_len = new_len;
3263                 }                                3318                 }
3264                                                  3319 
3265                 ret = __ocfs2_decrease_refcou    3320                 ret = __ocfs2_decrease_refcount(handle, ref_ci,
3266                                                  3321                                                 context->ref_root_bh,
3267                                                  3322                                                 p_cluster, set_len,
3268                                                  3323                                                 context->meta_ac,
3269                                                  3324                                                 &context->dealloc, delete);
3270                 if (ret) {                       3325                 if (ret) {
3271                         mlog_errno(ret);         3326                         mlog_errno(ret);
3272                         goto out_commit;         3327                         goto out_commit;
3273                 }                                3328                 }
3274                                                  3329 
3275                 cpos += set_len;                 3330                 cpos += set_len;
3276                 p_cluster += set_len;            3331                 p_cluster += set_len;
3277                 num_clusters -= set_len;         3332                 num_clusters -= set_len;
3278                 brelse(ref_leaf_bh);             3333                 brelse(ref_leaf_bh);
3279                 ref_leaf_bh = NULL;              3334                 ref_leaf_bh = NULL;
3280         }                                        3335         }
3281                                                  3336 
3282         /* handle any post_cow action. */        3337         /* handle any post_cow action. */
3283         if (context->post_refcount && context    3338         if (context->post_refcount && context->post_refcount->func) {
3284                 ret = context->post_refcount-    3339                 ret = context->post_refcount->func(context->inode, handle,
3285                                                  3340                                                 context->post_refcount->para);
3286                 if (ret) {                       3341                 if (ret) {
3287                         mlog_errno(ret);         3342                         mlog_errno(ret);
3288                         goto out_commit;         3343                         goto out_commit;
3289                 }                                3344                 }
3290         }                                        3345         }
3291                                                  3346 
3292         /*                                       3347         /*
3293          * Here we should write the new page     3348          * Here we should write the new page out first if we are
3294          * in write-back mode.                   3349          * in write-back mode.
3295          */                                      3350          */
3296         if (context->get_clusters == ocfs2_di    3351         if (context->get_clusters == ocfs2_di_get_clusters) {
3297                 ret = ocfs2_cow_sync_writebac    3352                 ret = ocfs2_cow_sync_writeback(sb, context->inode, cpos,
3298                                                  3353                                                orig_num_clusters);
3299                 if (ret)                         3354                 if (ret)
3300                         mlog_errno(ret);         3355                         mlog_errno(ret);
3301         }                                        3356         }
3302                                                  3357 
3303 out_commit:                                      3358 out_commit:
3304         ocfs2_commit_trans(osb, handle);         3359         ocfs2_commit_trans(osb, handle);
3305                                                  3360 
3306 out:                                             3361 out:
3307         if (context->data_ac) {                  3362         if (context->data_ac) {
3308                 ocfs2_free_alloc_context(cont    3363                 ocfs2_free_alloc_context(context->data_ac);
3309                 context->data_ac = NULL;         3364                 context->data_ac = NULL;
3310         }                                        3365         }
3311         if (context->meta_ac) {                  3366         if (context->meta_ac) {
3312                 ocfs2_free_alloc_context(cont    3367                 ocfs2_free_alloc_context(context->meta_ac);
3313                 context->meta_ac = NULL;         3368                 context->meta_ac = NULL;
3314         }                                        3369         }
3315         brelse(ref_leaf_bh);                     3370         brelse(ref_leaf_bh);
3316                                                  3371 
3317         return ret;                              3372         return ret;
3318 }                                                3373 }
3319                                                  3374 
3320 static int ocfs2_replace_cow(struct ocfs2_cow    3375 static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
3321 {                                                3376 {
3322         int ret = 0;                             3377         int ret = 0;
3323         struct inode *inode = context->inode;    3378         struct inode *inode = context->inode;
3324         u32 cow_start = context->cow_start, c    3379         u32 cow_start = context->cow_start, cow_len = context->cow_len;
3325         u32 p_cluster, num_clusters;             3380         u32 p_cluster, num_clusters;
3326         unsigned int ext_flags;                  3381         unsigned int ext_flags;
3327         struct ocfs2_super *osb = OCFS2_SB(in    3382         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3328                                                  3383 
3329         if (!ocfs2_refcount_tree(osb)) {      !! 3384         if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
3330                 return ocfs2_error(inode->i_s !! 3385                 ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
3331                                    inode->i_i !! 3386                             "tree, but the feature bit is not set in the "
                                                   >> 3387                             "super block.", inode->i_ino);
                                                   >> 3388                 return -EROFS;
3332         }                                        3389         }
3333                                                  3390 
3334         ocfs2_init_dealloc_ctxt(&context->dea    3391         ocfs2_init_dealloc_ctxt(&context->dealloc);
3335                                                  3392 
3336         while (cow_len) {                        3393         while (cow_len) {
3337                 ret = context->get_clusters(c    3394                 ret = context->get_clusters(context, cow_start, &p_cluster,
3338                                             &    3395                                             &num_clusters, &ext_flags);
3339                 if (ret) {                       3396                 if (ret) {
3340                         mlog_errno(ret);         3397                         mlog_errno(ret);
3341                         break;                   3398                         break;
3342                 }                                3399                 }
3343                                                  3400 
3344                 BUG_ON(!(ext_flags & OCFS2_EX    3401                 BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
3345                                                  3402 
3346                 if (cow_len < num_clusters)      3403                 if (cow_len < num_clusters)
3347                         num_clusters = cow_le    3404                         num_clusters = cow_len;
3348                                                  3405 
3349                 ret = ocfs2_make_clusters_wri    3406                 ret = ocfs2_make_clusters_writable(inode->i_sb, context,
3350                                                  3407                                                    cow_start, p_cluster,
3351                                                  3408                                                    num_clusters, ext_flags);
3352                 if (ret) {                       3409                 if (ret) {
3353                         mlog_errno(ret);         3410                         mlog_errno(ret);
3354                         break;                   3411                         break;
3355                 }                                3412                 }
3356                                                  3413 
3357                 cow_len -= num_clusters;         3414                 cow_len -= num_clusters;
3358                 cow_start += num_clusters;       3415                 cow_start += num_clusters;
3359         }                                        3416         }
3360                                                  3417 
3361         if (ocfs2_dealloc_has_cluster(&contex    3418         if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
3362                 ocfs2_schedule_truncate_log_f    3419                 ocfs2_schedule_truncate_log_flush(osb, 1);
3363                 ocfs2_run_deallocs(osb, &cont    3420                 ocfs2_run_deallocs(osb, &context->dealloc);
3364         }                                        3421         }
3365                                                  3422 
3366         return ret;                              3423         return ret;
3367 }                                                3424 }
3368                                                  3425 
                                                   >> 3426 static void ocfs2_readahead_for_cow(struct inode *inode,
                                                   >> 3427                                     struct file *file,
                                                   >> 3428                                     u32 start, u32 len)
                                                   >> 3429 {
                                                   >> 3430         struct address_space *mapping;
                                                   >> 3431         pgoff_t index;
                                                   >> 3432         unsigned long num_pages;
                                                   >> 3433         int cs_bits = OCFS2_SB(inode->i_sb)->s_clustersize_bits;
                                                   >> 3434 
                                                   >> 3435         if (!file)
                                                   >> 3436                 return;
                                                   >> 3437 
                                                   >> 3438         mapping = file->f_mapping;
                                                   >> 3439         num_pages = (len << cs_bits) >> PAGE_CACHE_SHIFT;
                                                   >> 3440         if (!num_pages)
                                                   >> 3441                 num_pages = 1;
                                                   >> 3442 
                                                   >> 3443         index = ((loff_t)start << cs_bits) >> PAGE_CACHE_SHIFT;
                                                   >> 3444         page_cache_sync_readahead(mapping, &file->f_ra, file,
                                                   >> 3445                                   index, num_pages);
                                                   >> 3446 }
                                                   >> 3447 
3369 /*                                               3448 /*
3370  * Starting at cpos, try to CoW write_len clu    3449  * Starting at cpos, try to CoW write_len clusters.  Don't CoW
3371  * past max_cpos.  This will stop when it run    3450  * past max_cpos.  This will stop when it runs into a hole or an
3372  * unrefcounted extent.                          3451  * unrefcounted extent.
3373  */                                              3452  */
3374 static int ocfs2_refcount_cow_hunk(struct ino    3453 static int ocfs2_refcount_cow_hunk(struct inode *inode,
                                                   >> 3454                                    struct file *file,
3375                                    struct buf    3455                                    struct buffer_head *di_bh,
3376                                    u32 cpos,     3456                                    u32 cpos, u32 write_len, u32 max_cpos)
3377 {                                                3457 {
3378         int ret;                                 3458         int ret;
3379         u32 cow_start = 0, cow_len = 0;          3459         u32 cow_start = 0, cow_len = 0;
                                                   >> 3460         struct ocfs2_inode_info *oi = OCFS2_I(inode);
3380         struct ocfs2_super *osb = OCFS2_SB(in    3461         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3381         struct ocfs2_dinode *di = (struct ocf    3462         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3382         struct buffer_head *ref_root_bh = NUL    3463         struct buffer_head *ref_root_bh = NULL;
3383         struct ocfs2_refcount_tree *ref_tree;    3464         struct ocfs2_refcount_tree *ref_tree;
3384         struct ocfs2_cow_context *context = N    3465         struct ocfs2_cow_context *context = NULL;
3385                                                  3466 
3386         BUG_ON(!ocfs2_is_refcount_inode(inode !! 3467         BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
3387                                                  3468 
3388         ret = ocfs2_refcount_cal_cow_clusters    3469         ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
3389                                                  3470                                               cpos, write_len, max_cpos,
3390                                                  3471                                               &cow_start, &cow_len);
3391         if (ret) {                               3472         if (ret) {
3392                 mlog_errno(ret);                 3473                 mlog_errno(ret);
3393                 goto out;                        3474                 goto out;
3394         }                                        3475         }
3395                                                  3476 
3396         trace_ocfs2_refcount_cow_hunk(OCFS2_I    3477         trace_ocfs2_refcount_cow_hunk(OCFS2_I(inode)->ip_blkno,
3397                                       cpos, w    3478                                       cpos, write_len, max_cpos,
3398                                       cow_sta    3479                                       cow_start, cow_len);
3399                                                  3480 
3400         BUG_ON(cow_len == 0);                    3481         BUG_ON(cow_len == 0);
3401                                                  3482 
                                                   >> 3483         ocfs2_readahead_for_cow(inode, file, cow_start, cow_len);
                                                   >> 3484 
3402         context = kzalloc(sizeof(struct ocfs2    3485         context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3403         if (!context) {                          3486         if (!context) {
3404                 ret = -ENOMEM;                   3487                 ret = -ENOMEM;
3405                 mlog_errno(ret);                 3488                 mlog_errno(ret);
3406                 goto out;                        3489                 goto out;
3407         }                                        3490         }
3408                                                  3491 
3409         ret = ocfs2_lock_refcount_tree(osb, l    3492         ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
3410                                        1, &re    3493                                        1, &ref_tree, &ref_root_bh);
3411         if (ret) {                               3494         if (ret) {
3412                 mlog_errno(ret);                 3495                 mlog_errno(ret);
3413                 goto out;                        3496                 goto out;
3414         }                                        3497         }
3415                                                  3498 
3416         context->inode = inode;                  3499         context->inode = inode;
3417         context->cow_start = cow_start;          3500         context->cow_start = cow_start;
3418         context->cow_len = cow_len;              3501         context->cow_len = cow_len;
3419         context->ref_tree = ref_tree;            3502         context->ref_tree = ref_tree;
3420         context->ref_root_bh = ref_root_bh;      3503         context->ref_root_bh = ref_root_bh;
3421         context->cow_duplicate_clusters = ocf    3504         context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
3422         context->get_clusters = ocfs2_di_get_    3505         context->get_clusters = ocfs2_di_get_clusters;
                                                   >> 3506         context->file = file;
3423                                                  3507 
3424         ocfs2_init_dinode_extent_tree(&contex    3508         ocfs2_init_dinode_extent_tree(&context->data_et,
3425                                       INODE_C    3509                                       INODE_CACHE(inode), di_bh);
3426                                                  3510 
3427         ret = ocfs2_replace_cow(context);        3511         ret = ocfs2_replace_cow(context);
3428         if (ret)                                 3512         if (ret)
3429                 mlog_errno(ret);                 3513                 mlog_errno(ret);
3430                                                  3514 
3431         /*                                       3515         /*
3432          * truncate the extent map here since    3516          * truncate the extent map here since no matter whether we meet with
3433          * any error during the action, we sh    3517          * any error during the action, we shouldn't trust cached extent map
3434          * any more.                             3518          * any more.
3435          */                                      3519          */
3436         ocfs2_extent_map_trunc(inode, cow_sta    3520         ocfs2_extent_map_trunc(inode, cow_start);
3437                                                  3521 
3438         ocfs2_unlock_refcount_tree(osb, ref_t    3522         ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3439         brelse(ref_root_bh);                     3523         brelse(ref_root_bh);
3440 out:                                             3524 out:
3441         kfree(context);                          3525         kfree(context);
3442         return ret;                              3526         return ret;
3443 }                                                3527 }
3444                                                  3528 
3445 /*                                               3529 /*
3446  * CoW any and all clusters between cpos and     3530  * CoW any and all clusters between cpos and cpos+write_len.
3447  * Don't CoW past max_cpos.  If this returns     3531  * Don't CoW past max_cpos.  If this returns successfully, all
3448  * clusters between cpos and cpos+write_len a    3532  * clusters between cpos and cpos+write_len are safe to modify.
3449  */                                              3533  */
3450 int ocfs2_refcount_cow(struct inode *inode,      3534 int ocfs2_refcount_cow(struct inode *inode,
                                                   >> 3535                        struct file *file,
3451                        struct buffer_head *di    3536                        struct buffer_head *di_bh,
3452                        u32 cpos, u32 write_le    3537                        u32 cpos, u32 write_len, u32 max_cpos)
3453 {                                                3538 {
3454         int ret = 0;                             3539         int ret = 0;
3455         u32 p_cluster, num_clusters;             3540         u32 p_cluster, num_clusters;
3456         unsigned int ext_flags;                  3541         unsigned int ext_flags;
3457                                                  3542 
3458         while (write_len) {                      3543         while (write_len) {
3459                 ret = ocfs2_get_clusters(inod    3544                 ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3460                                          &num    3545                                          &num_clusters, &ext_flags);
3461                 if (ret) {                       3546                 if (ret) {
3462                         mlog_errno(ret);         3547                         mlog_errno(ret);
3463                         break;                   3548                         break;
3464                 }                                3549                 }
3465                                                  3550 
3466                 if (write_len < num_clusters)    3551                 if (write_len < num_clusters)
3467                         num_clusters = write_    3552                         num_clusters = write_len;
3468                                                  3553 
3469                 if (ext_flags & OCFS2_EXT_REF    3554                 if (ext_flags & OCFS2_EXT_REFCOUNTED) {
3470                         ret = ocfs2_refcount_ !! 3555                         ret = ocfs2_refcount_cow_hunk(inode, file, di_bh, cpos,
3471                                                  3556                                                       num_clusters, max_cpos);
3472                         if (ret) {               3557                         if (ret) {
3473                                 mlog_errno(re    3558                                 mlog_errno(ret);
3474                                 break;           3559                                 break;
3475                         }                        3560                         }
3476                 }                                3561                 }
3477                                                  3562 
3478                 write_len -= num_clusters;       3563                 write_len -= num_clusters;
3479                 cpos += num_clusters;            3564                 cpos += num_clusters;
3480         }                                        3565         }
3481                                                  3566 
3482         return ret;                              3567         return ret;
3483 }                                                3568 }
3484                                                  3569 
3485 static int ocfs2_xattr_value_get_clusters(str    3570 static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
3486                                           u32    3571                                           u32 v_cluster, u32 *p_cluster,
3487                                           u32    3572                                           u32 *num_clusters,
3488                                           uns    3573                                           unsigned int *extent_flags)
3489 {                                                3574 {
3490         struct inode *inode = context->inode;    3575         struct inode *inode = context->inode;
3491         struct ocfs2_xattr_value_root *xv = c    3576         struct ocfs2_xattr_value_root *xv = context->cow_object;
3492                                                  3577 
3493         return ocfs2_xattr_get_clusters(inode    3578         return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
3494                                         num_c    3579                                         num_clusters, &xv->xr_list,
3495                                         exten    3580                                         extent_flags);
3496 }                                                3581 }
3497                                                  3582 
3498 /*                                               3583 /*
3499  * Given a xattr value root, calculate the mo    3584  * Given a xattr value root, calculate the most meta/credits we need for
3500  * refcount tree change if we truncate it to     3585  * refcount tree change if we truncate it to 0.
3501  */                                              3586  */
3502 int ocfs2_refcounted_xattr_delete_need(struct    3587 int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
3503                                        struct    3588                                        struct ocfs2_caching_info *ref_ci,
3504                                        struct    3589                                        struct buffer_head *ref_root_bh,
3505                                        struct    3590                                        struct ocfs2_xattr_value_root *xv,
3506                                        int *m    3591                                        int *meta_add, int *credits)
3507 {                                                3592 {
3508         int ret = 0, index, ref_blocks = 0;      3593         int ret = 0, index, ref_blocks = 0;
3509         u32 p_cluster, num_clusters;             3594         u32 p_cluster, num_clusters;
3510         u32 cpos = 0, clusters = le32_to_cpu(    3595         u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
3511         struct ocfs2_refcount_block *rb;         3596         struct ocfs2_refcount_block *rb;
3512         struct ocfs2_refcount_rec rec;           3597         struct ocfs2_refcount_rec rec;
3513         struct buffer_head *ref_leaf_bh = NUL    3598         struct buffer_head *ref_leaf_bh = NULL;
3514                                                  3599 
3515         while (cpos < clusters) {                3600         while (cpos < clusters) {
3516                 ret = ocfs2_xattr_get_cluster    3601                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
3517                                                  3602                                                &num_clusters, &xv->xr_list,
3518                                                  3603                                                NULL);
3519                 if (ret) {                       3604                 if (ret) {
3520                         mlog_errno(ret);         3605                         mlog_errno(ret);
3521                         goto out;                3606                         goto out;
3522                 }                                3607                 }
3523                                                  3608 
3524                 cpos += num_clusters;            3609                 cpos += num_clusters;
3525                                                  3610 
3526                 while (num_clusters) {           3611                 while (num_clusters) {
3527                         ret = ocfs2_get_refco    3612                         ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
3528                                                  3613                                                      p_cluster, num_clusters,
3529                                                  3614                                                      &rec, &index,
3530                                                  3615                                                      &ref_leaf_bh);
3531                         if (ret) {               3616                         if (ret) {
3532                                 mlog_errno(re    3617                                 mlog_errno(ret);
3533                                 goto out;        3618                                 goto out;
3534                         }                        3619                         }
3535                                                  3620 
3536                         BUG_ON(!rec.r_refcoun    3621                         BUG_ON(!rec.r_refcount);
3537                                                  3622 
3538                         rb = (struct ocfs2_re    3623                         rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
3539                                                  3624 
3540                         /*                       3625                         /*
3541                          * We really don't kn    3626                          * We really don't know whether the other clusters is in
3542                          * this refcount bloc    3627                          * this refcount block or not, so just take the worst
3543                          * case that all the     3628                          * case that all the clusters are in this block and each
3544                          * one will split a r    3629                          * one will split a refcount rec, so totally we need
3545                          * clusters * 2 new r    3630                          * clusters * 2 new refcount rec.
3546                          */                      3631                          */
3547                         if (le16_to_cpu(rb->r    3632                         if (le16_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
3548                             le16_to_cpu(rb->r    3633                             le16_to_cpu(rb->rf_records.rl_count))
3549                                 ref_blocks++;    3634                                 ref_blocks++;
3550                                                  3635 
3551                         *credits += 1;           3636                         *credits += 1;
3552                         brelse(ref_leaf_bh);     3637                         brelse(ref_leaf_bh);
3553                         ref_leaf_bh = NULL;      3638                         ref_leaf_bh = NULL;
3554                                                  3639 
3555                         if (num_clusters <= l    3640                         if (num_clusters <= le32_to_cpu(rec.r_clusters))
3556                                 break;           3641                                 break;
3557                         else                     3642                         else
3558                                 num_clusters     3643                                 num_clusters -= le32_to_cpu(rec.r_clusters);
3559                         p_cluster += num_clus    3644                         p_cluster += num_clusters;
3560                 }                                3645                 }
3561         }                                        3646         }
3562                                                  3647 
3563         *meta_add += ref_blocks;                 3648         *meta_add += ref_blocks;
3564         if (!ref_blocks)                         3649         if (!ref_blocks)
3565                 goto out;                        3650                 goto out;
3566                                                  3651 
3567         rb = (struct ocfs2_refcount_block *)r    3652         rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3568         if (le32_to_cpu(rb->rf_flags) & OCFS2    3653         if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
3569                 *credits += OCFS2_EXPAND_REFC    3654                 *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
3570         else {                                   3655         else {
3571                 struct ocfs2_extent_tree et;     3656                 struct ocfs2_extent_tree et;
3572                                                  3657 
3573                 ocfs2_init_refcount_extent_tr    3658                 ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
3574                 *credits += ocfs2_calc_extend    3659                 *credits += ocfs2_calc_extend_credits(inode->i_sb,
3575                                               !! 3660                                                       et.et_root_el,
                                                   >> 3661                                                       ref_blocks);
3576         }                                        3662         }
3577                                                  3663 
3578 out:                                             3664 out:
3579         brelse(ref_leaf_bh);                     3665         brelse(ref_leaf_bh);
3580         return ret;                              3666         return ret;
3581 }                                                3667 }
3582                                                  3668 
3583 /*                                               3669 /*
3584  * Do CoW for xattr.                             3670  * Do CoW for xattr.
3585  */                                              3671  */
3586 int ocfs2_refcount_cow_xattr(struct inode *in    3672 int ocfs2_refcount_cow_xattr(struct inode *inode,
3587                              struct ocfs2_din    3673                              struct ocfs2_dinode *di,
3588                              struct ocfs2_xat    3674                              struct ocfs2_xattr_value_buf *vb,
3589                              struct ocfs2_ref    3675                              struct ocfs2_refcount_tree *ref_tree,
3590                              struct buffer_he    3676                              struct buffer_head *ref_root_bh,
3591                              u32 cpos, u32 wr    3677                              u32 cpos, u32 write_len,
3592                              struct ocfs2_pos    3678                              struct ocfs2_post_refcount *post)
3593 {                                                3679 {
3594         int ret;                                 3680         int ret;
3595         struct ocfs2_xattr_value_root *xv = v    3681         struct ocfs2_xattr_value_root *xv = vb->vb_xv;
                                                   >> 3682         struct ocfs2_inode_info *oi = OCFS2_I(inode);
3596         struct ocfs2_cow_context *context = N    3683         struct ocfs2_cow_context *context = NULL;
3597         u32 cow_start, cow_len;                  3684         u32 cow_start, cow_len;
3598                                                  3685 
3599         BUG_ON(!ocfs2_is_refcount_inode(inode !! 3686         BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
3600                                                  3687 
3601         ret = ocfs2_refcount_cal_cow_clusters    3688         ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
3602                                                  3689                                               cpos, write_len, UINT_MAX,
3603                                                  3690                                               &cow_start, &cow_len);
3604         if (ret) {                               3691         if (ret) {
3605                 mlog_errno(ret);                 3692                 mlog_errno(ret);
3606                 goto out;                        3693                 goto out;
3607         }                                        3694         }
3608                                                  3695 
3609         BUG_ON(cow_len == 0);                    3696         BUG_ON(cow_len == 0);
3610                                                  3697 
3611         context = kzalloc(sizeof(struct ocfs2    3698         context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3612         if (!context) {                          3699         if (!context) {
3613                 ret = -ENOMEM;                   3700                 ret = -ENOMEM;
3614                 mlog_errno(ret);                 3701                 mlog_errno(ret);
3615                 goto out;                        3702                 goto out;
3616         }                                        3703         }
3617                                                  3704 
3618         context->inode = inode;                  3705         context->inode = inode;
3619         context->cow_start = cow_start;          3706         context->cow_start = cow_start;
3620         context->cow_len = cow_len;              3707         context->cow_len = cow_len;
3621         context->ref_tree = ref_tree;            3708         context->ref_tree = ref_tree;
3622         context->ref_root_bh = ref_root_bh;      3709         context->ref_root_bh = ref_root_bh;
3623         context->cow_object = xv;                3710         context->cow_object = xv;
3624                                                  3711 
3625         context->cow_duplicate_clusters = ocf    3712         context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
3626         /* We need the extra credits for dupl    3713         /* We need the extra credits for duplicate_clusters by jbd. */
3627         context->extra_credits =                 3714         context->extra_credits =
3628                 ocfs2_clusters_to_blocks(inod    3715                 ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
3629         context->get_clusters = ocfs2_xattr_v    3716         context->get_clusters = ocfs2_xattr_value_get_clusters;
3630         context->post_refcount = post;           3717         context->post_refcount = post;
3631                                                  3718 
3632         ocfs2_init_xattr_value_extent_tree(&c    3719         ocfs2_init_xattr_value_extent_tree(&context->data_et,
3633                                            IN    3720                                            INODE_CACHE(inode), vb);
3634                                                  3721 
3635         ret = ocfs2_replace_cow(context);        3722         ret = ocfs2_replace_cow(context);
3636         if (ret)                                 3723         if (ret)
3637                 mlog_errno(ret);                 3724                 mlog_errno(ret);
3638                                                  3725 
3639 out:                                             3726 out:
3640         kfree(context);                          3727         kfree(context);
3641         return ret;                              3728         return ret;
3642 }                                                3729 }
3643                                                  3730 
3644 /*                                               3731 /*
3645  * Insert a new extent into refcount tree and    3732  * Insert a new extent into refcount tree and mark a extent rec
3646  * as refcounted in the dinode tree.             3733  * as refcounted in the dinode tree.
3647  */                                              3734  */
3648 int ocfs2_add_refcount_flag(struct inode *ino    3735 int ocfs2_add_refcount_flag(struct inode *inode,
3649                             struct ocfs2_exte    3736                             struct ocfs2_extent_tree *data_et,
3650                             struct ocfs2_cach    3737                             struct ocfs2_caching_info *ref_ci,
3651                             struct buffer_hea    3738                             struct buffer_head *ref_root_bh,
3652                             u32 cpos, u32 p_c    3739                             u32 cpos, u32 p_cluster, u32 num_clusters,
3653                             struct ocfs2_cach    3740                             struct ocfs2_cached_dealloc_ctxt *dealloc,
3654                             struct ocfs2_post    3741                             struct ocfs2_post_refcount *post)
3655 {                                                3742 {
3656         int ret;                                 3743         int ret;
3657         handle_t *handle;                        3744         handle_t *handle;
3658         int credits = 1, ref_blocks = 0;         3745         int credits = 1, ref_blocks = 0;
3659         struct ocfs2_super *osb = OCFS2_SB(in    3746         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3660         struct ocfs2_alloc_context *meta_ac =    3747         struct ocfs2_alloc_context *meta_ac = NULL;
3661                                                  3748 
3662         /* We need to be able to handle at le << 
3663         ref_blocks = ocfs2_extend_meta_needed << 
3664                                               << 
3665         ret = ocfs2_calc_refcount_meta_credit    3749         ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
3666                                                  3750                                                ref_ci, ref_root_bh,
3667                                                  3751                                                p_cluster, num_clusters,
3668                                                  3752                                                &ref_blocks, &credits);
3669         if (ret) {                               3753         if (ret) {
3670                 mlog_errno(ret);                 3754                 mlog_errno(ret);
3671                 goto out;                        3755                 goto out;
3672         }                                        3756         }
3673                                                  3757 
3674         trace_ocfs2_add_refcount_flag(ref_blo    3758         trace_ocfs2_add_refcount_flag(ref_blocks, credits);
3675                                                  3759 
3676         if (ref_blocks) {                        3760         if (ref_blocks) {
3677                 ret = ocfs2_reserve_new_metad !! 3761                 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
3678                                                  3762                                                         ref_blocks, &meta_ac);
3679                 if (ret) {                       3763                 if (ret) {
3680                         mlog_errno(ret);         3764                         mlog_errno(ret);
3681                         goto out;                3765                         goto out;
3682                 }                                3766                 }
3683         }                                        3767         }
3684                                                  3768 
3685         if (post)                                3769         if (post)
3686                 credits += post->credits;        3770                 credits += post->credits;
3687                                                  3771 
3688         handle = ocfs2_start_trans(osb, credi    3772         handle = ocfs2_start_trans(osb, credits);
3689         if (IS_ERR(handle)) {                    3773         if (IS_ERR(handle)) {
3690                 ret = PTR_ERR(handle);           3774                 ret = PTR_ERR(handle);
3691                 mlog_errno(ret);                 3775                 mlog_errno(ret);
3692                 goto out;                        3776                 goto out;
3693         }                                        3777         }
3694                                                  3778 
3695         ret = ocfs2_mark_extent_refcounted(in    3779         ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3696                                            cp    3780                                            cpos, num_clusters, p_cluster,
3697                                            me    3781                                            meta_ac, dealloc);
3698         if (ret) {                               3782         if (ret) {
3699                 mlog_errno(ret);                 3783                 mlog_errno(ret);
3700                 goto out_commit;                 3784                 goto out_commit;
3701         }                                        3785         }
3702                                                  3786 
3703         ret = __ocfs2_increase_refcount(handl    3787         ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3704                                         p_clu    3788                                         p_cluster, num_clusters, 0,
3705                                         meta_    3789                                         meta_ac, dealloc);
3706         if (ret) {                               3790         if (ret) {
3707                 mlog_errno(ret);                 3791                 mlog_errno(ret);
3708                 goto out_commit;                 3792                 goto out_commit;
3709         }                                        3793         }
3710                                                  3794 
3711         if (post && post->func) {                3795         if (post && post->func) {
3712                 ret = post->func(inode, handl    3796                 ret = post->func(inode, handle, post->para);
3713                 if (ret)                         3797                 if (ret)
3714                         mlog_errno(ret);         3798                         mlog_errno(ret);
3715         }                                        3799         }
3716                                                  3800 
3717 out_commit:                                      3801 out_commit:
3718         ocfs2_commit_trans(osb, handle);         3802         ocfs2_commit_trans(osb, handle);
3719 out:                                             3803 out:
3720         if (meta_ac)                             3804         if (meta_ac)
3721                 ocfs2_free_alloc_context(meta    3805                 ocfs2_free_alloc_context(meta_ac);
3722         return ret;                              3806         return ret;
3723 }                                                3807 }
3724                                                  3808 
3725 static int ocfs2_change_ctime(struct inode *i    3809 static int ocfs2_change_ctime(struct inode *inode,
3726                               struct buffer_h    3810                               struct buffer_head *di_bh)
3727 {                                                3811 {
3728         int ret;                                 3812         int ret;
3729         handle_t *handle;                        3813         handle_t *handle;
3730         struct ocfs2_dinode *di = (struct ocf    3814         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3731                                                  3815 
3732         handle = ocfs2_start_trans(OCFS2_SB(i    3816         handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
3733                                    OCFS2_INOD    3817                                    OCFS2_INODE_UPDATE_CREDITS);
3734         if (IS_ERR(handle)) {                    3818         if (IS_ERR(handle)) {
3735                 ret = PTR_ERR(handle);           3819                 ret = PTR_ERR(handle);
3736                 mlog_errno(ret);                 3820                 mlog_errno(ret);
3737                 goto out;                        3821                 goto out;
3738         }                                        3822         }
3739                                                  3823 
3740         ret = ocfs2_journal_access_di(handle,    3824         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
3741                                       OCFS2_J    3825                                       OCFS2_JOURNAL_ACCESS_WRITE);
3742         if (ret) {                               3826         if (ret) {
3743                 mlog_errno(ret);                 3827                 mlog_errno(ret);
3744                 goto out_commit;                 3828                 goto out_commit;
3745         }                                        3829         }
3746                                                  3830 
3747         inode_set_ctime_current(inode);       !! 3831         inode->i_ctime = CURRENT_TIME;
3748         di->i_ctime = cpu_to_le64(inode_get_c !! 3832         di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3749         di->i_ctime_nsec = cpu_to_le32(inode_ !! 3833         di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3750                                                  3834 
3751         ocfs2_journal_dirty(handle, di_bh);      3835         ocfs2_journal_dirty(handle, di_bh);
3752                                                  3836 
3753 out_commit:                                      3837 out_commit:
3754         ocfs2_commit_trans(OCFS2_SB(inode->i_    3838         ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3755 out:                                             3839 out:
3756         return ret;                              3840         return ret;
3757 }                                                3841 }
3758                                                  3842 
3759 static int ocfs2_attach_refcount_tree(struct     3843 static int ocfs2_attach_refcount_tree(struct inode *inode,
3760                                       struct     3844                                       struct buffer_head *di_bh)
3761 {                                                3845 {
3762         int ret, data_changed = 0;               3846         int ret, data_changed = 0;
3763         struct buffer_head *ref_root_bh = NUL    3847         struct buffer_head *ref_root_bh = NULL;
3764         struct ocfs2_inode_info *oi = OCFS2_I    3848         struct ocfs2_inode_info *oi = OCFS2_I(inode);
3765         struct ocfs2_dinode *di = (struct ocf    3849         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3766         struct ocfs2_super *osb = OCFS2_SB(in    3850         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3767         struct ocfs2_refcount_tree *ref_tree;    3851         struct ocfs2_refcount_tree *ref_tree;
3768         unsigned int ext_flags;                  3852         unsigned int ext_flags;
3769         loff_t size;                             3853         loff_t size;
3770         u32 cpos, num_clusters, clusters, p_c    3854         u32 cpos, num_clusters, clusters, p_cluster;
3771         struct ocfs2_cached_dealloc_ctxt deal    3855         struct ocfs2_cached_dealloc_ctxt dealloc;
3772         struct ocfs2_extent_tree di_et;          3856         struct ocfs2_extent_tree di_et;
3773                                                  3857 
3774         ocfs2_init_dealloc_ctxt(&dealloc);       3858         ocfs2_init_dealloc_ctxt(&dealloc);
3775                                                  3859 
3776         if (!ocfs2_is_refcount_inode(inode))  !! 3860         if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) {
3777                 ret = ocfs2_create_refcount_t    3861                 ret = ocfs2_create_refcount_tree(inode, di_bh);
3778                 if (ret) {                       3862                 if (ret) {
3779                         mlog_errno(ret);         3863                         mlog_errno(ret);
3780                         goto out;                3864                         goto out;
3781                 }                                3865                 }
3782         }                                        3866         }
3783                                                  3867 
3784         BUG_ON(!di->i_refcount_loc);             3868         BUG_ON(!di->i_refcount_loc);
3785         ret = ocfs2_lock_refcount_tree(osb,      3869         ret = ocfs2_lock_refcount_tree(osb,
3786                                        le64_t    3870                                        le64_to_cpu(di->i_refcount_loc), 1,
3787                                        &ref_t    3871                                        &ref_tree, &ref_root_bh);
3788         if (ret) {                               3872         if (ret) {
3789                 mlog_errno(ret);                 3873                 mlog_errno(ret);
3790                 goto out;                        3874                 goto out;
3791         }                                        3875         }
3792                                                  3876 
3793         if (oi->ip_dyn_features & OCFS2_INLIN    3877         if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
3794                 goto attach_xattr;               3878                 goto attach_xattr;
3795                                                  3879 
3796         ocfs2_init_dinode_extent_tree(&di_et,    3880         ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
3797                                                  3881 
3798         size = i_size_read(inode);               3882         size = i_size_read(inode);
3799         clusters = ocfs2_clusters_for_bytes(i    3883         clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
3800                                                  3884 
3801         cpos = 0;                                3885         cpos = 0;
3802         while (cpos < clusters) {                3886         while (cpos < clusters) {
3803                 ret = ocfs2_get_clusters(inod    3887                 ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3804                                          &num    3888                                          &num_clusters, &ext_flags);
3805                 if (ret) {                    !! 3889 
3806                         mlog_errno(ret);      << 
3807                         goto unlock;          << 
3808                 }                             << 
3809                 if (p_cluster && !(ext_flags     3890                 if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
3810                         ret = ocfs2_add_refco    3891                         ret = ocfs2_add_refcount_flag(inode, &di_et,
3811                                                  3892                                                       &ref_tree->rf_ci,
3812                                                  3893                                                       ref_root_bh, cpos,
3813                                                  3894                                                       p_cluster, num_clusters,
3814                                                  3895                                                       &dealloc, NULL);
3815                         if (ret) {               3896                         if (ret) {
3816                                 mlog_errno(re    3897                                 mlog_errno(ret);
3817                                 goto unlock;     3898                                 goto unlock;
3818                         }                        3899                         }
3819                                                  3900 
3820                         data_changed = 1;        3901                         data_changed = 1;
3821                 }                                3902                 }
3822                 cpos += num_clusters;            3903                 cpos += num_clusters;
3823         }                                        3904         }
3824                                                  3905 
3825 attach_xattr:                                    3906 attach_xattr:
3826         if (oi->ip_dyn_features & OCFS2_HAS_X    3907         if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
3827                 ret = ocfs2_xattr_attach_refc    3908                 ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
3828                                                  3909                                                        &ref_tree->rf_ci,
3829                                                  3910                                                        ref_root_bh,
3830                                                  3911                                                        &dealloc);
3831                 if (ret) {                       3912                 if (ret) {
3832                         mlog_errno(ret);         3913                         mlog_errno(ret);
3833                         goto unlock;             3914                         goto unlock;
3834                 }                                3915                 }
3835         }                                        3916         }
3836                                                  3917 
3837         if (data_changed) {                      3918         if (data_changed) {
3838                 ret = ocfs2_change_ctime(inod    3919                 ret = ocfs2_change_ctime(inode, di_bh);
3839                 if (ret)                         3920                 if (ret)
3840                         mlog_errno(ret);         3921                         mlog_errno(ret);
3841         }                                        3922         }
3842                                                  3923 
3843 unlock:                                          3924 unlock:
3844         ocfs2_unlock_refcount_tree(osb, ref_t    3925         ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3845         brelse(ref_root_bh);                     3926         brelse(ref_root_bh);
3846                                                  3927 
3847         if (!ret && ocfs2_dealloc_has_cluster    3928         if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3848                 ocfs2_schedule_truncate_log_f    3929                 ocfs2_schedule_truncate_log_flush(osb, 1);
3849                 ocfs2_run_deallocs(osb, &deal    3930                 ocfs2_run_deallocs(osb, &dealloc);
3850         }                                        3931         }
3851 out:                                             3932 out:
3852         /*                                       3933         /*
3853          * Empty the extent map so that we ma    3934          * Empty the extent map so that we may get the right extent
3854          * record from the disk.                 3935          * record from the disk.
3855          */                                      3936          */
3856         ocfs2_extent_map_trunc(inode, 0);        3937         ocfs2_extent_map_trunc(inode, 0);
3857                                                  3938 
3858         return ret;                              3939         return ret;
3859 }                                                3940 }
3860                                                  3941 
3861 static int ocfs2_add_refcounted_extent(struct    3942 static int ocfs2_add_refcounted_extent(struct inode *inode,
3862                                    struct ocf    3943                                    struct ocfs2_extent_tree *et,
3863                                    struct ocf    3944                                    struct ocfs2_caching_info *ref_ci,
3864                                    struct buf    3945                                    struct buffer_head *ref_root_bh,
3865                                    u32 cpos,     3946                                    u32 cpos, u32 p_cluster, u32 num_clusters,
3866                                    unsigned i    3947                                    unsigned int ext_flags,
3867                                    struct ocf    3948                                    struct ocfs2_cached_dealloc_ctxt *dealloc)
3868 {                                                3949 {
3869         int ret;                                 3950         int ret;
3870         handle_t *handle;                        3951         handle_t *handle;
3871         int credits = 0;                         3952         int credits = 0;
3872         struct ocfs2_super *osb = OCFS2_SB(in    3953         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3873         struct ocfs2_alloc_context *meta_ac =    3954         struct ocfs2_alloc_context *meta_ac = NULL;
3874                                                  3955 
3875         ret = ocfs2_lock_refcount_allocators(    3956         ret = ocfs2_lock_refcount_allocators(inode->i_sb,
3876                                                  3957                                              p_cluster, num_clusters,
3877                                                  3958                                              et, ref_ci,
3878                                                  3959                                              ref_root_bh, &meta_ac,
3879                                                  3960                                              NULL, &credits);
3880         if (ret) {                               3961         if (ret) {
3881                 mlog_errno(ret);                 3962                 mlog_errno(ret);
3882                 goto out;                        3963                 goto out;
3883         }                                        3964         }
3884                                                  3965 
3885         handle = ocfs2_start_trans(osb, credi    3966         handle = ocfs2_start_trans(osb, credits);
3886         if (IS_ERR(handle)) {                    3967         if (IS_ERR(handle)) {
3887                 ret = PTR_ERR(handle);           3968                 ret = PTR_ERR(handle);
3888                 mlog_errno(ret);                 3969                 mlog_errno(ret);
3889                 goto out;                        3970                 goto out;
3890         }                                        3971         }
3891                                                  3972 
3892         ret = ocfs2_insert_extent(handle, et,    3973         ret = ocfs2_insert_extent(handle, et, cpos,
3893                         ocfs2_clusters_to_blo    3974                         ocfs2_clusters_to_blocks(inode->i_sb, p_cluster),
3894                         num_clusters, ext_fla    3975                         num_clusters, ext_flags, meta_ac);
3895         if (ret) {                               3976         if (ret) {
3896                 mlog_errno(ret);                 3977                 mlog_errno(ret);
3897                 goto out_commit;                 3978                 goto out_commit;
3898         }                                        3979         }
3899                                                  3980 
3900         ret = ocfs2_increase_refcount(handle,    3981         ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3901                                       p_clust    3982                                       p_cluster, num_clusters,
3902                                       meta_ac    3983                                       meta_ac, dealloc);
3903         if (ret) {                            << 
3904                 mlog_errno(ret);              << 
3905                 goto out_commit;              << 
3906         }                                     << 
3907                                               << 
3908         ret = dquot_alloc_space_nodirty(inode << 
3909                 ocfs2_clusters_to_bytes(osb-> << 
3910         if (ret)                                 3984         if (ret)
3911                 mlog_errno(ret);                 3985                 mlog_errno(ret);
3912                                                  3986 
3913 out_commit:                                      3987 out_commit:
3914         ocfs2_commit_trans(osb, handle);         3988         ocfs2_commit_trans(osb, handle);
3915 out:                                             3989 out:
3916         if (meta_ac)                             3990         if (meta_ac)
3917                 ocfs2_free_alloc_context(meta    3991                 ocfs2_free_alloc_context(meta_ac);
3918         return ret;                              3992         return ret;
3919 }                                                3993 }
3920                                                  3994 
3921 static int ocfs2_duplicate_inline_data(struct    3995 static int ocfs2_duplicate_inline_data(struct inode *s_inode,
3922                                        struct    3996                                        struct buffer_head *s_bh,
3923                                        struct    3997                                        struct inode *t_inode,
3924                                        struct    3998                                        struct buffer_head *t_bh)
3925 {                                                3999 {
3926         int ret;                                 4000         int ret;
3927         handle_t *handle;                        4001         handle_t *handle;
3928         struct ocfs2_super *osb = OCFS2_SB(s_    4002         struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
3929         struct ocfs2_dinode *s_di = (struct o    4003         struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
3930         struct ocfs2_dinode *t_di = (struct o    4004         struct ocfs2_dinode *t_di = (struct ocfs2_dinode *)t_bh->b_data;
3931                                                  4005 
3932         BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_fea    4006         BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
3933                                                  4007 
3934         handle = ocfs2_start_trans(osb, OCFS2    4008         handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
3935         if (IS_ERR(handle)) {                    4009         if (IS_ERR(handle)) {
3936                 ret = PTR_ERR(handle);           4010                 ret = PTR_ERR(handle);
3937                 mlog_errno(ret);                 4011                 mlog_errno(ret);
3938                 goto out;                        4012                 goto out;
3939         }                                        4013         }
3940                                                  4014 
3941         ret = ocfs2_journal_access_di(handle,    4015         ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
3942                                       OCFS2_J    4016                                       OCFS2_JOURNAL_ACCESS_WRITE);
3943         if (ret) {                               4017         if (ret) {
3944                 mlog_errno(ret);                 4018                 mlog_errno(ret);
3945                 goto out_commit;                 4019                 goto out_commit;
3946         }                                        4020         }
3947                                                  4021 
3948         t_di->id2.i_data.id_count = s_di->id2    4022         t_di->id2.i_data.id_count = s_di->id2.i_data.id_count;
3949         memcpy(t_di->id2.i_data.id_data, s_di    4023         memcpy(t_di->id2.i_data.id_data, s_di->id2.i_data.id_data,
3950                le16_to_cpu(s_di->id2.i_data.i    4024                le16_to_cpu(s_di->id2.i_data.id_count));
3951         spin_lock(&OCFS2_I(t_inode)->ip_lock)    4025         spin_lock(&OCFS2_I(t_inode)->ip_lock);
3952         OCFS2_I(t_inode)->ip_dyn_features |=     4026         OCFS2_I(t_inode)->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
3953         t_di->i_dyn_features = cpu_to_le16(OC    4027         t_di->i_dyn_features = cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features);
3954         spin_unlock(&OCFS2_I(t_inode)->ip_loc    4028         spin_unlock(&OCFS2_I(t_inode)->ip_lock);
3955                                                  4029 
3956         ocfs2_journal_dirty(handle, t_bh);       4030         ocfs2_journal_dirty(handle, t_bh);
3957                                                  4031 
3958 out_commit:                                      4032 out_commit:
3959         ocfs2_commit_trans(osb, handle);         4033         ocfs2_commit_trans(osb, handle);
3960 out:                                             4034 out:
3961         return ret;                              4035         return ret;
3962 }                                                4036 }
3963                                                  4037 
3964 static int ocfs2_duplicate_extent_list(struct    4038 static int ocfs2_duplicate_extent_list(struct inode *s_inode,
3965                                 struct inode     4039                                 struct inode *t_inode,
3966                                 struct buffer    4040                                 struct buffer_head *t_bh,
3967                                 struct ocfs2_    4041                                 struct ocfs2_caching_info *ref_ci,
3968                                 struct buffer    4042                                 struct buffer_head *ref_root_bh,
3969                                 struct ocfs2_    4043                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
3970 {                                                4044 {
3971         int ret = 0;                             4045         int ret = 0;
3972         u32 p_cluster, num_clusters, clusters    4046         u32 p_cluster, num_clusters, clusters, cpos;
3973         loff_t size;                             4047         loff_t size;
3974         unsigned int ext_flags;                  4048         unsigned int ext_flags;
3975         struct ocfs2_extent_tree et;             4049         struct ocfs2_extent_tree et;
3976                                                  4050 
3977         ocfs2_init_dinode_extent_tree(&et, IN    4051         ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
3978                                                  4052 
3979         size = i_size_read(s_inode);             4053         size = i_size_read(s_inode);
3980         clusters = ocfs2_clusters_for_bytes(s    4054         clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
3981                                                  4055 
3982         cpos = 0;                                4056         cpos = 0;
3983         while (cpos < clusters) {                4057         while (cpos < clusters) {
3984                 ret = ocfs2_get_clusters(s_in    4058                 ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
3985                                          &num    4059                                          &num_clusters, &ext_flags);
3986                 if (ret) {                    !! 4060 
3987                         mlog_errno(ret);      << 
3988                         goto out;             << 
3989                 }                             << 
3990                 if (p_cluster) {                 4061                 if (p_cluster) {
3991                         ret = ocfs2_add_refco    4062                         ret = ocfs2_add_refcounted_extent(t_inode, &et,
3992                                                  4063                                                           ref_ci, ref_root_bh,
3993                                                  4064                                                           cpos, p_cluster,
3994                                                  4065                                                           num_clusters,
3995                                                  4066                                                           ext_flags,
3996                                                  4067                                                           dealloc);
3997                         if (ret) {               4068                         if (ret) {
3998                                 mlog_errno(re    4069                                 mlog_errno(ret);
3999                                 goto out;        4070                                 goto out;
4000                         }                        4071                         }
4001                 }                                4072                 }
4002                                                  4073 
4003                 cpos += num_clusters;            4074                 cpos += num_clusters;
4004         }                                        4075         }
4005                                                  4076 
4006 out:                                             4077 out:
4007         return ret;                              4078         return ret;
4008 }                                                4079 }
4009                                                  4080 
4010 /*                                               4081 /*
4011  * change the new file's attributes to the sr    4082  * change the new file's attributes to the src.
4012  *                                               4083  *
4013  * reflink creates a snapshot of a file, that    4084  * reflink creates a snapshot of a file, that means the attributes
4014  * must be identical except for three excepti    4085  * must be identical except for three exceptions - nlink, ino, and ctime.
4015  */                                              4086  */
4016 static int ocfs2_complete_reflink(struct inod    4087 static int ocfs2_complete_reflink(struct inode *s_inode,
4017                                   struct buff    4088                                   struct buffer_head *s_bh,
4018                                   struct inod    4089                                   struct inode *t_inode,
4019                                   struct buff    4090                                   struct buffer_head *t_bh,
4020                                   bool preser    4091                                   bool preserve)
4021 {                                                4092 {
4022         int ret;                                 4093         int ret;
4023         handle_t *handle;                        4094         handle_t *handle;
4024         struct ocfs2_dinode *s_di = (struct o    4095         struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
4025         struct ocfs2_dinode *di = (struct ocf    4096         struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
4026         loff_t size = i_size_read(s_inode);      4097         loff_t size = i_size_read(s_inode);
4027                                                  4098 
4028         handle = ocfs2_start_trans(OCFS2_SB(t    4099         handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
4029                                    OCFS2_INOD    4100                                    OCFS2_INODE_UPDATE_CREDITS);
4030         if (IS_ERR(handle)) {                    4101         if (IS_ERR(handle)) {
4031                 ret = PTR_ERR(handle);           4102                 ret = PTR_ERR(handle);
4032                 mlog_errno(ret);                 4103                 mlog_errno(ret);
4033                 return ret;                      4104                 return ret;
4034         }                                        4105         }
4035                                                  4106 
4036         ret = ocfs2_journal_access_di(handle,    4107         ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
4037                                       OCFS2_J    4108                                       OCFS2_JOURNAL_ACCESS_WRITE);
4038         if (ret) {                               4109         if (ret) {
4039                 mlog_errno(ret);                 4110                 mlog_errno(ret);
4040                 goto out_commit;                 4111                 goto out_commit;
4041         }                                        4112         }
4042                                                  4113 
4043         spin_lock(&OCFS2_I(t_inode)->ip_lock)    4114         spin_lock(&OCFS2_I(t_inode)->ip_lock);
4044         OCFS2_I(t_inode)->ip_clusters = OCFS2    4115         OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
4045         OCFS2_I(t_inode)->ip_attr = OCFS2_I(s    4116         OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
4046         OCFS2_I(t_inode)->ip_dyn_features = O    4117         OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
4047         spin_unlock(&OCFS2_I(t_inode)->ip_loc    4118         spin_unlock(&OCFS2_I(t_inode)->ip_lock);
4048         i_size_write(t_inode, size);             4119         i_size_write(t_inode, size);
4049         t_inode->i_blocks = s_inode->i_blocks    4120         t_inode->i_blocks = s_inode->i_blocks;
4050                                                  4121 
4051         di->i_xattr_inline_size = s_di->i_xat    4122         di->i_xattr_inline_size = s_di->i_xattr_inline_size;
4052         di->i_clusters = s_di->i_clusters;       4123         di->i_clusters = s_di->i_clusters;
4053         di->i_size = s_di->i_size;               4124         di->i_size = s_di->i_size;
4054         di->i_dyn_features = s_di->i_dyn_feat    4125         di->i_dyn_features = s_di->i_dyn_features;
4055         di->i_attr = s_di->i_attr;               4126         di->i_attr = s_di->i_attr;
4056                                                  4127 
4057         if (preserve) {                          4128         if (preserve) {
4058                 t_inode->i_uid = s_inode->i_u    4129                 t_inode->i_uid = s_inode->i_uid;
4059                 t_inode->i_gid = s_inode->i_g    4130                 t_inode->i_gid = s_inode->i_gid;
4060                 t_inode->i_mode = s_inode->i_    4131                 t_inode->i_mode = s_inode->i_mode;
4061                 di->i_uid = s_di->i_uid;         4132                 di->i_uid = s_di->i_uid;
4062                 di->i_gid = s_di->i_gid;         4133                 di->i_gid = s_di->i_gid;
4063                 di->i_mode = s_di->i_mode;       4134                 di->i_mode = s_di->i_mode;
4064                                                  4135 
4065                 /*                               4136                 /*
4066                  * update time.                  4137                  * update time.
4067                  * we want mtime to appear id    4138                  * we want mtime to appear identical to the source and
4068                  * update ctime.                 4139                  * update ctime.
4069                  */                              4140                  */
4070                 inode_set_ctime_current(t_ino !! 4141                 t_inode->i_ctime = CURRENT_TIME;
4071                                                  4142 
4072                 di->i_ctime = cpu_to_le64(ino !! 4143                 di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec);
4073                 di->i_ctime_nsec = cpu_to_le3 !! 4144                 di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec);
4074                                                  4145 
4075                 inode_set_mtime_to_ts(t_inode !! 4146                 t_inode->i_mtime = s_inode->i_mtime;
4076                 di->i_mtime = s_di->i_mtime;     4147                 di->i_mtime = s_di->i_mtime;
4077                 di->i_mtime_nsec = s_di->i_mt    4148                 di->i_mtime_nsec = s_di->i_mtime_nsec;
4078         }                                        4149         }
4079                                                  4150 
4080         ocfs2_journal_dirty(handle, t_bh);       4151         ocfs2_journal_dirty(handle, t_bh);
4081                                                  4152 
4082 out_commit:                                      4153 out_commit:
4083         ocfs2_commit_trans(OCFS2_SB(t_inode->    4154         ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
4084         return ret;                              4155         return ret;
4085 }                                                4156 }
4086                                                  4157 
4087 static int ocfs2_create_reflink_node(struct i    4158 static int ocfs2_create_reflink_node(struct inode *s_inode,
4088                                      struct b    4159                                      struct buffer_head *s_bh,
4089                                      struct i    4160                                      struct inode *t_inode,
4090                                      struct b    4161                                      struct buffer_head *t_bh,
4091                                      bool pre    4162                                      bool preserve)
4092 {                                                4163 {
4093         int ret;                                 4164         int ret;
4094         struct buffer_head *ref_root_bh = NUL    4165         struct buffer_head *ref_root_bh = NULL;
4095         struct ocfs2_cached_dealloc_ctxt deal    4166         struct ocfs2_cached_dealloc_ctxt dealloc;
4096         struct ocfs2_super *osb = OCFS2_SB(s_    4167         struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
                                                   >> 4168         struct ocfs2_refcount_block *rb;
4097         struct ocfs2_dinode *di = (struct ocf    4169         struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
4098         struct ocfs2_refcount_tree *ref_tree;    4170         struct ocfs2_refcount_tree *ref_tree;
4099                                                  4171 
4100         ocfs2_init_dealloc_ctxt(&dealloc);       4172         ocfs2_init_dealloc_ctxt(&dealloc);
4101                                                  4173 
4102         ret = ocfs2_set_refcount_tree(t_inode    4174         ret = ocfs2_set_refcount_tree(t_inode, t_bh,
4103                                       le64_to    4175                                       le64_to_cpu(di->i_refcount_loc));
4104         if (ret) {                               4176         if (ret) {
4105                 mlog_errno(ret);                 4177                 mlog_errno(ret);
4106                 goto out;                        4178                 goto out;
4107         }                                        4179         }
4108                                                  4180 
4109         if (OCFS2_I(s_inode)->ip_dyn_features    4181         if (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4110                 ret = ocfs2_duplicate_inline_    4182                 ret = ocfs2_duplicate_inline_data(s_inode, s_bh,
4111                                                  4183                                                   t_inode, t_bh);
4112                 if (ret)                         4184                 if (ret)
4113                         mlog_errno(ret);         4185                         mlog_errno(ret);
4114                 goto out;                        4186                 goto out;
4115         }                                        4187         }
4116                                                  4188 
4117         ret = ocfs2_lock_refcount_tree(osb, l    4189         ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
4118                                        1, &re    4190                                        1, &ref_tree, &ref_root_bh);
4119         if (ret) {                               4191         if (ret) {
4120                 mlog_errno(ret);                 4192                 mlog_errno(ret);
4121                 goto out;                        4193                 goto out;
4122         }                                        4194         }
                                                   >> 4195         rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
4123                                                  4196 
4124         ret = ocfs2_duplicate_extent_list(s_i    4197         ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
4125                                           &re    4198                                           &ref_tree->rf_ci, ref_root_bh,
4126                                           &de    4199                                           &dealloc);
4127         if (ret) {                               4200         if (ret) {
4128                 mlog_errno(ret);                 4201                 mlog_errno(ret);
4129                 goto out_unlock_refcount;        4202                 goto out_unlock_refcount;
4130         }                                        4203         }
4131                                                  4204 
4132 out_unlock_refcount:                             4205 out_unlock_refcount:
4133         ocfs2_unlock_refcount_tree(osb, ref_t    4206         ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
4134         brelse(ref_root_bh);                     4207         brelse(ref_root_bh);
4135 out:                                             4208 out:
4136         if (ocfs2_dealloc_has_cluster(&deallo    4209         if (ocfs2_dealloc_has_cluster(&dealloc)) {
4137                 ocfs2_schedule_truncate_log_f    4210                 ocfs2_schedule_truncate_log_flush(osb, 1);
4138                 ocfs2_run_deallocs(osb, &deal    4211                 ocfs2_run_deallocs(osb, &dealloc);
4139         }                                        4212         }
4140                                                  4213 
4141         return ret;                              4214         return ret;
4142 }                                                4215 }
4143                                                  4216 
4144 static int __ocfs2_reflink(struct dentry *old    4217 static int __ocfs2_reflink(struct dentry *old_dentry,
4145                            struct buffer_head    4218                            struct buffer_head *old_bh,
4146                            struct inode *new_    4219                            struct inode *new_inode,
4147                            bool preserve)        4220                            bool preserve)
4148 {                                                4221 {
4149         int ret;                                 4222         int ret;
4150         struct inode *inode = d_inode(old_den !! 4223         struct inode *inode = old_dentry->d_inode;
4151         struct buffer_head *new_bh = NULL;       4224         struct buffer_head *new_bh = NULL;
4152         struct ocfs2_inode_info *oi = OCFS2_I << 
4153                                                  4225 
4154         if (oi->ip_flags & OCFS2_INODE_SYSTEM !! 4226         if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
4155                 ret = -EINVAL;                   4227                 ret = -EINVAL;
4156                 mlog_errno(ret);                 4228                 mlog_errno(ret);
4157                 goto out;                        4229                 goto out;
4158         }                                        4230         }
4159                                                  4231 
4160         ret = filemap_fdatawrite(inode->i_map    4232         ret = filemap_fdatawrite(inode->i_mapping);
4161         if (ret) {                               4233         if (ret) {
4162                 mlog_errno(ret);                 4234                 mlog_errno(ret);
4163                 goto out;                        4235                 goto out;
4164         }                                        4236         }
4165                                                  4237 
4166         ret = ocfs2_attach_refcount_tree(inod    4238         ret = ocfs2_attach_refcount_tree(inode, old_bh);
4167         if (ret) {                               4239         if (ret) {
4168                 mlog_errno(ret);                 4240                 mlog_errno(ret);
4169                 goto out;                        4241                 goto out;
4170         }                                        4242         }
4171                                                  4243 
4172         inode_lock_nested(new_inode, I_MUTEX_ !! 4244         mutex_lock_nested(&new_inode->i_mutex, I_MUTEX_CHILD);
4173         ret = ocfs2_inode_lock_nested(new_ino    4245         ret = ocfs2_inode_lock_nested(new_inode, &new_bh, 1,
4174                                       OI_LS_R    4246                                       OI_LS_REFLINK_TARGET);
4175         if (ret) {                               4247         if (ret) {
4176                 mlog_errno(ret);                 4248                 mlog_errno(ret);
4177                 goto out_unlock;                 4249                 goto out_unlock;
4178         }                                        4250         }
4179                                                  4251 
4180         if ((oi->ip_dyn_features & OCFS2_HAS_ << 
4181             (oi->ip_dyn_features & OCFS2_INLI << 
4182                 /*                            << 
4183                  * Adjust extent record count << 
4184                  * Inline data count had been << 
4185                  */                           << 
4186                 struct ocfs2_inode_info *new_ << 
4187                                               << 
4188                 if (!(new_oi->ip_dyn_features << 
4189                     !(ocfs2_inode_is_fast_sym << 
4190                         struct ocfs2_dinode * << 
4191                         struct ocfs2_dinode * << 
4192                         struct ocfs2_extent_l << 
4193                         int inline_size = le1 << 
4194                                               << 
4195                         le16_add_cpu(&el->l_c << 
4196                                         sizeo << 
4197                 }                             << 
4198         }                                     << 
4199                                               << 
4200         ret = ocfs2_create_reflink_node(inode    4252         ret = ocfs2_create_reflink_node(inode, old_bh,
4201                                         new_i    4253                                         new_inode, new_bh, preserve);
4202         if (ret) {                               4254         if (ret) {
4203                 mlog_errno(ret);                 4255                 mlog_errno(ret);
4204                 goto inode_unlock;               4256                 goto inode_unlock;
4205         }                                        4257         }
4206                                                  4258 
4207         if (oi->ip_dyn_features & OCFS2_HAS_X !! 4259         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
4208                 ret = ocfs2_reflink_xattrs(in    4260                 ret = ocfs2_reflink_xattrs(inode, old_bh,
4209                                            ne    4261                                            new_inode, new_bh,
4210                                            pr    4262                                            preserve);
4211                 if (ret) {                       4263                 if (ret) {
4212                         mlog_errno(ret);         4264                         mlog_errno(ret);
4213                         goto inode_unlock;       4265                         goto inode_unlock;
4214                 }                                4266                 }
4215         }                                        4267         }
4216                                                  4268 
4217         ret = ocfs2_complete_reflink(inode, o    4269         ret = ocfs2_complete_reflink(inode, old_bh,
4218                                      new_inod    4270                                      new_inode, new_bh, preserve);
4219         if (ret)                                 4271         if (ret)
4220                 mlog_errno(ret);                 4272                 mlog_errno(ret);
4221                                                  4273 
4222 inode_unlock:                                    4274 inode_unlock:
4223         ocfs2_inode_unlock(new_inode, 1);        4275         ocfs2_inode_unlock(new_inode, 1);
4224         brelse(new_bh);                          4276         brelse(new_bh);
4225 out_unlock:                                      4277 out_unlock:
4226         inode_unlock(new_inode);              !! 4278         mutex_unlock(&new_inode->i_mutex);
4227 out:                                             4279 out:
4228         if (!ret) {                              4280         if (!ret) {
4229                 ret = filemap_fdatawait(inode    4281                 ret = filemap_fdatawait(inode->i_mapping);
4230                 if (ret)                         4282                 if (ret)
4231                         mlog_errno(ret);         4283                         mlog_errno(ret);
4232         }                                        4284         }
4233         return ret;                              4285         return ret;
4234 }                                                4286 }
4235                                                  4287 
4236 static int ocfs2_reflink(struct dentry *old_d    4288 static int ocfs2_reflink(struct dentry *old_dentry, struct inode *dir,
4237                          struct dentry *new_d    4289                          struct dentry *new_dentry, bool preserve)
4238 {                                                4290 {
4239         int error, had_lock;                  !! 4291         int error;
4240         struct inode *inode = d_inode(old_den !! 4292         struct inode *inode = old_dentry->d_inode;
4241         struct buffer_head *old_bh = NULL;       4293         struct buffer_head *old_bh = NULL;
4242         struct inode *new_orphan_inode = NULL    4294         struct inode *new_orphan_inode = NULL;
4243         struct ocfs2_lock_holder oh;          << 
4244                                                  4295 
4245         if (!ocfs2_refcount_tree(OCFS2_SB(ino    4296         if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
4246                 return -EOPNOTSUPP;              4297                 return -EOPNOTSUPP;
4247                                                  4298 
4248                                               << 
4249         error = ocfs2_create_inode_in_orphan(    4299         error = ocfs2_create_inode_in_orphan(dir, inode->i_mode,
4250                                                  4300                                              &new_orphan_inode);
4251         if (error) {                             4301         if (error) {
4252                 mlog_errno(error);               4302                 mlog_errno(error);
4253                 goto out;                        4303                 goto out;
4254         }                                        4304         }
4255                                                  4305 
4256         error = ocfs2_rw_lock(inode, 1);      << 
4257         if (error) {                          << 
4258                 mlog_errno(error);            << 
4259                 goto out;                     << 
4260         }                                     << 
4261                                               << 
4262         error = ocfs2_inode_lock(inode, &old_    4306         error = ocfs2_inode_lock(inode, &old_bh, 1);
4263         if (error) {                             4307         if (error) {
4264                 mlog_errno(error);               4308                 mlog_errno(error);
4265                 ocfs2_rw_unlock(inode, 1);    << 
4266                 goto out;                        4309                 goto out;
4267         }                                        4310         }
4268                                                  4311 
4269         down_write(&OCFS2_I(inode)->ip_xattr_    4312         down_write(&OCFS2_I(inode)->ip_xattr_sem);
4270         down_write(&OCFS2_I(inode)->ip_alloc_    4313         down_write(&OCFS2_I(inode)->ip_alloc_sem);
4271         error = __ocfs2_reflink(old_dentry, o    4314         error = __ocfs2_reflink(old_dentry, old_bh,
4272                                 new_orphan_in    4315                                 new_orphan_inode, preserve);
4273         up_write(&OCFS2_I(inode)->ip_alloc_se    4316         up_write(&OCFS2_I(inode)->ip_alloc_sem);
4274         up_write(&OCFS2_I(inode)->ip_xattr_se    4317         up_write(&OCFS2_I(inode)->ip_xattr_sem);
4275                                                  4318 
4276         ocfs2_inode_unlock(inode, 1);            4319         ocfs2_inode_unlock(inode, 1);
4277         ocfs2_rw_unlock(inode, 1);            << 
4278         brelse(old_bh);                          4320         brelse(old_bh);
4279                                                  4321 
4280         if (error) {                             4322         if (error) {
4281                 mlog_errno(error);               4323                 mlog_errno(error);
4282                 goto out;                        4324                 goto out;
4283         }                                        4325         }
4284                                                  4326 
4285         had_lock = ocfs2_inode_lock_tracker(n << 
4286                                             & << 
4287         if (had_lock < 0) {                   << 
4288                 error = had_lock;             << 
4289                 mlog_errno(error);            << 
4290                 goto out;                     << 
4291         }                                     << 
4292                                               << 
4293         /* If the security isn't preserved, w    4327         /* If the security isn't preserved, we need to re-initialize them. */
4294         if (!preserve) {                         4328         if (!preserve) {
4295                 error = ocfs2_init_security_a    4329                 error = ocfs2_init_security_and_acl(dir, new_orphan_inode,
4296                                                  4330                                                     &new_dentry->d_name);
4297                 if (error)                       4331                 if (error)
4298                         mlog_errno(error);       4332                         mlog_errno(error);
4299         }                                        4333         }
                                                   >> 4334 out:
4300         if (!error) {                            4335         if (!error) {
4301                 error = ocfs2_mv_orphaned_ino    4336                 error = ocfs2_mv_orphaned_inode_to_new(dir, new_orphan_inode,
4302                                                  4337                                                        new_dentry);
4303                 if (error)                       4338                 if (error)
4304                         mlog_errno(error);       4339                         mlog_errno(error);
4305         }                                        4340         }
4306         ocfs2_inode_unlock_tracker(new_orphan << 
4307                                                  4341 
4308 out:                                          << 
4309         if (new_orphan_inode) {                  4342         if (new_orphan_inode) {
4310                 /*                               4343                 /*
4311                  * We need to open_unlock the    4344                  * We need to open_unlock the inode no matter whether we
4312                  * succeed or not, so that ot    4345                  * succeed or not, so that other nodes can delete it later.
4313                  */                              4346                  */
4314                 ocfs2_open_unlock(new_orphan_    4347                 ocfs2_open_unlock(new_orphan_inode);
4315                 if (error)                       4348                 if (error)
4316                         iput(new_orphan_inode    4349                         iput(new_orphan_inode);
4317         }                                        4350         }
4318                                                  4351 
4319         return error;                            4352         return error;
4320 }                                                4353 }
4321                                                  4354 
4322 /*                                               4355 /*
4323  * Below here are the bits used by OCFS2_IOC_    4356  * Below here are the bits used by OCFS2_IOC_REFLINK() to fake
4324  * sys_reflink().  This will go away when vfs    4357  * sys_reflink().  This will go away when vfs_reflink() exists in
4325  * fs/namei.c.                                   4358  * fs/namei.c.
4326  */                                              4359  */
4327                                                  4360 
4328 /* copied from may_create in VFS. */             4361 /* copied from may_create in VFS. */
4329 static inline int ocfs2_may_create(struct ino    4362 static inline int ocfs2_may_create(struct inode *dir, struct dentry *child)
4330 {                                                4363 {
4331         if (d_really_is_positive(child))      !! 4364         if (child->d_inode)
4332                 return -EEXIST;                  4365                 return -EEXIST;
4333         if (IS_DEADDIR(dir))                     4366         if (IS_DEADDIR(dir))
4334                 return -ENOENT;                  4367                 return -ENOENT;
4335         return inode_permission(&nop_mnt_idma !! 4368         return inode_permission(dir, MAY_WRITE | MAY_EXEC);
4336 }                                                4369 }
4337                                                  4370 
4338 /**                                              4371 /**
4339  * ocfs2_vfs_reflink - Create a reference-cou    4372  * ocfs2_vfs_reflink - Create a reference-counted link
4340  *                                               4373  *
4341  * @old_dentry:        source dentry + inode     4374  * @old_dentry:        source dentry + inode
4342  * @dir:       directory to create the target    4375  * @dir:       directory to create the target
4343  * @new_dentry:        target dentry             4376  * @new_dentry:        target dentry
4344  * @preserve:  if true, preserve all file att    4377  * @preserve:  if true, preserve all file attributes
4345  */                                              4378  */
4346 static int ocfs2_vfs_reflink(struct dentry *o    4379 static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir,
4347                              struct dentry *n    4380                              struct dentry *new_dentry, bool preserve)
4348 {                                                4381 {
4349         struct inode *inode = d_inode(old_den !! 4382         struct inode *inode = old_dentry->d_inode;
4350         int error;                               4383         int error;
4351                                                  4384 
4352         if (!inode)                              4385         if (!inode)
4353                 return -ENOENT;                  4386                 return -ENOENT;
4354                                                  4387 
4355         error = ocfs2_may_create(dir, new_den    4388         error = ocfs2_may_create(dir, new_dentry);
4356         if (error)                               4389         if (error)
4357                 return error;                    4390                 return error;
4358                                                  4391 
4359         if (dir->i_sb != inode->i_sb)            4392         if (dir->i_sb != inode->i_sb)
4360                 return -EXDEV;                   4393                 return -EXDEV;
4361                                                  4394 
4362         /*                                       4395         /*
4363          * A reflink to an append-only or imm    4396          * A reflink to an append-only or immutable file cannot be created.
4364          */                                      4397          */
4365         if (IS_APPEND(inode) || IS_IMMUTABLE(    4398         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4366                 return -EPERM;                   4399                 return -EPERM;
4367                                                  4400 
4368         /* Only regular files can be reflinke    4401         /* Only regular files can be reflinked. */
4369         if (!S_ISREG(inode->i_mode))             4402         if (!S_ISREG(inode->i_mode))
4370                 return -EPERM;                   4403                 return -EPERM;
4371                                                  4404 
4372         /*                                       4405         /*
4373          * If the caller wants to preserve ow    4406          * If the caller wants to preserve ownership, they require the
4374          * rights to do so.                      4407          * rights to do so.
4375          */                                      4408          */
4376         if (preserve) {                          4409         if (preserve) {
4377                 if (!uid_eq(current_fsuid(),     4410                 if (!uid_eq(current_fsuid(), inode->i_uid) && !capable(CAP_CHOWN))
4378                         return -EPERM;           4411                         return -EPERM;
4379                 if (!in_group_p(inode->i_gid)    4412                 if (!in_group_p(inode->i_gid) && !capable(CAP_CHOWN))
4380                         return -EPERM;           4413                         return -EPERM;
4381         }                                        4414         }
4382                                                  4415 
4383         /*                                       4416         /*
4384          * If the caller is modifying any asp    4417          * If the caller is modifying any aspect of the attributes, they
4385          * are not creating a snapshot.  They    4418          * are not creating a snapshot.  They need read permission on the
4386          * file.                                 4419          * file.
4387          */                                      4420          */
4388         if (!preserve) {                         4421         if (!preserve) {
4389                 error = inode_permission(&nop !! 4422                 error = inode_permission(inode, MAY_READ);
4390                 if (error)                       4423                 if (error)
4391                         return error;            4424                         return error;
4392         }                                        4425         }
4393                                                  4426 
4394         inode_lock(inode);                    !! 4427         mutex_lock(&inode->i_mutex);
4395         error = dquot_initialize(dir);        !! 4428         dquot_initialize(dir);
4396         if (!error)                           !! 4429         error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve);
4397                 error = ocfs2_reflink(old_den !! 4430         mutex_unlock(&inode->i_mutex);
4398         inode_unlock(inode);                  << 
4399         if (!error)                              4431         if (!error)
4400                 fsnotify_create(dir, new_dent    4432                 fsnotify_create(dir, new_dentry);
4401         return error;                            4433         return error;
4402 }                                                4434 }
4403 /*                                               4435 /*
4404  * Most codes are copied from sys_linkat.        4436  * Most codes are copied from sys_linkat.
4405  */                                              4437  */
4406 int ocfs2_reflink_ioctl(struct inode *inode,     4438 int ocfs2_reflink_ioctl(struct inode *inode,
4407                         const char __user *ol    4439                         const char __user *oldname,
4408                         const char __user *ne    4440                         const char __user *newname,
4409                         bool preserve)           4441                         bool preserve)
4410 {                                                4442 {
4411         struct dentry *new_dentry;               4443         struct dentry *new_dentry;
4412         struct path old_path, new_path;          4444         struct path old_path, new_path;
4413         int error;                               4445         int error;
4414                                                  4446 
4415         if (!ocfs2_refcount_tree(OCFS2_SB(ino    4447         if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
4416                 return -EOPNOTSUPP;              4448                 return -EOPNOTSUPP;
4417                                                  4449 
4418         error = user_path_at(AT_FDCWD, oldnam    4450         error = user_path_at(AT_FDCWD, oldname, 0, &old_path);
4419         if (error) {                             4451         if (error) {
4420                 mlog_errno(error);               4452                 mlog_errno(error);
4421                 return error;                    4453                 return error;
4422         }                                        4454         }
4423                                                  4455 
4424         new_dentry = user_path_create(AT_FDCW    4456         new_dentry = user_path_create(AT_FDCWD, newname, &new_path, 0);
4425         error = PTR_ERR(new_dentry);             4457         error = PTR_ERR(new_dentry);
4426         if (IS_ERR(new_dentry)) {                4458         if (IS_ERR(new_dentry)) {
4427                 mlog_errno(error);               4459                 mlog_errno(error);
4428                 goto out;                        4460                 goto out;
4429         }                                        4461         }
4430                                                  4462 
4431         error = -EXDEV;                          4463         error = -EXDEV;
4432         if (old_path.mnt != new_path.mnt) {      4464         if (old_path.mnt != new_path.mnt) {
4433                 mlog_errno(error);               4465                 mlog_errno(error);
4434                 goto out_dput;                   4466                 goto out_dput;
4435         }                                        4467         }
4436                                                  4468 
4437         error = ocfs2_vfs_reflink(old_path.de    4469         error = ocfs2_vfs_reflink(old_path.dentry,
4438                                   d_inode(new !! 4470                                   new_path.dentry->d_inode,
4439                                   new_dentry,    4471                                   new_dentry, preserve);
4440 out_dput:                                        4472 out_dput:
4441         done_path_create(&new_path, new_dentr    4473         done_path_create(&new_path, new_dentry);
4442 out:                                             4474 out:
4443         path_put(&old_path);                     4475         path_put(&old_path);
4444                                                  4476 
4445         return error;                            4477         return error;
4446 }                                             << 
4447                                               << 
4448 /* Update destination inode size, if necessar << 
4449 int ocfs2_reflink_update_dest(struct inode *d << 
4450                               struct buffer_h << 
4451                               loff_t newlen)  << 
4452 {                                             << 
4453         handle_t *handle;                     << 
4454         int ret;                              << 
4455                                               << 
4456         dest->i_blocks = ocfs2_inode_sector_c << 
4457                                               << 
4458         if (newlen <= i_size_read(dest))      << 
4459                 return 0;                     << 
4460                                               << 
4461         handle = ocfs2_start_trans(OCFS2_SB(d << 
4462                                    OCFS2_INOD << 
4463         if (IS_ERR(handle)) {                 << 
4464                 ret = PTR_ERR(handle);        << 
4465                 mlog_errno(ret);              << 
4466                 return ret;                   << 
4467         }                                     << 
4468                                               << 
4469         /* Extend i_size if needed. */        << 
4470         spin_lock(&OCFS2_I(dest)->ip_lock);   << 
4471         if (newlen > i_size_read(dest))       << 
4472                 i_size_write(dest, newlen);   << 
4473         spin_unlock(&OCFS2_I(dest)->ip_lock); << 
4474         inode_set_mtime_to_ts(dest, inode_set << 
4475                                               << 
4476         ret = ocfs2_mark_inode_dirty(handle,  << 
4477         if (ret) {                            << 
4478                 mlog_errno(ret);              << 
4479                 goto out_commit;              << 
4480         }                                     << 
4481                                               << 
4482 out_commit:                                   << 
4483         ocfs2_commit_trans(OCFS2_SB(dest->i_s << 
4484         return ret;                           << 
4485 }                                             << 
4486                                               << 
4487 /* Remap the range pos_in:len in s_inode to p << 
4488 static loff_t ocfs2_reflink_remap_extent(stru << 
4489                                          stru << 
4490                                          loff << 
4491                                          stru << 
4492                                          stru << 
4493                                          loff << 
4494                                          loff << 
4495                                          stru << 
4496 {                                             << 
4497         struct ocfs2_extent_tree s_et;        << 
4498         struct ocfs2_extent_tree t_et;        << 
4499         struct ocfs2_dinode *dis;             << 
4500         struct buffer_head *ref_root_bh = NUL << 
4501         struct ocfs2_refcount_tree *ref_tree; << 
4502         struct ocfs2_super *osb;              << 
4503         loff_t remapped_bytes = 0;            << 
4504         loff_t pstart, plen;                  << 
4505         u32 p_cluster, num_clusters, slast, s << 
4506         unsigned int ext_flags;               << 
4507         int ret = 0;                          << 
4508                                               << 
4509         osb = OCFS2_SB(s_inode->i_sb);        << 
4510         dis = (struct ocfs2_dinode *)s_bh->b_ << 
4511         ocfs2_init_dinode_extent_tree(&s_et,  << 
4512         ocfs2_init_dinode_extent_tree(&t_et,  << 
4513                                               << 
4514         spos = ocfs2_bytes_to_clusters(s_inod << 
4515         tpos = ocfs2_bytes_to_clusters(t_inod << 
4516         slast = ocfs2_clusters_for_bytes(s_in << 
4517                                               << 
4518         while (spos < slast) {                << 
4519                 if (fatal_signal_pending(curr << 
4520                         ret = -EINTR;         << 
4521                         goto out;             << 
4522                 }                             << 
4523                                               << 
4524                 /* Look up the extent. */     << 
4525                 ret = ocfs2_get_clusters(s_in << 
4526                                          &num << 
4527                 if (ret) {                    << 
4528                         mlog_errno(ret);      << 
4529                         goto out;             << 
4530                 }                             << 
4531                                               << 
4532                 num_clusters = min_t(u32, num << 
4533                                               << 
4534                 /* Punch out the dest range.  << 
4535                 pstart = ocfs2_clusters_to_by << 
4536                 plen = ocfs2_clusters_to_byte << 
4537                 ret = ocfs2_remove_inode_rang << 
4538                 if (ret) {                    << 
4539                         mlog_errno(ret);      << 
4540                         goto out;             << 
4541                 }                             << 
4542                                               << 
4543                 if (p_cluster == 0)           << 
4544                         goto next_loop;       << 
4545                                               << 
4546                 /* Lock the refcount btree... << 
4547                 ret = ocfs2_lock_refcount_tre << 
4548                                               << 
4549                                               << 
4550                 if (ret) {                    << 
4551                         mlog_errno(ret);      << 
4552                         goto out;             << 
4553                 }                             << 
4554                                               << 
4555                 /* Mark s_inode's extent as r << 
4556                 if (!(ext_flags & OCFS2_EXT_R << 
4557                         ret = ocfs2_add_refco << 
4558                                               << 
4559                                               << 
4560                                               << 
4561                                               << 
4562                         if (ret) {            << 
4563                                 mlog_errno(re << 
4564                                 goto out_unlo << 
4565                         }                     << 
4566                 }                             << 
4567                                               << 
4568                 /* Map in the new extent. */  << 
4569                 ext_flags |= OCFS2_EXT_REFCOU << 
4570                 ret = ocfs2_add_refcounted_ex << 
4571                                               << 
4572                                               << 
4573                                               << 
4574                                               << 
4575                                               << 
4576                                               << 
4577                 if (ret) {                    << 
4578                         mlog_errno(ret);      << 
4579                         goto out_unlock_refco << 
4580                 }                             << 
4581                                               << 
4582                 ocfs2_unlock_refcount_tree(os << 
4583                 brelse(ref_root_bh);          << 
4584 next_loop:                                    << 
4585                 spos += num_clusters;         << 
4586                 tpos += num_clusters;         << 
4587                 remapped_clus += num_clusters << 
4588         }                                     << 
4589                                               << 
4590         goto out;                             << 
4591 out_unlock_refcount:                          << 
4592         ocfs2_unlock_refcount_tree(osb, ref_t << 
4593         brelse(ref_root_bh);                  << 
4594 out:                                          << 
4595         remapped_bytes = ocfs2_clusters_to_by << 
4596         remapped_bytes = min_t(loff_t, len, r << 
4597                                               << 
4598         return remapped_bytes > 0 ? remapped_ << 
4599 }                                             << 
4600                                               << 
4601 /* Set up refcount tree and remap s_inode to  << 
4602 loff_t ocfs2_reflink_remap_blocks(struct inod << 
4603                                   struct buff << 
4604                                   loff_t pos_ << 
4605                                   struct inod << 
4606                                   struct buff << 
4607                                   loff_t pos_ << 
4608                                   loff_t len) << 
4609 {                                             << 
4610         struct ocfs2_cached_dealloc_ctxt deal << 
4611         struct ocfs2_super *osb;              << 
4612         struct ocfs2_dinode *dis;             << 
4613         struct ocfs2_dinode *dit;             << 
4614         loff_t ret;                           << 
4615                                               << 
4616         osb = OCFS2_SB(s_inode->i_sb);        << 
4617         dis = (struct ocfs2_dinode *)s_bh->b_ << 
4618         dit = (struct ocfs2_dinode *)t_bh->b_ << 
4619         ocfs2_init_dealloc_ctxt(&dealloc);    << 
4620                                               << 
4621         /*                                    << 
4622          * If we're reflinking the entire fil << 
4623          * data, just copy the contents.      << 
4624          */                                   << 
4625         if (pos_in == pos_out && pos_in == 0  << 
4626             i_size_read(t_inode) <= len &&    << 
4627             (OCFS2_I(s_inode)->ip_dyn_feature << 
4628                 ret = ocfs2_duplicate_inline_ << 
4629                 if (ret)                      << 
4630                         mlog_errno(ret);      << 
4631                 goto out;                     << 
4632         }                                     << 
4633                                               << 
4634         /*                                    << 
4635          * If both inodes belong to two diffe << 
4636          * forget it because we don't know ho << 
4637          * refcount trees.                    << 
4638          */                                   << 
4639         ret = -EOPNOTSUPP;                    << 
4640         if (ocfs2_is_refcount_inode(s_inode)  << 
4641             ocfs2_is_refcount_inode(t_inode)  << 
4642             le64_to_cpu(dis->i_refcount_loc)  << 
4643             le64_to_cpu(dit->i_refcount_loc)) << 
4644                 goto out;                     << 
4645                                               << 
4646         /* Neither inode has a refcount tree. << 
4647         if (!ocfs2_is_refcount_inode(s_inode) << 
4648             !ocfs2_is_refcount_inode(t_inode) << 
4649                 ret = ocfs2_create_refcount_t << 
4650                 if (ret) {                    << 
4651                         mlog_errno(ret);      << 
4652                         goto out;             << 
4653                 }                             << 
4654         }                                     << 
4655                                               << 
4656         /* Ensure that both inodes end up wit << 
4657         if (!ocfs2_is_refcount_inode(s_inode) << 
4658                 ret = ocfs2_set_refcount_tree << 
4659                                               << 
4660                 if (ret) {                    << 
4661                         mlog_errno(ret);      << 
4662                         goto out;             << 
4663                 }                             << 
4664         }                                     << 
4665         if (!ocfs2_is_refcount_inode(t_inode) << 
4666                 ret = ocfs2_set_refcount_tree << 
4667                                               << 
4668                 if (ret) {                    << 
4669                         mlog_errno(ret);      << 
4670                         goto out;             << 
4671                 }                             << 
4672         }                                     << 
4673                                               << 
4674         /* Turn off inline data in the dest f << 
4675         if (OCFS2_I(t_inode)->ip_dyn_features << 
4676                 ret = ocfs2_convert_inline_da << 
4677                 if (ret) {                    << 
4678                         mlog_errno(ret);      << 
4679                         goto out;             << 
4680                 }                             << 
4681         }                                     << 
4682                                               << 
4683         /* Actually remap extents now. */     << 
4684         ret = ocfs2_reflink_remap_extent(s_in << 
4685                                          pos_ << 
4686         if (ret < 0) {                        << 
4687                 mlog_errno(ret);              << 
4688                 goto out;                     << 
4689         }                                     << 
4690                                               << 
4691 out:                                          << 
4692         if (ocfs2_dealloc_has_cluster(&deallo << 
4693                 ocfs2_schedule_truncate_log_f << 
4694                 ocfs2_run_deallocs(osb, &deal << 
4695         }                                     << 
4696                                               << 
4697         return ret;                           << 
4698 }                                             << 
4699                                               << 
4700 /* Lock an inode and grab a bh pointing to th << 
4701 int ocfs2_reflink_inodes_lock(struct inode *s << 
4702                               struct buffer_h << 
4703                               struct inode *t << 
4704                               struct buffer_h << 
4705 {                                             << 
4706         struct inode *inode1 = s_inode;       << 
4707         struct inode *inode2 = t_inode;       << 
4708         struct ocfs2_inode_info *oi1;         << 
4709         struct ocfs2_inode_info *oi2;         << 
4710         struct buffer_head *bh1 = NULL;       << 
4711         struct buffer_head *bh2 = NULL;       << 
4712         bool same_inode = (s_inode == t_inode << 
4713         bool need_swap = (inode1->i_ino > ino << 
4714         int status;                           << 
4715                                               << 
4716         /* First grab the VFS and rw locks. * << 
4717         lock_two_nondirectories(s_inode, t_in << 
4718         if (need_swap)                        << 
4719                 swap(inode1, inode2);         << 
4720                                               << 
4721         status = ocfs2_rw_lock(inode1, 1);    << 
4722         if (status) {                         << 
4723                 mlog_errno(status);           << 
4724                 goto out_i1;                  << 
4725         }                                     << 
4726         if (!same_inode) {                    << 
4727                 status = ocfs2_rw_lock(inode2 << 
4728                 if (status) {                 << 
4729                         mlog_errno(status);   << 
4730                         goto out_i2;          << 
4731                 }                             << 
4732         }                                     << 
4733                                               << 
4734         /* Now go for the cluster locks */    << 
4735         oi1 = OCFS2_I(inode1);                << 
4736         oi2 = OCFS2_I(inode2);                << 
4737                                               << 
4738         trace_ocfs2_double_lock((unsigned lon << 
4739                                 (unsigned lon << 
4740                                               << 
4741         /* We always want to lock the one wit << 
4742         if (oi1->ip_blkno > oi2->ip_blkno)    << 
4743                 mlog_errno(-ENOLCK);          << 
4744                                               << 
4745         /* lock id1 */                        << 
4746         status = ocfs2_inode_lock_nested(inod << 
4747                                          OI_L << 
4748         if (status < 0) {                     << 
4749                 if (status != -ENOENT)        << 
4750                         mlog_errno(status);   << 
4751                 goto out_rw2;                 << 
4752         }                                     << 
4753                                               << 
4754         /* lock id2 */                        << 
4755         if (!same_inode) {                    << 
4756                 status = ocfs2_inode_lock_nes << 
4757                                               << 
4758                 if (status < 0) {             << 
4759                         if (status != -ENOENT << 
4760                                 mlog_errno(st << 
4761                         goto out_cl1;         << 
4762                 }                             << 
4763         } else {                              << 
4764                 bh2 = bh1;                    << 
4765         }                                     << 
4766                                               << 
4767         /*                                    << 
4768          * If we swapped inode order above, w << 
4769          * before passing them back to the ca << 
4770          */                                   << 
4771         if (need_swap)                        << 
4772                 swap(bh1, bh2);               << 
4773         *bh_s = bh1;                          << 
4774         *bh_t = bh2;                          << 
4775                                               << 
4776         trace_ocfs2_double_lock_end(          << 
4777                         (unsigned long long)o << 
4778                         (unsigned long long)o << 
4779                                               << 
4780         return 0;                             << 
4781                                               << 
4782 out_cl1:                                      << 
4783         ocfs2_inode_unlock(inode1, 1);        << 
4784         brelse(bh1);                          << 
4785 out_rw2:                                      << 
4786         ocfs2_rw_unlock(inode2, 1);           << 
4787 out_i2:                                       << 
4788         ocfs2_rw_unlock(inode1, 1);           << 
4789 out_i1:                                       << 
4790         unlock_two_nondirectories(s_inode, t_ << 
4791         return status;                        << 
4792 }                                             << 
4793                                               << 
4794 /* Unlock both inodes and release buffers. */ << 
4795 void ocfs2_reflink_inodes_unlock(struct inode << 
4796                                  struct buffe << 
4797                                  struct inode << 
4798                                  struct buffe << 
4799 {                                             << 
4800         ocfs2_inode_unlock(s_inode, 1);       << 
4801         ocfs2_rw_unlock(s_inode, 1);          << 
4802         brelse(s_bh);                         << 
4803         if (s_inode != t_inode) {             << 
4804                 ocfs2_inode_unlock(t_inode, 1 << 
4805                 ocfs2_rw_unlock(t_inode, 1);  << 
4806                 brelse(t_bh);                 << 
4807         }                                     << 
4808         unlock_two_nondirectories(s_inode, t_ << 
4809 }                                                4478 }
4810                                                  4479 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php