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