Whamcloud - gitweb
b=8007
[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 <linux/obd_class.h>
39 #include <linux/lustre_mds.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_cfg.h>
42 #include <linux/obd_ost.h>
43 #include <linux/lustre_fsfilt.h>
44 #include <linux/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 lvfs_run_ctxt saved;
95         int rc = 0;
96         ENTRY;
97
98         switch (oqctl->qc_cmd) {
99         case Q_QUOTAON:
100         case Q_QUOTAOFF:
101         case Q_GETOINFO:
102         case Q_GETOQUOTA:
103         case Q_GETQUOTA:
104                 /* In recovery scenario, this pending dqacq/dqrel might have
105                  * been processed by master successfully before it's dquot
106                  * on master enter recovery mode. We must wait for this 
107                  * dqacq/dqrel done then return the correct limits to master */
108                 if (oqctl->qc_stat == QUOTA_RECOVERING)
109                         qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
110                                                  oqctl->qc_id, oqctl->qc_type, 
111                                                  1);
112
113                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
114                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
115                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
116                 break;
117         case Q_INITQUOTA:
118                 {
119                 unsigned int uid = 0, gid = 0;
120
121                 /* Initialize quota limit to MIN_QLIMIT */
122                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
123                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
124
125                 /* There might be a pending dqacq/dqrel (which is going to
126                  * clear stale limits on slave). we should wait for it's 
127                  * completion then initialize limits */
128                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt, 
129                                          oqctl->qc_id, oqctl->qc_type, 1);
130
131                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
132                         goto adjust;
133                 
134                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
135                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
136                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
137
138                 /* Update on-disk quota, in case of lose the changed limits
139                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
140                 if (!rc) {
141                         oqctl->qc_cmd = Q_SYNC;
142                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
143                         oqctl->qc_cmd = Q_INITQUOTA;
144                 }
145                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
146
147                 if (rc)
148                         RETURN(rc);
149 adjust:
150                 /* Trigger qunit pre-acquire */
151                 if (oqctl->qc_type == USRQUOTA)
152                         uid = oqctl->qc_id;
153                 else
154                         gid = oqctl->qc_id;
155
156                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, 
157                                         uid, gid, 1, 0);
158                 break;
159                 }
160         default:
161                 CERROR("%s: unsupported filter_quotactl command: %d\n",
162                        obd->obd_name, oqctl->qc_cmd);
163                 RETURN(-EFAULT);
164         }
165
166         RETURN(rc);
167 }
168 #endif /* __KERNEL__ */
169
170 int client_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
171 {
172         struct ptlrpc_request *req;
173         struct obd_quotactl *oqc;
174         int size = sizeof(*oqctl), opc, version;
175         int rc;
176         ENTRY;
177
178         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
179                 opc = MDS_QUOTACTL;
180                 version = LUSTRE_MDS_VERSION;
181         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
182                 opc = OST_QUOTACTL;
183                 version = LUSTRE_OST_VERSION;
184         } else {
185                 RETURN(-EINVAL);
186         }
187
188         req = ptlrpc_prep_req(class_exp2cliimp(exp), version, opc, 1, &size,
189                               NULL);
190         if (!req)
191                 GOTO(out, rc = -ENOMEM);
192
193         oqc = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*oqctl));
194         *oqc = *oqctl;
195
196         req->rq_replen = lustre_msg_size(1, &size);
197
198         rc = ptlrpc_queue_wait(req);
199         if (!rc) {
200                 oqc = lustre_swab_repbuf(req, 0, sizeof (*oqc),
201                                          lustre_swab_obd_quotactl);
202                 if (oqc == NULL) {
203                         CERROR ("Can't unpack obd_quotactl\n");
204                         GOTO(out, rc = -EPROTO);
205                 }
206
207                 *oqctl = *oqc;
208         }
209 out:
210         ptlrpc_req_finished(req);
211         RETURN (rc);
212 }
213
214 int lov_quota_ctl(struct obd_export *exp, struct obd_quotactl *oqctl)
215 {
216         struct obd_device *obd = class_exp2obd(exp);
217         struct lov_obd *lov = &obd->u.lov;
218         __u64 curspace = 0;
219         __u32 bhardlimit = 0;
220         int i, rc = 0;
221         ENTRY;
222
223         if (oqctl->qc_cmd != Q_QUOTAON && oqctl->qc_cmd != Q_QUOTAOFF &&
224             oqctl->qc_cmd != Q_GETOQUOTA && oqctl->qc_cmd != Q_INITQUOTA) {
225                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
226                 RETURN(-EFAULT);
227         }
228
229         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
230                 int err;
231
232                 if (!lov->tgts[i].active) {
233                         if (oqctl->qc_cmd == Q_GETOQUOTA) {
234                                 CERROR("ost %d is inactive\n", i);
235                                 rc = -EIO;
236                                 break;
237                         } else {
238                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
239                                 continue;
240                         }
241                 }
242
243                 err = obd_quotactl(lov->tgts[i].ltd_exp, oqctl);
244                 if (err) {
245                         if (lov->tgts[i].active && !rc)
246                                 rc = err;
247                         continue;
248                 }
249
250                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
251                         curspace += oqctl->qc_dqblk.dqb_curspace;
252                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
253                 }
254         }
255
256         if (oqctl->qc_cmd == Q_GETOQUOTA) {
257                 oqctl->qc_dqblk.dqb_curspace = curspace;
258                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
259         }
260         RETURN(rc);
261 }
262