Whamcloud - gitweb
Branch b1_4_mountconf
[fs/lustre-release.git] / lustre / quota / quota_check.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/quota/quota_check.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 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
26 #  include <linux/smp_lock.h>
27 #  include <linux/buffer_head.h>
28 #  include <linux/workqueue.h>
29 #  include <linux/mount.h>
30 # else
31 #  include <linux/locks.h>
32 # endif
33 #else /* __KERNEL__ */
34 # include <liblustre.h>
35 #endif
36
37 #include <linux/obd_class.h>
38 #include <linux/lustre_mds.h>
39 #include <linux/lustre_dlm.h>
40 #include <linux/lustre_cfg.h>
41 #include <linux/obd_ost.h>
42 #include <linux/lustre_fsfilt.h>
43 #include <linux/lustre_quota.h>
44 #include "quota_internal.h"
45
46 #ifdef __KERNEL__
47 static int target_quotacheck_callback(struct obd_export *exp,
48                                       struct obd_quotactl *oqctl)
49 {
50         struct ptlrpc_request *req;
51         struct obd_quotactl *body;
52         int rc, size = sizeof(*oqctl);
53         ENTRY;
54
55         req = ptlrpc_prep_req(exp->exp_imp_reverse, LUSTRE_OBD_VERSION,
56                               OBD_QC_CALLBACK, 1, &size, NULL);
57         if (!req)
58                 RETURN(-ENOMEM);
59
60         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
61         *body = *oqctl;
62
63         req->rq_replen = lustre_msg_size(0, NULL);
64
65         rc = ptlrpc_queue_wait(req);
66         ptlrpc_req_finished(req);
67
68         RETURN(rc);
69 }
70
71 static int target_quotacheck_thread(void *data)
72 {
73         unsigned long flags;
74         struct quotacheck_thread_args *qta = data;
75         struct obd_export *exp;
76         struct obd_device *obd;
77         struct obd_quotactl *oqctl;
78         struct lvfs_run_ctxt saved;
79         int rc;
80
81         lock_kernel();
82         ptlrpc_daemonize();
83
84         SIGNAL_MASK_LOCK(current, flags);
85         sigfillset(&current->blocked);
86         RECALC_SIGPENDING;
87         SIGNAL_MASK_UNLOCK(current, flags);
88
89         THREAD_NAME(cfs_curproc_comm(), CFS_CURPROC_COMM_MAX, "%s",
90                     "quotacheck");
91         unlock_kernel();
92
93         exp = qta->qta_exp;
94         obd = exp->exp_obd;
95         oqctl = &qta->qta_oqctl;
96
97         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
98
99         rc = fsfilt_quotacheck(obd, qta->qta_sb, oqctl);
100         if (rc)
101                 CERROR("%s: fsfilt_quotacheck: %d\n", obd->obd_name, rc);
102
103         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
104
105         rc = target_quotacheck_callback(exp, oqctl);
106
107         atomic_inc(qta->qta_sem);
108
109         OBD_FREE_PTR(qta);
110         return rc;
111 }
112
113 int target_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
114 {
115         struct obd_device *obd = exp->exp_obd;
116         struct obd_device_target *obt = &obd->u.obt;
117         struct quotacheck_thread_args *qta;
118         int rc = 0;
119         ENTRY;
120
121         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
122                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
123                 GOTO(out, rc = -EBUSY);
124         }
125
126         OBD_ALLOC_PTR(qta);
127         if (!qta)
128                 GOTO(out, rc = -ENOMEM);
129
130         qta->qta_exp = exp;
131         qta->qta_oqctl = *oqctl;
132         qta->qta_sb = obt->obt_sb;
133         qta->qta_sem = &obt->obt_quotachecking;
134
135         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
136                 /* quota master */
137                 rc = init_admin_quotafiles(obd, &qta->qta_oqctl);
138                 if (rc) {
139                         CERROR("init_admin_quotafiles failed: %d\n", rc);
140                         OBD_FREE_PTR(qta);
141                         GOTO(out, rc);
142                 }
143         }
144
145         rc = kernel_thread(target_quotacheck_thread, qta, CLONE_VM|CLONE_FILES);
146         if (rc >= 0) {
147                 CDEBUG(D_INFO, "%s: target_quotacheck_thread: %d\n",
148                        obd->obd_name, rc);
149                 RETURN(0);
150         }
151
152         CERROR("%s: error starting quotacheck_thread: %d\n",
153                obd->obd_name, rc);
154         OBD_FREE_PTR(qta);
155 out:
156         atomic_inc(&obt->obt_quotachecking);
157         RETURN(rc);
158 }
159
160 #endif /* __KERNEL__ */
161
162 int client_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
163 {
164         struct client_obd *cli = &exp->exp_obd->u.cli;
165         struct ptlrpc_request *req;
166         struct obd_quotactl *body;
167         int size = sizeof(*body), opc, version;
168         int rc;
169         ENTRY;
170
171         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
172                 version = LUSTRE_MDS_VERSION;
173                 opc = MDS_QUOTACHECK;
174         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
175                 version = LUSTRE_OST_VERSION;
176                 opc = OST_QUOTACHECK;
177         } else {
178                 RETURN(-EINVAL);
179         }
180
181         req = ptlrpc_prep_req(class_exp2cliimp(exp), version, opc, 1, &size,
182                               NULL);
183         if (!req)
184                 GOTO(out, rc = -ENOMEM);
185
186         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
187         *body = *oqctl;
188
189         req->rq_replen = lustre_msg_size(0, NULL);
190
191         /* the next poll will find -ENODATA, that means quotacheck is
192          * going on */
193         cli->cl_qchk_stat = -ENODATA;
194         rc = ptlrpc_queue_wait(req);
195         if (rc)
196                 cli->cl_qchk_stat = rc;
197 out:
198         ptlrpc_req_finished(req);
199         RETURN(rc);
200 }
201
202 int client_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
203 {
204         struct client_obd *cli = &exp->exp_obd->u.cli;
205         int rc;
206         ENTRY;
207
208         rc = cli->cl_qchk_stat;
209
210         /* the client is not the previous one */
211         if (rc == CL_NOT_QUOTACHECKED)
212                 rc = -EINTR;
213
214         qchk->obd_uuid = cli->cl_import->imp_target_uuid;
215         if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME,
216             strlen(LUSTRE_OSC_NAME)))
217                 memcpy(qchk->obd_type, LUSTRE_FILTER_NAME,
218                        strlen(LUSTRE_FILTER_NAME));
219         else if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME,
220                  strlen(LUSTRE_MDC_NAME)))
221                 memcpy(qchk->obd_type, LUSTRE_MDS_NAME,
222                        strlen(LUSTRE_MDS_NAME));
223
224         RETURN(rc);
225 }
226
227 int lov_quota_check(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         int i, rc = 0;
232         ENTRY;
233
234         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
235                 int err;
236
237                 if (!lov->tgts[i].active) {
238                         CERROR("lov idx %d inactive\n", i);
239                         RETURN(-EIO);
240                 }
241
242                 err = obd_quotacheck(lov->tgts[i].ltd_exp, oqctl);
243                 if (err && lov->tgts[i].active && !rc)
244                         rc = err;
245         }
246
247         RETURN(rc);
248 }