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