Whamcloud - gitweb
c056048c71b9379fd4682f6ff29e6c51eb6032fa
[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_INITQUOTA:
132                 {
133                 unsigned int uid = 0, gid = 0;
134
135                 /* Initialize quota limit to MIN_QLIMIT */
136                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
137                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
138
139                 /* There might be a pending dqacq/dqrel (which is going to
140                  * clear stale limits on slave). we should wait for it's 
141                  * completion then initialize limits */
142                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt, 
143                                          oqctl->qc_id, oqctl->qc_type, 1);
144
145                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
146                         goto adjust;
147                 
148                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
149                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
150                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
151
152                 /* Update on-disk quota, in case of lose the changed limits
153                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
154                 if (!rc) {
155                         oqctl->qc_cmd = Q_SYNC;
156                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
157                         oqctl->qc_cmd = Q_INITQUOTA;
158                 }
159                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
160
161                 if (rc)
162                         RETURN(rc);
163 adjust:
164                 /* Trigger qunit pre-acquire */
165                 if (oqctl->qc_type == USRQUOTA)
166                         uid = oqctl->qc_id;
167                 else
168                         gid = oqctl->qc_id;
169
170                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, 
171                                         uid, gid, 1, 0);
172                 break;
173                 }
174         default:
175                 CERROR("%s: unsupported filter_quotactl command: %d\n",
176                        obd->obd_name, oqctl->qc_cmd);
177                 RETURN(-EFAULT);
178         }
179
180         RETURN(rc);
181 }
182 #endif /* __KERNEL__ */
183
184 int client_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
185 {
186         struct ptlrpc_request *req;
187         struct obd_quotactl *oqc;
188         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqctl) };
189         int ver, opc, rc;
190         ENTRY;
191
192         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
193                 ver = LUSTRE_MDS_VERSION,
194                 opc = MDS_QUOTACTL;
195         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
196                 ver = LUSTRE_OST_VERSION,
197                 opc = OST_QUOTACTL;
198         } else {
199                 RETURN(-EINVAL);
200         }
201
202         req = ptlrpc_prep_req(class_exp2cliimp(exp), ver, opc, 2, size, NULL);
203         if (!req)
204                 GOTO(out, rc = -ENOMEM);
205
206         oqc = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*oqctl));
207         *oqc = *oqctl;
208
209         ptlrpc_req_set_repsize(req, 2, size);
210
211         rc = ptlrpc_queue_wait(req);
212         if (!rc) {
213                 oqc = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*oqc),
214                                          lustre_swab_obd_quotactl);
215                 if (oqc == NULL) {
216                         CERROR ("Can't unpack obd_quotactl\n");
217                         GOTO(out, rc = -EPROTO);
218                 }
219
220                 *oqctl = *oqc;
221         }
222 out:
223         ptlrpc_req_finished(req);
224         RETURN (rc);
225 }
226
227 int lov_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
228 {
229         struct obd_device *obd = class_exp2obd(exp);
230         struct lov_obd *lov = &obd->u.lov;
231         __u64 curspace = 0;
232         __u32 bhardlimit = 0;
233         int i, rc = 0;
234         ENTRY;
235
236         if (oqctl->qc_cmd != Q_QUOTAON && oqctl->qc_cmd != Q_QUOTAOFF &&
237             oqctl->qc_cmd != Q_GETOQUOTA && oqctl->qc_cmd != Q_INITQUOTA) {
238                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
239                 RETURN(-EFAULT);
240         }
241
242         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
243                 int err;
244
245                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
246                         if (oqctl->qc_cmd == Q_GETOQUOTA) {
247                                 CERROR("ost %d is inactive\n", i);
248                                 rc = -EIO;
249                                 break;
250                         } else {
251                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
252                                 continue;
253                         }
254                 }
255
256                 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
257                 if (err) {
258                         if (lov->lov_tgts[i]->ltd_active && !rc)
259                                 rc = err;
260                         continue;
261                 }
262
263                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
264                         curspace += oqctl->qc_dqblk.dqb_curspace;
265                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
266                 }
267         }
268
269         if (oqctl->qc_cmd == Q_GETOQUOTA) {
270                 oqctl->qc_dqblk.dqb_curspace = curspace;
271                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
272         }
273         RETURN(rc);
274 }
275