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