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