Merge tag 'asm-generic-4.7' of git://git.kernel.org/pub/scm/linux/kernel/git/arnd...
[linux-2.6-block.git] / drivers / staging / lustre / lustre / fid / fid_request.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, 2015, 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_request.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 "../../include/linux/libcfs/libcfs.h"
46 #include <linux/module.h>
47
48 #include "../include/obd.h"
49 #include "../include/obd_class.h"
50 #include "../include/obd_support.h"
51 #include "../include/lustre_fid.h"
52 /* mdc RPC locks */
53 #include "../include/lustre_mdc.h"
54 #include "fid_internal.h"
55
56 static struct dentry *seq_debugfs_dir;
57
58 static int seq_client_rpc(struct lu_client_seq *seq,
59                           struct lu_seq_range *output, __u32 opc,
60                           const char *opcname)
61 {
62         struct obd_export     *exp = seq->lcs_exp;
63         struct ptlrpc_request *req;
64         struct lu_seq_range   *out, *in;
65         __u32                 *op;
66         unsigned int           debug_mask;
67         int                    rc;
68
69         LASSERT(exp && !IS_ERR(exp));
70         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
71                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
72         if (!req)
73                 return -ENOMEM;
74
75         /* Init operation code */
76         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
77         *op = opc;
78
79         /* Zero out input range, this is not recovery yet. */
80         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
81         range_init(in);
82
83         ptlrpc_request_set_replen(req);
84
85         in->lsr_index = seq->lcs_space.lsr_index;
86         if (seq->lcs_type == LUSTRE_SEQ_METADATA)
87                 fld_range_set_mdt(in);
88         else
89                 fld_range_set_ost(in);
90
91         if (opc == SEQ_ALLOC_SUPER) {
92                 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
93                 req->rq_reply_portal = MDC_REPLY_PORTAL;
94                 /* During allocating super sequence for data object,
95                  * the current thread might hold the export of MDT0(MDT0
96                  * precreating objects on this OST), and it will send the
97                  * request to MDT0 here, so we can not keep resending the
98                  * request here, otherwise if MDT0 is failed(umounted),
99                  * it can not release the export of MDT0
100                  */
101                 if (seq->lcs_type == LUSTRE_SEQ_DATA)
102                         req->rq_no_delay = req->rq_no_resend = 1;
103                 debug_mask = D_CONSOLE;
104         } else {
105                 if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
106                         req->rq_reply_portal = MDC_REPLY_PORTAL;
107                         req->rq_request_portal = SEQ_METADATA_PORTAL;
108                 } else {
109                         req->rq_reply_portal = OSC_REPLY_PORTAL;
110                         req->rq_request_portal = SEQ_DATA_PORTAL;
111                 }
112                 debug_mask = D_INFO;
113         }
114
115         ptlrpc_at_set_req_timeout(req);
116
117         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
118                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
119         rc = ptlrpc_queue_wait(req);
120         if (opc != SEQ_ALLOC_SUPER && seq->lcs_type == LUSTRE_SEQ_METADATA)
121                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
122         if (rc)
123                 goto out_req;
124
125         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
126         *output = *out;
127
128         if (!range_is_sane(output)) {
129                 CERROR("%s: Invalid range received from server: "
130                        DRANGE"\n", seq->lcs_name, PRANGE(output));
131                 rc = -EINVAL;
132                 goto out_req;
133         }
134
135         if (range_is_exhausted(output)) {
136                 CERROR("%s: Range received from server is exhausted: "
137                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
138                 rc = -EINVAL;
139                 goto out_req;
140         }
141
142         CDEBUG_LIMIT(debug_mask, "%s: Allocated %s-sequence "DRANGE"]\n",
143                      seq->lcs_name, opcname, PRANGE(output));
144
145 out_req:
146         ptlrpc_req_finished(req);
147         return rc;
148 }
149
150 /* Request sequence-controller node to allocate new meta-sequence. */
151 static int seq_client_alloc_meta(const struct lu_env *env,
152                                  struct lu_client_seq *seq)
153 {
154         int rc;
155
156         do {
157                 /* If meta server return -EINPROGRESS or EAGAIN,
158                  * it means meta server might not be ready to
159                  * allocate super sequence from sequence controller
160                  * (MDT0)yet
161                  */
162                 rc = seq_client_rpc(seq, &seq->lcs_space,
163                                     SEQ_ALLOC_META, "meta");
164         } while (rc == -EINPROGRESS || rc == -EAGAIN);
165
166         return rc;
167 }
168
169 /* Allocate new sequence for client. */
170 static int seq_client_alloc_seq(const struct lu_env *env,
171                                 struct lu_client_seq *seq, u64 *seqnr)
172 {
173         int rc;
174
175         LASSERT(range_is_sane(&seq->lcs_space));
176
177         if (range_is_exhausted(&seq->lcs_space)) {
178                 rc = seq_client_alloc_meta(env, seq);
179                 if (rc) {
180                         CERROR("%s: Can't allocate new meta-sequence, rc %d\n",
181                                seq->lcs_name, rc);
182                         return rc;
183                 }
184                 CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
185                        seq->lcs_name, PRANGE(&seq->lcs_space));
186         } else {
187                 rc = 0;
188         }
189
190         LASSERT(!range_is_exhausted(&seq->lcs_space));
191         *seqnr = seq->lcs_space.lsr_start;
192         seq->lcs_space.lsr_start += 1;
193
194         CDEBUG(D_INFO, "%s: Allocated sequence [%#llx]\n", seq->lcs_name,
195                *seqnr);
196
197         return rc;
198 }
199
200 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
201                               wait_queue_t *link)
202 {
203         if (seq->lcs_update) {
204                 add_wait_queue(&seq->lcs_waitq, link);
205                 set_current_state(TASK_UNINTERRUPTIBLE);
206                 mutex_unlock(&seq->lcs_mutex);
207
208                 schedule();
209
210                 mutex_lock(&seq->lcs_mutex);
211                 remove_wait_queue(&seq->lcs_waitq, link);
212                 set_current_state(TASK_RUNNING);
213                 return -EAGAIN;
214         }
215         ++seq->lcs_update;
216         mutex_unlock(&seq->lcs_mutex);
217         return 0;
218 }
219
220 static void seq_fid_alloc_fini(struct lu_client_seq *seq)
221 {
222         LASSERT(seq->lcs_update == 1);
223         mutex_lock(&seq->lcs_mutex);
224         --seq->lcs_update;
225         wake_up(&seq->lcs_waitq);
226 }
227
228 /* Allocate new fid on passed client @seq and save it to @fid. */
229 int seq_client_alloc_fid(const struct lu_env *env,
230                          struct lu_client_seq *seq, struct lu_fid *fid)
231 {
232         wait_queue_t link;
233         int rc;
234
235         LASSERT(seq);
236         LASSERT(fid);
237
238         init_waitqueue_entry(&link, current);
239         mutex_lock(&seq->lcs_mutex);
240
241         if (OBD_FAIL_CHECK(OBD_FAIL_SEQ_EXHAUST))
242                 seq->lcs_fid.f_oid = seq->lcs_width;
243
244         while (1) {
245                 u64 seqnr;
246
247                 if (!fid_is_zero(&seq->lcs_fid) &&
248                     fid_oid(&seq->lcs_fid) < seq->lcs_width) {
249                         /* Just bump last allocated fid and return to caller. */
250                         seq->lcs_fid.f_oid += 1;
251                         rc = 0;
252                         break;
253                 }
254
255                 rc = seq_fid_alloc_prep(seq, &link);
256                 if (rc)
257                         continue;
258
259                 rc = seq_client_alloc_seq(env, seq, &seqnr);
260                 if (rc) {
261                         CERROR("%s: Can't allocate new sequence, rc %d\n",
262                                seq->lcs_name, rc);
263                         seq_fid_alloc_fini(seq);
264                         mutex_unlock(&seq->lcs_mutex);
265                         return rc;
266                 }
267
268                 CDEBUG(D_INFO, "%s: Switch to sequence [0x%16.16Lx]\n",
269                        seq->lcs_name, seqnr);
270
271                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
272                 seq->lcs_fid.f_seq = seqnr;
273                 seq->lcs_fid.f_ver = 0;
274
275                 /*
276                  * Inform caller that sequence switch is performed to allow it
277                  * to setup FLD for it.
278                  */
279                 rc = 1;
280
281                 seq_fid_alloc_fini(seq);
282                 break;
283         }
284
285         *fid = seq->lcs_fid;
286         mutex_unlock(&seq->lcs_mutex);
287
288         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
289         return rc;
290 }
291 EXPORT_SYMBOL(seq_client_alloc_fid);
292
293 /*
294  * Finish the current sequence due to disconnect.
295  * See mdc_import_event()
296  */
297 void seq_client_flush(struct lu_client_seq *seq)
298 {
299         wait_queue_t link;
300
301         LASSERT(seq);
302         init_waitqueue_entry(&link, current);
303         mutex_lock(&seq->lcs_mutex);
304
305         while (seq->lcs_update) {
306                 add_wait_queue(&seq->lcs_waitq, &link);
307                 set_current_state(TASK_UNINTERRUPTIBLE);
308                 mutex_unlock(&seq->lcs_mutex);
309
310                 schedule();
311
312                 mutex_lock(&seq->lcs_mutex);
313                 remove_wait_queue(&seq->lcs_waitq, &link);
314                 set_current_state(TASK_RUNNING);
315         }
316
317         fid_zero(&seq->lcs_fid);
318         /**
319          * this id shld not be used for seq range allocation.
320          * set to -1 for dgb check.
321          */
322
323         seq->lcs_space.lsr_index = -1;
324
325         range_init(&seq->lcs_space);
326         mutex_unlock(&seq->lcs_mutex);
327 }
328 EXPORT_SYMBOL(seq_client_flush);
329
330 static void seq_client_debugfs_fini(struct lu_client_seq *seq)
331 {
332         if (!IS_ERR_OR_NULL(seq->lcs_debugfs_entry))
333                 ldebugfs_remove(&seq->lcs_debugfs_entry);
334 }
335
336 static int seq_client_debugfs_init(struct lu_client_seq *seq)
337 {
338         int rc;
339
340         seq->lcs_debugfs_entry = ldebugfs_register(seq->lcs_name,
341                                                    seq_debugfs_dir,
342                                                    NULL, NULL);
343
344         if (IS_ERR_OR_NULL(seq->lcs_debugfs_entry)) {
345                 CERROR("%s: LdebugFS failed in seq-init\n", seq->lcs_name);
346                 rc = seq->lcs_debugfs_entry ? PTR_ERR(seq->lcs_debugfs_entry)
347                                             : -ENOMEM;
348                 seq->lcs_debugfs_entry = NULL;
349                 return rc;
350         }
351
352         rc = ldebugfs_add_vars(seq->lcs_debugfs_entry,
353                                seq_client_debugfs_list, seq);
354         if (rc) {
355                 CERROR("%s: Can't init sequence manager debugfs, rc %d\n",
356                        seq->lcs_name, rc);
357                 goto out_cleanup;
358         }
359
360         return 0;
361
362 out_cleanup:
363         seq_client_debugfs_fini(seq);
364         return rc;
365 }
366
367 static void seq_client_fini(struct lu_client_seq *seq)
368 {
369         seq_client_debugfs_fini(seq);
370
371         if (seq->lcs_exp) {
372                 class_export_put(seq->lcs_exp);
373                 seq->lcs_exp = NULL;
374         }
375 }
376
377 static int seq_client_init(struct lu_client_seq *seq,
378                            struct obd_export *exp,
379                            enum lu_cli_type type,
380                            const char *prefix)
381 {
382         int rc;
383
384         LASSERT(seq);
385         LASSERT(prefix);
386
387         seq->lcs_type = type;
388
389         mutex_init(&seq->lcs_mutex);
390         if (type == LUSTRE_SEQ_METADATA)
391                 seq->lcs_width = LUSTRE_METADATA_SEQ_MAX_WIDTH;
392         else
393                 seq->lcs_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
394
395         init_waitqueue_head(&seq->lcs_waitq);
396         /* Make sure that things are clear before work is started. */
397         seq_client_flush(seq);
398
399         seq->lcs_exp = class_export_get(exp);
400
401         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
402                  "cli-%s", prefix);
403
404         rc = seq_client_debugfs_init(seq);
405         if (rc)
406                 seq_client_fini(seq);
407         return rc;
408 }
409
410 int client_fid_init(struct obd_device *obd,
411                     struct obd_export *exp, enum lu_cli_type type)
412 {
413         struct client_obd *cli = &obd->u.cli;
414         char *prefix;
415         int rc;
416
417         cli->cl_seq = kzalloc(sizeof(*cli->cl_seq), GFP_NOFS);
418         if (!cli->cl_seq)
419                 return -ENOMEM;
420
421         prefix = kzalloc(MAX_OBD_NAME + 5, GFP_NOFS);
422         if (!prefix) {
423                 rc = -ENOMEM;
424                 goto out_free_seq;
425         }
426
427         snprintf(prefix, MAX_OBD_NAME + 5, "cli-%s", obd->obd_name);
428
429         /* Init client side sequence-manager */
430         rc = seq_client_init(cli->cl_seq, exp, type, prefix);
431         kfree(prefix);
432         if (rc)
433                 goto out_free_seq;
434
435         return rc;
436 out_free_seq:
437         kfree(cli->cl_seq);
438         cli->cl_seq = NULL;
439         return rc;
440 }
441 EXPORT_SYMBOL(client_fid_init);
442
443 int client_fid_fini(struct obd_device *obd)
444 {
445         struct client_obd *cli = &obd->u.cli;
446
447         if (cli->cl_seq) {
448                 seq_client_fini(cli->cl_seq);
449                 kfree(cli->cl_seq);
450                 cli->cl_seq = NULL;
451         }
452
453         return 0;
454 }
455 EXPORT_SYMBOL(client_fid_fini);
456
457 static int __init fid_init(void)
458 {
459         seq_debugfs_dir = ldebugfs_register(LUSTRE_SEQ_NAME,
460                                             debugfs_lustre_root,
461                                             NULL, NULL);
462         return PTR_ERR_OR_ZERO(seq_debugfs_dir);
463 }
464
465 static void __exit fid_exit(void)
466 {
467         if (!IS_ERR_OR_NULL(seq_debugfs_dir))
468                 ldebugfs_remove(&seq_debugfs_dir);
469 }
470
471 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
472 MODULE_DESCRIPTION("Lustre File IDentifier");
473 MODULE_VERSION(LUSTRE_VERSION_STRING);
474 MODULE_LICENSE("GPL");
475
476 module_init(fid_init);
477 module_exit(fid_exit);