4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39 * Author: Mike Pershin <tappro@whamcloud.com>
40 * Author: Johann Lombardi <johann@whamcloud.com>
43 #define DEBUG_SUBSYSTEM S_FILTER
45 #include <obd_class.h>
46 #include <lustre_param.h>
47 #include <lustre_fid.h>
48 #include <lustre_lfsck.h>
49 #include <lustre/lustre_idl.h>
50 #include <lustre_dlm.h>
51 #include <lustre_quota.h>
53 #include "ofd_internal.h"
55 /* Slab for OFD object allocation */
56 static struct kmem_cache *ofd_object_kmem;
58 static struct lu_kmem_descr ofd_caches[] = {
60 .ckd_cache = &ofd_object_kmem,
61 .ckd_name = "ofd_obj",
62 .ckd_size = sizeof(struct ofd_object)
69 static int ofd_connect_to_next(const struct lu_env *env, struct ofd_device *m,
70 const char *next, struct obd_export **exp)
72 struct obd_connect_data *data = NULL;
73 struct obd_device *obd;
79 GOTO(out, rc = -ENOMEM);
81 obd = class_name2obd(next);
83 CERROR("%s: can't locate next device: %s\n",
85 GOTO(out, rc = -ENOTCONN);
88 data->ocd_connect_flags = OBD_CONNECT_VERSION;
89 data->ocd_version = LUSTRE_VERSION_CODE;
91 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
93 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
94 ofd_name(m), next, rc);
98 m->ofd_dt_dev.dd_lu_dev.ld_site =
99 m->ofd_osd_exp->exp_obd->obd_lu_dev->ld_site;
100 LASSERT(m->ofd_dt_dev.dd_lu_dev.ld_site);
101 m->ofd_osd = lu2dt_dev(m->ofd_osd_exp->exp_obd->obd_lu_dev);
102 m->ofd_dt_dev.dd_lu_dev.ld_site->ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
110 static int ofd_stack_init(const struct lu_env *env,
111 struct ofd_device *m, struct lustre_cfg *cfg)
113 const char *dev = lustre_cfg_string(cfg, 0);
115 struct ofd_thread_info *info = ofd_info(env);
116 struct lustre_mount_info *lmi;
122 lmi = server_get_mount(dev);
124 CERROR("Cannot get mount info for %s!\n", dev);
128 /* find bottom osd */
129 OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
133 snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
134 rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
135 OBD_FREE(osdname, MTI_NAME_MAXLEN);
139 d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
141 m->ofd_osd = lu2dt_dev(d);
143 snprintf(info->fti_u.name, sizeof(info->fti_u.name),
144 "%s-osd", lustre_cfg_string(cfg, 0));
149 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
150 struct lu_device *top)
152 struct obd_device *obd = ofd_obd(m);
153 struct lustre_cfg_bufs bufs;
154 struct lustre_cfg *lcfg;
159 lu_site_purge(env, top->ld_site, ~0);
160 /* process cleanup, pass mdt obd name to get obd umount flags */
161 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
166 lustre_cfg_bufs_set_string(&bufs, 1, flags);
167 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
169 CERROR("Cannot alloc lcfg!\n");
174 top->ld_ops->ldo_process_config(env, top, lcfg);
175 lustre_cfg_free(lcfg);
177 lu_site_purge(env, top->ld_site, ~0);
178 if (!cfs_hash_is_empty(top->ld_site->ls_obj_hash)) {
179 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
180 lu_site_print(env, top->ld_site, &msgdata, lu_cdebug_printer);
183 LASSERT(m->ofd_osd_exp);
184 obd_disconnect(m->ofd_osd_exp);
189 /* For interoperability, see mdt_interop_param[]. */
190 static struct cfg_interop_param ofd_interop_param[] = {
191 { "ost.quota_type", NULL },
195 /* Some parameters were moved from ofd to osd and only their
196 * symlinks were kept in ofd by LU-3106. They are:
197 * -writehthrough_cache_enable
198 * -readcache_max_filese
201 * Since they are not included by the static lprocfs var list,
202 * a pre-check is added for them to avoid "unknown param" error
203 * message confuses the customer. If they are matched in this
204 * check, they will be passed to the osd directly.
206 static bool match_symlink_param(char *param)
211 if (class_match_param(param, PARAM_OST, ¶m) == 0) {
212 sval = strchr(param, '=');
214 paramlen = sval - param;
215 if (strncmp(param, "writethrough_cache_enable",
217 strncmp(param, "readcache_max_filesize",
219 strncmp(param, "read_cache_enable",
221 strncmp(param, "brw_stats", paramlen) == 0)
229 /* used by MGS to process specific configurations */
230 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
231 struct lustre_cfg *cfg)
233 struct ofd_device *m = ofd_dev(d);
234 struct dt_device *dt_next = m->ofd_osd;
235 struct lu_device *next = &dt_next->dd_lu_dev;
240 switch (cfg->lcfg_command) {
242 struct obd_device *obd = ofd_obd(m);
243 /* For interoperability */
244 struct cfg_interop_param *ptr = NULL;
245 struct lustre_cfg *old_cfg = NULL;
248 param = lustre_cfg_string(cfg, 1);
250 CERROR("param is empty\n");
255 ptr = class_find_old_param(param, ofd_interop_param);
257 if (ptr->new_param == NULL) {
259 CWARN("For interoperability, skip this %s."
260 " It is obsolete.\n", ptr->old_param);
264 CWARN("Found old param %s, changed it to %s.\n",
265 ptr->old_param, ptr->new_param);
268 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
275 if (match_symlink_param(param)) {
276 rc = next->ld_ops->ldo_process_config(env, next, cfg);
280 rc = class_process_proc_seq_param(PARAM_OST, obd->obd_vars, cfg,
282 if (rc > 0 || rc == -ENOSYS) {
283 CDEBUG(D_CONFIG, "pass param %s down the stack.\n",
285 /* we don't understand; pass it on */
286 rc = next->ld_ops->ldo_process_config(env, next, cfg);
290 case LCFG_SPTLRPC_CONF: {
295 /* others are passed further */
296 rc = next->ld_ops->ldo_process_config(env, next, cfg);
302 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
303 const struct lu_object_conf *conf)
305 struct ofd_device *d = ofd_dev(o->lo_dev);
306 struct lu_device *under;
307 struct lu_object *below;
312 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
313 PFID(lu_object_fid(o)));
315 under = &d->ofd_osd->dd_lu_dev;
316 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
318 lu_object_add(o, below);
325 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
327 struct ofd_object *of = ofd_obj(o);
328 struct lu_object_header *h;
333 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
334 PFID(lu_object_fid(o)));
337 lu_object_header_fini(h);
338 OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
342 static int ofd_object_print(const struct lu_env *env, void *cookie,
343 lu_printer_t p, const struct lu_object *o)
345 return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
348 struct lu_object_operations ofd_obj_ops = {
349 .loo_object_init = ofd_object_init,
350 .loo_object_free = ofd_object_free,
351 .loo_object_print = ofd_object_print
354 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
355 const struct lu_object_header *hdr,
358 struct ofd_object *of;
362 OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, GFP_NOFS);
365 struct lu_object_header *h;
367 o = &of->ofo_obj.do_lu;
369 lu_object_header_init(h);
370 lu_object_init(o, h, d);
371 lu_object_add_top(h, o);
372 o->lo_ops = &ofd_obj_ops;
379 extern int ost_handle(struct ptlrpc_request *req);
381 static int ofd_lfsck_out_notify(const struct lu_env *env, void *data,
382 enum lfsck_events event)
384 struct ofd_device *ofd = data;
385 struct obd_device *obd = ofd_obd(ofd);
388 case LE_LASTID_REBUILDING:
389 CWARN("%s: Found crashed LAST_ID, deny creating new OST-object "
390 "on the device until the LAST_ID rebuilt successfully.\n",
392 down_write(&ofd->ofd_lastid_rwsem);
393 ofd->ofd_lastid_rebuilding = 1;
394 up_write(&ofd->ofd_lastid_rwsem);
396 case LE_LASTID_REBUILT: {
397 down_write(&ofd->ofd_lastid_rwsem);
398 ofd_seqs_free(env, ofd);
399 ofd->ofd_lastid_rebuilding = 0;
400 ofd->ofd_lastid_gen++;
401 up_write(&ofd->ofd_lastid_rwsem);
405 CERROR("%s: unknown lfsck event: rc = %d\n",
406 ofd_name(ofd), event);
413 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
414 struct lu_device *dev)
416 struct ofd_thread_info *info;
417 struct ofd_device *ofd = ofd_dev(dev);
418 struct obd_device *obd = ofd_obd(ofd);
419 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
424 info = ofd_info_init(env, NULL);
428 /* initialize lower device */
429 rc = next->ld_ops->ldo_prepare(env, dev, next);
433 rc = lfsck_register(env, ofd->ofd_osd, ofd->ofd_osd, obd,
434 ofd_lfsck_out_notify, ofd, false);
436 CERROR("%s: failed to initialize lfsck: rc = %d\n",
441 rc = lfsck_register_namespace(env, ofd->ofd_osd, ofd->ofd_namespace);
442 /* The LFSCK instance is registered just now, so it must be there when
443 * register the namespace to such instance. */
444 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
446 target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
447 LASSERT(obd->obd_no_conn);
448 spin_lock(&obd->obd_dev_lock);
449 obd->obd_no_conn = 0;
450 spin_unlock(&obd->obd_dev_lock);
452 if (obd->obd_recovering == 0)
453 ofd_postrecov(env, ofd);
458 static int ofd_recovery_complete(const struct lu_env *env,
459 struct lu_device *dev)
461 struct ofd_device *ofd = ofd_dev(dev);
462 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
463 int rc = 0, max_precreate;
467 /* Grant space for object precreation on the self export.
468 * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
469 * is enough to create 10k objects. More space is then acquired for
470 * precreation in ofd_grant_create().
472 max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
473 ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
475 rc = next->ld_ops->ldo_recovery_complete(env, next);
479 static struct lu_device_operations ofd_lu_ops = {
480 .ldo_object_alloc = ofd_object_alloc,
481 .ldo_process_config = ofd_process_config,
482 .ldo_recovery_complete = ofd_recovery_complete,
483 .ldo_prepare = ofd_prepare,
486 LPROC_SEQ_FOPS(lprocfs_nid_stats_clear);
488 static int ofd_procfs_init(struct ofd_device *ofd)
490 struct obd_device *obd = ofd_obd(ofd);
491 cfs_proc_dir_entry_t *entry;
496 /* lprocfs must be setup before the ofd so state can be safely added
497 * to /proc incrementally as the ofd is setup */
498 obd->obd_vars = lprocfs_ofd_obd_vars;
499 rc = lprocfs_seq_obd_setup(obd);
501 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
506 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
508 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
510 GOTO(obd_cleanup, rc);
513 obd->obd_uses_nid_stats = 1;
515 entry = lprocfs_seq_register("exports", obd->obd_proc_entry, NULL,
519 CERROR("%s: error %d setting up lprocfs for %s\n",
520 obd->obd_name, rc, "exports");
521 GOTO(obd_cleanup, rc);
523 obd->obd_proc_exports_entry = entry;
525 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
526 #ifndef HAVE_ONLY_PROCFS_SEQ
529 obd, &lprocfs_nid_stats_clear_fops);
532 CERROR("%s: add proc entry 'clear' failed: %d.\n",
534 GOTO(obd_cleanup, rc);
537 ofd_stats_counter_init(obd->obd_stats);
539 rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
540 ofd_stats_counter_init);
542 GOTO(obd_cleanup, rc);
545 lprocfs_obd_cleanup(obd);
546 lprocfs_free_obd_stats(obd);
552 * ofd_procfs_add_brw_stats_symlink - expose osd stats to ofd layer
554 * The osd interfaces to the backend file system exposes useful data
555 * such as brw_stats and read or write cache states. This same data
556 * needs to be exposed into the obdfilter (ofd) layer to maintain
557 * backwards compatibility. This function creates the symlinks in the
558 * proc layer to enable this.
560 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
562 struct obd_device *obd = ofd_obd(ofd);
563 struct obd_device *osd_obd = ofd->ofd_osd_exp->exp_obd;
565 if (obd->obd_proc_entry == NULL)
568 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
569 "../../%s/%s/brw_stats",
570 osd_obd->obd_type->typ_name, obd->obd_name);
572 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
573 "../../%s/%s/read_cache_enable",
574 osd_obd->obd_type->typ_name, obd->obd_name);
576 lprocfs_add_symlink("readcache_max_filesize",
578 "../../%s/%s/readcache_max_filesize",
579 osd_obd->obd_type->typ_name, obd->obd_name);
581 lprocfs_add_symlink("writethrough_cache_enable",
583 "../../%s/%s/writethrough_cache_enable",
584 osd_obd->obd_type->typ_name, obd->obd_name);
587 static void ofd_procfs_fini(struct ofd_device *ofd)
589 struct obd_device *obd = ofd_obd(ofd);
591 lprocfs_free_per_client_stats(obd);
592 lprocfs_obd_cleanup(obd);
593 lprocfs_free_obd_stats(obd);
594 lprocfs_job_stats_fini(obd);
597 extern int ost_handle(struct ptlrpc_request *req);
599 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
601 return seq_site_fini(env, &ofd->ofd_seq_site);
604 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
606 struct seq_server_site *ss = &ofd->ofd_seq_site;
607 struct lu_device *lu = &ofd->ofd_dt_dev.dd_lu_dev;
608 char *obd_name = ofd_name(ofd);
612 ss = &ofd->ofd_seq_site;
613 lu->ld_site->ld_seq_site = ss;
614 ss->ss_lu = lu->ld_site;
615 ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
617 OBD_ALLOC_PTR(ss->ss_server_seq);
618 if (ss->ss_server_seq == NULL)
619 GOTO(out_free, rc = -ENOMEM);
621 OBD_ALLOC(name, strlen(obd_name) + 10);
623 OBD_FREE_PTR(ss->ss_server_seq);
624 ss->ss_server_seq = NULL;
625 GOTO(out_free, rc = -ENOMEM);
628 rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
629 LUSTRE_SEQ_SERVER, ss);
631 CERROR("%s : seq server init error %d\n", obd_name, rc);
634 ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
636 OBD_ALLOC_PTR(ss->ss_client_seq);
637 if (ss->ss_client_seq == NULL)
638 GOTO(out_free, rc = -ENOMEM);
640 snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
641 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
644 CERROR("%s : seq client init error %d\n", obd_name, rc);
647 OBD_FREE(name, strlen(obd_name) + 10);
650 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
654 if (ss->ss_server_seq) {
655 seq_server_fini(ss->ss_server_seq, env);
656 OBD_FREE_PTR(ss->ss_server_seq);
657 ss->ss_server_seq = NULL;
660 if (ss->ss_client_seq) {
661 seq_client_fini(ss->ss_client_seq);
662 OBD_FREE_PTR(ss->ss_client_seq);
663 ss->ss_client_seq = NULL;
667 OBD_FREE(name, strlen(obd_name) + 10);
675 int ofd_set_info_hdl(struct tgt_session_info *tsi)
677 struct ptlrpc_request *req = tgt_ses_req(tsi);
678 struct ost_body *body = NULL, *repbody;
679 void *key, *val = NULL;
680 int keylen, vallen, rc = 0;
681 bool is_grant_shrink;
682 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
686 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
688 DEBUG_REQ(D_HA, req, "no set_info key");
689 RETURN(err_serious(-EFAULT));
691 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
694 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
696 DEBUG_REQ(D_HA, req, "no set_info val");
697 RETURN(err_serious(-EFAULT));
699 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
702 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
704 /* In this case the value is actually an RMF_OST_BODY, so we
705 * transmutate the type of this PTLRPC */
706 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
708 rc = req_capsule_server_pack(tsi->tsi_pill);
712 if (is_grant_shrink) {
713 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
715 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
718 /** handle grant shrink, similar to a read request */
719 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
721 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
723 obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
725 } else if (KEY_IS(KEY_CAPA_KEY)) {
726 rc = ofd_update_capa_key(ofd, val);
727 } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
728 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
730 CERROR("%s: Unsupported key %s\n",
731 tgt_name(tsi->tsi_tgt), (char *)key);
734 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
740 int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
741 struct lu_fid *fid, struct ll_user_fiemap *fiemap)
743 struct ofd_object *fo;
746 fo = ofd_object_find(env, ofd, fid);
748 CERROR("%s: error finding object "DFID"\n",
749 ofd_name(ofd), PFID(fid));
753 ofd_read_lock(env, fo);
754 if (ofd_object_exists(fo))
755 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
758 ofd_read_unlock(env, fo);
759 ofd_object_put(env, fo);
763 struct locked_region {
765 struct lustre_handle lh;
768 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
769 unsigned long long begin, unsigned long long end,
772 struct locked_region *region = NULL;
776 LASSERT(begin <= end);
777 OBD_ALLOC_PTR(region);
781 rc = tgt_extent_lock(ns, res_id, begin, end, ®ion->lh,
786 CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
788 cfs_list_add(®ion->list, locked);
793 static int lock_zero_regions(struct ldlm_namespace *ns,
794 struct ldlm_res_id *res_id,
795 struct ll_user_fiemap *fiemap,
798 __u64 begin = fiemap->fm_start;
801 struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
805 CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
806 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
807 if (fiemap_start[i].fe_logical > begin) {
808 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
809 begin, fiemap_start[i].fe_logical);
810 rc = lock_region(ns, res_id, begin,
811 fiemap_start[i].fe_logical, locked);
816 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
819 if (begin < (fiemap->fm_start + fiemap->fm_length)) {
820 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
821 begin, fiemap->fm_start + fiemap->fm_length);
822 rc = lock_region(ns, res_id, begin,
823 fiemap->fm_start + fiemap->fm_length, locked);
829 static void unlock_zero_regions(struct ldlm_namespace *ns, cfs_list_t *locked)
831 struct locked_region *entry, *temp;
833 cfs_list_for_each_entry_safe(entry, temp, locked, list) {
834 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
835 tgt_extent_unlock(&entry->lh, LCK_PR);
836 cfs_list_del(&entry->list);
841 int ofd_get_info_hdl(struct tgt_session_info *tsi)
843 struct obd_export *exp = tsi->tsi_exp;
844 struct ofd_device *ofd = ofd_exp(exp);
845 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
848 int replylen, rc = 0;
852 /* this common part for get_info rpc */
853 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
855 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
856 RETURN(err_serious(-EPROTO));
858 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
861 if (KEY_IS(KEY_LAST_ID)) {
863 struct ofd_seq *oseq;
865 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
866 rc = req_capsule_server_pack(tsi->tsi_pill);
868 RETURN(err_serious(rc));
870 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
872 oseq = ofd_seq_load(tsi->tsi_env, ofd,
873 (obd_seq)exp->exp_filter_data.fed_group);
877 *last_id = ofd_seq_last_oid(oseq);
878 ofd_seq_put(tsi->tsi_env, oseq);
879 } else if (KEY_IS(KEY_FIEMAP)) {
880 struct ll_fiemap_info_key *fm_key;
881 struct ll_user_fiemap *fiemap;
884 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
886 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
887 rc = tgt_validate_obdo(tsi, &fm_key->oa);
889 RETURN(err_serious(rc));
891 fid = &fm_key->oa.o_oi.oi_fid;
893 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
895 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
896 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
897 RCL_SERVER, replylen);
899 rc = req_capsule_server_pack(tsi->tsi_pill);
901 RETURN(err_serious(rc));
903 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
907 *fiemap = fm_key->fiemap;
908 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
910 /* LU-3219: Lock the sparse areas to make sure dirty
911 * flushed back from client, then call fiemap again. */
912 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
913 fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
914 cfs_list_t locked = CFS_LIST_HEAD_INIT(locked);
916 ost_fid_build_resid(fid, &fti->fti_resid);
917 rc = lock_zero_regions(ofd->ofd_namespace,
918 &fti->fti_resid, fiemap,
920 if (rc == 0 && !cfs_list_empty(&locked)) {
921 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
923 unlock_zero_regions(ofd->ofd_namespace,
927 } else if (KEY_IS(KEY_LAST_FID)) {
928 struct ofd_device *ofd = ofd_exp(exp);
929 struct ofd_seq *oseq;
933 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
934 rc = req_capsule_server_pack(tsi->tsi_pill);
936 RETURN(err_serious(rc));
938 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
940 RETURN(err_serious(-EPROTO));
942 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
944 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
948 oseq = ofd_seq_load(tsi->tsi_env, ofd,
949 ostid_seq(&fti->fti_ostid));
951 RETURN(PTR_ERR(oseq));
953 rc = ostid_to_fid(fid, &oseq->os_oi,
954 ofd->ofd_lut.lut_lsd.lsd_osd_index);
958 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
961 ofd_seq_put(tsi->tsi_env, oseq);
963 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
967 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
973 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
975 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
976 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
977 struct ost_body *repbody;
978 struct lustre_handle lh = { 0 };
979 struct ofd_object *fo;
981 ldlm_mode_t lock_mode = LCK_PR;
986 LASSERT(tsi->tsi_ost_body != NULL);
988 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
992 repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
993 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
995 srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
996 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
999 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
1002 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
1003 &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
1009 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1011 GOTO(out, rc = PTR_ERR(fo));
1013 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1017 obdo_from_la(&repbody->oa, &fti->fti_attr,
1018 OFD_VALID_FLAGS | LA_UID | LA_GID);
1019 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1021 /* Store object version in reply */
1022 curr_version = dt_version_get(tsi->tsi_env,
1023 ofd_object_child(fo));
1024 if ((__s64)curr_version != -EOPNOTSUPP) {
1025 repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
1026 repbody->oa.o_data_version = curr_version;
1030 ofd_object_put(tsi->tsi_env, fo);
1033 tgt_extent_unlock(&lh, lock_mode);
1035 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
1038 repbody->oa.o_valid |= OBD_MD_FLFLAGS;
1039 repbody->oa.o_flags = OBD_FL_FLUSH;
1044 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
1046 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1047 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1048 struct ost_body *body = tsi->tsi_ost_body;
1049 struct ost_body *repbody;
1050 struct ldlm_resource *res;
1051 struct ofd_object *fo;
1052 struct filter_fid *ff = NULL;
1057 LASSERT(body != NULL);
1059 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1060 if (repbody == NULL)
1063 repbody->oa.o_oi = body->oa.o_oi;
1064 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1066 /* This would be very bad - accidentally truncating a file when
1067 * changing the time or similar - bug 12203. */
1068 if (body->oa.o_valid & OBD_MD_FLSIZE &&
1069 body->oa.o_size != OBD_OBJECT_EOF) {
1070 static char mdsinum[48];
1072 if (body->oa.o_valid & OBD_MD_FLFID)
1073 snprintf(mdsinum, sizeof(mdsinum) - 1,
1074 "of parent "DFID, body->oa.o_parent_seq,
1075 body->oa.o_parent_oid, 0);
1079 CERROR("%s: setattr from %s is trying to truncate object "DFID
1080 " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1081 PFID(&tsi->tsi_fid), mdsinum);
1085 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1087 GOTO(out, rc = PTR_ERR(fo));
1089 la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1090 fti->fti_attr.la_valid &= ~LA_TYPE;
1092 if (body->oa.o_valid & OBD_MD_FLFID) {
1093 ff = &fti->fti_mds_fid;
1094 ofd_prepare_fidea(ff, &body->oa);
1097 /* setting objects attributes (including owner/group) */
1098 rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1102 obdo_from_la(&repbody->oa, &fti->fti_attr,
1103 OFD_VALID_FLAGS | LA_UID | LA_GID);
1104 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1106 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1110 ofd_object_put(tsi->tsi_env, fo);
1113 /* we do not call this before to avoid lu_object_find() in
1114 * ->lvbo_update() holding another reference on the object.
1115 * otherwise concurrent destroy can make the object unavailable
1116 * for 2nd lu_object_find() waiting for the first reference
1117 * to go... deadlock! */
1118 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1119 &tsi->tsi_resid, LDLM_EXTENT, 0);
1121 ldlm_res_lvbo_update(res, NULL, 0);
1122 ldlm_resource_putref(res);
1128 static int ofd_orphans_destroy(const struct lu_env *env,
1129 struct obd_export *exp,
1130 struct ofd_device *ofd, struct obdo *oa)
1132 struct ofd_thread_info *info = ofd_info(env);
1133 struct lu_fid *fid = &info->fti_fid;
1134 struct ost_id *oi = &oa->o_oi;
1135 struct ofd_seq *oseq;
1136 obd_seq seq = ostid_seq(oi);
1137 obd_id end_id = ostid_id(oi);
1145 oseq = ofd_seq_get(ofd, seq);
1147 CERROR("%s: Can not find seq for "DOSTID"\n",
1148 ofd_name(ofd), POSTID(oi));
1153 last = ofd_seq_last_oid(oseq);
1156 LASSERT(exp != NULL);
1157 skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1159 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NODESTROY))
1162 LCONSOLE(D_INFO, "%s: deleting orphan objects from "DOSTID
1163 " to "DOSTID"\n", ofd_name(ofd), seq, end_id + 1, seq, last);
1165 while (oid > end_id) {
1166 rc = fid_set_id(fid, oid);
1167 if (unlikely(rc != 0))
1170 rc = ofd_destroy_by_fid(env, ofd, fid, 1);
1171 if (rc != 0 && rc != -ENOENT && rc != -ESTALE &&
1172 likely(rc != -EREMCHG && rc != -EINPROGRESS))
1173 /* this is pretty fatal... */
1174 CEMERG("%s: error destroying precreated id "
1176 ofd_name(ofd), PFID(fid), rc);
1180 ofd_seq_last_oid_set(oseq, oid);
1181 /* update last_id on disk periodically so that if we
1182 * restart * we don't need to re-scan all of the just
1183 * deleted objects. */
1184 if ((oid & 511) == 0)
1185 ofd_seq_last_oid_write(env, ofd, oseq);
1189 CDEBUG(D_HA, "%s: after destroy: set last_id to "DOSTID"\n",
1190 ofd_name(ofd), seq, oid);
1194 ofd_seq_last_oid_set(oseq, oid);
1195 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1197 /* don't reuse orphan object, return last used objid */
1198 ostid_set_id(oi, last);
1205 ofd_seq_put(env, oseq);
1209 static int ofd_create_hdl(struct tgt_session_info *tsi)
1211 struct ptlrpc_request *req = tgt_ses_req(tsi);
1212 struct ost_body *repbody;
1213 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1214 struct obdo *rep_oa;
1215 struct obd_export *exp = tsi->tsi_exp;
1216 struct ofd_device *ofd = ofd_exp(exp);
1217 obd_seq seq = ostid_seq(&oa->o_oi);
1218 obd_id oid = ostid_id(&oa->o_oi);
1219 struct ofd_seq *oseq;
1225 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1228 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1229 if (repbody == NULL)
1232 down_read(&ofd->ofd_lastid_rwsem);
1233 /* Currently, for safe, we do not distinguish which LAST_ID is broken,
1234 * we may do that in the future.
1235 * Return -ENOSPC until the LAST_ID rebuilt. */
1236 if (unlikely(ofd->ofd_lastid_rebuilding))
1237 GOTO(out_sem, rc = -ENOSPC);
1239 rep_oa = &repbody->oa;
1240 rep_oa->o_oi = oa->o_oi;
1242 LASSERT(seq >= FID_SEQ_OST_MDT0);
1243 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1245 CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1247 oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1249 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1250 ofd_name(ofd), seq, PTR_ERR(oseq));
1251 GOTO(out_sem, rc = -EINVAL);
1254 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1255 (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1256 if (!ofd_obd(ofd)->obd_recovering ||
1257 oid > ofd_seq_last_oid(oseq)) {
1258 CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1259 "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1260 ofd_seq_last_oid(oseq));
1261 GOTO(out_nolock, rc = -EINVAL);
1263 /* Do nothing here, we re-create objects during recovery
1264 * upon write replay, see ofd_preprw_write() */
1265 GOTO(out_nolock, rc = 0);
1267 /* former ofd_handle_precreate */
1268 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1269 (oa->o_flags & OBD_FL_DELORPHAN)) {
1270 exp->exp_filter_data.fed_lastid_gen = ofd->ofd_lastid_gen;
1272 /* destroy orphans */
1273 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1274 exp->exp_conn_cnt) {
1275 CERROR("%s: dropping old orphan cleanup request\n",
1277 GOTO(out_nolock, rc = 0);
1279 /* This causes inflight precreates to abort and drop lock */
1280 oseq->os_destroys_in_progress = 1;
1281 mutex_lock(&oseq->os_create_lock);
1282 if (!oseq->os_destroys_in_progress) {
1283 CERROR("%s:["LPU64"] destroys_in_progress already"
1284 " cleared\n", ofd_name(ofd), seq);
1285 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1288 diff = oid - ofd_seq_last_oid(oseq);
1289 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1290 ofd_seq_last_oid(oseq), diff);
1291 if (-diff > OST_MAX_PRECREATE) {
1292 /* FIXME: should reset precreate_next_id on MDS */
1294 } else if (diff < 0) {
1295 rc = ofd_orphans_destroy(tsi->tsi_env, exp,
1297 oseq->os_destroys_in_progress = 0;
1299 /* XXX: Used by MDS for the first time! */
1300 oseq->os_destroys_in_progress = 0;
1303 if (unlikely(exp->exp_filter_data.fed_lastid_gen !=
1304 ofd->ofd_lastid_gen)) {
1305 ofd_obd_disconnect(exp);
1306 GOTO(out_nolock, rc = -ENOTCONN);
1309 mutex_lock(&oseq->os_create_lock);
1310 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1311 exp->exp_conn_cnt) {
1312 CERROR("%s: dropping old precreate request\n",
1316 /* only precreate if seq is 0, IDIF or normal and also o_id
1317 * must be specfied */
1318 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1319 !fid_seq_is_idif(seq)) || oid == 0) {
1320 diff = 1; /* shouldn't we create this right now? */
1322 diff = oid - ofd_seq_last_oid(oseq);
1323 /* Do sync create if the seq is about to used up */
1324 if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1325 if (unlikely(oid >= IDIF_MAX_OID - 1))
1327 } else if (fid_seq_is_norm(seq)) {
1329 LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1332 CERROR("%s : invalid o_seq "DOSTID"\n",
1333 ofd_name(ofd), POSTID(&oa->o_oi));
1334 GOTO(out, rc = -EINVAL);
1339 cfs_time_t enough_time = cfs_time_shift(DISK_TIMEOUT);
1344 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1345 !(oa->o_flags & OBD_FL_DELORPHAN)) {
1346 /* don't enforce grant during orphan recovery */
1347 rc = ofd_grant_create(tsi->tsi_env,
1348 ofd_obd(ofd)->obd_self_export,
1351 CDEBUG(D_HA, "%s: failed to acquire grant "
1352 "space for precreate (%d): rc = %d\n",
1353 ofd_name(ofd), diff, rc);
1358 /* This can happen if a new OST is formatted and installed
1359 * in place of an old one at the same index. Instead of
1360 * precreating potentially millions of deleted old objects
1361 * (possibly filling the OST), only precreate the last batch.
1362 * LFSCK will eventually clean up any orphans. LU-14 */
1363 if (diff > 5 * OST_MAX_PRECREATE) {
1364 diff = OST_MAX_PRECREATE / 2;
1365 LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1366 "larger than the LAST_ID "DOSTID", only "
1367 "precreating the last %u objects.\n",
1368 ofd_name(ofd), POSTID(&oa->o_oi),
1369 5 * OST_MAX_PRECREATE,
1370 POSTID(&oseq->os_oi), diff);
1371 ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1375 next_id = ofd_seq_last_oid(oseq) + 1;
1376 count = ofd_precreate_batch(ofd, diff);
1378 CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1379 " at "LPU64"\n", ofd_name(ofd),
1380 count, seq, next_id);
1382 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1383 && cfs_time_after(jiffies, enough_time)) {
1384 CDEBUG(D_HA, "%s: Slow creates, %d/%d objects"
1385 " created at a rate of %d/s\n",
1386 ofd_name(ofd), created, diff + created,
1387 created / DISK_TIMEOUT);
1391 rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1392 oseq, count, sync_trans);
1396 } else if (rc < 0) {
1402 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1403 LCONSOLE_WARN("%s: can't create the same count of"
1404 " objects when replaying the request"
1405 " (diff is %d). see LU-4621\n",
1406 ofd_name(ofd), diff);
1409 /* some objects got created, we can return
1410 * them, even if last creation failed */
1413 CERROR("%s: unable to precreate: rc = %d\n",
1416 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1417 !(oa->o_flags & OBD_FL_DELORPHAN))
1418 ofd_grant_commit(tsi->tsi_env,
1419 ofd_obd(ofd)->obd_self_export, rc);
1421 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1424 ofd_counter_incr(exp, LPROC_OFD_STATS_CREATE,
1427 mutex_unlock(&oseq->os_create_lock);
1430 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
1431 struct ofd_thread_info *info = ofd_info(tsi->tsi_env);
1432 struct lu_fid *fid = &info->fti_fid;
1434 /* For compatible purpose, it needs to convert back to
1435 * OST ID before put it on wire. */
1436 *fid = rep_oa->o_oi.oi_fid;
1437 fid_to_ostid(fid, &rep_oa->o_oi);
1439 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1441 ofd_seq_put(tsi->tsi_env, oseq);
1444 up_read(&ofd->ofd_lastid_rwsem);
1448 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1450 const struct ost_body *body = tsi->tsi_ost_body;
1451 struct ost_body *repbody;
1452 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1453 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1454 struct lu_fid *fid = &fti->fti_fid;
1461 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1464 /* This is old case for clients before Lustre 2.4 */
1465 /* If there's a DLM request, cancel the locks mentioned in it */
1466 if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1468 struct ldlm_request *dlm;
1470 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1473 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1476 *fid = body->oa.o_oi.oi_fid;
1477 oid = ostid_id(&body->oa.o_oi);
1480 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1482 /* check that o_misc makes sense */
1483 if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1484 count = body->oa.o_misc;
1486 count = 1; /* default case - single destroy */
1488 CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1489 POSTID(&body->oa.o_oi), count);
1494 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, fid, 0);
1495 if (lrc == -ENOENT) {
1497 "%s: destroying non-existent object "DFID"\n",
1498 ofd_name(ofd), PFID(fid));
1499 /* rewrite rc with -ENOENT only if it is 0 */
1502 } else if (lrc != 0) {
1503 CERROR("%s: error destroying object "DFID": %d\n",
1504 ofd_name(ofd), PFID(fid), lrc);
1510 lrc = fid_set_id(fid, oid);
1511 if (unlikely(lrc != 0 && count > 0))
1512 GOTO(out, rc = lrc);
1515 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1521 fid_to_ostid(fid, &repbody->oa.o_oi);
1525 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1527 struct obd_statfs *osfs;
1532 osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1534 rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1535 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1537 CERROR("%s: statfs failed: rc = %d\n",
1538 tgt_name(tsi->tsi_tgt), rc);
1540 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1543 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1549 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1551 struct ost_body *body = tsi->tsi_ost_body;
1552 struct ost_body *repbody;
1553 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1554 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1555 struct ofd_object *fo = NULL;
1560 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1562 /* if no objid is specified, it means "sync whole filesystem" */
1563 if (!fid_is_zero(&tsi->tsi_fid)) {
1564 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1566 RETURN(PTR_ERR(fo));
1569 rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1570 fo != NULL ? ofd_object_child(fo) : NULL,
1571 repbody->oa.o_size, repbody->oa.o_blocks);
1575 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1580 repbody->oa.o_oi = body->oa.o_oi;
1581 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1583 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1585 obdo_from_la(&repbody->oa, &fti->fti_attr,
1588 /* don't return rc from getattr */
1593 ofd_object_put(tsi->tsi_env, fo);
1597 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1599 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1600 struct ost_body *repbody;
1601 struct ofd_thread_info *info = tsi2ofd_info(tsi);
1602 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1603 struct ldlm_resource *res;
1604 struct ofd_object *fo;
1605 struct filter_fid *ff = NULL;
1607 struct lustre_handle lh = { 0, };
1614 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1615 CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1617 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1618 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1619 RETURN(err_serious(-EPROTO));
1621 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1622 if (repbody == NULL)
1623 RETURN(err_serious(-ENOMEM));
1625 /* punch start,end are passed in o_size,o_blocks throught wire */
1629 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1632 /* standard truncate optimization: if file body is completely
1633 * destroyed, don't send data back to the server. */
1635 flags |= LDLM_FL_AST_DISCARD_DATA;
1637 repbody->oa.o_oi = oa->o_oi;
1638 repbody->oa.o_valid = OBD_MD_FLID;
1640 srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1641 oa->o_flags & OBD_FL_SRVLOCK;
1644 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1650 CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1651 ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1652 oa->o_valid, start, end);
1654 fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1657 GOTO(out, rc = PTR_ERR(fo));
1659 la_from_obdo(&info->fti_attr, oa,
1660 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1661 info->fti_attr.la_size = start;
1662 info->fti_attr.la_valid |= LA_SIZE;
1664 if (oa->o_valid & OBD_MD_FLFID) {
1665 ff = &info->fti_mds_fid;
1666 ofd_prepare_fidea(ff, oa);
1669 rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1670 ff, (struct obdo *)oa);
1674 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1678 ofd_object_put(tsi->tsi_env, fo);
1681 tgt_extent_unlock(&lh, LCK_PW);
1683 /* we do not call this before to avoid lu_object_find() in
1684 * ->lvbo_update() holding another reference on the object.
1685 * otherwise concurrent destroy can make the object unavailable
1686 * for 2nd lu_object_find() waiting for the first reference
1687 * to go... deadlock! */
1688 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1691 ldlm_res_lvbo_update(res, NULL, 0);
1692 ldlm_resource_putref(res);
1698 static int ofd_quotactl(struct tgt_session_info *tsi)
1700 struct obd_quotactl *oqctl, *repoqc;
1705 oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1707 RETURN(err_serious(-EPROTO));
1709 repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1711 RETURN(err_serious(-ENOMEM));
1713 /* report success for quota on/off for interoperability with current MDT
1715 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1719 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1721 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1727 /* High priority request handlers for OFD */
1729 /* prolong locks for the current service time of the corresponding
1730 * portal (= OST_IO_PORTAL)
1732 static inline int prolong_timeout(struct ptlrpc_request *req)
1734 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1737 return obd_timeout / 2;
1739 return max(at_est2timeout(at_get(&svcpt->scp_at_estimate)),
1743 static int ofd_prolong_one_lock(struct tgt_session_info *tsi,
1744 struct ldlm_lock *lock,
1745 struct ldlm_extent *extent, int timeout)
1748 if (lock->l_flags & LDLM_FL_DESTROYED) /* lock already cancelled */
1751 /* XXX: never try to grab resource lock here because we're inside
1752 * exp_bl_list_lock; in ldlm_lockd.c to handle waiting list we take
1753 * res lock and then exp_bl_list_lock. */
1755 if (!(lock->l_flags & LDLM_FL_AST_SENT))
1756 /* ignore locks not being cancelled */
1759 LDLM_DEBUG(lock, "refreshed for req x"LPU64" ext("LPU64"->"LPU64") "
1760 "to %ds.\n", tgt_ses_req(tsi)->rq_xid, extent->start,
1761 extent->end, timeout);
1763 /* OK. this is a possible lock the user holds doing I/O
1764 * let's refresh eviction timer for it */
1765 ldlm_refresh_waiting_lock(lock, timeout);
1769 static int ofd_prolong_extent_locks(struct tgt_session_info *tsi,
1770 __u64 start, __u64 end)
1772 struct obd_export *exp = tsi->tsi_exp;
1773 struct obdo *oa = &tsi->tsi_ost_body->oa;
1774 struct ldlm_extent extent = {
1778 struct ldlm_lock *lock;
1779 int timeout = prolong_timeout(tgt_ses_req(tsi));
1784 if (oa->o_valid & OBD_MD_FLHANDLE) {
1785 /* mostly a request should be covered by only one lock, try
1787 lock = ldlm_handle2lock(&oa->o_handle);
1789 /* Fast path to check if the lock covers the whole IO
1790 * region exclusively. */
1791 if (lock->l_granted_mode == LCK_PW &&
1792 ldlm_extent_contain(&lock->l_policy_data.l_extent,
1795 LASSERT(lock->l_export == exp);
1796 lock_count = ofd_prolong_one_lock(tsi, lock,
1798 LDLM_LOCK_PUT(lock);
1801 LDLM_LOCK_PUT(lock);
1805 spin_lock_bh(&exp->exp_bl_list_lock);
1806 list_for_each_entry(lock, &exp->exp_bl_list, l_exp_list) {
1807 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1808 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
1810 if (!ldlm_res_eq(&tsi->tsi_resid, &lock->l_resource->lr_name))
1813 if (!ldlm_extent_overlap(&lock->l_policy_data.l_extent,
1817 lock_count += ofd_prolong_one_lock(tsi, lock, &extent, timeout);
1819 spin_unlock_bh(&exp->exp_bl_list_lock);
1825 * Returns 1 if the given PTLRPC matches the given LDLM lock, or 0 if it does
1828 static int ofd_rw_hpreq_lock_match(struct ptlrpc_request *req,
1829 struct ldlm_lock *lock)
1831 struct niobuf_remote *rnb;
1832 struct obd_ioobj *ioo;
1834 struct ldlm_extent ext;
1835 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1839 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1840 LASSERT(ioo != NULL);
1842 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1843 LASSERT(rnb != NULL);
1845 ext.start = rnb->offset;
1846 rnb += ioo->ioo_bufcnt - 1;
1847 ext.end = rnb->offset + rnb->len - 1;
1849 LASSERT(lock->l_resource != NULL);
1850 if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
1854 if (opc == OST_READ)
1857 if (!(lock->l_granted_mode & mode))
1860 RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
1864 * High-priority queue request check for whether the given PTLRPC request
1865 * (\a req) is blocking an LDLM lock cancel.
1867 * Returns 1 if the given given PTLRPC request (\a req) is blocking an LDLM lock
1868 * cancel, 0 if it is not, and -EFAULT if the request is malformed.
1870 * Only OST_READs, OST_WRITEs and OST_PUNCHes go on the h-p RPC queue. This
1871 * function looks only at OST_READs and OST_WRITEs.
1873 static int ofd_rw_hpreq_check(struct ptlrpc_request *req)
1875 struct tgt_session_info *tsi;
1876 struct obd_ioobj *ioo;
1877 struct niobuf_remote *rnb;
1883 /* Don't use tgt_ses_info() to get session info, because lock_match()
1884 * can be called while request has no processing thread yet. */
1885 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1886 LASSERT(tsi != NULL);
1889 * Use LASSERT below because malformed RPCs should have
1890 * been filtered out in tgt_hpreq_handler().
1892 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1893 LASSERT(ioo != NULL);
1895 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1896 LASSERT(rnb != NULL);
1897 LASSERT(!(rnb->flags & OBD_BRW_SRVLOCK));
1899 start = rnb->offset;
1900 rnb += ioo->ioo_bufcnt - 1;
1901 end = rnb->offset + rnb->len - 1;
1903 DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID
1904 " ("LPU64"->"LPU64")\n",
1905 tgt_name(tsi->tsi_tgt), current->comm,
1906 PFID(&tsi->tsi_fid), start, end);
1908 lock_count = ofd_prolong_extent_locks(tsi, start, end);
1910 CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1911 tgt_name(tsi->tsi_tgt), lock_count, req);
1913 RETURN(lock_count > 0);
1916 static void ofd_rw_hpreq_fini(struct ptlrpc_request *req)
1918 ofd_rw_hpreq_check(req);
1922 * Like tgt_rw_hpreq_lock_match(), but for OST_PUNCH RPCs.
1924 static int ofd_punch_hpreq_lock_match(struct ptlrpc_request *req,
1925 struct ldlm_lock *lock)
1927 struct tgt_session_info *tsi;
1929 /* Don't use tgt_ses_info() to get session info, because lock_match()
1930 * can be called while request has no processing thread yet. */
1931 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1932 LASSERT(tsi != NULL);
1934 LASSERT(tsi->tsi_ost_body != NULL);
1935 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
1936 tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
1943 * Like ost_rw_hpreq_check(), but for OST_PUNCH RPCs.
1945 static int ofd_punch_hpreq_check(struct ptlrpc_request *req)
1947 struct tgt_session_info *tsi;
1953 /* Don't use tgt_ses_info() to get session info, because lock_match()
1954 * can be called while request has no processing thread yet. */
1955 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1956 LASSERT(tsi != NULL);
1957 oa = &tsi->tsi_ost_body->oa;
1959 LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
1960 oa->o_flags & OBD_FL_SRVLOCK));
1963 "%s: refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
1964 tgt_name(tsi->tsi_tgt), tsi->tsi_resid.name[0],
1965 tsi->tsi_resid.name[1], oa->o_size, oa->o_blocks);
1967 lock_count = ofd_prolong_extent_locks(tsi, oa->o_size, oa->o_blocks);
1969 CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1970 tgt_name(tsi->tsi_tgt), lock_count, req);
1972 RETURN(lock_count > 0);
1975 static void ofd_punch_hpreq_fini(struct ptlrpc_request *req)
1977 ofd_punch_hpreq_check(req);
1980 struct ptlrpc_hpreq_ops ofd_hpreq_rw = {
1981 .hpreq_lock_match = ofd_rw_hpreq_lock_match,
1982 .hpreq_check = ofd_rw_hpreq_check,
1983 .hpreq_fini = ofd_rw_hpreq_fini
1986 struct ptlrpc_hpreq_ops ofd_hpreq_punch = {
1987 .hpreq_lock_match = ofd_punch_hpreq_lock_match,
1988 .hpreq_check = ofd_punch_hpreq_check,
1989 .hpreq_fini = ofd_punch_hpreq_fini
1992 /** Assign high priority operations to the IO requests */
1993 static void ofd_hp_brw(struct tgt_session_info *tsi)
1995 struct niobuf_remote *rnb;
1996 struct obd_ioobj *ioo;
2000 ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
2001 LASSERT(ioo != NULL); /* must exist after request preprocessing */
2002 if (ioo->ioo_bufcnt > 0) {
2003 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
2004 LASSERT(rnb != NULL); /* must exist after request preprocessing */
2006 /* no high priority if server lock is needed */
2007 if (rnb->flags & OBD_BRW_SRVLOCK)
2010 tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_rw;
2013 static void ofd_hp_punch(struct tgt_session_info *tsi)
2015 LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
2016 /* no high-priority if server lock is needed */
2017 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
2018 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK)
2020 tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_punch;
2023 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
2024 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
2025 #define OST_BRW_READ OST_READ
2026 #define OST_BRW_WRITE OST_WRITE
2028 static struct tgt_handler ofd_tgt_handlers[] = {
2029 TGT_RPC_HANDLER(OST_FIRST_OPC,
2030 0, OST_CONNECT, tgt_connect,
2031 &RQF_CONNECT, LUSTRE_OBD_VERSION),
2032 TGT_RPC_HANDLER(OST_FIRST_OPC,
2033 0, OST_DISCONNECT, tgt_disconnect,
2034 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
2035 TGT_RPC_HANDLER(OST_FIRST_OPC,
2036 0, OST_SET_INFO, ofd_set_info_hdl,
2037 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
2038 TGT_OST_HDL(0, OST_GET_INFO, ofd_get_info_hdl),
2039 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR, ofd_getattr_hdl),
2040 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2041 OST_SETATTR, ofd_setattr_hdl),
2042 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
2043 OST_CREATE, ofd_create_hdl),
2044 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
2045 OST_DESTROY, ofd_destroy_hdl),
2046 TGT_OST_HDL(0 | HABEO_REFERO, OST_STATFS, ofd_statfs_hdl),
2047 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO,
2048 OST_BRW_READ, tgt_brw_read,
2050 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
2051 TGT_OST_HDL_HP(HABEO_CORPUS| MUTABOR, OST_BRW_WRITE, tgt_brw_write,
2053 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2054 OST_PUNCH, ofd_punch_hdl,
2056 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC, ofd_sync_hdl),
2057 TGT_OST_HDL(0 | HABEO_REFERO, OST_QUOTACTL, ofd_quotactl),
2060 static struct tgt_opc_slice ofd_common_slice[] = {
2062 .tos_opc_start = OST_FIRST_OPC,
2063 .tos_opc_end = OST_LAST_OPC,
2064 .tos_hs = ofd_tgt_handlers
2067 .tos_opc_start = OBD_FIRST_OPC,
2068 .tos_opc_end = OBD_LAST_OPC,
2069 .tos_hs = tgt_obd_handlers
2072 .tos_opc_start = LDLM_FIRST_OPC,
2073 .tos_opc_end = LDLM_LAST_OPC,
2074 .tos_hs = tgt_dlm_handlers
2077 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
2078 .tos_opc_end = OUT_UPDATE_LAST_OPC,
2079 .tos_hs = tgt_out_handlers
2082 .tos_opc_start = SEQ_FIRST_OPC,
2083 .tos_opc_end = SEQ_LAST_OPC,
2084 .tos_hs = seq_handlers
2087 .tos_opc_start = LFSCK_FIRST_OPC,
2088 .tos_opc_end = LFSCK_LAST_OPC,
2089 .tos_hs = tgt_lfsck_handlers
2096 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
2097 struct lu_device_type *ldt, struct lustre_cfg *cfg)
2099 const char *dev = lustre_cfg_string(cfg, 0);
2100 struct ofd_thread_info *info = NULL;
2101 struct obd_device *obd;
2102 struct obd_statfs *osfs;
2107 obd = class_name2obd(dev);
2109 CERROR("Cannot find obd with name %s\n", dev);
2113 rc = lu_env_refill((struct lu_env *)env);
2117 obd->u.obt.obt_magic = OBT_MAGIC;
2119 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
2120 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
2122 spin_lock_init(&m->ofd_flags_lock);
2123 m->ofd_raid_degraded = 0;
2124 m->ofd_syncjournal = 0;
2126 m->ofd_grant_compat_disable = 0;
2127 m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
2130 spin_lock_init(&m->ofd_osfs_lock);
2131 m->ofd_osfs_age = cfs_time_shift_64(-1000);
2132 m->ofd_osfs_unstable = 0;
2133 m->ofd_statfs_inflight = 0;
2134 m->ofd_osfs_inflight = 0;
2137 spin_lock_init(&m->ofd_grant_lock);
2138 m->ofd_tot_dirty = 0;
2139 m->ofd_tot_granted = 0;
2140 m->ofd_tot_pending = 0;
2141 m->ofd_seq_count = 0;
2142 init_waitqueue_head(&m->ofd_inconsistency_thread.t_ctl_waitq);
2143 INIT_LIST_HEAD(&m->ofd_inconsistency_list);
2144 spin_lock_init(&m->ofd_inconsistency_lock);
2146 spin_lock_init(&m->ofd_batch_lock);
2147 init_rwsem(&m->ofd_lastid_rwsem);
2149 obd->u.filter.fo_fl_oss_capa = 0;
2150 CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
2151 obd->u.filter.fo_capa_hash = init_capa_hash();
2152 if (obd->u.filter.fo_capa_hash == NULL)
2155 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
2156 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
2157 /* set this lu_device to obd, because error handling need it */
2158 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
2160 rc = ofd_procfs_init(m);
2162 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
2166 /* No connection accepted until configurations will finish */
2167 spin_lock(&obd->obd_dev_lock);
2168 obd->obd_no_conn = 1;
2169 spin_unlock(&obd->obd_dev_lock);
2170 obd->obd_replayable = 1;
2171 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
2172 char *str = lustre_cfg_string(cfg, 4);
2174 if (strchr(str, 'n')) {
2175 CWARN("%s: recovery disabled\n", obd->obd_name);
2176 obd->obd_replayable = 0;
2180 info = ofd_info_init(env, NULL);
2184 rc = ofd_stack_init(env, m, cfg);
2186 CERROR("Can't init device stack, rc %d\n", rc);
2187 GOTO(err_fini_proc, rc);
2190 ofd_procfs_add_brw_stats_symlink(m);
2192 /* populate cached statfs data */
2193 osfs = &ofd_info(env)->fti_u.osfs;
2194 rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
2196 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
2197 GOTO(err_fini_stack, rc);
2199 if (!IS_PO2(osfs->os_bsize)) {
2200 CERROR("%s: blocksize (%d) is not a power of 2\n",
2201 obd->obd_name, osfs->os_bsize);
2202 GOTO(err_fini_stack, rc = -EPROTO);
2204 m->ofd_blockbits = fls(osfs->os_bsize) - 1;
2206 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
2207 if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
2208 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
2210 snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
2211 "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
2212 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
2213 LDLM_NAMESPACE_SERVER,
2214 LDLM_NAMESPACE_GREEDY,
2216 if (m->ofd_namespace == NULL)
2217 GOTO(err_fini_stack, rc = -ENOMEM);
2218 /* set obd_namespace for compatibility with old code */
2219 obd->obd_namespace = m->ofd_namespace;
2220 ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
2221 m->ofd_namespace->ns_lvbo = &ofd_lvbo;
2222 m->ofd_namespace->ns_lvbp = m;
2224 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2225 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
2227 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
2229 /* Allow at most ddp_grant_reserved% of the available filesystem space
2230 * to be granted to clients, so that any errors in the grant overhead
2231 * calculations do not allow granting more space to clients than can be
2232 * written. Assumes that in aggregate the grant overhead calculations do
2233 * not have more than ddp_grant_reserved% estimation error in them. */
2234 m->ofd_grant_ratio =
2235 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
2237 rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
2238 OBD_FAIL_OST_ALL_REQUEST_NET,
2239 OBD_FAIL_OST_ALL_REPLY_NET);
2241 GOTO(err_free_ns, rc);
2243 rc = ofd_fs_setup(env, m, obd);
2245 GOTO(err_fini_lut, rc);
2247 rc = ofd_start_inconsistency_verification_thread(m);
2249 GOTO(err_fini_fs, rc);
2254 ofd_fs_cleanup(env, m);
2256 tgt_fini(env, &m->ofd_lut);
2258 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
2259 obd->obd_namespace = m->ofd_namespace = NULL;
2261 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
2267 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
2269 struct obd_device *obd = ofd_obd(m);
2270 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
2271 struct lfsck_stop stop;
2273 stop.ls_status = LS_PAUSED;
2275 lfsck_stop(env, m->ofd_osd, &stop);
2276 target_recovery_fini(obd);
2277 obd_exports_barrier(obd);
2278 obd_zombie_barrier();
2280 tgt_fini(env, &m->ofd_lut);
2281 ofd_stop_inconsistency_verification_thread(m);
2282 lfsck_degister(env, m->ofd_osd);
2283 ofd_fs_cleanup(env, m);
2285 ofd_free_capa_keys(m);
2286 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
2288 if (m->ofd_namespace != NULL) {
2289 ldlm_namespace_free(m->ofd_namespace, NULL,
2290 d->ld_obd->obd_force);
2291 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
2294 ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
2296 LASSERT(atomic_read(&d->ld_ref) == 0);
2297 server_put_mount(obd->obd_name, true);
2301 static struct lu_device *ofd_device_fini(const struct lu_env *env,
2302 struct lu_device *d)
2305 ofd_fini(env, ofd_dev(d));
2309 static struct lu_device *ofd_device_free(const struct lu_env *env,
2310 struct lu_device *d)
2312 struct ofd_device *m = ofd_dev(d);
2314 dt_device_fini(&m->ofd_dt_dev);
2319 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
2320 struct lu_device_type *t,
2321 struct lustre_cfg *cfg)
2323 struct ofd_device *m;
2324 struct lu_device *l;
2329 return ERR_PTR(-ENOMEM);
2331 l = &m->ofd_dt_dev.dd_lu_dev;
2332 dt_device_init(&m->ofd_dt_dev, t);
2333 rc = ofd_init0(env, m, t, cfg);
2335 ofd_device_free(env, l);
2342 /* thread context key constructor/destructor */
2343 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
2345 static void ofd_key_exit(const struct lu_context *ctx,
2346 struct lu_context_key *key, void *data)
2348 struct ofd_thread_info *info = data;
2350 info->fti_env = NULL;
2351 info->fti_exp = NULL;
2354 info->fti_pre_version = 0;
2357 memset(&info->fti_attr, 0, sizeof info->fti_attr);
2360 struct lu_context_key ofd_thread_key = {
2361 .lct_tags = LCT_DT_THREAD,
2362 .lct_init = ofd_key_init,
2363 .lct_fini = ofd_key_fini,
2364 .lct_exit = ofd_key_exit
2367 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
2368 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
2370 static struct lu_device_type_operations ofd_device_type_ops = {
2371 .ldto_init = ofd_type_init,
2372 .ldto_fini = ofd_type_fini,
2374 .ldto_start = ofd_type_start,
2375 .ldto_stop = ofd_type_stop,
2377 .ldto_device_alloc = ofd_device_alloc,
2378 .ldto_device_free = ofd_device_free,
2379 .ldto_device_fini = ofd_device_fini
2382 static struct lu_device_type ofd_device_type = {
2383 .ldt_tags = LU_DEVICE_DT,
2384 .ldt_name = LUSTRE_OST_NAME,
2385 .ldt_ops = &ofd_device_type_ops,
2386 .ldt_ctx_tags = LCT_DT_THREAD
2389 int __init ofd_init(void)
2393 rc = lu_kmem_init(ofd_caches);
2397 rc = ofd_fmd_init();
2399 lu_kmem_fini(ofd_caches);
2403 rc = class_register_type(&ofd_obd_ops, NULL, true, NULL,
2404 #ifndef HAVE_ONLY_PROCFS_SEQ
2407 LUSTRE_OST_NAME, &ofd_device_type);
2411 void __exit ofd_exit(void)
2414 lu_kmem_fini(ofd_caches);
2415 class_unregister_type(LUSTRE_OST_NAME);
2418 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
2419 MODULE_DESCRIPTION("Lustre Object Filtering Device");
2420 MODULE_LICENSE("GPL");
2422 module_init(ofd_init);
2423 module_exit(ofd_exit);