Motr  M0
layout_db.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
345 #include "lib/errno.h"
346 #include "lib/memory.h"
347 #include "lib/misc.h" /* memset() */
348 #include "lib/vec.h" /* M0_BUFVEC_INIT_BUF() */
349 #include "lib/finject.h"
350 
351 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LAYOUT
352 #include "lib/trace.h"
353 
354 #include "layout/layout_internal.h"
355 #include "layout/layout_db.h"
356 
370 M0_INTERNAL void m0_layout_pair_set(struct m0_db_pair *pair, uint64_t *lid,
371  void *area, m0_bcount_t num_bytes)
372 {
373  pair->dp_key.db_buf.b_addr = lid;
374  pair->dp_key.db_buf.b_nob = sizeof *lid;
375  pair->dp_rec.db_buf.b_addr = area;
376  pair->dp_rec.db_buf.b_nob = num_bytes;
377 }
378 
379 static int pair_init(struct m0_db_pair *pair,
380  struct m0_layout *l,
381  struct m0_db_tx *tx,
382  enum m0_layout_xcode_op op,
383  m0_bcount_t recsize)
384 {
385  void *key_buf = pair->dp_key.db_buf.b_addr;
386  void *rec_buf = pair->dp_rec.db_buf.b_addr;
387  struct m0_bufvec bv;
388  struct m0_bufvec_cursor rec_cur;
389  int rc;
390 
391  M0_PRE(key_buf != NULL);
392  M0_PRE(rec_buf != NULL);
393  M0_PRE(pair->dp_key.db_buf.b_nob == sizeof l->l_id);
394  M0_PRE(pair->dp_rec.db_buf.b_nob >= recsize);
397  M0_PRE(recsize >= sizeof(struct m0_layout_rec));
398 
399  *(uint64_t *)key_buf = l->l_id;
400  memset(pair->dp_rec.db_buf.b_addr, 0, pair->dp_rec.db_buf.b_nob);
401  m0_db_pair_setup(pair, &l->l_dom->ld_layouts,
402  key_buf, sizeof l->l_id,
403  rec_buf, recsize);
404  if (op == M0_LXO_DB_LOOKUP)
405  rc = 0;
406  else {
407  bv = (struct m0_bufvec)M0_BUFVEC_INIT_BUF(&rec_buf,
408  &pair->dp_rec.db_buf.b_nob);
409  m0_bufvec_cursor_init(&rec_cur, &bv);
410 
411  rc = m0_layout_encode(l, op, tx, &rec_cur);
412  if (rc != 0) {
413  m0_layout__log("pair_init", "m0_layout_encode() failed",
414  l->l_id, rc);
415  m0_db_pair_fini(pair);
416  }
417  }
418  return M0_RC(rc);
419 }
420 
428 M0_INTERNAL int m0_layout_lookup(struct m0_layout_domain *dom,
429  uint64_t lid,
430  struct m0_layout_type *lt,
431  struct m0_db_tx *tx,
432  struct m0_db_pair *pair,
433  struct m0_layout **out)
434 {
435  int rc;
436  struct m0_bufvec bv;
437  struct m0_bufvec_cursor cur;
438  m0_bcount_t max_recsize;
439  m0_bcount_t recsize;
440  struct m0_layout *l;
441  struct m0_layout *ghost;
442 
444  M0_PRE(lid > 0);
445  M0_PRE(lt != NULL);
446  M0_PRE(tx != NULL);
447  M0_PRE(pair != NULL);
448  M0_PRE(out != NULL);
449 
450  M0_ENTRY("lid %llu", (unsigned long long)lid);
451  if (dom->ld_type[lt->lt_id] != lt) {
452  m0_layout__log("m0_layout_lookup", "Unregistered layout type",
453  lid, -EPROTO);
454  return M0_ERR(-EPROTO);
455  }
456 
457  m0_mutex_lock(&dom->ld_lock);
458  l = m0_layout__list_lookup(dom, lid, true);
459  m0_mutex_unlock(&dom->ld_lock);
460  if (l != NULL) {
461  /*
462  * Layout object exists in memory and m0_layout__list_lookup()
463  * has now acquired a reference on it.
464  */
465  *out = l;
467  M0_LEAVE("lid %llu, rc %d", (unsigned long long)lid, 0);
468  return 0;
469  }
470 
471  /* Allocate outside of the domain lock to improve concurrency. */
472  rc = lt->lt_ops->lto_allocate(dom, lid, &l);
473  if (rc != 0) {
474  m0_layout__log("m0_layout_lookup", "lto_allocate() failed",
475  lid, rc);
476  return M0_RC(rc);
477  }
478  /* Here, lto_allocate() has locked l->l_lock. */
479 
480  if (M0_FI_ENABLED("ghost_creation")) {}
481 
482  /* Re-check for possible concurrent layout creation. */
483  m0_mutex_lock(&dom->ld_lock);
484  ghost = m0_layout__list_lookup(dom, lid, true);
485  if (ghost != NULL) {
486  /*
487  * Another instance of the layout with the same layout id
488  * "ghost" was created while the domain lock was released.
489  * Use it. m0_layout__list_lookup() has now acquired a
490  * reference on "ghost".
491  */
492  m0_mutex_unlock(&dom->ld_lock);
493  l->l_ops->lo_delete(l);
494 
495  /* Wait for possible decoding completion. */
496  m0_mutex_lock(&ghost->l_lock);
497  m0_mutex_unlock(&ghost->l_lock);
498 
499  *out = ghost;
501  M0_POST(m0_ref_read(&(*out)->l_ref) > 1);
502  M0_LEAVE("lid %llu, ghost found, rc %d",
503  (unsigned long long)lid, 0);
504  return 0;
505  }
506  m0_mutex_unlock(&dom->ld_lock);
507 
508  max_recsize = m0_layout_max_recsize(dom);
509  recsize = pair->dp_rec.db_buf.b_nob <= max_recsize ?
510  pair->dp_rec.db_buf.b_nob : max_recsize;
511  rc = pair_init(pair, l, tx, M0_LXO_DB_LOOKUP, recsize);
512  M0_ASSERT(rc == 0);
513  rc = m0_table_lookup(tx, pair);
514  if (rc != 0) {
515  /* Error covered in UT. */
516  l->l_ops->lo_delete(l);
517  m0_layout__log("m0_layout_lookup", "m0_table_lookup() failed",
518  lid, rc);
519  goto out;
520  }
521 
522  bv = (struct m0_bufvec)M0_BUFVEC_INIT_BUF(&pair->dp_rec.db_buf.b_addr,
523  &recsize);
524  m0_bufvec_cursor_init(&cur, &bv);
526  if (rc != 0) {
527  /* Error covered in UT. */
528  l->l_ops->lo_delete(l);
529  m0_layout__log("m0_layout_lookup", "m0_layout_decode() failed",
530  lid, rc);
531  goto out;
532  }
533  *out = l;
534  M0_POST(m0_layout__invariant(*out) && m0_ref_read(&l->l_ref) > 0);
535  m0_mutex_unlock(&l->l_lock);
536 out:
537  m0_db_pair_fini(pair);
538  M0_LEAVE("lid %llu, rc %d", (unsigned long long)lid, rc);
539  return M0_RC(rc);
540 }
541 
542 M0_INTERNAL int m0_layout_add(struct m0_layout *l,
543  struct m0_db_tx *tx, struct m0_db_pair *pair)
544 {
545  m0_bcount_t recsize;
546  int rc;
547 
549  M0_PRE(tx != NULL);
550  M0_PRE(pair != NULL);
551 
552  M0_ENTRY("lid %llu", (unsigned long long)l->l_id);
553  m0_mutex_lock(&l->l_lock);
554  recsize = l->l_ops->lo_recsize(l);
555  rc = pair_init(pair, l, tx, M0_LXO_DB_ADD, recsize);
556  if (rc == 0) {
557  rc = m0_table_insert(tx, pair);
558  if (rc != 0)
559  m0_layout__log("m0_layout_add",
560  "m0_table_insert() failed", l->l_id, rc);
561  m0_db_pair_fini(pair);
562  } else
563  m0_layout__log("m0_layout_add", "pair_init() failed",
564  l->l_id, rc);
565  m0_mutex_unlock(&l->l_lock);
566  M0_LEAVE("lid %llu, rc %d", (unsigned long long)l->l_id, rc);
567  return M0_RC(rc);
568 }
569 
570 M0_INTERNAL int m0_layout_update(struct m0_layout *l,
571  struct m0_db_tx *tx, struct m0_db_pair *pair)
572 {
573  m0_bcount_t recsize;
574  int rc;
575 
577  M0_PRE(tx != NULL);
578  M0_PRE(pair != NULL);
579 
580  M0_ENTRY("lid %llu", (unsigned long long)l->l_id);
581  m0_mutex_lock(&l->l_lock);
582  recsize = l->l_ops->lo_recsize(l);
583  rc = pair_init(pair, l, tx, M0_LXO_DB_UPDATE, recsize);
584  if (rc == 0) {
585  if (M0_FI_ENABLED("table_update_err"))
586  { rc = L_TABLE_UPDATE_ERR; goto err1_injected; }
587  rc = m0_table_update(tx, pair);
588 err1_injected:
589  if (rc != 0)
590  m0_layout__log("m0_layout_update",
591  "m0_table_update() failed", l->l_id, rc);
592  m0_db_pair_fini(pair);
593  } else
594  m0_layout__log("m0_layout_update",
595  "pair_init() failed", l->l_id, rc);
596  m0_mutex_unlock(&l->l_lock);
597  M0_LEAVE("lid %llu, rc %d", (unsigned long long)l->l_id, rc);
598  return M0_RC(rc);
599 }
600 
601 M0_INTERNAL int m0_layout_delete(struct m0_layout *l,
602  struct m0_db_tx *tx, struct m0_db_pair *pair)
603 {
604  m0_bcount_t recsize;
605  int rc;
606 
608  M0_PRE(tx != NULL);
609  M0_PRE(pair != NULL);
610 
611  M0_ENTRY("lid %llu", (unsigned long long)l->l_id);
612  m0_mutex_lock(&l->l_lock);
613  if (l->l_user_count > 0) {
614  M0_LOG(M0_ERROR, "lid %llu, user_count %lu, Invalid "
615  "user_count, rc %d", (unsigned long long)l->l_id,
616  (unsigned long)l->l_user_count, -EPROTO);
617  m0_mutex_unlock(&l->l_lock);
618  return M0_ERR(-EPROTO);
619  }
620 
621  recsize = l->l_ops->lo_recsize(l);
622  rc = pair_init(pair, l, tx, M0_LXO_DB_DELETE, recsize);
623  if (rc == 0) {
624  rc = m0_table_delete(tx, pair);
625  if (rc != 0)
626  m0_layout__log("m0_layout_delete",
627  "m0_table_delete() failed", l->l_id, rc);
628  m0_db_pair_fini(pair);
629  } else
630  m0_layout__log("m0_layout_delete",
631  "pair_init() failed", l->l_id, rc);
632  m0_mutex_unlock(&l->l_lock);
633  M0_LEAVE("lid %llu, rc %d", (unsigned long long)l->l_id, rc);
634  return M0_RC(rc);
635 }
636 
637 #undef M0_TRACE_SUBSYSTEM
638 
641 /*
642  * Local variables:
643  * c-indentation-style: "K&R"
644  * c-basic-offset: 8
645  * tab-width: 8
646  * fill-column: 79
647  * scroll-step: 1
648  * End:
649  */
#define M0_BUFVEC_INIT_BUF(addr_ptr, count_ptr)
Definition: vec.h:165
M0_INTERNAL m0_bcount_t m0_layout_max_recsize(const struct m0_layout_domain *dom)
Definition: layout.c:1037
#define M0_PRE(cond)
M0_INTERNAL void m0_mutex_unlock(struct m0_mutex *mutex)
Definition: mutex.c:66
#define NULL
Definition: misc.h:38
M0_INTERNAL int m0_layout_update(struct m0_layout *l, struct m0_db_tx *tx, struct m0_db_pair *pair)
Definition: layout_db.c:570
static struct buffer * cur(struct m0_addb2_mach *mach, m0_bcount_t space)
Definition: addb2.c:791
M0_INTERNAL int m0_layout_add(struct m0_layout *l, struct m0_db_tx *tx, struct m0_db_pair *pair)
Definition: layout_db.c:542
#define M0_LOG(level,...)
Definition: trace.h:167
M0_LEAVE()
uint64_t m0_bcount_t
Definition: types.h:77
M0_INTERNAL void m0_mutex_lock(struct m0_mutex *mutex)
Definition: mutex.c:49
return M0_RC(rc)
op
Definition: libdemo.c:64
#define M0_ENTRY(...)
Definition: trace.h:170
M0_INTERNAL void m0_layout_pair_set(struct m0_db_pair *pair, uint64_t *lid, void *area, m0_bcount_t num_bytes)
Definition: layout_db.c:370
M0_INTERNAL int m0_layout_delete(struct m0_layout *l, struct m0_db_tx *tx, struct m0_db_pair *pair)
Definition: layout_db.c:601
return M0_ERR(-EOPNOTSUPP)
int(* lto_allocate)(struct m0_layout_domain *dom, uint64_t lid, struct m0_layout **out)
Definition: layout.h:401
M0_INTERNAL int m0_layout_lookup(struct m0_layout_domain *dom, uint64_t lid, struct m0_layout_type *lt, struct m0_db_tx *tx, struct m0_db_pair *pair, struct m0_layout **out)
Definition: layout_db.c:428
static int pair_init(struct m0_db_pair *pair, struct m0_layout *l, struct m0_db_tx *tx, enum m0_layout_xcode_op op, m0_bcount_t recsize)
Definition: layout_db.c:379
#define M0_ASSERT(cond)
M0_INTERNAL int m0_layout_encode(struct m0_layout *l, enum m0_layout_xcode_op op, struct m0_be_tx *tx, struct m0_bufvec_cursor *out)
Definition: layout.c:1000
static struct m0_stob_domain * dom
Definition: storage.c:38
M0_INTERNAL void m0_bufvec_cursor_init(struct m0_bufvec_cursor *cur, const struct m0_bufvec *bvec)
Definition: vec.c:563
#define M0_POST(cond)
M0_INTERNAL bool m0_layout__domain_invariant(const struct m0_layout_domain *dom)
Definition: layout.c:131
struct m0_mutex l_lock
Definition: layout.h:247
M0_INTERNAL void m0_layout__log(const char *fn_name, const char *err_msg, uint64_t lid, int rc)
Definition: layout.c:586
uint32_t lt_id
Definition: layout.h:360
M0_INTERNAL int m0_layout_decode(struct m0_layout *l, struct m0_bufvec_cursor *cur, enum m0_layout_xcode_op op, struct m0_be_tx *tx)
Definition: layout.c:947
static struct m0_clink l[NR]
Definition: chan.c:37
const struct m0_layout_type_ops * lt_ops
Definition: layout.h:370
M0_INTERNAL int64_t m0_ref_read(const struct m0_ref *ref)
Definition: refs.c:44
#define M0_FI_ENABLED(tag)
Definition: finject.h:231
M0_INTERNAL bool m0_layout__invariant(const struct m0_layout *l)
Definition: layout.c:155
Definition: xcode.c:1194
#define out(...)
Definition: gen.c:41
M0_INTERNAL struct m0_layout * m0_layout__list_lookup(const struct m0_layout_domain *dom, uint64_t lid, bool ref_increment)
Definition: layout.c:282
int32_t rc
Definition: trigger_fop.h:47
m0_layout_xcode_op
Definition: layout.h:161
Definition: vec.h:145