Whamcloud - gitweb
LU-4423 lustre: don't declare extern variables in C files.
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/fid/fid_handler.c
33  *
34  * Lustre Sequence Manager
35  *
36  * Author: Yury Umanets <umka@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_FID
40
41 #include <libcfs/libcfs.h>
42 #include <linux/module.h>
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <dt_object.h>
46 #include <obd_support.h>
47 #include <lustre_req_layout.h>
48 #include <lustre_fid.h>
49 #include "fid_internal.h"
50
51 /* Assigns client to sequence controller node. */
52 int seq_server_set_cli(const struct lu_env *env, struct lu_server_seq *seq,
53                        struct lu_client_seq *cli)
54 {
55         int rc = 0;
56         ENTRY;
57
58         /*
59          * Ask client for new range, assign that range to ->seq_space and write
60          * seq state to backing store should be atomic.
61          */
62         mutex_lock(&seq->lss_mutex);
63
64         if (cli == NULL) {
65                 CDEBUG(D_INFO, "%s: Detached sequence client\n", seq->lss_name);
66                 seq->lss_cli = NULL;
67                 GOTO(out_up, rc = 0);
68         }
69
70         if (seq->lss_cli != NULL) {
71                 CDEBUG(D_HA, "%s: Sequence controller is already "
72                        "assigned\n", seq->lss_name);
73                 GOTO(out_up, rc = -EEXIST);
74         }
75
76         CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
77                seq->lss_name, cli->lcs_name);
78
79         seq->lss_cli = cli;
80         cli->lcs_space.lsr_index = seq->lss_site->ss_node_id;
81         EXIT;
82 out_up:
83         mutex_unlock(&seq->lss_mutex);
84         return rc;
85 }
86 EXPORT_SYMBOL(seq_server_set_cli);
87 /*
88  * allocate \a w units of sequence from range \a from.
89  */
90 static inline void range_alloc(struct lu_seq_range *to,
91                                struct lu_seq_range *from,
92                                __u64 width)
93 {
94         width = min(lu_seq_range_space(from), width);
95         to->lsr_start = from->lsr_start;
96         to->lsr_end = from->lsr_start + width;
97         from->lsr_start += width;
98 }
99
100 /**
101  * On controller node, allocate new super sequence for regular sequence server.
102  * As this super sequence controller, this node suppose to maintain fld
103  * and update index.
104  * \a out range always has currect mds node number of requester.
105  */
106
107 static int __seq_server_alloc_super(struct lu_server_seq *seq,
108                                     struct lu_seq_range *out,
109                                     const struct lu_env *env)
110 {
111         struct lu_seq_range *space = &seq->lss_space;
112         int rc;
113         ENTRY;
114
115         LASSERT(lu_seq_range_is_sane(space));
116
117         if (lu_seq_range_is_exhausted(space)) {
118                 CERROR("%s: Sequences space is exhausted\n",
119                        seq->lss_name);
120                 RETURN(-ENOSPC);
121         } else {
122                 range_alloc(out, space, seq->lss_width);
123         }
124
125         rc = seq_store_update(env, seq, out, 1 /* sync */);
126
127         LCONSOLE_INFO("%s: super-sequence allocation rc = %d " DRANGE"\n",
128                       seq->lss_name, rc, PRANGE(out));
129
130         RETURN(rc);
131 }
132
133 int seq_server_alloc_super(struct lu_server_seq *seq,
134                            struct lu_seq_range *out,
135                            const struct lu_env *env)
136 {
137         int rc;
138         ENTRY;
139
140         mutex_lock(&seq->lss_mutex);
141         rc = __seq_server_alloc_super(seq, out, env);
142         mutex_unlock(&seq->lss_mutex);
143
144         RETURN(rc);
145 }
146
147 int seq_server_alloc_spec(struct lu_server_seq *seq,
148                           struct lu_seq_range *spec,
149                           const struct lu_env *env)
150 {
151         struct lu_seq_range *space = &seq->lss_space;
152         int rc = -ENOSPC;
153         ENTRY;
154
155         /*
156          * In some cases (like recovery after a disaster)
157          * we may need to allocate sequences manually
158          * Notice some sequences can be lost if requested
159          * range doesn't start at the beginning of current
160          * free space. Also notice it's not possible now
161          * to allocate sequences out of natural order.
162          */
163         if (spec->lsr_start >= spec->lsr_end)
164                 RETURN(-EINVAL);
165         if (spec->lsr_flags != LU_SEQ_RANGE_MDT &&
166             spec->lsr_flags != LU_SEQ_RANGE_OST)
167                 RETURN(-EINVAL);
168
169         mutex_lock(&seq->lss_mutex);
170         if (spec->lsr_start >= space->lsr_start) {
171                 space->lsr_start = spec->lsr_end;
172                 rc = seq_store_update(env, seq, spec, 1 /* sync */);
173
174                 LCONSOLE_INFO("%s: "DRANGE" sequences allocated: rc = %d \n",
175                               seq->lss_name, PRANGE(spec), rc);
176         }
177         mutex_unlock(&seq->lss_mutex);
178
179         RETURN(rc);
180 }
181
182 static int __seq_set_init(const struct lu_env *env,
183                             struct lu_server_seq *seq)
184 {
185         struct lu_seq_range *space = &seq->lss_space;
186         int rc;
187
188         range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
189         range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
190
191         rc = seq_store_update(env, seq, NULL, 1);
192
193         return rc;
194 }
195
196 /*
197  * This function implements new seq allocation algorithm using async
198  * updates to seq file on disk. ref bug 18857 for details.
199  * there are four variable to keep track of this process
200  *
201  * lss_space; - available lss_space
202  * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
203  * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
204  *                    not yet committed
205  *
206  * when lss_lowater_set reaches the end it is replaced with hiwater one and
207  * a write operation is initiated to allocate new hiwater range.
208  * if last seq write opearion is still not committed, current operation is
209  * flaged as sync write op.
210  */
211 static int range_alloc_set(const struct lu_env *env,
212                            struct lu_seq_range *out,
213                            struct lu_server_seq *seq)
214 {
215         struct lu_seq_range *space = &seq->lss_space;
216         struct lu_seq_range *loset = &seq->lss_lowater_set;
217         struct lu_seq_range *hiset = &seq->lss_hiwater_set;
218         int rc = 0;
219
220         if (lu_seq_range_is_zero(loset))
221                 __seq_set_init(env, seq);
222
223         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
224                 loset->lsr_start = loset->lsr_end;
225
226         if (lu_seq_range_is_exhausted(loset)) {
227                 /* reached high water mark. */
228                 struct lu_device *dev = seq->lss_site->ss_lu->ls_top_dev;
229                 int obd_num_clients = dev->ld_obd->obd_num_exports;
230                 __u64 set_sz;
231
232                 /* calculate new seq width based on number of clients */
233                 set_sz = max(seq->lss_set_width,
234                              obd_num_clients * seq->lss_width);
235                 set_sz = min(lu_seq_range_space(space), set_sz);
236
237                 /* Switch to hiwater range now */
238                 *loset = *hiset;
239                 /* allocate new hiwater range */
240                 range_alloc(hiset, space, set_sz);
241
242                 /* update ondisk seq with new *space */
243                 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
244         }
245
246         LASSERTF(!lu_seq_range_is_exhausted(loset) ||
247                  lu_seq_range_is_sane(loset),
248                  DRANGE"\n", PRANGE(loset));
249
250         if (rc == 0)
251                 range_alloc(out, loset, seq->lss_width);
252
253         RETURN(rc);
254 }
255
256 /**
257  * Check if the sequence server has sequence avaible
258  *
259  * Check if the sequence server has sequence avaible, if not, then
260  * allocating super sequence from sequence manager (MDT0).
261  *
262  * \param[in] env       execution environment
263  * \param[in] seq       server sequence
264  *
265  * \retval              negative errno if allocating new sequence fails
266  * \retval              0 if there is enough sequence or allocating
267  *                      new sequence succeeds
268  */
269 int seq_server_check_and_alloc_super(const struct lu_env *env,
270                                      struct lu_server_seq *seq)
271 {
272         struct lu_seq_range *space = &seq->lss_space;
273         int rc = 0;
274
275         ENTRY;
276
277         /* Check if available space ends and allocate new super seq */
278         if (lu_seq_range_is_exhausted(space)) {
279                 if (!seq->lss_cli) {
280                         CERROR("%s: No sequence controller is attached.\n",
281                                seq->lss_name);
282                         RETURN(-ENODEV);
283                 }
284
285                 rc = seq_client_alloc_super(seq->lss_cli, env);
286                 if (rc) {
287                         CDEBUG(D_HA, "%s: Can't allocate super-sequence:"
288                               " rc %d\n", seq->lss_name, rc);
289                         RETURN(rc);
290                 }
291
292                 /* Saving new range to allocation space. */
293                 *space = seq->lss_cli->lcs_space;
294                 LASSERT(lu_seq_range_is_sane(space));
295                 if (seq->lss_cli->lcs_srv == NULL) {
296                         struct lu_server_fld *fld;
297
298                         /* Insert it to the local FLDB */
299                         fld = seq->lss_site->ss_server_fld;
300                         mutex_lock(&fld->lsf_lock);
301                         rc = fld_insert_entry(env, fld, space);
302                         mutex_unlock(&fld->lsf_lock);
303                 }
304         }
305
306         if (lu_seq_range_is_zero(&seq->lss_lowater_set))
307                 __seq_set_init(env, seq);
308
309         RETURN(rc);
310 }
311 EXPORT_SYMBOL(seq_server_check_and_alloc_super);
312
313 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
314                                    struct lu_seq_range *out,
315                                    const struct lu_env *env)
316 {
317         struct lu_seq_range *space = &seq->lss_space;
318         int rc = 0;
319
320         ENTRY;
321
322         LASSERT(lu_seq_range_is_sane(space));
323
324         rc = seq_server_check_and_alloc_super(env, seq);
325         if (rc < 0) {
326                 if (rc == -EINPROGRESS) {
327                         static int printed;
328
329                         if (printed++ % 8 == 0)
330                                 LCONSOLE_INFO("%s: Waiting to contact MDT0000 "
331                                               "to allocate super-sequence\n",
332                                               seq->lss_name);
333                 } else {
334                         CERROR("%s: Allocated super-sequence failed: rc = %d\n",
335                                seq->lss_name, rc);
336                 }
337                 RETURN(rc);
338         }
339
340         rc = range_alloc_set(env, out, seq);
341         if (rc != 0) {
342                 CERROR("%s: Allocated meta-sequence failed: rc = %d\n",
343                         seq->lss_name, rc);
344                 RETURN(rc);
345         }
346
347         CDEBUG(D_INFO, "%s: Allocated meta-sequence " DRANGE"\n",
348                 seq->lss_name, PRANGE(out));
349
350         RETURN(rc);
351 }
352
353 int seq_server_alloc_meta(struct lu_server_seq *seq,
354                           struct lu_seq_range *out,
355                           const struct lu_env *env)
356 {
357         int rc;
358         ENTRY;
359
360         mutex_lock(&seq->lss_mutex);
361         rc = __seq_server_alloc_meta(seq, out, env);
362         mutex_unlock(&seq->lss_mutex);
363
364         RETURN(rc);
365 }
366 EXPORT_SYMBOL(seq_server_alloc_meta);
367
368 static int seq_server_handle(struct lu_site *site,
369                              const struct lu_env *env,
370                              __u32 opc, struct lu_seq_range *out)
371 {
372         int rc;
373         struct seq_server_site *ss_site;
374         struct dt_device *dev;
375         ENTRY;
376
377         ss_site = lu_site2seq(site);
378
379         switch (opc) {
380         case SEQ_ALLOC_META:
381                 if (!ss_site->ss_server_seq) {
382                         CERROR("Sequence server is not "
383                                "initialized\n");
384                         RETURN(-EINVAL);
385                 }
386
387                 dev = lu2dt_dev(ss_site->ss_server_seq->lss_obj->do_lu.lo_dev);
388                 if (dev->dd_rdonly)
389                         RETURN(-EROFS);
390
391                 rc = seq_server_alloc_meta(ss_site->ss_server_seq, out, env);
392                 break;
393         case SEQ_ALLOC_SUPER:
394                 if (!ss_site->ss_control_seq) {
395                         CERROR("Sequence controller is not "
396                                "initialized\n");
397                         RETURN(-EINVAL);
398                 }
399
400                 dev = lu2dt_dev(ss_site->ss_control_seq->lss_obj->do_lu.lo_dev);
401                 if (dev->dd_rdonly)
402                         RETURN(-EROFS);
403
404                 rc = seq_server_alloc_super(ss_site->ss_control_seq, out, env);
405                 break;
406         default:
407                 rc = -EINVAL;
408                 break;
409         }
410
411         RETURN(rc);
412 }
413
414 static int seq_handler(struct tgt_session_info *tsi)
415 {
416         struct lu_seq_range     *out, *tmp;
417         struct lu_site          *site;
418         int                      rc;
419         __u32                   *opc;
420
421         ENTRY;
422
423         LASSERT(!(lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY));
424         site = tsi->tsi_exp->exp_obd->obd_lu_dev->ld_site;
425         LASSERT(site != NULL);
426
427         opc = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_OPC);
428         if (opc != NULL) {
429                 out = req_capsule_server_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
430                 if (out == NULL)
431                         RETURN(err_serious(-EPROTO));
432
433                 tmp = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
434
435                 /* seq client passed mdt id, we need to pass that using out
436                  * range parameter */
437
438                 out->lsr_index = tmp->lsr_index;
439                 out->lsr_flags = tmp->lsr_flags;
440                 rc = seq_server_handle(site, tsi->tsi_env, *opc, out);
441         } else {
442                 rc = err_serious(-EPROTO);
443         }
444
445         RETURN(rc);
446 }
447
448 struct tgt_handler seq_handlers[] = {
449 TGT_SEQ_HDL(HAS_REPLY,  SEQ_QUERY,      seq_handler),
450 };
451 EXPORT_SYMBOL(seq_handlers);
452
453 /* context key constructor/destructor: seq_key_init, seq_key_fini */
454 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
455
456 /* context key: seq_thread_key */
457 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD | LCT_DT_THREAD);
458
459 static void seq_server_debugfs_fini(struct lu_server_seq *seq)
460 {
461         if (!IS_ERR_OR_NULL(seq->lss_debugfs_entry))
462                 ldebugfs_remove(&seq->lss_debugfs_entry);
463 }
464
465 static int seq_server_debugfs_init(struct lu_server_seq *seq)
466 {
467         int rc;
468         ENTRY;
469
470         seq->lss_debugfs_entry = ldebugfs_register(seq->lss_name,
471                                                    seq_debugfs_dir,
472                                                    NULL, NULL);
473         if (IS_ERR_OR_NULL(seq->lss_debugfs_entry)) {
474                 rc = seq->lss_debugfs_entry ? PTR_ERR(seq->lss_debugfs_entry)
475                                             : -ENOMEM;
476                 seq->lss_debugfs_entry = NULL;
477                 RETURN(rc);
478         }
479
480         rc = ldebugfs_add_vars(seq->lss_debugfs_entry,
481                                seq_server_debugfs_list, seq);
482         if (rc) {
483                 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
484                        seq->lss_name, rc);
485                 GOTO(out_cleanup, rc);
486         }
487
488         if (seq->lss_type == LUSTRE_SEQ_CONTROLLER) {
489                 rc = ldebugfs_seq_create(seq->lss_debugfs_entry, "fldb", 0644,
490                                          &seq_fld_debugfs_seq_fops, seq);
491                 if (rc) {
492                         CERROR("%s: Can't create fldb for sequence manager debugfs: rc = %d\n",
493                                seq->lss_name, rc);
494                         GOTO(out_cleanup, rc);
495                 }
496         }
497
498         RETURN(0);
499
500 out_cleanup:
501         seq_server_debugfs_fini(seq);
502         return rc;
503 }
504
505 int seq_server_init(const struct lu_env *env,
506                     struct lu_server_seq *seq,
507                     struct dt_device *dev,
508                     const char *prefix,
509                     enum lu_mgr_type type,
510                     struct seq_server_site *ss)
511 {
512         int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
513         ENTRY;
514
515         LASSERT(dev != NULL);
516         LASSERT(prefix != NULL);
517         LASSERT(ss != NULL);
518         LASSERT(ss->ss_lu != NULL);
519
520         /* A compile-time check for FIDs that used to be in lustre_idl.h
521          * but is moved here to remove CLASSERT/LASSERT in that header.
522          * Check all lu_fid fields are converted in fid_cpu_to_le() and friends
523          * and that there is no padding added by compiler to the struct. */
524         {
525                 struct lu_fid tst;
526
527                 CLASSERT(sizeof(tst) == sizeof(tst.f_seq) +
528                          sizeof(tst.f_oid) + sizeof(tst.f_ver));
529         }
530
531         seq->lss_cli = NULL;
532         seq->lss_type = type;
533         seq->lss_site = ss;
534         lu_seq_range_init(&seq->lss_space);
535
536         lu_seq_range_init(&seq->lss_lowater_set);
537         lu_seq_range_init(&seq->lss_hiwater_set);
538         seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
539
540         mutex_init(&seq->lss_mutex);
541
542         seq->lss_width = is_srv ?
543                 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
544
545         snprintf(seq->lss_name, sizeof(seq->lss_name),
546                  "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
547
548         rc = seq_store_init(seq, env, dev);
549         if (rc)
550                 GOTO(out, rc);
551         /* Request backing store for saved sequence info. */
552         rc = seq_store_read(seq, env);
553         if (rc == -ENODATA) {
554
555                 /* Nothing is read, init by default value. */
556                 seq->lss_space = is_srv ?
557                         LUSTRE_SEQ_ZERO_RANGE:
558                         LUSTRE_SEQ_SPACE_RANGE;
559
560                 seq->lss_space.lsr_index = ss->ss_node_id;
561                 LCONSOLE_INFO("%s: No data found "
562                               "on store. Initialize space\n",
563                               seq->lss_name);
564
565                 rc = seq_store_update(env, seq, NULL, 0);
566                 if (rc) {
567                         CERROR("%s: Can't write space data, "
568                                "rc %d\n", seq->lss_name, rc);
569                 }
570         } else if (rc) {
571                 CERROR("%s: Can't read space data, rc %d\n",
572                        seq->lss_name, rc);
573                 GOTO(out, rc);
574         }
575
576         if (is_srv) {
577                 LASSERT(lu_seq_range_is_sane(&seq->lss_space));
578         } else {
579                 LASSERT(!lu_seq_range_is_zero(&seq->lss_space) &&
580                         lu_seq_range_is_sane(&seq->lss_space));
581         }
582
583         rc  = seq_server_debugfs_init(seq);
584         if (rc)
585                 GOTO(out, rc);
586
587         EXIT;
588 out:
589         if (rc)
590                 seq_server_fini(seq, env);
591         return rc;
592 }
593 EXPORT_SYMBOL(seq_server_init);
594
595 void seq_server_fini(struct lu_server_seq *seq,
596                      const struct lu_env *env)
597 {
598         ENTRY;
599
600         seq_server_debugfs_fini(seq);
601         seq_store_fini(seq, env);
602
603         EXIT;
604 }
605 EXPORT_SYMBOL(seq_server_fini);
606
607 int seq_site_fini(const struct lu_env *env, struct seq_server_site *ss)
608 {
609         if (ss == NULL)
610                 RETURN(0);
611
612         if (ss->ss_server_seq) {
613                 seq_server_fini(ss->ss_server_seq, env);
614                 OBD_FREE_PTR(ss->ss_server_seq);
615                 ss->ss_server_seq = NULL;
616         }
617
618         if (ss->ss_control_seq) {
619                 seq_server_fini(ss->ss_control_seq, env);
620                 OBD_FREE_PTR(ss->ss_control_seq);
621                 ss->ss_control_seq = NULL;
622         }
623
624         if (ss->ss_client_seq) {
625                 seq_client_fini(ss->ss_client_seq);
626                 OBD_FREE_PTR(ss->ss_client_seq);
627                 ss->ss_client_seq = NULL;
628         }
629
630         RETURN(0);
631 }
632 EXPORT_SYMBOL(seq_site_fini);
633
634 int fid_server_mod_init(void)
635 {
636         LU_CONTEXT_KEY_INIT(&seq_thread_key);
637         return lu_context_key_register(&seq_thread_key);
638 }
639
640 void fid_server_mod_exit(void)
641 {
642         lu_context_key_degister(&seq_thread_key);
643 }