4

I have several pc connected to one network.

When I trying to write file with WriteFile function across network from several processes running on the different PC-s I getting ERROR_UNEXP_NET_ERR.

Details: Several PC-s does they own calculations and getting part of the common result wich should be writen to one file. One randomly choosen process calculates total file size and determines writing offsets for each process. This process creates the file and resize it up to calculated size.

After this I starting writing file in all processes simultaneously.

I open file on each process like this:

handle_ = ::CreateFile("////SERVERNAME//folder//filename.ext", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

And trying to write in the cycle like this:

const size_t BUFFER_SIZE = 30 * 1024;
std::vector<char> buffer(BUFFER_SIZE);
const size_t storage_size = storage_.size();
size_t internal_offset = 0;
while (size_t bytes = storage_.read(buffer.data(), buffer.size(), internal_offset))
{
    DWORD done = 0;

    LARGE_INTEGER li;
    li.QuadPart = offset + internal_offset;

    OVERLAPPED overlapped;
    overlapped.OffsetHigh = li.HighPart;
    overlapped.Offset = li.LowPart;
    overlapped.hEvent = 0;
    overlapped.Pointer = 0;
    overlapped.Internal = 0;
    overlapped.InternalHigh = 0;

    BOOL success = ::WriteFile(handle_, buffer, (DWORD)bytes, &done, &overlapped);
    if (!success || done != bytes)
            ThrowError(FFL, "Cannot write file: {1} ({2})", filepath_, GetLastError()); // Returns error code 59 (ERROR_UNEXP_NET_ERR)
    internal_offset += bytes;
}

This approch works with file size about 7MB but fails when I trying to write 186.5GB.

I tryed to do this by several differnt ways and tryed to write file part localy on each PC. Local write works fine but all network attempts fails.

I want to know why I getting errors when trying to write across the network. I will be gratefull for adwices how to do this in better maner.

Yuri Dolotkazin
  • 480
  • 4
  • 13
  • 1
    `ERROR_UNEXP_NET_ERR` several different ntstatus mapped to this error. if call `RtlGetLastNtStatus` after `WriteFile` fail can be more informative why – RbMm Apr 06 '17 at 17:17
  • According to [this answer](http://serverfault.com/a/510454), SMB does not support shared write access to a file. – zett42 Apr 06 '17 at 18:58
  • Does the code work over the network if there is only one process writing to the file? – Harry Johnston Apr 06 '17 at 21:54
  • Is the server `SERVERNAME` a Windows box? – Harry Johnston Apr 06 '17 at 21:57
  • Yeah code works over the network in any scenario. Server is dedicated machine based on Windows Server 2008 and uses only as files share. There is no any dedicated code related with described functionality running on the files share server and better if it never appear. – Yuri Dolotkazin Apr 07 '17 at 12:13
  • Zett42 this is realy bad news for me that I can't use shared acces for writing over SMB. Are there any way to achive shared writing across the network without polluting file share with yet another service? Maybe some specialised protocol? – Yuri Dolotkazin Apr 07 '17 at 12:19
  • @RbMm I can try to do this but if SMB don't allow me to achive shared access for writing as said Zett42 probably I should look for another solution insted debuging this. – Yuri Dolotkazin Apr 07 '17 at 12:25
  • i don't think that SMB don't allow shared access for writing. i just open file on compA from compB and compC at once with `FILE_GENERIC_WRITE|FILE_GENERIC_READ, FILE_SHARE_VALID_FLAGS` and both open and write is ok – RbMm Apr 07 '17 at 13:10
  • @RbMm Ok I will check RtlGetLastNtStatus. Thank you for your help. – Yuri Dolotkazin Apr 07 '17 at 13:14
  • and [Opportunistic Locks](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365433(v=vs.85).aspx) used for notify client, that another client open file (used for coordinate data caching and coherency). and if you only write to file - ask for `FILE_GENERIC_WRITE` only instead `GENERIC_WRITE | GENERIC_READ` – RbMm Apr 07 '17 at 13:28
  • @RbMm RtlGetLastNtStatus returned with 0xc000000d code. Accordinly MSDN this mean STATUS_INVALID_PARAMETER. So looks like FILE_SHARE_WRITE is not supported by SMB. Yes with small files this parameter works fine. As I mentioned in the question problems appear when I trying to write 186.5GB. I think with small writing amounts processes sequently write file fast enough to don't detect error. – Yuri Dolotkazin Apr 10 '17 at 13:05
  • @RbMm And yes I tried to open file without FILE_SHARE_READ. And a lot other experiments. Unfortunately nothing helps me to writing big file. – Yuri Dolotkazin Apr 10 '17 at 13:09
  • `c000000d` not converted to `ERROR_UNEXP_NET_ERR` - you can not got this 2 errors after same call. and after which call you got this ? `FILE_SHARE_WRITE` supported – RbMm Apr 10 '17 at 13:10
  • if you got *c000000d* in WriteFile faster of all error in *OffsetHigh* (<0) – RbMm Apr 10 '17 at 13:21

0 Answers0