Whamcloud - gitweb
- cleanups and small fixes accordingly to Nikita's DLDINSP.
[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_handler.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
56 fld_rrb_hash(struct lu_client_fld *fld, 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
65 fld_dht_hash(struct lu_client_fld *fld, seqno_t seq)
66 {
67         /* XXX: here should 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
90 fld_client_apt_target(struct fld_target *target, 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, seqno_t seq)
98 {
99         struct fld_target *target;
100         int hash;
101         ENTRY;
102
103         LASSERT(fld->fld_hash != NULL);
104
105         spin_lock(&fld->fld_lock);
106         hash = fld->fld_hash->fh_func(fld, seq);
107
108         list_for_each_entry(target,
109                             &fld->fld_targets, fldt_chain) {
110                 if (fld_client_apt_target(target, hash)) {
111                         spin_unlock(&fld->fld_lock);
112                         RETURN(target);
113                 }
114         }
115         spin_unlock(&fld->fld_lock);
116
117         /* if target is not found, there is logical error anyway, so here is
118          * LBUG() to catch that situation. */
119         LBUG();
120         RETURN(NULL);
121 }
122
123 /* add export to FLD. This is usually done by CMM and LMV as they are main users
124  * of FLD module. */
125 int
126 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, "FLD(cli): adding export %s\n",
136                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
167 fld_client_del_target(struct lu_client_fld *fld,
168                       struct obd_export *exp)
169 {
170         struct fld_target *target, *tmp;
171         ENTRY;
172
173         spin_lock(&fld->fld_lock);
174         list_for_each_entry_safe(target, tmp,
175                                  &fld->fld_targets, fldt_chain) {
176                 if (obd_uuid_equals(&target->fldt_exp->exp_client_uuid,
177                                     &exp->exp_client_uuid))
178                 {
179                         fld->fld_count--;
180                         list_del(&target->fldt_chain);
181                         spin_unlock(&fld->fld_lock);
182                         class_export_put(target->fldt_exp);
183                         OBD_FREE_PTR(target);
184                         RETURN(0);
185                 }
186         }
187         spin_unlock(&fld->fld_lock);
188         RETURN(-ENOENT);
189 }
190 EXPORT_SYMBOL(fld_client_del_target);
191
192 #ifdef LPROCFS
193 static int
194 fld_client_proc_init(struct lu_client_fld *fld)
195 {
196         int rc;
197         ENTRY;
198
199         fld->fld_proc_dir = lprocfs_register(fld->fld_name,
200                                              proc_lustre_root,
201                                              NULL, NULL);
202         
203         if (IS_ERR(fld->fld_proc_dir)) {
204                 CERROR("LProcFS failed in fld-init\n");
205                 rc = PTR_ERR(fld->fld_proc_dir);
206                 GOTO(err, rc);
207         }
208
209         rc = lprocfs_add_vars(fld->fld_proc_dir,
210                               fld_client_proc_list, fld);
211         if (rc) {
212                 CERROR("can't init FLD "
213                        "proc, rc %d\n", rc);
214                 GOTO(err, rc);
215         }
216
217         RETURN(0);
218
219 err:
220         fld->fld_proc_dir = NULL;
221         return rc;
222 }
223
224 static void
225 fld_client_proc_fini(struct lu_client_fld *fld)
226 {
227         ENTRY;
228         if (fld->fld_proc_dir) {
229                 lprocfs_remove(fld->fld_proc_dir);
230                 fld->fld_proc_dir = NULL;
231         }
232         EXIT;
233 }
234 #endif
235
236 static inline int hash_is_sane(int hash)
237 {
238         return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
239 }
240
241 int
242 fld_client_init(struct lu_client_fld *fld,
243                 const char *uuid, int hash)
244 {
245         int rc = 0;
246         ENTRY;
247
248         LASSERT(fld != NULL);
249
250         if (!hash_is_sane(hash)) {
251                 CERROR("wrong hash function 0x%x\n", hash);
252                 RETURN(-EINVAL);
253         }
254         
255         INIT_LIST_HEAD(&fld->fld_targets);
256         spin_lock_init(&fld->fld_lock);
257         fld->fld_hash = &fld_hash[hash];
258         fld->fld_count = 0;
259         
260         snprintf(fld->fld_name, sizeof(fld->fld_name),
261                  "%s-%s", LUSTRE_FLD_NAME, uuid);
262         
263 #ifdef __KERNEL__
264         fld->fld_cache = fld_cache_init(FLD_HTABLE_SIZE);
265         if (IS_ERR(fld->fld_cache)) {
266                 rc = PTR_ERR(fld->fld_cache);
267                 fld->fld_cache = NULL;
268                 GOTO(out, rc);
269         }
270 #endif
271
272 #ifdef LPROCFS
273         rc = fld_client_proc_init(fld);
274         if (rc)
275                 GOTO(out, rc);
276 #endif
277         EXIT;
278 out:
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
290 fld_client_fini(struct lu_client_fld *fld)
291 {
292         struct fld_target *target, *tmp;
293         ENTRY;
294
295 #ifdef LPROCFS
296         fld_client_proc_fini(fld);
297 #endif
298         
299         spin_lock(&fld->fld_lock);
300         list_for_each_entry_safe(target, tmp,
301                                  &fld->fld_targets, fldt_chain) {
302                 fld->fld_count--;
303                 list_del(&target->fldt_chain);
304                 class_export_put(target->fldt_exp);
305                 OBD_FREE_PTR(target);
306         }
307         spin_unlock(&fld->fld_lock);
308
309 #ifdef __KERNEL__
310         if (fld->fld_cache != NULL) {
311                 fld_cache_fini(fld->fld_cache);
312                 fld->fld_cache = NULL;
313         }
314 #endif
315
316         CDEBUG(D_INFO|D_WARNING, "Client FLD finalized\n");
317         EXIT;
318 }
319 EXPORT_SYMBOL(fld_client_fini);
320
321 static int
322 fld_client_rpc(struct obd_export *exp,
323                struct md_fld *mf, __u32 fld_op)
324 {
325         int size[2] = {sizeof(__u32), sizeof(struct md_fld)}, rc;
326         int mf_size = sizeof(struct md_fld);
327         struct ptlrpc_request *req;
328         struct md_fld *pmf;
329         __u32 *op;
330         ENTRY;
331
332         LASSERT(exp != NULL);
333
334         req = ptlrpc_prep_req(class_exp2cliimp(exp),
335                               LUSTRE_MDS_VERSION, FLD_QUERY,
336                               2, size, NULL);
337         if (req == NULL)
338                 RETURN(-ENOMEM);
339
340         op = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*op));
341         *op = fld_op;
342
343         pmf = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*pmf));
344         *pmf = *mf;
345
346         req->rq_replen = lustre_msg_size(1, &mf_size);
347         req->rq_request_portal = FLD_REQUEST_PORTAL;
348
349         rc = ptlrpc_queue_wait(req);
350         if (rc)
351                 GOTO(out_req, rc);
352
353         pmf = lustre_swab_repbuf(req, 0, sizeof(*pmf),
354                                  lustre_swab_md_fld);
355         *mf = *pmf; 
356 out_req:
357         ptlrpc_req_finished(req);
358         RETURN(rc);
359 }
360
361 static int
362 __fld_client_create(struct lu_client_fld *fld,
363                     seqno_t seq, mdsno_t mds,
364                     struct md_fld *md_fld)
365 {
366         struct fld_target *target;
367         __u32 rc;
368         ENTRY;
369
370         target = fld_client_get_target(fld, seq);
371         if (!target)
372                 RETURN(-EINVAL);
373         
374         rc = fld_client_rpc(target->fldt_exp, md_fld, FLD_CREATE);
375         
376         if (rc  == 0) {
377                 /* do not return result of calling fld_cache_insert()
378                  * here. First of all because it may return -EEXISTS. Another
379                  * reason is that, we do not want to stop proceeding because of
380                  * cache errors. --umka */
381                 fld_cache_insert(fld->fld_cache, seq, mds);
382         }
383         
384         RETURN(rc);
385 }
386
387 int
388 fld_client_create(struct lu_client_fld *fld,
389                   seqno_t seq, mdsno_t mds)
390 {
391         struct md_fld md_fld = { .mf_seq = seq, .mf_mds = mds };
392         __u32 rc;
393         ENTRY;
394
395         rc = __fld_client_create(fld, seq, mds, &md_fld);
396         RETURN(rc);
397 }
398 EXPORT_SYMBOL(fld_client_create);
399
400 static int
401 __fld_client_delete(struct lu_client_fld *fld,
402                     seqno_t seq, struct md_fld *md_fld)
403 {
404         struct fld_target *target;
405         __u32 rc;
406
407         fld_cache_delete(fld->fld_cache, seq);
408         
409         target = fld_client_get_target(fld, seq);
410         if (!target)
411                 RETURN(-EINVAL);
412
413         rc = fld_client_rpc(target->fldt_exp,
414                             md_fld, FLD_DELETE);
415         RETURN(rc);
416 }
417
418 int
419 fld_client_delete(struct lu_client_fld *fld,
420                   seqno_t seq)
421 {
422         struct md_fld md_fld = { .mf_seq = seq, .mf_mds = 0 };
423         __u32 rc;
424
425         rc = __fld_client_delete(fld, seq, &md_fld);
426         RETURN(rc);
427 }
428 EXPORT_SYMBOL(fld_client_delete);
429
430 static int
431 __fld_client_lookup(struct lu_client_fld *fld,
432                     seqno_t seq, mdsno_t *mds,
433                     struct md_fld *md_fld)
434 {
435         struct fld_target *target;
436         int rc;
437         ENTRY;
438
439         /* lookup it in the cache */
440         rc = fld_cache_lookup(fld->fld_cache, seq, mds);
441         if (rc == 0)
442                 RETURN(0);
443         
444         /* can not find it in the cache */
445         target = fld_client_get_target(fld, seq);
446         if (!target)
447                 RETURN(-EINVAL);
448                 
449         rc = fld_client_rpc(target->fldt_exp,
450                             md_fld, FLD_LOOKUP);
451         if (rc == 0)
452                 *mds = md_fld->mf_mds;
453
454         /* do not return error here as well. See previous comment in same
455          * situation in function fld_client_create(). --umka */
456         fld_cache_insert(fld->fld_cache, seq, *mds);
457         
458         RETURN(rc);
459 }
460
461 int
462 fld_client_lookup(struct lu_client_fld *fld,
463                   seqno_t seq, mdsno_t *mds)
464 {
465         struct md_fld md_fld = { .mf_seq = seq, .mf_mds = 0 };
466         int rc;
467         ENTRY;
468
469         rc = __fld_client_lookup(fld, seq, mds, &md_fld);
470         RETURN(rc);
471 }
472 EXPORT_SYMBOL(fld_client_lookup);