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);
68 static void osp_statfs_timer_cb(unsigned long _d)
70 struct osp_device *d = (struct osp_device *) _d;
73 wake_up(&d->opd_pre_waitq);
76 static int osp_statfs_interpret(const struct lu_env *env,
77 struct ptlrpc_request *req,
78 union ptlrpc_async_args *aa, int rc)
80 struct obd_import *imp = req->rq_import;
81 struct obd_statfs *msfs;
86 aa = ptlrpc_req_async_args(req);
87 d = aa->pointer_arg[0];
93 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
95 GOTO(out, rc = -EPROTO);
97 d->opd_statfs = *msfs;
99 osp_pre_update_status(d, rc);
101 /* schedule next update */
102 d->opd_statfs_fresh_till = cfs_time_shift(d->opd_statfs_maxage);
103 cfs_timer_arm(&d->opd_statfs_timer, d->opd_statfs_fresh_till);
104 d->opd_statfs_update_in_progress = 0;
106 CDEBUG(D_CACHE, "updated statfs %p\n", d);
110 /* couldn't update statfs, try again as soon as possible */
111 wake_up(&d->opd_pre_waitq);
112 if (req->rq_import_generation == imp->imp_generation)
113 CDEBUG(D_CACHE, "%s: couldn't update statfs: rc = %d\n",
114 d->opd_obd->obd_name, rc);
118 static int osp_statfs_update(struct osp_device *d)
120 struct ptlrpc_request *req;
121 struct obd_import *imp;
122 union ptlrpc_async_args *aa;
127 CDEBUG(D_CACHE, "going to update statfs\n");
129 imp = d->opd_obd->u.cli.cl_import;
132 req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
136 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
138 ptlrpc_request_free(req);
141 ptlrpc_request_set_replen(req);
142 req->rq_request_portal = OST_CREATE_PORTAL;
143 ptlrpc_at_set_req_timeout(req);
145 req->rq_interpret_reply = (ptlrpc_interpterer_t)osp_statfs_interpret;
146 aa = ptlrpc_req_async_args(req);
147 aa->pointer_arg[0] = d;
150 * no updates till reply
152 cfs_timer_disarm(&d->opd_statfs_timer);
153 d->opd_statfs_fresh_till = cfs_time_shift(obd_timeout * 1000);
154 d->opd_statfs_update_in_progress = 1;
156 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
162 * XXX: there might be a case where removed object(s) do not add free
163 * space (empty object). if the number of such deletions is high, then
164 * we can start to update statfs too often - a rpc storm
165 * TODO: some throttling is needed
167 void osp_statfs_need_now(struct osp_device *d)
169 if (!d->opd_statfs_update_in_progress) {
171 * if current status is -ENOSPC (lack of free space on OST)
172 * then we should poll OST immediately once object destroy
175 d->opd_statfs_fresh_till = cfs_time_shift(-1);
176 cfs_timer_disarm(&d->opd_statfs_timer);
177 wake_up(&d->opd_pre_waitq);
183 * OSP tries to maintain pool of available objects so that calls to create
184 * objects don't block most of time
186 * each time OSP gets connected to OST, we should start from precreation cleanup
188 static inline int osp_precreate_running(struct osp_device *d)
190 return !!(d->opd_pre_thread.t_flags & SVC_RUNNING);
193 static inline int osp_precreate_stopped(struct osp_device *d)
195 return !!(d->opd_pre_thread.t_flags & SVC_STOPPED);
198 static inline int osp_objs_precreated(const struct lu_env *env,
199 struct osp_device *osp)
201 struct lu_fid *fid1 = &osp->opd_pre_last_created_fid;
202 struct lu_fid *fid2 = &osp->opd_pre_used_fid;
204 LASSERTF(fid_seq(fid1) == fid_seq(fid2),
205 "Created fid"DFID" Next fid "DFID"\n", PFID(fid1), PFID(fid2));
207 if (fid_is_idif(fid1)) {
208 struct ost_id *oi1 = &osp_env_info(env)->osi_oi;
209 struct ost_id *oi2 = &osp_env_info(env)->osi_oi2;
211 LASSERT(fid_is_idif(fid1) && fid_is_idif(fid2));
212 fid_to_ostid(fid1, oi1);
213 fid_to_ostid(fid2, oi2);
214 LASSERT(ostid_id(oi1) >= ostid_id(oi2));
216 return ostid_id(oi1) - ostid_id(oi2);
219 return fid_oid(fid1) - fid_oid(fid2);
222 static inline int osp_precreate_near_empty_nolock(const struct lu_env *env,
223 struct osp_device *d)
225 int window = osp_objs_precreated(env, d);
227 /* don't consider new precreation till OST is healty and
229 return ((window - d->opd_pre_reserved < d->opd_pre_grow_count / 2) &&
230 (d->opd_pre_status == 0));
233 static inline int osp_precreate_near_empty(const struct lu_env *env,
234 struct osp_device *d)
238 /* XXX: do we really need locking here? */
239 spin_lock(&d->opd_pre_lock);
240 rc = osp_precreate_near_empty_nolock(env, d);
241 spin_unlock(&d->opd_pre_lock);
245 static inline int osp_create_end_seq(const struct lu_env *env,
246 struct osp_device *osp)
248 struct lu_fid *fid = &osp->opd_pre_used_fid;
251 spin_lock(&osp->opd_pre_lock);
252 rc = osp_fid_end_seq(env, fid);
253 spin_unlock(&osp->opd_pre_lock);
258 * Write fid into last_oid/last_seq file.
260 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
261 struct lu_fid *fid, int sync)
263 struct osp_thread_info *oti = osp_env_info(env);
264 struct lu_buf *lb_oid = &oti->osi_lb;
265 struct lu_buf *lb_oseq = &oti->osi_lb2;
272 /* Note: through f_oid is only 32bits, it will also write
273 * 64 bits for oid to keep compatiblity with the previous
275 lb_oid->lb_buf = &fid->f_oid;
276 lb_oid->lb_len = sizeof(obd_id);
277 oid_off = sizeof(obd_id) * osp->opd_index;
279 lb_oseq->lb_buf = &fid->f_seq;
280 lb_oseq->lb_len = sizeof(obd_id);
281 oseq_off = sizeof(obd_id) * osp->opd_index;
283 th = dt_trans_create(env, osp->opd_storage);
288 rc = dt_declare_record_write(env, osp->opd_last_used_oid_file,
289 lb_oid->lb_len, oid_off, th);
293 rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
294 lb_oseq->lb_len, oseq_off, th);
298 rc = dt_trans_start_local(env, osp->opd_storage, th);
302 rc = dt_record_write(env, osp->opd_last_used_oid_file, lb_oid,
305 CERROR("%s: can not write to last seq file: rc = %d\n",
306 osp->opd_obd->obd_name, rc);
309 rc = dt_record_write(env, osp->opd_last_used_seq_file, lb_oseq,
312 CERROR("%s: can not write to last seq file: rc = %d\n",
313 osp->opd_obd->obd_name, rc);
317 dt_trans_stop(env, osp->opd_storage, th);
321 int osp_precreate_rollover_new_seq(struct lu_env *env, struct osp_device *osp)
323 struct lu_fid *fid = &osp_env_info(env)->osi_fid;
324 struct lu_fid *last_fid = &osp->opd_last_used_fid;
328 rc = seq_client_get_seq(env, osp->opd_obd->u.cli.cl_seq, &fid->f_seq);
330 CERROR("%s: alloc fid error: rc = %d\n",
331 osp->opd_obd->obd_name, rc);
337 LASSERTF(fid_seq(fid) != fid_seq(last_fid),
338 "fid "DFID", last_fid "DFID"\n", PFID(fid),
341 rc = osp_write_last_oid_seq_files(env, osp, fid, 1);
343 CERROR("%s: Can not update oid/seq file: rc = %d\n",
344 osp->opd_obd->obd_name, rc);
348 LCONSOLE_INFO("%s: update sequence from "LPX64" to "LPX64"\n",
349 osp->opd_obd->obd_name, fid_seq(last_fid),
351 /* Update last_xxx to the new seq */
352 spin_lock(&osp->opd_pre_lock);
353 osp->opd_last_used_fid = *fid;
354 osp->opd_gap_start_fid = *fid;
355 osp->opd_pre_used_fid = *fid;
356 osp->opd_pre_last_created_fid = *fid;
357 spin_unlock(&osp->opd_pre_lock);
363 * alloc fids for precreation.
364 * rc = 0 Success, @grow is the count of real allocation.
365 * rc = 1 Current seq is used up.
366 * rc < 0 Other error.
368 static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
369 struct lu_fid *fid, int *grow)
371 struct osp_thread_info *osi = osp_env_info(env);
375 if (fid_is_idif(fid)) {
376 struct lu_fid *last_fid;
377 struct ost_id *oi = &osi->osi_oi;
379 spin_lock(&osp->opd_pre_lock);
380 last_fid = &osp->opd_pre_last_created_fid;
381 fid_to_ostid(last_fid, oi);
382 end = min(ostid_id(oi) + *grow, IDIF_MAX_OID);
383 *grow = end - ostid_id(oi);
384 ostid_set_id(oi, ostid_id(oi) + *grow);
385 spin_unlock(&osp->opd_pre_lock);
390 ostid_to_fid(fid, oi, osp->opd_index);
394 spin_lock(&osp->opd_pre_lock);
395 *fid = osp->opd_pre_last_created_fid;
397 end = min((end + *grow), (__u64)LUSTRE_DATA_SEQ_MAX_WIDTH);
398 *grow = end - fid->f_oid;
399 fid->f_oid += end - fid->f_oid;
400 spin_unlock(&osp->opd_pre_lock);
402 CDEBUG(D_INFO, "Expect %d, actual %d ["DFID" -- "DFID"]\n",
403 *grow, i, PFID(fid), PFID(&osp->opd_pre_last_created_fid));
405 return *grow > 0 ? 0 : 1;
408 static int osp_precreate_send(const struct lu_env *env, struct osp_device *d)
410 struct osp_thread_info *oti = osp_env_info(env);
411 struct ptlrpc_request *req;
412 struct obd_import *imp;
413 struct ost_body *body;
415 struct lu_fid *fid = &oti->osi_fid;
418 /* don't precreate new objects till OST healthy and has free space */
419 if (unlikely(d->opd_pre_status)) {
420 CDEBUG(D_INFO, "%s: don't send new precreate: rc = %d\n",
421 d->opd_obd->obd_name, d->opd_pre_status);
426 * if not connection/initialization is compeleted, ignore
428 imp = d->opd_obd->u.cli.cl_import;
431 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
434 req->rq_request_portal = OST_CREATE_PORTAL;
435 /* we should not resend create request - anyway we will have delorphan
436 * and kill these objects */
437 req->rq_no_delay = req->rq_no_resend = 1;
439 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
441 ptlrpc_request_free(req);
445 spin_lock(&d->opd_pre_lock);
446 if (d->opd_pre_grow_count > d->opd_pre_max_grow_count / 2)
447 d->opd_pre_grow_count = d->opd_pre_max_grow_count / 2;
448 grow = d->opd_pre_grow_count;
449 spin_unlock(&d->opd_pre_lock);
451 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
454 *fid = d->opd_pre_last_created_fid;
455 rc = osp_precreate_fids(env, d, fid, &grow);
457 /* Current seq has been used up*/
458 if (!osp_is_fid_client(d)) {
459 osp_pre_update_status(d, -ENOSPC);
462 wake_up(&d->opd_pre_waitq);
466 if (!osp_is_fid_client(d)) {
467 /* Non-FID client will always send seq 0 because of
469 LASSERTF(fid_is_idif(fid), "Invalid fid "DFID"\n", PFID(fid));
473 fid_to_ostid(fid, &body->oa.o_oi);
474 body->oa.o_valid = OBD_MD_FLGROUP;
476 ptlrpc_request_set_replen(req);
478 rc = ptlrpc_queue_wait(req);
480 CERROR("%s: can't precreate: rc = %d\n", d->opd_obd->obd_name,
484 LASSERT(req->rq_transno == 0);
486 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
488 GOTO(out_req, rc = -EPROTO);
490 ostid_to_fid(fid, &body->oa.o_oi, d->opd_index);
491 LASSERTF(lu_fid_diff(fid, &d->opd_pre_used_fid) > 0,
492 "reply fid "DFID" pre used fid "DFID"\n", PFID(fid),
493 PFID(&d->opd_pre_used_fid));
495 diff = lu_fid_diff(fid, &d->opd_pre_last_created_fid);
497 spin_lock(&d->opd_pre_lock);
499 /* the OST has not managed to create all the
500 * objects we asked for */
501 d->opd_pre_grow_count = max(diff, OST_MIN_PRECREATE);
502 d->opd_pre_grow_slow = 1;
504 /* the OST is able to keep up with the work,
505 * we could consider increasing grow_count
506 * next time if needed */
507 d->opd_pre_grow_slow = 0;
510 d->opd_pre_last_created_fid = *fid;
511 spin_unlock(&d->opd_pre_lock);
513 CDEBUG(D_HA, "%s: current precreated pool: "DFID"-"DFID"\n",
514 d->opd_obd->obd_name, PFID(&d->opd_pre_used_fid),
515 PFID(&d->opd_pre_last_created_fid));
517 /* now we can wakeup all users awaiting for objects */
518 osp_pre_update_status(d, rc);
519 wake_up(&d->opd_pre_user_waitq);
521 ptlrpc_req_finished(req);
525 static int osp_get_lastfid_from_ost(const struct lu_env *env,
526 struct osp_device *d)
528 struct ptlrpc_request *req = NULL;
529 struct obd_import *imp;
530 struct lu_fid *last_fid;
535 imp = d->opd_obd->u.cli.cl_import;
538 req = ptlrpc_request_alloc(imp, &RQF_OST_GET_INFO_LAST_FID);
542 req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY, RCL_CLIENT,
543 sizeof(KEY_LAST_FID));
545 req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_VAL, RCL_CLIENT,
546 sizeof(struct lu_fid));
548 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
550 ptlrpc_request_free(req);
554 tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
555 memcpy(tmp, KEY_LAST_FID, sizeof(KEY_LAST_FID));
557 req->rq_no_delay = req->rq_no_resend = 1;
558 tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
559 fid_cpu_to_le((struct lu_fid *)tmp, &d->opd_last_used_fid);
560 ptlrpc_request_set_replen(req);
562 rc = ptlrpc_queue_wait(req);
564 /* bad-bad OST.. let sysadm sort this out */
565 if (rc == -ENOTSUPP) {
566 CERROR("%s: server does not support FID: rc = %d\n",
567 d->opd_obd->obd_name, -ENOTSUPP);
569 ptlrpc_set_import_active(imp, 0);
573 last_fid = req_capsule_server_get(&req->rq_pill, &RMF_FID);
574 if (last_fid == NULL) {
575 CERROR("%s: Got last_fid failed.\n", d->opd_obd->obd_name);
576 GOTO(out, rc = -EPROTO);
579 if (!fid_is_sane(last_fid)) {
580 CERROR("%s: Got insane last_fid "DFID"\n",
581 d->opd_obd->obd_name, PFID(last_fid));
582 GOTO(out, rc = -EPROTO);
585 /* Only update the last used fid, if the OST has objects for
586 * this sequence, i.e. fid_oid > 0 */
587 if (fid_oid(last_fid) > 0)
588 d->opd_last_used_fid = *last_fid;
590 CDEBUG(D_HA, "%s: Got last_fid "DFID"\n", d->opd_obd->obd_name,
594 ptlrpc_req_finished(req);
599 * asks OST to clean precreate orphans
600 * and gets next id for new objects
602 static int osp_precreate_cleanup_orphans(struct lu_env *env,
603 struct osp_device *d)
605 struct osp_thread_info *osi = osp_env_info(env);
606 struct lu_fid *last_fid = &osi->osi_fid;
607 struct ptlrpc_request *req = NULL;
608 struct obd_import *imp;
609 struct ost_body *body;
610 struct l_wait_info lwi = { 0 };
611 int update_status = 0;
618 * wait for local recovery to finish, so we can cleanup orphans
619 * orphans are all objects since "last used" (assigned), but
620 * there might be objects reserved and in some cases they won't
621 * be used. we can't cleanup them till we're sure they won't be
622 * used. also can't we allow new reservations because they may
623 * end up getting orphans being cleaned up below. so we block
624 * new reservations and wait till all reserved objects either
627 spin_lock(&d->opd_pre_lock);
628 d->opd_pre_recovering = 1;
629 spin_unlock(&d->opd_pre_lock);
631 * The locking above makes sure the opd_pre_reserved check below will
632 * catch all osp_precreate_reserve() calls who find
633 * "!opd_pre_recovering".
635 l_wait_event(d->opd_pre_waitq,
636 (!d->opd_pre_reserved && d->opd_recovery_completed) ||
637 !osp_precreate_running(d) || d->opd_got_disconnected,
639 if (!osp_precreate_running(d) || d->opd_got_disconnected)
640 GOTO(out, rc = -EAGAIN);
642 CDEBUG(D_HA, "%s: going to cleanup orphans since "DFID"\n",
643 d->opd_obd->obd_name, PFID(&d->opd_last_used_fid));
645 *last_fid = d->opd_last_used_fid;
646 /* The OSP should already get the valid seq now */
647 LASSERT(!fid_is_zero(last_fid));
648 if (fid_oid(&d->opd_last_used_fid) < 2) {
649 /* lastfid looks strange... ask OST */
650 rc = osp_get_lastfid_from_ost(env, d);
655 imp = d->opd_obd->u.cli.cl_import;
658 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
660 GOTO(out, rc = -ENOMEM);
662 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
664 ptlrpc_request_free(req);
669 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
671 GOTO(out, rc = -EPROTO);
673 body->oa.o_flags = OBD_FL_DELORPHAN;
674 body->oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
676 fid_to_ostid(&d->opd_last_used_fid, &body->oa.o_oi);
678 ptlrpc_request_set_replen(req);
680 /* Don't resend the delorphan req */
681 req->rq_no_resend = req->rq_no_delay = 1;
683 rc = ptlrpc_queue_wait(req);
689 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
691 GOTO(out, rc = -EPROTO);
694 * OST provides us with id new pool starts from in body->oa.o_id
696 ostid_to_fid(last_fid, &body->oa.o_oi, d->opd_index);
698 spin_lock(&d->opd_pre_lock);
699 diff = lu_fid_diff(&d->opd_last_used_fid, last_fid);
701 d->opd_pre_grow_count = OST_MIN_PRECREATE + diff;
702 d->opd_pre_last_created_fid = d->opd_last_used_fid;
704 d->opd_pre_grow_count = OST_MIN_PRECREATE;
705 d->opd_pre_last_created_fid = *last_fid;
708 * This empties the pre-creation pool and effectively blocks any new
711 LASSERT(fid_oid(&d->opd_pre_last_created_fid) <=
712 LUSTRE_DATA_SEQ_MAX_WIDTH);
713 d->opd_pre_used_fid = d->opd_pre_last_created_fid;
714 d->opd_pre_grow_slow = 0;
715 spin_unlock(&d->opd_pre_lock);
717 CDEBUG(D_HA, "%s: Got last_id "DFID" from OST, last_created "DFID
718 "last_used is "DFID"\n", d->opd_obd->obd_name, PFID(last_fid),
719 PFID(&d->opd_pre_last_created_fid), PFID(&d->opd_last_used_fid));
722 ptlrpc_req_finished(req);
724 spin_lock(&d->opd_pre_lock);
725 d->opd_pre_recovering = 0;
726 spin_unlock(&d->opd_pre_lock);
729 * If rc is zero, the pre-creation window should have been emptied.
730 * Since waking up the herd would be useless without pre-created
731 * objects, we defer the signal to osp_precreate_send() in that case.
735 CERROR("%s: cannot cleanup orphans: rc = %d\n",
736 d->opd_obd->obd_name, rc);
737 /* we can't proceed from here, OST seem to
738 * be in a bad shape, better to wait for
739 * a new instance of the server and repeat
740 * from the beginning. notify possible waiters
741 * this OSP isn't quite functional yet */
742 osp_pre_update_status(d, rc);
744 wake_up(&d->opd_pre_user_waitq);
752 * the function updates current precreation status used: functional or not
754 * rc is a last code from the transport, rc == 0 meaning transport works
755 * well and users of lod can use objects from this OSP
757 * the status depends on current usage of OST
759 void osp_pre_update_status(struct osp_device *d, int rc)
761 struct obd_statfs *msfs = &d->opd_statfs;
762 int old = d->opd_pre_status;
765 d->opd_pre_status = rc;
769 /* Add a bit of hysteresis so this flag isn't continually flapping,
770 * and ensure that new files don't get extremely fragmented due to
771 * only a small amount of available space in the filesystem.
772 * We want to set the NOSPC flag when there is less than ~0.1% free
773 * and clear it when there is at least ~0.2% free space, so:
774 * avail < ~0.1% max max = avail + used
775 * 1025 * avail < avail + used used = blocks - free
776 * 1024 * avail < used
777 * 1024 * avail < blocks - free
778 * avail < ((blocks - free) >> 10)
780 * On very large disk, say 16TB 0.1% will be 16 GB. We don't want to
781 * lose that amount of space so in those cases we report no space left
782 * if their is less than 1 GB left. */
783 if (likely(msfs->os_type)) {
784 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
786 if ((msfs->os_ffree < 32) || (msfs->os_bavail < used)) {
787 d->opd_pre_status = -ENOSPC;
789 CDEBUG(D_INFO, "%s: status: "LPU64" blocks, "
790 LPU64" free, "LPU64" used, "LPU64" "
791 "avail -> %d: rc = %d\n",
792 d->opd_obd->obd_name, msfs->os_blocks,
793 msfs->os_bfree, used, msfs->os_bavail,
794 d->opd_pre_status, rc);
796 "non-commited changes: %lu, in progress: %u\n",
797 d->opd_syn_changes, d->opd_syn_rpc_in_progress);
798 } else if (old == -ENOSPC) {
799 d->opd_pre_status = 0;
800 spin_lock(&d->opd_pre_lock);
801 d->opd_pre_grow_slow = 0;
802 d->opd_pre_grow_count = OST_MIN_PRECREATE;
803 spin_unlock(&d->opd_pre_lock);
804 wake_up(&d->opd_pre_waitq);
805 CDEBUG(D_INFO, "%s: no space: "LPU64" blocks, "LPU64
806 " free, "LPU64" used, "LPU64" avail -> %d: "
807 "rc = %d\n", d->opd_obd->obd_name,
808 msfs->os_blocks, msfs->os_bfree, used,
809 msfs->os_bavail, d->opd_pre_status, rc);
814 wake_up(&d->opd_pre_user_waitq);
817 static int osp_init_pre_fid(struct osp_device *osp)
820 struct osp_thread_info *osi;
821 struct lu_client_seq *cli_seq;
822 struct lu_fid *last_fid;
826 /* Return if last_used fid has been initialized */
827 if (!fid_is_zero(&osp->opd_last_used_fid))
830 rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
832 CERROR("%s: init env error: rc = %d\n",
833 osp->opd_obd->obd_name, rc);
837 osi = osp_env_info(&env);
838 last_fid = &osi->osi_fid;
840 /* For a freshed fs, it will allocate a new sequence first */
841 if (osp_is_fid_client(osp) && osp->opd_group != 0) {
842 cli_seq = osp->opd_obd->u.cli.cl_seq;
843 rc = seq_client_get_seq(&env, cli_seq, &last_fid->f_seq);
845 CERROR("%s: alloc fid error: rc = %d\n",
846 osp->opd_obd->obd_name, rc);
850 last_fid->f_seq = fid_idif_seq(0, osp->opd_index);
855 spin_lock(&osp->opd_pre_lock);
856 osp->opd_last_used_fid = *last_fid;
857 osp->opd_pre_used_fid = *last_fid;
858 osp->opd_pre_last_created_fid = *last_fid;
859 spin_unlock(&osp->opd_pre_lock);
860 rc = osp_write_last_oid_seq_files(&env, osp, last_fid, 1);
862 CERROR("%s: write fid error: rc = %d\n",
863 osp->opd_obd->obd_name, rc);
871 static int osp_precreate_thread(void *_arg)
873 struct osp_device *d = _arg;
874 struct ptlrpc_thread *thread = &d->opd_pre_thread;
875 struct l_wait_info lwi = { 0 };
881 rc = lu_env_init(&env, d->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
883 CERROR("%s: init env error: rc = %d\n", d->opd_obd->obd_name,
888 spin_lock(&d->opd_pre_lock);
889 thread->t_flags = SVC_RUNNING;
890 spin_unlock(&d->opd_pre_lock);
891 wake_up(&thread->t_ctl_waitq);
893 while (osp_precreate_running(d)) {
895 * need to be connected to OST
897 while (osp_precreate_running(d)) {
898 l_wait_event(d->opd_pre_waitq,
899 !osp_precreate_running(d) ||
900 d->opd_new_connection,
903 if (!d->opd_new_connection)
906 d->opd_new_connection = 0;
907 d->opd_got_disconnected = 0;
911 if (!osp_precreate_running(d))
914 LASSERT(d->opd_obd->u.cli.cl_seq != NULL);
915 if (d->opd_obd->u.cli.cl_seq->lcs_exp == NULL) {
916 /* Get new sequence for client first */
917 LASSERT(d->opd_exp != NULL);
918 d->opd_obd->u.cli.cl_seq->lcs_exp =
919 class_export_get(d->opd_exp);
920 rc = osp_init_pre_fid(d);
922 class_export_put(d->opd_exp);
923 d->opd_obd->u.cli.cl_seq->lcs_exp = NULL;
924 CERROR("%s: init pre fid error: rc = %d\n",
925 d->opd_obd->obd_name, rc);
930 osp_statfs_update(d);
933 * Clean up orphans or recreate missing objects.
935 rc = osp_precreate_cleanup_orphans(&env, d);
939 * connected, can handle precreates now
941 while (osp_precreate_running(d)) {
942 l_wait_event(d->opd_pre_waitq,
943 !osp_precreate_running(d) ||
944 osp_precreate_near_empty(&env, d) ||
945 osp_statfs_need_update(d) ||
946 d->opd_got_disconnected, &lwi);
948 if (!osp_precreate_running(d))
951 /* something happened to the connection
952 * have to start from the beginning */
953 if (d->opd_got_disconnected)
956 if (osp_statfs_need_update(d))
957 osp_statfs_update(d);
959 /* To avoid handling different seq in precreate/orphan
960 * cleanup, it will hold precreate until current seq is
962 if (unlikely(osp_precreate_end_seq(&env, d) &&
963 !osp_create_end_seq(&env, d)))
966 if (unlikely(osp_precreate_end_seq(&env, d) &&
967 osp_create_end_seq(&env, d))) {
968 LCONSOLE_INFO("%s:"LPX64" is used up."
969 " Update to new seq\n",
970 d->opd_obd->obd_name,
971 fid_seq(&d->opd_pre_last_created_fid));
972 rc = osp_precreate_rollover_new_seq(&env, d);
977 if (osp_precreate_near_empty(&env, d)) {
978 rc = osp_precreate_send(&env, d);
979 /* osp_precreate_send() sets opd_pre_status
980 * in case of error, that prevent the using of
982 if (rc < 0 && rc != -ENOSPC &&
983 rc != -ETIMEDOUT && rc != -ENOTCONN)
984 CERROR("%s: cannot precreate objects:"
986 d->opd_obd->obd_name, rc);
991 thread->t_flags = SVC_STOPPED;
993 wake_up(&thread->t_ctl_waitq);
998 static int osp_precreate_ready_condition(const struct lu_env *env,
999 struct osp_device *d)
1001 if (d->opd_pre_recovering)
1004 /* ready if got enough precreated objects */
1005 /* we need to wait for others (opd_pre_reserved) and our object (+1) */
1006 if (d->opd_pre_reserved + 1 < osp_objs_precreated(env, d))
1009 /* ready if OST reported no space and no destroys in progress */
1010 if (d->opd_syn_changes + d->opd_syn_rpc_in_progress == 0 &&
1011 d->opd_pre_status == -ENOSPC)
1014 /* Bail out I/O fails to OST */
1015 if (d->opd_pre_status == -EIO)
1021 static int osp_precreate_timeout_condition(void *data)
1023 struct osp_device *d = data;
1025 LCONSOLE_WARN("%s: slow creates, last="DFID", next="DFID", "
1026 "reserved="LPU64", syn_changes=%lu, "
1027 "syn_rpc_in_progress=%d, status=%d\n",
1028 d->opd_obd->obd_name, PFID(&d->opd_pre_last_created_fid),
1029 PFID(&d->opd_pre_used_fid), d->opd_pre_reserved,
1030 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
1037 * called to reserve object in the pool
1039 * ENOSPC - no space on corresponded OST
1040 * EAGAIN - precreation is in progress, try later
1041 * EIO - no access to OST
1043 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d)
1045 struct l_wait_info lwi;
1046 cfs_time_t expire = cfs_time_shift(obd_timeout);
1051 LASSERTF(osp_objs_precreated(env, d) >= 0, "Last created FID "DFID
1052 "Next FID "DFID"\n", PFID(&d->opd_pre_last_created_fid),
1053 PFID(&d->opd_pre_used_fid));
1057 * - preallocation is done
1058 * - no free space expected soon
1059 * - can't connect to OST for too long (obd_timeout)
1060 * - OST can allocate fid sequence.
1062 while ((rc = d->opd_pre_status) == 0 || rc == -ENOSPC ||
1063 rc == -ENODEV || rc == -EAGAIN) {
1066 * increase number of precreations
1068 precreated = osp_objs_precreated(env, d);
1069 if (d->opd_pre_grow_count < d->opd_pre_max_grow_count &&
1070 d->opd_pre_grow_slow == 0 &&
1071 precreated <= (d->opd_pre_grow_count / 4 + 1)) {
1072 spin_lock(&d->opd_pre_lock);
1073 d->opd_pre_grow_slow = 1;
1074 d->opd_pre_grow_count *= 2;
1075 spin_unlock(&d->opd_pre_lock);
1078 spin_lock(&d->opd_pre_lock);
1079 precreated = osp_objs_precreated(env, d);
1080 if (precreated > d->opd_pre_reserved &&
1081 !d->opd_pre_recovering) {
1082 d->opd_pre_reserved++;
1083 spin_unlock(&d->opd_pre_lock);
1086 /* XXX: don't wake up if precreation is in progress */
1087 if (osp_precreate_near_empty_nolock(env, d) &&
1088 !osp_precreate_end_seq_nolock(env, d))
1089 wake_up(&d->opd_pre_waitq);
1093 spin_unlock(&d->opd_pre_lock);
1096 * all precreated objects have been used and no-space
1097 * status leave us no chance to succeed very soon
1098 * but if there is destroy in progress, then we should
1099 * wait till that is done - some space might be released
1101 if (unlikely(rc == -ENOSPC)) {
1102 if (d->opd_syn_changes) {
1103 /* force local commit to release space */
1104 dt_commit_async(env, d->opd_storage);
1106 if (d->opd_syn_rpc_in_progress) {
1107 /* just wait till destroys are done */
1108 /* see l_wait_even() few lines below */
1110 if (d->opd_syn_changes +
1111 d->opd_syn_rpc_in_progress == 0) {
1112 /* no hope for free space */
1117 /* XXX: don't wake up if precreation is in progress */
1118 wake_up(&d->opd_pre_waitq);
1120 lwi = LWI_TIMEOUT(expire - cfs_time_current(),
1121 osp_precreate_timeout_condition, d);
1122 if (cfs_time_aftereq(cfs_time_current(), expire)) {
1127 l_wait_event(d->opd_pre_user_waitq,
1128 osp_precreate_ready_condition(env, d), &lwi);
1135 * this function relies on reservation made before
1137 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
1140 /* grab next id from the pool */
1141 spin_lock(&d->opd_pre_lock);
1143 LASSERTF(lu_fid_diff(&d->opd_pre_used_fid,
1144 &d->opd_pre_last_created_fid) < 0,
1145 "next fid "DFID" last created fid "DFID"\n",
1146 PFID(&d->opd_pre_used_fid),
1147 PFID(&d->opd_pre_last_created_fid));
1149 d->opd_pre_used_fid.f_oid++;
1150 memcpy(fid, &d->opd_pre_used_fid, sizeof(*fid));
1151 d->opd_pre_reserved--;
1153 * last_used_id must be changed along with getting new id otherwise
1154 * we might miscalculate gap causing object loss or leak
1156 osp_update_last_fid(d, fid);
1157 spin_unlock(&d->opd_pre_lock);
1160 * probably main thread suspended orphan cleanup till
1161 * all reservations are released, see comment in
1162 * osp_precreate_thread() just before orphan cleanup
1164 if (unlikely(d->opd_pre_reserved == 0 && d->opd_pre_status))
1165 wake_up(&d->opd_pre_waitq);
1173 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt,
1176 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1177 struct ptlrpc_request *req = NULL;
1178 struct obd_import *imp;
1179 struct ost_body *body;
1180 struct obdo *oa = NULL;
1185 imp = d->opd_obd->u.cli.cl_import;
1188 req = ptlrpc_request_alloc(imp, &RQF_OST_PUNCH);
1192 /* XXX: capa support? */
1193 /* osc_set_capa_size(req, &RMF_CAPA1, capa); */
1194 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_PUNCH);
1196 ptlrpc_request_free(req);
1201 * XXX: decide how do we do here with resend
1202 * if we don't resend, then client may see wrong file size
1203 * if we do resend, then MDS thread can get stuck for quite long
1205 req->rq_no_resend = req->rq_no_delay = 1;
1207 req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
1208 ptlrpc_at_set_req_timeout(req);
1212 GOTO(out, rc = -ENOMEM);
1214 rc = fid_to_ostid(lu_object_fid(&dt->do_lu), &oa->o_oi);
1217 oa->o_blocks = OBD_OBJECT_EOF;
1218 oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1219 OBD_MD_FLID | OBD_MD_FLGROUP;
1221 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1223 lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
1225 /* XXX: capa support? */
1226 /* osc_pack_capa(req, body, capa); */
1228 ptlrpc_request_set_replen(req);
1230 rc = ptlrpc_queue_wait(req);
1232 CERROR("can't punch object: %d\n", rc);
1234 ptlrpc_req_finished(req);
1240 int osp_init_precreate(struct osp_device *d)
1242 struct l_wait_info lwi = { 0 };
1243 struct task_struct *task;
1247 /* initially precreation isn't ready */
1248 d->opd_pre_status = -EAGAIN;
1249 fid_zero(&d->opd_pre_used_fid);
1250 d->opd_pre_used_fid.f_oid = 1;
1251 fid_zero(&d->opd_pre_last_created_fid);
1252 d->opd_pre_last_created_fid.f_oid = 1;
1253 d->opd_pre_reserved = 0;
1254 d->opd_got_disconnected = 1;
1255 d->opd_pre_grow_slow = 0;
1256 d->opd_pre_grow_count = OST_MIN_PRECREATE;
1257 d->opd_pre_min_grow_count = OST_MIN_PRECREATE;
1258 d->opd_pre_max_grow_count = OST_MAX_PRECREATE;
1260 spin_lock_init(&d->opd_pre_lock);
1261 init_waitqueue_head(&d->opd_pre_waitq);
1262 init_waitqueue_head(&d->opd_pre_user_waitq);
1263 init_waitqueue_head(&d->opd_pre_thread.t_ctl_waitq);
1266 * Initialize statfs-related things
1268 d->opd_statfs_maxage = 5; /* default update interval */
1269 d->opd_statfs_fresh_till = cfs_time_shift(-1000);
1270 CDEBUG(D_OTHER, "current %llu, fresh till %llu\n",
1271 (unsigned long long)cfs_time_current(),
1272 (unsigned long long)d->opd_statfs_fresh_till);
1273 cfs_timer_init(&d->opd_statfs_timer, osp_statfs_timer_cb, d);
1276 * start thread handling precreation and statfs updates
1278 task = kthread_run(osp_precreate_thread, d,
1279 "osp-pre-%u", d->opd_index);
1281 CERROR("can't start precreate thread %ld\n", PTR_ERR(task));
1282 RETURN(PTR_ERR(task));
1285 l_wait_event(d->opd_pre_thread.t_ctl_waitq,
1286 osp_precreate_running(d) || osp_precreate_stopped(d),
1292 void osp_precreate_fini(struct osp_device *d)
1294 struct ptlrpc_thread *thread = &d->opd_pre_thread;
1298 cfs_timer_disarm(&d->opd_statfs_timer);
1300 thread->t_flags = SVC_STOPPING;
1301 wake_up(&d->opd_pre_waitq);
1303 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);