Whamcloud - gitweb
LU-969 debug: reduce stack usage
[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 (c) 2007, 2010, Oracle and/or its affiliates. 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,
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         unsigned int           debug_mask;
75         int                    rc;
76         ENTRY;
77
78         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
79                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
80         if (req == NULL)
81                 RETURN(-ENOMEM);
82
83         /* Init operation code */
84         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
85         *op = opc;
86
87         /* Zero out input range, this is not recovery yet. */
88         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
89         range_init(in);
90
91         ptlrpc_request_set_replen(req);
92
93         if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
94                 req->rq_request_portal = SEQ_METADATA_PORTAL;
95                 in->lsr_flags = LU_SEQ_RANGE_MDT;
96         } else {
97                 LASSERTF(seq->lcs_type == LUSTRE_SEQ_DATA,
98                          "unknown lcs_type %u\n", seq->lcs_type);
99                 req->rq_request_portal = SEQ_DATA_PORTAL;
100                 in->lsr_flags = LU_SEQ_RANGE_OST;
101         }
102
103         if (opc == SEQ_ALLOC_SUPER) {
104                 /* Update index field of *in, it is required for
105                  * FLD update on super sequence allocator node. */
106                 in->lsr_index = seq->lcs_space.lsr_index;
107                 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
108                 debug_mask = D_CONSOLE;
109         } else {
110                 debug_mask = D_INFO;
111                 LASSERTF(opc == SEQ_ALLOC_META,
112                          "unknown opcode %u\n, opc", opc);
113         }
114
115         ptlrpc_at_set_req_timeout(req);
116
117         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
118         rc = ptlrpc_queue_wait(req);
119         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
120
121         if (rc)
122                 GOTO(out_req, rc);
123
124         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
125         *output = *out;
126
127         if (!range_is_sane(output)) {
128                 CERROR("%s: Invalid range received from server: "
129                        DRANGE"\n", seq->lcs_name, PRANGE(output));
130                 GOTO(out_req, rc = -EINVAL);
131         }
132
133         if (range_is_exhausted(output)) {
134                 CERROR("%s: Range received from server is exhausted: "
135                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
136                 GOTO(out_req, rc = -EINVAL);
137         }
138
139         CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
140                      seq->lcs_name, opcname, PRANGE(output));
141
142         EXIT;
143 out_req:
144         ptlrpc_req_finished(req);
145         return rc;
146 }
147
148 /* Request sequence-controller node to allocate new super-sequence. */
149 int seq_client_alloc_super(struct lu_client_seq *seq,
150                            const struct lu_env *env)
151 {
152         int rc;
153         ENTRY;
154
155         cfs_down(&seq->lcs_sem);
156
157 #ifdef __KERNEL__
158         if (seq->lcs_srv) {
159                 LASSERT(env != NULL);
160                 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
161                                             env);
162         } else {
163 #endif
164                 rc = seq_client_rpc(seq, &seq->lcs_space,
165                                     SEQ_ALLOC_SUPER, "super");
166 #ifdef __KERNEL__
167         }
168 #endif
169         cfs_up(&seq->lcs_sem);
170         RETURN(rc);
171 }
172
173 /* Request sequence-controller node to allocate new meta-sequence. */
174 static int seq_client_alloc_meta(struct lu_client_seq *seq,
175                                  const struct lu_env *env)
176 {
177         int rc;
178         ENTRY;
179
180 #ifdef __KERNEL__
181         if (seq->lcs_srv) {
182                 LASSERT(env != NULL);
183                 rc = seq_server_alloc_meta(seq->lcs_srv, &seq->lcs_space, env);
184         } else {
185 #endif
186                 rc = seq_client_rpc(seq, &seq->lcs_space,
187                                     SEQ_ALLOC_META, "meta");
188 #ifdef __KERNEL__
189         }
190 #endif
191         RETURN(rc);
192 }
193
194 /* Allocate new sequence for client. */
195 static int seq_client_alloc_seq(struct lu_client_seq *seq, seqno_t *seqnr)
196 {
197         int rc;
198         ENTRY;
199
200         LASSERT(range_is_sane(&seq->lcs_space));
201
202         if (range_is_exhausted(&seq->lcs_space)) {
203                 rc = seq_client_alloc_meta(seq, NULL);
204                 if (rc) {
205                         CERROR("%s: Can't allocate new meta-sequence, "
206                                "rc %d\n", seq->lcs_name, rc);
207                         RETURN(rc);
208                 } else {
209                         CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
210                                seq->lcs_name, PRANGE(&seq->lcs_space));
211                 }
212         } else {
213                 rc = 0;
214         }
215
216         LASSERT(!range_is_exhausted(&seq->lcs_space));
217         *seqnr = seq->lcs_space.lsr_start;
218         seq->lcs_space.lsr_start += 1;
219
220         CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
221                *seqnr);
222
223         RETURN(rc);
224 }
225
226 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
227                               cfs_waitlink_t *link)
228 {
229         if (seq->lcs_update) {
230                 cfs_waitq_add(&seq->lcs_waitq, link);
231                 cfs_set_current_state(CFS_TASK_UNINT);
232                 cfs_up(&seq->lcs_sem);
233
234                 cfs_waitq_wait(link, CFS_TASK_UNINT);
235
236                 cfs_down(&seq->lcs_sem);
237                 cfs_waitq_del(&seq->lcs_waitq, link);
238                 cfs_set_current_state(CFS_TASK_RUNNING);
239                 return -EAGAIN;
240         }
241         ++seq->lcs_update;
242         cfs_up(&seq->lcs_sem);
243         return 0;
244 }
245
246 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
247 {
248         LASSERT(seq->lcs_update == 1);
249         cfs_down(&seq->lcs_sem);
250         --seq->lcs_update;
251         cfs_waitq_signal(&seq->lcs_waitq);
252 }
253
254 /* Allocate new fid on passed client @seq and save it to @fid. */
255 int seq_client_alloc_fid(struct lu_client_seq *seq, struct lu_fid *fid)
256 {
257         cfs_waitlink_t link;
258         int rc;
259         ENTRY;
260
261         LASSERT(seq != NULL);
262         LASSERT(fid != NULL);
263
264         cfs_waitlink_init(&link);
265         cfs_down(&seq->lcs_sem);
266
267         while (1) {
268                 seqno_t seqnr = 0;
269
270                 if (!fid_is_zero(&seq->lcs_fid) &&
271                     fid_oid(&seq->lcs_fid) < seq->lcs_width) {
272                         /* Just bump last allocated fid and return to caller. */
273                         seq->lcs_fid.f_oid += 1;
274                         rc = 0;
275                         break;
276                 }
277
278                 rc = seq_fid_alloc_prep(seq, &link);
279                 if (rc)
280                         continue;
281
282                 rc = seq_client_alloc_seq(seq, &seqnr);
283                 if (rc) {
284                         CERROR("%s: Can't allocate new sequence, "
285                                "rc %d\n", seq->lcs_name, rc);
286                         seq_fid_alloc_fini(seq);
287                         cfs_up(&seq->lcs_sem);
288                         RETURN(rc);
289                 }
290
291                 CDEBUG(D_INFO, "%s: Switch to sequence "
292                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
293
294                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
295                 seq->lcs_fid.f_seq = seqnr;
296                 seq->lcs_fid.f_ver = 0;
297
298                 /*
299                  * Inform caller that sequence switch is performed to allow it
300                  * to setup FLD for it.
301                  */
302                 rc = 1;
303
304                 seq_fid_alloc_fini(seq);
305                 break;
306         }
307
308         *fid = seq->lcs_fid;
309         cfs_up(&seq->lcs_sem);
310
311         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
312         RETURN(rc);
313 }
314 EXPORT_SYMBOL(seq_client_alloc_fid);
315
316 /*
317  * Finish the current sequence due to disconnect.
318  * See mdc_import_event()
319  */
320 void seq_client_flush(struct lu_client_seq *seq)
321 {
322         cfs_waitlink_t link;
323
324         LASSERT(seq != NULL);
325         cfs_waitlink_init(&link);
326         cfs_down(&seq->lcs_sem);
327
328         while (seq->lcs_update) {
329                 cfs_waitq_add(&seq->lcs_waitq, &link);
330                 cfs_set_current_state(CFS_TASK_UNINT);
331                 cfs_up(&seq->lcs_sem);
332
333                 cfs_waitq_wait(&link, CFS_TASK_UNINT);
334
335                 cfs_down(&seq->lcs_sem);
336                 cfs_waitq_del(&seq->lcs_waitq, &link);
337                 cfs_set_current_state(CFS_TASK_RUNNING);
338         }
339
340         fid_zero(&seq->lcs_fid);
341         /**
342          * this id shld not be used for seq range allocation.
343          * set to -1 for dgb check.
344          */
345
346         seq->lcs_space.lsr_index = -1;
347
348         range_init(&seq->lcs_space);
349         cfs_up(&seq->lcs_sem);
350 }
351 EXPORT_SYMBOL(seq_client_flush);
352
353 static void seq_client_proc_fini(struct lu_client_seq *seq);
354
355 #ifdef LPROCFS
356 static int seq_client_proc_init(struct lu_client_seq *seq)
357 {
358         int rc;
359         ENTRY;
360
361         seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
362                                              seq_type_proc_dir,
363                                              NULL, NULL);
364
365         if (IS_ERR(seq->lcs_proc_dir)) {
366                 CERROR("%s: LProcFS failed in seq-init\n",
367                        seq->lcs_name);
368                 rc = PTR_ERR(seq->lcs_proc_dir);
369                 RETURN(rc);
370         }
371
372         rc = lprocfs_add_vars(seq->lcs_proc_dir,
373                               seq_client_proc_list, seq);
374         if (rc) {
375                 CERROR("%s: Can't init sequence manager "
376                        "proc, rc %d\n", seq->lcs_name, rc);
377                 GOTO(out_cleanup, rc);
378         }
379
380         RETURN(0);
381
382 out_cleanup:
383         seq_client_proc_fini(seq);
384         return rc;
385 }
386
387 static void seq_client_proc_fini(struct lu_client_seq *seq)
388 {
389         ENTRY;
390         if (seq->lcs_proc_dir) {
391                 if (!IS_ERR(seq->lcs_proc_dir))
392                         lprocfs_remove(&seq->lcs_proc_dir);
393                 seq->lcs_proc_dir = NULL;
394         }
395         EXIT;
396 }
397 #else
398 static int seq_client_proc_init(struct lu_client_seq *seq)
399 {
400         return 0;
401 }
402
403 static void seq_client_proc_fini(struct lu_client_seq *seq)
404 {
405         return;
406 }
407 #endif
408
409 int seq_client_init(struct lu_client_seq *seq,
410                     struct obd_export *exp,
411                     enum lu_cli_type type,
412                     const char *prefix,
413                     struct lu_server_seq *srv)
414 {
415         int rc;
416         ENTRY;
417
418         LASSERT(seq != NULL);
419         LASSERT(prefix != NULL);
420
421         seq->lcs_exp = exp;
422         seq->lcs_srv = srv;
423         seq->lcs_type = type;
424         cfs_sema_init(&seq->lcs_sem, 1);
425         seq->lcs_width = LUSTRE_SEQ_MAX_WIDTH;
426         cfs_waitq_init(&seq->lcs_waitq);
427
428         /* Make sure that things are clear before work is started. */
429         seq_client_flush(seq);
430
431         if (exp == NULL) {
432                 LASSERT(seq->lcs_srv != NULL);
433         } else {
434                 LASSERT(seq->lcs_exp != NULL);
435                 seq->lcs_exp = class_export_get(seq->lcs_exp);
436         }
437
438         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
439                  "cli-%s", prefix);
440
441         rc = seq_client_proc_init(seq);
442         if (rc)
443                 seq_client_fini(seq);
444         RETURN(rc);
445 }
446 EXPORT_SYMBOL(seq_client_init);
447
448 void seq_client_fini(struct lu_client_seq *seq)
449 {
450         ENTRY;
451
452         seq_client_proc_fini(seq);
453
454         if (seq->lcs_exp != NULL) {
455                 class_export_put(seq->lcs_exp);
456                 seq->lcs_exp = NULL;
457         }
458
459         seq->lcs_srv = NULL;
460         EXIT;
461 }
462 EXPORT_SYMBOL(seq_client_fini);