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  *  lustre/quota/quota_ctl.c
5  *
6  *  Copyright (c) 2005 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   No redistribution or use is permitted outside of Cluster File Systems, Inc.
11  *
12  */
13 #ifndef EXPORT_SYMTAB
14 # define EXPORT_SYMTAB
15 #endif
16 #define DEBUG_SUBSYSTEM S_MDS
17
18 #ifdef __KERNEL__
19 # include <linux/version.h>
20 # include <linux/module.h>
21 # include <linux/init.h>
22 # include <linux/fs.h>
23 # include <linux/jbd.h>
24 # include <linux/ext3_fs.h>
25 # include <linux/quota.h>
26 # include <linux/smp_lock.h>
27 # include <linux/buffer_head.h>
28 # include <linux/workqueue.h>
29 # include <linux/mount.h>
30 #else /* __KERNEL__ */
31 # include <liblustre.h>
32 #endif
33
34 #include <obd_class.h>
35 #include <lustre_mds.h>
36 #include <lustre_dlm.h>
37 #include <lustre_cfg.h>
38 #include <obd_ost.h>
39 #include <lustre_fsfilt.h>
40 #include <lustre_quota.h>
41 #include "quota_internal.h"
42
43 #ifdef __KERNEL__
44 int mds_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
45 {
46         struct obd_device *obd = exp->exp_obd;
47         int rc = 0;
48         ENTRY;
49
50         switch (oqctl->qc_cmd) {
51         case Q_QUOTAON:
52                 rc = mds_quota_on(obd, oqctl);
53                 break;
54         case Q_QUOTAOFF:
55                 mds_quota_off(obd, oqctl);
56                 break;
57         case Q_SETINFO:
58                 rc = mds_set_dqinfo(obd, oqctl);
59                 break;
60         case Q_GETINFO:
61                 rc = mds_get_dqinfo(obd, oqctl);
62                 break;
63         case Q_SETQUOTA:
64                 rc = mds_set_dqblk(obd, oqctl);
65                 break;
66         case Q_GETQUOTA:
67                 rc = mds_get_dqblk(obd, oqctl);
68                 break;
69         case Q_GETOINFO:
70         case Q_GETOQUOTA:
71                 rc = mds_get_obd_quota(obd, oqctl);
72                 break;
73         default:
74                 CERROR("%s: unsupported mds_quotactl command: %d\n",
75                        obd->obd_name, oqctl->qc_cmd);
76                 RETURN(-EFAULT);
77         }
78
79         if (rc)
80                 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
81                                "type %d, failed: rc = %d\n",
82                        oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
83
84         RETURN(rc);
85 }
86
87 int filter_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
88 {
89         struct obd_device *obd = exp->exp_obd;
90         struct obd_device_target *obt = &obd->u.obt;
91         struct lvfs_run_ctxt saved;
92         int rc = 0;
93         ENTRY;
94
95         switch (oqctl->qc_cmd) {
96         case Q_QUOTAON:
97         case Q_QUOTAOFF:
98                 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
99                         CDEBUG(D_INFO, "other people are doing quotacheck\n");
100                         atomic_inc(&obt->obt_quotachecking);
101                         rc = -EBUSY;
102                         break;
103                 }
104         case Q_GETOINFO:
105         case Q_GETOQUOTA:
106         case Q_GETQUOTA:
107                 /* In recovery scenario, this pending dqacq/dqrel might have
108                  * been processed by master successfully before it's dquot
109                  * on master enter recovery mode. We must wait for this 
110                  * dqacq/dqrel done then return the correct limits to master */
111                 if (oqctl->qc_stat == QUOTA_RECOVERING)
112                         qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
113                                                  oqctl->qc_id, oqctl->qc_type, 
114                                                  1);
115
116                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
117                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
118                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
119
120                 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF) {
121                         if (!rc)
122                                 obt->obt_qctxt.lqc_status = 
123                                         (oqctl->qc_cmd == Q_QUOTAON) ? 1 : 0;
124                         atomic_inc(&obt->obt_quotachecking);
125                 }
126                 break;
127         case Q_SETQUOTA:
128                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt, 
129                                          oqctl->qc_id, oqctl->qc_type, 1);
130
131                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
132                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
133
134                 if (!rc) {
135                         oqctl->qc_cmd = Q_SYNC;
136                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
137                         oqctl->qc_cmd = Q_SETQUOTA;
138                 }
139                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
140                 break;
141         case Q_INITQUOTA:
142                 {
143                 unsigned int uid = 0, gid = 0;
144
145                 /* Initialize quota limit to MIN_QLIMIT */
146                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
147                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
148
149                 /* There might be a pending dqacq/dqrel (which is going to
150                  * clear stale limits on slave). we should wait for it's 
151                  * completion then initialize limits */
152                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt, 
153                                          oqctl->qc_id, oqctl->qc_type, 1);
154
155                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
156                         goto adjust;
157                 
158                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
159                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
160                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
161
162                 /* Update on-disk quota, in case of lose the changed limits
163                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
164                 if (!rc) {
165                         oqctl->qc_cmd = Q_SYNC;
166                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
167                         oqctl->qc_cmd = Q_INITQUOTA;
168                 }
169                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
170
171                 if (rc)
172                         RETURN(rc);
173 adjust:
174                 /* Trigger qunit pre-acquire */
175                 if (oqctl->qc_type == USRQUOTA)
176                         uid = oqctl->qc_id;
177                 else
178                         gid = oqctl->qc_id;
179
180                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, 
181                                         uid, gid, 1, 0);
182                 break;
183                 }
184         default:
185                 CERROR("%s: unsupported filter_quotactl command: %d\n",
186                        obd->obd_name, oqctl->qc_cmd);
187                 RETURN(-EFAULT);
188         }
189
190         RETURN(rc);
191 }
192 #endif /* __KERNEL__ */
193
194 int client_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
195 {
196         struct ptlrpc_request *req;
197         struct obd_quotactl *oqc;
198         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqctl) };
199         int ver, opc, rc;
200         ENTRY;
201
202         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
203                 ver = LUSTRE_MDS_VERSION,
204                 opc = MDS_QUOTACTL;
205         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
206                 ver = LUSTRE_OST_VERSION,
207                 opc = OST_QUOTACTL;
208         } else {
209                 RETURN(-EINVAL);
210         }
211
212         req = ptlrpc_prep_req(class_exp2cliimp(exp), ver, opc, 2, size, NULL);
213         if (!req)
214                 GOTO(out, rc = -ENOMEM);
215
216         oqc = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqctl));
217         *oqc = *oqctl;
218
219         ptlrpc_req_set_repsize(req, 2, size);
220
221         rc = ptlrpc_queue_wait(req);
222         if (!rc) {
223                 oqc = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*oqc),
224                                          lustre_swab_obd_quotactl);
225                 if (oqc == NULL) {
226                         CERROR ("Can't unpack obd_quotactl\n");
227                         GOTO(out, rc = -EPROTO);
228                 }
229
230                 *oqctl = *oqc;
231         }
232 out:
233         ptlrpc_req_finished(req);
234         RETURN (rc);
235 }
236
237 int lov_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
238 {
239         struct obd_device *obd = class_exp2obd(exp);
240         struct lov_obd *lov = &obd->u.lov;
241         __u64 curspace = 0;
242         __u32 bhardlimit = 0;
243         int i, rc = 0;
244         ENTRY;
245
246         if (oqctl->qc_cmd != Q_QUOTAON && oqctl->qc_cmd != Q_QUOTAOFF &&
247             oqctl->qc_cmd != Q_GETOQUOTA && oqctl->qc_cmd != Q_INITQUOTA &&
248             oqctl->qc_cmd != Q_SETQUOTA) {
249                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
250                 RETURN(-EFAULT);
251         }
252
253         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
254                 int err;
255
256                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
257                         if (oqctl->qc_cmd == Q_GETOQUOTA) {
258                                 CERROR("ost %d is inactive\n", i);
259                                 rc = -EIO;
260                                 break;
261                         } else {
262                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
263                                 continue;
264                         }
265                 }
266
267                 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
268                 if (err) {
269                         if (lov->lov_tgts[i]->ltd_active && !rc)
270                                 rc = err;
271                         continue;
272                 }
273
274                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
275                         curspace += oqctl->qc_dqblk.dqb_curspace;
276                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
277                 }
278         }
279
280         if (oqctl->qc_cmd == Q_GETOQUOTA) {
281                 oqctl->qc_dqblk.dqb_curspace = curspace;
282                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
283         }
284         RETURN(rc);
285 }
286