]> err.no Git - linux-2.6/blobdiff - net/sunrpc/svcauth_unix.c
Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6] / net / sunrpc / svcauth_unix.c
index 1020d54b01d074a7fd8c1d25976a7d9fe800935c..4b775dbf580d819f852afa62be6e4901b93452b4 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/seq_file.h>
 #include <linux/hash.h>
 #include <linux/string.h>
+#include <net/sock.h>
 
 #define RPCDBG_FACILITY        RPCDBG_AUTH
 
@@ -52,6 +53,10 @@ struct auth_domain *unix_domain_find(char *name)
                        return NULL;
                kref_init(&new->h.ref);
                new->h.name = kstrdup(name, GFP_KERNEL);
+               if (new->h.name == NULL) {
+                       kfree(new);
+                       return NULL;
+               }
                new->h.flavour = &svcauth_unix;
                new->addr_changes = 0;
                rv = auth_domain_lookup(name, &new->h);
@@ -100,9 +105,9 @@ static void ip_map_put(struct kref *kref)
  * IP addresses in reverse-endian (i.e. on a little-endian machine).
  * So use a trivial but reliable hash instead
  */
-static inline int hash_ip(unsigned long ip)
+static inline int hash_ip(__be32 ip)
 {
-       int hash = ip ^ (ip>>16);
+       int hash = (__force u32)ip ^ ((__force u32)ip>>16);
        return (hash ^ (hash>>8)) & 0xff;
 }
 #endif
@@ -146,7 +151,7 @@ static void ip_map_request(struct cache_detail *cd,
        char text_addr[20];
        struct ip_map *im = container_of(h, struct ip_map, h);
        __be32 addr = im->m_addr.s_addr;
-       
+
        snprintf(text_addr, 20, "%u.%u.%u.%u",
                 ntohl(addr) >> 24 & 0xff,
                 ntohl(addr) >> 16 & 0xff,
@@ -193,7 +198,7 @@ static int ip_map_parse(struct cache_detail *cd,
 
        if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
                return -EINVAL;
-       
+
        expiry = get_expiry(&mesg);
        if (expiry ==0)
                return -EINVAL;
@@ -243,7 +248,7 @@ static int ip_map_show(struct seq_file *m,
        /* class addr domain */
        addr = im->m_addr;
 
-       if (test_bit(CACHE_VALID, &h->flags) && 
+       if (test_bit(CACHE_VALID, &h->flags) &&
            !test_bit(CACHE_NEGATIVE, &h->flags))
                dom = im->m_client->h.name;
 
@@ -257,7 +262,7 @@ static int ip_map_show(struct seq_file *m,
                   );
        return 0;
 }
-       
+
 
 struct cache_detail ip_map_cache = {
        .owner          = THIS_MODULE,
@@ -283,7 +288,7 @@ static struct ip_map *ip_map_lookup(char *class, struct in_addr addr)
        ip.m_addr = addr;
        ch = sunrpc_cache_lookup(&ip_map_cache, &ip.h,
                                 hash_str(class, IP_HASHBITS) ^
-                                hash_ip((unsigned long)addr.s_addr));
+                                hash_ip(addr.s_addr));
 
        if (ch)
                return container_of(ch, struct ip_map, h);
@@ -312,7 +317,7 @@ static int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t ex
        ch = sunrpc_cache_update(&ip_map_cache,
                                 &ip.h, &ipm->h,
                                 hash_str(ipm->m_class, IP_HASHBITS) ^
-                                hash_ip((unsigned long)ipm->m_addr.s_addr));
+                                hash_ip(ipm->m_addr.s_addr));
        if (!ch)
                return -ENOMEM;
        cache_put(ch, &ip_map_cache);
@@ -338,7 +343,7 @@ int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
 int auth_unix_forget_old(struct auth_domain *dom)
 {
        struct unix_domain *udom;
-       
+
        if (dom->flavour != &svcauth_unix)
                return -EINVAL;
        udom = container_of(dom, struct unix_domain, h);
@@ -348,12 +353,9 @@ int auth_unix_forget_old(struct auth_domain *dom)
 
 struct auth_domain *auth_unix_lookup(struct in_addr addr)
 {
-       struct ip_map key, *ipm;
+       struct ip_map *ipm;
        struct auth_domain *rv;
 
-       strcpy(key.m_class, "nfsd");
-       key.m_addr = addr;
-
        ipm = ip_map_lookup("nfsd", addr);
 
        if (!ipm)
@@ -378,17 +380,58 @@ void svcauth_unix_purge(void)
        cache_purge(&ip_map_cache);
 }
 
+static inline struct ip_map *
+ip_map_cached_get(struct svc_rqst *rqstp)
+{
+       struct ip_map *ipm = rqstp->rq_sock->sk_info_authunix;
+       if (ipm != NULL) {
+               if (!cache_valid(&ipm->h)) {
+                       /*
+                        * The entry has been invalidated since it was
+                        * remembered, e.g. by a second mount from the
+                        * same IP address.
+                        */
+                       rqstp->rq_sock->sk_info_authunix = NULL;
+                       cache_put(&ipm->h, &ip_map_cache);
+                       return NULL;
+               }
+               cache_get(&ipm->h);
+       }
+       return ipm;
+}
+
+static inline void
+ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
+{
+       struct svc_sock *svsk = rqstp->rq_sock;
+
+       if (svsk->sk_sock->type == SOCK_STREAM && svsk->sk_info_authunix == NULL)
+               svsk->sk_info_authunix = ipm;   /* newly cached, keep the reference */
+       else
+               cache_put(&ipm->h, &ip_map_cache);
+}
+
+void
+svcauth_unix_info_release(void *info)
+{
+       struct ip_map *ipm = info;
+       cache_put(&ipm->h, &ip_map_cache);
+}
+
 static int
 svcauth_unix_set_client(struct svc_rqst *rqstp)
 {
+       struct sockaddr_in *sin = svc_addr_in(rqstp);
        struct ip_map *ipm;
 
        rqstp->rq_client = NULL;
        if (rqstp->rq_proc == 0)
                return SVC_OK;
 
-       ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
-                           rqstp->rq_addr.sin_addr);
+       ipm = ip_map_cached_get(rqstp);
+       if (ipm == NULL)
+               ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
+                                   sin->sin_addr);
 
        if (ipm == NULL)
                return SVC_DENIED;
@@ -397,13 +440,14 @@ svcauth_unix_set_client(struct svc_rqst *rqstp)
                default:
                        BUG();
                case -EAGAIN:
+               case -ETIMEDOUT:
                        return SVC_DROP;
                case -ENOENT:
                        return SVC_DENIED;
                case 0:
                        rqstp->rq_client = &ipm->m_client->h;
                        kref_get(&rqstp->rq_client->ref);
-                       cache_put(&ipm->h, &ip_map_cache);
+                       ip_map_cached_put(rqstp, ipm);
                        break;
        }
        return SVC_OK;
@@ -422,7 +466,7 @@ svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
        if (argv->iov_len < 3*4)
                return SVC_GARBAGE;
 
-       if (svc_getu32(argv) != 0) { 
+       if (svc_getu32(argv) != 0) {
                dprintk("svc: bad null cred\n");
                *authp = rpc_autherr_badcred;
                return SVC_DENIED;