9p: sysfs support for in-kenel servers
Subsystems (3)
Latchesar Ionkov
maintainerPATCH 7/10 9p: sysfs support for in-kenel servers
Sysfs support for 9P servers.
Every server type is represented as a directory in /sys/fs/9p/srv. Initially there is a single file in the directory -- 'clone'. Reading from the clone file creates a new instance of the file server and returns its id. Each instance is represented as a directory in /sys/fs/9p/srv/<file server>/. The file server instance can be controlled by the 'ctl' file in its directory. Currently the ctl file accepts the following commands:
listen-add <trans> <options>
listen-del <trans> <options>
destroy
Example:
echo 'listen-add tcp port=564' > /sys/fs/9p/srv/ramfs/0/ctl
--- include/net/9p/srv.h | 24 +++- net/9p/srv.c | 386 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 405 insertions(+), 5 deletions(-)
include/net/9p/srv.h +22 -2
@@ -69,11 +69,17 @@ struct p9srv {void (*wstat)(struct p9srv_req *);/* implementation specific */ +int id;atomic_t refcount;spinlock_t lock;/* covers all srv fields */ +unsigned long status;struct list_head conn_list;/* all connections for srv */ struct list_head req_list;/* requests not yet worked on */ struct list_head workreq_list;/* requests being worked on */ +struct kobject kobj; +struct list_head srv_list;/* all servers of a type */ +struct list_head lis_list;/* all listeners for the srv */ +struct work_struct shutwork; };struct p9srv_conn {
@@ -90,6 +96,16 @@ struct p9srv_conn {wait_queue_head_t reset_wqueue; }; +struct p9srv_type { +spinlock_t lock; +char *name; +int nextid; +struct kobject kobj; +struct list_head srv_list;/* all servers of a type */ +struct list_head stype_list;/* all server types */ +struct p9srv * (*create)(void ); +}; +enum {/* connection status values */ Reset =1 ,/* resetting */
@@ -149,11 +165,11 @@ extern char *Enotfound;extern char *Eopen;extern char *Eexist;extern char *Enotempty; - +extern struct kset p9srv_subsys;struct p9srv *p9srv_srv_create (void );void p9srv_srv_stop (struct p9srv *srv); -void p9srv_srv_destroy (struct p9srv *srv); +void p9srv_srv_shutdown (struct p9srv *srv);void p9srv_srv_start (struct p9srv *srv);void p9srv_respond (struct p9srv_req *req,struct p9_fcall *rc);void p9srv_respond_error (struct p9srv_req *req,char *ename,int ecode);
@@ -165,5 +181,9 @@ void p9srv_conn_destroy(struct p9srv_conn *conn);struct p9srv_fid *p9srv_fid_find (struct p9srv_fidpool *fp,u32 fid);void p9srv_fid_incref (struct p9srv_fid *fid);void p9srv_fid_decref (struct p9srv_fid *fid); +struct p9srv_type *p9srv_type_register (char *name,struct p9srv *(*create)(void )); +void p9srv_type_unregister (char *name); +int p9srv_listener_add (struct p9srv *,char *,char *); +int p9srv_listener_del (struct p9srv *,char *,char *);#endif
net/9p/srv.c +383 -3
467 lines not on this page · raw message
Quoted 8 lines · Latchesar Ionkov
Sysfs support for 9P servers.
Every server type is represented as a directory in/sys/fs/9p/srv. Initially there is a single file in the directory -- 'clone'. Reading from the clone file creates a new instance of the file server and returns its id. Each instance is represented as a directory in /sys/fs/9p/srv/<file server>/. The file server instance can be controlled by the 'ctl' file in its directory. Currently the ctl file accepts the following commands:
Quoted 6 lines
listen-add <trans> <options>
listen-del <trans> <options>
destroyExample:
echo 'listen-add tcp port=564' > /sys/fs/9p/srv/ramfs/0/ctl
Traditionally sysfs has not been used for "configuration" stuff like this, as that is what configfs is for.
Also, a sysfs file that causes an action to happen just by reading the file is not a safe thing to have. Lots of scripts have been known to just walk the whole sysfs tree and open and read everything they can for no real good reason. So you should seriously reconsider this kind of interaction.
net/9p/srv.c static void (*p9srv_fcall_post[])(struct p9srv_req *) = { · line 185
Quoted 4 lines
net/9p/srv.c +4
-0+static struct attribute p9srv_clone_attr = { + .name ="clone" , + .mode =0444 , +};
What's wrong with the __ATTR macro? Using a "raw" struct attribute seems a bit odd here.
Quoted 4 lines
net/9p/srv.c +4
-0+static struct sysfs_ops p9srv_srv_ops = { + .show = p9srv_srv_show, + .store = p9srv_srv_store, +};
net/9p/srv.c static void (*p9srv_fcall_post[])(struct p9srv_req *) = { · line 200
Quoted 10 lines
net/9p/srv.c +10
-0+ +static struct kobj_type p9srv_srv_ktype = { + .release = p9srv_srv_release, + .sysfs_ops = &p9srv_srv_ops, +}; + +static struct attribute p9srv_ctl_attr = { + .name ="ctl" , + .mode =0644 , +};
Same here.
Quoted 36 lines
net/9p/srv.c +36
-0+struct p9srv_type *p9srv_type_register (char *name, +struct p9srv *(*create)(void )) +{ +int err; +struct p9srv_type *ret, *stype; + + ret =kzalloc (sizeof (*ret),GFP_KERNEL ); +if (!ret) +return ERR_PTR (-ENOMEM ); + +spin_lock_init (&ret->lock); + ret->name = name; + ret->nextid =0 ; +INIT_LIST_HEAD (&ret->srv_list); +INIT_LIST_HEAD (&ret->stype_list); + ret->create = create; + +spin_lock (&p9srv_type_lock); +list_for_each_entry (stype, &p9srv_type_list, stype_list) { +if (strcmp (name, stype->name) ==0 ) { +spin_unlock (&p9srv_type_lock); +kfree (ret); +return ERR_PTR (-EEXIST ); + } + } + +list_add_tail (&ret->stype_list, &p9srv_type_list); +spin_unlock (&p9srv_type_lock); + +kobject_init (&ret->kobj); +kobject_set_name (&ret->kobj,"%s" , name); + ret->kobj.parent = &p9srv_subsys.kobj; + ret->kobj.ktype = &p9srv_type_ktype; + err =kobject_add (&ret->kobj); +if (err) + goto error;
net/9p/srv.c void p9srv_req_unref(struct p9srv_req *req) · line 501
Quoted 10 lines
net/9p/srv.c +10
-0+ + err =sysfs_create_file (&ret->kobj, &p9srv_clone_attr); +if (err) + goto error; + +return ret; + +error: +kobject_unregister (&ret->kobj); +/* kfree ??? */
Yes, otherwise you just leaked memory :)
Quoted 16 lines
net/9p/srv.c +16
-0+return ERR_PTR (err); +} +EXPORT_SYMBOL (p9srv_type_register); + +void p9srv_type_unregister (char *name) +{ +struct p9srv_type *stype; + +spin_lock (&p9srv_type_lock); +list_for_each_entry (stype, &p9srv_type_list, stype_list) { +if (strcmp (name, stype->name) ==0 ) { +list_del (&stype->stype_list); +spin_unlock (&p9srv_type_lock); +sysfs_remove_file (&stype->kobj, &p9srv_clone_attr); +kobject_unregister (&stype->kobj); +/* kfree ??? */
net/9p/srv.c void p9srv_req_unref(struct p9srv_req *req) · line 527
Quoted 10 lines
net/9p/srv.c +10
-0+return ; + } + } +spin_unlock (&p9srv_type_lock); +} +EXPORT_SYMBOL (p9srv_type_unregister); + +static void p9srv_type_release (struct kobject *kobj) +{ +}
{sigh}
Yes, the kernel complains if you do not have a release function for your kobject, but did you think that just providing an empty function is somehow the proper thing to do here?
It wants you to do something in the release function, like free the memory of the object. Not just provide empty functions to try to shut it up...
Please fix this.
net/9p/srv.c void p9srv_req_unref(struct p9srv_req *req) · line 537
Quoted 9 lines
net/9p/srv.c +9
-0+static ssize_t p9srv_type_show (struct kobject *kobj,struct attribute *attr, +char *buf) +{ +int err; +struct p9srv_type *stype; +struct p9srv *srv; + + stype = container_of(kobj, struct p9srv_type, kobj); + srv = (*stype->create)();
Again, creating stuff in a show function is not a good idea.
Quoted 18 lines
net/9p/srv.c +18
-0+if (IS_ERR (srv)) +return PTR_ERR (srv); + +spin_lock (&stype->lock); + srv->id = stype->nextid++; +list_add_tail (&srv->srv_list, &stype->srv_list); +spin_unlock (&stype->lock); + +kobject_init (&srv->kobj); +kobject_set_name (&srv->kobj,"%d" , srv->id); + srv->kobj.parent = &stype->kobj; + srv->kobj.ktype = &p9srv_srv_ktype; + err =kobject_add (&srv->kobj); +if (err) + goto error; + + err =sysfs_create_file (&srv->kobj, &p9srv_ctl_attr); +if (err)
net/9p/srv.c void p9srv_req_unref(struct p9srv_req *req) · line 565
Quoted 10 lines
net/9p/srv.c +10
-0+ goto error; + +return snprintf (buf,PAGE_SIZE ,"%d" , srv->id); + +error: +spin_lock (&stype->lock); +list_del (&srv->srv_list); +spin_unlock (&stype->lock); +kobject_unregister (&srv->kobj); +/* kfree ??? */
Well, no, not if your release function frees the memory, otherwise it is still present the way the code is written today...
But watch out, you are going here if kobject_add() fails, in which case, kobject_unregister makes no sense as the kobject was never properly added.
net/9p/srv.c void p9srv_req_unref(struct p9srv_req *req) · line 577
Quoted 9 lines
net/9p/srv.c +9
-0+static ssize_t p9srv_type_store (struct kobject *kobj,struct attribute *attr, +const char *buf,size_t buflen) +{ +return -EPERM ; +} + +static void p9srv_srv_release (struct kobject *kobj) +{ +}
Again with the empty release :(
net/9p/srv.c void p9srv_conn_respond(struct p9srv_req *req) · line 1639
Quoted 11 lines
static int
__initp9srv_init(void)
{net/9p/srv.c +3
-0+int err; + p9srv_wq =create_workqueue ("9psrv" );if (!p9srv_wq) {printk (KERN_WARNING "9psrv: creating workqueue failed\n" );return -ENOMEM ; } +kobj_set_kset_s (&p9srv_subsys, p9_subsys);
This macro is now gone away in the next -mm release :)
thanks,
greg k-h
Quoted 5 lines · Greg KH
Also, a sysfs file that causes an action to happen just by reading the file is not a safe thing to have. Lots of scripts have been known to just walk the whole sysfs tree and open and read everything they can for no real good reason. So you should seriously reconsider this kind of interaction.
Christoph, doesn't SLUB do exactly that?
Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Greg KH
Also, a sysfs file that causes an action to happen just by reading the file is not a safe thing to have. Lots of scripts have been known to just walk the whole sysfs tree and open and read everything they can for no real good reason. So you should seriously reconsider this kind of interaction.
Christoph, doesn't SLUB do exactly that?
SLUB gathers statistics and summaries and displays them on read.