-struct rsi {
- struct cache_head h;
- __u32 lustre_svc;
- lnet_nid_t nid4; /* FIXME Support larger NID */
- char nm_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
- wait_queue_head_t waitq;
- rawobj_t in_handle, in_token;
- rawobj_t out_handle, out_token;
- int major_status, minor_status;
-#ifdef HAVE_CACHE_HASH_SPINLOCK
- struct rcu_head rcu_head;
-#endif
-};
-
-#ifdef HAVE_CACHE_HEAD_HLIST
-static struct hlist_head rsi_table[RSI_HASHMAX];
-#else
-static struct cache_head *rsi_table[RSI_HASHMAX];
-#endif
-static struct cache_detail rsi_cache;
-static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
-static struct rsi *rsi_lookup(struct rsi *item);
-
-#ifdef HAVE_CACHE_DETAIL_WRITERS
-static inline int channel_users(struct cache_detail *cd)
-{
- return atomic_read(&cd->writers);
-}
-#else
-static inline int channel_users(struct cache_detail *cd)
-{
- return atomic_read(&cd->readers);
-}
-#endif
-
-static inline int rsi_hash(struct rsi *item)
-{
- return hash_mem((char *)item->in_handle.data, item->in_handle.len,
- RSI_HASHBITS) ^
- hash_mem((char *)item->in_token.data, item->in_token.len,
- RSI_HASHBITS);
-}
-
-static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
-{
- return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
- rawobj_equal(&item->in_token, &tmp->in_token));
-}
-
-static void rsi_free(struct rsi *rsi)
-{
- rawobj_free(&rsi->in_handle);
- rawobj_free(&rsi->in_token);
- rawobj_free(&rsi->out_handle);
- rawobj_free(&rsi->out_token);
-}
-
-/* See handle_channel_req() userspace for where the upcall data is read */
-static void rsi_request(struct cache_detail *cd,
- struct cache_head *h,
- char **bpp, int *blen)
-{
- struct rsi *rsi = container_of(h, struct rsi, h);
- __u64 index = 0;
-
- /* if in_handle is null, provide kernel suggestion */
- if (rsi->in_handle.len == 0)
- index = gss_get_next_ctx_index();
-
- qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
- sizeof(rsi->lustre_svc));
- qword_addhex(bpp, blen, (char *) &rsi->nid4, sizeof(rsi->nid4));
- qword_addhex(bpp, blen, (char *) &index, sizeof(index));
- qword_addhex(bpp, blen, (char *) rsi->nm_name,
- strlen(rsi->nm_name) + 1);
- qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
- qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
- (*bpp)[-1] = '\n';
-}
-
-static inline void __rsi_init(struct rsi *new, struct rsi *item)
-{
- new->out_handle = RAWOBJ_EMPTY;
- new->out_token = RAWOBJ_EMPTY;
-
- new->in_handle = item->in_handle;
- item->in_handle = RAWOBJ_EMPTY;
- new->in_token = item->in_token;
- item->in_token = RAWOBJ_EMPTY;
-
- new->lustre_svc = item->lustre_svc;
- new->nid4 = item->nid4;
- memcpy(new->nm_name, item->nm_name, sizeof(item->nm_name));
- init_waitqueue_head(&new->waitq);
-}
-
-static inline void __rsi_update(struct rsi *new, struct rsi *item)
-{
- LASSERT(new->out_handle.len == 0);
- LASSERT(new->out_token.len == 0);
-
- new->out_handle = item->out_handle;
- item->out_handle = RAWOBJ_EMPTY;
- new->out_token = item->out_token;
- item->out_token = RAWOBJ_EMPTY;
-
- new->major_status = item->major_status;
- new->minor_status = item->minor_status;
-}
-
-#ifdef HAVE_CACHE_HASH_SPINLOCK
-static void rsi_free_rcu(struct rcu_head *head)
-{
- struct rsi *rsi = container_of(head, struct rsi, rcu_head);
-
-#ifdef HAVE_CACHE_HEAD_HLIST
- LASSERT(hlist_unhashed(&rsi->h.cache_list));
-#else
- LASSERT(rsi->h.next == NULL);
-#endif
- rsi_free(rsi);
- OBD_FREE_PTR(rsi);
-}
-
-static void rsi_put(struct kref *ref)
-{
- struct rsi *rsi = container_of(ref, struct rsi, h.ref);
-
- call_rcu(&rsi->rcu_head, rsi_free_rcu);
-}
-#else /* !HAVE_CACHE_HASH_SPINLOCK */
-static void rsi_put(struct kref *ref)
-{
- struct rsi *rsi = container_of(ref, struct rsi, h.ref);
-
-#ifdef HAVE_CACHE_HEAD_HLIST
- LASSERT(hlist_unhashed(&rsi->h.cache_list));
-#else
- LASSERT(rsi->h.next == NULL);
-#endif
- rsi_free(rsi);
- OBD_FREE_PTR(rsi);
-}
-#endif /* HAVE_CACHE_HASH_SPINLOCK */
-
-static int rsi_match(struct cache_head *a, struct cache_head *b)
-{
- struct rsi *item = container_of(a, struct rsi, h);
- struct rsi *tmp = container_of(b, struct rsi, h);
-
- return __rsi_match(item, tmp);
-}
-
-static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
-{
- struct rsi *new = container_of(cnew, struct rsi, h);
- struct rsi *item = container_of(citem, struct rsi, h);
-
- __rsi_init(new, item);
-}
-
-static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
-{
- struct rsi *new = container_of(cnew, struct rsi, h);
- struct rsi *item = container_of(citem, struct rsi, h);
-
- __rsi_update(new, item);
-}
-
-static struct cache_head *rsi_alloc(void)
-{
- struct rsi *rsi;
-
- OBD_ALLOC_PTR(rsi);
- if (rsi)
- return &rsi->h;
- else
- return NULL;
-}
-
-static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
-{
- char *buf = mesg;
- int len;
- struct rsi rsii, *rsip = NULL;
- time64_t expiry;
- int status = -EINVAL;
- ENTRY;
-
- memset(&rsii, 0, sizeof(rsii));
-
- /* handle */
- len = qword_get(&mesg, buf, mlen);
- if (len < 0)
- goto out;
- if (rawobj_alloc(&rsii.in_handle, buf, len)) {
- status = -ENOMEM;
- goto out;
- }
-
- /* token */
- len = qword_get(&mesg, buf, mlen);
- if (len < 0)
- goto out;
- if (rawobj_alloc(&rsii.in_token, buf, len)) {
- status = -ENOMEM;
- goto out;
- }
-
- rsip = rsi_lookup(&rsii);
- if (!rsip)
- goto out;
- if (!test_bit(CACHE_PENDING, &rsip->h.flags)) {
- /* If this is not a pending request, it probably means
- * someone wrote arbitrary data to the init channel.
- * Directly return -EINVAL in this case.
- */
- status = -EINVAL;
- goto out;
- }
-
- rsii.h.flags = 0;
- /* expiry */
- status = get_expiry(&mesg, &expiry);
- if (status)
- goto out;
-
- len = qword_get(&mesg, buf, mlen);
- if (len <= 0)
- goto out;
-
- /* major */
- status = kstrtoint(buf, 10, &rsii.major_status);
- if (status)
- goto out;
-
- /* minor */
- len = qword_get(&mesg, buf, mlen);
- if (len <= 0) {
- status = -EINVAL;
- goto out;
- }
-
- status = kstrtoint(buf, 10, &rsii.minor_status);
- if (status)
- goto out;
-
- /* out_handle */
- len = qword_get(&mesg, buf, mlen);
- if (len < 0)
- goto out;
- if (rawobj_alloc(&rsii.out_handle, buf, len)) {
- status = -ENOMEM;
- goto out;
- }
-
- /* out_token */
- len = qword_get(&mesg, buf, mlen);
- if (len < 0)
- goto out;
- if (rawobj_alloc(&rsii.out_token, buf, len)) {
- status = -ENOMEM;
- goto out;
- }
-
- rsii.h.expiry_time = expiry;
- rsip = rsi_update(&rsii, rsip);
- status = 0;
-out:
- rsi_free(&rsii);
- if (rsip) {
- wake_up(&rsip->waitq);
- cache_put(&rsip->h, &rsi_cache);
- } else {
- status = -ENOMEM;
- }
-
- if (status)
- CERROR("rsi parse error %d\n", status);
- RETURN(status);
-}
-
-static struct cache_detail rsi_cache = {
- .hash_size = RSI_HASHMAX,
- .hash_table = rsi_table,
- .name = "auth.sptlrpc.init",
- .cache_put = rsi_put,
- .cache_request = rsi_request,
- .cache_upcall = sunrpc_cache_pipe_upcall,
- .cache_parse = rsi_parse,
- .match = rsi_match,
- .init = rsi_init,
- .update = update_rsi,
- .alloc = rsi_alloc,
-};
-
-static struct rsi *rsi_lookup(struct rsi *item)
-{
- struct cache_head *ch;
- int hash = rsi_hash(item);
-
- ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
- if (ch)
- return container_of(ch, struct rsi, h);
- else
- return NULL;
-}
-
-static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
-{
- struct cache_head *ch;
- int hash = rsi_hash(new);
-
- ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
- if (ch)
- return container_of(ch, struct rsi, h);
- else
- return NULL;
-}
-