summaryrefslogtreecommitdiff
path: root/quotaon_xfs.c
blob: d137240785d7efa157e75e5e4426030c24bff58d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 *	State changes for the XFS Quota Manager.
 *	Copyright (c) 2001 Silicon Graphics, Inc.
 */

#include "config.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "quotaon.h"
#include "dqblk_xfs.h"

#define QOFF	1
#define ACCT	2
#define ENFD	3

/*
 *	Ensure we don't attempt to go into a dodgey state.
 */

static int xfs_state_check(int qcmd, int type, int flags, const char *dev, int roothack, int xopts)
{
	struct xfs_mem_dqinfo info;
	int state;
	char *acctstr = "";

	/* we never want to operate via -a in XFS quota */
	if (flags & STATEFLAG_ALL)
		return 0;	/* noop */

	if (quotactl(QCMD(Q_XFS_GETQSTAT, type), dev, 0, (void *)&info) < 0) {
		errstr(_("quotactl() on %s: %s\n"), dev, strerror(errno));
		return -1;
	}

	/* establish current state before any transition */
	state = QOFF;
	if (type == USRQUOTA) {
		if (info.qs_flags & XFS_QUOTA_UDQ_ACCT)
			state = ACCT;
		if (info.qs_flags & XFS_QUOTA_UDQ_ENFD)
			state = ENFD;
	}
	else if (type == GRPQUOTA) {
		if (info.qs_flags & XFS_QUOTA_GDQ_ACCT)
			state = ACCT;
		if (info.qs_flags & XFS_QUOTA_GDQ_ENFD)
			state = ENFD;
	}
	else if (type == PRJQUOTA) {
		if (info.qs_flags & XFS_QUOTA_PDQ_ACCT)
			state = ACCT;
		if (info.qs_flags & XFS_QUOTA_PDQ_ENFD)
			state = ENFD;
	}

	switch (state) {
	  case QOFF:
		  switch (qcmd) {
		    case Q_XFS_QUOTARM:
			    return 1;
		    case Q_XFS_QUOTAON:
			    if (roothack) {
				    pinfo(_("Enabling %s quota on root filesystem"
					    " (reboot to take effect)\n"), _(type2name(type)));
				    return 1;
			    }
			    errstr(_("Enable XFS %s quota accounting during mount\n"),
				    _(type2name(type)));
			    return -1;
		    case Q_XFS_QUOTAOFF:
			    return 0;	/* noop */
		  }
		  break;
	  case ACCT:
		  switch (qcmd) {
		    case Q_XFS_QUOTARM:
			    errstr(_("Cannot delete %s quota on %s - "
					      "switch quota accounting off first\n"),
				    _(type2name(type)), dev);
			    return -1;
		    case Q_XFS_QUOTAON:
			    if (roothack) {
				    pinfo(_("Enabling %s quota on root filesystem"
					    " (reboot to take effect)\n"), _(type2name(type)));
				    return 1;
			    }
			    if (xopts & XFS_QUOTA_UDQ_ENFD ||
				xopts & XFS_QUOTA_GDQ_ENFD ||
				xopts & XFS_QUOTA_PDQ_ENFD) {
				    pinfo(_("Enabling %s quota enforcement on %s\n"), _(type2name(type)), dev);
				    return 1;
			    }
			    errstr(_("Already accounting %s quota on %s\n"),
					_(type2name(type)), dev);
			    return -1;
		    case Q_XFS_QUOTAOFF:
			    if (xopts & XFS_QUOTA_UDQ_ACCT ||
				xopts & XFS_QUOTA_GDQ_ACCT ||
				xopts & XFS_QUOTA_PDQ_ACCT) {
				    pinfo(_("Disabling %s quota accounting on %s\n"),
					   _(type2name(type)), dev);
			    	    return 1;
			    }
			    errstr(_("Quota enforcement already disabled for %s on %s\n"),
					_(type2name(type)), dev);
			    return -1;
		  }
		  break;

	  case ENFD:
		  switch (qcmd) {
		    case Q_XFS_QUOTARM:
			    errstr(_("Cannot delete %s quota on %s - "
				      "switch quota enforcement and accounting off first\n"),
				    _(type2name(type)), dev);
			    return -1;
		    case Q_XFS_QUOTAON:
			    errstr(_("Enforcing %s quota already on %s\n"),
				    _(type2name(type)), dev);
			    return -1;
		    case Q_XFS_QUOTAOFF:
			    if (xopts == XFS_QUOTA_UDQ_ACCT ||
				xopts == XFS_QUOTA_GDQ_ACCT ||
				xopts == XFS_QUOTA_PDQ_ACCT) {
				    errstr(_("Cannot switch off %s quota "
					"accounting on %s when enforcement is on\n"),
					_(type2name(type)), dev);
				    return -1;
			    }
			    if (xopts & XFS_QUOTA_UDQ_ACCT ||
				xopts & XFS_QUOTA_GDQ_ACCT ||
				xopts & XFS_QUOTA_PDQ_ACCT)
		    		    acctstr = _("and accounting ");
			    pinfo(_("Disabling %s quota enforcement %son %s\n"),
				  _(type2name(type)), acctstr, dev);
			    return 1;
		  }
		  break;
	}
	errstr(_("Unexpected XFS quota state sought on %s\n"), dev);
	return -1;
}

static int xfs_onoff(const char *dev, int type, int flags, int roothack, int xopts)
{
	int qoff, qcmd, check;

	qoff = (flags & STATEFLAG_OFF);
	qcmd = qoff ? Q_XFS_QUOTAOFF : Q_XFS_QUOTAON;
	check = xfs_state_check(qcmd, type, flags, dev, roothack, xopts);
	if (check != 1)
		return (check < 0);

	if (quotactl(QCMD(qcmd, type), dev, 0, (void *)&xopts) < 0) {
		errstr(_("quotactl on %s: %s\n"), dev, strerror(errno));
		return 1;
	}
	if (qoff)
		pinfo(_("%s: %s quotas turned off\n"), dev, _(type2name(type)));
	else
		pinfo(_("%s: %s quotas turned on\n"), dev, _(type2name(type)));
	return 0;
}

static int xfs_delete(const char *dev, int type, int flags, int roothack, int xopts)
{
	int qcmd, check;

	qcmd = Q_XFS_QUOTARM;
	check = xfs_state_check(qcmd, type, flags, dev, roothack, xopts);
	if (check != 1)
		return (check < 0);

	if (quotactl(QCMD(qcmd, type), dev, 0, (void *)&xopts) < 0) {
		errstr(_("Failed to delete quota: %s\n"),
			strerror(errno));
		return 1;
	}

	pinfo(_("%s: deleted %s quota blocks\n"), dev, _(type2name(type)));
	return 0;
}

/*
 *	Change state for given filesystem - on/off, acct/enfd, & delete.
 *	Must consider existing state and also whether or not this is the
 *	root filesystem.
 *	We are passed in the new requested state through "type" & "xarg".
 */
int xfs_newstate(struct mount_entry *mnt, int type, char *xarg, int flags)
{
	int err = 1;
	int xopts = 0;
	int roothack = 0;

#ifdef XFS_ROOTHACK
	/*
	 * Old XFS filesystems (up to XFS 1.2 / Linux 2.5.47) had a
	 * hack to allow enabling quota on the root filesystem without
	 * having to specify it at mount time.
	 */
	if ((strcmp(mnt->me_dir, "/") == 0)) {
		struct xfs_mem_dqinfo info;
		u_int16_t sbflags = 0;

		if (!quotactl(QCMD(Q_XFS_GETQSTAT, type), mnt->me_devname, 0, (void *)&info))
			sbflags = (info.qs_flags & 0xff00) >> 8;

		if ((type == USRQUOTA && (sbflags & XFS_QUOTA_UDQ_ACCT)) &&
		    (type == GRPQUOTA && (sbflags & XFS_QUOTA_GDQ_ACCT)))
			roothack = 1;
	}
#endif /* XFS_ROOTHACK */

	if (xarg == NULL || strcmp(xarg, "enforce") == 0) {	/* only enfd on/off */
		if (type == USRQUOTA)
			xopts |= XFS_QUOTA_UDQ_ENFD;
		else if (type == GRPQUOTA)
			xopts |= XFS_QUOTA_GDQ_ENFD;
		else if (type == PRJQUOTA)
			xopts |= XFS_QUOTA_PDQ_ENFD;
		err = xfs_onoff(mnt->me_devname, type, flags, roothack, xopts);
	}
	else if (strcmp(xarg, "account") == 0) {
		if (type == USRQUOTA)
			xopts |= XFS_QUOTA_UDQ_ACCT;
		else if (type == GRPQUOTA)
			xopts |= XFS_QUOTA_GDQ_ACCT;
		else if (type == PRJQUOTA)
			xopts |= XFS_QUOTA_PDQ_ACCT;
		err = xfs_onoff(mnt->me_devname, type, flags, roothack, xopts);
	}
	else if (strcmp(xarg, "delete") == 0) {
		if (type == USRQUOTA)
			xopts |= XFS_USER_QUOTA;
		else if (type == GRPQUOTA)
			xopts |= XFS_GROUP_QUOTA;
		else if (type == PRJQUOTA)
			xopts |= XFS_PROJ_QUOTA;
		err = xfs_delete(mnt->me_devname, type, flags, roothack, xopts);
	}
	else
		die(1, _("Invalid argument \"%s\"\n"), xarg);
	return err;
}