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