aboutsummaryrefslogtreecommitdiffstats
path: root/fs/quota/dquot.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/quota/dquot.c')
-rw-r--r--fs/quota/dquot.c291
1 files changed, 159 insertions, 132 deletions
diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index cd6bb9a33c13..3fc62b097bed 100644
--- a/fs/quota/dquot.c
+++ b/fs/quota/dquot.c
@@ -323,6 +323,30 @@ int dquot_mark_dquot_dirty(struct dquot *dquot)
323} 323}
324EXPORT_SYMBOL(dquot_mark_dquot_dirty); 324EXPORT_SYMBOL(dquot_mark_dquot_dirty);
325 325
326/* Dirtify all the dquots - this can block when journalling */
327static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
328{
329 int ret, err, cnt;
330
331 ret = err = 0;
332 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
333 if (dquot[cnt])
334 /* Even in case of error we have to continue */
335 ret = mark_dquot_dirty(dquot[cnt]);
336 if (!err)
337 err = ret;
338 }
339 return err;
340}
341
342static inline void dqput_all(struct dquot **dquot)
343{
344 unsigned int cnt;
345
346 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
347 dqput(dquot[cnt]);
348}
349
326/* This function needs dq_list_lock */ 350/* This function needs dq_list_lock */
327static inline int clear_dquot_dirty(struct dquot *dquot) 351static inline int clear_dquot_dirty(struct dquot *dquot)
328{ 352{
@@ -1268,8 +1292,7 @@ int dquot_initialize(struct inode *inode, int type)
1268out_err: 1292out_err:
1269 up_write(&sb_dqopt(sb)->dqptr_sem); 1293 up_write(&sb_dqopt(sb)->dqptr_sem);
1270 /* Drop unused references */ 1294 /* Drop unused references */
1271 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 1295 dqput_all(got);
1272 dqput(got[cnt]);
1273 return ret; 1296 return ret;
1274} 1297}
1275EXPORT_SYMBOL(dquot_initialize); 1298EXPORT_SYMBOL(dquot_initialize);
@@ -1288,9 +1311,7 @@ int dquot_drop(struct inode *inode)
1288 inode->i_dquot[cnt] = NULL; 1311 inode->i_dquot[cnt] = NULL;
1289 } 1312 }
1290 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); 1313 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1291 1314 dqput_all(put);
1292 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1293 dqput(put[cnt]);
1294 return 0; 1315 return 0;
1295} 1316}
1296EXPORT_SYMBOL(dquot_drop); 1317EXPORT_SYMBOL(dquot_drop);
@@ -1319,6 +1340,70 @@ void vfs_dq_drop(struct inode *inode)
1319EXPORT_SYMBOL(vfs_dq_drop); 1340EXPORT_SYMBOL(vfs_dq_drop);
1320 1341
1321/* 1342/*
1343 * inode_reserved_space is managed internally by quota, and protected by
1344 * i_lock similar to i_blocks+i_bytes.
1345 */
1346static qsize_t *inode_reserved_space(struct inode * inode)
1347{
1348 /* Filesystem must explicitly define it's own method in order to use
1349 * quota reservation interface */
1350 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1351 return inode->i_sb->dq_op->get_reserved_space(inode);
1352}
1353
1354static void inode_add_rsv_space(struct inode *inode, qsize_t number)
1355{
1356 spin_lock(&inode->i_lock);
1357 *inode_reserved_space(inode) += number;
1358 spin_unlock(&inode->i_lock);
1359}
1360
1361
1362static void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1363{
1364 spin_lock(&inode->i_lock);
1365 *inode_reserved_space(inode) -= number;
1366 __inode_add_bytes(inode, number);
1367 spin_unlock(&inode->i_lock);
1368}
1369
1370static void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1371{
1372 spin_lock(&inode->i_lock);
1373 *inode_reserved_space(inode) -= number;
1374 spin_unlock(&inode->i_lock);
1375}
1376
1377static qsize_t inode_get_rsv_space(struct inode *inode)
1378{
1379 qsize_t ret;
1380
1381 if (!inode->i_sb->dq_op->get_reserved_space)
1382 return 0;
1383 spin_lock(&inode->i_lock);
1384 ret = *inode_reserved_space(inode);
1385 spin_unlock(&inode->i_lock);
1386 return ret;
1387}
1388
1389static void inode_incr_space(struct inode *inode, qsize_t number,
1390 int reserve)
1391{
1392 if (reserve)
1393 inode_add_rsv_space(inode, number);
1394 else
1395 inode_add_bytes(inode, number);
1396}
1397
1398static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1399{
1400 if (reserve)
1401 inode_sub_rsv_space(inode, number);
1402 else
1403 inode_sub_bytes(inode, number);
1404}
1405
1406/*
1322 * Following four functions update i_blocks+i_bytes fields and 1407 * Following four functions update i_blocks+i_bytes fields and
1323 * quota information (together with appropriate checks) 1408 * quota information (together with appropriate checks)
1324 * NOTE: We absolutely rely on the fact that caller dirties 1409 * NOTE: We absolutely rely on the fact that caller dirties
@@ -1336,6 +1421,21 @@ int __dquot_alloc_space(struct inode *inode, qsize_t number,
1336 int cnt, ret = QUOTA_OK; 1421 int cnt, ret = QUOTA_OK;
1337 char warntype[MAXQUOTAS]; 1422 char warntype[MAXQUOTAS];
1338 1423
1424 /*
1425 * First test before acquiring mutex - solves deadlocks when we
1426 * re-enter the quota code and are already holding the mutex
1427 */
1428 if (IS_NOQUOTA(inode)) {
1429 inode_incr_space(inode, number, reserve);
1430 goto out;
1431 }
1432
1433 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1434 if (IS_NOQUOTA(inode)) {
1435 inode_incr_space(inode, number, reserve);
1436 goto out_unlock;
1437 }
1438
1339 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 1439 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1340 warntype[cnt] = QUOTA_NL_NOWARN; 1440 warntype[cnt] = QUOTA_NL_NOWARN;
1341 1441
@@ -1346,7 +1446,8 @@ int __dquot_alloc_space(struct inode *inode, qsize_t number,
1346 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) 1446 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt)
1347 == NO_QUOTA) { 1447 == NO_QUOTA) {
1348 ret = NO_QUOTA; 1448 ret = NO_QUOTA;
1349 goto out_unlock; 1449 spin_unlock(&dq_data_lock);
1450 goto out_flush_warn;
1350 } 1451 }
1351 } 1452 }
1352 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1453 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
@@ -1357,64 +1458,29 @@ int __dquot_alloc_space(struct inode *inode, qsize_t number,
1357 else 1458 else
1358 dquot_incr_space(inode->i_dquot[cnt], number); 1459 dquot_incr_space(inode->i_dquot[cnt], number);
1359 } 1460 }
1360 if (!reserve) 1461 inode_incr_space(inode, number, reserve);
1361 inode_add_bytes(inode, number);
1362out_unlock:
1363 spin_unlock(&dq_data_lock); 1462 spin_unlock(&dq_data_lock);
1463
1464 if (reserve)
1465 goto out_flush_warn;
1466 mark_all_dquot_dirty(inode->i_dquot);
1467out_flush_warn:
1364 flush_warnings(inode->i_dquot, warntype); 1468 flush_warnings(inode->i_dquot, warntype);
1469out_unlock:
1470 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1471out:
1365 return ret; 1472 return ret;
1366} 1473}
1367 1474
1368int dquot_alloc_space(struct inode *inode, qsize_t number, int warn) 1475int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1369{ 1476{
1370 int cnt, ret = QUOTA_OK; 1477 return __dquot_alloc_space(inode, number, warn, 0);
1371
1372 /*
1373 * First test before acquiring mutex - solves deadlocks when we
1374 * re-enter the quota code and are already holding the mutex
1375 */
1376 if (IS_NOQUOTA(inode)) {
1377 inode_add_bytes(inode, number);
1378 goto out;
1379 }
1380
1381 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1382 if (IS_NOQUOTA(inode)) {
1383 inode_add_bytes(inode, number);
1384 goto out_unlock;
1385 }
1386
1387 ret = __dquot_alloc_space(inode, number, warn, 0);
1388 if (ret == NO_QUOTA)
1389 goto out_unlock;
1390
1391 /* Dirtify all the dquots - this can block when journalling */
1392 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1393 if (inode->i_dquot[cnt])
1394 mark_dquot_dirty(inode->i_dquot[cnt]);
1395out_unlock:
1396 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1397out:
1398 return ret;
1399} 1478}
1400EXPORT_SYMBOL(dquot_alloc_space); 1479EXPORT_SYMBOL(dquot_alloc_space);
1401 1480
1402int dquot_reserve_space(struct inode *inode, qsize_t number, int warn) 1481int dquot_reserve_space(struct inode *inode, qsize_t number, int warn)
1403{ 1482{
1404 int ret = QUOTA_OK; 1483 return __dquot_alloc_space(inode, number, warn, 1);
1405
1406 if (IS_NOQUOTA(inode))
1407 goto out;
1408
1409 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1410 if (IS_NOQUOTA(inode))
1411 goto out_unlock;
1412
1413 ret = __dquot_alloc_space(inode, number, warn, 1);
1414out_unlock:
1415 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1416out:
1417 return ret;
1418} 1484}
1419EXPORT_SYMBOL(dquot_reserve_space); 1485EXPORT_SYMBOL(dquot_reserve_space);
1420 1486
@@ -1455,10 +1521,7 @@ int dquot_alloc_inode(const struct inode *inode, qsize_t number)
1455warn_put_all: 1521warn_put_all:
1456 spin_unlock(&dq_data_lock); 1522 spin_unlock(&dq_data_lock);
1457 if (ret == QUOTA_OK) 1523 if (ret == QUOTA_OK)
1458 /* Dirtify all the dquots - this can block when journalling */ 1524 mark_all_dquot_dirty(inode->i_dquot);
1459 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1460 if (inode->i_dquot[cnt])
1461 mark_dquot_dirty(inode->i_dquot[cnt]);
1462 flush_warnings(inode->i_dquot, warntype); 1525 flush_warnings(inode->i_dquot, warntype);
1463 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); 1526 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1464 return ret; 1527 return ret;
@@ -1471,14 +1534,14 @@ int dquot_claim_space(struct inode *inode, qsize_t number)
1471 int ret = QUOTA_OK; 1534 int ret = QUOTA_OK;
1472 1535
1473 if (IS_NOQUOTA(inode)) { 1536 if (IS_NOQUOTA(inode)) {
1474 inode_add_bytes(inode, number); 1537 inode_claim_rsv_space(inode, number);
1475 goto out; 1538 goto out;
1476 } 1539 }
1477 1540
1478 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem); 1541 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1479 if (IS_NOQUOTA(inode)) { 1542 if (IS_NOQUOTA(inode)) {
1480 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); 1543 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1481 inode_add_bytes(inode, number); 1544 inode_claim_rsv_space(inode, number);
1482 goto out; 1545 goto out;
1483 } 1546 }
1484 1547
@@ -1490,12 +1553,9 @@ int dquot_claim_space(struct inode *inode, qsize_t number)
1490 number); 1553 number);
1491 } 1554 }
1492 /* Update inode bytes */ 1555 /* Update inode bytes */
1493 inode_add_bytes(inode, number); 1556 inode_claim_rsv_space(inode, number);
1494 spin_unlock(&dq_data_lock); 1557 spin_unlock(&dq_data_lock);
1495 /* Dirtify all the dquots - this can block when journalling */ 1558 mark_all_dquot_dirty(inode->i_dquot);
1496 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1497 if (inode->i_dquot[cnt])
1498 mark_dquot_dirty(inode->i_dquot[cnt]);
1499 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); 1559 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1500out: 1560out:
1501 return ret; 1561 return ret;
@@ -1503,38 +1563,9 @@ out:
1503EXPORT_SYMBOL(dquot_claim_space); 1563EXPORT_SYMBOL(dquot_claim_space);
1504 1564
1505/* 1565/*
1506 * Release reserved quota space
1507 */
1508void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1509{
1510 int cnt;
1511
1512 if (IS_NOQUOTA(inode))
1513 goto out;
1514
1515 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1516 if (IS_NOQUOTA(inode))
1517 goto out_unlock;
1518
1519 spin_lock(&dq_data_lock);
1520 /* Release reserved dquots */
1521 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1522 if (inode->i_dquot[cnt])
1523 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1524 }
1525 spin_unlock(&dq_data_lock);
1526
1527out_unlock:
1528 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1529out:
1530 return;
1531}
1532EXPORT_SYMBOL(dquot_release_reserved_space);
1533
1534/*
1535 * This operation can block, but only after everything is updated 1566 * This operation can block, but only after everything is updated
1536 */ 1567 */
1537int dquot_free_space(struct inode *inode, qsize_t number) 1568int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
1538{ 1569{
1539 unsigned int cnt; 1570 unsigned int cnt;
1540 char warntype[MAXQUOTAS]; 1571 char warntype[MAXQUOTAS];
@@ -1543,7 +1574,7 @@ int dquot_free_space(struct inode *inode, qsize_t number)
1543 * re-enter the quota code and are already holding the mutex */ 1574 * re-enter the quota code and are already holding the mutex */
1544 if (IS_NOQUOTA(inode)) { 1575 if (IS_NOQUOTA(inode)) {
1545out_sub: 1576out_sub:
1546 inode_sub_bytes(inode, number); 1577 inode_decr_space(inode, number, reserve);
1547 return QUOTA_OK; 1578 return QUOTA_OK;
1548 } 1579 }
1549 1580
@@ -1558,21 +1589,40 @@ out_sub:
1558 if (!inode->i_dquot[cnt]) 1589 if (!inode->i_dquot[cnt])
1559 continue; 1590 continue;
1560 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number); 1591 warntype[cnt] = info_bdq_free(inode->i_dquot[cnt], number);
1561 dquot_decr_space(inode->i_dquot[cnt], number); 1592 if (reserve)
1593 dquot_free_reserved_space(inode->i_dquot[cnt], number);
1594 else
1595 dquot_decr_space(inode->i_dquot[cnt], number);
1562 } 1596 }
1563 inode_sub_bytes(inode, number); 1597 inode_decr_space(inode, number, reserve);
1564 spin_unlock(&dq_data_lock); 1598 spin_unlock(&dq_data_lock);
1565 /* Dirtify all the dquots - this can block when journalling */ 1599
1566 for (cnt = 0; cnt < MAXQUOTAS; cnt++) 1600 if (reserve)
1567 if (inode->i_dquot[cnt]) 1601 goto out_unlock;
1568 mark_dquot_dirty(inode->i_dquot[cnt]); 1602 mark_all_dquot_dirty(inode->i_dquot);
1603out_unlock:
1569 flush_warnings(inode->i_dquot, warntype); 1604 flush_warnings(inode->i_dquot, warntype);
1570 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); 1605 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1571 return QUOTA_OK; 1606 return QUOTA_OK;
1572} 1607}
1608
1609int dquot_free_space(struct inode *inode, qsize_t number)
1610{
1611 return __dquot_free_space(inode, number, 0);
1612}
1573EXPORT_SYMBOL(dquot_free_space); 1613EXPORT_SYMBOL(dquot_free_space);
1574 1614
1575/* 1615/*
1616 * Release reserved quota space
1617 */
1618void dquot_release_reserved_space(struct inode *inode, qsize_t number)
1619{
1620 __dquot_free_space(inode, number, 1);
1621
1622}
1623EXPORT_SYMBOL(dquot_release_reserved_space);
1624
1625/*
1576 * This operation can block, but only after everything is updated 1626 * This operation can block, but only after everything is updated
1577 */ 1627 */
1578int dquot_free_inode(const struct inode *inode, qsize_t number) 1628int dquot_free_inode(const struct inode *inode, qsize_t number)
@@ -1599,10 +1649,7 @@ int dquot_free_inode(const struct inode *inode, qsize_t number)
1599 dquot_decr_inodes(inode->i_dquot[cnt], number); 1649 dquot_decr_inodes(inode->i_dquot[cnt], number);
1600 } 1650 }
1601 spin_unlock(&dq_data_lock); 1651 spin_unlock(&dq_data_lock);
1602 /* Dirtify all the dquots - this can block when journalling */ 1652 mark_all_dquot_dirty(inode->i_dquot);
1603 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1604 if (inode->i_dquot[cnt])
1605 mark_dquot_dirty(inode->i_dquot[cnt]);
1606 flush_warnings(inode->i_dquot, warntype); 1653 flush_warnings(inode->i_dquot, warntype);
1607 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem); 1654 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1608 return QUOTA_OK; 1655 return QUOTA_OK;
@@ -1610,19 +1657,6 @@ int dquot_free_inode(const struct inode *inode, qsize_t number)
1610EXPORT_SYMBOL(dquot_free_inode); 1657EXPORT_SYMBOL(dquot_free_inode);
1611 1658
1612/* 1659/*
1613 * call back function, get reserved quota space from underlying fs
1614 */
1615qsize_t dquot_get_reserved_space(struct inode *inode)
1616{
1617 qsize_t reserved_space = 0;
1618
1619 if (sb_any_quota_active(inode->i_sb) &&
1620 inode->i_sb->dq_op->get_reserved_space)
1621 reserved_space = inode->i_sb->dq_op->get_reserved_space(inode);
1622 return reserved_space;
1623}
1624
1625/*
1626 * Transfer the number of inode and blocks from one diskquota to an other. 1660 * Transfer the number of inode and blocks from one diskquota to an other.
1627 * 1661 *
1628 * This operation can block, but only after everything is updated 1662 * This operation can block, but only after everything is updated
@@ -1665,7 +1699,7 @@ int dquot_transfer(struct inode *inode, struct iattr *iattr)
1665 } 1699 }
1666 spin_lock(&dq_data_lock); 1700 spin_lock(&dq_data_lock);
1667 cur_space = inode_get_bytes(inode); 1701 cur_space = inode_get_bytes(inode);
1668 rsv_space = dquot_get_reserved_space(inode); 1702 rsv_space = inode_get_rsv_space(inode);
1669 space = cur_space + rsv_space; 1703 space = cur_space + rsv_space;
1670 /* Build the transfer_from list and check the limits */ 1704 /* Build the transfer_from list and check the limits */
1671 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1705 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
@@ -1709,25 +1743,18 @@ int dquot_transfer(struct inode *inode, struct iattr *iattr)
1709 spin_unlock(&dq_data_lock); 1743 spin_unlock(&dq_data_lock);
1710 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem); 1744 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1711 1745
1712 /* Dirtify all the dquots - this can block when journalling */ 1746 mark_all_dquot_dirty(transfer_from);
1713 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1747 mark_all_dquot_dirty(transfer_to);
1714 if (transfer_from[cnt]) 1748 /* The reference we got is transferred to the inode */
1715 mark_dquot_dirty(transfer_from[cnt]); 1749 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1716 if (transfer_to[cnt]) { 1750 transfer_to[cnt] = NULL;
1717 mark_dquot_dirty(transfer_to[cnt]);
1718 /* The reference we got is transferred to the inode */
1719 transfer_to[cnt] = NULL;
1720 }
1721 }
1722warn_put_all: 1751warn_put_all:
1723 flush_warnings(transfer_to, warntype_to); 1752 flush_warnings(transfer_to, warntype_to);
1724 flush_warnings(transfer_from, warntype_from_inodes); 1753 flush_warnings(transfer_from, warntype_from_inodes);
1725 flush_warnings(transfer_from, warntype_from_space); 1754 flush_warnings(transfer_from, warntype_from_space);
1726put_all: 1755put_all:
1727 for (cnt = 0; cnt < MAXQUOTAS; cnt++) { 1756 dqput_all(transfer_from);
1728 dqput(transfer_from[cnt]); 1757 dqput_all(transfer_to);
1729 dqput(transfer_to[cnt]);
1730 }
1731 return ret; 1758 return ret;
1732over_quota: 1759over_quota:
1733 spin_unlock(&dq_data_lock); 1760 spin_unlock(&dq_data_lock);