Whamcloud - gitweb
LU-3467 seq: unified SEQ handler
[fs/lustre-release.git] / lustre / fid / fid_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
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_handler.c
37  *
38  * Lustre Sequence Manager
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FID
44
45 #include <libcfs/libcfs.h>
46 #include <linux/module.h>
47 #include <obd.h>
48 #include <obd_class.h>
49 #include <dt_object.h>
50 #include <md_object.h>
51 #include <obd_support.h>
52 #include <lustre_req_layout.h>
53 #include <lustre_fid.h>
54 #include "fid_internal.h"
55
56 static void seq_server_proc_fini(struct lu_server_seq *seq);
57
58 /* Assigns client to sequence controller node. */
59 int seq_server_set_cli(struct lu_server_seq *seq,
60                        struct lu_client_seq *cli,
61                        const struct lu_env *env)
62 {
63         int rc = 0;
64         ENTRY;
65
66         /*
67          * Ask client for new range, assign that range to ->seq_space and write
68          * seq state to backing store should be atomic.
69          */
70         mutex_lock(&seq->lss_mutex);
71
72         if (cli == NULL) {
73                 CDEBUG(D_INFO, "%s: Detached sequence client %s\n",
74                        seq->lss_name, cli->lcs_name);
75                 seq->lss_cli = cli;
76                 GOTO(out_up, rc = 0);
77         }
78
79         if (seq->lss_cli != NULL) {
80                 CDEBUG(D_HA, "%s: Sequence controller is already "
81                        "assigned\n", seq->lss_name);
82                 GOTO(out_up, rc = -EEXIST);
83         }
84
85         CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
86                seq->lss_name, cli->lcs_name);
87
88         seq->lss_cli = cli;
89         cli->lcs_space.lsr_index = seq->lss_site->ss_node_id;
90         EXIT;
91 out_up:
92         mutex_unlock(&seq->lss_mutex);
93         return rc;
94 }
95 EXPORT_SYMBOL(seq_server_set_cli);
96 /*
97  * allocate \a w units of sequence from range \a from.
98  */
99 static inline void range_alloc(struct lu_seq_range *to,
100                                struct lu_seq_range *from,
101                                __u64 width)
102 {
103         width = min(range_space(from), width);
104         to->lsr_start = from->lsr_start;
105         to->lsr_end = from->lsr_start + width;
106         from->lsr_start += width;
107 }
108
109 /**
110  * On controller node, allocate new super sequence for regular sequence server.
111  * As this super sequence controller, this node suppose to maintain fld
112  * and update index.
113  * \a out range always has currect mds node number of requester.
114  */
115
116 static int __seq_server_alloc_super(struct lu_server_seq *seq,
117                                     struct lu_seq_range *out,
118                                     const struct lu_env *env)
119 {
120         struct lu_seq_range *space = &seq->lss_space;
121         int rc;
122         ENTRY;
123
124         LASSERT(range_is_sane(space));
125
126         if (range_is_exhausted(space)) {
127                 CERROR("%s: Sequences space is exhausted\n",
128                        seq->lss_name);
129                 RETURN(-ENOSPC);
130         } else {
131                 range_alloc(out, space, seq->lss_width);
132         }
133
134         rc = seq_store_update(env, seq, out, 1 /* sync */);
135
136         LCONSOLE_INFO("%s: super-sequence allocation rc = %d " DRANGE"\n",
137                       seq->lss_name, rc, PRANGE(out));
138
139         RETURN(rc);
140 }
141
142 int seq_server_alloc_super(struct lu_server_seq *seq,
143                            struct lu_seq_range *out,
144                            const struct lu_env *env)
145 {
146         int rc;
147         ENTRY;
148
149         mutex_lock(&seq->lss_mutex);
150         rc = __seq_server_alloc_super(seq, out, env);
151         mutex_unlock(&seq->lss_mutex);
152
153         RETURN(rc);
154 }
155
156 static int __seq_set_init(const struct lu_env *env,
157                             struct lu_server_seq *seq)
158 {
159         struct lu_seq_range *space = &seq->lss_space;
160         int rc;
161
162         range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
163         range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
164
165         rc = seq_store_update(env, seq, NULL, 1);
166
167         return rc;
168 }
169
170 /*
171  * This function implements new seq allocation algorithm using async
172  * updates to seq file on disk. ref bug 18857 for details.
173  * there are four variable to keep track of this process
174  *
175  * lss_space; - available lss_space
176  * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
177  * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
178  *                    not yet committed
179  *
180  * when lss_lowater_set reaches the end it is replaced with hiwater one and
181  * a write operation is initiated to allocate new hiwater range.
182  * if last seq write opearion is still not commited, current operation is
183  * flaged as sync write op.
184  */
185 static int range_alloc_set(const struct lu_env *env,
186                             struct lu_seq_range *out,
187                             struct lu_server_seq *seq)
188 {
189         struct lu_seq_range *space = &seq->lss_space;
190         struct lu_seq_range *loset = &seq->lss_lowater_set;
191         struct lu_seq_range *hiset = &seq->lss_hiwater_set;
192         int rc = 0;
193
194         if (range_is_zero(loset))
195                 __seq_set_init(env, seq);
196
197         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
198                 loset->lsr_start = loset->lsr_end;
199
200         if (range_is_exhausted(loset)) {
201                 /* reached high water mark. */
202                 struct lu_device *dev = seq->lss_site->ss_lu->ls_top_dev;
203                 int obd_num_clients = dev->ld_obd->obd_num_exports;
204                 __u64 set_sz;
205
206                 /* calculate new seq width based on number of clients */
207                 set_sz = max(seq->lss_set_width,
208                              obd_num_clients * seq->lss_width);
209                 set_sz = min(range_space(space), set_sz);
210
211                 /* Switch to hiwater range now */
212                 *loset = *hiset;
213                 /* allocate new hiwater range */
214                 range_alloc(hiset, space, set_sz);
215
216                 /* update ondisk seq with new *space */
217                 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
218         }
219
220         LASSERTF(!range_is_exhausted(loset) || range_is_sane(loset),
221                  DRANGE"\n", PRANGE(loset));
222
223         if (rc == 0)
224                 range_alloc(out, loset, seq->lss_width);
225
226         RETURN(rc);
227 }
228
229 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
230                                    struct lu_seq_range *out,
231                                    const struct lu_env *env)
232 {
233         struct lu_seq_range *space = &seq->lss_space;
234         int rc = 0;
235
236         ENTRY;
237
238         LASSERT(range_is_sane(space));
239
240         /* Check if available space ends and allocate new super seq */
241         if (range_is_exhausted(space)) {
242                 if (!seq->lss_cli) {
243                         CERROR("%s: No sequence controller is attached.\n",
244                                seq->lss_name);
245                         RETURN(-ENODEV);
246                 }
247
248                 rc = seq_client_alloc_super(seq->lss_cli, env);
249                 if (rc) {
250                         CERROR("%s: Can't allocate super-sequence, rc %d\n",
251                                seq->lss_name, rc);
252                         RETURN(rc);
253                 }
254
255                 /* Saving new range to allocation space. */
256                 *space = seq->lss_cli->lcs_space;
257                 LASSERT(range_is_sane(space));
258         }
259
260         rc = range_alloc_set(env, out, seq);
261         if (rc != 0) {
262                 CERROR("%s: Allocated meta-sequence failed: rc = %d\n",
263                         seq->lss_name, rc);
264                 RETURN(rc);
265         }
266
267         CDEBUG(D_INFO, "%s: Allocated meta-sequence " DRANGE"\n",
268                 seq->lss_name, PRANGE(out));
269
270         RETURN(rc);
271 }
272
273 int seq_server_alloc_meta(struct lu_server_seq *seq,
274                           struct lu_seq_range *out,
275                           const struct lu_env *env)
276 {
277         int rc;
278         ENTRY;
279
280         mutex_lock(&seq->lss_mutex);
281         rc = __seq_server_alloc_meta(seq, out, env);
282         mutex_unlock(&seq->lss_mutex);
283
284         RETURN(rc);
285 }
286 EXPORT_SYMBOL(seq_server_alloc_meta);
287
288 static int seq_server_handle(struct lu_site *site,
289                              const struct lu_env *env,
290                              __u32 opc, struct lu_seq_range *out)
291 {
292         int rc;
293         struct seq_server_site *ss_site;
294         ENTRY;
295
296         ss_site = lu_site2seq(site);
297
298         switch (opc) {
299         case SEQ_ALLOC_META:
300                 if (!ss_site->ss_server_seq) {
301                         CERROR("Sequence server is not "
302                                "initialized\n");
303                         RETURN(-EINVAL);
304                 }
305                 rc = seq_server_alloc_meta(ss_site->ss_server_seq, out, env);
306                 break;
307         case SEQ_ALLOC_SUPER:
308                 if (!ss_site->ss_control_seq) {
309                         CERROR("Sequence controller is not "
310                                "initialized\n");
311                         RETURN(-EINVAL);
312                 }
313                 rc = seq_server_alloc_super(ss_site->ss_control_seq, out, env);
314                 break;
315         default:
316                 rc = -EINVAL;
317                 break;
318         }
319
320         RETURN(rc);
321 }
322
323 static int seq_handler(struct tgt_session_info *tsi)
324 {
325         struct lu_seq_range     *out, *tmp;
326         struct lu_site          *site;
327         int                      rc;
328         __u32                   *opc;
329
330         ENTRY;
331
332         LASSERT(!(lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY));
333         site = tsi->tsi_exp->exp_obd->obd_lu_dev->ld_site;
334         LASSERT(site != NULL);
335
336         opc = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_OPC);
337         if (opc != NULL) {
338                 out = req_capsule_server_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
339                 if (out == NULL)
340                         RETURN(err_serious(-EPROTO));
341
342                 tmp = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
343
344                 /* seq client passed mdt id, we need to pass that using out
345                  * range parameter */
346
347                 out->lsr_index = tmp->lsr_index;
348                 out->lsr_flags = tmp->lsr_flags;
349                 rc = seq_server_handle(site, tsi->tsi_env, *opc, out);
350         } else {
351                 rc = err_serious(-EPROTO);
352         }
353
354         RETURN(rc);
355 }
356
357 struct tgt_handler seq_handlers[] = {
358 TGT_SEQ_HDL(HABEO_REFERO,       SEQ_QUERY,      seq_handler),
359 };
360 EXPORT_SYMBOL(seq_handlers);
361
362 /* context key constructor/destructor: seq_key_init, seq_key_fini */
363 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
364
365 /* context key: seq_thread_key */
366 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD | LCT_DT_THREAD);
367
368 static int seq_server_proc_init(struct lu_server_seq *seq)
369 {
370 #ifdef LPROCFS
371
372         int rc;
373         ENTRY;
374
375         seq->lss_proc_dir = lprocfs_register(seq->lss_name,
376                                              seq_type_proc_dir,
377                                              NULL, NULL);
378         if (IS_ERR(seq->lss_proc_dir)) {
379                 rc = PTR_ERR(seq->lss_proc_dir);
380                 RETURN(rc);
381         }
382
383         rc = lprocfs_add_vars(seq->lss_proc_dir,
384                               seq_server_proc_list, seq);
385         if (rc) {
386                 CERROR("%s: Can't init sequence manager "
387                        "proc, rc %d\n", seq->lss_name, rc);
388                 GOTO(out_cleanup, rc);
389         }
390
391         RETURN(0);
392
393 out_cleanup:
394         seq_server_proc_fini(seq);
395         return rc;
396 #else /* LPROCFS */
397         return 0;
398 #endif
399 }
400
401 static void seq_server_proc_fini(struct lu_server_seq *seq)
402 {
403 #ifdef LPROCFS
404         ENTRY;
405         if (seq->lss_proc_dir != NULL) {
406                 if (!IS_ERR(seq->lss_proc_dir))
407                         lprocfs_remove(&seq->lss_proc_dir);
408                 seq->lss_proc_dir = NULL;
409         }
410         EXIT;
411 #endif /* LPROCFS */
412 }
413
414 int seq_server_init(struct lu_server_seq *seq,
415                     struct dt_device *dev,
416                     const char *prefix,
417                     enum lu_mgr_type type,
418                     struct seq_server_site *ss,
419                     const struct lu_env *env)
420 {
421         int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
422         ENTRY;
423
424         LASSERT(dev != NULL);
425         LASSERT(prefix != NULL);
426         LASSERT(ss != NULL);
427         LASSERT(ss->ss_lu != NULL);
428
429         seq->lss_cli = NULL;
430         seq->lss_type = type;
431         seq->lss_site = ss;
432         range_init(&seq->lss_space);
433
434         range_init(&seq->lss_lowater_set);
435         range_init(&seq->lss_hiwater_set);
436         seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
437
438         mutex_init(&seq->lss_mutex);
439
440         seq->lss_width = is_srv ?
441                 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
442
443         snprintf(seq->lss_name, sizeof(seq->lss_name),
444                  "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
445
446         rc = seq_store_init(seq, env, dev);
447         if (rc)
448                 GOTO(out, rc);
449         /* Request backing store for saved sequence info. */
450         rc = seq_store_read(seq, env);
451         if (rc == -ENODATA) {
452
453                 /* Nothing is read, init by default value. */
454                 seq->lss_space = is_srv ?
455                         LUSTRE_SEQ_ZERO_RANGE:
456                         LUSTRE_SEQ_SPACE_RANGE;
457
458                 LASSERT(ss != NULL);
459                 seq->lss_space.lsr_index = ss->ss_node_id;
460                 LCONSOLE_INFO("%s: No data found "
461                               "on store. Initialize space\n",
462                               seq->lss_name);
463
464                 rc = seq_store_update(env, seq, NULL, 0);
465                 if (rc) {
466                         CERROR("%s: Can't write space data, "
467                                "rc %d\n", seq->lss_name, rc);
468                 }
469         } else if (rc) {
470                 CERROR("%s: Can't read space data, rc %d\n",
471                        seq->lss_name, rc);
472                 GOTO(out, rc);
473         }
474
475         if (is_srv) {
476                 LASSERT(range_is_sane(&seq->lss_space));
477         } else {
478                 LASSERT(!range_is_zero(&seq->lss_space) &&
479                         range_is_sane(&seq->lss_space));
480         }
481
482         rc  = seq_server_proc_init(seq);
483         if (rc)
484                 GOTO(out, rc);
485
486         EXIT;
487 out:
488         if (rc)
489                 seq_server_fini(seq, env);
490         return rc;
491 }
492 EXPORT_SYMBOL(seq_server_init);
493
494 void seq_server_fini(struct lu_server_seq *seq,
495                      const struct lu_env *env)
496 {
497         ENTRY;
498
499         seq_server_proc_fini(seq);
500         seq_store_fini(seq, env);
501
502         EXIT;
503 }
504 EXPORT_SYMBOL(seq_server_fini);
505
506 int seq_site_fini(const struct lu_env *env, struct seq_server_site *ss)
507 {
508         if (ss == NULL)
509                 RETURN(0);
510
511         if (ss->ss_server_seq) {
512                 seq_server_fini(ss->ss_server_seq, env);
513                 OBD_FREE_PTR(ss->ss_server_seq);
514                 ss->ss_server_seq = NULL;
515         }
516
517         if (ss->ss_control_seq) {
518                 seq_server_fini(ss->ss_control_seq, env);
519                 OBD_FREE_PTR(ss->ss_control_seq);
520                 ss->ss_control_seq = NULL;
521         }
522
523         if (ss->ss_client_seq) {
524                 seq_client_fini(ss->ss_client_seq);
525                 OBD_FREE_PTR(ss->ss_client_seq);
526                 ss->ss_client_seq = NULL;
527         }
528
529         RETURN(0);
530 }
531 EXPORT_SYMBOL(seq_site_fini);
532
533 int fid_server_mod_init(void)
534 {
535         LU_CONTEXT_KEY_INIT(&seq_thread_key);
536         return lu_context_key_register(&seq_thread_key);
537 }
538
539 void fid_server_mod_exit(void)
540 {
541         lu_context_key_degister(&seq_thread_key);
542 }