Whamcloud - gitweb
LU-5971 llite: reorganize variable and data structures
[fs/lustre-release.git] / lustre / llite / lcommon_misc.c
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, 2014, 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
45 #include "llite_internal.h"
46
47 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
48  * us to make MDS RPCs with large enough reply buffers to hold the
49  * maximum-sized (= maximum striped) EA and cookie without having to
50  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
51 static int cl_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
52 {
53         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC_V3 };
54         __u32 valsize = sizeof(struct lov_desc);
55         int rc, easize, def_easize, cookiesize;
56         struct lov_desc desc;
57         __u16 stripes, def_stripes;
58         ENTRY;
59
60         rc = obd_get_info(NULL, dt_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC,
61                           &valsize, &desc, NULL);
62         if (rc)
63                 RETURN(rc);
64
65         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
66         lsm.lsm_stripe_count = stripes;
67         easize = obd_size_diskmd(dt_exp, &lsm);
68
69         def_stripes = min_t(__u32, desc.ld_default_stripe_count,
70                             LOV_MAX_STRIPE_COUNT);
71         lsm.lsm_stripe_count = def_stripes;
72         def_easize = obd_size_diskmd(dt_exp, &lsm);
73
74         cookiesize = stripes * sizeof(struct llog_cookie);
75
76         /* default cookiesize is 0 because from 2.4 server doesn't send
77          * llog cookies to client. */
78         CDEBUG(D_HA,
79                "updating def/max_easize: %d/%d def/max_cookiesize: 0/%d\n",
80                def_easize, easize, cookiesize);
81
82         rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize, 0);
83         RETURN(rc);
84 }
85
86 /**
87  * This function is used as an upcall-callback hooked by liblustre and llite
88  * clients into obd_notify() listeners chain to handle notifications about
89  * change of import connect_flags. See llu_fsswop_mount() and
90  * lustre_common_fill_super().
91  */
92 int cl_ocd_update(struct obd_device *host,
93                   struct obd_device *watched,
94                   enum obd_notify_event ev, void *owner, void *data)
95 {
96         struct lustre_client_ocd *lco;
97         struct client_obd        *cli;
98         __u64 flags;
99         int   result;
100
101         ENTRY;
102         if (!strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
103                 cli = &watched->u.cli;
104                 lco = owner;
105                 flags = cli->cl_import->imp_connect_data.ocd_connect_flags;
106                 CDEBUG(D_SUPER, "Changing connect_flags: "LPX64" -> "LPX64"\n",
107                        lco->lco_flags, flags);
108                 mutex_lock(&lco->lco_lock);
109                 lco->lco_flags &= flags;
110                 /* for each osc event update ea size */
111                 if (lco->lco_dt_exp)
112                         cl_init_ea_size(lco->lco_md_exp, lco->lco_dt_exp);
113
114                 mutex_unlock(&lco->lco_lock);
115                 result = 0;
116         } else {
117                 CERROR("unexpected notification from %s %s!\n",
118                        watched->obd_type->typ_name,
119                        watched->obd_name);
120                 result = -EINVAL;
121         }
122         RETURN(result);
123 }
124
125 #define GROUPLOCK_SCOPE "grouplock"
126
127 int cl_get_grouplock(struct cl_object *obj, unsigned long gid, int nonblock,
128                      struct ll_grouplock *lg)
129 {
130         struct lu_env          *env;
131         struct cl_io           *io;
132         struct cl_lock         *lock;
133         struct cl_lock_descr   *descr;
134         __u32                   enqflags;
135         int                     refcheck;
136         int                     rc;
137
138         env = cl_env_get(&refcheck);
139         if (IS_ERR(env))
140                 return PTR_ERR(env);
141
142         io = vvp_env_thread_io(env);
143         io->ci_obj = obj;
144         io->ci_ignore_layout = 1;
145
146         rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
147         if (rc != 0) {
148                 cl_io_fini(env, io);
149                 cl_env_put(env, &refcheck);
150                 /* Does not make sense to take GL for released layout */
151                 if (rc > 0)
152                         rc = -ENOTSUPP;
153                 return rc;
154         }
155
156         lock = vvp_env_lock(env);
157         descr = &lock->cll_descr;
158         descr->cld_obj = obj;
159         descr->cld_start = 0;
160         descr->cld_end = CL_PAGE_EOF;
161         descr->cld_gid = gid;
162         descr->cld_mode = CLM_GROUP;
163
164         enqflags = CEF_MUST | (nonblock ? CEF_NONBLOCK : 0);
165         descr->cld_enq_flags = enqflags;
166
167         rc = cl_lock_request(env, io, lock);
168         if (rc < 0) {
169                 cl_io_fini(env, io);
170                 cl_env_put(env, &refcheck);
171                 return rc;
172         }
173
174         lg->lg_env = cl_env_get(&refcheck);
175         lg->lg_io = io;
176         lg->lg_lock = lock;
177         lg->lg_gid = gid;
178         LASSERT(lg->lg_env == env);
179
180         cl_env_unplant(env, &refcheck);
181         return 0;
182 }
183
184 void cl_put_grouplock(struct ll_grouplock *lg)
185 {
186         struct lu_env  *env  = lg->lg_env;
187         struct cl_io   *io   = lg->lg_io;
188         struct cl_lock *lock = lg->lg_lock;
189         int             refcheck;
190
191         LASSERT(lg->lg_env != NULL);
192         LASSERT(lg->lg_gid != 0);
193
194         cl_env_implant(env, &refcheck);
195         cl_env_put(env, &refcheck);
196
197         cl_lock_release(env, lock);
198         cl_io_fini(env, io);
199         cl_env_put(env, NULL);
200 }