16185db85Sdougm /* 26185db85Sdougm * CDDL HEADER START 36185db85Sdougm * 46185db85Sdougm * The contents of this file are subject to the terms of the 56185db85Sdougm * Common Development and Distribution License (the "License"). 66185db85Sdougm * You may not use this file except in compliance with the License. 76185db85Sdougm * 86185db85Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 96185db85Sdougm * or http://www.opensolaris.org/os/licensing. 106185db85Sdougm * See the License for the specific language governing permissions 116185db85Sdougm * and limitations under the License. 126185db85Sdougm * 136185db85Sdougm * When distributing Covered Code, include this CDDL HEADER in each 146185db85Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 156185db85Sdougm * If applicable, add the following below this CDDL HEADER, with the 166185db85Sdougm * fields enclosed by brackets "[]" replaced with your own identifying 176185db85Sdougm * information: Portions Copyright [yyyy] [name of copyright owner] 186185db85Sdougm * 196185db85Sdougm * CDDL HEADER END 206185db85Sdougm */ 216185db85Sdougm 226185db85Sdougm /* 2324424a35Sdougm * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 246185db85Sdougm * Use is subject to license terms. 256185db85Sdougm */ 266185db85Sdougm 276185db85Sdougm #pragma ident "%Z%%M% %I% %E% SMI" 286185db85Sdougm 296185db85Sdougm #include <stdio.h> 306185db85Sdougm #include <stdlib.h> 316185db85Sdougm #include <string.h> 326185db85Sdougm #include <libshare.h> 336185db85Sdougm #include "libshare_impl.h" 346185db85Sdougm #include <dlfcn.h> 356185db85Sdougm #include <link.h> 366185db85Sdougm #include <sys/types.h> 376185db85Sdougm #include <sys/param.h> 386185db85Sdougm #include <sys/stat.h> 396185db85Sdougm #include <dirent.h> 406185db85Sdougm #include <libintl.h> 41549ec3ffSdougm #include <sys/systeminfo.h> 42549ec3ffSdougm 43549ec3ffSdougm #define MAXISALEN 257 /* based on sysinfo(2) man page */ 446185db85Sdougm 456185db85Sdougm /* 466185db85Sdougm * protocol plugin interface 476185db85Sdougm * 486185db85Sdougm * finds plugins and makes them accessible. This is only "used" by 496185db85Sdougm * libshare.so. 506185db85Sdougm */ 516185db85Sdougm 526185db85Sdougm struct sa_proto_plugin *sap_proto_list; 536185db85Sdougm 546185db85Sdougm static struct sa_proto_handle sa_proto_handle; 556185db85Sdougm 566185db85Sdougm void proto_plugin_fini(); 576185db85Sdougm 586185db85Sdougm /* 596185db85Sdougm * proto_plugin_init() 606185db85Sdougm * 616185db85Sdougm * Initialize the protocol specific plugin modules. 626185db85Sdougm * 636185db85Sdougm * Walk /usr/lib/fs/\* for libshare_*.so modules. That is, 646185db85Sdougm * /usr/lib/fs/nfs/libshare_nfs.so. The protocol specific directory 656185db85Sdougm * would have a modules with name libshare_<proto>.so. If one is 666185db85Sdougm * found, initialize it and add to the internal list of 676185db85Sdougm * protocols. These are used for protocol specifici operations. 686185db85Sdougm */ 696185db85Sdougm 706185db85Sdougm int 716185db85Sdougm proto_plugin_init() 726185db85Sdougm { 736185db85Sdougm struct sa_proto_plugin *proto; 746185db85Sdougm int num_protos = 0; 756185db85Sdougm int err; 766185db85Sdougm struct sa_plugin_ops *plugin_ops; 776185db85Sdougm void *dlhandle; 786185db85Sdougm DIR *dir; 796185db85Sdougm struct dirent *dent; 806185db85Sdougm int ret = SA_OK; 816185db85Sdougm struct stat st; 826185db85Sdougm 836185db85Sdougm /* 84*25a68471Sdougm * Should walk "/usr/lib/fs/" for files of the form: 856185db85Sdougm * libshare_*.so 866185db85Sdougm */ 876185db85Sdougm dir = opendir(SA_LIB_DIR); 886185db85Sdougm if (dir != NULL) { 896185db85Sdougm while (ret == SA_OK && (dent = readdir(dir)) != NULL) { 906185db85Sdougm char path[MAXPATHLEN]; 91549ec3ffSdougm char isa[MAXISALEN]; 92549ec3ffSdougm 93549ec3ffSdougm #if defined(_LP64) 94549ec3ffSdougm if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 95549ec3ffSdougm isa[0] = '\0'; 96549ec3ffSdougm #else 97549ec3ffSdougm isa[0] = '\0'; 98549ec3ffSdougm #endif 996185db85Sdougm (void) snprintf(path, MAXPATHLEN, 100*25a68471Sdougm "%s/%s/%s/libshare_%s.so.1", SA_LIB_DIR, 101*25a68471Sdougm dent->d_name, isa, dent->d_name); 102*25a68471Sdougm /* 103*25a68471Sdougm * If file doesn't exist, don't try to map it 104*25a68471Sdougm */ 105*25a68471Sdougm if (stat(path, &st) < 0) 1066185db85Sdougm continue; 107*25a68471Sdougm 108549ec3ffSdougm dlhandle = dlopen(path, RTLD_FIRST|RTLD_LAZY); 1096185db85Sdougm if (dlhandle != NULL) { 1106185db85Sdougm plugin_ops = (struct sa_plugin_ops *) 1116185db85Sdougm dlsym(dlhandle, "sa_plugin_ops"); 1126185db85Sdougm proto = (struct sa_proto_plugin *) 1136185db85Sdougm calloc(1, sizeof (struct sa_proto_plugin)); 1146185db85Sdougm if (proto != NULL) { 1156185db85Sdougm proto->plugin_ops = plugin_ops; 1166185db85Sdougm proto->plugin_handle = dlhandle; 1176185db85Sdougm num_protos++; 1186185db85Sdougm proto->plugin_next = sap_proto_list; 1196185db85Sdougm sap_proto_list = proto; 1206185db85Sdougm } else { 1216185db85Sdougm ret = SA_NO_MEMORY; 1226185db85Sdougm } 1236185db85Sdougm } else { 1246185db85Sdougm (void) fprintf(stderr, 12524424a35Sdougm dgettext(TEXT_DOMAIN, 12624424a35Sdougm "Error in plugin for protocol %s: %s\n"), 1276185db85Sdougm dent->d_name, dlerror()); 1286185db85Sdougm } 1296185db85Sdougm } 1306185db85Sdougm (void) closedir(dir); 1316185db85Sdougm } 1326185db85Sdougm if (ret == SA_OK) { 1336185db85Sdougm sa_proto_handle.sa_proto = 1346185db85Sdougm (char **)calloc(num_protos, sizeof (char *)); 1356185db85Sdougm sa_proto_handle.sa_ops = 1366185db85Sdougm (struct sa_plugin_ops **)calloc(num_protos, 1376185db85Sdougm sizeof (struct sa_plugin_ops *)); 1386185db85Sdougm if (sa_proto_handle.sa_proto != NULL && 1396185db85Sdougm sa_proto_handle.sa_ops != NULL) { 1406185db85Sdougm int i; 1416185db85Sdougm struct sa_proto_plugin *tmp; 142*25a68471Sdougm 143*25a68471Sdougm for (i = 0, tmp = sap_proto_list; 144*25a68471Sdougm i < num_protos; 1456185db85Sdougm tmp = tmp->plugin_next) { 1466185db85Sdougm err = 0; 1476185db85Sdougm if (tmp->plugin_ops->sa_init != NULL) 1486185db85Sdougm err = tmp->plugin_ops->sa_init(); 1496185db85Sdougm if (err == SA_OK) { 150*25a68471Sdougm /* 151*25a68471Sdougm * Only include if the init 152*25a68471Sdougm * succeeded or was NULL 153*25a68471Sdougm */ 1546185db85Sdougm sa_proto_handle.sa_num_proto++; 155*25a68471Sdougm sa_proto_handle.sa_ops[i] = 156*25a68471Sdougm tmp->plugin_ops; 1576185db85Sdougm sa_proto_handle.sa_proto[i] = 1586185db85Sdougm tmp->plugin_ops->sa_protocol; 1596185db85Sdougm i++; 1606185db85Sdougm } 1616185db85Sdougm } 1626185db85Sdougm } 1636185db85Sdougm } else { 164*25a68471Sdougm /* 165*25a68471Sdougm * There was an error, so cleanup prior to return of failure. 166*25a68471Sdougm */ 1676185db85Sdougm proto_plugin_fini(); 1686185db85Sdougm } 1696185db85Sdougm return (ret); 1706185db85Sdougm } 1716185db85Sdougm 1726185db85Sdougm /* 1736185db85Sdougm * proto_plugin_fini() 1746185db85Sdougm * 175*25a68471Sdougm * Uninitialize all the plugin modules. 1766185db85Sdougm */ 1776185db85Sdougm 1786185db85Sdougm void 1796185db85Sdougm proto_plugin_fini() 1806185db85Sdougm { 1816185db85Sdougm /* 182*25a68471Sdougm * Free up all the protocols, calling their fini, if there is 1836185db85Sdougm * one. 1846185db85Sdougm */ 1856185db85Sdougm while (sap_proto_list != NULL) { 1866185db85Sdougm struct sa_proto_plugin *next; 187*25a68471Sdougm 1886185db85Sdougm next = sap_proto_list->plugin_next; 1896185db85Sdougm sap_proto_list->plugin_ops->sa_fini(); 1906185db85Sdougm if (sap_proto_list->plugin_handle != NULL) 1916185db85Sdougm (void) dlclose(sap_proto_list->plugin_handle); 1926185db85Sdougm free(sap_proto_list); 1936185db85Sdougm sap_proto_list = next; 1946185db85Sdougm } 1956185db85Sdougm if (sa_proto_handle.sa_ops != NULL) { 1966185db85Sdougm free(sa_proto_handle.sa_ops); 1976185db85Sdougm sa_proto_handle.sa_ops = NULL; 1986185db85Sdougm } 1996185db85Sdougm if (sa_proto_handle.sa_proto != NULL) { 2006185db85Sdougm free(sa_proto_handle.sa_proto); 2016185db85Sdougm sa_proto_handle.sa_proto = NULL; 2026185db85Sdougm } 2036185db85Sdougm sa_proto_handle.sa_num_proto = 0; 2046185db85Sdougm } 2056185db85Sdougm 2066185db85Sdougm /* 2076185db85Sdougm * find_protocol(proto) 2086185db85Sdougm * 2096185db85Sdougm * Search the plugin list for the specified protocol and return the 2106185db85Sdougm * ops vector. NULL if protocol is not defined. 2116185db85Sdougm */ 2126185db85Sdougm 2136185db85Sdougm static struct sa_plugin_ops * 2146185db85Sdougm find_protocol(char *proto) 2156185db85Sdougm { 2166185db85Sdougm int i; 2176185db85Sdougm 2186185db85Sdougm if (proto != NULL) { 2196185db85Sdougm for (i = 0; i < sa_proto_handle.sa_num_proto; i++) { 2206185db85Sdougm if (strcmp(proto, sa_proto_handle.sa_proto[i]) == 0) 2216185db85Sdougm return (sa_proto_handle.sa_ops[i]); 2226185db85Sdougm } 2236185db85Sdougm } 2246185db85Sdougm return (NULL); 2256185db85Sdougm } 2266185db85Sdougm 2276185db85Sdougm /* 2286185db85Sdougm * sa_proto_share(proto, share) 2296185db85Sdougm * 2306185db85Sdougm * Activate a share for the specified protocol. 2316185db85Sdougm */ 2326185db85Sdougm 2336185db85Sdougm int 2346185db85Sdougm sa_proto_share(char *proto, sa_share_t share) 2356185db85Sdougm { 2366185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 2376185db85Sdougm int ret = SA_INVALID_PROTOCOL; 2386185db85Sdougm 2396185db85Sdougm if (ops != NULL && ops->sa_share != NULL) 2406185db85Sdougm ret = ops->sa_share(share); 2416185db85Sdougm return (ret); 2426185db85Sdougm } 2436185db85Sdougm 2446185db85Sdougm /* 2456185db85Sdougm * sa_proto_unshare(proto, path) 2466185db85Sdougm * 2476185db85Sdougm * Deactivate (unshare) the path for this protocol. 2486185db85Sdougm */ 2496185db85Sdougm 2506185db85Sdougm int 251ecd6cf80Smarks sa_proto_unshare(sa_share_t share, char *proto, char *path) 2526185db85Sdougm { 2536185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 2546185db85Sdougm int ret = SA_INVALID_PROTOCOL; 2556185db85Sdougm 2566185db85Sdougm if (ops != NULL && ops->sa_unshare != NULL) 257ecd6cf80Smarks ret = ops->sa_unshare(share, path); 2586185db85Sdougm return (ret); 2596185db85Sdougm } 2606185db85Sdougm 2616185db85Sdougm /* 2626185db85Sdougm * sa_proto_valid_prop(proto, prop, opt) 2636185db85Sdougm * 264*25a68471Sdougm * Check to see if the specified prop is valid for this protocol. 2656185db85Sdougm */ 2666185db85Sdougm 2676185db85Sdougm int 2686185db85Sdougm sa_proto_valid_prop(char *proto, sa_property_t prop, sa_optionset_t opt) 2696185db85Sdougm { 2706185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 2716185db85Sdougm int ret = 0; 2726185db85Sdougm 2736185db85Sdougm if (ops != NULL && ops->sa_valid_prop != NULL) 2746185db85Sdougm ret = ops->sa_valid_prop(prop, opt); 2756185db85Sdougm return (ret); 2766185db85Sdougm } 2776185db85Sdougm 2786185db85Sdougm /* 2796185db85Sdougm * sa_proto_valid_space(proto, space) 2806185db85Sdougm * 281*25a68471Sdougm * Check if space is valid optionspace for proto. 2826185db85Sdougm * Protocols that don't implement this don't support spaces. 2836185db85Sdougm */ 2846185db85Sdougm int 2856185db85Sdougm sa_proto_valid_space(char *proto, char *token) 2866185db85Sdougm { 2876185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 2886185db85Sdougm int ret = 0; 2896185db85Sdougm 2906185db85Sdougm if (ops != NULL && ops->sa_valid_space != NULL) 2916185db85Sdougm ret = ops->sa_valid_space(token); 2926185db85Sdougm return (ret); 2936185db85Sdougm } 2946185db85Sdougm 2956185db85Sdougm /* 2966185db85Sdougm * sa_proto_space_alias(proto, space) 2976185db85Sdougm * 298*25a68471Sdougm * If the name for space is an alias, return its proper name. This is 2996185db85Sdougm * used to translate "default" values into proper form. 3006185db85Sdougm */ 3016185db85Sdougm char * 3026185db85Sdougm sa_proto_space_alias(char *proto, char *space) 3036185db85Sdougm { 3046185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 3056185db85Sdougm char *ret = space; 3066185db85Sdougm 3076185db85Sdougm if (ops != NULL && ops->sa_space_alias != NULL) 3086185db85Sdougm ret = ops->sa_space_alias(space); 3096185db85Sdougm return (ret); 3106185db85Sdougm } 3116185db85Sdougm 3126185db85Sdougm /* 3136185db85Sdougm * sa_proto_security_prop(proto, token) 3146185db85Sdougm * 3156185db85Sdougm * Check to see if the property name in token is a valid named 3166185db85Sdougm * optionset property. 3176185db85Sdougm */ 3186185db85Sdougm 3196185db85Sdougm int 3206185db85Sdougm sa_proto_security_prop(char *proto, char *token) 3216185db85Sdougm { 3226185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 3236185db85Sdougm int ret = 0; 3246185db85Sdougm 3256185db85Sdougm if (ops != NULL && ops->sa_security_prop != NULL) 3266185db85Sdougm ret = ops->sa_security_prop(token); 3276185db85Sdougm return (ret); 3286185db85Sdougm } 3296185db85Sdougm 3306185db85Sdougm /* 3316185db85Sdougm * sa_proto_legacy_opts(proto, grouup, options) 3326185db85Sdougm * 3336185db85Sdougm * Have the protocol specific parser parse the options string and add 3346185db85Sdougm * an appropriate optionset to group. 3356185db85Sdougm */ 3366185db85Sdougm 3376185db85Sdougm int 3386185db85Sdougm sa_proto_legacy_opts(char *proto, sa_group_t group, char *options) 3396185db85Sdougm { 3406185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 3416185db85Sdougm int ret = SA_INVALID_PROTOCOL; 3426185db85Sdougm 3436185db85Sdougm if (ops != NULL && ops->sa_legacy_opts != NULL) 3446185db85Sdougm ret = ops->sa_legacy_opts(group, options); 3456185db85Sdougm return (ret); 3466185db85Sdougm } 3476185db85Sdougm 3486185db85Sdougm /* 3496185db85Sdougm * sa_proto_legacy_format(proto, group, hier) 3506185db85Sdougm * 3516185db85Sdougm * Return a legacy format string representing either the group's 3526185db85Sdougm * properties or the groups hierarchical properties. 3536185db85Sdougm */ 3546185db85Sdougm 3556185db85Sdougm char * 3566185db85Sdougm sa_proto_legacy_format(char *proto, sa_group_t group, int hier) 3576185db85Sdougm { 3586185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 3596185db85Sdougm char *ret = NULL; 3606185db85Sdougm 3616185db85Sdougm if (ops != NULL && ops->sa_legacy_format != NULL) 3626185db85Sdougm ret = ops->sa_legacy_format(group, hier); 3636185db85Sdougm return (ret); 3646185db85Sdougm } 3656185db85Sdougm 3666185db85Sdougm void 3676185db85Sdougm sa_format_free(char *str) 3686185db85Sdougm { 3696185db85Sdougm free(str); 3706185db85Sdougm } 3716185db85Sdougm 3726185db85Sdougm /* 3736185db85Sdougm * sharectl related API functions 3746185db85Sdougm */ 3756185db85Sdougm 3766185db85Sdougm /* 3776185db85Sdougm * sa_proto_get_properties(proto) 3786185db85Sdougm * 3796185db85Sdougm * Return the set of properties that are specific to the 3806185db85Sdougm * protocol. These are usually in /etc/dfs/<proto> and related files, 3816185db85Sdougm * but only the protocol module knows which ones for sure. 3826185db85Sdougm */ 3836185db85Sdougm 3846185db85Sdougm sa_protocol_properties_t 3856185db85Sdougm sa_proto_get_properties(char *proto) 3866185db85Sdougm { 3876185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 3886185db85Sdougm sa_protocol_properties_t props = NULL; 3896185db85Sdougm 3906185db85Sdougm if (ops != NULL && ops->sa_get_proto_set != NULL) 3916185db85Sdougm props = ops->sa_get_proto_set(); 3926185db85Sdougm return (props); 3936185db85Sdougm } 3946185db85Sdougm 3956185db85Sdougm /* 3966185db85Sdougm * sa_proto_set_property(proto, prop) 3976185db85Sdougm * 3986185db85Sdougm * Update the protocol specifiec property. 3996185db85Sdougm */ 4006185db85Sdougm 4016185db85Sdougm int 4026185db85Sdougm sa_proto_set_property(char *proto, sa_property_t prop) 4036185db85Sdougm { 4046185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 4056185db85Sdougm int ret = SA_OK; 406*25a68471Sdougm 4076185db85Sdougm if (ops != NULL && ops->sa_set_proto_prop != NULL) 4086185db85Sdougm ret = ops->sa_set_proto_prop(prop); 4096185db85Sdougm return (ret); 4106185db85Sdougm } 4116185db85Sdougm 4126185db85Sdougm /* 4136185db85Sdougm * sa_valid_protocol(proto) 4146185db85Sdougm * 415*25a68471Sdougm * Check to see if the protocol specified is defined by a 4166185db85Sdougm * plugin. Returns true (1) or false (0) 4176185db85Sdougm */ 4186185db85Sdougm 4196185db85Sdougm int 4206185db85Sdougm sa_valid_protocol(char *proto) 4216185db85Sdougm { 4226185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 4236185db85Sdougm return (ops != NULL); 4246185db85Sdougm } 4256185db85Sdougm 4266185db85Sdougm /* 4276185db85Sdougm * Return the current operational status of the protocol 4286185db85Sdougm */ 4296185db85Sdougm 4306185db85Sdougm char * 4316185db85Sdougm sa_get_protocol_status(char *proto) 4326185db85Sdougm { 4336185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 4346185db85Sdougm char *ret = NULL; 4356185db85Sdougm if (ops != NULL && ops->sa_get_proto_status != NULL) 4366185db85Sdougm ret = ops->sa_get_proto_status(proto); 4376185db85Sdougm return (ret); 4386185db85Sdougm } 4396185db85Sdougm 4406185db85Sdougm /* 4416185db85Sdougm * sa_proto_update_legacy(proto, share) 4426185db85Sdougm * 4436185db85Sdougm * Update the protocol specific legacy files if necessary for the 4446185db85Sdougm * specified share. 4456185db85Sdougm */ 4466185db85Sdougm 4476185db85Sdougm int 4486185db85Sdougm sa_proto_update_legacy(char *proto, sa_share_t share) 4496185db85Sdougm { 4506185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 4516185db85Sdougm int ret = SA_NOT_IMPLEMENTED; 4526185db85Sdougm 4536185db85Sdougm if (ops != NULL) { 4546185db85Sdougm if (ops->sa_update_legacy != NULL) 4556185db85Sdougm ret = ops->sa_update_legacy(share); 4566185db85Sdougm } 4576185db85Sdougm return (ret); 4586185db85Sdougm } 4596185db85Sdougm 4606185db85Sdougm /* 4616185db85Sdougm * sa_delete_legacy(proto, share) 4626185db85Sdougm * 463*25a68471Sdougm * Remove the specified share from the protocol specific legacy files. 4646185db85Sdougm */ 4656185db85Sdougm 4666185db85Sdougm int 4676185db85Sdougm sa_proto_delete_legacy(char *proto, sa_share_t share) 4686185db85Sdougm { 4696185db85Sdougm struct sa_plugin_ops *ops = find_protocol(proto); 4706185db85Sdougm int ret = SA_OK; 4716185db85Sdougm 4726185db85Sdougm if (ops != NULL) { 4736185db85Sdougm if (ops->sa_delete_legacy != NULL) 4746185db85Sdougm ret = ops->sa_delete_legacy(share); 4756185db85Sdougm } else { 4766185db85Sdougm if (proto != NULL) 4776185db85Sdougm ret = SA_NOT_IMPLEMENTED; 4786185db85Sdougm else 4796185db85Sdougm ret = SA_INVALID_PROTOCOL; 4806185db85Sdougm } 4816185db85Sdougm return (ret); 4826185db85Sdougm } 483