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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/osp/osp_precreate.c
33 * Lustre OST Proxy Device
35 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
36 * Author: Mikhail Pershin <mike.pershin@intel.com>
37 * Author: Di Wang <di.wang@intel.com>
40 #define DEBUG_SUBSYSTEM S_MDS
42 #include <linux/kthread.h>
44 #include <lustre_obdo.h>
46 #include "osp_internal.h"
49 * there are two specific states to take care about:
51 * = import is disconnected =
53 * = import is inactive =
54 * in this case osp_declare_create() returns an error
59 * Check whether statfs data is expired
61 * OSP device caches statfs data for the target, the function checks
62 * whether the data is expired or not.
64 * \param[in] d OSP device
66 * \retval 0 - not expired, 1 - expired
68 static inline int osp_statfs_need_update(struct osp_device *d)
70 return !ktime_before(ktime_get(), d->opd_statfs_fresh_till);
74 * OSP tries to maintain pool of available objects so that calls to create
75 * objects don't block most of time
77 * each time OSP gets connected to OST, we should start from precreation cleanup
79 static void osp_statfs_timer_cb(cfs_timer_cb_arg_t data)
81 struct osp_device *d = cfs_from_timer(d, data, opd_statfs_timer);
84 /* invalidate statfs data so osp_precreate_thread() can refresh */
85 d->opd_statfs_fresh_till = ktime_sub_ns(ktime_get(), NSEC_PER_SEC);
87 wake_up(&d->opd_pre_waitq);
90 static void osp_pre_update_msfs(struct osp_device *d, struct obd_statfs *msfs);
93 * The function updates current precreation status if broken, and
94 * updates that cached statfs state if functional, then wakes up waiters.
95 * We don't clear opd_pre_status directly here, but rather leave this
96 * to osp_pre_update_msfs() to do if everything is OK so that we don't
97 * have a race to clear opd_pre_status and then set it to -ENOSPC again.
99 * \param[in] d OSP device
100 * \param[in] msfs statfs data
101 * \param[in] rc new precreate status for device \a d
103 static void osp_pre_update_status_msfs(struct osp_device *d,
104 struct obd_statfs *msfs, int rc)
106 CDEBUG(D_INFO, "%s: Updating status = %d\n", d->opd_obd->obd_name, rc);
108 d->opd_pre_status = rc;
110 osp_pre_update_msfs(d, msfs);
112 wake_up_all(&d->opd_pre_user_waitq);
115 /* Pass in the old statfs data in case the limits have changed */
116 void osp_pre_update_status(struct osp_device *d, int rc)
118 osp_pre_update_status_msfs(d, &d->opd_statfs, rc);
123 * RPC interpret callback for OST_STATFS RPC
125 * An interpretation callback called by ptlrpc for OST_STATFS RPC when it is
126 * replied by the target. It's used to maintain statfs cache for the target.
127 * The function fills data from the reply if successful and schedules another
130 * \param[in] env LU environment provided by the caller
131 * \param[in] req RPC replied
132 * \param[in] aa callback data
133 * \param[in] rc RPC result
135 * \retval 0 on success
136 * \retval negative negated errno on error
138 static int osp_statfs_interpret(const struct lu_env *env,
139 struct ptlrpc_request *req, void *args, int rc)
141 union ptlrpc_async_args *aa = args;
142 struct obd_import *imp = req->rq_import;
143 struct obd_statfs *msfs;
144 struct obd_statfs *sfs;
145 struct osp_device *d;
150 aa = ptlrpc_req_async_args(aa, req);
151 d = aa->pointer_arg[0];
157 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
159 GOTO(out, rc = -EPROTO);
162 osp_pre_update_status_msfs(d, msfs, 0);
164 osp_pre_update_msfs(d, msfs);
166 /* schedule next update */
167 maxage_ns = d->opd_statfs_maxage * NSEC_PER_SEC;
168 d->opd_statfs_fresh_till = ktime_add_ns(ktime_get(), maxage_ns);
169 mod_timer(&d->opd_statfs_timer,
170 jiffies + cfs_time_seconds(d->opd_statfs_maxage));
171 d->opd_statfs_update_in_progress = 0;
173 sfs = &d->opd_statfs;
175 "%s (%p): %llu blocks, %llu free, %llu avail, %u bsize, %u reserved mb low, %u reserved mb high, %u reserved ino low, %u reserved ino high, %llu files, %llu free files %#x\n",
176 d->opd_obd->obd_name, d, sfs->os_blocks, sfs->os_bfree,
177 sfs->os_bavail, sfs->os_bsize, d->opd_reserved_mb_low,
178 d->opd_reserved_mb_high, d->opd_reserved_ino_low,
179 d->opd_reserved_ino_high, sfs->os_files, sfs->os_ffree,
184 /* couldn't update statfs, try again with a small delay */
185 d->opd_statfs_fresh_till = ktime_add_ns(ktime_get(), 10 * NSEC_PER_SEC);
186 d->opd_statfs_update_in_progress = 0;
187 if (d->opd_pre && d->opd_pre_task)
188 wake_up(&d->opd_pre_waitq);
190 if (req->rq_import_generation == imp->imp_generation)
191 CDEBUG(D_CACHE, "%s: couldn't update statfs: rc = %d\n",
192 d->opd_obd->obd_name, rc);
197 * Send OST_STATFS RPC
199 * Sends OST_STATFS RPC to refresh cached statfs data for the target.
200 * Also disables scheduled updates as times OSP may need to refresh
201 * statfs data before expiration. The function doesn't block, instead
202 * an interpretation callback osp_statfs_interpret() is used.
204 * \param[in] env LU environment provided by the caller
205 * \param[in] d OSP device
207 static int osp_statfs_update(const struct lu_env *env, struct osp_device *d)
209 u64 expire = obd_timeout * 1000 * NSEC_PER_SEC;
210 struct ptlrpc_request *req;
211 struct obd_import *imp;
212 union ptlrpc_async_args *aa;
217 CDEBUG(D_CACHE, "going to update statfs\n");
219 imp = d->opd_obd->u.cli.cl_import;
222 req = ptlrpc_request_alloc(imp,
223 d->opd_pre ? &RQF_OST_STATFS : &RQF_MDS_STATFS);
227 rc = ptlrpc_request_pack(req,
228 d->opd_pre ? LUSTRE_OST_VERSION : LUSTRE_MDS_VERSION,
229 d->opd_pre ? OST_STATFS : MDS_STATFS);
231 ptlrpc_request_free(req);
234 ptlrpc_request_set_replen(req);
236 req->rq_request_portal = OST_CREATE_PORTAL;
237 ptlrpc_at_set_req_timeout(req);
239 req->rq_interpret_reply = osp_statfs_interpret;
240 aa = ptlrpc_req_async_args(aa, req);
241 aa->pointer_arg[0] = d;
244 * no updates till reply
246 timer_delete(&d->opd_statfs_timer);
247 d->opd_statfs_fresh_till = ktime_add_ns(ktime_get(), expire);
248 d->opd_statfs_update_in_progress = 1;
250 ptlrpcd_add_req(req);
252 /* we still want to sync changes if no new changes are coming */
253 if (ktime_before(ktime_get(), d->opd_sync_next_commit_cb))
256 if (atomic_read(&d->opd_sync_changes)) {
259 th = dt_trans_create(env, d->opd_storage);
261 CERROR("%s: can't sync\n", d->opd_obd->obd_name);
264 rc = dt_trans_start_local(env, d->opd_storage, th);
266 CDEBUG(D_OTHER, "%s: sync forced, %d changes\n",
267 d->opd_obd->obd_name,
268 atomic_read(&d->opd_sync_changes));
269 osp_sync_add_commit_cb_1s(env, d, th);
271 dt_trans_stop(env, d->opd_storage, th);
279 * Schedule an immediate update for statfs data
281 * If cached statfs data claim no free space, but OSP has got a request to
282 * destroy an object (so release some space probably), then we may need to
283 * refresh cached statfs data sooner than planned. The function checks there
284 * is no statfs update going and schedules immediate update if so.
285 * XXX: there might be a case where removed object(s) do not add free space (empty
286 * object). If the number of such deletions is high, then we can start to update
287 * statfs too often causing a RPC storm. some throttling is needed...
289 * \param[in] d OSP device where statfs data needs to be refreshed
291 void osp_statfs_need_now(struct osp_device *d)
293 if (!d->opd_statfs_update_in_progress) {
295 * if current status is -ENOSPC (lack of free space on OST)
296 * then we should poll OST immediately once object destroy
299 d->opd_statfs_fresh_till = ktime_sub_ns(ktime_get(), NSEC_PER_SEC);
300 timer_delete(&d->opd_statfs_timer);
301 wake_up(&d->opd_pre_waitq);
306 * Check pool of precreated objects is getting low.
308 * We should not wait till the pool of the precreated objects is too low,
309 * because then there will be a long period of OSP being unavailable for the
310 * new creations due to lengthy precreate RPC. Instead we ask for another
311 * precreation ahead and hopefully have it ready before the current pool is
312 * empty. Notice this function relies on external locking by opd_pre_lock.
314 * \param[in] d OSP device
316 * \retval 0 - current pool is good enough, 1 - time to precreate
318 static inline int osp_precreate_is_low_nolock(struct osp_device *d)
320 int available = osp_objs_precreated_nolock(d) - d->opd_pre_reserved;
321 int precreate_needed = d->opd_pre_create_count > 1024 ?
322 d->opd_pre_create_count / 4 : d->opd_pre_create_count / 2;
324 if (precreate_needed > 1024)
325 precreate_needed = 1024;
327 /* no new precreation until OST is healthy and has free space */
328 return ((d->opd_pre_create_count - available > precreate_needed ||
329 d->opd_force_creation) && (d->opd_pre_status == 0));
333 * Check pool of precreated objects
335 * This is protected version of osp_precreate_is_low_nolock(), check that
338 * \param[in] d OSP device
340 * \retval 0 - current pool is good enough, 1 - time to precreate
342 static inline int osp_precreate_is_low(struct osp_device *d)
346 if (d->opd_pre == NULL)
349 /* XXX: do we really need locking here? */
350 spin_lock(&d->opd_pre_lock);
351 rc = osp_precreate_is_low_nolock(d);
352 spin_unlock(&d->opd_pre_lock);
357 * Write FID into into last_oid/last_seq file
359 * The function stores the sequence and the in-sequence id into two dedicated
360 * files. The sync argument can be used to request synchronous commit, so the
361 * function won't return until the updates are committed.
363 * \param[in] env LU environment provided by the caller
364 * \param[in] osp OSP device
365 * \param[in] fid fid where sequence/id is taken
366 * \param[in] sync update mode: 0 - asynchronously, 1 - synchronously
368 * \retval 0 on success
369 * \retval negative negated errno on error
371 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
372 struct lu_fid *fid, int sync)
374 struct osp_thread_info *oti = osp_env_info(env);
375 struct lu_buf *lb_oid = &oti->osi_lb;
376 struct lu_buf *lb_oseq = &oti->osi_lb2;
384 if (osp->opd_storage->dd_rdonly)
387 /* Note: through f_oid is only 32 bits, it will also write 64 bits
388 * for oid to keep compatibility with the previous version. */
390 osp_objid_buf_prep(lb_oid, &oid_off,
391 &oid, osp->opd_index);
393 osp_objseq_buf_prep(lb_oseq, &oseq_off,
394 &fid->f_seq, osp->opd_index);
396 th = dt_trans_create(env, osp->opd_storage);
401 rc = dt_declare_record_write(env, osp->opd_last_used_oid_file,
402 lb_oid, oid_off, th);
406 rc = dt_declare_record_write(env, osp->opd_last_used_seq_file,
407 lb_oseq, oseq_off, th);
411 rc = dt_trans_start_local(env, osp->opd_storage, th);
415 rc = dt_record_write(env, osp->opd_last_used_oid_file, lb_oid,
418 CERROR("%s: can not write to last seq file: rc = %d\n",
419 osp->opd_obd->obd_name, rc);
422 rc = dt_record_write(env, osp->opd_last_used_seq_file, lb_oseq,
425 CERROR("%s: can not write to last seq file: rc = %d\n",
426 osp->opd_obd->obd_name, rc);
430 dt_trans_stop(env, osp->opd_storage, th);
434 static void osp_update_fldb_cache(const struct lu_env *env,
435 struct osp_device *osp,
438 struct lu_seq_range range = { 0 };
439 struct lu_server_fld *server_fld;
440 struct lu_site *site;
442 site = osp->opd_storage->dd_lu_dev.ld_site;
443 server_fld = lu_site2seq(site)->ss_server_fld;
447 fld_range_set_type(&range, LU_SEQ_RANGE_ANY);
448 fld_server_lookup(env, server_fld, fid_seq(fid), &range);
452 * Switch to another sequence
454 * When a current sequence has no available IDs left, OSP has to switch to
455 * another new sequence. OSP requests it using the regular FLDB protocol
456 * and stores synchronously before that is used in precreate. This is needed
457 * to basically have the sequences referenced (not orphaned), otherwise it's
458 * possible that OST has some objects precreated and the clients have data
459 * written to it, but after MDT failover nobody refers those objects and OSP
460 * has no idea that the sequence need cleanup to be done.
461 * While this is very expensive operation, it's supposed to happen infrequently
462 * because sequence has LUSTRE_DATA_SEQ_MAX_WIDTH=32M objects by default.
464 * \param[in] env LU environment provided by the caller
465 * \param[in] osp OSP device
467 * \retval 0 on success
468 * \retval negative negated errno on error
470 static int osp_precreate_rollover_new_seq(struct lu_env *env,
471 struct osp_device *osp)
473 struct lu_fid *fid = &osp_env_info(env)->osi_fid;
474 struct lu_fid *last_fid = &osp->opd_last_used_fid;
478 rc = seq_client_get_seq(env, osp->opd_obd->u.cli.cl_seq, &fid->f_seq);
480 CERROR("%s: alloc fid error: rc = %d\n",
481 osp->opd_obd->obd_name, rc);
487 LASSERTF(fid_seq(fid) != fid_seq(last_fid),
488 "fid "DFID", last_fid "DFID"\n", PFID(fid),
491 rc = osp_write_last_oid_seq_files(env, osp, fid, 1);
493 CERROR("%s: Can not update oid/seq file: rc = %d\n",
494 osp->opd_obd->obd_name, rc);
498 LCONSOLE(D_INFO, "%s: update sequence from %#llx to %#llx\n",
499 osp->opd_obd->obd_name, fid_seq(last_fid),
501 /* Update last_xxx to the new seq */
502 spin_lock(&osp->opd_pre_lock);
503 osp->opd_last_used_fid = *fid;
504 osp_fid_to_obdid(fid, &osp->opd_last_id);
505 osp->opd_gap_start_fid = *fid;
506 osp->opd_pre_used_fid = *fid;
507 osp->opd_pre_last_created_fid = *fid;
508 spin_unlock(&osp->opd_pre_lock);
511 osp_update_fldb_cache(env, osp, fid);
517 * Find IDs available in current sequence
519 * The function calculates the highest possible ID and the number of IDs
520 * available in the current sequence OSP is using. The number is limited
521 * artifically by the caller (grow param) and the number of IDs available
522 * in the sequence by nature. The function doesn't require an external
525 * \param[in] env LU environment provided by the caller
526 * \param[in] osp OSP device
527 * \param[in] fid FID the caller wants to start with
528 * \param[in] grow how many the caller wants
529 * \param[out] fid the highest calculated FID
530 * \param[out] grow the number of available IDs calculated
532 * \retval 0 on success, 1 - the sequence is empty
534 static int osp_precreate_fids(const struct lu_env *env, struct osp_device *osp,
535 struct lu_fid *fid, int *grow)
537 struct osp_thread_info *osi = osp_env_info(env);
538 __u64 seq_width = osp->opd_pre_seq_width;
542 if (fid_is_idif(fid)) {
543 struct lu_fid *last_fid;
544 struct ost_id *oi = &osi->osi_oi;
547 spin_lock(&osp->opd_pre_lock);
548 last_fid = &osp->opd_pre_last_created_fid;
549 fid_to_ostid(last_fid, oi);
550 end = min(ostid_id(oi) + *grow, min(IDIF_MAX_OID, seq_width));
551 *grow = end - ostid_id(oi);
552 rc = ostid_set_id(oi, ostid_id(oi) + *grow);
553 spin_unlock(&osp->opd_pre_lock);
555 if (*grow == 0 || rc)
558 ostid_to_fid(fid, oi, osp->opd_index);
562 spin_lock(&osp->opd_pre_lock);
563 *fid = osp->opd_pre_last_created_fid;
565 end = min((end + *grow), min(OBIF_MAX_OID, seq_width));
566 *grow = end - fid->f_oid;
567 fid->f_oid += end - fid->f_oid;
568 spin_unlock(&osp->opd_pre_lock);
570 CDEBUG(D_INFO, "Expect %d, actual %d ["DFID" -- "DFID"]\n",
571 *grow, i, PFID(fid), PFID(&osp->opd_pre_last_created_fid));
573 return *grow > 0 ? 0 : 1;
577 * Prepare and send precreate RPC
579 * The function finds how many objects should be precreated. Then allocates,
580 * prepares and schedules precreate RPC synchronously. Upon reply the function
581 * wakes up the threads waiting for the new objects on this target. If the
582 * target wasn't able to create all the objects requested, then the next
583 * precreate will be asking for fewer objects (i.e. slow precreate down).
585 * \param[in] env LU environment provided by the caller
586 * \param[in] d OSP device
588 * \retval 0 on success
589 * \retval negative negated errno on error
591 static int osp_precreate_send(const struct lu_env *env, struct osp_device *d)
593 struct osp_thread_info *oti = osp_env_info(env);
594 struct ptlrpc_request *req;
595 struct obd_import *imp;
596 struct ost_body *body;
598 struct lu_fid *fid = &oti->osi_fid;
601 /* don't precreate new objects till OST healthy and has free space */
602 if (unlikely(d->opd_pre_status)) {
603 CDEBUG(D_INFO, "%s: don't send new precreate: rc = %d\n",
604 d->opd_obd->obd_name, d->opd_pre_status);
609 * if not connection/initialization is compeleted, ignore
611 imp = d->opd_obd->u.cli.cl_import;
614 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
617 req->rq_request_portal = OST_CREATE_PORTAL;
619 /* We should not resend create request - anyway we will have delorphan
620 * and kill these objects.
621 * Only needed for MDS+OSS rolling upgrade interop with 2.16+older.
623 if (unlikely(!imp_connect_replay_create(imp)))
624 req->rq_no_delay = req->rq_no_resend = 1;
626 /* Delorphan happens only with a first MDT-OST connect. resend/replay
627 * handles objects creation on reconnects, no need to do delorhpan
631 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
633 ptlrpc_request_free(req);
637 spin_lock(&d->opd_pre_lock);
638 if (d->opd_pre_create_count > d->opd_pre_max_create_count / 2)
639 d->opd_pre_create_count = d->opd_pre_max_create_count / 2;
640 grow = d->opd_pre_create_count;
641 spin_unlock(&d->opd_pre_lock);
643 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
646 *fid = d->opd_pre_last_created_fid;
647 rc = osp_precreate_fids(env, d, fid, &grow);
649 /* Current seq has been used up*/
650 GOTO(out_req, rc = -ENOSPC);
652 if (!osp_is_fid_client(d)) {
653 /* Non-FID client will always send seq 0 because of
655 LASSERTF(fid_is_idif(fid), "Invalid fid "DFID"\n", PFID(fid));
659 fid_to_ostid(fid, &body->oa.o_oi);
660 body->oa.o_valid = OBD_MD_FLGROUP;
662 ptlrpc_request_set_replen(req);
664 if (CFS_FAIL_CHECK(OBD_FAIL_OSP_FAKE_PRECREATE))
667 rc = ptlrpc_queue_wait(req);
669 CERROR("%s: can't precreate: rc = %d\n", d->opd_obd->obd_name,
672 /* have osp_precreate_reserve() to wait for repeat */
677 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
679 GOTO(out_req, rc = -EPROTO);
681 ostid_to_fid(fid, &body->oa.o_oi, d->opd_index);
684 spin_lock(&d->opd_pre_lock);
686 if (osp_fid_diff(fid, &d->opd_pre_used_fid) <= 0) {
687 CERROR("%s: precreate fid "DFID" <= local used fid "DFID
688 ": rc = %d\n", d->opd_obd->obd_name,
689 PFID(fid), PFID(&d->opd_pre_used_fid), -ESTALE);
690 spin_unlock(&d->opd_pre_lock);
691 GOTO(out_req, rc = -ESTALE);
694 diff = osp_fid_diff(fid, &d->opd_pre_last_created_fid);
697 /* the OST has not managed to create all the
698 * objects we asked for */
699 d->opd_pre_create_count = max(diff, OST_MIN_PRECREATE);
700 d->opd_pre_create_slow = 1;
702 /* the OST is able to keep up with the work,
703 * we could consider increasing create_count
704 * next time if needed */
705 d->opd_pre_create_slow = 0;
708 if ((body->oa.o_valid & OBD_MD_FLSIZE) && body->oa.o_size)
709 d->opd_pre_seq_width = body->oa.o_size;
711 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
712 fid_to_ostid(fid, &body->oa.o_oi);
714 d->opd_pre_last_created_fid = *fid;
715 d->opd_force_creation = false;
716 spin_unlock(&d->opd_pre_lock);
718 CDEBUG(D_HA, "%s: current precreated pool: "DFID"-"DFID"\n",
719 d->opd_obd->obd_name, PFID(&d->opd_pre_used_fid),
720 PFID(&d->opd_pre_last_created_fid));
722 /* now we can wakeup all users awaiting for objects */
723 osp_pre_update_status(d, rc);
725 ptlrpc_req_finished(req);
728 osp_update_fldb_cache(env, d, fid);
734 * Get last precreated object from target (OST)
736 * Sends synchronous RPC to the target (OST) to learn the last precreated
737 * object. This later is used to remove all unused objects (cleanup orphan
738 * procedure). Also, the next object after one we got will be used as a
739 * starting point for the new precreates.
741 * \param[in] env LU environment provided by the caller
742 * \param[in] d OSP device
743 * \param[in] update update or not update last used fid
745 * \retval 0 on success
746 * \retval negative negated errno on error
748 static int osp_get_lastfid_from_ost(const struct lu_env *env,
749 struct osp_device *d, bool update)
751 struct ptlrpc_request *req = NULL;
752 struct obd_import *imp;
753 struct lu_fid *last_fid;
758 imp = d->opd_obd->u.cli.cl_import;
761 req = ptlrpc_request_alloc(imp, &RQF_OST_GET_INFO_LAST_FID);
765 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY, RCL_CLIENT,
766 sizeof(KEY_LAST_FID));
768 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
770 ptlrpc_request_free(req);
774 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
775 memcpy(tmp, KEY_LAST_FID, sizeof(KEY_LAST_FID));
777 req->rq_no_delay = req->rq_no_resend = 1;
778 last_fid = req_capsule_client_get(&req->rq_pill, &RMF_FID);
779 fid_cpu_to_le(last_fid, &d->opd_last_used_fid);
781 ptlrpc_request_set_replen(req);
783 rc = ptlrpc_queue_wait(req);
785 /* -EFAULT means reading LAST_FID failed (see ofd_get_info_hld),
786 * let sysadm sort this * out.
789 ptlrpc_set_import_active(imp, 0);
793 last_fid = req_capsule_server_get(&req->rq_pill, &RMF_FID);
794 if (last_fid == NULL) {
795 CERROR("%s: Got last_fid failed.\n", d->opd_obd->obd_name);
796 GOTO(out, rc = -EPROTO);
799 if (!fid_is_sane(last_fid)) {
800 CERROR("%s: Got insane last_fid "DFID"\n",
801 d->opd_obd->obd_name, PFID(last_fid));
802 GOTO(out, rc = -EPROTO);
805 /* Only update the last used fid, if the OST has objects for
806 * this sequence, i.e. fid_oid > 0 */
807 if (fid_oid(last_fid) > 0 && update)
808 d->opd_last_used_fid = *last_fid;
810 if (fid_seq(last_fid) == fid_seq(&d->opd_last_used_fid)) {
811 if (fid_oid(last_fid) == 0 ||
812 (fid_seq_is_norm(fid_seq(last_fid)) &&
813 fid_oid(last_fid) == LUSTRE_FID_INIT_OID)) {
814 /* reformatted OST, it requires creation request
815 * to recreate objects
817 spin_lock(&d->opd_pre_lock);
818 d->opd_force_creation = true;
819 d->opd_pre_create_count = OST_MIN_PRECREATE;
820 spin_unlock(&d->opd_pre_lock);
823 CDEBUG(D_HA, "%s: Got last_fid "DFID"\n", d->opd_obd->obd_name,
827 ptlrpc_req_finished(req);
832 * Cleanup orphans on OST
834 * This function is called in a contex of a dedicated thread handling
835 * all the precreation suff. The function waits till local recovery
836 * is complete, then identify all the unreferenced objects (orphans)
837 * using the highest ID referenced by a local and the highest object
838 * precreated by the target. The found range is a subject to removal
839 * using specially flagged RPC. During this process OSP is marked
840 * unavailable for new objects.
842 * \param[in] env LU environment provided by the caller
843 * \param[in] d OSP device
845 * \retval 0 on success
846 * \retval negative negated errno on error
848 static int osp_precreate_cleanup_orphans(struct lu_env *env,
849 struct osp_device *d)
851 struct osp_thread_info *osi = osp_env_info(env);
852 struct lu_fid *last_fid = &osi->osi_fid;
853 struct ptlrpc_request *req = NULL;
854 struct obd_import *imp = d->opd_obd->u.cli.cl_import;
855 struct ost_body *body;
856 int update_status = 0;
863 * Do cleanup orphans only with a first connection, after that
864 * all precreate requests uses resend/replay flags to support OST
865 * failover/reconnect.
867 if (d->opd_cleanup_orphans_done && imp_connect_replay_create(imp)) {
868 rc = osp_get_lastfid_from_ost(env, d, false);
872 * wait for local recovery to finish, so we can cleanup orphans
873 * orphans are all objects since "last used" (assigned), but
874 * there might be objects reserved and in some cases they won't
875 * be used. we can't cleanup them till we're sure they won't be
876 * used. also can't we allow new reservations because they may
877 * end up getting orphans being cleaned up below. so we block
878 * new reservations and wait till all reserved objects either
881 spin_lock(&d->opd_pre_lock);
882 d->opd_pre_recovering = 1;
883 spin_unlock(&d->opd_pre_lock);
885 * The locking above makes sure the opd_pre_reserved check below will
886 * catch all osp_precreate_reserve() calls who find
887 * "!opd_pre_recovering".
889 wait_event_idle(d->opd_pre_waitq,
890 (!d->opd_pre_reserved && d->opd_recovery_completed) ||
891 !d->opd_pre_task || d->opd_got_disconnected);
892 if (!d->opd_pre_task || d->opd_got_disconnected)
893 GOTO(out, rc = -EAGAIN);
895 CDEBUG(D_HA, "%s: going to cleanup orphans since "DFID"\n",
896 d->opd_obd->obd_name, PFID(&d->opd_last_used_fid));
898 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_DELAY_DELORPHAN, cfs_fail_val);
900 *last_fid = d->opd_last_used_fid;
901 /* The OSP should already get the valid seq now */
902 LASSERT(!fid_is_zero(last_fid));
903 if (fid_oid(&d->opd_last_used_fid) < 2) {
904 /* lastfid looks strange... ask OST */
905 rc = osp_get_lastfid_from_ost(env, d, true);
910 imp = d->opd_obd->u.cli.cl_import;
913 req = ptlrpc_request_alloc(imp, &RQF_OST_CREATE);
915 GOTO(out, rc = -ENOMEM);
917 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_CREATE);
919 ptlrpc_request_free(req);
924 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
926 GOTO(out, rc = -EPROTO);
928 body->oa.o_flags = OBD_FL_DELORPHAN;
929 body->oa.o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
931 fid_to_ostid(&d->opd_last_used_fid, &body->oa.o_oi);
933 ptlrpc_request_set_replen(req);
935 /* Don't resend the delorphan req */
936 req->rq_no_resend = req->rq_no_delay = 1;
938 rc = ptlrpc_queue_wait(req);
944 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
946 GOTO(out, rc = -EPROTO);
949 * OST provides us with id new pool starts from in body->oa.o_id
951 ostid_to_fid(last_fid, &body->oa.o_oi, d->opd_index);
953 spin_lock(&d->opd_pre_lock);
954 diff = osp_fid_diff(&d->opd_last_used_fid, last_fid);
956 d->opd_pre_create_count = OST_MIN_PRECREATE + diff;
957 d->opd_pre_last_created_fid = d->opd_last_used_fid;
959 d->opd_pre_create_count = OST_MIN_PRECREATE;
960 d->opd_pre_last_created_fid = *last_fid;
963 * This empties the pre-creation pool and effectively blocks any new
966 d->opd_pre_used_fid = d->opd_pre_last_created_fid;
967 d->opd_pre_create_slow = 0;
968 if ((body->oa.o_valid & OBD_MD_FLSIZE) && body->oa.o_size)
969 d->opd_pre_seq_width = body->oa.o_size;
970 spin_unlock(&d->opd_pre_lock);
972 CDEBUG(D_HA, "%s: Got last_id "DFID" from OST, last_created "DFID
973 "last_used is "DFID"\n", d->opd_obd->obd_name, PFID(last_fid),
974 PFID(&d->opd_pre_last_created_fid), PFID(&d->opd_last_used_fid));
977 ptlrpc_req_finished(req);
981 * If rc is zero, the pre-creation window should have been emptied.
982 * Since waking up the herd would be useless without pre-created
983 * objects, we defer the signal to osp_precreate_send() in that case.
987 CERROR("%s: cannot cleanup orphans: rc = %d\n",
988 d->opd_obd->obd_name, rc);
989 /* we can't proceed from here, OST seem to
990 * be in a bad shape, better to wait for
991 * a new instance of the server and repeat
992 * from the beginning. notify possible waiters
993 * this OSP isn't quite functional yet */
994 osp_pre_update_status(d, rc);
996 wake_up_all(&d->opd_pre_user_waitq);
999 spin_lock(&d->opd_pre_lock);
1000 d->opd_pre_recovering = 0;
1001 spin_unlock(&d->opd_pre_lock);
1002 d->opd_cleanup_orphans_done = true;
1009 * Update precreate status using statfs data
1011 * The function decides whether this OSP should be used for new objects.
1012 * IOW, whether this OST is used up or has some free space. Cached statfs
1013 * data is used to make this decision. If the latest result of statfs
1014 * request (rc argument) is not success, then just mark OSP unavailable
1017 * The new statfs data is passed in \a msfs and needs to be stored into
1018 * opd_statfs, but only after the various flags in os_state are set, so
1019 * that the new statfs data is not visible without appropriate flags set.
1020 * As such, there is no need to clear the flags here, since this is called
1021 * with new statfs data, and they should not be cleared if sent from OST.
1023 * Add a bit of hysteresis so this flag isn't continually flapping, and
1024 * ensure that new files don't get extremely fragmented due to only a
1025 * small amount of available space in the filesystem. We want to set
1026 * the ENOSPC/ENOINO flags unconditionally when there is less than the
1027 * reserved size free, and still copy them from the old state when there
1028 * is less than 2*reserved size free space or inodes.
1030 * \param[in] d OSP device
1031 * \param[in] msfs statfs data
1033 static void osp_pre_update_msfs(struct osp_device *d, struct obd_statfs *msfs)
1035 u32 old_state = d->opd_statfs.os_state;
1038 /* statfs structure not initialized yet */
1039 if (unlikely(!msfs->os_type))
1042 /* if the low and high watermarks have not been initialized yet */
1043 if (unlikely(d->opd_reserved_mb_high == 0 &&
1044 d->opd_reserved_mb_low == 0)) {
1045 /* Use ~0.1% by default to disable object allocation,
1046 * and ~0.2% to enable, size in MB, set both watermark
1048 spin_lock(&d->opd_pre_lock);
1049 if (d->opd_reserved_mb_high == 0 &&
1050 d->opd_reserved_mb_low == 0) {
1051 d->opd_reserved_mb_low = ((msfs->os_bsize >> 10) *
1052 msfs->os_blocks) >> 20;
1053 if (d->opd_reserved_mb_low < 1)
1054 d->opd_reserved_mb_low = 1;
1055 d->opd_reserved_mb_high =
1056 (d->opd_reserved_mb_low << 1) + 1;
1058 spin_unlock(&d->opd_pre_lock);
1061 if (unlikely(d->opd_reserved_ino_high == 0 &&
1062 d->opd_reserved_ino_low == 0)) {
1063 /* Use ~0.0001% by default to disallow distributed transactions,
1064 * and ~0.0002% to allow, set both watermark
1066 spin_lock(&d->opd_pre_lock);
1067 if (d->opd_reserved_ino_high == 0 &&
1068 d->opd_reserved_ino_low == 0) {
1069 d->opd_reserved_ino_low = msfs->os_ffree >> 20;
1070 if (d->opd_reserved_ino_low < 32)
1071 d->opd_reserved_ino_low = 32;
1072 d->opd_reserved_ino_high =
1073 (d->opd_reserved_ino_low << 1) + 1;
1075 spin_unlock(&d->opd_pre_lock);
1078 available_mb = (msfs->os_bavail * (msfs->os_bsize >> 10)) >> 10;
1079 if (msfs->os_ffree < d->opd_reserved_ino_low)
1080 msfs->os_state |= OS_STATFS_ENOINO;
1081 else if (msfs->os_ffree <= d->opd_reserved_ino_high)
1082 msfs->os_state |= old_state & OS_STATFS_ENOINO;
1083 /* else don't clear flags in new msfs->os_state sent from OST */
1085 if (available_mb < d->opd_reserved_mb_low)
1086 msfs->os_state |= OS_STATFS_ENOSPC;
1087 else if (available_mb <= d->opd_reserved_mb_high)
1088 msfs->os_state |= old_state & OS_STATFS_ENOSPC;
1089 /* else don't clear flags in new msfs->os_state sent from OST */
1092 "%s: blocks=%llu free=%llu avail=%llu avail_mb=%llu hwm_mb=%u files=%llu ffree=%llu state=%x: rc = %d\n",
1093 d->opd_obd->obd_name, msfs->os_blocks, msfs->os_bfree,
1094 msfs->os_bavail, available_mb, d->opd_reserved_mb_high,
1095 msfs->os_files, msfs->os_ffree, msfs->os_state,
1096 d->opd_pre ? d->opd_pre_status : 0);
1101 if (msfs->os_state & (OS_STATFS_ENOINO | OS_STATFS_ENOSPC)) {
1102 d->opd_pre_status = -ENOSPC;
1103 if (!(old_state & (OS_STATFS_ENOINO | OS_STATFS_ENOSPC)))
1104 CDEBUG(D_INFO, "%s: full: state=%x: rc = %x\n",
1105 d->opd_obd->obd_name, msfs->os_state,
1107 CDEBUG(D_INFO, "uncommitted changes=%u in_progress=%u\n",
1108 atomic_read(&d->opd_sync_changes),
1109 atomic_read(&d->opd_sync_rpcs_in_progress));
1110 } else if (old_state & (OS_STATFS_ENOINO | OS_STATFS_ENOSPC)) {
1111 d->opd_pre_status = 0;
1112 spin_lock(&d->opd_pre_lock);
1113 d->opd_pre_create_slow = 0;
1114 d->opd_pre_create_count = OST_MIN_PRECREATE;
1115 spin_unlock(&d->opd_pre_lock);
1116 wake_up(&d->opd_pre_waitq);
1119 "%s: available: state=%x: rc = %d\n",
1120 d->opd_obd->obd_name, msfs->os_state,
1123 /* we only get here if rc == 0 in the caller */
1124 d->opd_pre_status = 0;
1127 /* Object precreation skipped on OST if manually disabled */
1128 if (d->opd_pre_max_create_count == 0)
1129 msfs->os_state |= OS_STATFS_NOCREATE;
1130 /* else don't clear flags in new msfs->os_state sent from OST */
1133 /* copy only new statfs state to make it visible to MDS threads */
1134 if (&d->opd_statfs != msfs)
1135 d->opd_statfs = *msfs;
1139 * Initialize FID for precreation
1141 * For a just created new target, a new sequence should be taken.
1142 * The function checks there is no IDIF in use (if the target was
1143 * added with the older version of Lustre), then requests a new
1144 * sequence from FLDB using the regular protocol. Then this new
1145 * sequence is stored on a persisten storage synchronously to prevent
1146 * possible object leakage (for the detail see the description for
1147 * osp_precreate_rollover_new_seq()).
1149 * \param[in] osp OSP device
1151 * \retval 0 on success
1152 * \retval negative negated errno on error
1154 static int osp_init_pre_fid(struct lu_env *env, struct osp_device *osp)
1156 struct osp_thread_info *osi;
1157 struct lu_client_seq *cli_seq;
1158 struct lu_fid *last_fid;
1162 LASSERT(osp->opd_pre != NULL);
1164 if (CFS_FAIL_CHECK(OBD_FAIL_OSP_FAIL_SEQ_ALLOC)) {
1165 unsigned int timeout = cfs_fail_val ?: 1;
1167 schedule_timeout_uninterruptible(cfs_time_seconds(timeout));
1171 /* Let's check if the current last_seq/fid is valid,
1172 * otherwise request new sequence from the controller */
1173 if (osp_is_fid_client(osp) && osp->opd_group != 0) {
1174 /* Non-MDT0 can only use normal sequence for
1176 if (fid_is_norm(&osp->opd_last_used_fid))
1179 /* Initially MDT0 will start with IDIF, after
1180 * that it will request new sequence from the
1182 if (fid_is_idif(&osp->opd_last_used_fid) ||
1183 fid_is_norm(&osp->opd_last_used_fid))
1187 if (!fid_is_zero(&osp->opd_last_used_fid))
1188 CWARN("%s: invalid last used fid "DFID
1189 ", try to get new sequence.\n",
1190 osp->opd_obd->obd_name,
1191 PFID(&osp->opd_last_used_fid));
1193 osi = osp_env_info(env);
1194 last_fid = &osi->osi_fid;
1196 /* For a freshed fs, it will allocate a new sequence first */
1197 if (osp_is_fid_client(osp) && osp->opd_group != 0) {
1198 cli_seq = osp->opd_obd->u.cli.cl_seq;
1199 rc = seq_client_get_seq(env, cli_seq, &last_fid->f_seq);
1201 CERROR("%s: alloc fid error: rc = %d\n",
1202 osp->opd_obd->obd_name, rc);
1206 last_fid->f_seq = fid_idif_seq(0, osp->opd_index);
1208 last_fid->f_oid = 1;
1209 last_fid->f_ver = 0;
1211 spin_lock(&osp->opd_pre_lock);
1212 osp->opd_last_used_fid = *last_fid;
1213 osp->opd_pre_used_fid = *last_fid;
1214 osp->opd_pre_last_created_fid = *last_fid;
1215 spin_unlock(&osp->opd_pre_lock);
1216 rc = osp_write_last_oid_seq_files(env, osp, last_fid, 1);
1218 CERROR("%s: write fid error: rc = %d\n",
1219 osp->opd_obd->obd_name, rc);
1227 struct osp_device *opta_dev;
1228 struct lu_env opta_env;
1229 struct completion *opta_started;
1232 * The core of precreate functionality
1234 * The function implements the main precreation loop. Basically it
1235 * involves connecting to the target, precerate FID initialization,
1236 * identifying and removing orphans, then serving precreation. As
1237 * part of the latter, the thread is responsible for statfs data
1238 * updates. The precreation is mostly driven by another threads
1239 * asking for new OST objects - those askers wake the thread when
1240 * the number of precreated objects reach low watermark.
1241 * After a disconnect, the sequence above repeats. This is keep going
1242 * until the thread is requested to stop.
1244 * \param[in] _arg private data the thread (OSP device to handle)
1246 * \retval 0 on success
1247 * \retval negative negated errno on error
1249 static int osp_precreate_thread(void *_args)
1251 struct opt_args *args = _args;
1252 struct osp_device *d = args->opta_dev;
1253 struct lu_env *env = &args->opta_env;
1258 complete(args->opta_started);
1260 /* wait for connection from the layers above */
1261 wait_event_idle(d->opd_pre_waitq,
1262 kthread_should_stop() ||
1263 d->opd_obd->u.cli.cl_seq->lcs_exp != NULL);
1265 while (!kthread_should_stop()) {
1267 * need to be connected to OST
1269 while (!kthread_should_stop()) {
1270 if ((d->opd_pre == NULL || d->opd_pre_recovering) &&
1271 d->opd_imp_connected &&
1272 !d->opd_got_disconnected)
1274 wait_event_idle(d->opd_pre_waitq,
1275 kthread_should_stop() ||
1276 d->opd_new_connection);
1278 if (!d->opd_new_connection)
1281 CFS_FAIL_TIMEOUT(OBD_FAIL_OSP_CON_EVENT_DELAY,
1283 d->opd_new_connection = 0;
1284 d->opd_got_disconnected = 0;
1288 if (kthread_should_stop())
1292 LASSERT(d->opd_obd->u.cli.cl_seq != NULL);
1293 LASSERT(d->opd_obd->u.cli.cl_seq->lcs_exp != NULL);
1295 /* Init fid for osp_precreate if necessary */
1296 rc = osp_init_pre_fid(env, d);
1298 CERROR("%s: init pre fid error: rc = %d\n",
1299 d->opd_obd->obd_name, rc);
1304 if (osp_statfs_update(env, d)) {
1305 if (wait_event_idle_timeout(d->opd_pre_waitq,
1306 kthread_should_stop(),
1307 cfs_time_seconds(5)) == 0)
1308 l_wait_event_abortable(
1310 kthread_should_stop());
1316 * Clean up orphans or recreate missing objects.
1318 rc = osp_precreate_cleanup_orphans(env, d);
1320 schedule_timeout_interruptible(cfs_time_seconds(1));
1326 * connected, can handle precreates now
1328 while (!kthread_should_stop()) {
1329 wait_event_idle(d->opd_pre_waitq,
1330 kthread_should_stop() ||
1331 (osp_precreate_is_low(d) &&
1332 !(osp_precreate_end_seq(d) &&
1333 osp_objs_precreated(d) != 0)) ||
1334 osp_statfs_need_update(d) ||
1335 d->opd_got_disconnected);
1337 if (kthread_should_stop())
1340 /* something happened to the connection
1341 * have to start from the beginning */
1342 if (d->opd_got_disconnected)
1345 if (osp_statfs_need_update(d))
1346 if (osp_statfs_update(env, d))
1349 if (d->opd_pre == NULL)
1352 /* To avoid handling different seq in precreate/orphan
1353 * cleanup, it will hold precreate until current seq is
1355 if (unlikely(osp_precreate_end_seq(d))) {
1356 if (osp_objs_precreated(d) == 0) {
1357 rc = osp_precreate_rollover_new_seq(env, d);
1365 if (osp_precreate_is_low(d)) {
1366 rc = osp_precreate_send(env, d);
1367 /* osp_precreate_send() sets opd_pre_status
1368 * in case of error, that prevent the using of
1370 if (rc < 0 && rc != -ENOSPC &&
1371 rc != -ETIMEDOUT && rc != -ENOTCONN)
1372 CERROR("%s: cannot precreate objects:"
1374 d->opd_obd->obd_name, rc);
1386 * Check when to stop to wait for precreate objects.
1388 * The caller wanting a new OST object can't wait undefinitely. The
1389 * function checks for few conditions including available new OST
1390 * objects, disconnected OST, lack of space with no pending destroys,
1391 * etc. IOW, it checks whether the current OSP state is good to keep
1392 * waiting or it's better to give up.
1394 * \param[in] env LU environment provided by the caller
1395 * \param[in] d OSP device
1397 * \retval 0 - keep waiting, 1 - no luck
1399 static int osp_precreate_ready_condition(const struct lu_env *env,
1400 struct osp_device *d)
1402 /* Bail out I/O fails to OST */
1403 if (d->opd_pre_status != 0 &&
1404 d->opd_pre_status != -EAGAIN &&
1405 d->opd_pre_status != -ENODEV &&
1406 d->opd_pre_status != -ENOTCONN &&
1407 d->opd_pre_status != -ENOSPC) {
1409 if (d->opd_pre_status != -EIO)
1410 CERROR("%s: precreate failed opd_pre_status %d\n",
1411 d->opd_obd->obd_name, d->opd_pre_status);
1415 if (d->opd_pre_recovering || d->opd_force_creation)
1418 /* ready if got enough precreated objects */
1419 /* we need to wait for others (opd_pre_reserved) and our object (+1) */
1420 if (d->opd_pre_reserved + 1 < osp_objs_precreated(d))
1423 /* ready if OST reported no space and no destroys in progress */
1424 if (atomic_read(&d->opd_sync_changes) +
1425 atomic_read(&d->opd_sync_rpcs_in_progress) == 0 &&
1426 d->opd_pre_status == -ENOSPC)
1433 * Reserve object in precreate pool
1435 * When the caller wants to create a new object on this target (target
1436 * represented by the given OSP), it should declare this intention using
1437 * a regular ->dt_declare_create() OSD API method. Then OSP will be trying
1438 * to reserve an object in the existing precreated pool or wait up to
1439 * obd_timeout for the available object to appear in the pool (a dedicated
1440 * thread will be doing real precreation in background). The object can be
1441 * consumed later with osp_precreate_get_fid() or be released with call to
1442 * lu_object_put(). Notice the function doesn't reserve a specific ID, just
1443 * some ID. The actual ID assignment happen in osp_precreate_get_fid().
1444 * If the space on the target is short and there is a pending object destroy,
1445 * then the function forces local commit to speedup space release (see
1446 * osp_sync.c for the details).
1448 * \param[in] env LU environment provided by the caller
1449 * \param[in] d OSP device
1451 * \retval 0 on success
1452 * \retval -ENOSPC when no space on OST
1453 * \retval -EAGAIN try later, slow precreation in progress
1454 * \retval -EIO when no access to OST
1456 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d,
1459 time64_t expire = ktime_get_seconds() + obd_timeout;
1460 int precreated, rc, synced = 0;
1464 LASSERTF(osp_objs_precreated(d) >= 0, "Last created FID "DFID
1465 "Next FID "DFID"\n", PFID(&d->opd_pre_last_created_fid),
1466 PFID(&d->opd_pre_used_fid));
1468 /* opd_pre_max_create_count 0 to not use specified OST. */
1469 if (d->opd_pre_max_create_count == 0)
1474 * - preallocation is done
1475 * - no free space expected soon
1476 * - can't connect to OST for too long (obd_timeout)
1477 * - OST can allocate fid sequence.
1479 while ((rc = d->opd_pre_status) == 0 || rc == -ENOSPC ||
1480 rc == -ENODEV || rc == -EAGAIN || rc == -ENOTCONN) {
1482 spin_lock(&d->opd_pre_lock);
1483 precreated = osp_objs_precreated_nolock(d);
1485 * increase number of precreations
1487 if (d->opd_pre_create_count < d->opd_pre_max_create_count &&
1488 d->opd_pre_create_slow == 0 &&
1489 precreated <= (d->opd_pre_create_count / 4 + 1)) {
1490 d->opd_pre_create_slow = 1;
1491 d->opd_pre_create_count *= 2;
1494 if (!d->opd_pre_recovering && !d->opd_force_creation) {
1495 if (precreated > d->opd_pre_reserved) {
1496 d->opd_pre_reserved++;
1497 spin_unlock(&d->opd_pre_lock);
1501 * XXX: don't wake up if precreation
1504 if (osp_precreate_is_low_nolock(d) &&
1505 !osp_precreate_end_seq_nolock(d))
1506 wake_up(&d->opd_pre_waitq);
1509 } else if (unlikely(precreated &&
1510 osp_precreate_end_seq_nolock(d))) {
1512 * precreate pool is reaching the end of the
1513 * current seq, and doesn't have enough objects
1516 spin_unlock(&d->opd_pre_lock);
1520 spin_unlock(&d->opd_pre_lock);
1523 * all precreated objects have been used and no-space
1524 * status leave us no chance to succeed very soon
1525 * but if there is destroy in progress, then we should
1526 * wait till that is done - some space might be released
1528 if (unlikely(rc == -ENOSPC)) {
1529 if (atomic_read(&d->opd_sync_changes) && synced == 0) {
1530 /* force local commit to release space */
1531 dt_commit_async(env, d->opd_storage);
1532 osp_sync_check_for_work(d);
1535 if (atomic_read(&d->opd_sync_rpcs_in_progress)) {
1536 /* just wait till destroys are done
1537 * see wait_event_idle_timeout() below
1540 if (atomic_read(&d->opd_sync_changes) +
1541 atomic_read(&d->opd_sync_rpcs_in_progress) == 0) {
1542 /* no hope for free space */
1547 /* XXX: don't wake up if precreation is in progress */
1548 wake_up(&d->opd_pre_waitq);
1550 if (ktime_get_seconds() >= expire) {
1556 LASSERT(d->opd_pre);
1561 CDEBUG(D_INFO, "%s: Sleeping on objects\n",
1562 d->opd_obd->obd_name);
1563 if (wait_event_idle_timeout(
1564 d->opd_pre_user_waitq,
1565 osp_precreate_ready_condition(env, d),
1566 cfs_time_seconds(obd_timeout)) == 0) {
1568 "%s: slow creates, last="DFID", next="DFID", "
1569 "reserved=%llu, sync_changes=%u, "
1570 "sync_rpcs_in_progress=%d, status=%d\n",
1571 d->opd_obd->obd_name,
1572 PFID(&d->opd_pre_last_created_fid),
1573 PFID(&d->opd_pre_used_fid), d->opd_pre_reserved,
1574 atomic_read(&d->opd_sync_changes),
1575 atomic_read(&d->opd_sync_rpcs_in_progress),
1578 CDEBUG(D_INFO, "%s: Waked up, status=%d\n",
1579 d->opd_obd->obd_name, d->opd_pre_status);
1587 * Get a FID from precreation pool
1589 * The function is a companion for osp_precreate_reserve() - it assigns
1590 * a specific FID from the precreate. The function should be called only
1591 * if the call to osp_precreate_reserve() was successful. The function
1592 * updates a local storage to remember the highest object ID referenced
1593 * by the node in the given sequence.
1595 * A very importan details: this is supposed to be called once the
1596 * transaction is started, so on-disk update will be atomic with the
1597 * data (like LOVEA) refering this object. Then the object won't be leaked:
1598 * either it's referenced by the committed transaction or it's a subject
1599 * to the orphan cleanup procedure.
1601 * \param[in] env LU environment provided by the caller
1602 * \param[in] d OSP device
1603 * \param[out] fid generated FID
1605 * \retval 0 on success
1606 * \retval negative negated errno on error
1608 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
1611 struct lu_fid *pre_used_fid = &d->opd_pre_used_fid;
1613 /* grab next id from the pool */
1614 spin_lock(&d->opd_pre_lock);
1616 LASSERTF(osp_fid_diff(&d->opd_pre_used_fid,
1617 &d->opd_pre_last_created_fid) < 0,
1618 "next fid "DFID" > last created fid "DFID"\n",
1619 PFID(&d->opd_pre_used_fid),
1620 PFID(&d->opd_pre_last_created_fid));
1622 /* Non-IDIF FIDs shouldn't get here with OID == OBIF_MAX_OID. For IDIF,
1623 * f_oid wraps and "f_seq" (holding high 16 bits of ID) needs increment
1625 if (fid_is_idif(pre_used_fid) &&
1626 unlikely(fid_oid(pre_used_fid) == OBIF_MAX_OID)) {
1628 __u32 idx = fid_idif_ost_idx(pre_used_fid);
1630 fid_to_ostid(pre_used_fid, &oi);
1632 ostid_to_fid(pre_used_fid, &oi, idx);
1634 pre_used_fid->f_oid++;
1637 memcpy(fid, pre_used_fid, sizeof(*fid));
1638 d->opd_pre_reserved--;
1640 * last_used_id must be changed along with getting new id otherwise
1641 * we might miscalculate gap causing object loss or leak
1643 osp_update_last_fid(d, fid);
1644 spin_unlock(&d->opd_pre_lock);
1647 * probably main thread suspended orphan cleanup till
1648 * all reservations are released, see comment in
1649 * osp_precreate_thread() just before orphan cleanup
1651 if (unlikely(d->opd_pre_reserved == 0 &&
1652 (d->opd_pre_recovering || d->opd_pre_status)))
1653 wake_up(&d->opd_pre_waitq);
1659 * Set size regular attribute on an object
1661 * When a striping is created late, it's possible that size is already
1662 * initialized on the file. Then the new striping should inherit size
1663 * from the file. The function sets size on the object using the regular
1664 * protocol (OST_PUNCH).
1665 * XXX: should be re-implemented using OUT ?
1667 * \param[in] env LU environment provided by the caller
1668 * \param[in] dt object
1669 * \param[in] size size to set.
1671 * \retval 0 on success
1672 * \retval negative negated errno on error
1674 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt,
1677 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1678 struct ptlrpc_request *req = NULL;
1679 struct obd_import *imp;
1680 struct ost_body *body;
1681 struct obdo *oa = NULL;
1686 imp = d->opd_obd->u.cli.cl_import;
1689 req = ptlrpc_request_alloc(imp, &RQF_OST_PUNCH);
1693 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_PUNCH);
1695 ptlrpc_request_free(req);
1700 * XXX: decide how do we do here with resend
1701 * if we don't resend, then client may see wrong file size
1702 * if we do resend, then MDS thread can get stuck for quite long
1703 * and if we don't resend, then client will also get -EAGAIN !!
1704 * (see LU-7975 and sanity/test_27F use cases)
1705 * but let's decide not to resend/delay this truncate request to OST
1706 * and allow Client to decide to resend, in a less agressive way from
1707 * after_reply(), by returning -EINPROGRESS instead of
1708 * -EAGAIN/-EAGAIN upon return from ptlrpc_queue_wait() at the
1709 * end of this routine
1711 req->rq_no_resend = req->rq_no_delay = 1;
1713 req->rq_request_portal = OST_IO_PORTAL; /* bug 7198 */
1714 ptlrpc_at_set_req_timeout(req);
1718 GOTO(out, rc = -ENOMEM);
1720 rc = fid_to_ostid(lu_object_fid(&dt->do_lu), &oa->o_oi);
1723 oa->o_blocks = OBD_OBJECT_EOF;
1724 oa->o_valid = OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1725 OBD_MD_FLID | OBD_MD_FLGROUP;
1727 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1729 lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
1731 /* XXX: capa support? */
1732 /* osc_pack_capa(req, body, capa); */
1734 ptlrpc_request_set_replen(req);
1736 rc = ptlrpc_queue_wait(req);
1738 /* -EAGAIN/-EWOULDBLOCK means OST is unreachable at the moment
1739 * since we have decided not to resend/delay, but this could
1740 * lead to wrong size to be seen at Client side and even process
1741 * trying to open to exit/fail if not itself handling -EAGAIN.
1742 * So it should be better to return -EINPROGRESS instead and
1743 * leave the decision to resend at Client side in after_reply()
1745 if (rc == -EAGAIN) {
1747 CDEBUG(D_HA, "returning -EINPROGRESS instead of "
1748 "-EWOULDBLOCK/-EAGAIN to allow Client to "
1751 CERROR("can't punch object: %d\n", rc);
1755 ptlrpc_req_finished(req);
1762 * Initialize precreation functionality of OSP
1764 * Prepares all the internal structures and starts the precreate thread
1766 * \param[in] d OSP device
1768 * \retval 0 on success
1769 * \retval negative negated errno on error
1771 int osp_init_precreate(struct osp_device *d)
1775 OBD_ALLOC_PTR(d->opd_pre);
1776 if (d->opd_pre == NULL)
1779 /* initially precreation isn't ready */
1780 init_waitqueue_head(&d->opd_pre_user_waitq);
1781 d->opd_pre_status = -EAGAIN;
1782 fid_zero(&d->opd_pre_used_fid);
1783 d->opd_pre_used_fid.f_oid = 1;
1784 fid_zero(&d->opd_pre_last_created_fid);
1785 d->opd_pre_last_created_fid.f_oid = 1;
1787 d->opd_pre_reserved = 0;
1788 d->opd_pre_seq_width = LUSTRE_DATA_SEQ_MAX_WIDTH;
1789 d->opd_got_disconnected = 1;
1790 d->opd_pre_create_slow = 0;
1791 d->opd_pre_create_count = OST_MIN_PRECREATE;
1792 d->opd_pre_min_create_count = OST_MIN_PRECREATE;
1793 d->opd_pre_max_create_count = OST_MAX_PRECREATE;
1794 d->opd_reserved_mb_high = 0;
1795 d->opd_reserved_mb_low = 0;
1796 d->opd_cleanup_orphans_done = false;
1797 d->opd_force_creation = false;
1803 * Finish precreate functionality of OSP
1806 * Asks all the activity (the thread, update timer) to stop, then
1807 * wait till that is done.
1809 * \param[in] d OSP device
1811 void osp_precreate_fini(struct osp_device *d)
1815 if (d->opd_pre == NULL)
1818 OBD_FREE_PTR(d->opd_pre);
1824 int osp_init_statfs(struct osp_device *d)
1826 struct task_struct *task;
1827 struct opt_args *args;
1828 DECLARE_COMPLETION_ONSTACK(started);
1833 spin_lock_init(&d->opd_pre_lock);
1834 init_waitqueue_head(&d->opd_pre_waitq);
1837 * Initialize statfs-related things
1839 d->opd_statfs_maxage = 5; /* defaultupdate interval */
1840 d->opd_statfs_fresh_till = ktime_sub_ns(ktime_get(),
1841 1000 * NSEC_PER_SEC);
1842 CDEBUG(D_OTHER, "current %lldns, fresh till %lldns\n",
1844 ktime_to_ns(d->opd_statfs_fresh_till));
1845 cfs_timer_setup(&d->opd_statfs_timer, osp_statfs_timer_cb,
1846 (unsigned long)d, 0);
1848 if (d->opd_storage->dd_rdonly)
1851 OBD_ALLOC_PTR(args);
1855 args->opta_started = &started;
1856 rc = lu_env_init(&args->opta_env,
1857 d->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1859 CERROR("%s: init env error: rc = %d\n", d->opd_obd->obd_name,
1866 * start thread handling precreation and statfs updates
1868 task = kthread_create(osp_precreate_thread, args,
1869 "osp-pre-%u-%u", d->opd_index, d->opd_group);
1871 CERROR("can't start precreate thread %ld\n", PTR_ERR(task));
1872 lu_env_fini(&args->opta_env);
1874 RETURN(PTR_ERR(task));
1876 d->opd_pre_task = task;
1877 wake_up_process(task);
1878 wait_for_completion(&started);
1883 void osp_statfs_fini(struct osp_device *d)
1885 struct task_struct *task = d->opd_pre_task;
1888 timer_delete(&d->opd_statfs_timer);
1890 d->opd_pre_task = NULL;