xfs_qm_bhv.c 4.4 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17 18 19 20 21 22
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_log.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
23
#include "xfs_ag.h"
L
Linus Torvalds 已提交
24 25 26 27 28
#include "xfs_alloc.h"
#include "xfs_quota.h"
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
#include "xfs_inode.h"
29
#include "xfs_itable.h"
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37
#include "xfs_bmap.h"
#include "xfs_rtalloc.h"
#include "xfs_error.h"
#include "xfs_attr.h"
#include "xfs_buf_item.h"
#include "xfs_qm.h"


38 39
STATIC void
xfs_fill_statvfs_from_dquot(
40
	struct kstatfs		*statp,
41
	struct xfs_dquot	*dqp)
42 43 44
{
	__uint64_t		limit;

45 46 47
	limit = dqp->q_core.d_blk_softlimit ?
		be64_to_cpu(dqp->q_core.d_blk_softlimit) :
		be64_to_cpu(dqp->q_core.d_blk_hardlimit);
48 49
	if (limit && statp->f_blocks > limit) {
		statp->f_blocks = limit;
50
		statp->f_bfree = statp->f_bavail =
51 52
			(statp->f_blocks > dqp->q_res_bcount) ?
			 (statp->f_blocks - dqp->q_res_bcount) : 0;
53
	}
54

55 56 57
	limit = dqp->q_core.d_ino_softlimit ?
		be64_to_cpu(dqp->q_core.d_ino_softlimit) :
		be64_to_cpu(dqp->q_core.d_ino_hardlimit);
58 59
	if (limit && statp->f_files > limit) {
		statp->f_files = limit;
60
		statp->f_ffree =
61 62
			(statp->f_files > dqp->q_res_icount) ?
			 (statp->f_ffree - dqp->q_res_icount) : 0;
63 64 65
	}
}

66 67 68 69 70 71 72 73

/*
 * Directory tree accounting is implemented using project quotas, where
 * the project identifier is inherited from parent directories.
 * A statvfs (df, etc.) of a directory that is using project quota should
 * return a statvfs of the project, not the entire filesystem.
 * This makes such trees appear as if they are filesystems in themselves.
 */
C
Christoph Hellwig 已提交
74
void
75 76
xfs_qm_statvfs(
	xfs_inode_t		*ip,
77
	struct kstatfs		*statp)
L
Linus Torvalds 已提交
78
{
79 80
	xfs_mount_t		*mp = ip->i_mount;
	xfs_dquot_t		*dqp;
L
Linus Torvalds 已提交
81

82
	if (!xfs_qm_dqget(mp, NULL, xfs_get_projid(ip), XFS_DQ_PROJ, 0, &dqp)) {
83
		xfs_fill_statvfs_from_dquot(statp, dqp);
84
		xfs_qm_dqput(dqp);
L
Linus Torvalds 已提交
85 86 87
	}
}

C
Christoph Hellwig 已提交
88
int
L
Linus Torvalds 已提交
89 90 91 92 93 94
xfs_qm_newmount(
	xfs_mount_t	*mp,
	uint		*needquotamount,
	uint		*quotaflags)
{
	uint		quotaondisk;
95
	uint		uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
L
Linus Torvalds 已提交
96

97
	quotaondisk = xfs_sb_version_hasquota(&mp->m_sb) &&
98
				(mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
L
Linus Torvalds 已提交
99 100 101

	if (quotaondisk) {
		uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
102
		pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
		gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
	}

	/*
	 * If the device itself is read-only, we can't allow
	 * the user to change the state of quota on the mount -
	 * this would generate a transaction on the ro device,
	 * which would lead to an I/O error and shutdown
	 */

	if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
	    (!uquotaondisk &&  XFS_IS_UQUOTA_ON(mp)) ||
	     (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
116 117 118
	    (!gquotaondisk &&  XFS_IS_GQUOTA_ON(mp)) ||
	     (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
	    (!pquotaondisk &&  XFS_IS_PQUOTA_ON(mp)))  &&
L
Linus Torvalds 已提交
119
	    xfs_dev_is_read_only(mp, "changing quota state")) {
120
		xfs_warn(mp, "please mount with%s%s%s%s.",
L
Linus Torvalds 已提交
121 122
			(!quotaondisk ? "out quota" : ""),
			(uquotaondisk ? " usrquota" : ""),
123 124
			(gquotaondisk ? " grpquota" : ""),
			(pquotaondisk ? " prjquota" : ""));
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134
		return XFS_ERROR(EPERM);
	}

	if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
		/*
		 * Call mount_quotas at this point only if we won't have to do
		 * a quotacheck.
		 */
		if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
			/*
L
Lucas De Marchi 已提交
135
			 * If an error occurred, qm_mount_quotas code
L
Linus Torvalds 已提交
136 137 138 139
			 * has already disabled quotas. So, just finish
			 * mounting, and get on with the boring life
			 * without disk quotas.
			 */
C
Christoph Hellwig 已提交
140
			xfs_qm_mount_quotas(mp);
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148
		} else {
			/*
			 * Clear the quota flags, but remember them. This
			 * is so that the quota code doesn't get invoked
			 * before we're ready. This can happen when an
			 * inode goes inactive and wants to free blocks,
			 * or via xfs_log_mount_finish.
			 */
149
			*needquotamount = true;
L
Linus Torvalds 已提交
150 151 152 153 154 155 156
			*quotaflags = mp->m_qflags;
			mp->m_qflags = 0;
		}
	}

	return 0;
}