1#- 2# Copyright (c) 2006-2009 Marcel Moolenaar 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# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. 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# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25# 26# $FreeBSD$ 27 28#include <sys/param.h> 29#include <sys/lock.h> 30#include <sys/malloc.h> 31#include <sys/mutex.h> 32#include <sys/sbuf.h> 33#include <sys/bus.h> 34#include <machine/bus.h> 35#include <sys/systm.h> 36#include <geom/geom.h> 37#include <geom/part/g_part.h> 38 39# The G_PART scheme interface. 40 41INTERFACE g_part; 42 43# Default implementations of methods. 44CODE { 45 static void 46 default_fullname(struct g_part_table *table, 47 struct g_part_entry *entry, struct sbuf *sb, const char *pfx) 48 { 49 char buf[32]; 50 51 sbuf_printf(sb, "%s%s%s", pfx, g_part_separator, 52 G_PART_NAME(table, entry, buf, sizeof(buf))); 53 } 54 55 static struct g_provider * 56 default_new_provider(struct g_part_table *table, struct g_geom *gp, 57 struct g_part_entry *entry, const char *pfx) 58 { 59 struct g_provider *ret; 60 struct sbuf *sb; 61 62 sb = sbuf_new_auto(); 63 G_PART_FULLNAME(table, entry, sb, pfx); 64 sbuf_finish(sb); 65 ret = g_new_providerf(gp, "%s", sbuf_data(sb)); 66 sbuf_delete(sb); 67 return (ret); 68 } 69 70 static void 71 default_add_alias(struct g_part_table *table, struct g_provider *pp, 72 struct g_part_entry *entry, const char *pfx) 73 { 74 struct sbuf *sb; 75 76 sb = sbuf_new_auto(); 77 G_PART_FULLNAME(table, entry, sb, pfx); 78 sbuf_finish(sb); 79 g_provider_add_alias(pp, "%s", sbuf_data(sb)); 80 sbuf_delete(sb); 81 } 82 83 static int 84 default_precheck(struct g_part_table *t __unused, 85 enum g_part_ctl r __unused, struct g_part_parms *p __unused) 86 { 87 return (0); 88 } 89 90 static int 91 default_resize(struct g_part_table *t __unused, 92 struct g_part_entry *e __unused, struct g_part_parms *p __unused) 93 { 94 return (ENOSYS); 95 } 96 97 static int 98 default_recover(struct g_part_table *t __unused) 99 { 100 return (ENOSYS); 101 } 102 103 static int 104 default_ioctl(struct g_part_table *table __unused, struct g_provider *pp __unused, 105 u_long cmd __unused, void *data __unused, int fflag __unused, 106 struct thread *td __unused) 107 { 108 return (ENOIOCTL); 109 } 110 111 static int 112 default_getattr(struct g_part_table *table __unused, 113 struct g_part_entry *entry __unused, struct bio *bp __unused) 114 { 115 return (ENOIOCTL); 116 } 117}; 118 119# add() - scheme specific processing for the add verb. 120METHOD int add { 121 struct g_part_table *table; 122 struct g_part_entry *entry; 123 struct g_part_parms *gpp; 124}; 125 126# add_alias() - Create aliases for the partition's provider with the given 127# alias prefixes. 128METHOD void add_alias { 129 struct g_part_table *table; 130 struct g_provider *pp; 131 struct g_part_entry *entry; 132 const char *pfx; 133} DEFAULT default_add_alias; 134 135# bootcode() - scheme specific processing for the bootcode verb. 136METHOD int bootcode { 137 struct g_part_table *table; 138 struct g_part_parms *gpp; 139}; 140 141# create() - scheme specific processing for the create verb. 142METHOD int create { 143 struct g_part_table *table; 144 struct g_part_parms *gpp; 145}; 146 147# destroy() - scheme specific processing for the destroy verb. 148METHOD int destroy { 149 struct g_part_table *table; 150 struct g_part_parms *gpp; 151}; 152 153# dumpconf() 154METHOD void dumpconf { 155 struct g_part_table *table; 156 struct g_part_entry *entry; 157 struct sbuf *sb; 158 const char *indent; 159}; 160 161# dumpto() - return whether the partiton can be used for kernel dumps. 162METHOD int dumpto { 163 struct g_part_table *table; 164 struct g_part_entry *entry; 165}; 166 167# fullname() - write the name of the given partition entry to the sbuf. 168METHOD void fullname { 169 struct g_part_table *table; 170 struct g_part_entry *entry; 171 struct sbuf *sb; 172 const char *pfx; 173} DEFAULT default_fullname; 174 175# ioctl() - implement historic ioctls, perhaps. 176METHOD int ioctl { 177 struct g_part_table *table; 178 struct g_provider *pp; 179 u_long cmd; 180 void *data; 181 int fflag; 182 struct thread *td; 183} DEFAULT default_ioctl; 184 185# modify() - scheme specific processing for the modify verb. 186METHOD int modify { 187 struct g_part_table *table; 188 struct g_part_entry *entry; 189 struct g_part_parms *gpp; 190}; 191 192# new_provider() - Create the partition's provider(s). 193METHOD struct g_provider * new_provider { 194 struct g_part_table *table; 195 struct g_geom *gp; 196 struct g_part_entry *entry; 197 const char *pfx; 198} DEFAULT default_new_provider; 199 200# resize() - scheme specific processing for the resize verb. 201METHOD int resize { 202 struct g_part_table *table; 203 struct g_part_entry *entry; 204 struct g_part_parms *gpp; 205} DEFAULT default_resize; 206 207# name() - return the name of the given partition entry. 208# Typical names are "p1", "s0" or "c". 209METHOD const char * name { 210 struct g_part_table *table; 211 struct g_part_entry *entry; 212 char *buf; 213 size_t bufsz; 214}; 215 216# precheck() - method to allow schemes to check the parameters given 217# to the mentioned ctl request. This only applies to the requests that 218# operate on a GEOM. In other words, it does not apply to the create 219# request. 220# It is allowed (intended actually) to change the parameters according 221# to the schemes needs before they are used. Returning an error will 222# terminate the request immediately. 223METHOD int precheck { 224 struct g_part_table *table; 225 enum g_part_ctl req; 226 struct g_part_parms *gpp; 227} DEFAULT default_precheck; 228 229# probe() - probe the provider attached to the given consumer for the 230# existence of the scheme implemented by the G_PART interface handler. 231METHOD int probe { 232 struct g_part_table *table; 233 struct g_consumer *cp; 234}; 235 236# read() - read the on-disk partition table into memory. 237METHOD int read { 238 struct g_part_table *table; 239 struct g_consumer *cp; 240}; 241 242# recover() - scheme specific processing for the recover verb. 243METHOD int recover { 244 struct g_part_table *table; 245} DEFAULT default_recover; 246 247# setunset() - set or unset partition entry attributes. 248METHOD int setunset { 249 struct g_part_table *table; 250 struct g_part_entry *entry; 251 const char *attrib; 252 unsigned int set; 253}; 254 255# type() - return a string representation of the partition type. 256# Preferably, the alias names. 257METHOD const char * type { 258 struct g_part_table *table; 259 struct g_part_entry *entry; 260 char *buf; 261 size_t bufsz; 262}; 263 264# write() - write the in-memory partition table to disk. 265METHOD int write { 266 struct g_part_table *table; 267 struct g_consumer *cp; 268}; 269 270# getattr() - get the specified attribute, if any 271METHOD int getattr { 272 struct g_part_table *table; 273 struct g_part_entry *entry; 274 struct bio *bp; 275} DEFAULT default_getattr; 276