diff options
Diffstat (limited to 'fs/ocfs2/file.c')
-rw-r--r-- | fs/ocfs2/file.c | 242 |
1 files changed, 240 insertions, 2 deletions
diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index f0a6b1330a6e..11f7cf9f2511 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c | |||
@@ -541,12 +541,15 @@ int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, | |||
541 | struct ocfs2_alloc_context **data_ac, | 541 | struct ocfs2_alloc_context **data_ac, |
542 | struct ocfs2_alloc_context **meta_ac) | 542 | struct ocfs2_alloc_context **meta_ac) |
543 | { | 543 | { |
544 | int ret, num_free_extents; | 544 | int ret = 0, num_free_extents; |
545 | unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; | 545 | unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; |
546 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | 546 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
547 | 547 | ||
548 | *meta_ac = NULL; | 548 | *meta_ac = NULL; |
549 | *data_ac = NULL; | 549 | if (data_ac) |
550 | *data_ac = NULL; | ||
551 | |||
552 | BUG_ON(clusters_to_add != 0 && data_ac == NULL); | ||
550 | 553 | ||
551 | mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " | 554 | mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " |
552 | "clusters_to_add = %u, extents_to_split = %u\n", | 555 | "clusters_to_add = %u, extents_to_split = %u\n", |
@@ -583,6 +586,9 @@ int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, | |||
583 | } | 586 | } |
584 | } | 587 | } |
585 | 588 | ||
589 | if (clusters_to_add == 0) | ||
590 | goto out; | ||
591 | |||
586 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); | 592 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); |
587 | if (ret < 0) { | 593 | if (ret < 0) { |
588 | if (ret != -ENOSPC) | 594 | if (ret != -ENOSPC) |
@@ -1252,6 +1258,238 @@ out: | |||
1252 | return ret; | 1258 | return ret; |
1253 | } | 1259 | } |
1254 | 1260 | ||
1261 | static int __ocfs2_remove_inode_range(struct inode *inode, | ||
1262 | struct buffer_head *di_bh, | ||
1263 | u32 cpos, u32 phys_cpos, u32 len, | ||
1264 | struct ocfs2_cached_dealloc_ctxt *dealloc) | ||
1265 | { | ||
1266 | int ret; | ||
1267 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); | ||
1268 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
1269 | struct inode *tl_inode = osb->osb_tl_inode; | ||
1270 | handle_t *handle; | ||
1271 | struct ocfs2_alloc_context *meta_ac = NULL; | ||
1272 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | ||
1273 | |||
1274 | ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac); | ||
1275 | if (ret) { | ||
1276 | mlog_errno(ret); | ||
1277 | return ret; | ||
1278 | } | ||
1279 | |||
1280 | mutex_lock(&tl_inode->i_mutex); | ||
1281 | |||
1282 | if (ocfs2_truncate_log_needs_flush(osb)) { | ||
1283 | ret = __ocfs2_flush_truncate_log(osb); | ||
1284 | if (ret < 0) { | ||
1285 | mlog_errno(ret); | ||
1286 | goto out; | ||
1287 | } | ||
1288 | } | ||
1289 | |||
1290 | handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); | ||
1291 | if (handle == NULL) { | ||
1292 | ret = -ENOMEM; | ||
1293 | mlog_errno(ret); | ||
1294 | goto out; | ||
1295 | } | ||
1296 | |||
1297 | ret = ocfs2_journal_access(handle, inode, di_bh, | ||
1298 | OCFS2_JOURNAL_ACCESS_WRITE); | ||
1299 | if (ret) { | ||
1300 | mlog_errno(ret); | ||
1301 | goto out; | ||
1302 | } | ||
1303 | |||
1304 | ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac, | ||
1305 | dealloc); | ||
1306 | if (ret) { | ||
1307 | mlog_errno(ret); | ||
1308 | goto out_commit; | ||
1309 | } | ||
1310 | |||
1311 | OCFS2_I(inode)->ip_clusters -= len; | ||
1312 | di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); | ||
1313 | |||
1314 | ret = ocfs2_journal_dirty(handle, di_bh); | ||
1315 | if (ret) { | ||
1316 | mlog_errno(ret); | ||
1317 | goto out_commit; | ||
1318 | } | ||
1319 | |||
1320 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); | ||
1321 | if (ret) | ||
1322 | mlog_errno(ret); | ||
1323 | |||
1324 | out_commit: | ||
1325 | ocfs2_commit_trans(osb, handle); | ||
1326 | out: | ||
1327 | mutex_unlock(&tl_inode->i_mutex); | ||
1328 | |||
1329 | if (meta_ac) | ||
1330 | ocfs2_free_alloc_context(meta_ac); | ||
1331 | |||
1332 | return ret; | ||
1333 | } | ||
1334 | |||
1335 | /* | ||
1336 | * Truncate a byte range, avoiding pages within partial clusters. This | ||
1337 | * preserves those pages for the zeroing code to write to. | ||
1338 | */ | ||
1339 | static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start, | ||
1340 | u64 byte_len) | ||
1341 | { | ||
1342 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
1343 | loff_t start, end; | ||
1344 | struct address_space *mapping = inode->i_mapping; | ||
1345 | |||
1346 | start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start); | ||
1347 | end = byte_start + byte_len; | ||
1348 | end = end & ~(osb->s_clustersize - 1); | ||
1349 | |||
1350 | if (start < end) { | ||
1351 | unmap_mapping_range(mapping, start, end - start, 0); | ||
1352 | truncate_inode_pages_range(mapping, start, end - 1); | ||
1353 | } | ||
1354 | } | ||
1355 | |||
1356 | static int ocfs2_zero_partial_clusters(struct inode *inode, | ||
1357 | u64 start, u64 len) | ||
1358 | { | ||
1359 | int ret = 0; | ||
1360 | u64 tmpend, end = start + len; | ||
1361 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
1362 | unsigned int csize = osb->s_clustersize; | ||
1363 | handle_t *handle; | ||
1364 | |||
1365 | /* | ||
1366 | * The "start" and "end" values are NOT necessarily part of | ||
1367 | * the range whose allocation is being deleted. Rather, this | ||
1368 | * is what the user passed in with the request. We must zero | ||
1369 | * partial clusters here. There's no need to worry about | ||
1370 | * physical allocation - the zeroing code knows to skip holes. | ||
1371 | */ | ||
1372 | mlog(0, "byte start: %llu, end: %llu\n", | ||
1373 | (unsigned long long)start, (unsigned long long)end); | ||
1374 | |||
1375 | /* | ||
1376 | * If both edges are on a cluster boundary then there's no | ||
1377 | * zeroing required as the region is part of the allocation to | ||
1378 | * be truncated. | ||
1379 | */ | ||
1380 | if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0) | ||
1381 | goto out; | ||
1382 | |||
1383 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | ||
1384 | if (handle == NULL) { | ||
1385 | ret = -ENOMEM; | ||
1386 | mlog_errno(ret); | ||
1387 | goto out; | ||
1388 | } | ||
1389 | |||
1390 | /* | ||
1391 | * We want to get the byte offset of the end of the 1st cluster. | ||
1392 | */ | ||
1393 | tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1)); | ||
1394 | if (tmpend > end) | ||
1395 | tmpend = end; | ||
1396 | |||
1397 | mlog(0, "1st range: start: %llu, tmpend: %llu\n", | ||
1398 | (unsigned long long)start, (unsigned long long)tmpend); | ||
1399 | |||
1400 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend); | ||
1401 | if (ret) | ||
1402 | mlog_errno(ret); | ||
1403 | |||
1404 | if (tmpend < end) { | ||
1405 | /* | ||
1406 | * This may make start and end equal, but the zeroing | ||
1407 | * code will skip any work in that case so there's no | ||
1408 | * need to catch it up here. | ||
1409 | */ | ||
1410 | start = end & ~(osb->s_clustersize - 1); | ||
1411 | |||
1412 | mlog(0, "2nd range: start: %llu, end: %llu\n", | ||
1413 | (unsigned long long)start, (unsigned long long)end); | ||
1414 | |||
1415 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, end); | ||
1416 | if (ret) | ||
1417 | mlog_errno(ret); | ||
1418 | } | ||
1419 | |||
1420 | ocfs2_commit_trans(osb, handle); | ||
1421 | out: | ||
1422 | return ret; | ||
1423 | } | ||
1424 | |||
1425 | static int ocfs2_remove_inode_range(struct inode *inode, | ||
1426 | struct buffer_head *di_bh, u64 byte_start, | ||
1427 | u64 byte_len) | ||
1428 | { | ||
1429 | int ret = 0; | ||
1430 | u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size; | ||
1431 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
1432 | struct ocfs2_cached_dealloc_ctxt dealloc; | ||
1433 | |||
1434 | ocfs2_init_dealloc_ctxt(&dealloc); | ||
1435 | |||
1436 | if (byte_len == 0) | ||
1437 | return 0; | ||
1438 | |||
1439 | trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start); | ||
1440 | trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits; | ||
1441 | if (trunc_len >= trunc_start) | ||
1442 | trunc_len -= trunc_start; | ||
1443 | else | ||
1444 | trunc_len = 0; | ||
1445 | |||
1446 | mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n", | ||
1447 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | ||
1448 | (unsigned long long)byte_start, | ||
1449 | (unsigned long long)byte_len, trunc_start, trunc_len); | ||
1450 | |||
1451 | ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len); | ||
1452 | if (ret) { | ||
1453 | mlog_errno(ret); | ||
1454 | goto out; | ||
1455 | } | ||
1456 | |||
1457 | cpos = trunc_start; | ||
1458 | while (trunc_len) { | ||
1459 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, | ||
1460 | &alloc_size, NULL); | ||
1461 | if (ret) { | ||
1462 | mlog_errno(ret); | ||
1463 | goto out; | ||
1464 | } | ||
1465 | |||
1466 | if (alloc_size > trunc_len) | ||
1467 | alloc_size = trunc_len; | ||
1468 | |||
1469 | /* Only do work for non-holes */ | ||
1470 | if (phys_cpos != 0) { | ||
1471 | ret = __ocfs2_remove_inode_range(inode, di_bh, cpos, | ||
1472 | phys_cpos, alloc_size, | ||
1473 | &dealloc); | ||
1474 | if (ret) { | ||
1475 | mlog_errno(ret); | ||
1476 | goto out; | ||
1477 | } | ||
1478 | } | ||
1479 | |||
1480 | cpos += alloc_size; | ||
1481 | trunc_len -= alloc_size; | ||
1482 | } | ||
1483 | |||
1484 | ocfs2_truncate_cluster_pages(inode, byte_start, byte_len); | ||
1485 | |||
1486 | out: | ||
1487 | ocfs2_schedule_truncate_log_flush(osb, 1); | ||
1488 | ocfs2_run_deallocs(osb, &dealloc); | ||
1489 | |||
1490 | return ret; | ||
1491 | } | ||
1492 | |||
1255 | static int ocfs2_prepare_inode_for_write(struct dentry *dentry, | 1493 | static int ocfs2_prepare_inode_for_write(struct dentry *dentry, |
1256 | loff_t *ppos, | 1494 | loff_t *ppos, |
1257 | size_t count, | 1495 | size_t count, |