@@ -1778,35 +1778,39 @@ struct smbd_connection *smbd_get_connection(
17781778}
17791779
17801780/*
1781- * Receive data from receive reassembly queue
1781+ * Receive data from the transport's receive reassembly queue
17821782 * All the incoming data packets are placed in reassembly queue
1783- * buf : the buffer to read data into
1783+ * iter : the buffer to read data into
17841784 * size: the length of data to read
17851785 * return value: actual data read
1786- * Note: this implementation copies the data from reassebmly queue to receive
1786+ *
1787+ * Note: this implementation copies the data from reassembly queue to receive
17871788 * buffers used by upper layer. This is not the optimal code path. A better way
17881789 * to do it is to not have upper layer allocate its receive buffers but rather
17891790 * borrow the buffer from reassembly queue, and return it after data is
17901791 * consumed. But this will require more changes to upper layer code, and also
17911792 * need to consider packet boundaries while they still being reassembled.
17921793 */
1793- static int smbd_recv_buf (struct smbd_connection * info , char * buf ,
1794- unsigned int size )
1794+ int smbd_recv (struct smbd_connection * info , struct msghdr * msg )
17951795{
17961796 struct smbdirect_socket * sc = & info -> socket ;
17971797 struct smbd_response * response ;
17981798 struct smbdirect_data_transfer * data_transfer ;
1799+ size_t size = iov_iter_count (& msg -> msg_iter );
17991800 int to_copy , to_read , data_read , offset ;
18001801 u32 data_length , remaining_data_length , data_offset ;
18011802 int rc ;
18021803
1804+ if (WARN_ON_ONCE (iov_iter_rw (& msg -> msg_iter ) == WRITE ))
1805+ return - EINVAL ; /* It's a bug in upper layer to get there */
1806+
18031807again :
18041808 /*
18051809 * No need to hold the reassembly queue lock all the time as we are
18061810 * the only one reading from the front of the queue. The transport
18071811 * may add more entries to the back of the queue at the same time
18081812 */
1809- log_read (INFO , "size=%d info->reassembly_data_length=%d\n" , size ,
1813+ log_read (INFO , "size=%zd info->reassembly_data_length=%d\n" , size ,
18101814 info -> reassembly_data_length );
18111815 if (info -> reassembly_data_length >= size ) {
18121816 int queue_length ;
@@ -1844,7 +1848,10 @@ static int smbd_recv_buf(struct smbd_connection *info, char *buf,
18441848 if (response -> first_segment && size == 4 ) {
18451849 unsigned int rfc1002_len =
18461850 data_length + remaining_data_length ;
1847- * ((__be32 * )buf ) = cpu_to_be32 (rfc1002_len );
1851+ __be32 rfc1002_hdr = cpu_to_be32 (rfc1002_len );
1852+ if (copy_to_iter (& rfc1002_hdr , sizeof (rfc1002_hdr ),
1853+ & msg -> msg_iter ) != sizeof (rfc1002_hdr ))
1854+ return - EFAULT ;
18481855 data_read = 4 ;
18491856 response -> first_segment = false;
18501857 log_read (INFO , "returning rfc1002 length %d\n" ,
@@ -1853,10 +1860,9 @@ static int smbd_recv_buf(struct smbd_connection *info, char *buf,
18531860 }
18541861
18551862 to_copy = min_t (int , data_length - offset , to_read );
1856- memcpy (
1857- buf + data_read ,
1858- (char * )data_transfer + data_offset + offset ,
1859- to_copy );
1863+ if (copy_to_iter ((char * )data_transfer + data_offset + offset ,
1864+ to_copy , & msg -> msg_iter ) != to_copy )
1865+ return - EFAULT ;
18601866
18611867 /* move on to the next buffer? */
18621868 if (to_copy == data_length - offset ) {
@@ -1921,90 +1927,6 @@ static int smbd_recv_buf(struct smbd_connection *info, char *buf,
19211927 goto again ;
19221928}
19231929
1924- /*
1925- * Receive a page from receive reassembly queue
1926- * page: the page to read data into
1927- * to_read: the length of data to read
1928- * return value: actual data read
1929- */
1930- static int smbd_recv_page (struct smbd_connection * info ,
1931- struct page * page , unsigned int page_offset ,
1932- unsigned int to_read )
1933- {
1934- struct smbdirect_socket * sc = & info -> socket ;
1935- int ret ;
1936- char * to_address ;
1937- void * page_address ;
1938-
1939- /* make sure we have the page ready for read */
1940- ret = wait_event_interruptible (
1941- info -> wait_reassembly_queue ,
1942- info -> reassembly_data_length >= to_read ||
1943- sc -> status != SMBDIRECT_SOCKET_CONNECTED );
1944- if (ret )
1945- return ret ;
1946-
1947- /* now we can read from reassembly queue and not sleep */
1948- page_address = kmap_atomic (page );
1949- to_address = (char * ) page_address + page_offset ;
1950-
1951- log_read (INFO , "reading from page=%p address=%p to_read=%d\n" ,
1952- page , to_address , to_read );
1953-
1954- ret = smbd_recv_buf (info , to_address , to_read );
1955- kunmap_atomic (page_address );
1956-
1957- return ret ;
1958- }
1959-
1960- /*
1961- * Receive data from transport
1962- * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
1963- * return: total bytes read, or 0. SMB Direct will not do partial read.
1964- */
1965- int smbd_recv (struct smbd_connection * info , struct msghdr * msg )
1966- {
1967- char * buf ;
1968- struct page * page ;
1969- unsigned int to_read , page_offset ;
1970- int rc ;
1971-
1972- if (iov_iter_rw (& msg -> msg_iter ) == WRITE ) {
1973- /* It's a bug in upper layer to get there */
1974- cifs_dbg (VFS , "Invalid msg iter dir %u\n" ,
1975- iov_iter_rw (& msg -> msg_iter ));
1976- rc = - EINVAL ;
1977- goto out ;
1978- }
1979-
1980- switch (iov_iter_type (& msg -> msg_iter )) {
1981- case ITER_KVEC :
1982- buf = msg -> msg_iter .kvec -> iov_base ;
1983- to_read = msg -> msg_iter .kvec -> iov_len ;
1984- rc = smbd_recv_buf (info , buf , to_read );
1985- break ;
1986-
1987- case ITER_BVEC :
1988- page = msg -> msg_iter .bvec -> bv_page ;
1989- page_offset = msg -> msg_iter .bvec -> bv_offset ;
1990- to_read = msg -> msg_iter .bvec -> bv_len ;
1991- rc = smbd_recv_page (info , page , page_offset , to_read );
1992- break ;
1993-
1994- default :
1995- /* It's a bug in upper layer to get there */
1996- cifs_dbg (VFS , "Invalid msg type %d\n" ,
1997- iov_iter_type (& msg -> msg_iter ));
1998- rc = - EINVAL ;
1999- }
2000-
2001- out :
2002- /* SMBDirect will read it all or nothing */
2003- if (rc > 0 )
2004- msg -> msg_iter .count = 0 ;
2005- return rc ;
2006- }
2007-
20081930/*
20091931 * Send data to transport
20101932 * Each rqst is transported as a SMBDirect payload
0 commit comments