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