-static int mdt_server_data_update(const struct lu_env *env,
- struct mdt_device *mdt)
-{
- struct mdt_thread_info *mti;
- struct thandle *th;
- int rc;
-
- mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-
- mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
- th = mdt_trans_start(env, mdt);
- if (IS_ERR(th))
- RETURN(PTR_ERR(th));
-
- CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
- mdt->mdt_mount_count, mdt->mdt_last_transno);
-
- cfs_spin_lock(&mdt->mdt_transno_lock);
- mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
- cfs_spin_unlock(&mdt->mdt_transno_lock);
-
- rc = mdt_last_rcvd_header_write(env, mdt, th);
- mdt_trans_stop(env, mdt, th);
- return rc;
-}
-
-
-int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
-{
- unsigned long *bitmap = mdt->mdt_client_bitmap;
- struct mdt_thread_info *mti;
- struct mdt_export_data *med;
- struct lsd_client_data *lcd;
- struct lr_server_data *lsd = &mdt->mdt_lsd;
- struct obd_device *obd = mdt2obd_dev(mdt);
- struct thandle *th;
- loff_t off;
- int rc;
- int cl_idx;
- ENTRY;
-
- mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
- LASSERT(mti != NULL);
-
- med = &mti->mti_exp->exp_mdt_data;
- lcd = med->med_lcd;
-
- LASSERT(bitmap != NULL);
- if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
- RETURN(0);
-
- /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
- * there's no need for extra complication here
- */
- cfs_spin_lock(&mdt->mdt_client_bitmap_lock);
- cl_idx = cfs_find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
- if (cl_idx >= LR_MAX_CLIENTS ||
- OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
- CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
- cl_idx);
- cfs_spin_unlock(&mdt->mdt_client_bitmap_lock);
- RETURN(-EOVERFLOW);
- }
- cfs_set_bit(cl_idx, bitmap);
- cfs_spin_unlock(&mdt->mdt_client_bitmap_lock);
-
- CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
- cl_idx, med->med_lcd->lcd_uuid);
-
- med->med_lr_idx = cl_idx;
- med->med_lr_off = lsd->lsd_client_start +
- (cl_idx * lsd->lsd_client_size);
- cfs_init_mutex(&med->med_lcd_lock);
-
- LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
-
- /* Write new client data. */
- off = med->med_lr_off;
-
- if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
- RETURN(-ENOSPC);
-
- mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
-
- th = mdt_trans_start(env, mdt);
- if (IS_ERR(th))
- RETURN(PTR_ERR(th));
-
- /*
- * Until this operations will be committed the sync is needed
- * for this export. This should be done _after_ starting the
- * transaction so that many connecting clients will not bring
- * server down with lots of sync writes.
- */
- mdt_trans_add_cb(th, lut_cb_client, class_export_cb_get(mti->mti_exp));
- cfs_spin_lock(&mti->mti_exp->exp_lock);
- mti->mti_exp->exp_need_sync = 1;
- cfs_spin_unlock(&mti->mti_exp->exp_lock);
-
- rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
- CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len %u)\n",
- cl_idx, med->med_lr_off, (int)sizeof(*lcd));
- mdt_trans_stop(env, mdt, th);
-
- RETURN(rc);
-}
-
-/* Add client data to the MDS. We use a bitmap to locate a free space
- * in the last_rcvd file if cl_off is -1 (i.e. a new client).
- * Otherwise, we just have to read the data from the last_rcvd file and
- * we know its offset.
- *
- * It should not be possible to fail adding an existing client - otherwise
- * mdt_init_server_data() callsite needs to be fixed.
- */
-int mdt_client_add(const struct lu_env *env,
- struct mdt_device *mdt, int cl_idx)
-{
- struct mdt_thread_info *mti;
- struct mdt_export_data *med;
- unsigned long *bitmap = mdt->mdt_client_bitmap;
- struct obd_device *obd = mdt2obd_dev(mdt);
- struct lr_server_data *lsd = &mdt->mdt_lsd;
- int rc = 0;
- ENTRY;
-
- mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
- LASSERT(mti != NULL);
-
- med = &mti->mti_exp->exp_mdt_data;
-
- LASSERT(bitmap != NULL);
- LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
-
- if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
- RETURN(0);
-
- cfs_spin_lock(&mdt->mdt_client_bitmap_lock);
- if (cfs_test_and_set_bit(cl_idx, bitmap)) {
- CERROR("MDS client %d: bit already set in bitmap!!\n",
- cl_idx);
- LBUG();
- }
- cfs_spin_unlock(&mdt->mdt_client_bitmap_lock);
-
- CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
- cl_idx, med->med_lcd->lcd_uuid);
-
- med->med_lr_idx = cl_idx;
- med->med_lr_off = lsd->lsd_client_start +
- (cl_idx * lsd->lsd_client_size);
- cfs_init_mutex(&med->med_lcd_lock);
-
- LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
-
- RETURN(rc);
-}
-
-int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
-{
- struct mdt_thread_info *mti;
- struct mdt_export_data *med;
- struct lsd_client_data *lcd;
- struct obd_device *obd = mdt2obd_dev(mdt);
- struct obd_export *exp;
- struct thandle *th;
- loff_t off;
- int rc = 0;
- ENTRY;
-
- mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
- LASSERT(mti != NULL);
-
- exp = mti->mti_exp;
- med = &exp->exp_mdt_data;
- lcd = med->med_lcd;
- if (!lcd)
- RETURN(0);
-
- /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
- if (!strcmp(lcd->lcd_uuid, obd->obd_uuid.uuid))
- GOTO(free, 0);
-
- CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
- med->med_lr_idx, med->med_lr_off);
-
- off = med->med_lr_off;
-
- /*
- * Don't clear med_lr_idx here as it is likely also unset. At worst we
- * leak a client slot that will be cleaned on the next recovery.
- */
- if (off <= 0) {
- CERROR("client idx %d has offset %lld\n",
- med->med_lr_idx, off);
- GOTO(free, rc = -EINVAL);
- }
-
- /*
- * Clear the bit _after_ zeroing out the client so we don't race with
- * mdt_client_add and zero out new clients.
- */
- if (!cfs_test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
- CERROR("MDT client %u: bit already clear in bitmap!!\n",
- med->med_lr_idx);
- LBUG();
- }
-
- /* Make sure the server's last_transno is up to date.
- * This should be done before zeroing client slot so last_transno will
- * be in server data or in client data in case of failure */
- mdt_server_data_update(env, mdt);
-
- mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
- th = mdt_trans_start(env, mdt);
- if (IS_ERR(th))
- GOTO(free, rc = PTR_ERR(th));
-
- cfs_mutex_down(&med->med_lcd_lock);
- memset(lcd->lcd_uuid, 0, sizeof lcd->lcd_uuid);
- rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
- cfs_mutex_up(&med->med_lcd_lock);
- mdt_trans_stop(env, mdt, th);
-
- CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
- "%s, rc %d\n", med->med_lr_idx, LAST_RCVD, rc);
- RETURN(0);
-free:
- return 0;
-}
-