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>
46 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_MDS
50 #include "osp_internal.h"
53 * there are two specific states to take care about:
55 * = import is disconnected =
57 * = import is inactive =
58 * in this case osp_declare_object_create() returns an error
65 static inline int osp_statfs_need_update(struct osp_device *d)
67 return !cfs_time_before(cfs_time_current(),
68 d->opd_statfs_fresh_till);
71 static void osp_statfs_timer_cb(unsigned long _d)
73 struct osp_device *d = (struct osp_device *) _d;
76 cfs_waitq_signal(&d->opd_pre_waitq);
79 static int osp_statfs_interpret(const struct lu_env *env,
80 struct ptlrpc_request *req,
81 union ptlrpc_async_args *aa, int rc)
83 struct obd_import *imp = req->rq_import;
84 struct obd_statfs *msfs;
89 aa = ptlrpc_req_async_args(req);
90 d = aa->pointer_arg[0];
96 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
98 GOTO(out, rc = -EPROTO);
100 d->opd_statfs = *msfs;
102 osp_pre_update_status(d, rc);
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;
109 CDEBUG(D_CACHE, "updated statfs %p\n", d);
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);
121 static int osp_statfs_update(struct osp_device *d)
123 struct ptlrpc_request *req;
124 struct obd_import *imp;
125 union ptlrpc_async_args *aa;
130 CDEBUG(D_CACHE, "going to update statfs\n");
132 imp = d->opd_obd->u.cli.cl_import;
135 req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
139 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
141 ptlrpc_request_free(req);
144 ptlrpc_request_set_replen(req);
145 req->rq_request_portal = OST_CREATE_PORTAL;
146 ptlrpc_at_set_req_timeout(req);
148 req->rq_interpret_reply = (ptlrpc_interpterer_t)osp_statfs_interpret;
149 aa = ptlrpc_req_async_args(req);
150 aa->pointer_arg[0] = d;
153 * no updates till reply
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;
159 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
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
170 void osp_statfs_need_now(struct osp_device *d)
172 if (!d->opd_statfs_update_in_progress) {
174 * if current status is -ENOSPC (lack of free space on OST)
175 * then we should poll OST immediately once object destroy
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);
186 * OSP tries to maintain pool of available objects so that calls to create
187 * objects don't block most of time
189 * each time OSP gets connected to OST, we should start from precreation cleanup
191 static inline int osp_precreate_running(struct osp_device *d)
193 return !!(d->opd_pre_thread.t_flags & SVC_RUNNING);
196 static inline int osp_precreate_stopped(struct osp_device *d)
198 return !!(d->opd_pre_thread.t_flags & SVC_STOPPED);
201 static inline int osp_objs_precreated(const struct lu_env *env,
202 struct osp_device *osp)
204 struct lu_fid *fid1 = &osp->opd_pre_last_created_fid;
205 struct lu_fid *fid2 = &osp->opd_pre_used_fid;
207 LASSERTF(fid_seq(fid1) == fid_seq(fid2),
208 "Created fid"DFID" Next fid "DFID"\n", PFID(fid1), PFID(fid2));
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;
214 LASSERT(fid_is_idif(fid1) && fid_is_idif(fid2));
215 fid_to_ostid(fid1, oi1);
216 fid_to_ostid(fid2, oi2);
217 LASSERT(ostid_id(oi1) >= ostid_id(oi2));
219 return ostid_id(oi1) - ostid_id(oi2);
222 return fid_oid(fid1) - fid_oid(fid2);
225 static inline int osp_precreate_near_empty_nolock(const struct lu_env *env,
226 struct osp_device *d)
228 int window = osp_objs_precreated(env, d);
230 /* don't consider new precreation till OST is healty and
232 return ((window - d->opd_pre_reserved < d->opd_pre_grow_count / 2) &&
233 (d->opd_pre_status == 0));
236 static inline int osp_precreate_near_empty(const struct lu_env *env,
237 struct osp_device *d)
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);
248 static inline int osp_create_end_seq(const struct lu_env *env,
249 struct osp_device *osp)
251 struct lu_fid *fid = &osp->opd_pre_used_fid;
254 spin_lock(&osp->opd_pre_lock);
255 rc = osp_fid_end_seq(env, fid);
256 spin_unlock(&osp->opd_pre_lock);
261 * Write fid into last_oid/last_seq file.
263 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
264 struct lu_fid *fid, int sync)
266 struct osp_thread_info *oti = osp_env_info(env);
267 struct lu_buf *lb_oid = &oti->osi_lb;
268 struct lu_buf *lb_oseq = &oti->osi_lb2;
275 /* Note: through f_oid is only 32bits, it will also write
276 * 64 bits for oid to keep compatiblity with the previous
278 lb_oid->lb_buf = &fid->f_oid;
279 lb_oid->lb_len = sizeof(obd_id);
280 oid_off = sizeof(obd_id) * osp->opd_index;
282 lb_oseq->lb_buf = &fid->f_seq;
283 lb_oseq->lb_len = sizeof(obd_id);
284 oseq_off = sizeof(obd_id) * osp->opd_index;
286 th = dt_trans_create(env, osp->opd_storage);
291 rc = dt_declare_record_write(env, osp->opd_last_used_oid_file,
292 lb_oid->lb_len, oid_off, th);
296 rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
297 lb_oseq->lb_len, oseq_off, th);
301 rc = dt_trans_start_local(env, osp->opd_storage, th);
305 rc = dt_record_write(env, osp->opd_last_used_oid_file, lb_oid,
308 CERROR("%s: can not write to last seq file: rc = %d\n",
309 osp->opd_obd->obd_name, rc);
312 rc = dt_record_write(env, osp->opd_last_used_seq_file, lb_oseq,
315 CERROR("%s: can not write to last seq file: rc = %d\n",
316 osp->opd_obd->obd_name, rc);
320 dt_trans_stop(env, osp->opd_storage, th);
324 int osp_precreate_rollover_new_seq(struct lu_env *env, struct osp_device *osp)
326 struct lu_fid *fid = &osp_env_info(env)->osi_fid;
327 struct lu_fid *last_fid = &osp->opd_last_used_fid;
331 rc = seq_client_get_seq(env, osp->opd_obd->u.cli.cl_seq, &fid->f_seq);
333 CERROR("%s: alloc fid error: rc = %d\n",
334 osp->opd_obd->obd_name, rc);
340 LASSERTF(fid_seq(fid) != fid_seq(last_fid),
341 "fid "DFID", last_fid "DFID"\n", PFID(fid),
344 rc = osp_write_last_oid_seq_files(env, osp, fid, 1);
346 CERROR("%s: Can not update oid/seq file: rc = %d\n",
347 osp->opd_obd->obd_name, rc);
351 LCONSOLE_INFO("%s: update sequence from "LPX64" to "LPX64"\n",
352 osp->opd_obd->obd_name, fid_seq(last_fid),
354 /* Update last_xxx to the new seq */
355 spin_lock(&osp->opd_pre_lock);
356 osp->opd_last_used_fid = *fid;
357 osp->opd_gap_start_fid = *fid;
358 osp->opd_pre_used_fid = *fid;
359 osp->opd_pre_last_created_fid = *fid;
360 spin_unlock(&osp->opd_pre_lock);
366 * alloc fids for precreation.
367 * rc = 0 Success, @grow is the count of real allocation.
368 * rc = 1 Current seq is used up.
369 * rc < 0 Other error.
371 static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
372 struct lu_fid *fid, int *grow)
374 struct osp_thread_info *osi = osp_env_info(env);
378 if (fid_is_idif(fid)) {
379 struct lu_fid *last_fid;
380 struct ost_id *oi = &osi->osi_oi;
382 spin_lock(&osp->opd_pre_lock);
383 last_fid = &osp->opd_pre_last_created_fid;
384 fid_to_ostid(last_fid, oi);
385 end = min(ostid_id(oi) + *grow, IDIF_MAX_OID);
386 *grow = end - ostid_id(oi);
387 ostid_set_id(oi, ostid_id(oi) + *grow);
388 spin_unlock(&osp->opd_pre_lock);
393 ostid_to_fid(fid, oi, osp->opd_index);
397 spin_lock(&osp->opd_pre_lock);
398 *fid = osp->opd_pre_last_created_fid;
400 end = min((end + *grow), (__u64)LUSTRE_DATA_SEQ_MAX_WIDTH);
401 *grow = end - fid->f_oid;
402 fid->f_oid += end - fid->f_oid;
403 spin_unlock(&osp->opd_pre_lock);
405 CDEBUG(D_INFO, "Expect %d, actual %d ["DFID" -- "DFID"]\n",
406 *grow, i, PFID(fid), PFID(&osp->opd_pre_last_created_fid));
408 return *grow > 0 ? 0 : 1;
411 static int osp_precreate_send(const struct lu_env *env, struct osp_device *d)
413 struct osp_thread_info *oti = osp_env_info(env);
414 struct ptlrpc_request *req;
415 struct obd_import *imp;
416 struct ost_body *body;
418 struct lu_fid *fid = &oti->osi_fid;
421 /* don't precreate new objects till OST healthy and has free space */
422 if (unlikely(d->opd_pre_status)) {
423 CDEBUG(D_INFO, "%s: don't send new precreate: rc = %d\n",
424 d->opd_obd->obd_name, d->opd_pre_status);
429 * if not connection/initialization is compeleted, ignore
431 imp = d->opd_obd->u.cli.cl_import;
434 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
437 req->rq_request_portal = OST_CREATE_PORTAL;
438 /* we should not resend create request - anyway we will have delorphan
439 * and kill these objects */
440 req->rq_no_delay = req->rq_no_resend = 1;
442 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
444 ptlrpc_request_free(req);
448 spin_lock(&d->opd_pre_lock);
449 if (d->opd_pre_grow_count > d->opd_pre_max_grow_count / 2)
450 d->opd_pre_grow_count = d->opd_pre_max_grow_count / 2;
451 grow = d->opd_pre_grow_count;
452 spin_unlock(&d->opd_pre_lock);
454 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
457 *fid = d->opd_pre_last_created_fid;
458 rc = osp_precreate_fids(env, d, fid, &grow);
460 /* Current seq has been used up*/
461 if (!osp_is_fid_client(d)) {
462 osp_pre_update_status(d, -ENOSPC);
465 cfs_waitq_signal(&d->opd_pre_waitq);
469 if (!osp_is_fid_client(d)) {
470 /* Non-FID client will always send seq 0 because of
472 LASSERTF(fid_is_idif(fid), "Invalid fid "DFID"\n", PFID(fid));
476 fid_to_ostid(fid, &body->oa.o_oi);
477 body->oa.o_valid = OBD_MD_FLGROUP;
479 ptlrpc_request_set_replen(req);
481 rc = ptlrpc_queue_wait(req);
483 CERROR("%s: can't precreate: rc = %d\n", d->opd_obd->obd_name,
487 LASSERT(req->rq_transno == 0);
489 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
491 GOTO(out_req, rc = -EPROTO);
493 ostid_to_fid(fid, &body->oa.o_oi, d->opd_index);
494 LASSERTF(lu_fid_diff(fid, &d->opd_pre_used_fid) > 0,
495 "reply fid "DFID" pre used fid "DFID"\n", PFID(fid),
496 PFID(&d->opd_pre_used_fid));
498 diff = lu_fid_diff(fid, &d->opd_pre_last_created_fid);
500 spin_lock(&d->opd_pre_lock);
502 /* the OST has not managed to create all the
503 * objects we asked for */
504 d->opd_pre_grow_count = max(diff, OST_MIN_PRECREATE);
505 d->opd_pre_grow_slow = 1;
507 /* the OST is able to keep up with the work,
508 * we could consider increasing grow_count
509 * next time if needed */
510 d->opd_pre_grow_slow = 0;
513 d->opd_pre_last_created_fid = *fid;
514 spin_unlock(&d->opd_pre_lock);
516 CDEBUG(D_HA, "%s: current precreated pool: "DFID"-"DFID"\n",
517 d->opd_obd->obd_name, PFID(&d->opd_pre_used_fid),
518 PFID(&d->opd_pre_last_created_fid));
520 /* now we can wakeup all users awaiting for objects */
521 osp_pre_update_status(d, rc);
522 cfs_waitq_signal(&d->opd_pre_user_waitq);
524 ptlrpc_req_finished(req);
528 static int osp_get_lastfid_from_ost(const struct lu_env *env,
529 struct osp_device *d)
531 struct ptlrpc_request *req = NULL;
532 struct obd_import *imp;
533 struct lu_fid *last_fid;
538 imp = d->opd_obd->u.cli.cl_import;
541 req = ptlrpc_request_alloc(imp, &RQF_OST_GET_INFO_LAST_FID);
545 req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY, RCL_CLIENT,
546 sizeof(KEY_LAST_FID));
548 req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_VAL, RCL_CLIENT,
549 sizeof(struct lu_fid));
551 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
553 ptlrpc_request_free(req);
557 tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
558 memcpy(tmp, KEY_LAST_FID, sizeof(KEY_LAST_FID));
560 req->rq_no_delay = req->rq_no_resend = 1;
561 tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
562 fid_cpu_to_le((struct lu_fid *)tmp, &d->opd_last_used_fid);
563 ptlrpc_request_set_replen(req);
565 rc = ptlrpc_queue_wait(req);
567 /* bad-bad OST.. let sysadm sort this out */
568 if (rc == -ENOTSUPP) {
569 CERROR("%s: server does not support FID: rc = %d\n",
570 d->opd_obd->obd_name, -ENOTSUPP);
572 ptlrpc_set_import_active(imp, 0);
576 last_fid = req_capsule_server_get(&req->rq_pill, &RMF_FID);
577 if (last_fid == NULL) {
578 CERROR("%s: Got last_fid failed.\n", d->opd_obd->obd_name);
579 GOTO(out, rc = -EPROTO);
582 if (!fid_is_sane(last_fid)) {
583 CERROR("%s: Got insane last_fid "DFID"\n",
584 d->opd_obd->obd_name, PFID(last_fid));
585 GOTO(out, rc = -EPROTO);
588 /* Only update the last used fid, if the OST has objects for
589 * this sequence, i.e. fid_oid > 0 */
590 if (fid_oid(last_fid) > 0)
591 d->opd_last_used_fid = *last_fid;
593 CDEBUG(D_HA, "%s: Got last_fid "DFID"\n", d->opd_obd->obd_name,
597 ptlrpc_req_finished(req);
602 * asks OST to clean precreate orphans
603 * and gets next id for new objects
605 static int osp_precreate_cleanup_orphans(struct lu_env *env,
606 struct osp_device *d)
608 struct osp_thread_info *osi = osp_env_info(env);
609 struct lu_fid *last_fid = &osi->osi_fid;
610 struct ptlrpc_request *req = NULL;
611 struct obd_import *imp;
612 struct ost_body *body;
613 struct l_wait_info lwi = { 0 };
614 int update_status = 0;
621 * wait for local recovery to finish, so we can cleanup orphans
622 * orphans are all objects since "last used" (assigned), but
623 * there might be objects reserved and in some cases they won't
624 * be used. we can't cleanup them till we're sure they won't be
625 * used. also can't we allow new reservations because they may
626 * end up getting orphans being cleaned up below. so we block
627 * new reservations and wait till all reserved objects either
630 spin_lock(&d->opd_pre_lock);
631 d->opd_pre_recovering = 1;
632 spin_unlock(&d->opd_pre_lock);
634 * The locking above makes sure the opd_pre_reserved check below will
635 * catch all osp_precreate_reserve() calls who find
636 * "!opd_pre_recovering".
638 l_wait_event(d->opd_pre_waitq,
639 (!d->opd_pre_reserved && d->opd_recovery_completed) ||
640 !osp_precreate_running(d) || d->opd_got_disconnected,
642 if (!osp_precreate_running(d) || d->opd_got_disconnected)
643 GOTO(out, rc = -EAGAIN);
645 CDEBUG(D_HA, "%s: going to cleanup orphans since "DFID"\n",
646 d->opd_obd->obd_name, PFID(&d->opd_last_used_fid));
648 *last_fid = d->opd_last_used_fid;
649 /* The OSP should already get the valid seq now */
650 LASSERT(!fid_is_zero(last_fid));
651 if (fid_oid(&d->opd_last_used_fid) < 2) {
652 /* lastfid looks strange... ask OST */
653 rc = osp_get_lastfid_from_ost(env, d);
658 imp = d->opd_obd->u.cli.cl_import;
661 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
663 GOTO(out, rc = -ENOMEM);
665 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
667 ptlrpc_request_free(req);
672 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
674 GOTO(out, rc = -EPROTO);
676 body->oa.o_flags = OBD_FL_DELORPHAN;
677 body->oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
679 fid_to_ostid(&d->opd_last_used_fid, &body->oa.o_oi);
681 ptlrpc_request_set_replen(req);
683 /* Don't resend the delorphan req */
684 req->rq_no_resend = req->rq_no_delay = 1;
686 rc = ptlrpc_queue_wait(req);
692 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
694 GOTO(out, rc = -EPROTO);
697 * OST provides us with id new pool starts from in body->oa.o_id
699 ostid_to_fid(last_fid, &body->oa.o_oi, d->opd_index);
701 spin_lock(&d->opd_pre_lock);
702 diff = lu_fid_diff(&d->opd_last_used_fid, last_fid);
704 d->opd_pre_grow_count = OST_MIN_PRECREATE + diff;
705 d->opd_pre_last_created_fid = d->opd_last_used_fid;
707 d->opd_pre_grow_count = OST_MIN_PRECREATE;
708 d->opd_pre_last_created_fid = *last_fid;
711 * This empties the pre-creation pool and effectively blocks any new
714 LASSERT(fid_oid(&d->opd_pre_last_created_fid) <=
715 LUSTRE_DATA_SEQ_MAX_WIDTH);
716 d->opd_pre_used_fid = d->opd_pre_last_created_fid;
717 d->opd_pre_grow_slow = 0;
718 spin_unlock(&d->opd_pre_lock);
720 CDEBUG(D_HA, "%s: Got last_id "DFID" from OST, last_created "DFID
721 "last_used is "DFID"\n", d->opd_obd->obd_name, PFID(last_fid),
722 PFID(&d->opd_pre_last_created_fid), PFID(&d->opd_last_used_fid));
725 ptlrpc_req_finished(req);
727 d->opd_pre_recovering = 0;
730 * If rc is zero, the pre-creation window should have been emptied.
731 * Since waking up the herd would be useless without pre-created
732 * objects, we defer the signal to osp_precreate_send() in that case.
736 CERROR("%s: cannot cleanup orphans: rc = %d\n",
737 d->opd_obd->obd_name, rc);
738 /* we can't proceed from here, OST seem to
739 * be in a bad shape, better to wait for
740 * a new instance of the server and repeat
741 * from the beginning. notify possible waiters
742 * this OSP isn't quite functional yet */
743 osp_pre_update_status(d, rc);
745 cfs_waitq_signal(&d->opd_pre_user_waitq);
753 * the function updates current precreation status used: functional or not
755 * rc is a last code from the transport, rc == 0 meaning transport works
756 * well and users of lod can use objects from this OSP
758 * the status depends on current usage of OST
760 void osp_pre_update_status(struct osp_device *d, int rc)
762 struct obd_statfs *msfs = &d->opd_statfs;
763 int old = d->opd_pre_status;
766 d->opd_pre_status = rc;
770 /* Add a bit of hysteresis so this flag isn't continually flapping,
771 * and ensure that new files don't get extremely fragmented due to
772 * only a small amount of available space in the filesystem.
773 * We want to set the NOSPC flag when there is less than ~0.1% free
774 * and clear it when there is at least ~0.2% free space, so:
775 * avail < ~0.1% max max = avail + used
776 * 1025 * avail < avail + used used = blocks - free
777 * 1024 * avail < used
778 * 1024 * avail < blocks - free
779 * avail < ((blocks - free) >> 10)
781 * On very large disk, say 16TB 0.1% will be 16 GB. We don't want to
782 * lose that amount of space so in those cases we report no space left
783 * if their is less than 1 GB left. */
784 if (likely(msfs->os_type)) {
785 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
787 if ((msfs->os_ffree < 32) || (msfs->os_bavail < used)) {
788 d->opd_pre_status = -ENOSPC;
790 CDEBUG(D_INFO, "%s: status: "LPU64" blocks, "
791 LPU64" free, "LPU64" used, "LPU64" "
792 "avail -> %d: rc = %d\n",
793 d->opd_obd->obd_name, msfs->os_blocks,
794 msfs->os_bfree, used, msfs->os_bavail,
795 d->opd_pre_status, rc);
797 "non-commited changes: %lu, in progress: %u\n",
798 d->opd_syn_changes, d->opd_syn_rpc_in_progress);
799 } else if (old == -ENOSPC) {
800 d->opd_pre_status = 0;
801 d->opd_pre_grow_slow = 0;
802 d->opd_pre_grow_count = OST_MIN_PRECREATE;
803 cfs_waitq_signal(&d->opd_pre_waitq);
804 CDEBUG(D_INFO, "%s: no space: "LPU64" blocks, "LPU64
805 " free, "LPU64" used, "LPU64" avail -> %d: "
806 "rc = %d\n", d->opd_obd->obd_name,
807 msfs->os_blocks, msfs->os_bfree, used,
808 msfs->os_bavail, d->opd_pre_status, rc);
813 cfs_waitq_signal(&d->opd_pre_user_waitq);
816 static int osp_init_pre_fid(struct osp_device *osp)
819 struct osp_thread_info *osi;
820 struct lu_client_seq *cli_seq;
821 struct lu_fid *last_fid;
825 /* Return if last_used fid has been initialized */
826 if (!fid_is_zero(&osp->opd_last_used_fid))
829 rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
831 CERROR("%s: init env error: rc = %d\n",
832 osp->opd_obd->obd_name, rc);
836 osi = osp_env_info(&env);
837 last_fid = &osi->osi_fid;
839 /* For a freshed fs, it will allocate a new sequence first */
840 if (osp_is_fid_client(osp) && osp->opd_group != 0) {
841 cli_seq = osp->opd_obd->u.cli.cl_seq;
842 rc = seq_client_get_seq(&env, cli_seq, &last_fid->f_seq);
844 CERROR("%s: alloc fid error: rc = %d\n",
845 osp->opd_obd->obd_name, rc);
849 last_fid->f_seq = fid_idif_seq(0, osp->opd_index);
854 spin_lock(&osp->opd_pre_lock);
855 osp->opd_last_used_fid = *last_fid;
856 osp->opd_pre_used_fid = *last_fid;
857 osp->opd_pre_last_created_fid = *last_fid;
858 spin_unlock(&osp->opd_pre_lock);
859 rc = osp_write_last_oid_seq_files(&env, osp, last_fid, 1);
861 CERROR("%s: write fid error: rc = %d\n",
862 osp->opd_obd->obd_name, rc);
870 static int osp_precreate_thread(void *_arg)
872 struct osp_device *d = _arg;
873 struct ptlrpc_thread *thread = &d->opd_pre_thread;
874 struct l_wait_info lwi = { 0 };
881 sprintf(pname, "osp-pre-%u", d->opd_index);
882 cfs_daemonize(pname);
884 rc = lu_env_init(&env, d->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
886 CERROR("%s: init env error: rc = %d\n", d->opd_obd->obd_name,
891 spin_lock(&d->opd_pre_lock);
892 thread->t_flags = SVC_RUNNING;
893 spin_unlock(&d->opd_pre_lock);
894 cfs_waitq_signal(&thread->t_ctl_waitq);
896 while (osp_precreate_running(d)) {
898 * need to be connected to OST
900 while (osp_precreate_running(d)) {
901 l_wait_event(d->opd_pre_waitq,
902 !osp_precreate_running(d) ||
903 d->opd_new_connection,
906 if (!d->opd_new_connection)
909 d->opd_new_connection = 0;
910 d->opd_got_disconnected = 0;
914 if (!osp_precreate_running(d))
917 LASSERT(d->opd_obd->u.cli.cl_seq != NULL);
918 if (d->opd_obd->u.cli.cl_seq->lcs_exp == NULL) {
919 /* Get new sequence for client first */
920 LASSERT(d->opd_exp != NULL);
921 d->opd_obd->u.cli.cl_seq->lcs_exp =
922 class_export_get(d->opd_exp);
923 rc = osp_init_pre_fid(d);
925 class_export_put(d->opd_exp);
926 d->opd_obd->u.cli.cl_seq->lcs_exp = NULL;
927 CERROR("%s: init pre fid error: rc = %d\n",
928 d->opd_obd->obd_name, rc);
933 osp_statfs_update(d);
936 * Clean up orphans or recreate missing objects.
938 rc = osp_precreate_cleanup_orphans(&env, d);
942 * connected, can handle precreates now
944 while (osp_precreate_running(d)) {
945 l_wait_event(d->opd_pre_waitq,
946 !osp_precreate_running(d) ||
947 osp_precreate_near_empty(&env, d) ||
948 osp_statfs_need_update(d) ||
949 d->opd_got_disconnected, &lwi);
951 if (!osp_precreate_running(d))
954 /* something happened to the connection
955 * have to start from the beginning */
956 if (d->opd_got_disconnected)
959 if (osp_statfs_need_update(d))
960 osp_statfs_update(d);
962 /* To avoid handling different seq in precreate/orphan
963 * cleanup, it will hold precreate until current seq is
965 if (unlikely(osp_precreate_end_seq(&env, d) &&
966 !osp_create_end_seq(&env, d)))
969 if (unlikely(osp_precreate_end_seq(&env, d) &&
970 osp_create_end_seq(&env, d))) {
971 LCONSOLE_INFO("%s:"LPX64" is used up."
972 " Update to new seq\n",
973 d->opd_obd->obd_name,
974 fid_seq(&d->opd_pre_last_created_fid));
975 rc = osp_precreate_rollover_new_seq(&env, d);
980 if (osp_precreate_near_empty(&env, d)) {
981 rc = osp_precreate_send(&env, d);
982 /* osp_precreate_send() sets opd_pre_status
983 * in case of error, that prevent the using of
985 if (rc < 0 && rc != -ENOSPC &&
986 rc != -ETIMEDOUT && rc != -ENOTCONN)
987 CERROR("%s: cannot precreate objects:"
989 d->opd_obd->obd_name, rc);
994 thread->t_flags = SVC_STOPPED;
996 cfs_waitq_signal(&thread->t_ctl_waitq);
1001 static int osp_precreate_ready_condition(const struct lu_env *env,
1002 struct osp_device *d)
1004 if (d->opd_pre_recovering)
1007 /* ready if got enough precreated objects */
1008 /* we need to wait for others (opd_pre_reserved) and our object (+1) */
1009 if (d->opd_pre_reserved + 1 < osp_objs_precreated(env, d))
1012 /* ready if OST reported no space and no destoys in progress */
1013 if (d->opd_syn_changes + d->opd_syn_rpc_in_progress == 0 &&
1014 d->opd_pre_status == -ENOSPC)
1020 static int osp_precreate_timeout_condition(void *data)
1022 struct osp_device *d = data;
1024 LCONSOLE_WARN("%s: slow creates, last="DFID", next="DFID", "
1025 "reserved="LPU64", syn_changes=%lu, "
1026 "syn_rpc_in_progress=%d, status=%d\n",
1027 d->opd_obd->obd_name, PFID(&d->opd_pre_last_created_fid),
1028 PFID(&d->opd_pre_used_fid), d->opd_pre_reserved,
1029 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
1036 * called to reserve object in the pool
1038 * ENOSPC - no space on corresponded OST
1039 * EAGAIN - precreation is in progress, try later
1040 * EIO - no access to OST
1042 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d)
1044 struct l_wait_info lwi;
1045 cfs_time_t expire = cfs_time_shift(obd_timeout);
1050 LASSERTF(osp_objs_precreated(env, d) >= 0, "Last created FID "DFID
1051 "Next FID "DFID"\n", PFID(&d->opd_pre_last_created_fid),
1052 PFID(&d->opd_pre_used_fid));
1056 * - preallocation is done
1057 * - no free space expected soon
1058 * - can't connect to OST for too long (obd_timeout)
1059 * - OST can allocate fid sequence.
1061 while ((rc = d->opd_pre_status) == 0 || rc == -ENOSPC ||
1062 rc == -ENODEV || rc == -EAGAIN) {
1065 * increase number of precreations
1067 precreated = osp_objs_precreated(env, d);
1068 if (d->opd_pre_grow_count < d->opd_pre_max_grow_count &&
1069 d->opd_pre_grow_slow == 0 &&
1070 precreated <= (d->opd_pre_grow_count / 4 + 1)) {
1071 spin_lock(&d->opd_pre_lock);
1072 d->opd_pre_grow_slow = 1;
1073 d->opd_pre_grow_count *= 2;
1074 spin_unlock(&d->opd_pre_lock);
1077 spin_lock(&d->opd_pre_lock);
1078 precreated = osp_objs_precreated(env, d);
1079 if (precreated > d->opd_pre_reserved &&
1080 !d->opd_pre_recovering) {
1081 d->opd_pre_reserved++;
1082 spin_unlock(&d->opd_pre_lock);
1085 /* XXX: don't wake up if precreation is in progress */
1086 if (osp_precreate_near_empty_nolock(env, d) &&
1087 !osp_precreate_end_seq_nolock(env, d))
1088 cfs_waitq_signal(&d->opd_pre_waitq);
1092 spin_unlock(&d->opd_pre_lock);
1095 * all precreated objects have been used and no-space
1096 * status leave us no chance to succeed very soon
1097 * but if there is destroy in progress, then we should
1098 * wait till that is done - some space might be released
1100 if (unlikely(rc == -ENOSPC)) {
1101 if (d->opd_syn_changes) {
1102 /* force local commit to release space */
1103 dt_commit_async(env, d->opd_storage);
1105 if (d->opd_syn_rpc_in_progress) {
1106 /* just wait till destroys are done */
1107 /* see l_wait_even() few lines below */
1109 if (d->opd_syn_changes +
1110 d->opd_syn_rpc_in_progress == 0) {
1111 /* no hope for free space */
1116 /* XXX: don't wake up if precreation is in progress */
1117 cfs_waitq_signal(&d->opd_pre_waitq);
1119 lwi = LWI_TIMEOUT(expire - cfs_time_current(),
1120 osp_precreate_timeout_condition, d);
1121 if (cfs_time_aftereq(cfs_time_current(), expire)) {
1126 l_wait_event(d->opd_pre_user_waitq,
1127 osp_precreate_ready_condition(env, d), &lwi);
1134 * this function relies on reservation made before
1136 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
1139 /* grab next id from the pool */
1140 spin_lock(&d->opd_pre_lock);
1142 LASSERTF(lu_fid_diff(&d->opd_pre_used_fid,
1143 &d->opd_pre_last_created_fid) < 0,
1144 "next fid "DFID" last created fid "DFID"\n",
1145 PFID(&d->opd_pre_used_fid),
1146 PFID(&d->opd_pre_last_created_fid));
1148 d->opd_pre_used_fid.f_oid++;
1149 memcpy(fid, &d->opd_pre_used_fid, sizeof(*fid));
1150 d->opd_pre_reserved--;
1152 * last_used_id must be changed along with getting new id otherwise
1153 * we might miscalculate gap causing object loss or leak
1155 osp_update_last_fid(d, fid);
1156 spin_unlock(&d->opd_pre_lock);
1159 * probably main thread suspended orphan cleanup till
1160 * all reservations are released, see comment in
1161 * osp_precreate_thread() just before orphan cleanup
1163 if (unlikely(d->opd_pre_reserved == 0 && d->opd_pre_status))
1164 cfs_waitq_signal(&d->opd_pre_waitq);
1172 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt,
1175 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1176 struct ptlrpc_request *req = NULL;
1177 struct obd_import *imp;
1178 struct ost_body *body;
1179 struct obdo *oa = NULL;
1184 imp = d->opd_obd->u.cli.cl_import;
1187 req = ptlrpc_request_alloc(imp, &RQF_OST_PUNCH);
1191 /* XXX: capa support? */
1192 /* osc_set_capa_size(req, &RMF_CAPA1, capa); */
1193 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_PUNCH);
1195 ptlrpc_request_free(req);
1200 * XXX: decide how do we do here with resend
1201 * if we don't resend, then client may see wrong file size
1202 * if we do resend, then MDS thread can get stuck for quite long
1204 req->rq_no_resend = req->rq_no_delay = 1;
1206 req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
1207 ptlrpc_at_set_req_timeout(req);
1211 GOTO(out, rc = -ENOMEM);
1213 rc = fid_to_ostid(lu_object_fid(&dt->do_lu), &oa->o_oi);
1216 oa->o_blocks = OBD_OBJECT_EOF;
1217 oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1218 OBD_MD_FLID | OBD_MD_FLGROUP;
1220 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1222 lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
1224 /* XXX: capa support? */
1225 /* osc_pack_capa(req, body, capa); */
1227 ptlrpc_request_set_replen(req);
1229 rc = ptlrpc_queue_wait(req);
1231 CERROR("can't punch object: %d\n", rc);
1233 ptlrpc_req_finished(req);
1239 int osp_init_precreate(struct osp_device *d)
1241 struct l_wait_info lwi = { 0 };
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 cfs_waitq_init(&d->opd_pre_waitq);
1261 cfs_waitq_init(&d->opd_pre_user_waitq);
1262 cfs_waitq_init(&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 rc = cfs_create_thread(osp_precreate_thread, d, 0);
1279 CERROR("can't start precreate thread %d\n", rc);
1283 l_wait_event(d->opd_pre_thread.t_ctl_waitq,
1284 osp_precreate_running(d) || osp_precreate_stopped(d),
1290 void osp_precreate_fini(struct osp_device *d)
1292 struct ptlrpc_thread *thread = &d->opd_pre_thread;
1296 cfs_timer_disarm(&d->opd_statfs_timer);
1298 thread->t_flags = SVC_STOPPING;
1299 cfs_waitq_signal(&d->opd_pre_waitq);
1301 cfs_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);