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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
/*
* 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 1994-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Methods of the cfsd_maptbl classes.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <synch.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/vfs.h>
#include <sys/cred.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/fs/cachefs_fs.h>
#include <sys/fs/cachefs_dlog.h>
#include <mdbug/mdbug.h>
#include "cfsd.h"
#include "cfsd_maptbl.h"
/*
* cfsd_maptbl_create
*
* Description:
* Constructor for the cfsd_maptbl class.
* Just does some setup not much else.
* Arguments:
* Returns:
* Preconditions:
*/
cfsd_maptbl_object_t *
cfsd_maptbl_create(void)
{
cfsd_maptbl_object_t *maptbl_object_p;
dbug_enter("cfsd_maptbl_create");
maptbl_object_p = cfsd_calloc(sizeof (cfsd_maptbl_object_t));
maptbl_object_p->i_fid = -1;
maptbl_object_p->i_pa = NULL;
maptbl_object_p->i_paoff = 0;
maptbl_object_p->i_paend = 0;
maptbl_object_p->i_palen = 0;
dbug_leave("cfsd_maptbl_create");
return (maptbl_object_p);
}
/*
* cfsd_maptbl_destroy
*
* Description:
* Destructor for the cfsd_maptbl class.
* Arguments:
* Returns:
* Preconditions:
*/
void
cfsd_maptbl_destroy(cfsd_maptbl_object_t *maptbl_object_p)
{
dbug_enter("cfsd_maptbl_destroy");
dbug_precond(maptbl_object_p);
maptbl_teardown(maptbl_object_p);
cfsd_free(maptbl_object_p);
dbug_leave("cfsd_maptbl_destroy");
}
/*
* maptbl_domap
*
* Description:
* Maps in the specified section of the file.
* Arguments:
* off The offset to map in. Must be i_pagesize aligned.
* Returns:
* Returns 0 for success or an errno value on failure.
* Preconditions:
*/
int
maptbl_domap(cfsd_maptbl_object_t *maptbl_object_p, off_t off)
{
int xx;
int len;
dbug_enter("maptbl_domap");
dbug_precond(maptbl_object_p);
dbug_precond(maptbl_object_p->i_fid >= 0);
len = maptbl_object_p->i_maplen;
maptbl_object_p->i_stat_mapmove++;
/* destroy old mapping if it exists */
if (maptbl_object_p->i_pa) {
/* determine how far we have to move the map */
maptbl_object_p->i_stat_mapdist +=
abs(maptbl_object_p->i_paoff - off);
/* remove the map */
xx = munmap(maptbl_object_p->i_pa, maptbl_object_p->i_palen);
if (xx == -1) {
xx = errno;
dbug_print(("error", "Could not unmap %s, %d, %p, %d",
maptbl_object_p->i_name, xx, maptbl_object_p->i_pa,
maptbl_object_p->i_palen));
}
maptbl_object_p->i_pa = NULL;
maptbl_object_p->i_palen = 0;
maptbl_object_p->i_paoff = 0;
maptbl_object_p->i_paend = 0;
}
/* do the mapping */
maptbl_object_p->i_pa =
mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
maptbl_object_p->i_fid, off);
if (maptbl_object_p->i_pa == MAP_FAILED) {
xx = errno;
dbug_print(("error",
"Could not map %s, error %d, off %d, len %d",
maptbl_object_p->i_name, xx, off, len));
maptbl_object_p->i_pa = NULL;
dbug_leave("maptbl_domap");
return (xx);
}
maptbl_object_p->i_palen = len;
maptbl_object_p->i_paoff = off;
maptbl_object_p->i_paend = off + len - 1;
dbug_leave("maptbl_domap");
return (0);
}
/*
* maptbl_getaddr
*
* Description:
* Returns an address of a particular entry in the file.
* Arguments:
* index
* Returns:
* Returns NULL for a failure with the mapping file.
* Preconditions:
*/
caddr_t
maptbl_getaddr(cfsd_maptbl_object_t *maptbl_object_p, int index)
{
off_t start;
off_t end;
caddr_t pa;
dbug_enter("maptbl_getaddr");
dbug_precond(maptbl_object_p);
dbug_precond(index < maptbl_object_p->i_entries);
/* find the boundaries of the entry */
start = index * sizeof (struct cfs_dlog_mapping_space);
end = start + sizeof (struct cfs_dlog_mapping_space) - 1;
/* map the entry in if necessary */
if ((start < maptbl_object_p->i_paoff) ||
(maptbl_object_p->i_paend < end)) {
if (maptbl_domap(maptbl_object_p,
start & maptbl_object_p->i_pagemask)) {
dbug_leave("maptbl_getaddr");
return (NULL);
}
}
/* make an address and return it */
pa = maptbl_object_p->i_pa + (start - maptbl_object_p->i_paoff);
dbug_leave("maptbl_getaddr");
return (pa);
}
/*
* maptbl_cidhashaddr
*
* Description:
* Finds the address of the specified cid by hashing to
* the appropriate entry. If the cid does not already
* exist in the file, then the address of where it should
* reside is returned.
* Arguments:
* cid
* addrp
* Returns:
* Returns 0 for success, 1 if entry not found, -1 if an
* error occurs in the mapping file.
* Preconditions:
*/
int
maptbl_cidhashaddr(cfsd_maptbl_object_t *maptbl_object_p,
cfs_cid_t cid,
caddr_t *addrp)
{
ino64_t *pa;
int index;
ino64_t fileno;
int start_index;
dbug_enter("maptbl_cidhashaddr");
dbug_precond(maptbl_object_p);
dbug_precond(addrp);
maptbl_object_p->i_stat_requests++;
/* get the index from the first hash function */
index = maptbl_hash1(maptbl_object_p, cid);
maptbl_object_p->i_stat_probes++;
/* get the address of the entry */
pa = (ino64_t *)maptbl_getaddr(maptbl_object_p, index);
if (pa == NULL) {
dbug_leave("maptbl_cidhashaddr");
return (-1);
}
fileno = *pa;
/* check for match */
if (fileno == cid.cid_fileno) {
*addrp = (caddr_t)pa;
dbug_leave("maptbl_cidhashaddr");
return (0);
}
/* check for not found */
if (fileno == 0) {
*addrp = (caddr_t)pa;
dbug_leave("maptbl_cidhashaddr");
return (1);
}
/* get the index from the second hash function */
index = maptbl_hash2(maptbl_object_p, cid, index);
/* do a linear search for a match or empty entry */
start_index = index;
do {
maptbl_object_p->i_stat_probes++;
/* get the address of the entry */
pa = (ino64_t *)maptbl_getaddr(maptbl_object_p, index);
if (pa == NULL) {
dbug_leave("maptbl_cidhashaddr");
return (-1);
}
fileno = *pa;
/* check for match */
if (fileno == cid.cid_fileno) {
*addrp = (caddr_t)pa;
dbug_leave("maptbl_cidhashaddr");
return (0);
}
/* check for not found */
if (fileno == 0) {
*addrp = (caddr_t)pa;
dbug_leave("maptbl_cidhashaddr");
return (1);
}
/* move to the next entry */
index++;
index = index % maptbl_object_p->i_entries;
} while (start_index != index);
/* table full, this is bad */
dbug_print(("error", "Table is full"));
dbug_leave("maptbl_cidhashaddr");
return (-1);
}
/*
* maptbl_hash1
*
* Description:
* Hashes a cid into an index into the table.
* Arguments:
* cid
* Returns:
* Returns the index.
* Preconditions:
*/
int
maptbl_hash1(cfsd_maptbl_object_t *maptbl_object_p, cfs_cid_t cid)
{
unsigned int xx;
unsigned int a, b;
dbug_precond(maptbl_object_p);
#if 0
xx = cid.cid_fileno % i_entries;
#else
a = cid.cid_fileno >> 16;
b = a ^ cid.cid_fileno;
xx = b % maptbl_object_p->i_entries;
#endif
return (xx);
}
/*
* maptbl_hash2
*
* Description:
* Hashes a cid into an index into the table.
* Arguments:
* cid
* index
* Returns:
* Returns the index.
* Preconditions:
*/
int
maptbl_hash2(cfsd_maptbl_object_t *maptbl_object_p, cfs_cid_t cid, int index)
{
unsigned int xx;
unsigned int a, b, c, d;
dbug_precond(maptbl_object_p);
#if 0
a = cid.cid_fileno & 0x0ff;
b = (cid.cid_fileno >> 8) & 0x0ff;
b = cid.cid_fileno ^ a ^ b;
xx = b % maptbl_object_p->i_hash2mod;
#else
a = cid.cid_fileno & 0x0ff;
b = (cid.cid_fileno >> 8) & 0x0ff;
c = (cid.cid_fileno >> 16) & 0x0ff;
d = (cid.cid_fileno >> 24) & 0x0ff;
xx = cid.cid_fileno ^ (a << 8) ^ b ^ c ^ d;
xx = xx % maptbl_object_p->i_hash2mod;
#endif
xx = (index + xx) % maptbl_object_p->i_entries;
return (xx);
}
/*
* maptbl_setup
*
* Description:
* Performs setup for the cfsd_maptbl class.
* This routine must be called before other routines are used.
* Arguments:
* filename
* Returns:
* Returns 0 for success or an errno value.
* Preconditions:
* precond(filename)
*/
int
maptbl_setup(cfsd_maptbl_object_t *maptbl_object_p, const char *filename)
{
int xx;
struct stat sinfo;
off_t offset;
long *lp;
size_t cnt;
off_t size;
dbug_enter("maptbl_setup");
dbug_precond(maptbl_object_p);
dbug_precond(filename);
/* clean up from a previous setup */
maptbl_teardown(maptbl_object_p);
strlcpy(maptbl_object_p->i_name, filename,
sizeof (maptbl_object_p->i_name));
dbug_print(("info", "filename %s", maptbl_object_p->i_name));
/* get the page info */
maptbl_object_p->i_pagesize = PAGESIZE;
maptbl_object_p->i_pagemask = PAGEMASK;
maptbl_object_p->i_maplen = maptbl_object_p->i_pagesize * 100;
/* open the file */
maptbl_object_p->i_fid = open(maptbl_object_p->i_name,
O_RDWR | O_NONBLOCK);
if (maptbl_object_p->i_fid == -1) {
xx = errno;
dbug_print(("error",
"Could not open %s, %d", maptbl_object_p->i_name, xx));
dbug_leave("maptbl_setup");
return (xx);
}
/* get the size and type of file */
xx = fstat(maptbl_object_p->i_fid, &sinfo);
if (xx) {
xx = errno;
dbug_print(("error",
"Could not stat %s, %d", maptbl_object_p->i_name, xx));
dbug_leave("maptbl_setup");
return (xx);
}
maptbl_object_p->i_size = sinfo.st_size;
/* sanity check, better be a regular file */
if (!S_ISREG(sinfo.st_mode)) {
xx = ENOTSUP;
dbug_print(("error",
"%s Not a regular file.", maptbl_object_p->i_name));
dbug_leave("maptbl_setup");
return (xx);
}
/* determine number of entries */
maptbl_object_p->i_entries =
maptbl_object_p->i_size / sizeof (struct cfs_dlog_mapping_space);
/* set up modulo value for second hash function */
maptbl_object_p->i_hash2mod = (maptbl_object_p->i_entries / 2) + 1;
/* initialize statistic gathering */
maptbl_object_p->i_stat_requests = 0;
maptbl_object_p->i_stat_probes = 0;
maptbl_object_p->i_stat_mapmove = 0;
maptbl_object_p->i_stat_mapdist = 0;
maptbl_object_p->i_stat_filled = 0;
/* zero the file */
for (offset = 0; offset < maptbl_object_p->i_size;
offset += maptbl_object_p->i_maplen) {
/* map in a section of the file */
xx = maptbl_domap(maptbl_object_p, offset);
if (xx) {
dbug_leave("maptbl_setup");
return (xx);
}
/* zero this section of the file */
lp = (long *)maptbl_object_p->i_pa;
size = maptbl_object_p->i_size - offset;
if (size < maptbl_object_p->i_palen) {
cnt = size / sizeof (long);
} else {
cnt = maptbl_object_p->i_palen / sizeof (long);
dbug_assert((cnt * sizeof (long)) ==
maptbl_object_p->i_palen);
}
memset(lp, 0, cnt * sizeof (*lp));
}
/* return success */
dbug_leave("maptbl_setup");
return (0);
}
/*
* maptbl_teardown
*
* Description:
* Arguments:
* Returns:
* Preconditions:
*/
void
maptbl_teardown(cfsd_maptbl_object_t *maptbl_object_p)
{
int xx;
dbug_enter("maptbl_teardown");
dbug_precond(maptbl_object_p);
if (maptbl_object_p->i_pa) {
xx = munmap(maptbl_object_p->i_pa, maptbl_object_p->i_palen);
if (xx == -1) {
xx = errno;
dbug_print(("error", "Could not unmap %s, %d, %p, %d",
maptbl_object_p->i_name, xx, maptbl_object_p->i_pa,
maptbl_object_p->i_palen));
}
maptbl_object_p->i_pa = NULL;
}
maptbl_object_p->i_paoff = 0;
maptbl_object_p->i_paend = 0;
maptbl_object_p->i_palen = 0;
if (maptbl_object_p->i_fid != -1) {
if (close(maptbl_object_p->i_fid))
dbug_print(("err", "cannot close maptbl fd, error %d",
errno));
maptbl_object_p->i_fid = -1;
}
dbug_leave("maptbl_teardown");
}
/*
* maptbl_get
*
* Description:
* Gets the mapping info for the specified cid.
* Arguments:
* cid
* valuep
* Returns:
* Returns 0 for success, 1 if entry not found, -1 if an
* error occurs in the mapping file.
* Preconditions:
* precond(valuep)
*/
int
maptbl_get(cfsd_maptbl_object_t *maptbl_object_p,
cfs_cid_t cid,
struct cfs_dlog_mapping_space *valuep)
{
int xx;
struct cfs_dlog_mapping_space *pa;
dbug_enter("maptbl_get");
dbug_precond(maptbl_object_p);
dbug_precond(valuep);
if (maptbl_object_p->i_entries == 0) {
dbug_leave("maptbl_get");
return (1);
}
xx = maptbl_cidhashaddr(maptbl_object_p, cid, (caddr_t *)&pa);
if (xx == 0)
*valuep = *pa;
dbug_leave("maptbl_get");
return (xx);
}
/*
* maptbl_set
*
* Description:
* Sets the mapping info for the cid.
* If insert is 1 then if the entry is not found it is put in the
* table.
* Arguments:
* valuep
* insert
* Returns:
* Returns 0 if mapping info placed in the table, 1 if entry
* is not found an insert is 0, -1 if an error occurs in the
* mapping file.
* Preconditions:
* precond(valuep)
*/
int
maptbl_set(cfsd_maptbl_object_t *maptbl_object_p,
struct cfs_dlog_mapping_space *valuep,
int insert)
{
int xx;
struct cfs_dlog_mapping_space *pa;
dbug_enter("maptbl_set");
dbug_precond(maptbl_object_p);
dbug_precond(valuep);
dbug_assert(maptbl_object_p->i_entries > 0);
xx = maptbl_cidhashaddr(maptbl_object_p, valuep->ms_cid,
(caddr_t *)&pa);
if ((xx == 0) || ((xx == 1) && insert)) {
*pa = *valuep;
if (xx == 1)
maptbl_object_p->i_stat_filled++;
xx = 0;
}
dbug_leave("maptbl_set");
return (xx);
}
/*
* maptbl_dumpstats
*
* Description:
* Prints out various stats about the hashing.
* Arguments:
* Returns:
* Preconditions:
*/
void
maptbl_dumpstats(cfsd_maptbl_object_t *maptbl_object_p)
{
int xx;
double dd;
dbug_enter("maptbl_dumpstats");
dbug_precond(maptbl_object_p);
dbug_print(("dump", "Total Entries %d", maptbl_object_p->i_entries));
dbug_print(("dump", "Filled Entries %d",
maptbl_object_p->i_stat_filled));
dbug_print(("dump", "Requests %d", maptbl_object_p->i_stat_requests));
dbug_print(("dump", "Probes %d", maptbl_object_p->i_stat_probes));
dbug_print(("dump", "Map Moves %d", maptbl_object_p->i_stat_mapmove));
dbug_print(("dump", "Mapping Size %d", maptbl_object_p->i_maplen));
dbug_print(("dump", "File Size %d", maptbl_object_p->i_size));
if (maptbl_object_p->i_stat_requests == 0) {
dbug_leave("maptbl_dumpstats");
return;
}
dd = (double)maptbl_object_p->i_stat_probes /
maptbl_object_p->i_stat_requests;
dbug_print(("dump", "Probes per Request %.2f", dd));
dd = (double)maptbl_object_p->i_stat_mapmove /
maptbl_object_p->i_stat_requests;
dbug_print(("dump", "Mmap moves per Request %.2f", dd));
xx = maptbl_object_p->i_stat_mapdist / maptbl_object_p->i_stat_mapmove;
dbug_print(("dump", "Average distance per mmap moves %d", xx));
xx = ((100.0 * maptbl_object_p->i_stat_filled) /
maptbl_object_p->i_entries) + .5;
dbug_print(("dump", "Table filled %d%%", xx));
dbug_leave("maptbl_dumpstats");
}
|