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