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