ckrm-E14
[linux-2.6.git] / fs / rcfs / rootdir.c
1 /* 
2  * fs/rcfs/rootdir.c 
3  *
4  * Copyright (C)   Vivek Kashyap,   IBM Corp. 2004
5  *           
6  * 
7  * Functions for creating root directories and magic files 
8  * for classtypes and classification engines under rcfs
9  *
10  * Latest version, more details at http://ckrm.sf.net
11  * 
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  */
18
19 /* Changes
20  *
21  * 08 April 2004
22  *        Created.
23  */
24
25
26 #include <linux/module.h>
27 #include <linux/fs.h>
28 #include <linux/namei.h>
29 #include <asm/namei.h>
30 #include <linux/namespace.h>
31 #include <linux/dcache.h>
32 #include <linux/seq_file.h>
33 #include <linux/pagemap.h>
34 #include <linux/highmem.h>
35 #include <linux/init.h>
36 #include <linux/string.h>
37 #include <linux/smp_lock.h>
38 #include <linux/backing-dev.h>
39 #include <linux/parser.h>
40
41 #include <asm/uaccess.h>
42
43 #include <linux/rcfs.h>
44
45
46
47 rbce_eng_callback_t rcfs_eng_callbacks = {
48         NULL, NULL
49 };
50
51 int
52 rcfs_register_engine(rbce_eng_callback_t *rcbs)
53 {
54         if (!rcbs->mkdir || rcfs_eng_callbacks.mkdir) {
55                 return -EINVAL;
56         }
57         rcfs_eng_callbacks = *rcbs;
58         return 0;
59 }
60 EXPORT_SYMBOL(rcfs_register_engine);
61
62
63
64 int
65 rcfs_unregister_engine(rbce_eng_callback_t *rcbs)
66 {
67         if (!rcbs->mkdir || !rcfs_eng_callbacks.mkdir ||
68                         (rcbs->mkdir != rcfs_eng_callbacks.mkdir)) {
69                 return -EINVAL;
70         }
71         rcfs_eng_callbacks.mkdir = NULL;
72         rcfs_eng_callbacks.rmdir = NULL;
73         return 0;
74 }
75 EXPORT_SYMBOL(rcfs_unregister_engine);
76
77
78
79
80 /* rcfs_mkroot
81  * Create and return a "root" dentry under /rcfs. Also create associated magic files 
82  *
83  * @mfdesc: array of rcfs_magf describing root dir and its magic files
84  * @count: number of entries in mfdesc
85  * @core:  core class to be associated with root
86  * @rootde: output parameter to return the newly created root dentry
87  */
88
89 int 
90 rcfs_mkroot(struct rcfs_magf *mfdesc, int mfcount, struct dentry **rootde)
91 {
92         int sz;
93         struct rcfs_magf *rootdesc = &mfdesc[0];
94         struct dentry *dentry ;
95         struct rcfs_inode_info *rootri;
96
97         if ((mfcount < 0) || (!mfdesc))
98                 return -EINVAL;
99         
100         rootdesc = &mfdesc[0];
101         printk("allocating classtype root <%s>\n",rootdesc->name);
102         dentry = rcfs_create_internal(rcfs_rootde, rootdesc,0);
103         
104         if (!dentry) {
105                 printk(KERN_ERR "Could not create %s\n",rootdesc->name);
106                 return -ENOMEM;
107         } 
108         
109         rootri = RCFS_I(dentry->d_inode);
110         sz = strlen(rootdesc->name) + strlen(RCFS_ROOT) + 2;
111         rootri->name = kmalloc(sz, GFP_KERNEL);
112         if (!rootri->name) {
113                 printk(KERN_ERR "Error allocating name for %s\n",
114                        rootdesc->name);
115                 rcfs_delete_internal(dentry);
116                 return -ENOMEM;
117         }
118         snprintf(rootri->name,sz,"%s/%s",RCFS_ROOT,rootdesc->name);
119         
120         if (rootdesc->i_fop)
121                 dentry->d_inode->i_fop = rootdesc->i_fop;
122         if (rootdesc->i_op)
123                 dentry->d_inode->i_op = rootdesc->i_op;
124
125         // set output parameters
126         *rootde = dentry;
127
128         return 0;
129 }
130 EXPORT_SYMBOL(rcfs_mkroot);
131
132
133 int 
134 rcfs_rmroot(struct dentry *rootde)
135 {
136         if (!rootde)
137                 return -EINVAL;
138
139         rcfs_clear_magic(rootde);
140         kfree(RCFS_I(rootde->d_inode)->name);
141         rcfs_delete_internal(rootde);
142         return 0;
143 }
144 EXPORT_SYMBOL(rcfs_rmroot);
145
146
147 int 
148 rcfs_register_classtype(ckrm_classtype_t *clstype)
149 {
150         int rc ;
151         struct rcfs_inode_info *rootri;
152         struct rcfs_magf *mfdesc;
153
154         // Initialize mfdesc, mfcount 
155         clstype->mfdesc = (void *) genmfdesc[clstype->mfidx]->rootmf;
156         clstype->mfcount = genmfdesc[clstype->mfidx]->rootmflen;
157
158         mfdesc = (struct rcfs_magf *)clstype->mfdesc;
159         
160         /* rcfs root entry has the same name as the classtype */
161         strncpy(mfdesc[0].name,clstype->name,RCFS_MAGF_NAMELEN) ;
162
163         rc = rcfs_mkroot(mfdesc,clstype->mfcount,
164                                 (struct dentry **)&(clstype->rootde));
165         if (rc)
166                 return rc;
167
168         rootri = RCFS_I(((struct dentry *)(clstype->rootde))->d_inode);
169         rootri->core = clstype->default_class;
170         clstype->default_class->name = rootri->name;
171         ckrm_core_grab(clstype->default_class);
172         
173         // Create magic files under root 
174         if ((rc = rcfs_create_magic(clstype->rootde, &mfdesc[1], 
175                                     clstype->mfcount-1))) {
176                 kfree(rootri->name);
177                 rcfs_delete_internal(clstype->rootde);
178                 return rc;
179         }
180
181         return rc;
182 }
183 EXPORT_SYMBOL(rcfs_register_classtype);
184
185
186 int 
187 rcfs_deregister_classtype(ckrm_classtype_t *clstype)
188 {
189         int rc;
190
191         rc = rcfs_rmroot((struct dentry *)clstype->rootde);
192         if (!rc) {
193                 clstype->default_class->name = NULL ;
194                 ckrm_core_drop(clstype->default_class);
195         }
196         return rc;
197 }
198 EXPORT_SYMBOL(rcfs_deregister_classtype);
199
200
201
202 // Common root and magic file entries.
203 // root name, root permissions, magic file names and magic file permissions are needed by
204 // all entities (classtypes and classification engines) existing under the rcfs mount point
205
206 // The common sets of these attributes are listed here as a table. Individual classtypes and
207 // classification engines can simple specify the index into the table to initialize their
208 // magf entries. 
209 //
210
211 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
212 extern struct rcfs_mfdesc tc_mfdesc;
213 #endif
214
215 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
216 extern struct rcfs_mfdesc sock_mfdesc;
217 #endif
218
219 // extern struct rcfs_magf rbce_mfdesc;
220
221
222 struct rcfs_mfdesc *genmfdesc[]={
223 #ifdef CONFIG_CKRM_TYPE_TASKCLASS
224         &tc_mfdesc,
225 #else
226         NULL,
227 #endif
228 #ifdef CONFIG_CKRM_TYPE_SOCKETCLASS
229         &sock_mfdesc,
230 #else
231         NULL,
232 #endif
233 // Create similar entry for RBCE ? 
234 //#ifdef CONFIG_CKRM_CE
235 //      &rbce_mfdesc,
236 //#else
237 //      NULL,
238 //#endif
239
240 };
241
242
243
244