linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Scott James Remnant <scott@ubuntu.com>
To: dm-devel@redhat.com
Cc: linux-hotplug-devel@lists.sourceforge.net
Subject: [PATCH] improve atomicity of device creation
Date: Tue, 11 Dec 2007 16:08:36 +0000	[thread overview]
Message-ID: <1197389316.25082.27.camel@quest> (raw)


[-- Attachment #1.1.1: Type: text/plain, Size: 199 bytes --]

This is a patch we've written and applied in Ubuntu to improve the
atomicity of devmapper device creation somewhat, and avoid races with
udev.

Scott
-- 
Scott James Remnant
scott@ubuntu.com

[-- Attachment #1.1.2: devmapper-1.02.20-atomic-mknod.patch --]
[-- Type: text/x-patch, Size: 1762 bytes --]

--- devmapper-1.02.20.orig/lib/libdm-common.c
+++ devmapper-1.02.20/lib/libdm-common.c
@@ -252,12 +252,11 @@
 static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
 			 uid_t uid, gid_t gid, mode_t mode)
 {
-	char path[PATH_MAX];
+	char path[PATH_MAX], tmppath[PATH_MAX + 7];
 	struct stat info;
 	dev_t dev = MKDEV(major, minor);
 	mode_t old_mask;
-
-	_build_dev_path(path, sizeof(path), dev_name);
+	int retval;
 
 	if (stat(path, &info) >= 0) {
 		if (!S_ISBLK(info.st_mode)) {
@@ -269,31 +268,39 @@
 		/* If right inode already exists we don't touch uid etc. */
 		if (info.st_rdev == dev)
 			return 1;
-
-		if (unlink(path) < 0) {
-			log_error("Unable to unlink device node for '%s'",
-				  dev_name);
-			return 0;
-		}
 	}
 
+	_build_dev_path(path, sizeof(path), dev_name);
+	strcpy (tmppath, path);
+	strcat (tmppath, ".dm-tmp");
+
 	old_mask = umask(0);
-	if (mknod(path, S_IFBLK | mode, dev) < 0) {
-		log_error("Unable to make device node for '%s'", dev_name);
+	retval = mknod(tmppath, S_IFBLK | mode, dev);
+	umask(old_mask);
+	if (retval < 0) {
+		log_error("Unable to make temporary device node for '%s'", dev_name);
 		return 0;
 	}
-	umask(old_mask);
 
-	if (chown(path, uid, gid) < 0) {
+	if (chown(tmppath, uid, gid) < 0) {
 		log_error("%s: chown failed: %s", path, strerror(errno));
+		unlink(tmppath);
 		return 0;
 	}
 
 #ifdef HAVE_SELINUX
-	if (!dm_set_selinux_context(path, S_IFBLK))
+	if (!dm_set_selinux_context(tmppath, S_IFBLK)) {
+		unlink(tmppath);
 		return 0;
+	}
 #endif
 
+	if (rename(tmppath, path) < 0) {
+		log_error("Unable to replace device node for '%s'", dev_name);
+		unlink(tmppath);
+		return 0;
+	}
+
 	return 1;
 }
 

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 278 bytes --]

-------------------------------------------------------------------------
SF.Net email is sponsored by: 
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

[-- Attachment #3: Type: text/plain, Size: 226 bytes --]

_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

             reply	other threads:[~2007-12-11 16:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-11 16:08 Scott James Remnant [this message]
2007-12-11 16:25 ` [dm-devel] [PATCH] improve atomicity of device creation Alasdair G Kergon
2007-12-11 16:42   ` Scott James Remnant
2007-12-11 17:03     ` Alasdair G Kergon
2007-12-11 17:18       ` Scott James Remnant
2007-12-11 17:51         ` Kay Sievers
2007-12-11 18:08           ` Alasdair G Kergon
2007-12-11 18:25             ` Kay Sievers
2007-12-11 19:13               ` Alasdair G Kergon
2007-12-11 19:43               ` Alasdair G Kergon
2007-12-11 17:35 ` Kay Sievers
2007-12-11 17:40   ` Scott James Remnant
2007-12-11 17:53     ` Alasdair G Kergon
2007-12-11 19:09       ` Scott James Remnant
2007-12-11 18:03     ` Kay Sievers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1197389316.25082.27.camel@quest \
    --to=scott@ubuntu.com \
    --cc=dm-devel@redhat.com \
    --cc=linux-hotplug-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).