LCOV - code coverage report
Current view: top level - fs/nfs - sysfs.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 26 65 40.0 %
Date: 2020-09-30 20:25:40 Functions: 6 12 50.0 %
Branches: 7 28 25.0 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  * Copyright (c) 2019 Hammerspace Inc
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <linux/module.h>
       7                 :            : #include <linux/kobject.h>
       8                 :            : #include <linux/sysfs.h>
       9                 :            : #include <linux/fs.h>
      10                 :            : #include <linux/slab.h>
      11                 :            : #include <linux/netdevice.h>
      12                 :            : #include <linux/string.h>
      13                 :            : #include <linux/nfs_fs.h>
      14                 :            : #include <linux/rcupdate.h>
      15                 :            : 
      16                 :            : #include "nfs4_fs.h"
      17                 :            : #include "netns.h"
      18                 :            : #include "sysfs.h"
      19                 :            : 
      20                 :            : struct kobject *nfs_client_kobj;
      21                 :            : static struct kset *nfs_client_kset;
      22                 :            : 
      23                 :          0 : static void nfs_netns_object_release(struct kobject *kobj)
      24                 :            : {
      25                 :          0 :         kfree(kobj);
      26                 :          0 : }
      27                 :            : 
      28                 :        621 : static const struct kobj_ns_type_operations *nfs_netns_object_child_ns_type(
      29                 :            :                 struct kobject *kobj)
      30                 :            : {
      31                 :        621 :         return &net_ns_type_operations;
      32                 :            : }
      33                 :            : 
      34                 :            : static struct kobj_type nfs_netns_object_type = {
      35                 :            :         .release = nfs_netns_object_release,
      36                 :            :         .sysfs_ops = &kobj_sysfs_ops,
      37                 :            :         .child_ns_type = nfs_netns_object_child_ns_type,
      38                 :            : };
      39                 :            : 
      40                 :        207 : static struct kobject *nfs_netns_object_alloc(const char *name,
      41                 :            :                 struct kset *kset, struct kobject *parent)
      42                 :            : {
      43                 :            :         struct kobject *kobj;
      44                 :            : 
      45                 :        207 :         kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
      46         [ +  - ]:        207 :         if (kobj) {
      47                 :        207 :                 kobj->kset = kset;
      48         [ -  + ]:        207 :                 if (kobject_init_and_add(kobj, &nfs_netns_object_type,
      49                 :            :                                         parent, "%s", name) == 0)
      50                 :            :                         return kobj;
      51                 :          0 :                 kobject_put(kobj);
      52                 :            :         }
      53                 :            :         return NULL;
      54                 :            : }
      55                 :            : 
      56                 :        207 : int nfs_sysfs_init(void)
      57                 :            : {
      58                 :        207 :         nfs_client_kset = kset_create_and_add("nfs", NULL, fs_kobj);
      59         [ +  - ]:        207 :         if (!nfs_client_kset)
      60                 :            :                 return -ENOMEM;
      61                 :        207 :         nfs_client_kobj = nfs_netns_object_alloc("net", nfs_client_kset, NULL);
      62         [ -  + ]:        207 :         if  (!nfs_client_kobj) {
      63                 :          0 :                 kset_unregister(nfs_client_kset);
      64                 :          0 :                 nfs_client_kset = NULL;
      65                 :          0 :                 return -ENOMEM;
      66                 :            :         }
      67                 :            :         return 0;
      68                 :            : }
      69                 :            : 
      70                 :          0 : void nfs_sysfs_exit(void)
      71                 :            : {
      72                 :          0 :         kobject_put(nfs_client_kobj);
      73                 :          0 :         kset_unregister(nfs_client_kset);
      74                 :          0 : }
      75                 :            : 
      76                 :          0 : static ssize_t nfs_netns_identifier_show(struct kobject *kobj,
      77                 :            :                 struct kobj_attribute *attr, char *buf)
      78                 :            : {
      79                 :            :         struct nfs_netns_client *c = container_of(kobj,
      80                 :            :                         struct nfs_netns_client,
      81                 :            :                         kobject);
      82                 :          0 :         return scnprintf(buf, PAGE_SIZE, "%s\n", c->identifier);
      83                 :            : }
      84                 :            : 
      85                 :            : /* Strip trailing '\n' */
      86                 :            : static size_t nfs_string_strip(const char *c, size_t len)
      87                 :            : {
      88   [ #  #  #  # ]:          0 :         while (len > 0 && c[len-1] == '\n')
      89                 :            :                 --len;
      90                 :          0 :         return len;
      91                 :            : }
      92                 :            : 
      93                 :          0 : static ssize_t nfs_netns_identifier_store(struct kobject *kobj,
      94                 :            :                 struct kobj_attribute *attr,
      95                 :            :                 const char *buf, size_t count)
      96                 :            : {
      97                 :            :         struct nfs_netns_client *c = container_of(kobj,
      98                 :            :                         struct nfs_netns_client,
      99                 :            :                         kobject);
     100                 :            :         const char *old;
     101                 :            :         char *p;
     102                 :            :         size_t len;
     103                 :            : 
     104                 :          0 :         len = nfs_string_strip(buf, min_t(size_t, count, CONTAINER_ID_MAXLEN));
     105         [ #  # ]:          0 :         if (!len)
     106                 :            :                 return 0;
     107                 :          0 :         p = kmemdup_nul(buf, len, GFP_KERNEL);
     108         [ #  # ]:          0 :         if (!p)
     109                 :            :                 return -ENOMEM;
     110                 :          0 :         old = xchg(&c->identifier, p);
     111         [ #  # ]:          0 :         if (old) {
     112                 :          0 :                 synchronize_rcu();
     113                 :          0 :                 kfree(old);
     114                 :            :         }
     115                 :          0 :         return count;
     116                 :            : }
     117                 :            : 
     118                 :          0 : static void nfs_netns_client_release(struct kobject *kobj)
     119                 :            : {
     120                 :            :         struct nfs_netns_client *c = container_of(kobj,
     121                 :            :                         struct nfs_netns_client,
     122                 :            :                         kobject);
     123                 :            : 
     124         [ #  # ]:          0 :         if (c->identifier)
     125                 :          0 :                 kfree(c->identifier);
     126                 :          0 :         kfree(c);
     127                 :          0 : }
     128                 :            : 
     129                 :        414 : static const void *nfs_netns_client_namespace(struct kobject *kobj)
     130                 :            : {
     131                 :        414 :         return container_of(kobj, struct nfs_netns_client, kobject)->net;
     132                 :            : }
     133                 :            : 
     134                 :            : static struct kobj_attribute nfs_netns_client_id = __ATTR(identifier,
     135                 :            :                 0644, nfs_netns_identifier_show, nfs_netns_identifier_store);
     136                 :            : 
     137                 :            : static struct attribute *nfs_netns_client_attrs[] = {
     138                 :            :         &nfs_netns_client_id.attr,
     139                 :            :         NULL,
     140                 :            : };
     141                 :            : 
     142                 :            : static struct kobj_type nfs_netns_client_type = {
     143                 :            :         .release = nfs_netns_client_release,
     144                 :            :         .default_attrs = nfs_netns_client_attrs,
     145                 :            :         .sysfs_ops = &kobj_sysfs_ops,
     146                 :            :         .namespace = nfs_netns_client_namespace,
     147                 :            : };
     148                 :            : 
     149                 :        207 : static struct nfs_netns_client *nfs_netns_client_alloc(struct kobject *parent,
     150                 :            :                 struct net *net)
     151                 :            : {
     152                 :            :         struct nfs_netns_client *p;
     153                 :            : 
     154                 :        207 :         p = kzalloc(sizeof(*p), GFP_KERNEL);
     155         [ +  - ]:        207 :         if (p) {
     156                 :        207 :                 p->net = net;
     157                 :        207 :                 p->kobject.kset = nfs_client_kset;
     158         [ -  + ]:        207 :                 if (kobject_init_and_add(&p->kobject, &nfs_netns_client_type,
     159                 :            :                                         parent, "nfs_client") == 0)
     160                 :            :                         return p;
     161                 :          0 :                 kobject_put(&p->kobject);
     162                 :            :         }
     163                 :            :         return NULL;
     164                 :            : }
     165                 :            : 
     166                 :        207 : void nfs_netns_sysfs_setup(struct nfs_net *netns, struct net *net)
     167                 :            : {
     168                 :            :         struct nfs_netns_client *clp;
     169                 :            : 
     170                 :        207 :         clp = nfs_netns_client_alloc(nfs_client_kobj, net);
     171         [ +  - ]:        207 :         if (clp) {
     172                 :        207 :                 netns->nfs_client = clp;
     173                 :        207 :                 kobject_uevent(&clp->kobject, KOBJ_ADD);
     174                 :            :         }
     175                 :        207 : }
     176                 :            : 
     177                 :          0 : void nfs_netns_sysfs_destroy(struct nfs_net *netns)
     178                 :            : {
     179                 :          0 :         struct nfs_netns_client *clp = netns->nfs_client;
     180                 :            : 
     181         [ #  # ]:          0 :         if (clp) {
     182                 :          0 :                 kobject_uevent(&clp->kobject, KOBJ_REMOVE);
     183                 :          0 :                 kobject_del(&clp->kobject);
     184                 :          0 :                 kobject_put(&clp->kobject);
     185                 :          0 :                 netns->nfs_client = NULL;
     186                 :            :         }
     187                 :          0 : }

Generated by: LCOV version 1.14