Skip to contentSkip to conversation

cifs: second pile of cleanups for cifsacl

PATCH In review Jeff Layton 2 participants 21 messages Oct 19, 2012 – Nov 26, 2012 10 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover cifs: second pile of cleanups for cifsacl

This patchset is a respin of the second pile of cleanups for the cifsacl code. Most of the changes to the already-sent patches are pretty minor cleanups and such.

The last two patches in the series are new. The first adds a timeout to the idmap keys so that they can eventually work their way out of the cache. The second fixes an existing bug that caused "ls -l" to show the ownership wrong when cifsacl is used.

With this set, everything seems to work as expected. Steve, could you pull these into your for-next branch soon so we can get them some testing exposure?

Jeff Layton (9):
cifs: make cifs_copy_sid handle a source sid with variable size
subauth arrays
cifs: redefine NUM_SUBAUTH constant from 5 to 15
cifs: fix the format specifiers in sid_to_str
cifs: remove uneeded __KERNEL__ block from cifsacl.h
cifs: simplify id_to_sid and sid_to_id mapping code
cifs: avoid extra allocation for small cifs.idmap keys
cifs: don't override the uid/gid in getattr when cifsacl is enabled
cifs: set a timeout on keys in cifs.idmap cache
cifs: ensure we revalidate the inode after readdir if cifsacl is
enabled

 fs/cifs/cifsacl.c   | 573 ++++++++++++----------------------------------------
 fs/cifs/cifsacl.h   |  49 ++---
 fs/cifs/cifsfs.c    |   1 -
 fs/cifs/cifsproto.h |   1 -
 fs/cifs/inode.c     |   7 +-
 fs/cifs/readdir.c   |  10 +
 6 files changed, 161 insertions(+), 480 deletions(-)
Signature · 1 line
1.7.11.7
#2 lore raw

PATCH v2 1/9 cifs: make cifs_copy_sid handle a source sid with variable size subauth arrays

In reply to a message not in the archive

...and lift the restriction in id_to_sid upcall that the size must be at least as big as a full cifs_sid.

---
 fs/cifs/cifsacl.c | 10 ++++++++--
 fs/cifs/cifsacl.h |  3 +++
 2 files changed, 11 insertions(+), 2 deletions(-)
fs/cifs/cifsacl.c +8 -2
index 5a312eb..141a944 100644
@@ -277,8 +277,14 @@ compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid)
 static void
 cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
 {
-	memcpy(dst, src, sizeof(*dst));
+	int i;
+
+	dst->revision = src->revision;
 	dst->num_subauth = min_t(u8, src->num_subauth, NUM_SUBAUTHS);
+	for (i = 0; i < NUM_AUTHS; ++i)
+		dst->authority[i] = src->authority[i];
+	for (i = 0; i < dst->num_subauth; ++i)
+		dst->sub_auth[i] = src->sub_auth[i];
 }
 
 static void
@@ -427,7 +433,7 @@ id_to_sid(unsigned long cid, uint sidtype, struct cifs_sid *ssid)
 		if (IS_ERR(sidkey)) {
 			rc = -EINVAL;
 			cFYI(1, "%s: Can't map and id to a SID", __func__);
-		} else if (sidkey->datalen < sizeof(struct cifs_sid)) {
+		} else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
 			rc = -EIO;
 			cFYI(1, "%s: Downcall contained malformed key "
 				"(datalen=%hu)", __func__, sidkey->datalen);
fs/cifs/cifsacl.h +3 -0
index 18c7521..7e52f19 100644
@@ -64,6 +64,9 @@ struct cifs_sid {
 	__le32 sub_auth[NUM_SUBAUTHS]; /* sub_auth[num_subauth] */
 } __attribute__((packed));
 
+/* size of a struct cifs_sid, sans sub_auth array */
+#define CIFS_SID_BASE_SIZE (1 + 1 + NUM_AUTHS)
+
 struct cifs_acl {
 	__le16 revision; /* revision level */
 	__le16 size;
Signature · 1 line
1.7.11.7
#3 lore raw

PATCH v2 2/9 cifs: redefine NUM_SUBAUTH constant from 5 to 15

In reply to a message not in the archive

According to several places on the Internet and the samba winbind code, this is hard limited to 15 in windows, not 5. This does balloon out the allocation of each by 40 bytes, but I don't see any alternative.

Also, rename it to SID_MAX_SUB_AUTHORITIES to match the alleged name of this constant in the windows header files

Finally, rename SIDLEN to SID_STRING_MAX, fix the value to reflect the change to SID_MAX_SUB_AUTHORITIES and document how it was determined.

---
 fs/cifs/cifsacl.c |  6 +++---
 fs/cifs/cifsacl.h | 19 ++++++++++++++++---
 2 files changed, 19 insertions(+), 6 deletions(-)
fs/cifs/cifsacl.c +3 -3
index 141a944..dd8d3df 100644
@@ -280,7 +280,7 @@ cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
 	int i;
 
 	dst->revision = src->revision;
-	dst->num_subauth = min_t(u8, src->num_subauth, NUM_SUBAUTHS);
+	dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES);
 	for (i = 0; i < NUM_AUTHS; ++i)
 		dst->authority[i] = src->authority[i];
 	for (i = 0; i < dst->num_subauth; ++i)
@@ -383,7 +383,7 @@ id_to_sid(unsigned long cid, uint sidtype, struct cifs_sid *ssid)
 		if (!npsidid)
 			return -ENOMEM;
 
-		npsidid->sidstr = kmalloc(SIDLEN, GFP_KERNEL);
+		npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
 		if (!npsidid->sidstr) {
 			kfree(npsidid);
 			return -ENOMEM;
@@ -500,7 +500,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 		if (!npsidid)
 			return -ENOMEM;
 
-		npsidid->sidstr = kmalloc(SIDLEN, GFP_KERNEL);
+		npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
 		if (!npsidid->sidstr) {
 			kfree(npsidid);
 			return -ENOMEM;
fs/cifs/cifsacl.h +16 -3
index 7e52f19..8b980cd 100644
@@ -24,7 +24,7 @@
 
 
 #define NUM_AUTHS 6 /* number of authority fields */
-#define NUM_SUBAUTHS 5 /* number of sub authority fields */
+#define SID_MAX_SUB_AUTHORITIES (15) /* max number of sub authority fields */
 #define NUM_WK_SIDS 7 /* number of well known sids */
 #define SIDNAMELENGTH 20 /* long enough for the ones we care about */
 #define DEFSECDESCLEN 192 /* sec desc len contaiting a dacl with three aces */
@@ -41,7 +41,20 @@
 
 #define SIDOWNER 1
 #define SIDGROUP 2
-#define SIDLEN 150 /* S- 1 revision- 6 authorities- max 5 sub authorities */
+
+/*
+ * Maximum size of a string representation of a SID:
+ *
+ * The fields are unsigned values in decimal. So:
+ *
+ * u8:  max 3 bytes in decimal
+ * u32: max 10 bytes in decimal
+ *
+ * "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes
+ * per number + 1 for '-') + 11 bytes for each subauthority field (10 bytes
+ * per number + 1 for '-') + NULL terminator.
+ */
+#define SID_STRING_MAX (195)
 
 #define SID_ID_MAPPED 0
 #define SID_ID_PENDING 1
@@ -61,7 +74,7 @@ struct cifs_sid {
 	__u8 revision; /* revision level */
 	__u8 num_subauth;
 	__u8 authority[NUM_AUTHS];
-	__le32 sub_auth[NUM_SUBAUTHS]; /* sub_auth[num_subauth] */
+	__le32 sub_auth[SID_MAX_SUB_AUTHORITIES]; /* sub_auth[num_subauth] */
 } __attribute__((packed));
 
 /* size of a struct cifs_sid, sans sub_auth array */
Signature · 1 line
1.7.11.7
#4 lore raw

PATCH v2 3/9 cifs: fix the format specifiers in sid_to_str

In reply to a message not in the archive

The format specifiers are for signed values, but these are unsigned. Given that '-' is a delimiter between fields, I don't think you'd get what you'd expect if you got a value here that would overflow the sign bit.

The version and authority fields are 8 bit values so use a "hh" length modifier there. The subauths are 32 bit values, so there's no need to use a "l" length modifier there.

---
 fs/cifs/cifsacl.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
fs/cifs/cifsacl.c +4 -7
index dd8d3df..9adcdb5 100644
@@ -199,27 +199,24 @@ static void
 sid_to_str(struct cifs_sid *sidptr, char *sidstr)
 {
 	int i;
-	unsigned long saval;
+	unsigned int saval;
 	char *strptr;
 
 	strptr = sidstr;
 
-	sprintf(strptr, "%s", "S");
-	strptr = sidstr + strlen(sidstr);
-
-	sprintf(strptr, "-%d", sidptr->revision);
+	sprintf(strptr, "S-%hhu", sidptr->revision);
 	strptr = sidstr + strlen(sidstr);
 
 	for (i = 0; i < NUM_AUTHS; ++i) {
 		if (sidptr->authority[i]) {
-			sprintf(strptr, "-%d", sidptr->authority[i]);
+			sprintf(strptr, "-%hhu", sidptr->authority[i]);
 			strptr = sidstr + strlen(sidstr);
 		}
 	}
 
 	for (i = 0; i < sidptr->num_subauth; ++i) {
 		saval = le32_to_cpu(sidptr->sub_auth[i]);
-		sprintf(strptr, "-%ld", saval);
+		sprintf(strptr, "-%u", saval);
 		strptr = sidstr + strlen(sidstr);
 	}
 }
Signature · 1 line
1.7.11.7
#5 lore raw

PATCH v2 4/9 cifs: remove uneeded __KERNEL__ block from cifsacl.h

In reply to a message not in the archive

...and make those symbols static in cifsacl.c. Nothing outside of that file refers to them.

---
 fs/cifs/cifsacl.c | 4 ++--
 fs/cifs/cifsacl.h | 5 -----
 2 files changed, 2 insertions(+), 7 deletions(-)
fs/cifs/cifsacl.c +2 -2
index 9adcdb5..42b3fe9 100644
@@ -42,7 +42,7 @@ static const struct cifs_sid sid_authusers = {
 /* group users */
 static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
 
-const struct cred *root_cred;
+static const struct cred *root_cred;
 
 static void
 shrink_idmap_tree(struct rb_root *root, int nr_to_scan, int *nr_rem,
@@ -187,7 +187,7 @@ cifs_idmap_key_destroy(struct key *key)
 	kfree(key->payload.data);
 }
 
-struct key_type cifs_idmap_key_type = {
+static struct key_type cifs_idmap_key_type = {
 	.name        = "cifs.idmap",
 	.instantiate = cifs_idmap_key_instantiate,
 	.destroy     = cifs_idmap_key_destroy,
fs/cifs/cifsacl.h +0 -5
index 8b980cd..249c94f 100644
@@ -109,9 +109,4 @@ struct cifs_sid_id {
 	struct cifs_sid sid;
 };
 
-#ifdef __KERNEL__
-extern struct key_type cifs_idmap_key_type;
-extern const struct cred *root_cred;
-#endif /* KERNEL */
-
 #endif /* _CIFSACL_H */
Signature · 1 line
1.7.11.7
#6 lore raw

PATCH v2 5/9 cifs: simplify id_to_sid and sid_to_id mapping code

In reply to a message not in the archive

The cifs.idmap handling code currently causes the kernel to cache the data from userspace twice. It first looks in a rbtree to see if there is a matching entry for the given id. If there isn't then it calls request_key which then checks its cache and then calls out to userland if it doesn't have one. If the userland program establishes a mapping and downcalls with that info, it then gets cached in the keyring and in this rbtree.

Aside from the double memory usage and the performance penalty in doing all of these extra copies, there are some nasty bugs in here too. The code declares four rbtrees and spinlocks to protect them, but only seems to use two of them. The upshot is that the same tree is used to hold (eg) uid:sid and sid:uid mappings. The comparitors aren't equipped to deal with that.

I think we'd be best off to remove a layer of caching in this code. If this was originally done for performance reasons, then that really seems like a premature optimization.

This patch does that -- it removes the rbtrees and the locks that protect them and simply has the code do a request_key call on each call into sid_to_id and id_to_sid. This greatly simplifies this code and should roughly halve the memory utilization from using the idmapping code.

---
 fs/cifs/cifsacl.c   | 535 +++++++++-------------------------------------------
 fs/cifs/cifsacl.h   |  28 +--
 fs/cifs/cifsfs.c    |   1 -
 fs/cifs/cifsproto.h |   1 -
 4 files changed, 99 insertions(+), 466 deletions(-)
fs/cifs/cifsacl.c +94 -441
index 42b3fe9..f4508ee 100644
@@ -44,128 +44,6 @@ static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
 
 static const struct cred *root_cred;
 
-static void
-shrink_idmap_tree(struct rb_root *root, int nr_to_scan, int *nr_rem,
-			int *nr_del)
-{
-	struct rb_node *node;
-	struct rb_node *tmp;
-	struct cifs_sid_id *psidid;
-
-	node = rb_first(root);
-	while (node) {
-		tmp = node;
-		node = rb_next(tmp);
-		psidid = rb_entry(tmp, struct cifs_sid_id, rbnode);
-		if (nr_to_scan == 0 || *nr_del == nr_to_scan)
-			++(*nr_rem);
-		else {
-			if (time_after(jiffies, psidid->time + SID_MAP_EXPIRE)
-						&& psidid->refcount == 0) {
-				rb_erase(tmp, root);
-				++(*nr_del);
-			} else
-				++(*nr_rem);
-		}
-	}
-}
-
-/*
- * Run idmap cache shrinker.
- */
-static int
-cifs_idmap_shrinker(struct shrinker *shrink, struct shrink_control *sc)
-{
-	int nr_to_scan = sc->nr_to_scan;
-	int nr_del = 0;
-	int nr_rem = 0;
-	struct rb_root *root;
-
-	root = &uidtree;
-	spin_lock(&siduidlock);
-	shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-	spin_unlock(&siduidlock);
-
-	root = &gidtree;
-	spin_lock(&sidgidlock);
-	shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-	spin_unlock(&sidgidlock);
-
-	root = &siduidtree;
-	spin_lock(&uidsidlock);
-	shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-	spin_unlock(&uidsidlock);
-
-	root = &sidgidtree;
-	spin_lock(&gidsidlock);
-	shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-	spin_unlock(&gidsidlock);
-
-	return nr_rem;
-}
-
-static void
-sid_rb_insert(struct rb_root *root, unsigned long cid,
-		struct cifs_sid_id **psidid, char *typestr)
-{
-	char *strptr;
-	struct rb_node *node = root->rb_node;
-	struct rb_node *parent = NULL;
-	struct rb_node **linkto = &(root->rb_node);
-	struct cifs_sid_id *lsidid;
-
-	while (node) {
-		lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-		parent = node;
-		if (cid > lsidid->id) {
-			linkto = &(node->rb_left);
-			node = node->rb_left;
-		}
-		if (cid < lsidid->id) {
-			linkto = &(node->rb_right);
-			node = node->rb_right;
-		}
-	}
-
-	(*psidid)->id = cid;
-	(*psidid)->time = jiffies - (SID_MAP_RETRY + 1);
-	(*psidid)->refcount = 0;
-
-	sprintf((*psidid)->sidstr, "%s", typestr);
-	strptr = (*psidid)->sidstr + strlen((*psidid)->sidstr);
-	sprintf(strptr, "%ld", cid);
-
-	clear_bit(SID_ID_PENDING, &(*psidid)->state);
-	clear_bit(SID_ID_MAPPED, &(*psidid)->state);
-
-	rb_link_node(&(*psidid)->rbnode, parent, linkto);
-	rb_insert_color(&(*psidid)->rbnode, root);
-}
-
-static struct cifs_sid_id *
-sid_rb_search(struct rb_root *root, unsigned long cid)
-{
-	struct rb_node *node = root->rb_node;
-	struct cifs_sid_id *lsidid;
-
-	while (node) {
-		lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-		if (cid > lsidid->id)
-			node = node->rb_left;
-		else if (cid < lsidid->id)
-			node = node->rb_right;
-		else /* node found */
-			return lsidid;
-	}
-
-	return NULL;
-}
-
-static struct shrinker cifs_shrinker = {
-	.shrink = cifs_idmap_shrinker,
-	.seeks = DEFAULT_SEEKS,
-};
-
 static int
 cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
@@ -195,30 +73,39 @@ static struct key_type cifs_idmap_key_type = {
 	.match       = user_match,
 };
 
-static void
-sid_to_str(struct cifs_sid *sidptr, char *sidstr)
+static char *
+sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
 {
-	int i;
+	int i, len;
 	unsigned int saval;
-	char *strptr;
+	char *sidstr, *strptr;
 
-	strptr = sidstr;
+	/* 3 bytes for prefix */
+	sidstr = kmalloc(3 + SID_STRING_BASE_SIZE +
+			 (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth),
+			 GFP_KERNEL);
+	if (!sidstr)
+		return sidstr;
 
-	sprintf(strptr, "S-%hhu", sidptr->revision);
-	strptr = sidstr + strlen(sidstr);
+	strptr = sidstr;
+	len = sprintf(strptr, "%cs:S-%hhu", type == SIDOWNER ? 'o' : 'g',
+			sidptr->revision);
+	strptr += len;
 
 	for (i = 0; i < NUM_AUTHS; ++i) {
 		if (sidptr->authority[i]) {
-			sprintf(strptr, "-%hhu", sidptr->authority[i]);
-			strptr = sidstr + strlen(sidstr);
+			len = sprintf(strptr, "-%hhu", sidptr->authority[i]);
+			strptr += len;
 		}
 	}
 
 	for (i = 0; i < sidptr->num_subauth; ++i) {
 		saval = le32_to_cpu(sidptr->sub_auth[i]);
-		sprintf(strptr, "-%u", saval);
-		strptr = sidstr + strlen(sidstr);
+		len = sprintf(strptr, "-%u", saval);
+		strptr += len;
 	}
+
+	return sidstr;
 }
 
 /*
@@ -284,184 +171,38 @@ cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
 		dst->sub_auth[i] = src->sub_auth[i];
 }
 
-static void
-id_rb_insert(struct rb_root *root, struct cifs_sid *sidptr,
-		struct cifs_sid_id **psidid, char *typestr)
-{
-	int rc;
-	char *strptr;
-	struct rb_node *node = root->rb_node;
-	struct rb_node *parent = NULL;
-	struct rb_node **linkto = &(root->rb_node);
-	struct cifs_sid_id *lsidid;
-
-	while (node) {
-		lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-		parent = node;
-		rc = compare_sids(sidptr, &((lsidid)->sid));
-		if (rc > 0) {
-			linkto = &(node->rb_left);
-			node = node->rb_left;
-		} else if (rc < 0) {
-			linkto = &(node->rb_right);
-			node = node->rb_right;
-		}
-	}
-
-	cifs_copy_sid(&(*psidid)->sid, sidptr);
-	(*psidid)->time = jiffies - (SID_MAP_RETRY + 1);
-	(*psidid)->refcount = 0;
-
-	sprintf((*psidid)->sidstr, "%s", typestr);
-	strptr = (*psidid)->sidstr + strlen((*psidid)->sidstr);
-	sid_to_str(&(*psidid)->sid, strptr);
-
-	clear_bit(SID_ID_PENDING, &(*psidid)->state);
-	clear_bit(SID_ID_MAPPED, &(*psidid)->state);
-
-	rb_link_node(&(*psidid)->rbnode, parent, linkto);
-	rb_insert_color(&(*psidid)->rbnode, root);
-}
-
-static struct cifs_sid_id *
-id_rb_search(struct rb_root *root, struct cifs_sid *sidptr)
-{
-	int rc;
-	struct rb_node *node = root->rb_node;
-	struct cifs_sid_id *lsidid;
-
-	while (node) {
-		lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-		rc = compare_sids(sidptr, &((lsidid)->sid));
-		if (rc > 0) {
-			node = node->rb_left;
-		} else if (rc < 0) {
-			node = node->rb_right;
-		} else /* node found */
-			return lsidid;
-	}
-
-	return NULL;
-}
-
-static int
-sidid_pending_wait(void *unused)
-{
-	schedule();
-	return signal_pending(current) ? -ERESTARTSYS : 0;
-}
-
 static int
-id_to_sid(unsigned long cid, uint sidtype, struct cifs_sid *ssid)
+id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 {
-	int rc = 0;
+	int rc;
 	struct key *sidkey;
+	char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */
 	const struct cred *saved_cred;
-	struct cifs_sid *lsid;
-	struct cifs_sid_id *psidid, *npsidid;
-	struct rb_root *cidtree;
-	spinlock_t *cidlock;
-
-	if (sidtype == SIDOWNER) {
-		cidlock = &siduidlock;
-		cidtree = &uidtree;
-	} else if (sidtype == SIDGROUP) {
-		cidlock = &sidgidlock;
-		cidtree = &gidtree;
-	} else
-		return -EINVAL;
-
-	spin_lock(cidlock);
-	psidid = sid_rb_search(cidtree, cid);
-
-	if (!psidid) { /* node does not exist, allocate one & attempt adding */
-		spin_unlock(cidlock);
-		npsidid = kzalloc(sizeof(struct cifs_sid_id), GFP_KERNEL);
-		if (!npsidid)
-			return -ENOMEM;
-
-		npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
-		if (!npsidid->sidstr) {
-			kfree(npsidid);
-			return -ENOMEM;
-		}
-
-		spin_lock(cidlock);
-		psidid = sid_rb_search(cidtree, cid);
-		if (psidid) { /* node happened to get inserted meanwhile */
-			++psidid->refcount;
-			spin_unlock(cidlock);
-			kfree(npsidid->sidstr);
-			kfree(npsidid);
-		} else {
-			psidid = npsidid;
-			sid_rb_insert(cidtree, cid, &psidid,
-					sidtype == SIDOWNER ? "oi:" : "gi:");
-			++psidid->refcount;
-			spin_unlock(cidlock);
-		}
-	} else {
-		++psidid->refcount;
-		spin_unlock(cidlock);
-	}
 
-	/*
-	 * If we are here, it is safe to access psidid and its fields
-	 * since a reference was taken earlier while holding the spinlock.
-	 * A reference on the node is put without holding the spinlock
-	 * and it is OK to do so in this case, shrinker will not erase
-	 * this node until all references are put and we do not access
-	 * any fields of the node after a reference is put .
-	 */
-	if (test_bit(SID_ID_MAPPED, &psidid->state)) {
-		cifs_copy_sid(ssid, &psidid->sid);
-		psidid->time = jiffies; /* update ts for accessing */
-		goto id_sid_out;
-	}
+	rc = snprintf(desc, sizeof(desc), "%ci:%u",
+			sidtype == SIDOWNER ? 'o' : 'g', cid);
+	if (rc >= sizeof(desc))
+		return -EINVAL;
 
-	if (time_after(psidid->time + SID_MAP_RETRY, jiffies)) {
+	rc = 0;
+	saved_cred = override_creds(root_cred);
+	sidkey = request_key(&cifs_idmap_key_type, desc, "");
+	if (IS_ERR(sidkey)) {
 		rc = -EINVAL;
-		goto id_sid_out;
-	}
-
-	if (!test_and_set_bit(SID_ID_PENDING, &psidid->state)) {
-		saved_cred = override_creds(root_cred);
-		sidkey = request_key(&cifs_idmap_key_type, psidid->sidstr, "");
-		if (IS_ERR(sidkey)) {
-			rc = -EINVAL;
-			cFYI(1, "%s: Can't map and id to a SID", __func__);
-		} else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
-			rc = -EIO;
-			cFYI(1, "%s: Downcall contained malformed key "
-				"(datalen=%hu)", __func__, sidkey->datalen);
-		} else {
-			lsid = (struct cifs_sid *)sidkey->payload.data;
-			cifs_copy_sid(&psidid->sid, lsid);
-			cifs_copy_sid(ssid, &psidid->sid);
-			set_bit(SID_ID_MAPPED, &psidid->state);
-			key_put(sidkey);
-			kfree(psidid->sidstr);
-		}
-		psidid->time = jiffies; /* update ts for accessing */
-		revert_creds(saved_cred);
-		clear_bit(SID_ID_PENDING, &psidid->state);
-		wake_up_bit(&psidid->state, SID_ID_PENDING);
-	} else {
-		rc = wait_on_bit(&psidid->state, SID_ID_PENDING,
-				sidid_pending_wait, TASK_INTERRUPTIBLE);
-		if (rc) {
-			cFYI(1, "%s: sidid_pending_wait interrupted %d",
-					__func__, rc);
-			--psidid->refcount;
-			return rc;
-		}
-		if (test_bit(SID_ID_MAPPED, &psidid->state))
-			cifs_copy_sid(ssid, &psidid->sid);
-		else
-			rc = -EINVAL;
-	}
-id_sid_out:
-	--psidid->refcount;
+		cFYI(1, "%s: Can't map %cid %u to a SID", __func__,
+			sidtype == SIDOWNER ? 'u' : 'g', cid);
+		goto out_revert_creds;
+	} else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
+		rc = -EIO;
+		cFYI(1, "%s: Downcall contained malformed key "
+			"(datalen=%hu)", __func__, sidkey->datalen);
+		goto out_key_put;
+	}
+	cifs_copy_sid(ssid, (struct cifs_sid *)sidkey->payload.data);
+out_key_put:
+	key_put(sidkey);
+out_revert_creds:
+	revert_creds(saved_cred);
 	return rc;
 }
 
@@ -470,111 +211,66 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 		struct cifs_fattr *fattr, uint sidtype)
 {
 	int rc;
-	unsigned long cid;
-	struct key *idkey;
+	struct key *sidkey;
+	char *sidstr;
 	const struct cred *saved_cred;
-	struct cifs_sid_id *psidid, *npsidid;
-	struct rb_root *cidtree;
-	spinlock_t *cidlock;
-
-	if (sidtype == SIDOWNER) {
-		cid = cifs_sb->mnt_uid; /* default uid, in case upcall fails */
-		cidlock = &siduidlock;
-		cidtree = &uidtree;
-	} else if (sidtype == SIDGROUP) {
-		cid = cifs_sb->mnt_gid; /* default gid, in case upcall fails */
-		cidlock = &sidgidlock;
-		cidtree = &gidtree;
-	} else
-		return -ENOENT;
-
-	spin_lock(cidlock);
-	psidid = id_rb_search(cidtree, psid);
-
-	if (!psidid) { /* node does not exist, allocate one & attempt adding */
-		spin_unlock(cidlock);
-		npsidid = kzalloc(sizeof(struct cifs_sid_id), GFP_KERNEL);
-		if (!npsidid)
-			return -ENOMEM;
-
-		npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
-		if (!npsidid->sidstr) {
-			kfree(npsidid);
-			return -ENOMEM;
-		}
-
-		spin_lock(cidlock);
-		psidid = id_rb_search(cidtree, psid);
-		if (psidid) { /* node happened to get inserted meanwhile */
-			++psidid->refcount;
-			spin_unlock(cidlock);
-			kfree(npsidid->sidstr);
-			kfree(npsidid);
-		} else {
-			psidid = npsidid;
-			id_rb_insert(cidtree, psid, &psidid,
-					sidtype == SIDOWNER ? "os:" : "gs:");
-			++psidid->refcount;
-			spin_unlock(cidlock);
-		}
-	} else {
-		++psidid->refcount;
-		spin_unlock(cidlock);
-	}
+	uid_t fuid = cifs_sb->mnt_uid;
+	gid_t fgid = cifs_sb->mnt_gid;
 
 	/*
-	 * If we are here, it is safe to access psidid and its fields
-	 * since a reference was taken earlier while holding the spinlock.
-	 * A reference on the node is put without holding the spinlock
-	 * and it is OK to do so in this case, shrinker will not erase
-	 * this node until all references are put and we do not access
-	 * any fields of the node after a reference is put .
+	 * If we have too many subauthorities, then something is really wrong.
+	 * Just return an error.
 	 */
-	if (test_bit(SID_ID_MAPPED, &psidid->state)) {
-		cid = psidid->id;
-		psidid->time = jiffies; /* update ts for accessing */
-		goto sid_to_id_out;
+	if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
+		cFYI(1, "%s: %u subauthorities is too many!", __func__,
+			psid->num_subauth);
+		return -EIO;
 	}
 
-	if (time_after(psidid->time + SID_MAP_RETRY, jiffies))
-		goto sid_to_id_out;
-
-	if (!test_and_set_bit(SID_ID_PENDING, &psidid->state)) {
-		saved_cred = override_creds(root_cred);
-		idkey = request_key(&cifs_idmap_key_type, psidid->sidstr, "");
-		if (IS_ERR(idkey))
-			cFYI(1, "%s: Can't map SID to an id", __func__);
-		else {
-			cid = *(unsigned long *)idkey->payload.value;
-			psidid->id = cid;
-			set_bit(SID_ID_MAPPED, &psidid->state);
-			key_put(idkey);
-			kfree(psidid->sidstr);
-		}
-		revert_creds(saved_cred);
-		psidid->time = jiffies; /* update ts for accessing */
-		clear_bit(SID_ID_PENDING, &psidid->state);
-		wake_up_bit(&psidid->state, SID_ID_PENDING);
-	} else {
-		rc = wait_on_bit(&psidid->state, SID_ID_PENDING,
-				sidid_pending_wait, TASK_INTERRUPTIBLE);
-		if (rc) {
-			cFYI(1, "%s: sidid_pending_wait interrupted %d",
-					__func__, rc);
-			--psidid->refcount; /* decremented without spinlock */
-			return rc;
-		}
-		if (test_bit(SID_ID_MAPPED, &psidid->state))
-			cid = psidid->id;
+	sidstr = sid_to_key_str(psid, sidtype);
+	if (!sidstr)
+		return -ENOMEM;
+
+	saved_cred = override_creds(root_cred);
+	sidkey = request_key(&cifs_idmap_key_type, sidstr, "");
+	if (IS_ERR(sidkey)) {
+		rc = -EINVAL;
+		cFYI(1, "%s: Can't map SID %s to a %cid", __func__, sidstr,
+			sidtype == SIDOWNER ? 'u' : 'g');
+		goto out_revert_creds;
+	}
+
+	/*
+	 * FIXME: Here we assume that uid_t and gid_t are same size. It's
+	 * probably a safe assumption but might be better to check based on
+	 * sidtype.
+	 */
+	if (sidkey->datalen < sizeof(uid_t)) {
+		rc = -EIO;
+		cFYI(1, "%s: Downcall contained malformed key "
+			"(datalen=%hu)", __func__, sidkey->datalen);
+		goto out_key_put;
 	}
 
-sid_to_id_out:
-	--psidid->refcount; /* decremented without spinlock */
 	if (sidtype == SIDOWNER)
-		fattr->cf_uid = cid;
+		fuid = *(uid_t *)sidkey->payload.value;
 	else
-		fattr->cf_gid = cid;
+		fgid = *(gid_t *)sidkey->payload.value;
 
+out_key_put:
+	key_put(sidkey);
+out_revert_creds:
+	revert_creds(saved_cred);
+	kfree(sidstr);
+
+	/*
+	 * Note that we return 0 here unconditionally. If the mapping
+	 * fails then we just fall back to using the mnt_uid/mnt_gid.
+	 */
+	if (sidtype == SIDOWNER)
+		fattr->cf_uid = fuid;
+	else
+		fattr->cf_gid = fgid;
 	return 0;
 }
 
@@ -621,17 +317,6 @@ init_cifs_idmap(void)
 	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
 	root_cred = cred;
 
-	spin_lock_init(&siduidlock);
-	uidtree = RB_ROOT;
-	spin_lock_init(&sidgidlock);
-	gidtree = RB_ROOT;
-
-	spin_lock_init(&uidsidlock);
-	siduidtree = RB_ROOT;
-	spin_lock_init(&gidsidlock);
-	sidgidtree = RB_ROOT;
-	register_shrinker(&cifs_shrinker);
-
 	cFYI(1, "cifs idmap keyring: %d", key_serial(keyring));
 	return 0;
 
@@ -648,41 +333,9 @@ exit_cifs_idmap(void)
 	key_revoke(root_cred->thread_keyring);
 	unregister_key_type(&cifs_idmap_key_type);
 	put_cred(root_cred);
-	unregister_shrinker(&cifs_shrinker);
 	cFYI(1, "Unregistered %s key type", cifs_idmap_key_type.name);
 }
 
-void
-cifs_destroy_idmaptrees(void)
-{
-	struct rb_root *root;
-	struct rb_node *node;
-
-	root = &uidtree;
-	spin_lock(&siduidlock);
-	while ((node = rb_first(root)))
-		rb_erase(node, root);
-	spin_unlock(&siduidlock);
-
-	root = &gidtree;
-	spin_lock(&sidgidlock);
-	while ((node = rb_first(root)))
-		rb_erase(node, root);
-	spin_unlock(&sidgidlock);
-
-	root = &siduidtree;
-	spin_lock(&uidsidlock);
-	while ((node = rb_first(root)))
-		rb_erase(node, root);
-	spin_unlock(&uidsidlock);
-
-	root = &sidgidtree;
-	spin_lock(&gidsidlock);
-	while ((node = rb_first(root)))
-		rb_erase(node, root);
-	spin_unlock(&gidsidlock);
-}
-
 /* copy ntsd, owner sid, and group sid from a security descriptor to another */
 static void copy_sec_desc(const struct cifs_ntsd *pntsd,
 				struct cifs_ntsd *pnntsd, __u32 sidsoffset)
fs/cifs/cifsacl.h +5 -23
index 249c94f..46cd444 100644
@@ -23,7 +23,7 @@
 #define _CIFSACL_H
 
 
-#define NUM_AUTHS 6 /* number of authority fields */
+#define NUM_AUTHS (6)	/* number of authority fields */
 #define SID_MAX_SUB_AUTHORITIES (15) /* max number of sub authority fields */
 #define NUM_WK_SIDS 7 /* number of well known sids */
 #define SIDNAMELENGTH 20 /* long enough for the ones we care about */
@@ -51,15 +51,12 @@
  * u32: max 10 bytes in decimal
  *
  * "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes
- * per number + 1 for '-') + 11 bytes for each subauthority field (10 bytes
  * per number + 1 for '-') + NULL terminator.
+ *
+ * Add 11 bytes for each subauthority field (10 bytes each + 1 for '-')
  */
-#define SID_STRING_MAX (195)
-
-#define SID_ID_MAPPED 0
-#define SID_ID_PENDING 1
-#define SID_MAP_EXPIRE (3600 * HZ) /* map entry expires after one hour */
-#define SID_MAP_RETRY (300 * HZ)   /* wait 5 minutes for next attempt to map */
+#define SID_STRING_BASE_SIZE (2 + 3 + (4 * NUM_AUTHS) + 1)
+#define SID_STRING_SUBAUTH_SIZE (11) /* size of a single subauth string */
 
 struct cifs_ntsd {
 	__le16 revision; /* revision level */
@@ -94,19 +91,4 @@ struct cifs_ace {
 	struct cifs_sid sid; /* ie UUID of user or group who gets these perms */
 } __attribute__((packed));
 
-struct cifs_wksid {
-	struct cifs_sid cifssid;
-	char sidname[SIDNAMELENGTH];
-} __attribute__((packed));
-
-struct cifs_sid_id {
-	unsigned int refcount; /* increment with spinlock, decrement without */
-	unsigned long id;
-	unsigned long time;
-	unsigned long state;
-	char *sidstr;
-	struct rb_node rbnode;
-	struct cifs_sid sid;
-};
-
 #endif /* _CIFSACL_H */
fs/cifs/cifsfs.c +0 -1
index 07a8ab52..5e62f44 100644
@@ -1204,7 +1204,6 @@ exit_cifs(void)
 	unregister_filesystem(&cifs_fs_type);
 	cifs_dfs_release_automount_timer();
 #ifdef CONFIG_CIFS_ACL
-	cifs_destroy_idmaptrees();
 	exit_cifs_idmap();
 #endif
 #ifdef CONFIG_CIFS_UPCALL
fs/cifs/cifsproto.h +0 -1
index 5144e9f..2d2ae69 100644
@@ -58,7 +58,6 @@ do {								\
 } while (0)
 extern int init_cifs_idmap(void);
 extern void exit_cifs_idmap(void);
-extern void cifs_destroy_idmaptrees(void);
 extern char *build_path_from_dentry(struct dentry *);
 extern char *build_wildcard_path_from_dentry(struct dentry *direntry);
 extern char *cifs_compose_mount_options(const char *sb_mountdata,
Signature · 1 line
1.7.11.7
#11 lore raw

PATCH v2 6/9 cifs: avoid extra allocation for small cifs.idmap keys

In reply to a message not in the archive

The cifs.idmap keytype always allocates memory to hold the payload from userspace. In the common case where we're translating a SID to a UID or GID, we're allocating memory to hold something that's less than or equal to the size of a pointer.

When the payload is the same size as a pointer or smaller, just store it in the payload.value union member instead. That saves us an extra allocation on the sid_to_id upcall.

Note that we have to take extra care to check the datalen when we go to dereference the .data pointer in the union, but the callers now check that as a matter of course anyway.

---
 fs/cifs/cifsacl.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
fs/cifs/cifsacl.c +19 -4
index f4508ee..12d70ee 100644
@@ -49,6 +49,20 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
 	char *payload;
 
+	/*
+	 * If the payload is less than or equal to the size of a pointer, then
+	 * an allocation here is wasteful. Just copy the data directly to the
+	 * payload.value union member instead.
+	 *
+	 * With this however, you must check the datalen before trying to
+	 * dereference payload.data!
+	 */
+	if (prep->datalen <= sizeof(void *)) {
+		key->payload.value = 0;
+		memcpy(&key->payload.value, prep->data, prep->datalen);
+		key->datalen = prep->datalen;
+		return 0;
+	}
 	payload = kmalloc(prep->datalen, GFP_KERNEL);
 	if (!payload)
 		return -ENOMEM;
@@ -62,7 +76,8 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 static inline void
 cifs_idmap_key_destroy(struct key *key)
 {
-	kfree(key->payload.data);
+	if (key->datalen > sizeof(void *))
+		kfree(key->payload.data);
 }
 
 static struct key_type cifs_idmap_key_type = {
@@ -245,7 +260,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	 * probably a safe assumption but might be better to check based on
 	 * sidtype.
 	 */
-	if (sidkey->datalen < sizeof(uid_t)) {
+	if (sidkey->datalen != sizeof(uid_t)) {
 		rc = -EIO;
 		cFYI(1, "%s: Downcall contained malformed key "
 			"(datalen=%hu)", __func__, sidkey->datalen);
@@ -253,9 +268,9 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	}
 
 	if (sidtype == SIDOWNER)
-		fuid = *(uid_t *)sidkey->payload.value;
+		fuid = (uid_t)sidkey->payload.value;
 	else
-		fgid = *(gid_t *)sidkey->payload.value;
+		fgid = (gid_t)sidkey->payload.value;
 
 out_key_put:
 	key_put(sidkey);
Signature · 1 line
1.7.11.7
#15 lore raw

PATCH v2 7/9 cifs: don't override the uid/gid in getattr when cifsacl is enabled

In reply to a message not in the archive

If we're using cifsacl, then we don't want to override the uid/gid with the current uid/gid, since that would prevent you from being able to upcall for this info.

---
 fs/cifs/inode.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/cifs/inode.c +4 -3
index afdff79..ed6208f 100644
@@ -1791,11 +1791,12 @@ int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
 	stat->ino = CIFS_I(inode)->uniqueid;
 
 	/*
-	 * If on a multiuser mount without unix extensions, and the admin hasn't
-	 * overridden them, set the ownership to the fsuid/fsgid of the current
-	 * process.
+	 * If on a multiuser mount without unix extensions or cifsacl being
+	 * enabled, and the admin hasn't overridden them, set the ownership
+	 * to the fsuid/fsgid of the current process.
 	 */
 	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
+	    !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
 	    !tcon->unix_ext) {
 		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
 			stat->uid = current_fsuid();
Signature · 1 line
1.7.11.7
#16 lore raw

PATCH v2 8/9 cifs: set a timeout on keys in cifs.idmap cache

In reply to a message not in the archive

We want to allow unused idmap keys to eventually be purged from the cache. Implement that by simply setting a timeout on the key whenever we use it. We use the same default timeout as the NFS idmap cache, but add a module parm to make it tunable.

---
 fs/cifs/cifsacl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/cifs/cifsacl.c +8 -0
index 12d70ee..0c0a594 100644
@@ -26,6 +26,7 @@
 #include <linux/string.h>
 #include <linux/keyctl.h>
 #include <linux/key-type.h>
+#include <linux/moduleparam.h>
 #include <keys/user-type.h>
 #include "cifspdu.h"
 #include "cifsglob.h"
@@ -44,6 +45,11 @@ static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
 
 static const struct cred *root_cred;
 
+static unsigned int cifs_idmap_cache_timeout = 600;
+module_param(cifs_idmap_cache_timeout, uint, 0644);
+MODULE_PARM_DESC(cifs_idmap_cache_timeout, "Number of seconds that ID mappings "
+			"stay in cache. (default=600)");
+
 static int
 cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
@@ -214,6 +220,7 @@ id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 		goto out_key_put;
 	}
 	cifs_copy_sid(ssid, (struct cifs_sid *)sidkey->payload.data);
+	key_set_timeout(sidkey, cifs_idmap_cache_timeout);
 out_key_put:
 	key_put(sidkey);
 out_revert_creds:
@@ -272,6 +279,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	else
 		fgid = (gid_t)sidkey->payload.value;
 
+	key_set_timeout(sidkey, cifs_idmap_cache_timeout);
 out_key_put:
 	key_put(sidkey);
 out_revert_creds:
Signature · 1 line
1.7.11.7
#19 lore raw

PATCH v2 9/9 cifs: ensure we revalidate the inode after readdir if cifsacl is enabled

In reply to a message not in the archive

Otherwise, "ls -l" will simply show the ownership of the files as the default mnt_uid/gid. This may make "ls -l" performance on large directories super-suck in some cases, but that's the cost of cifsacl.

One possibility to make it suck less would be to somehow proactively dispatch the ACL requests asynchronously from readdir codepath, but that's non-trivial to implement.

---
 fs/cifs/readdir.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/cifs/readdir.c +10 -0
index f9b5d3d..96fe44b 100644
@@ -134,6 +134,16 @@ cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
 	if (fattr->cf_cifsattrs & ATTR_READONLY)
 		fattr->cf_mode &= ~S_IWUGO;
 
+	/*
+	 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
+	 * mark it for revalidation so that "ls -l" will look right. It might
+	 * be super-slow, but if we don't do this then the ownership of files
+	 * may look wrong since the inodes may not have timed out by the time
+	 * "ls" does a stat() call on them.
+	 */
+	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
+		fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
+
 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
 	    fattr->cf_cifsattrs & ATTR_SYSTEM) {
 		if (fattr->cf_eof == 0)  {
Signature · 1 line
1.7.11.7
#29 lore raw

v2 cover cifs: second pile of cleanups for cifsacl

In reply to a message not in the archive

---------- Forwarded message ----------
From: Steve French <[email protected]>
Date: Fri, Oct 19, 2012 at 3:18 PM
Subject: Re: [PATCH v2 0/9] cifs: second pile of cleanups for cifsacl
To: Jeff Layton <[email protected]>
Cc: [email protected]

These are in cifs-2.6.git for-next now. Are there any patches in your
recent patch sets that you (or others) don't want going in until the following release?

Quoted 36 lines · Jeff Layton

This patchset is a respin of the second pile of cleanups for the cifsacl code. Most of the changes to the already-sent patches are pretty minor cleanups and such.

The last two patches in the series are new. The first adds a timeout to the idmap keys so that they can eventually work their way out of the cache. The second fixes an existing bug that caused "ls -l" to show the ownership wrong when cifsacl is used.

With this set, everything seems to work as expected. Steve, could you pull these into your for-next branch soon so we can get them some testing exposure?

Jeff Layton (9):
cifs: make cifs_copy_sid handle a source sid with variable size
subauth arrays
cifs: redefine NUM_SUBAUTH constant from 5 to 15
cifs: fix the format specifiers in sid_to_str
cifs: remove uneeded __KERNEL__ block from cifsacl.h
cifs: simplify id_to_sid and sid_to_id mapping code
cifs: avoid extra allocation for small cifs.idmap keys
cifs: don't override the uid/gid in getattr when cifsacl is enabled
cifs: set a timeout on keys in cifs.idmap cache
cifs: ensure we revalidate the inode after readdir if cifsacl is
enabled

 fs/cifs/cifsacl.c   | 573
++++++++++++----------------------------------------
 fs/cifs/cifsacl.h   |  49 ++---
 fs/cifs/cifsfs.c    |   1 -
 fs/cifs/cifsproto.h |   1 -
 fs/cifs/inode.c     |   7 +-
 fs/cifs/readdir.c   |  10 +
 6 files changed, 161 insertions(+), 480 deletions(-)
Quoted 3 lines

--
1.7.11.7

--
Thanks,

Steve

--
Thanks,

Steve

#30 lore raw
In reply to a message not in the archive
Quoted 4 lines · On Fri, 19 Oct 2012 15:18:46 -0500 Steve French…

These are in cifs-2.6.git for-next now. Are there any patches in your
recent patch sets that you (or others) don't want going in until the following release?

Thanks. I just sent another set of 3 as well. Looks like you got the first one, but the other two should probably go in as well.

The only one that I think is appropriate for 3.7 is c08094407. The rest of these patches are probably 3.8 material.

Signature · 1 line
Jeff Layton <[email protected]>
#18 lore raw

Re: [PATCH v2 8/9] cifs: set a timeout on keys in cifs.idmap cache

In reply to a message not in the archive
Quoted 43 lines · On Fri, 19 Oct 2012 08:20:49 -0400 Jeff Layton…

We want to allow unused idmap keys to eventually be purged from the cache. Implement that by simply setting a timeout on the key whenever we use it. We use the same default timeout as the NFS idmap cache, but add a module parm to make it tunable.

---
 fs/cifs/cifsacl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/cifs/cifsacl.c +7 -0
index 12d70ee..0c0a594 100644
@@ -26,6 +26,7 @@
 #include <linux/string.h>
 #include <linux/keyctl.h>
 #include <linux/key-type.h>
+#include <linux/moduleparam.h>
 #include <keys/user-type.h>
 #include "cifspdu.h"
 #include "cifsglob.h"
@@ -44,6 +45,11 @@ static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
 
 static const struct cred *root_cred;
 
+static unsigned int cifs_idmap_cache_timeout = 600;
+module_param(cifs_idmap_cache_timeout, uint, 0644);
+MODULE_PARM_DESC(cifs_idmap_cache_timeout, "Number of seconds that ID mappings "
+			"stay in cache. (default=600)");
+
 static int
 cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
@@ -214,6 +220,7 @@ id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 		goto out_key_put;
 	}
 	cifs_copy_sid(ssid, (struct cifs_sid *)sidkey->payload.data);
+	key_set_timeout(sidkey, cifs_idmap_cache_timeout);
 out_key_put:
 	key_put(sidkey);
 out_revert_creds:

fs/cifs/cifsacl.c sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid, · line 285

Quoted 7 lines
fs/cifs/cifsacl.c +1 -0
@@ -272,6 +279,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	else
 		fgid = (gid_t)sidkey->payload.value;
 
+	key_set_timeout(sidkey, cifs_idmap_cache_timeout);
 out_key_put:
 	key_put(sidkey);
 out_revert_creds:

Now that I've given this some thought, I think we might be better off removing the above patch and instead having the userspace program set the timeout on the keys.

It's a pity that older versions of the program didn't do that, but no one has reported a problem with that so far...

Thoughts?

Signature · 1 line
Jeff Layton <[email protected]>
#24 lore raw
In reply to a message not in the archive
Quoted 26 lines · On Fri, 19 Oct 2012 08:20:41 -0400 Jeff Layton…

This patchset is a respin of the second pile of cleanups for the cifsacl code. Most of the changes to the already-sent patches are pretty minor cleanups and such.

The last two patches in the series are new. The first adds a timeout to the idmap keys so that they can eventually work their way out of the cache. The second fixes an existing bug that caused "ls -l" to show the ownership wrong when cifsacl is used.

With this set, everything seems to work as expected. Steve, could you pull these into your for-next branch soon so we can get them some testing exposure?

Jeff Layton (9):
cifs: make cifs_copy_sid handle a source sid with variable size
subauth arrays
cifs: redefine NUM_SUBAUTH constant from 5 to 15
cifs: fix the format specifiers in sid_to_str
cifs: remove uneeded __KERNEL__ block from cifsacl.h
cifs: simplify id_to_sid and sid_to_id mapping code
cifs: avoid extra allocation for small cifs.idmap keys
cifs: don't override the uid/gid in getattr when cifsacl is enabled
cifs: set a timeout on keys in cifs.idmap cache
cifs: ensure we revalidate the inode after readdir if cifsacl is
enabled

Quoted 8 lines
 fs/cifs/cifsacl.c   | 573 ++++++++++++----------------------------------------
 fs/cifs/cifsacl.h   |  49 ++---
 fs/cifs/cifsfs.c    |   1 -
 fs/cifs/cifsproto.h |   1 -
 fs/cifs/inode.c     |   7 +-
 fs/cifs/readdir.c   |  10 +
 6 files changed, 161 insertions(+), 480 deletions(-)

Hi Steve,

I've got an updated version of this patchset in my tree. Most of the change are pretty minor, the main one being that I dropped this patch from the set:

cifs: set a timeout on keys in cifs.idmap cache

...as I think it's better to let the userspace program decide this timeout.

In any case, the new series is in my cifs-next branch. I think it would be best to drop the cifsacl patches you have in your for-next branch and pull these instead. Sound ok?

Thanks,

Signature · 1 line
Jeff Layton <[email protected]>
#26 lore raw
In reply to a message not in the archive
Quoted 46 lines · Jeff Layton
Quoted 26 lines · On Fri, 19 Oct 2012 08:20:41 -0400 Jeff Layton…

This patchset is a respin of the second pile of cleanups for the cifsacl code. Most of the changes to the already-sent patches are pretty minor cleanups and such.

The last two patches in the series are new. The first adds a timeout to the idmap keys so that they can eventually work their way out of the cache. The second fixes an existing bug that caused "ls -l" to show the ownership wrong when cifsacl is used.

With this set, everything seems to work as expected. Steve, could you pull these into your for-next branch soon so we can get them some testing exposure?

Jeff Layton (9):
cifs: make cifs_copy_sid handle a source sid with variable size
subauth arrays
cifs: redefine NUM_SUBAUTH constant from 5 to 15
cifs: fix the format specifiers in sid_to_str
cifs: remove uneeded __KERNEL__ block from cifsacl.h
cifs: simplify id_to_sid and sid_to_id mapping code
cifs: avoid extra allocation for small cifs.idmap keys
cifs: don't override the uid/gid in getattr when cifsacl is enabled
cifs: set a timeout on keys in cifs.idmap cache
cifs: ensure we revalidate the inode after readdir if cifsacl is
enabled

Quoted 9 lines
 fs/cifs/cifsacl.c   | 573
++++++++++++----------------------------------------
 fs/cifs/cifsacl.h   |  49 ++---
 fs/cifs/cifsfs.c    |   1 -
 fs/cifs/cifsproto.h |   1 -
 fs/cifs/inode.c     |   7 +-
 fs/cifs/readdir.c   |  10 +
 6 files changed, 161 insertions(+), 480 deletions(-)

Hi Steve,

I've got an updated version of this patchset in my tree. Most of the change are pretty minor, the main one being that I dropped this patch from the set:

Quoted 8 lines

cifs: set a timeout on keys in cifs.idmap cache

...as I think it's better to let the userspace program decide this timeout.

In any case, the new series is in my cifs-next branch. I think it would be best to drop the cifsacl patches you have in your for-next branch and pull these instead. Sound ok?

Yes - makes sense

--
Thanks,

Steve

#13 lore raw

Re: [PATCH v2 6/9] cifs: avoid extra allocation for small cifs.idmap keys

In reply to a message not in the archive
Quoted 63 lines · On Fri, 19 Oct 2012 08:20:47 -0400 Jeff Layton…

The cifs.idmap keytype always allocates memory to hold the payload from userspace. In the common case where we're translating a SID to a UID or GID, we're allocating memory to hold something that's less than or equal to the size of a pointer.

When the payload is the same size as a pointer or smaller, just store it in the payload.value union member instead. That saves us an extra allocation on the sid_to_id upcall.

Note that we have to take extra care to check the datalen when we go to dereference the .data pointer in the union, but the callers now check that as a matter of course anyway.

---
 fs/cifs/cifsacl.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
fs/cifs/cifsacl.c +17 -2
index f4508ee..12d70ee 100644
@@ -49,6 +49,20 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
 	char *payload;
 
+	/*
+	 * If the payload is less than or equal to the size of a pointer, then
+	 * an allocation here is wasteful. Just copy the data directly to the
+	 * payload.value union member instead.
+	 *
+	 * With this however, you must check the datalen before trying to
+	 * dereference payload.data!
+	 */
+	if (prep->datalen <= sizeof(void *)) {
+		key->payload.value = 0;
+		memcpy(&key->payload.value, prep->data, prep->datalen);
+		key->datalen = prep->datalen;
+		return 0;
+	}
 	payload = kmalloc(prep->datalen, GFP_KERNEL);
 	if (!payload)
 		return -ENOMEM;
@@ -62,7 +76,8 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 static inline void
 cifs_idmap_key_destroy(struct key *key)
 {
-	kfree(key->payload.data);
+	if (key->datalen > sizeof(void *))
+		kfree(key->payload.data);
 }
 
 static struct key_type cifs_idmap_key_type = {
@@ -245,7 +260,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	 * probably a safe assumption but might be better to check based on
 	 * sidtype.
 	 */
-	if (sidkey->datalen < sizeof(uid_t)) {
+	if (sidkey->datalen != sizeof(uid_t)) {
 		rc = -EIO;
 		cFYI(1, "%s: Downcall contained malformed key "
 			"(datalen=%hu)", __func__, sidkey->datalen);

fs/cifs/cifsacl.c sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid, · line 274

Quoted 9 lines
fs/cifs/cifsacl.c +2 -2
@@ -253,9 +268,9 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	}
 
 	if (sidtype == SIDOWNER)
-		fuid = *(uid_t *)sidkey->payload.value;
+		fuid = (uid_t)sidkey->payload.value;
 	else
-		fgid = *(gid_t *)sidkey->payload.value;
+		fgid = (gid_t)sidkey->payload.value;
 

Finally got a BE machine to test this stuff on, and figured out that the above delta is wrong. We're memcpy'ing the payload (sizeof(uid_t), which is same as unsigned int) into the payload.value (which is an unsigned long)

The above block then tries to cast the result of that to a uid_t/gid_t, which works fine on a little-endian machine. Not so much on a BE one...

I'll send a respin for this patch once I've done a bit more testing.

Thanks,

Signature · 1 line
Jeff Layton <[email protected]>
#14 lore raw

PATCH v3 6/9 cifs: avoid extra allocation for small cifs.idmap keys

In reply to a message not in the archive

The cifs.idmap keytype always allocates memory to hold the payload from userspace. In the common case where we're translating a SID to a UID or GID, we're allocating memory to hold something that's less than or equal to the size of a pointer.

When the payload is the same size as a pointer or smaller, just store it in the payload.value union member instead. That saves us an extra allocation on the sid_to_id upcall.

Note that we have to take extra care to check the datalen when we go to dereference the .data pointer in the union, but the callers now check that as a matter of course anyway.

---
 fs/cifs/cifsacl.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
fs/cifs/cifsacl.c +19 -4
index f4508ee..751d34b 100644
@@ -49,6 +49,20 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
 	char *payload;
 
+	/*
+	 * If the payload is less than or equal to the size of a pointer, then
+	 * an allocation here is wasteful. Just copy the data directly to the
+	 * payload.value union member instead.
+	 *
+	 * With this however, you must check the datalen before trying to
+	 * dereference payload.data!
+	 */
+	if (prep->datalen <= sizeof(void *)) {
+		key->payload.value = 0;
+		memcpy(&key->payload.value, prep->data, prep->datalen);
+		key->datalen = prep->datalen;
+		return 0;
+	}
 	payload = kmalloc(prep->datalen, GFP_KERNEL);
 	if (!payload)
 		return -ENOMEM;
@@ -62,7 +76,8 @@ cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 static inline void
 cifs_idmap_key_destroy(struct key *key)
 {
-	kfree(key->payload.data);
+	if (key->datalen > sizeof(void *))
+		kfree(key->payload.data);
 }
 
 static struct key_type cifs_idmap_key_type = {
@@ -245,7 +260,7 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	 * probably a safe assumption but might be better to check based on
 	 * sidtype.
 	 */
-	if (sidkey->datalen < sizeof(uid_t)) {
+	if (sidkey->datalen != sizeof(uid_t)) {
 		rc = -EIO;
 		cFYI(1, "%s: Downcall contained malformed key "
 			"(datalen=%hu)", __func__, sidkey->datalen);
@@ -253,9 +268,9 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
 	}
 
 	if (sidtype == SIDOWNER)
-		fuid = *(uid_t *)sidkey->payload.value;
+		memcpy(&fuid, &sidkey->payload.value, sizeof(uid_t));
 	else
-		fgid = *(gid_t *)sidkey->payload.value;
+		memcpy(&fgid, &sidkey->payload.value, sizeof(gid_t));
 
 out_key_put:
 	key_put(sidkey);
Signature · 1 line
1.7.11.7
#21 lore raw

Re: [PATCH v2 9/9] cifs: ensure we revalidate the inode after readdir if cifsacl is enabled

In reply to a message not in the archive

For the common case of simply "ls" (vs "ls -l") does this affect performance (have you double checked)?

Quoted 37 lines · Jeff Layton

Otherwise, "ls -l" will simply show the ownership of the files as the default mnt_uid/gid. This may make "ls -l" performance on large directories super-suck in some cases, but that's the cost of cifsacl.

One possibility to make it suck less would be to somehow proactively dispatch the ACL requests asynchronously from readdir codepath, but that's non-trivial to implement.

---
 fs/cifs/readdir.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/cifs/readdir.c +10 -1
index f9b5d3d..96fe44b 100644
@@ -134,6 +134,16 @@ cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
        if (fattr->cf_cifsattrs & ATTR_READONLY)
                fattr->cf_mode &= ~S_IWUGO;
 
+       /*
+        * We of course don't get ACL info in FIND_FIRST/NEXT results, so
+        * mark it for revalidation so that "ls -l" will look right. It might
+        * be super-slow, but if we don't do this then the ownership of files
+        * may look wrong since the inodes may not have timed out by the time
+        * "ls" does a stat() call on them.
+        */
+       if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
+               fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
+
        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
            fattr->cf_cifsattrs & ATTR_SYSTEM) {
                if (fattr->cf_eof == 0)  {
--

1.7.11.7

Signature · 3 lines
Thanks,

Steve
#8 lore raw

Re: [PATCH v2 5/9] cifs: simplify id_to_sid and sid_to_id mapping code

In reply to a message not in the archive

For this patch, your argument makes sense, but wondering if the performance hit is measurable (and remembering how atrocious ACL lookup performance can be on some servers). Have you or Shirish tried to measure the performance affect with this patch vs. current code?

Quoted 728 lines · Jeff Layton

The cifs.idmap handling code currently causes the kernel to cache the data from userspace twice. It first looks in a rbtree to see if there is a matching entry for the given id. If there isn't then it calls request_key which then checks its cache and then calls out to userland if it doesn't have one. If the userland program establishes a mapping and downcalls with that info, it then gets cached in the keyring and in this rbtree.

Aside from the double memory usage and the performance penalty in doing all of these extra copies, there are some nasty bugs in here too. The code declares four rbtrees and spinlocks to protect them, but only seems to use two of them. The upshot is that the same tree is used to hold (eg) uid:sid and sid:uid mappings. The comparitors aren't equipped to deal with that.

I think we'd be best off to remove a layer of caching in this code. If this was originally done for performance reasons, then that really seems like a premature optimization.

This patch does that -- it removes the rbtrees and the locks that protect them and simply has the code do a request_key call on each call into sid_to_id and id_to_sid. This greatly simplifies this code and should roughly halve the memory utilization from using the idmapping code.

---
 fs/cifs/cifsacl.c   | 535 +++++++++-------------------------------------------
 fs/cifs/cifsacl.h   |  28 +--
 fs/cifs/cifsfs.c    |   1 -
 fs/cifs/cifsproto.h |   1 -
 4 files changed, 99 insertions(+), 466 deletions(-)
fs/cifs/cifsacl.c +94 -441
index 42b3fe9..f4508ee 100644
@@ -44,128 +44,6 @@ static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
 
 static const struct cred *root_cred;
 
-static void
-shrink_idmap_tree(struct rb_root *root, int nr_to_scan, int *nr_rem,
-                       int *nr_del)
-{
-       struct rb_node *node;
-       struct rb_node *tmp;
-       struct cifs_sid_id *psidid;
-
-       node = rb_first(root);
-       while (node) {
-               tmp = node;
-               node = rb_next(tmp);
-               psidid = rb_entry(tmp, struct cifs_sid_id, rbnode);
-               if (nr_to_scan == 0 || *nr_del == nr_to_scan)
-                       ++(*nr_rem);
-               else {
-                       if (time_after(jiffies, psidid->time + SID_MAP_EXPIRE)
-                                               && psidid->refcount == 0) {
-                               rb_erase(tmp, root);
-                               ++(*nr_del);
-                       } else
-                               ++(*nr_rem);
-               }
-       }
-}
-
-/*
- * Run idmap cache shrinker.
- */
-static int
-cifs_idmap_shrinker(struct shrinker *shrink, struct shrink_control *sc)
-{
-       int nr_to_scan = sc->nr_to_scan;
-       int nr_del = 0;
-       int nr_rem = 0;
-       struct rb_root *root;
-
-       root = &uidtree;
-       spin_lock(&siduidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&siduidlock);
-
-       root = &gidtree;
-       spin_lock(&sidgidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&sidgidlock);
-
-       root = &siduidtree;
-       spin_lock(&uidsidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&uidsidlock);
-
-       root = &sidgidtree;
-       spin_lock(&gidsidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&gidsidlock);
-
-       return nr_rem;
-}
-
-static void
-sid_rb_insert(struct rb_root *root, unsigned long cid,
-               struct cifs_sid_id **psidid, char *typestr)
-{
-       char *strptr;
-       struct rb_node *node = root->rb_node;
-       struct rb_node *parent = NULL;
-       struct rb_node **linkto = &(root->rb_node);
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               parent = node;
-               if (cid > lsidid->id) {
-                       linkto = &(node->rb_left);
-                       node = node->rb_left;
-               }
-               if (cid < lsidid->id) {
-                       linkto = &(node->rb_right);
-                       node = node->rb_right;
-               }
-       }
-
-       (*psidid)->id = cid;
-       (*psidid)->time = jiffies - (SID_MAP_RETRY + 1);
-       (*psidid)->refcount = 0;
-
-       sprintf((*psidid)->sidstr, "%s", typestr);
-       strptr = (*psidid)->sidstr + strlen((*psidid)->sidstr);
-       sprintf(strptr, "%ld", cid);
-
-       clear_bit(SID_ID_PENDING, &(*psidid)->state);
-       clear_bit(SID_ID_MAPPED, &(*psidid)->state);
-
-       rb_link_node(&(*psidid)->rbnode, parent, linkto);
-       rb_insert_color(&(*psidid)->rbnode, root);
-}
-
-static struct cifs_sid_id *
-sid_rb_search(struct rb_root *root, unsigned long cid)
-{
-       struct rb_node *node = root->rb_node;
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               if (cid > lsidid->id)
-                       node = node->rb_left;
-               else if (cid < lsidid->id)
-                       node = node->rb_right;
-               else /* node found */
-                       return lsidid;
-       }
-
-       return NULL;
-}
-
-static struct shrinker cifs_shrinker = {
-       .shrink = cifs_idmap_shrinker,
-       .seeks = DEFAULT_SEEKS,
-};
-
 static int
 cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
@@ -195,30 +73,39 @@ static struct key_type cifs_idmap_key_type = {
        .match       = user_match,
 };
 
-static void
-sid_to_str(struct cifs_sid *sidptr, char *sidstr)
+static char *
+sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
 {
-       int i;
+       int i, len;
        unsigned int saval;
-       char *strptr;
+       char *sidstr, *strptr;
 
-       strptr = sidstr;
+       /* 3 bytes for prefix */
+       sidstr = kmalloc(3 + SID_STRING_BASE_SIZE +
+                        (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth),
+                        GFP_KERNEL);
+       if (!sidstr)
+               return sidstr;
 
-       sprintf(strptr, "S-%hhu", sidptr->revision);
-       strptr = sidstr + strlen(sidstr);
+       strptr = sidstr;
+       len = sprintf(strptr, "%cs:S-%hhu", type == SIDOWNER ? 'o' : 'g',
+                       sidptr->revision);
+       strptr += len;
 
        for (i = 0; i < NUM_AUTHS; ++i) {
                if (sidptr->authority[i]) {
-                       sprintf(strptr, "-%hhu", sidptr->authority[i]);
-                       strptr = sidstr + strlen(sidstr);
+                       len = sprintf(strptr, "-%hhu", sidptr->authority[i]);
+                       strptr += len;
                }
        }
 
        for (i = 0; i < sidptr->num_subauth; ++i) {
                saval = le32_to_cpu(sidptr->sub_auth[i]);
-               sprintf(strptr, "-%u", saval);
-               strptr = sidstr + strlen(sidstr);
+               len = sprintf(strptr, "-%u", saval);
+               strptr += len;
        }
+
+       return sidstr;
 }
 
 /*
@@ -284,184 +171,38 @@ cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
                dst->sub_auth[i] = src->sub_auth[i];
 }
 
-static void
-id_rb_insert(struct rb_root *root, struct cifs_sid *sidptr,
-               struct cifs_sid_id **psidid, char *typestr)
-{
-       int rc;
-       char *strptr;
-       struct rb_node *node = root->rb_node;
-       struct rb_node *parent = NULL;
-       struct rb_node **linkto = &(root->rb_node);
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               parent = node;
-               rc = compare_sids(sidptr, &((lsidid)->sid));
-               if (rc > 0) {
-                       linkto = &(node->rb_left);
-                       node = node->rb_left;
-               } else if (rc < 0) {
-                       linkto = &(node->rb_right);
-                       node = node->rb_right;
-               }
-       }
-
-       cifs_copy_sid(&(*psidid)->sid, sidptr);
-       (*psidid)->time = jiffies - (SID_MAP_RETRY + 1);
-       (*psidid)->refcount = 0;
-
-       sprintf((*psidid)->sidstr, "%s", typestr);
-       strptr = (*psidid)->sidstr + strlen((*psidid)->sidstr);
-       sid_to_str(&(*psidid)->sid, strptr);
-
-       clear_bit(SID_ID_PENDING, &(*psidid)->state);
-       clear_bit(SID_ID_MAPPED, &(*psidid)->state);
-
-       rb_link_node(&(*psidid)->rbnode, parent, linkto);
-       rb_insert_color(&(*psidid)->rbnode, root);
-}
-
-static struct cifs_sid_id *
-id_rb_search(struct rb_root *root, struct cifs_sid *sidptr)
-{
-       int rc;
-       struct rb_node *node = root->rb_node;
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               rc = compare_sids(sidptr, &((lsidid)->sid));
-               if (rc > 0) {
-                       node = node->rb_left;
-               } else if (rc < 0) {
-                       node = node->rb_right;
-               } else /* node found */
-                       return lsidid;
-       }
-
-       return NULL;
-}
-
-static int
-sidid_pending_wait(void *unused)
-{
-       schedule();
-       return signal_pending(current) ? -ERESTARTSYS : 0;
-}
-
 static int
-id_to_sid(unsigned long cid, uint sidtype, struct cifs_sid *ssid)
+id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 {
-       int rc = 0;
+       int rc;
        struct key *sidkey;
+       char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */
        const struct cred *saved_cred;
-       struct cifs_sid *lsid;
-       struct cifs_sid_id *psidid, *npsidid;
-       struct rb_root *cidtree;
-       spinlock_t *cidlock;
-
-       if (sidtype == SIDOWNER) {
-               cidlock = &siduidlock;
-               cidtree = &uidtree;
-       } else if (sidtype == SIDGROUP) {
-               cidlock = &sidgidlock;
-               cidtree = &gidtree;
-       } else
-               return -EINVAL;
-
-       spin_lock(cidlock);
-       psidid = sid_rb_search(cidtree, cid);
-
-       if (!psidid) { /* node does not exist, allocate one & attempt adding */
-               spin_unlock(cidlock);
-               npsidid = kzalloc(sizeof(struct cifs_sid_id), GFP_KERNEL);
-               if (!npsidid)
-                       return -ENOMEM;
-
-               npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
-               if (!npsidid->sidstr) {
-                       kfree(npsidid);
-                       return -ENOMEM;
-               }
-
-               spin_lock(cidlock);
-               psidid = sid_rb_search(cidtree, cid);
-               if (psidid) { /* node happened to get inserted meanwhile */
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-                       kfree(npsidid->sidstr);
-                       kfree(npsidid);
-               } else {
-                       psidid = npsidid;
-                       sid_rb_insert(cidtree, cid, &psidid,
-                                       sidtype == SIDOWNER ? "oi:" : "gi:");
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-               }
-       } else {
-               ++psidid->refcount;
-               spin_unlock(cidlock);
-       }
 
-       /*
-        * If we are here, it is safe to access psidid and its fields
-        * since a reference was taken earlier while holding the spinlock.
-        * A reference on the node is put without holding the spinlock
-        * and it is OK to do so in this case, shrinker will not erase
-        * this node until all references are put and we do not access
-        * any fields of the node after a reference is put .
-        */
-       if (test_bit(SID_ID_MAPPED, &psidid->state)) {
-               cifs_copy_sid(ssid, &psidid->sid);
-               psidid->time = jiffies; /* update ts for accessing */
-               goto id_sid_out;
-       }
+       rc = snprintf(desc, sizeof(desc), "%ci:%u",
+                       sidtype == SIDOWNER ? 'o' : 'g', cid);
+       if (rc >= sizeof(desc))
+               return -EINVAL;
 
-       if (time_after(psidid->time + SID_MAP_RETRY, jiffies)) {
+       rc = 0;
+       saved_cred = override_creds(root_cred);
+       sidkey = request_key(&cifs_idmap_key_type, desc, "");
+       if (IS_ERR(sidkey)) {
                rc = -EINVAL;
-               goto id_sid_out;
-       }
-
-       if (!test_and_set_bit(SID_ID_PENDING, &psidid->state)) {
-               saved_cred = override_creds(root_cred);
-               sidkey = request_key(&cifs_idmap_key_type, psidid->sidstr, "");
-               if (IS_ERR(sidkey)) {
-                       rc = -EINVAL;
-                       cFYI(1, "%s: Can't map and id to a SID", __func__);
-               } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
-                       rc = -EIO;
-                       cFYI(1, "%s: Downcall contained malformed key "
-                               "(datalen=%hu)", __func__, sidkey->datalen);
-               } else {
-                       lsid = (struct cifs_sid *)sidkey->payload.data;
-                       cifs_copy_sid(&psidid->sid, lsid);
-                       cifs_copy_sid(ssid, &psidid->sid);
-                       set_bit(SID_ID_MAPPED, &psidid->state);
-                       key_put(sidkey);
-                       kfree(psidid->sidstr);
-               }
-               psidid->time = jiffies; /* update ts for accessing */
-               revert_creds(saved_cred);
-               clear_bit(SID_ID_PENDING, &psidid->state);
-               wake_up_bit(&psidid->state, SID_ID_PENDING);
-       } else {
-               rc = wait_on_bit(&psidid->state, SID_ID_PENDING,
-                               sidid_pending_wait, TASK_INTERRUPTIBLE);
-               if (rc) {
-                       cFYI(1, "%s: sidid_pending_wait interrupted %d",
-                                       __func__, rc);
-                       --psidid->refcount;
-                       return rc;
-               }
-               if (test_bit(SID_ID_MAPPED, &psidid->state))
-                       cifs_copy_sid(ssid, &psidid->sid);
-               else
-                       rc = -EINVAL;
-       }
-id_sid_out:
-       --psidid->refcount;
+               cFYI(1, "%s: Can't map %cid %u to a SID", __func__,
+                       sidtype == SIDOWNER ? 'u' : 'g', cid);
+               goto out_revert_creds;
+       } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
+               rc = -EIO;
+               cFYI(1, "%s: Downcall contained malformed key "
+                       "(datalen=%hu)", __func__, sidkey->datalen);
+               goto out_key_put;
+       }
+       cifs_copy_sid(ssid, (struct cifs_sid *)sidkey->payload.data);
+out_key_put:
+       key_put(sidkey);
+out_revert_creds:
+       revert_creds(saved_cred);
        return rc;
 }
 
@@ -470,111 +211,66 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
                struct cifs_fattr *fattr, uint sidtype)
 {
        int rc;
-       unsigned long cid;
-       struct key *idkey;
+       struct key *sidkey;
+       char *sidstr;
        const struct cred *saved_cred;
-       struct cifs_sid_id *psidid, *npsidid;
-       struct rb_root *cidtree;
-       spinlock_t *cidlock;
-
-       if (sidtype == SIDOWNER) {
-               cid = cifs_sb->mnt_uid; /* default uid, in case upcall fails */
-               cidlock = &siduidlock;
-               cidtree = &uidtree;
-       } else if (sidtype == SIDGROUP) {
-               cid = cifs_sb->mnt_gid; /* default gid, in case upcall fails */
-               cidlock = &sidgidlock;
-               cidtree = &gidtree;
-       } else
-               return -ENOENT;
-
-       spin_lock(cidlock);
-       psidid = id_rb_search(cidtree, psid);
-
-       if (!psidid) { /* node does not exist, allocate one & attempt adding */
-               spin_unlock(cidlock);
-               npsidid = kzalloc(sizeof(struct cifs_sid_id), GFP_KERNEL);
-               if (!npsidid)
-                       return -ENOMEM;
-
-               npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
-               if (!npsidid->sidstr) {
-                       kfree(npsidid);
-                       return -ENOMEM;
-               }
-
-               spin_lock(cidlock);
-               psidid = id_rb_search(cidtree, psid);
-               if (psidid) { /* node happened to get inserted meanwhile */
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-                       kfree(npsidid->sidstr);
-                       kfree(npsidid);
-               } else {
-                       psidid = npsidid;
-                       id_rb_insert(cidtree, psid, &psidid,
-                                       sidtype == SIDOWNER ? "os:" : "gs:");
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-               }
-       } else {
-               ++psidid->refcount;
-               spin_unlock(cidlock);
-       }
+       uid_t fuid = cifs_sb->mnt_uid;
+       gid_t fgid = cifs_sb->mnt_gid;
 
        /*
-        * If we are here, it is safe to access psidid and its fields
-        * since a reference was taken earlier while holding the spinlock.
-        * A reference on the node is put without holding the spinlock
-        * and it is OK to do so in this case, shrinker will not erase
-        * this node until all references are put and we do not access
-        * any fields of the node after a reference is put .
+        * If we have too many subauthorities, then something is really wrong.
+        * Just return an error.
         */
-       if (test_bit(SID_ID_MAPPED, &psidid->state)) {
-               cid = psidid->id;
-               psidid->time = jiffies; /* update ts for accessing */
-               goto sid_to_id_out;
+       if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
+               cFYI(1, "%s: %u subauthorities is too many!", __func__,
+                       psid->num_subauth);
+               return -EIO;
        }
 
-       if (time_after(psidid->time + SID_MAP_RETRY, jiffies))
-               goto sid_to_id_out;
-
-       if (!test_and_set_bit(SID_ID_PENDING, &psidid->state)) {
-               saved_cred = override_creds(root_cred);
-               idkey = request_key(&cifs_idmap_key_type, psidid->sidstr, "");
-               if (IS_ERR(idkey))
-                       cFYI(1, "%s: Can't map SID to an id", __func__);
-               else {
-                       cid = *(unsigned long *)idkey->payload.value;
-                       psidid->id = cid;
-                       set_bit(SID_ID_MAPPED, &psidid->state);
-                       key_put(idkey);
-                       kfree(psidid->sidstr);
-               }
-               revert_creds(saved_cred);
-               psidid->time = jiffies; /* update ts for accessing */
-               clear_bit(SID_ID_PENDING, &psidid->state);
-               wake_up_bit(&psidid->state, SID_ID_PENDING);
-       } else {
-               rc = wait_on_bit(&psidid->state, SID_ID_PENDING,
-                               sidid_pending_wait, TASK_INTERRUPTIBLE);
-               if (rc) {
-                       cFYI(1, "%s: sidid_pending_wait interrupted %d",
-                                       __func__, rc);
-                       --psidid->refcount; /* decremented without spinlock */
-                       return rc;
-               }
-               if (test_bit(SID_ID_MAPPED, &psidid->state))
-                       cid = psidid->id;
+       sidstr = sid_to_key_str(psid, sidtype);
+       if (!sidstr)
+               return -ENOMEM;
+
+       saved_cred = override_creds(root_cred);
+       sidkey = request_key(&cifs_idmap_key_type, sidstr, "");
+       if (IS_ERR(sidkey)) {
+               rc = -EINVAL;
+               cFYI(1, "%s: Can't map SID %s to a %cid", __func__, sidstr,
+                       sidtype == SIDOWNER ? 'u' : 'g');
+               goto out_revert_creds;
+       }
+
+       /*
+        * FIXME: Here we assume that uid_t and gid_t are same size. It's
+        * probably a safe assumption but might be better to check based on
+        * sidtype.
+        */
+       if (sidkey->datalen < sizeof(uid_t)) {
+               rc = -EIO;
+               cFYI(1, "%s: Downcall contained malformed key "
+                       "(datalen=%hu)", __func__, sidkey->datalen);
+               goto out_key_put;
        }
 
-sid_to_id_out:
-       --psidid->refcount; /* decremented without spinlock */
        if (sidtype == SIDOWNER)
-               fattr->cf_uid = cid;
+               fuid = *(uid_t *)sidkey->payload.value;
        else
-               fattr->cf_gid = cid;
+               fgid = *(gid_t *)sidkey->payload.value;
 
+out_key_put:
+       key_put(sidkey);
+out_revert_creds:
+       revert_creds(saved_cred);
+       kfree(sidstr);
+
+       /*
+        * Note that we return 0 here unconditionally. If the mapping
+        * fails then we just fall back to using the mnt_uid/mnt_gid.
+        */
+       if (sidtype == SIDOWNER)
+               fattr->cf_uid = fuid;
+       else
+               fattr->cf_gid = fgid;
        return 0;
 }
 
@@ -621,17 +317,6 @@ init_cifs_idmap(void)
        cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
        root_cred = cred;
 
-       spin_lock_init(&siduidlock);
-       uidtree = RB_ROOT;
-       spin_lock_init(&sidgidlock);
-       gidtree = RB_ROOT;
-
-       spin_lock_init(&uidsidlock);
-       siduidtree = RB_ROOT;
-       spin_lock_init(&gidsidlock);
-       sidgidtree = RB_ROOT;
-       register_shrinker(&cifs_shrinker);
-
        cFYI(1, "cifs idmap keyring: %d", key_serial(keyring));
        return 0;
 
@@ -648,41 +333,9 @@ exit_cifs_idmap(void)
        key_revoke(root_cred->thread_keyring);
        unregister_key_type(&cifs_idmap_key_type);
        put_cred(root_cred);
-       unregister_shrinker(&cifs_shrinker);
        cFYI(1, "Unregistered %s key type", cifs_idmap_key_type.name);
 }
 
-void
-cifs_destroy_idmaptrees(void)
-{
-       struct rb_root *root;
-       struct rb_node *node;
-
-       root = &uidtree;
-       spin_lock(&siduidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&siduidlock);
-
-       root = &gidtree;
-       spin_lock(&sidgidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&sidgidlock);
-
-       root = &siduidtree;
-       spin_lock(&uidsidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&uidsidlock);
-
-       root = &sidgidtree;
-       spin_lock(&gidsidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&gidsidlock);
-}
-
 /* copy ntsd, owner sid, and group sid from a security descriptor to another */
 static void copy_sec_desc(const struct cifs_ntsd *pntsd,
                                struct cifs_ntsd *pnntsd, __u32 sidsoffset)
fs/cifs/cifsacl.h +5 -23
index 249c94f..46cd444 100644
@@ -23,7 +23,7 @@
 #define _CIFSACL_H
 
 
-#define NUM_AUTHS 6 /* number of authority fields */
+#define NUM_AUTHS (6)  /* number of authority fields */
 #define SID_MAX_SUB_AUTHORITIES (15) /* max number of sub authority fields */
 #define NUM_WK_SIDS 7 /* number of well known sids */
 #define SIDNAMELENGTH 20 /* long enough for the ones we care about */
@@ -51,15 +51,12 @@
  * u32: max 10 bytes in decimal
  *
  * "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes
- * per number + 1 for '-') + 11 bytes for each subauthority field (10 bytes
  * per number + 1 for '-') + NULL terminator.
+ *
+ * Add 11 bytes for each subauthority field (10 bytes each + 1 for '-')
  */
-#define SID_STRING_MAX (195)
-
-#define SID_ID_MAPPED 0
-#define SID_ID_PENDING 1
-#define SID_MAP_EXPIRE (3600 * HZ) /* map entry expires after one hour */
-#define SID_MAP_RETRY (300 * HZ)   /* wait 5 minutes for next attempt to map */
+#define SID_STRING_BASE_SIZE (2 + 3 + (4 * NUM_AUTHS) + 1)
+#define SID_STRING_SUBAUTH_SIZE (11) /* size of a single subauth string */
 
 struct cifs_ntsd {
        __le16 revision; /* revision level */
@@ -94,19 +91,4 @@ struct cifs_ace {
        struct cifs_sid sid; /* ie UUID of user or group who gets these perms */
 } __attribute__((packed));
 
-struct cifs_wksid {
-       struct cifs_sid cifssid;
-       char sidname[SIDNAMELENGTH];
-} __attribute__((packed));
-
-struct cifs_sid_id {
-       unsigned int refcount; /* increment with spinlock, decrement without */
-       unsigned long id;
-       unsigned long time;
-       unsigned long state;
-       char *sidstr;
-       struct rb_node rbnode;
-       struct cifs_sid sid;
-};
-
 #endif /* _CIFSACL_H */
fs/cifs/cifsfs.c +0 -1
index 07a8ab52..5e62f44 100644
@@ -1204,7 +1204,6 @@ exit_cifs(void)
        unregister_filesystem(&cifs_fs_type);
        cifs_dfs_release_automount_timer();
 #ifdef CONFIG_CIFS_ACL
-       cifs_destroy_idmaptrees();
        exit_cifs_idmap();
 #endif
 #ifdef CONFIG_CIFS_UPCALL
fs/cifs/cifsproto.h +0 -2
index 5144e9f..2d2ae69 100644
@@ -58,7 +58,6 @@ do {                                                          \
 } while (0)
 extern int init_cifs_idmap(void);
 extern void exit_cifs_idmap(void);
-extern void cifs_destroy_idmaptrees(void);
 extern char *build_path_from_dentry(struct dentry *);
 extern char *build_wildcard_path_from_dentry(struct dentry *direntry);
 extern char *cifs_compose_mount_options(const char *sb_mountdata,
--

1.7.11.7

Signature · 3 lines
Thanks,

Steve
#10 lore raw

Re: [PATCH v2 5/9] cifs: simplify id_to_sid and sid_to_id mapping code

In reply to a message not in the archive
Quoted 5 lines · On Sun, 25 Nov 2012 14:38:48 -0600 Steve French…

For this patch, your argument makes sense, but wondering if the performance hit is measurable (and remembering how atrocious ACL lookup performance can be on some servers). Have you or Shirish tried to measure the performance affect with this patch vs. current code?

I haven't, but the existing code was broken enough that any comparison before and after wouldn't mean much. You'd need to repair the existing code first and then do the comparison...

Quoted 732 lines · Jeff Layton
Quoted 728 lines

The cifs.idmap handling code currently causes the kernel to cache the data from userspace twice. It first looks in a rbtree to see if there is a matching entry for the given id. If there isn't then it calls request_key which then checks its cache and then calls out to userland if it doesn't have one. If the userland program establishes a mapping and downcalls with that info, it then gets cached in the keyring and in this rbtree.

Aside from the double memory usage and the performance penalty in doing all of these extra copies, there are some nasty bugs in here too. The code declares four rbtrees and spinlocks to protect them, but only seems to use two of them. The upshot is that the same tree is used to hold (eg) uid:sid and sid:uid mappings. The comparitors aren't equipped to deal with that.

I think we'd be best off to remove a layer of caching in this code. If this was originally done for performance reasons, then that really seems like a premature optimization.

This patch does that -- it removes the rbtrees and the locks that protect them and simply has the code do a request_key call on each call into sid_to_id and id_to_sid. This greatly simplifies this code and should roughly halve the memory utilization from using the idmapping code.

---
 fs/cifs/cifsacl.c   | 535 +++++++++-------------------------------------------
 fs/cifs/cifsacl.h   |  28 +--
 fs/cifs/cifsfs.c    |   1 -
 fs/cifs/cifsproto.h |   1 -
 4 files changed, 99 insertions(+), 466 deletions(-)
fs/cifs/cifsacl.c +94 -441
index 42b3fe9..f4508ee 100644
@@ -44,128 +44,6 @@ static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
 
 static const struct cred *root_cred;
 
-static void
-shrink_idmap_tree(struct rb_root *root, int nr_to_scan, int *nr_rem,
-                       int *nr_del)
-{
-       struct rb_node *node;
-       struct rb_node *tmp;
-       struct cifs_sid_id *psidid;
-
-       node = rb_first(root);
-       while (node) {
-               tmp = node;
-               node = rb_next(tmp);
-               psidid = rb_entry(tmp, struct cifs_sid_id, rbnode);
-               if (nr_to_scan == 0 || *nr_del == nr_to_scan)
-                       ++(*nr_rem);
-               else {
-                       if (time_after(jiffies, psidid->time + SID_MAP_EXPIRE)
-                                               && psidid->refcount == 0) {
-                               rb_erase(tmp, root);
-                               ++(*nr_del);
-                       } else
-                               ++(*nr_rem);
-               }
-       }
-}
-
-/*
- * Run idmap cache shrinker.
- */
-static int
-cifs_idmap_shrinker(struct shrinker *shrink, struct shrink_control *sc)
-{
-       int nr_to_scan = sc->nr_to_scan;
-       int nr_del = 0;
-       int nr_rem = 0;
-       struct rb_root *root;
-
-       root = &uidtree;
-       spin_lock(&siduidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&siduidlock);
-
-       root = &gidtree;
-       spin_lock(&sidgidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&sidgidlock);
-
-       root = &siduidtree;
-       spin_lock(&uidsidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&uidsidlock);
-
-       root = &sidgidtree;
-       spin_lock(&gidsidlock);
-       shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del);
-       spin_unlock(&gidsidlock);
-
-       return nr_rem;
-}
-
-static void
-sid_rb_insert(struct rb_root *root, unsigned long cid,
-               struct cifs_sid_id **psidid, char *typestr)
-{
-       char *strptr;
-       struct rb_node *node = root->rb_node;
-       struct rb_node *parent = NULL;
-       struct rb_node **linkto = &(root->rb_node);
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               parent = node;
-               if (cid > lsidid->id) {
-                       linkto = &(node->rb_left);
-                       node = node->rb_left;
-               }
-               if (cid < lsidid->id) {
-                       linkto = &(node->rb_right);
-                       node = node->rb_right;
-               }
-       }
-
-       (*psidid)->id = cid;
-       (*psidid)->time = jiffies - (SID_MAP_RETRY + 1);
-       (*psidid)->refcount = 0;
-
-       sprintf((*psidid)->sidstr, "%s", typestr);
-       strptr = (*psidid)->sidstr + strlen((*psidid)->sidstr);
-       sprintf(strptr, "%ld", cid);
-
-       clear_bit(SID_ID_PENDING, &(*psidid)->state);
-       clear_bit(SID_ID_MAPPED, &(*psidid)->state);
-
-       rb_link_node(&(*psidid)->rbnode, parent, linkto);
-       rb_insert_color(&(*psidid)->rbnode, root);
-}
-
-static struct cifs_sid_id *
-sid_rb_search(struct rb_root *root, unsigned long cid)
-{
-       struct rb_node *node = root->rb_node;
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               if (cid > lsidid->id)
-                       node = node->rb_left;
-               else if (cid < lsidid->id)
-                       node = node->rb_right;
-               else /* node found */
-                       return lsidid;
-       }
-
-       return NULL;
-}
-
-static struct shrinker cifs_shrinker = {
-       .shrink = cifs_idmap_shrinker,
-       .seeks = DEFAULT_SEEKS,
-};
-
 static int
 cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
 {
@@ -195,30 +73,39 @@ static struct key_type cifs_idmap_key_type = {
        .match       = user_match,
 };
 
-static void
-sid_to_str(struct cifs_sid *sidptr, char *sidstr)
+static char *
+sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
 {
-       int i;
+       int i, len;
        unsigned int saval;
-       char *strptr;
+       char *sidstr, *strptr;
 
-       strptr = sidstr;
+       /* 3 bytes for prefix */
+       sidstr = kmalloc(3 + SID_STRING_BASE_SIZE +
+                        (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth),
+                        GFP_KERNEL);
+       if (!sidstr)
+               return sidstr;
 
-       sprintf(strptr, "S-%hhu", sidptr->revision);
-       strptr = sidstr + strlen(sidstr);
+       strptr = sidstr;
+       len = sprintf(strptr, "%cs:S-%hhu", type == SIDOWNER ? 'o' : 'g',
+                       sidptr->revision);
+       strptr += len;
 
        for (i = 0; i < NUM_AUTHS; ++i) {
                if (sidptr->authority[i]) {
-                       sprintf(strptr, "-%hhu", sidptr->authority[i]);
-                       strptr = sidstr + strlen(sidstr);
+                       len = sprintf(strptr, "-%hhu", sidptr->authority[i]);
+                       strptr += len;
                }
        }
 
        for (i = 0; i < sidptr->num_subauth; ++i) {
                saval = le32_to_cpu(sidptr->sub_auth[i]);
-               sprintf(strptr, "-%u", saval);
-               strptr = sidstr + strlen(sidstr);
+               len = sprintf(strptr, "-%u", saval);
+               strptr += len;
        }
+
+       return sidstr;
 }
 
 /*
@@ -284,184 +171,38 @@ cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
                dst->sub_auth[i] = src->sub_auth[i];
 }
 
-static void
-id_rb_insert(struct rb_root *root, struct cifs_sid *sidptr,
-               struct cifs_sid_id **psidid, char *typestr)
-{
-       int rc;
-       char *strptr;
-       struct rb_node *node = root->rb_node;
-       struct rb_node *parent = NULL;
-       struct rb_node **linkto = &(root->rb_node);
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               parent = node;
-               rc = compare_sids(sidptr, &((lsidid)->sid));
-               if (rc > 0) {
-                       linkto = &(node->rb_left);
-                       node = node->rb_left;
-               } else if (rc < 0) {
-                       linkto = &(node->rb_right);
-                       node = node->rb_right;
-               }
-       }
-
-       cifs_copy_sid(&(*psidid)->sid, sidptr);
-       (*psidid)->time = jiffies - (SID_MAP_RETRY + 1);
-       (*psidid)->refcount = 0;
-
-       sprintf((*psidid)->sidstr, "%s", typestr);
-       strptr = (*psidid)->sidstr + strlen((*psidid)->sidstr);
-       sid_to_str(&(*psidid)->sid, strptr);
-
-       clear_bit(SID_ID_PENDING, &(*psidid)->state);
-       clear_bit(SID_ID_MAPPED, &(*psidid)->state);
-
-       rb_link_node(&(*psidid)->rbnode, parent, linkto);
-       rb_insert_color(&(*psidid)->rbnode, root);
-}
-
-static struct cifs_sid_id *
-id_rb_search(struct rb_root *root, struct cifs_sid *sidptr)
-{
-       int rc;
-       struct rb_node *node = root->rb_node;
-       struct cifs_sid_id *lsidid;
-
-       while (node) {
-               lsidid = rb_entry(node, struct cifs_sid_id, rbnode);
-               rc = compare_sids(sidptr, &((lsidid)->sid));
-               if (rc > 0) {
-                       node = node->rb_left;
-               } else if (rc < 0) {
-                       node = node->rb_right;
-               } else /* node found */
-                       return lsidid;
-       }
-
-       return NULL;
-}
-
-static int
-sidid_pending_wait(void *unused)
-{
-       schedule();
-       return signal_pending(current) ? -ERESTARTSYS : 0;
-}
-
 static int
-id_to_sid(unsigned long cid, uint sidtype, struct cifs_sid *ssid)
+id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
 {
-       int rc = 0;
+       int rc;
        struct key *sidkey;
+       char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */
        const struct cred *saved_cred;
-       struct cifs_sid *lsid;
-       struct cifs_sid_id *psidid, *npsidid;
-       struct rb_root *cidtree;
-       spinlock_t *cidlock;
-
-       if (sidtype == SIDOWNER) {
-               cidlock = &siduidlock;
-               cidtree = &uidtree;
-       } else if (sidtype == SIDGROUP) {
-               cidlock = &sidgidlock;
-               cidtree = &gidtree;
-       } else
-               return -EINVAL;
-
-       spin_lock(cidlock);
-       psidid = sid_rb_search(cidtree, cid);
-
-       if (!psidid) { /* node does not exist, allocate one & attempt adding */
-               spin_unlock(cidlock);
-               npsidid = kzalloc(sizeof(struct cifs_sid_id), GFP_KERNEL);
-               if (!npsidid)
-                       return -ENOMEM;
-
-               npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
-               if (!npsidid->sidstr) {
-                       kfree(npsidid);
-                       return -ENOMEM;
-               }
-
-               spin_lock(cidlock);
-               psidid = sid_rb_search(cidtree, cid);
-               if (psidid) { /* node happened to get inserted meanwhile */
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-                       kfree(npsidid->sidstr);
-                       kfree(npsidid);
-               } else {
-                       psidid = npsidid;
-                       sid_rb_insert(cidtree, cid, &psidid,
-                                       sidtype == SIDOWNER ? "oi:" : "gi:");
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-               }
-       } else {
-               ++psidid->refcount;
-               spin_unlock(cidlock);
-       }
 
-       /*
-        * If we are here, it is safe to access psidid and its fields
-        * since a reference was taken earlier while holding the spinlock.
-        * A reference on the node is put without holding the spinlock
-        * and it is OK to do so in this case, shrinker will not erase
-        * this node until all references are put and we do not access
-        * any fields of the node after a reference is put .
-        */
-       if (test_bit(SID_ID_MAPPED, &psidid->state)) {
-               cifs_copy_sid(ssid, &psidid->sid);
-               psidid->time = jiffies; /* update ts for accessing */
-               goto id_sid_out;
-       }
+       rc = snprintf(desc, sizeof(desc), "%ci:%u",
+                       sidtype == SIDOWNER ? 'o' : 'g', cid);
+       if (rc >= sizeof(desc))
+               return -EINVAL;
 
-       if (time_after(psidid->time + SID_MAP_RETRY, jiffies)) {
+       rc = 0;
+       saved_cred = override_creds(root_cred);
+       sidkey = request_key(&cifs_idmap_key_type, desc, "");
+       if (IS_ERR(sidkey)) {
                rc = -EINVAL;
-               goto id_sid_out;
-       }
-
-       if (!test_and_set_bit(SID_ID_PENDING, &psidid->state)) {
-               saved_cred = override_creds(root_cred);
-               sidkey = request_key(&cifs_idmap_key_type, psidid->sidstr, "");
-               if (IS_ERR(sidkey)) {
-                       rc = -EINVAL;
-                       cFYI(1, "%s: Can't map and id to a SID", __func__);
-               } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
-                       rc = -EIO;
-                       cFYI(1, "%s: Downcall contained malformed key "
-                               "(datalen=%hu)", __func__, sidkey->datalen);
-               } else {
-                       lsid = (struct cifs_sid *)sidkey->payload.data;
-                       cifs_copy_sid(&psidid->sid, lsid);
-                       cifs_copy_sid(ssid, &psidid->sid);
-                       set_bit(SID_ID_MAPPED, &psidid->state);
-                       key_put(sidkey);
-                       kfree(psidid->sidstr);
-               }
-               psidid->time = jiffies; /* update ts for accessing */
-               revert_creds(saved_cred);
-               clear_bit(SID_ID_PENDING, &psidid->state);
-               wake_up_bit(&psidid->state, SID_ID_PENDING);
-       } else {
-               rc = wait_on_bit(&psidid->state, SID_ID_PENDING,
-                               sidid_pending_wait, TASK_INTERRUPTIBLE);
-               if (rc) {
-                       cFYI(1, "%s: sidid_pending_wait interrupted %d",
-                                       __func__, rc);
-                       --psidid->refcount;
-                       return rc;
-               }
-               if (test_bit(SID_ID_MAPPED, &psidid->state))
-                       cifs_copy_sid(ssid, &psidid->sid);
-               else
-                       rc = -EINVAL;
-       }
-id_sid_out:
-       --psidid->refcount;
+               cFYI(1, "%s: Can't map %cid %u to a SID", __func__,
+                       sidtype == SIDOWNER ? 'u' : 'g', cid);
+               goto out_revert_creds;
+       } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
+               rc = -EIO;
+               cFYI(1, "%s: Downcall contained malformed key "
+                       "(datalen=%hu)", __func__, sidkey->datalen);
+               goto out_key_put;
+       }
+       cifs_copy_sid(ssid, (struct cifs_sid *)sidkey->payload.data);
+out_key_put:
+       key_put(sidkey);
+out_revert_creds:
+       revert_creds(saved_cred);
        return rc;
 }
 
@@ -470,111 +211,66 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
                struct cifs_fattr *fattr, uint sidtype)
 {
        int rc;
-       unsigned long cid;
-       struct key *idkey;
+       struct key *sidkey;
+       char *sidstr;
        const struct cred *saved_cred;
-       struct cifs_sid_id *psidid, *npsidid;
-       struct rb_root *cidtree;
-       spinlock_t *cidlock;
-
-       if (sidtype == SIDOWNER) {
-               cid = cifs_sb->mnt_uid; /* default uid, in case upcall fails */
-               cidlock = &siduidlock;
-               cidtree = &uidtree;
-       } else if (sidtype == SIDGROUP) {
-               cid = cifs_sb->mnt_gid; /* default gid, in case upcall fails */
-               cidlock = &sidgidlock;
-               cidtree = &gidtree;
-       } else
-               return -ENOENT;
-
-       spin_lock(cidlock);
-       psidid = id_rb_search(cidtree, psid);
-
-       if (!psidid) { /* node does not exist, allocate one & attempt adding */
-               spin_unlock(cidlock);
-               npsidid = kzalloc(sizeof(struct cifs_sid_id), GFP_KERNEL);
-               if (!npsidid)
-                       return -ENOMEM;
-
-               npsidid->sidstr = kmalloc(SID_STRING_MAX, GFP_KERNEL);
-               if (!npsidid->sidstr) {
-                       kfree(npsidid);
-                       return -ENOMEM;
-               }
-
-               spin_lock(cidlock);
-               psidid = id_rb_search(cidtree, psid);
-               if (psidid) { /* node happened to get inserted meanwhile */
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-                       kfree(npsidid->sidstr);
-                       kfree(npsidid);
-               } else {
-                       psidid = npsidid;
-                       id_rb_insert(cidtree, psid, &psidid,
-                                       sidtype == SIDOWNER ? "os:" : "gs:");
-                       ++psidid->refcount;
-                       spin_unlock(cidlock);
-               }
-       } else {
-               ++psidid->refcount;
-               spin_unlock(cidlock);
-       }
+       uid_t fuid = cifs_sb->mnt_uid;
+       gid_t fgid = cifs_sb->mnt_gid;
 
        /*
-        * If we are here, it is safe to access psidid and its fields
-        * since a reference was taken earlier while holding the spinlock.
-        * A reference on the node is put without holding the spinlock
-        * and it is OK to do so in this case, shrinker will not erase
-        * this node until all references are put and we do not access
-        * any fields of the node after a reference is put .
+        * If we have too many subauthorities, then something is really wrong.
+        * Just return an error.
         */
-       if (test_bit(SID_ID_MAPPED, &psidid->state)) {
-               cid = psidid->id;
-               psidid->time = jiffies; /* update ts for accessing */
-               goto sid_to_id_out;
+       if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
+               cFYI(1, "%s: %u subauthorities is too many!", __func__,
+                       psid->num_subauth);
+               return -EIO;
        }
 
-       if (time_after(psidid->time + SID_MAP_RETRY, jiffies))
-               goto sid_to_id_out;
-
-       if (!test_and_set_bit(SID_ID_PENDING, &psidid->state)) {
-               saved_cred = override_creds(root_cred);
-               idkey = request_key(&cifs_idmap_key_type, psidid->sidstr, "");
-               if (IS_ERR(idkey))
-                       cFYI(1, "%s: Can't map SID to an id", __func__);
-               else {
-                       cid = *(unsigned long *)idkey->payload.value;
-                       psidid->id = cid;
-                       set_bit(SID_ID_MAPPED, &psidid->state);
-                       key_put(idkey);
-                       kfree(psidid->sidstr);
-               }
-               revert_creds(saved_cred);
-               psidid->time = jiffies; /* update ts for accessing */
-               clear_bit(SID_ID_PENDING, &psidid->state);
-               wake_up_bit(&psidid->state, SID_ID_PENDING);
-       } else {
-               rc = wait_on_bit(&psidid->state, SID_ID_PENDING,
-                               sidid_pending_wait, TASK_INTERRUPTIBLE);
-               if (rc) {
-                       cFYI(1, "%s: sidid_pending_wait interrupted %d",
-                                       __func__, rc);
-                       --psidid->refcount; /* decremented without spinlock */
-                       return rc;
-               }
-               if (test_bit(SID_ID_MAPPED, &psidid->state))
-                       cid = psidid->id;
+       sidstr = sid_to_key_str(psid, sidtype);
+       if (!sidstr)
+               return -ENOMEM;
+
+       saved_cred = override_creds(root_cred);
+       sidkey = request_key(&cifs_idmap_key_type, sidstr, "");
+       if (IS_ERR(sidkey)) {
+               rc = -EINVAL;
+               cFYI(1, "%s: Can't map SID %s to a %cid", __func__, sidstr,
+                       sidtype == SIDOWNER ? 'u' : 'g');
+               goto out_revert_creds;
+       }
+
+       /*
+        * FIXME: Here we assume that uid_t and gid_t are same size. It's
+        * probably a safe assumption but might be better to check based on
+        * sidtype.
+        */
+       if (sidkey->datalen < sizeof(uid_t)) {
+               rc = -EIO;
+               cFYI(1, "%s: Downcall contained malformed key "
+                       "(datalen=%hu)", __func__, sidkey->datalen);
+               goto out_key_put;
        }
 
-sid_to_id_out:
-       --psidid->refcount; /* decremented without spinlock */
        if (sidtype == SIDOWNER)
-               fattr->cf_uid = cid;
+               fuid = *(uid_t *)sidkey->payload.value;
        else
-               fattr->cf_gid = cid;
+               fgid = *(gid_t *)sidkey->payload.value;
 
+out_key_put:
+       key_put(sidkey);
+out_revert_creds:
+       revert_creds(saved_cred);
+       kfree(sidstr);
+
+       /*
+        * Note that we return 0 here unconditionally. If the mapping
+        * fails then we just fall back to using the mnt_uid/mnt_gid.
+        */
+       if (sidtype == SIDOWNER)
+               fattr->cf_uid = fuid;
+       else
+               fattr->cf_gid = fgid;
        return 0;
 }
 
@@ -621,17 +317,6 @@ init_cifs_idmap(void)
        cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
        root_cred = cred;
 
-       spin_lock_init(&siduidlock);
-       uidtree = RB_ROOT;
-       spin_lock_init(&sidgidlock);
-       gidtree = RB_ROOT;
-
-       spin_lock_init(&uidsidlock);
-       siduidtree = RB_ROOT;
-       spin_lock_init(&gidsidlock);
-       sidgidtree = RB_ROOT;
-       register_shrinker(&cifs_shrinker);
-
        cFYI(1, "cifs idmap keyring: %d", key_serial(keyring));
        return 0;
 
@@ -648,41 +333,9 @@ exit_cifs_idmap(void)
        key_revoke(root_cred->thread_keyring);
        unregister_key_type(&cifs_idmap_key_type);
        put_cred(root_cred);
-       unregister_shrinker(&cifs_shrinker);
        cFYI(1, "Unregistered %s key type", cifs_idmap_key_type.name);
 }
 
-void
-cifs_destroy_idmaptrees(void)
-{
-       struct rb_root *root;
-       struct rb_node *node;
-
-       root = &uidtree;
-       spin_lock(&siduidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&siduidlock);
-
-       root = &gidtree;
-       spin_lock(&sidgidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&sidgidlock);
-
-       root = &siduidtree;
-       spin_lock(&uidsidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&uidsidlock);
-
-       root = &sidgidtree;
-       spin_lock(&gidsidlock);
-       while ((node = rb_first(root)))
-               rb_erase(node, root);
-       spin_unlock(&gidsidlock);
-}
-
 /* copy ntsd, owner sid, and group sid from a security descriptor to another */
 static void copy_sec_desc(const struct cifs_ntsd *pntsd,
                                struct cifs_ntsd *pnntsd, __u32 sidsoffset)
fs/cifs/cifsacl.h +5 -23
index 249c94f..46cd444 100644
@@ -23,7 +23,7 @@
 #define _CIFSACL_H
 
 
-#define NUM_AUTHS 6 /* number of authority fields */
+#define NUM_AUTHS (6)  /* number of authority fields */
 #define SID_MAX_SUB_AUTHORITIES (15) /* max number of sub authority fields */
 #define NUM_WK_SIDS 7 /* number of well known sids */
 #define SIDNAMELENGTH 20 /* long enough for the ones we care about */
@@ -51,15 +51,12 @@
  * u32: max 10 bytes in decimal
  *
  * "S-" + 3 bytes for version field + 4 bytes for each authority field (3 bytes
- * per number + 1 for '-') + 11 bytes for each subauthority field (10 bytes
  * per number + 1 for '-') + NULL terminator.
+ *
+ * Add 11 bytes for each subauthority field (10 bytes each + 1 for '-')
  */
-#define SID_STRING_MAX (195)
-
-#define SID_ID_MAPPED 0
-#define SID_ID_PENDING 1
-#define SID_MAP_EXPIRE (3600 * HZ) /* map entry expires after one hour */
-#define SID_MAP_RETRY (300 * HZ)   /* wait 5 minutes for next attempt to map */
+#define SID_STRING_BASE_SIZE (2 + 3 + (4 * NUM_AUTHS) + 1)
+#define SID_STRING_SUBAUTH_SIZE (11) /* size of a single subauth string */
 
 struct cifs_ntsd {
        __le16 revision; /* revision level */
@@ -94,19 +91,4 @@ struct cifs_ace {
        struct cifs_sid sid; /* ie UUID of user or group who gets these perms */
 } __attribute__((packed));
 
-struct cifs_wksid {
-       struct cifs_sid cifssid;
-       char sidname[SIDNAMELENGTH];
-} __attribute__((packed));
-
-struct cifs_sid_id {
-       unsigned int refcount; /* increment with spinlock, decrement without */
-       unsigned long id;
-       unsigned long time;
-       unsigned long state;
-       char *sidstr;
-       struct rb_node rbnode;
-       struct cifs_sid sid;
-};
-
 #endif /* _CIFSACL_H */
fs/cifs/cifsfs.c +0 -1
index 07a8ab52..5e62f44 100644
@@ -1204,7 +1204,6 @@ exit_cifs(void)
        unregister_filesystem(&cifs_fs_type);
        cifs_dfs_release_automount_timer();
 #ifdef CONFIG_CIFS_ACL
-       cifs_destroy_idmaptrees();
        exit_cifs_idmap();
 #endif
 #ifdef CONFIG_CIFS_UPCALL
fs/cifs/cifsproto.h +0 -2
index 5144e9f..2d2ae69 100644
@@ -58,7 +58,6 @@ do {                                                          \
 } while (0)
 extern int init_cifs_idmap(void);
 extern void exit_cifs_idmap(void);
-extern void cifs_destroy_idmaptrees(void);
 extern char *build_path_from_dentry(struct dentry *);
 extern char *build_wildcard_path_from_dentry(struct dentry *direntry);
 extern char *cifs_compose_mount_options(const char *sb_mountdata,
--

1.7.11.7

Signature · 1 line
Jeff Layton <[email protected]>
#23 lore raw

Re: [PATCH v2 9/9] cifs: ensure we revalidate the inode after readdir if cifsacl is enabled

In reply to a message not in the archive
Quoted 3 lines · On Sun, 25 Nov 2012 14:36:59 -0600 Steve French…

For the common case of simply "ls" (vs "ls -l") does this affect performance (have you double checked)?

I haven't measured it, but it's noticable. There really is no alternative though -- it's a correctness issue.

Quoted 41 lines · Jeff Layton
Quoted 37 lines

Otherwise, "ls -l" will simply show the ownership of the files as the default mnt_uid/gid. This may make "ls -l" performance on large directories super-suck in some cases, but that's the cost of cifsacl.

One possibility to make it suck less would be to somehow proactively dispatch the ACL requests asynchronously from readdir codepath, but that's non-trivial to implement.

---
 fs/cifs/readdir.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/cifs/readdir.c +10 -1
index f9b5d3d..96fe44b 100644
@@ -134,6 +134,16 @@ cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
        if (fattr->cf_cifsattrs & ATTR_READONLY)
                fattr->cf_mode &= ~S_IWUGO;
 
+       /*
+        * We of course don't get ACL info in FIND_FIRST/NEXT results, so
+        * mark it for revalidation so that "ls -l" will look right. It might
+        * be super-slow, but if we don't do this then the ownership of files
+        * may look wrong since the inodes may not have timed out by the time
+        * "ls" does a stat() call on them.
+        */
+       if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
+               fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
+
        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
            fattr->cf_cifsattrs & ATTR_SYSTEM) {
                if (fattr->cf_eof == 0)  {
--

1.7.11.7

Signature · 1 line
Jeff Layton <[email protected]>