1 | /*
|
---|
2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup volsrv
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Empty partition handling
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <block.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <label/empty.h>
|
---|
41 | #include <loc.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 |
|
---|
44 | #include "empty.h"
|
---|
45 |
|
---|
46 | static errno_t empty_get_bsize(void *, size_t *);
|
---|
47 | static errno_t empty_get_nblocks(void *, aoff64_t *);
|
---|
48 | static errno_t empty_read(void *, aoff64_t, size_t, void *);
|
---|
49 | static errno_t empty_write(void *, aoff64_t, size_t, const void *);
|
---|
50 |
|
---|
51 | /** Provide disk access to liblabel */
|
---|
52 | static label_bd_ops_t empty_bd_ops = {
|
---|
53 | .get_bsize = empty_get_bsize,
|
---|
54 | .get_nblocks = empty_get_nblocks,
|
---|
55 | .read = empty_read,
|
---|
56 | .write = empty_write
|
---|
57 | };
|
---|
58 |
|
---|
59 | errno_t volsrv_part_is_empty(service_id_t sid, bool *rempty)
|
---|
60 | {
|
---|
61 | errno_t rc;
|
---|
62 | label_bd_t lbd;
|
---|
63 |
|
---|
64 | rc = block_init(sid);
|
---|
65 | if (rc != EOK) {
|
---|
66 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error opening "
|
---|
67 | "block device service %zu", sid);
|
---|
68 | return EIO;
|
---|
69 | }
|
---|
70 |
|
---|
71 | lbd.ops = &empty_bd_ops;
|
---|
72 | lbd.arg = (void *)&sid;
|
---|
73 |
|
---|
74 | rc = label_bd_is_empty(&lbd, rempty);
|
---|
75 |
|
---|
76 | block_fini(sid);
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | errno_t volsrv_part_empty(service_id_t sid)
|
---|
81 | {
|
---|
82 | errno_t rc;
|
---|
83 | label_bd_t lbd;
|
---|
84 |
|
---|
85 | rc = block_init(sid);
|
---|
86 | if (rc != EOK) {
|
---|
87 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error opening "
|
---|
88 | "block device service %zu", sid);
|
---|
89 | return EIO;
|
---|
90 | }
|
---|
91 |
|
---|
92 | lbd.ops = &empty_bd_ops;
|
---|
93 | lbd.arg = (void *)&sid;
|
---|
94 |
|
---|
95 | rc = label_bd_empty(&lbd);
|
---|
96 |
|
---|
97 | block_fini(sid);
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** Get block size wrapper for liblabel */
|
---|
102 | static errno_t empty_get_bsize(void *arg, size_t *bsize)
|
---|
103 | {
|
---|
104 | service_id_t svc_id = *(service_id_t *)arg;
|
---|
105 | return block_get_bsize(svc_id, bsize);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Get number of blocks wrapper for liblabel */
|
---|
109 | static errno_t empty_get_nblocks(void *arg, aoff64_t *nblocks)
|
---|
110 | {
|
---|
111 | service_id_t svc_id = *(service_id_t *)arg;
|
---|
112 | return block_get_nblocks(svc_id, nblocks);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Read blocks wrapper for liblabel */
|
---|
116 | static errno_t empty_read(void *arg, aoff64_t ba, size_t cnt, void *buf)
|
---|
117 | {
|
---|
118 | service_id_t svc_id = *(service_id_t *)arg;
|
---|
119 | return block_read_direct(svc_id, ba, cnt, buf);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Write blocks wrapper for liblabel */
|
---|
123 | static errno_t empty_write(void *arg, aoff64_t ba, size_t cnt, const void *data)
|
---|
124 | {
|
---|
125 | service_id_t svc_id = *(service_id_t *)arg;
|
---|
126 | return block_write_direct(svc_id, ba, cnt, data);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** @}
|
---|
130 | */
|
---|