@@ -166,7 +166,7 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
166166
167167 cur_alloc_size = ins .offset ;
168168 ret = btrfs_add_ordered_extent (inode , start , ins .objectid ,
169- ins .offset );
169+ ins .offset , 0 );
170170 BUG_ON (ret );
171171 if (num_bytes < cur_alloc_size ) {
172172 printk ("num_bytes %Lu cur_alloc %Lu\n" , num_bytes ,
@@ -187,31 +187,32 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
187187 u64 extent_start ;
188188 u64 extent_end ;
189189 u64 bytenr ;
190- u64 cow_end ;
191190 u64 loops = 0 ;
192191 u64 total_fs_bytes ;
193192 struct btrfs_root * root = BTRFS_I (inode )-> root ;
194193 struct btrfs_block_group_cache * block_group ;
194+ struct btrfs_trans_handle * trans ;
195195 struct extent_buffer * leaf ;
196196 int found_type ;
197197 struct btrfs_path * path ;
198198 struct btrfs_file_extent_item * item ;
199199 int ret ;
200- int err ;
200+ int err = 0 ;
201201 struct btrfs_key found_key ;
202202
203203 total_fs_bytes = btrfs_super_total_bytes (& root -> fs_info -> super_copy );
204204 path = btrfs_alloc_path ();
205205 BUG_ON (!path );
206+ trans = btrfs_join_transaction (root , 1 );
207+ BUG_ON (!trans );
206208again :
207209 ret = btrfs_lookup_file_extent (NULL , root , path ,
208210 inode -> i_ino , start , 0 );
209211 if (ret < 0 ) {
210- btrfs_free_path ( path ) ;
211- return ret ;
212+ err = ret ;
213+ goto out ;
212214 }
213215
214- cow_end = end ;
215216 if (ret != 0 ) {
216217 if (path -> slots [0 ] == 0 )
217218 goto not_found ;
@@ -244,12 +245,11 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
244245 if (start < extent_start || start >= extent_end )
245246 goto not_found ;
246247
247- cow_end = min (end , extent_end - 1 );
248248 bytenr = btrfs_file_extent_disk_bytenr (leaf , item );
249249 if (bytenr == 0 )
250250 goto not_found ;
251251
252- if (btrfs_cross_ref_exists (root , & found_key , bytenr ))
252+ if (btrfs_cross_ref_exists (trans , root , & found_key , bytenr ))
253253 goto not_found ;
254254 /*
255255 * we may be called by the resizer, make sure we're inside
@@ -260,24 +260,32 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
260260 if (!block_group || block_group -> ro )
261261 goto not_found ;
262262
263+ bytenr += btrfs_file_extent_offset (leaf , item );
264+ extent_num_bytes = min (end + 1 , extent_end ) - start ;
265+ ret = btrfs_add_ordered_extent (inode , start , bytenr ,
266+ extent_num_bytes , 1 );
267+ if (ret ) {
268+ err = ret ;
269+ goto out ;
270+ }
271+
272+ btrfs_release_path (root , path );
263273 start = extent_end ;
274+ if (start <= end ) {
275+ loops ++ ;
276+ goto again ;
277+ }
264278 } else {
265- goto not_found ;
266- }
267- loop :
268- if (start > end ) {
279+ not_found :
280+ btrfs_end_transaction (trans , root );
269281 btrfs_free_path (path );
270- return 0 ;
282+ return cow_file_range ( inode , start , end ) ;
271283 }
272- btrfs_release_path (root , path );
273- loops ++ ;
274- goto again ;
275-
276- not_found :
277- btrfs_release_path (root , path );
278- cow_file_range (inode , start , end );
279- start = end + 1 ;
280- goto loop ;
284+ out :
285+ WARN_ON (err );
286+ btrfs_end_transaction (trans , root );
287+ btrfs_free_path (path );
288+ return err ;
281289}
282290
283291static int run_delalloc_range (struct inode * inode , u64 start , u64 end )
@@ -385,6 +393,11 @@ int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
385393 goto mapit ;
386394 }
387395
396+ if (btrfs_test_opt (root , NODATASUM ) ||
397+ btrfs_test_flag (inode , NODATASUM )) {
398+ goto mapit ;
399+ }
400+
388401 return btrfs_wq_submit_bio (BTRFS_I (inode )-> root -> fs_info ,
389402 inode , rw , bio , mirror_num ,
390403 __btrfs_submit_bio_hook );
@@ -527,6 +540,8 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
527540
528541 ordered_extent = btrfs_lookup_ordered_extent (inode , start );
529542 BUG_ON (!ordered_extent );
543+ if (test_bit (BTRFS_ORDERED_NOCOW , & ordered_extent -> flags ))
544+ goto nocow ;
530545
531546 lock_extent (io_tree , ordered_extent -> file_offset ,
532547 ordered_extent -> file_offset + ordered_extent -> len - 1 ,
@@ -567,6 +582,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
567582 unlock_extent (io_tree , ordered_extent -> file_offset ,
568583 ordered_extent -> file_offset + ordered_extent -> len - 1 ,
569584 GFP_NOFS );
585+ nocow :
570586 add_pending_csums (trans , inode , ordered_extent -> file_offset ,
571587 & ordered_extent -> list );
572588
0 commit comments