Whamcloud - gitweb
- mostly changes according to Mike's CODEINSP;
[fs/lustre-release.git] / lustre / fld / fld_request.c
1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/fld/fld_request.c
5  *  FLD (Fids Location Database)
6  *
7  *  Copyright (C) 2006 Cluster File Systems, Inc.
8  *   Author: Yury Umanets <umka@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_FLD
32
33 #ifdef __KERNEL__
34 # include <libcfs/libcfs.h>
35 # include <linux/module.h>
36 # include <linux/jbd.h>
37 # include <asm/div64.h>
38 #else /* __KERNEL__ */
39 # include <liblustre.h>
40 # include <libcfs/list.h>
41 #endif
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
48
49 #include <dt_object.h>
50 #include <md_object.h>
51 #include <lustre_req_layout.h>
52 #include <lustre_fld.h>
53 #include "fld_internal.h"
54
55 static int fld_rrb_hash(struct lu_client_fld *fld,
56                         seqno_t seq)
57 {
58         if (fld->fld_count == 0)
59                 return 0;
60
61         return do_div(seq, fld->fld_count);
62 }
63
64 static int fld_dht_hash(struct lu_client_fld *fld,
65                         seqno_t seq)
66 {
67         /* XXX: here should be DHT hash */
68         return fld_rrb_hash(fld, seq);
69 }
70
71 struct lu_fld_hash fld_hash[3] = {
72         {
73                 .fh_name = "DHT",
74                 .fh_func = fld_dht_hash
75         },
76         {
77                 .fh_name = "Round Robin",
78                 .fh_func = fld_rrb_hash
79         },
80         {
81                 0,
82         }
83 };
84
85 /* this function makes decision if passed @target appropriate acoordingly to
86  * passed @hash. In case of usual round-robin hash, this is decided by comparing
87  * hash and target's index. In the case of DHT, algorithm is a bit more
88  * complicated. */
89 static int fld_client_apt_target(struct fld_target *target,
90                                  int hash)
91 {
92         /* XXX: DHT case should be worked out. */
93         return (target->fldt_idx == hash);
94 }
95
96 static struct fld_target *
97 fld_client_get_target(struct lu_client_fld *fld,
98                       seqno_t seq)
99 {
100         struct fld_target *target;
101         int hash;
102         ENTRY;
103
104         LASSERT(fld->fld_hash != NULL);
105
106         spin_lock(&fld->fld_lock);
107         hash = fld->fld_hash->fh_func(fld, seq);
108
109         list_for_each_entry(target,
110                             &fld->fld_targets, fldt_chain) {
111                 if (fld_client_apt_target(target, hash)) {
112                         spin_unlock(&fld->fld_lock);
113                         RETURN(target);
114                 }
115         }
116         spin_unlock(&fld->fld_lock);
117
118         /* if target is not found, there is logical error anyway, so here is
119          * LBUG() to catch that situation. */
120         LBUG();
121         RETURN(NULL);
122 }
123
124 /* add export to FLD. This is usually done by CMM and LMV as they are main users
125  * of FLD module. */
126 int fld_client_add_target(struct lu_client_fld *fld,
127                           struct obd_export *exp)
128 {
129         struct client_obd *cli = &exp->exp_obd->u.cli;
130         struct fld_target *target, *tmp;
131         ENTRY;
132
133         LASSERT(exp != NULL);
134
135         CDEBUG(D_INFO|D_WARNING, "%s: adding export %s\n",
136                fld->fld_name, cli->cl_target_uuid.uuid);
137
138         OBD_ALLOC_PTR(target);
139         if (target == NULL)
140                 RETURN(-ENOMEM);
141
142         spin_lock(&fld->fld_lock);
143         list_for_each_entry(tmp, &fld->fld_targets, fldt_chain) {
144                 if (obd_uuid_equals(&tmp->fldt_exp->exp_client_uuid,
145                                     &exp->exp_client_uuid))
146                 {
147                         spin_unlock(&fld->fld_lock);
148                         OBD_FREE_PTR(target);
149                         RETURN(-EEXIST);
150                 }
151         }
152
153         target->fldt_exp = class_export_get(exp);
154         target->fldt_idx = fld->fld_count;
155
156         list_add_tail(&target->fldt_chain,
157                       &fld->fld_targets);
158         fld->fld_count++;
159         spin_unlock(&fld->fld_lock);
160
161         RETURN(0);
162 }
163 EXPORT_SYMBOL(fld_client_add_target);
164
165 /* remove export from FLD */
166 int fld_client_del_target(struct lu_client_fld *fld,
167                           struct obd_export *exp)
168 {
169         struct fld_target *target, *tmp;
170         ENTRY;
171
172         spin_lock(&fld->fld_lock);
173         list_for_each_entry_safe(target, tmp,
174                                  &fld->fld_targets, fldt_chain) {
175                 if (obd_uuid_equals(&target->fldt_exp->exp_client_uuid,
176                                     &exp->exp_client_uuid))
177                 {
178                         fld->fld_count--;
179                         list_del(&target->fldt_chain);
180                         spin_unlock(&fld->fld_lock);
181                         class_export_put(target->fldt_exp);
182                         OBD_FREE_PTR(target);
183                         RETURN(0);
184                 }
185         }
186         spin_unlock(&fld->fld_lock);
187         RETURN(-ENOENT);
188 }
189 EXPORT_SYMBOL(fld_client_del_target);
190
191 #ifdef LPROCFS
192 static int fld_client_proc_init(struct lu_client_fld *fld)
193 {
194         int rc;
195         ENTRY;
196
197         fld->fld_proc_dir = lprocfs_register(fld->fld_name,
198                                              proc_lustre_root,
199                                              NULL, NULL);
200
201         if (IS_ERR(fld->fld_proc_dir)) {
202                 CERROR("LProcFS failed in fld-init\n");
203                 rc = PTR_ERR(fld->fld_proc_dir);
204                 GOTO(err, rc);
205         }
206
207         rc = lprocfs_add_vars(fld->fld_proc_dir,
208                               fld_client_proc_list, fld);
209         if (rc) {
210                 CERROR("can't init FLD "
211                        "proc, rc %d\n", rc);
212                 GOTO(err_dir, rc);
213         }
214
215         RETURN(0);
216
217 err_dir:
218         lprocfs_remove(fld->fld_proc_dir);
219 err:
220         fld->fld_proc_dir = NULL;
221         return rc;
222 }
223
224 static void fld_client_proc_fini(struct lu_client_fld *fld)
225 {
226         ENTRY;
227         if (fld->fld_proc_dir) {
228                 lprocfs_remove(fld->fld_proc_dir);
229                 fld->fld_proc_dir = NULL;
230         }
231         EXIT;
232 }
233 #endif
234
235 static inline int hash_is_sane(int hash)
236 {
237         return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
238 }
239
240 int fld_client_init(struct lu_client_fld *fld,
241                     const char *uuid, int hash)
242 {
243         int rc = 0;
244         ENTRY;
245
246         LASSERT(fld != NULL);
247
248         if (!hash_is_sane(hash)) {
249                 CERROR("wrong hash function 0x%x\n", hash);
250                 RETURN(-EINVAL);
251         }
252
253         INIT_LIST_HEAD(&fld->fld_targets);
254         spin_lock_init(&fld->fld_lock);
255         fld->fld_hash = &fld_hash[hash];
256         fld->fld_count = 0;
257
258         snprintf(fld->fld_name, sizeof(fld->fld_name),
259                  "%s-cli-%s", LUSTRE_FLD_NAME, uuid);
260
261 #ifdef __KERNEL__
262         fld->fld_cache = fld_cache_init(FLD_HTABLE_SIZE);
263         if (IS_ERR(fld->fld_cache)) {
264                 rc = PTR_ERR(fld->fld_cache);
265                 fld->fld_cache = NULL;
266                 GOTO(out, rc);
267         }
268 #endif
269
270 #ifdef LPROCFS
271         rc = fld_client_proc_init(fld);
272         if (rc)
273                 GOTO(out, rc);
274 #endif
275         EXIT;
276 #ifdef __KERNEL__
277 out:
278 #endif
279         if (rc)
280                 fld_client_fini(fld);
281         else
282                 CDEBUG(D_INFO|D_WARNING,
283                        "Client FLD, using \"%s\" hash\n",
284                        fld->fld_hash->fh_name);
285         return rc;
286 }
287 EXPORT_SYMBOL(fld_client_init);
288
289 void fld_client_fini(struct lu_client_fld *fld)
290 {
291         struct fld_target *target, *tmp;
292         ENTRY;
293
294 #ifdef LPROCFS
295         fld_client_proc_fini(fld);
296 #endif
297
298         spin_lock(&fld->fld_lock);
299         list_for_each_entry_safe(target, tmp,
300                                  &fld->fld_targets, fldt_chain) {
301                 fld->fld_count--;
302                 list_del(&target->fldt_chain);
303                 class_export_put(target->fldt_exp);
304                 OBD_FREE_PTR(target);
305         }
306         spin_unlock(&fld->fld_lock);
307
308 #ifdef __KERNEL__
309         if (fld->fld_cache != NULL) {
310                 fld_cache_fini(fld->fld_cache);
311                 fld->fld_cache = NULL;
312         }
313 #endif
314
315         CDEBUG(D_INFO|D_WARNING, "Client FLD finalized\n");
316         EXIT;
317 }
318 EXPORT_SYMBOL(fld_client_fini);
319
320 static int fld_client_rpc(struct obd_export *exp,
321                           struct md_fld *mf, __u32 fld_op)
322 {
323         int size[2] = {sizeof(__u32), sizeof(struct md_fld)}, rc;
324         int mf_size = sizeof(struct md_fld);
325         struct ptlrpc_request *req;
326         struct req_capsule pill;
327         struct md_fld *pmf;
328         __u32 *op;
329         ENTRY;
330
331         LASSERT(exp != NULL);
332
333         req = ptlrpc_prep_req(class_exp2cliimp(exp),
334                               LUSTRE_MDS_VERSION, FLD_QUERY,
335                               2, size, NULL);
336         if (req == NULL)
337                 RETURN(-ENOMEM);
338
339         req_capsule_init(&pill, req, RCL_CLIENT,
340                          &mf_size);
341
342         req_capsule_set(&pill, &RQF_FLD_QUERY);
343
344         op = req_capsule_client_get(&pill, &RMF_FLD_OPC);
345         *op = fld_op;
346
347         pmf = req_capsule_client_get(&pill, &RMF_FLD_MDFLD);
348         *pmf = *mf;
349
350         req->rq_replen = lustre_msg_size(1, &mf_size);
351         req->rq_request_portal = FLD_REQUEST_PORTAL;
352
353         rc = ptlrpc_queue_wait(req);
354         if (rc)
355                 GOTO(out_req, rc);
356
357         pmf = req_capsule_server_get(&pill, &RMF_FLD_MDFLD);
358         if (pmf == NULL) {
359                 CERROR("Can't unpack FLD response\n");
360                 GOTO(out_req, rc = -EFAULT);
361         }
362         *mf = *pmf;
363         EXIT;
364 out_req:
365         req_capsule_fini(&pill);
366         ptlrpc_req_finished(req);
367         return rc;
368 }
369
370 static int __fld_client_create(struct lu_client_fld *fld,
371                                seqno_t seq, mdsno_t mds,
372                     struct md_fld *md_fld)
373 {
374         struct fld_target *target;
375         __u32 rc;
376         ENTRY;
377
378         target = fld_client_get_target(fld, seq);
379         if (!target)
380                 RETURN(-EINVAL);
381
382         rc = fld_client_rpc(target->fldt_exp, md_fld, FLD_CREATE);
383
384         if (rc  == 0) {
385                 /* do not return result of calling fld_cache_insert()
386                  * here. First of all because it may return -EEXISTS. Another
387                  * reason is that, we do not want to stop proceeding because of
388                  * cache errors. --umka */
389                 fld_cache_insert(fld->fld_cache, seq, mds);
390         }
391
392         RETURN(rc);
393 }
394
395 int fld_client_create(struct lu_client_fld *fld,
396                       seqno_t seq, mdsno_t mds)
397 {
398         struct md_fld md_fld = { .mf_seq = seq, .mf_mds = mds };
399         __u32 rc;
400         ENTRY;
401
402         rc = __fld_client_create(fld, seq, mds, &md_fld);
403         RETURN(rc);
404 }
405 EXPORT_SYMBOL(fld_client_create);
406
407 static int __fld_client_delete(struct lu_client_fld *fld,
408                                seqno_t seq, struct md_fld *md_fld)
409 {
410         struct fld_target *target;
411         __u32 rc;
412
413         fld_cache_delete(fld->fld_cache, seq);
414
415         target = fld_client_get_target(fld, seq);
416         if (!target)
417                 RETURN(-EINVAL);
418
419         rc = fld_client_rpc(target->fldt_exp,
420                             md_fld, FLD_DELETE);
421         RETURN(rc);
422 }
423
424 int fld_client_delete(struct lu_client_fld *fld,
425                       seqno_t seq)
426 {
427         struct md_fld md_fld = { .mf_seq = seq, .mf_mds = 0 };
428         __u32 rc;
429
430         rc = __fld_client_delete(fld, seq, &md_fld);
431         RETURN(rc);
432 }
433 EXPORT_SYMBOL(fld_client_delete);
434
435 static int __fld_client_lookup(struct lu_client_fld *fld,
436                                seqno_t seq, mdsno_t *mds,
437                                struct md_fld *md_fld)
438 {
439         struct fld_target *target;
440         int rc;
441         ENTRY;
442
443         /* lookup it in the cache */
444         rc = fld_cache_lookup(fld->fld_cache, seq, mds);
445         if (rc == 0)
446                 RETURN(0);
447
448         /* can not find it in the cache */
449         target = fld_client_get_target(fld, seq);
450         if (!target)
451                 RETURN(-EINVAL);
452
453         rc = fld_client_rpc(target->fldt_exp,
454                             md_fld, FLD_LOOKUP);
455         if (rc == 0)
456                 *mds = md_fld->mf_mds;
457
458         /* do not return error here as well. See previous comment in same
459          * situation in function fld_client_create(). --umka */
460         fld_cache_insert(fld->fld_cache, seq, *mds);
461
462         RETURN(rc);
463 }
464
465 int fld_client_lookup(struct lu_client_fld *fld,
466                       seqno_t seq, mdsno_t *mds)
467 {
468         struct md_fld md_fld = { .mf_seq = seq, .mf_mds = 0 };
469         int rc;
470         ENTRY;
471
472         rc = __fld_client_lookup(fld, seq, mds, &md_fld);
473         RETURN(rc);
474 }
475 EXPORT_SYMBOL(fld_client_lookup);