Whamcloud - gitweb
LU-8562 osp: osp_precreate_thread gets stuck after disconnect
[fs/lustre-release.git] / lustre / osp / osp_precreate.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) 2012, 2016, 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/osp/osp_sync.c
33  *
34  * Lustre OST Proxy Device
35  *
36  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
37  * Author: Mikhail Pershin <mike.pershin@intel.com>
38  * Author: Di Wang <di.wang@intel.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_MDS
42
43 #include <linux/kthread.h>
44
45 #include <lustre_obdo.h>
46
47 #include "osp_internal.h"
48
49 /*
50  * there are two specific states to take care about:
51  *
52  * = import is disconnected =
53  *
54  * = import is inactive =
55  *   in this case osp_declare_object_create() returns an error
56  *
57  */
58
59 /*
60  **
61  * Check whether statfs data is expired
62  *
63  * OSP device caches statfs data for the target, the function checks
64  * whether the data is expired or not.
65  *
66  * \param[in] d         OSP device
67  *
68  * \retval              0 - not expired, 1 - expired
69  */
70 static inline int osp_statfs_need_update(struct osp_device *d)
71 {
72         return !cfs_time_before(cfs_time_current(),
73                                 d->opd_statfs_fresh_till);
74 }
75
76 /*
77  * OSP tries to maintain pool of available objects so that calls to create
78  * objects don't block most of time
79  *
80  * each time OSP gets connected to OST, we should start from precreation cleanup
81  */
82 static inline bool osp_precreate_running(struct osp_device *d)
83 {
84         return !!(d->opd_pre_thread.t_flags & SVC_RUNNING);
85 }
86
87 static inline bool osp_precreate_stopped(struct osp_device *d)
88 {
89         return !!(d->opd_pre_thread.t_flags & SVC_STOPPED);
90 }
91
92 static void osp_statfs_timer_cb(unsigned long _d)
93 {
94         struct osp_device *d = (struct osp_device *) _d;
95
96         LASSERT(d);
97         if (d->opd_pre != NULL && osp_precreate_running(d))
98                 wake_up(&d->opd_pre_waitq);
99 }
100
101 /**
102  * RPC interpret callback for OST_STATFS RPC
103  *
104  * An interpretation callback called by ptlrpc for OST_STATFS RPC when it is
105  * replied by the target. It's used to maintain statfs cache for the target.
106  * The function fills data from the reply if successful and schedules another
107  * update.
108  *
109  * \param[in] env       LU environment provided by the caller
110  * \param[in] req       RPC replied
111  * \param[in] aa        callback data
112  * \param[in] rc        RPC result
113  *
114  * \retval 0            on success
115  * \retval negative     negated errno on error
116  */
117 static int osp_statfs_interpret(const struct lu_env *env,
118                                 struct ptlrpc_request *req,
119                                 union ptlrpc_async_args *aa, int rc)
120 {
121         struct obd_import       *imp = req->rq_import;
122         struct obd_statfs       *msfs;
123         struct osp_device       *d;
124
125         ENTRY;
126
127         aa = ptlrpc_req_async_args(req);
128         d = aa->pointer_arg[0];
129         LASSERT(d);
130
131         if (rc != 0)
132                 GOTO(out, rc);
133
134         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
135         if (msfs == NULL)
136                 GOTO(out, rc = -EPROTO);
137
138         d->opd_statfs = *msfs;
139
140         osp_pre_update_status(d, rc);
141
142         /* schedule next update */
143         d->opd_statfs_fresh_till = cfs_time_shift(d->opd_statfs_maxage);
144         mod_timer(&d->opd_statfs_timer, d->opd_statfs_fresh_till);
145         d->opd_statfs_update_in_progress = 0;
146
147         CDEBUG(D_CACHE, "updated statfs %p\n", d);
148
149         RETURN(0);
150 out:
151         /* couldn't update statfs, try again as soon as possible */
152         if (d->opd_pre != NULL && osp_precreate_running(d))
153                 wake_up(&d->opd_pre_waitq);
154
155         if (req->rq_import_generation == imp->imp_generation)
156                 CDEBUG(D_CACHE, "%s: couldn't update statfs: rc = %d\n",
157                        d->opd_obd->obd_name, rc);
158         RETURN(rc);
159 }
160
161 /**
162  * Send OST_STATFS RPC
163  *
164  * Sends OST_STATFS RPC to refresh cached statfs data for the target.
165  * Also disables scheduled updates as times OSP may need to refresh
166  * statfs data before expiration. The function doesn't block, instead
167  * an interpretation callback osp_statfs_interpret() is used.
168  *
169  * \param[in] d         OSP device
170  */
171 static int osp_statfs_update(struct osp_device *d)
172 {
173         struct ptlrpc_request   *req;
174         struct obd_import       *imp;
175         union ptlrpc_async_args *aa;
176         int                      rc;
177
178         ENTRY;
179
180         CDEBUG(D_CACHE, "going to update statfs\n");
181
182         imp = d->opd_obd->u.cli.cl_import;
183         LASSERT(imp);
184
185         req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
186         if (req == NULL)
187                 RETURN(-ENOMEM);
188
189         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
190         if (rc) {
191                 ptlrpc_request_free(req);
192                 RETURN(rc);
193         }
194         ptlrpc_request_set_replen(req);
195         req->rq_request_portal = OST_CREATE_PORTAL;
196         ptlrpc_at_set_req_timeout(req);
197
198         req->rq_interpret_reply = (ptlrpc_interpterer_t)osp_statfs_interpret;
199         aa = ptlrpc_req_async_args(req);
200         aa->pointer_arg[0] = d;
201
202         /*
203          * no updates till reply
204          */
205         del_timer(&d->opd_statfs_timer);
206         d->opd_statfs_fresh_till = cfs_time_shift(obd_timeout * 1000);
207         d->opd_statfs_update_in_progress = 1;
208
209         ptlrpcd_add_req(req);
210
211         RETURN(0);
212 }
213
214 /**
215  * Schedule an immediate update for statfs data
216  *
217  * If cached statfs data claim no free space, but OSP has got a request to
218  * destroy an object (so release some space probably), then we may need to
219  * refresh cached statfs data sooner than planned. The function checks there
220  * is no statfs update going and schedules immediate update if so.
221  * XXX: there might be a case where removed object(s) do not add free space (empty
222  * object). If the number of such deletions is high, then we can start to update
223  * statfs too often causing a RPC storm. some throttling is needed...
224  *
225  * \param[in] d         OSP device where statfs data needs to be refreshed
226  */
227 void osp_statfs_need_now(struct osp_device *d)
228 {
229         if (!d->opd_statfs_update_in_progress) {
230                 /*
231                  * if current status is -ENOSPC (lack of free space on OST)
232                  * then we should poll OST immediately once object destroy
233                  * is replied
234                  */
235                 d->opd_statfs_fresh_till = cfs_time_shift(-1);
236                 del_timer(&d->opd_statfs_timer);
237                 wake_up(&d->opd_pre_waitq);
238         }
239 }
240
241 /**
242  * Return number of precreated objects
243  *
244  * A simple helper to calculate the number of precreated objects on the device.
245  *
246  * \param[in] env       LU environment provided by the caller
247  * \param[in] osp       OSP device
248  *
249  * \retval              the number of the precreated objects
250  */
251 static inline int osp_objs_precreated(const struct lu_env *env,
252                                       struct osp_device *osp)
253 {
254         return osp_fid_diff(&osp->opd_pre_last_created_fid,
255                             &osp->opd_pre_used_fid);
256 }
257
258 /**
259  * Check pool of precreated objects is nearly empty
260  *
261  * We should not wait till the pool of the precreated objects is exhausted,
262  * because then there will be a long period of OSP being unavailable for the
263  * new creations due to lenghty precreate RPC. Instead we ask for another
264  * precreation ahead and hopefully have it ready before the current pool is
265  * empty. Notice this function relies on an external locking.
266  *
267  * \param[in] env       LU environment provided by the caller
268  * \param[in] d         OSP device
269  *
270  * \retval              0 - current pool is good enough, 1 - time to precreate
271  */
272 static inline int osp_precreate_near_empty_nolock(const struct lu_env *env,
273                                                   struct osp_device *d)
274 {
275         int window = osp_objs_precreated(env, d);
276
277         /* don't consider new precreation till OST is healty and
278          * has free space */
279         return ((window - d->opd_pre_reserved < d->opd_pre_create_count / 2) &&
280                 (d->opd_pre_status == 0));
281 }
282
283 /**
284  * Check pool of precreated objects
285  *
286  * This is protected version of osp_precreate_near_empty_nolock(), check that
287  * for the details.
288  *
289  * \param[in] env       LU environment provided by the caller
290  * \param[in] d         OSP device
291  *
292  * \retval              0 - current pool is good enough, 1 - time to precreate
293  */
294 static inline int osp_precreate_near_empty(const struct lu_env *env,
295                                            struct osp_device *d)
296 {
297         int rc;
298
299         /* XXX: do we really need locking here? */
300         spin_lock(&d->opd_pre_lock);
301         rc = osp_precreate_near_empty_nolock(env, d);
302         spin_unlock(&d->opd_pre_lock);
303         return rc;
304 }
305
306 /**
307  * Check given sequence is empty
308  *
309  * Returns a binary result whether the given sequence has some IDs left
310  * or not. Find the details in osp_fid_end_seq(). This is a lock protected
311  * version of that function.
312  *
313  * \param[in] env       LU environment provided by the caller
314  * \param[in] osp       OSP device
315  *
316  * \retval              0 - current sequence has no IDs, 1 - otherwise
317  */
318 static inline int osp_create_end_seq(const struct lu_env *env,
319                                      struct osp_device *osp)
320 {
321         struct lu_fid *fid = &osp->opd_pre_used_fid;
322         int rc;
323
324         spin_lock(&osp->opd_pre_lock);
325         rc = osp_fid_end_seq(env, fid);
326         spin_unlock(&osp->opd_pre_lock);
327         return rc;
328 }
329
330 /**
331  * Write FID into into last_oid/last_seq file
332  *
333  * The function stores the sequence and the in-sequence id into two dedicated
334  * files. The sync argument can be used to request synchronous commit, so the
335  * function won't return until the updates are committed.
336  *
337  * \param[in] env       LU environment provided by the caller
338  * \param[in] osp       OSP device
339  * \param[in] fid       fid where sequence/id is taken
340  * \param[in] sync      update mode: 0 - asynchronously, 1 - synchronously
341  *
342  * \retval 0            on success
343  * \retval negative     negated errno on error
344  **/
345 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
346                                  struct lu_fid *fid, int sync)
347 {
348         struct osp_thread_info  *oti = osp_env_info(env);
349         struct lu_buf      *lb_oid = &oti->osi_lb;
350         struct lu_buf      *lb_oseq = &oti->osi_lb2;
351         loff_t             oid_off;
352         loff_t             oseq_off;
353         struct thandle    *th;
354         int                   rc;
355         ENTRY;
356
357         /* Note: through f_oid is only 32 bits, it will also write 64 bits
358          * for oid to keep compatibility with the previous version. */
359         lb_oid->lb_buf = &fid->f_oid;
360         lb_oid->lb_len = sizeof(u64);
361         oid_off = sizeof(u64) * osp->opd_index;
362
363         lb_oseq->lb_buf = &fid->f_seq;
364         lb_oseq->lb_len = sizeof(u64);
365         oseq_off = sizeof(u64) * osp->opd_index;
366
367         th = dt_trans_create(env, osp->opd_storage);
368         if (IS_ERR(th))
369                 RETURN(PTR_ERR(th));
370
371         th->th_sync |= sync;
372         rc = dt_declare_record_write(env, osp->opd_last_used_oid_file,
373                                      lb_oid, oid_off, th);
374         if (rc != 0)
375                 GOTO(out, rc);
376
377         rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
378                                      lb_oseq, oseq_off, th);
379         if (rc != 0)
380                 GOTO(out, rc);
381
382         rc = dt_trans_start_local(env, osp->opd_storage, th);
383         if (rc != 0)
384                 GOTO(out, rc);
385
386         rc = dt_record_write(env, osp->opd_last_used_oid_file, lb_oid,
387                              &oid_off, th);
388         if (rc != 0) {
389                 CERROR("%s: can not write to last seq file: rc = %d\n",
390                         osp->opd_obd->obd_name, rc);
391                 GOTO(out, rc);
392         }
393         rc = dt_record_write(env, osp->opd_last_used_seq_file, lb_oseq,
394                              &oseq_off, th);
395         if (rc) {
396                 CERROR("%s: can not write to last seq file: rc = %d\n",
397                         osp->opd_obd->obd_name, rc);
398                 GOTO(out, rc);
399         }
400 out:
401         dt_trans_stop(env, osp->opd_storage, th);
402         RETURN(rc);
403 }
404
405 /**
406  * Switch to another sequence
407  *
408  * When a current sequence has no available IDs left, OSP has to switch to
409  * another new sequence. OSP requests it using the regular FLDB protocol
410  * and stores synchronously before that is used in precreated. This is needed
411  * to basically have the sequences referenced (not orphaned), otherwise it's
412  * possible that OST has some objects precreated and the clients have data
413  * written to it, but after MDT failover nobody refers those objects and OSP
414  * has no idea that the sequence need cleanup to be done.
415  * While this is very expensive operation, it's supposed to happen very very
416  * infrequently because sequence has 2^32 or 2^48 objects (depending on type)
417  *
418  * \param[in] env       LU environment provided by the caller
419  * \param[in] osp       OSP device
420  *
421  * \retval 0            on success
422  * \retval negative     negated errno on error
423  */
424 static int osp_precreate_rollover_new_seq(struct lu_env *env,
425                                           struct osp_device *osp)
426 {
427         struct lu_fid   *fid = &osp_env_info(env)->osi_fid;
428         struct lu_fid   *last_fid = &osp->opd_last_used_fid;
429         int             rc;
430         ENTRY;
431
432         rc = seq_client_get_seq(env, osp->opd_obd->u.cli.cl_seq, &fid->f_seq);
433         if (rc != 0) {
434                 CERROR("%s: alloc fid error: rc = %d\n",
435                        osp->opd_obd->obd_name, rc);
436                 RETURN(rc);
437         }
438
439         fid->f_oid = 1;
440         fid->f_ver = 0;
441         LASSERTF(fid_seq(fid) != fid_seq(last_fid),
442                  "fid "DFID", last_fid "DFID"\n", PFID(fid),
443                  PFID(last_fid));
444
445         rc = osp_write_last_oid_seq_files(env, osp, fid, 1);
446         if (rc != 0) {
447                 CERROR("%s: Can not update oid/seq file: rc = %d\n",
448                        osp->opd_obd->obd_name, rc);
449                 RETURN(rc);
450         }
451
452         LCONSOLE_INFO("%s: update sequence from %#llx to %#llx\n",
453                       osp->opd_obd->obd_name, fid_seq(last_fid),
454                       fid_seq(fid));
455         /* Update last_xxx to the new seq */
456         spin_lock(&osp->opd_pre_lock);
457         osp->opd_last_used_fid = *fid;
458         osp->opd_gap_start_fid = *fid;
459         osp->opd_pre_used_fid = *fid;
460         osp->opd_pre_last_created_fid = *fid;
461         spin_unlock(&osp->opd_pre_lock);
462
463         RETURN(rc);
464 }
465
466 /**
467  * Find IDs available in current sequence
468  *
469  * The function calculates the highest possible ID and the number of IDs
470  * available in the current sequence OSP is using. The number is limited
471  * artifically by the caller (grow param) and the number of IDs available
472  * in the sequence by nature. The function doesn't require an external
473  * locking.
474  *
475  * \param[in] env       LU environment provided by the caller
476  * \param[in] osp       OSP device
477  * \param[in] fid       FID the caller wants to start with
478  * \param[in] grow      how many the caller wants
479  * \param[out] fid      the highest calculated FID
480  * \param[out] grow     the number of available IDs calculated
481  *
482  * \retval              0 on success, 1 - the sequence is empty
483  */
484 static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
485                               struct lu_fid *fid, int *grow)
486 {
487         struct osp_thread_info  *osi = osp_env_info(env);
488         __u64                   end;
489         int                     i = 0;
490
491         if (fid_is_idif(fid)) {
492                 struct lu_fid   *last_fid;
493                 struct ost_id   *oi = &osi->osi_oi;
494
495                 spin_lock(&osp->opd_pre_lock);
496                 last_fid = &osp->opd_pre_last_created_fid;
497                 fid_to_ostid(last_fid, oi);
498                 end = min(ostid_id(oi) + *grow, IDIF_MAX_OID);
499                 *grow = end - ostid_id(oi);
500                 ostid_set_id(oi, ostid_id(oi) + *grow);
501                 spin_unlock(&osp->opd_pre_lock);
502
503                 if (*grow == 0)
504                         return 1;
505
506                 ostid_to_fid(fid, oi, osp->opd_index);
507                 return 0;
508         }
509
510         spin_lock(&osp->opd_pre_lock);
511         *fid = osp->opd_pre_last_created_fid;
512         end = fid->f_oid;
513         end = min((end + *grow), (__u64)LUSTRE_DATA_SEQ_MAX_WIDTH);
514         *grow = end - fid->f_oid;
515         fid->f_oid += end - fid->f_oid;
516         spin_unlock(&osp->opd_pre_lock);
517
518         CDEBUG(D_INFO, "Expect %d, actual %d ["DFID" -- "DFID"]\n",
519                *grow, i, PFID(fid), PFID(&osp->opd_pre_last_created_fid));
520
521         return *grow > 0 ? 0 : 1;
522 }
523
524 /**
525  * Prepare and send precreate RPC
526  *
527  * The function finds how many objects should be precreated.  Then allocates,
528  * prepares and schedules precreate RPC synchronously. Upon reply the function
529  * wake ups the threads waiting for the new objects on this target. If the
530  * target wasn't able to create all the objects requested, then the next
531  * precreate will be asking less objects (i.e. slow precreate down).
532  *
533  * \param[in] env       LU environment provided by the caller
534  * \param[in] d         OSP device
535  *
536  * \retval 0            on success
537  * \retval negative     negated errno on error
538  **/
539 static int osp_precreate_send(const struct lu_env *env, struct osp_device *d)
540 {
541         struct osp_thread_info  *oti = osp_env_info(env);
542         struct ptlrpc_request   *req;
543         struct obd_import       *imp;
544         struct ost_body         *body;
545         int                      rc, grow, diff;
546         struct lu_fid           *fid = &oti->osi_fid;
547         ENTRY;
548
549         /* don't precreate new objects till OST healthy and has free space */
550         if (unlikely(d->opd_pre_status)) {
551                 CDEBUG(D_INFO, "%s: don't send new precreate: rc = %d\n",
552                        d->opd_obd->obd_name, d->opd_pre_status);
553                 RETURN(0);
554         }
555
556         /*
557          * if not connection/initialization is compeleted, ignore
558          */
559         imp = d->opd_obd->u.cli.cl_import;
560         LASSERT(imp);
561
562         req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
563         if (req == NULL)
564                 RETURN(-ENOMEM);
565         req->rq_request_portal = OST_CREATE_PORTAL;
566         /* we should not resend create request - anyway we will have delorphan
567          * and kill these objects */
568         req->rq_no_delay = req->rq_no_resend = 1;
569
570         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
571         if (rc) {
572                 ptlrpc_request_free(req);
573                 RETURN(rc);
574         }
575
576         LASSERT(d->opd_pre->osp_pre_delorphan_sent != 0);
577         spin_lock(&d->opd_pre_lock);
578         if (d->opd_pre_create_count > d->opd_pre_max_create_count / 2)
579                 d->opd_pre_create_count = d->opd_pre_max_create_count / 2;
580         grow = d->opd_pre_create_count;
581         spin_unlock(&d->opd_pre_lock);
582
583         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
584         LASSERT(body);
585
586         *fid = d->opd_pre_last_created_fid;
587         rc = osp_precreate_fids(env, d, fid, &grow);
588         if (rc == 1) {
589                 /* Current seq has been used up*/
590                 if (!osp_is_fid_client(d)) {
591                         osp_pre_update_status(d, -ENOSPC);
592                         rc = -ENOSPC;
593                 }
594                 wake_up(&d->opd_pre_waitq);
595                 GOTO(out_req, rc);
596         }
597
598         if (!osp_is_fid_client(d)) {
599                 /* Non-FID client will always send seq 0 because of
600                  * compatiblity */
601                 LASSERTF(fid_is_idif(fid), "Invalid fid "DFID"\n", PFID(fid));
602                 fid->f_seq = 0;
603         }
604
605         fid_to_ostid(fid, &body->oa.o_oi);
606         body->oa.o_valid = OBD_MD_FLGROUP;
607
608         ptlrpc_request_set_replen(req);
609
610         if (OBD_FAIL_CHECK(OBD_FAIL_OSP_FAKE_PRECREATE))
611                 GOTO(ready, rc = 0);
612
613         rc = ptlrpc_queue_wait(req);
614         if (rc) {
615                 CERROR("%s: can't precreate: rc = %d\n", d->opd_obd->obd_name,
616                        rc);
617                 GOTO(out_req, rc);
618         }
619         LASSERT(req->rq_transno == 0);
620
621         body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
622         if (body == NULL)
623                 GOTO(out_req, rc = -EPROTO);
624
625         ostid_to_fid(fid, &body->oa.o_oi, d->opd_index);
626
627 ready:
628         if (osp_fid_diff(fid, &d->opd_pre_used_fid) <= 0) {
629                 CERROR("%s: precreate fid "DFID" < local used fid "DFID
630                        ": rc = %d\n", d->opd_obd->obd_name,
631                        PFID(fid), PFID(&d->opd_pre_used_fid), -ESTALE);
632                 GOTO(out_req, rc = -ESTALE);
633         }
634
635         diff = osp_fid_diff(fid, &d->opd_pre_last_created_fid);
636
637         spin_lock(&d->opd_pre_lock);
638         if (diff < grow) {
639                 /* the OST has not managed to create all the
640                  * objects we asked for */
641                 d->opd_pre_create_count = max(diff, OST_MIN_PRECREATE);
642                 d->opd_pre_create_slow = 1;
643         } else {
644                 /* the OST is able to keep up with the work,
645                  * we could consider increasing create_count
646                  * next time if needed */
647                 d->opd_pre_create_slow = 0;
648         }
649
650         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
651         fid_to_ostid(fid, &body->oa.o_oi);
652
653         d->opd_pre_last_created_fid = *fid;
654         spin_unlock(&d->opd_pre_lock);
655
656         CDEBUG(D_HA, "%s: current precreated pool: "DFID"-"DFID"\n",
657                d->opd_obd->obd_name, PFID(&d->opd_pre_used_fid),
658                PFID(&d->opd_pre_last_created_fid));
659 out_req:
660         /* now we can wakeup all users awaiting for objects */
661         osp_pre_update_status(d, rc);
662         wake_up(&d->opd_pre_user_waitq);
663
664         ptlrpc_req_finished(req);
665         RETURN(rc);
666 }
667
668 /**
669  * Get last precreated object from target (OST)
670  *
671  * Sends synchronous RPC to the target (OST) to learn the last precreated
672  * object. This later is used to remove all unused objects (cleanup orphan
673  * procedure). Also, the next object after one we got will be used as a
674  * starting point for the new precreates.
675  *
676  * \param[in] env       LU environment provided by the caller
677  * \param[in] d         OSP device
678  *
679  * \retval 0            on success
680  * \retval negative     negated errno on error
681  **/
682 static int osp_get_lastfid_from_ost(const struct lu_env *env,
683                                     struct osp_device *d)
684 {
685         struct ptlrpc_request   *req = NULL;
686         struct obd_import       *imp;
687         struct lu_fid           *last_fid;
688         char                    *tmp;
689         int                     rc;
690         ENTRY;
691
692         imp = d->opd_obd->u.cli.cl_import;
693         LASSERT(imp);
694
695         req = ptlrpc_request_alloc(imp, &RQF_OST_GET_INFO_LAST_FID);
696         if (req == NULL)
697                 RETURN(-ENOMEM);
698
699         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY, RCL_CLIENT,
700                              sizeof(KEY_LAST_FID));
701
702         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
703         if (rc) {
704                 ptlrpc_request_free(req);
705                 RETURN(rc);
706         }
707
708         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
709         memcpy(tmp, KEY_LAST_FID, sizeof(KEY_LAST_FID));
710
711         req->rq_no_delay = req->rq_no_resend = 1;
712         last_fid = req_capsule_client_get(&req->rq_pill, &RMF_FID);
713         fid_cpu_to_le(last_fid, &d->opd_last_used_fid);
714
715         ptlrpc_request_set_replen(req);
716
717         rc = ptlrpc_queue_wait(req);
718         if (rc) {
719                 /* bad-bad OST.. let sysadm sort this out */
720                 if (rc == -ENOTSUPP) {
721                         CERROR("%s: server does not support FID: rc = %d\n",
722                                d->opd_obd->obd_name, -ENOTSUPP);
723                 }
724                 ptlrpc_set_import_active(imp, 0);
725                 GOTO(out, rc);
726         }
727
728         last_fid = req_capsule_server_get(&req->rq_pill, &RMF_FID);
729         if (last_fid == NULL) {
730                 CERROR("%s: Got last_fid failed.\n", d->opd_obd->obd_name);
731                 GOTO(out, rc = -EPROTO);
732         }
733
734         if (!fid_is_sane(last_fid)) {
735                 CERROR("%s: Got insane last_fid "DFID"\n",
736                        d->opd_obd->obd_name, PFID(last_fid));
737                 GOTO(out, rc = -EPROTO);
738         }
739
740         /* Only update the last used fid, if the OST has objects for
741          * this sequence, i.e. fid_oid > 0 */
742         if (fid_oid(last_fid) > 0)
743                 d->opd_last_used_fid = *last_fid;
744
745         CDEBUG(D_HA, "%s: Got last_fid "DFID"\n", d->opd_obd->obd_name,
746                PFID(last_fid));
747
748 out:
749         ptlrpc_req_finished(req);
750         RETURN(rc);
751 }
752
753 /**
754  * Cleanup orphans on OST
755  *
756  * This function is called in a contex of a dedicated thread handling
757  * all the precreation suff. The function waits till local recovery
758  * is complete, then identify all the unreferenced objects (orphans)
759  * using the highest ID referenced by a local and the highest object
760  * precreated by the target. The found range is a subject to removal
761  * using specially flagged RPC. During this process OSP is marked
762  * unavailable for new objects.
763  *
764  * \param[in] env       LU environment provided by the caller
765  * \param[in] d         OSP device
766  *
767  * \retval 0            on success
768  * \retval negative     negated errno on error
769  */
770 static int osp_precreate_cleanup_orphans(struct lu_env *env,
771                                          struct osp_device *d)
772 {
773         struct osp_thread_info  *osi = osp_env_info(env);
774         struct lu_fid           *last_fid = &osi->osi_fid;
775         struct ptlrpc_request   *req = NULL;
776         struct obd_import       *imp;
777         struct ost_body         *body;
778         struct l_wait_info       lwi = { 0 };
779         int                      update_status = 0;
780         int                      rc;
781         int                      diff;
782         struct lu_fid            fid;
783
784         ENTRY;
785
786         /*
787          * wait for local recovery to finish, so we can cleanup orphans.
788          * orphans are all objects since "last used" (assigned).
789          * consider reserved objects as created otherwise we can get into
790          * a livelock when one blocked thread holding a reservation can
791          * block recovery. see LU-8367 for the details. in some cases this
792          * can result in gaps (i.e. leaked objects), but we've got LFSCK...
793          *
794          * do not allow new reservations because they may end up getting
795          * orphans being cleaned up below. so we block new reservations.
796          */
797         spin_lock(&d->opd_pre_lock);
798         d->opd_pre_recovering = 1;
799         spin_unlock(&d->opd_pre_lock);
800         /*
801          * The locking above makes sure the opd_pre_reserved check below will
802          * catch all osp_precreate_reserve() calls who find
803          * "!opd_pre_recovering".
804          */
805         l_wait_event(d->opd_pre_waitq, d->opd_recovery_completed ||
806                      !osp_precreate_running(d) || d->opd_got_disconnected,
807                      &lwi);
808         if (!osp_precreate_running(d) || d->opd_got_disconnected)
809                 GOTO(out, rc = -EAGAIN);
810
811         *last_fid = d->opd_last_used_fid;
812         /* The OSP should already get the valid seq now */
813         LASSERT(!fid_is_zero(last_fid));
814         if (fid_oid(&d->opd_last_used_fid) < 2) {
815                 /* lastfid looks strange... ask OST */
816                 rc = osp_get_lastfid_from_ost(env, d);
817                 if (rc)
818                         GOTO(out, rc);
819         }
820
821         imp = d->opd_obd->u.cli.cl_import;
822         LASSERT(imp);
823
824         req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
825         if (req == NULL)
826                 GOTO(out, rc = -ENOMEM);
827
828         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
829         if (rc) {
830                 ptlrpc_request_free(req);
831                 req = NULL;
832                 GOTO(out, rc);
833         }
834
835         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
836         if (body == NULL)
837                 GOTO(out, rc = -EPROTO);
838
839         body->oa.o_flags = OBD_FL_DELORPHAN;
840         body->oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
841
842         /* unless this is the very first DELORPHAN (when we really
843          * can destroy some orphans), just tell OST to recreate
844          * missing objects in our precreate pool */
845         spin_lock(&d->opd_pre_lock);
846         if (d->opd_pre->osp_pre_delorphan_sent)
847                 fid = d->opd_pre_last_created_fid;
848         else
849                 fid = d->opd_last_used_fid;
850         spin_unlock(&d->opd_pre_lock);
851         fid_to_ostid(&fid, &body->oa.o_oi);
852
853         CDEBUG(D_HA, "%s: going to cleanup orphans since "DFID"\n",
854                d->opd_obd->obd_name, PFID(&fid));
855
856         ptlrpc_request_set_replen(req);
857
858         /* Don't resend the delorphan req */
859         req->rq_no_resend = req->rq_no_delay = 1;
860
861         rc = ptlrpc_queue_wait(req);
862         if (rc) {
863                 update_status = 1;
864                 GOTO(out, rc);
865         }
866
867         body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
868         if (body == NULL)
869                 GOTO(out, rc = -EPROTO);
870
871         /*
872          * OST provides us with id new pool starts from in body->oa.o_id
873          */
874         ostid_to_fid(last_fid, &body->oa.o_oi, d->opd_index);
875
876         spin_lock(&d->opd_pre_lock);
877         diff = osp_fid_diff(&fid, last_fid);
878         if (diff > 0) {
879                 d->opd_pre_create_count = OST_MIN_PRECREATE + diff;
880                 d->opd_pre_last_created_fid = *last_fid;
881         } else {
882                 d->opd_pre_create_count = OST_MIN_PRECREATE;
883                 d->opd_pre_last_created_fid = *last_fid;
884         }
885         /*
886          * This empties the pre-creation pool and effectively blocks any new
887          * reservations.
888          */
889         LASSERT(fid_oid(&d->opd_pre_last_created_fid) <=
890                 LUSTRE_DATA_SEQ_MAX_WIDTH);
891         if (d->opd_pre->osp_pre_delorphan_sent == 0)
892                 d->opd_pre_used_fid = d->opd_pre_last_created_fid;
893         d->opd_pre_create_slow = 0;
894         spin_unlock(&d->opd_pre_lock);
895         d->opd_pre->osp_pre_delorphan_sent = 1;
896
897         CDEBUG(D_HA, "%s: Got last_id "DFID" from OST, last_created "DFID
898                "last_used is "DFID"\n", d->opd_obd->obd_name, PFID(last_fid),
899                PFID(&d->opd_pre_last_created_fid), PFID(&d->opd_last_used_fid));
900 out:
901         if (req)
902                 ptlrpc_req_finished(req);
903
904         /*
905          * If rc is zero, the pre-creation window should have been emptied.
906          * Since waking up the herd would be useless without pre-created
907          * objects, we defer the signal to osp_precreate_send() in that case.
908          */
909         if (rc != 0) {
910                 if (update_status) {
911                         CERROR("%s: cannot cleanup orphans: rc = %d\n",
912                                d->opd_obd->obd_name, rc);
913                         /* we can't proceed from here, OST seem to
914                          * be in a bad shape, better to wait for
915                          * a new instance of the server and repeat
916                          * from the beginning. notify possible waiters
917                          * this OSP isn't quite functional yet */
918                         osp_pre_update_status(d, rc);
919                 } else {
920                         wake_up(&d->opd_pre_user_waitq);
921                 }
922         } else {
923                 spin_lock(&d->opd_pre_lock);
924                 d->opd_pre_recovering = 0;
925                 spin_unlock(&d->opd_pre_lock);
926         }
927
928         RETURN(rc);
929 }
930
931 /**
932  * Update precreate status using statfs data
933  *
934  * The function decides whether this OSP should be used for new objects.
935  * IOW, whether this OST is used up or has some free space. Cached statfs
936  * data is used to make this decision. If the latest result of statfs
937  * request (rc argument) is not success, then just mark OSP unavailable
938  * right away.
939
940  * Add a bit of hysteresis so this flag isn't continually flapping,
941  * and ensure that new files don't get extremely fragmented due to
942  * only a small amount of available space in the filesystem.
943  * We want to set the ENOSPC when there is less than reserved size
944  * free and clear it when there is at least 2*reserved size free space.
945  * the function updates current precreation status used: functional or not
946  *
947  * \param[in] d         OSP device
948  * \param[in] rc        new precreate status for device \a d
949  *
950  * \retval 0            on success
951  * \retval negative     negated errno on error
952  */
953 void osp_pre_update_status(struct osp_device *d, int rc)
954 {
955         struct obd_statfs       *msfs = &d->opd_statfs;
956         int                      old = d->opd_pre_status;
957         __u64                    available;
958
959         d->opd_pre_status = rc;
960         if (rc)
961                 goto out;
962
963         if (likely(msfs->os_type)) {
964                 if (unlikely(d->opd_reserved_mb_high == 0 &&
965                              d->opd_reserved_mb_low == 0)) {
966                         /* Use ~0.1% by default to disable object allocation,
967                          * and ~0.2% to enable, size in MB, set both watermark
968                          */
969                         spin_lock(&d->opd_pre_lock);
970                         if (d->opd_reserved_mb_high == 0 &&
971                             d->opd_reserved_mb_low == 0) {
972                                 d->opd_reserved_mb_low =
973                                         ((msfs->os_bsize >> 10) *
974                                         msfs->os_blocks) >> 20;
975                                 if (d->opd_reserved_mb_low == 0)
976                                         d->opd_reserved_mb_low = 1;
977                                 d->opd_reserved_mb_high =
978                                         (d->opd_reserved_mb_low << 1) + 1;
979                         }
980                         spin_unlock(&d->opd_pre_lock);
981                 }
982                 /* in MB */
983                 available = (msfs->os_bavail * (msfs->os_bsize >> 10)) >> 10;
984                 if (msfs->os_ffree < 32)
985                         msfs->os_state |= OS_STATE_ENOINO;
986                 else if (msfs->os_ffree > 64)
987                         msfs->os_state &= ~OS_STATE_ENOINO;
988
989                 if (available < d->opd_reserved_mb_low)
990                         msfs->os_state |= OS_STATE_ENOSPC;
991                 else if (available > d->opd_reserved_mb_high)
992                         msfs->os_state &= ~OS_STATE_ENOSPC;
993                 if (msfs->os_state & (OS_STATE_ENOINO | OS_STATE_ENOSPC)) {
994                         d->opd_pre_status = -ENOSPC;
995                         if (old != -ENOSPC)
996                                 CDEBUG(D_INFO, "%s: status: %llu blocks, %llu "
997                                        "free, %llu avail, %llu MB avail, %u "
998                                        "hwm -> %d: rc = %d\n",
999                                        d->opd_obd->obd_name, msfs->os_blocks,
1000                                        msfs->os_bfree, msfs->os_bavail,
1001                                        available, d->opd_reserved_mb_high,
1002                                        d->opd_pre_status, rc);
1003                         CDEBUG(D_INFO,
1004                                "non-committed changes: %u, in progress: %u\n",
1005                                atomic_read(&d->opd_syn_changes),
1006                                atomic_read(&d->opd_syn_rpc_in_progress));
1007                 } else if (unlikely(old == -ENOSPC)) {
1008                         d->opd_pre_status = 0;
1009                         spin_lock(&d->opd_pre_lock);
1010                         d->opd_pre_create_slow = 0;
1011                         d->opd_pre_create_count = OST_MIN_PRECREATE;
1012                         spin_unlock(&d->opd_pre_lock);
1013                         wake_up(&d->opd_pre_waitq);
1014
1015                         CDEBUG(D_INFO, "%s: space available: %llu blocks, %llu"
1016                                " free, %llu avail, %lluMB avail, %u lwm"
1017                                " -> %d: rc = %d\n", d->opd_obd->obd_name,
1018                                msfs->os_blocks, msfs->os_bfree, msfs->os_bavail,
1019                                available, d->opd_reserved_mb_low,
1020                                d->opd_pre_status, rc);
1021                 }
1022         }
1023 out:
1024         wake_up(&d->opd_pre_user_waitq);
1025 }
1026
1027 /**
1028  * Initialize FID for precreation
1029  *
1030  * For a just created new target, a new sequence should be taken.
1031  * The function checks there is no IDIF in use (if the target was
1032  * added with the older version of Lustre), then requests a new
1033  * sequence from FLDB using the regular protocol. Then this new
1034  * sequence is stored on a persisten storage synchronously to prevent
1035  * possible object leakage (for the detail see the description for
1036  * osp_precreate_rollover_new_seq()).
1037  *
1038  * \param[in] osp       OSP device
1039  *
1040  * \retval 0            on success
1041  * \retval negative     negated errno on error
1042  */
1043 int osp_init_pre_fid(struct osp_device *osp)
1044 {
1045         struct lu_env           env;
1046         struct osp_thread_info  *osi;
1047         struct lu_client_seq    *cli_seq;
1048         struct lu_fid           *last_fid;
1049         int                     rc;
1050         ENTRY;
1051
1052         LASSERT(osp->opd_pre != NULL);
1053
1054         /* Let's check if the current last_seq/fid is valid,
1055          * otherwise request new sequence from the controller */
1056         if (osp_is_fid_client(osp) && osp->opd_group != 0) {
1057                 /* Non-MDT0 can only use normal sequence for
1058                  * OST objects */
1059                 if (fid_is_norm(&osp->opd_last_used_fid))
1060                         RETURN(0);
1061         } else {
1062                 /* Initially MDT0 will start with IDIF, after
1063                  * that it will request new sequence from the
1064                  * controller */
1065                 if (fid_is_idif(&osp->opd_last_used_fid) ||
1066                     fid_is_norm(&osp->opd_last_used_fid))
1067                         RETURN(0);
1068         }
1069
1070         if (!fid_is_zero(&osp->opd_last_used_fid))
1071                 CWARN("%s: invalid last used fid "DFID
1072                       ", try to get new sequence.\n",
1073                       osp->opd_obd->obd_name,
1074                       PFID(&osp->opd_last_used_fid));
1075
1076         rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1077         if (rc) {
1078                 CERROR("%s: init env error: rc = %d\n",
1079                        osp->opd_obd->obd_name, rc);
1080                 RETURN(rc);
1081         }
1082
1083         osi = osp_env_info(&env);
1084         last_fid = &osi->osi_fid;
1085         fid_zero(last_fid);
1086         /* For a freshed fs, it will allocate a new sequence first */
1087         if (osp_is_fid_client(osp) && osp->opd_group != 0) {
1088                 cli_seq = osp->opd_obd->u.cli.cl_seq;
1089                 rc = seq_client_get_seq(&env, cli_seq, &last_fid->f_seq);
1090                 if (rc != 0) {
1091                         CERROR("%s: alloc fid error: rc = %d\n",
1092                                osp->opd_obd->obd_name, rc);
1093                         GOTO(out, rc);
1094                 }
1095         } else {
1096                 last_fid->f_seq = fid_idif_seq(0, osp->opd_index);
1097         }
1098         last_fid->f_oid = 1;
1099         last_fid->f_ver = 0;
1100
1101         spin_lock(&osp->opd_pre_lock);
1102         osp->opd_last_used_fid = *last_fid;
1103         osp->opd_pre_used_fid = *last_fid;
1104         osp->opd_pre_last_created_fid = *last_fid;
1105         spin_unlock(&osp->opd_pre_lock);
1106         rc = osp_write_last_oid_seq_files(&env, osp, last_fid, 1);
1107         if (rc != 0) {
1108                 CERROR("%s: write fid error: rc = %d\n",
1109                        osp->opd_obd->obd_name, rc);
1110                 GOTO(out, rc);
1111         }
1112 out:
1113         lu_env_fini(&env);
1114         RETURN(rc);
1115 }
1116
1117 /**
1118  * The core of precreate functionality
1119  *
1120  * The function implements the main precreation loop. Basically it
1121  * involves connecting to the target, precerate FID initialization,
1122  * identifying and removing orphans, then serving precreation. As
1123  * part of the latter, the thread is responsible for statfs data
1124  * updates. The precreation is mostly driven by another threads
1125  * asking for new OST objects - those askers wake the thread when
1126  * the number of precreated objects reach low watermark.
1127  * After a disconnect, the sequence above repeats. This is keep going
1128  * until the thread is requested to stop.
1129  *
1130  * \param[in] _arg      private data the thread (OSP device to handle)
1131  *
1132  * \retval 0            on success
1133  * \retval negative     negated errno on error
1134  */
1135 static int osp_precreate_thread(void *_arg)
1136 {
1137         struct osp_device       *d = _arg;
1138         struct ptlrpc_thread    *thread = &d->opd_pre_thread;
1139         struct l_wait_info       lwi = { 0 };
1140         struct l_wait_info       lwi2 = LWI_TIMEOUT(cfs_time_seconds(5),
1141                                                     back_to_sleep, NULL);
1142         struct lu_env            env;
1143         int                      rc;
1144
1145         ENTRY;
1146
1147         rc = lu_env_init(&env, d->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1148         if (rc) {
1149                 CERROR("%s: init env error: rc = %d\n", d->opd_obd->obd_name,
1150                        rc);
1151                 RETURN(rc);
1152         }
1153
1154         spin_lock(&d->opd_pre_lock);
1155         thread->t_flags = SVC_RUNNING;
1156         spin_unlock(&d->opd_pre_lock);
1157         wake_up(&thread->t_ctl_waitq);
1158
1159         while (osp_precreate_running(d)) {
1160                 /*
1161                  * need to be connected to OST
1162                  */
1163                 while (osp_precreate_running(d)) {
1164                         if (d->opd_pre_recovering &&
1165                             d->opd_imp_connected &&
1166                             !d->opd_got_disconnected)
1167                                 break;
1168                         l_wait_event(d->opd_pre_waitq,
1169                                      !osp_precreate_running(d) ||
1170                                      d->opd_new_connection,
1171                                      &lwi);
1172
1173                         if (!d->opd_new_connection)
1174                                 continue;
1175
1176                         d->opd_new_connection = 0;
1177                         d->opd_got_disconnected = 0;
1178                         break;
1179                 }
1180
1181                 if (!osp_precreate_running(d))
1182                         break;
1183
1184                 LASSERT(d->opd_obd->u.cli.cl_seq != NULL);
1185                 /* Sigh, fid client is not ready yet */
1186                 if (d->opd_obd->u.cli.cl_seq->lcs_exp == NULL)
1187                         continue;
1188
1189                 /* Init fid for osp_precreate if necessary */
1190                 rc = osp_init_pre_fid(d);
1191                 if (rc != 0) {
1192                         class_export_put(d->opd_exp);
1193                         d->opd_obd->u.cli.cl_seq->lcs_exp = NULL;
1194                         CERROR("%s: init pre fid error: rc = %d\n",
1195                                d->opd_obd->obd_name, rc);
1196                         continue;
1197                 }
1198
1199                 if (osp_statfs_update(d)) {
1200                         l_wait_event(d->opd_pre_waitq,
1201                                      !osp_precreate_running(d), &lwi2);
1202                         continue;
1203                 }
1204
1205                 /*
1206                  * Clean up orphans or recreate missing objects.
1207                  */
1208                 rc = osp_precreate_cleanup_orphans(&env, d);
1209                 if (rc != 0) {
1210                         schedule_timeout_interruptible(cfs_time_seconds(1));
1211                         continue;
1212                 }
1213                 /*
1214                  * connected, can handle precreates now
1215                  */
1216                 while (osp_precreate_running(d)) {
1217                         l_wait_event(d->opd_pre_waitq,
1218                                      !osp_precreate_running(d) ||
1219                                      osp_precreate_near_empty(&env, d) ||
1220                                      osp_statfs_need_update(d) ||
1221                                      d->opd_got_disconnected, &lwi);
1222
1223                         if (!osp_precreate_running(d))
1224                                 break;
1225
1226                         /* something happened to the connection
1227                          * have to start from the beginning */
1228                         if (d->opd_got_disconnected)
1229                                 break;
1230
1231                         if (osp_statfs_need_update(d))
1232                                 if (osp_statfs_update(d))
1233                                         break;
1234
1235                         /* To avoid handling different seq in precreate/orphan
1236                          * cleanup, it will hold precreate until current seq is
1237                          * used up. */
1238                         if (unlikely(osp_precreate_end_seq(&env, d) &&
1239                             !osp_create_end_seq(&env, d)))
1240                                 continue;
1241
1242                         if (unlikely(osp_precreate_end_seq(&env, d) &&
1243                                      osp_create_end_seq(&env, d))) {
1244                                 LCONSOLE_INFO("%s:%#llx is used up."
1245                                               " Update to new seq\n",
1246                                               d->opd_obd->obd_name,
1247                                          fid_seq(&d->opd_pre_last_created_fid));
1248                                 rc = osp_precreate_rollover_new_seq(&env, d);
1249                                 if (rc)
1250                                         continue;
1251                         }
1252
1253                         if (osp_precreate_near_empty(&env, d)) {
1254                                 rc = osp_precreate_send(&env, d);
1255                                 /* osp_precreate_send() sets opd_pre_status
1256                                  * in case of error, that prevent the using of
1257                                  * failed device. */
1258                                 if (rc < 0 && rc != -ENOSPC &&
1259                                     rc != -ETIMEDOUT && rc != -ENOTCONN)
1260                                         CERROR("%s: cannot precreate objects:"
1261                                                " rc = %d\n",
1262                                                d->opd_obd->obd_name, rc);
1263                         }
1264                 }
1265         }
1266
1267         thread->t_flags = SVC_STOPPED;
1268         lu_env_fini(&env);
1269         wake_up(&thread->t_ctl_waitq);
1270
1271         RETURN(0);
1272 }
1273
1274 /**
1275  * Check when to stop to wait for precreate objects.
1276  *
1277  * The caller wanting a new OST object can't wait undefinitely. The
1278  * function checks for few conditions including available new OST
1279  * objects, disconnected OST, lack of space with no pending destroys,
1280  * etc. IOW, it checks whether the current OSP state is good to keep
1281  * waiting or it's better to give up.
1282  *
1283  * \param[in] env       LU environment provided by the caller
1284  * \param[in] d         OSP device
1285  *
1286  * \retval              0 - keep waiting, 1 - no luck
1287  */
1288 static int osp_precreate_ready_condition(const struct lu_env *env,
1289                                          struct osp_device *d)
1290 {
1291         if (d->opd_pre_recovering)
1292                 return 0;
1293
1294         /* ready if got enough precreated objects */
1295         /* we need to wait for others (opd_pre_reserved) and our object (+1) */
1296         if (d->opd_pre_reserved + 1 < osp_objs_precreated(env, d))
1297                 return 1;
1298
1299         /* ready if OST reported no space and no destroys in progress */
1300         if (atomic_read(&d->opd_syn_changes) +
1301             atomic_read(&d->opd_syn_rpc_in_progress) == 0 &&
1302             d->opd_pre_status == -ENOSPC)
1303                 return 1;
1304
1305         /* Bail out I/O fails to OST */
1306         if (d->opd_pre_status != 0 &&
1307             d->opd_pre_status != -EAGAIN &&
1308             d->opd_pre_status != -ENODEV &&
1309             d->opd_pre_status != -ENOTCONN &&
1310             d->opd_pre_status != -ENOSPC) {
1311                 /* DEBUG LU-3230 */
1312                 if (d->opd_pre_status != -EIO)
1313                         CERROR("%s: precreate failed opd_pre_status %d\n",
1314                                d->opd_obd->obd_name, d->opd_pre_status);
1315                 return 1;
1316         }
1317
1318         return 0;
1319 }
1320
1321 static int osp_precreate_timeout_condition(void *data)
1322 {
1323         struct osp_device *d = data;
1324
1325         CDEBUG(D_HA, "%s: slow creates, last="DFID", next="DFID", "
1326               "reserved=%llu, syn_changes=%u, "
1327               "syn_rpc_in_progress=%d, status=%d\n",
1328               d->opd_obd->obd_name, PFID(&d->opd_pre_last_created_fid),
1329               PFID(&d->opd_pre_used_fid), d->opd_pre_reserved,
1330               atomic_read(&d->opd_syn_changes),
1331               atomic_read(&d->opd_syn_rpc_in_progress),
1332               d->opd_pre_status);
1333
1334         return 1;
1335 }
1336
1337 /**
1338  * Reserve object in precreate pool
1339  *
1340  * When the caller wants to create a new object on this target (target
1341  * represented by the given OSP), it should declare this intention using
1342  * a regular ->dt_declare_create() OSD API method. Then OSP will be trying
1343  * to reserve an object in the existing precreated pool or wait up to
1344  * obd_timeout for the available object to appear in the pool (a dedicated
1345  * thread will be doing real precreation in background). The object can be
1346  * consumed later with osp_precreate_get_fid() or be released with call to
1347  * lu_object_put(). Notice the function doesn't reserve a specific ID, just
1348  * some ID. The actual ID assignment happen in osp_precreate_get_fid().
1349  * If the space on the target is short and there is a pending object destroy,
1350  * then the function forces local commit to speedup space release (see
1351  * osp_sync.c for the details).
1352  *
1353  * \param[in] env       LU environment provided by the caller
1354  * \param[in] d         OSP device
1355  *
1356  * \retval              0 on success
1357  * \retval              -ENOSPC when no space on OST
1358  * \retval              -EAGAIN try later, slow precreation in progress
1359  * \retval              -EIO when no access to OST
1360  */
1361 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d)
1362 {
1363         struct l_wait_info       lwi;
1364         cfs_time_t               expire = cfs_time_shift(obd_timeout);
1365         int                      precreated, rc;
1366
1367         ENTRY;
1368
1369         LASSERTF(osp_objs_precreated(env, d) >= 0, "Last created FID "DFID
1370                  "Next FID "DFID"\n", PFID(&d->opd_pre_last_created_fid),
1371                  PFID(&d->opd_pre_used_fid));
1372
1373         /* opd_pre_max_create_count 0 to not use specified OST. */
1374         if (d->opd_pre_max_create_count == 0)
1375                 RETURN(-ENOBUFS);
1376
1377         if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_OSP_PRECREATE_WAIT)) {
1378                 if (d->opd_index == cfs_fail_val)
1379                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_OSP_PRECREATE_WAIT,
1380                                          obd_timeout);
1381         }
1382
1383         /*
1384          * wait till:
1385          *  - preallocation is done
1386          *  - no free space expected soon
1387          *  - can't connect to OST for too long (obd_timeout)
1388          *  - OST can allocate fid sequence.
1389          */
1390         while ((rc = d->opd_pre_status) == 0 || rc == -ENOSPC ||
1391                 rc == -ENODEV || rc == -EAGAIN || rc == -ENOTCONN) {
1392
1393                 /*
1394                  * increase number of precreations
1395                  */
1396                 precreated = osp_objs_precreated(env, d);
1397                 if (d->opd_pre_create_count < d->opd_pre_max_create_count &&
1398                     d->opd_pre_create_slow == 0 &&
1399                     precreated <= (d->opd_pre_create_count / 4 + 1)) {
1400                         spin_lock(&d->opd_pre_lock);
1401                         d->opd_pre_create_slow = 1;
1402                         d->opd_pre_create_count *= 2;
1403                         spin_unlock(&d->opd_pre_lock);
1404                 }
1405
1406                 spin_lock(&d->opd_pre_lock);
1407                 precreated = osp_objs_precreated(env, d);
1408                 if (precreated > d->opd_pre_reserved &&
1409                     !d->opd_pre_recovering) {
1410                         d->opd_pre_reserved++;
1411                         spin_unlock(&d->opd_pre_lock);
1412                         rc = 0;
1413
1414                         /* XXX: don't wake up if precreation is in progress */
1415                         if (osp_precreate_near_empty_nolock(env, d) &&
1416                            !osp_precreate_end_seq_nolock(env, d))
1417                                 wake_up(&d->opd_pre_waitq);
1418
1419                         break;
1420                 }
1421                 spin_unlock(&d->opd_pre_lock);
1422
1423                 /*
1424                  * all precreated objects have been used and no-space
1425                  * status leave us no chance to succeed very soon
1426                  * but if there is destroy in progress, then we should
1427                  * wait till that is done - some space might be released
1428                  */
1429                 if (unlikely(rc == -ENOSPC)) {
1430                         if (atomic_read(&d->opd_syn_changes)) {
1431                                 /* force local commit to release space */
1432                                 dt_commit_async(env, d->opd_storage);
1433                         }
1434                         if (atomic_read(&d->opd_syn_rpc_in_progress)) {
1435                                 /* just wait till destroys are done */
1436                                 /* see l_wait_even() few lines below */
1437                         }
1438                         if (atomic_read(&d->opd_syn_changes) +
1439                             atomic_read(&d->opd_syn_rpc_in_progress) == 0) {
1440                                 /* no hope for free space */
1441                                 break;
1442                         }
1443                 }
1444
1445                 /* XXX: don't wake up if precreation is in progress */
1446                 wake_up(&d->opd_pre_waitq);
1447
1448                 lwi = LWI_TIMEOUT(expire - cfs_time_current(),
1449                                 osp_precreate_timeout_condition, d);
1450                 if (cfs_time_aftereq(cfs_time_current(), expire)) {
1451                         rc = -ETIMEDOUT;
1452                         break;
1453                 }
1454
1455                 l_wait_event(d->opd_pre_user_waitq,
1456                              osp_precreate_ready_condition(env, d), &lwi);
1457         }
1458
1459         RETURN(rc);
1460 }
1461
1462 /**
1463  * Get a FID from precreation pool
1464  *
1465  * The function is a companion for osp_precreate_reserve() - it assigns
1466  * a specific FID from the precreate. The function should be called only
1467  * if the call to osp_precreate_reserve() was successful. The function
1468  * updates a local storage to remember the highest object ID referenced
1469  * by the node in the given sequence.
1470  *
1471  * A very importan details: this is supposed to be called once the
1472  * transaction is started, so on-disk update will be atomic with the
1473  * data (like LOVEA) refering this object. Then the object won't be leaked:
1474  * either it's referenced by the committed transaction or it's a subject
1475  * to the orphan cleanup procedure.
1476  *
1477  * \param[in] env       LU environment provided by the caller
1478  * \param[in] d         OSP device
1479  * \param[out] fid      generated FID
1480  *
1481  * \retval 0            on success
1482  * \retval negative     negated errno on error
1483  */
1484 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
1485                           struct lu_fid *fid)
1486 {
1487         /* grab next id from the pool */
1488         spin_lock(&d->opd_pre_lock);
1489
1490         LASSERTF(osp_fid_diff(&d->opd_pre_used_fid,
1491                              &d->opd_pre_last_created_fid) < 0,
1492                  "next fid "DFID" last created fid "DFID"\n",
1493                  PFID(&d->opd_pre_used_fid),
1494                  PFID(&d->opd_pre_last_created_fid));
1495
1496         d->opd_pre_used_fid.f_oid++;
1497         memcpy(fid, &d->opd_pre_used_fid, sizeof(*fid));
1498         d->opd_pre_reserved--;
1499         /*
1500          * last_used_id must be changed along with getting new id otherwise
1501          * we might miscalculate gap causing object loss or leak
1502          */
1503         osp_update_last_fid(d, fid);
1504         spin_unlock(&d->opd_pre_lock);
1505
1506         /*
1507          * probably main thread suspended orphan cleanup till
1508          * all reservations are released, see comment in
1509          * osp_precreate_thread() just before orphan cleanup
1510          */
1511         if (unlikely(d->opd_pre_reserved == 0 && d->opd_pre_status))
1512                 wake_up(&d->opd_pre_waitq);
1513
1514         return 0;
1515 }
1516
1517 /*
1518  * Set size regular attribute on an object
1519  *
1520  * When a striping is created late, it's possible that size is already
1521  * initialized on the file. Then the new striping should inherit size
1522  * from the file. The function sets size on the object using the regular
1523  * protocol (OST_PUNCH).
1524  * XXX: should be re-implemented using OUT ?
1525  *
1526  * \param[in] env       LU environment provided by the caller
1527  * \param[in] dt        object
1528  * \param[in] size      size to set.
1529  *
1530  * \retval 0            on success
1531  * \retval negative     negated errno on error
1532  */
1533 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt,
1534                         __u64 size)
1535 {
1536         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
1537         struct ptlrpc_request   *req = NULL;
1538         struct obd_import       *imp;
1539         struct ost_body         *body;
1540         struct obdo             *oa = NULL;
1541         int                      rc;
1542
1543         ENTRY;
1544
1545         imp = d->opd_obd->u.cli.cl_import;
1546         LASSERT(imp);
1547
1548         req = ptlrpc_request_alloc(imp, &RQF_OST_PUNCH);
1549         if (req == NULL)
1550                 RETURN(-ENOMEM);
1551
1552         /* XXX: capa support? */
1553         /* osc_set_capa_size(req, &RMF_CAPA1, capa); */
1554         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_PUNCH);
1555         if (rc) {
1556                 ptlrpc_request_free(req);
1557                 RETURN(rc);
1558         }
1559
1560         /*
1561          * XXX: decide how do we do here with resend
1562          * if we don't resend, then client may see wrong file size
1563          * if we do resend, then MDS thread can get stuck for quite long
1564          * and if we don't resend, then client will also get -EWOULDBLOCK !!
1565          * (see LU-7975 and sanity/test_27F use cases)
1566          * but let's decide not to resend/delay this truncate request to OST
1567          * and allow Client to decide to resend, in a less agressive way from
1568          * after_reply(), by returning -EINPROGRESS instead of
1569          * -EAGAIN/-EWOULDBLOCK upon return from ptlrpc_queue_wait() at the
1570          * end of this routine
1571          */
1572         req->rq_no_resend = req->rq_no_delay = 1;
1573
1574         req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
1575         ptlrpc_at_set_req_timeout(req);
1576
1577         OBD_ALLOC_PTR(oa);
1578         if (oa == NULL)
1579                 GOTO(out, rc = -ENOMEM);
1580
1581         rc = fid_to_ostid(lu_object_fid(&dt->do_lu), &oa->o_oi);
1582         LASSERT(rc == 0);
1583         oa->o_size = size;
1584         oa->o_blocks = OBD_OBJECT_EOF;
1585         oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1586                       OBD_MD_FLID | OBD_MD_FLGROUP;
1587
1588         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1589         LASSERT(body);
1590         lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
1591
1592         /* XXX: capa support? */
1593         /* osc_pack_capa(req, body, capa); */
1594
1595         ptlrpc_request_set_replen(req);
1596
1597         rc = ptlrpc_queue_wait(req);
1598         if (rc) {
1599                 /* -EWOULDBLOCK/-EAGAIN means OST is unreachable at the moment
1600                  * since we have decided not to resend/delay, but this could
1601                  * lead to wrong size to be seen at Client side and even process
1602                  * trying to open to exit/fail if not itself handling -EAGAIN.
1603                  * So it should be better to return -EINPROGRESS instead and
1604                  * leave the decision to resend at Client side in after_reply()
1605                  */
1606                 if (rc == -EWOULDBLOCK) {
1607                         rc = -EINPROGRESS;
1608                         CDEBUG(D_HA, "returning -EINPROGRESS instead of "
1609                                "-EWOULDBLOCK/-EAGAIN to allow Client to "
1610                                "resend\n");
1611                 } else {
1612                         CERROR("can't punch object: %d\n", rc);
1613                 }
1614         }
1615 out:
1616         ptlrpc_req_finished(req);
1617         if (oa)
1618                 OBD_FREE_PTR(oa);
1619         RETURN(rc);
1620 }
1621
1622 /**
1623  * Initialize precreation functionality of OSP
1624  *
1625  * Prepares all the internal structures and starts the precreate thread
1626  *
1627  * \param[in] d         OSP device
1628  *
1629  * \retval 0            on success
1630  * \retval negative     negated errno on error
1631  */
1632 int osp_init_precreate(struct osp_device *d)
1633 {
1634         struct l_wait_info       lwi = { 0 };
1635         struct task_struct              *task;
1636
1637         ENTRY;
1638
1639         OBD_ALLOC_PTR(d->opd_pre);
1640         if (d->opd_pre == NULL)
1641                 RETURN(-ENOMEM);
1642
1643         /* initially precreation isn't ready */
1644         d->opd_pre_status = -EAGAIN;
1645         fid_zero(&d->opd_pre_used_fid);
1646         d->opd_pre_used_fid.f_oid = 1;
1647         fid_zero(&d->opd_pre_last_created_fid);
1648         d->opd_pre_last_created_fid.f_oid = 1;
1649         d->opd_pre_reserved = 0;
1650         d->opd_got_disconnected = 1;
1651         d->opd_pre_create_slow = 0;
1652         d->opd_pre_create_count = OST_MIN_PRECREATE;
1653         d->opd_pre_min_create_count = OST_MIN_PRECREATE;
1654         d->opd_pre_max_create_count = OST_MAX_PRECREATE;
1655         d->opd_reserved_mb_high = 0;
1656         d->opd_reserved_mb_low = 0;
1657
1658         spin_lock_init(&d->opd_pre_lock);
1659         init_waitqueue_head(&d->opd_pre_waitq);
1660         init_waitqueue_head(&d->opd_pre_user_waitq);
1661         init_waitqueue_head(&d->opd_pre_thread.t_ctl_waitq);
1662
1663         /*
1664          * Initialize statfs-related things
1665          */
1666         d->opd_statfs_maxage = 5; /* default update interval */
1667         d->opd_statfs_fresh_till = cfs_time_shift(-1000);
1668         CDEBUG(D_OTHER, "current %llu, fresh till %llu\n",
1669                (unsigned long long)cfs_time_current(),
1670                (unsigned long long)d->opd_statfs_fresh_till);
1671         setup_timer(&d->opd_statfs_timer, osp_statfs_timer_cb,
1672                     (unsigned long)d);
1673
1674         /*
1675          * start thread handling precreation and statfs updates
1676          */
1677         task = kthread_run(osp_precreate_thread, d,
1678                            "osp-pre-%u-%u", d->opd_index, d->opd_group);
1679         if (IS_ERR(task)) {
1680                 CERROR("can't start precreate thread %ld\n", PTR_ERR(task));
1681                 RETURN(PTR_ERR(task));
1682         }
1683
1684         l_wait_event(d->opd_pre_thread.t_ctl_waitq,
1685                      osp_precreate_running(d) || osp_precreate_stopped(d),
1686                      &lwi);
1687
1688         RETURN(0);
1689 }
1690
1691 /**
1692  * Finish precreate functionality of OSP
1693  *
1694  *
1695  * Asks all the activity (the thread, update timer) to stop, then
1696  * wait till that is done.
1697  *
1698  * \param[in] d         OSP device
1699  */
1700 void osp_precreate_fini(struct osp_device *d)
1701 {
1702         struct ptlrpc_thread *thread;
1703
1704         ENTRY;
1705
1706         del_timer(&d->opd_statfs_timer);
1707
1708         if (d->opd_pre == NULL)
1709                 RETURN_EXIT;
1710
1711         thread = &d->opd_pre_thread;
1712
1713         thread->t_flags = SVC_STOPPING;
1714         wake_up(&d->opd_pre_waitq);
1715
1716         wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1717
1718         OBD_FREE_PTR(d->opd_pre);
1719         d->opd_pre = NULL;
1720
1721         EXIT;
1722 }
1723