Whamcloud - gitweb
LU-5396 lnet: add sparse annotation __user wherever needed
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osp/osp_sync.c
37  *
38  * Lustre OST Proxy Device
39  *
40  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.pershin@intel.com>
42  * Author: Di Wang <di.wang@intel.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_MDS
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  * statfs
61  */
62 static inline int osp_statfs_need_update(struct osp_device *d)
63 {
64         return !cfs_time_before(cfs_time_current(),
65                                 d->opd_statfs_fresh_till);
66 }
67
68 /*
69  * OSP tries to maintain pool of available objects so that calls to create
70  * objects don't block most of time
71  *
72  * each time OSP gets connected to OST, we should start from precreation cleanup
73  */
74 static inline bool osp_precreate_running(struct osp_device *d)
75 {
76         return !!(d->opd_pre_thread.t_flags & SVC_RUNNING);
77 }
78
79 static inline bool osp_precreate_stopped(struct osp_device *d)
80 {
81         return !!(d->opd_pre_thread.t_flags & SVC_STOPPED);
82 }
83
84 static void osp_statfs_timer_cb(unsigned long _d)
85 {
86         struct osp_device *d = (struct osp_device *) _d;
87
88         LASSERT(d);
89         if (d->opd_pre != NULL && osp_precreate_running(d))
90                 wake_up(&d->opd_pre_waitq);
91 }
92
93 static int osp_statfs_interpret(const struct lu_env *env,
94                                 struct ptlrpc_request *req,
95                                 union ptlrpc_async_args *aa, int rc)
96 {
97         struct obd_import       *imp = req->rq_import;
98         struct obd_statfs       *msfs;
99         struct osp_device       *d;
100
101         ENTRY;
102
103         aa = ptlrpc_req_async_args(req);
104         d = aa->pointer_arg[0];
105         LASSERT(d);
106
107         if (rc != 0)
108                 GOTO(out, rc);
109
110         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
111         if (msfs == NULL)
112                 GOTO(out, rc = -EPROTO);
113
114         d->opd_statfs = *msfs;
115
116         osp_pre_update_status(d, rc);
117
118         /* schedule next update */
119         d->opd_statfs_fresh_till = cfs_time_shift(d->opd_statfs_maxage);
120         cfs_timer_arm(&d->opd_statfs_timer, d->opd_statfs_fresh_till);
121         d->opd_statfs_update_in_progress = 0;
122
123         CDEBUG(D_CACHE, "updated statfs %p\n", d);
124
125         RETURN(0);
126 out:
127         /* couldn't update statfs, try again as soon as possible */
128         if (d->opd_pre != NULL && osp_precreate_running(d))
129                 wake_up(&d->opd_pre_waitq);
130
131         if (req->rq_import_generation == imp->imp_generation)
132                 CDEBUG(D_CACHE, "%s: couldn't update statfs: rc = %d\n",
133                        d->opd_obd->obd_name, rc);
134         RETURN(rc);
135 }
136
137 static int osp_statfs_update(struct osp_device *d)
138 {
139         struct ptlrpc_request   *req;
140         struct obd_import       *imp;
141         union ptlrpc_async_args *aa;
142         int                      rc;
143
144         ENTRY;
145
146         CDEBUG(D_CACHE, "going to update statfs\n");
147
148         imp = d->opd_obd->u.cli.cl_import;
149         LASSERT(imp);
150
151         req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
152         if (req == NULL)
153                 RETURN(-ENOMEM);
154
155         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
156         if (rc) {
157                 ptlrpc_request_free(req);
158                 RETURN(rc);
159         }
160         ptlrpc_request_set_replen(req);
161         req->rq_request_portal = OST_CREATE_PORTAL;
162         ptlrpc_at_set_req_timeout(req);
163
164         req->rq_interpret_reply = (ptlrpc_interpterer_t)osp_statfs_interpret;
165         aa = ptlrpc_req_async_args(req);
166         aa->pointer_arg[0] = d;
167
168         /*
169          * no updates till reply
170          */
171         cfs_timer_disarm(&d->opd_statfs_timer);
172         d->opd_statfs_fresh_till = cfs_time_shift(obd_timeout * 1000);
173         d->opd_statfs_update_in_progress = 1;
174
175         ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
176
177         RETURN(0);
178 }
179
180 /*
181  * XXX: there might be a case where removed object(s) do not add free
182  * space (empty object). if the number of such deletions is high, then
183  * we can start to update statfs too often - a rpc storm
184  * TODO: some throttling is needed
185  */
186 void osp_statfs_need_now(struct osp_device *d)
187 {
188         if (!d->opd_statfs_update_in_progress) {
189                 /*
190                  * if current status is -ENOSPC (lack of free space on OST)
191                  * then we should poll OST immediately once object destroy
192                  * is replied
193                  */
194                 d->opd_statfs_fresh_till = cfs_time_shift(-1);
195                 cfs_timer_disarm(&d->opd_statfs_timer);
196                 wake_up(&d->opd_pre_waitq);
197         }
198 }
199
200 static inline int osp_objs_precreated(const struct lu_env *env,
201                                       struct osp_device *osp)
202 {
203         return osp_fid_diff(&osp->opd_pre_last_created_fid,
204                             &osp->opd_pre_used_fid);
205 }
206
207 static inline int osp_precreate_near_empty_nolock(const struct lu_env *env,
208                                                   struct osp_device *d)
209 {
210         int window = osp_objs_precreated(env, d);
211
212         /* don't consider new precreation till OST is healty and
213          * has free space */
214         return ((window - d->opd_pre_reserved < d->opd_pre_grow_count / 2) &&
215                 (d->opd_pre_status == 0));
216 }
217
218 static inline int osp_precreate_near_empty(const struct lu_env *env,
219                                            struct osp_device *d)
220 {
221         int rc;
222
223         /* XXX: do we really need locking here? */
224         spin_lock(&d->opd_pre_lock);
225         rc = osp_precreate_near_empty_nolock(env, d);
226         spin_unlock(&d->opd_pre_lock);
227         return rc;
228 }
229
230 static inline int osp_create_end_seq(const struct lu_env *env,
231                                      struct osp_device *osp)
232 {
233         struct lu_fid *fid = &osp->opd_pre_used_fid;
234         int rc;
235
236         spin_lock(&osp->opd_pre_lock);
237         rc = osp_fid_end_seq(env, fid);
238         spin_unlock(&osp->opd_pre_lock);
239         return rc;
240 }
241
242 /**
243  * Write fid into last_oid/last_seq file.
244  **/
245 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
246                                  struct lu_fid *fid, int sync)
247 {
248         struct osp_thread_info  *oti = osp_env_info(env);
249         struct lu_buf      *lb_oid = &oti->osi_lb;
250         struct lu_buf      *lb_oseq = &oti->osi_lb2;
251         loff_t             oid_off;
252         loff_t             oseq_off;
253         struct thandle    *th;
254         int                   rc;
255         ENTRY;
256
257         /* Note: through f_oid is only 32bits, it will also write
258          * 64 bits for oid to keep compatiblity with the previous
259          * version. */
260         lb_oid->lb_buf = &fid->f_oid;
261         lb_oid->lb_len = sizeof(obd_id);
262         oid_off = sizeof(obd_id) * osp->opd_index;
263
264         lb_oseq->lb_buf = &fid->f_seq;
265         lb_oseq->lb_len = sizeof(obd_id);
266         oseq_off = sizeof(obd_id) * osp->opd_index;
267
268         th = dt_trans_create(env, osp->opd_storage);
269         if (IS_ERR(th))
270                 RETURN(PTR_ERR(th));
271
272         th->th_sync |= sync;
273         rc = dt_declare_record_write(env, osp->opd_last_used_oid_file,
274                                      lb_oid, oid_off, th);
275         if (rc != 0)
276                 GOTO(out, rc);
277
278         rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
279                                      lb_oseq, oseq_off, th);
280         if (rc != 0)
281                 GOTO(out, rc);
282
283         rc = dt_trans_start_local(env, osp->opd_storage, th);
284         if (rc != 0)
285                 GOTO(out, rc);
286
287         rc = dt_record_write(env, osp->opd_last_used_oid_file, lb_oid,
288                              &oid_off, th);
289         if (rc != 0) {
290                 CERROR("%s: can not write to last seq file: rc = %d\n",
291                         osp->opd_obd->obd_name, rc);
292                 GOTO(out, rc);
293         }
294         rc = dt_record_write(env, osp->opd_last_used_seq_file, lb_oseq,
295                              &oseq_off, th);
296         if (rc) {
297                 CERROR("%s: can not write to last seq file: rc = %d\n",
298                         osp->opd_obd->obd_name, rc);
299                 GOTO(out, rc);
300         }
301 out:
302         dt_trans_stop(env, osp->opd_storage, th);
303         RETURN(rc);
304 }
305
306 int osp_precreate_rollover_new_seq(struct lu_env *env, struct osp_device *osp)
307 {
308         struct lu_fid   *fid = &osp_env_info(env)->osi_fid;
309         struct lu_fid   *last_fid = &osp->opd_last_used_fid;
310         int             rc;
311         ENTRY;
312
313         rc = seq_client_get_seq(env, osp->opd_obd->u.cli.cl_seq, &fid->f_seq);
314         if (rc != 0) {
315                 CERROR("%s: alloc fid error: rc = %d\n",
316                        osp->opd_obd->obd_name, rc);
317                 RETURN(rc);
318         }
319
320         fid->f_oid = 1;
321         fid->f_ver = 0;
322         LASSERTF(fid_seq(fid) != fid_seq(last_fid),
323                  "fid "DFID", last_fid "DFID"\n", PFID(fid),
324                  PFID(last_fid));
325
326         rc = osp_write_last_oid_seq_files(env, osp, fid, 1);
327         if (rc != 0) {
328                 CERROR("%s: Can not update oid/seq file: rc = %d\n",
329                        osp->opd_obd->obd_name, rc);
330                 RETURN(rc);
331         }
332
333         LCONSOLE_INFO("%s: update sequence from "LPX64" to "LPX64"\n",
334                       osp->opd_obd->obd_name, fid_seq(last_fid),
335                       fid_seq(fid));
336         /* Update last_xxx to the new seq */
337         spin_lock(&osp->opd_pre_lock);
338         osp->opd_last_used_fid = *fid;
339         osp->opd_gap_start_fid = *fid;
340         osp->opd_pre_used_fid = *fid;
341         osp->opd_pre_last_created_fid = *fid;
342         spin_unlock(&osp->opd_pre_lock);
343
344         RETURN(rc);
345 }
346
347 /**
348  * alloc fids for precreation.
349  * rc = 0 Success, @grow is the count of real allocation.
350  * rc = 1 Current seq is used up.
351  * rc < 0 Other error.
352  **/
353 static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
354                               struct lu_fid *fid, int *grow)
355 {
356         struct osp_thread_info  *osi = osp_env_info(env);
357         __u64                   end;
358         int                     i = 0;
359
360         if (fid_is_idif(fid)) {
361                 struct lu_fid   *last_fid;
362                 struct ost_id   *oi = &osi->osi_oi;
363
364                 spin_lock(&osp->opd_pre_lock);
365                 last_fid = &osp->opd_pre_last_created_fid;
366                 fid_to_ostid(last_fid, oi);
367                 end = min(ostid_id(oi) + *grow, IDIF_MAX_OID);
368                 *grow = end - ostid_id(oi);
369                 ostid_set_id(oi, ostid_id(oi) + *grow);
370                 spin_unlock(&osp->opd_pre_lock);
371
372                 if (*grow == 0)
373                         return 1;
374
375                 ostid_to_fid(fid, oi, osp->opd_index);
376                 return 0;
377         }
378
379         spin_lock(&osp->opd_pre_lock);
380         *fid = osp->opd_pre_last_created_fid;
381         end = fid->f_oid;
382         end = min((end + *grow), (__u64)LUSTRE_DATA_SEQ_MAX_WIDTH);
383         *grow = end - fid->f_oid;
384         fid->f_oid += end - fid->f_oid;
385         spin_unlock(&osp->opd_pre_lock);
386
387         CDEBUG(D_INFO, "Expect %d, actual %d ["DFID" -- "DFID"]\n",
388                *grow, i, PFID(fid), PFID(&osp->opd_pre_last_created_fid));
389
390         return *grow > 0 ? 0 : 1;
391 }
392
393 static int osp_precreate_send(const struct lu_env *env, struct osp_device *d)
394 {
395         struct osp_thread_info  *oti = osp_env_info(env);
396         struct ptlrpc_request   *req;
397         struct obd_import       *imp;
398         struct ost_body         *body;
399         int                      rc, grow, diff;
400         struct lu_fid           *fid = &oti->osi_fid;
401         ENTRY;
402
403         /* don't precreate new objects till OST healthy and has free space */
404         if (unlikely(d->opd_pre_status)) {
405                 CDEBUG(D_INFO, "%s: don't send new precreate: rc = %d\n",
406                        d->opd_obd->obd_name, d->opd_pre_status);
407                 RETURN(0);
408         }
409
410         /*
411          * if not connection/initialization is compeleted, ignore
412          */
413         imp = d->opd_obd->u.cli.cl_import;
414         LASSERT(imp);
415
416         req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
417         if (req == NULL)
418                 RETURN(-ENOMEM);
419         req->rq_request_portal = OST_CREATE_PORTAL;
420         /* we should not resend create request - anyway we will have delorphan
421          * and kill these objects */
422         req->rq_no_delay = req->rq_no_resend = 1;
423
424         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
425         if (rc) {
426                 ptlrpc_request_free(req);
427                 RETURN(rc);
428         }
429
430         spin_lock(&d->opd_pre_lock);
431         if (d->opd_pre_grow_count > d->opd_pre_max_grow_count / 2)
432                 d->opd_pre_grow_count = d->opd_pre_max_grow_count / 2;
433         grow = d->opd_pre_grow_count;
434         spin_unlock(&d->opd_pre_lock);
435
436         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
437         LASSERT(body);
438
439         *fid = d->opd_pre_last_created_fid;
440         rc = osp_precreate_fids(env, d, fid, &grow);
441         if (rc == 1) {
442                 /* Current seq has been used up*/
443                 if (!osp_is_fid_client(d)) {
444                         osp_pre_update_status(d, -ENOSPC);
445                         rc = -ENOSPC;
446                 }
447                 wake_up(&d->opd_pre_waitq);
448                 GOTO(out_req, rc);
449         }
450
451         if (!osp_is_fid_client(d)) {
452                 /* Non-FID client will always send seq 0 because of
453                  * compatiblity */
454                 LASSERTF(fid_is_idif(fid), "Invalid fid "DFID"\n", PFID(fid));
455                 fid->f_seq = 0;
456         }
457
458         fid_to_ostid(fid, &body->oa.o_oi);
459         body->oa.o_valid = OBD_MD_FLGROUP;
460
461         ptlrpc_request_set_replen(req);
462
463         rc = ptlrpc_queue_wait(req);
464         if (rc) {
465                 CERROR("%s: can't precreate: rc = %d\n", d->opd_obd->obd_name,
466                        rc);
467                 GOTO(out_req, rc);
468         }
469         LASSERT(req->rq_transno == 0);
470
471         body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
472         if (body == NULL)
473                 GOTO(out_req, rc = -EPROTO);
474
475         ostid_to_fid(fid, &body->oa.o_oi, d->opd_index);
476         if (osp_fid_diff(fid, &d->opd_pre_used_fid) <= 0) {
477                 CERROR("%s: precreate fid "DFID" < local used fid "DFID
478                        ": rc = %d\n", d->opd_obd->obd_name,
479                        PFID(fid), PFID(&d->opd_pre_used_fid), -ESTALE);
480                 GOTO(out_req, rc = -ESTALE);
481         }
482
483         diff = osp_fid_diff(fid, &d->opd_pre_last_created_fid);
484
485         spin_lock(&d->opd_pre_lock);
486         if (diff < grow) {
487                 /* the OST has not managed to create all the
488                  * objects we asked for */
489                 d->opd_pre_grow_count = max(diff, OST_MIN_PRECREATE);
490                 d->opd_pre_grow_slow = 1;
491         } else {
492                 /* the OST is able to keep up with the work,
493                  * we could consider increasing grow_count
494                  * next time if needed */
495                 d->opd_pre_grow_slow = 0;
496         }
497
498         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
499         fid_to_ostid(fid, &body->oa.o_oi);
500
501         d->opd_pre_last_created_fid = *fid;
502         spin_unlock(&d->opd_pre_lock);
503
504         CDEBUG(D_HA, "%s: current precreated pool: "DFID"-"DFID"\n",
505                d->opd_obd->obd_name, PFID(&d->opd_pre_used_fid),
506                PFID(&d->opd_pre_last_created_fid));
507 out_req:
508         /* now we can wakeup all users awaiting for objects */
509         osp_pre_update_status(d, rc);
510         wake_up(&d->opd_pre_user_waitq);
511
512         ptlrpc_req_finished(req);
513         RETURN(rc);
514 }
515
516 static int osp_get_lastfid_from_ost(const struct lu_env *env,
517                                     struct osp_device *d)
518 {
519         struct ptlrpc_request   *req = NULL;
520         struct obd_import       *imp;
521         struct lu_fid           *last_fid;
522         char                    *tmp;
523         int                     rc;
524         ENTRY;
525
526         imp = d->opd_obd->u.cli.cl_import;
527         LASSERT(imp);
528
529         req = ptlrpc_request_alloc(imp, &RQF_OST_GET_INFO_LAST_FID);
530         if (req == NULL)
531                 RETURN(-ENOMEM);
532
533         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY, RCL_CLIENT,
534                              sizeof(KEY_LAST_FID));
535
536         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
537         if (rc) {
538                 ptlrpc_request_free(req);
539                 RETURN(rc);
540         }
541
542         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
543         memcpy(tmp, KEY_LAST_FID, sizeof(KEY_LAST_FID));
544
545         req->rq_no_delay = req->rq_no_resend = 1;
546         last_fid = req_capsule_client_get(&req->rq_pill, &RMF_FID);
547         fid_cpu_to_le(last_fid, &d->opd_last_used_fid);
548
549         ptlrpc_request_set_replen(req);
550
551         rc = ptlrpc_queue_wait(req);
552         if (rc) {
553                 /* bad-bad OST.. let sysadm sort this out */
554                 if (rc == -ENOTSUPP) {
555                         CERROR("%s: server does not support FID: rc = %d\n",
556                                d->opd_obd->obd_name, -ENOTSUPP);
557                 }
558                 ptlrpc_set_import_active(imp, 0);
559                 GOTO(out, rc);
560         }
561
562         last_fid = req_capsule_server_get(&req->rq_pill, &RMF_FID);
563         if (last_fid == NULL) {
564                 CERROR("%s: Got last_fid failed.\n", d->opd_obd->obd_name);
565                 GOTO(out, rc = -EPROTO);
566         }
567
568         if (!fid_is_sane(last_fid)) {
569                 CERROR("%s: Got insane last_fid "DFID"\n",
570                        d->opd_obd->obd_name, PFID(last_fid));
571                 GOTO(out, rc = -EPROTO);
572         }
573
574         /* Only update the last used fid, if the OST has objects for
575          * this sequence, i.e. fid_oid > 0 */
576         if (fid_oid(last_fid) > 0)
577                 d->opd_last_used_fid = *last_fid;
578
579         CDEBUG(D_HA, "%s: Got last_fid "DFID"\n", d->opd_obd->obd_name,
580                PFID(last_fid));
581
582 out:
583         ptlrpc_req_finished(req);
584         RETURN(rc);
585 }
586
587 /**
588  * asks OST to clean precreate orphans
589  * and gets next id for new objects
590  */
591 static int osp_precreate_cleanup_orphans(struct lu_env *env,
592                                          struct osp_device *d)
593 {
594         struct osp_thread_info  *osi = osp_env_info(env);
595         struct lu_fid           *last_fid = &osi->osi_fid;
596         struct ptlrpc_request   *req = NULL;
597         struct obd_import       *imp;
598         struct ost_body         *body;
599         struct l_wait_info       lwi = { 0 };
600         int                      update_status = 0;
601         int                      rc;
602         int                      diff;
603
604         ENTRY;
605
606         /*
607          * wait for local recovery to finish, so we can cleanup orphans
608          * orphans are all objects since "last used" (assigned), but
609          * there might be objects reserved and in some cases they won't
610          * be used. we can't cleanup them till we're sure they won't be
611          * used. also can't we allow new reservations because they may
612          * end up getting orphans being cleaned up below. so we block
613          * new reservations and wait till all reserved objects either
614          * user or released.
615          */
616         spin_lock(&d->opd_pre_lock);
617         d->opd_pre_recovering = 1;
618         spin_unlock(&d->opd_pre_lock);
619         /*
620          * The locking above makes sure the opd_pre_reserved check below will
621          * catch all osp_precreate_reserve() calls who find
622          * "!opd_pre_recovering".
623          */
624         l_wait_event(d->opd_pre_waitq,
625                      (!d->opd_pre_reserved && d->opd_recovery_completed) ||
626                      !osp_precreate_running(d) || d->opd_got_disconnected,
627                      &lwi);
628         if (!osp_precreate_running(d) || d->opd_got_disconnected)
629                 GOTO(out, rc = -EAGAIN);
630
631         CDEBUG(D_HA, "%s: going to cleanup orphans since "DFID"\n",
632                d->opd_obd->obd_name, PFID(&d->opd_last_used_fid));
633
634         *last_fid = d->opd_last_used_fid;
635         /* The OSP should already get the valid seq now */
636         LASSERT(!fid_is_zero(last_fid));
637         if (fid_oid(&d->opd_last_used_fid) < 2) {
638                 /* lastfid looks strange... ask OST */
639                 rc = osp_get_lastfid_from_ost(env, d);
640                 if (rc)
641                         GOTO(out, rc);
642         }
643
644         imp = d->opd_obd->u.cli.cl_import;
645         LASSERT(imp);
646
647         req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
648         if (req == NULL)
649                 GOTO(out, rc = -ENOMEM);
650
651         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
652         if (rc) {
653                 ptlrpc_request_free(req);
654                 req = NULL;
655                 GOTO(out, rc);
656         }
657
658         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
659         if (body == NULL)
660                 GOTO(out, rc = -EPROTO);
661
662         body->oa.o_flags = OBD_FL_DELORPHAN;
663         body->oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
664
665         fid_to_ostid(&d->opd_last_used_fid, &body->oa.o_oi);
666
667         ptlrpc_request_set_replen(req);
668
669         /* Don't resend the delorphan req */
670         req->rq_no_resend = req->rq_no_delay = 1;
671
672         rc = ptlrpc_queue_wait(req);
673         if (rc) {
674                 update_status = 1;
675                 GOTO(out, rc);
676         }
677
678         body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
679         if (body == NULL)
680                 GOTO(out, rc = -EPROTO);
681
682         /*
683          * OST provides us with id new pool starts from in body->oa.o_id
684          */
685         ostid_to_fid(last_fid, &body->oa.o_oi, d->opd_index);
686
687         spin_lock(&d->opd_pre_lock);
688         diff = osp_fid_diff(&d->opd_last_used_fid, last_fid);
689         if (diff > 0) {
690                 d->opd_pre_grow_count = OST_MIN_PRECREATE + diff;
691                 d->opd_pre_last_created_fid = d->opd_last_used_fid;
692         } else {
693                 d->opd_pre_grow_count = OST_MIN_PRECREATE;
694                 d->opd_pre_last_created_fid = *last_fid;
695         }
696         /*
697          * This empties the pre-creation pool and effectively blocks any new
698          * reservations.
699          */
700         LASSERT(fid_oid(&d->opd_pre_last_created_fid) <=
701                 LUSTRE_DATA_SEQ_MAX_WIDTH);
702         d->opd_pre_used_fid = d->opd_pre_last_created_fid;
703         d->opd_pre_grow_slow = 0;
704         spin_unlock(&d->opd_pre_lock);
705
706         CDEBUG(D_HA, "%s: Got last_id "DFID" from OST, last_created "DFID
707                "last_used is "DFID"\n", d->opd_obd->obd_name, PFID(last_fid),
708                PFID(&d->opd_pre_last_created_fid), PFID(&d->opd_last_used_fid));
709 out:
710         if (req)
711                 ptlrpc_req_finished(req);
712
713         spin_lock(&d->opd_pre_lock);
714         d->opd_pre_recovering = 0;
715         spin_unlock(&d->opd_pre_lock);
716
717         /*
718          * If rc is zero, the pre-creation window should have been emptied.
719          * Since waking up the herd would be useless without pre-created
720          * objects, we defer the signal to osp_precreate_send() in that case.
721          */
722         if (rc != 0) {
723                 if (update_status) {
724                         CERROR("%s: cannot cleanup orphans: rc = %d\n",
725                                d->opd_obd->obd_name, rc);
726                         /* we can't proceed from here, OST seem to
727                          * be in a bad shape, better to wait for
728                          * a new instance of the server and repeat
729                          * from the beginning. notify possible waiters
730                          * this OSP isn't quite functional yet */
731                         osp_pre_update_status(d, rc);
732                 } else {
733                         wake_up(&d->opd_pre_user_waitq);
734                 }
735         }
736
737         RETURN(rc);
738 }
739
740 /*
741  * the function updates current precreation status used: functional or not
742  *
743  * rc is a last code from the transport, rc == 0 meaning transport works
744  * well and users of lod can use objects from this OSP
745  *
746  * the status depends on current usage of OST
747  */
748 void osp_pre_update_status(struct osp_device *d, int rc)
749 {
750         struct obd_statfs       *msfs = &d->opd_statfs;
751         int                      old = d->opd_pre_status;
752         __u64                    used;
753
754         d->opd_pre_status = rc;
755         if (rc)
756                 goto out;
757
758         /* Add a bit of hysteresis so this flag isn't continually flapping,
759          * and ensure that new files don't get extremely fragmented due to
760          * only a small amount of available space in the filesystem.
761          * We want to set the NOSPC flag when there is less than ~0.1% free
762          * and clear it when there is at least ~0.2% free space, so:
763          *                   avail < ~0.1% max          max = avail + used
764          *            1025 * avail < avail + used       used = blocks - free
765          *            1024 * avail < used
766          *            1024 * avail < blocks - free
767          *                   avail < ((blocks - free) >> 10)
768          *
769          * On very large disk, say 16TB 0.1% will be 16 GB. We don't want to
770          * lose that amount of space so in those cases we report no space left
771          * if their is less than 1 GB left.                             */
772         if (likely(msfs->os_type)) {
773                 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
774                                     1 << 30);
775                 if ((msfs->os_ffree < 32) || (msfs->os_bavail < used)) {
776                         d->opd_pre_status = -ENOSPC;
777                         if (old != -ENOSPC)
778                                 CDEBUG(D_INFO, "%s: status: "LPU64" blocks, "
779                                        LPU64" free, "LPU64" used, "LPU64" "
780                                        "avail -> %d: rc = %d\n",
781                                        d->opd_obd->obd_name, msfs->os_blocks,
782                                        msfs->os_bfree, used, msfs->os_bavail,
783                                        d->opd_pre_status, rc);
784                         CDEBUG(D_INFO,
785                                "non-commited changes: %lu, in progress: %u\n",
786                                d->opd_syn_changes, d->opd_syn_rpc_in_progress);
787                 } else if (old == -ENOSPC) {
788                         d->opd_pre_status = 0;
789                         spin_lock(&d->opd_pre_lock);
790                         d->opd_pre_grow_slow = 0;
791                         d->opd_pre_grow_count = OST_MIN_PRECREATE;
792                         spin_unlock(&d->opd_pre_lock);
793                         wake_up(&d->opd_pre_waitq);
794                         CDEBUG(D_INFO, "%s: no space: "LPU64" blocks, "LPU64
795                                " free, "LPU64" used, "LPU64" avail -> %d: "
796                                "rc = %d\n", d->opd_obd->obd_name,
797                                msfs->os_blocks, msfs->os_bfree, used,
798                                msfs->os_bavail, d->opd_pre_status, rc);
799                 }
800         }
801
802 out:
803         wake_up(&d->opd_pre_user_waitq);
804 }
805
806 int osp_init_pre_fid(struct osp_device *osp)
807 {
808         struct lu_env           env;
809         struct osp_thread_info  *osi;
810         struct lu_client_seq    *cli_seq;
811         struct lu_fid           *last_fid;
812         int                     rc;
813         ENTRY;
814
815         LASSERT(osp->opd_pre != NULL);
816
817         /* Return if last_used fid has been initialized */
818         if (!fid_is_zero(&osp->opd_last_used_fid))
819                 RETURN(0);
820
821         rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
822         if (rc) {
823                 CERROR("%s: init env error: rc = %d\n",
824                        osp->opd_obd->obd_name, rc);
825                 RETURN(rc);
826         }
827
828         osi = osp_env_info(&env);
829         last_fid = &osi->osi_fid;
830         fid_zero(last_fid);
831         /* For a freshed fs, it will allocate a new sequence first */
832         if (osp_is_fid_client(osp) && osp->opd_group != 0) {
833                 cli_seq = osp->opd_obd->u.cli.cl_seq;
834                 rc = seq_client_get_seq(&env, cli_seq, &last_fid->f_seq);
835                 if (rc != 0) {
836                         CERROR("%s: alloc fid error: rc = %d\n",
837                                osp->opd_obd->obd_name, rc);
838                         GOTO(out, rc);
839                 }
840         } else {
841                 last_fid->f_seq = fid_idif_seq(0, osp->opd_index);
842         }
843         last_fid->f_oid = 1;
844         last_fid->f_ver = 0;
845
846         spin_lock(&osp->opd_pre_lock);
847         osp->opd_last_used_fid = *last_fid;
848         osp->opd_pre_used_fid = *last_fid;
849         osp->opd_pre_last_created_fid = *last_fid;
850         spin_unlock(&osp->opd_pre_lock);
851         rc = osp_write_last_oid_seq_files(&env, osp, last_fid, 1);
852         if (rc != 0) {
853                 CERROR("%s: write fid error: rc = %d\n",
854                        osp->opd_obd->obd_name, rc);
855                 GOTO(out, rc);
856         }
857 out:
858         lu_env_fini(&env);
859         RETURN(rc);
860 }
861
862 static int osp_precreate_thread(void *_arg)
863 {
864         struct osp_device       *d = _arg;
865         struct ptlrpc_thread    *thread = &d->opd_pre_thread;
866         struct l_wait_info       lwi = { 0 };
867         struct lu_env            env;
868         int                      rc;
869
870         ENTRY;
871
872         rc = lu_env_init(&env, d->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
873         if (rc) {
874                 CERROR("%s: init env error: rc = %d\n", d->opd_obd->obd_name,
875                        rc);
876                 RETURN(rc);
877         }
878
879         spin_lock(&d->opd_pre_lock);
880         thread->t_flags = SVC_RUNNING;
881         spin_unlock(&d->opd_pre_lock);
882         wake_up(&thread->t_ctl_waitq);
883
884         while (osp_precreate_running(d)) {
885                 /*
886                  * need to be connected to OST
887                  */
888                 while (osp_precreate_running(d)) {
889                         l_wait_event(d->opd_pre_waitq,
890                                      !osp_precreate_running(d) ||
891                                      d->opd_new_connection,
892                                      &lwi);
893
894                         if (!d->opd_new_connection)
895                                 continue;
896
897                         d->opd_new_connection = 0;
898                         d->opd_got_disconnected = 0;
899                         break;
900                 }
901
902                 if (!osp_precreate_running(d))
903                         break;
904
905                 LASSERT(d->opd_obd->u.cli.cl_seq != NULL);
906                 /* Sigh, fid client is not ready yet */
907                 if (d->opd_obd->u.cli.cl_seq->lcs_exp == NULL)
908                         continue;
909
910                 /* Init fid for osp_precreate if necessary */
911                 rc = osp_init_pre_fid(d);
912                 if (rc != 0) {
913                         class_export_put(d->opd_exp);
914                         d->opd_obd->u.cli.cl_seq->lcs_exp = NULL;
915                         CERROR("%s: init pre fid error: rc = %d\n",
916                                d->opd_obd->obd_name, rc);
917                         continue;
918                 }
919
920                 osp_statfs_update(d);
921
922                 /*
923                  * Clean up orphans or recreate missing objects.
924                  */
925                 rc = osp_precreate_cleanup_orphans(&env, d);
926                 if (rc != 0)
927                         continue;
928                 /*
929                  * connected, can handle precreates now
930                  */
931                 while (osp_precreate_running(d)) {
932                         l_wait_event(d->opd_pre_waitq,
933                                      !osp_precreate_running(d) ||
934                                      osp_precreate_near_empty(&env, d) ||
935                                      osp_statfs_need_update(d) ||
936                                      d->opd_got_disconnected, &lwi);
937
938                         if (!osp_precreate_running(d))
939                                 break;
940
941                         /* something happened to the connection
942                          * have to start from the beginning */
943                         if (d->opd_got_disconnected)
944                                 break;
945
946                         if (osp_statfs_need_update(d))
947                                 osp_statfs_update(d);
948
949                         /* To avoid handling different seq in precreate/orphan
950                          * cleanup, it will hold precreate until current seq is
951                          * used up. */
952                         if (unlikely(osp_precreate_end_seq(&env, d) &&
953                             !osp_create_end_seq(&env, d)))
954                                 continue;
955
956                         if (unlikely(osp_precreate_end_seq(&env, d) &&
957                                      osp_create_end_seq(&env, d))) {
958                                 LCONSOLE_INFO("%s:"LPX64" is used up."
959                                               " Update to new seq\n",
960                                               d->opd_obd->obd_name,
961                                          fid_seq(&d->opd_pre_last_created_fid));
962                                 rc = osp_precreate_rollover_new_seq(&env, d);
963                                 if (rc)
964                                         continue;
965                         }
966
967                         if (osp_precreate_near_empty(&env, d)) {
968                                 rc = osp_precreate_send(&env, d);
969                                 /* osp_precreate_send() sets opd_pre_status
970                                  * in case of error, that prevent the using of
971                                  * failed device. */
972                                 if (rc < 0 && rc != -ENOSPC &&
973                                     rc != -ETIMEDOUT && rc != -ENOTCONN)
974                                         CERROR("%s: cannot precreate objects:"
975                                                " rc = %d\n",
976                                                d->opd_obd->obd_name, rc);
977                         }
978                 }
979         }
980
981         thread->t_flags = SVC_STOPPED;
982         lu_env_fini(&env);
983         wake_up(&thread->t_ctl_waitq);
984
985         RETURN(0);
986 }
987
988 static int osp_precreate_ready_condition(const struct lu_env *env,
989                                          struct osp_device *d)
990 {
991         if (d->opd_pre_recovering)
992                 return 0;
993
994         /* ready if got enough precreated objects */
995         /* we need to wait for others (opd_pre_reserved) and our object (+1) */
996         if (d->opd_pre_reserved + 1 < osp_objs_precreated(env, d))
997                 return 1;
998
999         /* ready if OST reported no space and no destroys in progress */
1000         if (d->opd_syn_changes + d->opd_syn_rpc_in_progress == 0 &&
1001             d->opd_pre_status == -ENOSPC)
1002                 return 1;
1003
1004         /* Bail out I/O fails to OST */
1005         if (d->opd_pre_status != 0 &&
1006             d->opd_pre_status != -EAGAIN &&
1007             d->opd_pre_status != -ENODEV &&
1008             d->opd_pre_status != -ENOSPC) {
1009                 /* DEBUG LU-3230 */
1010                 if (d->opd_pre_status != -EIO)
1011                         CERROR("%s: precreate failed opd_pre_status %d\n",
1012                                d->opd_obd->obd_name, d->opd_pre_status);
1013                 return 1;
1014         }
1015
1016         return 0;
1017 }
1018
1019 static int osp_precreate_timeout_condition(void *data)
1020 {
1021         struct osp_device *d = data;
1022
1023         CDEBUG(D_HA, "%s: slow creates, last="DFID", next="DFID", "
1024               "reserved="LPU64", syn_changes=%lu, "
1025               "syn_rpc_in_progress=%d, status=%d\n",
1026               d->opd_obd->obd_name, PFID(&d->opd_pre_last_created_fid),
1027               PFID(&d->opd_pre_used_fid), d->opd_pre_reserved,
1028               d->opd_syn_changes, d->opd_syn_rpc_in_progress,
1029               d->opd_pre_status);
1030
1031         return 1;
1032 }
1033
1034 /*
1035  * called to reserve object in the pool
1036  * return codes:
1037  *  ENOSPC - no space on corresponded OST
1038  *  EAGAIN - precreation is in progress, try later
1039  *  EIO    - no access to OST
1040  */
1041 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d)
1042 {
1043         struct l_wait_info       lwi;
1044         cfs_time_t               expire = cfs_time_shift(obd_timeout);
1045         int                      precreated, rc;
1046
1047         ENTRY;
1048
1049         LASSERTF(osp_objs_precreated(env, d) >= 0, "Last created FID "DFID
1050                  "Next FID "DFID"\n", PFID(&d->opd_pre_last_created_fid),
1051                  PFID(&d->opd_pre_used_fid));
1052
1053         /*
1054          * wait till:
1055          *  - preallocation is done
1056          *  - no free space expected soon
1057          *  - can't connect to OST for too long (obd_timeout)
1058          *  - OST can allocate fid sequence.
1059          */
1060         while ((rc = d->opd_pre_status) == 0 || rc == -ENOSPC ||
1061                 rc == -ENODEV || rc == -EAGAIN || rc == -ENOTCONN) {
1062
1063                 /*
1064                  * increase number of precreations
1065                  */
1066                 precreated = osp_objs_precreated(env, d);
1067                 if (d->opd_pre_grow_count < d->opd_pre_max_grow_count &&
1068                     d->opd_pre_grow_slow == 0 &&
1069                     precreated <= (d->opd_pre_grow_count / 4 + 1)) {
1070                         spin_lock(&d->opd_pre_lock);
1071                         d->opd_pre_grow_slow = 1;
1072                         d->opd_pre_grow_count *= 2;
1073                         spin_unlock(&d->opd_pre_lock);
1074                 }
1075
1076                 spin_lock(&d->opd_pre_lock);
1077                 precreated = osp_objs_precreated(env, d);
1078                 if (precreated > d->opd_pre_reserved &&
1079                     !d->opd_pre_recovering) {
1080                         d->opd_pre_reserved++;
1081                         spin_unlock(&d->opd_pre_lock);
1082                         rc = 0;
1083
1084                         /* XXX: don't wake up if precreation is in progress */
1085                         if (osp_precreate_near_empty_nolock(env, d) &&
1086                            !osp_precreate_end_seq_nolock(env, d))
1087                                 wake_up(&d->opd_pre_waitq);
1088
1089                         break;
1090                 }
1091                 spin_unlock(&d->opd_pre_lock);
1092
1093                 /*
1094                  * all precreated objects have been used and no-space
1095                  * status leave us no chance to succeed very soon
1096                  * but if there is destroy in progress, then we should
1097                  * wait till that is done - some space might be released
1098                  */
1099                 if (unlikely(rc == -ENOSPC)) {
1100                         if (d->opd_syn_changes) {
1101                                 /* force local commit to release space */
1102                                 dt_commit_async(env, d->opd_storage);
1103                         }
1104                         if (d->opd_syn_rpc_in_progress) {
1105                                 /* just wait till destroys are done */
1106                                 /* see l_wait_even() few lines below */
1107                         }
1108                         if (d->opd_syn_changes +
1109                             d->opd_syn_rpc_in_progress == 0) {
1110                                 /* no hope for free space */
1111                                 break;
1112                         }
1113                 }
1114
1115                 /* XXX: don't wake up if precreation is in progress */
1116                 wake_up(&d->opd_pre_waitq);
1117
1118                 lwi = LWI_TIMEOUT(expire - cfs_time_current(),
1119                                 osp_precreate_timeout_condition, d);
1120                 if (cfs_time_aftereq(cfs_time_current(), expire)) {
1121                         rc = -ETIMEDOUT;
1122                         break;
1123                 }
1124
1125                 l_wait_event(d->opd_pre_user_waitq,
1126                              osp_precreate_ready_condition(env, d), &lwi);
1127         }
1128
1129         RETURN(rc);
1130 }
1131
1132 /*
1133  * this function relies on reservation made before
1134  */
1135 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
1136                           struct lu_fid *fid)
1137 {
1138         /* grab next id from the pool */
1139         spin_lock(&d->opd_pre_lock);
1140
1141         LASSERTF(osp_fid_diff(&d->opd_pre_used_fid,
1142                              &d->opd_pre_last_created_fid) < 0,
1143                  "next fid "DFID" last created fid "DFID"\n",
1144                  PFID(&d->opd_pre_used_fid),
1145                  PFID(&d->opd_pre_last_created_fid));
1146
1147         d->opd_pre_used_fid.f_oid++;
1148         memcpy(fid, &d->opd_pre_used_fid, sizeof(*fid));
1149         d->opd_pre_reserved--;
1150         /*
1151          * last_used_id must be changed along with getting new id otherwise
1152          * we might miscalculate gap causing object loss or leak
1153          */
1154         osp_update_last_fid(d, fid);
1155         spin_unlock(&d->opd_pre_lock);
1156
1157         /*
1158          * probably main thread suspended orphan cleanup till
1159          * all reservations are released, see comment in
1160          * osp_precreate_thread() just before orphan cleanup
1161          */
1162         if (unlikely(d->opd_pre_reserved == 0 && d->opd_pre_status))
1163                 wake_up(&d->opd_pre_waitq);
1164
1165         return 0;
1166 }
1167
1168 /*
1169  *
1170  */
1171 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt,
1172                         __u64 size)
1173 {
1174         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
1175         struct ptlrpc_request   *req = NULL;
1176         struct obd_import       *imp;
1177         struct ost_body         *body;
1178         struct obdo             *oa = NULL;
1179         int                      rc;
1180
1181         ENTRY;
1182
1183         imp = d->opd_obd->u.cli.cl_import;
1184         LASSERT(imp);
1185
1186         req = ptlrpc_request_alloc(imp, &RQF_OST_PUNCH);
1187         if (req == NULL)
1188                 RETURN(-ENOMEM);
1189
1190         /* XXX: capa support? */
1191         /* osc_set_capa_size(req, &RMF_CAPA1, capa); */
1192         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_PUNCH);
1193         if (rc) {
1194                 ptlrpc_request_free(req);
1195                 RETURN(rc);
1196         }
1197
1198         /*
1199          * XXX: decide how do we do here with resend
1200          * if we don't resend, then client may see wrong file size
1201          * if we do resend, then MDS thread can get stuck for quite long
1202          */
1203         req->rq_no_resend = req->rq_no_delay = 1;
1204
1205         req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
1206         ptlrpc_at_set_req_timeout(req);
1207
1208         OBD_ALLOC_PTR(oa);
1209         if (oa == NULL)
1210                 GOTO(out, rc = -ENOMEM);
1211
1212         rc = fid_to_ostid(lu_object_fid(&dt->do_lu), &oa->o_oi);
1213         LASSERT(rc == 0);
1214         oa->o_size = size;
1215         oa->o_blocks = OBD_OBJECT_EOF;
1216         oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1217                       OBD_MD_FLID | OBD_MD_FLGROUP;
1218
1219         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1220         LASSERT(body);
1221         lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
1222
1223         /* XXX: capa support? */
1224         /* osc_pack_capa(req, body, capa); */
1225
1226         ptlrpc_request_set_replen(req);
1227
1228         rc = ptlrpc_queue_wait(req);
1229         if (rc)
1230                 CERROR("can't punch object: %d\n", rc);
1231 out:
1232         ptlrpc_req_finished(req);
1233         if (oa)
1234                 OBD_FREE_PTR(oa);
1235         RETURN(rc);
1236 }
1237
1238 int osp_init_precreate(struct osp_device *d)
1239 {
1240         struct l_wait_info       lwi = { 0 };
1241         struct task_struct              *task;
1242
1243         ENTRY;
1244
1245         OBD_ALLOC_PTR(d->opd_pre);
1246         if (d->opd_pre == NULL)
1247                 RETURN(-ENOMEM);
1248
1249         /* initially precreation isn't ready */
1250         d->opd_pre_status = -EAGAIN;
1251         fid_zero(&d->opd_pre_used_fid);
1252         d->opd_pre_used_fid.f_oid = 1;
1253         fid_zero(&d->opd_pre_last_created_fid);
1254         d->opd_pre_last_created_fid.f_oid = 1;
1255         d->opd_pre_reserved = 0;
1256         d->opd_got_disconnected = 1;
1257         d->opd_pre_grow_slow = 0;
1258         d->opd_pre_grow_count = OST_MIN_PRECREATE;
1259         d->opd_pre_min_grow_count = OST_MIN_PRECREATE;
1260         d->opd_pre_max_grow_count = OST_MAX_PRECREATE;
1261
1262         spin_lock_init(&d->opd_pre_lock);
1263         init_waitqueue_head(&d->opd_pre_waitq);
1264         init_waitqueue_head(&d->opd_pre_user_waitq);
1265         init_waitqueue_head(&d->opd_pre_thread.t_ctl_waitq);
1266
1267         /*
1268          * Initialize statfs-related things
1269          */
1270         d->opd_statfs_maxage = 5; /* default update interval */
1271         d->opd_statfs_fresh_till = cfs_time_shift(-1000);
1272         CDEBUG(D_OTHER, "current %llu, fresh till %llu\n",
1273                (unsigned long long)cfs_time_current(),
1274                (unsigned long long)d->opd_statfs_fresh_till);
1275         cfs_timer_init(&d->opd_statfs_timer, osp_statfs_timer_cb, d);
1276
1277         /*
1278          * start thread handling precreation and statfs updates
1279          */
1280         task = kthread_run(osp_precreate_thread, d,
1281                            "osp-pre-%u-%u", d->opd_index, d->opd_group);
1282         if (IS_ERR(task)) {
1283                 CERROR("can't start precreate thread %ld\n", PTR_ERR(task));
1284                 RETURN(PTR_ERR(task));
1285         }
1286
1287         l_wait_event(d->opd_pre_thread.t_ctl_waitq,
1288                      osp_precreate_running(d) || osp_precreate_stopped(d),
1289                      &lwi);
1290
1291         RETURN(0);
1292 }
1293
1294 void osp_precreate_fini(struct osp_device *d)
1295 {
1296         struct ptlrpc_thread *thread;
1297
1298         ENTRY;
1299
1300         cfs_timer_disarm(&d->opd_statfs_timer);
1301
1302         if (d->opd_pre == NULL)
1303                 RETURN_EXIT;
1304
1305         thread = &d->opd_pre_thread;
1306
1307         thread->t_flags = SVC_STOPPING;
1308         wake_up(&d->opd_pre_waitq);
1309
1310         wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1311
1312         OBD_FREE_PTR(d->opd_pre);
1313         d->opd_pre = NULL;
1314
1315         EXIT;
1316 }
1317