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