1433d6423SLionel Sambuc /* This file contains the procedures that look up path names in the directory 2433d6423SLionel Sambuc * system and determine the inode number that goes with a given path name. 3433d6423SLionel Sambuc */ 4433d6423SLionel Sambuc 5433d6423SLionel Sambuc #include "fs.h" 6ccaeedb2SDavid van Moolenbroek #include <assert.h> 7433d6423SLionel Sambuc #include <string.h> 8433d6423SLionel Sambuc #include "buf.h" 9433d6423SLionel Sambuc #include "inode.h" 10433d6423SLionel Sambuc #include "super.h" 11433d6423SLionel Sambuc 12433d6423SLionel Sambuc 13433d6423SLionel Sambuc /*===========================================================================* 14433d6423SLionel Sambuc * fs_lookup * 15433d6423SLionel Sambuc *===========================================================================*/ 16ccaeedb2SDavid van Moolenbroek int fs_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node, 17ccaeedb2SDavid van Moolenbroek int *is_mountpt) 18433d6423SLionel Sambuc { 19ccaeedb2SDavid van Moolenbroek struct inode *dirp, *rip; 20433d6423SLionel Sambuc 21ccaeedb2SDavid van Moolenbroek /* Find the starting inode. */ 22ccaeedb2SDavid van Moolenbroek if ((dirp = find_inode(fs_dev, dir_nr)) == NULL) 23ccaeedb2SDavid van Moolenbroek return EINVAL; 24433d6423SLionel Sambuc 25ccaeedb2SDavid van Moolenbroek /* Look up the directory entry. */ 26ccaeedb2SDavid van Moolenbroek if ((rip = advance(dirp, name)) == NULL) 27ccaeedb2SDavid van Moolenbroek return err_code; 28433d6423SLionel Sambuc 29ccaeedb2SDavid van Moolenbroek /* On success, leave the resulting inode open and return its details. */ 30ccaeedb2SDavid van Moolenbroek node->fn_ino_nr = rip->i_num; 31ccaeedb2SDavid van Moolenbroek node->fn_mode = rip->i_mode; 32ccaeedb2SDavid van Moolenbroek node->fn_size = rip->i_size; 33ccaeedb2SDavid van Moolenbroek node->fn_uid = rip->i_uid; 34ccaeedb2SDavid van Moolenbroek node->fn_gid = rip->i_gid; 35433d6423SLionel Sambuc /* This is only valid for block and character specials. But it doesn't 36433d6423SLionel Sambuc * cause any harm to always set the device field. */ 37ccaeedb2SDavid van Moolenbroek node->fn_dev = (dev_t) rip->i_zone[0]; 38433d6423SLionel Sambuc 39ccaeedb2SDavid van Moolenbroek *is_mountpt = rip->i_mountpoint; 40433d6423SLionel Sambuc 41ccaeedb2SDavid van Moolenbroek return OK; 42433d6423SLionel Sambuc } 43433d6423SLionel Sambuc 44433d6423SLionel Sambuc 45433d6423SLionel Sambuc /*===========================================================================* 46433d6423SLionel Sambuc * advance * 47433d6423SLionel Sambuc *===========================================================================*/ 48ccaeedb2SDavid van Moolenbroek struct inode *advance(dirp, string) 49433d6423SLionel Sambuc struct inode *dirp; /* inode for directory to be searched */ 50ccaeedb2SDavid van Moolenbroek const char *string; /* component name to look for */ 51433d6423SLionel Sambuc { 52433d6423SLionel Sambuc /* Given a directory and a component of a path, look up the component in 53433d6423SLionel Sambuc * the directory, find the inode, open it, and return a pointer to its inode 54433d6423SLionel Sambuc * slot. 55433d6423SLionel Sambuc */ 56433d6423SLionel Sambuc ino_t numb; 57433d6423SLionel Sambuc struct inode *rip; 58433d6423SLionel Sambuc 59ccaeedb2SDavid van Moolenbroek assert(dirp != NULL); 60ccaeedb2SDavid van Moolenbroek 61433d6423SLionel Sambuc /* If 'string' is empty, return an error. */ 62433d6423SLionel Sambuc if (string[0] == '\0') { 63433d6423SLionel Sambuc err_code = ENOENT; 64433d6423SLionel Sambuc return(NULL); 65433d6423SLionel Sambuc } 66433d6423SLionel Sambuc 67ccaeedb2SDavid van Moolenbroek /* If dir has been removed return ENOENT. */ 68ccaeedb2SDavid van Moolenbroek if (dirp->i_nlinks == NO_LINK) { 69ccaeedb2SDavid van Moolenbroek err_code = ENOENT; 70ccaeedb2SDavid van Moolenbroek return(NULL); 71ccaeedb2SDavid van Moolenbroek } 72433d6423SLionel Sambuc 73433d6423SLionel Sambuc /* If 'string' is not present in the directory, signal error. */ 74ccaeedb2SDavid van Moolenbroek if ( (err_code = search_dir(dirp, string, &numb, LOOK_UP)) != OK) { 75433d6423SLionel Sambuc return(NULL); 76433d6423SLionel Sambuc } 77433d6423SLionel Sambuc 78433d6423SLionel Sambuc /* The component has been found in the directory. Get inode. */ 79433d6423SLionel Sambuc if ( (rip = get_inode(dirp->i_dev, (int) numb)) == NULL) { 80ccaeedb2SDavid van Moolenbroek assert(err_code != OK); 81433d6423SLionel Sambuc return(NULL); 82433d6423SLionel Sambuc } 83433d6423SLionel Sambuc 84ccaeedb2SDavid van Moolenbroek assert(err_code == OK); 85433d6423SLionel Sambuc return(rip); 86433d6423SLionel Sambuc } 87433d6423SLionel Sambuc 88433d6423SLionel Sambuc 89433d6423SLionel Sambuc /*===========================================================================* 90433d6423SLionel Sambuc * search_dir * 91433d6423SLionel Sambuc *===========================================================================*/ 92ccaeedb2SDavid van Moolenbroek int search_dir(ldir_ptr, string, numb, flag) 93433d6423SLionel Sambuc register struct inode *ldir_ptr; /* ptr to inode for dir to search */ 94ccaeedb2SDavid van Moolenbroek const char *string; /* component to search for */ 95433d6423SLionel Sambuc ino_t *numb; /* pointer to inode number */ 96433d6423SLionel Sambuc int flag; /* LOOK_UP, ENTER, DELETE or IS_EMPTY */ 97433d6423SLionel Sambuc { 98433d6423SLionel Sambuc /* This function searches the directory whose inode is pointed to by 'ldip': 99433d6423SLionel Sambuc * if (flag == ENTER) enter 'string' in the directory with inode # '*numb'; 100433d6423SLionel Sambuc * if (flag == DELETE) delete 'string' from the directory; 101433d6423SLionel Sambuc * if (flag == LOOK_UP) search for 'string' and return inode # in 'numb'; 102433d6423SLionel Sambuc * if (flag == IS_EMPTY) return OK if only . and .. in dir else ENOTEMPTY; 103433d6423SLionel Sambuc * 104ccaeedb2SDavid van Moolenbroek * This function, and this function alone, implements name truncation, 105ccaeedb2SDavid van Moolenbroek * by simply considering only the first MFS_NAME_MAX bytes from 'string'. 106433d6423SLionel Sambuc */ 107433d6423SLionel Sambuc register struct direct *dp = NULL; 108433d6423SLionel Sambuc register struct buf *bp = NULL; 109433d6423SLionel Sambuc int i, r, e_hit, t, match; 110433d6423SLionel Sambuc off_t pos; 111433d6423SLionel Sambuc unsigned new_slots, old_slots; 112433d6423SLionel Sambuc struct super_block *sp; 113433d6423SLionel Sambuc int extended = 0; 114433d6423SLionel Sambuc 115433d6423SLionel Sambuc /* If 'ldir_ptr' is not a pointer to a dir inode, error. */ 116433d6423SLionel Sambuc if ( (ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY) { 117433d6423SLionel Sambuc return(ENOTDIR); 118433d6423SLionel Sambuc } 119433d6423SLionel Sambuc 120433d6423SLionel Sambuc if((flag == DELETE || flag == ENTER) && ldir_ptr->i_sp->s_rd_only) 121433d6423SLionel Sambuc return EROFS; 122433d6423SLionel Sambuc 123433d6423SLionel Sambuc /* Step through the directory one block at a time. */ 124433d6423SLionel Sambuc old_slots = (unsigned) (ldir_ptr->i_size/DIR_ENTRY_SIZE); 125433d6423SLionel Sambuc new_slots = 0; 126433d6423SLionel Sambuc e_hit = FALSE; 127433d6423SLionel Sambuc match = 0; /* set when a string match occurs */ 128433d6423SLionel Sambuc 129433d6423SLionel Sambuc pos = 0; 130433d6423SLionel Sambuc if (flag == ENTER && ldir_ptr->i_last_dpos < ldir_ptr->i_size) { 131433d6423SLionel Sambuc pos = ldir_ptr->i_last_dpos; 132433d6423SLionel Sambuc new_slots = (unsigned) (pos/DIR_ENTRY_SIZE); 133433d6423SLionel Sambuc } 134433d6423SLionel Sambuc 135433d6423SLionel Sambuc for (; pos < ldir_ptr->i_size; pos += ldir_ptr->i_sp->s_block_size) { 136433d6423SLionel Sambuc assert(ldir_ptr->i_dev != NO_DEV); 137433d6423SLionel Sambuc 138433d6423SLionel Sambuc /* Since directories don't have holes, 'b' cannot be NO_BLOCK. */ 139433d6423SLionel Sambuc bp = get_block_map(ldir_ptr, pos); 140433d6423SLionel Sambuc 141433d6423SLionel Sambuc assert(ldir_ptr->i_dev != NO_DEV); 142433d6423SLionel Sambuc assert(bp != NULL); 143433d6423SLionel Sambuc assert(lmfs_dev(bp) != NO_DEV); 144433d6423SLionel Sambuc 145433d6423SLionel Sambuc /* Search a directory block. */ 146433d6423SLionel Sambuc for (dp = &b_dir(bp)[0]; 147433d6423SLionel Sambuc dp < &b_dir(bp)[NR_DIR_ENTRIES(ldir_ptr->i_sp->s_block_size)]; 148433d6423SLionel Sambuc dp++) { 149433d6423SLionel Sambuc if (++new_slots > old_slots) { /* not found, but room left */ 150433d6423SLionel Sambuc if (flag == ENTER) e_hit = TRUE; 151433d6423SLionel Sambuc break; 152433d6423SLionel Sambuc } 153433d6423SLionel Sambuc 154433d6423SLionel Sambuc /* Match occurs if string found. */ 155433d6423SLionel Sambuc if (flag != ENTER && dp->mfs_d_ino != NO_ENTRY) { 156433d6423SLionel Sambuc if (flag == IS_EMPTY) { 157433d6423SLionel Sambuc /* If this test succeeds, dir is not empty. */ 158433d6423SLionel Sambuc if (strcmp(dp->mfs_d_name, "." ) != 0 && 159ccaeedb2SDavid van Moolenbroek strcmp(dp->mfs_d_name, "..") != 0) 160ccaeedb2SDavid van Moolenbroek match = 1; 161433d6423SLionel Sambuc } else { 162433d6423SLionel Sambuc if (strncmp(dp->mfs_d_name, string, 163433d6423SLionel Sambuc sizeof(dp->mfs_d_name)) == 0){ 164433d6423SLionel Sambuc match = 1; 165433d6423SLionel Sambuc } 166433d6423SLionel Sambuc } 167433d6423SLionel Sambuc } 168433d6423SLionel Sambuc 169433d6423SLionel Sambuc if (match) { 170433d6423SLionel Sambuc /* LOOK_UP or DELETE found what it wanted. */ 171433d6423SLionel Sambuc r = OK; 172433d6423SLionel Sambuc if (flag == IS_EMPTY) r = ENOTEMPTY; 173433d6423SLionel Sambuc else if (flag == DELETE) { 174433d6423SLionel Sambuc /* Save d_ino for recovery. */ 175433d6423SLionel Sambuc t = MFS_NAME_MAX - sizeof(ino_t); 176433d6423SLionel Sambuc *((ino_t *) &dp->mfs_d_name[t]) = dp->mfs_d_ino; 177433d6423SLionel Sambuc dp->mfs_d_ino = NO_ENTRY; /* erase entry */ 178433d6423SLionel Sambuc MARKDIRTY(bp); 179433d6423SLionel Sambuc ldir_ptr->i_update |= CTIME | MTIME; 180433d6423SLionel Sambuc IN_MARKDIRTY(ldir_ptr); 181433d6423SLionel Sambuc if (pos < ldir_ptr->i_last_dpos) 182433d6423SLionel Sambuc ldir_ptr->i_last_dpos = pos; 183433d6423SLionel Sambuc } else { 184433d6423SLionel Sambuc sp = ldir_ptr->i_sp; /* 'flag' is LOOK_UP */ 185433d6423SLionel Sambuc *numb = (ino_t) conv4(sp->s_native, 186433d6423SLionel Sambuc (int) dp->mfs_d_ino); 187433d6423SLionel Sambuc } 188433d6423SLionel Sambuc assert(lmfs_dev(bp) != NO_DEV); 189*0314acfbSDavid van Moolenbroek put_block(bp); 190433d6423SLionel Sambuc return(r); 191433d6423SLionel Sambuc } 192433d6423SLionel Sambuc 193433d6423SLionel Sambuc /* Check for free slot for the benefit of ENTER. */ 194433d6423SLionel Sambuc if (flag == ENTER && dp->mfs_d_ino == 0) { 195433d6423SLionel Sambuc e_hit = TRUE; /* we found a free slot */ 196433d6423SLionel Sambuc break; 197433d6423SLionel Sambuc } 198433d6423SLionel Sambuc } 199433d6423SLionel Sambuc 200433d6423SLionel Sambuc /* The whole block has been searched or ENTER has a free slot. */ 201433d6423SLionel Sambuc if (e_hit) break; /* e_hit set if ENTER can be performed now */ 202433d6423SLionel Sambuc assert(lmfs_dev(bp) != NO_DEV); 203*0314acfbSDavid van Moolenbroek put_block(bp); /* otherwise, continue searching dir */ 204433d6423SLionel Sambuc } 205433d6423SLionel Sambuc 206433d6423SLionel Sambuc /* The whole directory has now been searched. */ 207433d6423SLionel Sambuc if (flag != ENTER) { 208433d6423SLionel Sambuc return(flag == IS_EMPTY ? OK : ENOENT); 209433d6423SLionel Sambuc } 210433d6423SLionel Sambuc 211433d6423SLionel Sambuc /* When ENTER next time, start searching for free slot from 212433d6423SLionel Sambuc * i_last_dpos. It gives some performance improvement (3-5%). 213433d6423SLionel Sambuc */ 214433d6423SLionel Sambuc ldir_ptr->i_last_dpos = pos; 215433d6423SLionel Sambuc 216433d6423SLionel Sambuc /* This call is for ENTER. If no free slot has been found so far, try to 217433d6423SLionel Sambuc * extend directory. 218433d6423SLionel Sambuc */ 219433d6423SLionel Sambuc if (e_hit == FALSE) { /* directory is full and no room left in last block */ 220433d6423SLionel Sambuc new_slots++; /* increase directory size by 1 entry */ 221433d6423SLionel Sambuc if (new_slots == 0) return(EFBIG); /* dir size limited by slot count */ 222433d6423SLionel Sambuc if ( (bp = new_block(ldir_ptr, ldir_ptr->i_size)) == NULL) 223433d6423SLionel Sambuc return(err_code); 224433d6423SLionel Sambuc dp = &b_dir(bp)[0]; 225433d6423SLionel Sambuc extended = 1; 226433d6423SLionel Sambuc } 227433d6423SLionel Sambuc 228433d6423SLionel Sambuc /* 'bp' now points to a directory block with space. 'dp' points to slot. */ 229433d6423SLionel Sambuc (void) memset(dp->mfs_d_name, 0, (size_t) MFS_NAME_MAX); /* clear entry */ 230433d6423SLionel Sambuc for (i = 0; i < MFS_NAME_MAX && string[i]; i++) dp->mfs_d_name[i] = string[i]; 231433d6423SLionel Sambuc sp = ldir_ptr->i_sp; 232433d6423SLionel Sambuc dp->mfs_d_ino = conv4(sp->s_native, (int) *numb); 233433d6423SLionel Sambuc MARKDIRTY(bp); 234*0314acfbSDavid van Moolenbroek put_block(bp); 235433d6423SLionel Sambuc ldir_ptr->i_update |= CTIME | MTIME; /* mark mtime for update later */ 236433d6423SLionel Sambuc IN_MARKDIRTY(ldir_ptr); 237433d6423SLionel Sambuc if (new_slots > old_slots) { 238433d6423SLionel Sambuc ldir_ptr->i_size = (off_t) new_slots * DIR_ENTRY_SIZE; 239433d6423SLionel Sambuc /* Send the change to disk if the directory is extended. */ 240433d6423SLionel Sambuc if (extended) rw_inode(ldir_ptr, WRITING); 241433d6423SLionel Sambuc } 242433d6423SLionel Sambuc return(OK); 243433d6423SLionel Sambuc } 244433d6423SLionel Sambuc 245