1998-08-20 08:19:55 +00:00
|
|
|
/* $NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1996 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1996 TooLs GmbH.
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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.
|
|
|
|
*/
|
|
|
|
|
2001-09-30 22:28:01 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
/*
|
|
|
|
* Stand-alone ISO9660 file reading package.
|
|
|
|
*
|
|
|
|
* Note: This doesn't support Rock Ridge extensions, extended attributes,
|
|
|
|
* blocksizes other than 2048 bytes, multi-extent files, etc.
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <string.h>
|
2019-02-20 21:07:09 +00:00
|
|
|
#include <stdbool.h>
|
2000-01-18 07:37:10 +00:00
|
|
|
#include <sys/dirent.h>
|
2017-12-05 21:37:50 +00:00
|
|
|
#include <fs/cd9660/iso.h>
|
|
|
|
#include <fs/cd9660/cd9660_rrip.h>
|
1998-08-20 08:19:55 +00:00
|
|
|
|
|
|
|
#include "stand.h"
|
|
|
|
|
2001-11-06 19:59:19 +00:00
|
|
|
#define SUSP_CONTINUATION "CE"
|
|
|
|
#define SUSP_PRESENT "SP"
|
|
|
|
#define SUSP_STOP "ST"
|
|
|
|
#define SUSP_EXTREF "ER"
|
|
|
|
#define RRIP_NAME "NM"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ISO_SUSP_HEADER h;
|
|
|
|
u_char signature [ISODCL ( 5, 6)];
|
|
|
|
u_char len_skp [ISODCL ( 7, 7)]; /* 711 */
|
|
|
|
} ISO_SUSP_PRESENT;
|
|
|
|
|
2001-11-06 17:13:05 +00:00
|
|
|
static int buf_read_file(struct open_file *f, char **buf_p,
|
|
|
|
size_t *size_p);
|
1998-09-18 22:58:01 +00:00
|
|
|
static int cd9660_open(const char *path, struct open_file *f);
|
1998-08-20 08:19:55 +00:00
|
|
|
static int cd9660_close(struct open_file *f);
|
2001-11-06 17:13:05 +00:00
|
|
|
static int cd9660_read(struct open_file *f, void *buf, size_t size,
|
|
|
|
size_t *resid);
|
1998-08-20 08:19:55 +00:00
|
|
|
static off_t cd9660_seek(struct open_file *f, off_t offset, int where);
|
|
|
|
static int cd9660_stat(struct open_file *f, struct stat *sb);
|
2000-04-29 20:47:10 +00:00
|
|
|
static int cd9660_readdir(struct open_file *f, struct dirent *d);
|
2001-11-06 19:59:19 +00:00
|
|
|
static int dirmatch(struct open_file *f, const char *path,
|
|
|
|
struct iso_directory_record *dp, int use_rrip, int lenskip);
|
|
|
|
static int rrip_check(struct open_file *f, struct iso_directory_record *dp,
|
|
|
|
int *lenskip);
|
|
|
|
static char *rrip_lookup_name(struct open_file *f,
|
|
|
|
struct iso_directory_record *dp, int lenskip, size_t *len);
|
|
|
|
static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
|
|
|
|
const char *identifier, struct iso_directory_record *dp,
|
|
|
|
int lenskip);
|
1998-08-20 08:19:55 +00:00
|
|
|
|
|
|
|
struct fs_ops cd9660_fsops = {
|
2000-04-29 20:47:10 +00:00
|
|
|
"cd9660",
|
|
|
|
cd9660_open,
|
|
|
|
cd9660_close,
|
|
|
|
cd9660_read,
|
2018-02-27 12:53:25 +00:00
|
|
|
null_write,
|
2000-04-29 20:47:10 +00:00
|
|
|
cd9660_seek,
|
|
|
|
cd9660_stat,
|
|
|
|
cd9660_readdir
|
1998-08-20 08:19:55 +00:00
|
|
|
};
|
|
|
|
|
2001-11-06 22:31:10 +00:00
|
|
|
#define F_ISDIR 0x0001 /* Directory */
|
|
|
|
#define F_ROOTDIR 0x0002 /* Root directory */
|
|
|
|
#define F_RR 0x0004 /* Rock Ridge on this volume */
|
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
struct file {
|
2001-11-06 22:31:10 +00:00
|
|
|
int f_flags; /* file flags */
|
2000-04-29 20:47:10 +00:00
|
|
|
off_t f_off; /* Current offset within file */
|
|
|
|
daddr_t f_bno; /* Starting block number */
|
|
|
|
off_t f_size; /* Size of file */
|
|
|
|
daddr_t f_buf_blkno; /* block number of data block */
|
|
|
|
char *f_buf; /* buffer for data block */
|
2001-11-06 22:31:10 +00:00
|
|
|
int f_susp_skip; /* len_skip for SUSP records */
|
1998-08-20 08:19:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ptable_ent {
|
|
|
|
char namlen [ISODCL( 1, 1)]; /* 711 */
|
|
|
|
char extlen [ISODCL( 2, 2)]; /* 711 */
|
|
|
|
char block [ISODCL( 3, 6)]; /* 732 */
|
|
|
|
char parent [ISODCL( 7, 8)]; /* 722 */
|
|
|
|
char name [1];
|
|
|
|
};
|
|
|
|
#define PTFIXSZ 8
|
|
|
|
#define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
|
|
|
|
|
|
|
|
#define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
|
|
|
|
|
2001-11-06 19:59:19 +00:00
|
|
|
static ISO_SUSP_HEADER *
|
|
|
|
susp_lookup_record(struct open_file *f, const char *identifier,
|
|
|
|
struct iso_directory_record *dp, int lenskip)
|
|
|
|
{
|
|
|
|
static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
|
|
|
|
ISO_SUSP_HEADER *sh;
|
|
|
|
ISO_RRIP_CONT *shc;
|
|
|
|
char *p, *end;
|
|
|
|
int error;
|
|
|
|
size_t read;
|
|
|
|
|
|
|
|
p = dp->name + isonum_711(dp->name_len) + lenskip;
|
|
|
|
/* Names of even length have a padding byte after the name. */
|
|
|
|
if ((isonum_711(dp->name_len) & 1) == 0)
|
|
|
|
p++;
|
|
|
|
end = (char *)dp + isonum_711(dp->length);
|
|
|
|
while (p + 3 < end) {
|
|
|
|
sh = (ISO_SUSP_HEADER *)p;
|
|
|
|
if (bcmp(sh->type, identifier, 2) == 0)
|
|
|
|
return (sh);
|
|
|
|
if (bcmp(sh->type, SUSP_STOP, 2) == 0)
|
|
|
|
return (NULL);
|
|
|
|
if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
|
|
|
|
shc = (ISO_RRIP_CONT *)sh;
|
|
|
|
error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
|
2016-12-30 19:06:29 +00:00
|
|
|
cdb2devb(isonum_733(shc->location)),
|
2001-11-06 19:59:19 +00:00
|
|
|
ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
|
|
|
|
|
|
|
|
/* Bail if it fails. */
|
|
|
|
if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
|
|
|
|
return (NULL);
|
|
|
|
p = susp_buffer + isonum_733(shc->offset);
|
|
|
|
end = p + isonum_733(shc->length);
|
2014-12-30 16:55:53 +00:00
|
|
|
} else {
|
2001-11-06 19:59:19 +00:00
|
|
|
/* Ignore this record and skip to the next. */
|
|
|
|
p += isonum_711(sh->length);
|
2014-12-30 16:55:53 +00:00
|
|
|
|
|
|
|
/* Avoid infinite loops with corrupted file systems */
|
|
|
|
if (isonum_711(sh->length) == 0)
|
|
|
|
return (NULL);
|
|
|
|
}
|
2001-11-06 19:59:19 +00:00
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
|
|
|
|
int lenskip, size_t *len)
|
|
|
|
{
|
|
|
|
ISO_RRIP_ALTNAME *p;
|
|
|
|
|
|
|
|
if (len == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
|
|
|
|
if (p == NULL)
|
|
|
|
return (NULL);
|
|
|
|
switch (*p->flags) {
|
|
|
|
case ISO_SUSP_CFLAG_CURRENT:
|
|
|
|
*len = 1;
|
|
|
|
return (".");
|
|
|
|
case ISO_SUSP_CFLAG_PARENT:
|
|
|
|
*len = 2;
|
|
|
|
return ("..");
|
|
|
|
case 0:
|
|
|
|
*len = isonum_711(p->h.length) - 5;
|
|
|
|
return ((char *)p + 5);
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* We don't handle hostnames or continued names as they are
|
|
|
|
* too hard, so just bail and use the default name.
|
|
|
|
*/
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
static int
|
2001-11-06 19:59:19 +00:00
|
|
|
rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
|
1998-08-20 08:19:55 +00:00
|
|
|
{
|
2001-11-06 19:59:19 +00:00
|
|
|
ISO_SUSP_PRESENT *sp;
|
|
|
|
ISO_RRIP_EXTREF *er;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* First, see if we can find a SP field. */
|
|
|
|
p = dp->name + isonum_711(dp->name_len);
|
|
|
|
if (p > (char *)dp + isonum_711(dp->length))
|
|
|
|
return (0);
|
|
|
|
sp = (ISO_SUSP_PRESENT *)p;
|
|
|
|
if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
|
|
|
|
return (0);
|
|
|
|
if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
|
|
|
|
return (0);
|
|
|
|
if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
|
|
|
|
return (0);
|
|
|
|
*lenskip = isonum_711(sp->len_skp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now look for an ER field. If RRIP is present, then there must
|
|
|
|
* be at least one of these. It would be more pedantic to walk
|
|
|
|
* through the list of fields looking for a Rock Ridge ER field.
|
|
|
|
*/
|
|
|
|
er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
|
|
|
|
if (er == NULL)
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
|
|
|
|
int use_rrip, int lenskip)
|
|
|
|
{
|
2019-02-20 21:07:09 +00:00
|
|
|
size_t len, plen;
|
|
|
|
char *cp, *sep;
|
2001-11-06 19:59:19 +00:00
|
|
|
int i, icase;
|
1998-08-20 08:19:55 +00:00
|
|
|
|
2001-11-06 19:59:19 +00:00
|
|
|
if (use_rrip)
|
|
|
|
cp = rrip_lookup_name(f, dp, lenskip, &len);
|
|
|
|
else
|
|
|
|
cp = NULL;
|
|
|
|
if (cp == NULL) {
|
|
|
|
len = isonum_711(dp->name_len);
|
|
|
|
cp = dp->name;
|
|
|
|
icase = 1;
|
|
|
|
} else
|
|
|
|
icase = 0;
|
2019-02-18 08:26:18 +00:00
|
|
|
|
2019-02-20 21:07:09 +00:00
|
|
|
sep = strchr(path, '/');
|
|
|
|
if (sep != NULL) {
|
|
|
|
plen = sep - path;
|
|
|
|
} else {
|
|
|
|
plen = strlen(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plen != len)
|
2019-02-18 08:26:18 +00:00
|
|
|
return (0);
|
|
|
|
|
2001-11-06 19:59:19 +00:00
|
|
|
for (i = len; --i >= 0; path++, cp++) {
|
2000-01-18 07:37:10 +00:00
|
|
|
if (!*path || *path == '/')
|
1998-08-20 08:19:55 +00:00
|
|
|
break;
|
2001-11-06 19:59:19 +00:00
|
|
|
if (*path == *cp)
|
|
|
|
continue;
|
|
|
|
if (!icase && toupper(*path) == *cp)
|
1998-08-20 08:19:55 +00:00
|
|
|
continue;
|
|
|
|
return 0;
|
|
|
|
}
|
2000-01-18 07:37:10 +00:00
|
|
|
if (*path && *path != '/')
|
1998-08-20 08:19:55 +00:00
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Allow stripping of trailing dots and the version number.
|
|
|
|
* Note that this will find the first instead of the last version
|
|
|
|
* of a file.
|
|
|
|
*/
|
|
|
|
if (i >= 0 && (*cp == ';' || *cp == '.')) {
|
|
|
|
/* This is to prevent matching of numeric extensions */
|
|
|
|
if (*cp == '.' && cp[1] != ';')
|
|
|
|
return 0;
|
|
|
|
while (--i >= 0)
|
|
|
|
if (*++cp != ';' && (*cp < '0' || *cp > '9'))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-11-06 17:13:05 +00:00
|
|
|
cd9660_open(const char *path, struct open_file *f)
|
1998-08-20 08:19:55 +00:00
|
|
|
{
|
2016-04-18 14:45:56 +00:00
|
|
|
struct file *fp = NULL;
|
1998-08-20 08:19:55 +00:00
|
|
|
void *buf;
|
|
|
|
struct iso_primary_descriptor *vd;
|
2019-12-13 12:36:16 +00:00
|
|
|
size_t read, dsize, off;
|
2000-01-18 07:37:10 +00:00
|
|
|
daddr_t bno, boff;
|
|
|
|
struct iso_directory_record rec;
|
2016-04-18 14:45:56 +00:00
|
|
|
struct iso_directory_record *dp = NULL;
|
2001-11-06 19:59:19 +00:00
|
|
|
int rc, first, use_rrip, lenskip;
|
2019-02-20 21:07:09 +00:00
|
|
|
bool isdir = false;
|
2000-01-18 07:43:12 +00:00
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
/* First find the volume descriptor */
|
2019-12-13 12:36:16 +00:00
|
|
|
buf = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
|
|
|
|
sizeof(struct iso_primary_descriptor)));
|
1998-08-20 08:19:55 +00:00
|
|
|
vd = buf;
|
|
|
|
for (bno = 16;; bno++) {
|
2014-12-22 20:42:36 +00:00
|
|
|
twiddle(1);
|
1998-08-20 08:19:55 +00:00
|
|
|
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
|
2016-12-30 19:06:29 +00:00
|
|
|
ISO_DEFAULT_BLOCK_SIZE, buf, &read);
|
1998-08-20 08:19:55 +00:00
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
if (read != ISO_DEFAULT_BLOCK_SIZE) {
|
|
|
|
rc = EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rc = EINVAL;
|
|
|
|
if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
|
|
|
|
goto out;
|
|
|
|
if (isonum_711(vd->type) == ISO_VD_END)
|
|
|
|
goto out;
|
|
|
|
if (isonum_711(vd->type) == ISO_VD_PRIMARY)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
|
|
|
|
goto out;
|
2000-01-18 07:43:12 +00:00
|
|
|
|
2018-08-02 06:22:10 +00:00
|
|
|
bcopy(vd->root_directory_record, &rec, sizeof(rec));
|
2000-01-18 07:37:10 +00:00
|
|
|
if (*path == '/') path++; /* eat leading '/' */
|
1998-08-20 08:19:55 +00:00
|
|
|
|
2001-11-06 19:59:19 +00:00
|
|
|
first = 1;
|
|
|
|
use_rrip = 0;
|
2018-04-07 14:40:09 +00:00
|
|
|
lenskip = 0;
|
1998-08-20 08:19:55 +00:00
|
|
|
while (*path) {
|
2000-01-18 07:37:10 +00:00
|
|
|
bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
|
|
|
|
dsize = isonum_733(rec.size);
|
|
|
|
off = 0;
|
|
|
|
boff = 0;
|
1998-08-20 08:19:55 +00:00
|
|
|
|
2000-01-18 07:37:10 +00:00
|
|
|
while (off < dsize) {
|
|
|
|
if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
|
2014-12-22 20:42:36 +00:00
|
|
|
twiddle(1);
|
2000-01-18 07:37:10 +00:00
|
|
|
rc = f->f_dev->dv_strategy
|
|
|
|
(f->f_devdata, F_READ,
|
2016-12-30 19:06:29 +00:00
|
|
|
cdb2devb(bno + boff),
|
2000-01-18 07:37:10 +00:00
|
|
|
ISO_DEFAULT_BLOCK_SIZE,
|
|
|
|
buf, &read);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
if (read != ISO_DEFAULT_BLOCK_SIZE) {
|
|
|
|
rc = EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
boff++;
|
|
|
|
dp = (struct iso_directory_record *) buf;
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
2000-01-18 07:37:10 +00:00
|
|
|
if (isonum_711(dp->length) == 0) {
|
|
|
|
/* skip to next block, if any */
|
|
|
|
off = boff * ISO_DEFAULT_BLOCK_SIZE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-11-06 19:59:19 +00:00
|
|
|
/* See if RRIP is in use. */
|
|
|
|
if (first)
|
|
|
|
use_rrip = rrip_check(f, dp, &lenskip);
|
|
|
|
|
|
|
|
if (dirmatch(f, path, dp, use_rrip,
|
2018-04-07 14:40:09 +00:00
|
|
|
first ? 0 : lenskip)) {
|
2001-11-06 19:59:19 +00:00
|
|
|
first = 0;
|
2000-01-18 07:37:10 +00:00
|
|
|
break;
|
2001-11-06 19:59:19 +00:00
|
|
|
} else
|
|
|
|
first = 0;
|
2000-01-18 07:37:10 +00:00
|
|
|
|
|
|
|
dp = (struct iso_directory_record *)
|
|
|
|
((char *) dp + isonum_711(dp->length));
|
2016-10-03 14:07:15 +00:00
|
|
|
/* If the new block has zero length, it is padding. */
|
2016-09-30 22:28:20 +00:00
|
|
|
if (isonum_711(dp->length) == 0) {
|
2016-10-03 14:07:15 +00:00
|
|
|
/* Skip to next block, if any. */
|
2016-09-30 23:19:08 +00:00
|
|
|
off = boff * ISO_DEFAULT_BLOCK_SIZE;
|
|
|
|
continue;
|
2016-09-30 22:28:20 +00:00
|
|
|
}
|
2016-10-01 07:46:28 +00:00
|
|
|
off += isonum_711(dp->length);
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
2001-08-23 17:08:26 +00:00
|
|
|
if (off >= dsize) {
|
2000-01-18 07:37:10 +00:00
|
|
|
rc = ENOENT;
|
|
|
|
goto out;
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
|
|
|
|
2000-01-18 07:37:10 +00:00
|
|
|
rec = *dp;
|
|
|
|
while (*path && *path != '/') /* look for next component */
|
|
|
|
path++;
|
2019-02-20 21:07:09 +00:00
|
|
|
|
|
|
|
if (*path) /* this component was directory */
|
|
|
|
isdir = true;
|
|
|
|
|
|
|
|
while (*path == '/')
|
|
|
|
path++; /* skip '/' */
|
|
|
|
|
|
|
|
if (*path) /* We do have next component. */
|
|
|
|
isdir = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the path had trailing / but the path does point to file,
|
|
|
|
* report the error ENOTDIR.
|
|
|
|
*/
|
|
|
|
if (isdir == true && (isonum_711(rec.flags) & 2) == 0) {
|
|
|
|
rc = ENOTDIR;
|
|
|
|
goto out;
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
2000-01-18 07:43:12 +00:00
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
/* allocate file system specific data structure */
|
|
|
|
fp = malloc(sizeof(struct file));
|
|
|
|
bzero(fp, sizeof(struct file));
|
|
|
|
f->f_fsdata = (void *)fp;
|
|
|
|
|
2001-11-06 22:31:10 +00:00
|
|
|
if ((isonum_711(rec.flags) & 2) != 0) {
|
|
|
|
fp->f_flags = F_ISDIR;
|
|
|
|
}
|
|
|
|
if (first) {
|
|
|
|
fp->f_flags |= F_ROOTDIR;
|
|
|
|
|
|
|
|
/* Check for Rock Ridge since we didn't in the loop above. */
|
|
|
|
bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
|
2014-12-22 20:42:36 +00:00
|
|
|
twiddle(1);
|
2001-11-06 22:31:10 +00:00
|
|
|
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
|
2016-12-30 19:06:29 +00:00
|
|
|
ISO_DEFAULT_BLOCK_SIZE, buf, &read);
|
2001-11-06 22:31:10 +00:00
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
if (read != ISO_DEFAULT_BLOCK_SIZE) {
|
|
|
|
rc = EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
dp = (struct iso_directory_record *)buf;
|
|
|
|
use_rrip = rrip_check(f, dp, &lenskip);
|
|
|
|
}
|
|
|
|
if (use_rrip) {
|
|
|
|
fp->f_flags |= F_RR;
|
|
|
|
fp->f_susp_skip = lenskip;
|
|
|
|
}
|
2000-04-29 20:47:10 +00:00
|
|
|
fp->f_off = 0;
|
|
|
|
fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
|
|
|
|
fp->f_size = isonum_733(rec.size);
|
1998-08-20 08:19:55 +00:00
|
|
|
free(buf);
|
2000-01-18 07:43:12 +00:00
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
return 0;
|
2000-01-18 07:43:12 +00:00
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
out:
|
2019-12-13 12:36:16 +00:00
|
|
|
free(fp);
|
1998-08-20 08:19:55 +00:00
|
|
|
free(buf);
|
2000-01-18 07:43:12 +00:00
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-11-06 17:13:05 +00:00
|
|
|
cd9660_close(struct open_file *f)
|
1998-08-20 08:19:55 +00:00
|
|
|
{
|
|
|
|
struct file *fp = (struct file *)f->f_fsdata;
|
2000-01-18 07:43:12 +00:00
|
|
|
|
2016-04-18 14:45:56 +00:00
|
|
|
f->f_fsdata = NULL;
|
1998-08-20 08:19:55 +00:00
|
|
|
free(fp);
|
2000-01-18 07:43:12 +00:00
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-11-06 17:13:05 +00:00
|
|
|
buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
|
1998-08-20 08:19:55 +00:00
|
|
|
{
|
|
|
|
struct file *fp = (struct file *)f->f_fsdata;
|
2001-04-07 23:48:46 +00:00
|
|
|
daddr_t blkno, blkoff;
|
1998-08-20 08:19:55 +00:00
|
|
|
int rc = 0;
|
2000-04-29 20:47:10 +00:00
|
|
|
size_t read;
|
|
|
|
|
|
|
|
blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
|
2001-04-07 23:48:46 +00:00
|
|
|
blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
|
2000-04-29 20:47:10 +00:00
|
|
|
|
|
|
|
if (blkno != fp->f_buf_blkno) {
|
|
|
|
if (fp->f_buf == (char *)0)
|
|
|
|
fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
|
2000-01-18 07:43:12 +00:00
|
|
|
|
2014-12-22 20:42:36 +00:00
|
|
|
twiddle(16);
|
2000-04-29 20:47:10 +00:00
|
|
|
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
|
2016-12-30 19:06:29 +00:00
|
|
|
cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE,
|
A new implementation of the loader block cache
The block cache implementation in loader has proven to be almost useless, and in worst case even slowing down the disk reads due to insufficient cache size and extra memory copy.
Also the current cache implementation does not cache reads from CDs, or work with zfs built on top of multiple disks.
Instead of an LRU, this code uses a simple hash (O(1) read from cache), and instead of a single global cache, a separate cache per block device.
The cache also implements limited read-ahead to increase performance.
To simplify read ahead management, the read ahead will not wrap over bcache end, so in worst case, single block physical read will be performed to fill the last block in bcache.
Booting from a virtual CD over IPMI:
0ms latency, before: 27 second, after: 7 seconds
60ms latency, before: over 12 minutes, after: under 5 minutes.
Submitted by: Toomas Soome <tsoome@me.com>
Reviewed by: delphij (previous version), emaste (previous version)
Relnotes: yes
Differential Revision: https://reviews.freebsd.org/D4713
2016-04-18 23:09:22 +00:00
|
|
|
fp->f_buf, &read);
|
1998-08-20 08:19:55 +00:00
|
|
|
if (rc)
|
2000-04-29 20:47:10 +00:00
|
|
|
return (rc);
|
1998-08-20 08:19:55 +00:00
|
|
|
if (read != ISO_DEFAULT_BLOCK_SIZE)
|
2000-04-29 20:47:10 +00:00
|
|
|
return (EIO);
|
|
|
|
|
|
|
|
fp->f_buf_blkno = blkno;
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
2000-04-29 20:47:10 +00:00
|
|
|
|
2001-04-07 23:48:46 +00:00
|
|
|
*buf_p = fp->f_buf + blkoff;
|
|
|
|
*size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
|
2000-04-29 20:47:10 +00:00
|
|
|
|
|
|
|
if (*size_p > fp->f_size - fp->f_off)
|
|
|
|
*size_p = fp->f_size - fp->f_off;
|
|
|
|
return (rc);
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
|
|
|
|
2000-01-18 07:37:10 +00:00
|
|
|
static int
|
2001-11-06 17:13:05 +00:00
|
|
|
cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
|
2000-01-18 07:37:10 +00:00
|
|
|
{
|
|
|
|
struct file *fp = (struct file *)f->f_fsdata;
|
2000-04-29 20:47:10 +00:00
|
|
|
char *buf, *addr;
|
|
|
|
size_t buf_size, csize;
|
2000-01-18 07:37:10 +00:00
|
|
|
int rc = 0;
|
2000-01-18 07:43:12 +00:00
|
|
|
|
2000-04-29 20:47:10 +00:00
|
|
|
addr = start;
|
|
|
|
while (size) {
|
|
|
|
if (fp->f_off < 0 || fp->f_off >= fp->f_size)
|
|
|
|
break;
|
2000-01-18 07:37:10 +00:00
|
|
|
|
2000-04-29 20:47:10 +00:00
|
|
|
rc = buf_read_file(f, &buf, &buf_size);
|
2000-01-18 07:37:10 +00:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
|
2000-04-29 20:47:10 +00:00
|
|
|
csize = size > buf_size ? buf_size : size;
|
|
|
|
bcopy(buf, addr, csize);
|
2000-01-18 07:37:10 +00:00
|
|
|
|
2000-04-29 20:47:10 +00:00
|
|
|
fp->f_off += csize;
|
|
|
|
addr += csize;
|
|
|
|
size -= csize;
|
|
|
|
}
|
2000-01-18 07:37:10 +00:00
|
|
|
if (resid)
|
2000-04-29 20:47:10 +00:00
|
|
|
*resid = size;
|
|
|
|
return (rc);
|
2000-01-18 07:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-04-29 20:47:10 +00:00
|
|
|
cd9660_readdir(struct open_file *f, struct dirent *d)
|
2000-01-18 07:37:10 +00:00
|
|
|
{
|
|
|
|
struct file *fp = (struct file *)f->f_fsdata;
|
2000-04-29 20:47:10 +00:00
|
|
|
struct iso_directory_record *ep;
|
|
|
|
size_t buf_size, reclen, namelen;
|
|
|
|
int error = 0;
|
2001-11-06 22:31:10 +00:00
|
|
|
int lenskip;
|
|
|
|
char *buf, *name;
|
2000-04-29 20:47:10 +00:00
|
|
|
|
|
|
|
again:
|
|
|
|
if (fp->f_off >= fp->f_size)
|
|
|
|
return (ENOENT);
|
|
|
|
error = buf_read_file(f, &buf, &buf_size);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
ep = (struct iso_directory_record *)buf;
|
|
|
|
|
|
|
|
if (isonum_711(ep->length) == 0) {
|
|
|
|
daddr_t blkno;
|
|
|
|
|
|
|
|
/* skip to next block, if any */
|
|
|
|
blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
|
|
|
|
fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2001-11-06 22:31:10 +00:00
|
|
|
if (fp->f_flags & F_RR) {
|
|
|
|
if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
|
|
|
|
lenskip = 0;
|
|
|
|
else
|
|
|
|
lenskip = fp->f_susp_skip;
|
|
|
|
name = rrip_lookup_name(f, ep, lenskip, &namelen);
|
|
|
|
} else
|
|
|
|
name = NULL;
|
|
|
|
if (name == NULL) {
|
|
|
|
namelen = isonum_711(ep->name_len);
|
|
|
|
name = ep->name;
|
|
|
|
if (namelen == 1) {
|
|
|
|
if (ep->name[0] == 0)
|
|
|
|
name = ".";
|
|
|
|
else if (ep->name[0] == 1) {
|
|
|
|
namelen = 2;
|
|
|
|
name = "..";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-04-29 20:47:10 +00:00
|
|
|
reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
|
|
|
|
reclen = (reclen + 3) & ~3;
|
2000-01-18 07:37:10 +00:00
|
|
|
|
2000-04-29 20:47:10 +00:00
|
|
|
d->d_fileno = isonum_733(ep->extent);
|
|
|
|
d->d_reclen = reclen;
|
|
|
|
if (isonum_711(ep->flags) & 2)
|
|
|
|
d->d_type = DT_DIR;
|
2000-01-18 07:37:10 +00:00
|
|
|
else
|
2000-04-29 20:47:10 +00:00
|
|
|
d->d_type = DT_REG;
|
|
|
|
d->d_namlen = namelen;
|
|
|
|
|
2001-11-06 22:31:10 +00:00
|
|
|
bcopy(name, d->d_name, d->d_namlen);
|
2000-04-29 20:47:10 +00:00
|
|
|
d->d_name[d->d_namlen] = 0;
|
|
|
|
|
|
|
|
fp->f_off += isonum_711(ep->length);
|
|
|
|
return (0);
|
2000-01-18 07:37:10 +00:00
|
|
|
}
|
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
static off_t
|
2001-11-06 17:13:05 +00:00
|
|
|
cd9660_seek(struct open_file *f, off_t offset, int where)
|
1998-08-20 08:19:55 +00:00
|
|
|
{
|
|
|
|
struct file *fp = (struct file *)f->f_fsdata;
|
2000-01-18 07:43:12 +00:00
|
|
|
|
1998-08-20 08:19:55 +00:00
|
|
|
switch (where) {
|
|
|
|
case SEEK_SET:
|
2000-04-29 20:47:10 +00:00
|
|
|
fp->f_off = offset;
|
1998-08-20 08:19:55 +00:00
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
2000-04-29 20:47:10 +00:00
|
|
|
fp->f_off += offset;
|
1998-08-20 08:19:55 +00:00
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2000-04-29 20:47:10 +00:00
|
|
|
fp->f_off = fp->f_size - offset;
|
1998-08-20 08:19:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2000-04-29 20:47:10 +00:00
|
|
|
return fp->f_off;
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-11-06 17:13:05 +00:00
|
|
|
cd9660_stat(struct open_file *f, struct stat *sb)
|
1998-08-20 08:19:55 +00:00
|
|
|
{
|
|
|
|
struct file *fp = (struct file *)f->f_fsdata;
|
2000-01-18 07:43:12 +00:00
|
|
|
|
2000-01-18 07:37:10 +00:00
|
|
|
/* only important stuff */
|
|
|
|
sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
|
2001-11-06 22:31:10 +00:00
|
|
|
if (fp->f_flags & F_ISDIR)
|
2000-01-18 07:37:10 +00:00
|
|
|
sb->st_mode |= S_IFDIR;
|
|
|
|
else
|
|
|
|
sb->st_mode |= S_IFREG;
|
1998-08-20 08:19:55 +00:00
|
|
|
sb->st_uid = sb->st_gid = 0;
|
2000-04-29 20:47:10 +00:00
|
|
|
sb->st_size = fp->f_size;
|
1998-08-20 08:19:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|