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