4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osp/osp_sync.c
38 * Lustre OST Proxy Device
40 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.pershin@intel.com>
42 * Author: Di Wang <di.wang@intel.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include "osp_internal.h"
50 * there are two specific states to take care about:
52 * = import is disconnected =
54 * = import is inactive =
55 * in this case osp_declare_object_create() returns an error
62 static inline int osp_statfs_need_update(struct osp_device *d)
64 return !cfs_time_before(cfs_time_current(),
65 d->opd_statfs_fresh_till);
69 * OSP tries to maintain pool of available objects so that calls to create
70 * objects don't block most of time
72 * each time OSP gets connected to OST, we should start from precreation cleanup
74 static inline bool osp_precreate_running(struct osp_device *d)
76 return !!(d->opd_pre_thread.t_flags & SVC_RUNNING);
79 static inline bool osp_precreate_stopped(struct osp_device *d)
81 return !!(d->opd_pre_thread.t_flags & SVC_STOPPED);
84 static void osp_statfs_timer_cb(unsigned long _d)
86 struct osp_device *d = (struct osp_device *) _d;
89 if (d->opd_pre != NULL && osp_precreate_running(d))
90 wake_up(&d->opd_pre_waitq);
93 static int osp_statfs_interpret(const struct lu_env *env,
94 struct ptlrpc_request *req,
95 union ptlrpc_async_args *aa, int rc)
97 struct obd_import *imp = req->rq_import;
98 struct obd_statfs *msfs;
103 aa = ptlrpc_req_async_args(req);
104 d = aa->pointer_arg[0];
110 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
112 GOTO(out, rc = -EPROTO);
114 d->opd_statfs = *msfs;
116 osp_pre_update_status(d, rc);
118 /* schedule next update */
119 d->opd_statfs_fresh_till = cfs_time_shift(d->opd_statfs_maxage);
120 cfs_timer_arm(&d->opd_statfs_timer, d->opd_statfs_fresh_till);
121 d->opd_statfs_update_in_progress = 0;
123 CDEBUG(D_CACHE, "updated statfs %p\n", d);
127 /* couldn't update statfs, try again as soon as possible */
128 if (d->opd_pre != NULL && osp_precreate_running(d))
129 wake_up(&d->opd_pre_waitq);
131 if (req->rq_import_generation == imp->imp_generation)
132 CDEBUG(D_CACHE, "%s: couldn't update statfs: rc = %d\n",
133 d->opd_obd->obd_name, rc);
137 static int osp_statfs_update(struct osp_device *d)
139 struct ptlrpc_request *req;
140 struct obd_import *imp;
141 union ptlrpc_async_args *aa;
146 CDEBUG(D_CACHE, "going to update statfs\n");
148 imp = d->opd_obd->u.cli.cl_import;
151 req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
155 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
157 ptlrpc_request_free(req);
160 ptlrpc_request_set_replen(req);
161 req->rq_request_portal = OST_CREATE_PORTAL;
162 ptlrpc_at_set_req_timeout(req);
164 req->rq_interpret_reply = (ptlrpc_interpterer_t)osp_statfs_interpret;
165 aa = ptlrpc_req_async_args(req);
166 aa->pointer_arg[0] = d;
169 * no updates till reply
171 cfs_timer_disarm(&d->opd_statfs_timer);
172 d->opd_statfs_fresh_till = cfs_time_shift(obd_timeout * 1000);
173 d->opd_statfs_update_in_progress = 1;
175 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
181 * XXX: there might be a case where removed object(s) do not add free
182 * space (empty object). if the number of such deletions is high, then
183 * we can start to update statfs too often - a rpc storm
184 * TODO: some throttling is needed
186 void osp_statfs_need_now(struct osp_device *d)
188 if (!d->opd_statfs_update_in_progress) {
190 * if current status is -ENOSPC (lack of free space on OST)
191 * then we should poll OST immediately once object destroy
194 d->opd_statfs_fresh_till = cfs_time_shift(-1);
195 cfs_timer_disarm(&d->opd_statfs_timer);
196 wake_up(&d->opd_pre_waitq);
200 static inline int osp_objs_precreated(const struct lu_env *env,
201 struct osp_device *osp)
203 return osp_fid_diff(&osp->opd_pre_last_created_fid,
204 &osp->opd_pre_used_fid);
207 static inline int osp_precreate_near_empty_nolock(const struct lu_env *env,
208 struct osp_device *d)
210 int window = osp_objs_precreated(env, d);
212 /* don't consider new precreation till OST is healty and
214 return ((window - d->opd_pre_reserved < d->opd_pre_grow_count / 2) &&
215 (d->opd_pre_status == 0));
218 static inline int osp_precreate_near_empty(const struct lu_env *env,
219 struct osp_device *d)
223 /* XXX: do we really need locking here? */
224 spin_lock(&d->opd_pre_lock);
225 rc = osp_precreate_near_empty_nolock(env, d);
226 spin_unlock(&d->opd_pre_lock);
230 static inline int osp_create_end_seq(const struct lu_env *env,
231 struct osp_device *osp)
233 struct lu_fid *fid = &osp->opd_pre_used_fid;
236 spin_lock(&osp->opd_pre_lock);
237 rc = osp_fid_end_seq(env, fid);
238 spin_unlock(&osp->opd_pre_lock);
243 * Write fid into last_oid/last_seq file.
245 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
246 struct lu_fid *fid, int sync)
248 struct osp_thread_info *oti = osp_env_info(env);
249 struct lu_buf *lb_oid = &oti->osi_lb;
250 struct lu_buf *lb_oseq = &oti->osi_lb2;
257 /* Note: through f_oid is only 32bits, it will also write
258 * 64 bits for oid to keep compatiblity with the previous
260 lb_oid->lb_buf = &fid->f_oid;
261 lb_oid->lb_len = sizeof(obd_id);
262 oid_off = sizeof(obd_id) * osp->opd_index;
264 lb_oseq->lb_buf = &fid->f_seq;
265 lb_oseq->lb_len = sizeof(obd_id);
266 oseq_off = sizeof(obd_id) * osp->opd_index;
268 th = dt_trans_create(env, osp->opd_storage);
273 rc = dt_declare_record_write(env, osp->opd_last_used_oid_file,
274 lb_oid, oid_off, th);
278 rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
279 lb_oseq, oseq_off, th);
283 rc = dt_trans_start_local(env, osp->opd_storage, th);
287 rc = dt_record_write(env, osp->opd_last_used_oid_file, lb_oid,
290 CERROR("%s: can not write to last seq file: rc = %d\n",
291 osp->opd_obd->obd_name, rc);
294 rc = dt_record_write(env, osp->opd_last_used_seq_file, lb_oseq,
297 CERROR("%s: can not write to last seq file: rc = %d\n",
298 osp->opd_obd->obd_name, rc);
302 dt_trans_stop(env, osp->opd_storage, th);
306 int osp_precreate_rollover_new_seq(struct lu_env *env, struct osp_device *osp)
308 struct lu_fid *fid = &osp_env_info(env)->osi_fid;
309 struct lu_fid *last_fid = &osp->opd_last_used_fid;
313 rc = seq_client_get_seq(env, osp->opd_obd->u.cli.cl_seq, &fid->f_seq);
315 CERROR("%s: alloc fid error: rc = %d\n",
316 osp->opd_obd->obd_name, rc);
322 LASSERTF(fid_seq(fid) != fid_seq(last_fid),
323 "fid "DFID", last_fid "DFID"\n", PFID(fid),
326 rc = osp_write_last_oid_seq_files(env, osp, fid, 1);
328 CERROR("%s: Can not update oid/seq file: rc = %d\n",
329 osp->opd_obd->obd_name, rc);
333 LCONSOLE_INFO("%s: update sequence from "LPX64" to "LPX64"\n",
334 osp->opd_obd->obd_name, fid_seq(last_fid),
336 /* Update last_xxx to the new seq */
337 spin_lock(&osp->opd_pre_lock);
338 osp->opd_last_used_fid = *fid;
339 osp->opd_gap_start_fid = *fid;
340 osp->opd_pre_used_fid = *fid;
341 osp->opd_pre_last_created_fid = *fid;
342 spin_unlock(&osp->opd_pre_lock);
348 * alloc fids for precreation.
349 * rc = 0 Success, @grow is the count of real allocation.
350 * rc = 1 Current seq is used up.
351 * rc < 0 Other error.
353 static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
354 struct lu_fid *fid, int *grow)
356 struct osp_thread_info *osi = osp_env_info(env);
360 if (fid_is_idif(fid)) {
361 struct lu_fid *last_fid;
362 struct ost_id *oi = &osi->osi_oi;
364 spin_lock(&osp->opd_pre_lock);
365 last_fid = &osp->opd_pre_last_created_fid;
366 fid_to_ostid(last_fid, oi);
367 end = min(ostid_id(oi) + *grow, IDIF_MAX_OID);
368 *grow = end - ostid_id(oi);
369 ostid_set_id(oi, ostid_id(oi) + *grow);
370 spin_unlock(&osp->opd_pre_lock);
375 ostid_to_fid(fid, oi, osp->opd_index);
379 spin_lock(&osp->opd_pre_lock);
380 *fid = osp->opd_pre_last_created_fid;
382 end = min((end + *grow), (__u64)LUSTRE_DATA_SEQ_MAX_WIDTH);
383 *grow = end - fid->f_oid;
384 fid->f_oid += end - fid->f_oid;
385 spin_unlock(&osp->opd_pre_lock);
387 CDEBUG(D_INFO, "Expect %d, actual %d ["DFID" -- "DFID"]\n",
388 *grow, i, PFID(fid), PFID(&osp->opd_pre_last_created_fid));
390 return *grow > 0 ? 0 : 1;
393 static int osp_precreate_send(const struct lu_env *env, struct osp_device *d)
395 struct osp_thread_info *oti = osp_env_info(env);
396 struct ptlrpc_request *req;
397 struct obd_import *imp;
398 struct ost_body *body;
400 struct lu_fid *fid = &oti->osi_fid;
403 /* don't precreate new objects till OST healthy and has free space */
404 if (unlikely(d->opd_pre_status)) {
405 CDEBUG(D_INFO, "%s: don't send new precreate: rc = %d\n",
406 d->opd_obd->obd_name, d->opd_pre_status);
411 * if not connection/initialization is compeleted, ignore
413 imp = d->opd_obd->u.cli.cl_import;
416 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
419 req->rq_request_portal = OST_CREATE_PORTAL;
420 /* we should not resend create request - anyway we will have delorphan
421 * and kill these objects */
422 req->rq_no_delay = req->rq_no_resend = 1;
424 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
426 ptlrpc_request_free(req);
430 spin_lock(&d->opd_pre_lock);
431 if (d->opd_pre_grow_count > d->opd_pre_max_grow_count / 2)
432 d->opd_pre_grow_count = d->opd_pre_max_grow_count / 2;
433 grow = d->opd_pre_grow_count;
434 spin_unlock(&d->opd_pre_lock);
436 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
439 *fid = d->opd_pre_last_created_fid;
440 rc = osp_precreate_fids(env, d, fid, &grow);
442 /* Current seq has been used up*/
443 if (!osp_is_fid_client(d)) {
444 osp_pre_update_status(d, -ENOSPC);
447 wake_up(&d->opd_pre_waitq);
451 if (!osp_is_fid_client(d)) {
452 /* Non-FID client will always send seq 0 because of
454 LASSERTF(fid_is_idif(fid), "Invalid fid "DFID"\n", PFID(fid));
458 fid_to_ostid(fid, &body->oa.o_oi);
459 body->oa.o_valid = OBD_MD_FLGROUP;
461 ptlrpc_request_set_replen(req);
463 rc = ptlrpc_queue_wait(req);
465 CERROR("%s: can't precreate: rc = %d\n", d->opd_obd->obd_name,
469 LASSERT(req->rq_transno == 0);
471 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
473 GOTO(out_req, rc = -EPROTO);
475 ostid_to_fid(fid, &body->oa.o_oi, d->opd_index);
476 if (osp_fid_diff(fid, &d->opd_pre_used_fid) <= 0) {
477 CERROR("%s: precreate fid "DFID" < local used fid "DFID
478 ": rc = %d\n", d->opd_obd->obd_name,
479 PFID(fid), PFID(&d->opd_pre_used_fid), -ESTALE);
480 GOTO(out_req, rc = -ESTALE);
483 diff = osp_fid_diff(fid, &d->opd_pre_last_created_fid);
485 spin_lock(&d->opd_pre_lock);
487 /* the OST has not managed to create all the
488 * objects we asked for */
489 d->opd_pre_grow_count = max(diff, OST_MIN_PRECREATE);
490 d->opd_pre_grow_slow = 1;
492 /* the OST is able to keep up with the work,
493 * we could consider increasing grow_count
494 * next time if needed */
495 d->opd_pre_grow_slow = 0;
498 d->opd_pre_last_created_fid = *fid;
499 spin_unlock(&d->opd_pre_lock);
501 CDEBUG(D_HA, "%s: current precreated pool: "DFID"-"DFID"\n",
502 d->opd_obd->obd_name, PFID(&d->opd_pre_used_fid),
503 PFID(&d->opd_pre_last_created_fid));
505 /* now we can wakeup all users awaiting for objects */
506 osp_pre_update_status(d, rc);
507 wake_up(&d->opd_pre_user_waitq);
509 ptlrpc_req_finished(req);
513 static int osp_get_lastfid_from_ost(const struct lu_env *env,
514 struct osp_device *d)
516 struct ptlrpc_request *req = NULL;
517 struct obd_import *imp;
518 struct lu_fid *last_fid;
523 imp = d->opd_obd->u.cli.cl_import;
526 req = ptlrpc_request_alloc(imp, &RQF_OST_GET_INFO_LAST_FID);
530 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY, RCL_CLIENT,
531 sizeof(KEY_LAST_FID));
533 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
535 ptlrpc_request_free(req);
539 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
540 memcpy(tmp, KEY_LAST_FID, sizeof(KEY_LAST_FID));
542 req->rq_no_delay = req->rq_no_resend = 1;
543 last_fid = req_capsule_client_get(&req->rq_pill, &RMF_FID);
544 fid_cpu_to_le(last_fid, &d->opd_last_used_fid);
546 ptlrpc_request_set_replen(req);
548 rc = ptlrpc_queue_wait(req);
550 /* bad-bad OST.. let sysadm sort this out */
551 if (rc == -ENOTSUPP) {
552 CERROR("%s: server does not support FID: rc = %d\n",
553 d->opd_obd->obd_name, -ENOTSUPP);
555 ptlrpc_set_import_active(imp, 0);
559 last_fid = req_capsule_server_get(&req->rq_pill, &RMF_FID);
560 if (last_fid == NULL) {
561 CERROR("%s: Got last_fid failed.\n", d->opd_obd->obd_name);
562 GOTO(out, rc = -EPROTO);
565 if (!fid_is_sane(last_fid)) {
566 CERROR("%s: Got insane last_fid "DFID"\n",
567 d->opd_obd->obd_name, PFID(last_fid));
568 GOTO(out, rc = -EPROTO);
571 /* Only update the last used fid, if the OST has objects for
572 * this sequence, i.e. fid_oid > 0 */
573 if (fid_oid(last_fid) > 0)
574 d->opd_last_used_fid = *last_fid;
576 CDEBUG(D_HA, "%s: Got last_fid "DFID"\n", d->opd_obd->obd_name,
580 ptlrpc_req_finished(req);
585 * asks OST to clean precreate orphans
586 * and gets next id for new objects
588 static int osp_precreate_cleanup_orphans(struct lu_env *env,
589 struct osp_device *d)
591 struct osp_thread_info *osi = osp_env_info(env);
592 struct lu_fid *last_fid = &osi->osi_fid;
593 struct ptlrpc_request *req = NULL;
594 struct obd_import *imp;
595 struct ost_body *body;
596 struct l_wait_info lwi = { 0 };
597 int update_status = 0;
604 * wait for local recovery to finish, so we can cleanup orphans
605 * orphans are all objects since "last used" (assigned), but
606 * there might be objects reserved and in some cases they won't
607 * be used. we can't cleanup them till we're sure they won't be
608 * used. also can't we allow new reservations because they may
609 * end up getting orphans being cleaned up below. so we block
610 * new reservations and wait till all reserved objects either
613 spin_lock(&d->opd_pre_lock);
614 d->opd_pre_recovering = 1;
615 spin_unlock(&d->opd_pre_lock);
617 * The locking above makes sure the opd_pre_reserved check below will
618 * catch all osp_precreate_reserve() calls who find
619 * "!opd_pre_recovering".
621 l_wait_event(d->opd_pre_waitq,
622 (!d->opd_pre_reserved && d->opd_recovery_completed) ||
623 !osp_precreate_running(d) || d->opd_got_disconnected,
625 if (!osp_precreate_running(d) || d->opd_got_disconnected)
626 GOTO(out, rc = -EAGAIN);
628 CDEBUG(D_HA, "%s: going to cleanup orphans since "DFID"\n",
629 d->opd_obd->obd_name, PFID(&d->opd_last_used_fid));
631 *last_fid = d->opd_last_used_fid;
632 /* The OSP should already get the valid seq now */
633 LASSERT(!fid_is_zero(last_fid));
634 if (fid_oid(&d->opd_last_used_fid) < 2) {
635 /* lastfid looks strange... ask OST */
636 rc = osp_get_lastfid_from_ost(env, d);
641 imp = d->opd_obd->u.cli.cl_import;
644 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
646 GOTO(out, rc = -ENOMEM);
648 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
650 ptlrpc_request_free(req);
655 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
657 GOTO(out, rc = -EPROTO);
659 body->oa.o_flags = OBD_FL_DELORPHAN;
660 body->oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
662 fid_to_ostid(&d->opd_last_used_fid, &body->oa.o_oi);
664 ptlrpc_request_set_replen(req);
666 /* Don't resend the delorphan req */
667 req->rq_no_resend = req->rq_no_delay = 1;
669 rc = ptlrpc_queue_wait(req);
675 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
677 GOTO(out, rc = -EPROTO);
680 * OST provides us with id new pool starts from in body->oa.o_id
682 ostid_to_fid(last_fid, &body->oa.o_oi, d->opd_index);
684 spin_lock(&d->opd_pre_lock);
685 diff = osp_fid_diff(&d->opd_last_used_fid, last_fid);
687 d->opd_pre_grow_count = OST_MIN_PRECREATE + diff;
688 d->opd_pre_last_created_fid = d->opd_last_used_fid;
690 d->opd_pre_grow_count = OST_MIN_PRECREATE;
691 d->opd_pre_last_created_fid = *last_fid;
694 * This empties the pre-creation pool and effectively blocks any new
697 LASSERT(fid_oid(&d->opd_pre_last_created_fid) <=
698 LUSTRE_DATA_SEQ_MAX_WIDTH);
699 d->opd_pre_used_fid = d->opd_pre_last_created_fid;
700 d->opd_pre_grow_slow = 0;
701 spin_unlock(&d->opd_pre_lock);
703 CDEBUG(D_HA, "%s: Got last_id "DFID" from OST, last_created "DFID
704 "last_used is "DFID"\n", d->opd_obd->obd_name, PFID(last_fid),
705 PFID(&d->opd_pre_last_created_fid), PFID(&d->opd_last_used_fid));
708 ptlrpc_req_finished(req);
710 spin_lock(&d->opd_pre_lock);
711 d->opd_pre_recovering = 0;
712 spin_unlock(&d->opd_pre_lock);
715 * If rc is zero, the pre-creation window should have been emptied.
716 * Since waking up the herd would be useless without pre-created
717 * objects, we defer the signal to osp_precreate_send() in that case.
721 CERROR("%s: cannot cleanup orphans: rc = %d\n",
722 d->opd_obd->obd_name, rc);
723 /* we can't proceed from here, OST seem to
724 * be in a bad shape, better to wait for
725 * a new instance of the server and repeat
726 * from the beginning. notify possible waiters
727 * this OSP isn't quite functional yet */
728 osp_pre_update_status(d, rc);
730 wake_up(&d->opd_pre_user_waitq);
738 * the function updates current precreation status used: functional or not
740 * rc is a last code from the transport, rc == 0 meaning transport works
741 * well and users of lod can use objects from this OSP
743 * the status depends on current usage of OST
745 void osp_pre_update_status(struct osp_device *d, int rc)
747 struct obd_statfs *msfs = &d->opd_statfs;
748 int old = d->opd_pre_status;
751 d->opd_pre_status = rc;
755 /* Add a bit of hysteresis so this flag isn't continually flapping,
756 * and ensure that new files don't get extremely fragmented due to
757 * only a small amount of available space in the filesystem.
758 * We want to set the NOSPC flag when there is less than ~0.1% free
759 * and clear it when there is at least ~0.2% free space, so:
760 * avail < ~0.1% max max = avail + used
761 * 1025 * avail < avail + used used = blocks - free
762 * 1024 * avail < used
763 * 1024 * avail < blocks - free
764 * avail < ((blocks - free) >> 10)
766 * On very large disk, say 16TB 0.1% will be 16 GB. We don't want to
767 * lose that amount of space so in those cases we report no space left
768 * if their is less than 1 GB left. */
769 if (likely(msfs->os_type)) {
770 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
772 if ((msfs->os_ffree < 32) || (msfs->os_bavail < used)) {
773 d->opd_pre_status = -ENOSPC;
775 CDEBUG(D_INFO, "%s: status: "LPU64" blocks, "
776 LPU64" free, "LPU64" used, "LPU64" "
777 "avail -> %d: rc = %d\n",
778 d->opd_obd->obd_name, msfs->os_blocks,
779 msfs->os_bfree, used, msfs->os_bavail,
780 d->opd_pre_status, rc);
782 "non-commited changes: %lu, in progress: %u\n",
783 d->opd_syn_changes, d->opd_syn_rpc_in_progress);
784 } else if (old == -ENOSPC) {
785 d->opd_pre_status = 0;
786 spin_lock(&d->opd_pre_lock);
787 d->opd_pre_grow_slow = 0;
788 d->opd_pre_grow_count = OST_MIN_PRECREATE;
789 spin_unlock(&d->opd_pre_lock);
790 wake_up(&d->opd_pre_waitq);
791 CDEBUG(D_INFO, "%s: no space: "LPU64" blocks, "LPU64
792 " free, "LPU64" used, "LPU64" avail -> %d: "
793 "rc = %d\n", d->opd_obd->obd_name,
794 msfs->os_blocks, msfs->os_bfree, used,
795 msfs->os_bavail, d->opd_pre_status, rc);
800 wake_up(&d->opd_pre_user_waitq);
803 int osp_init_pre_fid(struct osp_device *osp)
806 struct osp_thread_info *osi;
807 struct lu_client_seq *cli_seq;
808 struct lu_fid *last_fid;
812 LASSERT(osp->opd_pre != NULL);
814 /* Return if last_used fid has been initialized */
815 if (!fid_is_zero(&osp->opd_last_used_fid))
818 rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
820 CERROR("%s: init env error: rc = %d\n",
821 osp->opd_obd->obd_name, rc);
825 osi = osp_env_info(&env);
826 last_fid = &osi->osi_fid;
828 /* For a freshed fs, it will allocate a new sequence first */
829 if (osp_is_fid_client(osp) && osp->opd_group != 0) {
830 cli_seq = osp->opd_obd->u.cli.cl_seq;
831 rc = seq_client_get_seq(&env, cli_seq, &last_fid->f_seq);
833 CERROR("%s: alloc fid error: rc = %d\n",
834 osp->opd_obd->obd_name, rc);
838 last_fid->f_seq = fid_idif_seq(0, osp->opd_index);
843 spin_lock(&osp->opd_pre_lock);
844 osp->opd_last_used_fid = *last_fid;
845 osp->opd_pre_used_fid = *last_fid;
846 osp->opd_pre_last_created_fid = *last_fid;
847 spin_unlock(&osp->opd_pre_lock);
848 rc = osp_write_last_oid_seq_files(&env, osp, last_fid, 1);
850 CERROR("%s: write fid error: rc = %d\n",
851 osp->opd_obd->obd_name, rc);
859 static int osp_precreate_thread(void *_arg)
861 struct osp_device *d = _arg;
862 struct ptlrpc_thread *thread = &d->opd_pre_thread;
863 struct l_wait_info lwi = { 0 };
869 rc = lu_env_init(&env, d->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
871 CERROR("%s: init env error: rc = %d\n", d->opd_obd->obd_name,
876 spin_lock(&d->opd_pre_lock);
877 thread->t_flags = SVC_RUNNING;
878 spin_unlock(&d->opd_pre_lock);
879 wake_up(&thread->t_ctl_waitq);
881 while (osp_precreate_running(d)) {
883 * need to be connected to OST
885 while (osp_precreate_running(d)) {
886 l_wait_event(d->opd_pre_waitq,
887 !osp_precreate_running(d) ||
888 d->opd_new_connection,
891 if (!d->opd_new_connection)
894 d->opd_new_connection = 0;
895 d->opd_got_disconnected = 0;
899 if (!osp_precreate_running(d))
902 LASSERT(d->opd_obd->u.cli.cl_seq != NULL);
903 /* Sigh, fid client is not ready yet */
904 if (d->opd_obd->u.cli.cl_seq->lcs_exp == NULL)
907 /* Init fid for osp_precreate if necessary */
908 rc = osp_init_pre_fid(d);
910 class_export_put(d->opd_exp);
911 d->opd_obd->u.cli.cl_seq->lcs_exp = NULL;
912 CERROR("%s: init pre fid error: rc = %d\n",
913 d->opd_obd->obd_name, rc);
917 osp_statfs_update(d);
920 * Clean up orphans or recreate missing objects.
922 rc = osp_precreate_cleanup_orphans(&env, d);
926 * connected, can handle precreates now
928 while (osp_precreate_running(d)) {
929 l_wait_event(d->opd_pre_waitq,
930 !osp_precreate_running(d) ||
931 osp_precreate_near_empty(&env, d) ||
932 osp_statfs_need_update(d) ||
933 d->opd_got_disconnected, &lwi);
935 if (!osp_precreate_running(d))
938 /* something happened to the connection
939 * have to start from the beginning */
940 if (d->opd_got_disconnected)
943 if (osp_statfs_need_update(d))
944 osp_statfs_update(d);
946 /* To avoid handling different seq in precreate/orphan
947 * cleanup, it will hold precreate until current seq is
949 if (unlikely(osp_precreate_end_seq(&env, d) &&
950 !osp_create_end_seq(&env, d)))
953 if (unlikely(osp_precreate_end_seq(&env, d) &&
954 osp_create_end_seq(&env, d))) {
955 LCONSOLE_INFO("%s:"LPX64" is used up."
956 " Update to new seq\n",
957 d->opd_obd->obd_name,
958 fid_seq(&d->opd_pre_last_created_fid));
959 rc = osp_precreate_rollover_new_seq(&env, d);
964 if (osp_precreate_near_empty(&env, d)) {
965 rc = osp_precreate_send(&env, d);
966 /* osp_precreate_send() sets opd_pre_status
967 * in case of error, that prevent the using of
969 if (rc < 0 && rc != -ENOSPC &&
970 rc != -ETIMEDOUT && rc != -ENOTCONN)
971 CERROR("%s: cannot precreate objects:"
973 d->opd_obd->obd_name, rc);
978 thread->t_flags = SVC_STOPPED;
980 wake_up(&thread->t_ctl_waitq);
985 static int osp_precreate_ready_condition(const struct lu_env *env,
986 struct osp_device *d)
988 if (d->opd_pre_recovering)
991 /* ready if got enough precreated objects */
992 /* we need to wait for others (opd_pre_reserved) and our object (+1) */
993 if (d->opd_pre_reserved + 1 < osp_objs_precreated(env, d))
996 /* ready if OST reported no space and no destroys in progress */
997 if (d->opd_syn_changes + d->opd_syn_rpc_in_progress == 0 &&
998 d->opd_pre_status == -ENOSPC)
1001 /* Bail out I/O fails to OST */
1002 if (d->opd_pre_status != 0 &&
1003 d->opd_pre_status != -EAGAIN &&
1004 d->opd_pre_status != -ENODEV &&
1005 d->opd_pre_status != -ENOSPC) {
1007 if (d->opd_pre_status != -EIO)
1008 CERROR("%s: precreate failed opd_pre_status %d\n",
1009 d->opd_obd->obd_name, d->opd_pre_status);
1016 static int osp_precreate_timeout_condition(void *data)
1018 struct osp_device *d = data;
1020 CDEBUG(D_HA, "%s: slow creates, last="DFID", next="DFID", "
1021 "reserved="LPU64", syn_changes=%lu, "
1022 "syn_rpc_in_progress=%d, status=%d\n",
1023 d->opd_obd->obd_name, PFID(&d->opd_pre_last_created_fid),
1024 PFID(&d->opd_pre_used_fid), d->opd_pre_reserved,
1025 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
1032 * called to reserve object in the pool
1034 * ENOSPC - no space on corresponded OST
1035 * EAGAIN - precreation is in progress, try later
1036 * EIO - no access to OST
1038 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d)
1040 struct l_wait_info lwi;
1041 cfs_time_t expire = cfs_time_shift(obd_timeout);
1046 LASSERTF(osp_objs_precreated(env, d) >= 0, "Last created FID "DFID
1047 "Next FID "DFID"\n", PFID(&d->opd_pre_last_created_fid),
1048 PFID(&d->opd_pre_used_fid));
1052 * - preallocation is done
1053 * - no free space expected soon
1054 * - can't connect to OST for too long (obd_timeout)
1055 * - OST can allocate fid sequence.
1057 while ((rc = d->opd_pre_status) == 0 || rc == -ENOSPC ||
1058 rc == -ENODEV || rc == -EAGAIN || rc == -ENOTCONN) {
1061 * increase number of precreations
1063 precreated = osp_objs_precreated(env, d);
1064 if (d->opd_pre_grow_count < d->opd_pre_max_grow_count &&
1065 d->opd_pre_grow_slow == 0 &&
1066 precreated <= (d->opd_pre_grow_count / 4 + 1)) {
1067 spin_lock(&d->opd_pre_lock);
1068 d->opd_pre_grow_slow = 1;
1069 d->opd_pre_grow_count *= 2;
1070 spin_unlock(&d->opd_pre_lock);
1073 spin_lock(&d->opd_pre_lock);
1074 precreated = osp_objs_precreated(env, d);
1075 if (precreated > d->opd_pre_reserved &&
1076 !d->opd_pre_recovering) {
1077 d->opd_pre_reserved++;
1078 spin_unlock(&d->opd_pre_lock);
1081 /* XXX: don't wake up if precreation is in progress */
1082 if (osp_precreate_near_empty_nolock(env, d) &&
1083 !osp_precreate_end_seq_nolock(env, d))
1084 wake_up(&d->opd_pre_waitq);
1088 spin_unlock(&d->opd_pre_lock);
1091 * all precreated objects have been used and no-space
1092 * status leave us no chance to succeed very soon
1093 * but if there is destroy in progress, then we should
1094 * wait till that is done - some space might be released
1096 if (unlikely(rc == -ENOSPC)) {
1097 if (d->opd_syn_changes) {
1098 /* force local commit to release space */
1099 dt_commit_async(env, d->opd_storage);
1101 if (d->opd_syn_rpc_in_progress) {
1102 /* just wait till destroys are done */
1103 /* see l_wait_even() few lines below */
1105 if (d->opd_syn_changes +
1106 d->opd_syn_rpc_in_progress == 0) {
1107 /* no hope for free space */
1112 /* XXX: don't wake up if precreation is in progress */
1113 wake_up(&d->opd_pre_waitq);
1115 lwi = LWI_TIMEOUT(expire - cfs_time_current(),
1116 osp_precreate_timeout_condition, d);
1117 if (cfs_time_aftereq(cfs_time_current(), expire)) {
1122 l_wait_event(d->opd_pre_user_waitq,
1123 osp_precreate_ready_condition(env, d), &lwi);
1130 * this function relies on reservation made before
1132 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
1135 /* grab next id from the pool */
1136 spin_lock(&d->opd_pre_lock);
1138 LASSERTF(osp_fid_diff(&d->opd_pre_used_fid,
1139 &d->opd_pre_last_created_fid) < 0,
1140 "next fid "DFID" last created fid "DFID"\n",
1141 PFID(&d->opd_pre_used_fid),
1142 PFID(&d->opd_pre_last_created_fid));
1144 d->opd_pre_used_fid.f_oid++;
1145 memcpy(fid, &d->opd_pre_used_fid, sizeof(*fid));
1146 d->opd_pre_reserved--;
1148 * last_used_id must be changed along with getting new id otherwise
1149 * we might miscalculate gap causing object loss or leak
1151 osp_update_last_fid(d, fid);
1152 spin_unlock(&d->opd_pre_lock);
1155 * probably main thread suspended orphan cleanup till
1156 * all reservations are released, see comment in
1157 * osp_precreate_thread() just before orphan cleanup
1159 if (unlikely(d->opd_pre_reserved == 0 && d->opd_pre_status))
1160 wake_up(&d->opd_pre_waitq);
1168 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt,
1171 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1172 struct ptlrpc_request *req = NULL;
1173 struct obd_import *imp;
1174 struct ost_body *body;
1175 struct obdo *oa = NULL;
1180 imp = d->opd_obd->u.cli.cl_import;
1183 req = ptlrpc_request_alloc(imp, &RQF_OST_PUNCH);
1187 /* XXX: capa support? */
1188 /* osc_set_capa_size(req, &RMF_CAPA1, capa); */
1189 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_PUNCH);
1191 ptlrpc_request_free(req);
1196 * XXX: decide how do we do here with resend
1197 * if we don't resend, then client may see wrong file size
1198 * if we do resend, then MDS thread can get stuck for quite long
1200 req->rq_no_resend = req->rq_no_delay = 1;
1202 req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
1203 ptlrpc_at_set_req_timeout(req);
1207 GOTO(out, rc = -ENOMEM);
1209 rc = fid_to_ostid(lu_object_fid(&dt->do_lu), &oa->o_oi);
1212 oa->o_blocks = OBD_OBJECT_EOF;
1213 oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1214 OBD_MD_FLID | OBD_MD_FLGROUP;
1216 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1218 lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
1220 /* XXX: capa support? */
1221 /* osc_pack_capa(req, body, capa); */
1223 ptlrpc_request_set_replen(req);
1225 rc = ptlrpc_queue_wait(req);
1227 CERROR("can't punch object: %d\n", rc);
1229 ptlrpc_req_finished(req);
1235 int osp_init_precreate(struct osp_device *d)
1237 struct l_wait_info lwi = { 0 };
1238 struct task_struct *task;
1242 OBD_ALLOC_PTR(d->opd_pre);
1243 if (d->opd_pre == NULL)
1246 /* initially precreation isn't ready */
1247 d->opd_pre_status = -EAGAIN;
1248 fid_zero(&d->opd_pre_used_fid);
1249 d->opd_pre_used_fid.f_oid = 1;
1250 fid_zero(&d->opd_pre_last_created_fid);
1251 d->opd_pre_last_created_fid.f_oid = 1;
1252 d->opd_pre_reserved = 0;
1253 d->opd_got_disconnected = 1;
1254 d->opd_pre_grow_slow = 0;
1255 d->opd_pre_grow_count = OST_MIN_PRECREATE;
1256 d->opd_pre_min_grow_count = OST_MIN_PRECREATE;
1257 d->opd_pre_max_grow_count = OST_MAX_PRECREATE;
1259 spin_lock_init(&d->opd_pre_lock);
1260 init_waitqueue_head(&d->opd_pre_waitq);
1261 init_waitqueue_head(&d->opd_pre_user_waitq);
1262 init_waitqueue_head(&d->opd_pre_thread.t_ctl_waitq);
1265 * Initialize statfs-related things
1267 d->opd_statfs_maxage = 5; /* default update interval */
1268 d->opd_statfs_fresh_till = cfs_time_shift(-1000);
1269 CDEBUG(D_OTHER, "current %llu, fresh till %llu\n",
1270 (unsigned long long)cfs_time_current(),
1271 (unsigned long long)d->opd_statfs_fresh_till);
1272 cfs_timer_init(&d->opd_statfs_timer, osp_statfs_timer_cb, d);
1275 * start thread handling precreation and statfs updates
1277 task = kthread_run(osp_precreate_thread, d,
1278 "osp-pre-%u-%u", d->opd_index, d->opd_group);
1280 CERROR("can't start precreate thread %ld\n", PTR_ERR(task));
1281 RETURN(PTR_ERR(task));
1284 l_wait_event(d->opd_pre_thread.t_ctl_waitq,
1285 osp_precreate_running(d) || osp_precreate_stopped(d),
1291 void osp_precreate_fini(struct osp_device *d)
1293 struct ptlrpc_thread *thread;
1297 cfs_timer_disarm(&d->opd_statfs_timer);
1299 if (d->opd_pre == NULL)
1302 thread = &d->opd_pre_thread;
1304 thread->t_flags = SVC_STOPPING;
1305 wake_up(&d->opd_pre_waitq);
1307 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1309 OBD_FREE_PTR(d->opd_pre);