Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / fid / fid_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/fid/fid_handler.c
39  *
40  * Lustre Sequence Manager
41  *
42  * Author: Yury Umanets <umka@clusterfs.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_FID
49
50 #ifdef __KERNEL__
51 # include <libcfs/libcfs.h>
52 # include <linux/module.h>
53 #else /* __KERNEL__ */
54 # include <liblustre.h>
55 #endif
56
57 #include <obd.h>
58 #include <obd_class.h>
59 #include <lu_target.h>
60 #include <dt_object.h>
61 #include <md_object.h>
62 #include <obd_support.h>
63 #include <lustre_req_layout.h>
64 #include <lustre_fid.h>
65 #include "fid_internal.h"
66
67 #ifdef __KERNEL__
68 /* Assigns client to sequence controller node. */
69 int seq_server_set_cli(struct lu_server_seq *seq,
70                        struct lu_client_seq *cli,
71                        const struct lu_env *env)
72 {
73         int rc = 0;
74         ENTRY;
75
76         /*
77          * Ask client for new range, assign that range to ->seq_space and write
78          * seq state to backing store should be atomic.
79          */
80         cfs_down(&seq->lss_sem);
81
82         if (cli == NULL) {
83                 CDEBUG(D_INFO, "%s: Detached sequence client %s\n",
84                        seq->lss_name, cli->lcs_name);
85                 seq->lss_cli = cli;
86                 GOTO(out_up, rc = 0);
87         }
88
89         if (seq->lss_cli != NULL) {
90                 CERROR("%s: Sequence controller is already "
91                        "assigned\n", seq->lss_name);
92                 GOTO(out_up, rc = -EINVAL);
93         }
94
95         CDEBUG(D_INFO, "%s: Attached sequence controller %s\n",
96                seq->lss_name, cli->lcs_name);
97
98         seq->lss_cli = cli;
99         cli->lcs_space.lsr_index = seq->lss_site->ms_node_id;
100         EXIT;
101 out_up:
102         cfs_up(&seq->lss_sem);
103         return rc;
104 }
105 EXPORT_SYMBOL(seq_server_set_cli);
106 /*
107  * allocate \a w units of sequence from range \a from.
108  */
109 static inline void range_alloc(struct lu_seq_range *to,
110                                struct lu_seq_range *from,
111                                __u64 width)
112 {
113         width = min(range_space(from), width);
114         to->lsr_start = from->lsr_start;
115         to->lsr_end = from->lsr_start + width;
116         from->lsr_start += width;
117 }
118
119 /**
120  * On controller node, allocate new super sequence for regular sequence server.
121  * As this super sequence controller, this node suppose to maintain fld
122  * and update index.
123  * \a out range always has currect mds node number of requester.
124  */
125
126 static int __seq_server_alloc_super(struct lu_server_seq *seq,
127                                     struct lu_seq_range *out,
128                                     const struct lu_env *env)
129 {
130         struct lu_seq_range *space = &seq->lss_space;
131         int rc;
132         ENTRY;
133
134         LASSERT(range_is_sane(space));
135
136         if (range_is_exhausted(space)) {
137                 CERROR("%s: Sequences space is exhausted\n",
138                        seq->lss_name);
139                 RETURN(-ENOSPC);
140         } else {
141                 range_alloc(out, space, seq->lss_width);
142         }
143
144         rc = seq_store_update(env, seq, out, 1 /* sync */);
145
146         CDEBUG(D_INFO, "%s: super-sequence allocation rc = %d "
147                DRANGE"\n", seq->lss_name, rc, PRANGE(out));
148
149         RETURN(rc);
150 }
151
152 int seq_server_alloc_super(struct lu_server_seq *seq,
153                            struct lu_seq_range *out,
154                            const struct lu_env *env)
155 {
156         int rc;
157         ENTRY;
158
159         cfs_down(&seq->lss_sem);
160         rc = __seq_server_alloc_super(seq, out, env);
161         cfs_up(&seq->lss_sem);
162
163         RETURN(rc);
164 }
165
166 static int __seq_set_init(const struct lu_env *env,
167                             struct lu_server_seq *seq)
168 {
169         struct lu_seq_range *space = &seq->lss_space;
170         int rc;
171
172         range_alloc(&seq->lss_lowater_set, space, seq->lss_set_width);
173         range_alloc(&seq->lss_hiwater_set, space, seq->lss_set_width);
174
175         rc = seq_store_update(env, seq, NULL, 1);
176
177         return rc;
178 }
179
180 /*
181  * This function implements new seq allocation algorithm using async
182  * updates to seq file on disk. ref bug 18857 for details.
183  * there are four variable to keep track of this process
184  *
185  * lss_space; - available lss_space
186  * lss_lowater_set; - lu_seq_range for all seqs before barrier, i.e. safe to use
187  * lss_hiwater_set; - lu_seq_range after barrier, i.e. allocated but may be
188  *                    not yet committed
189  *
190  * when lss_lowater_set reaches the end it is replaced with hiwater one and
191  * a write operation is initiated to allocate new hiwater range.
192  * if last seq write opearion is still not commited, current operation is
193  * flaged as sync write op.
194  */
195 static int range_alloc_set(const struct lu_env *env,
196                             struct lu_seq_range *out,
197                             struct lu_server_seq *seq)
198 {
199         struct lu_seq_range *space = &seq->lss_space;
200         struct lu_seq_range *loset = &seq->lss_lowater_set;
201         struct lu_seq_range *hiset = &seq->lss_hiwater_set;
202         int rc = 0;
203
204         if (range_is_zero(loset))
205                 __seq_set_init(env, seq);
206
207         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_ALLOC)) /* exhaust set */
208                 loset->lsr_start = loset->lsr_end;
209
210         if (range_is_exhausted(loset)) {
211                 /* reached high water mark. */
212                 struct lu_device *dev = seq->lss_site->ms_lu.ls_top_dev;
213                 int obd_num_clients = dev->ld_obd->obd_num_exports;
214                 __u64 set_sz;
215
216                 /* calculate new seq width based on number of clients */
217                 set_sz = max(seq->lss_set_width,
218                              obd_num_clients * seq->lss_width);
219                 set_sz = min(range_space(space), set_sz);
220
221                 /* Switch to hiwater range now */
222                 *loset = *hiset;
223                 /* allocate new hiwater range */
224                 range_alloc(hiset, space, set_sz);
225
226                 /* update ondisk seq with new *space */
227                 rc = seq_store_update(env, seq, NULL, seq->lss_need_sync);
228         }
229
230         LASSERTF(!range_is_exhausted(loset) || range_is_sane(loset),
231                  DRANGE"\n", PRANGE(loset));
232
233         if (rc == 0)
234                 range_alloc(out, loset, seq->lss_width);
235
236         RETURN(rc);
237 }
238
239 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
240                                    struct lu_seq_range *out,
241                                    const struct lu_env *env)
242 {
243         struct lu_seq_range *space = &seq->lss_space;
244         int rc = 0;
245
246         ENTRY;
247
248         LASSERT(range_is_sane(space));
249
250         /* Check if available space ends and allocate new super seq */
251         if (range_is_exhausted(space)) {
252                 if (!seq->lss_cli) {
253                         CERROR("%s: No sequence controller is attached.\n",
254                                seq->lss_name);
255                         RETURN(-ENODEV);
256                 }
257
258                 rc = seq_client_alloc_super(seq->lss_cli, env);
259                 if (rc) {
260                         CERROR("%s: Can't allocate super-sequence, rc %d\n",
261                                seq->lss_name, rc);
262                         RETURN(rc);
263                 }
264
265                 /* Saving new range to allocation space. */
266                 *space = seq->lss_cli->lcs_space;
267                 LASSERT(range_is_sane(space));
268         }
269
270         rc = range_alloc_set(env, out, seq);
271         if (rc == 0) {
272                 CDEBUG(D_INFO, "%s: Allocated meta-sequence "
273                        DRANGE"\n", seq->lss_name, PRANGE(out));
274         }
275
276         RETURN(rc);
277 }
278
279 int seq_server_alloc_meta(struct lu_server_seq *seq,
280                           struct lu_seq_range *out,
281                           const struct lu_env *env)
282 {
283         int rc;
284         ENTRY;
285
286         cfs_down(&seq->lss_sem);
287         rc = __seq_server_alloc_meta(seq, out, env);
288         cfs_up(&seq->lss_sem);
289
290         RETURN(rc);
291 }
292 EXPORT_SYMBOL(seq_server_alloc_meta);
293
294 static int seq_server_handle(struct lu_site *site,
295                              const struct lu_env *env,
296                              __u32 opc, struct lu_seq_range *out)
297 {
298         int rc;
299         struct md_site *mite;
300         ENTRY;
301
302         mite = lu_site2md(site);
303         switch (opc) {
304         case SEQ_ALLOC_META:
305                 if (!mite->ms_server_seq) {
306                         CERROR("Sequence server is not "
307                                "initialized\n");
308                         RETURN(-EINVAL);
309                 }
310                 rc = seq_server_alloc_meta(mite->ms_server_seq, out, env);
311                 break;
312         case SEQ_ALLOC_SUPER:
313                 if (!mite->ms_control_seq) {
314                         CERROR("Sequence controller is not "
315                                "initialized\n");
316                         RETURN(-EINVAL);
317                 }
318                 rc = seq_server_alloc_super(mite->ms_control_seq, out, env);
319                 break;
320         default:
321                 rc = -EINVAL;
322                 break;
323         }
324
325         RETURN(rc);
326 }
327
328 static int seq_req_handle(struct ptlrpc_request *req,
329                           const struct lu_env *env,
330                           struct seq_thread_info *info)
331 {
332         struct lu_seq_range *out, *tmp;
333         struct lu_site *site;
334         int rc = -EPROTO;
335         __u32 *opc;
336         ENTRY;
337
338         LASSERT(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY));
339         site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
340         LASSERT(site != NULL);
341
342         rc = req_capsule_server_pack(info->sti_pill);
343         if (rc)
344                 RETURN(err_serious(rc));
345
346         opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
347         if (opc != NULL) {
348                 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
349                 if (out == NULL)
350                         RETURN(err_serious(-EPROTO));
351
352                 tmp = req_capsule_client_get(info->sti_pill, &RMF_SEQ_RANGE);
353
354                 /* seq client passed mdt id, we need to pass that using out
355                  * range parameter */
356
357                 out->lsr_index = tmp->lsr_index;
358                 out->lsr_flags = tmp->lsr_flags;
359                 rc = seq_server_handle(site, env, *opc, out);
360         } else
361                 rc = err_serious(-EPROTO);
362
363         RETURN(rc);
364 }
365
366 /* context key constructor/destructor: seq_key_init, seq_key_fini */
367 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
368
369 /* context key: seq_thread_key */
370 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD);
371
372 static void seq_thread_info_init(struct ptlrpc_request *req,
373                                  struct seq_thread_info *info)
374 {
375         info->sti_pill = &req->rq_pill;
376         /* Init request capsule */
377         req_capsule_init(info->sti_pill, req, RCL_SERVER);
378         req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
379 }
380
381 static void seq_thread_info_fini(struct seq_thread_info *info)
382 {
383         req_capsule_fini(info->sti_pill);
384 }
385
386 static int seq_handle(struct ptlrpc_request *req)
387 {
388         const struct lu_env *env;
389         struct seq_thread_info *info;
390         int rc;
391
392         env = req->rq_svc_thread->t_env;
393         LASSERT(env != NULL);
394
395         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
396         LASSERT(info != NULL);
397
398         seq_thread_info_init(req, info);
399         rc = seq_req_handle(req, env, info);
400         /* XXX: we don't need replay but MDT assign transno in any case,
401          * remove it manually before reply*/
402         lustre_msg_set_transno(req->rq_repmsg, 0);
403         seq_thread_info_fini(info);
404
405         return rc;
406 }
407
408 /*
409  * Entry point for handling FLD RPCs called from MDT.
410  */
411 int seq_query(struct com_thread_info *info)
412 {
413         return seq_handle(info->cti_pill->rc_req);
414 }
415 EXPORT_SYMBOL(seq_query);
416
417 static void seq_server_proc_fini(struct lu_server_seq *seq);
418
419 #ifdef LPROCFS
420 static int seq_server_proc_init(struct lu_server_seq *seq)
421 {
422         int rc;
423         ENTRY;
424
425         seq->lss_proc_dir = lprocfs_register(seq->lss_name,
426                                              seq_type_proc_dir,
427                                              NULL, NULL);
428         if (IS_ERR(seq->lss_proc_dir)) {
429                 rc = PTR_ERR(seq->lss_proc_dir);
430                 RETURN(rc);
431         }
432
433         rc = lprocfs_add_vars(seq->lss_proc_dir,
434                               seq_server_proc_list, seq);
435         if (rc) {
436                 CERROR("%s: Can't init sequence manager "
437                        "proc, rc %d\n", seq->lss_name, rc);
438                 GOTO(out_cleanup, rc);
439         }
440
441         RETURN(0);
442
443 out_cleanup:
444         seq_server_proc_fini(seq);
445         return rc;
446 }
447
448 static void seq_server_proc_fini(struct lu_server_seq *seq)
449 {
450         ENTRY;
451         if (seq->lss_proc_dir != NULL) {
452                 if (!IS_ERR(seq->lss_proc_dir))
453                         lprocfs_remove(&seq->lss_proc_dir);
454                 seq->lss_proc_dir = NULL;
455         }
456         EXIT;
457 }
458 #else
459 static int seq_server_proc_init(struct lu_server_seq *seq)
460 {
461         return 0;
462 }
463
464 static void seq_server_proc_fini(struct lu_server_seq *seq)
465 {
466         return;
467 }
468 #endif
469
470
471 int seq_server_init(struct lu_server_seq *seq,
472                     struct dt_device *dev,
473                     const char *prefix,
474                     enum lu_mgr_type type,
475                     struct md_site *ms,
476                     const struct lu_env *env)
477 {
478         int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
479         ENTRY;
480
481         LASSERT(dev != NULL);
482         LASSERT(prefix != 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_sema_init(&seq->lss_sem, 1);
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                 CDEBUG(D_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 struct lu_local_obj_desc llod_seq_srv = {
563         .llod_name      = LUSTRE_SEQ_SRV_NAME,
564         .llod_oid       = FID_SEQ_SRV_OID,
565         .llod_is_index  = 0,
566 };
567
568 static struct lu_local_obj_desc llod_seq_ctl = {
569         .llod_name      = LUSTRE_SEQ_CTL_NAME,
570         .llod_oid       = FID_SEQ_CTL_OID,
571         .llod_is_index  = 0,
572 };
573
574 static int __init fid_mod_init(void)
575 {
576         seq_type_proc_dir = lprocfs_register(LUSTRE_SEQ_NAME,
577                                              proc_lustre_root,
578                                              NULL, NULL);
579         if (IS_ERR(seq_type_proc_dir))
580                 return PTR_ERR(seq_type_proc_dir);
581
582         llo_local_obj_register(&llod_seq_srv);
583         llo_local_obj_register(&llod_seq_ctl);
584
585         LU_CONTEXT_KEY_INIT(&seq_thread_key);
586         lu_context_key_register(&seq_thread_key);
587         return 0;
588 }
589
590 static void __exit fid_mod_exit(void)
591 {
592         llo_local_obj_unregister(&llod_seq_srv);
593         llo_local_obj_unregister(&llod_seq_ctl);
594
595         lu_context_key_degister(&seq_thread_key);
596         if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
597                 lprocfs_remove(&seq_type_proc_dir);
598                 seq_type_proc_dir = NULL;
599         }
600 }
601
602 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
603 MODULE_DESCRIPTION("Lustre FID Module");
604 MODULE_LICENSE("GPL");
605
606 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);
607 #endif