Whamcloud - gitweb
e3491db4560a89aac5ace956de80eade67d1722b
[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 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 = 0;
504         ENTRY;
505
506         down_read(&ofd->ofd_lastid_rwsem);
507         /* Currently, for safe, we do not distinguish which LAST_ID is broken,
508          * we may do that in the future.
509          * Return -ENOSPC until the LAST_ID rebuilt. */
510         if (unlikely(ofd->ofd_lastid_rebuilding))
511                 GOTO(out, rc = -ENOSPC);
512
513         spin_lock(&ofd->ofd_osfs_lock);
514         if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
515                 obd_size unstable;
516
517                 /* statfs data are too old, get up-to-date one.
518                  * we must be cautious here since multiple threads might be
519                  * willing to update statfs data concurrently and we must
520                  * grant that cached statfs data are always consistent */
521
522                 if (ofd->ofd_statfs_inflight == 0)
523                         /* clear inflight counter if no users, although it would
524                          * take a while to overflow this 64-bit counter ... */
525                         ofd->ofd_osfs_inflight = 0;
526                 /* notify ofd_grant_commit() that we want to track writes
527                  * completed as of now */
528                 ofd->ofd_statfs_inflight++;
529                 /* record value of inflight counter before running statfs to
530                  * compute the diff once statfs is completed */
531                 unstable = ofd->ofd_osfs_inflight;
532                 spin_unlock(&ofd->ofd_osfs_lock);
533
534                 /* statfs can sleep ... hopefully not for too long since we can
535                  * call it fairly often as space fills up */
536                 rc = dt_statfs(env, ofd->ofd_osd, osfs);
537                 if (unlikely(rc))
538                         GOTO(out, rc);
539
540                 spin_lock(&ofd->ofd_grant_lock);
541                 spin_lock(&ofd->ofd_osfs_lock);
542                 /* calculate how much space was written while we released the
543                  * ofd_osfs_lock */
544                 unstable = ofd->ofd_osfs_inflight - unstable;
545                 ofd->ofd_osfs_unstable = 0;
546                 if (unstable) {
547                         /* some writes completed while we were running statfs
548                          * w/o the ofd_osfs_lock. Those ones got added to
549                          * the cached statfs data that we are about to crunch.
550                          * Take them into account in the new statfs data */
551                         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
552                                                unstable >> ofd->ofd_blockbits);
553                         /* However, we don't really know if those writes got
554                          * accounted in the statfs call, so tell
555                          * ofd_grant_space_left() there is some uncertainty
556                          * on the accounting of those writes.
557                          * The purpose is to prevent spurious error messages in
558                          * ofd_grant_space_left() since those writes might be
559                          * accounted twice. */
560                         ofd->ofd_osfs_unstable += unstable;
561                 }
562                 /* similarly, there is some uncertainty on write requests
563                  * between prepare & commit */
564                 ofd->ofd_osfs_unstable += ofd->ofd_tot_pending;
565                 spin_unlock(&ofd->ofd_grant_lock);
566
567                 /* finally udpate cached statfs data */
568                 ofd->ofd_osfs = *osfs;
569                 ofd->ofd_osfs_age = cfs_time_current_64();
570
571                 ofd->ofd_statfs_inflight--; /* stop tracking */
572                 if (ofd->ofd_statfs_inflight == 0)
573                         ofd->ofd_osfs_inflight = 0;
574                 spin_unlock(&ofd->ofd_osfs_lock);
575
576                 if (from_cache)
577                         *from_cache = 0;
578         } else {
579                 /* use cached statfs data */
580                 *osfs = ofd->ofd_osfs;
581                 spin_unlock(&ofd->ofd_osfs_lock);
582                 if (from_cache)
583                         *from_cache = 1;
584         }
585
586         GOTO(out, rc);
587
588 out:
589         up_read(&ofd->ofd_lastid_rwsem);
590
591         return rc;
592 }
593
594 int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
595                struct obd_statfs *osfs, __u64 max_age, __u32 flags)
596 {
597         struct obd_device       *obd = class_exp2obd(exp);
598         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
599         int                      rc;
600
601         ENTRY;
602
603         rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
604         if (unlikely(rc))
605                 GOTO(out, rc);
606
607         /* at least try to account for cached pages.  its still racy and
608          * might be under-reporting if clients haven't announced their
609          * caches with brw recently */
610
611         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
612                " pending "LPU64" free "LPU64" avail "LPU64"\n",
613                ofd->ofd_tot_dirty, ofd->ofd_tot_granted, ofd->ofd_tot_pending,
614                osfs->os_bfree << ofd->ofd_blockbits,
615                osfs->os_bavail << ofd->ofd_blockbits);
616
617         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
618                                  ((ofd->ofd_tot_dirty + ofd->ofd_tot_pending +
619                                    osfs->os_bsize - 1) >> ofd->ofd_blockbits));
620
621         /* The QoS code on the MDS does not care about space reserved for
622          * precreate, so take it out. */
623         if (exp_connect_flags(exp) & OBD_CONNECT_MDS) {
624                 struct filter_export_data *fed;
625
626                 fed = &obd->obd_self_export->exp_filter_data;
627                 osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
628                                          fed->fed_grant >> ofd->ofd_blockbits);
629         }
630
631         ofd_grant_sanity_check(obd, __FUNCTION__);
632         CDEBUG(D_CACHE, LPU64" blocks: "LPU64" free, "LPU64" avail; "
633                LPU64" objects: "LPU64" free; state %x\n",
634                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
635                osfs->os_files, osfs->os_ffree, osfs->os_state);
636
637         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
638                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
639                 osfs->os_ffree = 0;
640
641         /* OS_STATE_READONLY can be set by OSD already */
642         if (ofd->ofd_raid_degraded)
643                 osfs->os_state |= OS_STATE_DEGRADED;
644
645         if (obd->obd_self_export != exp && ofd_grant_compat(exp, ofd)) {
646                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
647                  * should not see a block size > page size, otherwise
648                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
649                  * block size which is the biggest block size known to work
650                  * with all client's page size. */
651                 osfs->os_blocks <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
652                 osfs->os_bfree  <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
653                 osfs->os_bavail <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
654                 osfs->os_bsize    = 1 << COMPAT_BSIZE_SHIFT;
655         }
656
657         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
658                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
659                 osfs->os_bfree = osfs->os_bavail = 2;
660
661         EXIT;
662 out:
663         return rc;
664 }
665
666 /* needed by echo client only for now, RPC handler uses ofd_setattr_hdl() */
667 int ofd_echo_setattr(const struct lu_env *env, struct obd_export *exp,
668                      struct obd_info *oinfo, struct obd_trans_info *oti)
669 {
670         struct ofd_thread_info  *info;
671         struct ofd_device       *ofd = ofd_exp(exp);
672         struct ldlm_namespace   *ns = ofd->ofd_namespace;
673         struct ldlm_resource    *res;
674         struct ofd_object       *fo;
675         struct obdo             *oa = oinfo->oi_oa;
676         struct lu_fid           *fid = &oa->o_oi.oi_fid;
677         struct filter_fid       *ff = NULL;
678         int                      rc = 0;
679
680         ENTRY;
681
682         info = ofd_info_init(env, exp);
683
684         ost_fid_build_resid(fid, &info->fti_resid);
685
686         /* This would be very bad - accidentally truncating a file when
687          * changing the time or similar - bug 12203. */
688         if (oa->o_valid & OBD_MD_FLSIZE &&
689             oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
690                 static char mdsinum[48];
691
692                 if (oa->o_valid & OBD_MD_FLFID)
693                         snprintf(mdsinum, sizeof(mdsinum) - 1,
694                                  "of parent "DFID, oa->o_parent_seq,
695                                  oa->o_parent_oid, 0);
696                 else
697                         mdsinum[0] = '\0';
698
699                 CERROR("%s: setattr from %s trying to truncate object "DFID
700                        " %s\n", ofd_name(ofd), obd_export_nid2str(exp),
701                        PFID(fid), mdsinum);
702                 GOTO(out, rc = -EPERM);
703         }
704
705         fo = ofd_object_find_exists(env, ofd, fid);
706         if (IS_ERR(fo)) {
707                 CERROR("%s: can't find object "DFID"\n",
708                        ofd_name(ofd), PFID(fid));
709                 GOTO(out, rc = PTR_ERR(fo));
710         }
711
712         la_from_obdo(&info->fti_attr, oa, oa->o_valid);
713         info->fti_attr.la_valid &= ~LA_TYPE;
714
715         if (oa->o_valid & OBD_MD_FLFID) {
716                 ff = &info->fti_mds_fid;
717                 ofd_prepare_fidea(ff, oa);
718         }
719
720         /* setting objects attributes (including owner/group) */
721         rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
722         if (rc)
723                 GOTO(out_unlock, rc);
724
725         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, NULL, 1);
726         EXIT;
727 out_unlock:
728         ofd_object_put(env, fo);
729 out:
730         if (rc == 0) {
731                 /* we do not call this before to avoid lu_object_find() in
732                  *  ->lvbo_update() holding another reference on the object.
733                  * otherwise concurrent destroy can make the object unavailable
734                  * for 2nd lu_object_find() waiting for the first reference
735                  * to go... deadlock! */
736                 res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
737                 if (res != NULL) {
738                         ldlm_res_lvbo_update(res, NULL, 0);
739                         ldlm_resource_putref(res);
740                 }
741         }
742
743         return rc;
744 }
745
746 int ofd_destroy_by_fid(const struct lu_env *env, struct ofd_device *ofd,
747                        const struct lu_fid *fid, int orphan)
748 {
749         struct ofd_thread_info  *info = ofd_info(env);
750         struct lustre_handle     lockh;
751         __u64                    flags = LDLM_FL_AST_DISCARD_DATA;
752         __u64                    rc = 0;
753         ldlm_policy_data_t       policy = {
754                                         .l_extent = { 0, OBD_OBJECT_EOF }
755                                  };
756         struct ofd_object       *fo;
757
758         ENTRY;
759
760         fo = ofd_object_find_exists(env, ofd, fid);
761         if (IS_ERR(fo))
762                 RETURN(PTR_ERR(fo));
763
764         /* Tell the clients that the object is gone now and that they should
765          * throw away any cached pages. */
766         ost_fid_build_resid(fid, &info->fti_resid);
767         rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
768                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
769                                     ldlm_blocking_ast, ldlm_completion_ast,
770                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
771
772         /* We only care about the side-effects, just drop the lock. */
773         if (rc == ELDLM_OK)
774                 ldlm_lock_decref(&lockh, LCK_PW);
775
776         LASSERT(fo != NULL);
777
778         rc = ofd_object_destroy(env, fo, orphan);
779         EXIT;
780
781         ofd_object_put(env, fo);
782         RETURN(rc);
783 }
784
785 /* needed by echo client only for now, RPC handler uses ofd_destroy_hdl() */
786 int ofd_echo_destroy(const struct lu_env *env, struct obd_export *exp,
787                      struct obdo *oa, struct lov_stripe_md *md,
788                      struct obd_trans_info *oti, struct obd_export *md_exp,
789                      void *capa)
790 {
791         struct ofd_device       *ofd = ofd_exp(exp);
792         struct lu_fid           *fid = &oa->o_oi.oi_fid;
793         int                      rc = 0;
794
795         ENTRY;
796
797         ofd_info_init(env, exp);
798
799         CDEBUG(D_HA, "%s: Destroy object "DFID"\n", ofd_name(ofd), PFID(fid));
800
801         rc = ofd_destroy_by_fid(env, ofd, fid, 0);
802         if (rc == -ENOENT) {
803                 CDEBUG(D_INODE, "%s: destroying non-existent object "DFID"\n",
804                        ofd_name(ofd), PFID(fid));
805                 GOTO(out, rc);
806         } else if (rc != 0) {
807                 CERROR("%s: error destroying object "DFID": %d\n",
808                        ofd_name(ofd), PFID(fid), rc);
809                 GOTO(out, rc);
810         }
811         EXIT;
812 out:
813         return rc;
814 }
815
816 /* needed by echo client only for now, RPC handler uses ofd_create_hdl()
817  * It is much simpler and just create objects */
818 int ofd_echo_create(const struct lu_env *env, struct obd_export *exp,
819                     struct obdo *oa, struct lov_stripe_md **ea,
820                     struct obd_trans_info *oti)
821 {
822         struct ofd_device       *ofd = ofd_exp(exp);
823         struct ofd_thread_info  *info;
824         obd_seq                  seq = ostid_seq(&oa->o_oi);
825         struct ofd_seq          *oseq;
826         int                      rc = 0, diff = 1;
827         obd_id                   next_id;
828         int                      count;
829
830         ENTRY;
831
832         info = ofd_info_init(env, exp);
833
834         LASSERT(seq == FID_SEQ_ECHO);
835         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
836
837         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
838
839         down_read(&ofd->ofd_lastid_rwsem);
840         /* Currently, for safe, we do not distinguish which LAST_ID is broken,
841          * we may do that in the future.
842          * Return -ENOSPC until the LAST_ID rebuilt. */
843         if (unlikely(ofd->ofd_lastid_rebuilding))
844                 GOTO(out_sem, rc = -ENOSPC);
845
846         oseq = ofd_seq_load(env, ofd, seq);
847         if (IS_ERR(oseq)) {
848                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
849                        ofd_name(ofd), seq, PTR_ERR(oseq));
850                 GOTO(out_sem, rc = -EINVAL);
851         }
852
853         mutex_lock(&oseq->os_create_lock);
854         rc = ofd_grant_create(env, ofd_obd(ofd)->obd_self_export, &diff);
855         if (rc < 0) {
856                 CDEBUG(D_HA, "%s: failed to acquire grant space for "
857                        "precreate (%d): rc = %d\n", ofd_name(ofd), diff, rc);
858                 diff = 0;
859                 GOTO(out, rc);
860         }
861
862         next_id = ofd_seq_last_oid(oseq) + 1;
863         count = ofd_precreate_batch(ofd, diff);
864
865         rc = ofd_precreate_objects(env, ofd, next_id, oseq, count, 0);
866         if (rc < 0) {
867                 CERROR("%s: unable to precreate: rc = %d\n",
868                        ofd_name(ofd), rc);
869         } else {
870                 ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
871                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
872                 rc = 0;
873         }
874
875         ofd_grant_commit(env, ofd_obd(ofd)->obd_self_export, rc);
876 out:
877         mutex_unlock(&oseq->os_create_lock);
878         if (rc == 0 && ea != NULL) {
879                 struct lov_stripe_md *lsm = *ea;
880
881                 lsm->lsm_oi = oa->o_oi;
882         }
883         ofd_seq_put(env, oseq);
884
885 out_sem:
886         up_read(&ofd->ofd_lastid_rwsem);
887         RETURN(rc);
888 }
889
890 /* needed by echo client only for now, RPC handler uses ofd_getattr_hdl() */
891 int ofd_echo_getattr(const struct lu_env *env, struct obd_export *exp,
892                      struct obd_info *oinfo)
893 {
894         struct ofd_device       *ofd = ofd_exp(exp);
895         struct ofd_thread_info  *info;
896         struct lu_fid           *fid = &oinfo->oi_oa->o_oi.oi_fid;
897         struct ofd_object       *fo;
898         int                      rc = 0;
899
900         ENTRY;
901
902         info = ofd_info_init(env, exp);
903
904         fo = ofd_object_find_exists(env, ofd, fid);
905         if (IS_ERR(fo))
906                 GOTO(out, rc = PTR_ERR(fo));
907
908         LASSERT(fo != NULL);
909         rc = ofd_attr_get(env, fo, &info->fti_attr);
910         oinfo->oi_oa->o_valid = OBD_MD_FLID;
911         if (rc == 0) {
912                 __u64 curr_version;
913
914                 obdo_from_la(oinfo->oi_oa, &info->fti_attr,
915                              OFD_VALID_FLAGS | LA_UID | LA_GID);
916
917                 /* Store object version in reply */
918                 curr_version = dt_version_get(env, ofd_object_child(fo));
919                 if ((__s64)curr_version != -EOPNOTSUPP) {
920                         oinfo->oi_oa->o_valid |= OBD_MD_FLDATAVERSION;
921                         oinfo->oi_oa->o_data_version = curr_version;
922                 }
923         }
924
925         ofd_object_put(env, fo);
926 out:
927         RETURN(rc);
928 }
929
930 static int ofd_ioc_get_obj_version(const struct lu_env *env,
931                                    struct ofd_device *ofd, void *karg)
932 {
933         struct obd_ioctl_data *data = karg;
934         struct lu_fid          fid;
935         struct ofd_object     *fo;
936         dt_obj_version_t       version;
937         int                    rc = 0;
938
939         ENTRY;
940
941         if (data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
942                 GOTO(out, rc = -EINVAL);
943
944         if (data->ioc_inlbuf1 != NULL && data->ioc_inllen1 == sizeof(fid)) {
945                 fid = *(struct lu_fid *)data->ioc_inlbuf1;
946         } else if (data->ioc_inlbuf3 != NULL &&
947                    data->ioc_inllen3 == sizeof(__u64) &&
948                    data->ioc_inlbuf4 != NULL &&
949                    data->ioc_inllen4 == sizeof(__u64)) {
950                 struct ost_id ostid;
951
952                 ostid_set_seq(&ostid, *(__u64 *)data->ioc_inlbuf4);
953                 ostid_set_id(&ostid, *(__u64 *)data->ioc_inlbuf3);
954                 rc = ostid_to_fid(&fid, &ostid,
955                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
956                 if (rc != 0)
957                         GOTO(out, rc);
958         } else {
959                 GOTO(out, rc = -EINVAL);
960         }
961
962         if (!fid_is_sane(&fid))
963                 GOTO(out, rc = -EINVAL);
964
965         fo = ofd_object_find(env, ofd, &fid);
966         if (IS_ERR(fo))
967                 GOTO(out, rc = PTR_ERR(fo));
968
969         if (!ofd_object_exists(fo))
970                 GOTO(out_fo, rc = -ENOENT);
971
972         if (lu_object_remote(&fo->ofo_obj.do_lu))
973                 GOTO(out_fo, rc = -EREMOTE);
974
975         version = dt_version_get(env, ofd_object_child(fo));
976         if (version == 0)
977                 GOTO(out_fo, rc = -EIO);
978
979         *(dt_obj_version_t *)data->ioc_inlbuf2 = version;
980
981         EXIT;
982 out_fo:
983         ofd_object_put(env, fo);
984 out:
985         return rc;
986 }
987
988 int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
989                   void *karg, void *uarg)
990 {
991         struct lu_env            env;
992         struct ofd_device       *ofd = ofd_exp(exp);
993         struct obd_device       *obd = ofd_obd(ofd);
994         int                      rc;
995
996         ENTRY;
997
998         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
999         rc = lu_env_init(&env, LCT_DT_THREAD);
1000         if (rc)
1001                 RETURN(rc);
1002
1003         switch (cmd) {
1004         case OBD_IOC_ABORT_RECOVERY:
1005                 CERROR("%s: aborting recovery\n", obd->obd_name);
1006                 target_stop_recovery_thread(obd);
1007                 break;
1008         case OBD_IOC_SYNC:
1009                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1010                 rc = dt_sync(&env, ofd->ofd_osd);
1011                 break;
1012         case OBD_IOC_SET_READONLY:
1013                 rc = dt_sync(&env, ofd->ofd_osd);
1014                 if (rc == 0)
1015                         rc = dt_ro(&env, ofd->ofd_osd);
1016                 break;
1017         case OBD_IOC_START_LFSCK: {
1018                 struct obd_ioctl_data *data = karg;
1019                 struct lfsck_start_param lsp;
1020
1021                 if (unlikely(data == NULL)) {
1022                         rc = -EINVAL;
1023                         break;
1024                 }
1025
1026                 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
1027                 lsp.lsp_index_valid = 0;
1028                 rc = lfsck_start(&env, ofd->ofd_osd, &lsp);
1029                 break;
1030         }
1031         case OBD_IOC_STOP_LFSCK: {
1032                 struct obd_ioctl_data *data = karg;
1033                 struct lfsck_stop      stop;
1034
1035                 stop.ls_status = LS_STOPPED;
1036                 /* Old lfsck utils may pass NULL @stop. */
1037                 if (data->ioc_inlbuf1 == NULL)
1038                         stop.ls_flags = 0;
1039                 else
1040                         stop.ls_flags =
1041                         ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
1042
1043                 rc = lfsck_stop(&env, ofd->ofd_osd, &stop);
1044                 break;
1045         }
1046         case OBD_IOC_GET_OBJ_VERSION:
1047                 rc = ofd_ioc_get_obj_version(&env, ofd, karg);
1048                 break;
1049         default:
1050                 CERROR("%s: not supported cmd = %d\n", obd->obd_name, cmd);
1051                 rc = -ENOTTY;
1052         }
1053
1054         lu_env_fini(&env);
1055         RETURN(rc);
1056 }
1057
1058 static int ofd_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1059 {
1060         int rc = 0;
1061
1062         ENTRY;
1063
1064         switch(stage) {
1065         case OBD_CLEANUP_EARLY:
1066                 break;
1067         case OBD_CLEANUP_EXPORTS:
1068                 target_cleanup_recovery(obd);
1069                 break;
1070         }
1071         RETURN(rc);
1072 }
1073
1074 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1075 {
1076         ofd_fmd_expire(exp);
1077         return 0;
1078 }
1079
1080 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1081 {
1082         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1083         struct ofd_thread_info  *info;
1084         struct lu_env            env;
1085 #ifdef USE_HEALTH_CHECK_WRITE
1086         struct thandle          *th;
1087 #endif
1088         int                      rc = 0;
1089
1090         /* obd_proc_read_health pass NULL env, we need real one */
1091         rc = lu_env_init(&env, LCT_DT_THREAD);
1092         if (rc)
1093                 RETURN(rc);
1094
1095         info = ofd_info_init(&env, NULL);
1096         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1097         if (unlikely(rc))
1098                 GOTO(out, rc);
1099
1100         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1101                 GOTO(out, rc = -EROFS);
1102
1103 #ifdef USE_HEALTH_CHECK_WRITE
1104         OBD_ALLOC(info->fti_buf.lb_buf, PAGE_CACHE_SIZE);
1105         if (info->fti_buf.lb_buf == NULL)
1106                 GOTO(out, rc = -ENOMEM);
1107
1108         info->fti_buf.lb_len = PAGE_CACHE_SIZE;
1109         info->fti_off = 0;
1110
1111         th = dt_trans_create(&env, ofd->ofd_osd);
1112         if (IS_ERR(th))
1113                 GOTO(out, rc = PTR_ERR(th));
1114
1115         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1116                                      info->fti_buf.lb_len, info->fti_off, th);
1117         if (rc == 0) {
1118                 th->th_sync = 1; /* sync IO is needed */
1119                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1120                 if (rc == 0)
1121                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1122                                              &info->fti_buf, &info->fti_off,
1123                                              th);
1124         }
1125         dt_trans_stop(&env, ofd->ofd_osd, th);
1126
1127         OBD_FREE(info->fti_buf.lb_buf, PAGE_CACHE_SIZE);
1128
1129         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1130 #endif
1131 out:
1132         lu_env_fini(&env);
1133         return !!rc;
1134 }
1135
1136 /*
1137  * Handle quota control requests to consult current usage/limit.
1138  *
1139  * \param obd - is the obd device associated with the ofd
1140  * \param exp - is the client's export
1141  * \param oqctl - is the obd_quotactl request to be processed
1142  */
1143 static int ofd_quotactl(struct obd_device *obd, struct obd_export *exp,
1144                         struct obd_quotactl *oqctl)
1145 {
1146         struct ofd_device  *ofd = ofd_dev(obd->obd_lu_dev);
1147         struct lu_env       env;
1148         int                 rc;
1149         ENTRY;
1150
1151         /* report success for quota on/off for interoperability with current MDT
1152          * stack */
1153         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1154                 RETURN(0);
1155
1156         rc = lu_env_init(&env, LCT_DT_THREAD);
1157         if (rc)
1158                 RETURN(rc);
1159
1160         rc = lquotactl_slv(&env, ofd->ofd_osd, oqctl);
1161         lu_env_fini(&env);
1162
1163         RETURN(rc);
1164 }
1165
1166 struct obd_ops ofd_obd_ops = {
1167         .o_owner                = THIS_MODULE,
1168         .o_connect              = ofd_obd_connect,
1169         .o_reconnect            = ofd_obd_reconnect,
1170         .o_disconnect           = ofd_obd_disconnect,
1171         .o_create               = ofd_echo_create,
1172         .o_setattr              = ofd_echo_setattr,
1173         .o_preprw               = ofd_preprw,
1174         .o_commitrw             = ofd_commitrw,
1175         .o_destroy              = ofd_echo_destroy,
1176         .o_init_export          = ofd_init_export,
1177         .o_destroy_export       = ofd_destroy_export,
1178         .o_postrecov            = ofd_obd_postrecov,
1179         .o_getattr              = ofd_echo_getattr,
1180         .o_iocontrol            = ofd_iocontrol,
1181         .o_precleanup           = ofd_precleanup,
1182         .o_ping                 = ofd_ping,
1183         .o_health_check         = ofd_health_check,
1184         .o_quotactl             = ofd_quotactl,
1185         .o_set_info_async       = ofd_set_info_async,
1186         .o_get_info             = ofd_get_info,
1187 };