Whamcloud - gitweb
LU-14462 gss: fix support for namespace in lgss_keyring
[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) {
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) {
71                 CDEBUG(D_HA, "%s: Sequence controller is already assigned\n",
72                        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,
288                                "%s: Can't allocate super-sequence: rc = %d\n",
289                                seq->lss_name, rc);
290                         RETURN(rc);
291                 }
292
293                 /* Saving new range to allocation space. */
294                 *space = seq->lss_cli->lcs_space;
295                 LASSERT(lu_seq_range_is_sane(space));
296                 if (!seq->lss_cli->lcs_srv) {
297                         struct lu_server_fld *fld;
298
299                         /* Insert it to the local FLDB */
300                         fld = seq->lss_site->ss_server_fld;
301                         mutex_lock(&fld->lsf_lock);
302                         rc = fld_insert_entry(env, fld, space);
303                         mutex_unlock(&fld->lsf_lock);
304                 }
305         }
306
307         if (lu_seq_range_is_zero(&seq->lss_lowater_set))
308                 __seq_set_init(env, seq);
309
310         RETURN(rc);
311 }
312 EXPORT_SYMBOL(seq_server_check_and_alloc_super);
313
314 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
315                                    struct lu_seq_range *out,
316                                    const struct lu_env *env)
317 {
318         struct lu_seq_range *space = &seq->lss_space;
319         int rc = 0;
320
321         ENTRY;
322
323         LASSERT(lu_seq_range_is_sane(space));
324
325         rc = seq_server_check_and_alloc_super(env, seq);
326         if (rc < 0) {
327                 if (rc == -EINPROGRESS) {
328                         static int printed;
329
330                         if (printed++ % 8 == 0)
331                                 LCONSOLE_INFO("%s: Waiting to contact MDT0000 to allocate super-sequence: rc = %d\n",
332                                               seq->lss_name, rc);
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                         rc = -EINVAL;
383                         CERROR("Sequence server is not initialized: rc = %d\n",
384                                rc);
385                         RETURN(rc);
386                 }
387
388                 dev = lu2dt_dev(ss_site->ss_server_seq->lss_obj->do_lu.lo_dev);
389                 if (dev->dd_rdonly)
390                         RETURN(-EROFS);
391
392                 rc = seq_server_alloc_meta(ss_site->ss_server_seq, out, env);
393                 break;
394         case SEQ_ALLOC_SUPER:
395                 if (!ss_site->ss_control_seq) {
396                         rc = -EINVAL;
397                         CERROR("Sequence controller is not initialized: rc = %d\n",
398                                rc);
399                         RETURN(rc);
400                 }
401
402                 dev = lu2dt_dev(ss_site->ss_control_seq->lss_obj->do_lu.lo_dev);
403                 if (dev->dd_rdonly)
404                         RETURN(-EROFS);
405
406                 rc = seq_server_alloc_super(ss_site->ss_control_seq, out, env);
407                 break;
408         default:
409                 rc = -EINVAL;
410                 break;
411         }
412
413         RETURN(rc);
414 }
415
416 static int seq_handler(struct tgt_session_info *tsi)
417 {
418         struct lu_seq_range     *out, *tmp;
419         struct lu_site          *site;
420         int                      rc;
421         __u32                   *opc;
422
423         ENTRY;
424
425         LASSERT(!(lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY));
426         site = tsi->tsi_exp->exp_obd->obd_lu_dev->ld_site;
427         LASSERT(site != NULL);
428
429         opc = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_OPC);
430         if (opc) {
431                 out = req_capsule_server_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
432                 if (!out)
433                         RETURN(err_serious(-EPROTO));
434
435                 tmp = req_capsule_client_get(tsi->tsi_pill, &RMF_SEQ_RANGE);
436
437                 /*
438                  * seq client passed mdt id, we need to pass that using out
439                  * range parameter
440                  */
441                 out->lsr_index = tmp->lsr_index;
442                 out->lsr_flags = tmp->lsr_flags;
443                 rc = seq_server_handle(site, tsi->tsi_env, *opc, out);
444         } else {
445                 rc = err_serious(-EPROTO);
446         }
447
448         RETURN(rc);
449 }
450
451 struct tgt_handler seq_handlers[] = {
452 TGT_SEQ_HDL(HAS_REPLY,  SEQ_QUERY,      seq_handler),
453 };
454 EXPORT_SYMBOL(seq_handlers);
455
456 /* context key constructor/destructor: seq_key_init, seq_key_fini */
457 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
458
459 /* context key: seq_thread_key */
460 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD | LCT_DT_THREAD);
461
462 static void seq_server_debugfs_fini(struct lu_server_seq *seq)
463 {
464         debugfs_remove_recursive(seq->lss_debugfs_entry);
465 }
466
467 static void seq_server_debugfs_init(struct lu_server_seq *seq)
468 {
469         ENTRY;
470
471         seq->lss_debugfs_entry = debugfs_create_dir(seq->lss_name,
472                                                     seq_debugfs_dir);
473
474         ldebugfs_add_vars(seq->lss_debugfs_entry,
475                           seq_server_debugfs_list, seq);
476
477         if (seq->lss_type == LUSTRE_SEQ_CONTROLLER)
478                 debugfs_create_file("fldb", 0644, seq->lss_debugfs_entry,
479                                     seq, &seq_fld_debugfs_seq_fops);
480 }
481
482 int seq_server_init(const struct lu_env *env,
483                     struct lu_server_seq *seq,
484                     struct dt_device *dev,
485                     const char *prefix,
486                     enum lu_mgr_type type,
487                     struct seq_server_site *ss)
488 {
489         int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
490         ENTRY;
491
492         LASSERT(dev != NULL);
493         LASSERT(prefix != NULL);
494         LASSERT(ss != NULL);
495         LASSERT(ss->ss_lu != NULL);
496
497         /*
498          * Check all lu_fid fields are converted in fid_cpu_to_le() and friends
499          * and that there is no padding added by compiler to the struct.
500          */
501         {
502                 struct lu_fid tst;
503
504                 BUILD_BUG_ON(sizeof(tst) != sizeof(tst.f_seq) +
505                              sizeof(tst.f_oid) + sizeof(tst.f_ver));
506         }
507
508         seq->lss_cli = NULL;
509         seq->lss_type = type;
510         seq->lss_site = ss;
511         lu_seq_range_init(&seq->lss_space);
512
513         lu_seq_range_init(&seq->lss_lowater_set);
514         lu_seq_range_init(&seq->lss_hiwater_set);
515         seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
516
517         mutex_init(&seq->lss_mutex);
518
519         seq->lss_width = is_srv ?
520                 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
521
522         snprintf(seq->lss_name, sizeof(seq->lss_name),
523                  "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
524
525         rc = seq_store_init(seq, env, dev);
526         if (rc)
527                 GOTO(out, rc);
528         /* Request backing store for saved sequence info. */
529         rc = seq_store_read(seq, env);
530         if (rc == -ENODATA) {
531
532                 /* Nothing is read, init by default value. */
533                 seq->lss_space = is_srv ?
534                         LUSTRE_SEQ_ZERO_RANGE :
535                         LUSTRE_SEQ_SPACE_RANGE;
536
537                 seq->lss_space.lsr_index = ss->ss_node_id;
538                 LCONSOLE_INFO("%s: No data found on store. Initialize space: rc = %d\n",
539                               seq->lss_name, rc);
540
541                 rc = seq_store_update(env, seq, NULL, 0);
542                 if (rc) {
543                         CERROR("%s: Can't write space data: rc = %d\n",
544                                seq->lss_name, rc);
545                 }
546         } else if (rc) {
547                 CERROR("%s: Can't read space data: rc = %d\n",
548                        seq->lss_name, rc);
549                 GOTO(out, rc);
550         }
551
552         if (is_srv) {
553                 LASSERT(lu_seq_range_is_sane(&seq->lss_space));
554         } else {
555                 LASSERT(!lu_seq_range_is_zero(&seq->lss_space) &&
556                         lu_seq_range_is_sane(&seq->lss_space));
557         }
558
559         seq_server_debugfs_init(seq);
560
561         EXIT;
562 out:
563         if (rc)
564                 seq_server_fini(seq, env);
565         return rc;
566 }
567 EXPORT_SYMBOL(seq_server_init);
568
569 void seq_server_fini(struct lu_server_seq *seq,
570                      const struct lu_env *env)
571 {
572         ENTRY;
573
574         seq_server_debugfs_fini(seq);
575         seq_store_fini(seq, env);
576
577         EXIT;
578 }
579 EXPORT_SYMBOL(seq_server_fini);
580
581 int seq_site_fini(const struct lu_env *env, struct seq_server_site *ss)
582 {
583         if (!ss)
584                 RETURN(0);
585
586         if (ss->ss_server_seq) {
587                 seq_server_fini(ss->ss_server_seq, env);
588                 OBD_FREE_PTR(ss->ss_server_seq);
589                 ss->ss_server_seq = NULL;
590         }
591
592         if (ss->ss_control_seq) {
593                 seq_server_fini(ss->ss_control_seq, env);
594                 OBD_FREE_PTR(ss->ss_control_seq);
595                 ss->ss_control_seq = NULL;
596         }
597
598         if (ss->ss_client_seq) {
599                 seq_client_fini(ss->ss_client_seq);
600                 OBD_FREE_PTR(ss->ss_client_seq);
601                 ss->ss_client_seq = NULL;
602         }
603
604         RETURN(0);
605 }
606 EXPORT_SYMBOL(seq_site_fini);
607
608 int fid_server_mod_init(void)
609 {
610         LU_CONTEXT_KEY_INIT(&seq_thread_key);
611         return lu_context_key_register(&seq_thread_key);
612 }
613
614 void fid_server_mod_exit(void)
615 {
616         lu_context_key_degister(&seq_thread_key);
617 }