Whamcloud - gitweb
LU-1330 fid: prepare FID module for client server split
[fs/lustre-release.git] / lustre / fid / fid_handler.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, 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  * lustre/fid/fid_handler.c
37  *
38  * Lustre Sequence Manager
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FID
44
45 #include <libcfs/libcfs.h>
46 #include <linux/module.h>
47 #include <obd.h>
48 #include <obd_class.h>
49 #include <dt_object.h>
50 #include <md_object.h>
51 #include <obd_support.h>
52 #include <lustre_req_layout.h>
53 #include <lustre_fid.h>
54 #include <lustre_mdt.h> /* err_serious() */
55 #include "fid_internal.h"
56
57 static void seq_server_proc_fini(struct lu_server_seq *seq);
58
59 /* Assigns client to sequence controller node. */
60 int seq_server_set_cli(struct lu_server_seq *seq,
61                        struct lu_client_seq *cli,
62                        const struct lu_env *env)
63 {
64         int rc = 0;
65         ENTRY;
66
67         /*
68          * Ask client for new range, assign that range to ->seq_space and write
69          * seq state to backing store should be atomic.
70          */
71         mutex_lock(&seq->lss_mutex);
72
73         if (cli == NULL) {
74                 CDEBUG(D_INFO, "%s: Detached sequence client %s\n",
75                        seq->lss_name, cli->lcs_name);
76                 seq->lss_cli = cli;
77                 GOTO(out_up, rc = 0);
78         }
79
80         if (seq->lss_cli != NULL) {
81                 CDEBUG(D_HA, "%s: Sequence controller is already "
82                        "assigned\n", seq->lss_name);
83                 GOTO(out_up, rc = -EEXIST);
84         }
85
86         CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
87                seq->lss_name, cli->lcs_name);
88
89         seq->lss_cli = cli;
90         cli->lcs_space.lsr_index = seq->lss_site->ss_node_id;
91         EXIT;
92 out_up:
93         mutex_unlock(&seq->lss_mutex);
94         return rc;
95 }
96 EXPORT_SYMBOL(seq_server_set_cli);
97 /*
98  * allocate \a w units of sequence from range \a from.
99  */
100 static inline void range_alloc(struct lu_seq_range *to,
101                                struct lu_seq_range *from,
102                                __u64 width)
103 {
104         width = min(range_space(from), width);
105         to->lsr_start = from->lsr_start;
106         to->lsr_end = from->lsr_start + width;
107         from->lsr_start += width;
108 }
109
110 /**
111  * On controller node, allocate new super sequence for regular sequence server.
112  * As this super sequence controller, this node suppose to maintain fld
113  * and update index.
114  * \a out range always has currect mds node number of requester.
115  */
116
117 static int __seq_server_alloc_super(struct lu_server_seq *seq,
118                                     struct lu_seq_range *out,
119                                     const struct lu_env *env)
120 {
121         struct lu_seq_range *space = &seq->lss_space;
122         int rc;
123         ENTRY;
124
125         LASSERT(range_is_sane(space));
126
127         if (range_is_exhausted(space)) {
128                 CERROR("%s: Sequences space is exhausted\n",
129                        seq->lss_name);
130                 RETURN(-ENOSPC);
131         } else {
132                 range_alloc(out, space, seq->lss_width);
133         }
134
135         rc = seq_store_update(env, seq, out, 1 /* sync */);
136
137         LCONSOLE_INFO("%s: super-sequence allocation rc = %d " DRANGE"\n",
138                       seq->lss_name, rc, PRANGE(out));
139
140         RETURN(rc);
141 }
142
143 int seq_server_alloc_super(struct lu_server_seq *seq,
144                            struct lu_seq_range *out,
145                            const struct lu_env *env)
146 {
147         int rc;
148         ENTRY;
149
150         mutex_lock(&seq->lss_mutex);
151         rc = __seq_server_alloc_super(seq, out, env);
152         mutex_unlock(&seq->lss_mutex);
153
154         RETURN(rc);
155 }
156
157 static int __seq_set_init(const struct lu_env *env,
158                             struct lu_server_seq *seq)
159 {
160         struct lu_seq_range *space = &seq->lss_space;
161         int rc;
162
163         range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
164         range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
165
166         rc = seq_store_update(env, seq, NULL, 1);
167
168         return rc;
169 }
170
171 /*
172  * This function implements new seq allocation algorithm using async
173  * updates to seq file on disk. ref bug 18857 for details.
174  * there are four variable to keep track of this process
175  *
176  * lss_space; - available lss_space
177  * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
178  * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
179  *                    not yet committed
180  *
181  * when lss_lowater_set reaches the end it is replaced with hiwater one and
182  * a write operation is initiated to allocate new hiwater range.
183  * if last seq write opearion is still not commited, current operation is
184  * flaged as sync write op.
185  */
186 static int range_alloc_set(const struct lu_env *env,
187                             struct lu_seq_range *out,
188                             struct lu_server_seq *seq)
189 {
190         struct lu_seq_range *space = &seq->lss_space;
191         struct lu_seq_range *loset = &seq->lss_lowater_set;
192         struct lu_seq_range *hiset = &seq->lss_hiwater_set;
193         int rc = 0;
194
195         if (range_is_zero(loset))
196                 __seq_set_init(env, seq);
197
198         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
199                 loset->lsr_start = loset->lsr_end;
200
201         if (range_is_exhausted(loset)) {
202                 /* reached high water mark. */
203                 struct lu_device *dev = seq->lss_site->ss_lu->ls_top_dev;
204                 int obd_num_clients = dev->ld_obd->obd_num_exports;
205                 __u64 set_sz;
206
207                 /* calculate new seq width based on number of clients */
208                 set_sz = max(seq->lss_set_width,
209                              obd_num_clients * seq->lss_width);
210                 set_sz = min(range_space(space), set_sz);
211
212                 /* Switch to hiwater range now */
213                 *loset = *hiset;
214                 /* allocate new hiwater range */
215                 range_alloc(hiset, space, set_sz);
216
217                 /* update ondisk seq with new *space */
218                 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
219         }
220
221         LASSERTF(!range_is_exhausted(loset) || range_is_sane(loset),
222                  DRANGE"\n", PRANGE(loset));
223
224         if (rc == 0)
225                 range_alloc(out, loset, seq->lss_width);
226
227         RETURN(rc);
228 }
229
230 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
231                                    struct lu_seq_range *out,
232                                    const struct lu_env *env)
233 {
234         struct lu_seq_range *space = &seq->lss_space;
235         int rc = 0;
236
237         ENTRY;
238
239         LASSERT(range_is_sane(space));
240
241         /* Check if available space ends and allocate new super seq */
242         if (range_is_exhausted(space)) {
243                 if (!seq->lss_cli) {
244                         CERROR("%s: No sequence controller is attached.\n",
245                                seq->lss_name);
246                         RETURN(-ENODEV);
247                 }
248
249                 rc = seq_client_alloc_super(seq->lss_cli, env);
250                 if (rc) {
251                         CERROR("%s: Can't allocate super-sequence, rc %d\n",
252                                seq->lss_name, rc);
253                         RETURN(rc);
254                 }
255
256                 /* Saving new range to allocation space. */
257                 *space = seq->lss_cli->lcs_space;
258                 LASSERT(range_is_sane(space));
259         }
260
261         rc = range_alloc_set(env, out, seq);
262         if (rc != 0) {
263                 CERROR("%s: Allocated meta-sequence failed: rc = %d\n",
264                         seq->lss_name, rc);
265                 RETURN(rc);
266         }
267
268         CDEBUG(D_INFO, "%s: Allocated meta-sequence " DRANGE"\n",
269                 seq->lss_name, PRANGE(out));
270
271         RETURN(rc);
272 }
273
274 int seq_server_alloc_meta(struct lu_server_seq *seq,
275                           struct lu_seq_range *out,
276                           const struct lu_env *env)
277 {
278         int rc;
279         ENTRY;
280
281         mutex_lock(&seq->lss_mutex);
282         rc = __seq_server_alloc_meta(seq, out, env);
283         mutex_unlock(&seq->lss_mutex);
284
285         RETURN(rc);
286 }
287 EXPORT_SYMBOL(seq_server_alloc_meta);
288
289 static int seq_server_handle(struct lu_site *site,
290                              const struct lu_env *env,
291                              __u32 opc, struct lu_seq_range *out)
292 {
293         int rc;
294         struct seq_server_site *ss_site;
295         ENTRY;
296
297         ss_site = lu_site2seq(site);
298
299         switch (opc) {
300         case SEQ_ALLOC_META:
301                 if (!ss_site->ss_server_seq) {
302                         CERROR("Sequence server is not "
303                                "initialized\n");
304                         RETURN(-EINVAL);
305                 }
306                 rc = seq_server_alloc_meta(ss_site->ss_server_seq, out, env);
307                 break;
308         case SEQ_ALLOC_SUPER:
309                 if (!ss_site->ss_control_seq) {
310                         CERROR("Sequence controller is not "
311                                "initialized\n");
312                         RETURN(-EINVAL);
313                 }
314                 rc = seq_server_alloc_super(ss_site->ss_control_seq, out, env);
315                 break;
316         default:
317                 rc = -EINVAL;
318                 break;
319         }
320
321         RETURN(rc);
322 }
323
324 static int seq_req_handle(struct ptlrpc_request *req,
325                           const struct lu_env *env,
326                           struct seq_thread_info *info)
327 {
328         struct lu_seq_range *out, *tmp;
329         struct lu_site *site;
330         int rc = -EPROTO;
331         __u32 *opc;
332         ENTRY;
333
334         LASSERT(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY));
335         site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
336         LASSERT(site != NULL);
337
338         rc = req_capsule_server_pack(info->sti_pill);
339         if (rc)
340                 RETURN(err_serious(rc));
341
342         opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
343         if (opc != NULL) {
344                 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
345                 if (out == NULL)
346                         RETURN(err_serious(-EPROTO));
347
348                 tmp = req_capsule_client_get(info->sti_pill, &RMF_SEQ_RANGE);
349
350                 /* seq client passed mdt id, we need to pass that using out
351                  * range parameter */
352
353                 out->lsr_index = tmp->lsr_index;
354                 out->lsr_flags = tmp->lsr_flags;
355                 rc = seq_server_handle(site, env, *opc, out);
356         } else
357                 rc = err_serious(-EPROTO);
358
359         RETURN(rc);
360 }
361
362 /* context key constructor/destructor: seq_key_init, seq_key_fini */
363 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
364
365 /* context key: seq_thread_key */
366 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD | LCT_DT_THREAD);
367
368 static void seq_thread_info_init(struct ptlrpc_request *req,
369                                  struct seq_thread_info *info)
370 {
371         info->sti_pill = &req->rq_pill;
372         /* Init request capsule */
373         req_capsule_init(info->sti_pill, req, RCL_SERVER);
374         req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
375 }
376
377 static void seq_thread_info_fini(struct seq_thread_info *info)
378 {
379         req_capsule_fini(info->sti_pill);
380 }
381
382 int seq_handle(struct ptlrpc_request *req)
383 {
384         const struct lu_env *env;
385         struct seq_thread_info *info;
386         int rc;
387
388         env = req->rq_svc_thread->t_env;
389         LASSERT(env != NULL);
390
391         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
392         LASSERT(info != NULL);
393
394         seq_thread_info_init(req, info);
395         rc = seq_req_handle(req, env, info);
396         /* XXX: we don't need replay but MDT assign transno in any case,
397          * remove it manually before reply*/
398         lustre_msg_set_transno(req->rq_repmsg, 0);
399         seq_thread_info_fini(info);
400
401         return rc;
402 }
403 EXPORT_SYMBOL(seq_handle);
404
405 /*
406  * Entry point for handling FLD RPCs called from MDT.
407  */
408 int seq_query(struct com_thread_info *info)
409 {
410         return seq_handle(info->cti_pill->rc_req);
411 }
412 EXPORT_SYMBOL(seq_query);
413
414 static int seq_server_proc_init(struct lu_server_seq *seq)
415 {
416 #ifdef LPROCFS
417
418         int rc;
419         ENTRY;
420
421         seq->lss_proc_dir = lprocfs_register(seq->lss_name,
422                                              seq_type_proc_dir,
423                                              NULL, NULL);
424         if (IS_ERR(seq->lss_proc_dir)) {
425                 rc = PTR_ERR(seq->lss_proc_dir);
426                 RETURN(rc);
427         }
428
429         rc = lprocfs_add_vars(seq->lss_proc_dir,
430                               seq_server_proc_list, seq);
431         if (rc) {
432                 CERROR("%s: Can't init sequence manager "
433                        "proc, rc %d\n", seq->lss_name, rc);
434                 GOTO(out_cleanup, rc);
435         }
436
437         RETURN(0);
438
439 out_cleanup:
440         seq_server_proc_fini(seq);
441         return rc;
442 #else /* LPROCFS */
443         return 0;
444 #endif
445 }
446
447 static void seq_server_proc_fini(struct lu_server_seq *seq)
448 {
449 #ifdef LPROCFS
450         ENTRY;
451         if (seq->lss_proc_dir != NULL) {
452                 if (!IS_ERR(seq->lss_proc_dir))
453                         lprocfs_remove(&seq->lss_proc_dir);
454                 seq->lss_proc_dir = NULL;
455         }
456         EXIT;
457 #endif /* LPROCFS */
458 }
459
460 int seq_server_init(struct lu_server_seq *seq,
461                     struct dt_device *dev,
462                     const char *prefix,
463                     enum lu_mgr_type type,
464                     struct seq_server_site *ss,
465                     const struct lu_env *env)
466 {
467         int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
468         ENTRY;
469
470         LASSERT(dev != NULL);
471         LASSERT(prefix != NULL);
472         LASSERT(ss != NULL);
473         LASSERT(ss->ss_lu != NULL);
474
475         seq->lss_cli = NULL;
476         seq->lss_type = type;
477         seq->lss_site = ss;
478         range_init(&seq->lss_space);
479
480         range_init(&seq->lss_lowater_set);
481         range_init(&seq->lss_hiwater_set);
482         seq->lss_set_width = LUSTRE_SEQ_BATCH_WIDTH;
483
484         mutex_init(&seq->lss_mutex);
485
486         seq->lss_width = is_srv ?
487                 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
488
489         snprintf(seq->lss_name, sizeof(seq->lss_name),
490                  "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
491
492         rc = seq_store_init(seq, env, dev);
493         if (rc)
494                 GOTO(out, rc);
495         /* Request backing store for saved sequence info. */
496         rc = seq_store_read(seq, env);
497         if (rc == -ENODATA) {
498
499                 /* Nothing is read, init by default value. */
500                 seq->lss_space = is_srv ?
501                         LUSTRE_SEQ_ZERO_RANGE:
502                         LUSTRE_SEQ_SPACE_RANGE;
503
504                 LASSERT(ss != NULL);
505                 seq->lss_space.lsr_index = ss->ss_node_id;
506                 LCONSOLE_INFO("%s: No data found "
507                               "on store. Initialize space\n",
508                               seq->lss_name);
509
510                 rc = seq_store_update(env, seq, NULL, 0);
511                 if (rc) {
512                         CERROR("%s: Can't write space data, "
513                                "rc %d\n", seq->lss_name, rc);
514                 }
515         } else if (rc) {
516                 CERROR("%s: Can't read space data, rc %d\n",
517                        seq->lss_name, rc);
518                 GOTO(out, rc);
519         }
520
521         if (is_srv) {
522                 LASSERT(range_is_sane(&seq->lss_space));
523         } else {
524                 LASSERT(!range_is_zero(&seq->lss_space) &&
525                         range_is_sane(&seq->lss_space));
526         }
527
528         rc  = seq_server_proc_init(seq);
529         if (rc)
530                 GOTO(out, rc);
531
532         EXIT;
533 out:
534         if (rc)
535                 seq_server_fini(seq, env);
536         return rc;
537 }
538 EXPORT_SYMBOL(seq_server_init);
539
540 void seq_server_fini(struct lu_server_seq *seq,
541                      const struct lu_env *env)
542 {
543         ENTRY;
544
545         seq_server_proc_fini(seq);
546         seq_store_fini(seq, env);
547
548         EXIT;
549 }
550 EXPORT_SYMBOL(seq_server_fini);
551
552 int seq_site_fini(const struct lu_env *env, struct seq_server_site *ss)
553 {
554         if (ss == NULL)
555                 RETURN(0);
556
557         if (ss->ss_server_seq) {
558                 seq_server_fini(ss->ss_server_seq, env);
559                 OBD_FREE_PTR(ss->ss_server_seq);
560                 ss->ss_server_seq = NULL;
561         }
562
563         if (ss->ss_control_seq) {
564                 seq_server_fini(ss->ss_control_seq, env);
565                 OBD_FREE_PTR(ss->ss_control_seq);
566                 ss->ss_control_seq = NULL;
567         }
568
569         if (ss->ss_client_seq) {
570                 seq_client_fini(ss->ss_client_seq);
571                 OBD_FREE_PTR(ss->ss_client_seq);
572                 ss->ss_client_seq = NULL;
573         }
574
575         RETURN(0);
576 }
577 EXPORT_SYMBOL(seq_site_fini);
578
579 int fid_server_mod_init(void)
580 {
581         LU_CONTEXT_KEY_INIT(&seq_thread_key);
582         return lu_context_key_register(&seq_thread_key);
583 }
584
585 void fid_server_mod_exit(void)
586 {
587         lu_context_key_degister(&seq_thread_key);
588 }