1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1996 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <volmgt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/ddi.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <rpc/types.h>
#include "volmgt.h"
#include "volmgt_private.h"
#include "volmgt_fsi_private.h"
/* just utnil volmgt.h is up to date */
#ifndef VOL_RSV_MAXIDLEN
#define VOL_RSV_MAXIDLEN 256
#endif
/* this routine used by both acquire and release */
static char *fsi_xlate_name(char *);
/*
* arc approved interface
* - can not be modified without approval from an arc
*
* committment level:
* uncommitted
*
* description:
* volmgt_acquire: try to acquire the volmgt advisory device reservation
* for a specific device.
*
* arguments:
* dev - a device name to attempt reserving. This string can be:
* - a full path name to a device
* - a symbolic device name (e.g. floppy0)
*
* id - a reservation string that hopefully describes the application
* making this reservation.
*
* pid - a pointer to a pid_t type. If this argument is not NULL
* and the requested device is already reserved, the process
* id of the reservation owner will be returned in this
* location.
*
* ovr - an override indicator. If set to non-zero, the caller requests
* that this reservation be made unconditionally.
*
* err - the address of a pointer to a string which is to receive the
* id argument used when the current device was reserved. This
* is only used when the current reservation attempt fails due
* to an already existing reservation for this device.
*
* return value(s):
* A non-zero indicator if successful.
*
* A zero indicator if unsuccessful. If errno is EBUSY, then the err
* argument will be set to point to the string that the process currently
* holding the reservation supplied when reserving the device. It is up
* to the caller to release the storage occupied by the string via
* free(3C) when no longer needed.
*
* preconditions:
* none
*/
int
volmgt_acquire(char *dev, char *id, int ovr, char **err, pid_t *pidp)
{
char *targ_name = NULL;
struct stat sb;
dev_t idev;
vol_dbid_t dbid = (vol_dbid_t)-1;
vol_db_entry_t dbe;
vol_db_entry_t *dbp;
int retval = 0; /* default return => ERROR */
int reterr = 0;
#ifdef DEBUG
denter("volmgt_acquire(\"%s\", \"%s\", %s, %#p, %#p): entering\n",
dev ? dev : "<null ptr>", id ? id : "<null ptr>",
ovr ? "TRUE" : "FALSE", err, pidp);
#endif
/*
* the supplied arguments must not be NULL
*/
if ((dev == NULL) || (id == NULL) || (err == NULL)) {
errno = EINVAL;
goto dun;
}
/*
* the id string must not be longer than the maximum allowable
* number of characters
*/
if (strlen(id) > VOL_RSV_MAXIDLEN) {
errno = E2BIG;
goto dun;
}
if ((targ_name = fsi_xlate_name(dev)) == NULL) {
goto dun;
}
/*
* convert 'char *dev' to major/minor pair
*/
if (stat(targ_name, &sb) < 0) {
goto dun;
}
idev = sb.st_rdev;
/*
* open the database file
*/
if ((dbid = vol_db_open()) < 0) {
goto dun;
}
if ((dbp = vol_db_find(dbid, idev)) == NULL) {
/*
* the entry wasn't found, so reserve it
*/
dbe.dev_major = major(idev);
dbe.dev_minor = minor(idev);
dbe.pid = getpid();
dbe.id_tag = id;
if (vol_db_insert(dbid, &dbe) != 0) {
retval = 1; /* success! */
}
} else {
if (ovr || (vol_db_proc_find(dbp->pid) == 0)) {
/*
* the entry exists but either override was specified
* or the process holding the reservation is no longer
* active
*
* in either case we'll usurp the reservation
*/
if (vol_db_remove(dbid, idev) != 0) {
/* reserve the device */
dbe.dev_major = major(idev);
dbe.dev_minor = minor(idev);
dbe.pid = getpid();
dbe.id_tag = id;
if (vol_db_insert(dbid, &dbe) != 0) {
retval = 1;
}
}
} else {
/*
* the entry exists and override was NOT specified
*/
/*
* optionally return the pid of the reservation
* owner
*/
if (pidp != NULL) {
*pidp = dbp->pid;
}
*err = strdup(dbp->id_tag);
reterr = EBUSY;
}
vol_db_free(dbp); /* Release the entry */
}
/*
* if an error was encountered (currently only EBUSY supported)
* set errno to reflect it
*/
if (reterr != 0) {
errno = reterr;
}
dun:
if ((int)dbid >= 0) {
(void) vol_db_close(dbid);
}
if (targ_name != NULL) {
free(targ_name);
}
#ifdef DEBUG
dexit("volmgt_acquire: returning %s\n", retval ? "TRUE" : "FALSE");
#endif
return (retval);
}
/*
* arc approved interface
* - can not be modified without approval from an arc
*
* committment level:
* uncommitted
*
* description:
* volmgt_release: try to release the volmgt advisory device reservation
* for a specific device.
*
* arguments:
* dev - a device name to attempt reserving. This string can be:
* - a full path name to a device
* - a symbolic device name (e.g. floppy0)
*
* return value(s):
* A non-zero indicator if successful
* A zero indicator if unsuccessful
*
* preconditions:
* none
*/
int
volmgt_release(char *dev)
{
char *targ_name = NULL;
struct stat sb;
long idev;
vol_dbid_t dbid;
vol_db_entry_t *dbp;
int retval = 0; /* default return => FAILURE */
int reterr = 0;
#ifdef DEBUG
denter("volmgt_release(\"%s\"): entering\n", dev ? dev : "<null ptr>");
#endif
/*
* first let's do some minimal validation of the supplied arguments
*/
/*
* the supplied argument must not be NULL
*/
if (dev == NULL) {
errno = EINVAL;
goto dun;
}
if ((targ_name = fsi_xlate_name(dev)) == NULL) {
goto dun;
}
/*
* convert 'char *dev' to major/minor pair
*/
if (stat(targ_name, &sb) < 0) {
goto dun;
}
idev = sb.st_rdev;
/*
* open the database file
*/
if ((dbid = vol_db_open()) < 0) {
goto dun;
}
if ((dbp = vol_db_find(dbid, idev)) == NULL) {
/* the entry wasn't found so I can't clear reservation */
errno = ENOENT;
goto dun;
}
/* the entry was found so make sure I can clear it */
if (dbp->pid == getpid()) {
/*
* the reservation was made by me, clear it
*/
if (vol_db_remove(dbid, idev) != 0) {
retval = 1;
}
} else {
/*
* the entry wasn't made by me
*/
reterr = EBUSY;
}
vol_db_free(dbp);
/*
* if an error was encountered (currently only EBUSY supported)
* set errno to reflect it
*/
if (reterr != 0) {
errno = reterr;
}
dun:
if ((int)dbid >= 0) {
(void) vol_db_close(dbid);
}
if (targ_name != NULL) {
free(targ_name);
}
#ifdef DEBUG
dexit("volmgt_release: returning %s\n", retval ? "TRUE" : "FALSE");
#endif
return (retval);
}
/*
* translate suplied vol name into a pathname
*
* if volmgt is running, this pathname will be in /vol (or its equiv)
*
* if volmgt is not running then this path may be anywhere
*
* in either case the pathname will *not* be verified as a blk/chr dev
*
* if the return value is non-null then it's been alloced
*
* NOTE: assume "vol" is not a NULL ptr
*/
static char *
fsi_xlate_name(char *vol)
{
char *res = NULL; /* result to return */
char *vr; /* volmgt root dir */
bool_t vm_running = volmgt_running(); /* volmgt running? */
#ifdef DEBUG
denter("fsi_xlate_name(\"%s\"): entering\n", vol);
#endif
/* is it an absolute pathname ?? */
if (*vol == '/') {
if (vm_running) {
/* pathname must be in the /vol namespace */
vr = (char *)volmgt_root();
if (strncmp(vol, vr, strlen(vr)) != 0) {
/* not a cool pathname */
errno = EINVAL; /* XXX: is this correct */
goto dun;
}
}
res = strdup(vol);
} else {
/*
* if volmgt is running we can try to dereference it
* if volmgt isn't running then just give up
*/
if (!vm_running) {
/* some unknown "name" */
errno = ENOENT;
goto dun;
}
res = volmgt_symdev(vol);
}
dun:
#ifdef DEBUG
dexit("fsi_xlate_name: returning %s\n", res ? res : "<null ptr>");
#endif
return (res);
}
|