file.c (5f56406aabdf5444d040c5955effc665b1d0dbaf) | file.c (d1310b2e0cd98eb1348553e69b73827b436dca7b) |
---|---|
1/* 2 * Copyright (C) 2007 Oracle. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public 6 * License v2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, --- 219 unchanged lines hidden (view full) --- 228 struct page **pages, 229 size_t num_pages, 230 loff_t pos, 231 size_t write_bytes) 232{ 233 int err = 0; 234 int i; 235 struct inode *inode = fdentry(file)->d_inode; | 1/* 2 * Copyright (C) 2007 Oracle. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public 6 * License v2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, --- 219 unchanged lines hidden (view full) --- 228 struct page **pages, 229 size_t num_pages, 230 loff_t pos, 231 size_t write_bytes) 232{ 233 int err = 0; 234 int i; 235 struct inode *inode = fdentry(file)->d_inode; |
236 struct extent_map *em; 237 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | 236 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
238 u64 hint_byte; 239 u64 num_bytes; 240 u64 start_pos; 241 u64 end_of_last_block; 242 u64 end_pos = pos + write_bytes; 243 u64 inline_size; 244 loff_t isize = i_size_read(inode); | 237 u64 hint_byte; 238 u64 num_bytes; 239 u64 start_pos; 240 u64 end_of_last_block; 241 u64 end_pos = pos + write_bytes; 242 u64 inline_size; 243 loff_t isize = i_size_read(inode); |
245 em = alloc_extent_map(GFP_NOFS); 246 if (!em) 247 return -ENOMEM; | |
248 | 244 |
249 em->bdev = inode->i_sb->s_bdev; 250 | |
251 start_pos = pos & ~((u64)root->sectorsize - 1); 252 num_bytes = (write_bytes + pos - start_pos + 253 root->sectorsize - 1) & ~((u64)root->sectorsize - 1); 254 255 end_of_last_block = start_pos + num_bytes - 1; 256 | 245 start_pos = pos & ~((u64)root->sectorsize - 1); 246 num_bytes = (write_bytes + pos - start_pos + 247 root->sectorsize - 1) & ~((u64)root->sectorsize - 1); 248 249 end_of_last_block = start_pos + num_bytes - 1; 250 |
257 lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS); | 251 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS); |
258 mutex_lock(&root->fs_info->fs_mutex); 259 trans = btrfs_start_transaction(root, 1); 260 if (!trans) { 261 err = -ENOMEM; 262 goto out_unlock; 263 } 264 btrfs_set_trans_block_group(trans, inode); 265 inode->i_blocks += num_bytes >> 9; 266 hint_byte = 0; 267 268 if ((end_of_last_block & 4095) == 0) { 269 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block); 270 } | 252 mutex_lock(&root->fs_info->fs_mutex); 253 trans = btrfs_start_transaction(root, 1); 254 if (!trans) { 255 err = -ENOMEM; 256 goto out_unlock; 257 } 258 btrfs_set_trans_block_group(trans, inode); 259 inode->i_blocks += num_bytes >> 9; 260 hint_byte = 0; 261 262 if ((end_of_last_block & 4095) == 0) { 263 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block); 264 } |
271 set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS); | 265 set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS); |
272 273 /* FIXME...EIEIO, ENOSPC and more */ 274 275 /* insert any holes we need to create */ 276 if (inode->i_size < start_pos) { 277 u64 last_pos_in_file; 278 u64 hole_size; 279 u64 mask = root->sectorsize - 1; --- 8 unchanged lines hidden (view full) --- 288 &hint_byte); 289 if (err) 290 goto failed; 291 292 err = btrfs_insert_file_extent(trans, root, 293 inode->i_ino, 294 last_pos_in_file, 295 0, 0, hole_size); | 266 267 /* FIXME...EIEIO, ENOSPC and more */ 268 269 /* insert any holes we need to create */ 270 if (inode->i_size < start_pos) { 271 u64 last_pos_in_file; 272 u64 hole_size; 273 u64 mask = root->sectorsize - 1; --- 8 unchanged lines hidden (view full) --- 282 &hint_byte); 283 if (err) 284 goto failed; 285 286 err = btrfs_insert_file_extent(trans, root, 287 inode->i_ino, 288 last_pos_in_file, 289 0, 0, hole_size); |
290 btrfs_drop_extent_cache(inode, last_pos_in_file, 291 last_pos_in_file + hole_size -1); |
|
296 btrfs_check_file(root, inode); 297 } 298 if (err) 299 goto failed; 300 } 301 302 /* 303 * either allocate an extent for the new bytes or setup the key --- 11 unchanged lines hidden (view full) --- 315 SetPageUptodate(p); 316 set_page_dirty(p); 317 } 318 last_end = (u64)(pages[num_pages -1]->index) << 319 PAGE_CACHE_SHIFT; 320 last_end += PAGE_CACHE_SIZE - 1; 321 if (start_pos < isize) { 322 u64 delalloc_start = start_pos; | 292 btrfs_check_file(root, inode); 293 } 294 if (err) 295 goto failed; 296 } 297 298 /* 299 * either allocate an extent for the new bytes or setup the key --- 11 unchanged lines hidden (view full) --- 311 SetPageUptodate(p); 312 set_page_dirty(p); 313 } 314 last_end = (u64)(pages[num_pages -1]->index) << 315 PAGE_CACHE_SHIFT; 316 last_end += PAGE_CACHE_SIZE - 1; 317 if (start_pos < isize) { 318 u64 delalloc_start = start_pos; |
323 existing_delalloc = count_range_bits(em_tree, | 319 existing_delalloc = count_range_bits(io_tree, |
324 &delalloc_start, 325 end_of_last_block, (u64)-1, 326 EXTENT_DELALLOC); 327 } | 320 &delalloc_start, 321 end_of_last_block, (u64)-1, 322 EXTENT_DELALLOC); 323 } |
328 set_extent_delalloc(em_tree, start_pos, end_of_last_block, | 324 set_extent_delalloc(io_tree, start_pos, end_of_last_block, |
329 GFP_NOFS); 330 spin_lock(&root->fs_info->delalloc_lock); 331 root->fs_info->delalloc_bytes += (end_of_last_block + 1 - 332 start_pos) - existing_delalloc; 333 spin_unlock(&root->fs_info->delalloc_lock); 334 btrfs_add_ordered_inode(inode); 335 } else { 336 u64 aligned_end; --- 4 unchanged lines hidden (view full) --- 341 aligned_end, aligned_end, &hint_byte); 342 if (err) 343 goto failed; 344 if (isize > inline_size) 345 inline_size = min_t(u64, isize, aligned_end); 346 inline_size -= start_pos; 347 err = insert_inline_extent(trans, root, inode, start_pos, 348 inline_size, pages, 0, num_pages); | 325 GFP_NOFS); 326 spin_lock(&root->fs_info->delalloc_lock); 327 root->fs_info->delalloc_bytes += (end_of_last_block + 1 - 328 start_pos) - existing_delalloc; 329 spin_unlock(&root->fs_info->delalloc_lock); 330 btrfs_add_ordered_inode(inode); 331 } else { 332 u64 aligned_end; --- 4 unchanged lines hidden (view full) --- 337 aligned_end, aligned_end, &hint_byte); 338 if (err) 339 goto failed; 340 if (isize > inline_size) 341 inline_size = min_t(u64, isize, aligned_end); 342 inline_size -= start_pos; 343 err = insert_inline_extent(trans, root, inode, start_pos, 344 inline_size, pages, 0, num_pages); |
345 btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1); |
|
349 BUG_ON(err); 350 } 351 if (end_pos > isize) { 352 i_size_write(inode, end_pos); 353 btrfs_update_inode(trans, root, inode); 354 } 355failed: 356 err = btrfs_end_transaction(trans, root); 357out_unlock: 358 mutex_unlock(&root->fs_info->fs_mutex); | 346 BUG_ON(err); 347 } 348 if (end_pos > isize) { 349 i_size_write(inode, end_pos); 350 btrfs_update_inode(trans, root, inode); 351 } 352failed: 353 err = btrfs_end_transaction(trans, root); 354out_unlock: 355 mutex_unlock(&root->fs_info->fs_mutex); |
359 unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS); 360 free_extent_map(em); | 356 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS); |
361 return err; 362} 363 364int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end) 365{ 366 struct extent_map *em; 367 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 368 369 while(1) { | 357 return err; 358} 359 360int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end) 361{ 362 struct extent_map *em; 363 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 364 365 while(1) { |
366 spin_lock(&em_tree->lock); |
|
370 em = lookup_extent_mapping(em_tree, start, end); | 367 em = lookup_extent_mapping(em_tree, start, end); |
371 if (!em) | 368 if (!em) { 369 spin_unlock(&em_tree->lock); |
372 break; | 370 break; |
371 } |
|
373 remove_extent_mapping(em_tree, em); | 372 remove_extent_mapping(em_tree, em); |
373 spin_unlock(&em_tree->lock); 374 |
|
374 /* once for us */ 375 free_extent_map(em); 376 /* once for the tree*/ 377 free_extent_map(em); 378 } 379 return 0; 380} 381 --- 594 unchanged lines hidden --- | 375 /* once for us */ 376 free_extent_map(em); 377 /* once for the tree*/ 378 free_extent_map(em); 379 } 380 return 0; 381} 382 --- 594 unchanged lines hidden --- |