xref: /openbmc/linux/fs/xfs/xfs_quotaops.c (revision 867a0e05)
1 /*
2  * Copyright (c) 2008, Christoph Hellwig
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_format.h"
20 #include "xfs_log_format.h"
21 #include "xfs_trans_resv.h"
22 #include "xfs_mount.h"
23 #include "xfs_inode.h"
24 #include "xfs_quota.h"
25 #include "xfs_trans.h"
26 #include "xfs_qm.h"
27 #include <linux/quota.h>
28 
29 
30 STATIC int
31 xfs_quota_type(int type)
32 {
33 	switch (type) {
34 	case USRQUOTA:
35 		return XFS_DQ_USER;
36 	case GRPQUOTA:
37 		return XFS_DQ_GROUP;
38 	default:
39 		return XFS_DQ_PROJ;
40 	}
41 }
42 
43 STATIC int
44 xfs_fs_get_xstate(
45 	struct super_block	*sb,
46 	struct fs_quota_stat	*fqs)
47 {
48 	struct xfs_mount	*mp = XFS_M(sb);
49 
50 	if (!XFS_IS_QUOTA_RUNNING(mp))
51 		return -ENOSYS;
52 	return xfs_qm_scall_getqstat(mp, fqs);
53 }
54 
55 STATIC int
56 xfs_fs_get_xstatev(
57 	struct super_block	*sb,
58 	struct fs_quota_statv	*fqs)
59 {
60 	struct xfs_mount	*mp = XFS_M(sb);
61 
62 	if (!XFS_IS_QUOTA_RUNNING(mp))
63 		return -ENOSYS;
64 	return xfs_qm_scall_getqstatv(mp, fqs);
65 }
66 
67 STATIC int
68 xfs_fs_set_xstate(
69 	struct super_block	*sb,
70 	unsigned int		uflags,
71 	int			op)
72 {
73 	struct xfs_mount	*mp = XFS_M(sb);
74 	unsigned int		flags = 0;
75 
76 	if (sb->s_flags & MS_RDONLY)
77 		return -EROFS;
78 	if (op != Q_XQUOTARM && !XFS_IS_QUOTA_RUNNING(mp))
79 		return -ENOSYS;
80 
81 	if (uflags & FS_QUOTA_UDQ_ACCT)
82 		flags |= XFS_UQUOTA_ACCT;
83 	if (uflags & FS_QUOTA_PDQ_ACCT)
84 		flags |= XFS_PQUOTA_ACCT;
85 	if (uflags & FS_QUOTA_GDQ_ACCT)
86 		flags |= XFS_GQUOTA_ACCT;
87 	if (uflags & FS_QUOTA_UDQ_ENFD)
88 		flags |= XFS_UQUOTA_ENFD;
89 	if (uflags & FS_QUOTA_GDQ_ENFD)
90 		flags |= XFS_GQUOTA_ENFD;
91 	if (uflags & FS_QUOTA_PDQ_ENFD)
92 		flags |= XFS_PQUOTA_ENFD;
93 
94 	switch (op) {
95 	case Q_XQUOTAON:
96 		return xfs_qm_scall_quotaon(mp, flags);
97 	case Q_XQUOTAOFF:
98 		if (!XFS_IS_QUOTA_ON(mp))
99 			return -EINVAL;
100 		return xfs_qm_scall_quotaoff(mp, flags);
101 	}
102 
103 	return -EINVAL;
104 }
105 
106 STATIC int
107 xfs_fs_rm_xquota(
108 	struct super_block	*sb,
109 	unsigned int		uflags)
110 {
111 	struct xfs_mount	*mp = XFS_M(sb);
112 	unsigned int		flags = 0;
113 
114 	if (sb->s_flags & MS_RDONLY)
115 		return -EROFS;
116 
117 	if (XFS_IS_QUOTA_ON(mp))
118 		return -EINVAL;
119 
120 	if (uflags & FS_USER_QUOTA)
121 		flags |= XFS_DQ_USER;
122 	if (uflags & FS_GROUP_QUOTA)
123 		flags |= XFS_DQ_GROUP;
124 	if (uflags & FS_PROJ_QUOTA)
125 		flags |= XFS_DQ_PROJ;
126 
127 	return xfs_qm_scall_trunc_qfiles(mp, flags);
128 }
129 
130 STATIC int
131 xfs_fs_get_dqblk(
132 	struct super_block	*sb,
133 	struct kqid		qid,
134 	struct fs_disk_quota	*fdq)
135 {
136 	struct xfs_mount	*mp = XFS_M(sb);
137 
138 	if (!XFS_IS_QUOTA_RUNNING(mp))
139 		return -ENOSYS;
140 	if (!XFS_IS_QUOTA_ON(mp))
141 		return -ESRCH;
142 
143 	return xfs_qm_scall_getquota(mp, from_kqid(&init_user_ns, qid),
144 				      xfs_quota_type(qid.type), fdq);
145 }
146 
147 STATIC int
148 xfs_fs_set_dqblk(
149 	struct super_block	*sb,
150 	struct kqid		qid,
151 	struct fs_disk_quota	*fdq)
152 {
153 	struct xfs_mount	*mp = XFS_M(sb);
154 
155 	if (sb->s_flags & MS_RDONLY)
156 		return -EROFS;
157 	if (!XFS_IS_QUOTA_RUNNING(mp))
158 		return -ENOSYS;
159 	if (!XFS_IS_QUOTA_ON(mp))
160 		return -ESRCH;
161 
162 	return xfs_qm_scall_setqlim(mp, from_kqid(&init_user_ns, qid),
163 				     xfs_quota_type(qid.type), fdq);
164 }
165 
166 const struct quotactl_ops xfs_quotactl_operations = {
167 	.get_xstatev		= xfs_fs_get_xstatev,
168 	.get_xstate		= xfs_fs_get_xstate,
169 	.set_xstate		= xfs_fs_set_xstate,
170 	.rm_xquota		= xfs_fs_rm_xquota,
171 	.get_dqblk		= xfs_fs_get_dqblk,
172 	.set_dqblk		= xfs_fs_set_dqblk,
173 };
174