aboutsummaryrefslogtreecommitdiffstats
path: root/fs/xfs/xfs_trans.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/xfs/xfs_trans.c')
-rw-r--r--fs/xfs/xfs_trans.c475
1 files changed, 4 insertions, 471 deletions
diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
index 1f35b2feca97..329b06aba1c2 100644
--- a/fs/xfs/xfs_trans.c
+++ b/fs/xfs/xfs_trans.c
@@ -1158,7 +1158,6 @@ xfs_trans_add_item(
1158 1158
1159 lidp->lid_item = lip; 1159 lidp->lid_item = lip;
1160 lidp->lid_flags = 0; 1160 lidp->lid_flags = 0;
1161 lidp->lid_size = 0;
1162 list_add_tail(&lidp->lid_trans, &tp->t_items); 1161 list_add_tail(&lidp->lid_trans, &tp->t_items);
1163 1162
1164 lip->li_desc = lidp; 1163 lip->li_desc = lidp;
@@ -1210,219 +1209,6 @@ xfs_trans_free_items(
1210 } 1209 }
1211} 1210}
1212 1211
1213/*
1214 * Unlock the items associated with a transaction.
1215 *
1216 * Items which were not logged should be freed. Those which were logged must
1217 * still be tracked so they can be unpinned when the transaction commits.
1218 */
1219STATIC void
1220xfs_trans_unlock_items(
1221 struct xfs_trans *tp,
1222 xfs_lsn_t commit_lsn)
1223{
1224 struct xfs_log_item_desc *lidp, *next;
1225
1226 list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
1227 struct xfs_log_item *lip = lidp->lid_item;
1228
1229 lip->li_desc = NULL;
1230
1231 if (commit_lsn != NULLCOMMITLSN)
1232 IOP_COMMITTING(lip, commit_lsn);
1233 IOP_UNLOCK(lip);
1234
1235 /*
1236 * Free the descriptor if the item is not dirty
1237 * within this transaction.
1238 */
1239 if (!(lidp->lid_flags & XFS_LID_DIRTY))
1240 xfs_trans_free_item_desc(lidp);
1241 }
1242}
1243
1244/*
1245 * Total up the number of log iovecs needed to commit this
1246 * transaction. The transaction itself needs one for the
1247 * transaction header. Ask each dirty item in turn how many
1248 * it needs to get the total.
1249 */
1250static uint
1251xfs_trans_count_vecs(
1252 struct xfs_trans *tp)
1253{
1254 int nvecs;
1255 struct xfs_log_item_desc *lidp;
1256
1257 nvecs = 1;
1258
1259 /* In the non-debug case we need to start bailing out if we
1260 * didn't find a log_item here, return zero and let trans_commit
1261 * deal with it.
1262 */
1263 if (list_empty(&tp->t_items)) {
1264 ASSERT(0);
1265 return 0;
1266 }
1267
1268 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
1269 /*
1270 * Skip items which aren't dirty in this transaction.
1271 */
1272 if (!(lidp->lid_flags & XFS_LID_DIRTY))
1273 continue;
1274 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1275 nvecs += lidp->lid_size;
1276 }
1277
1278 return nvecs;
1279}
1280
1281/*
1282 * Fill in the vector with pointers to data to be logged
1283 * by this transaction. The transaction header takes
1284 * the first vector, and then each dirty item takes the
1285 * number of vectors it indicated it needed in xfs_trans_count_vecs().
1286 *
1287 * As each item fills in the entries it needs, also pin the item
1288 * so that it cannot be flushed out until the log write completes.
1289 */
1290static void
1291xfs_trans_fill_vecs(
1292 struct xfs_trans *tp,
1293 struct xfs_log_iovec *log_vector)
1294{
1295 struct xfs_log_item_desc *lidp;
1296 struct xfs_log_iovec *vecp;
1297 uint nitems;
1298
1299 /*
1300 * Skip over the entry for the transaction header, we'll
1301 * fill that in at the end.
1302 */
1303 vecp = log_vector + 1;
1304
1305 nitems = 0;
1306 ASSERT(!list_empty(&tp->t_items));
1307 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
1308 /* Skip items which aren't dirty in this transaction. */
1309 if (!(lidp->lid_flags & XFS_LID_DIRTY))
1310 continue;
1311
1312 /*
1313 * The item may be marked dirty but not log anything. This can
1314 * be used to get called when a transaction is committed.
1315 */
1316 if (lidp->lid_size)
1317 nitems++;
1318 IOP_FORMAT(lidp->lid_item, vecp);
1319 vecp += lidp->lid_size;
1320 IOP_PIN(lidp->lid_item);
1321 }
1322
1323 /*
1324 * Now that we've counted the number of items in this transaction, fill
1325 * in the transaction header. Note that the transaction header does not
1326 * have a log item.
1327 */
1328 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1329 tp->t_header.th_type = tp->t_type;
1330 tp->t_header.th_num_items = nitems;
1331 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1332 log_vector->i_len = sizeof(xfs_trans_header_t);
1333 log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
1334}
1335
1336/*
1337 * The committed item processing consists of calling the committed routine of
1338 * each logged item, updating the item's position in the AIL if necessary, and
1339 * unpinning each item. If the committed routine returns -1, then do nothing
1340 * further with the item because it may have been freed.
1341 *
1342 * Since items are unlocked when they are copied to the incore log, it is
1343 * possible for two transactions to be completing and manipulating the same
1344 * item simultaneously. The AIL lock will protect the lsn field of each item.
1345 * The value of this field can never go backwards.
1346 *
1347 * We unpin the items after repositioning them in the AIL, because otherwise
1348 * they could be immediately flushed and we'd have to race with the flusher
1349 * trying to pull the item from the AIL as we add it.
1350 */
1351static void
1352xfs_trans_item_committed(
1353 struct xfs_log_item *lip,
1354 xfs_lsn_t commit_lsn,
1355 int aborted)
1356{
1357 xfs_lsn_t item_lsn;
1358 struct xfs_ail *ailp;
1359
1360 if (aborted)
1361 lip->li_flags |= XFS_LI_ABORTED;
1362 item_lsn = IOP_COMMITTED(lip, commit_lsn);
1363
1364 /* item_lsn of -1 means the item needs no further processing */
1365 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
1366 return;
1367
1368 /*
1369 * If the returned lsn is greater than what it contained before, update
1370 * the location of the item in the AIL. If it is not, then do nothing.
1371 * Items can never move backwards in the AIL.
1372 *
1373 * While the new lsn should usually be greater, it is possible that a
1374 * later transaction completing simultaneously with an earlier one
1375 * using the same item could complete first with a higher lsn. This
1376 * would cause the earlier transaction to fail the test below.
1377 */
1378 ailp = lip->li_ailp;
1379 spin_lock(&ailp->xa_lock);
1380 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1381 /*
1382 * This will set the item's lsn to item_lsn and update the
1383 * position of the item in the AIL.
1384 *
1385 * xfs_trans_ail_update() drops the AIL lock.
1386 */
1387 xfs_trans_ail_update(ailp, lip, item_lsn);
1388 } else {
1389 spin_unlock(&ailp->xa_lock);
1390 }
1391
1392 /*
1393 * Now that we've repositioned the item in the AIL, unpin it so it can
1394 * be flushed. Pass information about buffer stale state down from the
1395 * log item flags, if anyone else stales the buffer we do not want to
1396 * pay any attention to it.
1397 */
1398 IOP_UNPIN(lip, 0);
1399}
1400
1401/*
1402 * This is typically called by the LM when a transaction has been fully
1403 * committed to disk. It needs to unpin the items which have
1404 * been logged by the transaction and update their positions
1405 * in the AIL if necessary.
1406 *
1407 * This also gets called when the transactions didn't get written out
1408 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1409 */
1410STATIC void
1411xfs_trans_committed(
1412 void *arg,
1413 int abortflag)
1414{
1415 struct xfs_trans *tp = arg;
1416 struct xfs_log_item_desc *lidp, *next;
1417
1418 list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
1419 xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
1420 xfs_trans_free_item_desc(lidp);
1421 }
1422
1423 xfs_trans_free(tp);
1424}
1425
1426static inline void 1212static inline void
1427xfs_log_item_batch_insert( 1213xfs_log_item_batch_insert(
1428 struct xfs_ail *ailp, 1214 struct xfs_ail *ailp,
@@ -1538,258 +1324,6 @@ xfs_trans_committed_bulk(
1538} 1324}
1539 1325
1540/* 1326/*
1541 * Called from the trans_commit code when we notice that the filesystem is in
1542 * the middle of a forced shutdown.
1543 *
1544 * When we are called here, we have already pinned all the items in the
1545 * transaction. However, neither IOP_COMMITTING or IOP_UNLOCK has been called
1546 * so we can simply walk the items in the transaction, unpin them with an abort
1547 * flag and then free the items. Note that unpinning the items can result in
1548 * them being freed immediately, so we need to use a safe list traversal method
1549 * here.
1550 */
1551STATIC void
1552xfs_trans_uncommit(
1553 struct xfs_trans *tp,
1554 uint flags)
1555{
1556 struct xfs_log_item_desc *lidp, *n;
1557
1558 list_for_each_entry_safe(lidp, n, &tp->t_items, lid_trans) {
1559 if (lidp->lid_flags & XFS_LID_DIRTY)
1560 IOP_UNPIN(lidp->lid_item, 1);
1561 }
1562
1563 xfs_trans_unreserve_and_mod_sb(tp);
1564 xfs_trans_unreserve_and_mod_dquots(tp);
1565
1566 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
1567 xfs_trans_free(tp);
1568}
1569
1570/*
1571 * Format the transaction direct to the iclog. This isolates the physical
1572 * transaction commit operation from the logical operation and hence allows
1573 * other methods to be introduced without affecting the existing commit path.
1574 */
1575static int
1576xfs_trans_commit_iclog(
1577 struct xfs_mount *mp,
1578 struct xfs_trans *tp,
1579 xfs_lsn_t *commit_lsn,
1580 int flags)
1581{
1582 int shutdown;
1583 int error;
1584 int log_flags = 0;
1585 struct xlog_in_core *commit_iclog;
1586#define XFS_TRANS_LOGVEC_COUNT 16
1587 struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
1588 struct xfs_log_iovec *log_vector;
1589 uint nvec;
1590
1591
1592 /*
1593 * Ask each log item how many log_vector entries it will
1594 * need so we can figure out how many to allocate.
1595 * Try to avoid the kmem_alloc() call in the common case
1596 * by using a vector from the stack when it fits.
1597 */
1598 nvec = xfs_trans_count_vecs(tp);
1599 if (nvec == 0) {
1600 return ENOMEM; /* triggers a shutdown! */
1601 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
1602 log_vector = log_vector_fast;
1603 } else {
1604 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
1605 sizeof(xfs_log_iovec_t),
1606 KM_SLEEP);
1607 }
1608
1609 /*
1610 * Fill in the log_vector and pin the logged items, and
1611 * then write the transaction to the log.
1612 */
1613 xfs_trans_fill_vecs(tp, log_vector);
1614
1615 if (flags & XFS_TRANS_RELEASE_LOG_RES)
1616 log_flags = XFS_LOG_REL_PERM_RESERV;
1617
1618 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
1619
1620 /*
1621 * The transaction is committed incore here, and can go out to disk
1622 * at any time after this call. However, all the items associated
1623 * with the transaction are still locked and pinned in memory.
1624 */
1625 *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
1626
1627 tp->t_commit_lsn = *commit_lsn;
1628 trace_xfs_trans_commit_lsn(tp);
1629
1630 if (nvec > XFS_TRANS_LOGVEC_COUNT)
1631 kmem_free(log_vector);
1632
1633 /*
1634 * If we got a log write error. Unpin the logitems that we
1635 * had pinned, clean up, free trans structure, and return error.
1636 */
1637 if (error || *commit_lsn == -1) {
1638 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1639 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
1640 return XFS_ERROR(EIO);
1641 }
1642
1643 /*
1644 * Once the transaction has committed, unused
1645 * reservations need to be released and changes to
1646 * the superblock need to be reflected in the in-core
1647 * version. Do that now.
1648 */
1649 xfs_trans_unreserve_and_mod_sb(tp);
1650
1651 /*
1652 * Tell the LM to call the transaction completion routine
1653 * when the log write with LSN commit_lsn completes (e.g.
1654 * when the transaction commit really hits the on-disk log).
1655 * After this call we cannot reference tp, because the call
1656 * can happen at any time and the call will free the transaction
1657 * structure pointed to by tp. The only case where we call
1658 * the completion routine (xfs_trans_committed) directly is
1659 * if the log is turned off on a debug kernel or we're
1660 * running in simulation mode (the log is explicitly turned
1661 * off).
1662 */
1663 tp->t_logcb.cb_func = xfs_trans_committed;
1664 tp->t_logcb.cb_arg = tp;
1665
1666 /*
1667 * We need to pass the iclog buffer which was used for the
1668 * transaction commit record into this function, and attach
1669 * the callback to it. The callback must be attached before
1670 * the items are unlocked to avoid racing with other threads
1671 * waiting for an item to unlock.
1672 */
1673 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
1674
1675 /*
1676 * Mark this thread as no longer being in a transaction
1677 */
1678 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1679
1680 /*
1681 * Once all the items of the transaction have been copied
1682 * to the in core log and the callback is attached, the
1683 * items can be unlocked.
1684 *
1685 * This will free descriptors pointing to items which were
1686 * not logged since there is nothing more to do with them.
1687 * For items which were logged, we will keep pointers to them
1688 * so they can be unpinned after the transaction commits to disk.
1689 * This will also stamp each modified meta-data item with
1690 * the commit lsn of this transaction for dependency tracking
1691 * purposes.
1692 */
1693 xfs_trans_unlock_items(tp, *commit_lsn);
1694
1695 /*
1696 * If we detected a log error earlier, finish committing
1697 * the transaction now (unpin log items, etc).
1698 *
1699 * Order is critical here, to avoid using the transaction
1700 * pointer after its been freed (by xfs_trans_committed
1701 * either here now, or as a callback). We cannot do this
1702 * step inside xfs_log_notify as was done earlier because
1703 * of this issue.
1704 */
1705 if (shutdown)
1706 xfs_trans_committed(tp, XFS_LI_ABORTED);
1707
1708 /*
1709 * Now that the xfs_trans_committed callback has been attached,
1710 * and the items are released we can finally allow the iclog to
1711 * go to disk.
1712 */
1713 return xfs_log_release_iclog(mp, commit_iclog);
1714}
1715
1716/*
1717 * Walk the log items and allocate log vector structures for
1718 * each item large enough to fit all the vectors they require.
1719 * Note that this format differs from the old log vector format in
1720 * that there is no transaction header in these log vectors.
1721 */
1722STATIC struct xfs_log_vec *
1723xfs_trans_alloc_log_vecs(
1724 xfs_trans_t *tp)
1725{
1726 struct xfs_log_item_desc *lidp;
1727 struct xfs_log_vec *lv = NULL;
1728 struct xfs_log_vec *ret_lv = NULL;
1729
1730
1731 /* Bail out if we didn't find a log item. */
1732 if (list_empty(&tp->t_items)) {
1733 ASSERT(0);
1734 return NULL;
1735 }
1736
1737 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
1738 struct xfs_log_vec *new_lv;
1739
1740 /* Skip items which aren't dirty in this transaction. */
1741 if (!(lidp->lid_flags & XFS_LID_DIRTY))
1742 continue;
1743
1744 /* Skip items that do not have any vectors for writing */
1745 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1746 if (!lidp->lid_size)
1747 continue;
1748
1749 new_lv = kmem_zalloc(sizeof(*new_lv) +
1750 lidp->lid_size * sizeof(struct xfs_log_iovec),
1751 KM_SLEEP);
1752
1753 /* The allocated iovec region lies beyond the log vector. */
1754 new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
1755 new_lv->lv_niovecs = lidp->lid_size;
1756 new_lv->lv_item = lidp->lid_item;
1757 if (!ret_lv)
1758 ret_lv = new_lv;
1759 else
1760 lv->lv_next = new_lv;
1761 lv = new_lv;
1762 }
1763
1764 return ret_lv;
1765}
1766
1767static int
1768xfs_trans_commit_cil(
1769 struct xfs_mount *mp,
1770 struct xfs_trans *tp,
1771 xfs_lsn_t *commit_lsn,
1772 int flags)
1773{
1774 struct xfs_log_vec *log_vector;
1775
1776 /*
1777 * Get each log item to allocate a vector structure for
1778 * the log item to to pass to the log write code. The
1779 * CIL commit code will format the vector and save it away.
1780 */
1781 log_vector = xfs_trans_alloc_log_vecs(tp);
1782 if (!log_vector)
1783 return ENOMEM;
1784
1785 xfs_log_commit_cil(mp, tp, log_vector, commit_lsn, flags);
1786
1787 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1788 xfs_trans_free(tp);
1789 return 0;
1790}
1791
1792/*
1793 * Commit the given transaction to the log. 1327 * Commit the given transaction to the log.
1794 * 1328 *
1795 * XFS disk error handling mechanism is not based on a typical 1329 * XFS disk error handling mechanism is not based on a typical
@@ -1845,17 +1379,16 @@ xfs_trans_commit(
1845 xfs_trans_apply_sb_deltas(tp); 1379 xfs_trans_apply_sb_deltas(tp);
1846 xfs_trans_apply_dquot_deltas(tp); 1380 xfs_trans_apply_dquot_deltas(tp);
1847 1381
1848 if (mp->m_flags & XFS_MOUNT_DELAYLOG) 1382 error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
1849 error = xfs_trans_commit_cil(mp, tp, &commit_lsn, flags);
1850 else
1851 error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
1852
1853 if (error == ENOMEM) { 1383 if (error == ENOMEM) {
1854 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR); 1384 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1855 error = XFS_ERROR(EIO); 1385 error = XFS_ERROR(EIO);
1856 goto out_unreserve; 1386 goto out_unreserve;
1857 } 1387 }
1858 1388
1389 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1390 xfs_trans_free(tp);
1391
1859 /* 1392 /*
1860 * If the transaction needs to be synchronous, then force the 1393 * If the transaction needs to be synchronous, then force the
1861 * log out now and wait for it. 1394 * log out now and wait for it.