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