1452 zfs_extend(znode_t *zp, uint64_t end)
1453 {
1454 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1455 dmu_tx_t *tx;
1456 rl_t *rl;
1457 uint64_t newblksz;
1458 int error;
1459
1460 /*
1461 * We will change zp_size, lock the whole file.
1462 */
1463 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1464
1465 /*
1466 * Nothing to do if file already at desired length.
1467 */
1468 if (end <= zp->z_size) {
1469 zfs_range_unlock(rl);
1470 return (0);
1471 }
1472 top:
1473 tx = dmu_tx_create(zfsvfs->z_os);
1474 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1475 zfs_sa_upgrade_txholds(tx, zp);
1476 if (end > zp->z_blksz &&
1477 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1478 /*
1479 * We are growing the file past the current block size.
1480 */
1481 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1482 ASSERT(!ISP2(zp->z_blksz));
1483 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1484 } else {
1485 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1486 }
1487 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1488 } else {
1489 newblksz = 0;
1490 }
1491
1492 error = dmu_tx_assign(tx, TXG_NOWAIT);
1493 if (error) {
1494 if (error == ERESTART) {
1495 dmu_tx_wait(tx);
1496 dmu_tx_abort(tx);
1497 goto top;
1498 }
1499 dmu_tx_abort(tx);
1500 zfs_range_unlock(rl);
1501 return (error);
1502 }
1503
1504 if (newblksz)
1505 zfs_grow_blocksize(zp, newblksz, tx);
1506
1507 zp->z_size = end;
1508
1509 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1510 &zp->z_size, sizeof (zp->z_size), tx));
1511
1512 zfs_range_unlock(rl);
1513
1514 dmu_tx_commit(tx);
1515
1516 return (0);
1517 }
1518
1519 /*
1575 int count = 0;
1576
1577 /*
1578 * We will change zp_size, lock the whole file.
1579 */
1580 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1581
1582 /*
1583 * Nothing to do if file already at desired length.
1584 */
1585 if (end >= zp->z_size) {
1586 zfs_range_unlock(rl);
1587 return (0);
1588 }
1589
1590 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
1591 if (error) {
1592 zfs_range_unlock(rl);
1593 return (error);
1594 }
1595 top:
1596 tx = dmu_tx_create(zfsvfs->z_os);
1597 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1598 zfs_sa_upgrade_txholds(tx, zp);
1599 error = dmu_tx_assign(tx, TXG_NOWAIT);
1600 if (error) {
1601 if (error == ERESTART) {
1602 dmu_tx_wait(tx);
1603 dmu_tx_abort(tx);
1604 goto top;
1605 }
1606 dmu_tx_abort(tx);
1607 zfs_range_unlock(rl);
1608 return (error);
1609 }
1610
1611 zp->z_size = end;
1612 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1613 NULL, &zp->z_size, sizeof (zp->z_size));
1614
1615 if (end == 0) {
1616 zp->z_pflags &= ~ZFS_SPARSE;
1617 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1618 NULL, &zp->z_pflags, 8);
1619 }
1620 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1621
1622 dmu_tx_commit(tx);
1623
1624 /*
1625 * Clear any mapped pages in the truncated region. This has to
1626 * happen outside of the transaction to avoid the possibility of
1692
1693 if (MANDLOCK(vp, (mode_t)mode)) {
1694 uint64_t length = (len ? len : zp->z_size - off);
1695 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1696 return (error);
1697 }
1698
1699 if (len == 0) {
1700 error = zfs_trunc(zp, off);
1701 } else {
1702 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1703 off + len > zp->z_size)
1704 error = zfs_extend(zp, off+len);
1705 }
1706 if (error || !log)
1707 return (error);
1708 log:
1709 tx = dmu_tx_create(zfsvfs->z_os);
1710 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1711 zfs_sa_upgrade_txholds(tx, zp);
1712 error = dmu_tx_assign(tx, TXG_NOWAIT);
1713 if (error) {
1714 if (error == ERESTART) {
1715 dmu_tx_wait(tx);
1716 dmu_tx_abort(tx);
1717 goto log;
1718 }
1719 dmu_tx_abort(tx);
1720 return (error);
1721 }
1722
1723 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1724 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1725 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1726 NULL, &zp->z_pflags, 8);
1727 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1728 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1729 ASSERT(error == 0);
1730
1731 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1732
1733 dmu_tx_commit(tx);
1734 return (0);
1735 }
1736
1737 void
1738 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1739 {
|
1452 zfs_extend(znode_t *zp, uint64_t end)
1453 {
1454 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1455 dmu_tx_t *tx;
1456 rl_t *rl;
1457 uint64_t newblksz;
1458 int error;
1459
1460 /*
1461 * We will change zp_size, lock the whole file.
1462 */
1463 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1464
1465 /*
1466 * Nothing to do if file already at desired length.
1467 */
1468 if (end <= zp->z_size) {
1469 zfs_range_unlock(rl);
1470 return (0);
1471 }
1472 tx = dmu_tx_create(zfsvfs->z_os);
1473 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1474 zfs_sa_upgrade_txholds(tx, zp);
1475 if (end > zp->z_blksz &&
1476 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1477 /*
1478 * We are growing the file past the current block size.
1479 */
1480 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1481 ASSERT(!ISP2(zp->z_blksz));
1482 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1483 } else {
1484 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1485 }
1486 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1487 } else {
1488 newblksz = 0;
1489 }
1490
1491 error = dmu_tx_assign(tx, TXG_WAIT);
1492 if (error) {
1493 dmu_tx_abort(tx);
1494 zfs_range_unlock(rl);
1495 return (error);
1496 }
1497
1498 if (newblksz)
1499 zfs_grow_blocksize(zp, newblksz, tx);
1500
1501 zp->z_size = end;
1502
1503 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1504 &zp->z_size, sizeof (zp->z_size), tx));
1505
1506 zfs_range_unlock(rl);
1507
1508 dmu_tx_commit(tx);
1509
1510 return (0);
1511 }
1512
1513 /*
1569 int count = 0;
1570
1571 /*
1572 * We will change zp_size, lock the whole file.
1573 */
1574 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1575
1576 /*
1577 * Nothing to do if file already at desired length.
1578 */
1579 if (end >= zp->z_size) {
1580 zfs_range_unlock(rl);
1581 return (0);
1582 }
1583
1584 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
1585 if (error) {
1586 zfs_range_unlock(rl);
1587 return (error);
1588 }
1589 tx = dmu_tx_create(zfsvfs->z_os);
1590 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1591 zfs_sa_upgrade_txholds(tx, zp);
1592 error = dmu_tx_assign(tx, TXG_WAIT);
1593 if (error) {
1594 dmu_tx_abort(tx);
1595 zfs_range_unlock(rl);
1596 return (error);
1597 }
1598
1599 zp->z_size = end;
1600 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1601 NULL, &zp->z_size, sizeof (zp->z_size));
1602
1603 if (end == 0) {
1604 zp->z_pflags &= ~ZFS_SPARSE;
1605 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1606 NULL, &zp->z_pflags, 8);
1607 }
1608 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1609
1610 dmu_tx_commit(tx);
1611
1612 /*
1613 * Clear any mapped pages in the truncated region. This has to
1614 * happen outside of the transaction to avoid the possibility of
1680
1681 if (MANDLOCK(vp, (mode_t)mode)) {
1682 uint64_t length = (len ? len : zp->z_size - off);
1683 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1684 return (error);
1685 }
1686
1687 if (len == 0) {
1688 error = zfs_trunc(zp, off);
1689 } else {
1690 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1691 off + len > zp->z_size)
1692 error = zfs_extend(zp, off+len);
1693 }
1694 if (error || !log)
1695 return (error);
1696 log:
1697 tx = dmu_tx_create(zfsvfs->z_os);
1698 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1699 zfs_sa_upgrade_txholds(tx, zp);
1700 error = dmu_tx_assign(tx, TXG_WAIT);
1701 if (error) {
1702 dmu_tx_abort(tx);
1703 return (error);
1704 }
1705
1706 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1707 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1708 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1709 NULL, &zp->z_pflags, 8);
1710 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1711 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1712 ASSERT(error == 0);
1713
1714 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1715
1716 dmu_tx_commit(tx);
1717 return (0);
1718 }
1719
1720 void
1721 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1722 {
|