Whamcloud - gitweb
LU-13719 lov: doesn't check lov_refcount
[fs/lustre-release.git] / lustre / lov / lov_obd.c
index 51e14f5..7972d62 100644 (file)
@@ -57,7 +57,7 @@
 
 /* Keep a refcount of lov->tgt usage to prevent racing with addition/deletion.
    Any function that expects lov_tgts to remain stationary must take a ref. */
-static void lov_getref(struct obd_device *obd)
+void lov_tgts_getref(struct obd_device *obd)
 {
        struct lov_obd *lov = &obd->u.lov;
 
@@ -65,19 +65,18 @@ static void lov_getref(struct obd_device *obd)
        mutex_lock(&lov->lov_lock);
        atomic_inc(&lov->lov_refcount);
        mutex_unlock(&lov->lov_lock);
-       return;
 }
 
 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt);
 
-static void lov_putref(struct obd_device *obd)
+void lov_tgts_putref(struct obd_device *obd)
 {
        struct lov_obd *lov = &obd->u.lov;
 
        mutex_lock(&lov->lov_lock);
        /* ok to dec to 0 more than once -- ltd_exp's will be null */
        if (atomic_dec_and_test(&lov->lov_refcount) && lov->lov_death_row) {
-               struct list_head kill = LIST_HEAD_INIT(kill);
+               LIST_HEAD(kill);
                struct lov_tgt_desc *tgt, *n;
                int i;
 
@@ -113,7 +112,7 @@ static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
 static int lov_notify(struct obd_device *obd, struct obd_device *watched,
                      enum obd_notify_event ev);
 
-int lov_connect_obd(struct obd_device *obd, u32 index, int activate,
+int lov_connect_osc(struct obd_device *obd, u32 index, int activate,
                    struct obd_connect_data *data)
 {
        struct lov_obd *lov = &obd->u.lov;
@@ -146,12 +145,12 @@ int lov_connect_obd(struct obd_device *obd, u32 index, int activate,
          */
         imp = tgt_obd->u.cli.cl_import;
 
-        if (activate) {
-                tgt_obd->obd_no_recov = 0;
-                /* FIXME this is probably supposed to be
-                   ptlrpc_set_import_active.  Horrible naming. */
-                ptlrpc_activate_import(imp);
-        }
+       if (activate) {
+               tgt_obd->obd_no_recov = 0;
+               /* FIXME this is probably supposed to be
+                  ptlrpc_set_import_active.  Horrible naming. */
+               ptlrpc_activate_import(imp, false);
+       }
 
         rc = obd_register_observer(tgt_obd, obd);
         if (rc) {
@@ -160,15 +159,14 @@ int lov_connect_obd(struct obd_device *obd, u32 index, int activate,
                 RETURN(rc);
         }
 
+       if (imp->imp_invalid) {
+               CDEBUG(D_CONFIG, "%s: not connecting - administratively disabled\n",
+                      obd_uuid2str(tgt_uuid));
+               RETURN(0);
+       }
 
-        if (imp->imp_invalid) {
-                CDEBUG(D_CONFIG, "not connecting OSC %s; administratively "
-                       "disabled\n", obd_uuid2str(tgt_uuid));
-                RETURN(0);
-        }
-
-        rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
-                         &lov_osc_uuid, data, NULL);
+       rc = obd_connect(NULL, &lov->lov_tgts[index]->ltd_exp, tgt_obd,
+                        &lov_osc_uuid, data, lov->lov_cache);
         if (rc || !lov->lov_tgts[index]->ltd_exp) {
                 CERROR("Target %s connect error %d\n",
                        obd_uuid2str(tgt_uuid), rc);
@@ -180,26 +178,17 @@ int lov_connect_obd(struct obd_device *obd, u32 index, int activate,
         CDEBUG(D_CONFIG, "Connected tgt idx %d %s (%s) %sactive\n", index,
                obd_uuid2str(tgt_uuid), tgt_obd->obd_name, activate ? "":"in");
 
-       if (lov->targets_proc_entry != NULL) {
-               struct proc_dir_entry *osc_symlink;
-               struct obd_device *osc_obd;
-
-               osc_obd = lov->lov_tgts[index]->ltd_exp->exp_obd;
-
-               LASSERT(osc_obd != NULL);
-               LASSERT(osc_obd->obd_magic == OBD_DEVICE_MAGIC);
-               LASSERT(osc_obd->obd_type->typ_name != NULL);
-
-               osc_symlink = lprocfs_add_symlink(osc_obd->obd_name,
-                                                 lov->targets_proc_entry,
-                                                 "../../../%s/%s",
-                                                 osc_obd->obd_type->typ_name,
-                                                 osc_obd->obd_name);
-               if (osc_symlink == NULL) {
-                       CERROR("cannot register LOV target "
-                              "/proc/fs/lustre/%s/%s/target_obds/%s\n",
-                              obd->obd_type->typ_name, obd->obd_name,
-                              osc_obd->obd_name);
+       if (lov->lov_tgts_kobj) {
+               /* Even if we failed, that's ok */
+               rc = sysfs_create_link(lov->lov_tgts_kobj,
+                                      &tgt_obd->obd_kset.kobj,
+                                      tgt_obd->obd_name);
+               if (rc) {
+                       CERROR("%s: can't register LOV target /sys/fs/lustre/%s/%s/target_obds/%s : rc = %d\n",
+                              obd->obd_name, obd->obd_type->typ_name,
+                              obd->obd_name,
+                              lov->lov_tgts[index]->ltd_exp->exp_obd->obd_name,
+                              rc);
                }
        }
        RETURN(0);
@@ -232,23 +221,19 @@ static int lov_connect(const struct lu_env *env,
         if (data)
                 lov->lov_ocd = *data;
 
-       lov->targets_proc_entry = lprocfs_register("target_obds",
-                                                  obd->obd_proc_entry,
-                                                  NULL, NULL);
-       if (IS_ERR(lov->targets_proc_entry)) {
-               CERROR("%s: cannot register "
-                      "/proc/fs/lustre/%s/%s/target_obds\n",
-                      obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
-               lov->targets_proc_entry = NULL;
+       lov_tgts_getref(obd);
+
+       if (localdata) {
+               lov->lov_cache = localdata;
+               cl_cache_incref(lov->lov_cache);
        }
 
-        obd_getref(obd);
-        for (i = 0; i < lov->desc.ld_tgt_count; i++) {
-                tgt = lov->lov_tgts[i];
-                if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
-                        continue;
-                /* Flags will be lowest common denominator */
-                rc = lov_connect_obd(obd, i, tgt->ltd_activate, &lov->lov_ocd);
+       for (i = 0; i < lov->desc.ld_tgt_count; i++) {
+               tgt = lov->lov_tgts[i];
+               if (!tgt || obd_uuid_empty(&tgt->ltd_uuid))
+                       continue;
+               /* Flags will be lowest common denominator */
+               rc = lov_connect_osc(obd, i, tgt->ltd_activate, &lov->lov_ocd);
                 if (rc) {
                         CERROR("%s: lov connect tgt %d failed: %d\n",
                                obd->obd_name, i, rc);
@@ -265,29 +250,34 @@ static int lov_connect(const struct lu_env *env,
                                obd->obd_name, rc);
                 }
         }
-        obd_putref(obd);
 
-        RETURN(0);
+       lov_tgts_putref(obd);
+
+       RETURN(0);
 }
 
 static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
 {
-        struct lov_obd *lov = &obd->u.lov;
-        struct obd_device *osc_obd;
-        int rc;
-        ENTRY;
+       struct lov_obd *lov = &obd->u.lov;
+       struct obd_device *osc_obd;
+       int rc;
+       ENTRY;
 
-        osc_obd = class_exp2obd(tgt->ltd_exp);
-        CDEBUG(D_CONFIG, "%s: disconnecting target %s\n",
-               obd->obd_name, osc_obd->obd_name);
+       osc_obd = class_exp2obd(tgt->ltd_exp);
+       CDEBUG(D_CONFIG, "%s: disconnecting target %s\n", obd->obd_name,
+              osc_obd ? osc_obd->obd_name : "<no obd>");
 
-        if (tgt->ltd_active) {
-                tgt->ltd_active = 0;
-                lov->desc.ld_active_tgt_count--;
-                tgt->ltd_exp->exp_obd->obd_inactive = 1;
-        }
+       if (tgt->ltd_active) {
+               tgt->ltd_active = 0;
+               lov->desc.ld_active_tgt_count--;
+               tgt->ltd_exp->exp_obd->obd_inactive = 1;
+       }
 
        if (osc_obd) {
+               if (lov->lov_tgts_kobj)
+                       sysfs_remove_link(lov->lov_tgts_kobj,
+                                         osc_obd->obd_name);
+
                /* Pass it on to our clients.
                 * XXX This should be an argument to disconnect,
                 * XXX not a back-door flag on the OBD.  Ah well.
@@ -301,17 +291,17 @@ static int lov_disconnect_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
                                                  lov->targets_proc_entry);
        }
 
-        obd_register_observer(osc_obd, NULL);
+       obd_register_observer(osc_obd, NULL);
 
-        rc = obd_disconnect(tgt->ltd_exp);
-        if (rc) {
-                CERROR("Target %s disconnect error %d\n",
-                       tgt->ltd_uuid.uuid, rc);
-                rc = 0;
-        }
+       rc = obd_disconnect(tgt->ltd_exp);
+       if (rc) {
+               CERROR("Target %s disconnect error %d\n",
+                      tgt->ltd_uuid.uuid, rc);
+               rc = 0;
+       }
 
-        tgt->ltd_exp = NULL;
-        RETURN(0);
+       tgt->ltd_exp = NULL;
+       RETURN(0);
 }
 
 static int lov_disconnect(struct obd_export *exp)
@@ -335,7 +325,7 @@ static int lov_disconnect(struct obd_export *exp)
        }
 
        /* hold another ref so lov_del_obd() doesn't spin in putref each time */
-       obd_getref(obd);
+       lov_tgts_getref(obd);
 
        for (index = 0; index < lov->desc.ld_tgt_count; index++) {
                if (lov->lov_tgts[index] && lov->lov_tgts[index]->ltd_exp) {
@@ -344,10 +334,7 @@ static int lov_disconnect(struct obd_export *exp)
                                       lov->lov_tgts[index]->ltd_gen);
                }
        }
-       obd_putref(obd);
-
-       if (lov->targets_proc_entry != NULL)
-               lprocfs_remove(&lov->targets_proc_entry);
+       lov_tgts_putref(obd);
 
 out:
        rc = class_disconnect(exp); /* bz 9811 */
@@ -372,30 +359,13 @@ static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
         CDEBUG(D_INFO, "Searching in lov %p for uuid %s event(%d)\n",
                lov, uuid->uuid, ev);
 
-       obd_getref(obd);
+       lov_tgts_getref(obd);
        for (index = 0; index < lov->desc.ld_tgt_count; index++) {
                tgt = lov->lov_tgts[index];
                if (!tgt)
                        continue;
-               /*
-                * LU-642, initially inactive OSC could miss the obd_connect,
-                * we make up for it here.
-                */
-               if (ev == OBD_NOTIFY_ACTIVATE && tgt->ltd_exp == NULL &&
-                   obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
-                       struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
-
-                       obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
-                                   &lov_osc_uuid, &lov->lov_ocd, NULL);
-               }
-               if (!tgt->ltd_exp)
-                       continue;
-
-               CDEBUG(D_INFO, "lov idx %d is %s conn %#llx\n",
-                       index, obd_uuid2str(&tgt->ltd_uuid),
-                       tgt->ltd_exp->exp_handle.h_cookie);
-                if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
-                        break;
+               if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
+                       break;
         }
 
         if (index == lov->desc.ld_tgt_count)
@@ -404,6 +374,21 @@ static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
         if (ev == OBD_NOTIFY_DEACTIVATE || ev == OBD_NOTIFY_ACTIVATE) {
                 activate = (ev == OBD_NOTIFY_ACTIVATE) ? 1 : 0;
 
+               /*
+                * LU-642, initially inactive OSC could miss the obd_connect,
+                * we make up for it here.
+                */
+               if (activate && !tgt->ltd_exp) {
+                       int rc;
+                       struct obd_uuid lov_osc_uuid = {"LOV_OSC_UUID"};
+
+                       rc = obd_connect(NULL, &tgt->ltd_exp, tgt->ltd_obd,
+                                        &lov_osc_uuid, &lov->lov_ocd,
+                                        lov->lov_cache);
+                       if (rc || tgt->ltd_exp == NULL)
+                               GOTO(out, index = rc);
+               }
+
                 if (lov->lov_tgts[index]->ltd_activate == activate) {
                         CDEBUG(D_INFO, "OSC %s already %sactivate!\n",
                                uuid->uuid, activate ? "" : "de");
@@ -438,8 +423,12 @@ static int lov_set_osc_active(struct obd_device *obd, struct obd_uuid *uuid,
                       ev, uuid->uuid);
        }
 
+       if (tgt->ltd_exp)
+               CDEBUG(D_INFO, "%s: lov idx %d conn %llx\n", obd_uuid2str(uuid),
+                      index, tgt->ltd_exp->exp_handle.h_cookie);
+
  out:
-       obd_putref(obd);
+       lov_tgts_putref(obd);
        RETURN(index);
 }
 
@@ -521,36 +510,36 @@ static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
                RETURN(rc);
        }
 
-        if (index >= lov->lov_tgt_size) {
-                /* We need to reallocate the lov target array. */
-                struct lov_tgt_desc **newtgts, **old = NULL;
-                __u32 newsize, oldsize = 0;
+       if (index >= lov->lov_tgt_size) {
+               /* We need to reallocate the lov target array. */
+               struct lov_tgt_desc **newtgts, **old = NULL;
+               __u32 newsize, oldsize = 0;
 
-                newsize = max(lov->lov_tgt_size, (__u32)2);
-                while (newsize < index + 1)
-                        newsize = newsize << 1;
-                OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
-                if (newtgts == NULL) {
+               newsize = max(lov->lov_tgt_size, 2U);
+               while (newsize < index + 1)
+                       newsize = newsize << 1;
+               OBD_ALLOC_PTR_ARRAY(newtgts, newsize);
+               if (newtgts == NULL) {
                        mutex_unlock(&lov->lov_lock);
-                        RETURN(-ENOMEM);
-                }
+                       RETURN(-ENOMEM);
+               }
 
-                if (lov->lov_tgt_size) {
-                        memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
-                               lov->lov_tgt_size);
-                        old = lov->lov_tgts;
-                        oldsize = lov->lov_tgt_size;
-                }
+               if (lov->lov_tgt_size) {
+                       memcpy(newtgts, lov->lov_tgts, sizeof(*newtgts) *
+                              lov->lov_tgt_size);
+                       old = lov->lov_tgts;
+                       oldsize = lov->lov_tgt_size;
+               }
 
                lov->lov_tgts = newtgts;
                lov->lov_tgt_size = newsize;
                smp_rmb();
                if (old)
-                       OBD_FREE(old, sizeof(*old) * oldsize);
+                       OBD_FREE_PTR_ARRAY(old, oldsize);
 
-                CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
-                       lov->lov_tgts, lov->lov_tgt_size);
-        }
+               CDEBUG(D_CONFIG, "tgts: %p size: %d\n",
+                      lov->lov_tgts, lov->lov_tgt_size);
+       }
 
         OBD_ALLOC_PTR(tgt);
         if (!tgt) {
@@ -580,16 +569,16 @@ static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
         CDEBUG(D_CONFIG, "idx=%d ltd_gen=%d ld_tgt_count=%d\n",
                 index, tgt->ltd_gen, lov->desc.ld_tgt_count);
 
-        if (lov->lov_connects == 0) {
-                /* lov_connect hasn't been called yet. We'll do the
-                   lov_connect_obd on this target when that fn first runs,
-                   because we don't know the connect flags yet. */
-                RETURN(0);
-        }
+       if (lov->lov_connects == 0) {
+               /* lov_connect hasn't been called yet. We'll do the
+                  lov_connect_osc on this target when that fn first runs,
+                  because we don't know the connect flags yet. */
+               RETURN(0);
+       }
 
-        obd_getref(obd);
+       lov_tgts_getref(obd);
 
-        rc = lov_connect_obd(obd, index, active, &lov->lov_ocd);
+       rc = lov_connect_osc(obd, index, active, &lov->lov_ocd);
         if (rc)
                 GOTO(out, rc);
 
@@ -597,15 +586,6 @@ static int lov_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
         if (!tgt->ltd_exp)
                 GOTO(out, rc = 0);
 
-       if (lov->lov_cache != NULL) {
-               rc = obd_set_info_async(NULL, tgt->ltd_exp,
-                               sizeof(KEY_CACHE_SET), KEY_CACHE_SET,
-                               sizeof(struct cl_client_cache), lov->lov_cache,
-                               NULL);
-               if (rc < 0)
-                       GOTO(out, rc);
-       }
-
        rc = lov_notify(obd, tgt->ltd_exp->exp_obd,
                        active ? OBD_NOTIFY_CONNECT : OBD_NOTIFY_INACTIVE);
 
@@ -615,7 +595,7 @@ out:
                       obd->obd_name, obd_uuid2str(&tgt->ltd_uuid), rc);
                lov_del_target(obd, index, NULL, 0);
        }
-       obd_putref(obd);
+       lov_tgts_putref(obd);
        RETURN(rc);
 }
 
@@ -636,7 +616,7 @@ int lov_del_target(struct obd_device *obd, u32 index,
 
        /* to make sure there's no ongoing lov_notify() now */
        down_write(&lov->lov_notify_lock);
-        obd_getref(obd);
+       lov_tgts_getref(obd);
 
         if (!lov->lov_tgts[index]) {
                 CERROR("LOV target at index %d is not setup.\n", index);
@@ -657,12 +637,12 @@ int lov_del_target(struct obd_device *obd, u32 index,
 
         lov->lov_tgts[index]->ltd_reap = 1;
         lov->lov_death_row++;
-        /* we really delete it from obd_putref */
+       /* we really delete it from lov_tgts_putref() */
 out:
-        obd_putref(obd);
+       lov_tgts_putref(obd);
        up_write(&lov->lov_notify_lock);
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 static void __lov_del_obd(struct obd_device *obd, struct lov_tgt_desc *tgt)
@@ -715,10 +695,10 @@ void lov_fix_desc_stripe_count(__u32 *val)
 void lov_fix_desc_pattern(__u32 *val)
 {
         /* from lov_setstripe */
-        if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
-                LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
-                *val = 0;
-        }
+       if ((*val != 0) && !lov_pattern_supported_normal_comp(*val)) {
+               LCONSOLE_WARN("lov: Unknown stripe pattern: %#x\n", *val);
+               *val = 0;
+       }
 }
 
 void lov_fix_desc_qos_maxage(__u32 *val)
@@ -739,9 +719,6 @@ int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
 {
        struct lov_desc *desc;
        struct lov_obd *lov = &obd->u.lov;
-#ifdef CONFIG_PROC_FS
-       struct obd_type *type;
-#endif
        int rc;
        ENTRY;
 
@@ -782,60 +759,23 @@ int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
 
        init_rwsem(&lov->lov_notify_lock);
 
-        lov->lov_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
-                                                   HASH_POOLS_MAX_BITS,
-                                                   HASH_POOLS_BKT_BITS, 0,
-                                                   CFS_HASH_MIN_THETA,
-                                                   CFS_HASH_MAX_THETA,
-                                                   &pool_hash_operations,
-                                                   CFS_HASH_DEFAULT);
        INIT_LIST_HEAD(&lov->lov_pool_list);
         lov->lov_pool_count = 0;
+       rc = lov_pool_hash_init(&lov->lov_pools_hash_body);
+       if (rc)
+               GOTO(out, rc);
+
         rc = lov_ost_pool_init(&lov->lov_packed, 0);
         if (rc)
                GOTO(out, rc);
 
-       obd->obd_vars = lprocfs_lov_obd_vars;
-#ifdef CONFIG_PROC_FS
-       /* If this is true then both client (lov) and server
-        * (lod) are on the same node. The lod layer if loaded
-        * first will register the lov proc directory. In that
-        * case obd->obd_type->typ_procroot will be not set.
-        * Instead we use type->typ_procsym as the parent.
-        */
-       type = class_search_type(LUSTRE_LOD_NAME);
-       if (type && type->typ_procsym) {
-               obd->obd_proc_entry = lprocfs_register(obd->obd_name,
-                                                      type->typ_procsym,
-                                                      obd->obd_vars, obd);
-               if (IS_ERR(obd->obd_proc_entry)) {
-                       rc = PTR_ERR(obd->obd_proc_entry);
-                       CERROR("error %d setting up lprocfs for %s\n", rc,
-                              obd->obd_name);
-                       obd->obd_proc_entry = NULL;
-               }
-       }
-#endif
-
-       rc = lprocfs_obd_setup(obd, false);
+       rc = lov_tunables_init(obd);
        if (rc)
                GOTO(out, rc);
 
-       rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd", 0444,
-                               &lov_proc_target_fops, obd);
-       if (rc)
-               CWARN("%s: Error adding the target_obd file : rc %d\n",
-                     obd->obd_name, rc);
-
-       lov->lov_pool_proc_entry = lprocfs_register("pools",
-                                                   obd->obd_proc_entry,
-                                                   NULL, NULL);
-       if (IS_ERR(lov->lov_pool_proc_entry)) {
-               rc = PTR_ERR(lov->lov_pool_proc_entry);
-               CERROR("%s: error setting up ldebugfs for pools : rc %d\n",
-                      obd->obd_name, rc);
-               lov->lov_pool_proc_entry = NULL;
-       }
+       lov->lov_tgts_kobj = kobject_create_and_add("target_obds",
+                                                   &obd->obd_kset.kobj);
+
 out:
        return rc;
 }
@@ -847,6 +787,11 @@ static int lov_cleanup(struct obd_device *obd)
         struct pool_desc *pool;
         ENTRY;
 
+       if (lov->lov_tgts_kobj) {
+               kobject_put(lov->lov_tgts_kobj);
+               lov->lov_tgts_kobj = NULL;
+       }
+
        list_for_each_safe(pos, tmp, &lov->lov_pool_list) {
                pool = list_entry(pos, struct pool_desc, pool_list);
                 /* free pool structs */
@@ -856,20 +801,19 @@ static int lov_cleanup(struct obd_device *obd)
                /* coverity[overrun-buffer-val] */
                 lov_pool_del(obd, pool->pool_name);
         }
-        cfs_hash_putref(lov->lov_pools_hash_body);
+       lov_pool_hash_destroy(&lov->lov_pools_hash_body);
         lov_ost_pool_free(&lov->lov_packed);
 
        lprocfs_obd_cleanup(obd);
         if (lov->lov_tgts) {
                 int i;
-                obd_getref(obd);
+               lov_tgts_getref(obd);
                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
                        if (!lov->lov_tgts[i])
                                continue;
 
                        /* Inactive targets may never have connected */
-                       if (lov->lov_tgts[i]->ltd_active ||
-                           atomic_read(&lov->lov_refcount))
+                       if (lov->lov_tgts[i]->ltd_active)
                                /* We should never get here - these
                                 * should have been removed in the
                                 * disconnect. */
@@ -879,11 +823,10 @@ static int lov_cleanup(struct obd_device *obd)
                                       atomic_read(&lov->lov_refcount));
                        lov_del_target(obd, i, NULL, 0);
                }
-                obd_putref(obd);
-                OBD_FREE(lov->lov_tgts, sizeof(*lov->lov_tgts) *
-                         lov->lov_tgt_size);
-                lov->lov_tgt_size = 0;
-        }
+               lov_tgts_putref(obd);
+               OBD_FREE_PTR_ARRAY(lov->lov_tgts, lov->lov_tgt_size);
+               lov->lov_tgt_size = 0;
+       }
 
        if (lov->lov_cache != NULL) {
                cl_cache_decref(lov->lov_cache);
@@ -936,15 +879,14 @@ int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
        }
        case LCFG_PARAM: {
                struct lov_desc *desc = &(obd->u.lov.desc);
+               ssize_t count;
 
                if (!desc)
                        GOTO(out, rc = -EINVAL);
 
-               rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
-                                             lcfg, obd);
-               if (rc > 0)
-                       rc = 0;
-                GOTO(out, rc);
+               count = class_modify_config(lcfg, PARAM_LOV,
+                                           &obd->obd_kset.kobj);
+               GOTO(out, rc = count < 0 ? count : 0);
         }
         case LCFG_POOL_NEW:
         case LCFG_POOL_ADD:
@@ -994,7 +936,7 @@ static int lov_statfs(const struct lu_env *env, struct obd_export *exp,
                        GOTO(out_set, rc);
        }
 
-       rc = ptlrpc_set_wait(rqset);
+       rc = ptlrpc_set_wait(env, rqset);
 
 out_set:
        if (rc < 0)
@@ -1013,35 +955,39 @@ out_rqset:
 static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                         void *karg, void __user *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;
+       struct obd_device *obd = class_exp2obd(exp);
+       struct lov_obd *lov = &obd->u.lov;
+       int i = 0, rc = 0, count = lov->desc.ld_tgt_count;
+       struct obd_uuid *uuidp;
 
-        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;
+       ENTRY;
+       switch (cmd) {
+       case IOC_OBD_STATFS: {
+               struct obd_ioctl_data *data = karg;
+               struct obd_device *osc_obd;
+               struct obd_statfs stat_buf = {0};
+               struct obd_import *imp;
+               __u32 index;
                __u32 flags;
 
-                memcpy(&index, data->ioc_inlbuf2, sizeof(index));
-                if ((index >= count))
-                        RETURN(-ENODEV);
+               memcpy(&index, data->ioc_inlbuf2, sizeof(index));
+               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);
+               if (!lov->lov_tgts[index])
+                       /* Try again with the next index */
+                       RETURN(-EAGAIN);
 
-                osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
-                if (!osc_obd)
-                        RETURN(-EINVAL);
+               osc_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
+               if (!osc_obd)
+                       RETURN(-EINVAL);
 
-                /* copy UUID */
+               imp = osc_obd->u.cli.cl_import;
+               if (!lov->lov_tgts[index]->ltd_active &&
+                   imp->imp_state != LUSTRE_IMP_IDLE)
+                       RETURN(-ENODATA);
+
+               /* copy UUID */
                if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(osc_obd),
                                 min_t(unsigned long, data->ioc_plen2,
                                       sizeof(struct obd_uuid))))
@@ -1050,12 +996,12 @@ static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                memcpy(&flags, data->ioc_inlbuf1, sizeof(flags));
                flags = flags & LL_STATFS_NODELAY ? OBD_STATFS_NODELAY : 0;
 
-                /* got statfs data */
-                rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
+               /* got statfs data */
+               rc = obd_statfs(NULL, lov->lov_tgts[index]->ltd_exp, &stat_buf,
                                ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
-                                flags);
-                if (rc)
-                        RETURN(rc);
+                               flags);
+               if (rc)
+                       RETURN(rc);
                if (copy_to_user(data->ioc_pbuf1, &stat_buf,
                                 min_t(unsigned long, data->ioc_plen1,
                                       sizeof(struct obd_statfs))))
@@ -1154,54 +1100,53 @@ static int lov_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
                 OBD_FREE_PTR(oqctl);
                 break;
         }
-        default: {
-                int set = 0;
+       default: {
+               int set = 0;
 
-                if (count == 0)
-                        RETURN(-ENOTTY);
+               if (count == 0)
+                       RETURN(-ENOTTY);
 
-                for (i = 0; i < count; i++) {
-                        int err;
-                        struct obd_device *osc_obd;
+               for (i = 0; i < count; i++) {
+                       int err;
+                       struct obd_device *osc_obd;
 
-                        /* OST was disconnected */
-                        if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
-                                continue;
+                       /* OST was disconnected */
+                       if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_exp)
+                               continue;
 
-                        /* ll_umount_begin() sets force flag but for lov, not
-                         * osc. Let's pass it through */
-                        osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
-                        osc_obd->obd_force = obddev->obd_force;
-                        err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
-                                            len, karg, uarg);
+                       /* ll_umount_begin() sets force on lov, pass to osc */
+                       osc_obd = class_exp2obd(lov->lov_tgts[i]->ltd_exp);
+                       if (osc_obd)
+                               osc_obd->obd_force = obd->obd_force;
+                       err = obd_iocontrol(cmd, lov->lov_tgts[i]->ltd_exp,
+                                           len, karg, uarg);
                        if (err) {
-                                if (lov->lov_tgts[i]->ltd_active) {
-                                        CDEBUG(err == -ENOTTY ?
-                                               D_IOCTL : D_WARNING,
-                                               "iocontrol OSC %s on OST "
-                                               "idx %d cmd %x: err = %d\n",
-                                               lov_uuid2str(lov, i),
-                                               i, cmd, err);
-                                        if (!rc)
-                                                rc = err;
-                                }
-                        } else {
-                                set = 1;
-                        }
-                }
-                if (!set && !rc)
-                        rc = -EIO;
-        }
-        }
+                               if (lov->lov_tgts[i]->ltd_active) {
+                                       CDEBUG(err == -ENOTTY ?
+                                              D_IOCTL : D_WARNING,
+                                              "iocontrol OSC %s on OST idx %d cmd %x: err = %d\n",
+                                              lov_uuid2str(lov, i),
+                                              i, cmd, err);
+                                       if (!rc)
+                                               rc = err;
+                               }
+                       } else {
+                               set = 1;
+                       }
+               }
+               if (!set && !rc)
+                       rc = -EIO;
+       }
+       }
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
                        __u32 keylen, void *key, __u32 *vallen, void *val)
 {
-       struct obd_device *obddev = class_exp2obd(exp);
-       struct lov_obd *lov = &obddev->u.lov;
+       struct obd_device *obd = class_exp2obd(exp);
+       struct lov_obd *lov = &obd->u.lov;
        struct lov_desc *ld = &lov->desc;
        int rc = 0;
        ENTRY;
@@ -1209,13 +1154,10 @@ static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
        if (vallen == NULL || val == NULL)
                RETURN(-EFAULT);
 
-       obd_getref(obddev);
+       lov_tgts_getref(obd);
 
        if (KEY_IS(KEY_MAX_EASIZE)) {
-               u32 max_stripe_count = min_t(u32, ld->ld_active_tgt_count,
-                                            LOV_MAX_STRIPE_COUNT);
-
-               *((u32 *)val) = lov_mds_md_size(max_stripe_count, LOV_MAGIC_V3);
+               *((u32 *)val) = exp->exp_connect_data.ocd_max_easize;
        } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
                u32 def_stripe_count = min_t(u32, ld->ld_default_stripe_count,
                                             LOV_MAX_STRIPE_COUNT);
@@ -1227,7 +1169,7 @@ static int lov_get_info(const struct lu_env *env, struct obd_export *exp,
                rc = -EINVAL;
        }
 
-       obd_putref(obddev);
+       lov_tgts_putref(obd);
 
        RETURN(rc);
 }
@@ -1237,8 +1179,8 @@ static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
                              __u32 vallen, void *val,
                              struct ptlrpc_request_set *set)
 {
-       struct obd_device *obddev = class_exp2obd(exp);
-       struct lov_obd *lov = &obddev->u.lov;
+       struct obd_device *obd = class_exp2obd(exp);
+       struct lov_obd *lov = &obd->u.lov;
        struct lov_tgt_desc *tgt;
        bool do_inactive = false, no_set = false;
        u32 i;
@@ -1254,16 +1196,10 @@ static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
                        RETURN(-ENOMEM);
        }
 
-       obd_getref(obddev);
+       lov_tgts_getref(obd);
 
-       if (KEY_IS(KEY_CHECKSUM)) {
-               do_inactive = true;
-       } else if (KEY_IS(KEY_CACHE_SET)) {
-               LASSERT(lov->lov_cache == NULL);
-               lov->lov_cache = val;
+       if (KEY_IS(KEY_CHECKSUM))
                do_inactive = true;
-               cl_cache_incref(lov->lov_cache);
-       }
 
        for (i = 0; i < lov->desc.ld_tgt_count; i++) {
                tgt = lov->lov_tgts[i];
@@ -1297,9 +1233,9 @@ static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
                        rc = err;
        }
 
-       obd_putref(obddev);
+       lov_tgts_putref(obd);
        if (no_set) {
-               err = ptlrpc_set_wait(set);
+               err = ptlrpc_set_wait(env, set);
                if (rc == 0)
                        rc = err;
                ptlrpc_set_destroy(set);
@@ -1310,39 +1246,51 @@ static int lov_set_info_async(const struct lu_env *env, struct obd_export *exp,
 void lov_stripe_lock(struct lov_stripe_md *md)
 __acquires(&md->lsm_lock)
 {
-       LASSERT(md->lsm_lock_owner != current_pid());
+       LASSERT(md->lsm_lock_owner != current->pid);
        spin_lock(&md->lsm_lock);
        LASSERT(md->lsm_lock_owner == 0);
-       md->lsm_lock_owner = current_pid();
+       md->lsm_lock_owner = current->pid;
 }
 
 void lov_stripe_unlock(struct lov_stripe_md *md)
 __releases(&md->lsm_lock)
 {
-       LASSERT(md->lsm_lock_owner == current_pid());
+       LASSERT(md->lsm_lock_owner == current->pid);
        md->lsm_lock_owner = 0;
        spin_unlock(&md->lsm_lock);
 }
 
 static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
-                        struct obd_quotactl *oqctl)
+                       struct obd_quotactl *oqctl)
 {
-        struct lov_obd      *lov = &obd->u.lov;
-        struct lov_tgt_desc *tgt;
-        __u64                curspace = 0;
-        __u64                bhardlimit = 0;
-        int                  i, rc = 0;
-        ENTRY;
+       struct lov_obd *lov = &obd->u.lov;
+       struct lov_tgt_desc *tgt;
+       struct pool_desc *pool = NULL;
+       __u64 curspace = 0;
+       __u64 bhardlimit = 0;
+       int i, rc = 0;
 
+       ENTRY;
        if (oqctl->qc_cmd != Q_GETOQUOTA &&
-           oqctl->qc_cmd != LUSTRE_Q_SETQUOTA) {
-               CERROR("%s: bad quota opc %x for lov obd\n",
-                      obd->obd_name, oqctl->qc_cmd);
-               RETURN(-EFAULT);
+           oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
+           oqctl->qc_cmd != LUSTRE_Q_GETQUOTAPOOL) {
+               rc = -EFAULT;
+               CERROR("%s: bad quota opc %x for lov obd: rc = %d\n",
+                      obd->obd_name, oqctl->qc_cmd, rc);
+               RETURN(rc);
+       }
+
+       if (oqctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
+               pool = lov_pool_find(obd, oqctl->qc_poolname);
+               if (!pool)
+                       RETURN(-ENOENT);
+               /* Set Q_GETOQUOTA back as targets report it's own
+                * usage and doesn't care about pools */
+               oqctl->qc_cmd = Q_GETOQUOTA;
        }
 
         /* for lov tgt */
-        obd_getref(obd);
+       lov_tgts_getref(obd);
         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
                 int err;
 
@@ -1351,16 +1299,21 @@ static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
                 if (!tgt)
                         continue;
 
-                if (!tgt->ltd_active || tgt->ltd_reap) {
-                        if (oqctl->qc_cmd == Q_GETOQUOTA &&
-                            lov->lov_tgts[i]->ltd_activate) {
+               if (pool &&
+                   tgt_check_index(tgt->ltd_index, &pool->pool_obds))
+                       continue;
+
+               if (!tgt->ltd_active || tgt->ltd_reap) {
+                       if (oqctl->qc_cmd == Q_GETOQUOTA &&
+                           lov->lov_tgts[i]->ltd_activate) {
                                rc = -ENETDOWN;
-                                CERROR("ost %d is inactive\n", i);
-                        } else {
-                                CDEBUG(D_HA, "ost %d is inactive\n", i);
-                        }
-                        continue;
-                }
+                               CERROR("%s: ost %d is inactive: rc = %d\n",
+                                      obd->obd_name, i, rc);
+                       } else {
+                               CDEBUG(D_HA, "ost %d is inactive\n", i);
+                       }
+                       continue;
+               }
 
                 err = obd_quotactl(tgt->ltd_exp, oqctl);
                 if (err) {
@@ -1374,7 +1327,9 @@ static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
                 }
         }
-        obd_putref(obd);
+       lov_tgts_putref(obd);
+       if (pool)
+               lov_pool_putref(pool);
 
         if (oqctl->qc_cmd == Q_GETOQUOTA) {
                 oqctl->qc_dqblk.dqb_curspace = curspace;
@@ -1383,7 +1338,7 @@ static int lov_quotactl(struct obd_device *obd, struct obd_export *exp,
         RETURN(rc);
 }
 
-static struct obd_ops lov_obd_ops = {
+static const struct obd_ops lov_obd_ops = {
        .o_owner                = THIS_MODULE,
        .o_setup                = lov_setup,
        .o_cleanup              = lov_cleanup,
@@ -1398,8 +1353,6 @@ static struct obd_ops lov_obd_ops = {
        .o_pool_rem             = lov_pool_remove,
        .o_pool_add             = lov_pool_add,
        .o_pool_del             = lov_pool_del,
-       .o_getref               = lov_getref,
-       .o_putref               = lov_putref,
        .o_quotactl             = lov_quotactl,
 };
 
@@ -1407,8 +1360,6 @@ struct kmem_cache *lov_oinfo_slab;
 
 static int __init lov_init(void)
 {
-       bool enable_proc = true;
-       struct obd_type *type;
        int rc;
        ENTRY;
 
@@ -1429,13 +1380,8 @@ static int __init lov_init(void)
                 return -ENOMEM;
         }
 
-       type = class_search_type(LUSTRE_LOD_NAME);
-       if (type != NULL && type->typ_procsym != NULL)
-               enable_proc = false;
-
-       rc = class_register_type(&lov_obd_ops, NULL, enable_proc, NULL,
+       rc = class_register_type(&lov_obd_ops, NULL, true, NULL,
                                 LUSTRE_LOV_NAME, &lov_device_type);
-
         if (rc) {
                kmem_cache_destroy(lov_oinfo_slab);
                 lu_kmem_fini(lov_caches);