The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_sglist.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2008 Yahoo!, Inc.
    3  * All rights reserved.
    4  * Written by: John Baldwin <jhb@FreeBSD.org>
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. Neither the name of the author nor the names of any co-contributors
   15  *    may be used to endorse or promote products derived from this software
   16  *    without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD: releng/10.2/sys/kern/subr_sglist.c 260856 2014-01-18 18:36:41Z bryanv $");
   33 
   34 #include <sys/param.h>
   35 #include <sys/kernel.h>
   36 #include <sys/bio.h>
   37 #include <sys/malloc.h>
   38 #include <sys/mbuf.h>
   39 #include <sys/proc.h>
   40 #include <sys/sglist.h>
   41 #include <sys/uio.h>
   42 
   43 #include <vm/vm.h>
   44 #include <vm/vm_page.h>
   45 #include <vm/pmap.h>
   46 #include <vm/vm_map.h>
   47 
   48 #include <sys/ktr.h>
   49 
   50 static MALLOC_DEFINE(M_SGLIST, "sglist", "scatter/gather lists");
   51 
   52 /*
   53  * Convenience macros to save the state of an sglist so it can be restored
   54  * if an append attempt fails.  Since sglist's only grow we only need to
   55  * save the current count of segments and the length of the ending segment.
   56  * Earlier segments will not be changed by an append, and the only change
   57  * that can occur to the ending segment is that it can be extended.
   58  */
   59 struct sgsave {
   60         u_short sg_nseg;
   61         size_t ss_len;
   62 };
   63 
   64 #define SGLIST_SAVE(sg, sgsave) do {                                    \
   65         (sgsave).sg_nseg = (sg)->sg_nseg;                               \
   66         if ((sgsave).sg_nseg > 0)                                       \
   67                 (sgsave).ss_len = (sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len; \
   68         else                                                            \
   69                 (sgsave).ss_len = 0;                                    \
   70 } while (0)
   71 
   72 #define SGLIST_RESTORE(sg, sgsave) do {                                 \
   73         (sg)->sg_nseg = (sgsave).sg_nseg;                               \
   74         if ((sgsave).sg_nseg > 0)                                       \
   75                 (sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len = (sgsave).ss_len; \
   76 } while (0)
   77 
   78 /*
   79  * Append a single (paddr, len) to a sglist.  sg is the list and ss is
   80  * the current segment in the list.  If we run out of segments then
   81  * EFBIG will be returned.
   82  */
   83 static __inline int
   84 _sglist_append_range(struct sglist *sg, struct sglist_seg **ssp,
   85     vm_paddr_t paddr, size_t len)
   86 {
   87         struct sglist_seg *ss;
   88 
   89         ss = *ssp;
   90         if (ss->ss_paddr + ss->ss_len == paddr)
   91                 ss->ss_len += len;
   92         else {
   93                 if (sg->sg_nseg == sg->sg_maxseg)
   94                         return (EFBIG);
   95                 ss++;
   96                 ss->ss_paddr = paddr;
   97                 ss->ss_len = len;
   98                 sg->sg_nseg++;
   99                 *ssp = ss;
  100         }
  101         return (0);
  102 }
  103 
  104 /*
  105  * Worker routine to append a virtual address range (either kernel or
  106  * user) to a scatter/gather list.
  107  */
  108 static __inline int
  109 _sglist_append_buf(struct sglist *sg, void *buf, size_t len, pmap_t pmap,
  110     size_t *donep)
  111 {
  112         struct sglist_seg *ss;
  113         vm_offset_t vaddr, offset;
  114         vm_paddr_t paddr;
  115         size_t seglen;
  116         int error;
  117 
  118         if (donep)
  119                 *donep = 0;
  120         if (len == 0)
  121                 return (0);
  122 
  123         /* Do the first page.  It may have an offset. */
  124         vaddr = (vm_offset_t)buf;
  125         offset = vaddr & PAGE_MASK;
  126         if (pmap != NULL)
  127                 paddr = pmap_extract(pmap, vaddr);
  128         else
  129                 paddr = pmap_kextract(vaddr);
  130         seglen = MIN(len, PAGE_SIZE - offset);
  131         if (sg->sg_nseg == 0) {
  132                 ss = sg->sg_segs;
  133                 ss->ss_paddr = paddr;
  134                 ss->ss_len = seglen;
  135                 sg->sg_nseg = 1;
  136         } else {
  137                 ss = &sg->sg_segs[sg->sg_nseg - 1];
  138                 error = _sglist_append_range(sg, &ss, paddr, seglen);
  139                 if (error)
  140                         return (error);
  141         }
  142         vaddr += seglen;
  143         len -= seglen;
  144         if (donep)
  145                 *donep += seglen;
  146 
  147         while (len > 0) {
  148                 seglen = MIN(len, PAGE_SIZE);
  149                 if (pmap != NULL)
  150                         paddr = pmap_extract(pmap, vaddr);
  151                 else
  152                         paddr = pmap_kextract(vaddr);
  153                 error = _sglist_append_range(sg, &ss, paddr, seglen);
  154                 if (error)
  155                         return (error);
  156                 vaddr += seglen;
  157                 len -= seglen;
  158                 if (donep)
  159                         *donep += seglen;
  160         }
  161 
  162         return (0);
  163 }
  164 
  165 /*
  166  * Determine the number of scatter/gather list elements needed to
  167  * describe a kernel virtual address range.
  168  */
  169 int
  170 sglist_count(void *buf, size_t len)
  171 {
  172         vm_offset_t vaddr, vendaddr;
  173         vm_paddr_t lastaddr, paddr;
  174         int nsegs;
  175 
  176         if (len == 0)
  177                 return (0);
  178 
  179         vaddr = trunc_page((vm_offset_t)buf);
  180         vendaddr = (vm_offset_t)buf + len;
  181         nsegs = 1;
  182         lastaddr = pmap_kextract(vaddr);
  183         vaddr += PAGE_SIZE;
  184         while (vaddr < vendaddr) {
  185                 paddr = pmap_kextract(vaddr);
  186                 if (lastaddr + PAGE_SIZE != paddr)
  187                         nsegs++;
  188                 lastaddr = paddr;
  189                 vaddr += PAGE_SIZE;
  190         }
  191         return (nsegs);
  192 }
  193 
  194 /*
  195  * Allocate a scatter/gather list along with 'nsegs' segments.  The
  196  * 'mflags' parameters are the same as passed to malloc(9).  The caller
  197  * should use sglist_free() to free this list.
  198  */
  199 struct sglist *
  200 sglist_alloc(int nsegs, int mflags)
  201 {
  202         struct sglist *sg;
  203 
  204         sg = malloc(sizeof(struct sglist) + nsegs * sizeof(struct sglist_seg),
  205             M_SGLIST, mflags);
  206         if (sg == NULL)
  207                 return (NULL);
  208         sglist_init(sg, nsegs, (struct sglist_seg *)(sg + 1));
  209         return (sg);
  210 }
  211 
  212 /*
  213  * Free a scatter/gather list allocated via sglist_allc().
  214  */
  215 void
  216 sglist_free(struct sglist *sg)
  217 {
  218 
  219         if (refcount_release(&sg->sg_refs))
  220                 free(sg, M_SGLIST);
  221 }
  222 
  223 /*
  224  * Append the segments to describe a single kernel virtual address
  225  * range to a scatter/gather list.  If there are insufficient
  226  * segments, then this fails with EFBIG.
  227  */
  228 int
  229 sglist_append(struct sglist *sg, void *buf, size_t len)
  230 {
  231         struct sgsave save;
  232         int error;
  233 
  234         if (sg->sg_maxseg == 0)
  235                 return (EINVAL);
  236         SGLIST_SAVE(sg, save);
  237         error = _sglist_append_buf(sg, buf, len, NULL, NULL);
  238         if (error)
  239                 SGLIST_RESTORE(sg, save);
  240         return (error);
  241 }
  242 
  243 /*
  244  * Append the segments to describe a bio's data to a scatter/gather list.
  245  * If there are insufficient segments, then this fails with EFBIG.
  246  *
  247  * NOTE: This function expects bio_bcount to be initialized.
  248  */
  249 int
  250 sglist_append_bio(struct sglist *sg, struct bio *bp)
  251 {
  252         struct sgsave save;
  253         vm_paddr_t paddr;
  254         size_t len, tlen;
  255         int error, i, ma_offs;
  256 
  257         if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
  258                 error = sglist_append(sg, bp->bio_data, bp->bio_bcount);
  259                 return (error);
  260         }
  261 
  262         if (sg->sg_maxseg == 0)
  263                 return (EINVAL);
  264 
  265         SGLIST_SAVE(sg, save);
  266         tlen = bp->bio_bcount;
  267         ma_offs = bp->bio_ma_offset;
  268         for (i = 0; tlen > 0; i++, tlen -= len) {
  269                 len = min(PAGE_SIZE - ma_offs, tlen);
  270                 paddr = VM_PAGE_TO_PHYS(bp->bio_ma[i]) + ma_offs;
  271                 error = sglist_append_phys(sg, paddr, len);
  272                 if (error) {
  273                         SGLIST_RESTORE(sg, save);
  274                         return (error);
  275                 }
  276                 ma_offs = 0;
  277         }
  278         return (0);
  279 }
  280 
  281 /*
  282  * Append a single physical address range to a scatter/gather list.
  283  * If there are insufficient segments, then this fails with EFBIG.
  284  */
  285 int
  286 sglist_append_phys(struct sglist *sg, vm_paddr_t paddr, size_t len)
  287 {
  288         struct sglist_seg *ss;
  289         struct sgsave save;
  290         int error;
  291 
  292         if (sg->sg_maxseg == 0)
  293                 return (EINVAL);
  294         if (len == 0)
  295                 return (0);
  296 
  297         if (sg->sg_nseg == 0) {
  298                 sg->sg_segs[0].ss_paddr = paddr;
  299                 sg->sg_segs[0].ss_len = len;
  300                 sg->sg_nseg = 1;
  301                 return (0);
  302         }
  303         ss = &sg->sg_segs[sg->sg_nseg - 1];
  304         SGLIST_SAVE(sg, save);
  305         error = _sglist_append_range(sg, &ss, paddr, len);
  306         if (error)
  307                 SGLIST_RESTORE(sg, save);
  308         return (error);
  309 }
  310 
  311 /*
  312  * Append the segments that describe a single mbuf chain to a
  313  * scatter/gather list.  If there are insufficient segments, then this
  314  * fails with EFBIG.
  315  */
  316 int
  317 sglist_append_mbuf(struct sglist *sg, struct mbuf *m0)
  318 {
  319         struct sgsave save;
  320         struct mbuf *m;
  321         int error;
  322 
  323         if (sg->sg_maxseg == 0)
  324                 return (EINVAL);
  325 
  326         error = 0;
  327         SGLIST_SAVE(sg, save);
  328         for (m = m0; m != NULL; m = m->m_next) {
  329                 if (m->m_len > 0) {
  330                         error = sglist_append(sg, m->m_data, m->m_len);
  331                         if (error) {
  332                                 SGLIST_RESTORE(sg, save);
  333                                 return (error);
  334                         }
  335                 }
  336         }
  337         return (0);
  338 }
  339 
  340 /*
  341  * Append the segments that describe a single user address range to a
  342  * scatter/gather list.  If there are insufficient segments, then this
  343  * fails with EFBIG.
  344  */
  345 int
  346 sglist_append_user(struct sglist *sg, void *buf, size_t len, struct thread *td)
  347 {
  348         struct sgsave save;
  349         int error;
  350 
  351         if (sg->sg_maxseg == 0)
  352                 return (EINVAL);
  353         SGLIST_SAVE(sg, save);
  354         error = _sglist_append_buf(sg, buf, len,
  355             vmspace_pmap(td->td_proc->p_vmspace), NULL);
  356         if (error)
  357                 SGLIST_RESTORE(sg, save);
  358         return (error);
  359 }
  360 
  361 /*
  362  * Append the segments that describe a single uio to a scatter/gather
  363  * list.  If there are insufficient segments, then this fails with
  364  * EFBIG.
  365  */
  366 int
  367 sglist_append_uio(struct sglist *sg, struct uio *uio)
  368 {
  369         struct iovec *iov;
  370         struct sgsave save;
  371         size_t resid, minlen;
  372         pmap_t pmap;
  373         int error, i;
  374 
  375         if (sg->sg_maxseg == 0)
  376                 return (EINVAL);
  377 
  378         resid = uio->uio_resid;
  379         iov = uio->uio_iov;
  380 
  381         if (uio->uio_segflg == UIO_USERSPACE) {
  382                 KASSERT(uio->uio_td != NULL,
  383                     ("sglist_append_uio: USERSPACE but no thread"));
  384                 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
  385         } else
  386                 pmap = NULL;
  387 
  388         error = 0;
  389         SGLIST_SAVE(sg, save);
  390         for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
  391                 /*
  392                  * Now at the first iovec to load.  Load each iovec
  393                  * until we have exhausted the residual count.
  394                  */
  395                 minlen = MIN(resid, iov[i].iov_len);
  396                 if (minlen > 0) {
  397                         error = _sglist_append_buf(sg, iov[i].iov_base, minlen,
  398                             pmap, NULL);
  399                         if (error) {
  400                                 SGLIST_RESTORE(sg, save);
  401                                 return (error);
  402                         }
  403                         resid -= minlen;
  404                 }
  405         }
  406         return (0);
  407 }
  408 
  409 /*
  410  * Append the segments that describe at most 'resid' bytes from a
  411  * single uio to a scatter/gather list.  If there are insufficient
  412  * segments, then only the amount that fits is appended.
  413  */
  414 int
  415 sglist_consume_uio(struct sglist *sg, struct uio *uio, size_t resid)
  416 {
  417         struct iovec *iov;
  418         size_t done;
  419         pmap_t pmap;
  420         int error, len;
  421 
  422         if (sg->sg_maxseg == 0)
  423                 return (EINVAL);
  424 
  425         if (uio->uio_segflg == UIO_USERSPACE) {
  426                 KASSERT(uio->uio_td != NULL,
  427                     ("sglist_consume_uio: USERSPACE but no thread"));
  428                 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
  429         } else
  430                 pmap = NULL;
  431 
  432         error = 0;
  433         while (resid > 0 && uio->uio_resid) {
  434                 iov = uio->uio_iov;
  435                 len = iov->iov_len;
  436                 if (len == 0) {
  437                         uio->uio_iov++;
  438                         uio->uio_iovcnt--;
  439                         continue;
  440                 }
  441                 if (len > resid)
  442                         len = resid;
  443 
  444                 /*
  445                  * Try to append this iovec.  If we run out of room,
  446                  * then break out of the loop.
  447                  */
  448                 error = _sglist_append_buf(sg, iov->iov_base, len, pmap, &done);
  449                 iov->iov_base = (char *)iov->iov_base + done;
  450                 iov->iov_len -= done;
  451                 uio->uio_resid -= done;
  452                 uio->uio_offset += done;
  453                 resid -= done;
  454                 if (error)
  455                         break;
  456         }
  457         return (0);
  458 }
  459 
  460 /*
  461  * Allocate and populate a scatter/gather list to describe a single
  462  * kernel virtual address range.
  463  */
  464 struct sglist *
  465 sglist_build(void *buf, size_t len, int mflags)
  466 {
  467         struct sglist *sg;
  468         int nsegs;
  469 
  470         if (len == 0)
  471                 return (NULL);
  472 
  473         nsegs = sglist_count(buf, len);
  474         sg = sglist_alloc(nsegs, mflags);
  475         if (sg == NULL)
  476                 return (NULL);
  477         if (sglist_append(sg, buf, len) != 0) {
  478                 sglist_free(sg);
  479                 return (NULL);
  480         }
  481         return (sg);
  482 }
  483 
  484 /*
  485  * Clone a new copy of a scatter/gather list.
  486  */
  487 struct sglist *
  488 sglist_clone(struct sglist *sg, int mflags)
  489 {
  490         struct sglist *new;
  491 
  492         if (sg == NULL)
  493                 return (NULL);
  494         new = sglist_alloc(sg->sg_maxseg, mflags);
  495         if (new == NULL)
  496                 return (NULL);
  497         new->sg_nseg = sg->sg_nseg;
  498         bcopy(sg->sg_segs, new->sg_segs, sizeof(struct sglist_seg) *
  499             sg->sg_nseg);
  500         return (new);
  501 }
  502 
  503 /*
  504  * Calculate the total length of the segments described in a
  505  * scatter/gather list.
  506  */
  507 size_t
  508 sglist_length(struct sglist *sg)
  509 {
  510         size_t space;
  511         int i;
  512 
  513         space = 0;
  514         for (i = 0; i < sg->sg_nseg; i++)
  515                 space += sg->sg_segs[i].ss_len;
  516         return (space);
  517 }
  518 
  519 /*
  520  * Split a scatter/gather list into two lists.  The scatter/gather
  521  * entries for the first 'length' bytes of the 'original' list are
  522  * stored in the '*head' list and are removed from 'original'.
  523  *
  524  * If '*head' is NULL, then a new list will be allocated using
  525  * 'mflags'.  If M_NOWAIT is specified and the allocation fails,
  526  * ENOMEM will be returned.
  527  *
  528  * If '*head' is not NULL, it should point to an empty sglist.  If it
  529  * does not have enough room for the remaining space, then EFBIG will
  530  * be returned.  If '*head' is not empty, then EINVAL will be
  531  * returned.
  532  *
  533  * If 'original' is shared (refcount > 1), then EDOOFUS will be
  534  * returned.
  535  */
  536 int
  537 sglist_split(struct sglist *original, struct sglist **head, size_t length,
  538     int mflags)
  539 {
  540         struct sglist *sg;
  541         size_t space, split;
  542         int count, i;
  543 
  544         if (original->sg_refs > 1)
  545                 return (EDOOFUS);
  546 
  547         /* Figure out how big of a sglist '*head' has to hold. */
  548         count = 0;
  549         space = 0;
  550         split = 0;
  551         for (i = 0; i < original->sg_nseg; i++) {
  552                 space += original->sg_segs[i].ss_len;
  553                 count++;
  554                 if (space >= length) {
  555                         /*
  556                          * If 'length' falls in the middle of a
  557                          * scatter/gather list entry, then 'split'
  558                          * holds how much of that entry will remain in
  559                          * 'original'.
  560                          */
  561                         split = space - length;
  562                         break;
  563                 }
  564         }
  565 
  566         /* Nothing to do, so leave head empty. */
  567         if (count == 0)
  568                 return (0);
  569 
  570         if (*head == NULL) {
  571                 sg = sglist_alloc(count, mflags);
  572                 if (sg == NULL)
  573                         return (ENOMEM);
  574                 *head = sg;
  575         } else {
  576                 sg = *head;
  577                 if (sg->sg_maxseg < count)
  578                         return (EFBIG);
  579                 if (sg->sg_nseg != 0)
  580                         return (EINVAL);
  581         }
  582 
  583         /* Copy 'count' entries to 'sg' from 'original'. */
  584         bcopy(original->sg_segs, sg->sg_segs, count *
  585             sizeof(struct sglist_seg));
  586         sg->sg_nseg = count;
  587 
  588         /*
  589          * If we had to split a list entry, fixup the last entry in
  590          * 'sg' and the new first entry in 'original'.  We also
  591          * decrement 'count' by 1 since we will only be removing
  592          * 'count - 1' segments from 'original' now.
  593          */
  594         if (split != 0) {
  595                 count--;
  596                 sg->sg_segs[count].ss_len -= split;
  597                 original->sg_segs[count].ss_paddr =
  598                     sg->sg_segs[count].ss_paddr + split;
  599                 original->sg_segs[count].ss_len = split;
  600         }
  601 
  602         /* Trim 'count' entries from the front of 'original'. */
  603         original->sg_nseg -= count;
  604         bcopy(original->sg_segs + count, original->sg_segs, count *
  605             sizeof(struct sglist_seg));
  606         return (0);
  607 }
  608 
  609 /*
  610  * Append the scatter/gather list elements in 'second' to the
  611  * scatter/gather list 'first'.  If there is not enough space in
  612  * 'first', EFBIG is returned.
  613  */
  614 int
  615 sglist_join(struct sglist *first, struct sglist *second)
  616 {
  617         struct sglist_seg *flast, *sfirst;
  618         int append;
  619 
  620         /* If 'second' is empty, there is nothing to do. */
  621         if (second->sg_nseg == 0)
  622                 return (0);
  623 
  624         /*
  625          * If the first entry in 'second' can be appended to the last entry
  626          * in 'first' then set append to '1'.
  627          */
  628         append = 0;
  629         flast = &first->sg_segs[first->sg_nseg - 1];
  630         sfirst = &second->sg_segs[0];
  631         if (first->sg_nseg != 0 &&
  632             flast->ss_paddr + flast->ss_len == sfirst->ss_paddr)
  633                 append = 1;
  634 
  635         /* Make sure 'first' has enough room. */
  636         if (first->sg_nseg + second->sg_nseg - append > first->sg_maxseg)
  637                 return (EFBIG);
  638 
  639         /* Merge last in 'first' and first in 'second' if needed. */
  640         if (append)
  641                 flast->ss_len += sfirst->ss_len;
  642 
  643         /* Append new segments from 'second' to 'first'. */
  644         bcopy(first->sg_segs + first->sg_nseg, second->sg_segs + append,
  645             (second->sg_nseg - append) * sizeof(struct sglist_seg));
  646         first->sg_nseg += second->sg_nseg - append;
  647         sglist_reset(second);
  648         return (0);
  649 }
  650 
  651 /*
  652  * Generate a new scatter/gather list from a range of an existing
  653  * scatter/gather list.  The 'offset' and 'length' parameters specify
  654  * the logical range of the 'original' list to extract.  If that range
  655  * is not a subset of the length of 'original', then EINVAL is
  656  * returned.  The new scatter/gather list is stored in '*slice'.
  657  *
  658  * If '*slice' is NULL, then a new list will be allocated using
  659  * 'mflags'.  If M_NOWAIT is specified and the allocation fails,
  660  * ENOMEM will be returned.
  661  *
  662  * If '*slice' is not NULL, it should point to an empty sglist.  If it
  663  * does not have enough room for the remaining space, then EFBIG will
  664  * be returned.  If '*slice' is not empty, then EINVAL will be
  665  * returned.
  666  */
  667 int
  668 sglist_slice(struct sglist *original, struct sglist **slice, size_t offset,
  669     size_t length, int mflags)
  670 {
  671         struct sglist *sg;
  672         size_t space, end, foffs, loffs;
  673         int count, i, fseg;
  674 
  675         /* Nothing to do. */
  676         if (length == 0)
  677                 return (0);
  678 
  679         /* Figure out how many segments '*slice' needs to have. */
  680         end = offset + length;
  681         space = 0;
  682         count = 0;
  683         fseg = 0;
  684         foffs = loffs = 0;
  685         for (i = 0; i < original->sg_nseg; i++) {
  686                 space += original->sg_segs[i].ss_len;
  687                 if (space > offset) {
  688                         /*
  689                          * When we hit the first segment, store its index
  690                          * in 'fseg' and the offset into the first segment
  691                          * of 'offset' in 'foffs'.
  692                          */
  693                         if (count == 0) {
  694                                 fseg = i;
  695                                 foffs = offset - (space -
  696                                     original->sg_segs[i].ss_len);
  697                                 CTR1(KTR_DEV, "sglist_slice: foffs = %08lx",
  698                                     foffs);
  699                         }
  700                         count++;
  701 
  702                         /*
  703                          * When we hit the last segment, break out of
  704                          * the loop.  Store the amount of extra space
  705                          * at the end of this segment in 'loffs'.
  706                          */
  707                         if (space >= end) {
  708                                 loffs = space - end;
  709                                 CTR1(KTR_DEV, "sglist_slice: loffs = %08lx",
  710                                     loffs);
  711                                 break;
  712                         }
  713                 }
  714         }
  715 
  716         /* If we never hit 'end', then 'length' ran off the end, so fail. */
  717         if (space < end)
  718                 return (EINVAL);
  719 
  720         if (*slice == NULL) {
  721                 sg = sglist_alloc(count, mflags);
  722                 if (sg == NULL)
  723                         return (ENOMEM);
  724                 *slice = sg;
  725         } else {
  726                 sg = *slice;
  727                 if (sg->sg_maxseg < count)
  728                         return (EFBIG);
  729                 if (sg->sg_nseg != 0)
  730                         return (EINVAL);
  731         }
  732 
  733         /*
  734          * Copy over 'count' segments from 'original' starting at
  735          * 'fseg' to 'sg'.
  736          */
  737         bcopy(original->sg_segs + fseg, sg->sg_segs,
  738             count * sizeof(struct sglist_seg));
  739         sg->sg_nseg = count;
  740 
  741         /* Fixup first and last segments if needed. */
  742         if (foffs != 0) {
  743                 sg->sg_segs[0].ss_paddr += foffs;
  744                 sg->sg_segs[0].ss_len -= foffs;
  745                 CTR2(KTR_DEV, "sglist_slice seg[0]: %08lx:%08lx",
  746                     (long)sg->sg_segs[0].ss_paddr, sg->sg_segs[0].ss_len);
  747         }
  748         if (loffs != 0) {
  749                 sg->sg_segs[count - 1].ss_len -= loffs;
  750                 CTR2(KTR_DEV, "sglist_slice seg[%d]: len %08x", count - 1,
  751                     sg->sg_segs[count - 1].ss_len);
  752         }
  753         return (0);
  754 }

Cache object: bff314e6d86d88c5cd53f68177f8005c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.