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