From 20ac8bbc2c6d521bbfd78ae9f91b9f9e76b432b3 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 19 Mar 2022 12:02:43 +0100 Subject: usb: gadget: mass-storage: fix clean up of file descriptors When encountering an issue during bind time, we currently close only the last opened file descriptor, leaking others when more than one LUN was registered. Fix this. Fixes: 57313f83e83e ("usbgadget: add support for USB mass storage gadget") Signed-off-by: Ahmad Fatoum Link: https://lore.barebox.org/20220319110246.2850396-4-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer --- drivers/usb/gadget/f_mass_storage.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'drivers/usb') diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 753042125d..a49ac78033 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -2419,7 +2419,7 @@ static int fsg_common_init(struct fsg_common *common, struct usb_gadget *gadget = cdev->gadget; struct file_list_entry *fentry; struct fsg_buffhd *bh; - int nluns, i, fd = -1, rc; + int nluns, i, rc; ums_count = 0; @@ -2436,17 +2436,20 @@ static int fsg_common_init(struct fsg_common *common, file_list_for_each_entry(ums_files, fentry) { unsigned flags = O_RDWR; struct stat st; + int fd; if (fentry->flags) { pr_err("flags not supported\n"); - return -ENOSYS; + rc = -ENOSYS; + goto close; } fd = open(fentry->filename, flags); if (fd < 0) { pr_err("open('%s') failed: %pe\n", fentry->filename, ERR_PTR(fd)); - return fd; + rc = fd; + goto close; } rc = fstat(fd, &st); @@ -2543,7 +2546,8 @@ error_release: common->state = FSG_STATE_TERMINATED; /* The thread is dead */ fsg_common_release(common); close: - close(fd); + for (i = 0; i < ums_count; i++) + close(ums[i].fd); return rc; } -- cgit v1.2.3