-/* If @oti is given, the request goes from MDS and responses from OSTs are not
- needed. Otherwise, a client is waiting for responses. */
-static int lov_setattr_async(struct obd_export *exp, struct obd_info *oinfo,
- struct obd_trans_info *oti,
- struct ptlrpc_request_set *rqset)
-{
- struct lov_request_set *set;
- struct lov_request *req;
- cfs_list_t *pos;
- struct lov_obd *lov;
- int rc = 0;
- ENTRY;
-
- LASSERT(oinfo);
- ASSERT_LSM_MAGIC(oinfo->oi_md);
- if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE) {
- LASSERT(oti);
- LASSERT(oti->oti_logcookies);
- }
-
- if (!exp || !exp->exp_obd)
- RETURN(-ENODEV);
-
- lov = &exp->exp_obd->u.lov;
- rc = lov_prep_setattr_set(exp, oinfo, oti, &set);
- if (rc)
- RETURN(rc);
-
- CDEBUG(D_INFO, "objid "DOSTID": %ux%u byte stripes\n",
- POSTID(&oinfo->oi_md->lsm_oi),
- oinfo->oi_md->lsm_stripe_count,
- oinfo->oi_md->lsm_stripe_size);
-
- cfs_list_for_each(pos, &set->set_list) {
- req = cfs_list_entry(pos, struct lov_request, rq_link);
-
- if (oinfo->oi_oa->o_valid & OBD_MD_FLCOOKIE)
- oti->oti_logcookies = set->set_cookies + req->rq_stripe;
-
- CDEBUG(D_INFO, "objid "DOSTID"[%d] has subobj "DOSTID" at idx"
- "%u\n", POSTID(&oinfo->oi_oa->o_oi), req->rq_stripe,
- POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx);
-
- rc = obd_setattr_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
- &req->rq_oi, oti, rqset);
- if (rc) {
- CERROR("error: setattr objid "DOSTID" subobj"
- DOSTID" on OST idx %d: rc = %d\n",
- POSTID(&set->set_oi->oi_oa->o_oi),
- POSTID(&req->rq_oi.oi_oa->o_oi),
- req->rq_idx, rc);
- break;
- }
- }
-
- /* If we are not waiting for responses on async requests, return. */
- if (rc || !rqset || cfs_list_empty(&rqset->set_requests)) {
- int err;
- if (rc)
- cfs_atomic_set(&set->set_completes, 0);
- err = lov_fini_setattr_set(set);
- RETURN(rc ? rc : err);
- }
-
- LASSERT(rqset->set_interpret == NULL);
- rqset->set_interpret = lov_setattr_interpret;
- rqset->set_arg = (void *)set;
-
- RETURN(0);
-}
-
-static int lov_punch_interpret(struct ptlrpc_request_set *rqset,
- void *data, int rc)
-{
- struct lov_request_set *lovset = (struct lov_request_set *)data;
- int err;
- ENTRY;
-
- if (rc)
- cfs_atomic_set(&lovset->set_completes, 0);
- err = lov_fini_punch_set(lovset);
- RETURN(rc ? rc : err);
-}
-
-/* FIXME: maybe we'll just make one node the authoritative attribute node, then
- * we can send this 'punch' to just the authoritative node and the nodes
- * that the punch will affect. */
-static int lov_punch(const struct lu_env *env, struct obd_export *exp,
- struct obd_info *oinfo, struct obd_trans_info *oti,
- struct ptlrpc_request_set *rqset)
-{
- struct lov_request_set *set;
- struct lov_obd *lov;
- cfs_list_t *pos;
- struct lov_request *req;
- int rc = 0;
- ENTRY;
-
- LASSERT(oinfo);
- ASSERT_LSM_MAGIC(oinfo->oi_md);
-
- if (!exp || !exp->exp_obd)
- RETURN(-ENODEV);
-
- lov = &exp->exp_obd->u.lov;
- rc = lov_prep_punch_set(exp, oinfo, oti, &set);
- if (rc)
- RETURN(rc);
-
- cfs_list_for_each (pos, &set->set_list) {
- req = cfs_list_entry(pos, struct lov_request, rq_link);
-
- rc = obd_punch(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
- &req->rq_oi, NULL, rqset);
- if (rc) {
- CERROR("%s: punch objid "DOSTID" subobj "DOSTID
- " on OST idx %d: rc = %d\n",
- exp->exp_obd->obd_name,
- POSTID(&set->set_oi->oi_oa->o_oi),
- POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx, rc);
- break;
- }
- }
-
- if (rc || cfs_list_empty(&rqset->set_requests)) {
- int err;
- err = lov_fini_punch_set(set);
- RETURN(rc ? rc : err);
- }
-
- LASSERT(rqset->set_interpret == NULL);
- rqset->set_interpret = lov_punch_interpret;
- rqset->set_arg = (void *)set;
-
- RETURN(0);
-}
-
-static int lov_sync_interpret(struct ptlrpc_request_set *rqset,
- void *data, int rc)
-{
- struct lov_request_set *lovset = data;
- int err;
- ENTRY;
-
- if (rc)
- cfs_atomic_set(&lovset->set_completes, 0);
- err = lov_fini_sync_set(lovset);
- RETURN(rc ?: err);
-}
-
-static int lov_sync(const struct lu_env *env, struct obd_export *exp,
- struct obd_info *oinfo, obd_off start, obd_off end,
- struct ptlrpc_request_set *rqset)
-{
- struct lov_request_set *set = NULL;
- struct lov_obd *lov;
- cfs_list_t *pos;
- struct lov_request *req;
- int rc = 0;
- ENTRY;
-
- ASSERT_LSM_MAGIC(oinfo->oi_md);
- LASSERT(rqset != NULL);
-
- if (!exp->exp_obd)
- RETURN(-ENODEV);
-
- lov = &exp->exp_obd->u.lov;
- rc = lov_prep_sync_set(exp, oinfo, start, end, &set);
- if (rc)
- RETURN(rc);
-
- CDEBUG(D_INFO, "fsync objid "DOSTID" ["LPX64", "LPX64"]\n",
- POSTID(&set->set_oi->oi_oa->o_oi), start, end);
-
- cfs_list_for_each (pos, &set->set_list) {
- req = cfs_list_entry(pos, struct lov_request, rq_link);
-
- rc = obd_sync(env, lov->lov_tgts[req->rq_idx]->ltd_exp,
- &req->rq_oi, req->rq_oi.oi_policy.l_extent.start,
- req->rq_oi.oi_policy.l_extent.end, rqset);
- if (rc) {
- CERROR("%s: fsync objid "DOSTID" subobj "DOSTID
- " on OST idx %d: rc = %d\n",
- exp->exp_obd->obd_name,
- POSTID(&set->set_oi->oi_oa->o_oi),
- POSTID(&req->rq_oi.oi_oa->o_oi), req->rq_idx,
- rc);
- break;
- }
- }
-
- /* If we are not waiting for responses on async requests, return. */
- if (rc || cfs_list_empty(&rqset->set_requests)) {
- int err = lov_fini_sync_set(set);
-
- RETURN(rc ?: err);
- }
-
- LASSERT(rqset->set_interpret == NULL);
- rqset->set_interpret = lov_sync_interpret;
- rqset->set_arg = (void *)set;
-
- RETURN(0);
-}
-
-static int lov_brw_check(struct lov_obd *lov, struct obd_info *lov_oinfo,
- obd_count oa_bufs, struct brw_page *pga)
-{
- struct obd_info oinfo = { { { 0 } } };
- int i, rc = 0;
-
- oinfo.oi_oa = lov_oinfo->oi_oa;
-
- /* The caller just wants to know if there's a chance that this
- * I/O can succeed */
- for (i = 0; i < oa_bufs; i++) {
- int stripe = lov_stripe_number(lov_oinfo->oi_md, pga[i].off);
- int ost = lov_oinfo->oi_md->lsm_oinfo[stripe]->loi_ost_idx;
- obd_off start, end;
-
- if (!lov_stripe_intersects(lov_oinfo->oi_md, i, pga[i].off,
- pga[i].off + pga[i].count - 1,
- &start, &end))
- continue;
-
- if (!lov->lov_tgts[ost] || !lov->lov_tgts[ost]->ltd_active) {
- CDEBUG(D_HA, "lov idx %d inactive\n", ost);
- return -EIO;
- }
-
- rc = obd_brw(OBD_BRW_CHECK, lov->lov_tgts[ost]->ltd_exp, &oinfo,
- 1, &pga[i], NULL);
- if (rc)
- break;
- }
- return rc;
-}
-
-static int lov_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
- obd_count oa_bufs, struct brw_page *pga,
- struct obd_trans_info *oti)
-{
- struct lov_request_set *set;
- struct lov_request *req;
- cfs_list_t *pos;
- struct lov_obd *lov = &exp->exp_obd->u.lov;
- int err, rc = 0;
- ENTRY;
-
- ASSERT_LSM_MAGIC(oinfo->oi_md);
-
- if (cmd == OBD_BRW_CHECK) {
- rc = lov_brw_check(lov, oinfo, oa_bufs, pga);
- RETURN(rc);
- }
-
- rc = lov_prep_brw_set(exp, oinfo, oa_bufs, pga, oti, &set);
- if (rc)
- RETURN(rc);
-
- cfs_list_for_each (pos, &set->set_list) {
- struct obd_export *sub_exp;
- struct brw_page *sub_pga;
- req = cfs_list_entry(pos, struct lov_request, rq_link);
-
- sub_exp = lov->lov_tgts[req->rq_idx]->ltd_exp;
- sub_pga = set->set_pga + req->rq_pgaidx;
- rc = obd_brw(cmd, sub_exp, &req->rq_oi, req->rq_oabufs,
- sub_pga, oti);
- if (rc)
- break;
- lov_update_common_set(set, req, rc);
- }
-
- err = lov_fini_brw_set(set);
- if (!rc)
- rc = err;
- RETURN(rc);
-}
-
-static int lov_enqueue_interpret(struct ptlrpc_request_set *rqset,
- void *data, int rc)
-{
- struct lov_request_set *lovset = (struct lov_request_set *)data;
- ENTRY;
- rc = lov_fini_enqueue_set(lovset, lovset->set_ei->ei_mode, rc, rqset);
- RETURN(rc);
-}
-
-static int lov_enqueue(struct obd_export *exp, struct obd_info *oinfo,
- struct ldlm_enqueue_info *einfo,
- struct ptlrpc_request_set *rqset)
-{
- ldlm_mode_t mode = einfo->ei_mode;
- struct lov_request_set *set;
- struct lov_request *req;
- cfs_list_t *pos;
- struct lov_obd *lov;
- ldlm_error_t rc;
- ENTRY;
-
- LASSERT(oinfo);
- ASSERT_LSM_MAGIC(oinfo->oi_md);
- LASSERT(mode == (mode & -mode));
-
- /* we should never be asked to replay a lock this way. */
- LASSERT((oinfo->oi_flags & LDLM_FL_REPLAY) == 0);
-
- if (!exp || !exp->exp_obd)
- RETURN(-ENODEV);
-
- lov = &exp->exp_obd->u.lov;
- rc = lov_prep_enqueue_set(exp, oinfo, einfo, &set);
- if (rc)
- RETURN(rc);
-
- cfs_list_for_each (pos, &set->set_list) {
- req = cfs_list_entry(pos, struct lov_request, rq_link);
-
- rc = obd_enqueue(lov->lov_tgts[req->rq_idx]->ltd_exp,
- &req->rq_oi, einfo, rqset);
- if (rc != ELDLM_OK)
- GOTO(out, rc);
- }
-
- if (rqset && !cfs_list_empty(&rqset->set_requests)) {
- LASSERT(rc == 0);
- LASSERT(rqset->set_interpret == NULL);
- rqset->set_interpret = lov_enqueue_interpret;
- rqset->set_arg = (void *)set;
- RETURN(rc);
- }
-out:
- rc = lov_fini_enqueue_set(set, mode, rc, rqset);
- RETURN(rc);
-}
-
-static int lov_change_cbdata(struct obd_export *exp,
- struct lov_stripe_md *lsm, ldlm_iterator_t it,
- void *data)
-{
- struct lov_obd *lov;
- int rc = 0, i;
- ENTRY;
-
- ASSERT_LSM_MAGIC(lsm);
-
- if (!exp || !exp->exp_obd)
- RETURN(-ENODEV);
-
- lov = &exp->exp_obd->u.lov;
- for (i = 0; i < lsm->lsm_stripe_count; i++) {
- struct lov_stripe_md submd;
- struct lov_oinfo *loi = lsm->lsm_oinfo[i];
-
- if (!lov->lov_tgts[loi->loi_ost_idx]) {
- CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
- continue;
- }
-
- submd.lsm_oi = loi->loi_oi;
- submd.lsm_stripe_count = 0;
- rc = obd_change_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
- &submd, it, data);
- }
- RETURN(rc);
-}
-
-/* find any ldlm lock of the inode in lov
- * return 0 not find
- * 1 find one
- * < 0 error */
-static int lov_find_cbdata(struct obd_export *exp,
- struct lov_stripe_md *lsm, ldlm_iterator_t it,
- void *data)
-{
- struct lov_obd *lov;
- int rc = 0, i;
- ENTRY;
-
- ASSERT_LSM_MAGIC(lsm);
-
- if (!exp || !exp->exp_obd)
- RETURN(-ENODEV);
-
- lov = &exp->exp_obd->u.lov;
- for (i = 0; i < lsm->lsm_stripe_count; i++) {
- struct lov_stripe_md submd;
- struct lov_oinfo *loi = lsm->lsm_oinfo[i];
-
- if (!lov->lov_tgts[loi->loi_ost_idx]) {
- CDEBUG(D_HA, "lov idx %d NULL \n", loi->loi_ost_idx);
- continue;
- }
- submd.lsm_oi = loi->loi_oi;
- submd.lsm_stripe_count = 0;
- rc = obd_find_cbdata(lov->lov_tgts[loi->loi_ost_idx]->ltd_exp,
- &submd, it, data);
- if (rc != 0)
- RETURN(rc);
- }
- RETURN(rc);
-}
-
-static int lov_cancel(struct obd_export *exp, struct lov_stripe_md *lsm,
- __u32 mode, struct lustre_handle *lockh)
-{
- struct lov_request_set *set;
- struct obd_info oinfo;
- struct lov_request *req;
- cfs_list_t *pos;
- struct lov_obd *lov;
- struct lustre_handle *lov_lockhp;
- int err = 0, rc = 0;
- ENTRY;
-
- ASSERT_LSM_MAGIC(lsm);
-
- if (!exp || !exp->exp_obd)
- RETURN(-ENODEV);
-
- LASSERT(lockh);
- lov = &exp->exp_obd->u.lov;
- rc = lov_prep_cancel_set(exp, &oinfo, lsm, mode, lockh, &set);
- if (rc)
- RETURN(rc);
-
- cfs_list_for_each(pos, &set->set_list) {
- req = cfs_list_entry(pos, struct lov_request, rq_link);
- lov_lockhp = set->set_lockh->llh_handles + req->rq_stripe;
-
- rc = obd_cancel(lov->lov_tgts[req->rq_idx]->ltd_exp,
- req->rq_oi.oi_md, mode, lov_lockhp);
- rc = lov_update_common_set(set, req, rc);
- if (rc) {
- CERROR("%s: cancel objid "DOSTID" subobj "
- DOSTID" on OST idx %d: rc = %d\n",
- exp->exp_obd->obd_name, POSTID(&lsm->lsm_oi),
- POSTID(&req->rq_oi.oi_md->lsm_oi),
- req->rq_idx, rc);
- err = rc;
- }
-
- }
- lov_fini_cancel_set(set);
- RETURN(err);
-}
-
-static int lov_cancel_unused(struct obd_export *exp,
- struct lov_stripe_md *lsm,
- ldlm_cancel_flags_t flags, void *opaque)
-{
- struct lov_obd *lov;
- int rc = 0, i;
- ENTRY;
-
- if (!exp || !exp->exp_obd)
- RETURN(-ENODEV);
-
- lov = &exp->exp_obd->u.lov;
- if (lsm == NULL) {
- for (i = 0; i < lov->desc.ld_tgt_count; i++) {
- int err;
- if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
- continue;
-
- err = obd_cancel_unused(lov->lov_tgts[i]->ltd_exp, NULL,
- flags, opaque);
- if (!rc)
- rc = err;
- }
- RETURN(rc);
- }
-
- ASSERT_LSM_MAGIC(lsm);
-
- for (i = 0; i < lsm->lsm_stripe_count; i++) {
- struct lov_stripe_md submd;
- struct lov_oinfo *loi = lsm->lsm_oinfo[i];
- int idx = loi->loi_ost_idx;
- int err;
-
- if (!lov->lov_tgts[idx]) {
- CDEBUG(D_HA, "lov idx %d NULL\n", idx);
- continue;
- }
-
- if (!lov->lov_tgts[idx]->ltd_active)
- CDEBUG(D_HA, "lov idx %d inactive\n", idx);
-
- submd.lsm_oi = loi->loi_oi;
- submd.lsm_stripe_count = 0;
- err = obd_cancel_unused(lov->lov_tgts[idx]->ltd_exp,
- &submd, flags, opaque);
- if (err && lov->lov_tgts[idx]->ltd_active) {
- CERROR("%s: cancel unused objid "DOSTID
- " subobj "DOSTID" on OST idx %d: rc = %d\n",
- exp->exp_obd->obd_name, POSTID(&lsm->lsm_oi),
- POSTID(&loi->loi_oi), idx, err);
- if (!rc)
- rc = err;
- }
- }
- RETURN(rc);
-}
-
-int lov_statfs_interpret(struct ptlrpc_request_set *rqset, void *data, int rc)
-{
- struct lov_request_set *lovset = (struct lov_request_set *)data;
- int err;
- ENTRY;
-
- if (rc)
- cfs_atomic_set(&lovset->set_completes, 0);
-
- err = lov_fini_statfs_set(lovset);
- RETURN(rc ? rc : err);
-}
-
-static int lov_statfs_async(struct obd_export *exp, struct obd_info *oinfo,
- __u64 max_age, struct ptlrpc_request_set *rqset)
-{
- struct obd_device *obd = class_exp2obd(exp);
- struct lov_request_set *set;
- struct lov_request *req;
- cfs_list_t *pos;
- struct lov_obd *lov;
- int rc = 0;
- ENTRY;
-
- LASSERT(oinfo != NULL);
- LASSERT(oinfo->oi_osfs != NULL);
-
- lov = &obd->u.lov;
- rc = lov_prep_statfs_set(obd, oinfo, &set);
- if (rc)
- RETURN(rc);
-
- cfs_list_for_each (pos, &set->set_list) {
- req = cfs_list_entry(pos, struct lov_request, rq_link);
- rc = obd_statfs_async(lov->lov_tgts[req->rq_idx]->ltd_exp,
- &req->rq_oi, max_age, rqset);
- if (rc)
- break;
- }
-
- if (rc || cfs_list_empty(&rqset->set_requests)) {
- int err;
- if (rc)
- cfs_atomic_set(&set->set_completes, 0);
- err = lov_fini_statfs_set(set);
- RETURN(rc ? rc : err);
- }
-
- LASSERT(rqset->set_interpret == NULL);
- rqset->set_interpret = lov_statfs_interpret;
- rqset->set_arg = (void *)set;
- RETURN(0);
-}
-
-static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
- struct obd_statfs *osfs, __u64 max_age, __u32 flags)
-{
- struct ptlrpc_request_set *set = NULL;
- struct obd_info oinfo = { { { 0 } } };
- int rc = 0;
- ENTRY;
-
-
- /* for obdclass we forbid using obd_statfs_rqset, but prefer using async
- * statfs requests */
- set = ptlrpc_prep_set();
- if (set == NULL)
- RETURN(-ENOMEM);
-
- oinfo.oi_osfs = osfs;
- oinfo.oi_flags = flags;
- rc = lov_statfs_async(exp, &oinfo, max_age, set);
- if (rc == 0)
- rc = ptlrpc_set_wait(set);
- ptlrpc_set_destroy(set);
-
- RETURN(rc);
-}
-
-static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
- void *karg, void *uarg)
-{
- struct obd_device *obddev = class_exp2obd(exp);
- struct lov_obd *lov = &obddev->u.lov;
- int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
- struct obd_uuid *uuidp;
- ENTRY;
-
- switch (cmd) {
- case IOC_OBD_STATFS: {
- struct obd_ioctl_data *data = karg;
- struct obd_device *osc_obd;
- struct obd_statfs stat_buf = {0};
- __u32 index;
- __u32 flags;
-
- memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
- if ((index >= count))
- RETURN(-ENODEV);
-
- if (!lov->lov_tgts[index])
- /* Try again with the next index */
- RETURN(-EAGAIN);
- if (!lov->lov_tgts[index]->ltd_active)
- RETURN(-ENODATA);
-
- osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
- if (!osc_obd)
- RETURN(-EINVAL);
-
- /* copy UUID */
- if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
- min((int)data->ioc_plen2,
- (int)sizeof(struct obd_uuid))))
- RETURN(-EFAULT);
-
- flags = uarg ? *(__u32*)uarg : 0;
- /* got statfs data */
- rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
- cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
- flags);
- if (rc)
- RETURN(rc);
- if (copy_to_user(data->ioc_pbuf1, &stat_buf,
- min((int) data->ioc_plen1,
- (int) sizeof(stat_buf))))
- RETURN(-EFAULT);
- break;
- }
- case OBD_IOC_LOV_GET_CONFIG: {
- struct obd_ioctl_data *data;
- struct lov_desc *desc;
- char *buf = NULL;
- __u32 *genp;
-
- len = 0;
- if (obd_ioctl_getdata(&buf, &len, (void *)uarg))
- RETURN(-EINVAL);
-
- data = (struct obd_ioctl_data *)buf;
-
- if (sizeof(*desc) > data->ioc_inllen1) {
- obd_ioctl_freedata(buf, len);
- RETURN(-EINVAL);
- }
-
- if (sizeof(uuidp->uuid) * count > data->ioc_inllen2) {
- obd_ioctl_freedata(buf, len);
- RETURN(-EINVAL);
- }
-
- if (sizeof(__u32) * count > data->ioc_inllen3) {
- obd_ioctl_freedata(buf, len);
- RETURN(-EINVAL);
- }
-
- desc = (struct lov_desc *)data->ioc_inlbuf1;
- memcpy(desc, &(lov->desc), sizeof(*desc));
-
- uuidp = (struct obd_uuid *)data->ioc_inlbuf2;
- genp = (__u32 *)data->ioc_inlbuf3;
- /* the uuid will be empty for deleted OSTs */
- for (i = 0; i < count; i++, uuidp++, genp++) {
- if (!lov->lov_tgts[i])
- continue;
- *uuidp = lov->lov_tgts[i]->ltd_uuid;
- *genp = lov->lov_tgts[i]->ltd_gen;
- }
-
- if (copy_to_user((void *)uarg, buf, len))