17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate *  GRUB  --  GRand Unified Bootloader
37c478bdstevel@tonic-gate *  Copyright (C) 1999,2000,2001,2002  Free Software Foundation, Inc.
47c478bdstevel@tonic-gate *
57c478bdstevel@tonic-gate *  This program is free software; you can redistribute it and/or modify
67c478bdstevel@tonic-gate *  it under the terms of the GNU General Public License as published by
77c478bdstevel@tonic-gate *  the Free Software Foundation; either version 2 of the License, or
87c478bdstevel@tonic-gate *  (at your option) any later version.
97c478bdstevel@tonic-gate *
107c478bdstevel@tonic-gate *  This program is distributed in the hope that it will be useful,
117c478bdstevel@tonic-gate *  but WITHOUT ANY WARRANTY; without even the implied warranty of
127c478bdstevel@tonic-gate *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137c478bdstevel@tonic-gate *  GNU General Public License for more details.
147c478bdstevel@tonic-gate *
157c478bdstevel@tonic-gate *  You should have received a copy of the GNU General Public License
167c478bdstevel@tonic-gate *  along with this program; if not, write to the Free Software
177c478bdstevel@tonic-gate *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
187c478bdstevel@tonic-gate */
197c478bdstevel@tonic-gate
207c478bdstevel@tonic-gate/* Restrictions:
217c478bdstevel@tonic-gate   This is MINIX V1 only (yet)
227c478bdstevel@tonic-gate   Disk creation is like:
237c478bdstevel@tonic-gate   mkfs.minix -c DEVICE
247c478bdstevel@tonic-gate*/
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#ifdef FSYS_MINIX
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include "shared.h"
297c478bdstevel@tonic-gate#include "filesys.h"
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate/* #define DEBUG_MINIX */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate/* indirect blocks */
347c478bdstevel@tonic-gatestatic int mapblock1, mapblock2, namelen;
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate/* sizes are always in bytes, BLOCK values are always in DEV_BSIZE (sectors) */
377c478bdstevel@tonic-gate#define DEV_BSIZE 512
387c478bdstevel@tonic-gate
397c478bdstevel@tonic-gate/* include/linux/fs.h */
407c478bdstevel@tonic-gate#define BLOCK_SIZE_BITS 10
417c478bdstevel@tonic-gate#define BLOCK_SIZE 	(1<<BLOCK_SIZE_BITS)
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate/* made up, defaults to 1 but can be passed via mount_opts */
447c478bdstevel@tonic-gate#define WHICH_SUPER 1
457c478bdstevel@tonic-gate/* kind of from fs/ext2/super.c (is OK for minix) */
467c478bdstevel@tonic-gate#define SBLOCK (WHICH_SUPER * BLOCK_SIZE / DEV_BSIZE)	/* = 2 */
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate/* include/asm-i386/type.h */
497c478bdstevel@tonic-gatetypedef __signed__ char __s8;
507c478bdstevel@tonic-gatetypedef unsigned char __u8;
517c478bdstevel@tonic-gatetypedef __signed__ short __s16;
527c478bdstevel@tonic-gatetypedef unsigned short __u16;
537c478bdstevel@tonic-gatetypedef __signed__ int __s32;
547c478bdstevel@tonic-gatetypedef unsigned int __u32;
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate/* include/linux/minix_fs.h */
577c478bdstevel@tonic-gate#define MINIX_ROOT_INO 1
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate/* Not the same as the bogus LINK_MAX in <linux/limits.h>. Oh well. */
607c478bdstevel@tonic-gate#define MINIX_LINK_MAX  250
617c478bdstevel@tonic-gate#define MINIX2_LINK_MAX 65530
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate#define MINIX_I_MAP_SLOTS       8
647c478bdstevel@tonic-gate#define MINIX_Z_MAP_SLOTS       64
657c478bdstevel@tonic-gate#define MINIX_SUPER_MAGIC       0x137F          /* original minix fs */
667c478bdstevel@tonic-gate#define MINIX_SUPER_MAGIC2      0x138F          /* minix fs, 30 char names */
677c478bdstevel@tonic-gate#define MINIX2_SUPER_MAGIC      0x2468          /* minix V2 fs */
687c478bdstevel@tonic-gate#define MINIX2_SUPER_MAGIC2     0x2478          /* minix V2 fs, 30 char names */
697c478bdstevel@tonic-gate#define MINIX_VALID_FS          0x0001          /* Clean fs. */
707c478bdstevel@tonic-gate#define MINIX_ERROR_FS          0x0002          /* fs has errors. */
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
737c478bdstevel@tonic-gate#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate#define MINIX_V1                0x0001          /* original minix fs */
767c478bdstevel@tonic-gate#define MINIX_V2                0x0002          /* minix V2 fs */
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate/* originally this is :
797c478bdstevel@tonic-gate#define INODE_VERSION(inode)    inode->i_sb->u.minix_sb.s_version
807c478bdstevel@tonic-gate   here we have */
817c478bdstevel@tonic-gate#define INODE_VERSION(inode)	(SUPERBLOCK->s_version)
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gate/*
847c478bdstevel@tonic-gate * This is the original minix inode layout on disk.
857c478bdstevel@tonic-gate * Note the 8-bit gid and atime and ctime.
867c478bdstevel@tonic-gate */
877c478bdstevel@tonic-gatestruct minix_inode {
887c478bdstevel@tonic-gate	__u16 i_mode;
897c478bdstevel@tonic-gate	__u16 i_uid;
907c478bdstevel@tonic-gate	__u32 i_size;
917c478bdstevel@tonic-gate	__u32 i_time;
927c478bdstevel@tonic-gate	__u8  i_gid;
937c478bdstevel@tonic-gate	__u8  i_nlinks;
947c478bdstevel@tonic-gate	__u16 i_zone[9];
957c478bdstevel@tonic-gate};
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate/*
987c478bdstevel@tonic-gate * The new minix inode has all the time entries, as well as
997c478bdstevel@tonic-gate * long block numbers and a third indirect block (7+1+1+1
1007c478bdstevel@tonic-gate * instead of 7+1+1). Also, some previously 8-bit values are
1017c478bdstevel@tonic-gate * now 16-bit. The inode is now 64 bytes instead of 32.
1027c478bdstevel@tonic-gate */
1037c478bdstevel@tonic-gatestruct minix2_inode {
1047c478bdstevel@tonic-gate	__u16 i_mode;
1057c478bdstevel@tonic-gate	__u16 i_nlinks;
1067c478bdstevel@tonic-gate	__u16 i_uid;
1077c478bdstevel@tonic-gate	__u16 i_gid;
1087c478bdstevel@tonic-gate	__u32 i_size;
1097c478bdstevel@tonic-gate	__u32 i_atime;
1107c478bdstevel@tonic-gate	__u32 i_mtime;
1117c478bdstevel@tonic-gate	__u32 i_ctime;
1127c478bdstevel@tonic-gate	__u32 i_zone[10];
1137c478bdstevel@tonic-gate};
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate/*
1167c478bdstevel@tonic-gate * minix super-block data on disk
1177c478bdstevel@tonic-gate */
1187c478bdstevel@tonic-gatestruct minix_super_block {
1197c478bdstevel@tonic-gate        __u16 s_ninodes;
1207c478bdstevel@tonic-gate        __u16 s_nzones;
1217c478bdstevel@tonic-gate        __u16 s_imap_blocks;
1227c478bdstevel@tonic-gate        __u16 s_zmap_blocks;
1237c478bdstevel@tonic-gate        __u16 s_firstdatazone;
1247c478bdstevel@tonic-gate        __u16 s_log_zone_size;
1257c478bdstevel@tonic-gate        __u32 s_max_size;
1267c478bdstevel@tonic-gate        __u16 s_magic;
1277c478bdstevel@tonic-gate        __u16 s_state;
1287c478bdstevel@tonic-gate        __u32 s_zones;
1297c478bdstevel@tonic-gate};
1307c478bdstevel@tonic-gate
1317c478bdstevel@tonic-gatestruct minix_dir_entry {
1327c478bdstevel@tonic-gate        __u16 inode;
1337c478bdstevel@tonic-gate        char name[0];
1347c478bdstevel@tonic-gate};
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate/* made up, these are pointers into FSYS_BUF */
1377c478bdstevel@tonic-gate/* read once, always stays there: */
1387c478bdstevel@tonic-gate#define SUPERBLOCK \
1397c478bdstevel@tonic-gate    ((struct minix_super_block *)(FSYS_BUF))
1407c478bdstevel@tonic-gate#define INODE \
1417c478bdstevel@tonic-gate    ((struct minix_inode *)((int) SUPERBLOCK + BLOCK_SIZE))
1427c478bdstevel@tonic-gate#define DATABLOCK1 \
1437c478bdstevel@tonic-gate    ((int)((int)INODE + sizeof(struct minix_inode)))
1447c478bdstevel@tonic-gate#define DATABLOCK2 \
1457c478bdstevel@tonic-gate    ((int)((int)DATABLOCK1 + BLOCK_SIZE))
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate/* linux/stat.h */
1487c478bdstevel@tonic-gate#define S_IFMT  00170000
1497c478bdstevel@tonic-gate#define S_IFLNK  0120000
1507c478bdstevel@tonic-gate#define S_IFREG  0100000
1517c478bdstevel@tonic-gate#define S_IFDIR  0040000
1527c478bdstevel@tonic-gate#define S_ISLNK(m)	(((m) & S_IFMT) == S_IFLNK)
1537c478bdstevel@tonic-gate#define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)
1547c478bdstevel@tonic-gate#define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate#define PATH_MAX                1024	/* include/linux/limits.h */
1577c478bdstevel@tonic-gate#define MAX_LINK_COUNT             5	/* number of symbolic links to follow */
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gate/* check filesystem types and read superblock into memory buffer */
1607c478bdstevel@tonic-gateint
1617c478bdstevel@tonic-gateminix_mount (void)
1627c478bdstevel@tonic-gate{
1637c478bdstevel@tonic-gate  if (((current_drive & 0x80) || current_slice != 0)
1647c478bdstevel@tonic-gate      && ! IS_PC_SLICE_TYPE_MINIX (current_slice)
1657c478bdstevel@tonic-gate      && ! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_OTHER))
1667c478bdstevel@tonic-gate    return 0;			/* The partition is not of MINIX type */
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate  if (part_length < (SBLOCK +
1697c478bdstevel@tonic-gate		     (sizeof (struct minix_super_block) / DEV_BSIZE)))
1707c478bdstevel@tonic-gate    return 0;			/* The partition is too short */
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate  if (!devread (SBLOCK, 0, sizeof (struct minix_super_block),
1737c478bdstevel@tonic-gate		(char *) SUPERBLOCK))
1747c478bdstevel@tonic-gate    return 0;			/* Cannot read superblock */
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate  switch (SUPERBLOCK->s_magic)
1777c478bdstevel@tonic-gate    {
1787c478bdstevel@tonic-gate    case MINIX_SUPER_MAGIC:
1797c478bdstevel@tonic-gate      namelen = 14;
1807c478bdstevel@tonic-gate      break;
1817c478bdstevel@tonic-gate    case MINIX_SUPER_MAGIC2:
1827c478bdstevel@tonic-gate      namelen = 30;
1837c478bdstevel@tonic-gate      break;
1847c478bdstevel@tonic-gate    default:
1857c478bdstevel@tonic-gate      return 0;			/* Unsupported type */
1867c478bdstevel@tonic-gate    }
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gate  return 1;
1897c478bdstevel@tonic-gate}
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate/* Takes a file system block number and reads it into BUFFER. */
1927c478bdstevel@tonic-gatestatic int
1937c478bdstevel@tonic-gateminix_rdfsb (int fsblock, int buffer)
1947c478bdstevel@tonic-gate{
1957c478bdstevel@tonic-gate  return devread (fsblock * (BLOCK_SIZE / DEV_BSIZE), 0,
1967c478bdstevel@tonic-gate		  BLOCK_SIZE, (char *) buffer);
1977c478bdstevel@tonic-gate}
1987c478bdstevel@tonic-gate
1997c478bdstevel@tonic-gate/* Maps LOGICAL_BLOCK (the file offset divided by the blocksize) into
2007c478bdstevel@tonic-gate   a physical block (the location in the file system) via an inode. */
2017c478bdstevel@tonic-gatestatic int
2027c478bdstevel@tonic-gateminix_block_map (int logical_block)
2037c478bdstevel@tonic-gate{
2047c478bdstevel@tonic-gate  int i;
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate  if (logical_block < 7)
2077c478bdstevel@tonic-gate    return INODE->i_zone[logical_block];
2087c478bdstevel@tonic-gate
2097c478bdstevel@tonic-gate  logical_block -= 7;
2107c478bdstevel@tonic-gate  if (logical_block < 512)
2117c478bdstevel@tonic-gate    {
2127c478bdstevel@tonic-gate      i = INODE->i_zone[7];
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gate      if (!i || ((mapblock1 != 1)
2157c478bdstevel@tonic-gate		 && !minix_rdfsb (i, DATABLOCK1)))
2167c478bdstevel@tonic-gate	{
2177c478bdstevel@tonic-gate	  errnum = ERR_FSYS_CORRUPT;
2187c478bdstevel@tonic-gate	  return -1;
2197c478bdstevel@tonic-gate	}
2207c478bdstevel@tonic-gate      mapblock1 = 1;
2217c478bdstevel@tonic-gate      return ((__u16 *) DATABLOCK1) [logical_block];
2227c478bdstevel@tonic-gate    }
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate  logical_block -= 512;
2257c478bdstevel@tonic-gate  i = INODE->i_zone[8];
2267c478bdstevel@tonic-gate  if (!i || ((mapblock1 != 2)
2277c478bdstevel@tonic-gate	     && !minix_rdfsb (i, DATABLOCK1)))
2287c478bdstevel@tonic-gate    {
2297c478bdstevel@tonic-gate      errnum = ERR_FSYS_CORRUPT;
2307c478bdstevel@tonic-gate      return -1;
2317c478bdstevel@tonic-gate    }
2327c478bdstevel@tonic-gate  mapblock1 = 2;
2337c478bdstevel@tonic-gate  i = ((__u16 *) DATABLOCK1)[logical_block >> 9];
2347c478bdstevel@tonic-gate  if (!i || ((mapblock2 != i)
2357c478bdstevel@tonic-gate	     && !minix_rdfsb (i, DATABLOCK2)))
2367c478bdstevel@tonic-gate    {
2377c478bdstevel@tonic-gate      errnum = ERR_FSYS_CORRUPT;
2387c478bdstevel@tonic-gate      return -1;
2397c478bdstevel@tonic-gate    }
2407c478bdstevel@tonic-gate  mapblock2 = i;
2417c478bdstevel@tonic-gate  return ((__u16 *) DATABLOCK2)[logical_block & 511];
2427c478bdstevel@tonic-gate}
2437c478bdstevel@tonic-gate
2447c478bdstevel@tonic-gate/* read from INODE into BUF */
2457c478bdstevel@tonic-gateint
2467c478bdstevel@tonic-gateminix_read (char *buf, int len)
2477c478bdstevel@tonic-gate{
2487c478bdstevel@tonic-gate  int logical_block;
2497c478bdstevel@tonic-gate  int offset;
2507c478bdstevel@tonic-gate  int map;
2517c478bdstevel@tonic-gate  int ret = 0;
2527c478bdstevel@tonic-gate  int size = 0;
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate  while (len > 0)
2557c478bdstevel@tonic-gate    {
2567c478bdstevel@tonic-gate      /* find the (logical) block component of our location */
2577c478bdstevel@tonic-gate      logical_block = filepos >> BLOCK_SIZE_BITS;
2587c478bdstevel@tonic-gate      offset = filepos & (BLOCK_SIZE - 1);
2597c478bdstevel@tonic-gate      map = minix_block_map (logical_block);
2607c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
2617c478bdstevel@tonic-gate      printf ("map=%d\n", map);
2627c478bdstevel@tonic-gate#endif
2637c478bdstevel@tonic-gate      if (map < 0)
2647c478bdstevel@tonic-gate	break;
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate      size = BLOCK_SIZE;
2677c478bdstevel@tonic-gate      size -= offset;
2687c478bdstevel@tonic-gate      if (size > len)
2697c478bdstevel@tonic-gate	size = len;
2707c478bdstevel@tonic-gate
2717c478bdstevel@tonic-gate      disk_read_func = disk_read_hook;
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gate      devread (map * (BLOCK_SIZE / DEV_BSIZE),
2747c478bdstevel@tonic-gate	       offset, size, buf);
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate      disk_read_func = NULL;
2777c478bdstevel@tonic-gate
2787c478bdstevel@tonic-gate      buf += size;
2797c478bdstevel@tonic-gate      len -= size;
2807c478bdstevel@tonic-gate      filepos += size;
2817c478bdstevel@tonic-gate      ret += size;
2827c478bdstevel@tonic-gate    }
2837c478bdstevel@tonic-gate
2847c478bdstevel@tonic-gate  if (errnum)
2857c478bdstevel@tonic-gate    ret = 0;
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate  return ret;
2887c478bdstevel@tonic-gate}
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate/* preconditions: minix_mount already executed, therefore supblk in buffer
2917c478bdstevel@tonic-gate     known as SUPERBLOCK
2927c478bdstevel@tonic-gate   returns: 0 if error, nonzero iff we were able to find the file successfully
2937c478bdstevel@tonic-gate   postconditions: on a nonzero return, buffer known as INODE contains the
2947c478bdstevel@tonic-gate     inode of the file we were trying to look up
2957c478bdstevel@tonic-gate   side effects: none yet  */
2967c478bdstevel@tonic-gateint
2977c478bdstevel@tonic-gateminix_dir (char *dirname)
2987c478bdstevel@tonic-gate{
2997c478bdstevel@tonic-gate  int current_ino = MINIX_ROOT_INO;  /* start at the root */
3007c478bdstevel@tonic-gate  int updir_ino = current_ino;	     /* the parent of the current directory */
3017c478bdstevel@tonic-gate  int ino_blk;			     /* fs pointer of the inode's info */
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate  int str_chk = 0;		     /* used ot hold the results of a string
3047c478bdstevel@tonic-gate				        compare */
3057c478bdstevel@tonic-gate
3067c478bdstevel@tonic-gate  struct minix_inode * raw_inode;    /* inode info for current_ino */
3077c478bdstevel@tonic-gate
3087c478bdstevel@tonic-gate  char linkbuf[PATH_MAX];	     /* buffer for following sym-links */
3097c478bdstevel@tonic-gate  int link_count = 0;
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate  char * rest;
3127c478bdstevel@tonic-gate  char ch;
3137c478bdstevel@tonic-gate
3147c478bdstevel@tonic-gate  int off;			     /* offset within block of directory
3157c478bdstevel@tonic-gate					entry */
3167c478bdstevel@tonic-gate  int loc;			     /* location within a directory */
3177c478bdstevel@tonic-gate  int blk;			     /* which data blk within dir entry */
3187c478bdstevel@tonic-gate  long map;			     /* fs pointer of a particular block from
3197c478bdstevel@tonic-gate					dir entry */
3207c478bdstevel@tonic-gate  struct minix_dir_entry * dp;	     /* pointer to directory entry */
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate  /* loop invariants:
3237c478bdstevel@tonic-gate     current_ino = inode to lookup
3247c478bdstevel@tonic-gate     dirname = pointer to filename component we are cur looking up within
3257c478bdstevel@tonic-gate     the directory known pointed to by current_ino (if any) */
3267c478bdstevel@tonic-gate
3277c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
3287c478bdstevel@tonic-gate  printf ("\n");
3297c478bdstevel@tonic-gate#endif
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate  while (1)
3327c478bdstevel@tonic-gate    {
3337c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
3347c478bdstevel@tonic-gate      printf ("inode %d, dirname %s\n", current_ino, dirname);
3357c478bdstevel@tonic-gate#endif
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gate      ino_blk = (2 + SUPERBLOCK->s_imap_blocks + SUPERBLOCK->s_zmap_blocks
3387c478bdstevel@tonic-gate		 + (current_ino - 1) / MINIX_INODES_PER_BLOCK);
3397c478bdstevel@tonic-gate      if (! minix_rdfsb (ino_blk, (int) INODE))
3407c478bdstevel@tonic-gate	return 0;
3417c478bdstevel@tonic-gate
3427c478bdstevel@tonic-gate      /* reset indirect blocks! */
3437c478bdstevel@tonic-gate      mapblock2 = mapblock1 = -1;
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gate      raw_inode = INODE + ((current_ino - 1) % MINIX_INODES_PER_BLOCK);
3467c478bdstevel@tonic-gate
3477c478bdstevel@tonic-gate      /* copy inode to fixed location */
3487c478bdstevel@tonic-gate      memmove ((void *) INODE, (void *) raw_inode,
3497c478bdstevel@tonic-gate	       sizeof (struct minix_inode));
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate      /* If we've got a symbolic link, then chase it. */
3527c478bdstevel@tonic-gate      if (S_ISLNK (INODE->i_mode))
3537c478bdstevel@tonic-gate	{
3547c478bdstevel@tonic-gate	  int len;
3557c478bdstevel@tonic-gate
3567c478bdstevel@tonic-gate	  if (++link_count > MAX_LINK_COUNT)
3577c478bdstevel@tonic-gate	    {
3587c478bdstevel@tonic-gate	      errnum = ERR_SYMLINK_LOOP;
3597c478bdstevel@tonic-gate	      return 0;
3607c478bdstevel@tonic-gate	    }
3617c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
3627c478bdstevel@tonic-gate	  printf ("S_ISLNK (%s)\n", dirname);
3637c478bdstevel@tonic-gate#endif
3647c478bdstevel@tonic-gate
3657c478bdstevel@tonic-gate	  /* Find out how long our remaining name is. */
3667c478bdstevel@tonic-gate	  len = 0;
3677c478bdstevel@tonic-gate	  while (dirname[len] && !isspace (dirname[len]))
3687c478bdstevel@tonic-gate	    len++;
3697c478bdstevel@tonic-gate
3707c478bdstevel@tonic-gate	  /* Get the symlink size. */
3717c478bdstevel@tonic-gate	  filemax = (INODE->i_size);
3727c478bdstevel@tonic-gate	  if (filemax + len > sizeof (linkbuf) - 2)
3737c478bdstevel@tonic-gate	    {
3747c478bdstevel@tonic-gate	      errnum = ERR_FILELENGTH;
3757c478bdstevel@tonic-gate	      return 0;
3767c478bdstevel@tonic-gate	    }
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate	  if (len)
3797c478bdstevel@tonic-gate	    {
3807c478bdstevel@tonic-gate	      /* Copy the remaining name to the end of the symlink data.
3817c478bdstevel@tonic-gate	         Note that DIRNAME and LINKBUF may overlap! */
3827c478bdstevel@tonic-gate	      memmove (linkbuf + filemax, dirname, len);
3837c478bdstevel@tonic-gate	    }
3847c478bdstevel@tonic-gate	  linkbuf[filemax + len] = '\0';
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gate	  /* Read the necessary blocks, and reset the file pointer. */
3877c478bdstevel@tonic-gate	  len = grub_read (linkbuf, filemax);
3887c478bdstevel@tonic-gate	  filepos = 0;
3897c478bdstevel@tonic-gate	  if (!len)
3907c478bdstevel@tonic-gate	    return 0;
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
3937c478bdstevel@tonic-gate	  printf ("symlink=%s\n", linkbuf);
3947c478bdstevel@tonic-gate#endif
3957c478bdstevel@tonic-gate
3967c478bdstevel@tonic-gate	  dirname = linkbuf;
3977c478bdstevel@tonic-gate	  if (*dirname == '/')
3987c478bdstevel@tonic-gate	    {
3997c478bdstevel@tonic-gate	      /* It's an absolute link, so look it up in root. */
4007c478bdstevel@tonic-gate	      current_ino = MINIX_ROOT_INO;
4017c478bdstevel@tonic-gate	      updir_ino = current_ino;
4027c478bdstevel@tonic-gate	    }
4037c478bdstevel@tonic-gate	  else
4047c478bdstevel@tonic-gate	    {
4057c478bdstevel@tonic-gate	      /* Relative, so look it up in our parent directory. */
4067c478bdstevel@tonic-gate	      current_ino = updir_ino;
4077c478bdstevel@tonic-gate	    }
4087c478bdstevel@tonic-gate
4097c478bdstevel@tonic-gate	  /* Try again using the new name. */
4107c478bdstevel@tonic-gate	  continue;
4117c478bdstevel@tonic-gate	}
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gate      /* If end of filename, INODE points to the file's inode */
4147c478bdstevel@tonic-gate      if (!*dirname || isspace (*dirname))
4157c478bdstevel@tonic-gate	{
4167c478bdstevel@tonic-gate	  if (!S_ISREG (INODE->i_mode))
4177c478bdstevel@tonic-gate	    {
4187c478bdstevel@tonic-gate	      errnum = ERR_BAD_FILETYPE;
4197c478bdstevel@tonic-gate	      return 0;
4207c478bdstevel@tonic-gate	    }
4217c478bdstevel@tonic-gate
4227c478bdstevel@tonic-gate	  filemax = (INODE->i_size);
4237c478bdstevel@tonic-gate	  return 1;
4247c478bdstevel@tonic-gate	}
4257c478bdstevel@tonic-gate
4267c478bdstevel@tonic-gate      /* else we have to traverse a directory */
4277c478bdstevel@tonic-gate      updir_ino = current_ino;
4287c478bdstevel@tonic-gate
4297c478bdstevel@tonic-gate      /* skip over slashes */
4307c478bdstevel@tonic-gate      while (*dirname == '/')
4317c478bdstevel@tonic-gate	dirname++;
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate      /* if this isn't a directory of sufficient size to hold our file,
4347c478bdstevel@tonic-gate	 abort */
4357c478bdstevel@tonic-gate      if (!(INODE->i_size) || !S_ISDIR (INODE->i_mode))
4367c478bdstevel@tonic-gate	{
4377c478bdstevel@tonic-gate	  errnum = ERR_BAD_FILETYPE;
4387c478bdstevel@tonic-gate	  return 0;
4397c478bdstevel@tonic-gate	}
4407c478bdstevel@tonic-gate
4417c478bdstevel@tonic-gate      /* skip to next slash or end of filename (space) */
4427c478bdstevel@tonic-gate      for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/';
4437c478bdstevel@tonic-gate	   rest++);
4447c478bdstevel@tonic-gate
4457c478bdstevel@tonic-gate      /* look through this directory and find the next filename component */
4467c478bdstevel@tonic-gate      /* invariant: rest points to slash after the next filename component */
4477c478bdstevel@tonic-gate      *rest = 0;
4487c478bdstevel@tonic-gate      loc = 0;
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate      do
4517c478bdstevel@tonic-gate	{
4527c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
4537c478bdstevel@tonic-gate	  printf ("dirname=`%s', rest=`%s', loc=%d\n", dirname, rest, loc);
4547c478bdstevel@tonic-gate#endif
4557c478bdstevel@tonic-gate
4567c478bdstevel@tonic-gate	  /* if our location/byte offset into the directory exceeds the size,
4577c478bdstevel@tonic-gate	     give up */
4587c478bdstevel@tonic-gate	  if (loc >= INODE->i_size)
4597c478bdstevel@tonic-gate	    {
4607c478bdstevel@tonic-gate	      if (print_possibilities < 0)
4617c478bdstevel@tonic-gate		{
4627c478bdstevel@tonic-gate#if 0
4637c478bdstevel@tonic-gate		  putchar ('\n');
4647c478bdstevel@tonic-gate#endif
4657c478bdstevel@tonic-gate		}
4667c478bdstevel@tonic-gate	      else
4677c478bdstevel@tonic-gate		{
4687c478bdstevel@tonic-gate		  errnum = ERR_FILE_NOT_FOUND;
4697c478bdstevel@tonic-gate		  *rest = ch;
4707c478bdstevel@tonic-gate		}
4717c478bdstevel@tonic-gate	      return (print_possibilities < 0);
4727c478bdstevel@tonic-gate	    }
4737c478bdstevel@tonic-gate
4747c478bdstevel@tonic-gate	  /* else, find the (logical) block component of our location */
4757c478bdstevel@tonic-gate	  blk = loc >> BLOCK_SIZE_BITS;
4767c478bdstevel@tonic-gate
4777c478bdstevel@tonic-gate	  /* we know which logical block of the directory entry we are looking
4787c478bdstevel@tonic-gate	     for, now we have to translate that to the physical (fs) block on
4797c478bdstevel@tonic-gate	     the disk */
4807c478bdstevel@tonic-gate	  map = minix_block_map (blk);
4817c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
4827c478bdstevel@tonic-gate	  printf ("fs block=%d\n", map);
4837c478bdstevel@tonic-gate#endif
4847c478bdstevel@tonic-gate	  mapblock2 = -1;
4857c478bdstevel@tonic-gate	  if ((map < 0) || !minix_rdfsb (map, DATABLOCK2))
4867c478bdstevel@tonic-gate	    {
4877c478bdstevel@tonic-gate	      errnum = ERR_FSYS_CORRUPT;
4887c478bdstevel@tonic-gate	      *rest = ch;
4897c478bdstevel@tonic-gate	      return 0;
4907c478bdstevel@tonic-gate	    }
4917c478bdstevel@tonic-gate	  off = loc & (BLOCK_SIZE - 1);
4927c478bdstevel@tonic-gate	  dp = (struct minix_dir_entry *) (DATABLOCK2 + off);
4937c478bdstevel@tonic-gate	  /* advance loc prematurely to next on-disk directory entry  */
4947c478bdstevel@tonic-gate	  loc += sizeof (dp->inode) + namelen;
4957c478bdstevel@tonic-gate
4967c478bdstevel@tonic-gate	  /* NOTE: minix filenames are NULL terminated if < NAMELEN
4977c478bdstevel@tonic-gate	     else exact */
4987c478bdstevel@tonic-gate
4997c478bdstevel@tonic-gate#ifdef DEBUG_MINIX
5007c478bdstevel@tonic-gate	  printf ("directory entry ino=%d\n", dp->inode);
5017c478bdstevel@tonic-gate	  if (dp->inode)
5027c478bdstevel@tonic-gate	    printf ("entry=%s\n", dp->name);
5037c478bdstevel@tonic-gate#endif
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gate	  if (dp->inode)
5067c478bdstevel@tonic-gate	    {
5077c478bdstevel@tonic-gate	      int saved_c = dp->name[namelen];
5087c478bdstevel@tonic-gate
5097c478bdstevel@tonic-gate	      dp->name[namelen] = 0;
5107c478bdstevel@tonic-gate	      str_chk = substring (dirname, dp->name);
5117c478bdstevel@tonic-gate
5127c478bdstevel@tonic-gate# ifndef STAGE1_5
5137c478bdstevel@tonic-gate	      if (print_possibilities && ch != '/'
5147c478bdstevel@tonic-gate		  && (!*dirname || str_chk <= 0))
5157c478bdstevel@tonic-gate		{
5167c478bdstevel@tonic-gate		  if (print_possibilities > 0)
5177c478bdstevel@tonic-gate		    print_possibilities = -print_possibilities;
5187c478bdstevel@tonic-gate		  print_a_completion (dp->name);
5197c478bdstevel@tonic-gate		}
5207c478bdstevel@tonic-gate# endif
5217c478bdstevel@tonic-gate
5227c478bdstevel@tonic-gate	      dp->name[namelen] = saved_c;
5237c478bdstevel@tonic-gate	    }
5247c478bdstevel@tonic-gate
5257c478bdstevel@tonic-gate	}
5267c478bdstevel@tonic-gate      while (!dp->inode || (str_chk || (print_possibilities && ch != '/')));
5277c478bdstevel@tonic-gate
5287c478bdstevel@tonic-gate      current_ino = dp->inode;
5297c478bdstevel@tonic-gate      *(dirname = rest) = ch;
5307c478bdstevel@tonic-gate    }
5317c478bdstevel@tonic-gate  /* never get here */
5327c478bdstevel@tonic-gate}
5337c478bdstevel@tonic-gate
5347c478bdstevel@tonic-gate#endif /* FSYS_MINIX */
535