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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014 Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ofd/ofd_dev.c
34 * This file contains OSD API methods for OBD Filter Device (OFD),
35 * request handlers and supplemental functions to set OFD up and clean it up.
37 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
38 * Author: Mike Pershin <mike.pershin@intel.com>
39 * Author: Johann Lombardi <johann.lombardi@intel.com>
42 * The OBD Filter Device (OFD) module belongs to the Object Storage
43 * Server stack and connects the RPC oriented Unified Target (TGT)
44 * layer (see lustre/include/lu_target.h) to the storage oriented OSD
45 * layer (see lustre/doc/osd-api.txt).
53 * OFD implements the LU and OBD device APIs and is responsible for:
55 * - Handling client requests (create, destroy, bulk IO, setattr,
56 * get_info, set_info, statfs) for the objects belonging to the OST
57 * (together with TGT).
59 * - Providing grant space management which allows clients to reserve
60 * disk space for data writeback. OFD tracks grants on global and
63 * - Handling object precreation requests from MDTs.
65 * - Operating the LDLM service that allows clients to maintain object
66 * data cache coherence.
69 #define DEBUG_SUBSYSTEM S_FILTER
71 #include <obd_class.h>
72 #include <lustre_param.h>
73 #include <lustre_fid.h>
74 #include <lustre_lfsck.h>
75 #include <lustre/lustre_idl.h>
76 #include <lustre_dlm.h>
77 #include <lustre_quota.h>
79 #include "ofd_internal.h"
81 /* Slab for OFD object allocation */
82 static struct kmem_cache *ofd_object_kmem;
84 static struct lu_kmem_descr ofd_caches[] = {
86 .ckd_cache = &ofd_object_kmem,
87 .ckd_name = "ofd_obj",
88 .ckd_size = sizeof(struct ofd_object)
96 * Connect OFD to the next device in the stack.
98 * This function is used for device stack configuration and links OFD
99 * device with bottom OSD device.
101 * \param[in] env execution environment
102 * \param[in] m OFD device
103 * \param[in] next name of next device in the stack
104 * \param[out] exp export to return
106 * \retval 0 and export in \a exp if successful
107 * \retval negative value on error
109 static int ofd_connect_to_next(const struct lu_env *env, struct ofd_device *m,
110 const char *next, struct obd_export **exp)
112 struct obd_connect_data *data = NULL;
113 struct obd_device *obd;
119 GOTO(out, rc = -ENOMEM);
121 obd = class_name2obd(next);
123 CERROR("%s: can't locate next device: %s\n",
125 GOTO(out, rc = -ENOTCONN);
128 data->ocd_connect_flags = OBD_CONNECT_VERSION;
129 data->ocd_version = LUSTRE_VERSION_CODE;
131 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
133 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
134 ofd_name(m), next, rc);
138 m->ofd_dt_dev.dd_lu_dev.ld_site =
139 m->ofd_osd_exp->exp_obd->obd_lu_dev->ld_site;
140 LASSERT(m->ofd_dt_dev.dd_lu_dev.ld_site);
141 m->ofd_osd = lu2dt_dev(m->ofd_osd_exp->exp_obd->obd_lu_dev);
142 m->ofd_dt_dev.dd_lu_dev.ld_site->ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
151 * Initialize stack of devices.
153 * This function initializes OFD-OSD device stack to serve OST requests
155 * \param[in] env execution environment
156 * \param[in] m OFD device
157 * \param[in] cfg Lustre config for this server
159 * \retval 0 if successful
160 * \retval negative value on error
162 static int ofd_stack_init(const struct lu_env *env,
163 struct ofd_device *m, struct lustre_cfg *cfg)
165 const char *dev = lustre_cfg_string(cfg, 0);
167 struct ofd_thread_info *info = ofd_info(env);
168 struct lustre_mount_info *lmi;
174 lmi = server_get_mount(dev);
176 CERROR("Cannot get mount info for %s!\n", dev);
180 /* find bottom osd */
181 OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
185 snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
186 rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
187 OBD_FREE(osdname, MTI_NAME_MAXLEN);
191 d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
193 m->ofd_osd = lu2dt_dev(d);
195 snprintf(info->fti_u.name, sizeof(info->fti_u.name),
196 "%s-osd", lustre_cfg_string(cfg, 0));
202 * Finalize the device stack OFD-OSD.
204 * This function cleans OFD-OSD device stack and
205 * disconnects OFD from the OSD.
207 * \param[in] env execution environment
208 * \param[in] m OFD device
209 * \param[in] top top device of stack
211 * \retval 0 if successful
212 * \retval negative value on error
214 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
215 struct lu_device *top)
217 struct obd_device *obd = ofd_obd(m);
218 struct lustre_cfg_bufs bufs;
219 struct lustre_cfg *lcfg;
224 lu_site_purge(env, top->ld_site, ~0);
225 /* process cleanup, pass mdt obd name to get obd umount flags */
226 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
231 lustre_cfg_bufs_set_string(&bufs, 1, flags);
232 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
234 CERROR("Cannot alloc lcfg!\n");
239 top->ld_ops->ldo_process_config(env, top, lcfg);
240 lustre_cfg_free(lcfg);
242 lu_site_purge(env, top->ld_site, ~0);
243 if (!cfs_hash_is_empty(top->ld_site->ls_obj_hash)) {
244 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
245 lu_site_print(env, top->ld_site, &msgdata, lu_cdebug_printer);
248 LASSERT(m->ofd_osd_exp);
249 obd_disconnect(m->ofd_osd_exp);
254 /* For interoperability, see mdt_interop_param[]. */
255 static struct cfg_interop_param ofd_interop_param[] = {
256 { "ost.quota_type", NULL },
261 * Check if parameters are symlinks to the OSD.
263 * Some parameters were moved from ofd to osd and only their
264 * symlinks were kept in ofd by LU-3106. They are:
265 * -writehthrough_cache_enable
266 * -readcache_max_filesize
270 * Since they are not included by the static lprocfs var list, a pre-check
271 * is added for them to avoid "unknown param" errors. If they are matched
272 * in this check, they will be passed to the OSD directly.
274 * \param[in] param parameters to check
276 * \retval true if param is symlink to OSD param
279 static bool match_symlink_param(char *param)
284 if (class_match_param(param, PARAM_OST, ¶m) == 0) {
285 sval = strchr(param, '=');
287 paramlen = sval - param;
288 if (strncmp(param, "writethrough_cache_enable",
290 strncmp(param, "readcache_max_filesize",
292 strncmp(param, "read_cache_enable",
294 strncmp(param, "brw_stats", paramlen) == 0)
303 * Process various configuration parameters.
305 * This function is used by MGS to process specific configurations and
306 * pass them through to the next device in server stack, i.e. the OSD.
308 * \param[in] env execution environment
309 * \param[in] d LU device of OFD
310 * \param[in] cfg parameters to process
312 * \retval 0 if successful
313 * \retval negative value on error
315 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
316 struct lustre_cfg *cfg)
318 struct ofd_device *m = ofd_dev(d);
319 struct dt_device *dt_next = m->ofd_osd;
320 struct lu_device *next = &dt_next->dd_lu_dev;
325 switch (cfg->lcfg_command) {
327 struct obd_device *obd = ofd_obd(m);
328 /* For interoperability */
329 struct cfg_interop_param *ptr = NULL;
330 struct lustre_cfg *old_cfg = NULL;
333 param = lustre_cfg_string(cfg, 1);
335 CERROR("param is empty\n");
340 ptr = class_find_old_param(param, ofd_interop_param);
342 if (ptr->new_param == NULL) {
344 CWARN("For interoperability, skip this %s."
345 " It is obsolete.\n", ptr->old_param);
349 CWARN("Found old param %s, changed it to %s.\n",
350 ptr->old_param, ptr->new_param);
353 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
360 if (match_symlink_param(param)) {
361 rc = next->ld_ops->ldo_process_config(env, next, cfg);
365 rc = class_process_proc_param(PARAM_OST, obd->obd_vars, cfg,
367 if (rc > 0 || rc == -ENOSYS) {
368 CDEBUG(D_CONFIG, "pass param %s down the stack.\n",
370 /* we don't understand; pass it on */
371 rc = next->ld_ops->ldo_process_config(env, next, cfg);
375 case LCFG_SPTLRPC_CONF: {
380 /* others are passed further */
381 rc = next->ld_ops->ldo_process_config(env, next, cfg);
388 * Implementation of lu_object_operations::loo_object_init for OFD
390 * Allocate just the next object (OSD) in stack.
392 * \param[in] env execution environment
393 * \param[in] o lu_object of OFD object
394 * \param[in] conf additional configuration parameters, not used here
396 * \retval 0 if successful
397 * \retval negative value on error
399 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
400 const struct lu_object_conf *conf)
402 struct ofd_device *d = ofd_dev(o->lo_dev);
403 struct lu_device *under;
404 struct lu_object *below;
409 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
410 PFID(lu_object_fid(o)));
412 under = &d->ofd_osd->dd_lu_dev;
413 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
415 lu_object_add(o, below);
423 * Implementation of lu_object_operations::loo_object_free.
425 * Finish OFD object lifecycle and free its memory.
427 * \param[in] env execution environment
428 * \param[in] o LU object of OFD object
430 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
432 struct ofd_object *of = ofd_obj(o);
433 struct lu_object_header *h;
438 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
439 PFID(lu_object_fid(o)));
442 lu_object_header_fini(h);
443 OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
448 * Implementation of lu_object_operations::loo_object_print.
450 * Print OFD part of compound OFD-OSD object. See lu_object_print() and
451 * LU_OBJECT_DEBUG() for more details about the compound object printing.
453 * \param[in] env execution environment
454 * \param[in] cookie opaque data passed to the printer function
455 * \param[in] p printer function to use
456 * \param[in] o LU object of OFD object
458 * \retval 0 if successful
459 * \retval negative value on error
461 static int ofd_object_print(const struct lu_env *env, void *cookie,
462 lu_printer_t p, const struct lu_object *o)
464 return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
467 struct lu_object_operations ofd_obj_ops = {
468 .loo_object_init = ofd_object_init,
469 .loo_object_free = ofd_object_free,
470 .loo_object_print = ofd_object_print
474 * Implementation of lu_device_operations::lod_object_alloc.
476 * This function allocates OFD part of compound OFD-OSD object and
477 * initializes its header, because OFD is the top device in stack
479 * \param[in] env execution environment
480 * \param[in] hdr object header, NULL for OFD
481 * \param[in] d lu_device
483 * \retval allocated object if successful
484 * \retval NULL value on failed allocation
486 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
487 const struct lu_object_header *hdr,
490 struct ofd_object *of;
494 OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, GFP_NOFS);
497 struct lu_object_header *h;
499 o = &of->ofo_obj.do_lu;
501 lu_object_header_init(h);
502 lu_object_init(o, h, d);
503 lu_object_add_top(h, o);
504 o->lo_ops = &ofd_obj_ops;
512 * Return the result of LFSCK run to the OFD.
514 * Notify OFD about result of LFSCK run. That may block the new object
515 * creation until problem is fixed by LFSCK.
517 * \param[in] env execution environment
518 * \param[in] data pointer to the OFD device
519 * \param[in] event LFSCK event type
521 * \retval 0 if successful
522 * \retval negative value on unknown event
524 static int ofd_lfsck_out_notify(const struct lu_env *env, void *data,
525 enum lfsck_events event)
527 struct ofd_device *ofd = data;
528 struct obd_device *obd = ofd_obd(ofd);
531 case LE_LASTID_REBUILDING:
532 CWARN("%s: Found crashed LAST_ID, deny creating new OST-object "
533 "on the device until the LAST_ID rebuilt successfully.\n",
535 down_write(&ofd->ofd_lastid_rwsem);
536 ofd->ofd_lastid_rebuilding = 1;
537 up_write(&ofd->ofd_lastid_rwsem);
539 case LE_LASTID_REBUILT: {
540 down_write(&ofd->ofd_lastid_rwsem);
541 ofd_seqs_free(env, ofd);
542 ofd->ofd_lastid_rebuilding = 0;
543 ofd->ofd_lastid_gen++;
544 up_write(&ofd->ofd_lastid_rwsem);
545 CWARN("%s: Rebuilt crashed LAST_ID files successfully.\n",
550 CERROR("%s: unknown lfsck event: rc = %d\n",
551 ofd_name(ofd), event);
559 * Implementation of lu_device_operations::ldo_prepare.
561 * This method is called after layer has been initialized and before it starts
562 * serving user requests. In OFD it starts lfsk check routines and initializes
565 * \param[in] env execution environment
566 * \param[in] pdev higher device in stack, NULL for OFD
567 * \param[in] dev lu_device of OFD device
569 * \retval 0 if successful
570 * \retval negative value on error
572 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
573 struct lu_device *dev)
575 struct ofd_thread_info *info;
576 struct ofd_device *ofd = ofd_dev(dev);
577 struct obd_device *obd = ofd_obd(ofd);
578 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
583 info = ofd_info_init(env, NULL);
587 /* initialize lower device */
588 rc = next->ld_ops->ldo_prepare(env, dev, next);
592 rc = lfsck_register(env, ofd->ofd_osd, ofd->ofd_osd, obd,
593 ofd_lfsck_out_notify, ofd, false);
595 CERROR("%s: failed to initialize lfsck: rc = %d\n",
600 rc = lfsck_register_namespace(env, ofd->ofd_osd, ofd->ofd_namespace);
601 /* The LFSCK instance is registered just now, so it must be there when
602 * register the namespace to such instance. */
603 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
605 target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
606 LASSERT(obd->obd_no_conn);
607 spin_lock(&obd->obd_dev_lock);
608 obd->obd_no_conn = 0;
609 spin_unlock(&obd->obd_dev_lock);
611 if (obd->obd_recovering == 0)
612 ofd_postrecov(env, ofd);
618 * Implementation of lu_device_operations::ldo_recovery_complete.
620 * This method notifies all layers about 'recovery complete' event. That means
621 * device is in full state and consistent. An OFD calculates available grant
622 * space upon this event.
624 * \param[in] env execution environment
625 * \param[in] dev lu_device of OFD device
627 * \retval 0 if successful
628 * \retval negative value on error
630 static int ofd_recovery_complete(const struct lu_env *env,
631 struct lu_device *dev)
633 struct ofd_device *ofd = ofd_dev(dev);
634 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
635 int rc = 0, max_precreate;
640 * Grant space for object precreation on the self export.
641 * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
642 * is enough to create 10k objects. More space is then acquired for
643 * precreation in ofd_grant_create().
645 max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
646 ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
648 rc = next->ld_ops->ldo_recovery_complete(env, next);
653 * lu_device_operations matrix for OFD device.
655 static struct lu_device_operations ofd_lu_ops = {
656 .ldo_object_alloc = ofd_object_alloc,
657 .ldo_process_config = ofd_process_config,
658 .ldo_recovery_complete = ofd_recovery_complete,
659 .ldo_prepare = ofd_prepare,
662 LPROC_SEQ_FOPS(lprocfs_nid_stats_clear);
665 * Initialize all needed procfs entries for OFD device.
667 * \param[in] ofd OFD device
669 * \retval 0 if successful
670 * \retval negative value on error
672 static int ofd_procfs_init(struct ofd_device *ofd)
674 struct obd_device *obd = ofd_obd(ofd);
675 struct proc_dir_entry *entry;
680 /* lprocfs must be setup before the ofd so state can be safely added
681 * to /proc incrementally as the ofd is setup */
682 obd->obd_vars = lprocfs_ofd_obd_vars;
683 rc = lprocfs_obd_setup(obd);
685 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
690 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
692 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
694 GOTO(obd_cleanup, rc);
697 obd->obd_uses_nid_stats = 1;
699 entry = lprocfs_seq_register("exports", obd->obd_proc_entry, NULL,
703 CERROR("%s: error %d setting up lprocfs for %s\n",
704 obd->obd_name, rc, "exports");
705 GOTO(obd_cleanup, rc);
707 obd->obd_proc_exports_entry = entry;
709 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
710 obd, &lprocfs_nid_stats_clear_fops);
713 CERROR("%s: add proc entry 'clear' failed: %d.\n",
715 GOTO(obd_cleanup, rc);
718 ofd_stats_counter_init(obd->obd_stats);
720 rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
721 ofd_stats_counter_init);
723 GOTO(obd_cleanup, rc);
726 lprocfs_obd_cleanup(obd);
727 lprocfs_free_obd_stats(obd);
733 * Expose OSD statistics to OFD layer.
735 * The osd interfaces to the backend file system exposes useful data
736 * such as brw_stats and read or write cache states. This same data
737 * needs to be exposed into the obdfilter (ofd) layer to maintain
738 * backwards compatibility. This function creates the symlinks in the
739 * proc layer to enable this.
741 * \param[in] ofd OFD device
743 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
745 struct obd_device *obd = ofd_obd(ofd);
746 struct obd_device *osd_obd = ofd->ofd_osd_exp->exp_obd;
748 if (obd->obd_proc_entry == NULL)
751 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
752 "../../%s/%s/brw_stats",
753 osd_obd->obd_type->typ_name, obd->obd_name);
755 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
756 "../../%s/%s/read_cache_enable",
757 osd_obd->obd_type->typ_name, obd->obd_name);
759 lprocfs_add_symlink("readcache_max_filesize",
761 "../../%s/%s/readcache_max_filesize",
762 osd_obd->obd_type->typ_name, obd->obd_name);
764 lprocfs_add_symlink("writethrough_cache_enable",
766 "../../%s/%s/writethrough_cache_enable",
767 osd_obd->obd_type->typ_name, obd->obd_name);
771 * Cleanup all procfs entries in OFD.
773 * \param[in] ofd OFD device
775 static void ofd_procfs_fini(struct ofd_device *ofd)
777 struct obd_device *obd = ofd_obd(ofd);
779 lprocfs_free_per_client_stats(obd);
780 lprocfs_obd_cleanup(obd);
781 lprocfs_free_obd_stats(obd);
782 lprocfs_job_stats_fini(obd);
786 * Stop SEQ/FID server on OFD.
788 * \param[in] env execution environment
789 * \param[in] ofd OFD device
791 * \retval 0 if successful
792 * \retval negative value on error
794 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
796 return seq_site_fini(env, &ofd->ofd_seq_site);
800 * Start SEQ/FID server on OFD.
802 * The SEQ/FID server on OFD is needed to allocate FIDs for new objects.
803 * It also connects to the master server to get own FID sequence (SEQ) range
804 * to this particular OFD. Typically that happens when the OST is first
805 * formatted or in the rare case that it exhausts the local sequence range.
807 * The sequence range is allocated out to the MDTs for OST object allocations,
808 * and not directly to the clients.
810 * \param[in] env execution environment
811 * \param[in] ofd OFD device
813 * \retval 0 if successful
814 * \retval negative value on error
816 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
818 struct seq_server_site *ss = &ofd->ofd_seq_site;
819 struct lu_device *lu = &ofd->ofd_dt_dev.dd_lu_dev;
820 char *obd_name = ofd_name(ofd);
824 ss = &ofd->ofd_seq_site;
825 lu->ld_site->ld_seq_site = ss;
826 ss->ss_lu = lu->ld_site;
827 ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
829 OBD_ALLOC_PTR(ss->ss_server_seq);
830 if (ss->ss_server_seq == NULL)
831 GOTO(out_free, rc = -ENOMEM);
833 OBD_ALLOC(name, strlen(obd_name) + 10);
835 OBD_FREE_PTR(ss->ss_server_seq);
836 ss->ss_server_seq = NULL;
837 GOTO(out_free, rc = -ENOMEM);
840 rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
841 LUSTRE_SEQ_SERVER, ss);
843 CERROR("%s : seq server init error %d\n", obd_name, rc);
846 ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
848 OBD_ALLOC_PTR(ss->ss_client_seq);
849 if (ss->ss_client_seq == NULL)
850 GOTO(out_free, rc = -ENOMEM);
852 snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
853 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
856 CERROR("%s : seq client init error %d\n", obd_name, rc);
859 OBD_FREE(name, strlen(obd_name) + 10);
862 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
866 if (ss->ss_server_seq) {
867 seq_server_fini(ss->ss_server_seq, env);
868 OBD_FREE_PTR(ss->ss_server_seq);
869 ss->ss_server_seq = NULL;
872 if (ss->ss_client_seq) {
873 seq_client_fini(ss->ss_client_seq);
874 OBD_FREE_PTR(ss->ss_client_seq);
875 ss->ss_client_seq = NULL;
879 OBD_FREE(name, strlen(obd_name) + 10);
888 * OFD request handler for OST_SET_INFO RPC.
890 * This is OFD-specific part of request handling
892 * \param[in] tsi target session environment for this request
894 * \retval 0 if successful
895 * \retval negative value on error
897 int ofd_set_info_hdl(struct tgt_session_info *tsi)
899 struct ptlrpc_request *req = tgt_ses_req(tsi);
900 struct ost_body *body = NULL, *repbody;
901 void *key, *val = NULL;
902 int keylen, vallen, rc = 0;
903 bool is_grant_shrink;
904 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
908 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
910 DEBUG_REQ(D_HA, req, "no set_info key");
911 RETURN(err_serious(-EFAULT));
913 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
916 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
918 DEBUG_REQ(D_HA, req, "no set_info val");
919 RETURN(err_serious(-EFAULT));
921 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
924 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
926 /* In this case the value is actually an RMF_OST_BODY, so we
927 * transmutate the type of this PTLRPC */
928 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
930 rc = req_capsule_server_pack(tsi->tsi_pill);
934 if (is_grant_shrink) {
935 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
937 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
940 /** handle grant shrink, similar to a read request */
941 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
943 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
945 obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
947 } else if (KEY_IS(KEY_CAPA_KEY)) {
948 rc = ofd_update_capa_key(ofd, val);
949 } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
950 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
952 CERROR("%s: Unsupported key %s\n",
953 tgt_name(tsi->tsi_tgt), (char *)key);
956 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
963 * Get FIEMAP (FIle Extent MAPping) for object with the given FID.
965 * This function returns a list of extents which describes how a file's
966 * blocks are laid out on the disk.
968 * \param[in] env execution environment
969 * \param[in] ofd OFD device
970 * \param[in] fid FID of object
971 * \param[in] fiemap fiemap structure to fill with data
973 * \retval 0 if \a fiemap is filled with data successfully
974 * \retval negative value on error
976 int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
977 struct lu_fid *fid, struct ll_user_fiemap *fiemap)
979 struct ofd_object *fo;
982 fo = ofd_object_find(env, ofd, fid);
984 CERROR("%s: error finding object "DFID"\n",
985 ofd_name(ofd), PFID(fid));
989 ofd_read_lock(env, fo);
990 if (ofd_object_exists(fo))
991 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
994 ofd_read_unlock(env, fo);
995 ofd_object_put(env, fo);
999 struct locked_region {
1000 struct list_head list;
1001 struct lustre_handle lh;
1005 * Lock single extent and save lock handle in the list.
1007 * This is supplemental function for lock_zero_regions(). It allocates
1008 * new locked_region structure and locks it with extent lock, then adds
1009 * it to the list of all such regions.
1011 * \param[in] ns LDLM namespace
1012 * \param[in] res_id resource ID
1013 * \param[in] begin start of region
1014 * \param[in] end end of region
1015 * \param[in] locked list head of regions list
1017 * \retval 0 if successful locking
1018 * \retval negative value on error
1020 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
1021 unsigned long long begin, unsigned long long end,
1022 struct list_head *locked)
1024 struct locked_region *region = NULL;
1028 LASSERT(begin <= end);
1029 OBD_ALLOC_PTR(region);
1033 rc = tgt_extent_lock(ns, res_id, begin, end, ®ion->lh,
1038 CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
1040 list_add(®ion->list, locked);
1046 * Lock the sparse areas of given resource.
1048 * The locking of sparse areas will cause dirty data to be flushed back from
1049 * clients. This is used when getting the FIEMAP of an object to make sure
1050 * there is no unaccounted cached data on clients.
1052 * This function goes through \a fiemap list of extents and locks only sparse
1053 * areas between extents.
1055 * \param[in] ns LDLM namespace
1056 * \param[in] res_id resource ID
1057 * \param[in] fiemap file extents mapping on disk
1058 * \param[in] locked list head of regions list
1060 * \retval 0 if successful
1061 * \retval negative value on error
1063 static int lock_zero_regions(struct ldlm_namespace *ns,
1064 struct ldlm_res_id *res_id,
1065 struct ll_user_fiemap *fiemap,
1066 struct list_head *locked)
1068 __u64 begin = fiemap->fm_start;
1071 struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
1075 CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
1076 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
1077 if (fiemap_start[i].fe_logical > begin) {
1078 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
1079 begin, fiemap_start[i].fe_logical);
1080 rc = lock_region(ns, res_id, begin,
1081 fiemap_start[i].fe_logical, locked);
1086 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
1089 if (begin < (fiemap->fm_start + fiemap->fm_length)) {
1090 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
1091 begin, fiemap->fm_start + fiemap->fm_length);
1092 rc = lock_region(ns, res_id, begin,
1093 fiemap->fm_start + fiemap->fm_length, locked);
1100 * Unlock all previously locked sparse areas for given resource.
1102 * This function goes through list of locked regions, unlocking and freeing
1105 * \param[in] ns LDLM namespace
1106 * \param[in] locked list head of regions list
1109 unlock_zero_regions(struct ldlm_namespace *ns, struct list_head *locked)
1111 struct locked_region *entry, *temp;
1113 list_for_each_entry_safe(entry, temp, locked, list) {
1114 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
1115 tgt_extent_unlock(&entry->lh, LCK_PR);
1116 list_del(&entry->list);
1117 OBD_FREE_PTR(entry);
1122 * OFD request handler for OST_GET_INFO RPC.
1124 * This is OFD-specific part of request handling. The OFD-specific keys are:
1125 * - KEY_LAST_ID (obsolete)
1129 * This function reads needed data from storage and fills reply with it.
1131 * Note: the KEY_LAST_ID is obsolete, replaced by KEY_LAST_FID on newer MDTs,
1132 * and is kept for compatibility.
1134 * \param[in] tsi target session environment for this request
1136 * \retval 0 if successful
1137 * \retval negative value on error
1139 int ofd_get_info_hdl(struct tgt_session_info *tsi)
1141 struct obd_export *exp = tsi->tsi_exp;
1142 struct ofd_device *ofd = ofd_exp(exp);
1143 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1146 int replylen, rc = 0;
1150 /* this common part for get_info rpc */
1151 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
1153 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
1154 RETURN(err_serious(-EPROTO));
1156 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
1159 if (KEY_IS(KEY_LAST_ID)) {
1161 struct ofd_seq *oseq;
1163 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
1164 rc = req_capsule_server_pack(tsi->tsi_pill);
1166 RETURN(err_serious(rc));
1168 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
1170 oseq = ofd_seq_load(tsi->tsi_env, ofd,
1171 (obd_seq)exp->exp_filter_data.fed_group);
1175 *last_id = ofd_seq_last_oid(oseq);
1176 ofd_seq_put(tsi->tsi_env, oseq);
1177 } else if (KEY_IS(KEY_FIEMAP)) {
1178 struct ll_fiemap_info_key *fm_key;
1179 struct ll_user_fiemap *fiemap;
1182 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
1184 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
1185 rc = tgt_validate_obdo(tsi, &fm_key->oa);
1187 RETURN(err_serious(rc));
1189 fid = &fm_key->oa.o_oi.oi_fid;
1191 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
1193 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
1194 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
1195 RCL_SERVER, replylen);
1197 rc = req_capsule_server_pack(tsi->tsi_pill);
1199 RETURN(err_serious(rc));
1201 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
1205 *fiemap = fm_key->fiemap;
1206 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
1208 /* LU-3219: Lock the sparse areas to make sure dirty
1209 * flushed back from client, then call fiemap again. */
1210 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
1211 fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
1212 struct list_head locked;
1214 INIT_LIST_HEAD(&locked);
1215 ost_fid_build_resid(fid, &fti->fti_resid);
1216 rc = lock_zero_regions(ofd->ofd_namespace,
1217 &fti->fti_resid, fiemap,
1219 if (rc == 0 && !list_empty(&locked)) {
1220 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
1222 unlock_zero_regions(ofd->ofd_namespace,
1226 } else if (KEY_IS(KEY_LAST_FID)) {
1227 struct ofd_device *ofd = ofd_exp(exp);
1228 struct ofd_seq *oseq;
1232 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
1233 rc = req_capsule_server_pack(tsi->tsi_pill);
1235 RETURN(err_serious(rc));
1237 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
1239 RETURN(err_serious(-EPROTO));
1241 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
1243 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
1247 oseq = ofd_seq_load(tsi->tsi_env, ofd,
1248 ostid_seq(&fti->fti_ostid));
1250 RETURN(PTR_ERR(oseq));
1252 rc = ostid_to_fid(fid, &oseq->os_oi,
1253 ofd->ofd_lut.lut_lsd.lsd_osd_index);
1257 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
1260 ofd_seq_put(tsi->tsi_env, oseq);
1262 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
1266 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
1273 * OFD request handler for OST_GETATTR RPC.
1275 * This is OFD-specific part of request handling. It finds the OFD object
1276 * by its FID, gets attributes from storage and packs result to the reply.
1278 * \param[in] tsi target session environment for this request
1280 * \retval 0 if successful
1281 * \retval negative value on error
1283 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
1285 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1286 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1287 struct ost_body *repbody;
1288 struct lustre_handle lh = { 0 };
1289 struct ofd_object *fo;
1291 ldlm_mode_t lock_mode = LCK_PR;
1296 LASSERT(tsi->tsi_ost_body != NULL);
1298 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1299 if (repbody == NULL)
1302 repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
1303 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1305 srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
1306 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
1309 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
1312 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
1313 &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
1319 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1321 GOTO(out, rc = PTR_ERR(fo));
1323 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1327 obdo_from_la(&repbody->oa, &fti->fti_attr,
1328 OFD_VALID_FLAGS | LA_UID | LA_GID);
1329 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1331 /* Store object version in reply */
1332 curr_version = dt_version_get(tsi->tsi_env,
1333 ofd_object_child(fo));
1334 if ((__s64)curr_version != -EOPNOTSUPP) {
1335 repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
1336 repbody->oa.o_data_version = curr_version;
1340 ofd_object_put(tsi->tsi_env, fo);
1343 tgt_extent_unlock(&lh, lock_mode);
1345 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
1348 repbody->oa.o_valid |= OBD_MD_FLFLAGS;
1349 repbody->oa.o_flags = OBD_FL_FLUSH;
1355 * OFD request handler for OST_SETATTR RPC.
1357 * This is OFD-specific part of request handling. It finds the OFD object
1358 * by its FID, sets attributes from request and packs result to the reply.
1360 * \param[in] tsi target session environment for this request
1362 * \retval 0 if successful
1363 * \retval negative value on error
1365 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
1367 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1368 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1369 struct ost_body *body = tsi->tsi_ost_body;
1370 struct ost_body *repbody;
1371 struct ldlm_resource *res;
1372 struct ofd_object *fo;
1373 struct filter_fid *ff = NULL;
1378 LASSERT(body != NULL);
1380 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1381 if (repbody == NULL)
1384 repbody->oa.o_oi = body->oa.o_oi;
1385 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1387 /* This would be very bad - accidentally truncating a file when
1388 * changing the time or similar - bug 12203. */
1389 if (body->oa.o_valid & OBD_MD_FLSIZE &&
1390 body->oa.o_size != OBD_OBJECT_EOF) {
1391 static char mdsinum[48];
1393 if (body->oa.o_valid & OBD_MD_FLFID)
1394 snprintf(mdsinum, sizeof(mdsinum) - 1,
1395 "of parent "DFID, body->oa.o_parent_seq,
1396 body->oa.o_parent_oid, 0);
1400 CERROR("%s: setattr from %s is trying to truncate object "DFID
1401 " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1402 PFID(&tsi->tsi_fid), mdsinum);
1406 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1408 GOTO(out, rc = PTR_ERR(fo));
1410 la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1411 fti->fti_attr.la_valid &= ~LA_TYPE;
1413 if (body->oa.o_valid & OBD_MD_FLFID) {
1414 ff = &fti->fti_mds_fid;
1415 ofd_prepare_fidea(ff, &body->oa);
1418 /* setting objects attributes (including owner/group) */
1419 rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1423 obdo_from_la(&repbody->oa, &fti->fti_attr,
1424 OFD_VALID_FLAGS | LA_UID | LA_GID);
1425 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1427 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1431 ofd_object_put(tsi->tsi_env, fo);
1434 /* we do not call this before to avoid lu_object_find() in
1435 * ->lvbo_update() holding another reference on the object.
1436 * otherwise concurrent destroy can make the object unavailable
1437 * for 2nd lu_object_find() waiting for the first reference
1438 * to go... deadlock! */
1439 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1440 &tsi->tsi_resid, LDLM_EXTENT, 0);
1442 ldlm_res_lvbo_update(res, NULL, 0);
1443 ldlm_resource_putref(res);
1450 * Destroy OST orphans.
1452 * This is part of OST_CREATE RPC handling. If there is flag OBD_FL_DELORPHAN
1453 * set then we must destroy possible orphaned objects.
1455 * \param[in] env execution environment
1456 * \param[in] exp OBD export
1457 * \param[in] ofd OFD device
1458 * \param[in] oa obdo structure for reply
1460 * \retval 0 if successful
1461 * \retval negative value on error
1463 static int ofd_orphans_destroy(const struct lu_env *env,
1464 struct obd_export *exp,
1465 struct ofd_device *ofd, struct obdo *oa)
1467 struct ofd_thread_info *info = ofd_info(env);
1468 struct lu_fid *fid = &info->fti_fid;
1469 struct ost_id *oi = &oa->o_oi;
1470 struct ofd_seq *oseq;
1471 obd_seq seq = ostid_seq(oi);
1472 obd_id end_id = ostid_id(oi);
1480 oseq = ofd_seq_get(ofd, seq);
1482 CERROR("%s: Can not find seq for "DOSTID"\n",
1483 ofd_name(ofd), POSTID(oi));
1488 last = ofd_seq_last_oid(oseq);
1491 LASSERT(exp != NULL);
1492 skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1494 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NODESTROY))
1497 LCONSOLE(D_INFO, "%s: deleting orphan objects from "DOSTID
1498 " to "DOSTID"\n", ofd_name(ofd), seq, end_id + 1, seq, last);
1500 while (oid > end_id) {
1501 rc = fid_set_id(fid, oid);
1502 if (unlikely(rc != 0))
1505 rc = ofd_destroy_by_fid(env, ofd, fid, 1);
1506 if (rc != 0 && rc != -ENOENT && rc != -ESTALE &&
1507 likely(rc != -EREMCHG && rc != -EINPROGRESS))
1508 /* this is pretty fatal... */
1509 CEMERG("%s: error destroying precreated id "
1511 ofd_name(ofd), PFID(fid), rc);
1515 ofd_seq_last_oid_set(oseq, oid);
1516 /* update last_id on disk periodically so that if we
1517 * restart * we don't need to re-scan all of the just
1518 * deleted objects. */
1519 if ((oid & 511) == 0)
1520 ofd_seq_last_oid_write(env, ofd, oseq);
1524 CDEBUG(D_HA, "%s: after destroy: set last_id to "DOSTID"\n",
1525 ofd_name(ofd), seq, oid);
1529 ofd_seq_last_oid_set(oseq, oid);
1530 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1532 /* don't reuse orphan object, return last used objid */
1533 ostid_set_id(oi, last);
1540 ofd_seq_put(env, oseq);
1545 * OFD request handler for OST_CREATE RPC.
1547 * This is OFD-specific part of request handling. Its main purpose is to
1548 * create new data objects on OST, but it also used to destroy orphans.
1550 * \param[in] tsi target session environment for this request
1552 * \retval 0 if successful
1553 * \retval negative value on error
1555 static int ofd_create_hdl(struct tgt_session_info *tsi)
1557 struct ptlrpc_request *req = tgt_ses_req(tsi);
1558 struct ost_body *repbody;
1559 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1560 struct obdo *rep_oa;
1561 struct obd_export *exp = tsi->tsi_exp;
1562 struct ofd_device *ofd = ofd_exp(exp);
1563 obd_seq seq = ostid_seq(&oa->o_oi);
1564 obd_id oid = ostid_id(&oa->o_oi);
1565 struct ofd_seq *oseq;
1571 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1574 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1575 if (repbody == NULL)
1578 down_read(&ofd->ofd_lastid_rwsem);
1579 /* Currently, for safe, we do not distinguish which LAST_ID is broken,
1580 * we may do that in the future.
1581 * Return -ENOSPC until the LAST_ID rebuilt. */
1582 if (unlikely(ofd->ofd_lastid_rebuilding))
1583 GOTO(out_sem, rc = -ENOSPC);
1585 rep_oa = &repbody->oa;
1586 rep_oa->o_oi = oa->o_oi;
1588 LASSERT(seq >= FID_SEQ_OST_MDT0);
1589 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1591 CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1593 oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1595 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1596 ofd_name(ofd), seq, PTR_ERR(oseq));
1597 GOTO(out_sem, rc = -EINVAL);
1600 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1601 (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1602 if (!ofd_obd(ofd)->obd_recovering ||
1603 oid > ofd_seq_last_oid(oseq)) {
1604 CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1605 "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1606 ofd_seq_last_oid(oseq));
1607 GOTO(out_nolock, rc = -EINVAL);
1609 /* Do nothing here, we re-create objects during recovery
1610 * upon write replay, see ofd_preprw_write() */
1611 GOTO(out_nolock, rc = 0);
1613 /* former ofd_handle_precreate */
1614 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1615 (oa->o_flags & OBD_FL_DELORPHAN)) {
1616 exp->exp_filter_data.fed_lastid_gen = ofd->ofd_lastid_gen;
1618 /* destroy orphans */
1619 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1620 exp->exp_conn_cnt) {
1621 CERROR("%s: dropping old orphan cleanup request\n",
1623 GOTO(out_nolock, rc = 0);
1625 /* This causes inflight precreates to abort and drop lock */
1626 oseq->os_destroys_in_progress = 1;
1627 mutex_lock(&oseq->os_create_lock);
1628 if (!oseq->os_destroys_in_progress) {
1629 CERROR("%s:["LPU64"] destroys_in_progress already"
1630 " cleared\n", ofd_name(ofd), seq);
1631 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1634 diff = oid - ofd_seq_last_oid(oseq);
1635 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1636 ofd_seq_last_oid(oseq), diff);
1637 if (-diff > OST_MAX_PRECREATE) {
1638 /* FIXME: should reset precreate_next_id on MDS */
1640 } else if (diff < 0) {
1641 rc = ofd_orphans_destroy(tsi->tsi_env, exp,
1643 oseq->os_destroys_in_progress = 0;
1645 /* XXX: Used by MDS for the first time! */
1646 oseq->os_destroys_in_progress = 0;
1649 if (unlikely(exp->exp_filter_data.fed_lastid_gen !=
1650 ofd->ofd_lastid_gen)) {
1651 /* Keep the export ref so we can send the reply. */
1652 ofd_obd_disconnect(class_export_get(exp));
1653 GOTO(out_nolock, rc = -ENOTCONN);
1656 mutex_lock(&oseq->os_create_lock);
1657 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1658 exp->exp_conn_cnt) {
1659 CERROR("%s: dropping old precreate request\n",
1663 /* only precreate if seq is 0, IDIF or normal and also o_id
1664 * must be specfied */
1665 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1666 !fid_seq_is_idif(seq)) || oid == 0) {
1667 diff = 1; /* shouldn't we create this right now? */
1669 diff = oid - ofd_seq_last_oid(oseq);
1670 /* Do sync create if the seq is about to used up */
1671 if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1672 if (unlikely(oid >= IDIF_MAX_OID - 1))
1674 } else if (fid_seq_is_norm(seq)) {
1676 LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1679 CERROR("%s : invalid o_seq "DOSTID"\n",
1680 ofd_name(ofd), POSTID(&oa->o_oi));
1681 GOTO(out, rc = -EINVAL);
1686 cfs_time_t enough_time = cfs_time_shift(DISK_TIMEOUT);
1691 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1692 !(oa->o_flags & OBD_FL_DELORPHAN)) {
1693 /* don't enforce grant during orphan recovery */
1694 rc = ofd_grant_create(tsi->tsi_env,
1695 ofd_obd(ofd)->obd_self_export,
1698 CDEBUG(D_HA, "%s: failed to acquire grant "
1699 "space for precreate (%d): rc = %d\n",
1700 ofd_name(ofd), diff, rc);
1705 /* This can happen if a new OST is formatted and installed
1706 * in place of an old one at the same index. Instead of
1707 * precreating potentially millions of deleted old objects
1708 * (possibly filling the OST), only precreate the last batch.
1709 * LFSCK will eventually clean up any orphans. LU-14 */
1710 if (diff > 5 * OST_MAX_PRECREATE) {
1711 diff = OST_MAX_PRECREATE / 2;
1712 LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1713 "larger than the LAST_ID "DOSTID", only "
1714 "precreating the last %u objects.\n",
1715 ofd_name(ofd), POSTID(&oa->o_oi),
1716 5 * OST_MAX_PRECREATE,
1717 POSTID(&oseq->os_oi), diff);
1718 ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1722 next_id = ofd_seq_last_oid(oseq) + 1;
1723 count = ofd_precreate_batch(ofd, diff);
1725 CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1726 " at "LPU64"\n", ofd_name(ofd),
1727 count, seq, next_id);
1729 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1730 && cfs_time_after(jiffies, enough_time)) {
1731 CDEBUG(D_HA, "%s: Slow creates, %d/%d objects"
1732 " created at a rate of %d/s\n",
1733 ofd_name(ofd), created, diff + created,
1734 created / DISK_TIMEOUT);
1738 rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1739 oseq, count, sync_trans);
1743 } else if (rc < 0) {
1749 lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1750 LCONSOLE_WARN("%s: can't create the same count of"
1751 " objects when replaying the request"
1752 " (diff is %d). see LU-4621\n",
1753 ofd_name(ofd), diff);
1756 /* some objects got created, we can return
1757 * them, even if last creation failed */
1760 CERROR("%s: unable to precreate: rc = %d\n",
1763 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1764 !(oa->o_flags & OBD_FL_DELORPHAN))
1765 ofd_grant_commit(tsi->tsi_env,
1766 ofd_obd(ofd)->obd_self_export, rc);
1768 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1771 ofd_counter_incr(exp, LPROC_OFD_STATS_CREATE,
1774 mutex_unlock(&oseq->os_create_lock);
1777 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
1778 struct ofd_thread_info *info = ofd_info(tsi->tsi_env);
1779 struct lu_fid *fid = &info->fti_fid;
1781 /* For compatible purpose, it needs to convert back to
1782 * OST ID before put it on wire. */
1783 *fid = rep_oa->o_oi.oi_fid;
1784 fid_to_ostid(fid, &rep_oa->o_oi);
1786 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1788 ofd_seq_put(tsi->tsi_env, oseq);
1791 up_read(&ofd->ofd_lastid_rwsem);
1796 * OFD request handler for OST_DESTROY RPC.
1798 * This is OFD-specific part of request handling. It destroys data objects
1799 * related to destroyed object on MDT.
1801 * \param[in] tsi target session environment for this request
1803 * \retval 0 if successful
1804 * \retval negative value on error
1806 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1808 const struct ost_body *body = tsi->tsi_ost_body;
1809 struct ost_body *repbody;
1810 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1811 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1812 struct lu_fid *fid = &fti->fti_fid;
1819 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1822 /* This is old case for clients before Lustre 2.4 */
1823 /* If there's a DLM request, cancel the locks mentioned in it */
1824 if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1826 struct ldlm_request *dlm;
1828 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1831 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1834 *fid = body->oa.o_oi.oi_fid;
1835 oid = ostid_id(&body->oa.o_oi);
1838 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1840 /* check that o_misc makes sense */
1841 if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1842 count = body->oa.o_misc;
1844 count = 1; /* default case - single destroy */
1846 CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1847 POSTID(&body->oa.o_oi), count);
1852 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, fid, 0);
1853 if (lrc == -ENOENT) {
1855 "%s: destroying non-existent object "DFID"\n",
1856 ofd_name(ofd), PFID(fid));
1857 /* rewrite rc with -ENOENT only if it is 0 */
1860 } else if (lrc != 0) {
1861 CERROR("%s: error destroying object "DFID": %d\n",
1862 ofd_name(ofd), PFID(fid), lrc);
1868 lrc = fid_set_id(fid, oid);
1869 if (unlikely(lrc != 0 && count > 0))
1870 GOTO(out, rc = lrc);
1873 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1879 fid_to_ostid(fid, &repbody->oa.o_oi);
1884 * OFD request handler for OST_STATFS RPC.
1886 * This function gets statfs data from storage as part of request
1889 * \param[in] tsi target session environment for this request
1891 * \retval 0 if successful
1892 * \retval negative value on error
1894 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1896 struct obd_statfs *osfs;
1901 osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1903 rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1904 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1906 CERROR("%s: statfs failed: rc = %d\n",
1907 tgt_name(tsi->tsi_tgt), rc);
1909 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1912 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1919 * OFD request handler for OST_SYNC RPC.
1921 * Sync object data or all filesystem data to the disk and pack the
1924 * \param[in] tsi target session environment for this request
1926 * \retval 0 if successful
1927 * \retval negative value on error
1929 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1931 struct ost_body *body = tsi->tsi_ost_body;
1932 struct ost_body *repbody;
1933 struct ofd_thread_info *fti = tsi2ofd_info(tsi);
1934 struct ofd_device *ofd = ofd_exp(tsi->tsi_exp);
1935 struct ofd_object *fo = NULL;
1940 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1942 /* if no objid is specified, it means "sync whole filesystem" */
1943 if (!fid_is_zero(&tsi->tsi_fid)) {
1944 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1946 RETURN(PTR_ERR(fo));
1949 rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1950 fo != NULL ? ofd_object_child(fo) : NULL,
1951 repbody->oa.o_size, repbody->oa.o_blocks);
1955 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1960 repbody->oa.o_oi = body->oa.o_oi;
1961 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1963 rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1965 obdo_from_la(&repbody->oa, &fti->fti_attr,
1968 /* don't return rc from getattr */
1973 ofd_object_put(tsi->tsi_env, fo);
1978 * OFD request handler for OST_PUNCH RPC.
1980 * This is part of request processing. Validate request fields,
1981 * punch (truncate) the given OFD object and pack reply.
1983 * \param[in] tsi target session environment for this request
1985 * \retval 0 if successful
1986 * \retval negative value on error
1988 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1990 const struct obdo *oa = &tsi->tsi_ost_body->oa;
1991 struct ost_body *repbody;
1992 struct ofd_thread_info *info = tsi2ofd_info(tsi);
1993 struct ldlm_namespace *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1994 struct ldlm_resource *res;
1995 struct ofd_object *fo;
1996 struct filter_fid *ff = NULL;
1998 struct lustre_handle lh = { 0, };
2005 /* check that we do support OBD_CONNECT_TRUNCLOCK. */
2006 CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
2008 if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
2009 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
2010 RETURN(err_serious(-EPROTO));
2012 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2013 if (repbody == NULL)
2014 RETURN(err_serious(-ENOMEM));
2016 /* punch start,end are passed in o_size,o_blocks throught wire */
2020 if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
2023 /* standard truncate optimization: if file body is completely
2024 * destroyed, don't send data back to the server. */
2026 flags |= LDLM_FL_AST_DISCARD_DATA;
2028 repbody->oa.o_oi = oa->o_oi;
2029 repbody->oa.o_valid = OBD_MD_FLID;
2031 srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
2032 oa->o_flags & OBD_FL_SRVLOCK;
2035 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
2041 CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
2042 ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
2043 oa->o_valid, start, end);
2045 fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
2048 GOTO(out, rc = PTR_ERR(fo));
2050 la_from_obdo(&info->fti_attr, oa,
2051 OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
2052 info->fti_attr.la_size = start;
2053 info->fti_attr.la_valid |= LA_SIZE;
2055 if (oa->o_valid & OBD_MD_FLFID) {
2056 ff = &info->fti_mds_fid;
2057 ofd_prepare_fidea(ff, oa);
2060 rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
2061 ff, (struct obdo *)oa);
2065 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
2069 ofd_object_put(tsi->tsi_env, fo);
2072 tgt_extent_unlock(&lh, LCK_PW);
2074 /* we do not call this before to avoid lu_object_find() in
2075 * ->lvbo_update() holding another reference on the object.
2076 * otherwise concurrent destroy can make the object unavailable
2077 * for 2nd lu_object_find() waiting for the first reference
2078 * to go... deadlock! */
2079 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
2082 ldlm_res_lvbo_update(res, NULL, 0);
2083 ldlm_resource_putref(res);
2090 * OFD request handler for OST_QUOTACTL RPC.
2092 * This is part of request processing to validate incoming request fields,
2093 * get the requested data from OSD and pack reply.
2095 * \param[in] tsi target session environment for this request
2097 * \retval 0 if successful
2098 * \retval negative value on error
2100 static int ofd_quotactl(struct tgt_session_info *tsi)
2102 struct obd_quotactl *oqctl, *repoqc;
2107 oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
2109 RETURN(err_serious(-EPROTO));
2111 repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
2113 RETURN(err_serious(-ENOMEM));
2115 /* report success for quota on/off for interoperability with current MDT
2117 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
2121 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
2123 ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
2130 * Calculate the amount of time for lock prolongation.
2132 * This is helper for ofd_prolong_extent_locks() function to get
2133 * the timeout extra time.
2135 * \param[in] req current request
2137 * \retval amount of time to extend the timeout with
2139 static inline int prolong_timeout(struct ptlrpc_request *req)
2141 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2144 return obd_timeout / 2;
2146 return max(at_est2timeout(at_get(&svcpt->scp_at_estimate)),
2151 * Prolong single lock timeout.
2153 * This is supplemental function to the ofd_prolong_locks(). It prolongs
2156 * \param[in] tsi target session environment for this request
2157 * \param[in] lock LDLM lock to prolong
2158 * \param[in] extent related extent
2159 * \param[in] timeout timeout value to add
2161 * \retval 0 if lock is not suitable for prolongation
2162 * \retval 1 if lock was prolonged successfully
2164 static int ofd_prolong_one_lock(struct tgt_session_info *tsi,
2165 struct ldlm_lock *lock,
2166 struct ldlm_extent *extent, int timeout)
2169 if (lock->l_flags & LDLM_FL_DESTROYED) /* lock already cancelled */
2172 /* XXX: never try to grab resource lock here because we're inside
2173 * exp_bl_list_lock; in ldlm_lockd.c to handle waiting list we take
2174 * res lock and then exp_bl_list_lock. */
2176 if (!(lock->l_flags & LDLM_FL_AST_SENT))
2177 /* ignore locks not being cancelled */
2180 LDLM_DEBUG(lock, "refreshed for req x"LPU64" ext("LPU64"->"LPU64") "
2181 "to %ds.\n", tgt_ses_req(tsi)->rq_xid, extent->start,
2182 extent->end, timeout);
2184 /* OK. this is a possible lock the user holds doing I/O
2185 * let's refresh eviction timer for it */
2186 ldlm_refresh_waiting_lock(lock, timeout);
2191 * Prolong lock timeout for the given extent.
2193 * This function finds all locks related with incoming request and
2194 * prolongs their timeout.
2196 * If a client is holding a lock for a long time while it sends
2197 * read or write RPCs to the OST for the object under this lock,
2198 * then we don't want the OST to evict the client. Otherwise,
2199 * if the network or disk is very busy then the client may not
2200 * be able to make any progress to clear out dirty pages under
2201 * the lock and the application will fail.
2203 * Every time a Bulk Read/Write (BRW) request arrives for the object
2204 * covered by the lock, extend the timeout on that lock. The RPC should
2205 * contain a lock handle for the lock it is using, but this
2206 * isn't handled correctly by all client versions, and the
2207 * request may cover multiple locks.
2209 * \param[in] tsi target session environment for this request
2210 * \param[in] start start of extent
2211 * \param[in] end end of extent
2213 * \retval number of prolonged locks
2215 static int ofd_prolong_extent_locks(struct tgt_session_info *tsi,
2216 __u64 start, __u64 end)
2218 struct obd_export *exp = tsi->tsi_exp;
2219 struct obdo *oa = &tsi->tsi_ost_body->oa;
2220 struct ldlm_extent extent = {
2224 struct ldlm_lock *lock;
2225 int timeout = prolong_timeout(tgt_ses_req(tsi));
2230 if (oa->o_valid & OBD_MD_FLHANDLE) {
2231 /* mostly a request should be covered by only one lock, try
2233 lock = ldlm_handle2lock(&oa->o_handle);
2235 /* Fast path to check if the lock covers the whole IO
2236 * region exclusively. */
2237 if (lock->l_granted_mode == LCK_PW &&
2238 ldlm_extent_contain(&lock->l_policy_data.l_extent,
2241 LASSERT(lock->l_export == exp);
2242 lock_count = ofd_prolong_one_lock(tsi, lock,
2244 LDLM_LOCK_PUT(lock);
2247 LDLM_LOCK_PUT(lock);
2251 spin_lock_bh(&exp->exp_bl_list_lock);
2252 list_for_each_entry(lock, &exp->exp_bl_list, l_exp_list) {
2253 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
2254 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
2256 if (!ldlm_res_eq(&tsi->tsi_resid, &lock->l_resource->lr_name))
2259 if (!ldlm_extent_overlap(&lock->l_policy_data.l_extent,
2263 lock_count += ofd_prolong_one_lock(tsi, lock, &extent, timeout);
2265 spin_unlock_bh(&exp->exp_bl_list_lock);
2271 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_match for OFD RW requests.
2273 * Determine if \a lock and the lock from request \a req are equivalent
2274 * by comparing their resource names, modes, and extents.
2276 * It is used to give priority to read and write RPCs being done
2277 * under this lock so that the client can drop the contended
2278 * lock more quickly and let other clients use it. This improves
2279 * overall performance in the case where the first client gets a
2280 * very large lock extent that prevents other clients from
2281 * submitting their writes.
2283 * \param[in] req ptlrpc_request being processed
2284 * \param[in] lock contended lock to match
2286 * \retval 1 if lock is matched
2287 * \retval 0 otherwise
2289 static int ofd_rw_hpreq_lock_match(struct ptlrpc_request *req,
2290 struct ldlm_lock *lock)
2292 struct niobuf_remote *rnb;
2293 struct obd_ioobj *ioo;
2295 struct ldlm_extent ext;
2296 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
2300 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
2301 LASSERT(ioo != NULL);
2303 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
2304 LASSERT(rnb != NULL);
2306 ext.start = rnb->rnb_offset;
2307 rnb += ioo->ioo_bufcnt - 1;
2308 ext.end = rnb->rnb_offset + rnb->rnb_len - 1;
2310 LASSERT(lock->l_resource != NULL);
2311 if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
2314 /* a bulk write can only hold a reference on a PW extent lock */
2316 if (opc == OST_READ)
2317 /* whereas a bulk read can be protected by either a PR or PW
2321 if (!(lock->l_granted_mode & mode))
2324 RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
2328 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OFD RW requests.
2330 * Check for whether the given PTLRPC request (\a req) is blocking
2331 * an LDLM lock cancel.
2333 * \param[in] req the incoming request
2335 * \retval 1 if \a req is blocking an LDLM lock cancel
2336 * \retval 0 if it is not
2338 static int ofd_rw_hpreq_check(struct ptlrpc_request *req)
2340 struct tgt_session_info *tsi;
2341 struct obd_ioobj *ioo;
2342 struct niobuf_remote *rnb;
2348 /* Don't use tgt_ses_info() to get session info, because lock_match()
2349 * can be called while request has no processing thread yet. */
2350 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
2353 * Use LASSERT below because malformed RPCs should have
2354 * been filtered out in tgt_hpreq_handler().
2356 ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
2357 LASSERT(ioo != NULL);
2359 rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
2360 LASSERT(rnb != NULL);
2361 LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
2363 start = rnb->rnb_offset;
2364 rnb += ioo->ioo_bufcnt - 1;
2365 end = rnb->rnb_offset + rnb->rnb_len - 1;
2367 DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID
2368 " ("LPU64"->"LPU64")\n",
2369 tgt_name(tsi->tsi_tgt), current->comm,
2370 PFID(&tsi->tsi_fid), start, end);
2372 lock_count = ofd_prolong_extent_locks(tsi, start, end);
2374 CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
2375 tgt_name(tsi->tsi_tgt), lock_count, req);
2377 RETURN(lock_count > 0);
2381 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OFD RW requests.
2383 * Called after the request has been handled. It refreshes lock timeout again
2384 * so that client has more time to send lock cancel RPC.
2386 * \param[in] req request which is being processed.
2388 static void ofd_rw_hpreq_fini(struct ptlrpc_request *req)
2390 ofd_rw_hpreq_check(req);
2394 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_match for OST_PUNCH request.
2396 * This function checks if the given lock is the same by its resname, mode
2397 * and extent as one taken from the request.
2398 * It is used to give priority to punch/truncate RPCs that might lead to
2399 * the fastest release of that lock when a lock is contended.
2401 * \param[in] req ptlrpc_request being processed
2402 * \param[in] lock contended lock to match
2404 * \retval 1 if lock is matched
2405 * \retval 0 otherwise
2407 static int ofd_punch_hpreq_lock_match(struct ptlrpc_request *req,
2408 struct ldlm_lock *lock)
2410 struct tgt_session_info *tsi;
2412 /* Don't use tgt_ses_info() to get session info, because lock_match()
2413 * can be called while request has no processing thread yet. */
2414 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
2417 * Use LASSERT below because malformed RPCs should have
2418 * been filtered out in tgt_hpreq_handler().
2420 LASSERT(tsi->tsi_ost_body != NULL);
2421 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
2422 tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
2429 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OST_PUNCH request.
2431 * High-priority queue request check for whether the given punch request
2432 * (\a req) is blocking an LDLM lock cancel.
2434 * \param[in] req the incoming request
2436 * \retval 1 if \a req is blocking an LDLM lock cancel
2437 * \retval 0 if it is not
2439 static int ofd_punch_hpreq_check(struct ptlrpc_request *req)
2441 struct tgt_session_info *tsi;
2447 /* Don't use tgt_ses_info() to get session info, because lock_match()
2448 * can be called while request has no processing thread yet. */
2449 tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
2450 LASSERT(tsi != NULL);
2451 oa = &tsi->tsi_ost_body->oa;
2453 LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
2454 oa->o_flags & OBD_FL_SRVLOCK));
2457 "%s: refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
2458 tgt_name(tsi->tsi_tgt), tsi->tsi_resid.name[0],
2459 tsi->tsi_resid.name[1], oa->o_size, oa->o_blocks);
2461 lock_count = ofd_prolong_extent_locks(tsi, oa->o_size, oa->o_blocks);
2463 CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
2464 tgt_name(tsi->tsi_tgt), lock_count, req);
2466 RETURN(lock_count > 0);
2470 * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OST_PUNCH request.
2472 * Called after the request has been handled. It refreshes lock timeout again
2473 * so that client has more time to send lock cancel RPC.
2475 * \param[in] req request which is being processed.
2477 static void ofd_punch_hpreq_fini(struct ptlrpc_request *req)
2479 ofd_punch_hpreq_check(req);
2482 struct ptlrpc_hpreq_ops ofd_hpreq_rw = {
2483 .hpreq_lock_match = ofd_rw_hpreq_lock_match,
2484 .hpreq_check = ofd_rw_hpreq_check,
2485 .hpreq_fini = ofd_rw_hpreq_fini
2488 struct ptlrpc_hpreq_ops ofd_hpreq_punch = {
2489 .hpreq_lock_match = ofd_punch_hpreq_lock_match,
2490 .hpreq_check = ofd_punch_hpreq_check,
2491 .hpreq_fini = ofd_punch_hpreq_fini
2495 * Assign high priority operations to an IO request.
2497 * Check if the incoming request is a candidate for
2498 * high-priority processing. If it is, assign it a high
2499 * priority operations table.
2501 * \param[in] tsi target session environment for this request
2503 static void ofd_hp_brw(struct tgt_session_info *tsi)
2505 struct niobuf_remote *rnb;
2506 struct obd_ioobj *ioo;
2510 ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
2511 LASSERT(ioo != NULL); /* must exist after request preprocessing */
2512 if (ioo->ioo_bufcnt > 0) {
2513 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
2514 LASSERT(rnb != NULL); /* must exist after request preprocessing */
2516 /* no high priority if server lock is needed */
2517 if (rnb->rnb_flags & OBD_BRW_SRVLOCK)
2520 tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_rw;
2524 * Assign high priority operations to an punch request.
2526 * Check if the incoming request is a candidate for
2527 * high-priority processing. If it is, assign it a high
2528 * priority operations table.
2530 * \param[in] tsi target session environment for this request
2532 static void ofd_hp_punch(struct tgt_session_info *tsi)
2534 LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
2535 /* no high-priority if server lock is needed */
2536 if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
2537 tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK)
2539 tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_punch;
2542 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
2543 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
2544 #define OST_BRW_READ OST_READ
2545 #define OST_BRW_WRITE OST_WRITE
2548 * Table of OFD-specific request handlers
2550 * This table contains all opcodes accepted by OFD and
2551 * specifies handlers for them. The tgt_request_handler()
2552 * uses such table from each target to process incoming
2555 static struct tgt_handler ofd_tgt_handlers[] = {
2556 TGT_RPC_HANDLER(OST_FIRST_OPC,
2557 0, OST_CONNECT, tgt_connect,
2558 &RQF_CONNECT, LUSTRE_OBD_VERSION),
2559 TGT_RPC_HANDLER(OST_FIRST_OPC,
2560 0, OST_DISCONNECT, tgt_disconnect,
2561 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
2562 TGT_RPC_HANDLER(OST_FIRST_OPC,
2563 0, OST_SET_INFO, ofd_set_info_hdl,
2564 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
2565 TGT_OST_HDL(0, OST_GET_INFO, ofd_get_info_hdl),
2566 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR, ofd_getattr_hdl),
2567 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2568 OST_SETATTR, ofd_setattr_hdl),
2569 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
2570 OST_CREATE, ofd_create_hdl),
2571 TGT_OST_HDL(0 | HABEO_REFERO | MUTABOR,
2572 OST_DESTROY, ofd_destroy_hdl),
2573 TGT_OST_HDL(0 | HABEO_REFERO, OST_STATFS, ofd_statfs_hdl),
2574 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO,
2575 OST_BRW_READ, tgt_brw_read,
2577 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
2578 TGT_OST_HDL_HP(HABEO_CORPUS| MUTABOR, OST_BRW_WRITE, tgt_brw_write,
2580 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2581 OST_PUNCH, ofd_punch_hdl,
2583 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC, ofd_sync_hdl),
2584 TGT_OST_HDL(0 | HABEO_REFERO, OST_QUOTACTL, ofd_quotactl),
2587 static struct tgt_opc_slice ofd_common_slice[] = {
2589 .tos_opc_start = OST_FIRST_OPC,
2590 .tos_opc_end = OST_LAST_OPC,
2591 .tos_hs = ofd_tgt_handlers
2594 .tos_opc_start = OBD_FIRST_OPC,
2595 .tos_opc_end = OBD_LAST_OPC,
2596 .tos_hs = tgt_obd_handlers
2599 .tos_opc_start = LDLM_FIRST_OPC,
2600 .tos_opc_end = LDLM_LAST_OPC,
2601 .tos_hs = tgt_dlm_handlers
2604 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
2605 .tos_opc_end = OUT_UPDATE_LAST_OPC,
2606 .tos_hs = tgt_out_handlers
2609 .tos_opc_start = SEQ_FIRST_OPC,
2610 .tos_opc_end = SEQ_LAST_OPC,
2611 .tos_hs = seq_handlers
2614 .tos_opc_start = LFSCK_FIRST_OPC,
2615 .tos_opc_end = LFSCK_LAST_OPC,
2616 .tos_hs = tgt_lfsck_handlers
2623 /* context key constructor/destructor: ofd_key_init(), ofd_key_fini() */
2624 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
2627 * Implementation of lu_context_key::lct_key_exit.
2629 * Optional method called on lu_context_exit() for all allocated
2631 * It is used in OFD to sanitize context values which may be re-used
2632 * during another request processing by the same thread.
2634 * \param[in] ctx execution context
2635 * \param[in] key context key
2636 * \param[in] data ofd_thread_info
2638 static void ofd_key_exit(const struct lu_context *ctx,
2639 struct lu_context_key *key, void *data)
2641 struct ofd_thread_info *info = data;
2643 info->fti_env = NULL;
2644 info->fti_exp = NULL;
2647 info->fti_pre_version = 0;
2650 memset(&info->fti_attr, 0, sizeof info->fti_attr);
2653 struct lu_context_key ofd_thread_key = {
2654 .lct_tags = LCT_DT_THREAD,
2655 .lct_init = ofd_key_init,
2656 .lct_fini = ofd_key_fini,
2657 .lct_exit = ofd_key_exit
2661 * Initialize OFD device according to parameters in the config log \a cfg.
2663 * This is the main starting point of OFD initialization. It fills all OFD
2664 * parameters with their initial values and calls other initializing functions
2665 * to set up all OFD subsystems.
2667 * \param[in] env execution environment
2668 * \param[in] m OFD device
2669 * \param[in] ldt LU device type of OFD
2670 * \param[in] cfg configuration log
2672 * \retval 0 if successful
2673 * \retval negative value on error
2675 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
2676 struct lu_device_type *ldt, struct lustre_cfg *cfg)
2678 const char *dev = lustre_cfg_string(cfg, 0);
2679 struct ofd_thread_info *info = NULL;
2680 struct obd_device *obd;
2681 struct obd_statfs *osfs;
2686 obd = class_name2obd(dev);
2688 CERROR("Cannot find obd with name %s\n", dev);
2692 rc = lu_env_refill((struct lu_env *)env);
2696 obd->u.obt.obt_magic = OBT_MAGIC;
2698 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
2699 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
2701 spin_lock_init(&m->ofd_flags_lock);
2702 m->ofd_raid_degraded = 0;
2703 m->ofd_syncjournal = 0;
2705 m->ofd_grant_compat_disable = 0;
2706 m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
2709 spin_lock_init(&m->ofd_osfs_lock);
2710 m->ofd_osfs_age = cfs_time_shift_64(-1000);
2711 m->ofd_osfs_unstable = 0;
2712 m->ofd_statfs_inflight = 0;
2713 m->ofd_osfs_inflight = 0;
2716 spin_lock_init(&m->ofd_grant_lock);
2717 m->ofd_tot_dirty = 0;
2718 m->ofd_tot_granted = 0;
2719 m->ofd_tot_pending = 0;
2720 m->ofd_seq_count = 0;
2721 init_waitqueue_head(&m->ofd_inconsistency_thread.t_ctl_waitq);
2722 INIT_LIST_HEAD(&m->ofd_inconsistency_list);
2723 spin_lock_init(&m->ofd_inconsistency_lock);
2725 spin_lock_init(&m->ofd_batch_lock);
2726 init_rwsem(&m->ofd_lastid_rwsem);
2728 obd->u.filter.fo_fl_oss_capa = 0;
2729 INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
2730 obd->u.filter.fo_capa_hash = init_capa_hash();
2731 if (obd->u.filter.fo_capa_hash == NULL)
2734 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
2735 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
2736 /* set this lu_device to obd, because error handling need it */
2737 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
2739 rc = ofd_procfs_init(m);
2741 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
2745 /* No connection accepted until configurations will finish */
2746 spin_lock(&obd->obd_dev_lock);
2747 obd->obd_no_conn = 1;
2748 spin_unlock(&obd->obd_dev_lock);
2749 obd->obd_replayable = 1;
2750 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
2751 char *str = lustre_cfg_string(cfg, 4);
2753 if (strchr(str, 'n')) {
2754 CWARN("%s: recovery disabled\n", obd->obd_name);
2755 obd->obd_replayable = 0;
2759 info = ofd_info_init(env, NULL);
2763 rc = ofd_stack_init(env, m, cfg);
2765 CERROR("Can't init device stack, rc %d\n", rc);
2766 GOTO(err_fini_proc, rc);
2769 ofd_procfs_add_brw_stats_symlink(m);
2771 /* populate cached statfs data */
2772 osfs = &ofd_info(env)->fti_u.osfs;
2773 rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
2775 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
2776 GOTO(err_fini_stack, rc);
2778 if (!IS_PO2(osfs->os_bsize)) {
2779 CERROR("%s: blocksize (%d) is not a power of 2\n",
2780 obd->obd_name, osfs->os_bsize);
2781 GOTO(err_fini_stack, rc = -EPROTO);
2783 m->ofd_blockbits = fls(osfs->os_bsize) - 1;
2785 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
2786 if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
2787 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
2789 snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
2790 "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
2791 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
2792 LDLM_NAMESPACE_SERVER,
2793 LDLM_NAMESPACE_GREEDY,
2795 if (m->ofd_namespace == NULL)
2796 GOTO(err_fini_stack, rc = -ENOMEM);
2797 /* set obd_namespace for compatibility with old code */
2798 obd->obd_namespace = m->ofd_namespace;
2799 ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
2800 m->ofd_namespace->ns_lvbo = &ofd_lvbo;
2801 m->ofd_namespace->ns_lvbp = m;
2803 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2804 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
2806 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
2808 /* Allow at most ddp_grant_reserved% of the available filesystem space
2809 * to be granted to clients, so that any errors in the grant overhead
2810 * calculations do not allow granting more space to clients than can be
2811 * written. Assumes that in aggregate the grant overhead calculations do
2812 * not have more than ddp_grant_reserved% estimation error in them. */
2813 m->ofd_grant_ratio =
2814 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
2816 rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
2817 OBD_FAIL_OST_ALL_REQUEST_NET,
2818 OBD_FAIL_OST_ALL_REPLY_NET);
2820 GOTO(err_free_ns, rc);
2822 rc = ofd_fs_setup(env, m, obd);
2824 GOTO(err_fini_lut, rc);
2826 rc = ofd_start_inconsistency_verification_thread(m);
2828 GOTO(err_fini_fs, rc);
2833 ofd_fs_cleanup(env, m);
2835 tgt_fini(env, &m->ofd_lut);
2837 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
2838 obd->obd_namespace = m->ofd_namespace = NULL;
2840 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
2847 * Stop the OFD device
2849 * This function stops the OFD device and all its subsystems.
2850 * This is the end of OFD lifecycle.
2852 * \param[in] env execution environment
2853 * \param[in] m OFD device
2855 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
2857 struct obd_device *obd = ofd_obd(m);
2858 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
2859 struct lfsck_stop stop;
2861 stop.ls_status = LS_PAUSED;
2863 lfsck_stop(env, m->ofd_osd, &stop);
2864 target_recovery_fini(obd);
2865 obd_exports_barrier(obd);
2866 obd_zombie_barrier();
2868 tgt_fini(env, &m->ofd_lut);
2869 ofd_stop_inconsistency_verification_thread(m);
2870 lfsck_degister(env, m->ofd_osd);
2871 ofd_fs_cleanup(env, m);
2873 ofd_free_capa_keys(m);
2874 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
2876 if (m->ofd_namespace != NULL) {
2877 ldlm_namespace_free(m->ofd_namespace, NULL,
2878 d->ld_obd->obd_force);
2879 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
2882 ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
2884 LASSERT(atomic_read(&d->ld_ref) == 0);
2885 server_put_mount(obd->obd_name, true);
2890 * Implementation of lu_device_type_operations::ldto_device_fini.
2892 * Finalize device. Dual to ofd_device_init(). It is called from
2893 * obd_precleanup() and stops the current device.
2895 * \param[in] env execution environment
2896 * \param[in] d LU device of OFD
2900 static struct lu_device *ofd_device_fini(const struct lu_env *env,
2901 struct lu_device *d)
2904 ofd_fini(env, ofd_dev(d));
2909 * Implementation of lu_device_type_operations::ldto_device_free.
2911 * Free OFD device. Dual to ofd_device_alloc().
2913 * \param[in] env execution environment
2914 * \param[in] d LU device of OFD
2918 static struct lu_device *ofd_device_free(const struct lu_env *env,
2919 struct lu_device *d)
2921 struct ofd_device *m = ofd_dev(d);
2923 dt_device_fini(&m->ofd_dt_dev);
2929 * Implementation of lu_device_type_operations::ldto_device_alloc.
2931 * This function allocates the new OFD device. It is called from
2932 * obd_setup() if OBD device had lu_device_type defined.
2934 * \param[in] env execution environment
2935 * \param[in] t lu_device_type of OFD device
2936 * \param[in] cfg configuration log
2938 * \retval pointer to the lu_device of just allocated OFD
2939 * \retval ERR_PTR of return value on error
2941 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
2942 struct lu_device_type *t,
2943 struct lustre_cfg *cfg)
2945 struct ofd_device *m;
2946 struct lu_device *l;
2951 return ERR_PTR(-ENOMEM);
2953 l = &m->ofd_dt_dev.dd_lu_dev;
2954 dt_device_init(&m->ofd_dt_dev, t);
2955 rc = ofd_init0(env, m, t, cfg);
2957 ofd_device_free(env, l);
2964 /* type constructor/destructor: ofd_type_init(), ofd_type_fini() */
2965 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
2967 static struct lu_device_type_operations ofd_device_type_ops = {
2968 .ldto_init = ofd_type_init,
2969 .ldto_fini = ofd_type_fini,
2971 .ldto_start = ofd_type_start,
2972 .ldto_stop = ofd_type_stop,
2974 .ldto_device_alloc = ofd_device_alloc,
2975 .ldto_device_free = ofd_device_free,
2976 .ldto_device_fini = ofd_device_fini
2979 static struct lu_device_type ofd_device_type = {
2980 .ldt_tags = LU_DEVICE_DT,
2981 .ldt_name = LUSTRE_OST_NAME,
2982 .ldt_ops = &ofd_device_type_ops,
2983 .ldt_ctx_tags = LCT_DT_THREAD
2987 * Initialize OFD module.
2989 * This function is called upon module loading. It registers OFD device type
2990 * and prepares all in-memory structures used by all OFD devices.
2992 * \retval 0 if successful
2993 * \retval negative value on error
2995 int __init ofd_init(void)
2999 rc = lu_kmem_init(ofd_caches);
3003 rc = ofd_fmd_init();
3005 lu_kmem_fini(ofd_caches);
3009 rc = class_register_type(&ofd_obd_ops, NULL, true, NULL,
3010 LUSTRE_OST_NAME, &ofd_device_type);
3017 * This function is called upon OFD module unloading.
3018 * It frees all related structures and unregisters OFD device type.
3020 void __exit ofd_exit(void)
3023 lu_kmem_fini(ofd_caches);
3024 class_unregister_type(LUSTRE_OST_NAME);
3027 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
3028 MODULE_DESCRIPTION("Lustre Object Filtering Device");
3029 MODULE_LICENSE("GPL");
3031 module_init(ofd_init);
3032 module_exit(ofd_exit);