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