Whamcloud - gitweb
5df08e9d11a3d1764ebce4a7c6c17d523f36c4eb
[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) 2011, 2012, Intel, Inc.
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  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include "osp_internal.h"
50
51 /*
52  * there are two specific states to take care about:
53  *
54  * = import is disconnected =
55  *
56  * = import is inactive =
57  *   in this case osp_declare_object_create() returns an error
58  *
59  */
60
61 /*
62  * statfs
63  */
64 static inline int osp_statfs_need_update(struct osp_device *d)
65 {
66         return !cfs_time_before(cfs_time_current(),
67                                 d->opd_statfs_fresh_till);
68 }
69
70 static void osp_statfs_timer_cb(unsigned long _d)
71 {
72         struct osp_device *d = (struct osp_device *) _d;
73
74         LASSERT(d);
75         cfs_waitq_signal(&d->opd_pre_waitq);
76 }
77
78 static int osp_statfs_interpret(const struct lu_env *env,
79                                 struct ptlrpc_request *req,
80                                 union ptlrpc_async_args *aa, int rc)
81 {
82         struct obd_import       *imp = req->rq_import;
83         struct obd_statfs       *msfs;
84         struct osp_device       *d;
85
86         ENTRY;
87
88         aa = ptlrpc_req_async_args(req);
89         d = aa->pointer_arg[0];
90         LASSERT(d);
91
92         if (rc != 0)
93                 GOTO(out, rc);
94
95         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
96         if (msfs == NULL)
97                 GOTO(out, rc = -EPROTO);
98
99         d->opd_statfs = *msfs;
100
101         osp_pre_update_status(d, rc);
102
103         /* schedule next update */
104         d->opd_statfs_fresh_till = cfs_time_shift(d->opd_statfs_maxage);
105         cfs_timer_arm(&d->opd_statfs_timer, d->opd_statfs_fresh_till);
106         d->opd_statfs_update_in_progress = 0;
107
108         CDEBUG(D_CACHE, "updated statfs %p\n", d);
109
110         RETURN(0);
111 out:
112         /* couldn't update statfs, try again as soon as possible */
113         cfs_waitq_signal(&d->opd_pre_waitq);
114         if (req->rq_import_generation == imp->imp_generation)
115                 CERROR("%s: couldn't update statfs: rc = %d\n",
116                        d->opd_obd->obd_name, rc);
117         RETURN(rc);
118 }
119
120 static int osp_statfs_update(struct osp_device *d)
121 {
122         struct ptlrpc_request   *req;
123         struct obd_import       *imp;
124         union ptlrpc_async_args *aa;
125         int                      rc;
126
127         ENTRY;
128
129         CDEBUG(D_CACHE, "going to update statfs\n");
130
131         imp = d->opd_obd->u.cli.cl_import;
132         LASSERT(imp);
133
134         req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
135         if (req == NULL)
136                 RETURN(-ENOMEM);
137
138         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
139         if (rc) {
140                 ptlrpc_request_free(req);
141                 RETURN(rc);
142         }
143         ptlrpc_request_set_replen(req);
144         req->rq_request_portal = OST_CREATE_PORTAL;
145         ptlrpc_at_set_req_timeout(req);
146
147         req->rq_interpret_reply = (ptlrpc_interpterer_t)osp_statfs_interpret;
148         aa = ptlrpc_req_async_args(req);
149         aa->pointer_arg[0] = d;
150
151         /*
152          * no updates till reply
153          */
154         cfs_timer_disarm(&d->opd_statfs_timer);
155         d->opd_statfs_fresh_till = cfs_time_shift(obd_timeout * 1000);
156         d->opd_statfs_update_in_progress = 1;
157
158         ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
159
160         RETURN(0);
161 }
162
163 /*
164  * XXX: there might be a case where removed object(s) do not add free
165  * space (empty object). if the number of such deletions is high, then
166  * we can start to update statfs too often - a rpc storm
167  * TODO: some throttling is needed
168  */
169 void osp_statfs_need_now(struct osp_device *d)
170 {
171         if (!d->opd_statfs_update_in_progress) {
172                 /*
173                  * if current status is -ENOSPC (lack of free space on OST)
174                  * then we should poll OST immediately once object destroy
175                  * is replied
176                  */
177                 d->opd_statfs_fresh_till = cfs_time_shift(-1);
178                 cfs_timer_disarm(&d->opd_statfs_timer);
179                 cfs_waitq_signal(&d->opd_pre_waitq);
180         }
181 }
182
183
184 /*
185  * OSP tries to maintain pool of available objects so that calls to create
186  * objects don't block most of time
187  *
188  * each time OSP gets connected to OST, we should start from precreation cleanup
189  */
190 static inline int osp_precreate_running(struct osp_device *d)
191 {
192         return !!(d->opd_pre_thread.t_flags & SVC_RUNNING);
193 }
194
195 static inline int osp_precreate_stopped(struct osp_device *d)
196 {
197         return !!(d->opd_pre_thread.t_flags & SVC_STOPPED);
198 }
199
200 static inline int osp_precreate_near_empty_nolock(struct osp_device *d)
201 {
202         int window = d->opd_pre_last_created - d->opd_pre_used_id;
203
204         /* don't consider new precreation till OST is healty and
205          * has free space */
206         return ((window - d->opd_pre_reserved < d->opd_pre_grow_count / 2) &&
207                 (d->opd_pre_status == 0));
208 }
209
210 static inline int osp_precreate_near_empty(struct osp_device *d)
211 {
212         int rc;
213
214         /* XXX: do we really need locking here? */
215         cfs_spin_lock(&d->opd_pre_lock);
216         rc = osp_precreate_near_empty_nolock(d);
217         cfs_spin_unlock(&d->opd_pre_lock);
218         return rc;
219 }
220
221 static int osp_precreate_send(struct osp_device *d)
222 {
223         struct ptlrpc_request   *req;
224         struct obd_import       *imp;
225         struct ost_body         *body;
226         int                      rc, grow, diff;
227
228         ENTRY;
229
230         /* don't precreate new objects till OST healthy and has free space */
231         if (unlikely(d->opd_pre_status)) {
232                 CDEBUG(D_INFO, "%s: don't send new precreate: rc = %d\n",
233                        d->opd_obd->obd_name, d->opd_pre_status);
234                 RETURN(0);
235         }
236
237         /*
238          * if not connection/initialization is compeleted, ignore
239          */
240         imp = d->opd_obd->u.cli.cl_import;
241         LASSERT(imp);
242
243         req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
244         if (req == NULL)
245                 RETURN(-ENOMEM);
246         req->rq_request_portal = OST_CREATE_PORTAL;
247         /* we should not resend create request - anyway we will have delorphan
248          * and kill these objects */
249         req->rq_no_delay = req->rq_no_resend = 1;
250
251         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
252         if (rc) {
253                 ptlrpc_request_free(req);
254                 RETURN(rc);
255         }
256
257         cfs_spin_lock(&d->opd_pre_lock);
258         if (d->opd_pre_grow_count > d->opd_pre_max_grow_count / 2)
259                 d->opd_pre_grow_count = d->opd_pre_max_grow_count / 2;
260         grow = d->opd_pre_grow_count;
261         cfs_spin_unlock(&d->opd_pre_lock);
262
263         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
264         LASSERT(body);
265         body->oa.o_id = d->opd_pre_last_created + grow;
266         body->oa.o_seq = FID_SEQ_OST_MDT0; /* XXX: support for CMD? */
267         body->oa.o_valid = OBD_MD_FLGROUP;
268
269         ptlrpc_request_set_replen(req);
270
271         rc = ptlrpc_queue_wait(req);
272         if (rc) {
273                 CERROR("%s: can't precreate: rc = %d\n",
274                        d->opd_obd->obd_name, rc);
275                 GOTO(out_req, rc);
276         }
277         LASSERT(req->rq_transno == 0);
278
279         body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
280         if (body == NULL)
281                 GOTO(out_req, rc = -EPROTO);
282
283         CDEBUG(D_HA, "new last_created %lu\n", (unsigned long) body->oa.o_id);
284         LASSERT(body->oa.o_id > d->opd_pre_used_id);
285
286         diff = body->oa.o_id - d->opd_pre_last_created;
287
288         cfs_spin_lock(&d->opd_pre_lock);
289         if (diff < grow) {
290                 /* the OST has not managed to create all the
291                  * objects we asked for */
292                 d->opd_pre_grow_count = max(diff, OST_MIN_PRECREATE);
293                 d->opd_pre_grow_slow = 1;
294         } else {
295                 /* the OST is able to keep up with the work,
296                  * we could consider increasing grow_count
297                  * next time if needed */
298                 d->opd_pre_grow_slow = 0;
299         }
300         d->opd_pre_last_created = body->oa.o_id;
301         cfs_spin_unlock(&d->opd_pre_lock);
302         CDEBUG(D_OTHER, "current precreated pool: %llu-%llu\n",
303                d->opd_pre_used_id, d->opd_pre_last_created);
304
305 out_req:
306         /* now we can wakeup all users awaiting for objects */
307         osp_pre_update_status(d, rc);
308         cfs_waitq_signal(&d->opd_pre_user_waitq);
309
310         ptlrpc_req_finished(req);
311         RETURN(rc);
312 }
313
314
315 static int osp_get_lastid_from_ost(struct osp_device *d)
316 {
317         struct ptlrpc_request   *req;
318         struct obd_import       *imp;
319         obd_id                  *reply;
320         char                    *tmp;
321         int                      rc;
322
323         imp = d->opd_obd->u.cli.cl_import;
324         LASSERT(imp);
325
326         req = ptlrpc_request_alloc(imp, &RQF_OST_GET_INFO_LAST_ID);
327         if (req == NULL)
328                 RETURN(-ENOMEM);
329
330         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY,
331                              RCL_CLIENT, sizeof(KEY_LAST_ID));
332         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
333         if (rc) {
334                 ptlrpc_request_free(req);
335                 RETURN(rc);
336         }
337
338         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
339         memcpy(tmp, KEY_LAST_ID, sizeof(KEY_LAST_ID));
340
341         req->rq_no_delay = req->rq_no_resend = 1;
342         ptlrpc_request_set_replen(req);
343         rc = ptlrpc_queue_wait(req);
344         if (rc) {
345                 /* bad-bad OST.. let sysadm sort this out */
346                 ptlrpc_set_import_active(imp, 0);
347                 GOTO(out, rc);
348         }
349
350         reply = req_capsule_server_get(&req->rq_pill, &RMF_OBD_ID);
351         if (reply == NULL)
352                 GOTO(out, rc = -EPROTO);
353
354         d->opd_last_used_id = *reply;
355         CDEBUG(D_HA, "%s: got last_id "LPU64" from OST\n",
356                d->opd_obd->obd_name, d->opd_last_used_id);
357
358 out:
359         ptlrpc_req_finished(req);
360         RETURN(rc);
361
362 }
363
364 /**
365  * asks OST to clean precreate orphans
366  * and gets next id for new objects
367  */
368 static int osp_precreate_cleanup_orphans(struct osp_device *d)
369 {
370         struct ptlrpc_request   *req = NULL;
371         struct obd_import       *imp;
372         struct ost_body         *body;
373         int                      rc;
374
375         ENTRY;
376
377         LASSERT(d->opd_recovery_completed);
378         LASSERT(d->opd_pre_reserved == 0);
379
380         CDEBUG(D_HA, "%s: going to cleanup orphans since "LPU64"\n",
381                 d->opd_obd->obd_name, d->opd_last_used_id);
382
383         if (d->opd_last_used_id < 2) {
384                 /* lastid looks strange... ask OST */
385                 rc = osp_get_lastid_from_ost(d);
386                 if (rc)
387                         GOTO(out, rc);
388         }
389
390         imp = d->opd_obd->u.cli.cl_import;
391         LASSERT(imp);
392
393         req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
394         if (req == NULL)
395                 GOTO(out, rc = -ENOMEM);
396
397         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
398         if (rc) {
399                 ptlrpc_request_free(req);
400                 GOTO(out, rc);
401         }
402
403         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
404         if (body == NULL)
405                 GOTO(out, rc = -EPROTO);
406
407         body->oa.o_flags = OBD_FL_DELORPHAN;
408         body->oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
409         body->oa.o_seq = FID_SEQ_OST_MDT0;
410
411         body->oa.o_id = d->opd_last_used_id;
412
413         ptlrpc_request_set_replen(req);
414
415         /* Don't resend the delorphan req */
416         req->rq_no_resend = req->rq_no_delay = 1;
417
418         rc = ptlrpc_queue_wait(req);
419         if (rc)
420                 GOTO(out, rc);
421
422         body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
423         if (body == NULL)
424                 GOTO(out, rc = -EPROTO);
425
426         /*
427          * OST provides us with id new pool starts from in body->oa.o_id
428          */
429         cfs_spin_lock(&d->opd_pre_lock);
430         if (le64_to_cpu(d->opd_last_used_id) > body->oa.o_id) {
431                 d->opd_pre_grow_count = OST_MIN_PRECREATE +
432                                         le64_to_cpu(d->opd_last_used_id) -
433                                         body->oa.o_id;
434                 d->opd_pre_last_created = le64_to_cpu(d->opd_last_used_id) + 1;
435         } else {
436                 d->opd_pre_grow_count = OST_MIN_PRECREATE;
437                 d->opd_pre_last_created = body->oa.o_id + 1;
438         }
439         d->opd_pre_used_id = d->opd_pre_last_created - 1;
440         d->opd_pre_grow_slow = 0;
441         cfs_spin_unlock(&d->opd_pre_lock);
442
443         CDEBUG(D_HA, "Got last_id "LPU64" from OST, last_used is "LPU64
444                ", next "LPU64"\n", body->oa.o_id,
445                le64_to_cpu(d->opd_last_used_id), d->opd_pre_used_id);
446
447 out:
448         if (req)
449                 ptlrpc_req_finished(req);
450
451         RETURN(rc);
452 }
453
454 /*
455  * the function updates current precreation status used: functional or not
456  *
457  * rc is a last code from the transport, rc == 0 meaning transport works
458  * well and users of lod can use objects from this OSP
459  *
460  * the status depends on current usage of OST
461  */
462 void osp_pre_update_status(struct osp_device *d, int rc)
463 {
464         struct obd_statfs       *msfs = &d->opd_statfs;
465         int                      old = d->opd_pre_status;
466         __u64                    used;
467
468         d->opd_pre_status = rc;
469         if (rc)
470                 goto out;
471
472         if (likely(msfs->os_type)) {
473                 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
474                                     1 << 30);
475                 if ((msfs->os_ffree < 32) || (msfs->os_bavail < used)) {
476                         d->opd_pre_status = -ENOSPC;
477                         if (old != -ENOSPC)
478                                 CDEBUG(D_INFO, "%s: status: "LPU64" blocks, "
479                                        LPU64" free, "LPU64" used, "LPU64" "
480                                        "avail -> %d: rc = %d\n",
481                                        d->opd_obd->obd_name, msfs->os_blocks,
482                                        msfs->os_bfree, used, msfs->os_bavail,
483                                        d->opd_pre_status, rc);
484                         CDEBUG(D_INFO,
485                                "non-commited changes: %lu, in progress: %u\n",
486                                d->opd_syn_changes, d->opd_syn_rpc_in_progress);
487                 } else if (old == -ENOSPC) {
488                         d->opd_pre_status = 0;
489                         d->opd_pre_grow_slow = 0;
490                         d->opd_pre_grow_count = OST_MIN_PRECREATE;
491                         cfs_waitq_signal(&d->opd_pre_waitq);
492                         CDEBUG(D_INFO, "%s: no space: "LPU64" blocks, "LPU64
493                                " free, "LPU64" used, "LPU64" avail -> %d: "
494                                "rc = %d\n", d->opd_obd->obd_name,
495                                msfs->os_blocks, msfs->os_bfree, used,
496                                msfs->os_bavail, d->opd_pre_status, rc);
497                 }
498         }
499
500 out:
501         cfs_waitq_signal(&d->opd_pre_user_waitq);
502 }
503
504 static int osp_precreate_thread(void *_arg)
505 {
506         struct osp_device       *d = _arg;
507         struct ptlrpc_thread    *thread = &d->opd_pre_thread;
508         struct l_wait_info       lwi = { 0 };
509         char                     pname[16];
510         int                      rc;
511
512         ENTRY;
513
514         sprintf(pname, "osp-pre-%u\n", d->opd_index);
515         cfs_daemonize(pname);
516
517         cfs_spin_lock(&d->opd_pre_lock);
518         thread->t_flags = SVC_RUNNING;
519         cfs_spin_unlock(&d->opd_pre_lock);
520         cfs_waitq_signal(&thread->t_ctl_waitq);
521
522         while (osp_precreate_running(d)) {
523                 /*
524                  * need to be connected to OST
525                  */
526                 while (osp_precreate_running(d)) {
527                         l_wait_event(d->opd_pre_waitq,
528                                      !osp_precreate_running(d) ||
529                                      d->opd_new_connection, &lwi);
530
531                         if (!osp_precreate_running(d))
532                                 break;
533
534                         if (!d->opd_new_connection)
535                                 continue;
536
537                         /* got connected */
538                         d->opd_new_connection = 0;
539                         d->opd_got_disconnected = 0;
540                         break;
541                 }
542
543                 osp_statfs_update(d);
544
545                 /*
546                  * wait for local recovery to finish, so we can cleanup orphans
547                  * orphans are all objects since "last used" (assigned), but
548                  * there might be objects reserved and in some cases they won't
549                  * be used. we can't cleanup them till we're sure they won't be
550                  * used. so we block new reservations and wait till all reserved
551                  * objects either user or released.
552                  */
553                 l_wait_event(d->opd_pre_waitq, (!d->opd_pre_reserved &&
554                                                 d->opd_recovery_completed) ||
555                              !osp_precreate_running(d) ||
556                              d->opd_got_disconnected, &lwi);
557
558                 if (osp_precreate_running(d) && !d->opd_got_disconnected) {
559                         rc = osp_precreate_cleanup_orphans(d);
560                         if (rc) {
561                                 CERROR("%s: cannot cleanup orphans: rc = %d\n",
562                                        d->opd_obd->obd_name,  rc);
563                                 /* we can't proceed from here, OST seem to
564                                  * be in a bad shape, better to wait for
565                                  * a new instance of the server and repeat
566                                  * from the beginning. notify possible waiters
567                                  * this OSP isn't quite functional yet */
568                                 osp_pre_update_status(d, rc);
569                                 cfs_waitq_signal(&d->opd_pre_user_waitq);
570                                 l_wait_event(d->opd_pre_waitq,
571                                              !osp_precreate_running(d) ||
572                                              d->opd_new_connection, &lwi);
573                                 continue;
574
575                         }
576                 }
577
578                 /*
579                  * connected, can handle precreates now
580                  */
581                 while (osp_precreate_running(d)) {
582                         l_wait_event(d->opd_pre_waitq,
583                                      !osp_precreate_running(d) ||
584                                      osp_precreate_near_empty(d) ||
585                                      osp_statfs_need_update(d) ||
586                                      d->opd_got_disconnected, &lwi);
587
588                         if (!osp_precreate_running(d))
589                                 break;
590
591                         /* something happened to the connection
592                          * have to start from the beginning */
593                         if (d->opd_got_disconnected)
594                                 break;
595
596                         if (osp_statfs_need_update(d))
597                                 osp_statfs_update(d);
598
599                         if (osp_precreate_near_empty(d)) {
600                                 rc = osp_precreate_send(d);
601                                 /* osp_precreate_send() sets opd_pre_status
602                                  * in case of error, that prevent the using of
603                                  * failed device. */
604                                 if (rc != 0 && rc != -ENOSPC &&
605                                     rc != -ETIMEDOUT && rc != -ENOTCONN)
606                                         CERROR("%s: cannot precreate objects:"
607                                                " rc = %d\n",
608                                                d->opd_obd->obd_name, rc);
609                         }
610                 }
611         }
612
613         thread->t_flags = SVC_STOPPED;
614         cfs_waitq_signal(&thread->t_ctl_waitq);
615
616         RETURN(0);
617 }
618
619 static int osp_precreate_ready_condition(struct osp_device *d)
620 {
621         __u64 next;
622
623         /* ready if got enough precreated objects */
624         /* we need to wait for others (opd_pre_reserved) and our object (+1) */
625         next = d->opd_pre_used_id + d->opd_pre_reserved + 1;
626         if (next <= d->opd_pre_last_created)
627                 return 1;
628
629         /* ready if OST reported no space and no destoys in progress */
630         if (d->opd_syn_changes + d->opd_syn_rpc_in_progress == 0 &&
631             d->opd_pre_status != 0)
632                 return 1;
633
634         return 0;
635 }
636
637 static int osp_precreate_timeout_condition(void *data)
638 {
639         struct osp_device *d = data;
640
641         LCONSOLE_WARN("%s: slow creates, last="LPU64", next="LPU64", "
642                       "reserved="LPU64", syn_changes=%lu, "
643                       "syn_rpc_in_progress=%d, status=%d\n",
644                       d->opd_obd->obd_name, d->opd_pre_last_created,
645                       d->opd_pre_used_id, d->opd_pre_reserved,
646                       d->opd_syn_changes, d->opd_syn_rpc_in_progress,
647                       d->opd_pre_status);
648
649         return 0;
650 }
651
652 /*
653  * called to reserve object in the pool
654  * return codes:
655  *  ENOSPC - no space on corresponded OST
656  *  EAGAIN - precreation is in progress, try later
657  *  EIO    - no access to OST
658  */
659 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d)
660 {
661         struct l_wait_info       lwi;
662         cfs_time_t               expire = cfs_time_shift(obd_timeout);
663         int                      precreated, rc;
664         int                      count = 0;
665
666         ENTRY;
667
668         LASSERT(d->opd_pre_last_created >= d->opd_pre_used_id);
669
670         lwi = LWI_TIMEOUT(cfs_time_seconds(obd_timeout),
671                           osp_precreate_timeout_condition, d);
672
673         /*
674          * wait till:
675          *  - preallocation is done
676          *  - no free space expected soon
677          *  - can't connect to OST for too long (obd_timeout)
678          */
679         while ((rc = d->opd_pre_status) == 0 || rc == -ENOSPC ||
680                 rc == -ENODEV) {
681                 if (unlikely(rc == -ENODEV)) {
682                         if (cfs_time_aftereq(cfs_time_current(), expire))
683                                 break;
684                 }
685
686 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 3, 90, 0)
687                 /*
688                  * to address Andreas's concern on possible busy-loop
689                  * between this thread and osp_precreate_send()
690                  */
691                 if (unlikely(count++ == 1000)) {
692                         osp_precreate_timeout_condition(d);
693                         LBUG();
694                 }
695 #endif
696
697                 /*
698                  * increase number of precreations
699                  */
700                 if (d->opd_pre_grow_count < d->opd_pre_max_grow_count &&
701                     d->opd_pre_grow_slow == 0 &&
702                     (d->opd_pre_last_created - d->opd_pre_used_id <=
703                      d->opd_pre_grow_count / 4 + 1)) {
704                         cfs_spin_lock(&d->opd_pre_lock);
705                         d->opd_pre_grow_slow = 1;
706                         d->opd_pre_grow_count *= 2;
707                         cfs_spin_unlock(&d->opd_pre_lock);
708                 }
709
710                 /*
711                  * we never use the last object in the window
712                  */
713                 cfs_spin_lock(&d->opd_pre_lock);
714                 precreated = d->opd_pre_last_created - d->opd_pre_used_id;
715                 if (precreated > d->opd_pre_reserved) {
716                         d->opd_pre_reserved++;
717                         cfs_spin_unlock(&d->opd_pre_lock);
718                         rc = 0;
719
720                         /* XXX: don't wake up if precreation is in progress */
721                         if (osp_precreate_near_empty_nolock(d))
722                                 cfs_waitq_signal(&d->opd_pre_waitq);
723
724                         break;
725                 }
726                 cfs_spin_unlock(&d->opd_pre_lock);
727
728                 /*
729                  * all precreated objects have been used and no-space
730                  * status leave us no chance to succeed very soon
731                  * but if there is destroy in progress, then we should
732                  * wait till that is done - some space might be released
733                  */
734                 if (unlikely(rc == -ENOSPC)) {
735                         if (d->opd_syn_changes) {
736                                 /* force local commit to release space */
737                                 dt_commit_async(env, d->opd_storage);
738                         }
739                         if (d->opd_syn_rpc_in_progress) {
740                                 /* just wait till destroys are done */
741                                 /* see l_wait_even() few lines below */
742                         }
743                         if (d->opd_syn_changes +
744                             d->opd_syn_rpc_in_progress == 0) {
745                                 /* no hope for free space */
746                                 break;
747                         }
748                 }
749
750                 /* XXX: don't wake up if precreation is in progress */
751                 cfs_waitq_signal(&d->opd_pre_waitq);
752
753                 l_wait_event(d->opd_pre_user_waitq,
754                              osp_precreate_ready_condition(d), &lwi);
755         }
756
757         RETURN(rc);
758 }
759
760 /*
761  * this function relies on reservation made before
762  */
763 __u64 osp_precreate_get_id(struct osp_device *d)
764 {
765         obd_id objid;
766
767         /* grab next id from the pool */
768         cfs_spin_lock(&d->opd_pre_lock);
769         LASSERT(d->opd_pre_used_id < d->opd_pre_last_created);
770         objid = ++d->opd_pre_used_id;
771         d->opd_pre_reserved--;
772         /*
773          * last_used_id must be changed along with getting new id otherwise
774          * we might miscalculate gap causing object loss or leak
775          */
776         osp_update_last_id(d, objid);
777         cfs_spin_unlock(&d->opd_pre_lock);
778
779         /*
780          * probably main thread suspended orphan cleanup till
781          * all reservations are released, see comment in
782          * osp_precreate_thread() just before orphan cleanup
783          */
784         if (unlikely(d->opd_pre_reserved == 0 && d->opd_pre_status))
785                 cfs_waitq_signal(&d->opd_pre_waitq);
786
787         return objid;
788 }
789
790 /*
791  *
792  */
793 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt,
794                         __u64 size)
795 {
796         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
797         struct ptlrpc_request   *req = NULL;
798         struct obd_import       *imp;
799         struct ost_body         *body;
800         struct obdo             *oa = NULL;
801         int                      rc;
802
803         ENTRY;
804
805         imp = d->opd_obd->u.cli.cl_import;
806         LASSERT(imp);
807
808         req = ptlrpc_request_alloc(imp, &RQF_OST_PUNCH);
809         if (req == NULL)
810                 RETURN(-ENOMEM);
811
812         /* XXX: capa support? */
813         /* osc_set_capa_size(req, &RMF_CAPA1, capa); */
814         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_PUNCH);
815         if (rc) {
816                 ptlrpc_request_free(req);
817                 RETURN(rc);
818         }
819
820         /*
821          * XXX: decide how do we do here with resend
822          * if we don't resend, then client may see wrong file size
823          * if we do resend, then MDS thread can get stuck for quite long
824          */
825         req->rq_no_resend = req->rq_no_delay = 1;
826
827         req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
828         ptlrpc_at_set_req_timeout(req);
829
830         OBD_ALLOC_PTR(oa);
831         if (oa == NULL)
832                 GOTO(out, rc = -ENOMEM);
833
834         rc = fid_ostid_pack(lu_object_fid(&dt->do_lu), &oa->o_oi);
835         LASSERT(rc == 0);
836         oa->o_size = size;
837         oa->o_blocks = OBD_OBJECT_EOF;
838         oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
839                       OBD_MD_FLID | OBD_MD_FLGROUP;
840
841         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
842         LASSERT(body);
843         lustre_set_wire_obdo(&body->oa, oa);
844
845         /* XXX: capa support? */
846         /* osc_pack_capa(req, body, capa); */
847
848         ptlrpc_request_set_replen(req);
849
850         rc = ptlrpc_queue_wait(req);
851         if (rc)
852                 CERROR("can't punch object: %d\n", rc);
853 out:
854         ptlrpc_req_finished(req);
855         if (oa)
856                 OBD_FREE_PTR(oa);
857         RETURN(rc);
858 }
859
860 int osp_init_precreate(struct osp_device *d)
861 {
862         struct l_wait_info       lwi = { 0 };
863         int                      rc;
864
865         ENTRY;
866
867         /* initially precreation isn't ready */
868         d->opd_pre_status = -EAGAIN;
869         d->opd_pre_used_id = 0;
870         d->opd_pre_last_created = 0;
871         d->opd_pre_reserved = 0;
872         d->opd_got_disconnected = 1;
873         d->opd_pre_grow_slow = 0;
874         d->opd_pre_grow_count = OST_MIN_PRECREATE;
875         d->opd_pre_min_grow_count = OST_MIN_PRECREATE;
876         d->opd_pre_max_grow_count = OST_MAX_PRECREATE;
877
878         cfs_spin_lock_init(&d->opd_pre_lock);
879         cfs_waitq_init(&d->opd_pre_waitq);
880         cfs_waitq_init(&d->opd_pre_user_waitq);
881         cfs_waitq_init(&d->opd_pre_thread.t_ctl_waitq);
882
883         /*
884          * Initialize statfs-related things
885          */
886         d->opd_statfs_maxage = 5; /* default update interval */
887         d->opd_statfs_fresh_till = cfs_time_shift(-1000);
888         CDEBUG(D_OTHER, "current %llu, fresh till %llu\n",
889                (unsigned long long)cfs_time_current(),
890                (unsigned long long)d->opd_statfs_fresh_till);
891         cfs_timer_init(&d->opd_statfs_timer, osp_statfs_timer_cb, d);
892
893         /*
894          * start thread handling precreation and statfs updates
895          */
896         rc = cfs_create_thread(osp_precreate_thread, d, 0);
897         if (rc < 0) {
898                 CERROR("can't start precreate thread %d\n", rc);
899                 RETURN(rc);
900         }
901
902         l_wait_event(d->opd_pre_thread.t_ctl_waitq,
903                      osp_precreate_running(d) || osp_precreate_stopped(d),
904                      &lwi);
905
906         RETURN(0);
907 }
908
909 void osp_precreate_fini(struct osp_device *d)
910 {
911         struct ptlrpc_thread *thread = &d->opd_pre_thread;
912
913         ENTRY;
914
915         cfs_timer_disarm(&d->opd_statfs_timer);
916
917         thread->t_flags = SVC_STOPPING;
918         cfs_waitq_signal(&d->opd_pre_waitq);
919
920         cfs_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
921
922         EXIT;
923 }
924