89495dd599b4677f0352c62492d7b2aaf11b0c70
[linux-2.6.git] / fs / rcfs / socket_fs.c
1 /* ckrm_socketaq.c 
2  *
3  * Copyright (C) Vivek Kashyap,      IBM Corp. 2004
4  * 
5  * Latest version, more details at http://ckrm.sf.net
6  * 
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  */
13
14 /* Changes
15  * Initial version
16  */
17
18 /*******************************************************************************
19  *  Socket class type
20  *   
21  * Defines the root structure for socket based classes. Currently only inbound
22  * connection control is supported based on prioritized accept queues. 
23  ******************************************************************************/
24
25 #include <linux/rcfs.h>
26 #include <net/tcp.h>
27
28 extern int rcfs_create(struct inode *, struct dentry *, int,
29                        struct nameidata *);
30 extern int rcfs_unlink(struct inode *, struct dentry *);
31 extern int rcfs_symlink(struct inode *, struct dentry *, const char *);
32 extern int rcfs_mknod(struct inode *, struct dentry *, int mode, dev_t);
33 extern int rcfs_mkdir(struct inode *, struct dentry *, int);
34 extern int rcfs_rmdir(struct inode *, struct dentry *);
35 extern int rcfs_rename(struct inode *, struct dentry *, struct inode *,
36                        struct dentry *);
37
38 extern int rcfs_create_coredir(struct inode *, struct dentry *);
39 int sock_mkdir(struct inode *, struct dentry *, int mode);
40 int sock_rmdir(struct inode *, struct dentry *);
41
42 int sock_create_noperm(struct inode *, struct dentry *, int,
43                        struct nameidata *);
44 int sock_unlink_noperm(struct inode *, struct dentry *);
45 int sock_mkdir_noperm(struct inode *, struct dentry *, int);
46 int sock_rmdir_noperm(struct inode *, struct dentry *);
47 int sock_mknod_noperm(struct inode *, struct dentry *, int, dev_t);
48
49 void sock_set_directory(void);
50
51 extern struct file_operations config_fileops,
52     members_fileops, shares_fileops, stats_fileops;
53
54 struct inode_operations my_iops = {
55         .create = rcfs_create,
56         .lookup = simple_lookup,
57         .link = simple_link,
58         .unlink = rcfs_unlink,
59         .symlink = rcfs_symlink,
60         .mkdir = sock_mkdir,
61         .rmdir = sock_rmdir,
62         .mknod = rcfs_mknod,
63         .rename = rcfs_rename,
64 };
65
66 struct inode_operations class_iops = {
67         .create = sock_create_noperm,
68         .lookup = simple_lookup,
69         .link = simple_link,
70         .unlink = sock_unlink_noperm,
71         .symlink = rcfs_symlink,
72         .mkdir = sock_mkdir_noperm,
73         .rmdir = sock_rmdir_noperm,
74         .mknod = sock_mknod_noperm,
75         .rename = rcfs_rename,
76 };
77
78 struct inode_operations sub_iops = {
79         .create = sock_create_noperm,
80         .lookup = simple_lookup,
81         .link = simple_link,
82         .unlink = sock_unlink_noperm,
83         .symlink = rcfs_symlink,
84         .mkdir = sock_mkdir_noperm,
85         .rmdir = sock_rmdir_noperm,
86         .mknod = sock_mknod_noperm,
87         .rename = rcfs_rename,
88 };
89
90 struct rcfs_magf def_magf = {
91         .mode = RCFS_DEFAULT_DIR_MODE,
92         .i_op = &sub_iops,
93         .i_fop = NULL,
94 };
95
96 struct rcfs_magf sock_rootdesc[] = {
97         {
98          //      .name = should not be set, copy from classtype name,
99          .mode = RCFS_DEFAULT_DIR_MODE,
100          .i_op = &my_iops,
101          //.i_fop   = &simple_dir_operations,
102          .i_fop = NULL,
103          },
104         {
105          .name = "members",
106          .mode = RCFS_DEFAULT_FILE_MODE,
107          .i_op = &my_iops,
108          .i_fop = &members_fileops,
109          },
110         {
111          .name = "reclassify",
112          .mode = RCFS_DEFAULT_FILE_MODE,
113          .i_op = &my_iops,
114          .i_fop = &reclassify_fileops,
115          },
116 };
117
118 struct rcfs_magf sock_magf[] = {
119         {
120          .name = "config",
121          .mode = RCFS_DEFAULT_FILE_MODE,
122          .i_op = &my_iops,
123          .i_fop = &config_fileops,
124          },
125         {
126          .name = "members",
127          .mode = RCFS_DEFAULT_FILE_MODE,
128          .i_op = &my_iops,
129          .i_fop = &members_fileops,
130          },
131         {
132          .name = "shares",
133          .mode = RCFS_DEFAULT_FILE_MODE,
134          .i_op = &my_iops,
135          .i_fop = &shares_fileops,
136          },
137         {
138          .name = "stats",
139          .mode = RCFS_DEFAULT_FILE_MODE,
140          .i_op = &my_iops,
141          .i_fop = &stats_fileops,
142          },
143 };
144
145 struct rcfs_magf sub_magf[] = {
146         {
147          .name = "config",
148          .mode = RCFS_DEFAULT_FILE_MODE,
149          .i_op = &my_iops,
150          .i_fop = &config_fileops,
151          },
152         {
153          .name = "shares",
154          .mode = RCFS_DEFAULT_FILE_MODE,
155          .i_op = &my_iops,
156          .i_fop = &shares_fileops,
157          },
158         {
159          .name = "stats",
160          .mode = RCFS_DEFAULT_FILE_MODE,
161          .i_op = &my_iops,
162          .i_fop = &stats_fileops,
163          },
164 };
165
166 struct rcfs_mfdesc sock_mfdesc = {
167         .rootmf = sock_rootdesc,
168         .rootmflen = (sizeof(sock_rootdesc) / sizeof(struct rcfs_magf)),
169 };
170
171 #define SOCK_MAX_MAGF (sizeof(sock_magf)/sizeof(struct rcfs_magf))
172 #define LAQ_MAX_SUBMAGF (sizeof(sub_magf)/sizeof(struct rcfs_magf))
173
174 int sock_rmdir(struct inode *p, struct dentry *me)
175 {
176         struct dentry *mftmp, *mfdentry;
177         int ret = 0;
178
179         // delete all magic sub directories
180         list_for_each_entry_safe(mfdentry, mftmp, &me->d_subdirs, d_child) {
181                 if (S_ISDIR(mfdentry->d_inode->i_mode)) {
182                         ret = rcfs_rmdir(me->d_inode, mfdentry);
183                         if (ret)
184                                 return ret;
185                 }
186         }
187         // delete ourselves
188         ret = rcfs_rmdir(p, me);
189
190         return ret;
191 }
192
193 #ifdef NUM_ACCEPT_QUEUES
194 #define LAQ_NUM_ACCEPT_QUEUES NUM_ACCEPT_QUEUES
195 #else
196 #define LAQ_NUM_ACCEPT_QUEUES 0
197 #endif
198
199 int sock_mkdir(struct inode *dir, struct dentry *dentry, int mode)
200 {
201         int retval = 0;
202         int i, j;
203         struct dentry *pentry, *mfdentry;
204
205         if (_rcfs_mknod(dir, dentry, mode | S_IFDIR, 0)) {
206                 printk(KERN_ERR "rcfs_mkdir: error reaching parent\n");
207                 return retval;
208         }
209         // Needed if only _rcfs_mknod is used instead of i_op->mkdir
210         dir->i_nlink++;
211
212         retval = rcfs_create_coredir(dir, dentry);
213         if (retval)
214                 goto mkdir_err;
215
216         /* create the default set of magic files */
217         for (i = 0; i < SOCK_MAX_MAGF; i++) {
218                 mfdentry = rcfs_create_internal(dentry, &sock_magf[i], 0);
219                 mfdentry->d_fsdata = &RCFS_IS_MAGIC;
220                 RCFS_I(mfdentry->d_inode)->core = RCFS_I(dentry->d_inode)->core;
221                 if (sock_magf[i].i_fop)
222                         mfdentry->d_inode->i_fop = sock_magf[i].i_fop;
223                 if (sock_magf[i].i_op)
224                         mfdentry->d_inode->i_op = sock_magf[i].i_op;
225         }
226
227         for (i = 1; i < LAQ_NUM_ACCEPT_QUEUES; i++) {
228                 j = sprintf(def_magf.name, "%d", i);
229                 def_magf.name[j] = '\0';
230
231                 pentry = rcfs_create_internal(dentry, &def_magf, 0);
232                 retval = rcfs_create_coredir(dentry->d_inode, pentry);
233                 if (retval)
234                         goto mkdir_err;
235                 pentry->d_fsdata = &RCFS_IS_MAGIC;
236                 for (j = 0; j < LAQ_MAX_SUBMAGF; j++) {
237                         mfdentry =
238                             rcfs_create_internal(pentry, &sub_magf[j], 0);
239                         mfdentry->d_fsdata = &RCFS_IS_MAGIC;
240                         RCFS_I(mfdentry->d_inode)->core =
241                             RCFS_I(pentry->d_inode)->core;
242                         if (sub_magf[j].i_fop)
243                                 mfdentry->d_inode->i_fop = sub_magf[j].i_fop;
244                         if (sub_magf[j].i_op)
245                                 mfdentry->d_inode->i_op = sub_magf[j].i_op;
246                 }
247                 pentry->d_inode->i_op = &sub_iops;
248         }
249         dentry->d_inode->i_op = &class_iops;
250         return 0;
251
252       mkdir_err:
253         // Needed
254         dir->i_nlink--;
255         return retval;
256 }
257
258 #ifndef NUM_ACCEPT_QUEUES
259 #define NUM_ACCEPT_QUEUES 0
260 #endif
261
262 char *sock_get_name(struct ckrm_core_class *c)
263 {
264         char *p = (char *)c->name;
265
266         while (*p)
267                 p++;
268         while (*p != '/' && p != c->name)
269                 p--;
270
271         return ++p;
272 }
273
274 int
275 sock_create_noperm(struct inode *dir, struct dentry *dentry, int mode,
276                    struct nameidata *nd)
277 {
278         return -EPERM;
279 }
280
281 int sock_unlink_noperm(struct inode *dir, struct dentry *dentry)
282 {
283         return -EPERM;
284 }
285
286 int sock_mkdir_noperm(struct inode *dir, struct dentry *dentry, int mode)
287 {
288         return -EPERM;
289 }
290
291 int sock_rmdir_noperm(struct inode *dir, struct dentry *dentry)
292 {
293         return -EPERM;
294 }
295
296 int
297 sock_mknod_noperm(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
298 {
299         return -EPERM;
300 }
301
302 #if 0
303 void sock_set_directory()
304 {
305         struct dentry *pentry, *dentry;
306
307         pentry = rcfs_set_magf_byname("listen_aq", (void *)&my_dir_magf[0]);
308         if (pentry) {
309                 dentry = rcfs_create_internal(pentry, &my_dir_magf[1], 0);
310                 if (my_dir_magf[1].i_fop)
311                         dentry->d_inode->i_fop = my_dir_magf[1].i_fop;
312                 RCFS_I(dentry->d_inode)->core = RCFS_I(pentry->d_inode)->core;
313                 dentry = rcfs_create_internal(pentry, &my_dir_magf[2], 0);
314                 if (my_dir_magf[2].i_fop)
315                         dentry->d_inode->i_fop = my_dir_magf[2].i_fop;
316                 RCFS_I(dentry->d_inode)->core = RCFS_I(pentry->d_inode)->core;
317         } else {
318                 printk(KERN_ERR "Could not create /rcfs/listen_aq\n"
319                        "Perhaps /rcfs needs to be mounted\n");
320         }
321 }
322 #endif