public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] [g_mass_storage] Fix unmount problem with OS-X
  2012-05-16  8:54 [PATCH] [g_mass_storage] Fix unmount problem with OS-X Pantelis Antoniou
@ 2012-05-15 15:58 ` Russ Dill
  0 siblings, 0 replies; 2+ messages in thread
From: Russ Dill @ 2012-05-15 15:58 UTC (permalink / raw)
  To: Pantelis Antoniou; +Cc: linux-kernel

On 05/16/2012 01:54 AM, Pantelis Antoniou wrote:
> OS-X uses a sequence on unmounting that makes us close the backing
> file. Apparently there's no way to open it again, which makes it
> impossible to remount without rmmod'ing g_mass_storage.
>
> Fix this by adding a media_ejected flag to each lun, and keeping
> track of it properly.
>
> Any other method (i.e. preventing unmount, and not keeping track
> of the eject status) failed due to OS-X sending start stop commands
> after the unmount and checking again.
> ---
>  drivers/usb/gadget/f_mass_storage.c |   21 ++++++++++++++-------
>  drivers/usb/gadget/storage_common.c |   11 +++++++++++
>  2 files changed, 25 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
> index cb8c162..b865d20 100644
> --- a/drivers/usb/gadget/f_mass_storage.c
> +++ b/drivers/usb/gadget/f_mass_storage.c
> @@ -1445,6 +1445,9 @@ static int do_start_stop(struct fsg_common *common)
>  			curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
>  			return -EINVAL;
>  		}
> +
> +		/* if the host is trying again, the media are mounted */
> +		fsg_lun_set_ejected(curlun, 0);
>  		return 0;
>  	}
>  
> @@ -1468,11 +1471,15 @@ static int do_start_stop(struct fsg_common *common)
>  			return 0;
>  	}
>  
> -	up_read(&common->filesem);
> -	down_write(&common->filesem);
> -	fsg_lun_close(curlun);
> -	up_write(&common->filesem);
> -	down_read(&common->filesem);
> +	/*
> +	 * Do not close the LUNs, just made them invisible.
> +	 * When the next exception comes we make them visible
> +	 * again. This fixes the bug where MacOSX unmounts
> +	 * and then it's impossible to mount again, since
> +	 * fsg_lun_open is never called again.
> +	 */
> +
> +	fsg_lun_set_ejected(curlun, 1);
>  
>  	return common->ops && common->ops->post_eject
>  		? min(0, common->ops->post_eject(common, curlun,
> @@ -1531,7 +1538,6 @@ static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
>  	return -EINVAL;
>  }
>  
> -
>  /*-------------------------------------------------------------------------*/
>  
>  static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
> @@ -1917,7 +1923,7 @@ static int check_command(struct fsg_common *common, int cmnd_size,
>  
>  	/* If the medium isn't mounted and the command needs to access
>  	 * it, return an error. */
> -	if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
> +	if (curlun && !fsg_lun_is_open_and_mounted(curlun) && needs_medium) {
>  		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
>  		return -EINVAL;
>  	}
> @@ -2533,6 +2539,7 @@ static void handle_exception(struct fsg_common *common)
>  			curlun->unit_attention_data = SS_NO_SENSE;
>  			curlun->sense_data_info = 0;
>  			curlun->info_valid = 0;
> +			curlun->media_ejected = 0;
>  		}
>  		common->state = FSG_STATE_IDLE;
>  	}
> diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
> index 8081ca3..723c6aa 100644
> --- a/drivers/usb/gadget/storage_common.c
> +++ b/drivers/usb/gadget/storage_common.c
> @@ -199,6 +199,7 @@ struct fsg_lun {
>  	unsigned int	registered:1;
>  	unsigned int	info_valid:1;
>  	unsigned int	nofua:1;
> +	unsigned int	media_ejected:1;
>  
>  	u32		sense_data;
>  	u32		sense_data_info;
> @@ -211,6 +212,16 @@ struct fsg_lun {
>  
>  #define fsg_lun_is_open(curlun)	((curlun)->filp != NULL)
>  
> +#define fsg_lun_is_ejected(curlun) ((curlun)->media_ejected)
> +
> +#define fsg_lun_set_ejected(curlun, v) ((curlun)->media_ejected = !!(v))
> +
> +#define fsg_lun_is_open_and_mounted(curlun) \
> +	({ \
> +		const struct fsg_lun *_curlun = (curlun); \
> +		fsg_lun_is_open(_curlun) && !fsg_lun_is_ejected(_curlun); \
> +	 })
> +
>  static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
>  {
>  	return container_of(dev, struct fsg_lun, dev);


Acked-by: Russ.Dill@ti.com



^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH] [g_mass_storage] Fix unmount problem with OS-X
@ 2012-05-16  8:54 Pantelis Antoniou
  2012-05-15 15:58 ` Russ Dill
  0 siblings, 1 reply; 2+ messages in thread
From: Pantelis Antoniou @ 2012-05-16  8:54 UTC (permalink / raw)
  To: Russ.Dill, linux-kernel; +Cc: Pantelis Antoniou

OS-X uses a sequence on unmounting that makes us close the backing
file. Apparently there's no way to open it again, which makes it
impossible to remount without rmmod'ing g_mass_storage.

Fix this by adding a media_ejected flag to each lun, and keeping
track of it properly.

Any other method (i.e. preventing unmount, and not keeping track
of the eject status) failed due to OS-X sending start stop commands
after the unmount and checking again.
---
 drivers/usb/gadget/f_mass_storage.c |   21 ++++++++++++++-------
 drivers/usb/gadget/storage_common.c |   11 +++++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index cb8c162..b865d20 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -1445,6 +1445,9 @@ static int do_start_stop(struct fsg_common *common)
 			curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
 			return -EINVAL;
 		}
+
+		/* if the host is trying again, the media are mounted */
+		fsg_lun_set_ejected(curlun, 0);
 		return 0;
 	}
 
@@ -1468,11 +1471,15 @@ static int do_start_stop(struct fsg_common *common)
 			return 0;
 	}
 
-	up_read(&common->filesem);
-	down_write(&common->filesem);
-	fsg_lun_close(curlun);
-	up_write(&common->filesem);
-	down_read(&common->filesem);
+	/*
+	 * Do not close the LUNs, just made them invisible.
+	 * When the next exception comes we make them visible
+	 * again. This fixes the bug where MacOSX unmounts
+	 * and then it's impossible to mount again, since
+	 * fsg_lun_open is never called again.
+	 */
+
+	fsg_lun_set_ejected(curlun, 1);
 
 	return common->ops && common->ops->post_eject
 		? min(0, common->ops->post_eject(common, curlun,
@@ -1531,7 +1538,6 @@ static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
 	return -EINVAL;
 }
 
-
 /*-------------------------------------------------------------------------*/
 
 static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
@@ -1917,7 +1923,7 @@ static int check_command(struct fsg_common *common, int cmnd_size,
 
 	/* If the medium isn't mounted and the command needs to access
 	 * it, return an error. */
-	if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
+	if (curlun && !fsg_lun_is_open_and_mounted(curlun) && needs_medium) {
 		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
 		return -EINVAL;
 	}
@@ -2533,6 +2539,7 @@ static void handle_exception(struct fsg_common *common)
 			curlun->unit_attention_data = SS_NO_SENSE;
 			curlun->sense_data_info = 0;
 			curlun->info_valid = 0;
+			curlun->media_ejected = 0;
 		}
 		common->state = FSG_STATE_IDLE;
 	}
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 8081ca3..723c6aa 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -199,6 +199,7 @@ struct fsg_lun {
 	unsigned int	registered:1;
 	unsigned int	info_valid:1;
 	unsigned int	nofua:1;
+	unsigned int	media_ejected:1;
 
 	u32		sense_data;
 	u32		sense_data_info;
@@ -211,6 +212,16 @@ struct fsg_lun {
 
 #define fsg_lun_is_open(curlun)	((curlun)->filp != NULL)
 
+#define fsg_lun_is_ejected(curlun) ((curlun)->media_ejected)
+
+#define fsg_lun_set_ejected(curlun, v) ((curlun)->media_ejected = !!(v))
+
+#define fsg_lun_is_open_and_mounted(curlun) \
+	({ \
+		const struct fsg_lun *_curlun = (curlun); \
+		fsg_lun_is_open(_curlun) && !fsg_lun_is_ejected(_curlun); \
+	 })
+
 static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
 {
 	return container_of(dev, struct fsg_lun, dev);
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-05-15 15:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-16  8:54 [PATCH] [g_mass_storage] Fix unmount problem with OS-X Pantelis Antoniou
2012-05-15 15:58 ` Russ Dill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox