Whamcloud - gitweb
Branch HEAD
[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
68 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
69                   struct obd_quotactl *oqctl)
70 {
71         struct obd_device_target *obt = &obd->u.obt;
72         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
73         struct timeval work_start;
74         struct timeval work_end;
75         long timediff;
76         int rc = 0;
77         ENTRY;
78
79         do_gettimeofday(&work_start);
80         switch (oqctl->qc_cmd) {
81         case Q_QUOTAON:
82                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
83                 rc = mds_quota_on(obd, oqctl);
84                 break;
85         case Q_QUOTAOFF:
86                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
87                 rc = mds_quota_off(obd, oqctl);
88                 break;
89         case Q_SETINFO:
90                 rc = mds_set_dqinfo(obd, oqctl);
91                 break;
92         case Q_GETINFO:
93                 rc = mds_get_dqinfo(obd, oqctl);
94                 break;
95         case Q_SETQUOTA:
96                 rc = mds_set_dqblk(obd, oqctl);
97                 break;
98         case Q_GETQUOTA:
99                 rc = mds_get_dqblk(obd, oqctl);
100                 break;
101         case Q_GETOINFO:
102         case Q_GETOQUOTA:
103                 rc = mds_get_obd_quota(obd, oqctl);
104                 break;
105         case LUSTRE_Q_INVALIDATE:
106                 rc = mds_quota_invalidate(obd, oqctl);
107                 break;
108         case LUSTRE_Q_FINVALIDATE:
109                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
110                 rc = mds_quota_finvalidate(obd, oqctl);
111                 break;
112         default:
113                 CERROR("%s: unsupported mds_quotactl command: %d\n",
114                        obd->obd_name, oqctl->qc_cmd);
115                 RETURN(-EFAULT);
116         }
117
118         if (rc)
119                 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
120                                "type %d, failed: rc = %d\n",
121                        oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
122         do_gettimeofday(&work_end);
123         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
124         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
125
126         RETURN(rc);
127 }
128
129 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
130                      struct obd_quotactl *oqctl)
131 {
132         struct obd_device *obd = exp->exp_obd;
133         struct obd_device_target *obt = &obd->u.obt;
134         struct lvfs_run_ctxt saved;
135         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
136         struct lustre_qunit_size *lqs;
137         void *handle = NULL;
138         struct timeval work_start;
139         struct timeval work_end;
140         long timediff;
141         int rc = 0;
142         ENTRY;
143
144         do_gettimeofday(&work_start);
145         switch (oqctl->qc_cmd) {
146         case Q_FINVALIDATE:
147         case Q_QUOTAON:
148         case Q_QUOTAOFF:
149                 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                         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_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
176                     oqctl->qc_cmd == Q_FINVALIDATE) {
177                         if (oqctl->qc_cmd == Q_QUOTAON) {
178                                 if (!rc) {
179                                         obt->obt_qctxt.lqc_flags |=
180                                                 UGQUOTA2LQC(oqctl->qc_type);
181                                         /* when quotaon, create lqs for every
182                                          * quota uid/gid b=18574 */
183                                         build_lqs(obd);
184                                 } else if (rc == -EBUSY &&
185                                          quota_is_on(qctxt, oqctl)) {
186                                                 rc = -EALREADY;
187                                 }
188                         } else if (oqctl->qc_cmd == Q_QUOTAOFF) {
189                                 if (!rc)
190                                         obt->obt_qctxt.lqc_flags &=
191                                                 ~UGQUOTA2LQC(oqctl->qc_type);
192                                 else if (quota_is_off(qctxt, oqctl))
193                                                 rc = -EALREADY;
194                         }
195                         up(&obt->obt_quotachecking);
196                 }
197
198                 break;
199         case Q_SETQUOTA:
200                 /* currently, it is only used for nullifying the quota */
201                 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
202
203                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
204                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
205
206                 if (!rc) {
207                         oqctl->qc_cmd = Q_SYNC;
208                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
209                         oqctl->qc_cmd = Q_SETQUOTA;
210                 }
211                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
212                 quota_unbarrier(handle);
213
214                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
215                                        qctxt, 0);
216                 if (lqs == NULL || IS_ERR(lqs)){
217                         CDEBUG(D_ERROR, "fail to create lqs when setquota\n");
218                 } else {
219                         lqs->lqs_flags &= ~QB_SET;
220                         lqs_putref(lqs);
221                 }
222
223                 break;
224         case Q_INITQUOTA:
225                 {
226                 unsigned int id[MAXQUOTAS] = { 0, 0 };
227
228                 /* Initialize quota limit to MIN_QLIMIT */
229                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
230                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
231
232                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
233                         goto adjust;
234
235                /* There might be a pending dqacq/dqrel (which is going to
236                  * clear stale limits on slave). we should wait for it's
237                  * completion then initialize limits */
238                 handle = quota_barrier(&obd->u.obt.obt_qctxt, oqctl, 1);
239                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
240                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
241                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
242
243                 /* Update on-disk quota, in case of lose the changed limits
244                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
245                 if (!rc) {
246                         oqctl->qc_cmd = Q_SYNC;
247                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
248                         oqctl->qc_cmd = Q_INITQUOTA;
249                 }
250                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
251                 quota_unbarrier(handle);
252
253                 if (rc)
254                         RETURN(rc);
255 adjust:
256                 lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
257                                        qctxt, 1);
258                 if (lqs == NULL || IS_ERR(lqs)){
259                         CDEBUG(D_ERROR, "fail to create lqs when setquota\n");
260                         break;
261                 } else {
262                         lqs->lqs_flags |= QB_SET;
263                         lqs_putref(lqs);
264                 }
265
266                 /* Trigger qunit pre-acquire */
267                 if (oqctl->qc_type == USRQUOTA)
268                         id[USRQUOTA] = oqctl->qc_id;
269                 else
270                         id[GRPQUOTA] = oqctl->qc_id;
271
272                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
273                                         id, 1, 0, NULL);
274                 if (rc == -EDQUOT || rc == -EBUSY) {
275                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
276                         rc = 0;
277                 }
278
279                 break;
280                 }
281         default:
282                 CERROR("%s: unsupported filter_quotactl command: %d\n",
283                        obd->obd_name, oqctl->qc_cmd);
284                 RETURN(-EFAULT);
285         }
286         do_gettimeofday(&work_end);
287         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
288         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
289
290         RETURN(rc);
291 }
292 #endif /* __KERNEL__ */
293 #endif
294
295 int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
296                      struct obd_quotactl *oqctl)
297 {
298         struct ptlrpc_request   *req;
299         struct obd_quotactl     *oqc;
300         const struct req_format *rf;
301         int                      ver, opc, rc;
302         ENTRY;
303
304         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
305                 rf  = &RQF_MDS_QUOTACTL;
306                 ver = LUSTRE_MDS_VERSION,
307                 opc = MDS_QUOTACTL;
308         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
309                 rf  = &RQF_OST_QUOTACTL;
310                 ver = LUSTRE_OST_VERSION,
311                 opc = OST_QUOTACTL;
312         } else {
313                 RETURN(-EINVAL);
314         }
315
316         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
317         if (req == NULL)
318                 RETURN(-ENOMEM);
319
320         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
321         *oqc = *oqctl;
322
323         ptlrpc_request_set_replen(req);
324
325         rc = ptlrpc_queue_wait(req);
326         if (rc) {
327                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
328                 GOTO(out, rc);
329         }
330
331         oqc = NULL;
332         if (req->rq_repmsg)
333                 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
334
335         if (oqc == NULL) {
336                 CERROR ("Can't unpack obd_quotactl\n");
337                 GOTO(out, rc = -EPROTO);
338         }
339
340         *oqctl = *oqc;
341         EXIT;
342 out:
343         ptlrpc_req_finished(req);
344         return rc;
345 }
346
347 /**
348  * For lmv, only need to send request to master MDT, and the master MDT will
349  * process with other slave MDTs.
350  */
351 int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
352                   struct obd_quotactl *oqctl)
353 {
354         struct obd_device *obd = class_exp2obd(exp);
355         struct lmv_obd *lmv = &obd->u.lmv;
356         struct lmv_tgt_desc *tgt = &lmv->tgts[0];
357         int rc;
358         ENTRY;
359
360         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
361                 CERROR("master lmv inactive\n");
362                 RETURN(-EIO);
363         }
364
365         rc = obd_quotactl(tgt->ltd_exp, oqctl);
366         RETURN(rc);
367 }
368
369 int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
370                   struct obd_quotactl *oqctl)
371 {
372         struct obd_device *obd = class_exp2obd(exp);
373         struct lov_obd *lov = &obd->u.lov;
374         struct lov_tgt_desc *tgt;
375         __u64 curspace = 0;
376         __u64 bhardlimit = 0;
377         int i, rc = 0;
378         ENTRY;
379
380         if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
381             oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
382             oqctl->qc_cmd != Q_GETOQUOTA &&
383             oqctl->qc_cmd != Q_INITQUOTA &&
384             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
385             oqctl->qc_cmd != Q_FINVALIDATE) {
386                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
387                 RETURN(-EFAULT);
388         }
389
390         /* for lov tgt */
391         obd_getref(obd);
392         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
393                 int err;
394
395                 tgt = lov->lov_tgts[i];
396                 if (!tgt || !tgt->ltd_active || tgt->ltd_reap) {
397                         if (oqctl->qc_cmd == Q_GETOQUOTA) {
398                                 rc = -EREMOTEIO;
399                                 CERROR("ost %d is inactive\n", i);
400                         } else {
401                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
402                         }
403                         continue;
404                 }
405
406                 err = obd_quotactl(tgt->ltd_exp, oqctl);
407                 if (err) {
408                         if (tgt->ltd_active && !rc)
409                                 rc = err;
410                         continue;
411                 }
412
413                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
414                         curspace += oqctl->qc_dqblk.dqb_curspace;
415                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
416                 }
417         }
418         obd_putref(obd);
419
420         if (oqctl->qc_cmd == Q_GETOQUOTA) {
421                 oqctl->qc_dqblk.dqb_curspace = curspace;
422                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
423         }
424         RETURN(rc);
425 }