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