xref: /minix/minix/lib/libvtreefs/vtreefs.c (revision 0a6a1f1d)
1 /* VTreeFS - vtreefs.c - initialization and message loop */
2 
3 #include "inc.h"
4 
5 static unsigned int inodes;
6 static struct inode_stat *root_stat;
7 static index_t root_entries;
8 static size_t buf_size;
9 static size_t extra_size;
10 
11 /*
12  * Initialize internal state.  This is the only place where dynamic memory
13  * allocation takes place.
14  */
15 static int
16 init_server(int __unused type, sef_init_info_t * __unused info)
17 {
18 	int r;
19 
20 	/* Initialize the virtual tree. */
21 	if ((r = init_inodes(inodes, root_stat, root_entries)) != OK)
22 		panic("init_inodes failed: %d", r);
23 
24 	/* Initialize extra data. */
25 	if ((r = init_extra(inodes, extra_size)) != OK)
26 		panic("init_extra failed: %d", r);
27 
28 	/* Initialize the I/O buffer. */
29 	if ((r = init_buf(buf_size)) != OK)
30 		panic("init_buf failed: %d", r);
31 
32 	return OK;
33 }
34 
35 /*
36  * We received a signal.
37  */
38 static void
39 got_signal(int sig)
40 {
41 
42 	if (sig != SIGTERM)
43 		return;
44 
45 	fsdriver_terminate();
46 }
47 
48 /*
49  * SEF initialization.
50  */
51 static void
52 sef_local_startup(void)
53 {
54 	sef_setcb_init_fresh(init_server);
55 	sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL);
56 
57 	sef_setcb_signal_handler(got_signal);
58 
59 	sef_startup();
60 }
61 
62 /*
63  * We have received a message that is not a file system request from VFS.
64  * Call the message hook, if there is one.
65  */
66 void
67 fs_other(const message * m_ptr, int ipc_status)
68 {
69 	message msg;
70 
71 	if (vtreefs_hooks->message_hook != NULL) {
72 		/*
73 		 * Not all of vtreefs's users play nice with the message, so
74 		 * make a copy to allow it to be modified.
75 		 */
76 		msg = *m_ptr;
77 
78 		vtreefs_hooks->message_hook(&msg, ipc_status);
79 	}
80 }
81 
82 /*
83  * This is the main routine of this service.  It uses the main loop as provided
84  * by the fsdriver library.  The routine returns once the file system has been
85  * unmounted and the process is signaled to exit.
86  */
87 void
88 run_vtreefs(struct fs_hooks * hooks, unsigned int nr_inodes,
89 	size_t inode_extra, struct inode_stat * istat,
90 	index_t nr_indexed_entries, size_t bufsize)
91 {
92 
93 	/*
94 	 * Use global variables to work around the inability to pass parameters
95 	 * through SEF to the initialization function..
96 	 */
97 	vtreefs_hooks = hooks;
98 	inodes = nr_inodes;
99 	extra_size = inode_extra;
100 	root_stat = istat;
101 	root_entries = nr_indexed_entries;
102 	buf_size = bufsize;
103 
104 	sef_local_startup();
105 
106 	fsdriver_task(&vtreefs_table);
107 
108 	cleanup_buf();
109 	cleanup_inodes();
110 }
111