home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / UTILS / S / UZI.ARK / FILESYS.C < prev    next >
Text File  |  1988-11-30  |  22KB  |  1,074 lines

  1. /**************************************************
  2. UZI (Unix Z80 Implementation) Kernel:  filesys.c
  3. ***************************************************/
  4.  
  5.  
  6. #include "unix.h"
  7. #include "extern.h"
  8.  
  9.  
  10. char *bread();
  11.  
  12.  
  13. /* N_open is given a string containing a path name,
  14.   and returns a inode table pointer.  If it returns NULL,
  15.   the file did not exist.  If the parent existed,
  16.   and parent is not null, parent will be filled in with
  17.   the parents inoptr. Otherwise, parent will be set to NULL. */
  18.  
  19. inoptr
  20. n_open(name,parent)
  21. register char *name;
  22. register inoptr *parent;
  23. {
  24.  
  25.     register inoptr wd;  /* the directory we are currently searching. */
  26.     register inoptr ninode;
  27.     register inoptr temp;
  28.     inoptr srch_dir();
  29.     inoptr srch_mt();
  30.  
  31.     if (*name == '/')
  32.     wd = root;
  33.     else
  34.     wd = udata.u_cwd;
  35.  
  36.     i_ref(ninode = wd);
  37.     i_ref(ninode);
  38.  
  39.     for(;;)
  40.     {
  41.     if (ninode)
  42.         magic(ninode);
  43.  
  44.     /* See if we are at a mount point */
  45.     if (ninode)
  46.         ninode = srch_mt(ninode);
  47.  
  48.     while (*name == '/')    /* Skip (possibly repeated) slashes */
  49.         ++name;
  50.     ifnot (*name)           /* No more components of path? */
  51.         break;
  52.     ifnot (ninode)
  53.     {
  54.         udata.u_error = ENOENT;
  55.         goto nodir;
  56.     }
  57.     i_deref(wd);
  58.     wd = ninode;
  59.     if (getmode(wd) != F_DIR)
  60.     {
  61.         udata.u_error = ENOTDIR;
  62.         goto nodir;
  63.     }
  64.     ifnot (getperm(wd) & OTH_EX)
  65.     {
  66.         udata.u_error = EPERM;
  67.         goto nodir;
  68.     }
  69.  
  70.     /* See if we are going up through a mount point */
  71.     if ( wd->c_num == ROOTINODE && wd->c_dev != ROOTDEV && name[1] == '.')
  72.     {
  73.        temp = fs_tab[wd->c_dev].s_mntpt;
  74.        ++temp->c_refs;
  75.        i_deref(wd);
  76.        wd = temp;
  77.     }
  78.  
  79.     ninode = srch_dir(wd,name);
  80.  
  81.     while (*name != '/' && *name )
  82.         ++name;
  83.     }
  84.  
  85.     if (parent)
  86.     *parent = wd;
  87.     else
  88.     i_deref(wd);
  89.     ifnot (parent || ninode)
  90.     udata.u_error = ENOENT;
  91.     return (ninode);
  92.  
  93. nodir:
  94.     if (parent)
  95.     *parent = NULLINODE;
  96.     i_deref(wd);
  97.     return(NULLINODE);
  98.  
  99. }
  100.  
  101.  
  102.  
  103. /* Srch_dir is given a inode pointer of an open directory
  104. and a string containing a filename, and searches the directory
  105. for the file.  If it exists, it opens it and returns the inode pointer,
  106. otherwise NULL. This depends on the fact that ba_read will return unallocated
  107. blocks as zero-filled, and a partially allocated block will be padded with
  108. zeroes.  */
  109.  
  110. inoptr
  111. srch_dir(wd,compname)
  112. register inoptr wd;
  113. register char *compname;
  114. {
  115.     register int curentry;
  116.     register blkno_t curblock;
  117.     register struct direct *buf;
  118.     register int nblocks;
  119.     unsigned inum;
  120.     inoptr i_open();
  121.     blkno_t bmap();
  122.  
  123.     nblocks = wd->c_node.i_size.o_blkno;
  124.     if (wd->c_node.i_size.o_offset)
  125.     ++nblocks;
  126.  
  127.     for (curblock=0; curblock < nblocks; ++curblock)
  128.     {
  129.     buf = (struct direct *)bread( wd->c_dev, bmap(wd, curblock, 1), 0);
  130.     for (curentry = 0; curentry < 32; ++curentry)
  131.     {
  132.         if (namecomp(compname,buf[curentry].d_name))
  133.         {
  134.             inum = buf[curentry&0x1f].d_ino;
  135.             brelse(buf);
  136.             return(i_open(wd->c_dev, inum));
  137.         }
  138.     }
  139.     brelse(buf);
  140.     }
  141.     return(NULLINODE);
  142. }
  143.  
  144.  
  145.  
  146. /* Srch_mt sees if the given inode is a mount point. If
  147. so it dereferences it, and references and returns a pointer
  148. to the root of the mounted filesystem. */
  149.  
  150. inoptr
  151. srch_mt(ino)
  152. register inoptr ino;
  153. {
  154.     register int j;
  155.     inoptr i_open();
  156.  
  157.     for (j=0; j < NDEVS; ++j)
  158.     if (fs_tab[j].s_mounted == SMOUNTED && fs_tab[j].s_mntpt == ino)
  159.     {
  160.         i_deref(ino);
  161.         return(i_open(j,ROOTINODE));
  162.     }
  163.  
  164.     return(ino);
  165. }
  166.  
  167.  
  168. /* I_open is given an inode number and a device number,
  169. and makes an entry in the inode table for them, or
  170. increases it reference count if it is already there.
  171. An inode # of zero means a newly allocated inode */
  172.  
  173. inoptr
  174. i_open(dev,ino)
  175. register int dev;
  176. register unsigned ino;
  177. {
  178.  
  179.     struct dinode *buf;
  180.     register inoptr nindex;
  181.     int i;
  182.     register inoptr j;
  183.     int new;
  184.     static nexti = i_tab;
  185.     unsigned i_alloc();
  186.  
  187.     if (dev<0 || dev>=NDEVS)
  188.     panic("i_open: Bad dev");
  189.  
  190.     new = 0;
  191.     ifnot (ino)  /* Want a new one */
  192.     {
  193.     new = 1;
  194.     ifnot (ino = i_alloc(dev))
  195.     {
  196.         udata.u_error = ENOSPC;
  197.         return (NULLINODE);
  198.     }
  199.     }
  200.  
  201.     if (ino < ROOTINODE || ino >= (fs_tab[dev].s_isize-2)*8)
  202.     {
  203.     warning("i_open: bad inode number");
  204.     return (NULLINODE);
  205.     }
  206.  
  207.  
  208.     nindex = NULLINODE;
  209.     j = nexti;
  210.     for (i=0; i < ITABSIZE; ++i)
  211.     {
  212.     nexti =j;
  213.     if (++j >= i_tab+ITABSIZE)
  214.         j = i_tab;
  215.  
  216.     ifnot (j->c_refs)
  217.        nindex = j;
  218.  
  219.     if (j->c_dev == dev && j->c_num == ino)
  220.     {
  221.         nindex = j;
  222.         goto found;
  223.     }
  224.     }
  225.  
  226.     /* Not already in table. */
  227.  
  228.     ifnot (nindex)  /* No unrefed slots in inode table */
  229.     {
  230.     udata.u_error = ENFILE;
  231.     return(NULLINODE);
  232.     }
  233.  
  234.     buf = (struct dinode *)bread(dev, (ino>>3)+2, 0);
  235.     bcopy((char *)&(buf[ino & 0x07]), (char *)&(nindex->c_node), 64);
  236.     brelse(buf);
  237.  
  238.     nindex->c_dev = dev;
  239.     nindex->c_num = ino;
  240.     nindex->c_magic = CMAGIC;
  241.  
  242. found:
  243.     if (new)
  244.     {
  245.     if (nindex->c_node.i_nlink || nindex->c_node.i_mode & F_MASK)
  246.         goto badino;
  247.     }
  248.     else
  249.     {
  250.     ifnot (nindex->c_node.i_nlink && nindex->c_node.i_mode & F_MASK)
  251.         goto badino;
  252.     }
  253.  
  254.     ++nindex->c_refs;
  255.     return(nindex);
  256.  
  257. badino:
  258.     warning("i_open: bad disk inode");
  259.     return (NULLINODE);
  260. }
  261.  
  262.  
  263.  
  264. /* Ch_link modifies or makes a new entry in the directory for the name
  265. and inode pointer given. The directory is searched for oldname.
  266. When found, it is changed to newname, and it inode # is that of
  267. *nindex.  A oldname of "" matches a unused slot, and a nindex
  268. of NULLINODE means an inode # of 0.  A return status of 0 means there
  269. was no space left in the filesystem, or a non-empty oldname was not found,
  270. or the user did not have write permission. */
  271.  
  272. ch_link(wd,oldname,newname,nindex)
  273. register inoptr wd;
  274. char *oldname;
  275. char *newname;
  276. inoptr nindex;
  277. {
  278.     struct direct curentry;
  279.  
  280.     ifnot (getperm(wd) & OTH_WR)
  281.     {
  282.     udata.u_error = EPERM;
  283.     return (0);
  284.     }
  285.  
  286.     /* Search the directory for the desired slot. */
  287.  
  288.     udata.u_offset.o_blkno = 0;
  289.     udata.u_offset.o_offset = 0;
  290.  
  291.     for (;;)
  292.     {
  293.     udata.u_count = 16;
  294.     udata.u_base = (char *)&curentry;
  295.     readi(wd);
  296.  
  297.     /* Read until EOF or name is found */
  298.     /* readi() advances udata.u_offset */
  299.     if (udata.u_count == 0 || namecomp(oldname, curentry.d_name))
  300.         break;
  301.     }
  302.  
  303.     if (udata.u_count == 0 && *oldname)
  304.     return (0);   /* Entry not found */
  305.  
  306.     bcopy(newname, curentry.d_name, 14);
  307.     if (nindex)
  308.     curentry.d_ino = nindex->c_num;
  309.     else
  310.     curentry.d_ino = 0;
  311.  
  312.     /* If an existing slot is being used, we must back up the file offset */
  313.     if (udata.u_count)
  314.     {
  315.     ifnot (udata.u_offset.o_offset)
  316.     {
  317.         --udata.u_offset.o_blkno;
  318.         udata.u_offset.o_offset = 512;
  319.     }
  320.     udata.u_offset.o_offset -= 16;
  321.     }
  322.  
  323.     udata.u_count = 16;
  324.     udata.u_base = (char *)&curentry;
  325.     writei(wd);
  326.  
  327.     if (udata.u_error)
  328.     return (0);
  329.  
  330.     setftime(wd, A_TIME|M_TIME|C_TIME);  /* Sets c_dirty */
  331.  
  332.     /* Update file length to next block */
  333.     if (wd->c_node.i_size.o_offset)
  334.     {
  335.     wd->c_node.i_size.o_offset = 0;
  336.     ++wd->c_node.i_size.o_blkno;
  337.     }
  338.  
  339.     return (1);
  340. }
  341.  
  342.  
  343.  
  344. /* Filename is given a path name, and returns a pointer
  345. to the final component of it. */
  346.  
  347. char *
  348. filename(path)
  349. char *path;
  350. {
  351.     register char *ptr;
  352.  
  353.     ptr = path;
  354.     while (*ptr)
  355.     ++ptr;
  356.     while (*ptr != '/' && ptr-- > path)
  357.     ;
  358.     return (ptr+1);
  359. }
  360.  
  361.  
  362. /* Namecomp compares two strings to see if they are the same file name.
  363. It stops at 14 chars or a null or a slash. It returns 0 for difference. */
  364.  
  365. namecomp(n1,n2)
  366. register char *n1;
  367. register char *n2;
  368. {
  369.     register int n;
  370.  
  371.     n = 14;
  372.     while (*n1 && *n1 != '/')
  373.     {
  374.     if (*n1++ != *n2++)
  375.         return(0);
  376.     ifnot (--n)
  377.         return(-1);
  378.     }
  379.     return(*n2 == '\0' || *n2 == '/');
  380. }
  381.  
  382.  
  383.  
  384. /* Newfile is given a pointer to a directory and a name, and
  385.    creates an entry in the directory for the name, dereferences
  386.    the parent, and returns a pointer to the new inode.
  387.    It allocates an inode number,
  388.    and creates a new entry in the inode table for the new file,
  389.    and initializes the inode table entry for the new file.  The new file
  390.    will have one reference, and 0 links to it.
  391.    Better make sure there isn't already an entry with the same name. */
  392.  
  393. inoptr
  394. newfile(pino, name)
  395. register inoptr pino;
  396. register char *name;
  397. {
  398.  
  399.     register inoptr nindex;
  400.     register int j;
  401.     inoptr i_open();
  402.  
  403.     ifnot (nindex = i_open(pino->c_dev, 0))
  404.     goto nogood;
  405.  
  406.     nindex->c_node.i_mode = F_REG;   /* For the time being */
  407.     nindex->c_node.i_nlink = 1;
  408.     nindex->c_node.i_size.o_offset = 0;
  409.     nindex->c_node.i_size.o_blkno = 0;
  410.     for (j=0; j <20; j++)
  411.     nindex->c_node.i_addr[j] = 0;
  412.     wr_inode(nindex);
  413.  
  414.     ifnot (ch_link(pino,"",filename(name),nindex))
  415.     {
  416.     i_deref(nindex);
  417.     goto nogood;
  418.     }
  419.  
  420.     i_deref (pino);
  421.     return(nindex);
  422.  
  423. nogood:
  424.     i_deref (pino);
  425.     return (NULLINODE);
  426. }
  427.  
  428.  
  429.  
  430. /* Check the given device number, and return its address in the mount table.
  431. Also time-stamp the superblock of dev, and mark it modified.
  432. Used when freeing and allocating blocks and inodes. */
  433.  
  434. fsptr
  435. getdev(devno)
  436. register int devno;
  437. {
  438.     register fsptr dev;
  439.  
  440.     dev = fs_tab + devno;
  441.     if (devno < 0 || devno >= NDEVS || !dev->s_mounted)
  442.     panic("getdev: bad dev");
  443.     rdtime(&(dev->s_time));
  444.     dev->s_fmod = 1;
  445.     return (dev);
  446. }
  447.  
  448.  
  449. /* Returns true if the magic number of a superblock is corrupt */
  450.  
  451. baddev(dev)
  452. fsptr dev;
  453. {
  454.     return (dev->s_mounted != SMOUNTED);
  455. }
  456.  
  457.  
  458. /* I_alloc finds an unused inode number, and returns it,
  459. or 0 if there are no more inodes available. */
  460.  
  461. unsigned
  462. i_alloc(devno)
  463. int devno;
  464. {
  465.     fsptr dev;
  466.     blkno_t blk;
  467.     struct dinode *buf;
  468.     register int j;
  469.     register int k;
  470.     unsigned ino;
  471.  
  472.     if (baddev(dev = getdev(devno)))
  473.     goto corrupt;
  474.  
  475. tryagain:
  476.     if (dev->s_ninode)
  477.     {
  478.     ifnot (dev->s_tinode)
  479.         goto corrupt;
  480.     ino = dev->s_inode[--dev->s_ninode];
  481.     if (ino < 2 || ino >= (dev->s_isize-2)*8)
  482.         goto corrupt;
  483.     --dev->s_tinode;
  484.     return(ino);
  485.     }
  486.  
  487.     /* We must scan the inodes, and fill up the table */
  488.  
  489.     _sync();  /* Make on-disk inodes consistent */
  490.     k = 0;
  491.     for (blk = 2; blk < dev->s_isize; blk++)
  492.     {
  493.     buf = (struct dinode *)bread(devno, blk, 0);
  494.     for (j=0; j < 8; j++)
  495.     {
  496.         ifnot (buf[j].i_mode || buf[j].i_nlink)
  497.             dev->s_inode[k++] = 8*(blk-2) + j;
  498.         if (k==50)
  499.         {
  500.             brelse(buf);
  501.             goto done;
  502.         }
  503.     }
  504.     brelse(buf);
  505.     }
  506.  
  507. done:
  508.     ifnot (k)
  509.     {
  510.     if (dev->s_tinode)
  511.         goto corrupt;
  512.     udata.u_error = ENOSPC;
  513.     return(0);
  514.     }
  515.  
  516.     dev->s_ninode = k;
  517.     goto tryagain;
  518.  
  519. corrupt:
  520.     warning("i_alloc: corrupt superblock");
  521.     dev->s_mounted = 1;
  522.     udata.u_error = ENOSPC;
  523.     return(0);
  524. }
  525.  
  526.  
  527. /* I_free is given a device and inode number,
  528. and frees the inode.  It is assumed that there
  529. are no references to the inode in the inode table or
  530. in the filesystem. */
  531.  
  532. i_free(devno, ino)
  533. register int devno;
  534. register unsigned ino;
  535. {
  536.     register fsptr dev;
  537.  
  538.     if (baddev(dev = getdev(devno)))
  539.     return;
  540.  
  541.     if (ino < 2 || ino >= (dev->s_isize-2)*8)
  542.     panic("i_free: bad ino");
  543.  
  544.     ++dev->s_tinode;
  545.     if (dev->s_ninode < 50)
  546.     dev->s_inode[dev->s_ninode++] = ino;
  547. }
  548.  
  549.  
  550. /* Blk_alloc is given a device number, and allocates an unused block
  551. from it. A returned block number of zero means no more blocks. */
  552.  
  553. blkno_t
  554. blk_alloc(devno)
  555. register int devno;
  556. {
  557.  
  558.     register fsptr dev;
  559.     register blkno_t newno;
  560.     blkno_t *buf;
  561.     register int j;
  562.  
  563.     if (baddev(dev = getdev(devno)))
  564.     goto corrupt2;
  565.  
  566.     if (dev->s_nfree <= 0 || dev->s_nfree > 50)
  567.     goto corrupt;
  568.  
  569.     newno = dev->s_free[--dev->s_nfree];
  570.     ifnot (newno)
  571.     {
  572.     if (dev->s_tfree != 0)
  573.         goto corrupt;
  574.     udata.u_error = ENOSPC;
  575.     ++dev->s_nfree;
  576.     return(0);
  577.     }
  578.  
  579.     /* See if we must refill the s_free array */
  580.  
  581.     ifnot (dev->s_nfree)
  582.     {
  583.     buf = (blkno_t *)bread(devno,newno, 0);
  584.     dev->s_nfree = buf[0];
  585.     for (j=0; j < 50; j++)
  586.     {
  587.         dev->s_free[j] = buf[j+1];
  588.     }
  589.     brelse((char *)buf);
  590.     }
  591.  
  592.     validblk(devno, newno);
  593.  
  594.     ifnot (dev->s_tfree)
  595.     goto corrupt;
  596.     --dev->s_tfree;
  597.  
  598.     /* Zero out the new block */
  599.     buf = bread(devno, newno, 2);
  600.     bzero(buf, 512);
  601.     bawrite(buf);
  602.     return(newno);
  603.  
  604. corrupt:
  605.     warning("blk_alloc: corrupt");
  606.     dev->s_mounted = 1;
  607. corrupt2:
  608.     udata.u_error = ENOSPC;
  609.     return(0);
  610. }
  611.  
  612.  
  613. /* Blk_free is given a device number and a block number,
  614. and frees the block. */
  615.  
  616. blk_free(devno,blk)
  617. register int devno;
  618. register blkno_t blk;
  619. {
  620.     register fsptr dev;
  621.     register char *buf;
  622.  
  623.     ifnot (blk)
  624.     return;
  625.  
  626.     if (baddev(dev = getdev(devno)))
  627.     return;
  628.  
  629.     validblk(devno, blk);
  630.  
  631.     if (dev->s_nfree == 50)
  632.     {
  633.     buf = bread(devno, blk, 1);
  634.     bcopy((char *)&(dev->s_nfree), buf, 512);
  635.     bawrite(buf);
  636.     dev->s_nfree = 0;
  637.     }
  638.  
  639.     ++dev->s_tfree;
  640.     dev->s_free[(dev->s_nfree)++] = blk;
  641.  
  642. }
  643.  
  644.  
  645.  
  646.  
  647. /* Oft_alloc and oft_deref allocate and dereference (and possibly free)
  648. entries in the open file table. */
  649.  
  650. oft_alloc()
  651. {
  652.     register int j;
  653.  
  654.     for (j=0; j < OFTSIZE ; ++j)
  655.     {
  656.     ifnot (of_tab[j].o_refs)
  657.     {
  658.         of_tab[j].o_refs = 1;
  659.         of_tab[j].o_inode = NULLINODE;
  660.         return (j);
  661.     }
  662.     }
  663.     udata.u_error = ENFILE;
  664.     return(-1);
  665. }
  666.  
  667. oft_deref(of)
  668. register int of;
  669. {
  670.     register struct oft *ofptr;
  671.  
  672.     ofptr = of_tab + of;
  673.  
  674.     if (!(--ofptr->o_refs) && ofptr->o_inode)
  675.     {
  676.     i_deref(ofptr->o_inode);
  677.     ofptr->o_inode = NULLINODE;
  678.     }
  679. }
  680.  
  681.  
  682.  
  683. /* Uf_alloc finds an unused slot in the user file table. */
  684.  
  685. uf_alloc()
  686. {
  687.     register int j;
  688.  
  689.     for (j=0; j < UFTSIZE ; ++j)
  690.     {
  691.     if (udata.u_files[j] & 0x80)  /* Portable, unlike  == -1 */
  692.     {
  693.         return (j);
  694.     }
  695.     }
  696.     udata.u_error = ENFILE;
  697.     return(-1);
  698. }
  699.  
  700.  
  701.  
  702. /* I_ref increases the reference count of the given inode table entry. */
  703.  
  704. i_ref(ino)
  705. inoptr ino;
  706. {
  707.     if (++(ino->c_refs) == 2*ITABSIZE)  /* Arbitrary limit. */
  708.     panic("too many i-refs");
  709. }
  710.  
  711.  
  712. /* I_deref decreases the reference count of an inode, and frees it
  713. from the table if there are no more references to it.  If it also
  714. has no links, the inode itself and its blocks (if not a device) is freed. */
  715.  
  716. i_deref(ino)
  717. register inoptr ino;
  718. {
  719.     magic(ino);
  720.  
  721.     ifnot (ino->c_refs)
  722.     panic("inode freed.");
  723.  
  724.     if ((ino->c_node.i_mode & F_MASK) == F_PIPE)
  725.     wakeup((char *)ino);
  726.  
  727.     /* If the inode has no links and no refs, it must have
  728.     its blocks freed. */
  729.  
  730.     ifnot (--ino->c_refs || ino->c_node.i_nlink)
  731.         f_trunc(ino);
  732.  
  733.     /* If the inode was modified, we must write it to disk. */
  734.     if (!(ino->c_refs) && ino->c_dirty)
  735.     {
  736.     ifnot (ino->c_node.i_nlink)
  737.     {
  738.         ino->c_node.i_mode = 0;
  739.         i_free(ino->c_dev, ino->c_num);
  740.     }
  741.     wr_inode(ino);
  742.     }
  743. }
  744.  
  745.  
  746. /* Wr_inode writes out the given inode in the inode table out to disk,
  747. and resets its dirty bit. */
  748.  
  749. wr_inode(ino)
  750. register inoptr ino;
  751. {
  752.     struct dinode *buf;
  753.     register blkno_t blkno;
  754.  
  755.     magic(ino);
  756.  
  757.     blkno = (ino->c_num >> 3) + 2;
  758.     buf = (struct dinode *)bread(ino->c_dev, blkno,0);
  759.     bcopy((char *)(&ino->c_node),
  760.     (char *)((char **)&buf[ino->c_num & 0x07]), 64);
  761.     bfree(buf, 2);
  762.     ino->c_dirty = 0;
  763. }
  764.  
  765.  
  766. /* isdevice(ino) returns true if ino points to a device */
  767. isdevice(ino)
  768. inoptr ino;
  769. {
  770.     return (ino->c_node.i_mode & 020000);
  771. }
  772.  
  773.  
  774. /* This returns the device number of an inode representing a device */
  775. devnum(ino)
  776. inoptr ino;
  777. {
  778.     return (*(ino->c_node.i_addr));
  779. }
  780.  
  781.  
  782. /* F_trunc frees all the blocks associated with the file,
  783. if it is a disk file. */
  784.  
  785. f_trunc(ino)
  786. register inoptr ino;
  787. {
  788.     int dev;
  789.     int j;
  790.  
  791.     dev = ino->c_dev;
  792.  
  793.     /* First deallocate the double indirect blocks */
  794.     freeblk(dev, ino->c_node.i_addr[19], 2);
  795.  
  796.     /* Also deallocate the indirect blocks */
  797.     freeblk(dev, ino->c_node.i_addr[18], 1);
  798.  
  799.     /* Finally, free the direct blocks */
  800.     for (j=17; j >= 0; --j)
  801.     freeblk(dev, ino->c_node.i_addr[j], 0);
  802.  
  803.     bzero((char *)ino->c_node.i_addr, sizeof(ino->c_node.i_addr));
  804.  
  805.     ino->c_dirty = 1;
  806.     ino->c_node.i_size.o_blkno = 0;
  807.     ino->c_node.i_size.o_offset = 0;
  808. }
  809.  
  810.  
  811. /* Companion function to f_trunc(). */
  812. freeblk(dev, blk, level)
  813. int dev;
  814. blkno_t blk;
  815. int level;
  816. {
  817.     blkno_t *buf;
  818.     int j;
  819.  
  820.     ifnot (blk)
  821.     return;
  822.  
  823.     if (level)
  824.     {
  825.     buf = (blkno_t *)bread(dev, blk, 0);
  826.     for (j=255; j >= 0; --j)
  827.         freeblk(dev, buf[j], level-1);
  828.     brelse((char *)buf);
  829.     }
  830.  
  831.     blk_free(dev,blk);
  832. }
  833.  
  834.  
  835.  
  836. /* Changes: blk_alloc zeroes block it allocates */
  837.  
  838. /*
  839.  * Bmap defines the structure of file system storage
  840.  * by returning the physical block number on a device given the
  841.  * inode and the logical block number in a file.
  842.  * The block is zeroed if created.
  843.  */
  844. blkno_t
  845. bmap(ip, bn, rwflg)
  846. inoptr ip;
  847. register blkno_t bn;
  848. int rwflg;
  849. {
  850.     register int i;
  851.     register bufptr bp;
  852.     register int j;
  853.     register blkno_t nb;
  854.     int sh;
  855.     int dev;
  856.  
  857.     blkno_t blk_alloc();
  858.  
  859.     if (getmode(ip) == F_BDEV)
  860.         return (bn);
  861.  
  862.     dev = ip->c_dev;
  863.  
  864.     /*
  865.      * blocks 0..17 are direct blocks
  866.      */
  867.     if(bn < 18) {
  868.             nb = ip->c_node.i_addr[bn];
  869.             if(nb == 0) {
  870.                     if(rwflg || (nb = blk_alloc(dev))==0)
  871.                             return(NULLBLK);
  872.                     ip->c_node.i_addr[bn] = nb;
  873.                     ip->c_dirty = 1;
  874.             }
  875.             return(nb);
  876.     }
  877.  
  878.     /*
  879.      * addresses 18 and 19
  880.      * have single and double indirect blocks.
  881.      * the first step is to determine
  882.      * how many levels of indirection.
  883.      */
  884.     bn -= 18;
  885.     sh = 0;
  886.     j = 2;
  887.     if (bn & 0xff00)   /* bn > 255  so double indirect */
  888.     {
  889.         sh = 8;
  890.         bn -= 256;
  891.         j = 1;
  892.     }
  893.  
  894.     /*
  895.      * fetch the address from the inode
  896.      * Create the first indirect block if needed.
  897.      */
  898.     ifnot (nb = ip->c_node.i_addr[20-j])
  899.     {
  900.             if(rwflg || !(nb = blk_alloc(dev)))
  901.                     return(NULLBLK);
  902.             ip->c_node.i_addr[20-j] = nb;
  903.             ip->c_dirty = 1;
  904.     }
  905.  
  906.     /*
  907.      * fetch through the indirect blocks
  908.      */
  909.     for(; j<=2; j++) {
  910.             bp = (bufptr)bread(dev, nb, 0);
  911.             /******
  912.             if(bp->bf_error) {
  913.                     brelse(bp);
  914.                     return((blkno_t)0);
  915.             }
  916.             ******/
  917.             i = (bn>>sh) & 0xff;
  918.             if (nb = ((blkno_t *)bp)[i])
  919.                 brelse(bp);
  920.             else
  921.             {
  922.                     if(rwflg || !(nb = blk_alloc(dev))) {
  923.                             brelse(bp);
  924.                             return(NULLBLK);
  925.                     }
  926.                     ((blkno_t *)bp)[i] = nb;
  927.                     bawrite(bp);
  928.             }
  929.             sh -= 8;
  930.     }
  931.  
  932.     return(nb);
  933. }
  934.  
  935.  
  936.  
  937. /* Validblk panics if the given block number is not a valid data block
  938. for the given device. */
  939.  
  940. validblk(dev, num)
  941. int dev;
  942. blkno_t num;
  943. {
  944.     register fsptr devptr;
  945.  
  946.     devptr = fs_tab + dev;
  947.  
  948.     if (devptr->s_mounted == 0)
  949.     panic("validblk: not mounted");
  950.  
  951.     if (num < devptr->s_isize || num >= devptr->s_fsize)
  952.     panic("validblk: invalid blk");
  953. }
  954.  
  955.  
  956.  
  957. /* This returns the inode pointer associated with a user's
  958. file descriptor, checking for valid data structures */
  959.  
  960. inoptr
  961. getinode(uindex)
  962. register int uindex;
  963. {
  964.     register int oftindex;
  965.     register inoptr inoindex;
  966.  
  967.     if (uindex < 0 || uindex >= UFTSIZE || udata.u_files[uindex] & 0x80 )
  968.     {
  969.     udata.u_error = EBADF;
  970.     return (NULLINODE);
  971.     }
  972.  
  973.     if ((oftindex = udata.u_files[uindex]) < 0 || oftindex >= OFTSIZE)
  974.     panic("Getinode: bad desc table");
  975.  
  976.     if ((inoindex = of_tab[oftindex].o_inode) < i_tab ||
  977.             inoindex >= i_tab+ITABSIZE)
  978.     panic("Getinode: bad OFT");
  979.  
  980.     magic(inoindex);
  981.  
  982.     return(inoindex);
  983. }
  984.  
  985. /* Super returns true if we are the superuser */
  986. super()
  987. {
  988.     return(udata.u_euid == 0);
  989. }
  990.  
  991. /* Getperm looks at the given inode and the effective user/group ids, and
  992. returns the effective permissions in the low-order 3 bits. */
  993.  
  994. getperm(ino)
  995. inoptr ino;
  996. {
  997.     int mode;
  998.  
  999.     if (super())
  1000.     return(07);
  1001.  
  1002.     mode = ino->c_node.i_mode;
  1003.     if (ino->c_node.i_uid == udata.u_euid)
  1004.     mode >>= 6;
  1005.     else if (ino->c_node.i_gid == udata.u_egid)
  1006.     mode >>= 3;
  1007.  
  1008.     return(mode & 07);
  1009. }
  1010.  
  1011.  
  1012. /* This sets the times of the given inode, according to the flags */
  1013.  
  1014. setftime(ino, flag)
  1015. register inoptr ino;
  1016. register int flag;
  1017. {
  1018.     ino->c_dirty = 1;
  1019.  
  1020.     if (flag & A_TIME)
  1021.     rdtime(&(ino->c_node.i_atime));
  1022.     if (flag & M_TIME)
  1023.     rdtime(&(ino->c_node.i_mtime));
  1024.     if (flag & C_TIME)
  1025.     rdtime(&(ino->c_node.i_ctime));
  1026. }
  1027.  
  1028.  
  1029. getmode(ino)
  1030. inoptr ino;
  1031. {
  1032.     return( ino->c_node.i_mode & F_MASK);
  1033. }
  1034.  
  1035.  
  1036. /* Fmount places the given device in the mount table with
  1037. mount point ino */
  1038.  
  1039. fmount(dev,ino)
  1040. register int dev;
  1041. register inoptr ino;
  1042. {
  1043.     char *buf;
  1044.     register struct filesys *fp;
  1045.  
  1046.     if (d_open(dev) != 0)
  1047.     panic("fmount: Cant open filesystem");
  1048.     /* Dev 0 blk 1 */
  1049.     fp = fs_tab + dev;
  1050.     buf = bread(dev, 1, 0);
  1051.     bcopy(buf, (char *)fp, sizeof(struct filesys));
  1052.     brelse(buf);
  1053.  
  1054.     /* See if there really is a filesystem on the device */
  1055.     if (fp->s_mounted != SMOUNTED ||
  1056.      fp->s_isize >= fp->s_fsize)
  1057.     return (-1);
  1058.  
  1059.     fp->s_mntpt = ino;
  1060.     if (ino)
  1061.     ++ino->c_refs;
  1062.  
  1063.     return (0);
  1064. }
  1065.  
  1066.  
  1067. magic(ino)
  1068. inoptr ino;
  1069. {
  1070.     if (ino->c_magic != CMAGIC)
  1071.     panic("Corrupt inode");
  1072. }
  1073.  
  1074.