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