lcommon_misc.c revision d7e09d0397e84eefbabfd9cb353221f3c6448d83
1/* 2 * GPL HEADER START 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 only, 8 * as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * General Public License version 2 for more details (a copy is included 14 * in the LICENSE file that accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License 17 * version 2 along with this program; If not, see 18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf 19 * 20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 21 * CA 95054 USA or visit www.sun.com if you need additional information or 22 * have any questions. 23 * 24 * GPL HEADER END 25 */ 26/* 27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 28 * Use is subject to license terms. 29 * 30 * Copyright (c) 2011, 2012, Intel Corporation. 31 */ 32/* 33 * This file is part of Lustre, http://www.lustre.org/ 34 * Lustre is a trademark of Sun Microsystems, Inc. 35 * 36 * cl code shared between vvp and liblustre (and other Lustre clients in the 37 * future). 38 * 39 */ 40#include <obd_class.h> 41#include <obd_support.h> 42#include <obd.h> 43#include <cl_object.h> 44#include <lclient.h> 45 46#include <lustre_lite.h> 47 48 49/* Initialize the default and maximum LOV EA and cookie sizes. This allows 50 * us to make MDS RPCs with large enough reply buffers to hold the 51 * maximum-sized (= maximum striped) EA and cookie without having to 52 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */ 53int cl_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp) 54{ 55 struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC_V3 }; 56 __u32 valsize = sizeof(struct lov_desc); 57 int rc, easize, def_easize, cookiesize; 58 struct lov_desc desc; 59 __u16 stripes; 60 ENTRY; 61 62 rc = obd_get_info(NULL, dt_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC, 63 &valsize, &desc, NULL); 64 if (rc) 65 RETURN(rc); 66 67 stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT); 68 lsm.lsm_stripe_count = stripes; 69 easize = obd_size_diskmd(dt_exp, &lsm); 70 71 lsm.lsm_stripe_count = desc.ld_default_stripe_count; 72 def_easize = obd_size_diskmd(dt_exp, &lsm); 73 74 cookiesize = stripes * sizeof(struct llog_cookie); 75 76 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n", 77 easize, cookiesize); 78 79 rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize); 80 RETURN(rc); 81} 82 83/** 84 * This function is used as an upcall-callback hooked by liblustre and llite 85 * clients into obd_notify() listeners chain to handle notifications about 86 * change of import connect_flags. See llu_fsswop_mount() and 87 * lustre_common_fill_super(). 88 */ 89int cl_ocd_update(struct obd_device *host, 90 struct obd_device *watched, 91 enum obd_notify_event ev, void *owner, void *data) 92{ 93 struct lustre_client_ocd *lco; 94 struct client_obd *cli; 95 __u64 flags; 96 int result; 97 98 ENTRY; 99 if (!strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) { 100 cli = &watched->u.cli; 101 lco = owner; 102 flags = cli->cl_import->imp_connect_data.ocd_connect_flags; 103 CDEBUG(D_SUPER, "Changing connect_flags: "LPX64" -> "LPX64"\n", 104 lco->lco_flags, flags); 105 mutex_lock(&lco->lco_lock); 106 lco->lco_flags &= flags; 107 /* for each osc event update ea size */ 108 if (lco->lco_dt_exp) 109 cl_init_ea_size(lco->lco_md_exp, lco->lco_dt_exp); 110 111 mutex_unlock(&lco->lco_lock); 112 result = 0; 113 } else { 114 CERROR("unexpected notification from %s %s!\n", 115 watched->obd_type->typ_name, 116 watched->obd_name); 117 result = -EINVAL; 118 } 119 RETURN(result); 120} 121 122#define GROUPLOCK_SCOPE "grouplock" 123 124int cl_get_grouplock(struct cl_object *obj, unsigned long gid, int nonblock, 125 struct ccc_grouplock *cg) 126{ 127 struct lu_env *env; 128 struct cl_io *io; 129 struct cl_lock *lock; 130 struct cl_lock_descr *descr; 131 __u32 enqflags; 132 int refcheck; 133 int rc; 134 135 env = cl_env_get(&refcheck); 136 if (IS_ERR(env)) 137 return PTR_ERR(env); 138 139 io = ccc_env_thread_io(env); 140 io->ci_obj = obj; 141 io->ci_ignore_layout = 1; 142 143 rc = cl_io_init(env, io, CIT_MISC, io->ci_obj); 144 if (rc) { 145 LASSERT(rc < 0); 146 cl_env_put(env, &refcheck); 147 return rc; 148 } 149 150 descr = &ccc_env_info(env)->cti_descr; 151 descr->cld_obj = obj; 152 descr->cld_start = 0; 153 descr->cld_end = CL_PAGE_EOF; 154 descr->cld_gid = gid; 155 descr->cld_mode = CLM_GROUP; 156 157 enqflags = CEF_MUST | (nonblock ? CEF_NONBLOCK : 0); 158 descr->cld_enq_flags = enqflags; 159 160 lock = cl_lock_request(env, io, descr, GROUPLOCK_SCOPE, current); 161 if (IS_ERR(lock)) { 162 cl_io_fini(env, io); 163 cl_env_put(env, &refcheck); 164 return PTR_ERR(lock); 165 } 166 167 cg->cg_env = cl_env_get(&refcheck); 168 cg->cg_io = io; 169 cg->cg_lock = lock; 170 cg->cg_gid = gid; 171 LASSERT(cg->cg_env == env); 172 173 cl_env_unplant(env, &refcheck); 174 return 0; 175} 176 177void cl_put_grouplock(struct ccc_grouplock *cg) 178{ 179 struct lu_env *env = cg->cg_env; 180 struct cl_io *io = cg->cg_io; 181 struct cl_lock *lock = cg->cg_lock; 182 int refcheck; 183 184 LASSERT(cg->cg_env); 185 LASSERT(cg->cg_gid); 186 187 cl_env_implant(env, &refcheck); 188 cl_env_put(env, &refcheck); 189 190 cl_unuse(env, lock); 191 cl_lock_release(env, lock, GROUPLOCK_SCOPE, current); 192 cl_io_fini(env, io); 193 cl_env_put(env, NULL); 194} 195