Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

btrfs: always pass readahead state to defrag

Defrag ioctl passes readahead from the file, but autodefrag does not
have a file so the readahead state is allocated when needed.

The autodefrag loop in cleaner thread iterates over inodes so we can
simply provide an on-stack readahead state and will not need to allocate
it in btrfs_defrag_file(). The size is 32 bytes which is acceptable.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>

+11 -21
+11 -21
fs/btrfs/defrag.c
··· 219 219 #define BTRFS_DEFRAG_BATCH 1024 220 220 221 221 static int btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info, 222 - struct inode_defrag *defrag) 222 + struct inode_defrag *defrag, 223 + struct file_ra_state *ra) 223 224 { 224 225 struct btrfs_root *inode_root; 225 226 struct inode *inode; ··· 259 258 range.len = (u64)-1; 260 259 range.start = cur; 261 260 range.extent_thresh = defrag->extent_thresh; 261 + file_ra_state_init(ra, inode->i_mapping); 262 262 263 263 sb_start_write(fs_info->sb); 264 - ret = btrfs_defrag_file(inode, NULL, &range, defrag->transid, 264 + ret = btrfs_defrag_file(inode, ra, &range, defrag->transid, 265 265 BTRFS_DEFRAG_BATCH); 266 266 sb_end_write(fs_info->sb); 267 267 iput(inode); ··· 289 287 290 288 atomic_inc(&fs_info->defrag_running); 291 289 while (1) { 290 + struct file_ra_state ra = { 0 }; 291 + 292 292 /* Pause the auto defragger. */ 293 293 if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)) 294 294 break; ··· 313 309 first_ino = defrag->ino + 1; 314 310 root_objectid = defrag->root; 315 311 316 - btrfs_run_defrag_inode(fs_info, defrag); 312 + btrfs_run_defrag_inode(fs_info, defrag, &ra); 317 313 } 318 314 atomic_dec(&fs_info->defrag_running); 319 315 ··· 1306 1302 if (entry->start + range_len <= *last_scanned_ret) 1307 1303 continue; 1308 1304 1309 - if (ra) 1310 - page_cache_sync_readahead(inode->vfs_inode.i_mapping, 1305 + page_cache_sync_readahead(inode->vfs_inode.i_mapping, 1311 1306 ra, NULL, entry->start >> PAGE_SHIFT, 1312 1307 ((entry->start + range_len - 1) >> PAGE_SHIFT) - 1313 1308 (entry->start >> PAGE_SHIFT) + 1); ··· 1338 1335 * Entry point to file defragmentation. 1339 1336 * 1340 1337 * @inode: inode to be defragged 1341 - * @ra: readahead state (can be NUL) 1338 + * @ra: readahead state 1342 1339 * @range: defrag options including range and flags 1343 1340 * @newer_than: minimum transid to defrag 1344 1341 * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode ··· 1360 1357 u64 cur; 1361 1358 u64 last_byte; 1362 1359 bool do_compress = (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS); 1363 - bool ra_allocated = false; 1364 1360 int compress_type = BTRFS_COMPRESS_ZLIB; 1365 1361 int ret = 0; 1366 1362 u32 extent_thresh = range->extent_thresh; 1367 1363 pgoff_t start_index; 1364 + 1365 + ASSERT(ra); 1368 1366 1369 1367 if (isize == 0) 1370 1368 return 0; ··· 1394 1390 /* Align the range */ 1395 1391 cur = round_down(range->start, fs_info->sectorsize); 1396 1392 last_byte = round_up(last_byte, fs_info->sectorsize) - 1; 1397 - 1398 - /* 1399 - * If we were not given a ra, allocate a readahead context. As 1400 - * readahead is just an optimization, defrag will work without it so 1401 - * we don't error out. 1402 - */ 1403 - if (!ra) { 1404 - ra_allocated = true; 1405 - ra = kzalloc(sizeof(*ra), GFP_KERNEL); 1406 - if (ra) 1407 - file_ra_state_init(ra, inode->i_mapping); 1408 - } 1409 1393 1410 1394 /* 1411 1395 * Make writeback start from the beginning of the range, so that the ··· 1449 1457 cond_resched(); 1450 1458 } 1451 1459 1452 - if (ra_allocated) 1453 - kfree(ra); 1454 1460 /* 1455 1461 * Update range.start for autodefrag, this will indicate where to start 1456 1462 * in next run.