Krishna iResearch Intelligent Cloud Platform - VIRtual Generic Os - VIRGO - Linux kernel extensions for cloud
 All Classes
virgo_cloud_fs_kernelspace.c
1 /***************************************************************************************
2 VIRGO - a linux module extension with CPU and Memory pooling with cloud capabilities
3 
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 --------------------------------------------------------------------------------------------------
19 Copyright (C):
20 Srinivasan Kannan (alias) Ka.Shrinivaasan (alias) Shrinivas Kannan
21 Independent Open Source Developer, Researcher and Consultant
22 Ph: 9789346927, 9003082186, 9791165980
23 Open Source Products Profile(Krishna iResearch): http://sourceforge.net/users/ka_shrinivaasan
24 Personal website(research): https://sites.google.com/site/kuja27/
25 emails: ka.shrinivaasan@gmail.com, shrinivas.kannan@gmail.com, kashrinivaasan@live.com
26 --------------------------------------------------------------------------------------------------
27 
28 *****************************************************************************************/
29 
30 
31 #include <linux/string.h>
32 #include <linux/module.h>
33 #include <linux/virgo_fs.h>
34 #include <linux/string.h>
35 #include <kstrtox.h>
36 #include <linux/ctype.h>
37 
38 unsigned int virgo_parse_integer(const char *s, unsigned int base, unsigned long long *p);
39 void printBytes(const char* str);
40 char* toKernelAddress(const char*);
41 int toInteger(char*);
42 void* virgo_cloud_open_kernelspace(struct virgo_fs_args*);
43 void* virgo_cloud_close_kernelspace(struct virgo_fs_args*);
44 void* virgo_cloud_read_kernelspace(struct virgo_fs_args*);
45 void* virgo_cloud_write_kernelspace(struct virgo_fs_args*);
46 struct virgo_fs_args* parse_virgofs_command_kernelspace(char* fsFunction);
47 
48 static int __init
49 virgo_cloud_fs_kernelspace_init(void)
50 {
51  printk(KERN_INFO "virgo_cloud_fs_kernelspace_init(): doing init() of virgo cloud kernel space test module\n");
52  return 0;
53 }
54 EXPORT_SYMBOL(virgo_cloud_fs_kernelspace_init);
55 
56 
57 static void __exit
58 virgo_cloud_fs_kernelspace_exit(void)
59 {
60  printk(KERN_INFO "virgo_cloud_fs_kernelspace_exit(): exiting virgo cloud test kernel space module \n");
61  do_exit(1);
62 }
63 EXPORT_SYMBOL(virgo_cloud_fs_kernelspace_exit);
64 
65 
66 
67 void* virgo_cloud_open_kernelspace(struct virgo_fs_args* args)
68 {
69  struct virgo_fs_args* vmargs=(struct virgo_fs_args*)args;
70  printk(KERN_INFO "virgo_cloud_fs_kernelspace.c:virgo_cloud_open_kernelspace(): file path to open=%s\n",vmargs->fs_args[0]);
71  fs=get_fs();
72  set_fs(get_ds());
73  struct file* f=NULL;
74  f=filp_open(vmargs->fs_args[0], O_RDWR|O_APPEND, 0);
75  /*f=filp_open("/var/log/virgo_fs/virgofstest.txt", O_RDWR|O_APPEND, 0);*/
76  if(IS_ERR(f))
77  printk(KERN_INFO "virgo_cloud_open_kernelspace(): filp_open return value is error code : %p", f);
78  set_fs(fs);
79  no_of_openfiles++;
80  open_VFS_files[no_of_openfiles]=f;
81  printk(KERN_INFO "virgo_cloud_fs_kernelspace.c:virgo_cloud_open_kernelspace(): f=%p, no_of_openfiles=%d, file struct=%p\n",f,no_of_openfiles, open_VFS_files[no_of_openfiles]);
82  return toVFSString(&no_of_openfiles); /* This is returned as VFS file descriptor to VFS telnet and syscall clients */
83 }
84 EXPORT_SYMBOL(virgo_cloud_open_kernelspace);
85 
86 
87 
88 void* virgo_cloud_read_kernelspace(struct virgo_fs_args* args)
89 {
90  struct virgo_fs_args* vmargs=args;
91  char *buf=kmalloc(sizeof(char)*500,GFP_KERNEL);
92  printk(KERN_INFO "virgo_cloud_fs_kernelspace.c: virgo_cloud_read_kernelspace(): vmargs->fs_cmd=%s, vmargs->fs_args[0] = %s\n, vmargs->fs_args[1]=%s, vmargs->fs_args[2]=%s, vmargs->fs_args[3]=%s \n",vmargs->fs_cmd, vmargs->fs_args[0],vmargs->fs_args[1],vmargs->fs_args[2],vmargs->fs_args[3]);
93  unsigned long long ll;
94  virgo_parse_integer(vmargs->fs_args[0],10,&ll);
95  unsigned long long ll2;
96  virgo_parse_integer(vmargs->fs_args[2],10,&ll2);
97  unsigned long long ll3;
98  virgo_parse_integer(vmargs->fs_args[3],10,&ll3);
99  int vfsdesc=(int)ll;
100  loff_t pos = (loff_t)ll3;
101  size_t cnt = (size_t)ll2;
102  printk(KERN_INFO "virgo_cloud_read_kernelspace(): buf=%s\n", buf);
103  printk(KERN_INFO "virgo_cloud_read_kernelspace(): vfsdesc=%d\n", vfsdesc);
104  printk(KERN_INFO "virgo_cloud_read_kernelspace(): cnt=%d\n", cnt);
105  printk(KERN_INFO "virgo_cloud_read_kernelspace(): pos=%d\n", pos);
106  printk(KERN_INFO "virgo_cloud_read_kernelspace(): open_VFS_files[vfsdesc]=%p\n", open_VFS_files[vfsdesc]);
107  fs=get_fs();
108  set_fs(get_ds());
109  loff_t bytesread=vfs_read(open_VFS_files[vfsdesc], buf, cnt, &pos);
110  printk(KERN_INFO "virgo_cloud_read_kernelspace(): after vfs_read(): bytesread=%d, buf=%s\n", bytesread, buf);
111  set_fs(fs);
112  return buf;
113 }
114 EXPORT_SYMBOL(virgo_cloud_read_kernelspace);
115 
116 
117 
118 void* virgo_cloud_write_kernelspace(struct virgo_fs_args* args)
119 {
120  struct virgo_fs_args* vmargs=args;
121  printk(KERN_INFO "virgo_cloud_fs_kernelspace.c: virgo_cloud_write_kernelspace(): vmargs->fs_cmd=%s, vmargs->fs_args[0] = %s\n, vmargs->fs_args[1]=%s, vmargs->fs_args[2]=%d, vmargs->fs_args[3]=%d \n",vmargs->fs_cmd, vmargs->fs_args[0],vmargs->fs_args[1],vmargs->fs_args[2],vmargs->fs_args[3]);
122  unsigned long long ll;
123  virgo_parse_integer(vmargs->fs_args[0],10,&ll);
124  unsigned long long ll2;
125  virgo_parse_integer(vmargs->fs_args[2],10,&ll2);
126  unsigned long long ll3;
127  virgo_parse_integer(vmargs->fs_args[3],10,&ll3);
128  int vfsdesc=(int)ll;
129  loff_t pos = (loff_t)ll3;
130  size_t cnt = (size_t)ll2;
131  printk(KERN_INFO "virgo_cloud_read_kernelspace(): vmargs->fs_args[1]=%s\n", vmargs->fs_args[1]);
132  printk(KERN_INFO "virgo_cloud_read_kernelspace(): vfsdesc=%d\n", vfsdesc);
133  printk(KERN_INFO "virgo_cloud_read_kernelspace(): cnt=%d\n", cnt);
134  printk(KERN_INFO "virgo_cloud_read_kernelspace(): cnt=%d\n", cnt);
135  printk(KERN_INFO "virgo_cloud_read_kernelspace(): pos=%d\n", pos);
136  printk(KERN_INFO "virgo_cloud_read_kernelspace(): open_VFS_files[vfsdesc]=%p\n", open_VFS_files[vfsdesc]);
137  fs=get_fs();
138  set_fs(get_ds());
139  vfs_write(open_VFS_files[(int)ll], vmargs->fs_args[1], cnt, &pos);
140  set_fs(fs);
141  return NULL;
142 }
143 EXPORT_SYMBOL(virgo_cloud_write_kernelspace);
144 
145 
146 
147 void* virgo_cloud_close_kernelspace(struct virgo_fs_args* args)
148 {
149  struct virgo_fs_args* vmargs=(struct virgo_fs_args*)args;
150  printk(KERN_INFO "virgo_cloud_fs_kernelspace.c: virgo_cloud_close_kernelspace(): vmargs->fs_args[0]=%s\n",vmargs->fs_args[0]);
151  unsigned long long ll;
152  virgo_parse_integer(vmargs->fs_args[0],10,&ll);
153  filp_close(open_VFS_files[(int)ll],NULL);
154  return NULL;
155 }
156 EXPORT_SYMBOL(virgo_cloud_close_kernelspace);
157 
158 
159 
160 void printBytes(const char* str)
161 {
162  const char* p=str;
163  while(*p != '\0')
164  {
165  printk(KERN_INFO "printBytes(): %c\n",*p);
166  p++;
167  }
168  return;
169 }
170 
171 /*
172 Carried over _parse_integer() from lib/kstrtox.c and modified for VIRGO by
173 adding an additional if clause for quote and unquote as below.
174 Probably this missing clause could be causing the kstrtoll() and simple_strtoll()
175 which use _parse_integer() internally to randomly return junk addresses.
176 - Ka.Shrinivaasan 6November2013
177 */
178 unsigned int virgo_parse_integer(const char *s, unsigned int base, unsigned long long *p)
179 {
180  unsigned long long res;
181  unsigned int rv;
182  int overflow;
183 
184  res = 0;
185  rv = 0;
186  overflow = 0;
187  while (*s) {
188  printk(KERN_INFO "virgo_parse_integer(): *s=%c, res=%ld\n",*s, res);
189  unsigned int val;
190  if(*s=='\"')
191  {
192  s++;
193  continue;
194  }
195  if ('0' <= *s && *s <= '9')
196  val = *s - '0';
197  else if ('a' <= tolower(*s) && tolower(*s) <= 'f')
198  val = tolower(*s) - 'a' + 10;
199  else
200  break;
201 
202  if (val >= base)
203  break;
204  /*
205  * Check for overflow only if we are within range of
206  * it in the max base we support (16)
207  if (unlikely(res & (~0ull << 60))) {
208  if (res > div_u64(ULLONG_MAX - val, base))
209  overflow = 1;
210  }
211  */
212  res = res * base + val;
213  rv++;
214  s++;
215  }
216  *p = res;
217  return rv;
218 }
219 
220 char* toVFSString(int* data)
221 {
222  char* VFSString=kmalloc(BUF_SIZE, GFP_KERNEL);
223  sprintf(VFSString,"%d",*data);
224  printk(KERN_INFO "toVFSString(): VFSString=%s\n", VFSString);
225  return VFSString;
226 }
227 
228 
229 
230 MODULE_LICENSE("GPL");
231 module_init(virgo_cloud_fs_kernelspace_init);
232 module_exit(virgo_cloud_fs_kernelspace_exit);