Skip to contentSkip to conversation

cifs: Close deferred files that may be open via hard links

PATCH In review Ross Lagerwall 4 participants 8 messages May 16, 2023 – May 23, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cifs: Close deferred files that may be open via hard links

Windows Server (tested with 2016) disallows opening the same inode via two different hardlinks at the same time. With deferred closes, this can result in unexpected behaviour as the following Python snippet shows:

    # Create file
    fd = os.open('test', os.O_WRONLY|os.O_CREAT)
    os.write(fd, b'foo')
    os.close(fd)

    # Open and close the file to leave a pending deferred close
    fd = os.open('test', os.O_RDONLY|os.O_DIRECT)
    os.close(fd)

    # Try to open the file via a hard link
    os.link('test', 'new')
    newfd = os.open('new', os.O_RDONLY|os.O_DIRECT)

The final open returns EINVAL due to the server returning STATUS_INVALID_PARAMETER.

Fix this by closing any deferred closes that may be open via other hard links if we haven't successfully reused a cached handle.

Fixes c3f207ab29f7 ("cifs: Deferred close for files")

---

This is kind of an RFC. Is the server doing the wrong thing? Is it correct to work around this in the client?

 fs/cifs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/cifs/file.c +1 -0
index c5fcefdfd797..723cbc060f57 100644
@@ -749,6 +749,7 @@ int cifs_open(struct inode *inode, struct file *file)
 			_cifsFileInfo_put(cfile, true, false);
 		}
 	}
+	cifs_close_deferred_file(CIFS_I(inode));
 
 	if (server->oplocks)
 		oplock = REQ_OPLOCK;
Signature · 1 line
2.31.1
reviewer
#1 lore raw
Quoted 17 lines · Ross Lagerwall

Windows Server (tested with 2016) disallows opening the same inode via two different hardlinks at the same time. With deferred closes, this can result in unexpected behaviour as the following Python snippet shows:

     # Create file
     fd = os.open('test', os.O_WRONLY|os.O_CREAT)
     os.write(fd, b'foo')
     os.close(fd)

     # Open and close the file to leave a pending deferred close
     fd = os.open('test', os.O_RDONLY|os.O_DIRECT)
     os.close(fd)

     # Try to open the file via a hard link
     os.link('test', 'new')
     newfd = os.open('new', os.O_RDONLY|os.O_DIRECT)
Quoted 2 lines

The final open returns EINVAL due to the server returning STATUS_INVALID_PARAMETER.

Good sleuthing. The hardlink behavior is slightly surprising, but it is certainly the case that certain incompatible combinations of open/create flags can conflict with other handles, including cached opens.

Quoted 9 lines

Fix this by closing any deferred closes that may be open via other hard links if we haven't successfully reused a cached handle.

Fixes c3f207ab29f7 ("cifs: Deferred close for files")

---

This is kind of an RFC. Is the server doing the wrong thing? Is it correct to work around this in the client?

By definition the server is doing what it does, and it's a moot question whether it's appropriate to work around it. However, I don't see this behavior explicitly stated in MS-FSA. And INVALID_PARAMETER is a strange error code to return for this. Do you have a packet trace? We should ask Microsoft.

fs/cifs/file.c int cifs_open(struct inode *inode, struct file *file) · line 752

Quoted 12 lines
  fs/cifs/file.c | 1 +
  1 file changed, 1 insertion(+)
fs/cifs/file.c +1 -0
index c5fcefdfd797..723cbc060f57 100644
@@ -749,6 +749,7 @@ int cifs_open(struct inode *inode, struct file *file)
  			_cifsFileInfo_put(cfile, true, false);
  		}
  	}
+	cifs_close_deferred_file(CIFS_I(inode));

But, is this correct? I don't believe this function blocks. And I'm not sure this triggers the close only in the case you've identified.

Tom.

Quoted 2 lines
fs/cifs/file.c +0 -0
  	if (server->oplocks)
  		oplock = REQ_OPLOCK;
#2 lore raw
Quoted 5 lines · Tom Talpey
Quoted 4 lines · Ross Lagerwall

Windows Server (tested with 2016) disallows opening the same inode via two different hardlinks at the same time. With deferred closes, this can result in unexpected behaviour as the following Python snippet shows:

Quoted 7 lines
Quoted 2 lines

The final open returns EINVAL due to the server returning STATUS_INVALID_PARAMETER.

Good sleuthing. The hardlink behavior is slightly surprising, but it is certainly the case that certain incompatible combinations of open/create flags can conflict with other handles, including cached opens.

That certainly sounds like implementation-dependent behavior, probably due to running into the same dev/ino numbers via a new path :-).

Quoted 8 lines
Quoted 2 lines

This is kind of an RFC. Is the server doing the wrong thing? Is it correct to work around this in the client?

By definition the server is doing what it does, and it's a moot question whether it's appropriate to work around it. However, I don't see this behavior explicitly stated in MS-FSA. And INVALID_PARAMETER is a strange error code to return for this. Do you have a packet trace? We should ask Microsoft.

A packet trace of this would be really interesting :-).

#3 lore raw
In reply to Tom Talpey
Quoted 51 lines

From: Tom Talpey <[email protected]>
Sent: Tuesday, May 16, 2023 7:41 PM
To: Ross Lagerwall <[email protected]>; [email protected] <[email protected]>
Cc: Steve French <[email protected]>; Paulo Alcantara <[email protected]>; Ronnie Sahlberg <[email protected]>; Shyam Prasad N <[email protected]>; Rohith Surabattula <[email protected]>
Subject: Re: [PATCH] cifs: Close deferred files that may be open via hard links

Quoted 13 lines · Ross Lagerwall

Windows Server (tested with 2016) disallows opening the same inode via two different hardlinks at the same time. With deferred closes, this can result in unexpected behaviour as the following Python snippet shows:

      # Create file
      fd = os.open('test', os.O_WRONLY|os.O_CREAT) os.write(fd, b'foo')
      os.close(fd)

      # Open and close the file to leave a pending deferred close fd = os.open('test', os.O_RDONLY|os.O_DIRECT) os.close(fd)

Quoted 6 lines

      # Try to open the file via a hard link os.link('test', 'new')
      newfd = os.open('new', os.O_RDONLY|os.O_DIRECT)

The final open returns EINVAL due to the server returning STATUS_INVALID_PARAMETER.

Good sleuthing. The hardlink behavior is slightly surprising, but it is certainly the case that certain incompatible combinations of open/create flags can conflict with other handles, including cached opens.

Quoted 9 lines

Fix this by closing any deferred closes that may be open via other hard links if we haven't successfully reused a cached handle.

Fixes c3f207ab29f7 ("cifs: Deferred close for files")

---

This is kind of an RFC. Is the server doing the wrong thing? Is it correct to work around this in the client?

By definition the server is doing what it does, and it's a moot question whether it's appropriate to work around it. However, I don't see this behavior explicitly stated in MS-FSA. And INVALID_PARAMETER is a strange error code to return for this. Do you have a packet trace? We should ask Microsoft.

Quoted 3 lines

   fs/cifs/file.c | 1 +

   1 file changed, 1 insertion(+)
Quoted 13 lines

fs/cifs/file.c int cifs_open(struct inode *inode, struct file *file) · line 752

Quoted 9 lines
fs/cifs/file.c +1 -0
index c5fcefdfd797..723cbc060f57 100644
@@ -749,6 +749,7 @@ int cifs_open(struct inode *inode, struct file *file)
                         _cifsFileInfo_put(cfile, true, false);
                 }
         }
+     cifs_close_deferred_file(CIFS_I(inode));

But, is this correct? I don't believe this function blocks. And I'm not sure this triggers the close only in the case you've identified.

It calls _cifsFileInfo_put() on each cached handle which calls server->ops->close() which is synchronous as far as I can tell.

I could restrict it to check if the inode has > 1 link but as you mentioned above, there are various combinations of open/create flags that can conflict with cached handles, so maybe it is correct to always called it in the cache miss path?

In any case, I have attached a packet trace from running the above Python reproducer.

Client IP == 10.71.56.138
Server IP == 10.71.57.49
Server OS == Windows Server 2016 1607 (build 14393.5717)

Thanks,
Ross

#4 lore raw
Quoted 2 lines · Ross Lagerwall

In any case, I have attached a packet trace from running the above Python reproducer.

afaict the STATUS_INVALID_PARAMETER comes from the lease code as you're passing the same lease key for the open on the link which is illegal afair.

Can you retry the experiment without requesting a lease or ensuring the second open on the link uses a different lease key?

-slow

reviewer
#5 lore raw
Quoted 7 lines · Ralph Boehme
Quoted 2 lines · Ross Lagerwall

In any case, I have attached a packet trace from running the above Python reproducer.

afaict the STATUS_INVALID_PARAMETER comes from the lease code as you're passing the same lease key for the open on the link which is illegal afair.

Can you retry the experiment without requesting a lease or ensuring the second open on the link uses a different lease key?

Indeed, the same lease key is coming in both opens, first in packet 3 where it opens "test", and again in packet 18 where it opens the link "new". So it's triggering this assertion in MS-SMB2 section 3.3.5.9.11

Quoted 4 lines

The server MUST attempt to locate a Lease by performing a lookup in the LeaseTable.LeaseList using the LeaseKey in the SMB2_CREATE_REQUEST_LEASE_V2 as the lookup key. If a lease is found, Lease.FileDeleteOnClose is FALSE, and Lease.Filename does not match the file name for the incoming request, the request MUST be failed with STATUS_INVALID_PARAMETER

Steve/Rohith, is this new behavior in the client code?

Tom.

#6 lore raw
Quoted 24 lines

From: Tom Talpey <[email protected]>
Sent: Wednesday, May 17, 2023 2:24 PM
To: Ralph Boehme <[email protected]>; Ross Lagerwall <[email protected]>; [email protected] <[email protected]>
Cc: Steve French <[email protected]>; Paulo Alcantara <[email protected]>; Ronnie Sahlberg <[email protected]>; Shyam Prasad N <[email protected]>; Rohith Surabattula <[email protected]>
Subject: Re: [PATCH] cifs: Close deferred files that may be open via hard links

[CAUTION - EXTERNAL EMAIL] DO NOT reply, click links, or open attachments unless you have verified the sender and know the content is safe.

Quoted 9 lines · Ralph Boehme
Quoted 2 lines · Ross Lagerwall

In any case, I have attached a packet trace from running the above Python reproducer.

afaict the STATUS_INVALID_PARAMETER comes from the lease code as you're passing the same lease key for the open on the link which is illegal afair.

Can you retry the experiment without requesting a lease or ensuring the second open on the link uses a different lease key?

Indeed, the same lease key is coming in both opens, first in packet 3 where it opens "test", and again in packet 18 where it opens the link "new". So it's triggering this assertion in MS-SMB2 section 3.3.5.9.11

Quoted 8 lines
Quoted 4 lines

The server MUST attempt to locate a Lease by performing a lookup in the LeaseTable.LeaseList using the LeaseKey in the SMB2_CREATE_REQUEST_LEASE_V2 as the lookup key. If a lease is found, Lease.FileDeleteOnClose is FALSE, and Lease.Filename does not match the file name for the incoming request, the request MUST be failed with STATUS_INVALID_PARAMETER

Steve/Rohith, is this new behavior in the client code?

Tom.

It looks like smb2_get_lease_key() returns the lease key stored in the inode, which clearly breaks that assertion when hard links are involved.

I can confirm that when the lease keys are forced to be different, the test does not fail.

Ross

#7 lore raw
Quoted 35 lines

From: Ross Lagerwall <[email protected]>
Sent: Wednesday, May 17, 2023 2:48 PM
To: Tom Talpey <[email protected]>; Ralph Boehme <[email protected]>; [email protected] <[email protected]>
Cc: Steve French <[email protected]>; Paulo Alcantara <[email protected]>; Ronnie Sahlberg <[email protected]>; Shyam Prasad N <[email protected]>; Rohith Surabattula <[email protected]>
Subject: Re: [PATCH] cifs: Close deferred files that may be open via hard links

Quoted 29 lines

From: Tom Talpey <[email protected]>
Sent: Wednesday, May 17, 2023 2:24 PM
To: Ralph Boehme <[email protected]>; Ross Lagerwall <[email protected]>; [email protected] <[email protected]>
Cc: Steve French <[email protected]>; Paulo Alcantara <[email protected]>; Ronnie Sahlberg <[email protected]>; Shyam Prasad N <[email protected]>; Rohith Surabattula <[email protected]>
Subject: Re: [PATCH] cifs: Close deferred files that may be open via hard links

Quoted 9 lines · Ralph Boehme
Quoted 2 lines · Ross Lagerwall

In any case, I have attached a packet trace from running the above Python reproducer.

afaict the STATUS_INVALID_PARAMETER comes from the lease code as you're passing the same lease key for the open on the link which is illegal afair.

Can you retry the experiment without requesting a lease or ensuring the second open on the link uses a different lease key?

Indeed, the same lease key is coming in both opens, first in packet 3 where it opens "test", and again in packet 18 where it opens the link "new". So it's triggering this assertion in MS-SMB2 section 3.3.5.9.11

Quoted 4 lines

The server MUST attempt to locate a Lease by performing a lookup in the LeaseTable.LeaseList using the LeaseKey in the SMB2_CREATE_REQUEST_LEASE_V2 as the lookup key. If a lease is found, Lease.FileDeleteOnClose is FALSE, and Lease.Filename does not match the file name for the incoming request, the request MUST be failed with STATUS_INVALID_PARAMETER

Steve/Rohith, is this new behavior in the client code?

Quoted 9 lines
Quoted 1 line

Tom.

It looks like smb2_get_lease_key() returns the lease key stored in the inode, which clearly breaks that assertion when hard links are involved.

I can confirm that when the lease keys are forced to be different, the test does not fail.

Hi,

Am I correct in the assertion that the client is associating lease keys with the inode and that is incompatible with the spec (at least when hard links are involved)?

A brief look at the code suggests changing that would be somewhat involved... At least more work than I have time to spare at the moment.

Thanks,
Ross