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