Whamcloud - gitweb
LU-1818 quota: en/disable quota enforcement via conf_param
[fs/lustre-release.git] / lustre / quota / quota_ctl.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LQUOTA
38
39 #ifdef __KERNEL__
40 # include <linux/version.h>
41 # include <linux/module.h>
42 # include <linux/init.h>
43 # include <linux/fs.h>
44 # include <linux/jbd.h>
45 # include <linux/quota.h>
46 # include <linux/buffer_head.h>
47 # include <linux/workqueue.h>
48 # include <linux/mount.h>
49 #else /* __KERNEL__ */
50 # include <liblustre.h>
51 #endif
52
53 #include <obd_class.h>
54 #include <lustre_mds.h>
55 #include <lustre_dlm.h>
56 #include <lustre_cfg.h>
57 #include <obd_ost.h>
58 #include <lustre_fsfilt.h>
59 #include <lustre_quota.h>
60 #include "quota_internal.h"
61
62 #ifdef __KERNEL__
63
64 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
65                   struct obd_quotactl *oqctl)
66 {
67         struct obd_device_target *obt = &obd->u.obt;
68         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
69         struct timeval work_start;
70         struct timeval work_end;
71         long timediff;
72         int rc = 0;
73         ENTRY;
74
75         cfs_gettimeofday(&work_start);
76         switch (oqctl->qc_cmd) {
77         case Q_QUOTAON:
78                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
79                 rc = mds_quota_on(obd, oqctl);
80                 break;
81         case Q_QUOTAOFF:
82                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
83                 rc = mds_quota_off(obd, oqctl);
84                 break;
85         case Q_SETINFO:
86                 rc = mds_set_dqinfo(obd, oqctl);
87                 break;
88         case Q_GETINFO:
89                 rc = mds_get_dqinfo(obd, oqctl);
90                 break;
91         case Q_SETQUOTA:
92                 rc = mds_set_dqblk(obd, oqctl);
93                 break;
94         case Q_GETQUOTA:
95                 rc = mds_get_dqblk(obd, oqctl);
96                 break;
97         case Q_GETOINFO:
98         case Q_GETOQUOTA:
99                 rc = mds_get_obd_quota(obd, oqctl);
100                 break;
101         case LUSTRE_Q_INVALIDATE:
102                 rc = mds_quota_invalidate(obd, oqctl);
103                 break;
104         case LUSTRE_Q_FINVALIDATE:
105                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
106                 rc = mds_quota_finvalidate(obd, oqctl);
107                 break;
108         default:
109                 CERROR("%s: unsupported mds_quotactl command: %d\n",
110                        obd->obd_name, oqctl->qc_cmd);
111                 RETURN(-EFAULT);
112         }
113
114         if (rc)
115                 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
116                                "type %d, failed: rc = %d\n",
117                        oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
118         cfs_gettimeofday(&work_end);
119         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
120         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
121
122         RETURN(rc);
123 }
124
125 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
126                      struct obd_quotactl *oqctl)
127 {
128         struct obd_device *obd = exp->exp_obd;
129         struct obd_device_target *obt = &obd->u.obt;
130         struct lvfs_run_ctxt saved;
131         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
132         struct lustre_qunit_size *lqs;
133         void *handle = NULL;
134         struct timeval work_start;
135         struct timeval work_end;
136         long timediff;
137         int rc = 0;
138         ENTRY;
139
140         cfs_gettimeofday(&work_start);
141         switch (oqctl->qc_cmd) {
142         case Q_QUOTAON:
143                 oqctl->qc_id = obt->obt_qfmt;
144                 rc = generic_quota_on(obd, oqctl, 0);
145                 break;
146         case Q_FINVALIDATE:
147         case Q_QUOTAOFF:
148                 cfs_down(&obt->obt_quotachecking);
149                 if (oqctl->qc_cmd == Q_FINVALIDATE &&
150                     (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
151                         CWARN("quota[%u] is on yet\n", oqctl->qc_type);
152                         cfs_up(&obt->obt_quotachecking);
153                         rc = -EBUSY;
154                         break;
155                 }
156                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
157         case Q_GETOINFO:
158         case Q_GETOQUOTA:
159         case Q_GETQUOTA:
160                 /* In recovery scenario, this pending dqacq/dqrel might have
161                  * been processed by master successfully before it's dquot
162                  * on master enter recovery mode. We must wait for this
163                  * dqacq/dqrel done then return the correct limits to master */
164                 if (oqctl->qc_stat == QUOTA_RECOVERING)
165                         handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
166
167                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
168                 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
169                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
170
171                 if (oqctl->qc_stat == QUOTA_RECOVERING)
172                         quota_unbarrier(handle);
173
174                 if (oqctl->qc_cmd == Q_QUOTAOFF ||
175                     oqctl->qc_cmd == Q_FINVALIDATE) {
176                         if (oqctl->qc_cmd == Q_QUOTAOFF) {
177                                 if (!rc)
178                                         obt->obt_qctxt.lqc_flags &=
179                                                 ~UGQUOTA2LQC(oqctl->qc_type);
180                                 else if (quota_is_off(qctxt, oqctl))
181                                                 rc = -EALREADY;
182                                 CDEBUG(D_QUOTA, "%s: quotaoff type:flags:rc "
183                                        "%u:%lu:%d\n", obd->obd_name,
184                                        oqctl->qc_type, qctxt->lqc_flags, rc);
185                         }
186                         cfs_up(&obt->obt_quotachecking);
187                 }
188
189                 break;
190         case Q_SETQUOTA:
191                 /* currently, it is only used for nullifying the quota */
192                 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
193
194                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
195                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
196
197                 if (!rc) {
198                         oqctl->qc_cmd = Q_SYNC;
199                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
200                         oqctl->qc_cmd = Q_SETQUOTA;
201                 }
202                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
203                 quota_unbarrier(handle);
204
205                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
206                                        qctxt, 0);
207                 if (lqs == NULL || IS_ERR(lqs)){
208                         CERROR("fail to create lqs during setquota operation "
209                                "for %sid %u\n", oqctl->qc_type ? "g" : "u",
210                                oqctl->qc_id);
211                 } else {
212                         lqs->lqs_flags &= ~QB_SET;
213                         lqs_putref(lqs);
214                 }
215
216                 break;
217         case Q_INITQUOTA:
218                 {
219                 unsigned int id[MAXQUOTAS] = { 0, 0 };
220
221                 /* Initialize quota limit to MIN_QLIMIT */
222                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
223                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
224
225                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
226                         goto adjust;
227
228                /* There might be a pending dqacq/dqrel (which is going to
229                  * clear stale limits on slave). we should wait for it's
230                  * completion then initialize limits */
231                 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
232                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
233                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
234                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
235
236                 /* Update on-disk quota, in case of lose the changed limits
237                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
238                 if (!rc) {
239                         oqctl->qc_cmd = Q_SYNC;
240                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
241                         oqctl->qc_cmd = Q_INITQUOTA;
242                 }
243                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
244                 quota_unbarrier(handle);
245
246                 if (rc)
247                         RETURN(rc);
248 adjust:
249                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
250                                        qctxt, 1);
251                 if (lqs == NULL || IS_ERR(lqs)){
252                         CERROR("fail to create lqs during setquota operation "
253                                "for %sid %u\n", oqctl->qc_type ? "g" : "u",
254                                oqctl->qc_id);
255                         break;
256                 } else {
257                         lqs->lqs_flags |= QB_SET;
258                         lqs_putref(lqs);
259                 }
260
261                 /* Trigger qunit pre-acquire */
262                 if (oqctl->qc_type == USRQUOTA)
263                         id[USRQUOTA] = oqctl->qc_id;
264                 else
265                         id[GRPQUOTA] = oqctl->qc_id;
266
267                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
268                                         id, 1, 0, NULL);
269                 if (rc == -EDQUOT || rc == -EBUSY) {
270                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
271                         rc = 0;
272                 }
273
274                 break;
275                 }
276         default:
277                 CERROR("%s: unsupported filter_quotactl command: %d\n",
278                        obd->obd_name, oqctl->qc_cmd);
279                 RETURN(-EFAULT);
280         }
281         cfs_gettimeofday(&work_end);
282         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
283         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
284
285         RETURN(rc);
286 }
287 #endif /* __KERNEL__ */