Whamcloud - gitweb
Land b_head_interop_disk on HEAD (20081119_1314)
[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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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         down(&seq->lss_sem);
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         EXIT;
97 out_up:
98         up(&seq->lss_sem);
99         return rc;
100 }
101 EXPORT_SYMBOL(seq_server_set_cli);
102
103 /*
104  * On controller node, allocate new super sequence for regular sequence server.
105  */
106 static int __seq_server_alloc_super(struct lu_server_seq *seq,
107                                     struct lu_range *in,
108                                     struct lu_range *out,
109                                     const struct lu_env *env)
110 {
111         struct lu_range *space = &seq->lss_space;
112         int rc;
113         ENTRY;
114
115         LASSERT(range_is_sane(space));
116
117         if (in != NULL) {
118                 CDEBUG(D_INFO, "%s: Input seq range: "
119                        DRANGE"\n", seq->lss_name, PRANGE(in));
120
121                 if (in->lr_end > space->lr_start)
122                         space->lr_start = in->lr_end;
123                 *out = *in;
124
125                 CDEBUG(D_INFO, "%s: Recovered space: "DRANGE"\n",
126                        seq->lss_name, PRANGE(space));
127         } else {
128                 if (range_space(space) < seq->lss_width) {
129                         CWARN("%s: Sequences space to be exhausted soon. "
130                               "Only "LPU64" sequences left\n", seq->lss_name,
131                               range_space(space));
132                         *out = *space;
133                         space->lr_start = space->lr_end;
134                 } else if (range_is_exhausted(space)) {
135                         CERROR("%s: Sequences space is exhausted\n",
136                                seq->lss_name);
137                         RETURN(-ENOSPC);
138                 } else {
139                         range_alloc(out, space, seq->lss_width);
140                 }
141         }
142
143         rc = seq_store_write(seq, env);
144         if (rc) {
145                 CERROR("%s: Can't write space data, rc %d\n",
146                        seq->lss_name, rc);
147                 RETURN(rc);
148         }
149
150         CDEBUG(D_INFO, "%s: Allocated super-sequence "
151                DRANGE"\n", seq->lss_name, PRANGE(out));
152
153         RETURN(rc);
154 }
155
156 int seq_server_alloc_super(struct lu_server_seq *seq,
157                            struct lu_range *in,
158                            struct lu_range *out,
159                            const struct lu_env *env)
160 {
161         int rc;
162         ENTRY;
163
164         down(&seq->lss_sem);
165         rc = __seq_server_alloc_super(seq, in, out, env);
166         up(&seq->lss_sem);
167
168         RETURN(rc);
169 }
170
171 static int __seq_server_alloc_meta(struct lu_server_seq *seq,
172                                    struct lu_range *in,
173                                    struct lu_range *out,
174                                    const struct lu_env *env)
175 {
176         struct lu_range *space = &seq->lss_space;
177         int rc = 0;
178         ENTRY;
179
180         LASSERT(range_is_sane(space));
181
182         /*
183          * This is recovery case. Adjust super range if input range looks like
184          * it is allocated from new super.
185          */
186         if (in != NULL) {
187                 CDEBUG(D_INFO, "%s: Input seq range: "
188                        DRANGE"\n", seq->lss_name, PRANGE(in));
189
190                 if (range_is_exhausted(space)) {
191                         /*
192                          * Server cannot send empty range to client, this is why
193                          * we check here that range from client is "newer" than
194                          * exhausted super.
195                          */
196                         LASSERT(in->lr_end > space->lr_start);
197
198                         /*
199                          * Start is set to end of last allocated, because it
200                          * *is* already allocated so we take that into account
201                          * and do not use for other allocations.
202                          */
203                         space->lr_start = in->lr_end;
204
205                         /*
206                          * End is set to in->lr_start + super sequence
207                          * allocation unit. That is because in->lr_start is
208                          * first seq in new allocated range from controller
209                          * before failure.
210                          */
211                         space->lr_end = in->lr_start + LUSTRE_SEQ_SUPER_WIDTH;
212
213                         if (!seq->lss_cli) {
214                                 CERROR("%s: No sequence controller "
215                                        "is attached.\n", seq->lss_name);
216                                 RETURN(-ENODEV);
217                         }
218
219                         /*
220                          * Let controller know that this is recovery and last
221                          * obtained range from it was @space.
222                          */
223                         rc = seq_client_replay_super(seq->lss_cli, space, env);
224                         if (rc) {
225                                 CERROR("%s: Can't replay super-sequence, "
226                                        "rc %d\n", seq->lss_name, rc);
227                                 RETURN(rc);
228                         }
229                 } else {
230                         /*
231                          * Update super start by end from client's range. Super
232                          * end should not be changed if range was not exhausted.
233                          */
234                         if (in->lr_end > space->lr_start)
235                                 space->lr_start = in->lr_end;
236                 }
237
238                 *out = *in;
239
240                 CDEBUG(D_INFO, "%s: Recovered space: "DRANGE"\n",
241                        seq->lss_name, PRANGE(space));
242         } else {
243                 /*
244                  * XXX: Avoid cascading RPCs using kind of async preallocation
245                  * when meta-sequence is close to exhausting.
246                  */
247                 if (range_is_exhausted(space)) {
248                         if (!seq->lss_cli) {
249                                 CERROR("%s: No sequence controller "
250                                        "is attached.\n", seq->lss_name);
251                                 RETURN(-ENODEV);
252                         }
253
254                         rc = seq_client_alloc_super(seq->lss_cli, env);
255                         if (rc) {
256                                 CERROR("%s: Can't allocate super-sequence, "
257                                        "rc %d\n", seq->lss_name, rc);
258                                 RETURN(rc);
259                         }
260
261                         /* Saving new range to allocation space. */
262                         *space = seq->lss_cli->lcs_space;
263                         LASSERT(range_is_sane(space));
264                 }
265
266                 range_alloc(out, space, seq->lss_width);
267         }
268
269         rc = seq_store_write(seq, env);
270         if (rc) {
271                 CERROR("%s: Can't write space data, rc %d\n",
272                        seq->lss_name, rc);
273         }
274
275         if (rc == 0) {
276                 CDEBUG(D_INFO, "%s: Allocated meta-sequence "
277                        DRANGE"\n", seq->lss_name, PRANGE(out));
278         }
279
280         RETURN(rc);
281 }
282
283 int seq_server_alloc_meta(struct lu_server_seq *seq,
284                           struct lu_range *in,
285                           struct lu_range *out,
286                           const struct lu_env *env)
287 {
288         int rc;
289         ENTRY;
290
291         down(&seq->lss_sem);
292         rc = __seq_server_alloc_meta(seq, in, out, env);
293         up(&seq->lss_sem);
294
295         RETURN(rc);
296 }
297 EXPORT_SYMBOL(seq_server_alloc_meta);
298
299 static int seq_server_handle(struct lu_site *site,
300                              const struct lu_env *env,
301                              __u32 opc, struct lu_range *in,
302                              struct lu_range *out)
303 {
304         int rc;
305         struct md_site *mite;
306         ENTRY;
307
308         mite = lu_site2md(site);
309         switch (opc) {
310         case SEQ_ALLOC_META:
311                 if (!mite->ms_server_seq) {
312                         CERROR("Sequence server is not "
313                                "initialized\n");
314                         RETURN(-EINVAL);
315                 }
316                 rc = seq_server_alloc_meta(mite->ms_server_seq,
317                                            in, out, env);
318                 break;
319         case SEQ_ALLOC_SUPER:
320                 if (!mite->ms_control_seq) {
321                         CERROR("Sequence controller is not "
322                                "initialized\n");
323                         RETURN(-EINVAL);
324                 }
325                 rc = seq_server_alloc_super(mite->ms_control_seq,
326                                             in, out, env);
327                 break;
328         default:
329                 rc = -EINVAL;
330                 break;
331         }
332
333         RETURN(rc);
334 }
335
336 static int seq_req_handle(struct ptlrpc_request *req,
337                           const struct lu_env *env,
338                           struct seq_thread_info *info)
339 {
340         struct lu_range *out, *in = NULL;
341         struct lu_site *site;
342         int rc = -EPROTO;
343         __u32 *opc;
344         ENTRY;
345
346         site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
347         LASSERT(site != NULL);
348                         
349         rc = req_capsule_server_pack(info->sti_pill);
350         if (rc)
351                 RETURN(err_serious(rc));
352
353         opc = req_capsule_client_get(info->sti_pill, &RMF_SEQ_OPC);
354         if (opc != NULL) {
355                 out = req_capsule_server_get(info->sti_pill, &RMF_SEQ_RANGE);
356                 if (out == NULL)
357                         RETURN(err_serious(-EPROTO));
358
359                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
360                         in = req_capsule_client_get(info->sti_pill,
361                                                     &RMF_SEQ_RANGE);
362
363                         LASSERT(!range_is_zero(in) && range_is_sane(in));
364                 }
365
366                 rc = seq_server_handle(site, env, *opc, in, out);
367         } else
368                 rc = err_serious(-EPROTO);
369
370         RETURN(rc);
371 }
372
373 /* context key constructor/destructor: seq_key_init, seq_key_fini */
374 LU_KEY_INIT_FINI(seq, struct seq_thread_info);
375
376 /* context key: seq_thread_key */
377 LU_CONTEXT_KEY_DEFINE(seq, LCT_MD_THREAD);
378
379 static void seq_thread_info_init(struct ptlrpc_request *req,
380                                  struct seq_thread_info *info)
381 {
382         info->sti_pill = &req->rq_pill;
383         /* Init request capsule */
384         req_capsule_init(info->sti_pill, req, RCL_SERVER);
385         req_capsule_set(info->sti_pill, &RQF_SEQ_QUERY);
386 }
387
388 static void seq_thread_info_fini(struct seq_thread_info *info)
389 {
390         req_capsule_fini(info->sti_pill);
391 }
392
393 static int seq_handle(struct ptlrpc_request *req)
394 {
395         const struct lu_env *env;
396         struct seq_thread_info *info;
397         int rc;
398
399         env = req->rq_svc_thread->t_env;
400         LASSERT(env != NULL);
401
402         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
403         LASSERT(info != NULL);
404
405         seq_thread_info_init(req, info);
406         rc = seq_req_handle(req, env, info);
407         seq_thread_info_fini(info);
408
409         return rc;
410 }
411
412 /*
413  * Entry point for handling FLD RPCs called from MDT.
414  */
415 int seq_query(struct com_thread_info *info)
416 {
417         return seq_handle(info->cti_pill->rc_req);
418 }
419 EXPORT_SYMBOL(seq_query);
420
421 static void seq_server_proc_fini(struct lu_server_seq *seq);
422
423 #ifdef LPROCFS
424 static int seq_server_proc_init(struct lu_server_seq *seq)
425 {
426         int rc;
427         ENTRY;
428
429         seq->lss_proc_dir = lprocfs_register(seq->lss_name,
430                                              seq_type_proc_dir,
431                                              NULL, NULL);
432         if (IS_ERR(seq->lss_proc_dir)) {
433                 rc = PTR_ERR(seq->lss_proc_dir);
434                 RETURN(rc);
435         }
436
437         rc = lprocfs_add_vars(seq->lss_proc_dir,
438                               seq_server_proc_list, seq);
439         if (rc) {
440                 CERROR("%s: Can't init sequence manager "
441                        "proc, rc %d\n", seq->lss_name, rc);
442                 GOTO(out_cleanup, rc);
443         }
444
445         RETURN(0);
446
447 out_cleanup:
448         seq_server_proc_fini(seq);
449         return rc;
450 }
451
452 static void seq_server_proc_fini(struct lu_server_seq *seq)
453 {
454         ENTRY;
455         if (seq->lss_proc_dir != NULL) {
456                 if (!IS_ERR(seq->lss_proc_dir))
457                         lprocfs_remove(&seq->lss_proc_dir);
458                 seq->lss_proc_dir = NULL;
459         }
460         EXIT;
461 }
462 #else
463 static int seq_server_proc_init(struct lu_server_seq *seq)
464 {
465         return 0;
466 }
467
468 static void seq_server_proc_fini(struct lu_server_seq *seq)
469 {
470         return;
471 }
472 #endif
473
474 int seq_server_init(struct lu_server_seq *seq,
475                     struct dt_device *dev,
476                     const char *prefix,
477                     enum lu_mgr_type type,
478                     const struct lu_env *env)
479 {
480         int rc, is_srv = (type == LUSTRE_SEQ_SERVER);
481         ENTRY;
482
483         LASSERT(dev != NULL);
484         LASSERT(prefix != NULL);
485
486         seq->lss_cli = NULL;
487         seq->lss_type = type;
488         range_init(&seq->lss_space);
489         sema_init(&seq->lss_sem, 1);
490
491         seq->lss_width = is_srv ?
492                 LUSTRE_SEQ_META_WIDTH : LUSTRE_SEQ_SUPER_WIDTH;
493
494         snprintf(seq->lss_name, sizeof(seq->lss_name),
495                  "%s-%s", (is_srv ? "srv" : "ctl"), prefix);
496
497         rc = seq_store_init(seq, env, dev);
498         if (rc)
499                 GOTO(out, rc);
500
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                 CDEBUG(D_INFO, "%s: No data found "
511                        "on store. Initialize space\n",
512                        seq->lss_name);
513
514                 /* Save default controller value to store. */
515                 rc = seq_store_write(seq, env);
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         lu_context_key_degister(&seq_thread_key);
590         if (seq_type_proc_dir != NULL && !IS_ERR(seq_type_proc_dir)) {
591                 lprocfs_remove(&seq_type_proc_dir);
592                 seq_type_proc_dir = NULL;
593         }
594 }
595
596 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
597 MODULE_DESCRIPTION("Lustre FID Module");
598 MODULE_LICENSE("GPL");
599
600 cfs_module(fid, "0.1.0", fid_mod_init, fid_mod_exit);
601 #endif