struct ptlrpc_connection * ptlrpc_connection_get(struct lnet_process_id peer, lnet_nid_t self, struct obd_uuid *uuid) { struct ptlrpc_connection *conn, *conn2; ENTRY; peer.nid = LNetPrimaryNID(peer.nid); conn = cfs_hash_lookup(conn_hash, &peer); if (conn) GOTO(out, conn); OBD_ALLOC_PTR(conn); if (!conn) RETURN(NULL); conn->c_peer = peer; conn->c_self = self; INIT_HLIST_NODE(&conn->c_hash); atomic_set(&conn->c_refcount, 1); if (uuid) obd_str2uuid(&conn->c_remote_uuid, uuid->uuid); /* * Add the newly created conn to the hash, on key collision we * lost a racing addition and must destroy our newly allocated * connection. The object which exists in the hash will be * returned and may be compared against out object. */ /* In the function below, .hs_keycmp resolves to * conn_keycmp() */ /* coverity[overrun-buffer-val] */ conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash); if (conn != conn2) { OBD_FREE_PTR(conn); conn = conn2; } EXIT; out: CDEBUG(D_INFO, "conn=%p refcount %d to %s\n", conn, atomic_read(&conn->c_refcount), libcfs_nid2str(conn->c_peer.nid)); return conn; }
struct ptlrpc_connection * ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self, struct obd_uuid *uuid) { struct ptlrpc_connection *conn, *conn2; conn = cfs_hash_lookup(conn_hash, &peer); if (conn) goto out; conn = kzalloc(sizeof(*conn), GFP_NOFS); if (!conn) return NULL; conn->c_peer = peer; conn->c_self = self; INIT_HLIST_NODE(&conn->c_hash); atomic_set(&conn->c_refcount, 1); if (uuid) obd_str2uuid(&conn->c_remote_uuid, uuid->uuid); /* * Add the newly created conn to the hash, on key collision we * lost a racing addition and must destroy our newly allocated * connection. The object which exists in the has will be * returned and may be compared against out object. */ /* In the function below, .hs_keycmp resolves to * conn_keycmp() */ /* coverity[overrun-buffer-val] */ conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash); if (conn != conn2) { kfree(conn); conn = conn2; } out: CDEBUG(D_INFO, "conn=%p refcount %d to %s\n", conn, atomic_read(&conn->c_refcount), libcfs_nid2str(conn->c_peer.nid)); return conn; }
int lprocfs_exp_setup(struct obd_export *exp, lnet_nid_t *nid, int *newnid) { struct nid_stat *new_stat, *old_stat; struct obd_device *obd = NULL; struct proc_dir_entry *entry; char *buffer = NULL; int rc = 0; ENTRY; *newnid = 0; if (!exp || !exp->exp_obd || !exp->exp_obd->obd_proc_exports_entry || !exp->exp_obd->obd_nid_stats_hash) RETURN(-EINVAL); /* not test against zero because eric say: * You may only test nid against another nid, or LNET_NID_ANY. * Anything else is nonsense.*/ if (!nid || *nid == LNET_NID_ANY) RETURN(0); spin_lock(&exp->exp_lock); if (exp->exp_nid_stats != NULL) { spin_unlock(&exp->exp_lock); RETURN(-EALREADY); } spin_unlock(&exp->exp_lock); obd = exp->exp_obd; CDEBUG(D_CONFIG, "using hash %p\n", obd->obd_nid_stats_hash); OBD_ALLOC_PTR(new_stat); if (new_stat == NULL) RETURN(-ENOMEM); new_stat->nid = *nid; new_stat->nid_obd = exp->exp_obd; /* we need set default refcount to 1 to balance obd_disconnect */ atomic_set(&new_stat->nid_exp_ref_count, 1); old_stat = cfs_hash_findadd_unique(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash); CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n", old_stat, libcfs_nid2str(*nid), atomic_read(&new_stat->nid_exp_ref_count)); /* Return -EALREADY here so that we know that the /proc * entry already has been created */ if (old_stat != new_stat) { spin_lock(&exp->exp_lock); if (exp->exp_nid_stats) { LASSERT(exp->exp_nid_stats == old_stat); nidstat_putref(exp->exp_nid_stats); } exp->exp_nid_stats = old_stat; spin_unlock(&exp->exp_lock); GOTO(destroy_new, rc = -EALREADY); } /* not found - create */ OBD_ALLOC(buffer, LNET_NIDSTR_SIZE); if (buffer == NULL) GOTO(destroy_new, rc = -ENOMEM); memcpy(buffer, libcfs_nid2str(*nid), LNET_NIDSTR_SIZE); #ifndef HAVE_ONLY_PROCFS_SEQ new_stat->nid_proc = lprocfs_register(buffer, obd->obd_proc_exports_entry, NULL, NULL); #else new_stat->nid_proc = lprocfs_seq_register(buffer, obd->obd_proc_exports_entry, NULL, NULL); #endif OBD_FREE(buffer, LNET_NIDSTR_SIZE); if (IS_ERR(new_stat->nid_proc)) { rc = PTR_ERR(new_stat->nid_proc); new_stat->nid_proc = NULL; CERROR("%s: cannot create proc entry for export %s: rc = %d\n", obd->obd_name, libcfs_nid2str(*nid), rc); GOTO(destroy_new_ns, rc); } entry = lprocfs_add_simple(new_stat->nid_proc, "uuid", new_stat, &lprocfs_exp_uuid_fops); if (IS_ERR(entry)) { CWARN("Error adding the NID stats file\n"); rc = PTR_ERR(entry); GOTO(destroy_new_ns, rc); } entry = lprocfs_add_simple(new_stat->nid_proc, "hash", new_stat, &lprocfs_exp_hash_fops); if (IS_ERR(entry)) { CWARN("Error adding the hash file\n"); rc = PTR_ERR(entry); GOTO(destroy_new_ns, rc); } spin_lock(&exp->exp_lock); exp->exp_nid_stats = new_stat; spin_unlock(&exp->exp_lock); *newnid = 1; /* protect competitive add to list, not need locking on destroy */ spin_lock(&obd->obd_nid_lock); list_add(&new_stat->nid_list, &obd->obd_nid_stats); spin_unlock(&obd->obd_nid_lock); RETURN(rc); destroy_new_ns: if (new_stat->nid_proc != NULL) lprocfs_remove(&new_stat->nid_proc); cfs_hash_del(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash); destroy_new: nidstat_putref(new_stat); OBD_FREE_PTR(new_stat); RETURN(rc); }
struct lustre_qunit_size *quota_search_lqs(unsigned long long lqs_key, struct lustre_quota_ctxt *qctxt, int create) { struct lustre_qunit_size *lqs; struct lustre_qunit_size *lqs2; cfs_hash_t *hs = NULL; int rc = 0; cfs_spin_lock(&qctxt->lqc_lock); if (qctxt->lqc_valid) { LASSERT(qctxt->lqc_lqs_hash != NULL); hs = cfs_hash_getref(qctxt->lqc_lqs_hash); } cfs_spin_unlock(&qctxt->lqc_lock); if (hs == NULL) { rc = -EBUSY; goto out; } /* cfs_hash_lookup will +1 refcount for caller */ lqs = cfs_hash_lookup(qctxt->lqc_lqs_hash, &lqs_key); if (lqs != NULL) /* found */ goto out_put; if (!create) goto out_put; OBD_ALLOC_PTR(lqs); if (!lqs) { rc = -ENOMEM; goto out_put; } lqs->lqs_key = lqs_key; cfs_spin_lock_init(&lqs->lqs_lock); lqs->lqs_bwrite_pending = 0; lqs->lqs_iwrite_pending = 0; lqs->lqs_ino_rec = 0; lqs->lqs_blk_rec = 0; lqs->lqs_id = LQS_KEY_ID(lqs->lqs_key); lqs->lqs_flags = LQS_KEY_GRP(lqs->lqs_key) ? LQUOTA_FLAGS_GRP : 0; lqs->lqs_bunit_sz = qctxt->lqc_bunit_sz; lqs->lqs_iunit_sz = qctxt->lqc_iunit_sz; lqs->lqs_btune_sz = qctxt->lqc_btune_sz; lqs->lqs_itune_sz = qctxt->lqc_itune_sz; if (qctxt->lqc_handler) { lqs->lqs_last_bshrink = 0; lqs->lqs_last_ishrink = 0; } lqs->lqs_ctxt = qctxt; /* must be called before lqs_initref */ cfs_atomic_set(&lqs->lqs_refcount, 1); /* 1 for caller */ cfs_atomic_inc(&lqs->lqs_ctxt->lqc_lqs); /* lqc_lqs_hash will take +1 refcount on lqs on adding */ lqs2 = cfs_hash_findadd_unique(qctxt->lqc_lqs_hash, &lqs->lqs_key, &lqs->lqs_hash); if (lqs2 == lqs) /* added to hash */ goto out_put; create = 0; lqs_putref(lqs); lqs = lqs2; out_put: cfs_hash_putref(hs); out: if (rc != 0) { /* error */ CERROR("get lqs error(rc: %d)\n", rc); return ERR_PTR(rc); } if (lqs != NULL) { LQS_DEBUG(lqs, "%s\n", (create == 1 ? "create lqs" : "search lqs")); } return lqs; }