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