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