1 /* BLURB lgpl
2 
3                            Coda File System
4                               Release 5
5 
6           Copyright (c) 1987-1999 Carnegie Mellon University
7                   Additional copyrights listed below
8 
9 This  code  is  distributed "AS IS" without warranty of any kind under
10 the  terms of the  GNU  Library General Public Licence  Version 2,  as
11 shown in the file LICENSE. The technical and financial contributors to
12 Coda are listed in the file CREDITS.
13 
14                         Additional copyrights
15                            none currently
16 
17 #*/
18 
19 /*
20 *
21 *                   RVM Initialization and Termination
22 *
23 */
24 
25 #include "rvm_private.h"
26 
27 /* global variables */
28 extern log_t        *default_log;       /* default log descriptor ptr */
29 extern rvm_bool_t   rvm_utlsw;          /* true if call by rvmutl */
30 
31 char                rvm_errmsg;         /* internal error message ptr */
32 
33 /* initialization control */
34 /* Cannot statically initialize locks with pthreads. */
35 static RVM_MUTEX    init_lock = MUTEX_INITIALIZER;
36 static rvm_bool_t   inited = rvm_false;     /* initialization complete flag */
37 static rvm_bool_t   terminated = rvm_false; /* shutdown flag -- no
38                                                restart allowed */
39 
40 /* check that RVM properly initialized (for interface functions) */
bad_init(void)41 rvm_bool_t bad_init(void)
42 {
43     if (inited == rvm_true)           /* return reverse sense */
44         return rvm_false;
45     else
46         return rvm_true;
47 }
48 
49 /* rvm_initialize */
rvm_initialize(const char * rvm_version,rvm_options_t * rvm_options)50 rvm_return_t rvm_initialize(const char *rvm_version, rvm_options_t *rvm_options)
51 {
52     rvm_return_t    retval = RVM_SUCCESS;
53 
54     rvm_debug(0);                       /* only causes module loading */
55     if (strcmp(rvm_version,RVM_VERSION) != 0)
56         return RVM_EVERSION_SKEW;       /* version skew */
57     assert(sizeof(rvm_length_t) == sizeof(char *));
58     if ((retval=bad_options(rvm_options,rvm_true)) != RVM_SUCCESS)
59         return retval;                  /* bad options ptr or record */
60 
61     CRITICAL(init_lock,                 /* begin init_lock crit sec */
62         {
63         if (inited) goto err_exit;      /* did it all already ... */
64         if (terminated)
65             {
66             retval = RVM_EINIT;         /* restart not allowed */
67             goto err_exit;
68             }
69 
70         cthread_init();                 /* init Cthreads */
71 
72         /* init basic structures */
73         if ((init_utils()) != 0)
74             {
75             retval =  RVM_EIO;          /* can't get time stamp */
76 	    printf("Error in init_utils\n");
77             goto err_exit;
78             }
79         init_map_roots();               /* mapping list and tree */
80         init_log_list();                /* log device list */
81 
82         if (rvm_options && rvm_options->create_log_file)
83         {
84             retval = rvm_create_log(rvm_options, &rvm_options->create_log_size,
85                                     rvm_options->create_log_mode);
86 
87             if (retval != RVM_SUCCESS) {
88 		printf("rvm_create_log failed\n");
89 		goto err_exit;
90             }
91         }
92 
93         /* process options */
94         if ((retval=do_rvm_options(rvm_options)) != RVM_SUCCESS) {
95 		printf("do_rvm_options failed\n");
96 		goto err_exit;
97 	}
98 
99         /* take care of default log */
100         if (default_log == NULL) {
101 		if ((retval=do_log_options(NULL,NULL)) != RVM_SUCCESS) {
102 			printf("do_rvm_options failed\n");
103 			goto err_exit;
104 		}
105 	}
106         inited = rvm_true;              /* all done */
107 
108 err_exit:;
109         });                             /* end init_lock crit sec */
110 
111     return retval;
112     }
113 
114 /* rvm_terminate */
rvm_terminate(void)115 rvm_return_t rvm_terminate(void)
116 {
117     rvm_return_t    retval = RVM_SUCCESS;
118 
119     CRITICAL(init_lock,                 /* begin init_lock crit sec */
120         {
121         if (terminated) goto err_exit;  /* already terminated... */
122         if (!inited)
123             {
124             retval = RVM_EINIT;         /* RVM not initialized */
125             goto err_exit;
126             }
127 
128         /* close log devices (will check for active transactions) */
129         if ((retval=close_all_logs()) != RVM_SUCCESS)
130             goto err_exit;
131 
132         /* close segment devices */
133         if ((retval=close_all_segs()) != RVM_SUCCESS)
134             goto err_exit;
135 
136         /* abort any further RVM function calls */
137         inited = rvm_false;
138         terminated = rvm_true;
139 
140 err_exit:;
141         });                             /* end init_lock crit sec */
142 
143     return retval;
144 }
145