Whamcloud - gitweb
LU-3467 ofd: remove obsoleted OBD methods
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ofd/ofd_obd.c
37  *
38  * Author: Andreas Dilger <adilger@whamcloud.com>
39  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40  * Author: Mike Pershin <tappro@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include <lustre/lustre_idl.h>
46 #include "ofd_internal.h"
47 #include <obd_cksum.h>
48 #include <lustre_quota.h>
49 #include <lustre_lfsck.h>
50
51 static int ofd_export_stats_init(struct ofd_device *ofd,
52                                  struct obd_export *exp, void *client_nid)
53 {
54         struct obd_device       *obd = ofd_obd(ofd);
55         struct nid_stat         *stats;
56         int                      num_stats;
57         int                      rc, newnid = 0;
58
59         ENTRY;
60
61         LASSERT(obd->obd_uses_nid_stats);
62
63         if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
64                 /* Self-export gets no proc entry */
65                 RETURN(0);
66
67         rc = lprocfs_exp_setup(exp, client_nid, &newnid);
68         if (rc) {
69                 /* Mask error for already created
70                  * /proc entries */
71                 if (rc == -EALREADY)
72                         rc = 0;
73                 RETURN(rc);
74         }
75
76         if (newnid == 0)
77                 RETURN(0);
78
79         stats = exp->exp_nid_stats;
80         LASSERT(stats != NULL);
81
82         num_stats = NUM_OBD_STATS + LPROC_OFD_STATS_LAST;
83
84         stats->nid_stats = lprocfs_alloc_stats(num_stats,
85                                                LPROCFS_STATS_FLAG_NOPERCPU);
86         if (stats->nid_stats == NULL)
87                 return -ENOMEM;
88
89         lprocfs_init_ops_stats(LPROC_OFD_STATS_LAST, stats->nid_stats);
90         ofd_stats_counter_init(stats->nid_stats);
91         rc = lprocfs_register_stats(stats->nid_proc, "stats",
92                                     stats->nid_stats);
93         if (rc)
94                 GOTO(clean, rc);
95
96         rc = lprocfs_nid_ldlm_stats_init(stats);
97         if (rc) {
98                 lprocfs_free_stats(&stats->nid_stats);
99                 GOTO(clean, rc);
100         }
101
102         RETURN(0);
103 clean:
104         return rc;
105 }
106
107 static int ofd_parse_connect_data(const struct lu_env *env,
108                                   struct obd_export *exp,
109                                   struct obd_connect_data *data,
110                                   bool new_connection)
111 {
112         struct ofd_device                *ofd = ofd_exp(exp);
113         struct filter_export_data        *fed = &exp->exp_filter_data;
114
115         if (!data)
116                 RETURN(0);
117
118         CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: "LPX64
119                " ocd_version: %x ocd_grant: %d ocd_index: %u"
120                " ocd_group %u\n",
121                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
122                data->ocd_connect_flags, data->ocd_version,
123                data->ocd_grant, data->ocd_index, data->ocd_group);
124
125         if (fed->fed_group != 0 && fed->fed_group != data->ocd_group) {
126                 CWARN("!!! This export (nid %s) used object group %d "
127                       "earlier; now it's trying to use group %d!  This could "
128                       "be a bug in the MDS. Please report to "
129                       "http://bugs.whamcloud.com/\n",
130                       obd_export_nid2str(exp), fed->fed_group,
131                       data->ocd_group);
132                 RETURN(-EPROTO);
133         }
134         fed->fed_group = data->ocd_group;
135
136         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
137         data->ocd_version = LUSTRE_VERSION_CODE;
138
139         /* Kindly make sure the SKIP_ORPHAN flag is from MDS. */
140         if (data->ocd_connect_flags & OBD_CONNECT_MDS)
141                 CDEBUG(D_HA, "%s: Received MDS connection for group %u\n",
142                        exp->exp_obd->obd_name, data->ocd_group);
143         else if (data->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN)
144                 RETURN(-EPROTO);
145
146         if (ofd_grant_param_supp(exp)) {
147                 exp->exp_filter_data.fed_pagesize = data->ocd_blocksize;
148                 /* ocd_{blocksize,inodespace} are log2 values */
149                 data->ocd_blocksize  = ofd->ofd_blockbits;
150                 data->ocd_inodespace = ofd->ofd_dt_conf.ddp_inodespace;
151                 /* ocd_grant_extent is in 1K blocks */
152                 data->ocd_grant_extent = ofd->ofd_dt_conf.ddp_grant_frag >> 10;
153         }
154
155         if (data->ocd_connect_flags & OBD_CONNECT_GRANT)
156                 data->ocd_grant = ofd_grant_connect(env, exp, data->ocd_grant,
157                                                     new_connection);
158
159         if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
160                 struct lr_server_data *lsd = &ofd->ofd_lut.lut_lsd;
161                 int                    index = lsd->lsd_osd_index;
162
163                 if (index != data->ocd_index) {
164                         LCONSOLE_ERROR_MSG(0x136, "Connection from %s to index"
165                                            " %u doesn't match actual OST index"
166                                            " %u in last_rcvd file, bad "
167                                            "configuration?\n",
168                                            obd_export_nid2str(exp), index,
169                                            data->ocd_index);
170                         RETURN(-EBADF);
171                 }
172                 if (!(lsd->lsd_feature_compat & OBD_COMPAT_OST)) {
173                         /* this will only happen on the first connect */
174                         lsd->lsd_feature_compat |= OBD_COMPAT_OST;
175                         /* sync is not needed here as lut_client_add will
176                          * set exp_need_sync flag */
177                         tgt_server_data_update(env, &ofd->ofd_lut, 0);
178                 }
179         }
180         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
181                 data->ocd_brw_size = 65536;
182         } else if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
183                 data->ocd_brw_size = min(data->ocd_brw_size,
184                                          (__u32)DT_MAX_BRW_SIZE);
185                 if (data->ocd_brw_size == 0) {
186                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
187                                " ocd_version: %x ocd_grant: %d ocd_index: %u "
188                                "ocd_brw_size is unexpectedly zero, "
189                                "network data corruption?"
190                                "Refusing connection of this client\n",
191                                exp->exp_obd->obd_name,
192                                exp->exp_client_uuid.uuid,
193                                exp, data->ocd_connect_flags, data->ocd_version,
194                                data->ocd_grant, data->ocd_index);
195                         RETURN(-EPROTO);
196                 }
197         }
198
199         if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
200                 __u32 cksum_types = data->ocd_cksum_types;
201
202                 /* The client set in ocd_cksum_types the checksum types it
203                  * supports. We have to mask off the algorithms that we don't
204                  * support */
205                 data->ocd_cksum_types &= cksum_types_supported_server();
206
207                 if (unlikely(data->ocd_cksum_types == 0)) {
208                         CERROR("%s: Connect with checksum support but no "
209                                "ocd_cksum_types is set\n",
210                                exp->exp_obd->obd_name);
211                         RETURN(-EPROTO);
212                 }
213
214                 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
215                        "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
216                        cksum_types, data->ocd_cksum_types);
217         } else {
218                 /* This client does not support OBD_CONNECT_CKSUM
219                  * fall back to CRC32 */
220                 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
221                        "OBD_CONNECT_CKSUM, CRC32 will be used\n",
222                        exp->exp_obd->obd_name, obd_export_nid2str(exp));
223         }
224
225         if (data->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
226                 data->ocd_maxbytes = ofd->ofd_dt_conf.ddp_maxbytes;
227
228         if (OCD_HAS_FLAG(data, PINGLESS)) {
229                 if (ptlrpc_pinger_suppress_pings()) {
230                         spin_lock(&exp->exp_obd->obd_dev_lock);
231                         list_del_init(&exp->exp_obd_chain_timed);
232                         spin_unlock(&exp->exp_obd->obd_dev_lock);
233                 } else {
234                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
235                 }
236         }
237
238         RETURN(0);
239 }
240
241 static int ofd_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
242                              struct obd_device *obd, struct obd_uuid *cluuid,
243                              struct obd_connect_data *data, void *localdata)
244 {
245         struct ofd_device       *ofd;
246         int                      rc;
247
248         ENTRY;
249
250         if (exp == NULL || obd == NULL || cluuid == NULL)
251                 RETURN(-EINVAL);
252
253         ofd = ofd_dev(obd->obd_lu_dev);
254
255         rc = ofd_parse_connect_data(env, exp, data, false);
256         if (rc == 0)
257                 ofd_export_stats_init(ofd, exp, localdata);
258
259         RETURN(rc);
260 }
261
262 static int ofd_obd_connect(const struct lu_env *env, struct obd_export **_exp,
263                            struct obd_device *obd, struct obd_uuid *cluuid,
264                            struct obd_connect_data *data, void *localdata)
265 {
266         struct obd_export       *exp;
267         struct ofd_device       *ofd;
268         struct lustre_handle     conn = { 0 };
269         int                      rc;
270         ENTRY;
271
272         if (_exp == NULL || obd == NULL || cluuid == NULL)
273                 RETURN(-EINVAL);
274
275         ofd = ofd_dev(obd->obd_lu_dev);
276
277         rc = class_connect(&conn, obd, cluuid);
278         if (rc)
279                 RETURN(rc);
280
281         exp = class_conn2export(&conn);
282         LASSERT(exp != NULL);
283
284         rc = ofd_parse_connect_data(env, exp, data, true);
285         if (rc)
286                 GOTO(out, rc);
287
288         if (obd->obd_replayable) {
289                 struct tg_export_data *ted = &exp->exp_target_data;
290
291                 memcpy(ted->ted_lcd->lcd_uuid, cluuid,
292                        sizeof(ted->ted_lcd->lcd_uuid));
293                 rc = tgt_client_new(env, exp);
294                 if (rc != 0)
295                         GOTO(out, rc);
296                 ofd_export_stats_init(ofd, exp, localdata);
297         }
298
299         CDEBUG(D_HA, "%s: get connection from MDS %d\n", obd->obd_name,
300                data ? data->ocd_group : -1);
301
302 out:
303         if (rc != 0) {
304                 class_disconnect(exp);
305                 *_exp = NULL;
306         } else {
307                 *_exp = exp;
308         }
309         RETURN(rc);
310 }
311
312 static int ofd_obd_disconnect(struct obd_export *exp)
313 {
314         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
315         struct lu_env            env;
316         int                      rc;
317
318         ENTRY;
319
320         LASSERT(exp);
321         class_export_get(exp);
322
323         if (!(exp->exp_flags & OBD_OPT_FORCE))
324                 ofd_grant_sanity_check(ofd_obd(ofd), __FUNCTION__);
325
326         rc = server_disconnect_export(exp);
327
328         ofd_grant_discard(exp);
329
330         /* Do not erase record for recoverable client. */
331         if (exp->exp_obd->obd_replayable &&
332             (!exp->exp_obd->obd_fail || exp->exp_failed)) {
333                 rc = lu_env_init(&env, LCT_DT_THREAD);
334                 if (rc)
335                         GOTO(out, rc);
336
337                 tgt_client_del(&env, exp);
338                 lu_env_fini(&env);
339         }
340 out:
341         class_export_put(exp);
342         RETURN(rc);
343 }
344
345 static int ofd_init_export(struct obd_export *exp)
346 {
347         int rc;
348
349         spin_lock_init(&exp->exp_filter_data.fed_lock);
350         CFS_INIT_LIST_HEAD(&exp->exp_filter_data.fed_mod_list);
351         atomic_set(&exp->exp_filter_data.fed_soft_sync_count, 0);
352         spin_lock(&exp->exp_lock);
353         exp->exp_connecting = 1;
354         spin_unlock(&exp->exp_lock);
355
356         /* self-export doesn't need client data and ldlm initialization */
357         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
358                                      &exp->exp_client_uuid)))
359                 return 0;
360
361         rc = tgt_client_alloc(exp);
362         if (rc == 0)
363                 ldlm_init_export(exp);
364         if (rc)
365                 CERROR("%s: Can't initialize export: rc %d\n",
366                        exp->exp_obd->obd_name, rc);
367         return rc;
368 }
369
370 static int ofd_destroy_export(struct obd_export *exp)
371 {
372         struct ofd_device *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
373
374         if (exp->exp_filter_data.fed_pending)
375                 CERROR("%s: cli %s/%p has %lu pending on destroyed export"
376                        "\n", exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
377                        exp, exp->exp_filter_data.fed_pending);
378
379         target_destroy_export(exp);
380
381         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
382                                      &exp->exp_client_uuid)))
383                 return 0;
384
385         ldlm_destroy_export(exp);
386         tgt_client_free(exp);
387
388         ofd_fmd_cleanup(exp);
389
390         /*
391          * discard grants once we're sure no more
392          * interaction with the client is possible
393          */
394         ofd_grant_discard(exp);
395         ofd_fmd_cleanup(exp);
396
397         if (exp_connect_flags(exp) & OBD_CONNECT_GRANT_SHRINK) {
398                 if (ofd->ofd_tot_granted_clients > 0)
399                         ofd->ofd_tot_granted_clients --;
400         }
401
402         if (!(exp->exp_flags & OBD_OPT_FORCE))
403                 ofd_grant_sanity_check(exp->exp_obd, __FUNCTION__);
404
405         LASSERT(cfs_list_empty(&exp->exp_filter_data.fed_mod_list));
406         return 0;
407 }
408
409 int ofd_postrecov(const struct lu_env *env, struct ofd_device *ofd)
410 {
411         struct lu_device *ldev = &ofd->ofd_dt_dev.dd_lu_dev;
412
413         CDEBUG(D_HA, "%s: recovery is over\n", ofd_obd(ofd)->obd_name);
414         return ldev->ld_ops->ldo_recovery_complete(env, ldev);
415 }
416
417 int ofd_obd_postrecov(struct obd_device *obd)
418 {
419         struct lu_env            env;
420         struct lu_device        *ldev = obd->obd_lu_dev;
421         int                      rc;
422
423         ENTRY;
424
425         rc = lu_env_init(&env, LCT_DT_THREAD);
426         if (rc)
427                 RETURN(rc);
428         ofd_info_init(&env, obd->obd_self_export);
429
430         rc = ofd_postrecov(&env, ofd_dev(ldev));
431
432         lu_env_fini(&env);
433         RETURN(rc);
434 }
435
436 /* This is not called from request handler, check ofd_set_info_hdl() instead
437  * this OBD functions is only used by class_notify_sptlrpc_conf() locally
438  * by direct obd_set_info_async() call */
439 static int ofd_set_info_async(const struct lu_env *env, struct obd_export *exp,
440                               __u32 keylen, void *key, __u32 vallen, void *val,
441                               struct ptlrpc_request_set *set)
442 {
443         int rc = 0;
444
445         ENTRY;
446
447         if (exp->exp_obd == NULL) {
448                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
449                 RETURN(-EINVAL);
450         }
451
452         if (KEY_IS(KEY_SPTLRPC_CONF)) {
453                 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
454         } else {
455                 CERROR("%s: Unsupported key %s\n",
456                        exp->exp_obd->obd_name, (char*)key);
457                 rc = -EOPNOTSUPP;
458         }
459         RETURN(rc);
460 }
461
462 /* used by nrs_orr_range_fill_physical() in ptlrpc, see LU-3239 */
463 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
464                         __u32 keylen, void *key, __u32 *vallen, void *val,
465                         struct lov_stripe_md *lsm)
466 {
467         struct ofd_thread_info          *info;
468         struct ofd_device               *ofd;
469         struct ll_fiemap_info_key       *fm_key = key;
470         struct ll_user_fiemap           *fiemap = val;
471         int                              rc = 0;
472
473         ENTRY;
474
475         if (exp->exp_obd == NULL) {
476                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
477                 RETURN(-EINVAL);
478         }
479
480         ofd = ofd_exp(exp);
481
482         if (KEY_IS(KEY_FIEMAP)) {
483                 info = ofd_info_init(env, exp);
484
485                 rc = ostid_to_fid(&info->fti_fid, &fm_key->oa.o_oi,
486                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
487                 if (rc != 0)
488                         RETURN(rc);
489
490                 rc = ofd_fiemap_get(env, ofd, &info->fti_fid, fiemap);
491         } else {
492                 CERROR("%s: not supported key %s\n", ofd_name(ofd), (char*)key);
493                 rc = -EOPNOTSUPP;
494         }
495
496         RETURN(rc);
497 }
498
499 /** helper function for statfs, also used by grant code */
500 int ofd_statfs_internal(const struct lu_env *env, struct ofd_device *ofd,
501                         struct obd_statfs *osfs, __u64 max_age, int *from_cache)
502 {
503         int rc;
504
505         spin_lock(&ofd->ofd_osfs_lock);
506         if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
507                 obd_size unstable;
508
509                 /* statfs data are too old, get up-to-date one.
510                  * we must be cautious here since multiple threads might be
511                  * willing to update statfs data concurrently and we must
512                  * grant that cached statfs data are always consistent */
513
514                 if (ofd->ofd_statfs_inflight == 0)
515                         /* clear inflight counter if no users, although it would
516                          * take a while to overflow this 64-bit counter ... */
517                         ofd->ofd_osfs_inflight = 0;
518                 /* notify ofd_grant_commit() that we want to track writes
519                  * completed as of now */
520                 ofd->ofd_statfs_inflight++;
521                 /* record value of inflight counter before running statfs to
522                  * compute the diff once statfs is completed */
523                 unstable = ofd->ofd_osfs_inflight;
524                 spin_unlock(&ofd->ofd_osfs_lock);
525
526                 /* statfs can sleep ... hopefully not for too long since we can
527                  * call it fairly often as space fills up */
528                 rc = dt_statfs(env, ofd->ofd_osd, osfs);
529                 if (unlikely(rc))
530                         return rc;
531
532                 spin_lock(&ofd->ofd_grant_lock);
533                 spin_lock(&ofd->ofd_osfs_lock);
534                 /* calculate how much space was written while we released the
535                  * ofd_osfs_lock */
536                 unstable = ofd->ofd_osfs_inflight - unstable;
537                 ofd->ofd_osfs_unstable = 0;
538                 if (unstable) {
539                         /* some writes completed while we were running statfs
540                          * w/o the ofd_osfs_lock. Those ones got added to
541                          * the cached statfs data that we are about to crunch.
542                          * Take them into account in the new statfs data */
543                         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
544                                                unstable >> ofd->ofd_blockbits);
545                         /* However, we don't really know if those writes got
546                          * accounted in the statfs call, so tell
547                          * ofd_grant_space_left() there is some uncertainty
548                          * on the accounting of those writes.
549                          * The purpose is to prevent spurious error messages in
550                          * ofd_grant_space_left() since those writes might be
551                          * accounted twice. */
552                         ofd->ofd_osfs_unstable += unstable;
553                 }
554                 /* similarly, there is some uncertainty on write requests
555                  * between prepare & commit */
556                 ofd->ofd_osfs_unstable += ofd->ofd_tot_pending;
557                 spin_unlock(&ofd->ofd_grant_lock);
558
559                 /* finally udpate cached statfs data */
560                 ofd->ofd_osfs = *osfs;
561                 ofd->ofd_osfs_age = cfs_time_current_64();
562
563                 ofd->ofd_statfs_inflight--; /* stop tracking */
564                 if (ofd->ofd_statfs_inflight == 0)
565                         ofd->ofd_osfs_inflight = 0;
566                 spin_unlock(&ofd->ofd_osfs_lock);
567
568                 if (from_cache)
569                         *from_cache = 0;
570         } else {
571                 /* use cached statfs data */
572                 *osfs = ofd->ofd_osfs;
573                 spin_unlock(&ofd->ofd_osfs_lock);
574                 if (from_cache)
575                         *from_cache = 1;
576         }
577         return 0;
578 }
579
580 int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
581                struct obd_statfs *osfs, __u64 max_age, __u32 flags)
582 {
583         struct obd_device       *obd = class_exp2obd(exp);
584         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
585         int                      rc;
586
587         ENTRY;
588
589         rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
590         if (unlikely(rc))
591                 GOTO(out, rc);
592
593         /* at least try to account for cached pages.  its still racy and
594          * might be under-reporting if clients haven't announced their
595          * caches with brw recently */
596
597         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
598                " pending "LPU64" free "LPU64" avail "LPU64"\n",
599                ofd->ofd_tot_dirty, ofd->ofd_tot_granted, ofd->ofd_tot_pending,
600                osfs->os_bfree << ofd->ofd_blockbits,
601                osfs->os_bavail << ofd->ofd_blockbits);
602
603         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
604                                  ((ofd->ofd_tot_dirty + ofd->ofd_tot_pending +
605                                    osfs->os_bsize - 1) >> ofd->ofd_blockbits));
606
607         /* The QoS code on the MDS does not care about space reserved for
608          * precreate, so take it out. */
609         if (exp_connect_flags(exp) & OBD_CONNECT_MDS) {
610                 struct filter_export_data *fed;
611
612                 fed = &obd->obd_self_export->exp_filter_data;
613                 osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
614                                          fed->fed_grant >> ofd->ofd_blockbits);
615         }
616
617         ofd_grant_sanity_check(obd, __FUNCTION__);
618         CDEBUG(D_CACHE, LPU64" blocks: "LPU64" free, "LPU64" avail; "
619                LPU64" objects: "LPU64" free; state %x\n",
620                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
621                osfs->os_files, osfs->os_ffree, osfs->os_state);
622
623         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
624                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
625                 osfs->os_ffree = 0;
626
627         /* OS_STATE_READONLY can be set by OSD already */
628         if (ofd->ofd_raid_degraded)
629                 osfs->os_state |= OS_STATE_DEGRADED;
630
631         if (obd->obd_self_export != exp && ofd_grant_compat(exp, ofd)) {
632                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
633                  * should not see a block size > page size, otherwise
634                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
635                  * block size which is the biggest block size known to work
636                  * with all client's page size. */
637                 osfs->os_blocks <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
638                 osfs->os_bfree  <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
639                 osfs->os_bavail <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
640                 osfs->os_bsize    = 1 << COMPAT_BSIZE_SHIFT;
641         }
642
643         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
644                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
645                 osfs->os_bfree = osfs->os_bavail = 2;
646
647         EXIT;
648 out:
649         return rc;
650 }
651
652 /* needed by echo client only for now, RPC handler uses ofd_setattr_hdl() */
653 int ofd_echo_setattr(const struct lu_env *env, struct obd_export *exp,
654                      struct obd_info *oinfo, struct obd_trans_info *oti)
655 {
656         struct ofd_thread_info  *info;
657         struct ofd_device       *ofd = ofd_exp(exp);
658         struct ldlm_namespace   *ns = ofd->ofd_namespace;
659         struct ldlm_resource    *res;
660         struct ofd_object       *fo;
661         struct obdo             *oa = oinfo->oi_oa;
662         struct lu_fid           *fid = &oa->o_oi.oi_fid;
663         struct filter_fid       *ff = NULL;
664         int                      rc = 0;
665
666         ENTRY;
667
668         info = ofd_info_init(env, exp);
669
670         ost_fid_build_resid(fid, &info->fti_resid);
671
672         /* This would be very bad - accidentally truncating a file when
673          * changing the time or similar - bug 12203. */
674         if (oa->o_valid & OBD_MD_FLSIZE &&
675             oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
676                 static char mdsinum[48];
677
678                 if (oa->o_valid & OBD_MD_FLFID)
679                         snprintf(mdsinum, sizeof(mdsinum) - 1,
680                                  "of parent "DFID, oa->o_parent_seq,
681                                  oa->o_parent_oid, 0);
682                 else
683                         mdsinum[0] = '\0';
684
685                 CERROR("%s: setattr from %s trying to truncate object "DFID
686                        " %s\n", ofd_name(ofd), obd_export_nid2str(exp),
687                        PFID(fid), mdsinum);
688                 GOTO(out, rc = -EPERM);
689         }
690
691         fo = ofd_object_find_exists(env, ofd, fid);
692         if (IS_ERR(fo)) {
693                 CERROR("%s: can't find object "DFID"\n",
694                        ofd_name(ofd), PFID(fid));
695                 GOTO(out, rc = PTR_ERR(fo));
696         }
697
698         la_from_obdo(&info->fti_attr, oa, oa->o_valid);
699         info->fti_attr.la_valid &= ~LA_TYPE;
700
701         if (oa->o_valid & OBD_MD_FLFID) {
702                 ff = &info->fti_mds_fid;
703                 ofd_prepare_fidea(ff, oa);
704         }
705
706         /* setting objects attributes (including owner/group) */
707         rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
708         if (rc)
709                 GOTO(out_unlock, rc);
710
711         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, NULL, 1);
712         EXIT;
713 out_unlock:
714         ofd_object_put(env, fo);
715 out:
716         if (rc == 0) {
717                 /* we do not call this before to avoid lu_object_find() in
718                  *  ->lvbo_update() holding another reference on the object.
719                  * otherwise concurrent destroy can make the object unavailable
720                  * for 2nd lu_object_find() waiting for the first reference
721                  * to go... deadlock! */
722                 res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
723                 if (res != NULL) {
724                         ldlm_res_lvbo_update(res, NULL, 0);
725                         ldlm_resource_putref(res);
726                 }
727         }
728
729         return rc;
730 }
731
732 int ofd_destroy_by_fid(const struct lu_env *env, struct ofd_device *ofd,
733                        const struct lu_fid *fid, int orphan)
734 {
735         struct ofd_thread_info  *info = ofd_info(env);
736         struct lustre_handle     lockh;
737         __u64                    flags = LDLM_FL_AST_DISCARD_DATA;
738         __u64                    rc = 0;
739         ldlm_policy_data_t       policy = {
740                                         .l_extent = { 0, OBD_OBJECT_EOF }
741                                  };
742         struct ofd_object       *fo;
743
744         ENTRY;
745
746         fo = ofd_object_find_exists(env, ofd, fid);
747         if (IS_ERR(fo))
748                 RETURN(PTR_ERR(fo));
749
750         /* Tell the clients that the object is gone now and that they should
751          * throw away any cached pages. */
752         ost_fid_build_resid(fid, &info->fti_resid);
753         rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
754                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
755                                     ldlm_blocking_ast, ldlm_completion_ast,
756                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
757
758         /* We only care about the side-effects, just drop the lock. */
759         if (rc == ELDLM_OK)
760                 ldlm_lock_decref(&lockh, LCK_PW);
761
762         LASSERT(fo != NULL);
763
764         rc = ofd_object_destroy(env, fo, orphan);
765         EXIT;
766
767         ofd_object_put(env, fo);
768         RETURN(rc);
769 }
770
771 /* needed by echo client only for now, RPC handler uses ofd_destroy_hdl() */
772 int ofd_echo_destroy(const struct lu_env *env, struct obd_export *exp,
773                      struct obdo *oa, struct lov_stripe_md *md,
774                      struct obd_trans_info *oti, struct obd_export *md_exp,
775                      void *capa)
776 {
777         struct ofd_device       *ofd = ofd_exp(exp);
778         struct lu_fid           *fid = &oa->o_oi.oi_fid;
779         int                      rc = 0;
780
781         ENTRY;
782
783         ofd_info_init(env, exp);
784
785         CDEBUG(D_HA, "%s: Destroy object "DFID"\n", ofd_name(ofd), PFID(fid));
786
787         rc = ofd_destroy_by_fid(env, ofd, fid, 0);
788         if (rc == -ENOENT) {
789                 CDEBUG(D_INODE, "%s: destroying non-existent object "DFID"\n",
790                        ofd_name(ofd), PFID(fid));
791                 GOTO(out, rc);
792         } else if (rc != 0) {
793                 CERROR("%s: error destroying object "DFID": %d\n",
794                        ofd_name(ofd), PFID(fid), rc);
795                 GOTO(out, rc);
796         }
797         EXIT;
798 out:
799         return rc;
800 }
801
802 /* needed by echo client only for now, RPC handler uses ofd_create_hdl()
803  * It is much simpler and just create objects */
804 int ofd_echo_create(const struct lu_env *env, struct obd_export *exp,
805                     struct obdo *oa, struct lov_stripe_md **ea,
806                     struct obd_trans_info *oti)
807 {
808         struct ofd_device       *ofd = ofd_exp(exp);
809         struct ofd_thread_info  *info;
810         obd_seq                  seq = ostid_seq(&oa->o_oi);
811         struct ofd_seq          *oseq;
812         int                      rc = 0, diff = 1;
813         obd_id                   next_id;
814         int                      count;
815
816         ENTRY;
817
818         info = ofd_info_init(env, exp);
819
820         LASSERT(seq == FID_SEQ_ECHO);
821         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
822
823         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
824
825         oseq = ofd_seq_load(env, ofd, seq);
826         if (IS_ERR(oseq)) {
827                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
828                        ofd_name(ofd), seq, PTR_ERR(oseq));
829                 RETURN(-EINVAL);
830         }
831
832         mutex_lock(&oseq->os_create_lock);
833         rc = ofd_grant_create(env, ofd_obd(ofd)->obd_self_export, &diff);
834         if (rc < 0) {
835                 CDEBUG(D_HA, "%s: failed to acquire grant space for "
836                        "precreate (%d): rc = %d\n", ofd_name(ofd), diff, rc);
837                 diff = 0;
838                 GOTO(out, rc);
839         }
840
841         next_id = ofd_seq_last_oid(oseq) + 1;
842         count = ofd_precreate_batch(ofd, diff);
843
844         rc = ofd_precreate_objects(env, ofd, next_id, oseq, count, 0);
845         if (rc < 0) {
846                 CERROR("%s: unable to precreate: rc = %d\n",
847                        ofd_name(ofd), rc);
848         } else {
849                 ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
850                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
851                 rc = 0;
852         }
853
854         ofd_grant_commit(env, ofd_obd(ofd)->obd_self_export, rc);
855 out:
856         mutex_unlock(&oseq->os_create_lock);
857         if (rc == 0 && ea != NULL) {
858                 struct lov_stripe_md *lsm = *ea;
859
860                 lsm->lsm_oi = oa->o_oi;
861         }
862         ofd_seq_put(env, oseq);
863         RETURN(rc);
864 }
865
866 /* needed by echo client only for now, RPC handler uses ofd_getattr_hdl() */
867 int ofd_echo_getattr(const struct lu_env *env, struct obd_export *exp,
868                      struct obd_info *oinfo)
869 {
870         struct ofd_device       *ofd = ofd_exp(exp);
871         struct ofd_thread_info  *info;
872         struct lu_fid           *fid = &oinfo->oi_oa->o_oi.oi_fid;
873         struct ofd_object       *fo;
874         int                      rc = 0;
875
876         ENTRY;
877
878         info = ofd_info_init(env, exp);
879
880         fo = ofd_object_find_exists(env, ofd, fid);
881         if (IS_ERR(fo))
882                 GOTO(out, rc = PTR_ERR(fo));
883
884         LASSERT(fo != NULL);
885         rc = ofd_attr_get(env, fo, &info->fti_attr);
886         oinfo->oi_oa->o_valid = OBD_MD_FLID;
887         if (rc == 0) {
888                 __u64 curr_version;
889
890                 obdo_from_la(oinfo->oi_oa, &info->fti_attr,
891                              OFD_VALID_FLAGS | LA_UID | LA_GID);
892
893                 /* Store object version in reply */
894                 curr_version = dt_version_get(env, ofd_object_child(fo));
895                 if ((__s64)curr_version != -EOPNOTSUPP) {
896                         oinfo->oi_oa->o_valid |= OBD_MD_FLDATAVERSION;
897                         oinfo->oi_oa->o_data_version = curr_version;
898                 }
899         }
900
901         ofd_object_put(env, fo);
902 out:
903         RETURN(rc);
904 }
905
906 static int ofd_ioc_get_obj_version(const struct lu_env *env,
907                                    struct ofd_device *ofd, void *karg)
908 {
909         struct obd_ioctl_data *data = karg;
910         struct lu_fid          fid;
911         struct ofd_object     *fo;
912         dt_obj_version_t       version;
913         int                    rc = 0;
914
915         ENTRY;
916
917         if (data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
918                 GOTO(out, rc = -EINVAL);
919
920         if (data->ioc_inlbuf1 != NULL && data->ioc_inllen1 == sizeof(fid)) {
921                 fid = *(struct lu_fid *)data->ioc_inlbuf1;
922         } else if (data->ioc_inlbuf3 != NULL &&
923                    data->ioc_inllen3 == sizeof(__u64) &&
924                    data->ioc_inlbuf4 != NULL &&
925                    data->ioc_inllen4 == sizeof(__u64)) {
926                 struct ost_id ostid;
927
928                 ostid_set_seq(&ostid, *(__u64 *)data->ioc_inlbuf4);
929                 ostid_set_id(&ostid, *(__u64 *)data->ioc_inlbuf3);
930                 rc = ostid_to_fid(&fid, &ostid,
931                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
932                 if (rc != 0)
933                         GOTO(out, rc);
934         } else {
935                 GOTO(out, rc = -EINVAL);
936         }
937
938         if (!fid_is_sane(&fid))
939                 GOTO(out, rc = -EINVAL);
940
941         fo = ofd_object_find(env, ofd, &fid);
942         if (IS_ERR(fo))
943                 GOTO(out, rc = PTR_ERR(fo));
944
945         if (!ofd_object_exists(fo))
946                 GOTO(out_fo, rc = -ENOENT);
947
948         if (lu_object_remote(&fo->ofo_obj.do_lu))
949                 GOTO(out_fo, rc = -EREMOTE);
950
951         version = dt_version_get(env, ofd_object_child(fo));
952         if (version == 0)
953                 GOTO(out_fo, rc = -EIO);
954
955         *(dt_obj_version_t *)data->ioc_inlbuf2 = version;
956
957         EXIT;
958 out_fo:
959         ofd_object_put(env, fo);
960 out:
961         return rc;
962 }
963
964 int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
965                   void *karg, void *uarg)
966 {
967         struct lu_env            env;
968         struct ofd_device       *ofd = ofd_exp(exp);
969         struct obd_device       *obd = ofd_obd(ofd);
970         int                      rc;
971
972         ENTRY;
973
974         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
975         rc = lu_env_init(&env, LCT_DT_THREAD);
976         if (rc)
977                 RETURN(rc);
978
979         switch (cmd) {
980         case OBD_IOC_ABORT_RECOVERY:
981                 CERROR("%s: aborting recovery\n", obd->obd_name);
982                 target_stop_recovery_thread(obd);
983                 break;
984         case OBD_IOC_SYNC:
985                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
986                 rc = dt_sync(&env, ofd->ofd_osd);
987                 break;
988         case OBD_IOC_SET_READONLY:
989                 rc = dt_sync(&env, ofd->ofd_osd);
990                 if (rc == 0)
991                         rc = dt_ro(&env, ofd->ofd_osd);
992                 break;
993         case OBD_IOC_START_LFSCK: {
994                 struct obd_ioctl_data *data = karg;
995                 struct lfsck_start_param lsp;
996
997                 if (unlikely(data == NULL)) {
998                         rc = -EINVAL;
999                         break;
1000                 }
1001
1002                 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
1003                 lsp.lsp_namespace = ofd->ofd_namespace;
1004                 rc = lfsck_start(&env, ofd->ofd_osd, &lsp);
1005                 break;
1006         }
1007         case OBD_IOC_STOP_LFSCK: {
1008                 rc = lfsck_stop(&env, ofd->ofd_osd, false);
1009                 break;
1010         }
1011         case OBD_IOC_GET_OBJ_VERSION:
1012                 rc = ofd_ioc_get_obj_version(&env, ofd, karg);
1013                 break;
1014         default:
1015                 CERROR("%s: not supported cmd = %d\n", obd->obd_name, cmd);
1016                 rc = -ENOTTY;
1017         }
1018
1019         lu_env_fini(&env);
1020         RETURN(rc);
1021 }
1022
1023 static int ofd_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1024 {
1025         int rc = 0;
1026
1027         ENTRY;
1028
1029         switch(stage) {
1030         case OBD_CLEANUP_EARLY:
1031                 break;
1032         case OBD_CLEANUP_EXPORTS:
1033                 target_cleanup_recovery(obd);
1034                 break;
1035         }
1036         RETURN(rc);
1037 }
1038
1039 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1040 {
1041         ofd_fmd_expire(exp);
1042         return 0;
1043 }
1044
1045 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1046 {
1047         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1048         struct ofd_thread_info  *info;
1049         struct lu_env            env;
1050 #ifdef USE_HEALTH_CHECK_WRITE
1051         struct thandle          *th;
1052 #endif
1053         int                      rc = 0;
1054
1055         /* obd_proc_read_health pass NULL env, we need real one */
1056         rc = lu_env_init(&env, LCT_DT_THREAD);
1057         if (rc)
1058                 RETURN(rc);
1059
1060         info = ofd_info_init(&env, NULL);
1061         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1062         if (unlikely(rc))
1063                 GOTO(out, rc);
1064
1065         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1066                 GOTO(out, rc = -EROFS);
1067
1068 #ifdef USE_HEALTH_CHECK_WRITE
1069         OBD_ALLOC(info->fti_buf.lb_buf, PAGE_CACHE_SIZE);
1070         if (info->fti_buf.lb_buf == NULL)
1071                 GOTO(out, rc = -ENOMEM);
1072
1073         info->fti_buf.lb_len = PAGE_CACHE_SIZE;
1074         info->fti_off = 0;
1075
1076         th = dt_trans_create(&env, ofd->ofd_osd);
1077         if (IS_ERR(th))
1078                 GOTO(out, rc = PTR_ERR(th));
1079
1080         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1081                                      info->fti_buf.lb_len, info->fti_off, th);
1082         if (rc == 0) {
1083                 th->th_sync = 1; /* sync IO is needed */
1084                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1085                 if (rc == 0)
1086                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1087                                              &info->fti_buf, &info->fti_off,
1088                                              th);
1089         }
1090         dt_trans_stop(&env, ofd->ofd_osd, th);
1091
1092         OBD_FREE(info->fti_buf.lb_buf, PAGE_CACHE_SIZE);
1093
1094         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1095 #endif
1096 out:
1097         lu_env_fini(&env);
1098         return !!rc;
1099 }
1100
1101 /*
1102  * Handle quota control requests to consult current usage/limit.
1103  *
1104  * \param obd - is the obd device associated with the ofd
1105  * \param exp - is the client's export
1106  * \param oqctl - is the obd_quotactl request to be processed
1107  */
1108 static int ofd_quotactl(struct obd_device *obd, struct obd_export *exp,
1109                         struct obd_quotactl *oqctl)
1110 {
1111         struct ofd_device  *ofd = ofd_dev(obd->obd_lu_dev);
1112         struct lu_env       env;
1113         int                 rc;
1114         ENTRY;
1115
1116         /* report success for quota on/off for interoperability with current MDT
1117          * stack */
1118         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1119                 RETURN(0);
1120
1121         rc = lu_env_init(&env, LCT_DT_THREAD);
1122         if (rc)
1123                 RETURN(rc);
1124
1125         rc = lquotactl_slv(&env, ofd->ofd_osd, oqctl);
1126         lu_env_fini(&env);
1127
1128         RETURN(rc);
1129 }
1130
1131 struct obd_ops ofd_obd_ops = {
1132         .o_owner                = THIS_MODULE,
1133         .o_connect              = ofd_obd_connect,
1134         .o_reconnect            = ofd_obd_reconnect,
1135         .o_disconnect           = ofd_obd_disconnect,
1136         .o_create               = ofd_echo_create,
1137         .o_setattr              = ofd_echo_setattr,
1138         .o_preprw               = ofd_preprw,
1139         .o_commitrw             = ofd_commitrw,
1140         .o_destroy              = ofd_echo_destroy,
1141         .o_init_export          = ofd_init_export,
1142         .o_destroy_export       = ofd_destroy_export,
1143         .o_postrecov            = ofd_obd_postrecov,
1144         .o_getattr              = ofd_echo_getattr,
1145         .o_iocontrol            = ofd_iocontrol,
1146         .o_precleanup           = ofd_precleanup,
1147         .o_ping                 = ofd_ping,
1148         .o_health_check         = ofd_health_check,
1149         .o_quotactl             = ofd_quotactl,
1150         .o_set_info_async       = ofd_set_info_async,
1151         .o_get_info             = ofd_get_info,
1152 };