diskmap.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* $OpenBSD: diskmap.c,v 1.11 2015/05/07 08:53:33 mpi Exp $ */
  2. /*
  3. * Copyright (c) 2009, 2010 Joel Sing <jsing@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Disk mapper.
  19. */
  20. #include <sys/param.h>
  21. #include <sys/types.h>
  22. #include <sys/systm.h>
  23. #include <sys/device.h>
  24. #include <sys/errno.h>
  25. #include <sys/conf.h>
  26. #include <sys/dkio.h>
  27. #include <sys/disk.h>
  28. #include <sys/disklabel.h>
  29. #include <sys/file.h>
  30. #include <sys/filedesc.h>
  31. #include <sys/lock.h>
  32. #include <sys/malloc.h>
  33. #include <sys/namei.h>
  34. #include <sys/proc.h>
  35. #include <sys/vnode.h>
  36. int
  37. diskmapopen(dev_t dev, int flag, int fmt, struct proc *p)
  38. {
  39. return 0;
  40. }
  41. int
  42. diskmapclose(dev_t dev, int flag, int fmt, struct proc *p)
  43. {
  44. return 0;
  45. }
  46. int
  47. diskmapioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  48. {
  49. struct dk_diskmap *dm;
  50. struct nameidata ndp;
  51. struct filedesc *fdp;
  52. struct file *fp = NULL;
  53. struct vnode *vp = NULL, *ovp;
  54. char *devname;
  55. int fd, error = EINVAL;
  56. if (cmd != DIOCMAP)
  57. return EINVAL;
  58. /*
  59. * Map a request for a disk to the correct device. We should be
  60. * supplied with either a diskname or a disklabel UID.
  61. */
  62. dm = (struct dk_diskmap *)addr;
  63. fd = dm->fd;
  64. devname = malloc(PATH_MAX, M_DEVBUF, M_WAITOK);
  65. if (copyinstr(dm->device, devname, PATH_MAX, NULL))
  66. goto invalid;
  67. if (disk_map(devname, devname, PATH_MAX, dm->flags) == 0)
  68. if (copyoutstr(devname, dm->device, PATH_MAX, NULL))
  69. goto invalid;
  70. /* Attempt to open actual device. */
  71. if ((error = getvnode(p, fd, &fp)) != 0)
  72. goto invalid;
  73. fdp = p->p_fd;
  74. fdplock(fdp);
  75. ndp.ni_segflg = UIO_SYSSPACE;
  76. ndp.ni_dirfd = AT_FDCWD;
  77. ndp.ni_dirp = devname;
  78. ndp.ni_cnd.cn_proc = p;
  79. if ((error = vn_open(&ndp, fp->f_flag, 0)) != 0)
  80. goto bad;
  81. vp = ndp.ni_vp;
  82. /* Close the original vnode. */
  83. ovp = (struct vnode *)fp->f_data;
  84. if (fp->f_flag & FWRITE)
  85. ovp->v_writecount--;
  86. if (ovp->v_writecount == 0) {
  87. vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, p);
  88. VOP_CLOSE(ovp, fp->f_flag, p->p_ucred, p);
  89. vput(ovp);
  90. }
  91. fp->f_type = DTYPE_VNODE;
  92. fp->f_ops = &vnops;
  93. fp->f_data = (caddr_t)vp;
  94. fp->f_offset = 0;
  95. fp->f_rxfer = 0;
  96. fp->f_wxfer = 0;
  97. fp->f_seek = 0;
  98. fp->f_rbytes = 0;
  99. fp->f_wbytes = 0;
  100. VOP_UNLOCK(vp, 0, p);
  101. FRELE(fp, p);
  102. fdpunlock(fdp);
  103. free(devname, M_DEVBUF, 0);
  104. return 0;
  105. bad:
  106. if (vp)
  107. vput(vp);
  108. if (fp)
  109. FRELE(fp, p);
  110. fdpunlock(fdp);
  111. invalid:
  112. free(devname, M_DEVBUF, 0);
  113. return (error);
  114. }
  115. int
  116. diskmapread(dev_t dev, struct uio *uio, int flag)
  117. {
  118. return ENXIO;
  119. }
  120. int
  121. diskmapwrite(dev_t dev, struct uio *uio, int flag)
  122. {
  123. return ENXIO;
  124. }