Advertisement
Guest User

igetino

a guest
Apr 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.35 KB | None | 0 0
  1. #include "type.h"
  2.  
  3. /* Loads ino of inode to Minode[] array. If it exists, ++ ref and return its Minode ptr. If not,
  4.  Load the inode from disk to new Minode[i].INODE. Init it, and return its Minode ptr */
  5. MINODE *iget(int dev, int ino)
  6. {
  7.     int i = 0, j = 0, block, node;
  8.    
  9.     MINODE *retPtr = 0;
  10.     // 1. Search minode[i] for an entry whose refCount > 0 with the SAME (dev,ino)
  11.     for (i = 0; i < 100; i++)
  12.     {
  13.         if(minode[i].ino == ino && minode[i].refCount > 0) // It exists! ++ ref
  14.         {
  15.             minode[i].refCount++;
  16.             retPtr = (MINODE *) &minode[i];
  17.             //printf("Already exists. i_mode = %d\n", retPtr->INODE.i_mode);
  18.             return retPtr;  // Return it's Minode ptr
  19.         }
  20.     }
  21.  
  22.     // 2. Find a minode[i] whose refCount = 0 => let MINODE *mip = &minode[i];
  23.     for (i = 0; i < 100; i++)
  24.     {
  25.         if (minode[i].refCount == 0)
  26.         {
  27.             // 3. Use mailman's algorithm to compute
  28.             block = (ino - 1)/8 + inodes_start;
  29.             node = (ino - 1) % 8;
  30.             //printf("block: %d\nnode: %d\n", block, node);
  31.  
  32.             // 4. read blk into buf[ ];
  33.             get_block(dev, block, buf);
  34.             // 4. let INODE *ip = (     )buf + offset
  35.             ip = (INODE *) buf + node;  // Points at INODE in buf[]
  36.            
  37.             // 5.  COPY *ip into mip->INODE
  38.             minode[i].INODE = *(ip);
  39.  
  40.             // 6. initialize other fields of *mip:
  41.             minode[i].refCount++;
  42.             minode[i].dev = dev;
  43.             minode[i].ino = ino;
  44.             minode[i].dirty = 0;
  45.             minode[i].mounted = 0;
  46.             minode[i].mountptr = 0;
  47.  
  48.             retPtr = (MINODE *) &minode[i];
  49.  
  50.             // 7. return mip
  51.             return retPtr;
  52.         }
  53.     }
  54. }
  55.  
  56. // This function releases a Minode[] pointed by mip
  57. int iput(MINODE *mip)
  58. {  
  59.     int block, node;
  60.  
  61.     // 1. dec refCount by 1.
  62.     mip->refCount--;
  63.    
  64.     // 2. CASE 1: if (after dec) refCount > 0 ==> return;
  65.     if (mip->refCount > 0)
  66.     {
  67.         return 0;
  68.     }
  69.     if (mip->dirty != 1) // CASE 2: no need to write back, so return;
  70.     {
  71.         return 0;
  72.     }
  73.     // CASE 3: (refCount > 0 AND dirty==1) case ==> must write the INODE back to disk
  74.     // 3. Use Mailman's algorithm to determine the disk block and inode's offset in that block.
  75.     block = (mip->ino - 1)/8 + inodes_start;
  76.     node = (mip->ino - 1) % 8;
  77.  
  78.     mip->dirty = 0;
  79.    
  80.     // 4. Read that block into a buf[ ]
  81.     get_block(mip->dev, block, buf);
  82.     ip = (INODE *) buf + node; // let INODE *ip point at the INODE in buf[ ].
  83.     // 5. Copy mip->INODE into *ip in buf[ ];
  84.     *ip = mip->INODE;
  85.     // 6. Write the block (in buf[ ]) back to disk.
  86.     //printf("writing inode with mode: %x\n", ip->i_mode);
  87.     put_block(mip->dev, block, buf);
  88. }
  89.  
  90. void printInode(INODE *inode)
  91. {
  92.     printf("i_mode: %x\n", inode->i_mode);
  93.     printf("i_uid: %d\n", inode->i_uid);
  94.     printf("i_size: %d\n", inode->i_size);
  95. }
  96.  
  97. // Finds the name string of myino in the parent's data block
  98. int search(int dev, INODE *inodePtr, char *name)
  99. {
  100.   int i = 0, j = 0, k = 0;
  101.   char *cp;
  102.   char temp[BLOCK_SIZE];
  103.  
  104.   for (; i < 12; i++)
  105.     {
  106.       get_block(dev, inodePtr->i_block[i], buf);
  107.       dp = (DIR *) buf;
  108.       cp = buf;
  109.       while(cp < buf+BLOCK_SIZE && dp->rec_len != 0) //4*((11+dp->name_len)/4)//while(cp < buf+BLOCK_SIZE && dp->rec_len != 0)
  110.  
  111.         {
  112.             for (j = 0; j < dp->name_len; j++)
  113.             {
  114.                 temp[j] = dp->name[j];
  115.             }
  116.             temp[j] = '\0';
  117.  
  118.             printf("%s\n", temp);
  119.      
  120.             if (strcmp(name, temp) == 0)
  121.             {
  122.                 printf("Found %s at ino %d\n", name, dp->inode);
  123.                 return dp->inode;
  124.             }
  125.             cp += dp->rec_len;
  126.             dp = (DIR *) cp;
  127.         }
  128.      }
  129.     printf("Could not find \'%s\'\n", name);
  130.     return -1;
  131. }
  132.  
  133. // Converts a pathname into its inode number (ino)
  134. int getino(int dev, char *pathname)
  135. {
  136.     char *dirtok, *temp = 0;
  137.     char *cp;
  138.     int i, j, currInode = 1, currBlock, currIno = 1;
  139.     int InodeBeginBlock;
  140.  
  141.     // read SUPER block
  142.     get_block(dev, 1, buf);  
  143.     sp = (SUPER *)buf;
  144.  
  145.     // Check if it's an EXT2 FS
  146.     if (sp->s_magic != 0xEF53)
  147.     {
  148.         printf("NOT an EXT2 FS\n");
  149.             return 1;
  150.     }
  151.  
  152.     // Group descriptor block
  153.     get_block(dev, 2, buf);
  154.     gp = (GD *)buf;
  155.  
  156.     // read InodeBeginBLock
  157.     InodeBeginBlock = gp->bg_inode_table;
  158.  
  159.     // Case 1: no pathname, or / aka current working directory
  160.     if (pathname == 0 || !strcmp(pathname, "") || !strcmp(pathname, "/"))
  161.     {
  162.         printf("Running cwd ino %d\n", running->cwd->ino);
  163.         return running->cwd->ino;   // return ino of cwd
  164.     }
  165.     // Case 2: ..
  166.     else if(!strcmp(pathname, ".."))
  167.     {   // Mailman's algorithm from InodeBeginBlock, search within parent dir
  168.         currBlock = (running->cwd->ino - 1)/8 + InodeBeginBlock;
  169.         currInode = (running->cwd->ino -1) % 8;
  170.     }
  171.     // Case 3: Not current working directory, not .., and doesn't begin with slash
  172.     else if(pathname[0] != '/')
  173.     {   // Mailman's algorithm from InodeBeginBlock, search all dirs?
  174.         currBlock = (running->cwd->ino-1)/8 + inodes_start;
  175.         currInode = (running->cwd->ino - 1)%8;
  176.     }
  177.     else
  178.     {
  179.         currBlock = InodeBeginBlock;
  180.     }
  181.     temp = malloc(sizeof(char)*(strlen(pathname)+1));
  182.     strcpy(temp, pathname);
  183.     dirtok = (char *) strtok(temp, "/");
  184.     do
  185.     {
  186.         get_block(dev, currBlock, buf);
  187.         ip = (INODE *)buf+currInode;
  188.         currIno = search(dev, ip, dirtok);
  189.         if (currIno != -1) 
  190.         {
  191.             currBlock = (currIno - 1)/8 + InodeBeginBlock;
  192.             currInode = (currIno - 1) % 8;
  193.         }
  194.         else
  195.         {
  196.             //printf("Could not find directory: %s\n", dirtok);
  197.             free(temp);
  198.             return -1;
  199.         }
  200.     }while((dirtok=(char *)strtok(NULL, "/")) && (currIno > 0));
  201.     free(temp);
  202.     return currIno;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement