Whamcloud - gitweb
LU-6245 client: remove types abstraction from client code
[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, 2015, 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 #define DEBUG_SUBSYSTEM S_LLITE
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <obd.h>
44 #include <cl_object.h>
45
46 #include "llite_internal.h"
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 static int cl_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
53 {
54         u32 val_size;
55         u32 max_easize;
56         u32 def_easize;
57         int rc;
58         ENTRY;
59
60         val_size = sizeof(max_easize);
61         rc = obd_get_info(NULL, dt_exp, sizeof(KEY_MAX_EASIZE), KEY_MAX_EASIZE,
62                           &val_size, &max_easize);
63         if (rc != 0)
64                 RETURN(rc);
65
66         val_size = sizeof(def_easize);
67         rc = obd_get_info(NULL, dt_exp, sizeof(KEY_DEFAULT_EASIZE),
68                           KEY_DEFAULT_EASIZE, &val_size, &def_easize);
69         if (rc != 0)
70                 RETURN(rc);
71
72         /* default cookiesize is 0 because from 2.4 server doesn't send
73          * llog cookies to client. */
74         CDEBUG(D_HA, "updating def/max_easize: %d/%d\n",
75                def_easize, max_easize);
76
77         rc = md_init_ea_size(md_exp, max_easize, def_easize);
78         RETURN(rc);
79 }
80
81 /**
82  * This function is used as an upcall-callback hooked by liblustre and llite
83  * clients into obd_notify() listeners chain to handle notifications about
84  * change of import connect_flags. See llu_fsswop_mount() and
85  * lustre_common_fill_super().
86  */
87 int cl_ocd_update(struct obd_device *host,
88                   struct obd_device *watched,
89                   enum obd_notify_event ev, void *owner, void *data)
90 {
91         struct lustre_client_ocd *lco;
92         struct client_obd        *cli;
93         __u64 flags;
94         int   result;
95
96         ENTRY;
97         if (!strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME) &&
98             watched->obd_set_up && !watched->obd_stopping) {
99                 cli = &watched->u.cli;
100                 lco = owner;
101                 flags = cli->cl_import->imp_connect_data.ocd_connect_flags;
102                 CDEBUG(D_SUPER, "Changing connect_flags: %#llx -> %#llx\n",
103                        lco->lco_flags, flags);
104                 mutex_lock(&lco->lco_lock);
105                 lco->lco_flags &= flags;
106                 /* for each osc event update ea size */
107                 if (lco->lco_dt_exp)
108                         cl_init_ea_size(lco->lco_md_exp, lco->lco_dt_exp);
109
110                 mutex_unlock(&lco->lco_lock);
111                 result = 0;
112         } else {
113                 CERROR("unexpected notification from %s %s"
114                        "(setup:%d,stopping:%d)!\n",
115                        watched->obd_type->typ_name,
116                        watched->obd_name, watched->obd_set_up,
117                        watched->obd_stopping);
118                 result = -EINVAL;
119         }
120         RETURN(result);
121 }
122
123 #define GROUPLOCK_SCOPE "grouplock"
124
125 int cl_get_grouplock(struct cl_object *obj, unsigned long gid, int nonblock,
126                      struct ll_grouplock *lg)
127 {
128         struct lu_env          *env;
129         struct cl_io           *io;
130         struct cl_lock         *lock;
131         struct cl_lock_descr   *descr;
132         __u32                   enqflags;
133         __u16                   refcheck;
134         int                     rc;
135
136         env = cl_env_get(&refcheck);
137         if (IS_ERR(env))
138                 return PTR_ERR(env);
139
140         io = vvp_env_thread_io(env);
141         io->ci_obj = obj;
142
143         rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
144         if (rc != 0) {
145                 cl_io_fini(env, io);
146                 cl_env_put(env, &refcheck);
147                 /* Does not make sense to take GL for released layout */
148                 if (rc > 0)
149                         rc = -ENOTSUPP;
150                 return rc;
151         }
152
153         lock = vvp_env_lock(env);
154         descr = &lock->cll_descr;
155         descr->cld_obj = obj;
156         descr->cld_start = 0;
157         descr->cld_end = CL_PAGE_EOF;
158         descr->cld_gid = gid;
159         descr->cld_mode = CLM_GROUP;
160
161         enqflags = CEF_MUST | (nonblock ? CEF_NONBLOCK : 0);
162         descr->cld_enq_flags = enqflags;
163
164         rc = cl_lock_request(env, io, lock);
165         if (rc < 0) {
166                 cl_io_fini(env, io);
167                 cl_env_put(env, &refcheck);
168                 return rc;
169         }
170
171         lg->lg_env = env;
172         lg->lg_io = io;
173         lg->lg_lock = lock;
174         lg->lg_gid = gid;
175
176         return 0;
177 }
178
179 void cl_put_grouplock(struct ll_grouplock *lg)
180 {
181         struct lu_env  *env  = lg->lg_env;
182         struct cl_io   *io   = lg->lg_io;
183         struct cl_lock *lock = lg->lg_lock;
184
185         LASSERT(lg->lg_env != NULL);
186         LASSERT(lg->lg_gid != 0);
187
188         cl_lock_release(env, lock);
189         cl_io_fini(env, io);
190         cl_env_put(env, NULL);
191 }