vfslookup.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * VFS operations relating to pathname translation
  31. */
  32. #include <types.h>
  33. #include <kern/errno.h>
  34. #include <limits.h>
  35. #include <lib.h>
  36. #include <synch.h>
  37. #include <vfs.h>
  38. #include <fs.h>
  39. #include <vnode.h>
  40. static struct vnode *bootfs_vnode = NULL;
  41. /*
  42. * Helper function for actually changing bootfs_vnode.
  43. */
  44. static
  45. void
  46. change_bootfs(struct vnode *newvn)
  47. {
  48. struct vnode *oldvn;
  49. oldvn = bootfs_vnode;
  50. bootfs_vnode = newvn;
  51. if (oldvn != NULL) {
  52. VOP_DECREF(oldvn);
  53. }
  54. }
  55. /*
  56. * Set bootfs_vnode.
  57. *
  58. * Bootfs_vnode is the vnode used for beginning path translation of
  59. * pathnames starting with /.
  60. *
  61. * It is also incidentally the system's first current directory.
  62. */
  63. int
  64. vfs_setbootfs(const char *fsname)
  65. {
  66. char tmp[NAME_MAX+1];
  67. char *s;
  68. int result;
  69. struct vnode *newguy;
  70. vfs_biglock_acquire();
  71. snprintf(tmp, sizeof(tmp)-1, "%s", fsname);
  72. s = strchr(tmp, ':');
  73. if (s) {
  74. /* If there's a colon, it must be at the end */
  75. if (strlen(s)>0) {
  76. vfs_biglock_release();
  77. return EINVAL;
  78. }
  79. }
  80. else {
  81. strcat(tmp, ":");
  82. }
  83. result = vfs_chdir(tmp);
  84. if (result) {
  85. vfs_biglock_release();
  86. return result;
  87. }
  88. result = vfs_getcurdir(&newguy);
  89. if (result) {
  90. vfs_biglock_release();
  91. return result;
  92. }
  93. change_bootfs(newguy);
  94. vfs_biglock_release();
  95. return 0;
  96. }
  97. /*
  98. * Clear the bootfs vnode (preparatory to system shutdown).
  99. */
  100. void
  101. vfs_clearbootfs(void)
  102. {
  103. vfs_biglock_acquire();
  104. change_bootfs(NULL);
  105. vfs_biglock_release();
  106. }
  107. /*
  108. * Common code to pull the device name, if any, off the front of a
  109. * path and choose the vnode to begin the name lookup relative to.
  110. */
  111. static
  112. int
  113. getdevice(char *path, char **subpath, struct vnode **startvn)
  114. {
  115. int slash=-1, colon=-1, i;
  116. struct vnode *vn;
  117. int result;
  118. KASSERT(vfs_biglock_do_i_hold());
  119. /*
  120. * Locate the first colon or slash.
  121. */
  122. for (i=0; path[i]; i++) {
  123. if (path[i]==':') {
  124. colon = i;
  125. break;
  126. }
  127. if (path[i]=='/') {
  128. slash = i;
  129. break;
  130. }
  131. }
  132. if (colon < 0 && slash != 0) {
  133. /*
  134. * No colon before a slash, so no device name
  135. * specified, and the slash isn't leading or is also
  136. * absent, so this is a relative path or just a bare
  137. * filename. Start from the current directory, and
  138. * use the whole thing as the subpath.
  139. */
  140. *subpath = path;
  141. return vfs_getcurdir(startvn);
  142. }
  143. if (colon>0) {
  144. /* device:path - get root of device's filesystem */
  145. path[colon]=0;
  146. while (path[colon+1]=='/') {
  147. /* device:/path - skip slash, treat as device:path */
  148. colon++;
  149. }
  150. *subpath = &path[colon+1];
  151. result = vfs_getroot(path, startvn);
  152. if (result) {
  153. return result;
  154. }
  155. return 0;
  156. }
  157. /*
  158. * We have either /path or :path.
  159. *
  160. * /path is a path relative to the root of the "boot filesystem".
  161. * :path is a path relative to the root of the current filesystem.
  162. */
  163. KASSERT(colon==0 || slash==0);
  164. if (path[0]=='/') {
  165. if (bootfs_vnode==NULL) {
  166. return ENOENT;
  167. }
  168. VOP_INCREF(bootfs_vnode);
  169. *startvn = bootfs_vnode;
  170. }
  171. else {
  172. KASSERT(path[0]==':');
  173. result = vfs_getcurdir(&vn);
  174. if (result) {
  175. return result;
  176. }
  177. /*
  178. * The current directory may not be a device, so it
  179. * must have a fs.
  180. */
  181. KASSERT(vn->vn_fs!=NULL);
  182. *startvn = FSOP_GETROOT(vn->vn_fs);
  183. VOP_DECREF(vn);
  184. }
  185. while (path[1]=='/') {
  186. /* ///... or :/... */
  187. path++;
  188. }
  189. *subpath = path+1;
  190. return 0;
  191. }
  192. /*
  193. * Name-to-vnode translation.
  194. * (In BSD, both of these are subsumed by namei().)
  195. */
  196. int
  197. vfs_lookparent(char *path, struct vnode **retval,
  198. char *buf, size_t buflen)
  199. {
  200. struct vnode *startvn;
  201. int result;
  202. vfs_biglock_acquire();
  203. result = getdevice(path, &path, &startvn);
  204. if (result) {
  205. vfs_biglock_release();
  206. return result;
  207. }
  208. if (strlen(path)==0) {
  209. /*
  210. * It does not make sense to use just a device name in
  211. * a context where "lookparent" is the desired
  212. * operation.
  213. */
  214. result = EINVAL;
  215. }
  216. else {
  217. result = VOP_LOOKPARENT(startvn, path, retval, buf, buflen);
  218. }
  219. VOP_DECREF(startvn);
  220. vfs_biglock_release();
  221. return result;
  222. }
  223. int
  224. vfs_lookup(char *path, struct vnode **retval)
  225. {
  226. struct vnode *startvn;
  227. int result;
  228. vfs_biglock_acquire();
  229. result = getdevice(path, &path, &startvn);
  230. if (result) {
  231. vfs_biglock_release();
  232. return result;
  233. }
  234. if (strlen(path)==0) {
  235. *retval = startvn;
  236. vfs_biglock_release();
  237. return 0;
  238. }
  239. result = VOP_LOOKUP(startvn, path, retval);
  240. VOP_DECREF(startvn);
  241. vfs_biglock_release();
  242. return result;
  243. }