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