device.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. * Vnode operations for VFS devices.
  31. *
  32. * These hand off to the functions in the VFS device structure (see dev.h)
  33. * but take care of a bunch of common tasks in a uniform fashion.
  34. */
  35. #include <types.h>
  36. #include <kern/errno.h>
  37. #include <kern/fcntl.h>
  38. #include <stat.h>
  39. #include <lib.h>
  40. #include <uio.h>
  41. #include <synch.h>
  42. #include <vnode.h>
  43. #include <device.h>
  44. /*
  45. * Called for each open().
  46. *
  47. * We reject O_APPEND.
  48. */
  49. static
  50. int
  51. dev_open(struct vnode *v, int flags)
  52. {
  53. struct device *d = v->vn_data;
  54. if (flags & (O_CREAT | O_TRUNC | O_EXCL | O_APPEND)) {
  55. return EINVAL;
  56. }
  57. return d->d_open(d, flags);
  58. }
  59. /*
  60. * Called on the last close().
  61. * Just pass through.
  62. */
  63. static
  64. int
  65. dev_close(struct vnode *v)
  66. {
  67. struct device *d = v->vn_data;
  68. return d->d_close(d);
  69. }
  70. /*
  71. * Called when the vnode refcount reaches zero.
  72. * Do nothing; devices are permanent.
  73. */
  74. static
  75. int
  76. dev_reclaim(struct vnode *v)
  77. {
  78. (void)v;
  79. /* nothing - device continues to exist even when not in use */
  80. return 0;
  81. }
  82. /*
  83. * Called for read. Hand off to d_io.
  84. */
  85. static
  86. int
  87. dev_read(struct vnode *v, struct uio *uio)
  88. {
  89. struct device *d = v->vn_data;
  90. KASSERT(uio->uio_rw == UIO_READ);
  91. return d->d_io(d, uio);
  92. }
  93. /*
  94. * Used for several functions with the same type signature that are
  95. * not meaningful on devices.
  96. */
  97. static
  98. int
  99. null_io(struct vnode *v, struct uio *uio)
  100. {
  101. (void)v;
  102. (void)uio;
  103. return EINVAL;
  104. }
  105. /*
  106. * Called for write. Hand off to d_io.
  107. */
  108. static
  109. int
  110. dev_write(struct vnode *v, struct uio *uio)
  111. {
  112. struct device *d = v->vn_data;
  113. KASSERT(uio->uio_rw == UIO_WRITE);
  114. return d->d_io(d, uio);
  115. }
  116. /*
  117. * Called for ioctl(). Just pass through.
  118. */
  119. static
  120. int
  121. dev_ioctl(struct vnode *v, int op, userptr_t data)
  122. {
  123. struct device *d = v->vn_data;
  124. return d->d_ioctl(d, op, data);
  125. }
  126. /*
  127. * Called for stat().
  128. * Set the type and the size (block devices only).
  129. * The link count for a device is always 1.
  130. */
  131. static
  132. int
  133. dev_stat(struct vnode *v, struct stat *statbuf)
  134. {
  135. struct device *d = v->vn_data;
  136. int result;
  137. bzero(statbuf, sizeof(struct stat));
  138. if (d->d_blocks > 0) {
  139. statbuf->st_size = d->d_blocks * d->d_blocksize;
  140. statbuf->st_blksize = d->d_blocksize;
  141. }
  142. else {
  143. statbuf->st_size = 0;
  144. }
  145. result = VOP_GETTYPE(v, &statbuf->st_mode);
  146. if (result) {
  147. return result;
  148. }
  149. /* Make up some plausible default permissions. */
  150. statbuf->st_mode |= 0600;
  151. statbuf->st_nlink = 1;
  152. statbuf->st_blocks = d->d_blocks;
  153. /* The device number this device sits on (in OS/161, it doesn't) */
  154. statbuf->st_dev = 0;
  155. /* The device number this device *is* */
  156. statbuf->st_rdev = d->d_devnumber;
  157. return 0;
  158. }
  159. /*
  160. * Return the type. A device is a "block device" if it has a known
  161. * length. A device that generates data in a stream is a "character
  162. * device".
  163. */
  164. static
  165. int
  166. dev_gettype(struct vnode *v, mode_t *ret)
  167. {
  168. struct device *d = v->vn_data;
  169. if (d->d_blocks > 0) {
  170. *ret = S_IFBLK;
  171. }
  172. else {
  173. *ret = S_IFCHR;
  174. }
  175. return 0;
  176. }
  177. /*
  178. * Attempt a seek.
  179. * For block devices, require block alignment.
  180. * For character devices, prohibit seeking entirely.
  181. */
  182. static
  183. int
  184. dev_tryseek(struct vnode *v, off_t pos)
  185. {
  186. struct device *d = v->vn_data;
  187. if (d->d_blocks > 0) {
  188. if ((pos % d->d_blocksize)!=0) {
  189. /* not block-aligned */
  190. return EINVAL;
  191. }
  192. if (pos < 0) {
  193. /*
  194. * Nonsensical.
  195. * (note: off_t must be signed for SEEK_CUR or
  196. * SEEK_END seeks to work, so this case must
  197. * be checked.)
  198. */
  199. return EINVAL;
  200. }
  201. if (pos / d->d_blocksize >= d->d_blocks) {
  202. /* off the end */
  203. return EINVAL;
  204. }
  205. }
  206. else {
  207. return ESPIPE;
  208. }
  209. return 0;
  210. }
  211. /*
  212. * For fsync() - meaningless, do nothing.
  213. */
  214. static
  215. int
  216. null_fsync(struct vnode *v)
  217. {
  218. (void)v;
  219. return 0;
  220. }
  221. /*
  222. * For mmap. If you want this to do anything, you have to write it
  223. * yourself. Some devices may not make sense to map. Others do.
  224. */
  225. static
  226. int
  227. dev_mmap(struct vnode *v /* add stuff as needed */)
  228. {
  229. (void)v;
  230. return EUNIMP;
  231. }
  232. /*
  233. * For ftruncate().
  234. */
  235. static
  236. int
  237. dev_truncate(struct vnode *v, off_t len)
  238. {
  239. struct device *d = v->vn_data;
  240. /*
  241. * Allow truncating to the object's own size, if it has one.
  242. */
  243. if (d->d_blocks > 0 && (off_t)(d->d_blocks*d->d_blocksize) == len) {
  244. return 0;
  245. }
  246. return EINVAL;
  247. }
  248. /*
  249. * For namefile (which implements "pwd")
  250. *
  251. * This should never be reached, as it's not possible to chdir to a
  252. * device vnode.
  253. */
  254. static
  255. int
  256. dev_namefile(struct vnode *v, struct uio *uio)
  257. {
  258. /*
  259. * The name of a device is always just "device:". The VFS
  260. * layer puts in the device name for us, so we don't need to
  261. * do anything further.
  262. */
  263. (void)v;
  264. (void)uio;
  265. return 0;
  266. }
  267. /*
  268. * Operations that are completely meaningless on devices.
  269. */
  270. static
  271. int
  272. null_creat(struct vnode *v, const char *name, bool excl, mode_t mode,
  273. struct vnode **result)
  274. {
  275. (void)v;
  276. (void)name;
  277. (void)excl;
  278. (void)mode;
  279. (void)result;
  280. return ENOTDIR;
  281. }
  282. static
  283. int
  284. null_mkdir(struct vnode *v, const char *name, mode_t mode)
  285. {
  286. (void)v;
  287. (void)name;
  288. (void)mode;
  289. return ENOTDIR;
  290. }
  291. static
  292. int
  293. null_symlink(struct vnode *v, const char *contents, const char *name)
  294. {
  295. (void)v;
  296. (void)contents;
  297. (void)name;
  298. return ENOTDIR;
  299. }
  300. static
  301. int
  302. null_nameop(struct vnode *v, const char *name)
  303. {
  304. (void)v;
  305. (void)name;
  306. return ENOTDIR;
  307. }
  308. static
  309. int
  310. null_link(struct vnode *v, const char *name, struct vnode *file)
  311. {
  312. (void)v;
  313. (void)name;
  314. (void)file;
  315. return ENOTDIR;
  316. }
  317. static
  318. int
  319. null_rename(struct vnode *v, const char *n1, struct vnode *v2, const char *n2)
  320. {
  321. (void)v;
  322. (void)n1;
  323. (void)v2;
  324. (void)n2;
  325. return ENOTDIR;
  326. }
  327. /*
  328. * Name lookup.
  329. *
  330. * One interesting feature of device:name pathname syntax is that you
  331. * can implement pathnames on arbitrary devices. For instance, if you
  332. * had a graphics device that supported multiple resolutions (which we
  333. * don't), you might arrange things so that you could open it with
  334. * pathnames like "video:800x600/24bpp" in order to select the operating
  335. * mode.
  336. *
  337. * However, we have no support for this in the base system.
  338. */
  339. static
  340. int
  341. dev_lookup(struct vnode *dir,
  342. char *pathname, struct vnode **result)
  343. {
  344. /*
  345. * If the path was "device:", we get "". For that, return self.
  346. * Anything else is an error.
  347. * Increment the ref count of the vnode before returning it.
  348. */
  349. if (strlen(pathname)>0) {
  350. return ENOENT;
  351. }
  352. VOP_INCREF(dir);
  353. *result = dir;
  354. return 0;
  355. }
  356. static
  357. int
  358. dev_lookparent(struct vnode *dir,
  359. char *pathname, struct vnode **result,
  360. char *namebuf, size_t buflen)
  361. {
  362. /*
  363. * This is always an error.
  364. */
  365. (void)dir;
  366. (void)pathname;
  367. (void)result;
  368. (void)namebuf;
  369. (void)buflen;
  370. return ENOTDIR;
  371. }
  372. /*
  373. * Function table for device vnodes.
  374. */
  375. static const struct vnode_ops dev_vnode_ops = {
  376. VOP_MAGIC,
  377. dev_open,
  378. dev_close,
  379. dev_reclaim,
  380. dev_read,
  381. null_io, /* readlink */
  382. null_io, /* getdirentry */
  383. dev_write,
  384. dev_ioctl,
  385. dev_stat,
  386. dev_gettype,
  387. dev_tryseek,
  388. null_fsync,
  389. dev_mmap,
  390. dev_truncate,
  391. dev_namefile,
  392. null_creat,
  393. null_symlink,
  394. null_mkdir,
  395. null_link,
  396. null_nameop, /* remove */
  397. null_nameop, /* rmdir */
  398. null_rename,
  399. dev_lookup,
  400. dev_lookparent,
  401. };
  402. /*
  403. * Function to create a vnode for a VFS device.
  404. */
  405. struct vnode *
  406. dev_create_vnode(struct device *dev)
  407. {
  408. int result;
  409. struct vnode *v;
  410. v = kmalloc(sizeof(struct vnode));
  411. if (v==NULL) {
  412. return NULL;
  413. }
  414. result = VOP_INIT(v, &dev_vnode_ops, NULL, dev);
  415. if (result != 0) {
  416. panic("While creating vnode for device: VOP_INIT: %s\n",
  417. strerror(result));
  418. }
  419. return v;
  420. }