Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[cascardo/linux.git] / drivers / staging / lustre / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * cl code shared between vvp and liblustre (and other Lustre clients in the
33  * future).
34  *
35  */
36 #include "../include/obd_class.h"
37 #include "../include/obd_support.h"
38 #include "../include/obd.h"
39 #include "../include/cl_object.h"
40
41 #include "../include/lustre_lite.h"
42 #include "llite_internal.h"
43
44 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
45  * us to make MDS RPCs with large enough reply buffers to hold the
46  * maximum-sized (= maximum striped) EA and cookie without having to
47  * calculate this (via a call into the LOV + OSCs) each time we make an RPC.
48  */
49 int cl_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
50 {
51         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC_V3 };
52         __u32 valsize = sizeof(struct lov_desc);
53         int rc, easize, def_easize, cookiesize;
54         struct lov_desc desc;
55         __u16 stripes, def_stripes;
56
57         rc = obd_get_info(NULL, dt_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC,
58                           &valsize, &desc, NULL);
59         if (rc)
60                 return rc;
61
62         stripes = min_t(__u32, desc.ld_tgt_count, LOV_MAX_STRIPE_COUNT);
63         lsm.lsm_stripe_count = stripes;
64         easize = obd_size_diskmd(dt_exp, &lsm);
65
66         def_stripes = min_t(__u32, desc.ld_default_stripe_count,
67                             LOV_MAX_STRIPE_COUNT);
68         lsm.lsm_stripe_count = def_stripes;
69         def_easize = obd_size_diskmd(dt_exp, &lsm);
70
71         cookiesize = stripes * sizeof(struct llog_cookie);
72
73         /* default cookiesize is 0 because from 2.4 server doesn't send
74          * llog cookies to client.
75          */
76         CDEBUG(D_HA,
77                "updating def/max_easize: %d/%d def/max_cookiesize: 0/%d\n",
78                def_easize, easize, cookiesize);
79
80         rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize, 0);
81         return rc;
82 }
83
84 /**
85  * This function is used as an upcall-callback hooked by liblustre and llite
86  * clients into obd_notify() listeners chain to handle notifications about
87  * change of import connect_flags. See llu_fsswop_mount() and
88  * lustre_common_fill_super().
89  */
90 int cl_ocd_update(struct obd_device *host,
91                   struct obd_device *watched,
92                   enum obd_notify_event ev, void *owner, void *data)
93 {
94         struct lustre_client_ocd *lco;
95         struct client_obd       *cli;
96         __u64 flags;
97         int   result;
98
99         if (!strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME) &&
100             watched->obd_set_up && !watched->obd_stopping) {
101                 cli = &watched->u.cli;
102                 lco = owner;
103                 flags = cli->cl_import->imp_connect_data.ocd_connect_flags;
104                 CDEBUG(D_SUPER, "Changing connect_flags: %#llx -> %#llx\n",
105                        lco->lco_flags, flags);
106                 mutex_lock(&lco->lco_lock);
107                 lco->lco_flags &= flags;
108                 /* for each osc event update ea size */
109                 if (lco->lco_dt_exp)
110                         cl_init_ea_size(lco->lco_md_exp, lco->lco_dt_exp);
111
112                 mutex_unlock(&lco->lco_lock);
113                 result = 0;
114         } else {
115                 CERROR("unexpected notification from %s %s (setup:%d,stopping:%d)!\n",
116                        watched->obd_type->typ_name,
117                        watched->obd_name, watched->obd_set_up,
118                        watched->obd_stopping);
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 ll_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 = vvp_env_thread_io(env);
142         io->ci_obj = obj;
143         io->ci_ignore_layout = 1;
144
145         rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
146         if (rc != 0) {
147                 cl_io_fini(env, io);
148                 cl_env_put(env, &refcheck);
149                 /* Does not make sense to take GL for released layout */
150                 if (rc > 0)
151                         rc = -ENOTSUPP;
152                 return rc;
153         }
154
155         lock = vvp_env_lock(env);
156         descr = &lock->cll_descr;
157         descr->cld_obj = obj;
158         descr->cld_start = 0;
159         descr->cld_end = CL_PAGE_EOF;
160         descr->cld_gid = gid;
161         descr->cld_mode = CLM_GROUP;
162
163         enqflags = CEF_MUST | (nonblock ? CEF_NONBLOCK : 0);
164         descr->cld_enq_flags = enqflags;
165
166         rc = cl_lock_request(env, io, lock);
167         if (rc < 0) {
168                 cl_io_fini(env, io);
169                 cl_env_put(env, &refcheck);
170                 return rc;
171         }
172
173         cg->lg_env  = cl_env_get(&refcheck);
174         cg->lg_io   = io;
175         cg->lg_lock = lock;
176         cg->lg_gid  = gid;
177         LASSERT(cg->lg_env == env);
178
179         cl_env_unplant(env, &refcheck);
180         return 0;
181 }
182
183 void cl_put_grouplock(struct ll_grouplock *cg)
184 {
185         struct lu_env  *env  = cg->lg_env;
186         struct cl_io   *io   = cg->lg_io;
187         struct cl_lock *lock = cg->lg_lock;
188         int          refcheck;
189
190         LASSERT(cg->lg_env);
191         LASSERT(cg->lg_gid);
192
193         cl_env_implant(env, &refcheck);
194         cl_env_put(env, &refcheck);
195
196         cl_lock_release(env, lock);
197         cl_io_fini(env, io);
198         cl_env_put(env, NULL);
199 }