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