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 /* used by MGS to process specific configurations */
197 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
198 struct lustre_cfg *cfg)
200 struct ofd_device *m = ofd_dev(d);
201 struct dt_device *dt_next = m->ofd_osd;
202 struct lu_device *next = &dt_next->dd_lu_dev;
207 switch (cfg->lcfg_command) {
209 struct lprocfs_static_vars lvars;
211 /* For interoperability */
212 struct cfg_interop_param *ptr = NULL;
213 struct lustre_cfg *old_cfg = NULL;
216 param = lustre_cfg_string(cfg, 1);
218 CERROR("param is empty\n");
223 ptr = class_find_old_param(param, ofd_interop_param);
225 if (ptr->new_param == NULL) {
227 CWARN("For interoperability, skip this %s."
228 " It is obsolete.\n", ptr->old_param);
232 CWARN("Found old param %s, changed it to %s.\n",
233 ptr->old_param, ptr->new_param);
236 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
243 lprocfs_ofd_init_vars(&lvars);
244 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
246 if (rc > 0 || rc == -ENOSYS)
247 /* we don't understand; pass it on */
248 rc = next->ld_ops->ldo_process_config(env, next, cfg);
251 case LCFG_SPTLRPC_CONF: {
256 /* others are passed further */
257 rc = next->ld_ops->ldo_process_config(env, next, cfg);
263 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
264 const struct lu_object_conf *conf)
266 struct ofd_device *d = ofd_dev(o->lo_dev);
267 struct lu_device *under;
268 struct lu_object *below;
273 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
274 PFID(lu_object_fid(o)));
276 under = &d->ofd_osd->dd_lu_dev;
277 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
279 lu_object_add(o, below);
286 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
288 struct ofd_object *of = ofd_obj(o);
289 struct lu_object_header *h;
294 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
295 PFID(lu_object_fid(o)));
298 lu_object_header_fini(h);
299 OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
303 static int ofd_object_print(const struct lu_env *env, void *cookie,
304 lu_printer_t p, const struct lu_object *o)
306 return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
309 struct lu_object_operations ofd_obj_ops = {
310 .loo_object_init = ofd_object_init,
311 .loo_object_free = ofd_object_free,
312 .loo_object_print = ofd_object_print
315 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
316 const struct lu_object_header *hdr,
319 struct ofd_object *of;
323 OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, __GFP_IO);
326 struct lu_object_header *h;
328 o = &of->ofo_obj.do_lu;
330 lu_object_header_init(h);
331 lu_object_init(o, h, d);
332 lu_object_add_top(h, o);
333 o->lo_ops = &ofd_obj_ops;
340 extern int ost_handle(struct ptlrpc_request *req);
342 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
343 struct lu_device *dev)
345 struct ofd_thread_info *info;
346 struct ofd_device *ofd = ofd_dev(dev);
347 struct obd_device *obd = ofd_obd(ofd);
348 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
349 struct lfsck_start_param lsp;
354 rc = lu_env_refill((struct lu_env *)env);
356 CERROR("Failure to refill session: '%d'\n", rc);
360 info = ofd_info_init(env, NULL);
364 /* initialize lower device */
365 rc = next->ld_ops->ldo_prepare(env, dev, next);
369 rc = lfsck_register(env, ofd->ofd_osd, &ofd->ofd_dt_dev, false);
371 CERROR("%s: failed to initialize lfsck: rc = %d\n",
376 lsp.lsp_start = NULL;
377 lsp.lsp_namespace = ofd->ofd_namespace;
378 rc = lfsck_start(env, ofd->ofd_osd, &lsp);
380 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
385 target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
386 LASSERT(obd->obd_no_conn);
387 spin_lock(&obd->obd_dev_lock);
388 obd->obd_no_conn = 0;
389 spin_unlock(&obd->obd_dev_lock);
391 if (obd->obd_recovering == 0)
392 ofd_postrecov(env, ofd);
397 static int ofd_recovery_complete(const struct lu_env *env,
398 struct lu_device *dev)
400 struct ofd_device *ofd = ofd_dev(dev);
401 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
402 int rc = 0, max_precreate;
406 /* Grant space for object precreation on the self export.
407 * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
408 * is enough to create 10k objects. More space is then acquired for
409 * precreation in ofd_grant_create().
411 max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
412 ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
414 rc = next->ld_ops->ldo_recovery_complete(env, next);
418 static struct lu_device_operations ofd_lu_ops = {
419 .ldo_object_alloc = ofd_object_alloc,
420 .ldo_process_config = ofd_process_config,
421 .ldo_recovery_complete = ofd_recovery_complete,
422 .ldo_prepare = ofd_prepare,
425 static int ofd_procfs_init(struct ofd_device *ofd)
427 struct lprocfs_static_vars lvars;
428 struct obd_device *obd = ofd_obd(ofd);
429 cfs_proc_dir_entry_t *entry;
434 /* lprocfs must be setup before the ofd so state can be safely added
435 * to /proc incrementally as the ofd is setup */
436 lprocfs_ofd_init_vars(&lvars);
437 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
439 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
444 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
446 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
448 GOTO(obd_cleanup, rc);
451 obd->obd_uses_nid_stats = 1;
453 entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
456 CERROR("%s: error %d setting up lprocfs for %s\n",
457 obd->obd_name, rc, "exports");
458 GOTO(obd_cleanup, rc);
460 obd->obd_proc_exports_entry = entry;
462 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
463 lprocfs_nid_stats_clear_read,
464 lprocfs_nid_stats_clear_write, obd, NULL);
467 CERROR("%s: add proc entry 'clear' failed: %d.\n",
469 GOTO(obd_cleanup, rc);
472 ofd_stats_counter_init(obd->obd_stats);
474 rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
475 ofd_stats_counter_init);
477 GOTO(remove_entry_clear, rc);
480 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
482 lprocfs_obd_cleanup(obd);
483 lprocfs_free_obd_stats(obd);
488 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
490 struct obd_device *obd = ofd_obd(ofd);
491 struct obd_device *osd_obd = ofd->ofd_osd_exp->exp_obd;
492 cfs_proc_dir_entry_t *osd_root = osd_obd->obd_type->typ_procroot;
493 cfs_proc_dir_entry_t *osd_dir;
495 osd_dir = lprocfs_srch(osd_root, obd->obd_name);
499 if (lprocfs_srch(osd_dir, "brw_stats") != NULL)
500 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
501 "../../%s/%s/brw_stats",
502 osd_root->name, osd_dir->name);
504 if (lprocfs_srch(osd_dir, "read_cache_enable") != NULL)
505 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
506 "../../%s/%s/read_cache_enable",
507 osd_root->name, osd_dir->name);
509 if (lprocfs_srch(osd_dir, "readcache_max_filesize") != NULL)
510 lprocfs_add_symlink("readcache_max_filesize",
512 "../../%s/%s/readcache_max_filesize",
513 osd_root->name, osd_dir->name);
515 if (lprocfs_srch(osd_dir, "writethrough_cache_enable") != NULL)
516 lprocfs_add_symlink("writethrough_cache_enable",
518 "../../%s/%s/writethrough_cache_enable",
519 osd_root->name, osd_dir->name);
522 static void ofd_procfs_fini(struct ofd_device *ofd)
524 struct obd_device *obd = ofd_obd(ofd);
526 lprocfs_remove_proc_entry("writethrough_cache_enable",
527 obd->obd_proc_entry);
528 lprocfs_remove_proc_entry("readcache_max_filesize",
529 obd->obd_proc_entry);
530 lprocfs_remove_proc_entry("read_cache_enable", obd->obd_proc_entry);
531 lprocfs_remove_proc_entry("brw_stats", obd->obd_proc_entry);
532 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
533 lprocfs_free_per_client_stats(obd);
534 lprocfs_obd_cleanup(obd);
535 lprocfs_free_obd_stats(obd);
536 lprocfs_job_stats_fini(obd);
539 extern int ost_handle(struct ptlrpc_request *req);
541 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
543 return seq_site_fini(env, &ofd->ofd_seq_site);
546 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
548 struct seq_server_site *ss = &ofd->ofd_seq_site;
549 struct lu_device *lu = &ofd->ofd_dt_dev.dd_lu_dev;
550 char *obd_name = ofd_name(ofd);
554 ss = &ofd->ofd_seq_site;
555 lu->ld_site->ld_seq_site = ss;
556 ss->ss_lu = lu->ld_site;
557 ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
559 OBD_ALLOC_PTR(ss->ss_server_seq);
560 if (ss->ss_server_seq == NULL)
561 GOTO(out_free, rc = -ENOMEM);
563 OBD_ALLOC(name, strlen(obd_name) + 10);
565 OBD_FREE_PTR(ss->ss_server_seq);
566 ss->ss_server_seq = NULL;
567 GOTO(out_free, rc = -ENOMEM);
570 rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
571 LUSTRE_SEQ_SERVER, ss);
573 CERROR("%s : seq server init error %d\n", obd_name, rc);
576 ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
578 OBD_ALLOC_PTR(ss->ss_client_seq);
579 if (ss->ss_client_seq == NULL)
580 GOTO(out_free, rc = -ENOMEM);
582 snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
583 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
586 CERROR("%s : seq client init error %d\n", obd_name, rc);
589 OBD_FREE(name, strlen(obd_name) + 10);
592 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
596 if (ss->ss_server_seq) {
597 seq_server_fini(ss->ss_server_seq, env);
598 OBD_FREE_PTR(ss->ss_server_seq);
599 ss->ss_server_seq = NULL;
602 if (ss->ss_client_seq) {
603 seq_client_fini(ss->ss_client_seq);
604 OBD_FREE_PTR(ss->ss_client_seq);
605 ss->ss_client_seq = NULL;
609 OBD_FREE(name, strlen(obd_name) + 10);
617 int ofd_set_info_hdl(struct tgt_session_info *tsi)
619 struct ptlrpc_request *req = tgt_ses_req(tsi);
620 struct ost_body *body = NULL, *repbody;
621 void *key, *val = NULL;
622 int keylen, vallen, rc = 0;
623 bool is_grant_shrink;
624 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
628 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
630 DEBUG_REQ(D_HA, req, "no set_info key");
631 RETURN(err_serious(-EFAULT));
633 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
636 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
638 DEBUG_REQ(D_HA, req, "no set_info val");
639 RETURN(err_serious(-EFAULT));
641 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
644 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
646 /* In this case the value is actually an RMF_OST_BODY, so we
647 * transmutate the type of this PTLRPC */
648 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
650 rc = req_capsule_server_pack(tsi->tsi_pill);
654 if (is_grant_shrink) {
655 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
657 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
660 /** handle grant shrink, similar to a read request */
661 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
663 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
665 obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
667 } else if (KEY_IS(KEY_CAPA_KEY)) {
668 rc = ofd_update_capa_key(ofd, val);
669 } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
670 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
672 CERROR("%s: Unsupported key %s\n",
673 tgt_name(tsi->tsi_tgt), (char *)key);
676 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
682 static int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
683 struct lu_fid *fid, struct ll_user_fiemap *fiemap)
685 struct ofd_object *fo;
688 fo = ofd_object_find(env, ofd, fid);
690 CERROR("%s: error finding object "DFID"\n",
691 ofd_name(ofd), PFID(fid));
695 ofd_read_lock(env, fo);
696 if (ofd_object_exists(fo))
697 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
700 ofd_read_unlock(env, fo);
701 ofd_object_put(env, fo);
705 struct locked_region {
707 struct lustre_handle lh;
710 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
711 unsigned long long begin, unsigned long long end,
714 struct locked_region *region = NULL;
718 LASSERT(begin <= end);
719 OBD_ALLOC_PTR(region);
723 rc = tgt_extent_lock(ns, res_id, begin, end, ®ion->lh,
728 CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
730 cfs_list_add(®ion->list, locked);
735 static int lock_zero_regions(struct ldlm_namespace *ns,
736 struct ldlm_res_id *res_id,
737 struct ll_user_fiemap *fiemap,
740 __u64 begin = fiemap->fm_start;
743 struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
747 CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
748 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
749 if (fiemap_start[i].fe_logical > begin) {
750 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
751 begin, fiemap_start[i].fe_logical);
752 rc = lock_region(ns, res_id, begin,
753 fiemap_start[i].fe_logical, locked);
758 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
761 if (begin < (fiemap->fm_start + fiemap->fm_length)) {
762 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
763 begin, fiemap->fm_start + fiemap->fm_length);
764 rc = lock_region(ns, res_id, begin,
765 fiemap->fm_start + fiemap->fm_length, locked);
771 static void unlock_zero_regions(struct ldlm_namespace *ns, cfs_list_t *locked)
773 struct locked_region *entry, *temp;
775 cfs_list_for_each_entry_safe(entry, temp, locked, list) {
776 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
777 tgt_extent_unlock(&entry->lh, LCK_PR);
778 cfs_list_del(&entry->list);
783 int ofd_get_info_hdl(struct tgt_session_info *tsi)
785 struct obd_export *exp = tsi->tsi_exp;
786 struct ofd_device *ofd = ofd_exp(exp);
787 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
790 int replylen, rc = 0;
794 /* this common part for get_info rpc */
795 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
797 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
798 RETURN(err_serious(-EPROTO));
800 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
803 if (KEY_IS(KEY_LAST_ID)) {
805 struct ofd_seq *oseq;
807 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
808 rc = req_capsule_server_pack(tsi->tsi_pill);
810 RETURN(err_serious(rc));
812 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
814 oseq = ofd_seq_load(tsi->tsi_env, ofd,
815 (obd_seq)exp->exp_filter_data.fed_group);
819 *last_id = ofd_seq_last_oid(oseq);
820 ofd_seq_put(tsi->tsi_env, oseq);
821 } else if (KEY_IS(KEY_FIEMAP)) {
822 struct ll_fiemap_info_key *fm_key;
823 struct ll_user_fiemap *fiemap;
824 struct lu_fid *fid = &fti->fti_fid;
826 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
828 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
829 rc = tgt_validate_obdo(tsi, &fm_key->oa);
831 RETURN(err_serious(rc));
833 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
834 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
835 RCL_SERVER, replylen);
837 rc = req_capsule_server_pack(tsi->tsi_pill);
839 RETURN(err_serious(rc));
841 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
845 rc = ostid_to_fid(fid, &fm_key->oa.o_oi, 0);
849 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
851 *fiemap = fm_key->fiemap;
852 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
854 /* LU-3219: Lock the sparse areas to make sure dirty
855 * flushed back from client, then call fiemap again. */
856 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
857 fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
858 cfs_list_t locked = CFS_LIST_HEAD_INIT(locked);
860 ost_fid_build_resid(fid, &fti->fti_resid);
861 rc = lock_zero_regions(ofd->ofd_namespace,
862 &fti->fti_resid, fiemap,
864 if (rc == 0 && !cfs_list_empty(&locked)) {
865 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
867 unlock_zero_regions(ofd->ofd_namespace,
871 } else if (KEY_IS(KEY_LAST_FID)) {
872 struct ofd_device *ofd = ofd_exp(exp);
873 struct ofd_seq *oseq;
877 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
878 rc = req_capsule_server_pack(tsi->tsi_pill);
880 RETURN(err_serious(rc));
882 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
884 RETURN(err_serious(-EPROTO));
886 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
888 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
892 oseq = ofd_seq_load(tsi->tsi_env, ofd,
893 ostid_seq(&fti->fti_ostid));
895 RETURN(PTR_ERR(oseq));
897 rc = ostid_to_fid(fid, &oseq->os_oi,
898 ofd->ofd_lut.lut_lsd.lsd_osd_index);
902 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
905 ofd_seq_put(tsi->tsi_env, oseq);
907 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
911 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
917 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
919 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
920 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
921 struct ost_body *repbody;
922 struct lustre_handle lh = { 0 };
923 struct ofd_object *fo;
925 ldlm_mode_t lock_mode = LCK_PR;
930 LASSERT(tsi->tsi_ost_body != NULL);
932 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
936 repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
937 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
939 srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
940 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
943 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
946 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
947 &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
953 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
955 GOTO(out, rc = PTR_ERR(fo));
957 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
961 obdo_from_la(&repbody->oa, &fti->fti_attr,
962 OFD_VALID_FLAGS | LA_UID | LA_GID);
963 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
965 /* Store object version in reply */
966 curr_version = dt_version_get(tsi->tsi_env,
967 ofd_object_child(fo));
968 if ((__s64)curr_version != -EOPNOTSUPP) {
969 repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
970 repbody->oa.o_data_version = curr_version;
974 ofd_object_put(tsi->tsi_env, fo);
977 tgt_extent_unlock(&lh, lock_mode);
979 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
982 repbody->oa.o_valid |= OBD_MD_FLFLAGS;
983 repbody->oa.o_flags = OBD_FL_FLUSH;
988 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
990 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
991 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
992 struct ost_body *body = tsi->tsi_ost_body;
993 struct ost_body *repbody;
994 struct ldlm_resource *res;
995 struct ofd_object *fo;
996 struct filter_fid *ff = NULL;
1001 LASSERT(body != NULL);
1003 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1004 if (repbody == NULL)
1007 repbody->oa.o_oi = body->oa.o_oi;
1008 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1010 /* This would be very bad - accidentally truncating a file when
1011 * changing the time or similar - bug 12203. */
1012 if (body->oa.o_valid & OBD_MD_FLSIZE &&
1013 body->oa.o_size != OBD_OBJECT_EOF) {
1014 static char mdsinum[48];
1016 if (body->oa.o_valid & OBD_MD_FLFID)
1017 snprintf(mdsinum, sizeof(mdsinum) - 1,
1018 "of parent "DFID, body->oa.o_parent_seq,
1019 body->oa.o_parent_oid, 0);
1023 CERROR("%s: setattr from %s is trying to truncate object "DFID
1024 " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1025 PFID(&tsi->tsi_fid), mdsinum);
1029 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1031 GOTO(out, rc = PTR_ERR(fo));
1033 la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1034 fti->fti_attr.la_valid &= ~LA_TYPE;
1036 if (body->oa.o_valid & OBD_MD_FLFID) {
1037 ff = &fti->fti_mds_fid;
1038 ofd_prepare_fidea(ff, &body->oa);
1041 /* setting objects attributes (including owner/group) */
1042 rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1046 obdo_from_la(&repbody->oa, &fti->fti_attr,
1047 OFD_VALID_FLAGS | LA_UID | LA_GID);
1048 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1050 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1054 ofd_object_put(tsi->tsi_env, fo);
1057 /* we do not call this before to avoid lu_object_find() in
1058 * ->lvbo_update() holding another reference on the object.
1059 * otherwise concurrent destroy can make the object unavailable
1060 * for 2nd lu_object_find() waiting for the first reference
1061 * to go... deadlock! */
1062 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1063 &tsi->tsi_resid, LDLM_EXTENT, 0);
1065 ldlm_res_lvbo_update(res, NULL, 0);
1066 ldlm_resource_putref(res);
1072 static int ofd_create_hdl(struct tgt_session_info *tsi)
1074 struct ost_body *repbody;
1075 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1076 struct obdo *rep_oa;
1077 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1078 obd_seq seq = ostid_seq(&oa->o_oi);
1079 obd_id oid = ostid_id(&oa->o_oi);
1080 struct ofd_seq *oseq;
1086 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1089 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1090 if (repbody == NULL)
1093 rep_oa = &repbody->oa;
1094 rep_oa->o_oi = oa->o_oi;
1096 LASSERT(seq >= FID_SEQ_OST_MDT0);
1097 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1099 CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1101 oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1103 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1104 ofd_name(ofd), seq, PTR_ERR(oseq));
1108 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1109 (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1110 if (!ofd_obd(ofd)->obd_recovering ||
1111 oid > ofd_seq_last_oid(oseq)) {
1112 CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1113 "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1114 ofd_seq_last_oid(oseq));
1115 GOTO(out_nolock, rc = -EINVAL);
1117 /* Do nothing here, we re-create objects during recovery
1118 * upon write replay, see ofd_preprw_write() */
1119 GOTO(out_nolock, rc = 0);
1121 /* former ofd_handle_precreate */
1122 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1123 (oa->o_flags & OBD_FL_DELORPHAN)) {
1124 /* destroy orphans */
1125 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1126 tsi->tsi_exp->exp_conn_cnt) {
1127 CERROR("%s: dropping old orphan cleanup request\n",
1129 GOTO(out_nolock, rc = 0);
1131 /* This causes inflight precreates to abort and drop lock */
1132 oseq->os_destroys_in_progress = 1;
1133 mutex_lock(&oseq->os_create_lock);
1134 if (!oseq->os_destroys_in_progress) {
1135 CERROR("%s:["LPU64"] destroys_in_progress already"
1136 " cleared\n", ofd_name(ofd), seq);
1137 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1140 diff = oid - ofd_seq_last_oid(oseq);
1141 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1142 ofd_seq_last_oid(oseq), diff);
1143 if (-diff > OST_MAX_PRECREATE) {
1144 /* FIXME: should reset precreate_next_id on MDS */
1146 } else if (diff < 0) {
1147 rc = ofd_orphans_destroy(tsi->tsi_env, tsi->tsi_exp,
1149 oseq->os_destroys_in_progress = 0;
1151 /* XXX: Used by MDS for the first time! */
1152 oseq->os_destroys_in_progress = 0;
1155 mutex_lock(&oseq->os_create_lock);
1156 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1157 tsi->tsi_exp->exp_conn_cnt) {
1158 CERROR("%s: dropping old precreate request\n",
1162 /* only precreate if seq is 0, IDIF or normal and also o_id
1163 * must be specfied */
1164 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1165 !fid_seq_is_idif(seq)) || oid == 0) {
1166 diff = 1; /* shouldn't we create this right now? */
1168 diff = oid - ofd_seq_last_oid(oseq);
1169 /* Do sync create if the seq is about to used up */
1170 if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1171 if (unlikely(oid >= IDIF_MAX_OID - 1))
1173 } else if (fid_seq_is_norm(seq)) {
1175 LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1178 CERROR("%s : invalid o_seq "DOSTID"\n",
1179 ofd_name(ofd), POSTID(&oa->o_oi));
1180 GOTO(out, rc = -EINVAL);
1185 cfs_time_t enough_time = cfs_time_shift(DISK_TIMEOUT);
1190 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1191 !(oa->o_flags & OBD_FL_DELORPHAN)) {
1192 /* don't enforce grant during orphan recovery */
1193 rc = ofd_grant_create(tsi->tsi_env,
1194 ofd_obd(ofd)->obd_self_export,
1197 CDEBUG(D_HA, "%s: failed to acquire grant "
1198 "space for precreate (%d): rc = %d\n",
1199 ofd_name(ofd), diff, rc);
1204 /* This can happen if a new OST is formatted and installed
1205 * in place of an old one at the same index. Instead of
1206 * precreating potentially millions of deleted old objects
1207 * (possibly filling the OST), only precreate the last batch.
1208 * LFSCK will eventually clean up any orphans. LU-14 */
1209 if (diff > 5 * OST_MAX_PRECREATE) {
1210 diff = OST_MAX_PRECREATE / 2;
1211 LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1212 "larger than the LAST_ID "DOSTID", only "
1213 "precreating the last %u objects.\n",
1214 ofd_name(ofd), POSTID(&oa->o_oi),
1215 5 * OST_MAX_PRECREATE,
1216 POSTID(&oseq->os_oi), diff);
1217 ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1221 next_id = ofd_seq_last_oid(oseq) + 1;
1222 count = ofd_precreate_batch(ofd, diff);
1224 CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1225 " at "LPU64"\n", ofd_name(ofd),
1226 count, seq, next_id);
1228 if (cfs_time_after(jiffies, enough_time)) {
1229 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1230 " created at a rate of %d/s\n",
1231 ofd_name(ofd), created,
1233 created / DISK_TIMEOUT);
1237 rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1238 oseq, count, sync_trans);
1242 } else if (rc < 0) {
1247 /* some objects got created, we can return
1248 * them, even if last creation failed */
1251 CERROR("%s: unable to precreate: rc = %d\n",
1254 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1255 !(oa->o_flags & OBD_FL_DELORPHAN))
1256 ofd_grant_commit(tsi->tsi_env,
1257 ofd_obd(ofd)->obd_self_export, rc);
1259 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1262 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_CREATE,
1265 mutex_unlock(&oseq->os_create_lock);
1268 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1270 ofd_seq_put(tsi->tsi_env, oseq);
1274 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1276 const struct ost_body *body = tsi->tsi_ost_body;
1277 struct ost_body *repbody;
1278 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1279 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1285 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1288 /* This is old case for clients before Lustre 2.4 */
1289 /* If there's a DLM request, cancel the locks mentioned in it */
1290 if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1292 struct ldlm_request *dlm;
1294 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1297 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1300 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1301 repbody->oa.o_oi = body->oa.o_oi;
1303 /* check that o_misc makes sense */
1304 if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1305 count = body->oa.o_misc;
1307 count = 1; /* default case - single destroy */
1309 CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1310 POSTID(&body->oa.o_oi), count);
1314 lrc = ostid_to_fid(&fti->fti_fid, &repbody->oa.o_oi, 0);
1320 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, &fti->fti_fid, 0);
1321 if (lrc == -ENOENT) {
1323 "%s: destroying non-existent object "DFID"\n",
1324 ofd_name(ofd), PFID(&fti->fti_fid));
1325 /* rewrite rc with -ENOENT only if it is 0 */
1328 } else if (lrc != 0) {
1329 CERROR("%s: error destroying object "DFID": %d\n",
1330 ofd_name(ofd), PFID(&fti->fti_fid),
1335 ostid_inc_id(&repbody->oa.o_oi);
1338 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1344 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1346 struct obd_statfs *osfs;
1351 osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1353 rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1354 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1356 CERROR("%s: statfs failed: rc = %d\n",
1357 tgt_name(tsi->tsi_tgt), rc);
1359 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1362 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1368 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1370 struct ost_body *body = tsi->tsi_ost_body;
1371 struct ost_body *repbody;
1372 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1373 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1374 struct ofd_object *fo = NULL;
1379 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1381 /* if no objid is specified, it means "sync whole filesystem" */
1382 if (!fid_is_zero(&tsi->tsi_fid)) {
1383 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1385 RETURN(PTR_ERR(fo));
1388 rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1389 fo != NULL ? ofd_object_child(fo) : NULL);
1393 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1398 repbody->oa.o_oi = body->oa.o_oi;
1399 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1401 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1403 obdo_from_la(&repbody->oa, &fti->fti_attr,
1406 /* don't return rc from getattr */
1411 ofd_object_put(tsi->tsi_env, fo);
1415 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1417 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1418 struct ost_body *repbody;
1419 struct ofd_thread_info *info = tsi2ofd_info(tsi);
1420 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1421 struct ldlm_resource *res;
1422 struct ofd_object *fo;
1423 struct filter_fid *ff = NULL;
1425 struct lustre_handle lh = { 0, };
1432 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1433 CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1435 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1436 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1437 RETURN(err_serious(-EPROTO));
1439 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1440 if (repbody == NULL)
1441 RETURN(err_serious(-ENOMEM));
1443 /* punch start,end are passed in o_size,o_blocks throught wire */
1447 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1450 /* standard truncate optimization: if file body is completely
1451 * destroyed, don't send data back to the server. */
1453 flags |= LDLM_FL_AST_DISCARD_DATA;
1455 repbody->oa.o_oi = oa->o_oi;
1456 repbody->oa.o_valid = OBD_MD_FLID;
1458 srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1459 oa->o_flags & OBD_FL_SRVLOCK;
1462 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1468 CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1469 ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1470 oa->o_valid, start, end);
1472 fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1475 GOTO(out, rc = PTR_ERR(fo));
1477 la_from_obdo(&info->fti_attr, oa,
1478 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1479 info->fti_attr.la_size = start;
1480 info->fti_attr.la_valid |= LA_SIZE;
1482 if (oa->o_valid & OBD_MD_FLFID) {
1483 ff = &info->fti_mds_fid;
1484 ofd_prepare_fidea(ff, oa);
1487 rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1492 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1496 ofd_object_put(tsi->tsi_env, fo);
1499 tgt_extent_unlock(&lh, LCK_PW);
1501 /* we do not call this before to avoid lu_object_find() in
1502 * ->lvbo_update() holding another reference on the object.
1503 * otherwise concurrent destroy can make the object unavailable
1504 * for 2nd lu_object_find() waiting for the first reference
1505 * to go... deadlock! */
1506 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1509 ldlm_res_lvbo_update(res, NULL, 0);
1510 ldlm_resource_putref(res);
1517 static int ofd_quotactl(struct tgt_session_info *tsi)
1519 struct obd_quotactl *oqctl, *repoqc;
1524 oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1526 RETURN(err_serious(-EPROTO));
1528 repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1530 RETURN(err_serious(-ENOMEM));
1532 /* report success for quota on/off for interoperability with current MDT
1534 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1538 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1540 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1546 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
1547 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
1548 #define OST_BRW_READ OST_READ
1549 #define OST_BRW_WRITE OST_WRITE
1551 static struct tgt_handler ofd_tgt_handlers[] = {
1552 TGT_RPC_HANDLER(OST_FIRST_OPC,
1553 0, OST_CONNECT, tgt_connect,
1554 &RQF_CONNECT, LUSTRE_OBD_VERSION),
1555 TGT_RPC_HANDLER(OST_FIRST_OPC,
1556 0, OST_DISCONNECT, tgt_disconnect,
1557 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
1558 TGT_RPC_HANDLER(OST_FIRST_OPC,
1559 0, OST_SET_INFO, ofd_set_info_hdl,
1560 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
1561 TGT_OST_HDL(0, OST_GET_INFO, ofd_get_info_hdl),
1562 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR, ofd_getattr_hdl),
1563 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1564 OST_SETATTR, ofd_setattr_hdl),
1565 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
1566 OST_CREATE, ofd_create_hdl),
1567 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
1568 OST_DESTROY, ofd_destroy_hdl),
1569 TGT_OST_HDL(0 | HABEO_REFERO, OST_STATFS, ofd_statfs_hdl),
1570 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_BRW_READ, tgt_brw_read),
1571 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
1572 TGT_OST_HDL(MUTABOR, OST_BRW_WRITE, tgt_brw_write),
1573 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1574 OST_PUNCH, ofd_punch_hdl),
1575 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC, ofd_sync_hdl),
1576 TGT_OST_HDL(0 | HABEO_REFERO, OST_QUOTACTL, ofd_quotactl),
1579 static struct tgt_opc_slice ofd_common_slice[] = {
1581 .tos_opc_start = OST_FIRST_OPC,
1582 .tos_opc_end = OST_LAST_OPC,
1583 .tos_hs = ofd_tgt_handlers
1586 .tos_opc_start = OBD_FIRST_OPC,
1587 .tos_opc_end = OBD_LAST_OPC,
1588 .tos_hs = tgt_obd_handlers
1591 .tos_opc_start = LDLM_FIRST_OPC,
1592 .tos_opc_end = LDLM_LAST_OPC,
1593 .tos_hs = tgt_dlm_handlers
1596 .tos_opc_start = UPDATE_OBJ,
1597 .tos_opc_end = UPDATE_LAST_OPC,
1598 .tos_hs = tgt_out_handlers
1601 .tos_opc_start = SEQ_FIRST_OPC,
1602 .tos_opc_end = SEQ_LAST_OPC,
1603 .tos_hs = seq_handlers
1610 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
1611 struct lu_device_type *ldt, struct lustre_cfg *cfg)
1613 const char *dev = lustre_cfg_string(cfg, 0);
1614 struct ofd_thread_info *info = NULL;
1615 struct obd_device *obd;
1616 struct obd_statfs *osfs;
1621 obd = class_name2obd(dev);
1623 CERROR("Cannot find obd with name %s\n", dev);
1627 rc = lu_env_refill((struct lu_env *)env);
1631 obd->u.obt.obt_magic = OBT_MAGIC;
1633 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
1634 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
1636 spin_lock_init(&m->ofd_flags_lock);
1637 m->ofd_raid_degraded = 0;
1638 m->ofd_syncjournal = 0;
1640 m->ofd_grant_compat_disable = 0;
1641 m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
1644 spin_lock_init(&m->ofd_osfs_lock);
1645 m->ofd_osfs_age = cfs_time_shift_64(-1000);
1646 m->ofd_osfs_unstable = 0;
1647 m->ofd_statfs_inflight = 0;
1648 m->ofd_osfs_inflight = 0;
1651 spin_lock_init(&m->ofd_grant_lock);
1652 m->ofd_tot_dirty = 0;
1653 m->ofd_tot_granted = 0;
1654 m->ofd_tot_pending = 0;
1655 m->ofd_seq_count = 0;
1657 spin_lock_init(&m->ofd_batch_lock);
1658 rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
1659 sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
1661 obd->u.filter.fo_fl_oss_capa = 0;
1662 CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
1663 obd->u.filter.fo_capa_hash = init_capa_hash();
1664 if (obd->u.filter.fo_capa_hash == NULL)
1667 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
1668 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
1669 /* set this lu_device to obd, because error handling need it */
1670 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
1672 rc = ofd_procfs_init(m);
1674 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
1678 /* No connection accepted until configurations will finish */
1679 spin_lock(&obd->obd_dev_lock);
1680 obd->obd_no_conn = 1;
1681 spin_unlock(&obd->obd_dev_lock);
1682 obd->obd_replayable = 1;
1683 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
1684 char *str = lustre_cfg_string(cfg, 4);
1686 if (strchr(str, 'n')) {
1687 CWARN("%s: recovery disabled\n", obd->obd_name);
1688 obd->obd_replayable = 0;
1692 info = ofd_info_init(env, NULL);
1696 rc = ofd_stack_init(env, m, cfg);
1698 CERROR("Can't init device stack, rc %d\n", rc);
1699 GOTO(err_fini_proc, rc);
1702 ofd_procfs_add_brw_stats_symlink(m);
1704 /* populate cached statfs data */
1705 osfs = &ofd_info(env)->fti_u.osfs;
1706 rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
1708 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
1709 GOTO(err_fini_stack, rc);
1711 if (!IS_PO2(osfs->os_bsize)) {
1712 CERROR("%s: blocksize (%d) is not a power of 2\n",
1713 obd->obd_name, osfs->os_bsize);
1714 GOTO(err_fini_stack, rc = -EPROTO);
1716 m->ofd_blockbits = fls(osfs->os_bsize) - 1;
1718 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
1719 if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
1720 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
1722 snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
1723 "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
1724 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
1725 LDLM_NAMESPACE_SERVER,
1726 LDLM_NAMESPACE_GREEDY,
1728 if (m->ofd_namespace == NULL)
1729 GOTO(err_fini_stack, rc = -ENOMEM);
1730 /* set obd_namespace for compatibility with old code */
1731 obd->obd_namespace = m->ofd_namespace;
1732 ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
1733 m->ofd_namespace->ns_lvbo = &ofd_lvbo;
1734 m->ofd_namespace->ns_lvbp = m;
1736 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1737 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1739 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
1741 /* Allow at most ddp_grant_reserved% of the available filesystem space
1742 * to be granted to clients, so that any errors in the grant overhead
1743 * calculations do not allow granting more space to clients than can be
1744 * written. Assumes that in aggregate the grant overhead calculations do
1745 * not have more than ddp_grant_reserved% estimation error in them. */
1746 m->ofd_grant_ratio =
1747 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
1749 rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
1750 OBD_FAIL_OST_ALL_REQUEST_NET,
1751 OBD_FAIL_OST_ALL_REPLY_NET);
1753 GOTO(err_free_ns, rc);
1755 rc = ofd_fs_setup(env, m, obd);
1757 GOTO(err_fini_lut, rc);
1761 tgt_fini(env, &m->ofd_lut);
1763 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
1764 obd->obd_namespace = m->ofd_namespace = NULL;
1766 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
1772 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
1774 struct obd_device *obd = ofd_obd(m);
1775 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
1777 lfsck_stop(env, m->ofd_osd, true);
1778 lfsck_degister(env, m->ofd_osd);
1779 target_recovery_fini(obd);
1780 obd_exports_barrier(obd);
1781 obd_zombie_barrier();
1783 tgt_fini(env, &m->ofd_lut);
1784 ofd_fs_cleanup(env, m);
1786 ofd_free_capa_keys(m);
1787 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
1789 if (m->ofd_namespace != NULL) {
1790 ldlm_namespace_free(m->ofd_namespace, NULL,
1791 d->ld_obd->obd_force);
1792 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
1795 ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
1797 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
1798 server_put_mount(obd->obd_name);
1802 static struct lu_device *ofd_device_fini(const struct lu_env *env,
1803 struct lu_device *d)
1806 ofd_fini(env, ofd_dev(d));
1810 static struct lu_device *ofd_device_free(const struct lu_env *env,
1811 struct lu_device *d)
1813 struct ofd_device *m = ofd_dev(d);
1815 dt_device_fini(&m->ofd_dt_dev);
1820 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
1821 struct lu_device_type *t,
1822 struct lustre_cfg *cfg)
1824 struct ofd_device *m;
1825 struct lu_device *l;
1830 return ERR_PTR(-ENOMEM);
1832 l = &m->ofd_dt_dev.dd_lu_dev;
1833 dt_device_init(&m->ofd_dt_dev, t);
1834 rc = ofd_init0(env, m, t, cfg);
1836 ofd_device_free(env, l);
1843 /* thread context key constructor/destructor */
1844 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
1846 static void ofd_key_exit(const struct lu_context *ctx,
1847 struct lu_context_key *key, void *data)
1849 struct ofd_thread_info *info = data;
1851 info->fti_env = NULL;
1852 info->fti_exp = NULL;
1855 info->fti_pre_version = 0;
1858 memset(&info->fti_attr, 0, sizeof info->fti_attr);
1861 struct lu_context_key ofd_thread_key = {
1862 .lct_tags = LCT_DT_THREAD,
1863 .lct_init = ofd_key_init,
1864 .lct_fini = ofd_key_fini,
1865 .lct_exit = ofd_key_exit
1868 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
1869 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
1871 static struct lu_device_type_operations ofd_device_type_ops = {
1872 .ldto_init = ofd_type_init,
1873 .ldto_fini = ofd_type_fini,
1875 .ldto_start = ofd_type_start,
1876 .ldto_stop = ofd_type_stop,
1878 .ldto_device_alloc = ofd_device_alloc,
1879 .ldto_device_free = ofd_device_free,
1880 .ldto_device_fini = ofd_device_fini
1883 static struct lu_device_type ofd_device_type = {
1884 .ldt_tags = LU_DEVICE_DT,
1885 .ldt_name = LUSTRE_OST_NAME,
1886 .ldt_ops = &ofd_device_type_ops,
1887 .ldt_ctx_tags = LCT_DT_THREAD
1890 int __init ofd_init(void)
1892 struct lprocfs_static_vars lvars;
1895 rc = lu_kmem_init(ofd_caches);
1899 rc = ofd_fmd_init();
1901 lu_kmem_fini(ofd_caches);
1905 lprocfs_ofd_init_vars(&lvars);
1907 rc = class_register_type(&ofd_obd_ops, NULL, NULL,
1908 #ifndef HAVE_ONLY_PROCFS_SEQ
1911 LUSTRE_OST_NAME, &ofd_device_type);
1915 void __exit ofd_exit(void)
1918 lu_kmem_fini(ofd_caches);
1919 class_unregister_type(LUSTRE_OST_NAME);
1922 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
1923 MODULE_DESCRIPTION("Lustre Object Filtering Device");
1924 MODULE_LICENSE("GPL");
1926 module_init(ofd_init);
1927 module_exit(ofd_exit);