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