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",
84 m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next);
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 m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, 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);
190 /* For interoperability, see mdt_interop_param[]. */
191 static struct cfg_interop_param ofd_interop_param[] = {
192 { "ost.quota_type", NULL },
196 /* Some parameters were moved from ofd to osd and only their
197 * symlinks were kept in ofd by LU-3106. They are:
198 * -writehthrough_cache_enable
199 * -readcache_max_filese
202 * Since they are not included by the static lprocfs var list,
203 * a pre-check is added for them to avoid "unknown param" error
204 * message confuses the customer. If they are matched in this
205 * check, they will be passed to the osd directly.
207 static bool match_symlink_param(char *param)
212 if (class_match_param(param, PARAM_OST, ¶m) == 0) {
213 sval = strchr(param, '=');
215 paramlen = sval - param;
216 if (strncmp(param, "writethrough_cache_enable",
218 strncmp(param, "readcache_max_filesize",
220 strncmp(param, "read_cache_enable",
222 strncmp(param, "brw_stats", paramlen) == 0)
230 /* used by MGS to process specific configurations */
231 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
232 struct lustre_cfg *cfg)
234 struct ofd_device *m = ofd_dev(d);
235 struct dt_device *dt_next = m->ofd_osd;
236 struct lu_device *next = &dt_next->dd_lu_dev;
241 switch (cfg->lcfg_command) {
243 struct lprocfs_static_vars lvars;
245 /* For interoperability */
246 struct cfg_interop_param *ptr = NULL;
247 struct lustre_cfg *old_cfg = NULL;
250 param = lustre_cfg_string(cfg, 1);
252 CERROR("param is empty\n");
257 ptr = class_find_old_param(param, ofd_interop_param);
259 if (ptr->new_param == NULL) {
261 CWARN("For interoperability, skip this %s."
262 " It is obsolete.\n", ptr->old_param);
266 CWARN("Found old param %s, changed it to %s.\n",
267 ptr->old_param, ptr->new_param);
270 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
277 if (match_symlink_param(param)) {
278 rc = next->ld_ops->ldo_process_config(env, next, cfg);
282 lprocfs_ofd_init_vars(&lvars);
283 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
285 if (rc > 0 || rc == -ENOSYS) {
286 CDEBUG(D_CONFIG, "pass param %s down the stack.\n",
288 /* we don't understand; pass it on */
289 rc = next->ld_ops->ldo_process_config(env, next, cfg);
293 case LCFG_SPTLRPC_CONF: {
298 /* others are passed further */
299 rc = next->ld_ops->ldo_process_config(env, next, cfg);
305 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
306 const struct lu_object_conf *conf)
308 struct ofd_device *d = ofd_dev(o->lo_dev);
309 struct lu_device *under;
310 struct lu_object *below;
315 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
316 PFID(lu_object_fid(o)));
318 under = &d->ofd_osd->dd_lu_dev;
319 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
321 lu_object_add(o, below);
328 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
330 struct ofd_object *of = ofd_obj(o);
331 struct lu_object_header *h;
336 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
337 PFID(lu_object_fid(o)));
340 lu_object_header_fini(h);
341 OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
345 static int ofd_object_print(const struct lu_env *env, void *cookie,
346 lu_printer_t p, const struct lu_object *o)
348 return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
351 struct lu_object_operations ofd_obj_ops = {
352 .loo_object_init = ofd_object_init,
353 .loo_object_free = ofd_object_free,
354 .loo_object_print = ofd_object_print
357 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
358 const struct lu_object_header *hdr,
361 struct ofd_object *of;
365 OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, __GFP_IO);
368 struct lu_object_header *h;
370 o = &of->ofo_obj.do_lu;
372 lu_object_header_init(h);
373 lu_object_init(o, h, d);
374 lu_object_add_top(h, o);
375 o->lo_ops = &ofd_obj_ops;
382 extern int ost_handle(struct ptlrpc_request *req);
384 static int ofd_lfsck_out_notify(const struct lu_env *env, void *data,
385 enum lfsck_events event)
387 struct ofd_device *ofd = data;
388 struct obd_device *obd = ofd_obd(ofd);
391 case LE_LASTID_REBUILDING:
392 CWARN("%s: Found crashed LAST_ID, deny creating new OST-object "
393 "on the device until the LAST_ID rebuilt successfully.\n",
395 down_write(&ofd->ofd_lastid_rwsem);
396 ofd->ofd_lastid_rebuilding = 1;
397 up_write(&ofd->ofd_lastid_rwsem);
399 case LE_LASTID_REBUILT: {
400 down_write(&ofd->ofd_lastid_rwsem);
401 ofd_seqs_free(env, ofd);
402 ofd->ofd_lastid_rebuilding = 0;
403 ofd->ofd_lastid_gen++;
404 up_write(&ofd->ofd_lastid_rwsem);
408 CERROR("%s: unknown lfsck event: rc = %d\n",
409 ofd_obd(ofd)->obd_name, event);
416 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
417 struct lu_device *dev)
419 struct ofd_thread_info *info;
420 struct ofd_device *ofd = ofd_dev(dev);
421 struct obd_device *obd = ofd_obd(ofd);
422 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
423 struct lfsck_start_param lsp;
428 info = ofd_info_init(env, NULL);
432 /* initialize lower device */
433 rc = next->ld_ops->ldo_prepare(env, dev, next);
437 rc = lfsck_register(env, ofd->ofd_osd, ofd->ofd_osd, obd,
438 ofd_lfsck_out_notify, ofd, false);
440 CERROR("%s: failed to initialize lfsck: rc = %d\n",
445 lsp.lsp_namespace = ofd->ofd_namespace;
446 lsp.lsp_start = NULL;
447 lsp.lsp_index_valid = 0;
448 rc = lfsck_start(env, ofd->ofd_osd, &lsp);
450 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
455 target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
456 LASSERT(obd->obd_no_conn);
457 spin_lock(&obd->obd_dev_lock);
458 obd->obd_no_conn = 0;
459 spin_unlock(&obd->obd_dev_lock);
461 if (obd->obd_recovering == 0)
462 ofd_postrecov(env, ofd);
467 static int ofd_recovery_complete(const struct lu_env *env,
468 struct lu_device *dev)
470 struct ofd_device *ofd = ofd_dev(dev);
471 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
472 int rc = 0, max_precreate;
476 /* Grant space for object precreation on the self export.
477 * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
478 * is enough to create 10k objects. More space is then acquired for
479 * precreation in ofd_grant_create().
481 max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
482 ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
484 rc = next->ld_ops->ldo_recovery_complete(env, next);
488 static struct lu_device_operations ofd_lu_ops = {
489 .ldo_object_alloc = ofd_object_alloc,
490 .ldo_process_config = ofd_process_config,
491 .ldo_recovery_complete = ofd_recovery_complete,
492 .ldo_prepare = ofd_prepare,
495 static int ofd_procfs_init(struct ofd_device *ofd)
497 struct lprocfs_static_vars lvars;
498 struct obd_device *obd = ofd_obd(ofd);
499 cfs_proc_dir_entry_t *entry;
504 /* lprocfs must be setup before the ofd so state can be safely added
505 * to /proc incrementally as the ofd is setup */
506 lprocfs_ofd_init_vars(&lvars);
507 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
509 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
514 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
516 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
518 GOTO(obd_cleanup, rc);
521 obd->obd_uses_nid_stats = 1;
523 entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
526 CERROR("%s: error %d setting up lprocfs for %s\n",
527 obd->obd_name, rc, "exports");
528 GOTO(obd_cleanup, rc);
530 obd->obd_proc_exports_entry = entry;
532 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
533 lprocfs_nid_stats_clear_read,
534 lprocfs_nid_stats_clear_write, obd, NULL);
537 CERROR("%s: add proc entry 'clear' failed: %d.\n",
539 GOTO(obd_cleanup, rc);
542 ofd_stats_counter_init(obd->obd_stats);
544 rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
545 ofd_stats_counter_init);
547 GOTO(remove_entry_clear, rc);
550 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
552 lprocfs_obd_cleanup(obd);
553 lprocfs_free_obd_stats(obd);
558 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
560 struct obd_device *obd = ofd_obd(ofd);
561 struct obd_device *osd_obd = ofd->ofd_osd_exp->exp_obd;
562 cfs_proc_dir_entry_t *osd_root = osd_obd->obd_type->typ_procroot;
563 cfs_proc_dir_entry_t *osd_dir;
565 osd_dir = lprocfs_srch(osd_root, obd->obd_name);
569 if (lprocfs_srch(osd_dir, "brw_stats") != NULL)
570 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
571 "../../%s/%s/brw_stats",
572 osd_root->name, osd_dir->name);
574 if (lprocfs_srch(osd_dir, "read_cache_enable") != NULL)
575 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
576 "../../%s/%s/read_cache_enable",
577 osd_root->name, osd_dir->name);
579 if (lprocfs_srch(osd_dir, "readcache_max_filesize") != NULL)
580 lprocfs_add_symlink("readcache_max_filesize",
582 "../../%s/%s/readcache_max_filesize",
583 osd_root->name, osd_dir->name);
585 if (lprocfs_srch(osd_dir, "writethrough_cache_enable") != NULL)
586 lprocfs_add_symlink("writethrough_cache_enable",
588 "../../%s/%s/writethrough_cache_enable",
589 osd_root->name, osd_dir->name);
592 static void ofd_procfs_fini(struct ofd_device *ofd)
594 struct obd_device *obd = ofd_obd(ofd);
596 lprocfs_remove_proc_entry("writethrough_cache_enable",
597 obd->obd_proc_entry);
598 lprocfs_remove_proc_entry("readcache_max_filesize",
599 obd->obd_proc_entry);
600 lprocfs_remove_proc_entry("read_cache_enable", obd->obd_proc_entry);
601 lprocfs_remove_proc_entry("brw_stats", obd->obd_proc_entry);
602 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
603 lprocfs_free_per_client_stats(obd);
604 lprocfs_obd_cleanup(obd);
605 lprocfs_free_obd_stats(obd);
606 lprocfs_job_stats_fini(obd);
609 extern int ost_handle(struct ptlrpc_request *req);
611 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
613 return seq_site_fini(env, &ofd->ofd_seq_site);
616 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
618 struct seq_server_site *ss = &ofd->ofd_seq_site;
619 struct lu_device *lu = &ofd->ofd_dt_dev.dd_lu_dev;
620 char *obd_name = ofd_name(ofd);
624 ss = &ofd->ofd_seq_site;
625 lu->ld_site->ld_seq_site = ss;
626 ss->ss_lu = lu->ld_site;
627 ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
629 OBD_ALLOC_PTR(ss->ss_server_seq);
630 if (ss->ss_server_seq == NULL)
631 GOTO(out_free, rc = -ENOMEM);
633 OBD_ALLOC(name, strlen(obd_name) + 10);
635 OBD_FREE_PTR(ss->ss_server_seq);
636 ss->ss_server_seq = NULL;
637 GOTO(out_free, rc = -ENOMEM);
640 rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
641 LUSTRE_SEQ_SERVER, ss);
643 CERROR("%s : seq server init error %d\n", obd_name, rc);
646 ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
648 OBD_ALLOC_PTR(ss->ss_client_seq);
649 if (ss->ss_client_seq == NULL)
650 GOTO(out_free, rc = -ENOMEM);
652 snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
653 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
656 CERROR("%s : seq client init error %d\n", obd_name, rc);
659 OBD_FREE(name, strlen(obd_name) + 10);
662 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
666 if (ss->ss_server_seq) {
667 seq_server_fini(ss->ss_server_seq, env);
668 OBD_FREE_PTR(ss->ss_server_seq);
669 ss->ss_server_seq = NULL;
672 if (ss->ss_client_seq) {
673 seq_client_fini(ss->ss_client_seq);
674 OBD_FREE_PTR(ss->ss_client_seq);
675 ss->ss_client_seq = NULL;
679 OBD_FREE(name, strlen(obd_name) + 10);
687 int ofd_set_info_hdl(struct tgt_session_info *tsi)
689 struct ptlrpc_request *req = tgt_ses_req(tsi);
690 struct ost_body *body = NULL, *repbody;
691 void *key, *val = NULL;
692 int keylen, vallen, rc = 0;
693 bool is_grant_shrink;
694 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
698 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
700 DEBUG_REQ(D_HA, req, "no set_info key");
701 RETURN(err_serious(-EFAULT));
703 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
706 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
708 DEBUG_REQ(D_HA, req, "no set_info val");
709 RETURN(err_serious(-EFAULT));
711 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
714 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
716 /* In this case the value is actually an RMF_OST_BODY, so we
717 * transmutate the type of this PTLRPC */
718 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
720 rc = req_capsule_server_pack(tsi->tsi_pill);
724 if (is_grant_shrink) {
725 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
727 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
730 /** handle grant shrink, similar to a read request */
731 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
733 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
735 obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
737 } else if (KEY_IS(KEY_CAPA_KEY)) {
738 rc = ofd_update_capa_key(ofd, val);
739 } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
740 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
742 CERROR("%s: Unsupported key %s\n",
743 tgt_name(tsi->tsi_tgt), (char *)key);
746 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
752 int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
753 struct lu_fid *fid, struct ll_user_fiemap *fiemap)
755 struct ofd_object *fo;
758 fo = ofd_object_find(env, ofd, fid);
760 CERROR("%s: error finding object "DFID"\n",
761 ofd_name(ofd), PFID(fid));
765 ofd_read_lock(env, fo);
766 if (ofd_object_exists(fo))
767 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
770 ofd_read_unlock(env, fo);
771 ofd_object_put(env, fo);
775 struct locked_region {
777 struct lustre_handle lh;
780 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
781 unsigned long long begin, unsigned long long end,
784 struct locked_region *region = NULL;
788 LASSERT(begin <= end);
789 OBD_ALLOC_PTR(region);
793 rc = tgt_extent_lock(ns, res_id, begin, end, ®ion->lh,
798 CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
800 cfs_list_add(®ion->list, locked);
805 static int lock_zero_regions(struct ldlm_namespace *ns,
806 struct ldlm_res_id *res_id,
807 struct ll_user_fiemap *fiemap,
810 __u64 begin = fiemap->fm_start;
813 struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
817 CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
818 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
819 if (fiemap_start[i].fe_logical > begin) {
820 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
821 begin, fiemap_start[i].fe_logical);
822 rc = lock_region(ns, res_id, begin,
823 fiemap_start[i].fe_logical, locked);
828 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
831 if (begin < (fiemap->fm_start + fiemap->fm_length)) {
832 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
833 begin, fiemap->fm_start + fiemap->fm_length);
834 rc = lock_region(ns, res_id, begin,
835 fiemap->fm_start + fiemap->fm_length, locked);
841 static void unlock_zero_regions(struct ldlm_namespace *ns, cfs_list_t *locked)
843 struct locked_region *entry, *temp;
845 cfs_list_for_each_entry_safe(entry, temp, locked, list) {
846 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
847 tgt_extent_unlock(&entry->lh, LCK_PR);
848 cfs_list_del(&entry->list);
853 int ofd_get_info_hdl(struct tgt_session_info *tsi)
855 struct obd_export *exp = tsi->tsi_exp;
856 struct ofd_device *ofd = ofd_exp(exp);
857 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
860 int replylen, rc = 0;
864 /* this common part for get_info rpc */
865 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
867 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
868 RETURN(err_serious(-EPROTO));
870 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
873 if (KEY_IS(KEY_LAST_ID)) {
875 struct ofd_seq *oseq;
877 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
878 rc = req_capsule_server_pack(tsi->tsi_pill);
880 RETURN(err_serious(rc));
882 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
884 oseq = ofd_seq_load(tsi->tsi_env, ofd,
885 (obd_seq)exp->exp_filter_data.fed_group);
889 *last_id = ofd_seq_last_oid(oseq);
890 ofd_seq_put(tsi->tsi_env, oseq);
891 } else if (KEY_IS(KEY_FIEMAP)) {
892 struct ll_fiemap_info_key *fm_key;
893 struct ll_user_fiemap *fiemap;
896 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
898 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
899 rc = tgt_validate_obdo(tsi, &fm_key->oa);
901 RETURN(err_serious(rc));
903 fid = &fm_key->oa.o_oi.oi_fid;
905 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
907 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
908 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
909 RCL_SERVER, replylen);
911 rc = req_capsule_server_pack(tsi->tsi_pill);
913 RETURN(err_serious(rc));
915 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
919 *fiemap = fm_key->fiemap;
920 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
922 /* LU-3219: Lock the sparse areas to make sure dirty
923 * flushed back from client, then call fiemap again. */
924 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
925 fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
926 cfs_list_t locked = CFS_LIST_HEAD_INIT(locked);
928 ost_fid_build_resid(fid, &fti->fti_resid);
929 rc = lock_zero_regions(ofd->ofd_namespace,
930 &fti->fti_resid, fiemap,
932 if (rc == 0 && !cfs_list_empty(&locked)) {
933 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
935 unlock_zero_regions(ofd->ofd_namespace,
939 } else if (KEY_IS(KEY_LAST_FID)) {
940 struct ofd_device *ofd = ofd_exp(exp);
941 struct ofd_seq *oseq;
945 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
946 rc = req_capsule_server_pack(tsi->tsi_pill);
948 RETURN(err_serious(rc));
950 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
952 RETURN(err_serious(-EPROTO));
954 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
956 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
960 oseq = ofd_seq_load(tsi->tsi_env, ofd,
961 ostid_seq(&fti->fti_ostid));
963 RETURN(PTR_ERR(oseq));
965 rc = ostid_to_fid(fid, &oseq->os_oi,
966 ofd->ofd_lut.lut_lsd.lsd_osd_index);
970 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
973 ofd_seq_put(tsi->tsi_env, oseq);
975 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
979 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
985 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
987 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
988 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
989 struct ost_body *repbody;
990 struct lustre_handle lh = { 0 };
991 struct ofd_object *fo;
993 ldlm_mode_t lock_mode = LCK_PR;
998 LASSERT(tsi->tsi_ost_body != NULL);
1000 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1001 if (repbody == NULL)
1004 repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
1005 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1007 srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
1008 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
1011 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
1014 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
1015 &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
1021 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1023 GOTO(out, rc = PTR_ERR(fo));
1025 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1029 obdo_from_la(&repbody->oa, &fti->fti_attr,
1030 OFD_VALID_FLAGS | LA_UID | LA_GID);
1031 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1033 /* Store object version in reply */
1034 curr_version = dt_version_get(tsi->tsi_env,
1035 ofd_object_child(fo));
1036 if ((__s64)curr_version != -EOPNOTSUPP) {
1037 repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
1038 repbody->oa.o_data_version = curr_version;
1042 ofd_object_put(tsi->tsi_env, fo);
1045 tgt_extent_unlock(&lh, lock_mode);
1047 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
1050 repbody->oa.o_valid |= OBD_MD_FLFLAGS;
1051 repbody->oa.o_flags = OBD_FL_FLUSH;
1056 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
1058 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1059 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1060 struct ost_body *body = tsi->tsi_ost_body;
1061 struct ost_body *repbody;
1062 struct ldlm_resource *res;
1063 struct ofd_object *fo;
1064 struct filter_fid *ff = NULL;
1069 LASSERT(body != NULL);
1071 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1072 if (repbody == NULL)
1075 repbody->oa.o_oi = body->oa.o_oi;
1076 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1078 /* This would be very bad - accidentally truncating a file when
1079 * changing the time or similar - bug 12203. */
1080 if (body->oa.o_valid & OBD_MD_FLSIZE &&
1081 body->oa.o_size != OBD_OBJECT_EOF) {
1082 static char mdsinum[48];
1084 if (body->oa.o_valid & OBD_MD_FLFID)
1085 snprintf(mdsinum, sizeof(mdsinum) - 1,
1086 "of parent "DFID, body->oa.o_parent_seq,
1087 body->oa.o_parent_oid, 0);
1091 CERROR("%s: setattr from %s is trying to truncate object "DFID
1092 " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1093 PFID(&tsi->tsi_fid), mdsinum);
1097 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1099 GOTO(out, rc = PTR_ERR(fo));
1101 la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1102 fti->fti_attr.la_valid &= ~LA_TYPE;
1104 if (body->oa.o_valid & OBD_MD_FLFID) {
1105 ff = &fti->fti_mds_fid;
1106 ofd_prepare_fidea(ff, &body->oa);
1109 /* setting objects attributes (including owner/group) */
1110 rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1114 obdo_from_la(&repbody->oa, &fti->fti_attr,
1115 OFD_VALID_FLAGS | LA_UID | LA_GID);
1116 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1118 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1122 ofd_object_put(tsi->tsi_env, fo);
1125 /* we do not call this before to avoid lu_object_find() in
1126 * ->lvbo_update() holding another reference on the object.
1127 * otherwise concurrent destroy can make the object unavailable
1128 * for 2nd lu_object_find() waiting for the first reference
1129 * to go... deadlock! */
1130 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1131 &tsi->tsi_resid, LDLM_EXTENT, 0);
1133 ldlm_res_lvbo_update(res, NULL, 0);
1134 ldlm_resource_putref(res);
1140 static int ofd_orphans_destroy(const struct lu_env *env,
1141 struct obd_export *exp,
1142 struct ofd_device *ofd, struct obdo *oa)
1144 struct ofd_thread_info *info = ofd_info(env);
1145 struct lu_fid *fid = &info->fti_fid;
1146 struct ost_id *oi = &oa->o_oi;
1147 struct ofd_seq *oseq;
1148 obd_seq seq = ostid_seq(oi);
1149 obd_id end_id = ostid_id(oi);
1157 oseq = ofd_seq_get(ofd, seq);
1159 CERROR("%s: Can not find seq for "DOSTID"\n",
1160 ofd_name(ofd), POSTID(oi));
1165 last = ofd_seq_last_oid(oseq);
1168 LASSERT(exp != NULL);
1169 skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1171 LCONSOLE(D_INFO, "%s: deleting orphan objects from "DOSTID
1172 " to "DOSTID"\n", ofd_name(ofd), seq, end_id + 1, seq, last);
1174 while (oid > end_id) {
1175 rc = fid_set_id(fid, oid);
1176 if (unlikely(rc != 0))
1179 rc = ofd_destroy_by_fid(env, ofd, fid, 1);
1180 if (rc != 0 && rc != -ENOENT) /* this is pretty fatal... */
1181 CEMERG("%s: error destroying precreated id "DFID
1182 ": rc = %d\n", ofd_name(ofd), PFID(fid), rc);
1186 ofd_seq_last_oid_set(oseq, oid);
1187 /* update last_id on disk periodically so that if we
1188 * restart * we don't need to re-scan all of the just
1189 * deleted objects. */
1190 if ((oid & 511) == 0)
1191 ofd_seq_last_oid_write(env, ofd, oseq);
1195 CDEBUG(D_HA, "%s: after destroy: set last_id to "DOSTID"\n",
1196 ofd_name(ofd), seq, oid);
1199 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1201 /* don't reuse orphan object, return last used objid */
1202 ostid_set_id(oi, last);
1209 ofd_seq_put(env, oseq);
1213 static int ofd_create_hdl(struct tgt_session_info *tsi)
1215 struct ost_body *repbody;
1216 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1217 struct obdo *rep_oa;
1218 struct obd_export *exp = tsi->tsi_exp;
1219 struct ofd_device *ofd = ofd_exp(exp);
1220 obd_seq seq = ostid_seq(&oa->o_oi);
1221 obd_id oid = ostid_id(&oa->o_oi);
1222 struct ofd_seq *oseq;
1228 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1231 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1232 if (repbody == NULL)
1235 down_read(&ofd->ofd_lastid_rwsem);
1236 /* Currently, for safe, we do not distinguish which LAST_ID is broken,
1237 * we may do that in the future.
1238 * Return -ENOSPC until the LAST_ID rebuilt. */
1239 if (unlikely(ofd->ofd_lastid_rebuilding))
1240 GOTO(out_sem, rc = -ENOSPC);
1242 rep_oa = &repbody->oa;
1243 rep_oa->o_oi = oa->o_oi;
1245 LASSERT(seq >= FID_SEQ_OST_MDT0);
1246 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1248 CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1250 oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1252 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1253 ofd_name(ofd), seq, PTR_ERR(oseq));
1254 GOTO(out_sem, rc = -EINVAL);
1257 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1258 (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1259 if (!ofd_obd(ofd)->obd_recovering ||
1260 oid > ofd_seq_last_oid(oseq)) {
1261 CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1262 "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1263 ofd_seq_last_oid(oseq));
1264 GOTO(out_nolock, rc = -EINVAL);
1266 /* Do nothing here, we re-create objects during recovery
1267 * upon write replay, see ofd_preprw_write() */
1268 GOTO(out_nolock, rc = 0);
1270 /* former ofd_handle_precreate */
1271 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1272 (oa->o_flags & OBD_FL_DELORPHAN)) {
1273 exp->exp_filter_data.fed_lastid_gen = ofd->ofd_lastid_gen;
1275 /* destroy orphans */
1276 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1277 exp->exp_conn_cnt) {
1278 CERROR("%s: dropping old orphan cleanup request\n",
1280 GOTO(out_nolock, rc = 0);
1282 /* This causes inflight precreates to abort and drop lock */
1283 oseq->os_destroys_in_progress = 1;
1284 mutex_lock(&oseq->os_create_lock);
1285 if (!oseq->os_destroys_in_progress) {
1286 CERROR("%s:["LPU64"] destroys_in_progress already"
1287 " cleared\n", ofd_name(ofd), seq);
1288 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1291 diff = oid - ofd_seq_last_oid(oseq);
1292 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1293 ofd_seq_last_oid(oseq), diff);
1294 if (-diff > OST_MAX_PRECREATE) {
1295 /* FIXME: should reset precreate_next_id on MDS */
1297 } else if (diff < 0) {
1298 rc = ofd_orphans_destroy(tsi->tsi_env, exp,
1300 oseq->os_destroys_in_progress = 0;
1302 /* XXX: Used by MDS for the first time! */
1303 oseq->os_destroys_in_progress = 0;
1306 if (unlikely(exp->exp_filter_data.fed_lastid_gen !=
1307 ofd->ofd_lastid_gen)) {
1308 ofd_obd_disconnect(exp);
1309 GOTO(out_nolock, rc = -ENOTCONN);
1312 mutex_lock(&oseq->os_create_lock);
1313 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1314 exp->exp_conn_cnt) {
1315 CERROR("%s: dropping old precreate request\n",
1319 /* only precreate if seq is 0, IDIF or normal and also o_id
1320 * must be specfied */
1321 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1322 !fid_seq_is_idif(seq)) || oid == 0) {
1323 diff = 1; /* shouldn't we create this right now? */
1325 diff = oid - ofd_seq_last_oid(oseq);
1326 /* Do sync create if the seq is about to used up */
1327 if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1328 if (unlikely(oid >= IDIF_MAX_OID - 1))
1330 } else if (fid_seq_is_norm(seq)) {
1332 LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1335 CERROR("%s : invalid o_seq "DOSTID"\n",
1336 ofd_name(ofd), POSTID(&oa->o_oi));
1337 GOTO(out, rc = -EINVAL);
1342 cfs_time_t enough_time = cfs_time_shift(DISK_TIMEOUT);
1347 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1348 !(oa->o_flags & OBD_FL_DELORPHAN)) {
1349 /* don't enforce grant during orphan recovery */
1350 rc = ofd_grant_create(tsi->tsi_env,
1351 ofd_obd(ofd)->obd_self_export,
1354 CDEBUG(D_HA, "%s: failed to acquire grant "
1355 "space for precreate (%d): rc = %d\n",
1356 ofd_name(ofd), diff, rc);
1361 /* This can happen if a new OST is formatted and installed
1362 * in place of an old one at the same index. Instead of
1363 * precreating potentially millions of deleted old objects
1364 * (possibly filling the OST), only precreate the last batch.
1365 * LFSCK will eventually clean up any orphans. LU-14 */
1366 if (diff > 5 * OST_MAX_PRECREATE) {
1367 diff = OST_MAX_PRECREATE / 2;
1368 LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1369 "larger than the LAST_ID "DOSTID", only "
1370 "precreating the last %u objects.\n",
1371 ofd_name(ofd), POSTID(&oa->o_oi),
1372 5 * OST_MAX_PRECREATE,
1373 POSTID(&oseq->os_oi), diff);
1374 ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1378 next_id = ofd_seq_last_oid(oseq) + 1;
1379 count = ofd_precreate_batch(ofd, diff);
1381 CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1382 " at "LPU64"\n", ofd_name(ofd),
1383 count, seq, next_id);
1385 if (cfs_time_after(jiffies, enough_time)) {
1386 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1387 " created at a rate of %d/s\n",
1388 ofd_name(ofd), created,
1390 created / DISK_TIMEOUT);
1394 rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1395 oseq, count, sync_trans);
1399 } else if (rc < 0) {
1404 /* some objects got created, we can return
1405 * them, even if last creation failed */
1408 CERROR("%s: unable to precreate: rc = %d\n",
1411 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1412 !(oa->o_flags & OBD_FL_DELORPHAN))
1413 ofd_grant_commit(tsi->tsi_env,
1414 ofd_obd(ofd)->obd_self_export, rc);
1416 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1419 ofd_counter_incr(exp, LPROC_OFD_STATS_CREATE,
1422 mutex_unlock(&oseq->os_create_lock);
1425 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1427 ofd_seq_put(tsi->tsi_env, oseq);
1430 up_read(&ofd->ofd_lastid_rwsem);
1434 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1436 const struct ost_body *body = tsi->tsi_ost_body;
1437 struct ost_body *repbody;
1438 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1439 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1440 struct lu_fid *fid = &fti->fti_fid;
1447 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1450 /* This is old case for clients before Lustre 2.4 */
1451 /* If there's a DLM request, cancel the locks mentioned in it */
1452 if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1454 struct ldlm_request *dlm;
1456 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1459 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1462 *fid = body->oa.o_oi.oi_fid;
1463 oid = ostid_id(&body->oa.o_oi);
1466 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1468 /* check that o_misc makes sense */
1469 if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1470 count = body->oa.o_misc;
1472 count = 1; /* default case - single destroy */
1474 CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1475 POSTID(&body->oa.o_oi), count);
1480 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, fid, 0);
1481 if (lrc == -ENOENT) {
1483 "%s: destroying non-existent object "DFID"\n",
1484 ofd_name(ofd), PFID(fid));
1485 /* rewrite rc with -ENOENT only if it is 0 */
1488 } else if (lrc != 0) {
1489 CERROR("%s: error destroying object "DFID": %d\n",
1490 ofd_name(ofd), PFID(fid), lrc);
1496 lrc = fid_set_id(fid, oid);
1497 if (unlikely(lrc != 0 && count > 0))
1498 GOTO(out, rc = lrc);
1501 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1507 fid_to_ostid(fid, &repbody->oa.o_oi);
1511 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1513 struct obd_statfs *osfs;
1518 osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1520 rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1521 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1523 CERROR("%s: statfs failed: rc = %d\n",
1524 tgt_name(tsi->tsi_tgt), rc);
1526 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1529 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1535 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1537 struct ost_body *body = tsi->tsi_ost_body;
1538 struct ost_body *repbody;
1539 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1540 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1541 struct ofd_object *fo = NULL;
1546 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1548 /* if no objid is specified, it means "sync whole filesystem" */
1549 if (!fid_is_zero(&tsi->tsi_fid)) {
1550 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1552 RETURN(PTR_ERR(fo));
1555 rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1556 fo != NULL ? ofd_object_child(fo) : NULL);
1560 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1565 repbody->oa.o_oi = body->oa.o_oi;
1566 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1568 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1570 obdo_from_la(&repbody->oa, &fti->fti_attr,
1573 /* don't return rc from getattr */
1578 ofd_object_put(tsi->tsi_env, fo);
1582 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1584 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1585 struct ost_body *repbody;
1586 struct ofd_thread_info *info = tsi2ofd_info(tsi);
1587 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1588 struct ldlm_resource *res;
1589 struct ofd_object *fo;
1590 struct filter_fid *ff = NULL;
1592 struct lustre_handle lh = { 0, };
1599 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1600 CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1602 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1603 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1604 RETURN(err_serious(-EPROTO));
1606 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1607 if (repbody == NULL)
1608 RETURN(err_serious(-ENOMEM));
1610 /* punch start,end are passed in o_size,o_blocks throught wire */
1614 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1617 /* standard truncate optimization: if file body is completely
1618 * destroyed, don't send data back to the server. */
1620 flags |= LDLM_FL_AST_DISCARD_DATA;
1622 repbody->oa.o_oi = oa->o_oi;
1623 repbody->oa.o_valid = OBD_MD_FLID;
1625 srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1626 oa->o_flags & OBD_FL_SRVLOCK;
1629 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1635 CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1636 ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1637 oa->o_valid, start, end);
1639 fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1642 GOTO(out, rc = PTR_ERR(fo));
1644 la_from_obdo(&info->fti_attr, oa,
1645 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1646 info->fti_attr.la_size = start;
1647 info->fti_attr.la_valid |= LA_SIZE;
1649 if (oa->o_valid & OBD_MD_FLFID) {
1650 ff = &info->fti_mds_fid;
1651 ofd_prepare_fidea(ff, oa);
1654 rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1659 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1663 ofd_object_put(tsi->tsi_env, fo);
1666 tgt_extent_unlock(&lh, LCK_PW);
1668 /* we do not call this before to avoid lu_object_find() in
1669 * ->lvbo_update() holding another reference on the object.
1670 * otherwise concurrent destroy can make the object unavailable
1671 * for 2nd lu_object_find() waiting for the first reference
1672 * to go... deadlock! */
1673 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1676 ldlm_res_lvbo_update(res, NULL, 0);
1677 ldlm_resource_putref(res);
1683 static int ofd_quotactl(struct tgt_session_info *tsi)
1685 struct obd_quotactl *oqctl, *repoqc;
1690 oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1692 RETURN(err_serious(-EPROTO));
1694 repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1696 RETURN(err_serious(-ENOMEM));
1698 /* report success for quota on/off for interoperability with current MDT
1700 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1704 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1706 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1712 /* High priority request handlers for OFD */
1714 /* prolong locks for the current service time of the corresponding
1715 * portal (= OST_IO_PORTAL)
1717 static inline int prolong_timeout(struct ptlrpc_request *req)
1719 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1722 return obd_timeout / 2;
1724 return max(at_est2timeout(at_get(&svcpt->scp_at_estimate)),
1728 static int ofd_prolong_one_lock(struct tgt_session_info *tsi,
1729 struct ldlm_lock *lock,
1730 struct ldlm_extent *extent, int timeout)
1733 if (lock->l_flags & LDLM_FL_DESTROYED) /* lock already cancelled */
1736 /* XXX: never try to grab resource lock here because we're inside
1737 * exp_bl_list_lock; in ldlm_lockd.c to handle waiting list we take
1738 * res lock and then exp_bl_list_lock. */
1740 if (!(lock->l_flags & LDLM_FL_AST_SENT))
1741 /* ignore locks not being cancelled */
1744 LDLM_DEBUG(lock, "refreshed for req x"LPU64" ext("LPU64"->"LPU64") "
1745 "to %ds.\n", tgt_ses_req(tsi)->rq_xid, extent->start,
1746 extent->end, timeout);
1748 /* OK. this is a possible lock the user holds doing I/O
1749 * let's refresh eviction timer for it */
1750 ldlm_refresh_waiting_lock(lock, timeout);
1754 static int ofd_prolong_extent_locks(struct tgt_session_info *tsi,
1755 __u64 start, __u64 end)
1757 struct obd_export *exp = tsi->tsi_exp;
1758 struct obdo *oa = &tsi->tsi_ost_body->oa;
1759 struct ldlm_extent extent = {
1763 struct ldlm_lock *lock;
1764 int timeout = prolong_timeout(tgt_ses_req(tsi));
1769 if (oa->o_valid & OBD_MD_FLHANDLE) {
1770 /* mostly a request should be covered by only one lock, try
1772 lock = ldlm_handle2lock(&oa->o_handle);
1774 /* Fast path to check if the lock covers the whole IO
1775 * region exclusively. */
1776 if (lock->l_granted_mode == LCK_PW &&
1777 ldlm_extent_contain(&lock->l_policy_data.l_extent,
1780 LASSERT(lock->l_export == exp);
1781 lock_count = ofd_prolong_one_lock(tsi, lock,
1783 LDLM_LOCK_PUT(lock);
1786 LDLM_LOCK_PUT(lock);
1790 spin_lock_bh(&exp->exp_bl_list_lock);
1791 list_for_each_entry(lock, &exp->exp_bl_list, l_exp_list) {
1792 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1793 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
1795 if (!ldlm_res_eq(&tsi->tsi_resid, &lock->l_resource->lr_name))
1798 if (!ldlm_extent_overlap(&lock->l_policy_data.l_extent,
1802 lock_count += ofd_prolong_one_lock(tsi, lock, &extent, timeout);
1804 spin_unlock_bh(&exp->exp_bl_list_lock);
1810 * Returns 1 if the given PTLRPC matches the given LDLM lock, or 0 if it does
1813 static int ofd_rw_hpreq_lock_match(struct ptlrpc_request *req,
1814 struct ldlm_lock *lock)
1816 struct niobuf_remote *rnb;
1817 struct obd_ioobj *ioo;
1819 struct ldlm_extent ext;
1820 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1824 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1825 LASSERT(ioo != NULL);
1827 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1828 LASSERT(rnb != NULL);
1830 ext.start = rnb->offset;
1831 rnb += ioo->ioo_bufcnt - 1;
1832 ext.end = rnb->offset + rnb->len - 1;
1834 LASSERT(lock->l_resource != NULL);
1835 if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
1839 if (opc == OST_READ)
1842 if (!(lock->l_granted_mode & mode))
1845 RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
1849 * High-priority queue request check for whether the given PTLRPC request
1850 * (\a req) is blocking an LDLM lock cancel.
1852 * Returns 1 if the given given PTLRPC request (\a req) is blocking an LDLM lock
1853 * cancel, 0 if it is not, and -EFAULT if the request is malformed.
1855 * Only OST_READs, OST_WRITEs and OST_PUNCHes go on the h-p RPC queue. This
1856 * function looks only at OST_READs and OST_WRITEs.
1858 static int ofd_rw_hpreq_check(struct ptlrpc_request *req)
1860 struct tgt_session_info *tsi;
1861 struct obd_ioobj *ioo;
1862 struct niobuf_remote *rnb;
1868 /* Don't use tgt_ses_info() to get session info, because lock_match()
1869 * can be called while request has no processing thread yet. */
1870 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1871 LASSERT(tsi != NULL);
1874 * Use LASSERT below because malformed RPCs should have
1875 * been filtered out in tgt_hpreq_handler().
1877 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1878 LASSERT(ioo != NULL);
1880 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1881 LASSERT(rnb != NULL);
1882 LASSERT(!(rnb->flags & OBD_BRW_SRVLOCK));
1884 start = rnb->offset;
1885 rnb += ioo->ioo_bufcnt - 1;
1886 end = rnb->offset + rnb->len - 1;
1888 DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID
1889 " ("LPU64"->"LPU64")\n",
1890 tgt_name(tsi->tsi_tgt), current->comm,
1891 PFID(&tsi->tsi_fid), start, end);
1893 lock_count = ofd_prolong_extent_locks(tsi, start, end);
1895 CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1896 tgt_name(tsi->tsi_tgt), lock_count, req);
1898 RETURN(lock_count > 0);
1901 static void ofd_rw_hpreq_fini(struct ptlrpc_request *req)
1903 ofd_rw_hpreq_check(req);
1907 * Like tgt_rw_hpreq_lock_match(), but for OST_PUNCH RPCs.
1909 static int ofd_punch_hpreq_lock_match(struct ptlrpc_request *req,
1910 struct ldlm_lock *lock)
1912 struct tgt_session_info *tsi;
1914 /* Don't use tgt_ses_info() to get session info, because lock_match()
1915 * can be called while request has no processing thread yet. */
1916 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1917 LASSERT(tsi != NULL);
1919 LASSERT(tsi->tsi_ost_body != NULL);
1920 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
1921 tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
1928 * Like ost_rw_hpreq_check(), but for OST_PUNCH RPCs.
1930 static int ofd_punch_hpreq_check(struct ptlrpc_request *req)
1932 struct tgt_session_info *tsi;
1938 /* Don't use tgt_ses_info() to get session info, because lock_match()
1939 * can be called while request has no processing thread yet. */
1940 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1941 LASSERT(tsi != NULL);
1942 oa = &tsi->tsi_ost_body->oa;
1944 LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
1945 oa->o_flags & OBD_FL_SRVLOCK));
1948 "%s: refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
1949 tgt_name(tsi->tsi_tgt), tsi->tsi_resid.name[0],
1950 tsi->tsi_resid.name[1], oa->o_size, oa->o_blocks);
1952 lock_count = ofd_prolong_extent_locks(tsi, oa->o_size, oa->o_blocks);
1954 CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1955 tgt_name(tsi->tsi_tgt), lock_count, req);
1957 RETURN(lock_count > 0);
1960 static void ofd_punch_hpreq_fini(struct ptlrpc_request *req)
1962 ofd_punch_hpreq_check(req);
1965 struct ptlrpc_hpreq_ops ofd_hpreq_rw = {
1966 .hpreq_lock_match = ofd_rw_hpreq_lock_match,
1967 .hpreq_check = ofd_rw_hpreq_check,
1968 .hpreq_fini = ofd_rw_hpreq_fini
1971 struct ptlrpc_hpreq_ops ofd_hpreq_punch = {
1972 .hpreq_lock_match = ofd_punch_hpreq_lock_match,
1973 .hpreq_check = ofd_punch_hpreq_check,
1974 .hpreq_fini = ofd_punch_hpreq_fini
1977 /** Assign high priority operations to the IO requests */
1978 static void ofd_hp_brw(struct tgt_session_info *tsi)
1980 struct niobuf_remote *rnb;
1981 struct obd_ioobj *ioo;
1985 ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
1986 LASSERT(ioo != NULL); /* must exist after request preprocessing */
1987 if (ioo->ioo_bufcnt > 0) {
1988 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
1989 LASSERT(rnb != NULL); /* must exist after request preprocessing */
1991 /* no high priority if server lock is needed */
1992 if (rnb->flags & OBD_BRW_SRVLOCK)
1995 tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_rw;
1998 static void ofd_hp_punch(struct tgt_session_info *tsi)
2000 LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
2001 /* no high-priority if server lock is needed */
2002 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
2003 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK)
2005 tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_punch;
2008 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
2009 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
2010 #define OST_BRW_READ OST_READ
2011 #define OST_BRW_WRITE OST_WRITE
2013 static struct tgt_handler ofd_tgt_handlers[] = {
2014 TGT_RPC_HANDLER(OST_FIRST_OPC,
2015 0, OST_CONNECT, tgt_connect,
2016 &RQF_CONNECT, LUSTRE_OBD_VERSION),
2017 TGT_RPC_HANDLER(OST_FIRST_OPC,
2018 0, OST_DISCONNECT, tgt_disconnect,
2019 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
2020 TGT_RPC_HANDLER(OST_FIRST_OPC,
2021 0, OST_SET_INFO, ofd_set_info_hdl,
2022 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
2023 TGT_OST_HDL(0, OST_GET_INFO, ofd_get_info_hdl),
2024 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR, ofd_getattr_hdl),
2025 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2026 OST_SETATTR, ofd_setattr_hdl),
2027 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
2028 OST_CREATE, ofd_create_hdl),
2029 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
2030 OST_DESTROY, ofd_destroy_hdl),
2031 TGT_OST_HDL(0 | HABEO_REFERO, OST_STATFS, ofd_statfs_hdl),
2032 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO,
2033 OST_BRW_READ, tgt_brw_read,
2035 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
2036 TGT_OST_HDL_HP(HABEO_CORPUS| MUTABOR, OST_BRW_WRITE, tgt_brw_write,
2038 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2039 OST_PUNCH, ofd_punch_hdl,
2041 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC, ofd_sync_hdl),
2042 TGT_OST_HDL(0 | HABEO_REFERO, OST_QUOTACTL, ofd_quotactl),
2045 static struct tgt_opc_slice ofd_common_slice[] = {
2047 .tos_opc_start = OST_FIRST_OPC,
2048 .tos_opc_end = OST_LAST_OPC,
2049 .tos_hs = ofd_tgt_handlers
2052 .tos_opc_start = OBD_FIRST_OPC,
2053 .tos_opc_end = OBD_LAST_OPC,
2054 .tos_hs = tgt_obd_handlers
2057 .tos_opc_start = LDLM_FIRST_OPC,
2058 .tos_opc_end = LDLM_LAST_OPC,
2059 .tos_hs = tgt_dlm_handlers
2062 .tos_opc_start = UPDATE_OBJ,
2063 .tos_opc_end = UPDATE_LAST_OPC,
2064 .tos_hs = tgt_out_handlers
2067 .tos_opc_start = SEQ_FIRST_OPC,
2068 .tos_opc_end = SEQ_LAST_OPC,
2069 .tos_hs = seq_handlers
2072 .tos_opc_start = LFSCK_FIRST_OPC,
2073 .tos_opc_end = LFSCK_LAST_OPC,
2074 .tos_hs = tgt_lfsck_handlers
2081 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
2082 struct lu_device_type *ldt, struct lustre_cfg *cfg)
2084 const char *dev = lustre_cfg_string(cfg, 0);
2085 struct ofd_thread_info *info = NULL;
2086 struct obd_device *obd;
2087 struct obd_statfs *osfs;
2092 obd = class_name2obd(dev);
2094 CERROR("Cannot find obd with name %s\n", dev);
2098 rc = lu_env_refill((struct lu_env *)env);
2102 obd->u.obt.obt_magic = OBT_MAGIC;
2104 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
2105 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
2107 spin_lock_init(&m->ofd_flags_lock);
2108 m->ofd_raid_degraded = 0;
2109 m->ofd_syncjournal = 0;
2111 m->ofd_grant_compat_disable = 0;
2112 m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
2115 spin_lock_init(&m->ofd_osfs_lock);
2116 m->ofd_osfs_age = cfs_time_shift_64(-1000);
2117 m->ofd_osfs_unstable = 0;
2118 m->ofd_statfs_inflight = 0;
2119 m->ofd_osfs_inflight = 0;
2122 spin_lock_init(&m->ofd_grant_lock);
2123 m->ofd_tot_dirty = 0;
2124 m->ofd_tot_granted = 0;
2125 m->ofd_tot_pending = 0;
2126 m->ofd_seq_count = 0;
2128 spin_lock_init(&m->ofd_batch_lock);
2129 rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
2130 sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
2131 init_rwsem(&m->ofd_lastid_rwsem);
2133 obd->u.filter.fo_fl_oss_capa = 0;
2134 CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
2135 obd->u.filter.fo_capa_hash = init_capa_hash();
2136 if (obd->u.filter.fo_capa_hash == NULL)
2139 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
2140 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
2141 /* set this lu_device to obd, because error handling need it */
2142 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
2144 rc = ofd_procfs_init(m);
2146 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
2150 /* No connection accepted until configurations will finish */
2151 spin_lock(&obd->obd_dev_lock);
2152 obd->obd_no_conn = 1;
2153 spin_unlock(&obd->obd_dev_lock);
2154 obd->obd_replayable = 1;
2155 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
2156 char *str = lustre_cfg_string(cfg, 4);
2158 if (strchr(str, 'n')) {
2159 CWARN("%s: recovery disabled\n", obd->obd_name);
2160 obd->obd_replayable = 0;
2164 info = ofd_info_init(env, NULL);
2168 rc = ofd_stack_init(env, m, cfg);
2170 CERROR("Can't init device stack, rc %d\n", rc);
2171 GOTO(err_fini_proc, rc);
2174 ofd_procfs_add_brw_stats_symlink(m);
2176 /* populate cached statfs data */
2177 osfs = &ofd_info(env)->fti_u.osfs;
2178 rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
2180 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
2181 GOTO(err_fini_stack, rc);
2183 if (!IS_PO2(osfs->os_bsize)) {
2184 CERROR("%s: blocksize (%d) is not a power of 2\n",
2185 obd->obd_name, osfs->os_bsize);
2186 GOTO(err_fini_stack, rc = -EPROTO);
2188 m->ofd_blockbits = fls(osfs->os_bsize) - 1;
2190 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
2191 if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
2192 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
2194 snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
2195 "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
2196 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
2197 LDLM_NAMESPACE_SERVER,
2198 LDLM_NAMESPACE_GREEDY,
2200 if (m->ofd_namespace == NULL)
2201 GOTO(err_fini_stack, rc = -ENOMEM);
2202 /* set obd_namespace for compatibility with old code */
2203 obd->obd_namespace = m->ofd_namespace;
2204 ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
2205 m->ofd_namespace->ns_lvbo = &ofd_lvbo;
2206 m->ofd_namespace->ns_lvbp = m;
2208 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2209 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
2211 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
2213 /* Allow at most ddp_grant_reserved% of the available filesystem space
2214 * to be granted to clients, so that any errors in the grant overhead
2215 * calculations do not allow granting more space to clients than can be
2216 * written. Assumes that in aggregate the grant overhead calculations do
2217 * not have more than ddp_grant_reserved% estimation error in them. */
2218 m->ofd_grant_ratio =
2219 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
2221 rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
2222 OBD_FAIL_OST_ALL_REQUEST_NET,
2223 OBD_FAIL_OST_ALL_REPLY_NET);
2225 GOTO(err_free_ns, rc);
2227 rc = ofd_fs_setup(env, m, obd);
2229 GOTO(err_fini_lut, rc);
2233 tgt_fini(env, &m->ofd_lut);
2235 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
2236 obd->obd_namespace = m->ofd_namespace = NULL;
2238 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
2244 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
2246 struct obd_device *obd = ofd_obd(m);
2247 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
2248 struct lfsck_stop stop;
2250 stop.ls_status = LS_PAUSED;
2251 lfsck_stop(env, m->ofd_osd, &stop);
2252 lfsck_degister(env, m->ofd_osd);
2253 target_recovery_fini(obd);
2254 obd_exports_barrier(obd);
2255 obd_zombie_barrier();
2257 tgt_fini(env, &m->ofd_lut);
2258 ofd_fs_cleanup(env, m);
2260 ofd_free_capa_keys(m);
2261 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
2263 if (m->ofd_namespace != NULL) {
2264 ldlm_namespace_free(m->ofd_namespace, NULL,
2265 d->ld_obd->obd_force);
2266 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
2269 ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
2271 LASSERT(atomic_read(&d->ld_ref) == 0);
2272 server_put_mount(obd->obd_name);
2276 static struct lu_device *ofd_device_fini(const struct lu_env *env,
2277 struct lu_device *d)
2280 ofd_fini(env, ofd_dev(d));
2284 static struct lu_device *ofd_device_free(const struct lu_env *env,
2285 struct lu_device *d)
2287 struct ofd_device *m = ofd_dev(d);
2289 dt_device_fini(&m->ofd_dt_dev);
2294 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
2295 struct lu_device_type *t,
2296 struct lustre_cfg *cfg)
2298 struct ofd_device *m;
2299 struct lu_device *l;
2304 return ERR_PTR(-ENOMEM);
2306 l = &m->ofd_dt_dev.dd_lu_dev;
2307 dt_device_init(&m->ofd_dt_dev, t);
2308 rc = ofd_init0(env, m, t, cfg);
2310 ofd_device_free(env, l);
2317 /* thread context key constructor/destructor */
2318 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
2320 static void ofd_key_exit(const struct lu_context *ctx,
2321 struct lu_context_key *key, void *data)
2323 struct ofd_thread_info *info = data;
2325 info->fti_env = NULL;
2326 info->fti_exp = NULL;
2329 info->fti_pre_version = 0;
2332 memset(&info->fti_attr, 0, sizeof info->fti_attr);
2335 struct lu_context_key ofd_thread_key = {
2336 .lct_tags = LCT_DT_THREAD,
2337 .lct_init = ofd_key_init,
2338 .lct_fini = ofd_key_fini,
2339 .lct_exit = ofd_key_exit
2342 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
2343 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
2345 static struct lu_device_type_operations ofd_device_type_ops = {
2346 .ldto_init = ofd_type_init,
2347 .ldto_fini = ofd_type_fini,
2349 .ldto_start = ofd_type_start,
2350 .ldto_stop = ofd_type_stop,
2352 .ldto_device_alloc = ofd_device_alloc,
2353 .ldto_device_free = ofd_device_free,
2354 .ldto_device_fini = ofd_device_fini
2357 static struct lu_device_type ofd_device_type = {
2358 .ldt_tags = LU_DEVICE_DT,
2359 .ldt_name = LUSTRE_OST_NAME,
2360 .ldt_ops = &ofd_device_type_ops,
2361 .ldt_ctx_tags = LCT_DT_THREAD
2364 int __init ofd_init(void)
2366 struct lprocfs_static_vars lvars;
2369 rc = lu_kmem_init(ofd_caches);
2373 rc = ofd_fmd_init();
2375 lu_kmem_fini(ofd_caches);
2379 lprocfs_ofd_init_vars(&lvars);
2381 rc = class_register_type(&ofd_obd_ops, NULL, NULL,
2382 #ifndef HAVE_ONLY_PROCFS_SEQ
2385 LUSTRE_OST_NAME, &ofd_device_type);
2389 void __exit ofd_exit(void)
2392 lu_kmem_fini(ofd_caches);
2393 class_unregister_type(LUSTRE_OST_NAME);
2396 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
2397 MODULE_DESCRIPTION("Lustre Object Filtering Device");
2398 MODULE_LICENSE("GPL");
2400 module_init(ofd_init);
2401 module_exit(ofd_exit);