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