Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*      $NetBSD: lfs_inode.c,v 1.28 2015/10/15 06:25:12 dholland Exp $ */

/*-
 * Copyright (c) 1980, 1991, 1993, 1994
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
 The Regents of the University of California.  All rights reserved.");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/1/95";
#else
__RCSID("$NetBSD: lfs_inode.c,v 1.28 2015/10/15 06:25:12 dholland Exp $");
#endif
#endif /* not lint */

#include <sys/param.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/mount.h>

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "dump.h"
#undef di_inumber

#define	HASDUMPEDFILE	0x1
#define	HASSUBDIRS	0x2

struct lfs *sblock;

int is_ufs2 = 0;

/*
 * Read the superblock from disk, and check its magic number.
 * Determine whether byte-swapping needs to be done on this filesystem.
 */
int
fs_read_sblock(char *superblock)
{
	/*
	 * XXX this should not be assuming that the in-memory
	 * superblock has the on-disk superblock at the front of the
	 * structure.
	 */
	union {
		char tbuf[LFS_SBPAD];
		struct lfs lfss;
	} u;

	off_t sboff = LFS_LABELPAD;

	sblock = (struct lfs *)superblock;
	while(1) {
		rawread(sboff, (char *) sblock, LFS_SBPAD);
		switch (sblock->lfs_dlfs_u.u_32.dlfs_magic) {
		    case LFS_MAGIC:
			sblock->lfs_is64 = false;
			sblock->lfs_dobyteswap = false;
			break;
		    case LFS_MAGIC_SWAPPED:
			sblock->lfs_is64 = false;
			sblock->lfs_dobyteswap = true;
			break;
		    case LFS64_MAGIC:
			sblock->lfs_is64 = true;
			sblock->lfs_dobyteswap = false;
			break;
		    case LFS64_MAGIC_SWAPPED:
			sblock->lfs_is64 = true;
			sblock->lfs_dobyteswap = true;
			break;
		    default:
			quit("bad sblock magic number\n");
			break;
		}
		if (lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 0)) != sboff) {
			sboff = lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 0));
			continue;
		}
		break;
	}

	/*
	 * Read the secondary and take the older of the two
	 */
	rawread(lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 1)), u.tbuf,
	    sizeof(u.tbuf));

	if (u.lfss.lfs_dlfs_u.u_32.dlfs_magic !=
	    sblock->lfs_dlfs_u.u_32.dlfs_magic) {
		msg("Warning: secondary superblock at 0x%" PRIx64 " mismatched or wrong magic\n",
			LFS_FSBTODB(sblock, (off_t)lfs_sb_getsboff(sblock, 1)));
	} else {
		u.lfss.lfs_is64 = sblock->lfs_is64;
		u.lfss.lfs_dobyteswap = sblock->lfs_dobyteswap;

		if (lfs_sb_getversion(sblock) > 1) {
			if (lfs_sb_getserial(&u.lfss) < lfs_sb_getserial(sblock)) {
				memcpy(sblock, u.tbuf, sizeof(u.tbuf));
				sboff = lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 1));
			}
		} else {
			if (lfs_sb_getotstamp(&u.lfss) < lfs_sb_getotstamp(sblock)) {
				memcpy(sblock, u.tbuf, sizeof(u.tbuf));
				sboff = lfs_fsbtob(sblock, (off_t)lfs_sb_getsboff(sblock, 1));
			}
		}
	}
	if (sboff != LFS_SBPAD) {
		msg("Using superblock at alternate location 0x%lx\n",
		    (unsigned long)(btodb(sboff)));
	}

	/* ugh */
	is_ufs2 = sblock->lfs_is64;

	/*
	 * XXX for now dump won't work on lfs64 because of the 64-bit
	 * inodes in directories. dump needs more abstraction.
	 */
	if (sblock->lfs_is64) {
		quit("LFS64 directory entries not supported yet");
	}

	return sblock->lfs_dobyteswap;
}

/*
 * Fill in the ufsi struct, as well as the maxino and dev_bsize global
 * variables.
 */
struct ufsi *
fs_parametrize(void)
{
	static struct ufsi ufsi;

	spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);

	ufsi.ufs_dsize = LFS_FSBTODB(sblock, lfs_sb_getsize(sblock));
	if (lfs_sb_getversion(sblock) == 1) 
		ufsi.ufs_dsize = lfs_sb_getsize(sblock) >> lfs_sb_getblktodb(sblock);
	ufsi.ufs_bsize = lfs_sb_getbsize(sblock);
	ufsi.ufs_bshift = lfs_sb_getbshift(sblock);
	ufsi.ufs_fsize = lfs_sb_getfsize(sblock);
	ufsi.ufs_frag = lfs_sb_getfrag(sblock);
	ufsi.ufs_fsatoda = lfs_sb_getfsbtodb(sblock);
	if (lfs_sb_getversion(sblock) == 1)
		ufsi.ufs_fsatoda = 0;
	ufsi.ufs_nindir = lfs_sb_getnindir(sblock);
	ufsi.ufs_inopb = lfs_sb_getinopb(sblock);
	ufsi.ufs_maxsymlinklen = lfs_sb_getmaxsymlinklen(sblock);
	ufsi.ufs_bmask = ~(lfs_sb_getbmask(sblock));
	ufsi.ufs_qbmask = lfs_sb_getbmask(sblock);
	ufsi.ufs_fmask = ~(lfs_sb_getffmask(sblock));
	ufsi.ufs_qfmask = lfs_sb_getffmask(sblock);

	dev_bsize = lfs_sb_getbsize(sblock) >> lfs_sb_getblktodb(sblock);

	return &ufsi;
}

ino_t
fs_maxino(void)
{
	return ((getino(LFS_IFILE_INUM)->dp1.di_size
		   - (lfs_sb_getcleansz(sblock) + lfs_sb_getsegtabsz(sblock))
		   * lfs_sb_getbsize(sblock))
		  / lfs_sb_getbsize(sblock)) * lfs_sb_getifpb(sblock) - 1;
}

void
fs_mapinodes(ino_t maxino, u_int64_t *tapesz, int *anydirskipped)
{
	ino_t ino;

	for (ino = ULFS_ROOTINO; ino < maxino; ino++)
		mapfileino(ino, tapesz, anydirskipped);
}

/*
 * XXX KS - I know there's a better way to do this.
 */
#define BASE_SINDIR (ULFS_NDADDR)
#define BASE_DINDIR (ULFS_NDADDR+LFS_NINDIR(fs))
#define BASE_TINDIR (ULFS_NDADDR+LFS_NINDIR(fs)+LFS_NINDIR(fs)*LFS_NINDIR(fs))

#define D_UNITS (LFS_NINDIR(fs))
#define T_UNITS (LFS_NINDIR(fs)*LFS_NINDIR(fs))

static daddr_t
lfs_bmap(struct lfs *fs, union lfs_dinode *idinode, daddr_t lbn)
{
	daddr_t residue, up;
	int off=0;
	char bp[MAXBSIZE];

	up = UNASSIGNED;	/* XXXGCC -Wunitialized [sh3] */
	
	if(lbn > 0 && lbn > lfs_lblkno(fs, lfs_dino_getsize(fs, idinode))) {
		return UNASSIGNED;
	}
	/*
	 * Indirect blocks: if it is a first-level indirect, pull its
	 * address from the inode; otherwise, call ourselves to find the
	 * address of the parent indirect block, and load that to find
	 * the desired address.
	 */
	if(lbn < 0) {
		lbn *= -1;
		if (lbn == ULFS_NDADDR) {
			/* printf("lbn %d: single indir base\n", -lbn); */
			return lfs_dino_getib(fs, idinode, 0); /* single indirect */
		} else if(lbn == BASE_DINDIR+1) {
			/* printf("lbn %d: double indir base\n", -lbn); */
			return lfs_dino_getib(fs, idinode, 1); /* double indirect */
		} else if(lbn == BASE_TINDIR+2) {
			/* printf("lbn %d: triple indir base\n", -lbn); */
			return lfs_dino_getib(fs, idinode, 2); /* triple indirect */
		}

		/*
		 * Find the immediate parent. This is essentially finding the
		 * residue of modulus, and then rounding accordingly.
		 */
		residue = (lbn-ULFS_NDADDR) % LFS_NINDIR(fs);
		if(residue == 1) {
			/* Double indirect.  Parent is the triple. */
			up = lfs_dino_getib(fs, idinode, 2);
			off = (lbn-2-BASE_TINDIR)/(LFS_NINDIR(fs)*LFS_NINDIR(fs));
			if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
				return UNASSIGNED;
			/* printf("lbn %d: parent is the triple\n", -lbn); */
			bread(LFS_FSBTODB(sblock, up), bp, lfs_sb_getbsize(sblock));
			return lfs_iblock_get(fs, bp, off);
		} else /* residue == 0 */ {
			/* Single indirect.  Two cases. */
			if(lbn < BASE_TINDIR) {
				/* Parent is the double, simple */
				up = -(BASE_DINDIR) - 1;
				off = (lbn-BASE_DINDIR) / D_UNITS;
				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
			} else {
				/* Ancestor is the triple, more complex */
				up = ((lbn-BASE_TINDIR) / T_UNITS)
					* T_UNITS + BASE_TINDIR + 1;
				off = (lbn/D_UNITS) - (up/D_UNITS);
				up = -up;
				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
			}
		}
	} else {
		/* Direct block.  Its parent must be a single indirect. */
		if (lbn < ULFS_NDADDR)
			return lfs_dino_getdb(fs, idinode, lbn);
		else {
			/* Parent is an indirect block. */
			up = -(((lbn-ULFS_NDADDR) / D_UNITS) * D_UNITS + ULFS_NDADDR);
			off = (lbn-ULFS_NDADDR) % D_UNITS;
			/* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
		}
	}
	up = lfs_bmap(fs,idinode,up);
	if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
		return UNASSIGNED;
	bread(LFS_FSBTODB(sblock, up), bp, lfs_sb_getbsize(sblock));
	return lfs_iblock_get(fs, bp, off);
}

static IFILE *
lfs_ientry(ino_t ino)
{
	static char ifileblock[MAXBSIZE];
	static daddr_t ifblkno;
	daddr_t lbn;
	daddr_t blkno;
	union dinode *dp;
	union lfs_dinode *ldp;
	unsigned index;
    
	lbn = ino/lfs_sb_getifpb(sblock) + lfs_sb_getcleansz(sblock) + lfs_sb_getsegtabsz(sblock);
	dp = getino(LFS_IFILE_INUM);
	/* XXX this is foolish */
	if (sblock->lfs_is64) {
		ldp = (union lfs_dinode *)&dp->dlp64;
	} else {
		ldp = (union lfs_dinode *)&dp->dlp32;
	}
	blkno = lfs_bmap(sblock, ldp, lbn);
	if (blkno != ifblkno)
		bread(LFS_FSBTODB(sblock, blkno), ifileblock,
		    lfs_sb_getbsize(sblock));
	index = ino % lfs_sb_getifpb(sblock);
	if (sblock->lfs_is64) {
		return (IFILE *) &((IFILE64 *)ifileblock)[index];
	} else if (lfs_sb_getversion(sblock) > 1) {
		return (IFILE *) &((IFILE32 *)ifileblock)[index];
	} else {
		return (IFILE *) &((IFILE_V1 *)ifileblock)[index];
	}
}

/* Search a block for a specific dinode. */
static union lfs_dinode *
lfs_ifind(struct lfs *fs, ino_t ino, void *block)
{
	union lfs_dinode *dip;
	unsigned i, num;

	num = LFS_INOPB(fs);
	for (i = num; i-- > 0; ) {
		dip = DINO_IN_BLOCK(fs, block, i);
		if (lfs_dino_getinumber(fs, dip) == ino)
			return dip;
	}
	return NULL;
}

union dinode *
getino(ino_t inum)
{
	static daddr_t inoblkno;
	daddr_t blkno;
	static union {
		char space[MAXBSIZE];
		struct lfs64_dinode u_64[MAXBSIZE/sizeof(struct lfs64_dinode)];
		struct lfs32_dinode u_32[MAXBSIZE/sizeof(struct lfs32_dinode)];
	} inoblock;
	static union dinode ifile_dinode; /* XXX fill this in */
	static union dinode empty_dinode; /* Always stays zeroed */
	union lfs_dinode *dp;
	ino_t inum2;

	if (inum == LFS_IFILE_INUM) {
		/* Load the ifile inode if not already */
		inum2 = sblock->lfs_is64 ?
			ifile_dinode.dlp64.di_inumber :
			ifile_dinode.dlp32.di_inumber;
		if (inum2 == 0) {
			blkno = lfs_sb_getidaddr(sblock);
			bread(LFS_FSBTODB(sblock, blkno), inoblock.space,
				(int)lfs_sb_getbsize(sblock));
			dp = lfs_ifind(sblock, inum, inoblock.space);
			/* Structure copy */
			if (sblock->lfs_is64) {
				ifile_dinode.dlp64 = dp->u_64;
			} else {
				ifile_dinode.dlp32 = dp->u_32;
			}
		}
		return &ifile_dinode;
	}

	curino = inum;
	blkno = lfs_if_getdaddr(sblock, lfs_ientry(inum));
	if(blkno == LFS_UNUSED_DADDR)
		return &empty_dinode;

	if(blkno != inoblkno) {
		bread(LFS_FSBTODB(sblock, blkno), inoblock.space,
			(int)lfs_sb_getbsize(sblock));
	}
	return (void *)lfs_ifind(sblock, inum, inoblock.space);
}

/*
 * Tell the filesystem not to overwrite any currently dirty segments
 * until we are finished.  (It may still write into clean segments, of course,
 * since we're not using those.)  This is only called when dump_lfs is called
 * with -X, i.e. we are working on a mounted filesystem.
 */
static int root_fd = -1;
char *wrap_mpname;

int
lfs_wrap_stop(char *mpname)
{
	int waitfor = 0;

	root_fd = open(mpname, O_RDONLY, 0);
	if (root_fd < 0)
		return -1;
	wrap_mpname = mpname;
	fcntl(root_fd, LFCNREWIND, -1); /* Ignore return value */
	if (fcntl(root_fd, LFCNWRAPSTOP, &waitfor) < 0) {
		perror("LFCNWRAPSTOP");
		return -1;
	}
	msg("Disabled log wrap on %s\n", mpname);
	return 0;
}

/*
 * Allow the filesystem to continue normal operation.
 * This would happen anyway when we exit; we do it explicitly here
 * to show the message, for the user's benefit.
 */
void
lfs_wrap_go(void)
{
	int waitfor = 0;

	if (root_fd < 0)
		return;

	fcntl(root_fd, LFCNWRAPGO, &waitfor);
	close(root_fd);
	root_fd = -1;
	msg("Re-enabled log wrap on %s\n", wrap_mpname);
}