ublock.h revision 2045e6e39d6dbfed1e46c333c2910c66e788cd36
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef UBLOCK_UBLOCK_H
18#define UBLOCK_UBLOCK_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#define UBLOCK_VERSION 0
24
25struct ublock_ctx;
26
27struct ublock_ops {
28  int (*read)(char *buf, uint64_t length, uint64_t offset);
29  int (*write)(const char *buf, uint64_t length, uint64_t offset);
30};
31
32/*
33 * Initializes a ublock block device.
34 *
35 * May call your read and write functions as the kernel scans partition
36 * tables.  Will return once the kernel has finished adding the block device.
37 *
38 * Returns 0 on success,
39 *   -ENOMEM if we ran out of memory,
40 *   -ENOENT if the kernel appears to lack ublock support, and
41 *   -EPROTO if the kernel did something unexpected.
42 */
43int ublock_init(struct ublock_ctx **ub, struct ublock_ops *ops, uint64_t size);
44
45/*
46 * Returns the index of a ublock block device.
47 *
48 * Returns -EFAULT if the context is NULL,
49 *         -EINVAL if the context is invalid.
50 */
51int ublock_index(struct ublock_ctx *ub);
52
53/*
54 * Runs a loop waiting for ublock requests and calling the ops callbacks.
55 *
56 * Returns 0 if the loop was stopped by ublock_stop,
57 *   -EPROTO if the loop was stopped by a protocol error,
58 */
59int ublock_run(struct ublock_ctx *ub);
60
61/*
62 * Stops ublock_run, if it is running.  Should be called from one of the ops
63 * callbacks.
64 */
65void ublock_stop(struct ublock_ctx *ub);
66
67/*
68 * Destroys a ublock block device.
69 */
70void ublock_destroy(struct ublock_ctx *ub);
71
72#endif
73