geom.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2002 Poul-Henning Kamp
  5. * Copyright (c) 2002 Networks Associates Technology, Inc.
  6. * All rights reserved.
  7. *
  8. * This software was developed for the FreeBSD Project by Poul-Henning Kamp
  9. * and NAI Labs, the Security Research Division of Network Associates, Inc.
  10. * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
  11. * DARPA CHATS research program.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. The names of the authors may not be used to endorse or promote
  22. * products derived from this software without specific prior written
  23. * permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #ifndef _GEOM_GEOM_H_
  38. #define _GEOM_GEOM_H_
  39. #include <sys/lock.h>
  40. #include <sys/mutex.h>
  41. #include <sys/sx.h>
  42. #include <sys/queue.h>
  43. #include <sys/ioccom.h>
  44. #include <sys/conf.h>
  45. #include <sys/module.h>
  46. struct g_class;
  47. struct g_geom;
  48. struct g_consumer;
  49. struct g_provider;
  50. struct g_stat;
  51. struct thread;
  52. struct bio;
  53. struct sbuf;
  54. struct gctl_req;
  55. struct g_configargs;
  56. struct disk_zone_args;
  57. struct thread;
  58. typedef int g_config_t (struct g_configargs *ca);
  59. typedef void g_ctl_req_t (struct gctl_req *, struct g_class *cp, char const *verb);
  60. typedef int g_ctl_create_geom_t (struct gctl_req *, struct g_class *cp, struct g_provider *pp);
  61. typedef int g_ctl_destroy_geom_t (struct gctl_req *, struct g_class *cp, struct g_geom *gp);
  62. typedef int g_ctl_config_geom_t (struct gctl_req *, struct g_geom *gp, const char *verb);
  63. typedef void g_init_t (struct g_class *mp);
  64. typedef void g_fini_t (struct g_class *mp);
  65. typedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *, int flags);
  66. typedef int g_ioctl_t(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td);
  67. #define G_TF_NORMAL 0
  68. #define G_TF_INSIST 1
  69. #define G_TF_TRANSPARENT 2
  70. typedef int g_access_t (struct g_provider *, int, int, int);
  71. /* XXX: not sure about the thread arg */
  72. typedef void g_orphan_t (struct g_consumer *);
  73. typedef void g_start_t (struct bio *);
  74. typedef void g_spoiled_t (struct g_consumer *);
  75. typedef void g_attrchanged_t (struct g_consumer *, const char *attr);
  76. typedef void g_provgone_t (struct g_provider *);
  77. typedef void g_dumpconf_t (struct sbuf *, const char *indent, struct g_geom *,
  78. struct g_consumer *, struct g_provider *);
  79. typedef void g_resize_t(struct g_consumer *cp);
  80. /*
  81. * The g_class structure describes a transformation class. In other words
  82. * all BSD disklabel handlers share one g_class, all MBR handlers share
  83. * one common g_class and so on.
  84. * Certain operations are instantiated on the class, most notably the
  85. * taste and config_geom functions.
  86. */
  87. struct g_class {
  88. const char *name;
  89. u_int version;
  90. u_int spare0;
  91. g_taste_t *taste;
  92. g_ctl_req_t *ctlreq;
  93. g_init_t *init;
  94. g_fini_t *fini;
  95. g_ctl_destroy_geom_t *destroy_geom;
  96. /*
  97. * Default values for geom methods
  98. */
  99. g_start_t *start;
  100. g_spoiled_t *spoiled;
  101. g_attrchanged_t *attrchanged;
  102. g_dumpconf_t *dumpconf;
  103. g_access_t *access;
  104. g_orphan_t *orphan;
  105. g_ioctl_t *ioctl;
  106. g_provgone_t *providergone;
  107. g_resize_t *resize;
  108. void *spare1;
  109. void *spare2;
  110. /*
  111. * The remaining elements are private
  112. */
  113. LIST_ENTRY(g_class) class;
  114. LIST_HEAD(,g_geom) geom;
  115. };
  116. #define G_VERSION_00 0x19950323
  117. #define G_VERSION_01 0x20041207 /* add fflag to g_ioctl_t */
  118. #define G_VERSION G_VERSION_01
  119. /*
  120. * The g_geom is an instance of a g_class.
  121. */
  122. struct g_geom {
  123. char *name;
  124. struct g_class *class;
  125. LIST_ENTRY(g_geom) geom;
  126. LIST_HEAD(,g_consumer) consumer;
  127. LIST_HEAD(,g_provider) provider;
  128. TAILQ_ENTRY(g_geom) geoms; /* XXX: better name */
  129. int rank;
  130. g_start_t *start;
  131. g_spoiled_t *spoiled;
  132. g_attrchanged_t *attrchanged;
  133. g_dumpconf_t *dumpconf;
  134. g_access_t *access;
  135. g_orphan_t *orphan;
  136. g_ioctl_t *ioctl;
  137. g_provgone_t *providergone;
  138. g_resize_t *resize;
  139. void *spare0;
  140. void *spare1;
  141. void *softc;
  142. unsigned flags;
  143. #define G_GEOM_WITHER 0x01
  144. #define G_GEOM_VOLATILE_BIO 0x02
  145. #define G_GEOM_IN_ACCESS 0x04
  146. #define G_GEOM_ACCESS_WAIT 0x08
  147. };
  148. /*
  149. * The g_bioq is a queue of struct bio's.
  150. * XXX: possibly collection point for statistics.
  151. * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
  152. */
  153. struct g_bioq {
  154. TAILQ_HEAD(, bio) bio_queue;
  155. struct mtx bio_queue_lock;
  156. int bio_queue_length;
  157. };
  158. /*
  159. * A g_consumer is an attachment point for a g_provider. One g_consumer
  160. * can only be attached to one g_provider, but multiple g_consumers
  161. * can be attached to one g_provider.
  162. */
  163. struct g_consumer {
  164. struct g_geom *geom;
  165. LIST_ENTRY(g_consumer) consumer;
  166. struct g_provider *provider;
  167. LIST_ENTRY(g_consumer) consumers; /* XXX: better name */
  168. int acr, acw, ace;
  169. int flags;
  170. #define G_CF_SPOILED 0x1
  171. #define G_CF_ORPHAN 0x4
  172. #define G_CF_DIRECT_SEND 0x10
  173. #define G_CF_DIRECT_RECEIVE 0x20
  174. struct devstat *stat;
  175. u_int nstart, nend;
  176. /* Two fields for the implementing class to use */
  177. void *private;
  178. u_int index;
  179. };
  180. /*
  181. * The g_geom_alias is a list node for aliases for the provider name for device
  182. * node creation.
  183. */
  184. struct g_geom_alias {
  185. LIST_ENTRY(g_geom_alias) ga_next;
  186. const char *ga_alias;
  187. };
  188. /*
  189. * A g_provider is a "logical disk".
  190. */
  191. struct g_provider {
  192. char *name;
  193. LIST_ENTRY(g_provider) provider;
  194. struct g_geom *geom;
  195. LIST_HEAD(,g_consumer) consumers;
  196. int acr, acw, ace;
  197. int error;
  198. TAILQ_ENTRY(g_provider) orphan;
  199. off_t mediasize;
  200. u_int sectorsize;
  201. off_t stripesize;
  202. off_t stripeoffset;
  203. struct devstat *stat;
  204. u_int spare1;
  205. u_int spare2;
  206. u_int flags;
  207. #define G_PF_WITHER 0x2
  208. #define G_PF_ORPHAN 0x4
  209. #define G_PF_ACCEPT_UNMAPPED 0x8
  210. #define G_PF_DIRECT_SEND 0x10
  211. #define G_PF_DIRECT_RECEIVE 0x20
  212. LIST_HEAD(,g_geom_alias) aliases;
  213. /* Two fields for the implementing class to use */
  214. void *private;
  215. u_int index;
  216. };
  217. /* BIO_GETATTR("GEOM::setstate") argument values. */
  218. #define G_STATE_FAILED 0
  219. #define G_STATE_REBUILD 1
  220. #define G_STATE_RESYNC 2
  221. #define G_STATE_ACTIVE 3
  222. /* geom_dev.c */
  223. struct cdev;
  224. void g_dev_print(void);
  225. void g_dev_physpath_changed(void);
  226. struct g_provider *g_dev_getprovider(struct cdev *dev);
  227. /* geom_dump.c */
  228. void (g_trace)(int level, const char *, ...) __printflike(2, 3);
  229. #define G_T_TOPOLOGY 0x01
  230. #define G_T_BIO 0x02
  231. #define G_T_ACCESS 0x04
  232. extern int g_debugflags;
  233. #define G_F_FOOTSHOOTING 0x10
  234. #define G_F_DISKIOCTL 0x40
  235. #define G_F_CTLDUMP 0x80
  236. #define g_trace(level, fmt, ...) do { \
  237. if (__predict_false(g_debugflags & (level))) \
  238. (g_trace)(level, fmt, ## __VA_ARGS__); \
  239. } while (0)
  240. /* geom_event.c */
  241. typedef void g_event_t(void *, int flag);
  242. struct g_event;
  243. #define EV_CANCEL 1
  244. int g_post_event(g_event_t *func, void *arg, int flag, ...);
  245. int g_waitfor_event(g_event_t *func, void *arg, int flag, ...);
  246. void g_cancel_event(void *ref);
  247. int g_attr_changed(struct g_provider *pp, const char *attr, int flag);
  248. int g_media_changed(struct g_provider *pp, int flag);
  249. int g_media_gone(struct g_provider *pp, int flag);
  250. void g_orphan_provider(struct g_provider *pp, int error);
  251. struct g_event *g_alloc_event(int flag);
  252. void g_post_event_ep(g_event_t *func, void *arg, struct g_event *ep, ...);
  253. void g_waitidle(struct thread *td);
  254. /* geom_subr.c */
  255. int g_access(struct g_consumer *cp, int nread, int nwrite, int nexcl);
  256. int g_attach(struct g_consumer *cp, struct g_provider *pp);
  257. int g_compare_names(const char *namea, const char *nameb);
  258. void g_destroy_consumer(struct g_consumer *cp);
  259. void g_destroy_geom(struct g_geom *pp);
  260. void g_destroy_provider(struct g_provider *pp);
  261. void g_detach(struct g_consumer *cp);
  262. void g_error_provider(struct g_provider *pp, int error);
  263. struct g_provider *g_provider_by_name(char const *arg);
  264. int g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
  265. #define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
  266. int g_handleattr(struct bio *bp, const char *attribute, const void *val,
  267. int len);
  268. int g_handleattr_int(struct bio *bp, const char *attribute, int val);
  269. int g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val);
  270. int g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val);
  271. int g_handleattr_str(struct bio *bp, const char *attribute, const char *str);
  272. struct g_consumer * g_new_consumer(struct g_geom *gp);
  273. struct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...)
  274. __printflike(2, 3);
  275. struct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...)
  276. __printflike(2, 3);
  277. void g_provider_add_alias(struct g_provider *pp, const char *fmt, ...)
  278. __printflike(2, 3);
  279. void g_resize_provider(struct g_provider *pp, off_t size);
  280. int g_retaste(struct g_class *mp);
  281. void g_spoil(struct g_provider *pp, struct g_consumer *cp);
  282. int g_std_access(struct g_provider *pp, int dr, int dw, int de);
  283. void g_std_done(struct bio *bp);
  284. void g_std_spoiled(struct g_consumer *cp);
  285. void g_wither_geom(struct g_geom *gp, int error);
  286. void g_wither_geom_close(struct g_geom *gp, int error);
  287. void g_wither_provider(struct g_provider *pp, int error);
  288. #if defined(DIAGNOSTIC) || defined(DDB)
  289. int g_valid_obj(void const *ptr);
  290. #endif
  291. #ifdef DIAGNOSTIC
  292. #define G_VALID_CLASS(foo) \
  293. KASSERT(g_valid_obj(foo) == 1, ("%p is not a g_class", foo))
  294. #define G_VALID_GEOM(foo) \
  295. KASSERT(g_valid_obj(foo) == 2, ("%p is not a g_geom", foo))
  296. #define G_VALID_CONSUMER(foo) \
  297. KASSERT(g_valid_obj(foo) == 3, ("%p is not a g_consumer", foo))
  298. #define G_VALID_PROVIDER(foo) \
  299. KASSERT(g_valid_obj(foo) == 4, ("%p is not a g_provider", foo))
  300. #else
  301. #define G_VALID_CLASS(foo) do { } while (0)
  302. #define G_VALID_GEOM(foo) do { } while (0)
  303. #define G_VALID_CONSUMER(foo) do { } while (0)
  304. #define G_VALID_PROVIDER(foo) do { } while (0)
  305. #endif
  306. int g_modevent(module_t, int, void *);
  307. /* geom_io.c */
  308. struct bio * g_clone_bio(struct bio *);
  309. struct bio * g_duplicate_bio(struct bio *);
  310. void g_destroy_bio(struct bio *);
  311. void g_io_deliver(struct bio *bp, int error);
  312. int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
  313. int g_io_zonecmd(struct disk_zone_args *zone_args, struct g_consumer *cp);
  314. int g_io_flush(struct g_consumer *cp);
  315. int g_io_speedup(off_t shortage, u_int flags, size_t *resid,
  316. struct g_consumer *cp);
  317. void g_io_request(struct bio *bp, struct g_consumer *cp);
  318. struct bio *g_new_bio(void);
  319. struct bio *g_alloc_bio(void);
  320. void g_reset_bio(struct bio *);
  321. void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
  322. int g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length);
  323. int g_delete_data(struct g_consumer *cp, off_t offset, off_t length);
  324. void g_format_bio(struct sbuf *, const struct bio *bp);
  325. void g_print_bio(const char *prefix, const struct bio *bp, const char *fmtsuffix, ...) __printflike(3, 4);
  326. int g_use_g_read_data(void *, off_t, void **, int);
  327. int g_use_g_write_data(void *, off_t, void *, int);
  328. /* geom_kern.c / geom_kernsim.c */
  329. #ifdef _KERNEL
  330. extern struct sx topology_lock;
  331. struct g_kerneldump {
  332. off_t offset;
  333. off_t length;
  334. struct dumperinfo di;
  335. };
  336. MALLOC_DECLARE(M_GEOM);
  337. static __inline void *
  338. g_malloc(int size, int flags)
  339. {
  340. void *p;
  341. p = malloc(size, M_GEOM, flags);
  342. return (p);
  343. }
  344. static __inline void
  345. g_free(void *ptr)
  346. {
  347. #ifdef DIAGNOSTIC
  348. if (sx_xlocked(&topology_lock)) {
  349. KASSERT(g_valid_obj(ptr) == 0,
  350. ("g_free(%p) of live object, type %d", ptr,
  351. g_valid_obj(ptr)));
  352. }
  353. #endif
  354. free(ptr, M_GEOM);
  355. }
  356. #define g_topology_lock() \
  357. do { \
  358. sx_xlock(&topology_lock); \
  359. } while (0)
  360. #define g_topology_try_lock() sx_try_xlock(&topology_lock)
  361. #define g_topology_unlock() \
  362. do { \
  363. sx_xunlock(&topology_lock); \
  364. } while (0)
  365. #define g_topology_locked() sx_xlocked(&topology_lock)
  366. #define g_topology_assert() \
  367. do { \
  368. sx_assert(&topology_lock, SX_XLOCKED); \
  369. } while (0)
  370. #define g_topology_assert_not() \
  371. do { \
  372. sx_assert(&topology_lock, SX_UNLOCKED); \
  373. } while (0)
  374. #define g_topology_sleep(chan, timo) \
  375. sx_sleep(chan, &topology_lock, 0, "gtopol", timo)
  376. #define DECLARE_GEOM_CLASS(class, name) \
  377. static moduledata_t name##_mod = { \
  378. #name, g_modevent, &class \
  379. }; \
  380. DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
  381. int g_is_geom_thread(struct thread *td);
  382. #ifndef _PATH_DEV
  383. #define _PATH_DEV "/dev/"
  384. #endif
  385. #endif /* _KERNEL */
  386. /* geom_ctl.c */
  387. int gctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len);
  388. void gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr, int len);
  389. void *gctl_get_param(struct gctl_req *req, const char *param, int *len);
  390. void *gctl_get_param_flags(struct gctl_req *req, const char *param, int flags, int *len);
  391. char const *gctl_get_asciiparam(struct gctl_req *req, const char *param);
  392. void *gctl_get_paraml(struct gctl_req *req, const char *param, int len);
  393. void *gctl_get_paraml_opt(struct gctl_req *req, const char *param, int len);
  394. int gctl_error(struct gctl_req *req, const char *fmt, ...) __printflike(2, 3);
  395. void gctl_msg(struct gctl_req *req, int, const char *fmt, ...) __printflike(3, 4);
  396. void gctl_post_messages(struct gctl_req *req);
  397. struct g_class *gctl_get_class(struct gctl_req *req, char const *arg);
  398. struct g_geom *gctl_get_geom(struct gctl_req *req, struct g_class *mp, char const *arg);
  399. struct g_provider *gctl_get_provider(struct gctl_req *req, char const *arg);
  400. #endif /* _GEOM_GEOM_H_ */