/*1* Copyright (c) 2006 Oracle. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*31*/32#include <linux/percpu.h>33#include <linux/seq_file.h>34#include <linux/slab.h>35#include <linux/proc_fs.h>3637#include "rds.h"3839/*40* This file implements a getsockopt() call which copies a set of fixed41* sized structs into a user-specified buffer as a means of providing42* read-only information about RDS.43*44* For a given information source there are a given number of fixed sized45* structs at a given time. The structs are only copied if the user-specified46* buffer is big enough. The destination pages that make up the buffer47* are pinned for the duration of the copy.48*49* This gives us the following benefits:50*51* - simple implementation, no copy "position" across multiple calls52* - consistent snapshot of an info source53* - atomic copy works well with whatever locking info source has54* - one portable tool to get rds info across implementations55* - long-lived tool can get info without allocating56*57* at the following costs:58*59* - info source copy must be pinned, may be "large"60*/6162struct rds_info_iterator {63struct page **pages;64void *addr;65unsigned long offset;66};6768static DEFINE_SPINLOCK(rds_info_lock);69static rds_info_func rds_info_funcs[RDS_INFO_LAST - RDS_INFO_FIRST + 1];7071void rds_info_register_func(int optname, rds_info_func func)72{73int offset = optname - RDS_INFO_FIRST;7475BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST);7677spin_lock(&rds_info_lock);78BUG_ON(rds_info_funcs[offset]);79rds_info_funcs[offset] = func;80spin_unlock(&rds_info_lock);81}82EXPORT_SYMBOL_GPL(rds_info_register_func);8384void rds_info_deregister_func(int optname, rds_info_func func)85{86int offset = optname - RDS_INFO_FIRST;8788BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST);8990spin_lock(&rds_info_lock);91BUG_ON(rds_info_funcs[offset] != func);92rds_info_funcs[offset] = NULL;93spin_unlock(&rds_info_lock);94}95EXPORT_SYMBOL_GPL(rds_info_deregister_func);9697/*98* Typically we hold an atomic kmap across multiple rds_info_copy() calls99* because the kmap is so expensive. This must be called before using blocking100* operations while holding the mapping and as the iterator is torn down.101*/102void rds_info_iter_unmap(struct rds_info_iterator *iter)103{104if (iter->addr) {105kunmap_atomic(iter->addr, KM_USER0);106iter->addr = NULL;107}108}109110/*111* get_user_pages() called flush_dcache_page() on the pages for us.112*/113void rds_info_copy(struct rds_info_iterator *iter, void *data,114unsigned long bytes)115{116unsigned long this;117118while (bytes) {119if (!iter->addr)120iter->addr = kmap_atomic(*iter->pages, KM_USER0);121122this = min(bytes, PAGE_SIZE - iter->offset);123124rdsdebug("page %p addr %p offset %lu this %lu data %p "125"bytes %lu\n", *iter->pages, iter->addr,126iter->offset, this, data, bytes);127128memcpy(iter->addr + iter->offset, data, this);129130data += this;131bytes -= this;132iter->offset += this;133134if (iter->offset == PAGE_SIZE) {135kunmap_atomic(iter->addr, KM_USER0);136iter->addr = NULL;137iter->offset = 0;138iter->pages++;139}140}141}142EXPORT_SYMBOL_GPL(rds_info_copy);143144/*145* @optval points to the userspace buffer that the information snapshot146* will be copied into.147*148* @optlen on input is the size of the buffer in userspace. @optlen149* on output is the size of the requested snapshot in bytes.150*151* This function returns -errno if there is a failure, particularly -ENOSPC152* if the given userspace buffer was not large enough to fit the snapshot.153* On success it returns the positive number of bytes of each array element154* in the snapshot.155*/156int rds_info_getsockopt(struct socket *sock, int optname, char __user *optval,157int __user *optlen)158{159struct rds_info_iterator iter;160struct rds_info_lengths lens;161unsigned long nr_pages = 0;162unsigned long start;163unsigned long i;164rds_info_func func;165struct page **pages = NULL;166int ret;167int len;168int total;169170if (get_user(len, optlen)) {171ret = -EFAULT;172goto out;173}174175/* check for all kinds of wrapping and the like */176start = (unsigned long)optval;177if (len < 0 || len + PAGE_SIZE - 1 < len || start + len < start) {178ret = -EINVAL;179goto out;180}181182/* a 0 len call is just trying to probe its length */183if (len == 0)184goto call_func;185186nr_pages = (PAGE_ALIGN(start + len) - (start & PAGE_MASK))187>> PAGE_SHIFT;188189pages = kmalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);190if (!pages) {191ret = -ENOMEM;192goto out;193}194ret = get_user_pages_fast(start, nr_pages, 1, pages);195if (ret != nr_pages) {196if (ret > 0)197nr_pages = ret;198else199nr_pages = 0;200ret = -EAGAIN; /* XXX ? */201goto out;202}203204rdsdebug("len %d nr_pages %lu\n", len, nr_pages);205206call_func:207func = rds_info_funcs[optname - RDS_INFO_FIRST];208if (!func) {209ret = -ENOPROTOOPT;210goto out;211}212213iter.pages = pages;214iter.addr = NULL;215iter.offset = start & (PAGE_SIZE - 1);216217func(sock, len, &iter, &lens);218BUG_ON(lens.each == 0);219220total = lens.nr * lens.each;221222rds_info_iter_unmap(&iter);223224if (total > len) {225len = total;226ret = -ENOSPC;227} else {228len = total;229ret = lens.each;230}231232if (put_user(len, optlen))233ret = -EFAULT;234235out:236for (i = 0; pages && i < nr_pages; i++)237put_page(pages[i]);238kfree(pages);239240return ret;241}242243244