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