~ [ 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-5.10.228)


** Warning: Cannot open xref database.

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

~ [ 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