Whamcloud - gitweb
LU-1214 quota: don't compile quota module for client
[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 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_LQUOTA
40
41 #ifdef __KERNEL__
42 # include <linux/version.h>
43 # include <linux/module.h>
44 # include <linux/init.h>
45 # include <linux/fs.h>
46 # include <linux/jbd.h>
47 # include <linux/quota.h>
48 # include <linux/smp_lock.h>
49 # include <linux/buffer_head.h>
50 # include <linux/workqueue.h>
51 # include <linux/mount.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
54 #endif
55
56 #include <obd_class.h>
57 #include <lustre_mds.h>
58 #include <lustre_dlm.h>
59 #include <lustre_cfg.h>
60 #include <obd_ost.h>
61 #include <lustre_fsfilt.h>
62 #include <lustre_quota.h>
63 #include "quota_internal.h"
64
65 #ifdef __KERNEL__
66
67 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
68                   struct obd_quotactl *oqctl)
69 {
70         struct obd_device_target *obt = &obd->u.obt;
71         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
72         struct timeval work_start;
73         struct timeval work_end;
74         long timediff;
75         int rc = 0;
76         ENTRY;
77
78         cfs_gettimeofday(&work_start);
79         switch (oqctl->qc_cmd) {
80         case Q_QUOTAON:
81                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
82                 rc = mds_quota_on(obd, oqctl);
83                 break;
84         case Q_QUOTAOFF:
85                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
86                 rc = mds_quota_off(obd, oqctl);
87                 break;
88         case Q_SETINFO:
89                 rc = mds_set_dqinfo(obd, oqctl);
90                 break;
91         case Q_GETINFO:
92                 rc = mds_get_dqinfo(obd, oqctl);
93                 break;
94         case Q_SETQUOTA:
95                 rc = mds_set_dqblk(obd, oqctl);
96                 break;
97         case Q_GETQUOTA:
98                 rc = mds_get_dqblk(obd, oqctl);
99                 break;
100         case Q_GETOINFO:
101         case Q_GETOQUOTA:
102                 rc = mds_get_obd_quota(obd, oqctl);
103                 break;
104         case LUSTRE_Q_INVALIDATE:
105                 rc = mds_quota_invalidate(obd, oqctl);
106                 break;
107         case LUSTRE_Q_FINVALIDATE:
108                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
109                 rc = mds_quota_finvalidate(obd, oqctl);
110                 break;
111         default:
112                 CERROR("%s: unsupported mds_quotactl command: %d\n",
113                        obd->obd_name, oqctl->qc_cmd);
114                 RETURN(-EFAULT);
115         }
116
117         if (rc)
118                 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
119                                "type %d, failed: rc = %d\n",
120                        oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
121         cfs_gettimeofday(&work_end);
122         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
123         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
124
125         RETURN(rc);
126 }
127
128 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
129                      struct obd_quotactl *oqctl)
130 {
131         struct obd_device *obd = exp->exp_obd;
132         struct obd_device_target *obt = &obd->u.obt;
133         struct lvfs_run_ctxt saved;
134         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
135         struct lustre_qunit_size *lqs;
136         void *handle = NULL;
137         struct timeval work_start;
138         struct timeval work_end;
139         long timediff;
140         int rc = 0;
141         ENTRY;
142
143         cfs_gettimeofday(&work_start);
144         switch (oqctl->qc_cmd) {
145         case Q_QUOTAON:
146                 oqctl->qc_id = obt->obt_qfmt;
147                 rc = generic_quota_on(obd, oqctl, 0);
148                 break;
149         case Q_FINVALIDATE:
150         case Q_QUOTAOFF:
151                 cfs_down(&obt->obt_quotachecking);
152                 if (oqctl->qc_cmd == Q_FINVALIDATE &&
153                     (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
154                         CWARN("quota[%u] is on yet\n", oqctl->qc_type);
155                         cfs_up(&obt->obt_quotachecking);
156                         rc = -EBUSY;
157                         break;
158                 }
159                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
160         case Q_GETOINFO:
161         case Q_GETOQUOTA:
162         case Q_GETQUOTA:
163                 /* In recovery scenario, this pending dqacq/dqrel might have
164                  * been processed by master successfully before it's dquot
165                  * on master enter recovery mode. We must wait for this
166                  * dqacq/dqrel done then return the correct limits to master */
167                 if (oqctl->qc_stat == QUOTA_RECOVERING)
168                         handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
169
170                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
171                 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
172                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
173
174                 if (oqctl->qc_stat == QUOTA_RECOVERING)
175                         quota_unbarrier(handle);
176
177                 if (oqctl->qc_cmd == Q_QUOTAOFF ||
178                     oqctl->qc_cmd == Q_FINVALIDATE) {
179                         if (oqctl->qc_cmd == Q_QUOTAOFF) {
180                                 if (!rc)
181                                         obt->obt_qctxt.lqc_flags &=
182                                                 ~UGQUOTA2LQC(oqctl->qc_type);
183                                 else if (quota_is_off(qctxt, oqctl))
184                                                 rc = -EALREADY;
185                                 CDEBUG(D_QUOTA, "%s: quotaoff type:flags:rc "
186                                        "%u:%lu:%d\n", obd->obd_name,
187                                        oqctl->qc_type, qctxt->lqc_flags, rc);
188                         }
189                         cfs_up(&obt->obt_quotachecking);
190                 }
191
192                 break;
193         case Q_SETQUOTA:
194                 /* currently, it is only used for nullifying the quota */
195                 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
196
197                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
198                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
199
200                 if (!rc) {
201                         oqctl->qc_cmd = Q_SYNC;
202                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
203                         oqctl->qc_cmd = Q_SETQUOTA;
204                 }
205                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
206                 quota_unbarrier(handle);
207
208                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
209                                        qctxt, 0);
210                 if (lqs == NULL || IS_ERR(lqs)){
211                         CERROR("fail to create lqs during setquota operation "
212                                "for %sid %u\n", oqctl->qc_type ? "g" : "u",
213                                oqctl->qc_id);
214                 } else {
215                         lqs->lqs_flags &= ~QB_SET;
216                         lqs_putref(lqs);
217                 }
218
219                 break;
220         case Q_INITQUOTA:
221                 {
222                 unsigned int id[MAXQUOTAS] = { 0, 0 };
223
224                 /* Initialize quota limit to MIN_QLIMIT */
225                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
226                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
227
228                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
229                         goto adjust;
230
231                /* There might be a pending dqacq/dqrel (which is going to
232                  * clear stale limits on slave). we should wait for it's
233                  * completion then initialize limits */
234                 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
235                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
236                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
237                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
238
239                 /* Update on-disk quota, in case of lose the changed limits
240                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
241                 if (!rc) {
242                         oqctl->qc_cmd = Q_SYNC;
243                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
244                         oqctl->qc_cmd = Q_INITQUOTA;
245                 }
246                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
247                 quota_unbarrier(handle);
248
249                 if (rc)
250                         RETURN(rc);
251 adjust:
252                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
253                                        qctxt, 1);
254                 if (lqs == NULL || IS_ERR(lqs)){
255                         CERROR("fail to create lqs during setquota operation "
256                                "for %sid %u\n", oqctl->qc_type ? "g" : "u",
257                                oqctl->qc_id);
258                         break;
259                 } else {
260                         lqs->lqs_flags |= QB_SET;
261                         lqs_putref(lqs);
262                 }
263
264                 /* Trigger qunit pre-acquire */
265                 if (oqctl->qc_type == USRQUOTA)
266                         id[USRQUOTA] = oqctl->qc_id;
267                 else
268                         id[GRPQUOTA] = oqctl->qc_id;
269
270                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
271                                         id, 1, 0, NULL);
272                 if (rc == -EDQUOT || rc == -EBUSY) {
273                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
274                         rc = 0;
275                 }
276
277                 break;
278                 }
279         default:
280                 CERROR("%s: unsupported filter_quotactl command: %d\n",
281                        obd->obd_name, oqctl->qc_cmd);
282                 RETURN(-EFAULT);
283         }
284         cfs_gettimeofday(&work_end);
285         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
286         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
287
288         RETURN(rc);
289 }
290 #endif /* __KERNEL__ */