inode.c

Go to the documentation of this file.
00001 /* This file manages the inode table.  There are procedures to allocate and
00002  * deallocate inodes, acquire, erase, and release them, and read and write
00003  * them from the disk.
00004  *
00005  * The entry points into this file are
00006  *   get_inode:    search inode table for a given inode; if not there,
00007  *                 read it
00008  *   put_inode:    indicate that an inode is no longer needed in memory
00009  *   alloc_inode:  allocate a new, unused inode
00010  *   wipe_inode:   erase some fields of a newly allocated inode
00011  *   free_inode:   mark an inode as available for a new file
00012  *   update_times: update atime, ctime, and mtime
00013  *   rw_inode:     read a disk block and extract an inode, or corresp. write
00014  *   old_icopy:    copy to/from in-core inode struct and disk inode (V1.x)
00015  *   new_icopy:    copy to/from in-core inode struct and disk inode (V2.x)
00016  *   dup_inode:    indicate that someone else is using an inode table entry
00017  */
00018 
00019 #include "fs.h"
00020 #include "buf.h"
00021 #include "file.h"
00022 #include "fproc.h"
00023 #include "inode.h"
00024 #include "super.h"
00025 
00026 FORWARD _PROTOTYPE( void old_icopy, (struct inode *rip, d1_inode *dip,
00027                                                 int direction, int norm));
00028 FORWARD _PROTOTYPE( void new_icopy, (struct inode *rip, d2_inode *dip,
00029                                                 int direction, int norm));
00030 
00031 /*===========================================================================*
00032  *                              get_inode                                    *
00033  *===========================================================================*/
00034 PUBLIC struct inode *get_inode(dev, numb)
00035 dev_t dev;                      /* device on which inode resides */
00036 int numb;                       /* inode number (ANSI: may not be unshort) */
00037 {
00038 /* Find a slot in the inode table, load the specified inode into it, and
00039  * return a pointer to the slot.  If 'dev' == NO_DEV, just return a free slot.
00040  */
00041 
00042   register struct inode *rip, *xp;
00043 
00044   /* Search the inode table both for (dev, numb) and a free slot. */
00045   xp = NIL_INODE;
00046   for (rip = &inode[0]; rip < &inode[NR_INODES]; rip++) {
00047         if (rip->i_count > 0) { /* only check used slots for (dev, numb) */
00048                 if (rip->i_dev == dev && rip->i_num == numb) {
00049                         /* This is the inode that we are looking for. */
00050                         rip->i_count++;
00051                         return(rip);    /* (dev, numb) found */
00052                 }
00053         } else {
00054                 xp = rip;       /* remember this free slot for later */
00055         }
00056   }
00057 
00058   /* Inode we want is not currently in use.  Did we find a free slot? */
00059   if (xp == NIL_INODE) {        /* inode table completely full */
00060         err_code = ENFILE;
00061         return(NIL_INODE);
00062   }
00063 
00064   /* A free inode slot has been located.  Load the inode into it. */
00065   xp->i_dev = dev;
00066   xp->i_num = numb;
00067   xp->i_count = 1;
00068   if (dev != NO_DEV) rw_inode(xp, READING);     /* get inode from disk */
00069   xp->i_update = 0;             /* all the times are initially up-to-date */
00070 
00071   return(xp);
00072 }
00073 
00074 /*===========================================================================*
00075  *                              put_inode                                    *
00076  *===========================================================================*/
00077 PUBLIC void put_inode(rip)
00078 register struct inode *rip;     /* pointer to inode to be released */
00079 {
00080 /* The caller is no longer using this inode.  If no one else is using it either
00081  * write it back to the disk immediately.  If it has no links, truncate it and
00082  * return it to the pool of available inodes.
00083  */
00084 
00085   if (rip == NIL_INODE) return; /* checking here is easier than in caller */
00086   if (--rip->i_count == 0) {    /* i_count == 0 means no one is using it now */
00087         if (rip->i_nlinks == 0) {
00088                 /* i_nlinks == 0 means free the inode. */
00089                 truncate_inode(rip, 0); /* return all the disk blocks */
00090                 rip->i_mode = I_NOT_ALLOC;      /* clear I_TYPE field */
00091                 rip->i_dirt = DIRTY;
00092                 free_inode(rip->i_dev, rip->i_num);
00093         } else {
00094                 if (rip->i_pipe == I_PIPE) truncate_inode(rip, 0);
00095         }
00096         rip->i_pipe = NO_PIPE;  /* should always be cleared */
00097         if (rip->i_dirt == DIRTY) rw_inode(rip, WRITING);
00098   }
00099 }
00100 
00101 /*===========================================================================*
00102  *                              alloc_inode                                  *
00103  *===========================================================================*/
00104 PUBLIC struct inode *alloc_inode(dev_t dev, mode_t bits)
00105 {
00106 /* Allocate a free inode on 'dev', and return a pointer to it. */
00107 
00108   register struct inode *rip;
00109   register struct super_block *sp;
00110   int major, minor, inumb;
00111   bit_t b;
00112 
00113   sp = get_super(dev);  /* get pointer to super_block */
00114   if (sp->s_rd_only) {  /* can't allocate an inode on a read only device. */
00115         err_code = EROFS;
00116         return(NIL_INODE);
00117   }
00118 
00119   /* Acquire an inode from the bit map. */
00120   b = alloc_bit(sp, IMAP, sp->s_isearch);
00121   if (b == NO_BIT) {
00122         err_code = ENFILE;
00123         major = (int) (sp->s_dev >> MAJOR) & BYTE;
00124         minor = (int) (sp->s_dev >> MINOR) & BYTE;
00125         printf("Out of i-nodes on %sdevice %d/%d\n",
00126                 sp->s_dev == root_dev ? "root " : "", major, minor);
00127         return(NIL_INODE);
00128   }
00129   sp->s_isearch = b;            /* next time start here */
00130   inumb = (int) b;              /* be careful not to pass unshort as param */
00131 
00132   /* Try to acquire a slot in the inode table. */
00133   if ((rip = get_inode(NO_DEV, inumb)) == NIL_INODE) {
00134         /* No inode table slots available.  Free the inode just allocated. */
00135         free_bit(sp, IMAP, b);
00136   } else {
00137         /* An inode slot is available. Put the inode just allocated into it. */
00138         rip->i_mode = bits;             /* set up RWX bits */
00139         rip->i_nlinks = 0;              /* initial no links */
00140         rip->i_uid = fp->fp_effuid;     /* file's uid is owner's */
00141         rip->i_gid = fp->fp_effgid;     /* ditto group id */
00142         rip->i_dev = dev;               /* mark which device it is on */
00143         rip->i_ndzones = sp->s_ndzones; /* number of direct zones */
00144         rip->i_nindirs = sp->s_nindirs; /* number of indirect zones per blk*/
00145         rip->i_sp = sp;                 /* pointer to super block */
00146 
00147         /* Fields not cleared already are cleared in wipe_inode().  They have
00148          * been put there because truncate() needs to clear the same fields if
00149          * the file happens to be open while being truncated.  It saves space
00150          * not to repeat the code twice.
00151          */
00152         wipe_inode(rip);
00153   }
00154 
00155   return(rip);
00156 }
00157 
00158 /*===========================================================================*
00159  *                              wipe_inode                                   *
00160  *===========================================================================*/
00161 PUBLIC void wipe_inode(rip)
00162 register struct inode *rip;     /* the inode to be erased */
00163 {
00164 /* Erase some fields in the inode.  This function is called from alloc_inode()
00165  * when a new inode is to be allocated, and from truncate(), when an existing
00166  * inode is to be truncated.
00167  */
00168 
00169   register int i;
00170 
00171   rip->i_size = 0;
00172   rip->i_update = ATIME | CTIME | MTIME;        /* update all times later */
00173   rip->i_dirt = DIRTY;
00174   for (i = 0; i < V2_NR_TZONES; i++) rip->i_zone[i] = NO_ZONE;
00175 }
00176 
00177 /*===========================================================================*
00178  *                              free_inode                                   *
00179  *===========================================================================*/
00180 PUBLIC void free_inode(dev, inumb)
00181 dev_t dev;                      /* on which device is the inode */
00182 ino_t inumb;                    /* number of inode to be freed */
00183 {
00184 /* Return an inode to the pool of unallocated inodes. */
00185 
00186   register struct super_block *sp;
00187   bit_t b;
00188 
00189   /* Locate the appropriate super_block. */
00190   sp = get_super(dev);
00191   if (inumb <= 0 || inumb > sp->s_ninodes) return;
00192   b = inumb;
00193   free_bit(sp, IMAP, b);
00194   if (b < sp->s_isearch) sp->s_isearch = b;
00195 }
00196 
00197 /*===========================================================================*
00198  *                              update_times                                 *
00199  *===========================================================================*/
00200 PUBLIC void update_times(rip)
00201 register struct inode *rip;     /* pointer to inode to be read/written */
00202 {
00203 /* Various system calls are required by the standard to update atime, ctime,
00204  * or mtime.  Since updating a time requires sending a message to the clock
00205  * task--an expensive business--the times are marked for update by setting
00206  * bits in i_update.  When a stat, fstat, or sync is done, or an inode is 
00207  * released, update_times() may be called to actually fill in the times.
00208  */
00209 
00210   time_t cur_time;
00211   struct super_block *sp;
00212 
00213   sp = rip->i_sp;               /* get pointer to super block. */
00214   if (sp->s_rd_only) return;    /* no updates for read-only file systems */
00215 
00216   cur_time = clock_time();
00217   if (rip->i_update & ATIME) rip->i_atime = cur_time;
00218   if (rip->i_update & CTIME) rip->i_ctime = cur_time;
00219   if (rip->i_update & MTIME) rip->i_mtime = cur_time;
00220   rip->i_update = 0;            /* they are all up-to-date now */
00221 }
00222 
00223 /*===========================================================================*
00224  *                              rw_inode                                     *
00225  *===========================================================================*/
00226 PUBLIC void rw_inode(rip, rw_flag)
00227 register struct inode *rip;     /* pointer to inode to be read/written */
00228 int rw_flag;                    /* READING or WRITING */
00229 {
00230 /* An entry in the inode table is to be copied to or from the disk. */
00231 
00232   register struct buf *bp;
00233   register struct super_block *sp;
00234   d1_inode *dip;
00235   d2_inode *dip2;
00236   block_t b, offset;
00237 
00238   /* Get the block where the inode resides. */
00239   sp = get_super(rip->i_dev);   /* get pointer to super block */
00240   rip->i_sp = sp;               /* inode must contain super block pointer */
00241   offset = sp->s_imap_blocks + sp->s_zmap_blocks + 2;
00242   b = (block_t) (rip->i_num - 1)/sp->s_inodes_per_block + offset;
00243   bp = get_block(rip->i_dev, b, NORMAL);
00244   dip  = bp->b_v1_ino + (rip->i_num - 1) % V1_INODES_PER_BLOCK;
00245   dip2 = bp->b_v2_ino + (rip->i_num - 1) %
00246          V2_INODES_PER_BLOCK(sp->s_block_size);
00247 
00248   /* Do the read or write. */
00249   if (rw_flag == WRITING) {
00250         if (rip->i_update) update_times(rip);   /* times need updating */
00251         if (sp->s_rd_only == FALSE) bp->b_dirt = DIRTY;
00252   }
00253 
00254   /* Copy the inode from the disk block to the in-core table or vice versa.
00255    * If the fourth parameter below is FALSE, the bytes are swapped.
00256    */
00257   if (sp->s_version == V1)
00258         old_icopy(rip, dip,  rw_flag, sp->s_native);
00259   else
00260         new_icopy(rip, dip2, rw_flag, sp->s_native);
00261   
00262   put_block(bp, INODE_BLOCK);
00263   rip->i_dirt = CLEAN;
00264 }
00265 
00266 /*===========================================================================*
00267  *                              old_icopy                                    *
00268  *===========================================================================*/
00269 PRIVATE void old_icopy(rip, dip, direction, norm)
00270 register struct inode *rip;     /* pointer to the in-core inode struct */
00271 register d1_inode *dip;         /* pointer to the d1_inode inode struct */
00272 int direction;                  /* READING (from disk) or WRITING (to disk) */
00273 int norm;                       /* TRUE = do not swap bytes; FALSE = swap */
00274 
00275 {
00276 /* The V1.x IBM disk, the V1.x 68000 disk, and the V2 disk (same for IBM and
00277  * 68000) all have different inode layouts.  When an inode is read or written
00278  * this routine handles the conversions so that the information in the inode
00279  * table is independent of the disk structure from which the inode came.
00280  * The old_icopy routine copies to and from V1 disks.
00281  */
00282 
00283   int i;
00284 
00285   if (direction == READING) {
00286         /* Copy V1.x inode to the in-core table, swapping bytes if need be. */
00287         rip->i_mode    = conv2(norm, (int) dip->d1_mode);
00288         rip->i_uid     = conv2(norm, (int) dip->d1_uid );
00289         rip->i_size    = conv4(norm,       dip->d1_size);
00290         rip->i_mtime   = conv4(norm,       dip->d1_mtime);
00291         rip->i_atime   = rip->i_mtime;
00292         rip->i_ctime   = rip->i_mtime;
00293         rip->i_nlinks  = dip->d1_nlinks;                /* 1 char */
00294         rip->i_gid     = dip->d1_gid;                   /* 1 char */
00295         rip->i_ndzones = V1_NR_DZONES;
00296         rip->i_nindirs = V1_INDIRECTS;
00297         for (i = 0; i < V1_NR_TZONES; i++)
00298                 rip->i_zone[i] = conv2(norm, (int) dip->d1_zone[i]);
00299   } else {
00300         /* Copying V1.x inode to disk from the in-core table. */
00301         dip->d1_mode   = conv2(norm, (int) rip->i_mode);
00302         dip->d1_uid    = conv2(norm, (int) rip->i_uid );
00303         dip->d1_size   = conv4(norm,       rip->i_size);
00304         dip->d1_mtime  = conv4(norm,       rip->i_mtime);
00305         dip->d1_nlinks = rip->i_nlinks;                 /* 1 char */
00306         dip->d1_gid    = rip->i_gid;                    /* 1 char */
00307         for (i = 0; i < V1_NR_TZONES; i++)
00308                 dip->d1_zone[i] = conv2(norm, (int) rip->i_zone[i]);
00309   }
00310 }
00311 
00312 /*===========================================================================*
00313  *                              new_icopy                                    *
00314  *===========================================================================*/
00315 PRIVATE void new_icopy(rip, dip, direction, norm)
00316 register struct inode *rip;     /* pointer to the in-core inode struct */
00317 register d2_inode *dip; /* pointer to the d2_inode struct */
00318 int direction;                  /* READING (from disk) or WRITING (to disk) */
00319 int norm;                       /* TRUE = do not swap bytes; FALSE = swap */
00320 
00321 {
00322 /* Same as old_icopy, but to/from V2 disk layout. */
00323 
00324   int i;
00325 
00326   if (direction == READING) {
00327         /* Copy V2.x inode to the in-core table, swapping bytes if need be. */
00328         rip->i_mode    = conv2(norm,dip->d2_mode);
00329         rip->i_uid     = conv2(norm,dip->d2_uid);
00330         rip->i_nlinks  = conv2(norm,dip->d2_nlinks);
00331         rip->i_gid     = conv2(norm,dip->d2_gid);
00332         rip->i_size    = conv4(norm,dip->d2_size);
00333         rip->i_atime   = conv4(norm,dip->d2_atime);
00334         rip->i_ctime   = conv4(norm,dip->d2_ctime);
00335         rip->i_mtime   = conv4(norm,dip->d2_mtime);
00336         rip->i_ndzones = V2_NR_DZONES;
00337         rip->i_nindirs = V2_INDIRECTS(rip->i_sp->s_block_size);
00338         for (i = 0; i < V2_NR_TZONES; i++)
00339                 rip->i_zone[i] = conv4(norm, (long) dip->d2_zone[i]);
00340   } else {
00341         /* Copying V2.x inode to disk from the in-core table. */
00342         dip->d2_mode   = conv2(norm,rip->i_mode);
00343         dip->d2_uid    = conv2(norm,rip->i_uid);
00344         dip->d2_nlinks = conv2(norm,rip->i_nlinks);
00345         dip->d2_gid    = conv2(norm,rip->i_gid);
00346         dip->d2_size   = conv4(norm,rip->i_size);
00347         dip->d2_atime  = conv4(norm,rip->i_atime);
00348         dip->d2_ctime  = conv4(norm,rip->i_ctime);
00349         dip->d2_mtime  = conv4(norm,rip->i_mtime);
00350         for (i = 0; i < V2_NR_TZONES; i++)
00351                 dip->d2_zone[i] = conv4(norm, (long) rip->i_zone[i]);
00352   }
00353 }
00354 
00355 /*===========================================================================*
00356  *                              dup_inode                                    *
00357  *===========================================================================*/
00358 PUBLIC void dup_inode(ip)
00359 struct inode *ip;               /* The inode to be duplicated. */
00360 {
00361 /* This routine is a simplified form of get_inode() for the case where
00362  * the inode pointer is already known.
00363  */
00364 
00365   ip->i_count++;
00366 }

Generated on Fri Apr 14 22:57:29 2006 for minix by  doxygen 1.4.6