3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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.
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.
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/>.
29 #include <partition.h>
30 #include <net/socket.h>
34 #define PROTO_MKDIR 0x1
35 #define PROTO_RMDIR 0x2
36 #define PROTO_CREAT 0x3
38 #define PROTO_CHDIR 0x5
39 #define PROTO_READ 0x6
41 partition_t
*znfs_part
;
43 unsigned char znfs_dir_id
;
45 /* clear and pre-set dir[] structure */
46 void znfs_dir_flush (unsigned n
)
48 strcpy (dir
[0].name
, ".");
49 strcpy (dir
[1].name
, "..");
53 unsigned i
= znfs_dir_id
;
56 memset (dir
[i
].name
, 0, 10);
59 int znfs_send (int fd
, unsigned char cmd
, char *data
, unsigned len
)
61 char *s
= kmalloc (sizeof (char) * len
+ 2);
67 memcpy (s
+1, data
, len
);
69 int ret
= send (fd
, s
, len
+1, 0);
76 int znfs_recv (int fd
, char *data
, unsigned len
)
78 return recv (fd
, data
, len
, 0);
81 /* function for read file content */
82 unsigned znfs_read (partition_t
*p
, unsigned char *name
, vfs_content_t
*content
)
84 unsigned char l
= strlen (name
);
92 memcpy (data
+1, name
, l
);
94 // Set socket to blocking mode
95 int oldFlag
= fcntl (sock
, F_GETFL
, 0);
96 oldFlag
&= ~O_NONBLOCK
;
98 if (fcntl (sock
, F_SETFL
, oldFlag
) == -1) {
99 printf ("Cant set socket to nonblocking mode\n");
103 znfs_send (sock
, PROTO_READ
, data
, l
+1);
107 cache_t
*cache
= cache_create (0, 0, 0);
115 r
= znfs_recv (sock
, block
, 512);
117 cache_add (block
, r
);
123 // Set socket to non-blocking mode
124 oldFlag
= fcntl (sock
, F_GETFL
, 0);
125 if (fcntl (sock
, F_SETFL
, oldFlag
| O_NONBLOCK
) == -1) {
126 printf ("Cant set socket to nonblocking mode\n");
130 content
->ptr
= (char *) &cache
->data
;
131 content
->len
= cache
->limit
;
136 /* function for create clear directory */
137 unsigned znfs_create_directory (partition_t
*p
, unsigned char *name
)
139 unsigned char l
= strlen (name
);
147 memcpy (data
+1, name
, l
);
149 znfs_send (sock
, PROTO_MKDIR
, data
, l
+1);
152 memcpy (dir
[znfs_dir_id
].name
, name
, l
);
153 dir
[znfs_dir_id
].name
[l
] = '\0';
155 dir
[znfs_dir_id
].dir
= 1;
162 /* function for create blank file */
163 unsigned znfs_create_file (partition_t
*p
, unsigned char *name
)
165 unsigned char l
= strlen (name
);
173 memcpy (data
+1, name
, l
);
175 znfs_send (sock
, PROTO_CREAT
, data
, l
+1);
177 memcpy (dir
[znfs_dir_id
].name
, name
, l
);
178 dir
[znfs_dir_id
].name
[l
] = '\0';
180 dir
[znfs_dir_id
].dir
= 0;
187 /* go to next directory placed in current directory */
188 void znfs_cd (partition_t
*p
, unsigned char *name
)
190 unsigned short l
= strlen (name
);
198 memcpy (data
+1, name
, l
);
200 znfs_send (sock
, PROTO_CHDIR
, data
, l
+1);
204 // Set socket to blocking mode
205 int oldFlag
= fcntl (sock
, F_GETFL
, 0);
206 oldFlag
&= ~O_NONBLOCK
;
208 if (fcntl (sock
, F_SETFL
, oldFlag
) == -1) {
209 printf ("Cant set socket to nonblocking mode\n");
212 int r
= znfs_recv (sock
, data
, 1024);
214 // Set socket to non-blocking mode
215 oldFlag
= fcntl (sock
, F_GETFL
, 0);
216 if (fcntl (sock
, F_SETFL
, oldFlag
| O_NONBLOCK
) == -1) {
217 printf ("Cant set socket to nonblocking mode\n");
221 kprintf ("ZNFS -> error in receiving directory list\n");
225 unsigned char n
= r
/13;
230 } __attribute__ ((__packed__
));
232 struct dent
*d
= (struct dent
*) data
;
234 for (l
= 0; l
< n
; l
++) {
235 memcpy (dir
[l
].name
, d
[l
].name
, 12);
237 if (d
[l
].type
== 0x4)
244 /* go to root '/' directory on partition */
245 void znfs_cd_root (partition_t
*p
)
252 znfs_send (sock
, PROTO_CHDIR
, data
, 2);
256 // Set socket to blocking mode
257 int oldFlag
= fcntl (sock
, F_GETFL
, 0);
258 oldFlag
&= ~O_NONBLOCK
;
260 if (fcntl (sock
, F_SETFL
, oldFlag
) == -1) {
261 printf ("Cant set socket to nonblocking mode\n");
264 int r
= znfs_recv (sock
, data
, 1024);
266 // Set socket to non-blocking mode
267 oldFlag
= fcntl (sock
, F_GETFL
, 0);
268 if (fcntl (sock
, F_SETFL
, oldFlag
| O_NONBLOCK
) == -1) {
269 printf ("Cant set socket to nonblocking mode\n");
273 kprintf ("ZNFS -> error in receiving directory list\n");
277 unsigned char n
= r
/13;
283 } __attribute__ ((__packed__
));
285 struct dent
*d
= (struct dent
*) data
;
287 for (l
= 0; l
< n
; l
++) {
288 memcpy (dir
[l
].name
, d
[l
].name
, 12);
290 if (d
[l
].type
== 0x4)
297 /* zexos filesystem initialization function */
298 unsigned znfs_init (partition_t
*p
, char *addr
)
300 #ifdef CONFIG_DRV_ZNFS
305 sockaddr_in serverSock
;
307 // Let's get info about remote computer
308 if ((host
= gethostbyname ((char *) addr
)) == NULL
) {
309 printf ("Wrong address -> %s:%d\n", addr
, port
);
314 if ((sock
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) == -1) {
315 printf ("Cant create socket\n");
319 // Fill structure sockaddr_in
320 // 1) Family of protocols
321 serverSock
.sin_family
= AF_INET
;
322 // 2) Number of server port
323 serverSock
.sin_port
= swap16 (port
);
324 // 3) Setup ip address of server, where we want to connect
325 memcpy (&(serverSock
.sin_addr
), host
->h_addr
, host
->h_length
);
329 // Now we are able to connect to remote server
330 if (connect (sock
, (void *) &serverSock
, sizeof (serverSock
)) == -1) {
331 printf ("Connection cant be estabilished -> %s:%d\n", addr
, port
);
335 // Set socket to non-blocking mode
336 int oldFlag
= fcntl (sock
, F_GETFL
, 0);
337 if (fcntl (sock
, F_SETFL
, oldFlag
| O_NONBLOCK
) == -1) {
338 printf ("Cant set socket to nonblocking mode\n");
347 /* handler which is used by device management and pretty needed
348 for communication between zexos and this filesystem */
349 bool znfs_handler (unsigned act
, char *block
, unsigned n
, unsigned long l
)
359 return znfs_read (curr_part
, dir
[n
].name
, (vfs_content_t
*) block
);
365 //znfs_write (curr_part, block);
373 znfs_cd_root (curr_part
);
377 /* re-load content of current directory */
381 /* go to previous directory */
383 znfs_cd (curr_part
, "..");
387 /* go to another directory */
388 znfs_cd (curr_part
, dir
[n
].name
);
395 znfs_create_directory (curr_part
, block
);
402 znfs_create_file (curr_part
, block
);