From mboxrd@z Thu Jan 1 00:00:00 1970 From: Randy Dunlap Subject: Re: dummy file read periodically for external USB Hard Disk Date: Fri, 11 Jul 2014 20:01:48 -0700 Message-ID: <53C0A51C.5020906@infradead.org> References: <20140708130127.0f29a46f@notabene.brown> <20140710104704.4c3fdb51@notabene.brown> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: Linux-FSDevel To: loody , NeilBrown Return-path: Received: from bombadil.infradead.org ([198.137.202.9]:45270 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750768AbaGLDBu (ORCPT ); Fri, 11 Jul 2014 23:01:50 -0400 In-Reply-To: Sender: linux-fsdevel-owner@vger.kernel.org List-ID: On 07/11/14 02:04, loody wrote: > hi NeilBrown: > > 2014-07-10 8:47 GMT+08:00 NeilBrown : >> On Wed, 9 Jul 2014 22:13:28 +0800 loody wrote: >> >>> hi NeilBrown: >>> we use below c source but there is no read command firing from usb >>> host to device. >>> except O_DIRECT, is there any flag we need to use? >>> appreciate all your kind help, >>> >>> #include >>> #include >>> #include >>> #include >>> #include >>> >>> char message[] = "/mnt/usb/4854344154343452/ >>> test.txt"; >>> int main() >>> { >>> int fd; >>> char buffer[5]; >>> int count = 0; >>> char *buf="1234567890"; >>> if((fd=open(message,O_CREAT|O_TRUNC|O_RDWR|O_DIRECT, 0777))<0) >> >> I said "Open the device with O_DIRECT". You are opening a file in the >> filesystem which is mounted from the device. That is a different thing. > sorry for misunderstanding your explanation. > >> >>> { >>> perror("open"); >>> return -1; >>> } >>> printf("fd=%d\n", fd); >>> write(fd, buf, strlen(buf)); >>> while(1){ >>> lseek(fd,0,SEEK_SET); >>> sleep(3); >>> count = read(fd, buffer, 3); >> >> Did you do any research to understand how O_DIRECT works? >> I recommend the man page for "open(2)". >> >> You need to read thoroughly, but towards the end it says: >> >> Under Linux 2.4, transfer sizes, and the alignment of the user buffer and the >> file offset must all be multiples of the logical block size of the file sys- >> tem. Under Linux 2.6, alignment to 512-byte boundaries suffices. >> >> Neither your buffer nor your IO size is 512-byte aligned. > I follow your suggestion and try to read /dev/sda or /dev/sda1 for > 512Bytes like below. > But the read back count is -1, that mean the read is not successful. > I try to open both with "O_DIRECT" or "O_DIRECT|O_RDONLY" but all of > them get read back count are -1. > Does that mean block device node not support system read command? > appreciate your help, It means that the still is not aligned to a 512-byte boundary. Try char buffer[1024] __attribute__ ((aligned(1024))); Well, that is aligned to 1024 bytes, not 512, but whatever. > > #define _GNU_SOURCE > #include > #include > #include > #include > #include > > char message[] = "/dev/sda1"; > int main() > { > int fd; > char buffer[1024]; > int count; > //if((fd=open(message,O_DIRECT))<0) > if((fd=open(message,O_DIRECT|O_RDONLY))<0) > { > perror("open"); > return -1; > } > printf("fd=%d\n", fd); > while(1){ > sleep(3); > count = read(fd, buffer, 512); //read back fail > printf("count=%d,%x,%x,%x\n", count,buffer[0],buffer[1],buffer[2]); > } > close(fd); > } -- ~Randy