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_prepare(const struct lu_env *env, struct lu_device *pdev,
385 struct lu_device *dev)
387 struct ofd_thread_info *info;
388 struct ofd_device *ofd = ofd_dev(dev);
389 struct obd_device *obd = ofd_obd(ofd);
390 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
391 struct lfsck_start_param lsp;
396 rc = lu_env_refill((struct lu_env *)env);
398 CERROR("Failure to refill session: '%d'\n", rc);
402 info = ofd_info_init(env, NULL);
406 /* initialize lower device */
407 rc = next->ld_ops->ldo_prepare(env, dev, next);
411 rc = lfsck_register(env, ofd->ofd_osd, &ofd->ofd_dt_dev, false);
413 CERROR("%s: failed to initialize lfsck: rc = %d\n",
418 lsp.lsp_start = NULL;
419 lsp.lsp_namespace = ofd->ofd_namespace;
420 rc = lfsck_start(env, ofd->ofd_osd, &lsp);
422 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
427 target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
428 LASSERT(obd->obd_no_conn);
429 spin_lock(&obd->obd_dev_lock);
430 obd->obd_no_conn = 0;
431 spin_unlock(&obd->obd_dev_lock);
433 if (obd->obd_recovering == 0)
434 ofd_postrecov(env, ofd);
439 static int ofd_recovery_complete(const struct lu_env *env,
440 struct lu_device *dev)
442 struct ofd_device *ofd = ofd_dev(dev);
443 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
444 int rc = 0, max_precreate;
448 /* Grant space for object precreation on the self export.
449 * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
450 * is enough to create 10k objects. More space is then acquired for
451 * precreation in ofd_grant_create().
453 max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
454 ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
456 rc = next->ld_ops->ldo_recovery_complete(env, next);
460 static struct lu_device_operations ofd_lu_ops = {
461 .ldo_object_alloc = ofd_object_alloc,
462 .ldo_process_config = ofd_process_config,
463 .ldo_recovery_complete = ofd_recovery_complete,
464 .ldo_prepare = ofd_prepare,
467 static int ofd_procfs_init(struct ofd_device *ofd)
469 struct lprocfs_static_vars lvars;
470 struct obd_device *obd = ofd_obd(ofd);
471 cfs_proc_dir_entry_t *entry;
476 /* lprocfs must be setup before the ofd so state can be safely added
477 * to /proc incrementally as the ofd is setup */
478 lprocfs_ofd_init_vars(&lvars);
479 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
481 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
486 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
488 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
490 GOTO(obd_cleanup, rc);
493 obd->obd_uses_nid_stats = 1;
495 entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
498 CERROR("%s: error %d setting up lprocfs for %s\n",
499 obd->obd_name, rc, "exports");
500 GOTO(obd_cleanup, rc);
502 obd->obd_proc_exports_entry = entry;
504 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
505 lprocfs_nid_stats_clear_read,
506 lprocfs_nid_stats_clear_write, obd, NULL);
509 CERROR("%s: add proc entry 'clear' failed: %d.\n",
511 GOTO(obd_cleanup, rc);
514 ofd_stats_counter_init(obd->obd_stats);
516 rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
517 ofd_stats_counter_init);
519 GOTO(remove_entry_clear, rc);
522 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
524 lprocfs_obd_cleanup(obd);
525 lprocfs_free_obd_stats(obd);
530 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
532 struct obd_device *obd = ofd_obd(ofd);
533 struct obd_device *osd_obd = ofd->ofd_osd_exp->exp_obd;
534 cfs_proc_dir_entry_t *osd_root = osd_obd->obd_type->typ_procroot;
535 cfs_proc_dir_entry_t *osd_dir;
537 osd_dir = lprocfs_srch(osd_root, obd->obd_name);
541 if (lprocfs_srch(osd_dir, "brw_stats") != NULL)
542 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
543 "../../%s/%s/brw_stats",
544 osd_root->name, osd_dir->name);
546 if (lprocfs_srch(osd_dir, "read_cache_enable") != NULL)
547 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
548 "../../%s/%s/read_cache_enable",
549 osd_root->name, osd_dir->name);
551 if (lprocfs_srch(osd_dir, "readcache_max_filesize") != NULL)
552 lprocfs_add_symlink("readcache_max_filesize",
554 "../../%s/%s/readcache_max_filesize",
555 osd_root->name, osd_dir->name);
557 if (lprocfs_srch(osd_dir, "writethrough_cache_enable") != NULL)
558 lprocfs_add_symlink("writethrough_cache_enable",
560 "../../%s/%s/writethrough_cache_enable",
561 osd_root->name, osd_dir->name);
564 static void ofd_procfs_fini(struct ofd_device *ofd)
566 struct obd_device *obd = ofd_obd(ofd);
568 lprocfs_remove_proc_entry("writethrough_cache_enable",
569 obd->obd_proc_entry);
570 lprocfs_remove_proc_entry("readcache_max_filesize",
571 obd->obd_proc_entry);
572 lprocfs_remove_proc_entry("read_cache_enable", obd->obd_proc_entry);
573 lprocfs_remove_proc_entry("brw_stats", obd->obd_proc_entry);
574 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
575 lprocfs_free_per_client_stats(obd);
576 lprocfs_obd_cleanup(obd);
577 lprocfs_free_obd_stats(obd);
578 lprocfs_job_stats_fini(obd);
581 extern int ost_handle(struct ptlrpc_request *req);
583 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
585 return seq_site_fini(env, &ofd->ofd_seq_site);
588 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
590 struct seq_server_site *ss = &ofd->ofd_seq_site;
591 struct lu_device *lu = &ofd->ofd_dt_dev.dd_lu_dev;
592 char *obd_name = ofd_name(ofd);
596 ss = &ofd->ofd_seq_site;
597 lu->ld_site->ld_seq_site = ss;
598 ss->ss_lu = lu->ld_site;
599 ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
601 OBD_ALLOC_PTR(ss->ss_server_seq);
602 if (ss->ss_server_seq == NULL)
603 GOTO(out_free, rc = -ENOMEM);
605 OBD_ALLOC(name, strlen(obd_name) + 10);
607 OBD_FREE_PTR(ss->ss_server_seq);
608 ss->ss_server_seq = NULL;
609 GOTO(out_free, rc = -ENOMEM);
612 rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
613 LUSTRE_SEQ_SERVER, ss);
615 CERROR("%s : seq server init error %d\n", obd_name, rc);
618 ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
620 OBD_ALLOC_PTR(ss->ss_client_seq);
621 if (ss->ss_client_seq == NULL)
622 GOTO(out_free, rc = -ENOMEM);
624 snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
625 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
628 CERROR("%s : seq client init error %d\n", obd_name, rc);
631 OBD_FREE(name, strlen(obd_name) + 10);
634 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
638 if (ss->ss_server_seq) {
639 seq_server_fini(ss->ss_server_seq, env);
640 OBD_FREE_PTR(ss->ss_server_seq);
641 ss->ss_server_seq = NULL;
644 if (ss->ss_client_seq) {
645 seq_client_fini(ss->ss_client_seq);
646 OBD_FREE_PTR(ss->ss_client_seq);
647 ss->ss_client_seq = NULL;
651 OBD_FREE(name, strlen(obd_name) + 10);
659 int ofd_set_info_hdl(struct tgt_session_info *tsi)
661 struct ptlrpc_request *req = tgt_ses_req(tsi);
662 struct ost_body *body = NULL, *repbody;
663 void *key, *val = NULL;
664 int keylen, vallen, rc = 0;
665 bool is_grant_shrink;
666 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
670 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
672 DEBUG_REQ(D_HA, req, "no set_info key");
673 RETURN(err_serious(-EFAULT));
675 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
678 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
680 DEBUG_REQ(D_HA, req, "no set_info val");
681 RETURN(err_serious(-EFAULT));
683 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
686 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
688 /* In this case the value is actually an RMF_OST_BODY, so we
689 * transmutate the type of this PTLRPC */
690 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
692 rc = req_capsule_server_pack(tsi->tsi_pill);
696 if (is_grant_shrink) {
697 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
699 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
702 /** handle grant shrink, similar to a read request */
703 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
705 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
707 obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
709 } else if (KEY_IS(KEY_CAPA_KEY)) {
710 rc = ofd_update_capa_key(ofd, val);
711 } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
712 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
714 CERROR("%s: Unsupported key %s\n",
715 tgt_name(tsi->tsi_tgt), (char *)key);
718 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
724 static int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
725 struct lu_fid *fid, struct ll_user_fiemap *fiemap)
727 struct ofd_object *fo;
730 fo = ofd_object_find(env, ofd, fid);
732 CERROR("%s: error finding object "DFID"\n",
733 ofd_name(ofd), PFID(fid));
737 ofd_read_lock(env, fo);
738 if (ofd_object_exists(fo))
739 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
742 ofd_read_unlock(env, fo);
743 ofd_object_put(env, fo);
747 struct locked_region {
749 struct lustre_handle lh;
752 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
753 unsigned long long begin, unsigned long long end,
756 struct locked_region *region = NULL;
760 LASSERT(begin <= end);
761 OBD_ALLOC_PTR(region);
765 rc = tgt_extent_lock(ns, res_id, begin, end, ®ion->lh,
770 CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
772 cfs_list_add(®ion->list, locked);
777 static int lock_zero_regions(struct ldlm_namespace *ns,
778 struct ldlm_res_id *res_id,
779 struct ll_user_fiemap *fiemap,
782 __u64 begin = fiemap->fm_start;
785 struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
789 CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
790 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
791 if (fiemap_start[i].fe_logical > begin) {
792 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
793 begin, fiemap_start[i].fe_logical);
794 rc = lock_region(ns, res_id, begin,
795 fiemap_start[i].fe_logical, locked);
800 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
803 if (begin < (fiemap->fm_start + fiemap->fm_length)) {
804 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
805 begin, fiemap->fm_start + fiemap->fm_length);
806 rc = lock_region(ns, res_id, begin,
807 fiemap->fm_start + fiemap->fm_length, locked);
813 static void unlock_zero_regions(struct ldlm_namespace *ns, cfs_list_t *locked)
815 struct locked_region *entry, *temp;
817 cfs_list_for_each_entry_safe(entry, temp, locked, list) {
818 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
819 tgt_extent_unlock(&entry->lh, LCK_PR);
820 cfs_list_del(&entry->list);
825 int ofd_get_info_hdl(struct tgt_session_info *tsi)
827 struct obd_export *exp = tsi->tsi_exp;
828 struct ofd_device *ofd = ofd_exp(exp);
829 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
832 int replylen, rc = 0;
836 /* this common part for get_info rpc */
837 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
839 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
840 RETURN(err_serious(-EPROTO));
842 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
845 if (KEY_IS(KEY_LAST_ID)) {
847 struct ofd_seq *oseq;
849 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
850 rc = req_capsule_server_pack(tsi->tsi_pill);
852 RETURN(err_serious(rc));
854 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
856 oseq = ofd_seq_load(tsi->tsi_env, ofd,
857 (obd_seq)exp->exp_filter_data.fed_group);
861 *last_id = ofd_seq_last_oid(oseq);
862 ofd_seq_put(tsi->tsi_env, oseq);
863 } else if (KEY_IS(KEY_FIEMAP)) {
864 struct ll_fiemap_info_key *fm_key;
865 struct ll_user_fiemap *fiemap;
866 struct lu_fid *fid = &fti->fti_fid;
868 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
870 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
871 rc = tgt_validate_obdo(tsi, &fm_key->oa);
873 RETURN(err_serious(rc));
875 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
876 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
877 RCL_SERVER, replylen);
879 rc = req_capsule_server_pack(tsi->tsi_pill);
881 RETURN(err_serious(rc));
883 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
887 rc = ostid_to_fid(fid, &fm_key->oa.o_oi, 0);
891 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
893 *fiemap = fm_key->fiemap;
894 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
896 /* LU-3219: Lock the sparse areas to make sure dirty
897 * flushed back from client, then call fiemap again. */
898 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
899 fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
900 cfs_list_t locked = CFS_LIST_HEAD_INIT(locked);
902 ost_fid_build_resid(fid, &fti->fti_resid);
903 rc = lock_zero_regions(ofd->ofd_namespace,
904 &fti->fti_resid, fiemap,
906 if (rc == 0 && !cfs_list_empty(&locked)) {
907 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
909 unlock_zero_regions(ofd->ofd_namespace,
913 } else if (KEY_IS(KEY_LAST_FID)) {
914 struct ofd_device *ofd = ofd_exp(exp);
915 struct ofd_seq *oseq;
919 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
920 rc = req_capsule_server_pack(tsi->tsi_pill);
922 RETURN(err_serious(rc));
924 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
926 RETURN(err_serious(-EPROTO));
928 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
930 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
934 oseq = ofd_seq_load(tsi->tsi_env, ofd,
935 ostid_seq(&fti->fti_ostid));
937 RETURN(PTR_ERR(oseq));
939 rc = ostid_to_fid(fid, &oseq->os_oi,
940 ofd->ofd_lut.lut_lsd.lsd_osd_index);
944 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
947 ofd_seq_put(tsi->tsi_env, oseq);
949 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
953 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
959 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
961 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
962 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
963 struct ost_body *repbody;
964 struct lustre_handle lh = { 0 };
965 struct ofd_object *fo;
967 ldlm_mode_t lock_mode = LCK_PR;
972 LASSERT(tsi->tsi_ost_body != NULL);
974 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
978 repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
979 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
981 srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
982 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
985 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
988 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
989 &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
995 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
997 GOTO(out, rc = PTR_ERR(fo));
999 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1003 obdo_from_la(&repbody->oa, &fti->fti_attr,
1004 OFD_VALID_FLAGS | LA_UID | LA_GID);
1005 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1007 /* Store object version in reply */
1008 curr_version = dt_version_get(tsi->tsi_env,
1009 ofd_object_child(fo));
1010 if ((__s64)curr_version != -EOPNOTSUPP) {
1011 repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
1012 repbody->oa.o_data_version = curr_version;
1016 ofd_object_put(tsi->tsi_env, fo);
1019 tgt_extent_unlock(&lh, lock_mode);
1021 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
1024 repbody->oa.o_valid |= OBD_MD_FLFLAGS;
1025 repbody->oa.o_flags = OBD_FL_FLUSH;
1030 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
1032 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1033 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1034 struct ost_body *body = tsi->tsi_ost_body;
1035 struct ost_body *repbody;
1036 struct ldlm_resource *res;
1037 struct ofd_object *fo;
1038 struct filter_fid *ff = NULL;
1043 LASSERT(body != NULL);
1045 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1046 if (repbody == NULL)
1049 repbody->oa.o_oi = body->oa.o_oi;
1050 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1052 /* This would be very bad - accidentally truncating a file when
1053 * changing the time or similar - bug 12203. */
1054 if (body->oa.o_valid & OBD_MD_FLSIZE &&
1055 body->oa.o_size != OBD_OBJECT_EOF) {
1056 static char mdsinum[48];
1058 if (body->oa.o_valid & OBD_MD_FLFID)
1059 snprintf(mdsinum, sizeof(mdsinum) - 1,
1060 "of parent "DFID, body->oa.o_parent_seq,
1061 body->oa.o_parent_oid, 0);
1065 CERROR("%s: setattr from %s is trying to truncate object "DFID
1066 " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1067 PFID(&tsi->tsi_fid), mdsinum);
1071 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1073 GOTO(out, rc = PTR_ERR(fo));
1075 la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1076 fti->fti_attr.la_valid &= ~LA_TYPE;
1078 if (body->oa.o_valid & OBD_MD_FLFID) {
1079 ff = &fti->fti_mds_fid;
1080 ofd_prepare_fidea(ff, &body->oa);
1083 /* setting objects attributes (including owner/group) */
1084 rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1088 obdo_from_la(&repbody->oa, &fti->fti_attr,
1089 OFD_VALID_FLAGS | LA_UID | LA_GID);
1090 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1092 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1096 ofd_object_put(tsi->tsi_env, fo);
1099 /* we do not call this before to avoid lu_object_find() in
1100 * ->lvbo_update() holding another reference on the object.
1101 * otherwise concurrent destroy can make the object unavailable
1102 * for 2nd lu_object_find() waiting for the first reference
1103 * to go... deadlock! */
1104 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1105 &tsi->tsi_resid, LDLM_EXTENT, 0);
1107 ldlm_res_lvbo_update(res, NULL, 0);
1108 ldlm_resource_putref(res);
1114 static int ofd_create_hdl(struct tgt_session_info *tsi)
1116 struct ost_body *repbody;
1117 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1118 struct obdo *rep_oa;
1119 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1120 obd_seq seq = ostid_seq(&oa->o_oi);
1121 obd_id oid = ostid_id(&oa->o_oi);
1122 struct ofd_seq *oseq;
1128 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1131 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1132 if (repbody == NULL)
1135 rep_oa = &repbody->oa;
1136 rep_oa->o_oi = oa->o_oi;
1138 LASSERT(seq >= FID_SEQ_OST_MDT0);
1139 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1141 CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1143 oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1145 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1146 ofd_name(ofd), seq, PTR_ERR(oseq));
1150 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1151 (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1152 if (!ofd_obd(ofd)->obd_recovering ||
1153 oid > ofd_seq_last_oid(oseq)) {
1154 CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1155 "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1156 ofd_seq_last_oid(oseq));
1157 GOTO(out_nolock, rc = -EINVAL);
1159 /* Do nothing here, we re-create objects during recovery
1160 * upon write replay, see ofd_preprw_write() */
1161 GOTO(out_nolock, rc = 0);
1163 /* former ofd_handle_precreate */
1164 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1165 (oa->o_flags & OBD_FL_DELORPHAN)) {
1166 /* destroy orphans */
1167 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1168 tsi->tsi_exp->exp_conn_cnt) {
1169 CERROR("%s: dropping old orphan cleanup request\n",
1171 GOTO(out_nolock, rc = 0);
1173 /* This causes inflight precreates to abort and drop lock */
1174 oseq->os_destroys_in_progress = 1;
1175 mutex_lock(&oseq->os_create_lock);
1176 if (!oseq->os_destroys_in_progress) {
1177 CERROR("%s:["LPU64"] destroys_in_progress already"
1178 " cleared\n", ofd_name(ofd), seq);
1179 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1182 diff = oid - ofd_seq_last_oid(oseq);
1183 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1184 ofd_seq_last_oid(oseq), diff);
1185 if (-diff > OST_MAX_PRECREATE) {
1186 /* FIXME: should reset precreate_next_id on MDS */
1188 } else if (diff < 0) {
1189 rc = ofd_orphans_destroy(tsi->tsi_env, tsi->tsi_exp,
1191 oseq->os_destroys_in_progress = 0;
1193 /* XXX: Used by MDS for the first time! */
1194 oseq->os_destroys_in_progress = 0;
1197 mutex_lock(&oseq->os_create_lock);
1198 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1199 tsi->tsi_exp->exp_conn_cnt) {
1200 CERROR("%s: dropping old precreate request\n",
1204 /* only precreate if seq is 0, IDIF or normal and also o_id
1205 * must be specfied */
1206 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1207 !fid_seq_is_idif(seq)) || oid == 0) {
1208 diff = 1; /* shouldn't we create this right now? */
1210 diff = oid - ofd_seq_last_oid(oseq);
1211 /* Do sync create if the seq is about to used up */
1212 if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1213 if (unlikely(oid >= IDIF_MAX_OID - 1))
1215 } else if (fid_seq_is_norm(seq)) {
1217 LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1220 CERROR("%s : invalid o_seq "DOSTID"\n",
1221 ofd_name(ofd), POSTID(&oa->o_oi));
1222 GOTO(out, rc = -EINVAL);
1227 cfs_time_t enough_time = cfs_time_shift(DISK_TIMEOUT);
1232 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1233 !(oa->o_flags & OBD_FL_DELORPHAN)) {
1234 /* don't enforce grant during orphan recovery */
1235 rc = ofd_grant_create(tsi->tsi_env,
1236 ofd_obd(ofd)->obd_self_export,
1239 CDEBUG(D_HA, "%s: failed to acquire grant "
1240 "space for precreate (%d): rc = %d\n",
1241 ofd_name(ofd), diff, rc);
1246 /* This can happen if a new OST is formatted and installed
1247 * in place of an old one at the same index. Instead of
1248 * precreating potentially millions of deleted old objects
1249 * (possibly filling the OST), only precreate the last batch.
1250 * LFSCK will eventually clean up any orphans. LU-14 */
1251 if (diff > 5 * OST_MAX_PRECREATE) {
1252 diff = OST_MAX_PRECREATE / 2;
1253 LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1254 "larger than the LAST_ID "DOSTID", only "
1255 "precreating the last %u objects.\n",
1256 ofd_name(ofd), POSTID(&oa->o_oi),
1257 5 * OST_MAX_PRECREATE,
1258 POSTID(&oseq->os_oi), diff);
1259 ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1263 next_id = ofd_seq_last_oid(oseq) + 1;
1264 count = ofd_precreate_batch(ofd, diff);
1266 CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1267 " at "LPU64"\n", ofd_name(ofd),
1268 count, seq, next_id);
1270 if (cfs_time_after(jiffies, enough_time)) {
1271 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1272 " created at a rate of %d/s\n",
1273 ofd_name(ofd), created,
1275 created / DISK_TIMEOUT);
1279 rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1280 oseq, count, sync_trans);
1284 } else if (rc < 0) {
1289 /* some objects got created, we can return
1290 * them, even if last creation failed */
1293 CERROR("%s: unable to precreate: rc = %d\n",
1296 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1297 !(oa->o_flags & OBD_FL_DELORPHAN))
1298 ofd_grant_commit(tsi->tsi_env,
1299 ofd_obd(ofd)->obd_self_export, rc);
1301 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1304 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_CREATE,
1307 mutex_unlock(&oseq->os_create_lock);
1310 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1312 ofd_seq_put(tsi->tsi_env, oseq);
1316 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1318 const struct ost_body *body = tsi->tsi_ost_body;
1319 struct ost_body *repbody;
1320 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1321 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1327 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1330 /* This is old case for clients before Lustre 2.4 */
1331 /* If there's a DLM request, cancel the locks mentioned in it */
1332 if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1334 struct ldlm_request *dlm;
1336 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1339 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1342 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1343 repbody->oa.o_oi = body->oa.o_oi;
1345 /* check that o_misc makes sense */
1346 if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1347 count = body->oa.o_misc;
1349 count = 1; /* default case - single destroy */
1351 CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1352 POSTID(&body->oa.o_oi), count);
1356 lrc = ostid_to_fid(&fti->fti_fid, &repbody->oa.o_oi, 0);
1362 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, &fti->fti_fid, 0);
1363 if (lrc == -ENOENT) {
1365 "%s: destroying non-existent object "DFID"\n",
1366 ofd_name(ofd), PFID(&fti->fti_fid));
1367 /* rewrite rc with -ENOENT only if it is 0 */
1370 } else if (lrc != 0) {
1371 CERROR("%s: error destroying object "DFID": %d\n",
1372 ofd_name(ofd), PFID(&fti->fti_fid),
1377 ostid_inc_id(&repbody->oa.o_oi);
1380 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1386 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1388 struct obd_statfs *osfs;
1393 osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1395 rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1396 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1398 CERROR("%s: statfs failed: rc = %d\n",
1399 tgt_name(tsi->tsi_tgt), rc);
1401 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1404 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1410 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1412 struct ost_body *body = tsi->tsi_ost_body;
1413 struct ost_body *repbody;
1414 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1415 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1416 struct ofd_object *fo = NULL;
1421 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1423 /* if no objid is specified, it means "sync whole filesystem" */
1424 if (!fid_is_zero(&tsi->tsi_fid)) {
1425 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1427 RETURN(PTR_ERR(fo));
1430 rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1431 fo != NULL ? ofd_object_child(fo) : NULL);
1435 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1440 repbody->oa.o_oi = body->oa.o_oi;
1441 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1443 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1445 obdo_from_la(&repbody->oa, &fti->fti_attr,
1448 /* don't return rc from getattr */
1453 ofd_object_put(tsi->tsi_env, fo);
1457 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1459 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1460 struct ost_body *repbody;
1461 struct ofd_thread_info *info = tsi2ofd_info(tsi);
1462 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1463 struct ldlm_resource *res;
1464 struct ofd_object *fo;
1465 struct filter_fid *ff = NULL;
1467 struct lustre_handle lh = { 0, };
1474 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1475 CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1477 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1478 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1479 RETURN(err_serious(-EPROTO));
1481 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1482 if (repbody == NULL)
1483 RETURN(err_serious(-ENOMEM));
1485 /* punch start,end are passed in o_size,o_blocks throught wire */
1489 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1492 /* standard truncate optimization: if file body is completely
1493 * destroyed, don't send data back to the server. */
1495 flags |= LDLM_FL_AST_DISCARD_DATA;
1497 repbody->oa.o_oi = oa->o_oi;
1498 repbody->oa.o_valid = OBD_MD_FLID;
1500 srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1501 oa->o_flags & OBD_FL_SRVLOCK;
1504 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1510 CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1511 ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1512 oa->o_valid, start, end);
1514 fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1517 GOTO(out, rc = PTR_ERR(fo));
1519 la_from_obdo(&info->fti_attr, oa,
1520 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1521 info->fti_attr.la_size = start;
1522 info->fti_attr.la_valid |= LA_SIZE;
1524 if (oa->o_valid & OBD_MD_FLFID) {
1525 ff = &info->fti_mds_fid;
1526 ofd_prepare_fidea(ff, oa);
1529 rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1534 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1538 ofd_object_put(tsi->tsi_env, fo);
1541 tgt_extent_unlock(&lh, LCK_PW);
1543 /* we do not call this before to avoid lu_object_find() in
1544 * ->lvbo_update() holding another reference on the object.
1545 * otherwise concurrent destroy can make the object unavailable
1546 * for 2nd lu_object_find() waiting for the first reference
1547 * to go... deadlock! */
1548 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1551 ldlm_res_lvbo_update(res, NULL, 0);
1552 ldlm_resource_putref(res);
1559 static int ofd_quotactl(struct tgt_session_info *tsi)
1561 struct obd_quotactl *oqctl, *repoqc;
1566 oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1568 RETURN(err_serious(-EPROTO));
1570 repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1572 RETURN(err_serious(-ENOMEM));
1574 /* report success for quota on/off for interoperability with current MDT
1576 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1580 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1582 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1588 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
1589 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
1590 #define OST_BRW_READ OST_READ
1591 #define OST_BRW_WRITE OST_WRITE
1593 static struct tgt_handler ofd_tgt_handlers[] = {
1594 TGT_RPC_HANDLER(OST_FIRST_OPC,
1595 0, OST_CONNECT, tgt_connect,
1596 &RQF_CONNECT, LUSTRE_OBD_VERSION),
1597 TGT_RPC_HANDLER(OST_FIRST_OPC,
1598 0, OST_DISCONNECT, tgt_disconnect,
1599 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
1600 TGT_RPC_HANDLER(OST_FIRST_OPC,
1601 0, OST_SET_INFO, ofd_set_info_hdl,
1602 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
1603 TGT_OST_HDL(0, OST_GET_INFO, ofd_get_info_hdl),
1604 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR, ofd_getattr_hdl),
1605 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1606 OST_SETATTR, ofd_setattr_hdl),
1607 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
1608 OST_CREATE, ofd_create_hdl),
1609 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
1610 OST_DESTROY, ofd_destroy_hdl),
1611 TGT_OST_HDL(0 | HABEO_REFERO, OST_STATFS, ofd_statfs_hdl),
1612 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_BRW_READ, tgt_brw_read),
1613 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
1614 TGT_OST_HDL(MUTABOR, OST_BRW_WRITE, tgt_brw_write),
1615 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1616 OST_PUNCH, ofd_punch_hdl),
1617 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC, ofd_sync_hdl),
1618 TGT_OST_HDL(0 | HABEO_REFERO, OST_QUOTACTL, ofd_quotactl),
1621 static struct tgt_opc_slice ofd_common_slice[] = {
1623 .tos_opc_start = OST_FIRST_OPC,
1624 .tos_opc_end = OST_LAST_OPC,
1625 .tos_hs = ofd_tgt_handlers
1628 .tos_opc_start = OBD_FIRST_OPC,
1629 .tos_opc_end = OBD_LAST_OPC,
1630 .tos_hs = tgt_obd_handlers
1633 .tos_opc_start = LDLM_FIRST_OPC,
1634 .tos_opc_end = LDLM_LAST_OPC,
1635 .tos_hs = tgt_dlm_handlers
1638 .tos_opc_start = UPDATE_OBJ,
1639 .tos_opc_end = UPDATE_LAST_OPC,
1640 .tos_hs = tgt_out_handlers
1643 .tos_opc_start = SEQ_FIRST_OPC,
1644 .tos_opc_end = SEQ_LAST_OPC,
1645 .tos_hs = seq_handlers
1652 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
1653 struct lu_device_type *ldt, struct lustre_cfg *cfg)
1655 const char *dev = lustre_cfg_string(cfg, 0);
1656 struct ofd_thread_info *info = NULL;
1657 struct obd_device *obd;
1658 struct obd_statfs *osfs;
1663 obd = class_name2obd(dev);
1665 CERROR("Cannot find obd with name %s\n", dev);
1669 rc = lu_env_refill((struct lu_env *)env);
1673 obd->u.obt.obt_magic = OBT_MAGIC;
1675 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
1676 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
1678 spin_lock_init(&m->ofd_flags_lock);
1679 m->ofd_raid_degraded = 0;
1680 m->ofd_syncjournal = 0;
1682 m->ofd_grant_compat_disable = 0;
1683 m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
1686 spin_lock_init(&m->ofd_osfs_lock);
1687 m->ofd_osfs_age = cfs_time_shift_64(-1000);
1688 m->ofd_osfs_unstable = 0;
1689 m->ofd_statfs_inflight = 0;
1690 m->ofd_osfs_inflight = 0;
1693 spin_lock_init(&m->ofd_grant_lock);
1694 m->ofd_tot_dirty = 0;
1695 m->ofd_tot_granted = 0;
1696 m->ofd_tot_pending = 0;
1697 m->ofd_seq_count = 0;
1699 spin_lock_init(&m->ofd_batch_lock);
1700 rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
1701 sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
1703 obd->u.filter.fo_fl_oss_capa = 0;
1704 CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
1705 obd->u.filter.fo_capa_hash = init_capa_hash();
1706 if (obd->u.filter.fo_capa_hash == NULL)
1709 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
1710 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
1711 /* set this lu_device to obd, because error handling need it */
1712 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
1714 rc = ofd_procfs_init(m);
1716 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
1720 /* No connection accepted until configurations will finish */
1721 spin_lock(&obd->obd_dev_lock);
1722 obd->obd_no_conn = 1;
1723 spin_unlock(&obd->obd_dev_lock);
1724 obd->obd_replayable = 1;
1725 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
1726 char *str = lustre_cfg_string(cfg, 4);
1728 if (strchr(str, 'n')) {
1729 CWARN("%s: recovery disabled\n", obd->obd_name);
1730 obd->obd_replayable = 0;
1734 info = ofd_info_init(env, NULL);
1738 rc = ofd_stack_init(env, m, cfg);
1740 CERROR("Can't init device stack, rc %d\n", rc);
1741 GOTO(err_fini_proc, rc);
1744 ofd_procfs_add_brw_stats_symlink(m);
1746 /* populate cached statfs data */
1747 osfs = &ofd_info(env)->fti_u.osfs;
1748 rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
1750 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
1751 GOTO(err_fini_stack, rc);
1753 if (!IS_PO2(osfs->os_bsize)) {
1754 CERROR("%s: blocksize (%d) is not a power of 2\n",
1755 obd->obd_name, osfs->os_bsize);
1756 GOTO(err_fini_stack, rc = -EPROTO);
1758 m->ofd_blockbits = fls(osfs->os_bsize) - 1;
1760 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
1761 if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
1762 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
1764 snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
1765 "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
1766 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
1767 LDLM_NAMESPACE_SERVER,
1768 LDLM_NAMESPACE_GREEDY,
1770 if (m->ofd_namespace == NULL)
1771 GOTO(err_fini_stack, rc = -ENOMEM);
1772 /* set obd_namespace for compatibility with old code */
1773 obd->obd_namespace = m->ofd_namespace;
1774 ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
1775 m->ofd_namespace->ns_lvbo = &ofd_lvbo;
1776 m->ofd_namespace->ns_lvbp = m;
1778 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1779 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1781 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
1783 /* Allow at most ddp_grant_reserved% of the available filesystem space
1784 * to be granted to clients, so that any errors in the grant overhead
1785 * calculations do not allow granting more space to clients than can be
1786 * written. Assumes that in aggregate the grant overhead calculations do
1787 * not have more than ddp_grant_reserved% estimation error in them. */
1788 m->ofd_grant_ratio =
1789 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
1791 rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
1792 OBD_FAIL_OST_ALL_REQUEST_NET,
1793 OBD_FAIL_OST_ALL_REPLY_NET);
1795 GOTO(err_free_ns, rc);
1797 rc = ofd_fs_setup(env, m, obd);
1799 GOTO(err_fini_lut, rc);
1803 tgt_fini(env, &m->ofd_lut);
1805 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
1806 obd->obd_namespace = m->ofd_namespace = NULL;
1808 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
1814 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
1816 struct obd_device *obd = ofd_obd(m);
1817 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
1819 lfsck_stop(env, m->ofd_osd, true);
1820 lfsck_degister(env, m->ofd_osd);
1821 target_recovery_fini(obd);
1822 obd_exports_barrier(obd);
1823 obd_zombie_barrier();
1825 tgt_fini(env, &m->ofd_lut);
1826 ofd_fs_cleanup(env, m);
1828 ofd_free_capa_keys(m);
1829 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
1831 if (m->ofd_namespace != NULL) {
1832 ldlm_namespace_free(m->ofd_namespace, NULL,
1833 d->ld_obd->obd_force);
1834 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
1837 ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
1839 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
1840 server_put_mount(obd->obd_name);
1844 static struct lu_device *ofd_device_fini(const struct lu_env *env,
1845 struct lu_device *d)
1848 ofd_fini(env, ofd_dev(d));
1852 static struct lu_device *ofd_device_free(const struct lu_env *env,
1853 struct lu_device *d)
1855 struct ofd_device *m = ofd_dev(d);
1857 dt_device_fini(&m->ofd_dt_dev);
1862 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
1863 struct lu_device_type *t,
1864 struct lustre_cfg *cfg)
1866 struct ofd_device *m;
1867 struct lu_device *l;
1872 return ERR_PTR(-ENOMEM);
1874 l = &m->ofd_dt_dev.dd_lu_dev;
1875 dt_device_init(&m->ofd_dt_dev, t);
1876 rc = ofd_init0(env, m, t, cfg);
1878 ofd_device_free(env, l);
1885 /* thread context key constructor/destructor */
1886 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
1888 static void ofd_key_exit(const struct lu_context *ctx,
1889 struct lu_context_key *key, void *data)
1891 struct ofd_thread_info *info = data;
1893 info->fti_env = NULL;
1894 info->fti_exp = NULL;
1897 info->fti_pre_version = 0;
1900 memset(&info->fti_attr, 0, sizeof info->fti_attr);
1903 struct lu_context_key ofd_thread_key = {
1904 .lct_tags = LCT_DT_THREAD,
1905 .lct_init = ofd_key_init,
1906 .lct_fini = ofd_key_fini,
1907 .lct_exit = ofd_key_exit
1910 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
1911 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
1913 static struct lu_device_type_operations ofd_device_type_ops = {
1914 .ldto_init = ofd_type_init,
1915 .ldto_fini = ofd_type_fini,
1917 .ldto_start = ofd_type_start,
1918 .ldto_stop = ofd_type_stop,
1920 .ldto_device_alloc = ofd_device_alloc,
1921 .ldto_device_free = ofd_device_free,
1922 .ldto_device_fini = ofd_device_fini
1925 static struct lu_device_type ofd_device_type = {
1926 .ldt_tags = LU_DEVICE_DT,
1927 .ldt_name = LUSTRE_OST_NAME,
1928 .ldt_ops = &ofd_device_type_ops,
1929 .ldt_ctx_tags = LCT_DT_THREAD
1932 int __init ofd_init(void)
1934 struct lprocfs_static_vars lvars;
1937 rc = lu_kmem_init(ofd_caches);
1941 rc = ofd_fmd_init();
1943 lu_kmem_fini(ofd_caches);
1947 lprocfs_ofd_init_vars(&lvars);
1949 rc = class_register_type(&ofd_obd_ops, NULL, NULL,
1950 #ifndef HAVE_ONLY_PROCFS_SEQ
1953 LUSTRE_OST_NAME, &ofd_device_type);
1957 void __exit ofd_exit(void)
1960 lu_kmem_fini(ofd_caches);
1961 class_unregister_type(LUSTRE_OST_NAME);
1964 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
1965 MODULE_DESCRIPTION("Lustre Object Filtering Device");
1966 MODULE_LICENSE("GPL");
1968 module_init(ofd_init);
1969 module_exit(ofd_exit);