path.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* This file contains the procedures that look up path names in the directory
  2. * system and determine the inode number that goes with a given path name.
  3. */
  4. #include "fs.h"
  5. #include <assert.h>
  6. #include <string.h>
  7. #include "buf.h"
  8. #include "inode.h"
  9. #include "super.h"
  10. /*===========================================================================*
  11. * fs_lookup *
  12. *===========================================================================*/
  13. int fs_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node,
  14. int *is_mountpt)
  15. {
  16. struct inode *dirp, *rip;
  17. /* Find the starting inode. */
  18. if ((dirp = find_inode(fs_dev, dir_nr)) == NULL)
  19. return EINVAL;
  20. /* Look up the directory entry. */
  21. if ((rip = advance(dirp, name)) == NULL)
  22. return err_code;
  23. /* On success, leave the resulting inode open and return its details. */
  24. node->fn_ino_nr = rip->i_num;
  25. node->fn_mode = rip->i_mode;
  26. node->fn_size = rip->i_size;
  27. node->fn_uid = rip->i_uid;
  28. node->fn_gid = rip->i_gid;
  29. /* This is only valid for block and character specials. But it doesn't
  30. * cause any harm to always set the device field. */
  31. node->fn_dev = (dev_t) rip->i_zone[0];
  32. *is_mountpt = rip->i_mountpoint;
  33. return OK;
  34. }
  35. /*===========================================================================*
  36. * advance *
  37. *===========================================================================*/
  38. struct inode *advance(dirp, string)
  39. struct inode *dirp; /* inode for directory to be searched */
  40. const char *string; /* component name to look for */
  41. {
  42. /* Given a directory and a component of a path, look up the component in
  43. * the directory, find the inode, open it, and return a pointer to its inode
  44. * slot.
  45. */
  46. ino_t numb;
  47. struct inode *rip;
  48. assert(dirp != NULL);
  49. /* If 'string' is empty, return an error. */
  50. if (string[0] == '\0') {
  51. err_code = ENOENT;
  52. return(NULL);
  53. }
  54. /* If dir has been removed return ENOENT. */
  55. if (dirp->i_nlinks == NO_LINK) {
  56. err_code = ENOENT;
  57. return(NULL);
  58. }
  59. /* If 'string' is not present in the directory, signal error. */
  60. if ( (err_code = search_dir(dirp, string, &numb, LOOK_UP)) != OK) {
  61. return(NULL);
  62. }
  63. /* The component has been found in the directory. Get inode. */
  64. if ( (rip = get_inode(dirp->i_dev, (int) numb)) == NULL) {
  65. assert(err_code != OK);
  66. return(NULL);
  67. }
  68. assert(err_code == OK);
  69. return(rip);
  70. }
  71. /*===========================================================================*
  72. * search_dir *
  73. *===========================================================================*/
  74. int search_dir(ldir_ptr, string, numb, flag)
  75. register struct inode *ldir_ptr; /* ptr to inode for dir to search */
  76. const char *string; /* component to search for */
  77. ino_t *numb; /* pointer to inode number */
  78. int flag; /* LOOK_UP, ENTER, DELETE or IS_EMPTY */
  79. {
  80. /* This function searches the directory whose inode is pointed to by 'ldip':
  81. * if (flag == ENTER) enter 'string' in the directory with inode # '*numb';
  82. * if (flag == DELETE) delete 'string' from the directory;
  83. * if (flag == LOOK_UP) search for 'string' and return inode # in 'numb';
  84. * if (flag == IS_EMPTY) return OK if only . and .. in dir else ENOTEMPTY;
  85. *
  86. * This function, and this function alone, implements name truncation,
  87. * by simply considering only the first MFS_NAME_MAX bytes from 'string'.
  88. */
  89. register struct direct *dp = NULL;
  90. register struct buf *bp = NULL;
  91. int i, r, e_hit, t, match;
  92. off_t pos;
  93. unsigned new_slots, old_slots;
  94. struct super_block *sp;
  95. int extended = 0;
  96. /* If 'ldir_ptr' is not a pointer to a dir inode, error. */
  97. if ( (ldir_ptr->i_mode & I_TYPE) != I_DIRECTORY) {
  98. return(ENOTDIR);
  99. }
  100. if((flag == DELETE || flag == ENTER) && ldir_ptr->i_sp->s_rd_only)
  101. return EROFS;
  102. /* Step through the directory one block at a time. */
  103. old_slots = (unsigned) (ldir_ptr->i_size/DIR_ENTRY_SIZE);
  104. new_slots = 0;
  105. e_hit = FALSE;
  106. match = 0; /* set when a string match occurs */
  107. pos = 0;
  108. if (flag == ENTER && ldir_ptr->i_last_dpos < ldir_ptr->i_size) {
  109. pos = ldir_ptr->i_last_dpos;
  110. new_slots = (unsigned) (pos/DIR_ENTRY_SIZE);
  111. }
  112. for (; pos < ldir_ptr->i_size; pos += ldir_ptr->i_sp->s_block_size) {
  113. assert(ldir_ptr->i_dev != NO_DEV);
  114. /* Since directories don't have holes, 'b' cannot be NO_BLOCK. */
  115. bp = get_block_map(ldir_ptr, pos);
  116. assert(ldir_ptr->i_dev != NO_DEV);
  117. assert(bp != NULL);
  118. /* Search a directory block. */
  119. for (dp = &b_dir(bp)[0];
  120. dp < &b_dir(bp)[NR_DIR_ENTRIES(ldir_ptr->i_sp->s_block_size)];
  121. dp++) {
  122. if (++new_slots > old_slots) { /* not found, but room left */
  123. if (flag == ENTER) e_hit = TRUE;
  124. break;
  125. }
  126. /* Match occurs if string found. */
  127. if (flag != ENTER && dp->mfs_d_ino != NO_ENTRY) {
  128. if (flag == IS_EMPTY) {
  129. /* If this test succeeds, dir is not empty. */
  130. if (strcmp(dp->mfs_d_name, "." ) != 0 &&
  131. strcmp(dp->mfs_d_name, "..") != 0)
  132. match = 1;
  133. } else {
  134. if (strncmp(dp->mfs_d_name, string,
  135. sizeof(dp->mfs_d_name)) == 0){
  136. match = 1;
  137. }
  138. }
  139. }
  140. if (match) {
  141. /* LOOK_UP or DELETE found what it wanted. */
  142. r = OK;
  143. if (flag == IS_EMPTY) r = ENOTEMPTY;
  144. else if (flag == DELETE) {
  145. /* Save d_ino for recovery. */
  146. t = MFS_NAME_MAX - sizeof(ino_t);
  147. *((ino_t *) &dp->mfs_d_name[t]) = dp->mfs_d_ino;
  148. dp->mfs_d_ino = NO_ENTRY; /* erase entry */
  149. MARKDIRTY(bp);
  150. ldir_ptr->i_update |= CTIME | MTIME;
  151. IN_MARKDIRTY(ldir_ptr);
  152. if (pos < ldir_ptr->i_last_dpos)
  153. ldir_ptr->i_last_dpos = pos;
  154. } else {
  155. sp = ldir_ptr->i_sp; /* 'flag' is LOOK_UP */
  156. *numb = (ino_t) conv4(sp->s_native,
  157. (int) dp->mfs_d_ino);
  158. }
  159. put_block(bp);
  160. return(r);
  161. }
  162. /* Check for free slot for the benefit of ENTER. */
  163. if (flag == ENTER && dp->mfs_d_ino == 0) {
  164. e_hit = TRUE; /* we found a free slot */
  165. break;
  166. }
  167. }
  168. /* The whole block has been searched or ENTER has a free slot. */
  169. if (e_hit) break; /* e_hit set if ENTER can be performed now */
  170. put_block(bp); /* otherwise, continue searching dir */
  171. }
  172. /* The whole directory has now been searched. */
  173. if (flag != ENTER) {
  174. return(flag == IS_EMPTY ? OK : ENOENT);
  175. }
  176. /* When ENTER next time, start searching for free slot from
  177. * i_last_dpos. It gives some performance improvement (3-5%).
  178. */
  179. ldir_ptr->i_last_dpos = pos;
  180. /* This call is for ENTER. If no free slot has been found so far, try to
  181. * extend directory.
  182. */
  183. if (e_hit == FALSE) { /* directory is full and no room left in last block */
  184. new_slots++; /* increase directory size by 1 entry */
  185. if (new_slots == 0) return(EFBIG); /* dir size limited by slot count */
  186. if ( (bp = new_block(ldir_ptr, ldir_ptr->i_size)) == NULL)
  187. return(err_code);
  188. dp = &b_dir(bp)[0];
  189. extended = 1;
  190. }
  191. /* 'bp' now points to a directory block with space. 'dp' points to slot. */
  192. (void) memset(dp->mfs_d_name, 0, (size_t) MFS_NAME_MAX); /* clear entry */
  193. for (i = 0; i < MFS_NAME_MAX && string[i]; i++) dp->mfs_d_name[i] = string[i];
  194. sp = ldir_ptr->i_sp;
  195. dp->mfs_d_ino = conv4(sp->s_native, (int) *numb);
  196. MARKDIRTY(bp);
  197. put_block(bp);
  198. ldir_ptr->i_update |= CTIME | MTIME; /* mark mtime for update later */
  199. IN_MARKDIRTY(ldir_ptr);
  200. if (new_slots > old_slots) {
  201. ldir_ptr->i_size = (off_t) new_slots * DIR_ENTRY_SIZE;
  202. /* Send the change to disk if the directory is extended. */
  203. if (extended) rw_inode(ldir_ptr, WRITING);
  204. }
  205. return(OK);
  206. }