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