Whamcloud - gitweb
09922d81f3f913fdb0532a45972e272771fb0350
[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, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ofd/ofd_obd.c
33  *
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.
38  *
39  * Author: Andreas Dilger <andreas.dilger@intel.com>
40  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.pershin@intel.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_FILTER
45
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>
53
54 /**
55  * Initialize OFD per-export statistics.
56  *
57  * This function sets up procfs entries for various OFD export counters. These
58  * counters are for per-client statistics tracked on the server.
59  *
60  * \param[in] ofd        OFD device
61  * \param[in] exp        OBD export
62  * \param[in] client_nid NID of client
63  *
64  * \retval              0 if successful
65  * \retval              negative value on error
66  */
67 static int ofd_export_stats_init(struct ofd_device *ofd,
68                                  struct obd_export *exp,
69                                  lnet_nid_t *client_nid)
70 {
71         struct obd_device       *obd = ofd_obd(ofd);
72         struct nid_stat         *stats;
73         int                      rc;
74         ENTRY;
75
76         LASSERT(obd->obd_uses_nid_stats);
77
78         if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
79                 /* Self-export gets no proc entry */
80                 RETURN(0);
81
82         rc = lprocfs_exp_setup(exp, client_nid);
83         if (rc != 0)
84                 /* Mask error for already created /proc entries */
85                 RETURN(rc == -EALREADY ? 0 : rc);
86
87         stats = exp->exp_nid_stats;
88         stats->nid_stats = lprocfs_alloc_stats(NUM_OBD_STATS +
89                                                 LPROC_OFD_STATS_LAST,
90                                                 LPROCFS_STATS_FLAG_NOPERCPU);
91         if (stats->nid_stats == NULL)
92                 RETURN(-ENOMEM);
93
94         lprocfs_init_ops_stats(LPROC_OFD_STATS_LAST, stats->nid_stats);
95
96         ofd_stats_counter_init(stats->nid_stats);
97
98         rc = lprocfs_register_stats(stats->nid_proc, "stats", stats->nid_stats);
99         if (rc != 0) {
100                 lprocfs_free_stats(&stats->nid_stats);
101                 GOTO(out, rc);
102         }
103
104         rc = lprocfs_nid_ldlm_stats_init(stats);
105         if (rc != 0)
106                 GOTO(out, rc);
107
108 out:
109         RETURN(rc);
110 }
111
112 /**
113  * Match client and OST server connection feature flags.
114  *
115  * Compute the compatibility flags for a connection request based on
116  * features mutually supported by client and server.
117  *
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().
124  *
125  * \param[in] env               execution environment
126  * \param[in] exp               the obd_export associated with this
127  *                              client/target pair
128  * \param[in] data              stores data for this connect request
129  * \param[in] new_connection    is this connection new or not
130  *
131  * \retval              0 if success
132  * \retval              -EPROTO client and server feature requirements are
133  *                      incompatible
134  * \retval              -EBADF  OST index in connect request doesn't match
135  *                      real OST index
136  */
137 static int ofd_parse_connect_data(const struct lu_env *env,
138                                   struct obd_export *exp,
139                                   struct obd_connect_data *data,
140                                   bool new_connection)
141 {
142         struct ofd_device                *ofd = ofd_exp(exp);
143         struct filter_export_data        *fed = &exp->exp_filter_data;
144
145         if (!data)
146                 RETURN(0);
147
148         CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: %#llx"
149                " ocd_version: %x ocd_grant: %d ocd_index: %u"
150                " ocd_group %u\n",
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);
154
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,
161                       data->ocd_group);
162                 RETURN(-EPROTO);
163         }
164         fed->fed_group = data->ocd_group;
165
166         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
167         data->ocd_connect_flags2 &= OST_CONNECT_SUPPORTED2;
168         data->ocd_version = LUSTRE_VERSION_CODE;
169
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)
175                 RETURN(-EPROTO);
176
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);
193                         RETURN(-EPROTO);
194                 }
195         }
196
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;
208         }
209
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);
215         }
216
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;
220
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 "
225                                            "configuration?\n",
226                                            obd_export_nid2str(exp), index,
227                                            data->ocd_index);
228                         RETURN(-EBADF);
229                 }
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);
236                 }
237         }
238
239         if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
240                 __u32 cksum_types = data->ocd_cksum_types;
241
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
244                  * support */
245                 data->ocd_cksum_types &= cksum_types_supported_server();
246
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);
251                         RETURN(-EPROTO);
252                 }
253
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);
257         } else {
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));
263         }
264
265         if (data->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
266                 data->ocd_maxbytes = ofd->ofd_dt_conf.ddp_maxbytes;
267
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);
273                 } else {
274                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
275                 }
276         }
277
278         RETURN(0);
279 }
280
281 /**
282  * Re-initialize export upon client reconnection.
283  *
284  * This function parses connection data from reconnect and resets
285  * export statistics.
286  *
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
293  *
294  * \retval              0 if successful
295  * \retval              negative value on error
296  */
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,
300                              void *client_nid)
301 {
302         struct ofd_device       *ofd;
303         int                      rc;
304
305         ENTRY;
306
307         if (exp == NULL || obd == NULL || cluuid == NULL)
308                 RETURN(-EINVAL);
309
310         rc = nodemap_add_member(*(lnet_nid_t *)client_nid, exp);
311         if (rc != 0 && rc != -EEXIST)
312                 RETURN(rc);
313
314         ofd = ofd_dev(obd->obd_lu_dev);
315
316         rc = ofd_parse_connect_data(env, exp, data, false);
317         if (rc == 0)
318                 ofd_export_stats_init(ofd, exp, client_nid);
319         else
320                 nodemap_del_member(exp);
321
322         RETURN(rc);
323 }
324
325 /**
326  * Initialize new client connection.
327  *
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.
331  *
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
338  *
339  * \retval              0 if successful
340  * \retval              negative value on error
341  */
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)
345 {
346         struct obd_export       *exp;
347         struct ofd_device       *ofd;
348         struct lustre_handle     conn = { 0 };
349         int                      rc;
350         ENTRY;
351
352         if (_exp == NULL || obd == NULL || cluuid == NULL)
353                 RETURN(-EINVAL);
354
355         ofd = ofd_dev(obd->obd_lu_dev);
356
357         rc = class_connect(&conn, obd, cluuid);
358         if (rc)
359                 RETURN(rc);
360
361         exp = class_conn2export(&conn);
362         LASSERT(exp != NULL);
363
364         if (localdata != NULL) {
365                 rc = nodemap_add_member(*(lnet_nid_t *)localdata, exp);
366                 if (rc != 0 && rc != -EEXIST)
367                         GOTO(out, rc);
368         } else {
369                 CDEBUG(D_HA, "%s: cannot find nodemap for client %s: "
370                        "nid is null\n", obd->obd_name, cluuid->uuid);
371         }
372
373         rc = ofd_parse_connect_data(env, exp, data, true);
374         if (rc)
375                 GOTO(out, rc);
376
377         if (obd->obd_replayable) {
378                 struct tg_export_data *ted = &exp->exp_target_data;
379
380                 memcpy(ted->ted_lcd->lcd_uuid, cluuid,
381                        sizeof(ted->ted_lcd->lcd_uuid));
382                 rc = tgt_client_new(env, exp);
383                 if (rc != 0)
384                         GOTO(out, rc);
385                 ofd_export_stats_init(ofd, exp, localdata);
386         }
387
388         CDEBUG(D_HA, "%s: get connection from MDS %d\n", obd->obd_name,
389                data ? data->ocd_group : -1);
390
391 out:
392         if (rc != 0) {
393                 class_disconnect(exp);
394                 nodemap_del_member(exp);
395                 *_exp = NULL;
396         } else {
397                 *_exp = exp;
398         }
399         RETURN(rc);
400 }
401
402 /**
403  * Disconnect a connected client.
404  *
405  * This function terminates the client connection. The client export is
406  * disconnected (cleaned up) and client data on persistent storage is removed.
407  *
408  * \param[in] exp       OBD export
409  *
410  * \retval              0 if successful
411  * \retval              negative value on error
412  */
413 int ofd_obd_disconnect(struct obd_export *exp)
414 {
415         struct ofd_device       *ofd = ofd_exp(exp);
416         struct lu_env            env;
417         int                      rc;
418
419         ENTRY;
420
421         LASSERT(exp);
422         class_export_get(exp);
423
424         if (!(exp->exp_flags & OBD_OPT_FORCE))
425                 ofd_grant_sanity_check(ofd_obd(ofd), __FUNCTION__);
426
427         rc = server_disconnect_export(exp);
428
429         ofd_grant_discard(exp);
430
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);
435                 if (rc)
436                         GOTO(out, rc);
437
438                 tgt_client_del(&env, exp);
439                 lu_env_fini(&env);
440         }
441 out:
442         nodemap_del_member(exp);
443         class_export_put(exp);
444         RETURN(rc);
445 }
446
447 /**
448  * Implementation of obd_ops::o_init_export.
449  *
450  * This function is called from class_new_export() and initializes
451  * the OFD-specific data for new export.
452  *
453  * \param[in] exp       OBD export
454  *
455  * \retval              0 if successful
456  * \retval              negative value on error
457  */
458 static int ofd_init_export(struct obd_export *exp)
459 {
460         int rc;
461
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);
468
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)))
472                 return 0;
473
474         rc = tgt_client_alloc(exp);
475         if (rc == 0)
476                 ldlm_init_export(exp);
477         if (rc)
478                 CERROR("%s: Can't initialize export: rc %d\n",
479                        exp->exp_obd->obd_name, rc);
480         return rc;
481 }
482
483 /**
484  * Implementation of obd_ops::o_destroy_export.
485  *
486  * This function is called from class_export_destroy() to cleanup
487  * the OFD-specific data for export being destroyed.
488  *
489  * \param[in] exp       OBD export
490  *
491  * \retval              0 if successful
492  * \retval              negative value on error
493  */
494 static int ofd_destroy_export(struct obd_export *exp)
495 {
496         struct ofd_device *ofd = ofd_exp(exp);
497
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);
502
503         target_destroy_export(exp);
504
505         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
506                                      &exp->exp_client_uuid)))
507                 return 0;
508
509         ldlm_destroy_export(exp);
510         tgt_client_free(exp);
511
512         ofd_fmd_cleanup(exp);
513
514         /*
515          * discard grants once we're sure no more
516          * interaction with the client is possible
517          */
518         ofd_grant_discard(exp);
519         ofd_fmd_cleanup(exp);
520
521         if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
522                 ofd->ofd_tot_granted_clients--;
523
524         if (!(exp->exp_flags & OBD_OPT_FORCE))
525                 ofd_grant_sanity_check(exp->exp_obd, __FUNCTION__);
526
527         LASSERT(list_empty(&exp->exp_filter_data.fed_mod_list));
528         return 0;
529 }
530
531 /**
532  * Notify all devices in server stack about recovery completion.
533  *
534  * This function calls ldo_recovery_complete() for all lower devices in the
535  * server stack so they will be prepared for normal operations.
536  *
537  * \param[in] env       execution environment
538  * \param[in] ofd       OFD device
539  *
540  * \retval              0 if successful
541  * \retval              negative value on error
542  */
543 int ofd_postrecov(const struct lu_env *env, struct ofd_device *ofd)
544 {
545         struct lu_device *ldev = &ofd->ofd_dt_dev.dd_lu_dev;
546         int rc;
547
548         CDEBUG(D_HA, "%s: recovery is over\n", ofd_name(ofd));
549
550         if (!ofd->ofd_skip_lfsck) {
551                 struct lfsck_start_param lsp;
552
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",
558                               ofd_name(ofd), rc);
559         }
560
561         return ldev->ld_ops->ldo_recovery_complete(env, ldev);
562 }
563
564 /**
565  * Implementation of obd_ops::o_postrecov.
566  *
567  * This function is called from target_finish_recovery() upon recovery
568  * completion.
569  *
570  * \param[in] obd       OBD device of OFD
571  *
572  * \retval              0 if successful
573  * \retval              negative value on error
574  */
575 static int ofd_obd_postrecov(struct obd_device *obd)
576 {
577         struct lu_env            env;
578         struct lu_device        *ldev = obd->obd_lu_dev;
579         int                      rc;
580
581         ENTRY;
582
583         rc = lu_env_init(&env, LCT_DT_THREAD);
584         if (rc)
585                 RETURN(rc);
586         ofd_info_init(&env, obd->obd_self_export);
587
588         rc = ofd_postrecov(&env, ofd_dev(ldev));
589
590         lu_env_fini(&env);
591         RETURN(rc);
592 }
593
594 /**
595  * Implementation of obd_ops::o_set_info_async.
596  *
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.
600  *
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
608  *
609  * \retval              0 if successful
610  * \retval              negative value on error
611  */
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)
615 {
616         int rc = 0;
617
618         ENTRY;
619
620         if (exp->exp_obd == NULL) {
621                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
622                 RETURN(-EINVAL);
623         }
624
625         if (KEY_IS(KEY_SPTLRPC_CONF)) {
626                 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
627         } else {
628                 CERROR("%s: Unsupported key %s\n",
629                        exp->exp_obd->obd_name, (char*)key);
630                 rc = -EOPNOTSUPP;
631         }
632         RETURN(rc);
633 }
634
635 /**
636  * Implementation of obd_ops::o_get_info.
637  *
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.
640  *
641  * \see  ofd_get_info_hdl() for request handler function.
642  *
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
649  *
650  * \retval              0 if successful
651  * \retval              negative value on error
652  */
653 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
654                         __u32 keylen, void *key, __u32 *vallen, void *val)
655 {
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;
660         int                              rc = 0;
661
662         ENTRY;
663
664         if (exp->exp_obd == NULL) {
665                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
666                 RETURN(-EINVAL);
667         }
668
669         ofd = ofd_exp(exp);
670
671         if (KEY_IS(KEY_FIEMAP)) {
672                 info = ofd_info_init(env, exp);
673
674                 rc = ostid_to_fid(&info->fti_fid, &fm_key->lfik_oa.o_oi,
675                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
676                 if (rc != 0)
677                         RETURN(rc);
678
679                 rc = ofd_fiemap_get(env, ofd, &info->fti_fid, fiemap);
680         } else {
681                 CERROR("%s: not supported key %s\n", ofd_name(ofd), (char*)key);
682                 rc = -EOPNOTSUPP;
683         }
684
685         RETURN(rc);
686 }
687
688 /**
689  * Get file system statistics of OST server.
690  *
691  * Helper function for ofd_statfs(), also used by grant code.
692  * Implements caching for statistics to avoid calling OSD device each time.
693  *
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
699  *
700  * \retval              0 if successful
701  * \retval              negative value on error
702  */
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)
705 {
706         int rc = 0;
707         ENTRY;
708
709         spin_lock(&ofd->ofd_osfs_lock);
710         if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
711                 u64 unstable;
712
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 */
717
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);
729
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);
733                 if (unlikely(rc))
734                         GOTO(out, rc);
735
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
739                  * ofd_osfs_lock */
740                 unstable = ofd->ofd_osfs_inflight - unstable;
741                 ofd->ofd_osfs_unstable = 0;
742                 if (unstable) {
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;
757                 }
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);
762
763                 /* finally udpate cached statfs data */
764                 ofd->ofd_osfs = *osfs;
765                 ofd->ofd_osfs_age = cfs_time_current_64();
766
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);
771
772                 if (from_cache)
773                         *from_cache = 0;
774         } else {
775                 /* use cached statfs data */
776                 *osfs = ofd->ofd_osfs;
777                 spin_unlock(&ofd->ofd_osfs_lock);
778                 if (from_cache)
779                         *from_cache = 1;
780         }
781         GOTO(out, rc);
782
783 out:
784         return rc;
785 }
786
787 /**
788  * Implementation of obd_ops::o_statfs.
789  *
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.
793  *
794  * \see  ofd_statfs_hdl() for request handler function.
795  *
796  * Report also the state of the OST to the caller in osfs->os_state
797  * (OS_STATE_READONLY, OS_STATE_DEGRADED).
798  *
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
804  *
805  * \retval              0 if successful
806  * \retval              negative value on error
807  */
808 int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
809                struct obd_statfs *osfs, __u64 max_age, __u32 flags)
810 {
811         struct obd_device       *obd = class_exp2obd(exp);
812         struct ofd_device       *ofd = ofd_exp(exp);
813         int                      rc;
814
815         ENTRY;
816
817         rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
818         if (unlikely(rc))
819                 GOTO(out, rc);
820
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 */
824
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);
830
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));
834
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;
839
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);
843         }
844
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);
850
851         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
852                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
853                 osfs->os_ffree = 0;
854
855         /* OS_STATE_READONLY can be set by OSD already */
856         if (ofd->ofd_raid_degraded)
857                 osfs->os_state |= OS_STATE_DEGRADED;
858
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;
870         }
871
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;
875
876         EXIT;
877 out:
878         return rc;
879 }
880
881 /**
882  * Implementation of obd_ops::o_setattr.
883  *
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.
886
887  * \param[in] env       execution environment
888  * \param[in] exp       OBD export of OFD device
889  * \param[in] oa        setattr parameters
890  *
891  * \retval              0 if successful
892  * \retval              negative value on error
893  */
894 static int ofd_echo_setattr(const struct lu_env *env, struct obd_export *exp,
895                             struct obdo *oa)
896 {
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;
904         int                      rc = 0;
905
906         ENTRY;
907
908         info = ofd_info_init(env, exp);
909
910         ost_fid_build_resid(fid, &info->fti_resid);
911
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];
916
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);
921                 else
922                         mdsinum[0] = '\0';
923
924                 CERROR("%s: setattr from %s trying to truncate object "DFID
925                        " %s\n", ofd_name(ofd), obd_export_nid2str(exp),
926                        PFID(fid), mdsinum);
927                 GOTO(out, rc = -EPERM);
928         }
929
930         fo = ofd_object_find_exists(env, ofd, fid);
931         if (IS_ERR(fo)) {
932                 CERROR("%s: can't find object "DFID"\n",
933                        ofd_name(ofd), PFID(fid));
934                 GOTO(out, rc = PTR_ERR(fo));
935         }
936
937         la_from_obdo(&info->fti_attr, oa, oa->o_valid);
938         info->fti_attr.la_valid &= ~LA_TYPE;
939
940         if (oa->o_valid & OBD_MD_FLFID) {
941                 ff = &info->fti_mds_fid;
942                 ofd_prepare_fidea(ff, oa);
943         }
944
945         /* setting objects attributes (including owner/group) */
946         rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
947         if (rc)
948                 GOTO(out_unlock, rc);
949
950         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, NULL, 1);
951         EXIT;
952 out_unlock:
953         ofd_object_put(env, fo);
954 out:
955         if (rc == 0) {
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);
962                 if (!IS_ERR(res)) {
963                         ldlm_res_lvbo_update(res, NULL, 0);
964                         ldlm_resource_putref(res);
965                 }
966         }
967
968         return rc;
969 }
970
971 /**
972  * Destroy OFD object by its FID.
973  *
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.
977  *
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
982  *
983  * \retval              0 if successful
984  * \retval              negative value on error
985  */
986 int ofd_destroy_by_fid(const struct lu_env *env, struct ofd_device *ofd,
987                        const struct lu_fid *fid, int orphan)
988 {
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;
994         __u64 rc = 0;
995
996         ENTRY;
997
998         fo = ofd_object_find_exists(env, ofd, fid);
999         if (IS_ERR(fo))
1000                 RETURN(PTR_ERR(fo));
1001
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);
1009
1010         /* We only care about the side-effects, just drop the lock. */
1011         if (rc == ELDLM_OK)
1012                 ldlm_lock_decref(&lockh, LCK_PW);
1013
1014         LASSERT(fo != NULL);
1015
1016         rc = ofd_object_destroy(env, fo, orphan);
1017         EXIT;
1018
1019         ofd_object_put(env, fo);
1020         RETURN(rc);
1021 }
1022
1023 /**
1024  * Implementation of obd_ops::o_destroy.
1025  *
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.
1028
1029  * \param[in] env       execution environment
1030  * \param[in] exp       OBD export of OFD device
1031  * \param[in] oa        obdo structure with FID
1032  *
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.
1036  *
1037  * \retval              0 if successful
1038  * \retval              negative value on error
1039  */
1040 static int ofd_echo_destroy(const struct lu_env *env, struct obd_export *exp,
1041                             struct obdo *oa)
1042 {
1043         struct ofd_device       *ofd = ofd_exp(exp);
1044         struct lu_fid           *fid = &oa->o_oi.oi_fid;
1045         int                      rc = 0;
1046
1047         ENTRY;
1048
1049         ofd_info_init(env, exp);
1050
1051         CDEBUG(D_HA, "%s: Destroy object "DFID"\n", ofd_name(ofd), PFID(fid));
1052
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));
1057                 GOTO(out, rc);
1058         } else if (rc != 0) {
1059                 CERROR("%s: error destroying object "DFID": %d\n",
1060                        ofd_name(ofd), PFID(fid), rc);
1061                 GOTO(out, rc);
1062         }
1063         EXIT;
1064 out:
1065         return rc;
1066 }
1067
1068 /**
1069  * Implementation of obd_ops::o_create.
1070  *
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.
1074  *
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
1078  *
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.
1082  *
1083  * \retval              0 if successful
1084  * \retval              negative value on error
1085  */
1086 static int ofd_echo_create(const struct lu_env *env, struct obd_export *exp,
1087                            struct obdo *oa)
1088 {
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;
1094         long                     granted;
1095         u64                      next_id;
1096         int                      count;
1097
1098         ENTRY;
1099
1100         info = ofd_info_init(env, exp);
1101
1102         LASSERT(seq == FID_SEQ_ECHO);
1103         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1104
1105         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1106
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);
1113
1114         oseq = ofd_seq_load(env, ofd, seq);
1115         if (IS_ERR(oseq)) {
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);
1119         }
1120
1121         mutex_lock(&oseq->os_create_lock);
1122         granted = ofd_grant_create(env, ofd_obd(ofd)->obd_self_export, &diff);
1123         if (granted < 0) {
1124                 rc = granted;
1125                 granted = 0;
1126                 CDEBUG(D_HA, "%s: failed to acquire grant space for "
1127                        "precreate (%d): rc = %d\n", ofd_name(ofd), diff, rc);
1128                 diff = 0;
1129                 GOTO(out, rc);
1130         }
1131
1132         next_id = ofd_seq_last_oid(oseq) + 1;
1133         count = ofd_precreate_batch(ofd, diff);
1134
1135         rc = ofd_precreate_objects(env, ofd, next_id, oseq, count, 0);
1136         if (rc < 0) {
1137                 CERROR("%s: unable to precreate: rc = %d\n",
1138                        ofd_name(ofd), rc);
1139         } else {
1140                 ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
1141                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1142                 rc = 0;
1143         }
1144
1145         ofd_grant_commit(ofd_obd(ofd)->obd_self_export, granted, rc);
1146 out:
1147         mutex_unlock(&oseq->os_create_lock);
1148         ofd_seq_put(env, oseq);
1149
1150 out_sem:
1151         up_read(&ofd->ofd_lastid_rwsem);
1152         RETURN(rc);
1153 }
1154
1155 /**
1156  * Implementation of obd_ops::o_getattr.
1157  *
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.
1161  *
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
1166  *
1167  * \retval              0 if successful
1168  * \retval              negative value on error
1169  */
1170 static int ofd_echo_getattr(const struct lu_env *env, struct obd_export *exp,
1171                             struct obdo *oa)
1172 {
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;
1177         int                      rc = 0;
1178
1179         ENTRY;
1180
1181         info = ofd_info_init(env, exp);
1182
1183         fo = ofd_object_find_exists(env, ofd, fid);
1184         if (IS_ERR(fo))
1185                 GOTO(out, rc = PTR_ERR(fo));
1186
1187         LASSERT(fo != NULL);
1188         rc = ofd_attr_get(env, fo, &info->fti_attr);
1189         oa->o_valid = OBD_MD_FLID;
1190         if (rc == 0) {
1191                 __u64 curr_version;
1192
1193                 obdo_from_la(oa, &info->fti_attr,
1194                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1195
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;
1201                 }
1202         }
1203
1204         ofd_object_put(env, fo);
1205 out:
1206         RETURN(rc);
1207 }
1208
1209 /**
1210  * Get object version for OBD_IOC_GET_OBJ_VERSION ioctl.
1211  *
1212  * This is supplemental function for ofd_iocontrol() to return object
1213  * version for lctl tool.
1214  *
1215  * \param[in]  env      execution environment
1216  * \param[in]  ofd      OFD device
1217  * \param[out] karg     ioctl data
1218  *
1219  * \retval              0 if successful
1220  * \retval              negative value on error
1221  */
1222 static int ofd_ioc_get_obj_version(const struct lu_env *env,
1223                                    struct ofd_device *ofd, void *karg)
1224 {
1225         struct obd_ioctl_data *data = karg;
1226         struct lu_fid          fid;
1227         struct ofd_object     *fo;
1228         dt_obj_version_t       version;
1229         int                    rc = 0;
1230
1231         ENTRY;
1232
1233         if (data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
1234                 GOTO(out, rc = -EINVAL);
1235
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;
1243
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);
1248                 if (rc != 0)
1249                         GOTO(out, rc);
1250         } else {
1251                 GOTO(out, rc = -EINVAL);
1252         }
1253
1254         if (!fid_is_sane(&fid))
1255                 GOTO(out, rc = -EINVAL);
1256
1257         fo = ofd_object_find(env, ofd, &fid);
1258         if (IS_ERR(fo))
1259                 GOTO(out, rc = PTR_ERR(fo));
1260
1261         if (!ofd_object_exists(fo))
1262                 GOTO(out_fo, rc = -ENOENT);
1263
1264         if (lu_object_remote(&fo->ofo_obj.do_lu))
1265                 GOTO(out_fo, rc = -EREMOTE);
1266
1267         version = dt_version_get(env, ofd_object_child(fo));
1268         if (version == 0)
1269                 GOTO(out_fo, rc = -EIO);
1270
1271         *(dt_obj_version_t *)data->ioc_inlbuf2 = version;
1272
1273         EXIT;
1274 out_fo:
1275         ofd_object_put(env, fo);
1276 out:
1277         return rc;
1278 }
1279
1280 /**
1281  * Implementation of obd_ops::o_iocontrol.
1282  *
1283  * This is OFD ioctl handling function which is primary interface for
1284  * Lustre tools like lfs, lctl and lfsck.
1285  *
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
1291  *
1292  * \retval              0 if successful
1293  * \retval              negative value on error
1294  */
1295 static int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1296                          void *karg, void __user *uarg)
1297 {
1298         struct lu_env            env;
1299         struct ofd_device       *ofd = ofd_exp(exp);
1300         struct obd_device       *obd = ofd_obd(ofd);
1301         int                      rc;
1302
1303         ENTRY;
1304
1305         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1306         rc = lu_env_init(&env, LCT_DT_THREAD);
1307         if (rc)
1308                 RETURN(rc);
1309
1310         switch (cmd) {
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);
1315                 break;
1316         case OBD_IOC_SYNC:
1317                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1318                 rc = dt_sync(&env, ofd->ofd_osd);
1319                 break;
1320         case OBD_IOC_SET_READONLY:
1321                 rc = dt_sync(&env, ofd->ofd_osd);
1322                 if (rc == 0)
1323                         rc = dt_ro(&env, ofd->ofd_osd);
1324                 break;
1325         case OBD_IOC_START_LFSCK: {
1326                 struct obd_ioctl_data *data = karg;
1327                 struct lfsck_start_param lsp;
1328
1329                 if (unlikely(data == NULL)) {
1330                         rc = -EINVAL;
1331                         break;
1332                 }
1333
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);
1337                 break;
1338         }
1339         case OBD_IOC_STOP_LFSCK: {
1340                 struct obd_ioctl_data *data = karg;
1341                 struct lfsck_stop      stop;
1342
1343                 stop.ls_status = LS_STOPPED;
1344                 /* Old lfsck utils may pass NULL @stop. */
1345                 if (data->ioc_inlbuf1 == NULL)
1346                         stop.ls_flags = 0;
1347                 else
1348                         stop.ls_flags =
1349                         ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
1350
1351                 rc = lfsck_stop(&env, ofd->ofd_osd, &stop);
1352                 break;
1353         }
1354         case OBD_IOC_GET_OBJ_VERSION:
1355                 rc = ofd_ioc_get_obj_version(&env, ofd, karg);
1356                 break;
1357         default:
1358                 CERROR("%s: not supported cmd = %#x\n", obd->obd_name, cmd);
1359                 rc = -ENOTTY;
1360         }
1361
1362         lu_env_fini(&env);
1363         RETURN(rc);
1364 }
1365
1366 /**
1367  * Implementation of obd_ops::o_precleanup.
1368  *
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().
1372  *
1373  * \param[in] obd       OBD device of OFD
1374  *
1375  * \retval              0
1376  */
1377 static int ofd_precleanup(struct obd_device *obd)
1378 {
1379         ENTRY;
1380         target_cleanup_recovery(obd);
1381         RETURN(0);
1382 }
1383
1384 /**
1385  * Implementation of obd_ops::o_ping.
1386  *
1387  * This is OFD-specific part of OBD_PING request handling.
1388  * It controls Filter Modification Data (FMD) expiration each time PING is
1389  * received.
1390  *
1391  * \see  ofd_fmd_expire() and ofd_fmd.c for details
1392  *
1393  * \param[in] env       execution environment
1394  * \param[in] exp       OBD export of client
1395  *
1396  * \retval              0
1397  */
1398 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1399 {
1400         ofd_fmd_expire(exp);
1401         return 0;
1402 }
1403
1404 /**
1405  * Implementation of obd_ops::o_health_check.
1406  *
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
1410  *   bottom device
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
1414  *   by default.
1415  *
1416  * \param[in] nul       not used
1417  * \param[in] obd       OBD device of OFD
1418  *
1419  * \retval              0 if successful
1420  * \retval              negative value in case of error
1421  */
1422 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1423 {
1424         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1425         struct ofd_thread_info  *info;
1426         struct lu_env            env;
1427 #ifdef USE_HEALTH_CHECK_WRITE
1428         struct thandle          *th;
1429 #endif
1430         int                      rc = 0;
1431
1432         /* obd_proc_read_health pass NULL env, we need real one */
1433         rc = lu_env_init(&env, LCT_DT_THREAD);
1434         if (rc)
1435                 RETURN(rc);
1436
1437         info = ofd_info_init(&env, NULL);
1438         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1439         if (unlikely(rc))
1440                 GOTO(out, rc);
1441
1442         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1443                 GOTO(out, rc = -EROFS);
1444
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);
1449
1450         info->fti_buf.lb_len = PAGE_SIZE;
1451         info->fti_off = 0;
1452
1453         th = dt_trans_create(&env, ofd->ofd_osd);
1454         if (IS_ERR(th))
1455                 GOTO(out, rc = PTR_ERR(th));
1456
1457         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1458                                      &info->fti_buf, info->fti_off, th);
1459         if (rc == 0) {
1460                 th->th_sync = 1; /* sync IO is needed */
1461                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1462                 if (rc == 0)
1463                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1464                                              &info->fti_buf, &info->fti_off,
1465                                              th);
1466         }
1467         dt_trans_stop(&env, ofd->ofd_osd, th);
1468
1469         OBD_FREE(info->fti_buf.lb_buf, PAGE_SIZE);
1470
1471         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1472 #endif
1473 out:
1474         lu_env_fini(&env);
1475         return !!rc;
1476 }
1477
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,
1495         .o_ping                 = ofd_ping,
1496         .o_health_check         = ofd_health_check,
1497         .o_set_info_async       = ofd_set_info_async,
1498         .o_get_info             = ofd_get_info,
1499 };