Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "type.h"
- /* Loads ino of inode to Minode[] array. If it exists, ++ ref and return its Minode ptr. If not,
- Load the inode from disk to new Minode[i].INODE. Init it, and return its Minode ptr */
- MINODE *iget(int dev, int ino)
- {
- int i = 0, j = 0, block, node;
- MINODE *retPtr = 0;
- // 1. Search minode[i] for an entry whose refCount > 0 with the SAME (dev,ino)
- for (i = 0; i < 100; i++)
- {
- if(minode[i].ino == ino && minode[i].refCount > 0) // It exists! ++ ref
- {
- minode[i].refCount++;
- retPtr = (MINODE *) &minode[i];
- //printf("Already exists. i_mode = %d\n", retPtr->INODE.i_mode);
- return retPtr; // Return it's Minode ptr
- }
- }
- // 2. Find a minode[i] whose refCount = 0 => let MINODE *mip = &minode[i];
- for (i = 0; i < 100; i++)
- {
- if (minode[i].refCount == 0)
- {
- // 3. Use mailman's algorithm to compute
- block = (ino - 1)/8 + inodes_start;
- node = (ino - 1) % 8;
- //printf("block: %d\nnode: %d\n", block, node);
- // 4. read blk into buf[ ];
- get_block(dev, block, buf);
- // 4. let INODE *ip = ( )buf + offset
- ip = (INODE *) buf + node; // Points at INODE in buf[]
- // 5. COPY *ip into mip->INODE
- minode[i].INODE = *(ip);
- // 6. initialize other fields of *mip:
- minode[i].refCount++;
- minode[i].dev = dev;
- minode[i].ino = ino;
- minode[i].dirty = 0;
- minode[i].mounted = 0;
- minode[i].mountptr = 0;
- retPtr = (MINODE *) &minode[i];
- // 7. return mip
- return retPtr;
- }
- }
- }
- // This function releases a Minode[] pointed by mip
- int iput(MINODE *mip)
- {
- int block, node;
- // 1. dec refCount by 1.
- mip->refCount--;
- // 2. CASE 1: if (after dec) refCount > 0 ==> return;
- if (mip->refCount > 0)
- {
- return 0;
- }
- if (mip->dirty != 1) // CASE 2: no need to write back, so return;
- {
- return 0;
- }
- // CASE 3: (refCount > 0 AND dirty==1) case ==> must write the INODE back to disk
- // 3. Use Mailman's algorithm to determine the disk block and inode's offset in that block.
- block = (mip->ino - 1)/8 + inodes_start;
- node = (mip->ino - 1) % 8;
- mip->dirty = 0;
- // 4. Read that block into a buf[ ]
- get_block(mip->dev, block, buf);
- ip = (INODE *) buf + node; // let INODE *ip point at the INODE in buf[ ].
- // 5. Copy mip->INODE into *ip in buf[ ];
- *ip = mip->INODE;
- // 6. Write the block (in buf[ ]) back to disk.
- //printf("writing inode with mode: %x\n", ip->i_mode);
- put_block(mip->dev, block, buf);
- }
- void printInode(INODE *inode)
- {
- printf("i_mode: %x\n", inode->i_mode);
- printf("i_uid: %d\n", inode->i_uid);
- printf("i_size: %d\n", inode->i_size);
- }
- // Finds the name string of myino in the parent's data block
- int search(int dev, INODE *inodePtr, char *name)
- {
- int i = 0, j = 0, k = 0;
- char *cp;
- char temp[BLOCK_SIZE];
- for (; i < 12; i++)
- {
- get_block(dev, inodePtr->i_block[i], buf);
- dp = (DIR *) buf;
- cp = buf;
- while(cp < buf+BLOCK_SIZE && dp->rec_len != 0) //4*((11+dp->name_len)/4)//while(cp < buf+BLOCK_SIZE && dp->rec_len != 0)
- {
- for (j = 0; j < dp->name_len; j++)
- {
- temp[j] = dp->name[j];
- }
- temp[j] = '\0';
- printf("%s\n", temp);
- if (strcmp(name, temp) == 0)
- {
- printf("Found %s at ino %d\n", name, dp->inode);
- return dp->inode;
- }
- cp += dp->rec_len;
- dp = (DIR *) cp;
- }
- }
- printf("Could not find \'%s\'\n", name);
- return -1;
- }
- // Converts a pathname into its inode number (ino)
- int getino(int dev, char *pathname)
- {
- char *dirtok, *temp = 0;
- char *cp;
- int i, j, currInode = 1, currBlock, currIno = 1;
- int InodeBeginBlock;
- // read SUPER block
- get_block(dev, 1, buf);
- sp = (SUPER *)buf;
- // Check if it's an EXT2 FS
- if (sp->s_magic != 0xEF53)
- {
- printf("NOT an EXT2 FS\n");
- return 1;
- }
- // Group descriptor block
- get_block(dev, 2, buf);
- gp = (GD *)buf;
- // read InodeBeginBLock
- InodeBeginBlock = gp->bg_inode_table;
- // Case 1: no pathname, or / aka current working directory
- if (pathname == 0 || !strcmp(pathname, "") || !strcmp(pathname, "/"))
- {
- printf("Running cwd ino %d\n", running->cwd->ino);
- return running->cwd->ino; // return ino of cwd
- }
- // Case 2: ..
- else if(!strcmp(pathname, ".."))
- { // Mailman's algorithm from InodeBeginBlock, search within parent dir
- currBlock = (running->cwd->ino - 1)/8 + InodeBeginBlock;
- currInode = (running->cwd->ino -1) % 8;
- }
- // Case 3: Not current working directory, not .., and doesn't begin with slash
- else if(pathname[0] != '/')
- { // Mailman's algorithm from InodeBeginBlock, search all dirs?
- currBlock = (running->cwd->ino-1)/8 + inodes_start;
- currInode = (running->cwd->ino - 1)%8;
- }
- else
- {
- currBlock = InodeBeginBlock;
- }
- temp = malloc(sizeof(char)*(strlen(pathname)+1));
- strcpy(temp, pathname);
- dirtok = (char *) strtok(temp, "/");
- do
- {
- get_block(dev, currBlock, buf);
- ip = (INODE *)buf+currInode;
- currIno = search(dev, ip, dirtok);
- if (currIno != -1)
- {
- currBlock = (currIno - 1)/8 + InodeBeginBlock;
- currInode = (currIno - 1) % 8;
- }
- else
- {
- //printf("Could not find directory: %s\n", dirtok);
- free(temp);
- return -1;
- }
- }while((dirtok=(char *)strtok(NULL, "/")) && (currIno > 0));
- free(temp);
- return currIno;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement