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
|
/* Virtual mount table related routines.
*
*/
#include "fs.h"
#include "vmnt.h"
#include <assert.h>
#include <string.h>
static int is_vmnt_locked(struct vmnt *vmp);
static void clear_vmnt(struct vmnt *vmp);
/* Is vmp pointer reasonable? */
#define SANEVMP(v) ((((v) >= &vmnt[0] && (v) < &vmnt[NR_MNTS])))
#define BADVMP(v, f, l) printf("%s:%d: bad vmp %p\n", f, l, v)
/* vp check that panics */
#define ASSERTVMP(v) if(!SANEVMP(v)) { \
BADVMP(v, __FILE__, __LINE__); panic("bad vmp"); }
#if LOCK_DEBUG
/*===========================================================================*
* check_vmnt_locks_by_me *
*===========================================================================*/
void check_vmnt_locks_by_me(struct fproc *rfp)
{
/* Check whether this thread still has locks held on vmnts */
struct vmnt *vmp;
for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; vmp++) {
if (tll_locked_by_me(&vmp->m_lock))
panic("Thread %d still holds vmnt lock on vmp %p call_nr=%d\n",
mthread_self(), vmp, job_call_nr);
}
if (rfp->fp_vmnt_rdlocks != 0)
panic("Thread %d still holds read locks on a vmnt (%d) call_nr=%d\n",
mthread_self(), rfp->fp_vmnt_rdlocks, job_call_nr);
}
#endif
/*===========================================================================*
* check_vmnt_locks *
*===========================================================================*/
void
check_vmnt_locks(void)
{
struct vmnt *vmp;
int count = 0;
for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; vmp++)
if (is_vmnt_locked(vmp)) {
count++;
printf("vmnt %p is %s, fs_e=%d dev=%llx\n", vmp, (tll_islocked(&vmp->m_lock) ? "locked":"pending locked"), vmp->m_fs_e, vmp->m_dev);
}
if (count) panic("%d locked vmnts\n", count);
#if 0
printf("check_vmnt_locks OK\n");
#endif
}
/*===========================================================================*
* mark_vmnt_free *
*===========================================================================*/
void mark_vmnt_free(struct vmnt *vmp)
{
ASSERTVMP(vmp);
vmp->m_fs_e = NONE;
vmp->m_dev = NO_DEV;
}
/*===========================================================================*
* clear_vmnt *
*===========================================================================*/
static void clear_vmnt(struct vmnt *vmp)
{
/* Reset vmp to initial parameters */
ASSERTVMP(vmp);
vmp->m_fs_e = NONE;
vmp->m_dev = NO_DEV;
vmp->m_flags = 0;
vmp->m_mounted_on = NULL;
vmp->m_root_node = NULL;
vmp->m_label[0] = '\0';
vmp->m_comm.c_max_reqs = 1;
vmp->m_comm.c_cur_reqs = 0;
vmp->m_comm.c_req_queue = NULL;
}
/*===========================================================================*
* get_free_vmnt *
*===========================================================================*/
struct vmnt *get_free_vmnt(void)
{
struct vmnt *vmp;
for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
if (vmp->m_dev == NO_DEV) {
clear_vmnt(vmp);
return(vmp);
}
}
return(NULL);
}
/*===========================================================================*
* find_vmnt *
*===========================================================================*/
struct vmnt *find_vmnt(endpoint_t fs_e)
{
/* Find the vmnt belonging to an FS with endpoint 'fs_e' iff it's in use */
struct vmnt *vp;
for (vp = &vmnt[0]; vp < &vmnt[NR_MNTS]; ++vp)
if (vp->m_fs_e == fs_e && vp->m_dev != NO_DEV)
return(vp);
return(NULL);
}
/*===========================================================================*
* init_vmnts *
*===========================================================================*/
void init_vmnts(void)
{
/* Initialize vmnt table */
struct vmnt *vmp;
for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; vmp++) {
clear_vmnt(vmp);
tll_init(&vmp->m_lock);
}
}
/*===========================================================================*
* is_vmnt_locked *
*===========================================================================*/
static int is_vmnt_locked(struct vmnt *vmp)
{
ASSERTVMP(vmp);
return(tll_islocked(&vmp->m_lock) || tll_haspendinglock(&vmp->m_lock));
}
/*===========================================================================*
* lock_vmnt *
*===========================================================================*/
int lock_vmnt(struct vmnt *vmp, tll_access_t locktype)
{
int r;
tll_access_t initial_locktype;
ASSERTVMP(vmp);
initial_locktype = (locktype == VMNT_EXCL) ? VMNT_WRITE : locktype;
if (vmp->m_fs_e == who_e) return(EDEADLK);
r = tll_lock(&vmp->m_lock, initial_locktype);
if (r == EBUSY) return(r);
if (initial_locktype != locktype) {
upgrade_vmnt_lock(vmp);
}
#if LOCK_DEBUG
if (locktype == VMNT_READ)
fp->fp_vmnt_rdlocks++;
#endif
return(OK);
}
/*===========================================================================*
* vmnt_unmap_by_endpoint *
*===========================================================================*/
void vmnt_unmap_by_endpt(endpoint_t proc_e)
{
struct vmnt *vmp;
if ((vmp = find_vmnt(proc_e)) != NULL) {
mark_vmnt_free(vmp);
fs_cancel(vmp);
invalidate_filp_by_endpt(proc_e);
if (vmp->m_mounted_on) {
/* Only put mount point when it was actually used as mount
* point. That is, the mount was succesful. */
put_vnode(vmp->m_mounted_on);
}
}
}
/*===========================================================================*
* unlock_vmnt *
*===========================================================================*/
void unlock_vmnt(struct vmnt *vmp)
{
ASSERTVMP(vmp);
#if LOCK_DEBUG
/* Decrease read-only lock counter when not locked as VMNT_WRITE or
* VMNT_EXCL */
if (!tll_locked_by_me(&vmp->m_lock))
fp->fp_vmnt_rdlocks--;
#endif
tll_unlock(&vmp->m_lock);
#if LOCK_DEBUG
assert(!tll_locked_by_me(&vmp->m_lock));
#endif
}
/*===========================================================================*
* downgrade_vmnt_lock *
*===========================================================================*/
void downgrade_vmnt_lock(struct vmnt *vmp)
{
ASSERTVMP(vmp);
tll_downgrade(&vmp->m_lock);
#if LOCK_DEBUG
/* If we're no longer the owner of a lock, we downgraded to VMNT_READ */
if (!tll_locked_by_me(&vmp->m_lock)) {
fp->fp_vmnt_rdlocks++;
}
#endif
}
/*===========================================================================*
* upgrade_vmnt_lock *
*===========================================================================*/
void upgrade_vmnt_lock(struct vmnt *vmp)
{
ASSERTVMP(vmp);
tll_upgrade(&vmp->m_lock);
}
/*===========================================================================*
* fetch_vmnt_paths *
*===========================================================================*/
void fetch_vmnt_paths(void)
{
struct vmnt *vmp;
struct vnode *cur_wd;
char orig_path[PATH_MAX];
cur_wd = fp->fp_wd;
for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; vmp++) {
if (vmp->m_dev == NO_DEV)
continue;
if (vmp->m_fs_e == PFS_PROC_NR)
continue;
strlcpy(orig_path, vmp->m_mount_path, PATH_MAX);
/* Find canonical path */
if (canonical_path(vmp->m_mount_path, fp) != OK) {
/* We failed to find it (moved somewhere else?). Let's try
* again by starting at the node on which we are mounted:
* pretend that node is our working directory and look for the
* canonical path of the relative path to the mount point
* (which should be in our 'working directory').
*/
char *mp;
int len;
fp->fp_wd = vmp->m_mounted_on; /* Change our working dir */
/* Isolate the mount point name of the full path */
len = strlen(vmp->m_mount_path);
if (vmp->m_mount_path[len - 1] == '/') {
vmp->m_mount_path[len - 1] = '\0';
}
mp = strrchr(vmp->m_mount_path, '/');
strlcpy(vmp->m_mount_path, mp+1, NAME_MAX+1);
if (canonical_path(vmp->m_mount_path, fp) != OK) {
/* Our second try failed too. Maybe an FS has crashed
* and we're missing part of the tree. Revert path.
*/
strlcpy(vmp->m_mount_path, orig_path, PATH_MAX);
}
fp->fp_wd = cur_wd; /* Revert working dir */
}
}
}
|