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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ofd/ofd_obd.c
34 * This file contains OBD API methods for OBD Filter Device (OFD) which are
35 * used for export handling, configuration purposes and recovery.
36 * Several methods are used by ECHO client only since it still uses OBD API.
37 * Such methods have _echo_ prefix in name.
39 * Author: Andreas Dilger <andreas.dilger@intel.com>
40 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.pershin@intel.com>
44 #define DEBUG_SUBSYSTEM S_FILTER
46 #include <lustre/lustre_idl.h>
47 #include "ofd_internal.h"
48 #include <obd_cksum.h>
49 #include <lustre_ioctl.h>
50 #include <lustre_quota.h>
51 #include <lustre_lfsck.h>
52 #include <lustre_nodemap.h>
55 * Initialize OFD per-export statistics.
57 * This function sets up procfs entries for various OFD export counters. These
58 * counters are for per-client statistics tracked on the server.
60 * \param[in] ofd OFD device
61 * \param[in] exp OBD export
62 * \param[in] client_nid NID of client
64 * \retval 0 if successful
65 * \retval negative value on error
67 static int ofd_export_stats_init(struct ofd_device *ofd,
68 struct obd_export *exp,
69 lnet_nid_t *client_nid)
71 struct obd_device *obd = ofd_obd(ofd);
72 struct nid_stat *stats;
76 LASSERT(obd->obd_uses_nid_stats);
78 if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
79 /* Self-export gets no proc entry */
82 rc = lprocfs_exp_setup(exp, client_nid);
84 /* Mask error for already created /proc entries */
85 RETURN(rc == -EALREADY ? 0 : rc);
87 stats = exp->exp_nid_stats;
88 stats->nid_stats = lprocfs_alloc_stats(NUM_OBD_STATS +
90 LPROCFS_STATS_FLAG_NOPERCPU);
91 if (stats->nid_stats == NULL)
94 lprocfs_init_ops_stats(LPROC_OFD_STATS_LAST, stats->nid_stats);
96 ofd_stats_counter_init(stats->nid_stats);
98 rc = lprocfs_register_stats(stats->nid_proc, "stats", stats->nid_stats);
100 lprocfs_free_stats(&stats->nid_stats);
104 rc = lprocfs_nid_ldlm_stats_init(stats);
113 * Match client and OST server connection feature flags.
115 * Compute the compatibility flags for a connection request based on
116 * features mutually supported by client and server.
118 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
119 * must not be updated here, otherwise a partially initialized value may
120 * be exposed. After the connection request is successfully processed,
121 * the top-level tgt_connect() request handler atomically updates the export
122 * connect flags from the obd_connect_data::ocd_connect_flags field of the
123 * reply. \see tgt_connect().
125 * \param[in] env execution environment
126 * \param[in] exp the obd_export associated with this
128 * \param[in] data stores data for this connect request
129 * \param[in] new_connection is this connection new or not
131 * \retval 0 if success
132 * \retval -EPROTO client and server feature requirements are
134 * \retval -EBADF OST index in connect request doesn't match
137 static int ofd_parse_connect_data(const struct lu_env *env,
138 struct obd_export *exp,
139 struct obd_connect_data *data,
142 struct ofd_device *ofd = ofd_exp(exp);
143 struct filter_export_data *fed = &exp->exp_filter_data;
148 CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: %#llx"
149 " ocd_version: %x ocd_grant: %d ocd_index: %u"
151 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
152 data->ocd_connect_flags, data->ocd_version,
153 data->ocd_grant, data->ocd_index, data->ocd_group);
155 if (fed->fed_group != 0 && fed->fed_group != data->ocd_group) {
156 CWARN("!!! This export (nid %s) used object group %d "
157 "earlier; now it's trying to use group %d! This could "
158 "be a bug in the MDS. Please report to "
159 "https://jira.hpdd.intel.com/\n",
160 obd_export_nid2str(exp), fed->fed_group,
164 fed->fed_group = data->ocd_group;
166 data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
167 data->ocd_connect_flags2 &= OST_CONNECT_SUPPORTED2;
168 data->ocd_version = LUSTRE_VERSION_CODE;
170 /* Kindly make sure the SKIP_ORPHAN flag is from MDS. */
171 if (data->ocd_connect_flags & OBD_CONNECT_MDS)
172 CDEBUG(D_HA, "%s: Received MDS connection for group %u\n",
173 exp->exp_obd->obd_name, data->ocd_group);
174 else if (data->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN)
177 /* Determine optimal brw size before calculating grant */
178 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
179 data->ocd_brw_size = 65536;
180 } else if (OCD_HAS_FLAG(data, BRW_SIZE)) {
181 if (data->ocd_brw_size > ofd->ofd_brw_size)
182 data->ocd_brw_size = ofd->ofd_brw_size;
183 if (data->ocd_brw_size == 0) {
184 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx"
185 " ocd_version: %x ocd_grant: %d ocd_index: %u "
186 "ocd_brw_size is unexpectedly zero, "
187 "network data corruption?"
188 "Refusing connection of this client\n",
189 exp->exp_obd->obd_name,
190 exp->exp_client_uuid.uuid,
191 exp, data->ocd_connect_flags, data->ocd_version,
192 data->ocd_grant, data->ocd_index);
197 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
198 /* client is reporting its page size, for future use */
199 exp->exp_filter_data.fed_pagebits = data->ocd_grant_blkbits;
200 data->ocd_grant_blkbits = ofd->ofd_blockbits;
201 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
202 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
203 data->ocd_grant_inobits =
204 fls(ofd->ofd_dt_conf.ddp_inodespace - 1);
205 /* ocd_grant_tax_kb is in 1K byte blocks */
206 data->ocd_grant_tax_kb = ofd->ofd_dt_conf.ddp_extent_tax >> 10;
207 data->ocd_grant_max_blks = ofd->ofd_dt_conf.ddp_max_extent_blks;
210 if (OCD_HAS_FLAG(data, GRANT)) {
211 /* Save connect_data we have so far because ofd_grant_connect()
212 * uses it to calculate grant. */
213 exp->exp_connect_data = *data;
214 ofd_grant_connect(env, exp, data, new_connection);
217 if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
218 struct lr_server_data *lsd = &ofd->ofd_lut.lut_lsd;
219 int index = lsd->lsd_osd_index;
221 if (index != data->ocd_index) {
222 LCONSOLE_ERROR_MSG(0x136, "Connection from %s to index"
223 " %u doesn't match actual OST index"
224 " %u in last_rcvd file, bad "
226 obd_export_nid2str(exp), index,
230 if (!(lsd->lsd_feature_compat & OBD_COMPAT_OST)) {
231 /* this will only happen on the first connect */
232 lsd->lsd_feature_compat |= OBD_COMPAT_OST;
233 /* sync is not needed here as lut_client_add will
234 * set exp_need_sync flag */
235 tgt_server_data_update(env, &ofd->ofd_lut, 0);
239 if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
240 __u32 cksum_types = data->ocd_cksum_types;
242 /* The client set in ocd_cksum_types the checksum types it
243 * supports. We have to mask off the algorithms that we don't
245 data->ocd_cksum_types &= cksum_types_supported_server();
247 if (unlikely(data->ocd_cksum_types == 0)) {
248 CERROR("%s: Connect with checksum support but no "
249 "ocd_cksum_types is set\n",
250 exp->exp_obd->obd_name);
254 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
255 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
256 cksum_types, data->ocd_cksum_types);
258 /* This client does not support OBD_CONNECT_CKSUM
259 * fall back to CRC32 */
260 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
261 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
262 exp->exp_obd->obd_name, obd_export_nid2str(exp));
265 if (data->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
266 data->ocd_maxbytes = ofd->ofd_dt_conf.ddp_maxbytes;
268 if (OCD_HAS_FLAG(data, PINGLESS)) {
269 if (ptlrpc_pinger_suppress_pings()) {
270 spin_lock(&exp->exp_obd->obd_dev_lock);
271 list_del_init(&exp->exp_obd_chain_timed);
272 spin_unlock(&exp->exp_obd->obd_dev_lock);
274 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
282 * Re-initialize export upon client reconnection.
284 * This function parses connection data from reconnect and resets
287 * \param[in] env execution environment
288 * \param[in] exp OBD export
289 * \param[in] obd OFD device
290 * \param[in] cluuid NID of client
291 * \param[in] data connection data from request
292 * \param[in] localdata client NID
294 * \retval 0 if successful
295 * \retval negative value on error
297 static int ofd_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
298 struct obd_device *obd, struct obd_uuid *cluuid,
299 struct obd_connect_data *data,
302 struct ofd_device *ofd;
307 if (exp == NULL || obd == NULL || cluuid == NULL)
310 rc = nodemap_add_member(*(lnet_nid_t *)client_nid, exp);
311 if (rc != 0 && rc != -EEXIST)
314 ofd = ofd_dev(obd->obd_lu_dev);
316 rc = ofd_parse_connect_data(env, exp, data, false);
318 ofd_export_stats_init(ofd, exp, client_nid);
320 nodemap_del_member(exp);
326 * Initialize new client connection.
328 * This function handles new connection to the OFD. The new export is
329 * created (in context of class_connect()) and persistent client data is
330 * initialized on storage.
332 * \param[in] env execution environment
333 * \param[out] _exp stores pointer to new export
334 * \param[in] obd OFD device
335 * \param[in] cluuid client UUID
336 * \param[in] data connection data from request
337 * \param[in] localdata client NID
339 * \retval 0 if successful
340 * \retval negative value on error
342 static int ofd_obd_connect(const struct lu_env *env, struct obd_export **_exp,
343 struct obd_device *obd, struct obd_uuid *cluuid,
344 struct obd_connect_data *data, void *localdata)
346 struct obd_export *exp;
347 struct ofd_device *ofd;
348 struct lustre_handle conn = { 0 };
352 if (_exp == NULL || obd == NULL || cluuid == NULL)
355 ofd = ofd_dev(obd->obd_lu_dev);
357 rc = class_connect(&conn, obd, cluuid);
361 exp = class_conn2export(&conn);
362 LASSERT(exp != NULL);
364 if (localdata != NULL) {
365 rc = nodemap_add_member(*(lnet_nid_t *)localdata, exp);
366 if (rc != 0 && rc != -EEXIST)
369 CDEBUG(D_HA, "%s: cannot find nodemap for client %s: "
370 "nid is null\n", obd->obd_name, cluuid->uuid);
373 rc = ofd_parse_connect_data(env, exp, data, true);
377 if (obd->obd_replayable) {
378 struct tg_export_data *ted = &exp->exp_target_data;
380 memcpy(ted->ted_lcd->lcd_uuid, cluuid,
381 sizeof(ted->ted_lcd->lcd_uuid));
382 rc = tgt_client_new(env, exp);
385 ofd_export_stats_init(ofd, exp, localdata);
388 CDEBUG(D_HA, "%s: get connection from MDS %d\n", obd->obd_name,
389 data ? data->ocd_group : -1);
393 class_disconnect(exp);
394 nodemap_del_member(exp);
403 * Disconnect a connected client.
405 * This function terminates the client connection. The client export is
406 * disconnected (cleaned up) and client data on persistent storage is removed.
408 * \param[in] exp OBD export
410 * \retval 0 if successful
411 * \retval negative value on error
413 int ofd_obd_disconnect(struct obd_export *exp)
415 struct ofd_device *ofd = ofd_exp(exp);
422 class_export_get(exp);
424 if (!(exp->exp_flags & OBD_OPT_FORCE))
425 ofd_grant_sanity_check(ofd_obd(ofd), __FUNCTION__);
427 rc = server_disconnect_export(exp);
429 ofd_grant_discard(exp);
431 /* Do not erase record for recoverable client. */
432 if (exp->exp_obd->obd_replayable &&
433 (!exp->exp_obd->obd_fail || exp->exp_failed)) {
434 rc = lu_env_init(&env, LCT_DT_THREAD);
438 tgt_client_del(&env, exp);
442 nodemap_del_member(exp);
443 class_export_put(exp);
448 * Implementation of obd_ops::o_init_export.
450 * This function is called from class_new_export() and initializes
451 * the OFD-specific data for new export.
453 * \param[in] exp OBD export
455 * \retval 0 if successful
456 * \retval negative value on error
458 static int ofd_init_export(struct obd_export *exp)
462 spin_lock_init(&exp->exp_filter_data.fed_lock);
463 INIT_LIST_HEAD(&exp->exp_filter_data.fed_mod_list);
464 atomic_set(&exp->exp_filter_data.fed_soft_sync_count, 0);
465 spin_lock(&exp->exp_lock);
466 exp->exp_connecting = 1;
467 spin_unlock(&exp->exp_lock);
469 /* self-export doesn't need client data and ldlm initialization */
470 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
471 &exp->exp_client_uuid)))
474 rc = tgt_client_alloc(exp);
476 ldlm_init_export(exp);
478 CERROR("%s: Can't initialize export: rc %d\n",
479 exp->exp_obd->obd_name, rc);
484 * Implementation of obd_ops::o_destroy_export.
486 * This function is called from class_export_destroy() to cleanup
487 * the OFD-specific data for export being destroyed.
489 * \param[in] exp OBD export
491 * \retval 0 if successful
492 * \retval negative value on error
494 static int ofd_destroy_export(struct obd_export *exp)
496 struct ofd_device *ofd = ofd_exp(exp);
498 if (exp->exp_filter_data.fed_pending)
499 CERROR("%s: cli %s/%p has %lu pending on destroyed export"
500 "\n", exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
501 exp, exp->exp_filter_data.fed_pending);
503 target_destroy_export(exp);
505 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
506 &exp->exp_client_uuid)))
509 ldlm_destroy_export(exp);
510 tgt_client_free(exp);
512 ofd_fmd_cleanup(exp);
515 * discard grants once we're sure no more
516 * interaction with the client is possible
518 ofd_grant_discard(exp);
519 ofd_fmd_cleanup(exp);
521 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
522 ofd->ofd_tot_granted_clients--;
524 if (!(exp->exp_flags & OBD_OPT_FORCE))
525 ofd_grant_sanity_check(exp->exp_obd, __FUNCTION__);
527 LASSERT(list_empty(&exp->exp_filter_data.fed_mod_list));
532 * Notify all devices in server stack about recovery completion.
534 * This function calls ldo_recovery_complete() for all lower devices in the
535 * server stack so they will be prepared for normal operations.
537 * \param[in] env execution environment
538 * \param[in] ofd OFD device
540 * \retval 0 if successful
541 * \retval negative value on error
543 int ofd_postrecov(const struct lu_env *env, struct ofd_device *ofd)
545 struct lu_device *ldev = &ofd->ofd_dt_dev.dd_lu_dev;
548 CDEBUG(D_HA, "%s: recovery is over\n", ofd_name(ofd));
550 if (!ofd->ofd_skip_lfsck) {
551 struct lfsck_start_param lsp;
553 lsp.lsp_start = NULL;
554 lsp.lsp_index_valid = 0;
555 rc = lfsck_start(env, ofd->ofd_osd, &lsp);
556 if (rc != 0 && rc != -EALREADY)
557 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
561 return ldev->ld_ops->ldo_recovery_complete(env, ldev);
565 * Implementation of obd_ops::o_postrecov.
567 * This function is called from target_finish_recovery() upon recovery
570 * \param[in] obd OBD device of OFD
572 * \retval 0 if successful
573 * \retval negative value on error
575 static int ofd_obd_postrecov(struct obd_device *obd)
578 struct lu_device *ldev = obd->obd_lu_dev;
583 rc = lu_env_init(&env, LCT_DT_THREAD);
586 ofd_info_init(&env, obd->obd_self_export);
588 rc = ofd_postrecov(&env, ofd_dev(ldev));
595 * Implementation of obd_ops::o_set_info_async.
597 * This function is not called from request handler, it is only used by
598 * class_notify_sptlrpc_conf() locally by direct obd_set_info_async() call.
599 * \see ofd_set_info_hdl() for request handler function.
601 * \param[in] env execution environment
602 * \param[in] exp OBD export of OFD device
603 * \param[in] keylen length of \a key
604 * \param[in] key key name
605 * \param[in] vallen length of \a val
606 * \param[in] val the \a key value
607 * \param[in] set not used in OFD
609 * \retval 0 if successful
610 * \retval negative value on error
612 static int ofd_set_info_async(const struct lu_env *env, struct obd_export *exp,
613 __u32 keylen, void *key, __u32 vallen, void *val,
614 struct ptlrpc_request_set *set)
620 if (exp->exp_obd == NULL) {
621 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
625 if (KEY_IS(KEY_SPTLRPC_CONF)) {
626 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
628 CERROR("%s: Unsupported key %s\n",
629 exp->exp_obd->obd_name, (char*)key);
636 * Implementation of obd_ops::o_get_info.
638 * This function is not called from request handler, it is only used by
639 * direct call from nrs_orr_range_fill_physical() in ptlrpc, see LU-3239.
641 * \see ofd_get_info_hdl() for request handler function.
643 * \param[in] env execution environment
644 * \param[in] exp OBD export of OFD device
645 * \param[in] keylen length of \a key
646 * \param[in] key key name
647 * \param[out] vallen length of key value
648 * \param[out] val the key value to return
650 * \retval 0 if successful
651 * \retval negative value on error
653 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
654 __u32 keylen, void *key, __u32 *vallen, void *val)
656 struct ofd_thread_info *info;
657 struct ofd_device *ofd;
658 struct ll_fiemap_info_key *fm_key = key;
659 struct fiemap *fiemap = val;
664 if (exp->exp_obd == NULL) {
665 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
671 if (KEY_IS(KEY_FIEMAP)) {
672 info = ofd_info_init(env, exp);
674 rc = ostid_to_fid(&info->fti_fid, &fm_key->lfik_oa.o_oi,
675 ofd->ofd_lut.lut_lsd.lsd_osd_index);
679 rc = ofd_fiemap_get(env, ofd, &info->fti_fid, fiemap);
681 CERROR("%s: not supported key %s\n", ofd_name(ofd), (char*)key);
689 * Get file system statistics of OST server.
691 * Helper function for ofd_statfs(), also used by grant code.
692 * Implements caching for statistics to avoid calling OSD device each time.
694 * \param[in] env execution environment
695 * \param[in] ofd OFD device
696 * \param[out] osfs statistic data to return
697 * \param[in] max_age maximum age for cached data
698 * \param[in] from_cache show that data was get from cache or not
700 * \retval 0 if successful
701 * \retval negative value on error
703 int ofd_statfs_internal(const struct lu_env *env, struct ofd_device *ofd,
704 struct obd_statfs *osfs, __u64 max_age, int *from_cache)
709 spin_lock(&ofd->ofd_osfs_lock);
710 if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
713 /* statfs data are too old, get up-to-date one.
714 * we must be cautious here since multiple threads might be
715 * willing to update statfs data concurrently and we must
716 * grant that cached statfs data are always consistent */
718 if (ofd->ofd_statfs_inflight == 0)
719 /* clear inflight counter if no users, although it would
720 * take a while to overflow this 64-bit counter ... */
721 ofd->ofd_osfs_inflight = 0;
722 /* notify ofd_grant_commit() that we want to track writes
723 * completed as of now */
724 ofd->ofd_statfs_inflight++;
725 /* record value of inflight counter before running statfs to
726 * compute the diff once statfs is completed */
727 unstable = ofd->ofd_osfs_inflight;
728 spin_unlock(&ofd->ofd_osfs_lock);
730 /* statfs can sleep ... hopefully not for too long since we can
731 * call it fairly often as space fills up */
732 rc = dt_statfs(env, ofd->ofd_osd, osfs);
736 spin_lock(&ofd->ofd_grant_lock);
737 spin_lock(&ofd->ofd_osfs_lock);
738 /* calculate how much space was written while we released the
740 unstable = ofd->ofd_osfs_inflight - unstable;
741 ofd->ofd_osfs_unstable = 0;
743 /* some writes committed while we were running statfs
744 * w/o the ofd_osfs_lock. Those ones got added to
745 * the cached statfs data that we are about to crunch.
746 * Take them into account in the new statfs data */
747 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
748 unstable >> ofd->ofd_blockbits);
749 /* However, we don't really know if those writes got
750 * accounted in the statfs call, so tell
751 * ofd_grant_space_left() there is some uncertainty
752 * on the accounting of those writes.
753 * The purpose is to prevent spurious error messages in
754 * ofd_grant_space_left() since those writes might be
755 * accounted twice. */
756 ofd->ofd_osfs_unstable += unstable;
758 /* similarly, there is some uncertainty on write requests
759 * between prepare & commit */
760 ofd->ofd_osfs_unstable += ofd->ofd_tot_pending;
761 spin_unlock(&ofd->ofd_grant_lock);
763 /* finally udpate cached statfs data */
764 ofd->ofd_osfs = *osfs;
765 ofd->ofd_osfs_age = cfs_time_current_64();
767 ofd->ofd_statfs_inflight--; /* stop tracking */
768 if (ofd->ofd_statfs_inflight == 0)
769 ofd->ofd_osfs_inflight = 0;
770 spin_unlock(&ofd->ofd_osfs_lock);
775 /* use cached statfs data */
776 *osfs = ofd->ofd_osfs;
777 spin_unlock(&ofd->ofd_osfs_lock);
788 * Implementation of obd_ops::o_statfs.
790 * This function returns information about a storage file system.
791 * It is called from several places by using the OBD API as well as
792 * by direct call, e.g. from request handler.
794 * \see ofd_statfs_hdl() for request handler function.
796 * Report also the state of the OST to the caller in osfs->os_state
797 * (OS_STATE_READONLY, OS_STATE_DEGRADED).
799 * \param[in] env execution environment
800 * \param[in] exp OBD export of OFD device
801 * \param[out] osfs statistic data to return
802 * \param[in] max_age maximum age for cached data
803 * \param[in] flags not used in OFD
805 * \retval 0 if successful
806 * \retval negative value on error
808 int ofd_statfs(const struct lu_env *env, struct obd_export *exp,
809 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
811 struct obd_device *obd = class_exp2obd(exp);
812 struct ofd_device *ofd = ofd_exp(exp);
817 rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
821 /* at least try to account for cached pages. its still racy and
822 * might be under-reporting if clients haven't announced their
823 * caches with brw recently */
825 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
826 " pending %llu free %llu avail %llu\n",
827 ofd->ofd_tot_dirty, ofd->ofd_tot_granted, ofd->ofd_tot_pending,
828 osfs->os_bfree << ofd->ofd_blockbits,
829 osfs->os_bavail << ofd->ofd_blockbits);
831 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
832 ((ofd->ofd_tot_dirty + ofd->ofd_tot_pending +
833 osfs->os_bsize - 1) >> ofd->ofd_blockbits));
835 /* The QoS code on the MDS does not care about space reserved for
836 * precreate, so take it out. */
837 if (exp_connect_flags(exp) & OBD_CONNECT_MDS) {
838 struct filter_export_data *fed;
840 fed = &obd->obd_self_export->exp_filter_data;
841 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
842 fed->fed_grant >> ofd->ofd_blockbits);
845 ofd_grant_sanity_check(obd, __FUNCTION__);
846 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
847 "%llu objects: %llu free; state %x\n",
848 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
849 osfs->os_files, osfs->os_ffree, osfs->os_state);
851 if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
852 ofd->ofd_lut.lut_lsd.lsd_osd_index))
855 /* OS_STATE_READONLY can be set by OSD already */
856 if (ofd->ofd_raid_degraded)
857 osfs->os_state |= OS_STATE_DEGRADED;
859 if (obd->obd_self_export != exp && !ofd_grant_param_supp(exp) &&
860 ofd->ofd_blockbits > COMPAT_BSIZE_SHIFT) {
861 /* clients which don't support OBD_CONNECT_GRANT_PARAM
862 * should not see a block size > page size, otherwise
863 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
864 * block size which is the biggest block size known to work
865 * with all client's page size. */
866 osfs->os_blocks <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
867 osfs->os_bfree <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
868 osfs->os_bavail <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
869 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
872 if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
873 ofd->ofd_lut.lut_lsd.lsd_osd_index))
874 osfs->os_bfree = osfs->os_bavail = 2;
882 * Implementation of obd_ops::o_setattr.
884 * This function is only used by ECHO client when it is run on top of OFD,
885 * \see ofd_setattr_hdl() for request handler function.
887 * \param[in] env execution environment
888 * \param[in] exp OBD export of OFD device
889 * \param[in] oa setattr parameters
891 * \retval 0 if successful
892 * \retval negative value on error
894 static int ofd_echo_setattr(const struct lu_env *env, struct obd_export *exp,
897 struct ofd_thread_info *info;
898 struct ofd_device *ofd = ofd_exp(exp);
899 struct ldlm_namespace *ns = ofd->ofd_namespace;
900 struct ldlm_resource *res;
901 struct ofd_object *fo;
902 struct lu_fid *fid = &oa->o_oi.oi_fid;
903 struct filter_fid *ff = NULL;
908 info = ofd_info_init(env, exp);
910 ost_fid_build_resid(fid, &info->fti_resid);
912 /* This would be very bad - accidentally truncating a file when
913 * changing the time or similar - bug 12203. */
914 if (oa->o_valid & OBD_MD_FLSIZE) {
915 static char mdsinum[48];
917 if (oa->o_valid & OBD_MD_FLFID)
918 snprintf(mdsinum, sizeof(mdsinum) - 1,
919 "of parent "DFID, oa->o_parent_seq,
920 oa->o_parent_oid, 0);
924 CERROR("%s: setattr from %s trying to truncate object "DFID
925 " %s\n", ofd_name(ofd), obd_export_nid2str(exp),
927 GOTO(out, rc = -EPERM);
930 fo = ofd_object_find_exists(env, ofd, fid);
932 CERROR("%s: can't find object "DFID"\n",
933 ofd_name(ofd), PFID(fid));
934 GOTO(out, rc = PTR_ERR(fo));
937 la_from_obdo(&info->fti_attr, oa, oa->o_valid);
938 info->fti_attr.la_valid &= ~LA_TYPE;
940 if (oa->o_valid & OBD_MD_FLFID) {
941 ff = &info->fti_mds_fid;
942 ofd_prepare_fidea(ff, oa);
945 /* setting objects attributes (including owner/group) */
946 rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
948 GOTO(out_unlock, rc);
950 ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, NULL, 1);
953 ofd_object_put(env, fo);
956 /* we do not call this before to avoid lu_object_find() in
957 * ->lvbo_update() holding another reference on the object.
958 * otherwise concurrent destroy can make the object unavailable
959 * for 2nd lu_object_find() waiting for the first reference
960 * to go... deadlock! */
961 res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
963 ldlm_res_lvbo_update(res, NULL, 0);
964 ldlm_resource_putref(res);
972 * Destroy OFD object by its FID.
974 * Supplemental function to destroy object by FID, it is used by request
975 * handler and by ofd_echo_destroy() below to find object by FID, lock it
976 * and call ofd_object_destroy() finally.
978 * \param[in] env execution environment
979 * \param[in] ofd OFD device
980 * \param[in] fid FID of object
981 * \param[in] orphan set if object being destroyed is an orphan
983 * \retval 0 if successful
984 * \retval negative value on error
986 int ofd_destroy_by_fid(const struct lu_env *env, struct ofd_device *ofd,
987 const struct lu_fid *fid, int orphan)
989 struct ofd_thread_info *info = ofd_info(env);
990 struct lustre_handle lockh;
991 union ldlm_policy_data policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
992 struct ofd_object *fo;
993 __u64 flags = LDLM_FL_AST_DISCARD_DATA;
998 fo = ofd_object_find_exists(env, ofd, fid);
1000 RETURN(PTR_ERR(fo));
1002 /* Tell the clients that the object is gone now and that they should
1003 * throw away any cached pages. */
1004 ost_fid_build_resid(fid, &info->fti_resid);
1005 rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
1006 LDLM_EXTENT, &policy, LCK_PW, &flags,
1007 ldlm_blocking_ast, ldlm_completion_ast,
1008 NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
1010 /* We only care about the side-effects, just drop the lock. */
1012 ldlm_lock_decref(&lockh, LCK_PW);
1014 LASSERT(fo != NULL);
1016 rc = ofd_object_destroy(env, fo, orphan);
1019 ofd_object_put(env, fo);
1024 * Implementation of obd_ops::o_destroy.
1026 * This function is only used by ECHO client when it is run on top of OFD,
1027 * \see ofd_destroy_hdl() for request handler function.
1029 * \param[in] env execution environment
1030 * \param[in] exp OBD export of OFD device
1031 * \param[in] oa obdo structure with FID
1033 * Note: this is OBD API method which is common API for server OBDs and
1034 * client OBDs. Thus some parameters used in client OBDs may not be used
1035 * on server OBDs and vice versa.
1037 * \retval 0 if successful
1038 * \retval negative value on error
1040 static int ofd_echo_destroy(const struct lu_env *env, struct obd_export *exp,
1043 struct ofd_device *ofd = ofd_exp(exp);
1044 struct lu_fid *fid = &oa->o_oi.oi_fid;
1049 ofd_info_init(env, exp);
1051 CDEBUG(D_HA, "%s: Destroy object "DFID"\n", ofd_name(ofd), PFID(fid));
1053 rc = ofd_destroy_by_fid(env, ofd, fid, 0);
1054 if (rc == -ENOENT) {
1055 CDEBUG(D_INODE, "%s: destroying non-existent object "DFID"\n",
1056 ofd_name(ofd), PFID(fid));
1058 } else if (rc != 0) {
1059 CERROR("%s: error destroying object "DFID": %d\n",
1060 ofd_name(ofd), PFID(fid), rc);
1069 * Implementation of obd_ops::o_create.
1071 * This function is only used by ECHO client when it is run on top of OFD
1072 * and just creates an object.
1073 * \see ofd_create_hdl() for request handler function.
1075 * \param[in] env execution environment
1076 * \param[in] exp OBD export of OFD device
1077 * \param[in] oa obdo structure with FID sequence to use
1079 * Note: this is OBD API method which is common API for server OBDs and
1080 * client OBDs. Thus some parameters used in client OBDs may not be used
1081 * on server OBDs and vice versa.
1083 * \retval 0 if successful
1084 * \retval negative value on error
1086 static int ofd_echo_create(const struct lu_env *env, struct obd_export *exp,
1089 struct ofd_device *ofd = ofd_exp(exp);
1090 struct ofd_thread_info *info;
1091 u64 seq = ostid_seq(&oa->o_oi);
1092 struct ofd_seq *oseq;
1093 int rc = 0, diff = 1;
1100 info = ofd_info_init(env, exp);
1102 LASSERT(seq == FID_SEQ_ECHO);
1103 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1105 CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1107 down_read(&ofd->ofd_lastid_rwsem);
1108 /* Currently, for safe, we do not distinguish which LAST_ID is broken,
1109 * we may do that in the future.
1110 * Return -ENOSPC until the LAST_ID rebuilt. */
1111 if (unlikely(ofd->ofd_lastid_rebuilding))
1112 GOTO(out_sem, rc = -ENOSPC);
1114 oseq = ofd_seq_load(env, ofd, seq);
1116 CERROR("%s: Can't find FID Sequence %#llx: rc = %ld\n",
1117 ofd_name(ofd), seq, PTR_ERR(oseq));
1118 GOTO(out_sem, rc = -EINVAL);
1121 mutex_lock(&oseq->os_create_lock);
1122 granted = ofd_grant_create(env, ofd_obd(ofd)->obd_self_export, &diff);
1126 CDEBUG(D_HA, "%s: failed to acquire grant space for "
1127 "precreate (%d): rc = %d\n", ofd_name(ofd), diff, rc);
1132 next_id = ofd_seq_last_oid(oseq) + 1;
1133 count = ofd_precreate_batch(ofd, diff);
1135 rc = ofd_precreate_objects(env, ofd, next_id, oseq, count, 0);
1137 CERROR("%s: unable to precreate: rc = %d\n",
1140 ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
1141 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1145 ofd_grant_commit(ofd_obd(ofd)->obd_self_export, granted, rc);
1147 mutex_unlock(&oseq->os_create_lock);
1148 ofd_seq_put(env, oseq);
1151 up_read(&ofd->ofd_lastid_rwsem);
1156 * Implementation of obd_ops::o_getattr.
1158 * This function is only used by ECHO client when it is run on top of OFD
1159 * and returns attributes of object.
1160 * \see ofd_getattr_hdl() for request handler function.
1162 * \param[in] env execution environment
1163 * \param[in] exp OBD export of OFD device
1164 * \param[in,out] oa contains FID of object to get attributes from and
1165 * is used to return attributes back
1167 * \retval 0 if successful
1168 * \retval negative value on error
1170 static int ofd_echo_getattr(const struct lu_env *env, struct obd_export *exp,
1173 struct ofd_device *ofd = ofd_exp(exp);
1174 struct ofd_thread_info *info;
1175 struct lu_fid *fid = &oa->o_oi.oi_fid;
1176 struct ofd_object *fo;
1181 info = ofd_info_init(env, exp);
1183 fo = ofd_object_find_exists(env, ofd, fid);
1185 GOTO(out, rc = PTR_ERR(fo));
1187 LASSERT(fo != NULL);
1188 rc = ofd_attr_get(env, fo, &info->fti_attr);
1189 oa->o_valid = OBD_MD_FLID;
1193 obdo_from_la(oa, &info->fti_attr,
1194 OFD_VALID_FLAGS | LA_UID | LA_GID);
1196 /* Store object version in reply */
1197 curr_version = dt_version_get(env, ofd_object_child(fo));
1198 if ((__s64)curr_version != -EOPNOTSUPP) {
1199 oa->o_valid |= OBD_MD_FLDATAVERSION;
1200 oa->o_data_version = curr_version;
1204 ofd_object_put(env, fo);
1210 * Get object version for OBD_IOC_GET_OBJ_VERSION ioctl.
1212 * This is supplemental function for ofd_iocontrol() to return object
1213 * version for lctl tool.
1215 * \param[in] env execution environment
1216 * \param[in] ofd OFD device
1217 * \param[out] karg ioctl data
1219 * \retval 0 if successful
1220 * \retval negative value on error
1222 static int ofd_ioc_get_obj_version(const struct lu_env *env,
1223 struct ofd_device *ofd, void *karg)
1225 struct obd_ioctl_data *data = karg;
1227 struct ofd_object *fo;
1228 dt_obj_version_t version;
1233 if (data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
1234 GOTO(out, rc = -EINVAL);
1236 if (data->ioc_inlbuf1 != NULL && data->ioc_inllen1 == sizeof(fid)) {
1237 fid = *(struct lu_fid *)data->ioc_inlbuf1;
1238 } else if (data->ioc_inlbuf3 != NULL &&
1239 data->ioc_inllen3 == sizeof(__u64) &&
1240 data->ioc_inlbuf4 != NULL &&
1241 data->ioc_inllen4 == sizeof(__u64)) {
1242 struct ost_id ostid;
1244 ostid_set_seq(&ostid, *(__u64 *)data->ioc_inlbuf4);
1245 ostid_set_id(&ostid, *(__u64 *)data->ioc_inlbuf3);
1246 rc = ostid_to_fid(&fid, &ostid,
1247 ofd->ofd_lut.lut_lsd.lsd_osd_index);
1251 GOTO(out, rc = -EINVAL);
1254 if (!fid_is_sane(&fid))
1255 GOTO(out, rc = -EINVAL);
1257 fo = ofd_object_find(env, ofd, &fid);
1259 GOTO(out, rc = PTR_ERR(fo));
1261 if (!ofd_object_exists(fo))
1262 GOTO(out_fo, rc = -ENOENT);
1264 if (lu_object_remote(&fo->ofo_obj.do_lu))
1265 GOTO(out_fo, rc = -EREMOTE);
1267 version = dt_version_get(env, ofd_object_child(fo));
1269 GOTO(out_fo, rc = -EIO);
1271 *(dt_obj_version_t *)data->ioc_inlbuf2 = version;
1275 ofd_object_put(env, fo);
1281 * Implementation of obd_ops::o_iocontrol.
1283 * This is OFD ioctl handling function which is primary interface for
1284 * Lustre tools like lfs, lctl and lfsck.
1286 * \param[in] cmd ioctl command
1287 * \param[in] exp OBD export of OFD
1288 * \param[in] len not used
1289 * \param[in,out] karg buffer with data
1290 * \param[in] uarg not used
1292 * \retval 0 if successful
1293 * \retval negative value on error
1295 static int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1296 void *karg, void __user *uarg)
1299 struct ofd_device *ofd = ofd_exp(exp);
1300 struct obd_device *obd = ofd_obd(ofd);
1305 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1306 rc = lu_env_init(&env, LCT_DT_THREAD);
1311 case OBD_IOC_ABORT_RECOVERY:
1312 CERROR("%s: aborting recovery\n", obd->obd_name);
1313 obd->obd_abort_recovery = 1;
1314 target_stop_recovery_thread(obd);
1317 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1318 rc = dt_sync(&env, ofd->ofd_osd);
1320 case OBD_IOC_SET_READONLY:
1321 rc = dt_sync(&env, ofd->ofd_osd);
1323 rc = dt_ro(&env, ofd->ofd_osd);
1325 case OBD_IOC_START_LFSCK: {
1326 struct obd_ioctl_data *data = karg;
1327 struct lfsck_start_param lsp;
1329 if (unlikely(data == NULL)) {
1334 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
1335 lsp.lsp_index_valid = 0;
1336 rc = lfsck_start(&env, ofd->ofd_osd, &lsp);
1339 case OBD_IOC_STOP_LFSCK: {
1340 struct obd_ioctl_data *data = karg;
1341 struct lfsck_stop stop;
1343 stop.ls_status = LS_STOPPED;
1344 /* Old lfsck utils may pass NULL @stop. */
1345 if (data->ioc_inlbuf1 == NULL)
1349 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
1351 rc = lfsck_stop(&env, ofd->ofd_osd, &stop);
1354 case OBD_IOC_GET_OBJ_VERSION:
1355 rc = ofd_ioc_get_obj_version(&env, ofd, karg);
1358 CERROR("%s: not supported cmd = %#x\n", obd->obd_name, cmd);
1367 * Implementation of obd_ops::o_precleanup.
1369 * This function stops device activity before shutting it down. It is called
1370 * from a cleanup function upon forceful device cleanup. For OFD there are no
1371 * special actions, it just invokes target_recovery_cleanup().
1373 * \param[in] obd OBD device of OFD
1377 static int ofd_precleanup(struct obd_device *obd)
1380 target_cleanup_recovery(obd);
1385 * Implementation of obd_ops::o_ping.
1387 * This is OFD-specific part of OBD_PING request handling.
1388 * It controls Filter Modification Data (FMD) expiration each time PING is
1391 * \see ofd_fmd_expire() and ofd_fmd.c for details
1393 * \param[in] env execution environment
1394 * \param[in] exp OBD export of client
1398 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1400 ofd_fmd_expire(exp);
1405 * Implementation of obd_ops::o_health_check.
1407 * This function checks the OFD device health - ability to respond on
1408 * incoming requests. There are two health_check methods:
1409 * - get statfs from the OSD. It checks just responsiveness of
1411 * - do write attempt on bottom device to check it is fully operational and
1412 * is not stuck. This is expensive method and requires special configuration
1413 * option --enable-health-write while building Lustre, it is turned off
1416 * \param[in] nul not used
1417 * \param[in] obd OBD device of OFD
1419 * \retval 0 if successful
1420 * \retval negative value in case of error
1422 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1424 struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
1425 struct ofd_thread_info *info;
1427 #ifdef USE_HEALTH_CHECK_WRITE
1432 /* obd_proc_read_health pass NULL env, we need real one */
1433 rc = lu_env_init(&env, LCT_DT_THREAD);
1437 info = ofd_info_init(&env, NULL);
1438 rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1442 if (info->fti_u.osfs.os_state & OS_STATE_READONLY)
1443 GOTO(out, rc = -EROFS);
1445 #ifdef USE_HEALTH_CHECK_WRITE
1446 OBD_ALLOC(info->fti_buf.lb_buf, PAGE_SIZE);
1447 if (info->fti_buf.lb_buf == NULL)
1448 GOTO(out, rc = -ENOMEM);
1450 info->fti_buf.lb_len = PAGE_SIZE;
1453 th = dt_trans_create(&env, ofd->ofd_osd);
1455 GOTO(out, rc = PTR_ERR(th));
1457 rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1458 &info->fti_buf, info->fti_off, th);
1460 th->th_sync = 1; /* sync IO is needed */
1461 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1463 rc = dt_record_write(&env, ofd->ofd_health_check_file,
1464 &info->fti_buf, &info->fti_off,
1467 dt_trans_stop(&env, ofd->ofd_osd, th);
1469 OBD_FREE(info->fti_buf.lb_buf, PAGE_SIZE);
1471 CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1478 struct obd_ops ofd_obd_ops = {
1479 .o_owner = THIS_MODULE,
1480 .o_connect = ofd_obd_connect,
1481 .o_reconnect = ofd_obd_reconnect,
1482 .o_disconnect = ofd_obd_disconnect,
1483 .o_create = ofd_echo_create,
1484 .o_statfs = ofd_statfs,
1485 .o_setattr = ofd_echo_setattr,
1486 .o_preprw = ofd_preprw,
1487 .o_commitrw = ofd_commitrw,
1488 .o_destroy = ofd_echo_destroy,
1489 .o_init_export = ofd_init_export,
1490 .o_destroy_export = ofd_destroy_export,
1491 .o_postrecov = ofd_obd_postrecov,
1492 .o_getattr = ofd_echo_getattr,
1493 .o_iocontrol = ofd_iocontrol,
1494 .o_precleanup = ofd_precleanup,
1496 .o_health_check = ofd_health_check,
1497 .o_set_info_async = ofd_set_info_async,
1498 .o_get_info = ofd_get_info,