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