ia64/xen-unstable

changeset 15233:1bad5a932df5

Fix xenstore entry accounting

xenstored is incorrectly accounting domain nodes when transactions
fail. Store pending count changes in the transaction structure, and
apply at transaction completion, instead of directly applying the
changes.

Signed-off-by: Max Zhen <max.zhen@sun.com>
author kfraser@localhost.localdomain
date Wed May 30 10:04:23 2007 +0100 (2007-05-30)
parents 33242afccb32
children 152dc0d812b2
files tools/xenstore/xenstored_domain.c tools/xenstore/xenstored_domain.h tools/xenstore/xenstored_transaction.c tools/xenstore/xenstored_transaction.h
line diff
     1.1 --- a/tools/xenstore/xenstored_domain.c	Wed May 30 09:59:09 2007 +0100
     1.2 +++ b/tools/xenstore/xenstored_domain.c	Wed May 30 10:04:23 2007 +0100
     1.3 @@ -576,12 +576,21 @@ void domain_entry_inc(struct connection 
     1.4  		return;
     1.5  
     1.6  	if (node->perms && node->perms[0].id != conn->id) {
     1.7 -		d = find_domain_by_domid(node->perms[0].id);
     1.8 -		if (d)
     1.9 -			d->nbentry++;
    1.10 -	}
    1.11 -	else if (conn->domain) {
    1.12 -		conn->domain->nbentry++;
    1.13 +		if (conn->transaction) {
    1.14 +			transaction_entry_inc(conn->transaction,
    1.15 +				node->perms[0].id);
    1.16 +		} else {
    1.17 +			d = find_domain_by_domid(node->perms[0].id);
    1.18 +			if (d)
    1.19 +				d->nbentry++;
    1.20 +		}
    1.21 +	} else if (conn->domain) {
    1.22 +		if (conn->transaction) {
    1.23 +			transaction_entry_inc(conn->transaction,
    1.24 +				conn->domain->domid);
    1.25 + 		} else {
    1.26 + 			conn->domain->nbentry++;
    1.27 +		}
    1.28  	}
    1.29  }
    1.30  
    1.31 @@ -593,11 +602,36 @@ void domain_entry_dec(struct connection 
    1.32  		return;
    1.33  
    1.34  	if (node->perms && node->perms[0].id != conn->id) {
    1.35 -		d = find_domain_by_domid(node->perms[0].id);
    1.36 -		if (d && d->nbentry)
    1.37 -			d->nbentry--;
    1.38 -	} else if (conn->domain && conn->domain->nbentry)
    1.39 -		conn->domain->nbentry--;
    1.40 +		if (conn->transaction) {
    1.41 +			transaction_entry_dec(conn->transaction,
    1.42 +				node->perms[0].id);
    1.43 +		} else {
    1.44 +			d = find_domain_by_domid(node->perms[0].id);
    1.45 +			if (d && d->nbentry)
    1.46 +				d->nbentry--;
    1.47 +		}
    1.48 +	} else if (conn->domain && conn->domain->nbentry) {
    1.49 +		if (conn->transaction) {
    1.50 +			transaction_entry_dec(conn->transaction,
    1.51 +				conn->domain->domid);
    1.52 +		} else {
    1.53 +			conn->domain->nbentry--;
    1.54 +		}
    1.55 +	}
    1.56 +}
    1.57 +
    1.58 +void domain_entry_fix(unsigned int domid, int num)
    1.59 +{
    1.60 +	struct domain *d;
    1.61 +
    1.62 +	d = find_domain_by_domid(domid);
    1.63 +	if (d) {
    1.64 +		if ((d->nbentry += num) < 0) {
    1.65 +			eprintf("invalid domain entry number %d",
    1.66 +				d->nbentry);
    1.67 +			d->nbentry = 0;
    1.68 +		}
    1.69 +	}
    1.70  }
    1.71  
    1.72  int domain_entry(struct connection *conn)
     2.1 --- a/tools/xenstore/xenstored_domain.h	Wed May 30 09:59:09 2007 +0100
     2.2 +++ b/tools/xenstore/xenstored_domain.h	Wed May 30 10:04:23 2007 +0100
     2.3 @@ -55,6 +55,7 @@ bool domain_is_unprivileged(struct conne
     2.4  /* Quota manipulation */
     2.5  void domain_entry_inc(struct connection *conn, struct node *);
     2.6  void domain_entry_dec(struct connection *conn, struct node *);
     2.7 +void domain_entry_fix(unsigned int domid, int num);
     2.8  int domain_entry(struct connection *conn);
     2.9  void domain_watch_inc(struct connection *conn);
    2.10  void domain_watch_dec(struct connection *conn);
     3.1 --- a/tools/xenstore/xenstored_transaction.c	Wed May 30 09:59:09 2007 +0100
     3.2 +++ b/tools/xenstore/xenstored_transaction.c	Wed May 30 10:04:23 2007 +0100
     3.3 @@ -32,6 +32,7 @@
     3.4  #include "list.h"
     3.5  #include "xenstored_transaction.h"
     3.6  #include "xenstored_watch.h"
     3.7 +#include "xenstored_domain.h"
     3.8  #include "xs_lib.h"
     3.9  #include "utils.h"
    3.10  #include "xenstored_test.h"
    3.11 @@ -48,6 +49,18 @@ struct changed_node
    3.12  	bool recurse;
    3.13  };
    3.14  
    3.15 +struct changed_domain
    3.16 +{
    3.17 +	/* List of all changed domains in the context of this transaction. */
    3.18 +	struct list_head list;
    3.19 +
    3.20 +	/* Identifier of the changed domain. */
    3.21 +	unsigned int domid;
    3.22 +
    3.23 +	/* Amount by which this domain's nbentry field has changed. */
    3.24 +	int nbentry;
    3.25 +};
    3.26 +
    3.27  struct transaction
    3.28  {
    3.29  	/* List of all transactions active on this connection. */
    3.30 @@ -65,6 +78,9 @@ struct transaction
    3.31  
    3.32  	/* List of changed nodes. */
    3.33  	struct list_head changes;
    3.34 +
    3.35 +	/* List of changed domains - to record the changed domain entry number */
    3.36 +	struct list_head changed_domains;
    3.37  };
    3.38  
    3.39  extern int quota_max_transaction;
    3.40 @@ -142,6 +158,7 @@ void do_transaction_start(struct connect
    3.41  	/* Attach transaction to input for autofree until it's complete */
    3.42  	trans = talloc(in, struct transaction);
    3.43  	INIT_LIST_HEAD(&trans->changes);
    3.44 +	INIT_LIST_HEAD(&trans->changed_domains);
    3.45  	trans->generation = generation;
    3.46  	trans->tdb_name = talloc_asprintf(trans, "%s.%p",
    3.47  					  xs_daemon_tdb(), trans);
    3.48 @@ -172,6 +189,7 @@ void do_transaction_start(struct connect
    3.49  void do_transaction_end(struct connection *conn, const char *arg)
    3.50  {
    3.51  	struct changed_node *i;
    3.52 +	struct changed_domain *d;
    3.53  	struct transaction *trans;
    3.54  
    3.55  	if (!arg || (!streq(arg, "T") && !streq(arg, "F"))) {
    3.56 @@ -204,6 +222,10 @@ void do_transaction_end(struct connectio
    3.57  		/* Don't close this: we won! */
    3.58  		trans->tdb = NULL;
    3.59  
    3.60 +		/* fix domain entry for each changed domain */
    3.61 +		list_for_each_entry(d, &trans->changed_domains, list)
    3.62 +			domain_entry_fix(d->domid, d->nbentry);
    3.63 +
    3.64  		/* Fire off the watches for everything that changed. */
    3.65  		list_for_each_entry(i, &trans->changes, list)
    3.66  			fire_watches(conn, i->node, i->recurse);
    3.67 @@ -212,6 +234,38 @@ void do_transaction_end(struct connectio
    3.68  	send_ack(conn, XS_TRANSACTION_END);
    3.69  }
    3.70  
    3.71 +void transaction_entry_inc(struct transaction *trans, unsigned int domid)
    3.72 +{
    3.73 +	struct changed_domain *d;
    3.74 +
    3.75 +	list_for_each_entry(d, &trans->changed_domains, list)
    3.76 +		if (d->domid == domid) {
    3.77 +			d->nbentry++;
    3.78 +			return;
    3.79 +		}
    3.80 +
    3.81 +	d = talloc(trans, struct changed_domain);
    3.82 +	d->domid = domid;
    3.83 +	d->nbentry = 1;
    3.84 +	list_add_tail(&d->list, &trans->changed_domains);
    3.85 +}
    3.86 +
    3.87 +void transaction_entry_dec(struct transaction *trans, unsigned int domid)
    3.88 +{
    3.89 +	struct changed_domain *d;
    3.90 +
    3.91 +	list_for_each_entry(d, &trans->changed_domains, list)
    3.92 +		if (d->domid == domid) {
    3.93 +			d->nbentry--;
    3.94 +			return;
    3.95 +		}
    3.96 +
    3.97 +	d = talloc(trans, struct changed_domain);
    3.98 +	d->domid = domid;
    3.99 +	d->nbentry = -1;
   3.100 +	list_add_tail(&d->list, &trans->changed_domains);
   3.101 +}
   3.102 +
   3.103  void conn_delete_all_transactions(struct connection *conn)
   3.104  {
   3.105  	struct transaction *trans;
     4.1 --- a/tools/xenstore/xenstored_transaction.h	Wed May 30 09:59:09 2007 +0100
     4.2 +++ b/tools/xenstore/xenstored_transaction.h	Wed May 30 10:04:23 2007 +0100
     4.3 @@ -27,6 +27,10 @@ void do_transaction_end(struct connectio
     4.4  
     4.5  struct transaction *transaction_lookup(struct connection *conn, uint32_t id);
     4.6  
     4.7 +/* inc/dec entry number local to trans while changing a node */
     4.8 +void transaction_entry_inc(struct transaction *trans, unsigned int domid);
     4.9 +void transaction_entry_dec(struct transaction *trans, unsigned int domid);
    4.10 +
    4.11  /* This node was changed: can fail and longjmp. */
    4.12  void add_change_node(struct transaction *trans, const char *node,
    4.13                       bool recurse);