Print this page
3742 zfs comments need cleaner, more consistent style
Submitted by:   Will Andrews <willa@spectralogic.com>
Submitted by:   Alan Somers <alans@spectralogic.com>
Reviewed by:    Matthew Ahrens <mahrens@delphix.com>
Reviewed by:    George Wilson <george.wilson@delphix.com>
Reviewed by:    Eric Schrock <eric.schrock@delphix.com>


 989                 *zpp = dzp;
 990 
 991                 (*zpp)->z_sa_hdl = sa_hdl;
 992         }
 993 
 994         (*zpp)->z_pflags = pflags;
 995         (*zpp)->z_mode = mode;
 996 
 997         if (vap->va_mask & AT_XVATTR)
 998                 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
 999 
1000         if (obj_type == DMU_OT_ZNODE ||
1001             acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1002                 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
1003                 ASSERT0(err);
1004         }
1005         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1006 }
1007 
1008 /*
1009  * zfs_xvattr_set only updates the in-core attributes
1010  * it is assumed the caller will be doing an sa_bulk_update
1011  * to push the changes out
1012  */
1013 void
1014 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1015 {
1016         xoptattr_t *xoap;
1017 
1018         xoap = xva_getxoptattr(xvap);
1019         ASSERT(xoap);
1020 
1021         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1022                 uint64_t times[2];
1023                 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1024                 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1025                     &times, sizeof (times), tx);
1026                 XVA_SET_RTN(xvap, XAT_CREATETIME);
1027         }
1028         if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1029                 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1030                     zp->z_pflags, tx);
1031                 XVA_SET_RTN(xvap, XAT_READONLY);


1430 /*
1431  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1432  * be calling back into the fs for a putpage().  E.g.: when truncating
1433  * a file, the pages being "thrown away* don't need to be written out.
1434  */
1435 /* ARGSUSED */
1436 static int
1437 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1438     int flags, cred_t *cr)
1439 {
1440         ASSERT(0);
1441         return (0);
1442 }
1443 
1444 /*
1445  * Increase the file length
1446  *
1447  *      IN:     zp      - znode of file to free data in.
1448  *              end     - new end-of-file
1449  *
1450  *      RETURN: 0 if success
1451  *              error code if failure
1452  */
1453 static int
1454 zfs_extend(znode_t *zp, uint64_t end)
1455 {
1456         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1457         dmu_tx_t *tx;
1458         rl_t *rl;
1459         uint64_t newblksz;
1460         int error;
1461 
1462         /*
1463          * We will change zp_size, lock the whole file.
1464          */
1465         rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1466 
1467         /*
1468          * Nothing to do if file already at desired length.
1469          */
1470         if (end <= zp->z_size) {
1471                 zfs_range_unlock(rl);


1508 
1509         zp->z_size = end;
1510 
1511         VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1512             &zp->z_size, sizeof (zp->z_size), tx));
1513 
1514         zfs_range_unlock(rl);
1515 
1516         dmu_tx_commit(tx);
1517 
1518         return (0);
1519 }
1520 
1521 /*
1522  * Free space in a file.
1523  *
1524  *      IN:     zp      - znode of file to free data in.
1525  *              off     - start of section to free.
1526  *              len     - length of section to free.
1527  *
1528  *      RETURN: 0 if success
1529  *              error code if failure
1530  */
1531 static int
1532 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1533 {
1534         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1535         rl_t *rl;
1536         int error;
1537 
1538         /*
1539          * Lock the range being freed.
1540          */
1541         rl = zfs_range_lock(zp, off, len, RL_WRITER);
1542 
1543         /*
1544          * Nothing to do if file already at desired length.
1545          */
1546         if (off >= zp->z_size) {
1547                 zfs_range_unlock(rl);
1548                 return (0);
1549         }
1550 
1551         if (off + len > zp->z_size)
1552                 len = zp->z_size - off;
1553 
1554         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1555 
1556         zfs_range_unlock(rl);
1557 
1558         return (error);
1559 }
1560 
1561 /*
1562  * Truncate a file
1563  *
1564  *      IN:     zp      - znode of file to free data in.
1565  *              end     - new end-of-file.
1566  *
1567  *      RETURN: 0 if success
1568  *              error code if failure
1569  */
1570 static int
1571 zfs_trunc(znode_t *zp, uint64_t end)
1572 {
1573         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1574         vnode_t *vp = ZTOV(zp);
1575         dmu_tx_t *tx;
1576         rl_t *rl;
1577         int error;
1578         sa_bulk_attr_t bulk[2];
1579         int count = 0;
1580 
1581         /*
1582          * We will change zp_size, lock the whole file.
1583          */
1584         rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1585 
1586         /*
1587          * Nothing to do if file already at desired length.
1588          */


1646                 }
1647                 error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1648                     B_INVAL | B_TRUNC, NULL);
1649                 ASSERT(error == 0);
1650         }
1651 
1652         zfs_range_unlock(rl);
1653 
1654         return (0);
1655 }
1656 
1657 /*
1658  * Free space in a file
1659  *
1660  *      IN:     zp      - znode of file to free data in.
1661  *              off     - start of range
1662  *              len     - end of range (0 => EOF)
1663  *              flag    - current file open mode flags.
1664  *              log     - TRUE if this action should be logged
1665  *
1666  *      RETURN: 0 if success
1667  *              error code if failure
1668  */
1669 int
1670 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1671 {
1672         vnode_t *vp = ZTOV(zp);
1673         dmu_tx_t *tx;
1674         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1675         zilog_t *zilog = zfsvfs->z_log;
1676         uint64_t mode;
1677         uint64_t mtime[2], ctime[2];
1678         sa_bulk_attr_t bulk[3];
1679         int count = 0;
1680         int error;
1681 
1682         if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1683             sizeof (mode))) != 0)
1684                 return (error);
1685 
1686         if (off > zp->z_size) {
1687                 error =  zfs_extend(zp, off+len);




 989                 *zpp = dzp;
 990 
 991                 (*zpp)->z_sa_hdl = sa_hdl;
 992         }
 993 
 994         (*zpp)->z_pflags = pflags;
 995         (*zpp)->z_mode = mode;
 996 
 997         if (vap->va_mask & AT_XVATTR)
 998                 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
 999 
1000         if (obj_type == DMU_OT_ZNODE ||
1001             acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1002                 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
1003                 ASSERT0(err);
1004         }
1005         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1006 }
1007 
1008 /*
1009  * Update in-core attributes.  It is assumed the caller will be doing an
1010  * sa_bulk_update to push the changes out.

1011  */
1012 void
1013 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1014 {
1015         xoptattr_t *xoap;
1016 
1017         xoap = xva_getxoptattr(xvap);
1018         ASSERT(xoap);
1019 
1020         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1021                 uint64_t times[2];
1022                 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1023                 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1024                     &times, sizeof (times), tx);
1025                 XVA_SET_RTN(xvap, XAT_CREATETIME);
1026         }
1027         if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1028                 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1029                     zp->z_pflags, tx);
1030                 XVA_SET_RTN(xvap, XAT_READONLY);


1429 /*
1430  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1431  * be calling back into the fs for a putpage().  E.g.: when truncating
1432  * a file, the pages being "thrown away* don't need to be written out.
1433  */
1434 /* ARGSUSED */
1435 static int
1436 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1437     int flags, cred_t *cr)
1438 {
1439         ASSERT(0);
1440         return (0);
1441 }
1442 
1443 /*
1444  * Increase the file length
1445  *
1446  *      IN:     zp      - znode of file to free data in.
1447  *              end     - new end-of-file
1448  *
1449  *      RETURN: 0 on success, error code on failure

1450  */
1451 static int
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);


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 /*
1520  * Free space in a file.
1521  *
1522  *      IN:     zp      - znode of file to free data in.
1523  *              off     - start of section to free.
1524  *              len     - length of section to free.
1525  *
1526  *      RETURN: 0 on success, error code on failure

1527  */
1528 static int
1529 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1530 {
1531         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1532         rl_t *rl;
1533         int error;
1534 
1535         /*
1536          * Lock the range being freed.
1537          */
1538         rl = zfs_range_lock(zp, off, len, RL_WRITER);
1539 
1540         /*
1541          * Nothing to do if file already at desired length.
1542          */
1543         if (off >= zp->z_size) {
1544                 zfs_range_unlock(rl);
1545                 return (0);
1546         }
1547 
1548         if (off + len > zp->z_size)
1549                 len = zp->z_size - off;
1550 
1551         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1552 
1553         zfs_range_unlock(rl);
1554 
1555         return (error);
1556 }
1557 
1558 /*
1559  * Truncate a file
1560  *
1561  *      IN:     zp      - znode of file to free data in.
1562  *              end     - new end-of-file.
1563  *
1564  *      RETURN: 0 on success, error code on failure

1565  */
1566 static int
1567 zfs_trunc(znode_t *zp, uint64_t end)
1568 {
1569         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1570         vnode_t *vp = ZTOV(zp);
1571         dmu_tx_t *tx;
1572         rl_t *rl;
1573         int error;
1574         sa_bulk_attr_t bulk[2];
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          */


1642                 }
1643                 error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1644                     B_INVAL | B_TRUNC, NULL);
1645                 ASSERT(error == 0);
1646         }
1647 
1648         zfs_range_unlock(rl);
1649 
1650         return (0);
1651 }
1652 
1653 /*
1654  * Free space in a file
1655  *
1656  *      IN:     zp      - znode of file to free data in.
1657  *              off     - start of range
1658  *              len     - end of range (0 => EOF)
1659  *              flag    - current file open mode flags.
1660  *              log     - TRUE if this action should be logged
1661  *
1662  *      RETURN: 0 on success, error code on failure

1663  */
1664 int
1665 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1666 {
1667         vnode_t *vp = ZTOV(zp);
1668         dmu_tx_t *tx;
1669         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1670         zilog_t *zilog = zfsvfs->z_log;
1671         uint64_t mode;
1672         uint64_t mtime[2], ctime[2];
1673         sa_bulk_attr_t bulk[3];
1674         int count = 0;
1675         int error;
1676 
1677         if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1678             sizeof (mode))) != 0)
1679                 return (error);
1680 
1681         if (off > zp->z_size) {
1682                 error =  zfs_extend(zp, off+len);