Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[muen/linux.git] / drivers / target / target_core_transport.c
index 74b646f165d4e4fea16c955884dcf2a119cb82c3..4558f2e1fe1bb5ae3b4907f6ba4b86cb7f1d2e3a 100644 (file)
@@ -2303,13 +2303,7 @@ queue_full:
 
 void target_free_sgl(struct scatterlist *sgl, int nents)
 {
-       struct scatterlist *sg;
-       int count;
-
-       for_each_sg(sgl, sg, nents, count)
-               __free_page(sg_page(sg));
-
-       kfree(sgl);
+       sgl_free_n_order(sgl, nents, 0);
 }
 EXPORT_SYMBOL(target_free_sgl);
 
@@ -2417,42 +2411,10 @@ int
 target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
                 bool zero_page, bool chainable)
 {
-       struct scatterlist *sg;
-       struct page *page;
-       gfp_t zero_flag = (zero_page) ? __GFP_ZERO : 0;
-       unsigned int nalloc, nent;
-       int i = 0;
-
-       nalloc = nent = DIV_ROUND_UP(length, PAGE_SIZE);
-       if (chainable)
-               nalloc++;
-       sg = kmalloc_array(nalloc, sizeof(struct scatterlist), GFP_KERNEL);
-       if (!sg)
-               return -ENOMEM;
+       gfp_t gfp = GFP_KERNEL | (zero_page ? __GFP_ZERO : 0);
 
-       sg_init_table(sg, nalloc);
-
-       while (length) {
-               u32 page_len = min_t(u32, length, PAGE_SIZE);
-               page = alloc_page(GFP_KERNEL | zero_flag);
-               if (!page)
-                       goto out;
-
-               sg_set_page(&sg[i], page, page_len, 0);
-               length -= page_len;
-               i++;
-       }
-       *sgl = sg;
-       *nents = nent;
-       return 0;
-
-out:
-       while (i > 0) {
-               i--;
-               __free_page(sg_page(&sg[i]));
-       }
-       kfree(sg);
-       return -ENOMEM;
+       *sgl = sgl_alloc_order(length, 0, chainable, gfp, nents);
+       return *sgl ? 0 : -ENOMEM;
 }
 EXPORT_SYMBOL(target_alloc_sgl);