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                    ver, opc, rc;
199         ENTRY;
200
201         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
202                 ver = LUSTRE_MDS_VERSION,
203                 opc = MDS_QUOTACTL;
204         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
205                 ver = LUSTRE_OST_VERSION,
206                 opc = OST_QUOTACTL;
207         } else {
208                 RETURN(-EINVAL);
209         }
210
211         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
212                                         &RQF_MDS_QUOTACTL, ver, opc);
213         if (req == NULL)
214                 RETURN(-ENOMEM);
215
216         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
217         *oqc = *oqctl;
218
219         ptlrpc_request_set_replen(req);
220
221         rc = ptlrpc_queue_wait(req);
222         if (!rc) {
223                 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
224                 if (oqc == NULL)
225                         GOTO(out, rc = -EPROTO);
226
227                 *oqctl = *oqc;
228         }
229 out:
230         ptlrpc_req_finished(req);
231         RETURN (rc);
232 }
233
234 int lov_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
235 {
236         struct obd_device *obd = class_exp2obd(exp);
237         struct lov_obd *lov = &obd->u.lov;
238         __u64 curspace = 0;
239         __u32 bhardlimit = 0;
240         int i, rc = 0;
241         ENTRY;
242
243         if (oqctl->qc_cmd != Q_QUOTAON && oqctl->qc_cmd != Q_QUOTAOFF &&
244             oqctl->qc_cmd != Q_GETOQUOTA && oqctl->qc_cmd != Q_INITQUOTA &&
245             oqctl->qc_cmd != Q_SETQUOTA) {
246                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
247                 RETURN(-EFAULT);
248         }
249
250         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
251                 int err;
252
253                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
254                         if (oqctl->qc_cmd == Q_GETOQUOTA) {
255                                 CERROR("ost %d is inactive\n", i);
256                                 rc = -EIO;
257                                 break;
258                         } else {
259                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
260                                 continue;
261                         }
262                 }
263
264                 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
265                 if (err) {
266                         if (lov->lov_tgts[i]->ltd_active && !rc)
267                                 rc = err;
268                         continue;
269                 }
270
271                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
272                         curspace += oqctl->qc_dqblk.dqb_curspace;
273                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
274                 }
275         }
276
277         if (oqctl->qc_cmd == Q_GETOQUOTA) {
278                 oqctl->qc_dqblk.dqb_curspace = curspace;
279                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
280         }
281         RETURN(rc);
282 }
283