title: aio_write() and lio_listio() of POSIX AIO don't work correctly in write operation
body: <p>I'm setting up a asynchronized writer and want operation order on file be guaranteed by that. I use it as writer in another program which read a pcap file. Consider I have already tried to use both linux-aio and POSIX aio in my code seperately, to write pcap file on disk. I need to know are aio_write() and lio_listio() of POSIX AIO guaranteed to be executed in order?</p>
  
  <p>In linux-aio I use iocb structure to represent a single request for write operation. After fixed number of single operation, io_getevents blocks until fixed number completion report.</p>
  
  <pre><code>struct iocb io_cb;
  memset(&amp;io_cb, 0, sizeof(io_cb));
  io_cb.aio_fildes = file_descriptor;
  io_cb.aio_lio_opcode = IOCB_CMD_PWRITE;
  io_cb.aio_reqprio = 0;
  io_cb.aio_buf = (__u64)(buffer);
  io_cb.aio_nbytes = size;
  io_cb.aio_offset = 0;
  struct iocb* io_cb_pointer = &amp;io_cb;
  static int32_t BLOCKS_COUNT = 1;
  syscall(SYS_io_submit, io_context, BLOCKS_COUNT, &amp;io_cb_pointer) 
  write_count++;
   if ((write_count == MAX_EVENT_COUNT) || flush == true)//flush is to flush before exit program.
      {
          struct io_event* events = (struct io_event *)malloc((write_count) * sizeof(struct io_event));
          syscall(SYS_io_getevents, io_context, write_count / 2, MAX_EVENT_COUNT, events, NULL)
      }
  </code></pre>
  
  <p>above piece of code which is writing responsible results in proper pcap output that can be opened by Wireshark.</p>
  
  <p>But my problem is with POSIX aio which seem doesn't guarantee order of operation in execution. The piece of writing code is demonstrated in below:</p>
  
  <pre><code>    aiocb_array = calloc(MAX_EVENT_COUNT, sizeof (struct aiocb*));
      aiocb_element = calloc(MAX_EVENT_COUNT, sizeof (struct aiocb));
  ...
      struct aiocb** aiocb_array;
      struct aiocb* aiocb_element;
  ...
      static size_t pre_nbytes = 0;
      static __off_t pre_offset = 24;//Due to pcap header file length
      aiocb_element[write_count].aio_fildes = file_descriptor;
      aiocb_element[write_count].aio_lio_opcode = LIO_WRITE;
      aiocb_element[write_count].aio_reqprio = 0;
      aiocb_element[write_count].aio_buf = (void *)(buffer);
      aiocb_element[write_count].aio_nbytes = size;
      aiocb_element[write_count].aio_offset = pre_nbytes + pre_offset;
  
      pre_nbytes = size;
      pre_offset = aiocb_element[write_count].aio_offset;
  
      aiocb_array[write_count] = &amp;aiocb_element[write_count];
      write_count++;
          if ((write_count == MAX_EVENT_COUNT) || flush == true )//flush is to flush before exit program.
      {
          if(flush)
          {
              if(lio_listio(LIO_WAIT, aiocb_array, write_count, NULL) != 0)
              {
                   printf("Flush lio_listio errno :%d\n", errno);
                   input-&gt;error_code = errno;
                   return false;
              }
          }
          else
          {
              if(lio_listio(LIO_NOWAIT, aiocb_array, write_count, NULL) != 0)
              {
                   printf("lio_listio errno :%d\n", errno);
                   input-&gt;error_code = errno;
                   return false;
              }
          }
  
          if(write_count == MAX_EVENT_COUNT)
              write_count = 0;
      }
  </code></pre>
  
  <p>As clear in above code, I prepare write request in aiocb structure and assign it's offset, because the file that is used to write was opened without append mode. the piece of code generates pcap file that has same byte like input pcap but unfortunately Wireshark can't open it and show error that indicate:"The capture file appears to be damaged or corrupt. (pcap: File has .......-byte packet, bigger than maximum 0f 65535)". But when I call lio_listio with LIO_WAIT mode and set MAX_EVENT_COUNT = 1, it generates proper pcap output that can be opened by Wireshark correctly.</p>
  
  <p>In addition when I replace lio_listio with aio_write function of POSIX, it produces corrupted file again.
  According to mentioned condition which results in proper output pcap, it seems that lio_listio and aio_write function of POSIX execution order are different with program submit order!!!
  Does it carried out the operations in arbitrary order? if the answer is yes why it ignore offset value and proper output are not generated? why linux-aio guarantee and keep operation order?
  if the answer is no so what is my code and logic problem?</p>
  
  <p>Any help will be appreciated.</p>