Whamcloud - gitweb
62d73496d8aca330977917230577c25af5a565bc
[fs/lustre-release.git] / lustre / fid / fid_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/fid/fid_request.c
37  *
38  * Lustre Sequence Manager
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_FID
47
48 #ifdef __KERNEL__
49 # include <libcfs/libcfs.h>
50 # include <linux/module.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <dt_object.h>
58 #include <md_object.h>
59 #include <obd_support.h>
60 #include <lustre_req_layout.h>
61 #include <lustre_fid.h>
62 /* mdc RPC locks */
63 #include <lustre_mdc.h>
64 #include "fid_internal.h"
65
66 static int seq_client_rpc(struct lu_client_seq *seq, struct lu_seq_range *input,
67                           struct lu_seq_range *output, __u32 opc,
68                           const char *opcname)
69 {
70         struct obd_export     *exp = seq->lcs_exp;
71         struct ptlrpc_request *req;
72         struct lu_seq_range       *out, *in;
73         __u32                 *op;
74         int                    rc;
75         ENTRY;
76
77         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
78                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
79         if (req == NULL)
80                 RETURN(-ENOMEM);
81
82         /* Init operation code */
83         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
84         *op = opc;
85
86         /* Zero out input range, this is not recovery yet. */
87         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
88         if (input != NULL)
89                 *in = *input;
90         else
91                 range_init(in);
92
93         ptlrpc_request_set_replen(req);
94
95         if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
96                 req->rq_request_portal = (opc == SEQ_ALLOC_SUPER) ?
97                         SEQ_CONTROLLER_PORTAL : SEQ_METADATA_PORTAL;
98                 /* update mdt field of *in, it is required for fld update
99                  * on super sequence allocator node. */
100                 if (opc == SEQ_ALLOC_SUPER)
101                         in->lsr_mdt = seq->lcs_space.lsr_mdt;
102         } else {
103                 LASSERT(opc == SEQ_ALLOC_META);
104                 req->rq_request_portal = SEQ_DATA_PORTAL;
105         }
106         ptlrpc_at_set_req_timeout(req);
107
108         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
109         rc = ptlrpc_queue_wait(req);
110         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
111
112         if (rc)
113                 GOTO(out_req, rc);
114
115         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
116         *output = *out;
117
118         if (!range_is_sane(output)) {
119                 CERROR("%s: Invalid range received from server: "
120                        DRANGE"\n", seq->lcs_name, PRANGE(output));
121                 GOTO(out_req, rc = -EINVAL);
122         }
123
124         if (range_is_exhausted(output)) {
125                 CERROR("%s: Range received from server is exhausted: "
126                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
127                 GOTO(out_req, rc = -EINVAL);
128         }
129         *in = *out;
130
131         CDEBUG(D_INFO, "%s: Allocated %s-sequence "DRANGE"]\n",
132                seq->lcs_name, opcname, PRANGE(output));
133
134         EXIT;
135 out_req:
136         ptlrpc_req_finished(req);
137         return rc;
138 }
139
140 /* Request sequence-controller node to allocate new super-sequence. */
141 int seq_client_replay_super(struct lu_client_seq *seq,
142                             struct lu_seq_range *range,
143                             const struct lu_env *env)
144 {
145         int rc;
146         ENTRY;
147
148         cfs_down(&seq->lcs_sem);
149
150 #ifdef __KERNEL__
151         if (seq->lcs_srv) {
152                 LASSERT(env != NULL);
153                 rc = seq_server_alloc_super(seq->lcs_srv, range,
154                                             &seq->lcs_space, env);
155         } else {
156 #endif
157                 rc = seq_client_rpc(seq, range, &seq->lcs_space,
158                                     SEQ_ALLOC_SUPER, "super");
159 #ifdef __KERNEL__
160         }
161 #endif
162         cfs_up(&seq->lcs_sem);
163         RETURN(rc);
164 }
165
166 /* Request sequence-controller node to allocate new super-sequence. */
167 int seq_client_alloc_super(struct lu_client_seq *seq,
168                            const struct lu_env *env)
169 {
170         ENTRY;
171         RETURN(seq_client_replay_super(seq, NULL, env));
172 }
173
174 /* Request sequence-controller node to allocate new meta-sequence. */
175 static int seq_client_alloc_meta(struct lu_client_seq *seq,
176                                  const struct lu_env *env)
177 {
178         int rc;
179         ENTRY;
180
181 #ifdef __KERNEL__
182         if (seq->lcs_srv) {
183                 LASSERT(env != NULL);
184                 rc = seq_server_alloc_meta(seq->lcs_srv, NULL,
185                                            &seq->lcs_space, env);
186         } else {
187 #endif
188                 rc = seq_client_rpc(seq, NULL, &seq->lcs_space,
189                                     SEQ_ALLOC_META, "meta");
190 #ifdef __KERNEL__
191         }
192 #endif
193         RETURN(rc);
194 }
195
196 /* Allocate new sequence for client. */
197 static int seq_client_alloc_seq(struct lu_client_seq *seq, seqno_t *seqnr)
198 {
199         int rc;
200         ENTRY;
201
202         LASSERT(range_is_sane(&seq->lcs_space));
203
204         if (range_is_exhausted(&seq->lcs_space)) {
205                 rc = seq_client_alloc_meta(seq, NULL);
206                 if (rc) {
207                         CERROR("%s: Can't allocate new meta-sequence, "
208                                "rc %d\n", seq->lcs_name, rc);
209                         RETURN(rc);
210                 } else {
211                         CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
212                                seq->lcs_name, PRANGE(&seq->lcs_space));
213                 }
214         } else {
215                 rc = 0;
216         }
217
218         LASSERT(!range_is_exhausted(&seq->lcs_space));
219         *seqnr = seq->lcs_space.lsr_start;
220         seq->lcs_space.lsr_start += 1;
221
222         CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
223                *seqnr);
224
225         RETURN(rc);
226 }
227
228 /* Allocate new fid on passed client @seq and save it to @fid. */
229 int seq_client_alloc_fid(struct lu_client_seq *seq, struct lu_fid *fid)
230 {
231         int rc;
232         ENTRY;
233
234         LASSERT(seq != NULL);
235         LASSERT(fid != NULL);
236
237         cfs_down(&seq->lcs_sem);
238
239         if (fid_is_zero(&seq->lcs_fid) ||
240             fid_oid(&seq->lcs_fid) >= seq->lcs_width)
241         {
242                 seqno_t seqnr;
243
244                 rc = seq_client_alloc_seq(seq, &seqnr);
245                 if (rc) {
246                         CERROR("%s: Can't allocate new sequence, "
247                                "rc %d\n", seq->lcs_name, rc);
248                         cfs_up(&seq->lcs_sem);
249                         RETURN(rc);
250                 }
251
252                 CDEBUG(D_INFO, "%s: Switch to sequence "
253                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
254
255                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
256                 seq->lcs_fid.f_seq = seqnr;
257                 seq->lcs_fid.f_ver = 0;
258
259                 /*
260                  * Inform caller that sequence switch is performed to allow it
261                  * to setup FLD for it.
262                  */
263                 rc = 1;
264         } else {
265                 /* Just bump last allocated fid and return to caller. */
266                 seq->lcs_fid.f_oid += 1;
267                 rc = 0;
268         }
269
270         *fid = seq->lcs_fid;
271         cfs_up(&seq->lcs_sem);
272
273         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
274         RETURN(rc);
275 }
276 EXPORT_SYMBOL(seq_client_alloc_fid);
277
278 /*
279  * Finish the current sequence due to disconnect.
280  * See mdc_import_event()
281  */
282 void seq_client_flush(struct lu_client_seq *seq)
283 {
284         LASSERT(seq != NULL);
285         cfs_down(&seq->lcs_sem);
286         fid_zero(&seq->lcs_fid);
287         /**
288          * this id shld not be used for seq range allocation.
289          * set to -1 for dgb check.
290          */
291
292         seq->lcs_space.lsr_mdt = -1;
293
294         range_init(&seq->lcs_space);
295         cfs_up(&seq->lcs_sem);
296 }
297 EXPORT_SYMBOL(seq_client_flush);
298
299 static void seq_client_proc_fini(struct lu_client_seq *seq);
300
301 #ifdef LPROCFS
302 static int seq_client_proc_init(struct lu_client_seq *seq)
303 {
304         int rc;
305         ENTRY;
306
307         seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
308                                              seq_type_proc_dir,
309                                              NULL, NULL);
310
311         if (IS_ERR(seq->lcs_proc_dir)) {
312                 CERROR("%s: LProcFS failed in seq-init\n",
313                        seq->lcs_name);
314                 rc = PTR_ERR(seq->lcs_proc_dir);
315                 RETURN(rc);
316         }
317
318         rc = lprocfs_add_vars(seq->lcs_proc_dir,
319                               seq_client_proc_list, seq);
320         if (rc) {
321                 CERROR("%s: Can't init sequence manager "
322                        "proc, rc %d\n", seq->lcs_name, rc);
323                 GOTO(out_cleanup, rc);
324         }
325
326         RETURN(0);
327
328 out_cleanup:
329         seq_client_proc_fini(seq);
330         return rc;
331 }
332
333 static void seq_client_proc_fini(struct lu_client_seq *seq)
334 {
335         ENTRY;
336         if (seq->lcs_proc_dir) {
337                 if (!IS_ERR(seq->lcs_proc_dir))
338                         lprocfs_remove(&seq->lcs_proc_dir);
339                 seq->lcs_proc_dir = NULL;
340         }
341         EXIT;
342 }
343 #else
344 static int seq_client_proc_init(struct lu_client_seq *seq)
345 {
346         return 0;
347 }
348
349 static void seq_client_proc_fini(struct lu_client_seq *seq)
350 {
351         return;
352 }
353 #endif
354
355 int seq_client_init(struct lu_client_seq *seq,
356                     struct obd_export *exp,
357                     enum lu_cli_type type,
358                     const char *prefix,
359                     struct lu_server_seq *srv)
360 {
361         int rc;
362         ENTRY;
363
364         LASSERT(seq != NULL);
365         LASSERT(prefix != NULL);
366
367         seq->lcs_exp = exp;
368         seq->lcs_srv = srv;
369         seq->lcs_type = type;
370         cfs_sema_init(&seq->lcs_sem, 1);
371         seq->lcs_width = LUSTRE_SEQ_MAX_WIDTH;
372
373         /* Make sure that things are clear before work is started. */
374         seq_client_flush(seq);
375
376         if (exp == NULL) {
377                 LASSERT(seq->lcs_srv != NULL);
378         } else {
379                 LASSERT(seq->lcs_exp != NULL);
380                 seq->lcs_exp = class_export_get(seq->lcs_exp);
381         }
382
383         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
384                  "cli-%s", prefix);
385
386         rc = seq_client_proc_init(seq);
387         if (rc)
388                 seq_client_fini(seq);
389         RETURN(rc);
390 }
391 EXPORT_SYMBOL(seq_client_init);
392
393 void seq_client_fini(struct lu_client_seq *seq)
394 {
395         ENTRY;
396
397         seq_client_proc_fini(seq);
398
399         if (seq->lcs_exp != NULL) {
400                 class_export_put(seq->lcs_exp);
401                 seq->lcs_exp = NULL;
402         }
403
404         seq->lcs_srv = NULL;
405         EXIT;
406 }
407 EXPORT_SYMBOL(seq_client_fini);