summaryrefslogtreecommitdiff
path: root/sys/fs/efs/efs_subr.c
blob: 9364cb730e075d4821cd71eb10749c774c37398a (plain)
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
/*	$NetBSD: efs_subr.c,v 1.14 2021/12/10 20:36:04 andvar Exp $	*/

/*
 * Copyright (c) 2006 Stephen M. Rumble <rumble@ephemeral.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: efs_subr.c,v 1.14 2021/12/10 20:36:04 andvar Exp $");

#include <sys/param.h>
#include <sys/kauth.h>
#include <sys/lwp.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/stat.h>
#include <sys/malloc.h>

#include <miscfs/genfs/genfs_node.h>

#include <fs/efs/efs.h>
#include <fs/efs/efs_sb.h>
#include <fs/efs/efs_dir.h>
#include <fs/efs/efs_genfs.h>
#include <fs/efs/efs_mount.h>
#include <fs/efs/efs_extent.h>
#include <fs/efs/efs_dinode.h>
#include <fs/efs/efs_inode.h>
#include <fs/efs/efs_subr.h>

struct pool efs_inode_pool;

/*
 * Calculate a checksum for the provided superblock in __host byte order__.
 *
 * At some point SGI changed the checksum algorithm slightly, which can be
 * enabled with the 'new' flag.
 *
 * Presumably this change occurred on or before 24 Oct 1988 (around IRIX 3.1),
 * so we're pretty unlikely to ever actually see an old checksum. Further, it
 * means that EFS_NEWMAGIC filesystems (IRIX >= 3.3) must match the new
 * checksum whereas EFS_MAGIC filesystems could potentially use either
 * algorithm.
 *
 * See comp.sys.sgi <1991Aug9.050838.16876@odin.corp.sgi.com>
 */
int32_t
efs_sb_checksum(struct efs_sb *esb, int new)
{
	int i;
	int32_t cksum;
	uint8_t *sbarray = (uint8_t *)esb;

	KASSERT((EFS_SB_CHECKSUM_SIZE % 2) == 0);

	for (i = cksum = 0; i < EFS_SB_CHECKSUM_SIZE; i += 2) {
		uint16_t v;
		memcpy(&v, &sbarray[i], sizeof(v));
		cksum ^= be16toh(v);
		cksum  = (cksum << 1) | (new && cksum < 0);
	}

	return (cksum);
}

/*
 * Determine if the superblock is valid.
 *
 * Returns 0 if valid, else invalid. If invalid, 'why' is set to an
 * explanation.
 */
int 
efs_sb_validate(struct efs_sb *esb, const char **why)
{
	uint32_t ocksum, ncksum;

	*why = NULL;

	if (be32toh(esb->sb_magic) != EFS_SB_MAGIC &&
	    be32toh(esb->sb_magic) != EFS_SB_NEWMAGIC) {
		*why = "sb_magic invalid";
		return (1);
	}

	ocksum = htobe32(efs_sb_checksum(esb, 0));
	ncksum = htobe32(efs_sb_checksum(esb, 1));
	if (esb->sb_checksum != ocksum && esb->sb_checksum != ncksum) {
		*why = "sb_checksum invalid";
		return (1);
	}

	if (be32toh(esb->sb_size) > EFS_SIZE_MAX) {
		*why = "sb_size > EFS_SIZE_MAX";
		return (1);
	}

	if (be32toh(esb->sb_firstcg) <= EFS_BB_BITMAP) {
		*why = "sb_firstcg <= EFS_BB_BITMAP";
		return (1);
	}

	/* XXX - add better sb consistency checks here */
	if (esb->sb_cgfsize == 0 ||
	    esb->sb_cgisize == 0 ||
	    esb->sb_ncg == 0 ||
	    esb->sb_bmsize == 0) {
		*why = "something bad happened";
		return (1);
	}

	return (0);
}

/*
 * Determine the basic block offset and inode index within that block, given
 * the inode 'ino' and filesystem parameters _in host byte order_. The inode
 * will live at byte address 'bboff' * EFS_BB_SIZE + 'index' * EFS_DINODE_SIZE.
 */
void
efs_locate_inode(ino_t ino, struct efs_sb *sbp, uint32_t *bboff, int *index)
{
	uint32_t cgfsize, firstcg;
	uint16_t cgisize;

	cgisize = be16toh(sbp->sb_cgisize);
	cgfsize = be32toh(sbp->sb_cgfsize);
	firstcg = be32toh(sbp->sb_firstcg);

	*bboff = firstcg + ((ino / (cgisize * EFS_DINODES_PER_BB)) * cgfsize) +
	    ((ino % (cgisize * EFS_DINODES_PER_BB)) / EFS_DINODES_PER_BB);
	*index = ino & (EFS_DINODES_PER_BB - 1);
}

/*
 * Read in an inode from disk.
 *
 * We actually take in four inodes at a time. Hopefully these will stick
 * around in the buffer cache and get used without going to disk.
 *
 * Returns 0 on success.
 */
int
efs_read_inode(struct efs_mount *emp, ino_t ino, struct lwp *l,
    struct efs_dinode *di)
{
	struct efs_sb *sbp;
	struct buf *bp;
	int index, err;
	uint32_t bboff;

	sbp = &emp->em_sb;
	efs_locate_inode(ino, sbp, &bboff, &index);

	err = efs_bread(emp, bboff, l, &bp);
	if (err) {
		return (err);
	}
	memcpy(di, ((struct efs_dinode *)bp->b_data) + index, sizeof(*di));
	brelse(bp, 0);

	return (0);
}

/*
 * Perform a read from our device handling the potential DEV_BSIZE
 * messiness (although as of 19.2.2006, all ports appear to use 512) as
 * we as EFS block sizing.
 *
 * bboff: basic block offset
 *
 * Returns 0 on success.
 */
int
efs_bread(struct efs_mount *emp, uint32_t bboff, struct lwp *l, struct buf **bp)
{
	KASSERT(bboff < EFS_SIZE_MAX);

	return (bread(emp->em_devvp, (daddr_t)bboff * (EFS_BB_SIZE / DEV_BSIZE),
	    EFS_BB_SIZE, 0, bp));
}

/*
 * Synchronise the in-core, host ordered and typed inode fields with their
 * corresponding on-disk, EFS ordered and typed copies.
 *
 * This is the inverse of efs_dinode_sync_inode(), and should be called when
 * an inode is loaded from disk.
 */
void
efs_sync_dinode_to_inode(struct efs_inode *ei)
{

	ei->ei_mode		= be16toh(ei->ei_di.di_mode);	/*same as nbsd*/
	ei->ei_nlink		= be16toh(ei->ei_di.di_nlink);
	ei->ei_uid		= be16toh(ei->ei_di.di_uid);
	ei->ei_gid		= be16toh(ei->ei_di.di_gid);
	ei->ei_size		= be32toh(ei->ei_di.di_size);
	ei->ei_atime		= be32toh(ei->ei_di.di_atime);
	ei->ei_mtime		= be32toh(ei->ei_di.di_mtime);
	ei->ei_ctime		= be32toh(ei->ei_di.di_ctime);
	ei->ei_gen		= be32toh(ei->ei_di.di_gen);
	ei->ei_numextents 	= be16toh(ei->ei_di.di_numextents);
	ei->ei_version		= ei->ei_di.di_version;
}

/*
 * Synchronise the on-disk, EFS ordered and typed inode fields with their
 * corresponding in-core, host ordered and typed copies.
 *
 * This is the inverse of efs_inode_sync_dinode(), and should be called before
 * an inode is flushed to disk.
 */
void
efs_sync_inode_to_dinode(struct efs_inode *ei)
{
	
	panic("readonly -- no need to call me");
}

#ifdef DIAGNOSTIC
/*
 * Ensure that the in-core inode's host cached fields match its on-disk copy.
 * 
 * Returns 0 if they match.
 */
static int
efs_is_inode_synced(struct efs_inode *ei)
{
	int s;

	s = 0;
	/* XXX -- see above remarks about assumption */
	s += (ei->ei_mode	!= be16toh(ei->ei_di.di_mode));
	s += (ei->ei_nlink	!= be16toh(ei->ei_di.di_nlink));
	s += (ei->ei_uid	!= be16toh(ei->ei_di.di_uid));
	s += (ei->ei_gid	!= be16toh(ei->ei_di.di_gid));
	s += (ei->ei_size	!= be32toh(ei->ei_di.di_size));
	s += (ei->ei_atime	!= be32toh(ei->ei_di.di_atime));
	s += (ei->ei_mtime	!= be32toh(ei->ei_di.di_mtime));
	s += (ei->ei_ctime	!= be32toh(ei->ei_di.di_ctime));
	s += (ei->ei_gen	!= be32toh(ei->ei_di.di_gen));
	s += (ei->ei_numextents	!= be16toh(ei->ei_di.di_numextents));
	s += (ei->ei_version	!= ei->ei_di.di_version);

	return (s);
}
#endif

/*
 * Given an efs_dirblk structure and a componentname to search for, return the
 * corresponding inode if it is found.
 *
 * Returns 0 on success.
 */
static int
efs_dirblk_lookup(struct efs_dirblk *dir, struct componentname *cn,
    ino_t *inode)
{
	struct efs_dirent *de;
	int i, slot __diagused, offset;

	KASSERT(cn->cn_namelen <= EFS_DIRENT_NAMELEN_MAX);

	slot = offset = 0;

	for (i = 0; i < dir->db_slots; i++) {
		offset = EFS_DIRENT_OFF_EXPND(dir->db_space[i]);

		if (offset == EFS_DIRBLK_SLOT_FREE)
			continue;

		de = (struct efs_dirent *)((char *)dir + offset);
		if (de->de_namelen == cn->cn_namelen &&
		   (strncmp(cn->cn_nameptr, de->de_name, cn->cn_namelen) == 0)){
			slot = i;
			break;
		}
	}
	if (i == dir->db_slots)
		return (ENOENT);

	KASSERT(slot < offset && offset < EFS_DIRBLK_SPACE_SIZE);
	de = (struct efs_dirent *)((char *)dir + offset);
	*inode = be32toh(de->de_inumber);

	return (0);
}

/*
 * Given an extent descriptor that represents a directory, look up
 * componentname within its efs_dirblk's. If it is found, return the
 * corresponding inode in 'ino'.
 *
 * Returns 0 on success.
 */
static int
efs_extent_lookup(struct efs_mount *emp, struct efs_extent *ex,
    struct componentname *cn, ino_t *ino)
{
	struct efs_dirblk *db;
	struct buf *bp;
	int i, err;

	/*
	 * Read in each of the dirblks until we find our entry.
	 * If we don't, return ENOENT.
	 */
	for (i = 0; i < ex->ex_length; i++) {
		err = efs_bread(emp, ex->ex_bn + i, NULL, &bp);
		if (err) {
			printf("efs: warning: invalid extent descriptor\n");
			return (err);
		}

		db = (struct efs_dirblk *)bp->b_data;
		if (efs_dirblk_lookup(db, cn, ino) == 0) {
			brelse(bp, 0);
			return (0);
		}
		brelse(bp, 0);
	}
	
	return (ENOENT);
}

/*
 * Given the provided in-core inode, look up the pathname requested. If
 * we find it, 'ino' reflects its corresponding on-disk inode number.
 *
 * Returns 0 on success.
 */
int
efs_inode_lookup(struct efs_mount *emp, struct efs_inode *ei,
    struct componentname *cn, ino_t *ino)
{
	struct efs_extent ex;
	struct efs_extent_iterator exi;
	int ret;
	
	KASSERT(VOP_ISLOCKED(ei->ei_vp));
#ifdef DIAGNOSTIC
	KASSERT(efs_is_inode_synced(ei) == 0);
#endif
	KASSERT((ei->ei_mode & S_IFMT) == S_IFDIR);

	efs_extent_iterator_init(&exi, ei, 0);
	while ((ret = efs_extent_iterator_next(&exi, &ex)) == 0) {
		if (efs_extent_lookup(emp, &ex, cn, ino) == 0) {
			return (0);
		}
	}

	return ((ret == -1) ? ENOENT : ret);
}

/*
 * Convert on-disk extent structure to in-core format.
 */
void 
efs_dextent_to_extent(struct efs_dextent *dex, struct efs_extent *ex)
{

	KASSERT(dex != NULL && ex != NULL);

	ex->ex_magic	= dex->ex_bytes[0];
	ex->ex_bn	= be32toh(dex->ex_words[0]) & 0x00ffffff;
	ex->ex_length	= dex->ex_bytes[4];
	ex->ex_offset	= be32toh(dex->ex_words[1]) & 0x00ffffff;
}

/*
 * Convert in-core extent format to on-disk structure.
 */
void
efs_extent_to_dextent(struct efs_extent *ex, struct efs_dextent *dex)
{

	KASSERT(ex != NULL && dex != NULL);
	KASSERT(ex->ex_magic == EFS_EXTENT_MAGIC);
	KASSERT((ex->ex_bn & ~EFS_EXTENT_BN_MASK) == 0);
	KASSERT((ex->ex_offset & ~EFS_EXTENT_OFFSET_MASK) == 0);

	dex->ex_words[0] = htobe32(ex->ex_bn);
	dex->ex_bytes[0] = ex->ex_magic;
	dex->ex_words[1] = htobe32(ex->ex_offset);
	dex->ex_bytes[4] = ex->ex_length;
}

/*
 * Initialise an extent iterator.
 *
 * If start_hint is non-0, attempt to set up the iterator beginning with the
 * extent descriptor in which the start_hint'th byte exists. Callers must not
 * expect success (this is simply an optimisation), so we reserve the right
 * to start from the beginning.
 */
void
efs_extent_iterator_init(struct efs_extent_iterator *exi, struct efs_inode *eip,
    off_t start_hint)
{
	struct efs_extent ex, ex2;
	struct buf *bp;
	struct efs_mount *emp = VFSTOEFS(eip->ei_vp->v_mount);
	off_t offset, length, next;
	int i, err, numextents, numinextents;
	int hi, lo, mid;
	int indir;
	
	exi->exi_eip	= eip;
	exi->exi_next	= 0;
	exi->exi_dnext	= 0;
	exi->exi_innext	= 0;

	if (start_hint == 0)
		return;

	/* force iterator to end if hint is too big */
	if (start_hint >= eip->ei_size) {
		exi->exi_next = eip->ei_numextents;
		return;
	}

	/*
	 * Use start_hint to jump to the right extent descriptor. We'll
	 * iterate over the 12 indirect extents because it's cheap, then
	 * bring the appropriate vector into core and binary search it.
	 */

	/*
	 * Handle the small file case separately first...
	 */
	if (eip->ei_numextents <= EFS_DIRECTEXTENTS) {
		for (i = 0; i < eip->ei_numextents; i++) {
			efs_dextent_to_extent(&eip->ei_di.di_extents[i], &ex);

			offset = ex.ex_offset * EFS_BB_SIZE;
			length = ex.ex_length * EFS_BB_SIZE;

			if (start_hint >= offset &&
			    start_hint < (offset + length)) {
				exi->exi_next = exi->exi_dnext = i;
				return;
			}
		}

		/* shouldn't get here, no? */
		EFS_DPRINTF(("efs_extent_iterator_init: bad direct extents\n"));
		return;
	}

	/*
	 * Now do the large files with indirect extents...
	 *
	 * The first indirect extent's ex_offset field contains the
	 * number of indirect extents used.
	 */
	efs_dextent_to_extent(&eip->ei_di.di_extents[0], &ex);

	numinextents = ex.ex_offset;
	if (numinextents < 1 || numinextents >= EFS_DIRECTEXTENTS) {
		EFS_DPRINTF(("efs_extent_iterator_init: bad ex.ex_offset\n"));
		return;
	}

	next = 0;
	indir = -1;
	numextents = 0;
	for (i = 0; i < numinextents; i++) {
		efs_dextent_to_extent(&eip->ei_di.di_extents[i], &ex);

		err = efs_bread(emp, ex.ex_bn, NULL, &bp);
		if (err) {
			return;
		}

		efs_dextent_to_extent((struct efs_dextent *)bp->b_data, &ex2);
		brelse(bp, 0);

		offset = ex2.ex_offset * EFS_BB_SIZE;

		if (offset > start_hint) {
			indir = MAX(0, i - 1);
			break;
		}

		/* number of extents prior to this indirect vector of extents */
		next += numextents;

		/* number of extents within this indirect vector of extents */
		numextents = ex.ex_length * EFS_EXTENTS_PER_BB;
		numextents = MIN(numextents, eip->ei_numextents - next);
	}

	/*
	 * We hit the end, so assume it's in the last extent.
	 */
	if (indir == -1)
		indir = numinextents - 1;

	/*
	 * Binary search to find our desired direct extent.
	 */
	lo = 0;
	mid = 0;
	hi = numextents - 1;
	efs_dextent_to_extent(&eip->ei_di.di_extents[indir], &ex);
	while (lo <= hi) {
		int bboff;
		int index;

		mid = (lo + hi) / 2;

		bboff = mid / EFS_EXTENTS_PER_BB;
		index = mid % EFS_EXTENTS_PER_BB;

		err = efs_bread(emp, ex.ex_bn + bboff, NULL, &bp);
		if (err) {
			EFS_DPRINTF(("efs_extent_iterator_init: bsrch read\n"));
			return;
		}

		efs_dextent_to_extent((struct efs_dextent *)bp->b_data + index,
		    &ex2);
		brelse(bp, 0);

		offset = ex2.ex_offset * EFS_BB_SIZE;
		length = ex2.ex_length * EFS_BB_SIZE;

		if (start_hint >= offset && start_hint < (offset + length))
			break;

		if (start_hint < offset)
			hi = mid - 1;
		else
			lo = mid + 1;
	}

	/*
	 * This is bad. Either the hint is bogus (which shouldn't
	 * happen) or the extent list must be screwed up. We
	 * have to abort.
	 */
	if (lo > hi) {
		EFS_DPRINTF(("efs_extent_iterator_init: bsearch "
		    "failed to find extent\n"));
		return;
	}

	exi->exi_next	= next + mid;
	exi->exi_dnext	= indir;
	exi->exi_innext	= mid;
}

/*
 * Return the next EFS extent.
 *
 * Returns 0 if another extent was iterated, -1 if we've exhausted all
 * extents, or an error number. If 'exi' is non-NULL, the next extent is
 * written to it (should it exist).
 */
int
efs_extent_iterator_next(struct efs_extent_iterator *exi,
    struct efs_extent *exp)
{
	struct efs_extent ex;
	struct efs_dextent *dexp;
	struct efs_inode *eip = exi->exi_eip;
	struct buf *bp;
	int err, bboff, index;

	if (exi->exi_next++ >= eip->ei_numextents)
		return (-1);

	/* direct or indirect extents? */
	if (eip->ei_numextents <= EFS_DIRECTEXTENTS) {
		if (exp != NULL) {
			dexp = &eip->ei_di.di_extents[exi->exi_dnext++];
			efs_dextent_to_extent(dexp, exp);
		}
	} else {
		efs_dextent_to_extent(
		    &eip->ei_di.di_extents[exi->exi_dnext], &ex);

		bboff	= exi->exi_innext / EFS_EXTENTS_PER_BB;
		index	= exi->exi_innext % EFS_EXTENTS_PER_BB;

		err = efs_bread(VFSTOEFS(eip->ei_vp->v_mount),
		    ex.ex_bn + bboff, NULL, &bp);
		if (err) {
			EFS_DPRINTF(("efs_extent_iterator_next: "
			    "efs_bread failed: %d\n", err));
			return (err);
		}

		if (exp != NULL) {
			dexp = (struct efs_dextent *)bp->b_data + index;
			efs_dextent_to_extent(dexp, exp);
		}
		brelse(bp, 0);

		bboff = exi->exi_innext++ / EFS_EXTENTS_PER_BB;
		if (bboff >= ex.ex_length) {
			exi->exi_innext = 0;
			exi->exi_dnext++;
		}
	}

	return (0);
}