Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: f_mass_storage: limit number of reported LUNs

Mass storage function created via configfs always reports eight LUNs
to the hosts even if only one LUN has been configured. Adjust the
number when the USB function is allocated based on LUNs that user
has created.

Cc: <stable@vger.kernel.org>
Tested-by: Gregory CLEMENT <gregory.clement@free-electrons.com>
Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>

authored by

Michal Nazarewicz and committed by
Felipe Balbi
8515bac0 43cacb03

+13 -3
+13 -3
drivers/usb/gadget/function/f_mass_storage.c
··· 2786 2786 return -EINVAL; 2787 2787 } 2788 2788 2789 - curlun = kcalloc(nluns, sizeof(*curlun), GFP_KERNEL); 2789 + curlun = kcalloc(FSG_MAX_LUNS, sizeof(*curlun), GFP_KERNEL); 2790 2790 if (unlikely(!curlun)) 2791 2791 return -ENOMEM; 2792 2792 ··· 2795 2795 2796 2796 common->luns = curlun; 2797 2797 common->nluns = nluns; 2798 - 2799 - pr_info("Number of LUNs=%d\n", common->nluns); 2800 2798 2801 2799 return 0; 2802 2800 } ··· 3561 3563 struct fsg_opts *opts = fsg_opts_from_func_inst(fi); 3562 3564 struct fsg_common *common = opts->common; 3563 3565 struct fsg_dev *fsg; 3566 + unsigned nluns, i; 3564 3567 3565 3568 fsg = kzalloc(sizeof(*fsg), GFP_KERNEL); 3566 3569 if (unlikely(!fsg)) 3567 3570 return ERR_PTR(-ENOMEM); 3568 3571 3569 3572 mutex_lock(&opts->lock); 3573 + if (!opts->refcnt) { 3574 + for (nluns = i = 0; i < FSG_MAX_LUNS; ++i) 3575 + if (common->luns[i]) 3576 + nluns = i + 1; 3577 + if (!nluns) 3578 + pr_warn("No LUNS defined, continuing anyway\n"); 3579 + else 3580 + common->nluns = nluns; 3581 + pr_info("Number of LUNs=%u\n", common->nluns); 3582 + } 3570 3583 opts->refcnt++; 3571 3584 mutex_unlock(&opts->lock); 3585 + 3572 3586 fsg->function.name = FSG_DRIVER_DESC; 3573 3587 fsg->function.bind = fsg_bind; 3574 3588 fsg->function.unbind = fsg_unbind;