Whamcloud - gitweb
a1961cf07a40ac4dbd209db921bc7577c6a0b90d
[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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 HAVE_QUOTA_SUPPORT
66 #ifdef __KERNEL__
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         do_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         do_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 = &obd->u.obt.obt_qctxt;
135         struct timeval work_start;
136         struct timeval work_end;
137         long timediff;
138         int rc = 0;
139         ENTRY;
140
141         do_gettimeofday(&work_start);
142         switch (oqctl->qc_cmd) {
143         case Q_FINVALIDATE:
144         case Q_QUOTAON:
145         case Q_QUOTAOFF:
146                 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
147                         CDEBUG(D_INFO, "other people are doing quotacheck\n");
148                         atomic_inc(&obt->obt_quotachecking);
149                         rc = -EBUSY;
150                         break;
151                 }
152                 if (oqctl->qc_cmd == Q_FINVALIDATE &&
153                     (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
154                         atomic_inc(&obt->obt_quotachecking);
155                         rc = -EBUSY;
156                         break;
157                 }
158                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
159         case Q_GETOINFO:
160         case Q_GETOQUOTA:
161         case Q_GETQUOTA:
162                 /* In recovery scenario, this pending dqacq/dqrel might have
163                  * been processed by master successfully before it's dquot
164                  * on master enter recovery mode. We must wait for this 
165                  * dqacq/dqrel done then return the correct limits to master */
166                 if (oqctl->qc_stat == QUOTA_RECOVERING)
167                         qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
168                                                  oqctl->qc_id, oqctl->qc_type,
169                                                  1);
170
171                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
172                 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
173                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
174
175                 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
176                     oqctl->qc_cmd == Q_FINVALIDATE) {
177                         if (!rc && oqctl->qc_cmd == Q_QUOTAON)
178                                 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
179                         if (!rc && oqctl->qc_cmd == Q_QUOTAOFF)
180                                 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
181                         atomic_inc(&obt->obt_quotachecking);
182                 }
183                 break;
184         case Q_SETQUOTA:
185                 /* currently, it is only used for nullifying the quota */
186                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
187                                          oqctl->qc_id, oqctl->qc_type, 1);
188
189                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
190                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
191
192                 if (!rc) {
193                         oqctl->qc_cmd = Q_SYNC;
194                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
195                         oqctl->qc_cmd = Q_SETQUOTA;
196                 }
197                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
198                 break;
199         case Q_INITQUOTA:
200                 {
201                 unsigned int id[MAXQUOTAS] = { 0, 0 };
202
203                 /* Initialize quota limit to MIN_QLIMIT */
204                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
205                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
206
207                 /* There might be a pending dqacq/dqrel (which is going to
208                  * clear stale limits on slave). we should wait for it's
209                  * completion then initialize limits */
210                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
211                                          oqctl->qc_id, oqctl->qc_type, 1);
212
213                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
214                         goto adjust;
215
216                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
217                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
218                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
219
220                 /* Update on-disk quota, in case of lose the changed limits
221                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
222                 if (!rc) {
223                         oqctl->qc_cmd = Q_SYNC;
224                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
225                         oqctl->qc_cmd = Q_INITQUOTA;
226                 }
227                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
228
229                 if (rc)
230                         RETURN(rc);
231 adjust:
232                 /* Trigger qunit pre-acquire */
233                 if (oqctl->qc_type == USRQUOTA)
234                         id[USRQUOTA] = oqctl->qc_id;
235                 else
236                         id[GRPQUOTA] = oqctl->qc_id;
237
238                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
239                                         id, 1, 0, NULL);
240                 if (rc == -EDQUOT || rc == -EBUSY) {
241                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
242                         rc = 0;
243                 }
244
245                 break;
246                 }
247         default:
248                 CERROR("%s: unsupported filter_quotactl command: %d\n",
249                        obd->obd_name, oqctl->qc_cmd);
250                 RETURN(-EFAULT);
251         }
252         do_gettimeofday(&work_end);
253         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
254         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
255
256         RETURN(rc);
257 }
258 #endif /* __KERNEL__ */
259 #endif
260
261 int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
262                      struct obd_quotactl *oqctl)
263 {
264         struct ptlrpc_request   *req;
265         struct obd_quotactl     *oqc;
266         const struct req_format *rf;
267         int                      ver, opc, rc;
268         ENTRY;
269
270         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
271                 rf  = &RQF_MDS_QUOTACTL;
272                 ver = LUSTRE_MDS_VERSION,
273                 opc = MDS_QUOTACTL;
274         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
275                 rf  = &RQF_OST_QUOTACTL;
276                 ver = LUSTRE_OST_VERSION,
277                 opc = OST_QUOTACTL;
278         } else {
279                 RETURN(-EINVAL);
280         }
281
282         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
283         if (req == NULL)
284                 RETURN(-ENOMEM);
285
286         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
287         *oqc = *oqctl;
288
289         ptlrpc_request_set_replen(req);
290
291         rc = ptlrpc_queue_wait(req);
292         if (rc) {
293                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
294                 GOTO(out, rc);
295         }
296
297         oqc = NULL;
298         if (req->rq_repmsg)
299                 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
300
301         if (oqc == NULL) {
302                 CERROR ("Can't unpack obd_quotactl\n");
303                 GOTO(out, rc = -EPROTO);
304         }
305
306         *oqctl = *oqc;
307         EXIT;
308 out:
309         ptlrpc_req_finished(req);
310         return rc;
311 }
312
313 /**
314  * For lmv, only need to send request to master MDT, and the master MDT will
315  * process with other slave MDTs.
316  */
317 int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
318                   struct obd_quotactl *oqctl)
319 {
320         struct obd_device *obd = class_exp2obd(exp);
321         struct lmv_obd *lmv = &obd->u.lmv;
322         struct lmv_tgt_desc *tgt = &lmv->tgts[0];
323         int rc;
324         ENTRY;
325
326         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
327                 CERROR("master lmv inactive\n");
328                 RETURN(-EIO);
329         }
330
331         rc = obd_quotactl(tgt->ltd_exp, oqctl);
332         RETURN(rc);
333 }
334
335 int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
336                   struct obd_quotactl *oqctl)
337 {
338         struct obd_device *obd = class_exp2obd(exp);
339         struct lov_obd *lov = &obd->u.lov;
340         struct lov_tgt_desc *tgt;
341         __u64 curspace = 0;
342         __u64 bhardlimit = 0;
343         int i, rc = 0;
344         ENTRY;
345
346         if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
347             oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
348             oqctl->qc_cmd != Q_GETOQUOTA &&
349             oqctl->qc_cmd != Q_INITQUOTA &&
350             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
351             oqctl->qc_cmd != Q_FINVALIDATE) {
352                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
353                 RETURN(-EFAULT);
354         }
355
356         /* for lov tgt */
357         obd_getref(obd);
358         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
359                 int err;
360
361                 tgt = lov->lov_tgts[i];
362                 if (!tgt || !tgt->ltd_active || tgt->ltd_reap) {
363                         if (oqctl->qc_cmd == Q_GETOQUOTA) {
364                                 CERROR("ost %d is inactive\n", i);
365                                 rc = -EIO;
366                         } else {
367                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
368                         }
369                         continue;
370                 }
371
372                 err = obd_quotactl(tgt->ltd_exp, oqctl);
373                 if (err) {
374                         if (tgt->ltd_active && !rc)
375                                 rc = err;
376                         continue;
377                 }
378
379                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
380                         curspace += oqctl->qc_dqblk.dqb_curspace;
381                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
382                 }
383         }
384         obd_putref(obd);
385
386         if (oqctl->qc_cmd == Q_GETOQUOTA) {
387                 oqctl->qc_dqblk.dqb_curspace = curspace;
388                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
389         }
390         RETURN(rc);
391 }