Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / ofd / ofd_obd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ofd/ofd_obd.c
32  *
33  * This file contains OBD API methods for OBD Filter Device (OFD) which are
34  * used for export handling, configuration purposes and recovery.
35  * Several methods are used by ECHO client only since it still uses OBD API.
36  * Such methods have _echo_ prefix in name.
37  *
38  * Author: Andreas Dilger <andreas.dilger@intel.com>
39  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
40  * Author: Mikhail Pershin <mike.pershin@intel.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include "ofd_internal.h"
46 #include <obd_cksum.h>
47 #include <uapi/linux/lustre/lustre_ioctl.h>
48 #include <lustre_quota.h>
49 #include <lustre_lfsck.h>
50 #include <lustre_nodemap.h>
51 #include <obd_support.h>
52
53 /**
54  * Initialize OFD per-export statistics.
55  *
56  * This function sets up procfs entries for various OFD export counters. These
57  * counters are for per-client statistics tracked on the server.
58  *
59  * \param[in] ofd        OFD device
60  * \param[in] exp        OBD export
61  * \param[in] client_nid NID of client
62  *
63  * \retval              0 if successful
64  * \retval              negative value on error
65  */
66 static int ofd_export_stats_init(struct ofd_device *ofd,
67                                  struct obd_export *exp,
68                                  struct lnet_nid *client_nid)
69 {
70         struct obd_device       *obd = ofd_obd(ofd);
71         struct nid_stat         *stats;
72         int                      rc;
73
74         ENTRY;
75
76         if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
77                 /* Self-export gets no proc entry */
78                 RETURN(0);
79
80         rc = lprocfs_exp_setup(exp, client_nid);
81         if (rc != 0)
82                 /* Mask error for already created /proc entries */
83                 RETURN(rc == -EALREADY ? 0 : rc);
84
85         stats = exp->exp_nid_stats;
86         stats->nid_stats = lprocfs_stats_alloc(LPROC_OFD_STATS_LAST,
87                                                LPROCFS_STATS_FLAG_NOPERCPU);
88         if (!stats->nid_stats)
89                 RETURN(-ENOMEM);
90
91         ofd_stats_counter_init(stats->nid_stats, 0, LPROCFS_CNTR_HISTOGRAM);
92
93         rc = lprocfs_stats_register(stats->nid_proc, "stats", stats->nid_stats);
94         if (rc != 0) {
95                 lprocfs_stats_free(&stats->nid_stats);
96                 GOTO(out, rc);
97         }
98
99         rc = lprocfs_nid_ldlm_stats_init(stats);
100         if (rc != 0)
101                 GOTO(out, rc);
102
103 out:
104         RETURN(rc);
105 }
106
107 /**
108  * Match client and OST server connection feature flags.
109  *
110  * Compute the compatibility flags for a connection request based on
111  * features mutually supported by client and server.
112  *
113  * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
114  * must not be updated here, otherwise a partially initialized value may
115  * be exposed. After the connection request is successfully processed,
116  * the top-level tgt_connect() request handler atomically updates the export
117  * connect flags from the obd_connect_data::ocd_connect_flags field of the
118  * reply. \see tgt_connect().
119  *
120  * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
121  * full struct obd_connect_data. So care must be taken when accessing fields
122  * that are not present in struct obd_connect_data_v1. See LU-16.
123  *
124  * \param[in] env               execution environment
125  * \param[in] exp               the obd_export associated with this
126  *                              client/target pair
127  * \param[in] data              stores data for this connect request
128  * \param[in] new_connection    is this connection new or not
129  *
130  * \retval              0 if success
131  * \retval              -EPROTO client and server feature requirements are
132  *                      incompatible
133  * \retval              -EBADF  OST index in connect request doesn't match
134  *                      real OST index
135  */
136 static int ofd_parse_connect_data(const struct lu_env *env,
137                                   struct obd_export *exp,
138                                   struct obd_connect_data *data,
139                                   bool new_connection)
140 {
141         struct ofd_device *ofd = ofd_exp(exp);
142         struct filter_export_data *fed = &exp->exp_filter_data;
143
144         if (!data)
145                 RETURN(0);
146
147         CDEBUG(D_RPCTRACE,
148                "%s: cli %s/%p ocd_connect_flags: %#llx ocd_version: %x ocd_grant: %d ocd_index: %u ocd_group %u\n",
149                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
150                data->ocd_connect_flags, data->ocd_version,
151                data->ocd_grant, data->ocd_index, data->ocd_group);
152
153         if (fed->fed_group != 0 && fed->fed_group != data->ocd_group) {
154                 CWARN("!!! This export (nid %s) used object group %d earlier; now it's trying to use group %d!  This could be a bug in the MDS. Please report to https://jira.whamcloud.com/\n",
155                       obd_export_nid2str(exp), fed->fed_group,
156                       data->ocd_group);
157                 RETURN(-EPROTO);
158         }
159         fed->fed_group = data->ocd_group;
160
161         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
162
163         if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
164                 data->ocd_connect_flags2 &= OST_CONNECT_SUPPORTED2;
165
166         /* Kindly make sure the SKIP_ORPHAN flag is from MDS. */
167         if (data->ocd_connect_flags & OBD_CONNECT_MDS)
168                 CDEBUG(D_HA, "%s: Received MDS connection for group %u\n",
169                        exp->exp_obd->obd_name, data->ocd_group);
170         else if (data->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN)
171                 RETURN(-EPROTO);
172
173         /* Determine optimal brw size before calculating grant */
174         if (CFS_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
175                 data->ocd_brw_size = 65536;
176         } else if (OCD_HAS_FLAG(data, BRW_SIZE)) {
177                 if (data->ocd_brw_size > ofd->ofd_brw_size)
178                         data->ocd_brw_size = ofd->ofd_brw_size;
179                 if (data->ocd_brw_size == 0) {
180                         CERROR("%s: cli %s/%p ocd_connect_flags: %#llx ocd_version: %x ocd_grant: %d ocd_index: %u ocd_brw_size is unexpectedly zero, network data corruption? Refusing connection of this client\n",
181                                exp->exp_obd->obd_name,
182                                exp->exp_client_uuid.uuid,
183                                exp, data->ocd_connect_flags, data->ocd_version,
184                                data->ocd_grant, data->ocd_index);
185                         RETURN(-EPROTO);
186                 }
187         }
188
189         if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
190                 struct dt_device_param *ddp = &ofd->ofd_lut.lut_dt_conf;
191
192                 /* client is reporting its page size, for future use */
193                 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
194                 data->ocd_grant_blkbits  = ofd->ofd_lut.lut_tgd.tgd_blockbits;
195                 /*
196                  * ddp_inodespace may not be power-of-two value, eg. for ldiskfs
197                  * it's LDISKFS_DIR_REC_LEN(20) = 28.
198                  */
199                 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
200                 /* ocd_grant_tax_kb is in 1K byte blocks */
201                 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
202                 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
203         }
204
205         /*
206          * Save connect_data we have so far because tgt_grant_connect()
207          * uses it to calculate grant, and we want to save the client
208          * version before it is overwritten by LUSTRE_VERSION_CODE.
209          */
210         exp->exp_connect_data = *data;
211         if (OCD_HAS_FLAG(data, GRANT))
212                 tgt_grant_connect(env, exp, data, new_connection);
213
214         if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
215                 struct lr_server_data *lsd = &ofd->ofd_lut.lut_lsd;
216                 int                    index = lsd->lsd_osd_index;
217
218                 if (index != data->ocd_index) {
219                         LCONSOLE_ERROR_MSG(0x136,
220                                            "Connection from %s to index %u doesn't match actual OST index %u in last_rcvd file, bad configuration?\n",
221                                            obd_export_nid2str(exp), index,
222                                            data->ocd_index);
223                         RETURN(-EBADF);
224                 }
225                 if (!(lsd->lsd_feature_compat & OBD_COMPAT_OST)) {
226                         /* this will only happen on the first connect */
227                         lsd->lsd_feature_compat |= OBD_COMPAT_OST;
228                         /*
229                          * sync is not needed here as tgt_client_new will
230                          * set exp_need_sync flag
231                          */
232                         tgt_server_data_update(env, &ofd->ofd_lut, 0);
233                 }
234         }
235
236         if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
237                 __u32 cksum_types = data->ocd_cksum_types;
238
239                 tgt_mask_cksum_types(&ofd->ofd_lut, &data->ocd_cksum_types);
240
241                 if (unlikely(data->ocd_cksum_types == 0)) {
242                         CERROR("%s: Connect with checksum support but no ocd_cksum_types is set\n",
243                                exp->exp_obd->obd_name);
244                         RETURN(-EPROTO);
245                 }
246
247                 CDEBUG(D_RPCTRACE,
248                        "%s: cli %s supports cksum type %x, return %x\n",
249                        exp->exp_obd->obd_name, obd_export_nid2str(exp),
250                        cksum_types, data->ocd_cksum_types);
251         } else {
252                 /*
253                  * This client does not support OBD_CONNECT_CKSUM.
254                  * Report failure to negotiate checksum at connect
255                  */
256                 CDEBUG(D_RPCTRACE,
257                        "%s: cli %s does not support OBD_CONNECT_CKSUM\n",
258                        exp->exp_obd->obd_name, obd_export_nid2str(exp));
259         }
260
261         if (data->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
262                 data->ocd_maxbytes = ofd->ofd_lut.lut_dt_conf.ddp_maxbytes;
263
264         data->ocd_version = LUSTRE_VERSION_CODE;
265
266         if (OCD_HAS_FLAG(data, PINGLESS)) {
267                 if (ptlrpc_pinger_suppress_pings()) {
268                         spin_lock(&exp->exp_obd->obd_dev_lock);
269                         list_del_init(&exp->exp_obd_chain_timed);
270                         spin_unlock(&exp->exp_obd->obd_dev_lock);
271                 } else {
272                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
273                 }
274         }
275
276         if (!ofd->ofd_lut.lut_dt_conf.ddp_has_lseek_data_hole)
277                 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
278
279         RETURN(0);
280 }
281
282 /**
283  * Re-initialize export upon client reconnection.
284  *
285  * This function parses connection data from reconnect and resets
286  * export statistics.
287  *
288  * \param[in] env       execution environment
289  * \param[in] exp       OBD export
290  * \param[in] obd       OFD device
291  * \param[in] cluuid    NID of client
292  * \param[in] data      connection data from request
293  * \param[in] localdata client NID
294  *
295  * \retval              0 if successful
296  * \retval              negative value on error
297  */
298 static int ofd_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
299                              struct obd_device *obd, struct obd_uuid *cluuid,
300                              struct obd_connect_data *data,
301                              void *localdata)
302 {
303         struct ofd_device *ofd;
304         struct lnet_nid *client_nid = localdata;
305         int rc;
306
307         ENTRY;
308
309         if (!exp || !obd || !cluuid)
310                 RETURN(-EINVAL);
311
312         rc = nodemap_add_member(client_nid, exp);
313         if (rc != 0 && rc != -EEXIST)
314                 RETURN(rc);
315
316         ofd = ofd_dev(obd->obd_lu_dev);
317
318         rc = ofd_parse_connect_data(env, exp, data, false);
319         if (rc == 0)
320                 ofd_export_stats_init(ofd, exp, client_nid);
321         else
322                 nodemap_del_member(exp);
323
324         RETURN(rc);
325 }
326
327 /**
328  * Initialize new client connection.
329  *
330  * This function handles new connection to the OFD. The new export is
331  * created (in context of class_connect()) and persistent client data is
332  * initialized on storage.
333  *
334  * \param[in] env       execution environment
335  * \param[out] _exp     stores pointer to new export
336  * \param[in] obd       OFD device
337  * \param[in] cluuid    client UUID
338  * \param[in] data      connection data from request
339  * \param[in] localdata client NID
340  *
341  * \retval              0 if successful
342  * \retval              negative value on error
343  */
344 static int ofd_obd_connect(const struct lu_env *env, struct obd_export **_exp,
345                            struct obd_device *obd, struct obd_uuid *cluuid,
346                            struct obd_connect_data *data, void *localdata)
347 {
348         struct obd_export *exp;
349         struct ofd_device *ofd;
350         struct lustre_handle conn = { 0 };
351         struct lnet_nid *client_nid = localdata;
352         int rc;
353
354         ENTRY;
355
356         if (!_exp || !obd || !cluuid)
357                 RETURN(-EINVAL);
358
359         ofd = ofd_dev(obd->obd_lu_dev);
360
361         rc = class_connect(&conn, obd, cluuid);
362         if (rc)
363                 RETURN(rc);
364
365         exp = class_conn2export(&conn);
366         LASSERT(exp != NULL);
367
368         if (client_nid) {
369                 rc = nodemap_add_member(client_nid, exp);
370                 if (rc != 0 && rc != -EEXIST)
371                         GOTO(out, rc);
372         } else {
373                 CDEBUG(D_HA,
374                        "%s: cannot find nodemap for client %s: nid is null\n",
375                        obd->obd_name, cluuid->uuid);
376         }
377
378         rc = ofd_parse_connect_data(env, exp, data, true);
379         if (rc)
380                 GOTO(out, rc);
381
382         if (obd->obd_replayable) {
383                 struct tg_export_data *ted = &exp->exp_target_data;
384
385                 memcpy(ted->ted_lcd->lcd_uuid, cluuid,
386                        sizeof(ted->ted_lcd->lcd_uuid));
387                 rc = tgt_client_new(env, exp);
388                 if (rc != 0)
389                         GOTO(out, rc);
390                 ofd_export_stats_init(ofd, exp, client_nid);
391         }
392
393         CDEBUG(D_HA, "%s: get connection from MDS %d\n", obd->obd_name,
394                data ? data->ocd_group : -1);
395
396 out:
397         if (rc != 0) {
398                 class_disconnect(exp);
399                 nodemap_del_member(exp);
400                 *_exp = NULL;
401         } else {
402                 *_exp = exp;
403         }
404         RETURN(rc);
405 }
406
407 /**
408  * Disconnect a connected client.
409  *
410  * This function terminates the client connection. The client export is
411  * disconnected (cleaned up) and client data on persistent storage is removed.
412  *
413  * \param[in] exp       OBD export
414  *
415  * \retval              0 if successful
416  * \retval              negative value on error
417  */
418 int ofd_obd_disconnect(struct obd_export *exp)
419 {
420         struct ofd_device *ofd = ofd_exp(exp);
421         struct lu_env env;
422         int rc;
423
424         ENTRY;
425
426         LASSERT(exp);
427         class_export_get(exp);
428
429         rc = server_disconnect_export(exp);
430
431         tgt_grant_discard(exp);
432
433         if (!(exp->exp_flags & OBD_OPT_FORCE))
434                 tgt_grant_sanity_check(ofd_obd(ofd), __func__);
435
436         /* Do not erase record for recoverable client. */
437         if (exp->exp_obd->obd_replayable &&
438             (!exp->exp_obd->obd_fail || exp->exp_failed)) {
439                 rc = lu_env_init(&env, LCT_DT_THREAD);
440                 if (rc)
441                         GOTO(out, rc);
442
443                 tgt_client_del(&env, exp);
444                 lu_env_fini(&env);
445         }
446 out:
447         nodemap_del_member(exp);
448         class_export_put(exp);
449         RETURN(rc);
450 }
451
452 /**
453  * Implementation of obd_ops::o_init_export.
454  *
455  * This function is called from class_new_export() and initializes
456  * the OFD-specific data for new export.
457  *
458  * \param[in] exp       OBD export
459  *
460  * \retval              0 if successful
461  * \retval              negative value on error
462  */
463 static int ofd_init_export(struct obd_export *exp)
464 {
465         int rc;
466
467         atomic_set(&exp->exp_filter_data.fed_soft_sync_count, 0);
468         spin_lock(&exp->exp_lock);
469         exp->exp_connecting = 1;
470         spin_unlock(&exp->exp_lock);
471
472         /* self-export doesn't need client data and ldlm initialization */
473         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
474                                      &exp->exp_client_uuid)))
475                 return 0;
476
477         rc = tgt_client_alloc(exp);
478         if (rc == 0)
479                 ldlm_init_export(exp);
480         if (rc)
481                 CERROR("%s: Can't initialize export: rc %d\n",
482                        exp->exp_obd->obd_name, rc);
483         return rc;
484 }
485
486 /**
487  * Implementation of obd_ops::o_destroy_export.
488  *
489  * This function is called from class_export_destroy() to cleanup
490  * the OFD-specific data for export being destroyed.
491  *
492  * \param[in] exp       OBD export
493  *
494  * \retval              0 if successful
495  * \retval              negative value on error
496  */
497 static int ofd_destroy_export(struct obd_export *exp)
498 {
499         struct ofd_device *ofd = ofd_exp(exp);
500
501         if (exp->exp_target_data.ted_pending)
502                 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
503                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
504                        exp, exp->exp_target_data.ted_pending);
505
506         target_destroy_export(exp);
507
508         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
509                                      &exp->exp_client_uuid)))
510                 return 0;
511
512         ldlm_destroy_export(exp);
513         tgt_client_free(exp);
514
515         /*
516          * discard grants once we're sure no more
517          * interaction with the client is possible
518          */
519         tgt_grant_discard(exp);
520
521         if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
522                 ofd->ofd_lut.lut_tgd.tgd_tot_granted_clients--;
523
524         if (!(exp->exp_flags & OBD_OPT_FORCE))
525                 tgt_grant_sanity_check(exp->exp_obd, __func__);
526
527         return 0;
528 }
529
530 /**
531  * Notify all devices in server stack about recovery completion.
532  *
533  * This function calls ldo_recovery_complete() for all lower devices in the
534  * server stack so they will be prepared for normal operations.
535  *
536  * \param[in] env       execution environment
537  * \param[in] ofd       OFD device
538  *
539  * \retval              0 if successful
540  * \retval              negative value on error
541  */
542 int ofd_postrecov(const struct lu_env *env, struct ofd_device *ofd)
543 {
544         struct lu_device *ldev = &ofd->ofd_dt_dev.dd_lu_dev;
545         int rc;
546
547         CDEBUG(D_HA, "%s: recovery is over\n", ofd_name(ofd));
548
549         if (!ofd->ofd_skip_lfsck && !ofd->ofd_osd->dd_rdonly) {
550                 struct lfsck_start_param lsp;
551
552                 lsp.lsp_start = NULL;
553                 lsp.lsp_index_valid = 0;
554                 rc = lfsck_start(env, ofd->ofd_osd, &lsp);
555                 if (rc != 0 && rc != -EALREADY)
556                         CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
557                               ofd_name(ofd), rc);
558         }
559
560         return ldev->ld_ops->ldo_recovery_complete(env, ldev);
561 }
562
563 /**
564  * Implementation of obd_ops::o_postrecov.
565  *
566  * This function is called from target_finish_recovery() upon recovery
567  * completion.
568  *
569  * \param[in] obd       OBD device of OFD
570  *
571  * \retval              0 if successful
572  * \retval              negative value on error
573  */
574 static int ofd_obd_postrecov(struct obd_device *obd)
575 {
576         struct lu_env env;
577         struct lu_device *ldev = obd->obd_lu_dev;
578         int rc;
579
580         ENTRY;
581
582         rc = lu_env_init(&env, LCT_DT_THREAD);
583         if (rc)
584                 RETURN(rc);
585         ofd_info_init(&env, obd->obd_self_export);
586
587         rc = ofd_postrecov(&env, ofd_dev(ldev));
588
589         lu_env_fini(&env);
590         RETURN(rc);
591 }
592
593 /**
594  * Implementation of obd_ops::o_set_info_async.
595  *
596  * This function is not called from request handler, it is only used by
597  * class_notify_sptlrpc_conf() locally by direct obd_set_info_async() call.
598  * \see  ofd_set_info_hdl() for request handler function.
599  *
600  * \param[in] env       execution environment
601  * \param[in] exp       OBD export of OFD device
602  * \param[in] keylen    length of \a key
603  * \param[in] key       key name
604  * \param[in] vallen    length of \a val
605  * \param[in] val       the \a key value
606  * \param[in] set       not used in OFD
607  *
608  * \retval              0 if successful
609  * \retval              negative value on error
610  */
611 static int ofd_set_info_async(const struct lu_env *env, struct obd_export *exp,
612                               __u32 keylen, void *key, __u32 vallen, void *val,
613                               struct ptlrpc_request_set *set)
614 {
615         int rc = 0;
616
617         ENTRY;
618
619         if (!exp->exp_obd) {
620                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
621                 RETURN(-EINVAL);
622         }
623
624         if (KEY_IS(KEY_SPTLRPC_CONF)) {
625                 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
626         } else {
627                 CERROR("%s: Unsupported key %s\n",
628                        exp->exp_obd->obd_name, (char *)key);
629                 rc = -EOPNOTSUPP;
630         }
631         RETURN(rc);
632 }
633
634 /**
635  * Implementation of obd_ops::o_get_info.
636  *
637  * This function is not called from request handler, it is only used by
638  * direct call from nrs_orr_range_fill_physical() in ptlrpc, see LU-3239.
639  *
640  * \see  ofd_get_info_hdl() for request handler function.
641  *
642  * \param[in]  env      execution environment
643  * \param[in]  exp      OBD export of OFD device
644  * \param[in]  keylen   length of \a key
645  * \param[in]  key      key name
646  * \param[out] vallen   length of key value
647  * \param[out] val      the key value to return
648  *
649  * \retval              0 if successful
650  * \retval              negative value on error
651  */
652 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
653                         __u32 keylen, void *key, __u32 *vallen, void *val)
654 {
655         struct ofd_thread_info *info;
656         struct ofd_device *ofd;
657         struct ll_fiemap_info_key *fm_key = key;
658         struct fiemap *fiemap = val;
659         int rc = 0;
660
661         ENTRY;
662
663         if (!exp->exp_obd) {
664                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
665                 RETURN(-EINVAL);
666         }
667
668         ofd = ofd_exp(exp);
669
670         if (KEY_IS(KEY_FIEMAP)) {
671                 info = ofd_info_init(env, exp);
672
673                 rc = ostid_to_fid(&info->fti_fid, &fm_key->lfik_oa.o_oi,
674                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
675                 if (rc != 0)
676                         RETURN(rc);
677
678                 rc = ofd_fiemap_get(env, ofd, &info->fti_fid, fiemap);
679         } else {
680                 CERROR("%s: not supported key %s\n",
681                        ofd_name(ofd), (char *)key);
682                 rc = -EOPNOTSUPP;
683         }
684
685         RETURN(rc);
686 }
687
688 /**
689  * Implementation of obd_ops::o_statfs.
690  *
691  * This function returns information about a storage file system.
692  * It is called from several places by using the OBD API as well as
693  * by direct call, e.g. from request handler.
694  *
695  * \see  ofd_statfs_hdl() for request handler function.
696  *
697  * Report also the state of the OST to the caller in osfs->os_state
698  * (OS_STATFS_READONLY, OS_STATFS_DEGRADED).
699  *
700  * \param[in]  env      execution environment
701  * \param[in]  exp      OBD export of OFD device
702  * \param[out] osfs     statistic data to return
703  * \param[in]  max_age  maximum age for cached data
704  * \param[in]  flags    not used in OFD
705  *
706  * \retval              0 if successful
707  * \retval              negative value on error
708  */
709 int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
710                struct obd_statfs *osfs, time64_t max_age, __u32 flags)
711 {
712         struct obd_device *obd = class_exp2obd(exp);
713         struct ofd_device *ofd = ofd_exp(exp);
714         struct tg_grants_data *tgd = &ofd->ofd_lut.lut_tgd;
715         int current_blockbits;
716         int rc;
717
718         ENTRY;
719
720         rc = tgt_statfs_internal(env, &ofd->ofd_lut, osfs, max_age, NULL);
721         if (unlikely(rc))
722                 GOTO(out, rc);
723
724         /* tgd_blockbit is recordsize bits set during mkfs.
725          * This once set does not change. However, 'zfs set'
726          * can be used to change the OST blocksize. Instead
727          * of using cached value of 'tgd_blockbit' always
728          * calculate the blocksize bits which may have
729          * changed.
730          */
731         current_blockbits = fls64(osfs->os_bsize) - 1;
732
733         /*
734          * at least try to account for cached pages.  its still racy and
735          * might be under-reporting if clients haven't announced their
736          * caches with brw recently
737          */
738         CDEBUG(D_SUPER | D_CACHE,
739                "blocks cached %llu granted %llu pending %llu free %llu avail %llu\n",
740                tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
741                tgd->tgd_tot_pending,
742                osfs->os_bfree << current_blockbits,
743                osfs->os_bavail << current_blockbits);
744
745         osfs->os_bavail -= min_t(u64, osfs->os_bavail,
746                                  ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
747                                    osfs->os_bsize - 1) >> current_blockbits));
748
749         /*
750          * The QoS code on the MDS does not care about space reserved for
751          * precreate, so take it out.
752          */
753         if (exp_connect_flags(exp) & OBD_CONNECT_MDS) {
754                 struct tg_export_data *ted;
755
756                 ted = &obd->obd_self_export->exp_target_data;
757                 osfs->os_granted = min_t(u64, osfs->os_bavail,
758                                           ted->ted_grant >> current_blockbits);
759                 osfs->os_bavail -= osfs->os_granted;
760         }
761
762         tgt_grant_sanity_check(obd, __func__);
763         CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
764                "%llu objects: %llu free; state %x\n",
765                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
766                osfs->os_files, osfs->os_ffree, osfs->os_state);
767
768         if (CFS_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
769                                  ofd->ofd_lut.lut_lsd.lsd_osd_index)) {
770                 /* Reduce free inode count to zero, but keep "used" intact */
771                 osfs->os_files -= osfs->os_ffree;
772                 osfs->os_ffree -= osfs->os_ffree;
773         }
774
775         /* OS_STATFS_READONLY can be set by OSD already, only add flags */
776         if (ofd->ofd_raid_degraded)
777                 osfs->os_state |= OS_STATFS_DEGRADED;
778
779         if (ofd->ofd_lut.lut_no_create)
780                 osfs->os_state |= OS_STATFS_NOCREATE;
781
782         if (obd->obd_self_export != exp && !exp_grant_param_supp(exp) &&
783             current_blockbits > COMPAT_BSIZE_SHIFT) {
784                 /*
785                  * clients which don't support OBD_CONNECT_GRANT_PARAM
786                  * should not see a block size > page size, otherwise
787                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
788                  * block size which is the biggest block size known to work
789                  * with all client's page size.
790                  */
791                 osfs->os_blocks <<= current_blockbits - COMPAT_BSIZE_SHIFT;
792                 osfs->os_bfree  <<= current_blockbits - COMPAT_BSIZE_SHIFT;
793                 osfs->os_bavail <<= current_blockbits - COMPAT_BSIZE_SHIFT;
794                 osfs->os_granted <<= current_blockbits - COMPAT_BSIZE_SHIFT;
795                 osfs->os_bsize    = 1 << COMPAT_BSIZE_SHIFT;
796         }
797
798         if (CFS_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
799                                  ofd->ofd_lut.lut_lsd.lsd_osd_index)) {
800                 /* Reduce free blocks count near zero, but keep "used" intact */
801                 osfs->os_bavail -= osfs->os_bavail - 2;
802                 osfs->os_blocks -= osfs->os_bfree - 2;
803                 osfs->os_bfree -= osfs->os_bfree - 2;
804         }
805
806         EXIT;
807 out:
808         return rc;
809 }
810
811 /**
812  * Implementation of obd_ops::o_setattr.
813  *
814  * This function is only used by ECHO client when it is run on top of OFD,
815  * \see  ofd_setattr_hdl() for request handler function.
816
817  * \param[in] env       execution environment
818  * \param[in] exp       OBD export of OFD device
819  * \param[in] oa        setattr parameters
820  *
821  * \retval              0 if successful
822  * \retval              negative value on error
823  */
824 static int ofd_echo_setattr(const struct lu_env *env, struct obd_export *exp,
825                             struct obdo *oa)
826 {
827         struct ofd_thread_info *info;
828         struct ofd_device *ofd = ofd_exp(exp);
829         struct ldlm_namespace *ns = ofd->ofd_namespace;
830         struct ldlm_resource *res;
831         struct ofd_object *fo;
832         struct lu_fid *fid = &oa->o_oi.oi_fid;
833         ktime_t kstart = ktime_get();
834         int rc = 0;
835
836         ENTRY;
837
838         info = ofd_info_init(env, exp);
839
840         ost_fid_build_resid(fid, &info->fti_resid);
841
842         /*
843          * This would be very bad - accidentally truncating a file when
844          * changing the time or similar - bug 12203.
845          */
846         if (oa->o_valid & OBD_MD_FLSIZE) {
847                 static char mdsinum[48];
848
849                 if (oa->o_valid & OBD_MD_FLFID)
850                         snprintf(mdsinum, sizeof(mdsinum) - 1,
851                                  "of parent "DFID, oa->o_parent_seq,
852                                  oa->o_parent_oid, 0);
853                 else
854                         mdsinum[0] = '\0';
855
856                 CERROR("%s: setattr from %s trying to truncate object "DFID
857                        " %s\n", ofd_name(ofd), obd_export_nid2str(exp),
858                        PFID(fid), mdsinum);
859                 GOTO(out, rc = -EPERM);
860         }
861
862         fo = ofd_object_find_exists(env, ofd, fid);
863         if (IS_ERR(fo)) {
864                 CERROR("%s: can't find object "DFID"\n",
865                        ofd_name(ofd), PFID(fid));
866                 GOTO(out, rc = PTR_ERR(fo));
867         }
868
869         la_from_obdo(&info->fti_attr, oa, oa->o_valid);
870         info->fti_attr.la_valid &= ~LA_TYPE;
871
872         /* setting objects attributes (including owner/group) */
873         rc = ofd_attr_set(env, fo, &info->fti_attr, oa);
874         if (rc)
875                 GOTO(out_unlock, rc);
876
877         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, NULL,
878                          ktime_us_delta(ktime_get(), kstart));
879         EXIT;
880 out_unlock:
881         ofd_object_put(env, fo);
882 out:
883         if (rc == 0) {
884                 /*
885                  * we do not call this before to avoid lu_object_find() in
886                  *  ->lvbo_update() holding another reference on the object.
887                  * otherwise concurrent destroy can make the object unavailable
888                  * for 2nd lu_object_find() waiting for the first reference
889                  * to go... deadlock!
890                  */
891                 res = ldlm_resource_get(ns, &info->fti_resid, LDLM_EXTENT, 0);
892                 if (!IS_ERR(res)) {
893                         ldlm_res_lvbo_update(res, NULL, 0);
894                         ldlm_resource_putref(res);
895                 }
896         }
897
898         return rc;
899 }
900
901 /**
902  * Destroy OFD object by its FID.
903  *
904  * Supplemental function to destroy object by FID, it is used by request
905  * handler and by ofd_echo_destroy() below to find object by FID, lock it
906  * and call ofd_destroy() finally.
907  *
908  * \param[in] env       execution environment
909  * \param[in] ofd       OFD device
910  * \param[in] fid       FID of object
911  * \param[in] orphan    set if object being destroyed is an orphan
912  *
913  * \retval              0 if successful
914  * \retval              negative value on error
915  */
916 int ofd_destroy_by_fid(const struct lu_env *env, struct ofd_device *ofd,
917                        const struct lu_fid *fid, int orphan)
918 {
919         struct ofd_thread_info *info = ofd_info(env);
920         struct lustre_handle lockh;
921         union ldlm_policy_data policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
922         struct ofd_object *fo;
923         __u64 flags = LDLM_FL_AST_DISCARD_DATA;
924         __u64 rc = 0;
925
926         ENTRY;
927
928         fo = ofd_object_find_exists(env, ofd, fid);
929         if (IS_ERR(fo))
930                 RETURN(PTR_ERR(fo));
931
932         /*
933          * Tell the clients that the object is gone now and that they should
934          * throw away any cached pages.
935          */
936         ost_fid_build_resid(fid, &info->fti_resid);
937         rc = ldlm_cli_enqueue_local(env, ofd->ofd_namespace, &info->fti_resid,
938                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
939                                     ldlm_blocking_ast, ldlm_completion_ast,
940                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
941
942         /* We only care about the side-effects, just drop the lock. */
943         if (rc == ELDLM_OK)
944                 ldlm_lock_decref(&lockh, LCK_PW);
945
946         LASSERT(fo != NULL);
947
948         rc = ofd_destroy(env, fo, orphan);
949         EXIT;
950
951         ofd_object_put(env, fo);
952         RETURN(rc);
953 }
954
955 /**
956  * Implementation of obd_ops::o_destroy.
957  *
958  * This function is only used by ECHO client when it is run on top of OFD,
959  * \see  ofd_destroy_hdl() for request handler function.
960
961  * \param[in] env       execution environment
962  * \param[in] exp       OBD export of OFD device
963  * \param[in] oa        obdo structure with FID
964  *
965  * Note: this is OBD API method which is common API for server OBDs and
966  * client OBDs. Thus some parameters used in client OBDs may not be used
967  * on server OBDs and vice versa.
968  *
969  * \retval              0 if successful
970  * \retval              negative value on error
971  */
972 static int ofd_echo_destroy(const struct lu_env *env, struct obd_export *exp,
973                             struct obdo *oa)
974 {
975         struct ofd_device *ofd = ofd_exp(exp);
976         struct lu_fid *fid = &oa->o_oi.oi_fid;
977         int rc = 0;
978
979         ENTRY;
980
981         ofd_info_init(env, exp);
982
983         rc = ofd_validate_seq(exp, ostid_seq(&oa->o_oi));
984         if (rc != 0)
985                 RETURN(rc);
986
987         CDEBUG(D_HA, "%s: Destroy object "DFID"\n", ofd_name(ofd), PFID(fid));
988
989         rc = ofd_destroy_by_fid(env, ofd, fid, 0);
990         if (rc == -ENOENT) {
991                 CDEBUG(D_INODE, "%s: destroying non-existent object "DFID"\n",
992                        ofd_name(ofd), PFID(fid));
993                 GOTO(out, rc);
994         } else if (rc != 0) {
995                 CERROR("%s: error destroying object "DFID": %d\n",
996                        ofd_name(ofd), PFID(fid), rc);
997                 GOTO(out, rc);
998         }
999         EXIT;
1000 out:
1001         return rc;
1002 }
1003
1004 /**
1005  * Implementation of obd_ops::o_create.
1006  *
1007  * This function is only used by ECHO client when it is run on top of OFD
1008  * and just creates an object.
1009  * \see  ofd_create_hdl() for request handler function.
1010  *
1011  * \param[in]  env      execution environment
1012  * \param[in]  exp      OBD export of OFD device
1013  * \param[in]  oa       obdo structure with FID sequence to use
1014  *
1015  * Note: this is OBD API method which is common API for server OBDs and
1016  * client OBDs. Thus some parameters used in client OBDs may not be used
1017  * on server OBDs and vice versa.
1018  *
1019  * \retval              0 if successful
1020  * \retval              negative value on error
1021  */
1022 static int ofd_echo_create(const struct lu_env *env, struct obd_export *exp,
1023                            struct obdo *oa)
1024 {
1025         struct ofd_device *ofd = ofd_exp(exp);
1026         u64 seq = ostid_seq(&oa->o_oi);
1027         struct ofd_seq *oseq;
1028         long granted;
1029         u64 next_id;
1030         s64 diff = 1;
1031         int rc = 0;
1032         int count;
1033
1034         ENTRY;
1035
1036         if (ofd->ofd_lut.lut_no_create)
1037                 return -EPERM;
1038
1039         ofd_info_init(env, exp);
1040
1041         LASSERT(seq == FID_SEQ_ECHO);
1042         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1043
1044         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1045
1046         down_read(&ofd->ofd_lastid_rwsem);
1047         /*
1048          * Currently, for safe, we do not distinguish which LAST_ID is broken,
1049          * we may do that in the future.
1050          * Return -ENOSPC until the LAST_ID rebuilt.
1051          */
1052         if (unlikely(ofd->ofd_lastid_rebuilding))
1053                 GOTO(out_sem, rc = -ENOSPC);
1054
1055         rc = ofd_validate_seq(exp, seq);
1056         if (rc != 0)
1057                 RETURN(rc);
1058
1059         oseq = ofd_seq_load(env, ofd, seq);
1060         if (IS_ERR(oseq)) {
1061                 CERROR("%s: Can't find FID Sequence %#llx: rc = %ld\n",
1062                        ofd_name(ofd), seq, PTR_ERR(oseq));
1063                 GOTO(out_sem, rc = -EINVAL);
1064         }
1065
1066         mutex_lock(&oseq->os_create_lock);
1067         granted = tgt_grant_create(env, ofd_obd(ofd)->obd_self_export, &diff);
1068         if (granted < 0) {
1069                 rc = granted;
1070                 granted = 0;
1071                 CDEBUG(D_HA,
1072                        "%s: failed to acquire grant space for precreate (%lld): rc = %d\n",
1073                        ofd_name(ofd), diff, rc);
1074                 diff = 0;
1075                 GOTO(out, rc);
1076         }
1077
1078         next_id = ofd_seq_last_oid(oseq) + 1;
1079         count = ofd_precreate_batch(ofd, (int)diff);
1080
1081         rc = ofd_precreate_objects(env, ofd, next_id, oseq, count, 0, false);
1082         if (rc < 0) {
1083                 CERROR("%s: unable to precreate: rc = %d\n",
1084                        ofd_name(ofd), rc);
1085         } else {
1086                 rc = ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
1087                 if (rc) {
1088                         CERROR("%s: Bad %llu to set " DOSTID " : rc %d\n",
1089                                ofd_name(ofd),
1090                                (unsigned long long)ofd_seq_last_oid(oseq),
1091                                POSTID(&oa->o_oi), rc);
1092                 }
1093                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1094         }
1095
1096         tgt_grant_commit(ofd_obd(ofd)->obd_self_export, granted, rc);
1097 out:
1098         mutex_unlock(&oseq->os_create_lock);
1099         ofd_seq_put(env, oseq);
1100
1101 out_sem:
1102         up_read(&ofd->ofd_lastid_rwsem);
1103         RETURN(rc);
1104 }
1105
1106 /**
1107  * Implementation of obd_ops::o_getattr.
1108  *
1109  * This function is only used by ECHO client when it is run on top of OFD
1110  * and returns attributes of object.
1111  * \see  ofd_getattr_hdl() for request handler function.
1112  *
1113  * \param[in]     env   execution environment
1114  * \param[in]     exp   OBD export of OFD device
1115  * \param[in,out] oa    contains FID of object to get attributes from and
1116  *                      is used to return attributes back
1117  *
1118  * \retval              0 if successful
1119  * \retval              negative value on error
1120  */
1121 static int ofd_echo_getattr(const struct lu_env *env, struct obd_export *exp,
1122                             struct obdo *oa)
1123 {
1124         struct ofd_device *ofd = ofd_exp(exp);
1125         struct ofd_thread_info *info;
1126         struct lu_fid *fid = &oa->o_oi.oi_fid;
1127         struct ofd_object *fo;
1128         int rc = 0;
1129
1130         ENTRY;
1131
1132         info = ofd_info_init(env, exp);
1133
1134         fo = ofd_object_find_exists(env, ofd, fid);
1135         if (IS_ERR(fo))
1136                 GOTO(out, rc = PTR_ERR(fo));
1137
1138         LASSERT(fo != NULL);
1139         rc = ofd_attr_get(env, fo, &info->fti_attr);
1140         oa->o_valid = OBD_MD_FLID;
1141         if (rc == 0) {
1142                 __u64 curr_version;
1143
1144                 obdo_from_la(oa, &info->fti_attr,
1145                              OFD_VALID_FLAGS | LA_UID | LA_GID | LA_PROJID);
1146
1147                 /* Store object version in reply */
1148                 curr_version = dt_version_get(env, ofd_object_child(fo));
1149                 if ((__s64)curr_version != -EOPNOTSUPP) {
1150                         oa->o_valid |= OBD_MD_FLDATAVERSION;
1151                         oa->o_data_version = curr_version;
1152                 }
1153         }
1154
1155         ofd_object_put(env, fo);
1156 out:
1157         RETURN(rc);
1158 }
1159
1160 /**
1161  * Get object version for OBD_IOC_GET_OBJ_VERSION ioctl.
1162  *
1163  * This is supplemental function for ofd_iocontrol() to return object
1164  * version for lctl tool.
1165  *
1166  * \param[in]  env      execution environment
1167  * \param[in]  ofd      OFD device
1168  * \param[out] karg     ioctl data
1169  *
1170  * \retval              0 if successful
1171  * \retval              negative value on error
1172  */
1173 static int ofd_ioc_get_obj_version(const struct lu_env *env,
1174                                    struct ofd_device *ofd, void *karg)
1175 {
1176         struct obd_ioctl_data *data = karg;
1177         struct lu_fid fid;
1178         struct ofd_object *fo;
1179         dt_obj_version_t version;
1180         int rc = 0;
1181
1182         ENTRY;
1183         if (!data || !data->ioc_inlbuf2 || data->ioc_inllen2 != sizeof(version))
1184                 GOTO(out, rc = -EINVAL);
1185
1186         if (data->ioc_inlbuf1 && data->ioc_inllen1 == sizeof(fid)) {
1187                 fid = *(struct lu_fid *)data->ioc_inlbuf1;
1188         } else if (data->ioc_inlbuf3 &&
1189                    data->ioc_inllen3 == sizeof(__u64) &&
1190                    data->ioc_inlbuf4 &&
1191                    data->ioc_inllen4 == sizeof(__u64)) {
1192                 struct ost_id ostid = { };
1193
1194                 ostid_set_seq(&ostid, *(__u64 *)data->ioc_inlbuf4);
1195                 rc = ostid_set_id(&ostid, *(__u64 *)data->ioc_inlbuf3);
1196                 if (rc)
1197                         GOTO(out, rc);
1198                 rc = ostid_to_fid(&fid, &ostid,
1199                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
1200                 if (rc != 0)
1201                         GOTO(out, rc);
1202         } else {
1203                 GOTO(out, rc = -EINVAL);
1204         }
1205
1206         if (!fid_is_sane(&fid))
1207                 GOTO(out, rc = -EINVAL);
1208
1209         fo = ofd_object_find(env, ofd, &fid);
1210         if (IS_ERR(fo))
1211                 GOTO(out, rc = PTR_ERR(fo));
1212
1213         if (!ofd_object_exists(fo))
1214                 GOTO(out_fo, rc = -ENOENT);
1215
1216         if (lu_object_remote(&fo->ofo_obj.do_lu))
1217                 GOTO(out_fo, rc = -EREMOTE);
1218
1219         version = dt_version_get(env, ofd_object_child(fo));
1220         if (version == 0)
1221                 GOTO(out_fo, rc = -EIO);
1222
1223         *(dt_obj_version_t *)data->ioc_inlbuf2 = version;
1224
1225         EXIT;
1226 out_fo:
1227         ofd_object_put(env, fo);
1228 out:
1229         return rc;
1230 }
1231
1232 /**
1233  * Implementation of obd_ops::o_iocontrol.
1234  *
1235  * This is OFD ioctl handling function which is primary interface for
1236  * Lustre tools like lfs, lctl and lfsck.
1237  *
1238  * \param[in]     cmd   ioctl command
1239  * \param[in]     exp   OBD export of OFD
1240  * \param[in]     len   not used
1241  * \param[in,out] karg  buffer with data
1242  * \param[in]     uarg  not used
1243  *
1244  * \retval              0 if successful
1245  * \retval              negative value on error
1246  */
1247 static int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1248                          void *karg, void __user *uarg)
1249 {
1250         struct lu_env env;
1251         struct ofd_device *ofd = ofd_exp(exp);
1252         struct obd_device *obd = ofd_obd(ofd);
1253         struct obd_ioctl_data *data;
1254         int rc;
1255
1256         ENTRY;
1257         CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
1258                obd->obd_name, cmd, len, karg, uarg);
1259         rc = lu_env_init(&env, LCT_DT_THREAD);
1260         if (rc)
1261                 RETURN(rc);
1262
1263         /* handle commands that don't use @karg first */
1264         rc = -EINVAL;
1265         switch (cmd) {
1266         case OBD_IOC_ABORT_RECOVERY:
1267                 LCONSOLE_WARN("%s: Aborting recovery\n", obd->obd_name);
1268                 obd->obd_abort_recovery = 1;
1269                 target_stop_recovery_thread(obd);
1270                 GOTO(out, rc);
1271         case OBD_IOC_SYNC:
1272                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1273                 rc = dt_sync(&env, ofd->ofd_osd);
1274                 GOTO(out, rc);
1275         case OBD_IOC_SET_READONLY:
1276                 rc = dt_sync(&env, ofd->ofd_osd);
1277                 if (rc == 0)
1278                         rc = dt_ro(&env, ofd->ofd_osd);
1279                 GOTO(out, rc);
1280         }
1281
1282         if (unlikely(karg == NULL)) {
1283                 OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", rc = -EINVAL);
1284                 GOTO(out, rc);
1285         }
1286         data = karg;
1287
1288         switch (cmd) {
1289         case OBD_IOC_START_LFSCK: {
1290                 struct lfsck_start_param lsp;
1291
1292                 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
1293                 lsp.lsp_index_valid = 0;
1294                 rc = lfsck_start(&env, ofd->ofd_osd, &lsp);
1295                 break;
1296         }
1297         case OBD_IOC_STOP_LFSCK: {
1298                 struct lfsck_stop stop;
1299
1300                 stop.ls_status = LS_STOPPED;
1301                 /* Old lfsck utils may pass NULL @stop. */
1302                 if (!data->ioc_inlbuf1)
1303                         stop.ls_flags = 0;
1304                 else
1305                         stop.ls_flags =
1306                            ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
1307
1308                 rc = lfsck_stop(&env, ofd->ofd_osd, &stop);
1309                 break;
1310         }
1311         case OBD_IOC_GET_OBJ_VERSION:
1312                 rc = ofd_ioc_get_obj_version(&env, ofd, karg);
1313                 break;
1314         default:
1315                 rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
1316                 break;
1317         }
1318 out:
1319         lu_env_fini(&env);
1320         RETURN(rc);
1321 }
1322
1323 /**
1324  * Implementation of obd_ops::o_precleanup.
1325  *
1326  * This function stops device activity before shutting it down. It is called
1327  * from a cleanup function upon forceful device cleanup. For OFD there are no
1328  * special actions, it just invokes target_recovery_cleanup().
1329  *
1330  * \param[in] obd       OBD device of OFD
1331  *
1332  * \retval              0
1333  */
1334 static int ofd_precleanup(struct obd_device *obd)
1335 {
1336         ENTRY;
1337         target_cleanup_recovery(obd);
1338         RETURN(0);
1339 }
1340
1341 /**
1342  * Implementation of obd_ops::o_health_check.
1343  *
1344  * This function checks the OFD device health - ability to respond on
1345  * incoming requests. There are two health_check methods:
1346  * - get statfs from the OSD. It checks just responsiveness of
1347  *   bottom device
1348  * - do write attempt on bottom device to check it is fully operational and
1349  *   is not stuck. This is expensive method and must be manually enabled
1350  *   by setting the enable_health_write sysfs parameter.
1351  *
1352  * \param[in] nul       not used
1353  * \param[in] obd       OBD device of OFD
1354  *
1355  * \retval              0 if successful
1356  * \retval              negative value in case of error
1357  */
1358 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1359 {
1360         struct ofd_device *ofd = ofd_dev(obd->obd_lu_dev);
1361         struct ofd_thread_info *info;
1362         struct lu_env env;
1363         struct thandle *th;
1364         int rc1 = 0;
1365         int rc = 0;
1366
1367         /* obd_proc_read_health pass NULL env, we need real one */
1368         rc = lu_env_init(&env, LCT_DT_THREAD);
1369         if (rc)
1370                 RETURN(rc);
1371
1372         info = ofd_info_init(&env, NULL);
1373         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1374         if (unlikely(rc))
1375                 GOTO(out, rc);
1376
1377         if (info->fti_u.osfs.os_state & OS_STATFS_READONLY)
1378                 GOTO(out, rc = -EROFS);
1379
1380         if (!obd_enable_health_write)
1381                 goto out;
1382
1383         OBD_ALLOC(info->fti_buf.lb_buf, PAGE_SIZE);
1384         if (!info->fti_buf.lb_buf)
1385                 GOTO(out, rc = -ENOMEM);
1386
1387         info->fti_buf.lb_len = PAGE_SIZE;
1388         info->fti_off = 0;
1389
1390         th = dt_trans_create(&env, ofd->ofd_osd);
1391         if (IS_ERR(th))
1392                 GOTO(out, rc = PTR_ERR(th));
1393
1394         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1395                                      &info->fti_buf, info->fti_off, th);
1396         if (rc)
1397                 goto out_stop;
1398
1399         th->th_sync = 1; /* sync IO is needed */
1400         rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1401
1402         if (rc)
1403                 goto out_stop;
1404
1405         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1406                              &info->fti_buf, &info->fti_off,
1407                              th);
1408
1409 out_stop:
1410         rc1 = dt_trans_stop(&env, ofd->ofd_osd, th);
1411         rc = rc ? rc : rc1;
1412
1413         OBD_FREE(info->fti_buf.lb_buf, PAGE_SIZE);
1414
1415         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",
1416                rc);
1417
1418 out:
1419         lu_env_fini(&env);
1420         return !!rc;
1421 }
1422
1423 const struct obd_ops ofd_obd_ops = {
1424         .o_owner                = THIS_MODULE,
1425         .o_connect              = ofd_obd_connect,
1426         .o_reconnect            = ofd_obd_reconnect,
1427         .o_disconnect           = ofd_obd_disconnect,
1428         .o_create               = ofd_echo_create,
1429         .o_statfs               = ofd_statfs,
1430         .o_setattr              = ofd_echo_setattr,
1431         .o_preprw               = ofd_preprw,
1432         .o_commitrw             = ofd_commitrw,
1433         .o_destroy              = ofd_echo_destroy,
1434         .o_init_export          = ofd_init_export,
1435         .o_destroy_export       = ofd_destroy_export,
1436         .o_postrecov            = ofd_obd_postrecov,
1437         .o_getattr              = ofd_echo_getattr,
1438         .o_iocontrol            = ofd_iocontrol,
1439         .o_precleanup           = ofd_precleanup,
1440         .o_health_check         = ofd_health_check,
1441         .o_set_info_async       = ofd_set_info_async,
1442         .o_get_info             = ofd_get_info,
1443 };