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