cifs: fix handling of signing with writepages (try #5)
Subsystems (2)
PATCH 3/15 cifs: fix handling of signing with writepages (try #5)
Get a reference to the file early so we can eventually base the decision about signing on the correct tcon. If that doesn't work for some reason, then fall back to generic_writepages. That's just as likely to fail, but it simplifies the error handling.
In truth, I'm not sure how that could occur anyway, so maybe a NULL open_file here ought to be a BUG()?
After that, we drop the reference to the open_file and then we re-get one prior to each WriteAndX call. This helps ensure that the filehandle isn't held open any longer than necessary and that open files are reclaimed prior to each write call.
--- fs/cifs/file.c | 70 ++++++++++++++++++++++++++++++------------------------- 1 files changed, 38 insertions(+), 32 deletions(-)
fs/cifs/file.c +38 -32
@@ -1353,6 +1353,15 @@ static int cifs_writepages(struct address_space *mapping,int scanned =0 ;int xid, long_op; +/* +* BB: Is this meaningful for a non-block-device file system? +* If it is, we should test it again after we do I/O +*/ +if (wbc->nonblocking &&bdi_write_congested (bdi)) { + wbc->encountered_congestion =1 ; +return 0 ; + } + cifs_sb =CIFS_SB (mapping->host->i_sb);/*
@@ -1362,26 +1371,28 @@ static int cifs_writepages(struct address_space *mapping,if (cifs_sb->wsize <PAGE_CACHE_SIZE )return generic_writepages (mapping, wbc); -if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server)) -if (cifs_sb->tcon->ses->server->secMode & - (SECMODE_SIGN_REQUIRED |SECMODE_SIGN_ENABLED )) -if (!experimEnabled) -return generic_writepages (mapping, wbc); - iov =kmalloc (32 *sizeof (struct kvec ),GFP_KERNEL );if (iov ==NULL )return generic_writepages (mapping, wbc); -/* -* BB: Is this meaningful for a non-block-device file system? -* If it is, we should test it again after we do I/O +* if there's no open file, then this is likely to fail too, +* but it'll at least handle the return. Maybe it should be +* a BUG() instead? */ -if (wbc->nonblocking &&bdi_write_congested (bdi)) { - wbc->encountered_congestion =1 ; + open_file =find_writable_file (CIFS_I (mapping->host)); +if (!open_file) {kfree (iov); -return 0 ; +return generic_writepages (mapping, wbc); + } + + tcon = open_file->tcon; +if (!experimEnabled && tcon->ses->server->secMode & + (SECMODE_SIGN_REQUIRED |SECMODE_SIGN_ENABLED )) { +cifsFileInfo_put (open_file); +return generic_writepages (mapping, wbc); } +cifsFileInfo_put (open_file); xid =GetXid ();
@@ -1486,38 +1497,33 @@ retry:break ; }if (n_iov) { -/* Search for a writable handle every time we call -* CIFSSMBWrite2. We can't rely on the last handle -* we used to still be valid -*/ open_file =find_writable_file (CIFS_I (mapping->host));if (!open_file) {cERROR (1 ,"No writable handles for inode" ); rc = -EBADF ; }else { - tcon = open_file->tcon; long_op =cifs_write_timeout (cifsi, offset); - rc =CIFSSMBWrite2 (xid, tcon, - open_file->netfid, + rc =CIFSSMBWrite2 (xid, tcon, open_file->netfid, bytes_to_write, offset, &bytes_written, iov, n_iov, long_op);cifsFileInfo_put (open_file); -cifs_update_eof (cifsi, offset, bytes_written); + } -if (rc || bytes_written < bytes_to_write) { -cERROR (1 ,"Write2 ret %d, wrote %d" , - rc, bytes_written); -/* BB what if continued retry is -requested via mount flags? */ -if (rc == -ENOSPC ) -set_bit (AS_ENOSPC , &mapping->flags); -else -set_bit (AS_EIO , &mapping->flags); - }else { -cifs_stats_bytes_written (tcon, bytes_written); - } +if (rc || bytes_written < bytes_to_write) { +cERROR (1 ,"Write2 ret %d, wrote %d" , + rc, bytes_written); +/* BB what if continued retry is +requested via mount flags? */ +if (rc == -ENOSPC ) +set_bit (AS_ENOSPC , &mapping->flags); +else +set_bit (AS_EIO , &mapping->flags); + }else { +cifs_update_eof (cifsi, offset, bytes_written); +cifs_stats_bytes_written (tcon, bytes_written); } +for (i =0 ; i < n_iov; i++) { page = pvec.pages[first + i];/* Should we also set page error on
Signature · 1 line
1.7.3
A message that is not in the archive (sent off-list or not yet received): 1285190586-10750-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
PATCH 10/15 cifs: have find_readable/writable_file filter by fsuid (try #5)
When we implement multiuser mounts, we'll need to filter filehandles by fsuid. Add a flag for multiuser mounts and code to filter by fsuid when it's set.
--- fs/cifs/cifs_fs_sb.h | 3 ++- fs/cifs/cifsacl.c | 4 ++-- fs/cifs/cifsproto.h | 4 ++-- fs/cifs/dir.c | 1 + fs/cifs/file.c | 33 +++++++++++++++++++++++++-------- fs/cifs/inode.c | 6 +++--- 6 files changed, 35 insertions(+), 16 deletions(-)
fs/cifs/cifs_fs_sb.h +2 -1
@@ -37,6 +37,7 @@#define CIFS_MOUNT_NOSSYNC 0x4000/* don't do slow SMBflush on every sync*/ #define CIFS_MOUNT_FSCACHE 0x8000/* local caching enabled */ #define CIFS_MOUNT_MF_SYMLINKS 0x10000/* Minshall+French Symlinks enabled */ +#define CIFS_MOUNT_MULTIUSER 0x20000/* multiuser mount */ struct cifs_sb_info {struct cifsTconInfo *ptcon;/* primary mount */
@@ -48,7 +49,7 @@ struct cifs_sb_info {gid_t mnt_gid;mode_t mnt_file_mode;mode_t mnt_dir_mode; -int mnt_cifs_flags; +unsigned int mnt_cifs_flags;int prepathlen;char *prepath;/* relative path under the share to mount to */ #ifdef CONFIG_CIFS_DFS_UPCALL
fs/cifs/cifsacl.c +2 -2
@@ -615,7 +615,7 @@ static struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb,struct cifsFileInfo *open_file =NULL ;if (inode) - open_file =find_readable_file (CIFS_I (inode)); + open_file =find_readable_file (CIFS_I (inode), true);if (!open_file)return get_cifs_acl_by_path (cifs_sb, path, pacllen);
@@ -685,7 +685,7 @@ static int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen,cFYI (DBG2 ,"set ACL for %s from mode 0x%x" , path, inode->i_mode); - open_file =find_readable_file (CIFS_I (inode)); + open_file =find_readable_file (CIFS_I (inode), true);if (!open_file)return set_cifs_acl_by_path (cifs_sb, path, pnntsd, acllen);
fs/cifs/cifsproto.h +2 -2
@@ -78,9 +78,9 @@ extern int checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length);extern bool is_valid_oplock_break (struct smb_hdr *smb,struct TCP_Server_Info *);extern bool is_size_safe_to_change (struct cifsInodeInfo *,__u64 eof); -extern struct cifsFileInfo *find_writable_file (struct cifsInodeInfo *); +extern struct cifsFileInfo *find_writable_file (struct cifsInodeInfo *,bool );#ifdef CONFIG_CIFS_EXPERIMENTAL -extern struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *); +extern struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *,bool );#endif extern unsigned int smbCalcSize (struct smb_hdr *ptr);extern unsigned int smbCalcSize_LE (struct smb_hdr *ptr);
fs/cifs/dir.c +1 -0
@@ -144,6 +144,7 @@ cifs_new_fileinfo(struct inode *newinode, __u16 fileHandle, struct file *file, pCifsFile->netfid = fileHandle; pCifsFile->pid = current->tgid; + pCifsFile->uid =current_fsuid (); pCifsFile->pInode =igrab (newinode); pCifsFile->mnt = mnt; pCifsFile->pfile = file;
fs/cifs/file.c +25 -8
@@ -1168,9 +1168,15 @@ static ssize_t cifs_write(struct file *file, const char *write_data, }#ifdef CONFIG_CIFS_EXPERIMENTAL -struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *cifs_inode) +struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *cifs_inode, +bool fsuid_only) {struct cifsFileInfo *open_file =NULL ; +struct cifs_sb_info *cifs_sb =CIFS_SB (cifs_inode->vfs_inode.i_sb); + +/* only filter by fsuid on multiuser mounts */ +if (!(cifs_sb->mnt_cifs_flags &CIFS_MOUNT_MULTIUSER )) + fsuid_only = false;read_lock (&GlobalSMBSeslock);/* we could simply get the first_list_entry since write-only entries
@@ -1179,6 +1185,8 @@ struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode)list_for_each_entry (open_file, &cifs_inode->openFileList, flist) {if (open_file->closePend)continue ; +if (fsuid_only && open_file->uid !=current_fsuid ()) +continue ;if (open_file->pfile && ((open_file->pfile->f_flags &O_RDWR ) || (open_file->pfile->f_flags &O_RDONLY ))) {if (!open_file->invalidHandle) {
@@ -1198,9 +1206,11 @@ struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode) }#endif -struct cifsFileInfo *find_writable_file (struct cifsInodeInfo *cifs_inode) +struct cifsFileInfo *find_writable_file (struct cifsInodeInfo *cifs_inode, +bool fsuid_only) {struct cifsFileInfo *open_file; +struct cifs_sb_info *cifs_sb =CIFS_SB (cifs_inode->vfs_inode.i_sb);bool any_available = false;int rc;
@@ -1214,13 +1224,19 @@ struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode)return NULL ; } +/* only filter by fsuid on multiuser mounts */ +if (!(cifs_sb->mnt_cifs_flags &CIFS_MOUNT_MULTIUSER )) + fsuid_only = false; +read_lock (&GlobalSMBSeslock); refind_writable:list_for_each_entry (open_file, &cifs_inode->openFileList, flist) { -if (open_file->closePend || - (!any_available && open_file->pid != current->tgid)) +if (open_file->closePend) +continue ; +if (!any_available && open_file->pid != current->tgid) +continue ; +if (fsuid_only && open_file->uid !=current_fsuid ())continue ; -if (open_file->pfile && ((open_file->pfile->f_flags &O_RDWR ) || (open_file->pfile->f_flags &O_WRONLY ))) {
@@ -1315,7 +1331,7 @@ static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)if (mapping->host->i_size - offset < (loff_t )to) to = (unsigned )(mapping->host->i_size - offset); - open_file =find_writable_file (CIFS_I (mapping->host)); + open_file =find_writable_file (CIFS_I (mapping->host), false);if (open_file) { bytes_written =cifs_write (open_file->pfile, write_data, to-from, &offset);
@@ -1388,7 +1404,7 @@ static int cifs_writepages(struct address_space *mapping,* but it'll at least handle the return. Maybe it should be * a BUG() instead? */ - open_file =find_writable_file (CIFS_I (mapping->host)); + open_file =find_writable_file (CIFS_I (mapping->host), false);if (!open_file) {kfree (iov);return generic_writepages (mapping, wbc);
@@ -1505,7 +1521,8 @@ retry:break ; }if (n_iov) { - open_file =find_writable_file (CIFS_I (mapping->host)); + open_file =find_writable_file (CIFS_I (mapping->host), + false);if (!open_file) {cERROR (1 ,"No writable handles for inode" ); rc = -EBADF ;
fs/cifs/inode.c +3 -3
@@ -969,7 +969,7 @@ cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,/* * If the file is already open for write, just use that fileid */ - open_file =find_writable_file (cifsInode); + open_file =find_writable_file (cifsInode, true);if (open_file) { netfid = open_file->netfid; netpid = open_file->pid;
@@ -1819,7 +1819,7 @@ cifs_set_file_size(struct inode *inode, struct iattr *attrs,* writebehind data than the SMB timeout for the SetPathInfo * request would allow */ - open_file =find_writable_file (cifsInode); + open_file =find_writable_file (cifsInode, true);if (open_file) {__u16 nfid = open_file->netfid;__u32 npid = open_file->pid;
@@ -1984,7 +1984,7 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs) args->ctime =NO_CHANGE_64 ; args->device =0 ; - open_file =find_writable_file (cifsInode); + open_file =find_writable_file (cifsInode, true);if (open_file) {u16 nfid = open_file->netfid;u32 npid = open_file->pid;
Signature · 1 line
1.7.3
The patch does seem to change one behavior.
If CIFSSMBWrite2 writes data, but less than requested (bytes written < bytes_to_write) this may change the file size but the (local copy of eof?) eof won't be updated in that case (as it was before)
Quoted 137 lines · Jeff Layton
Get a reference to the file early so we can eventually base the decision about signing on the correct tcon. If that doesn't work for some reason, then fall back to
generic_writepages. That's just as likely to fail, but it simplifies the error handling.In truth, I'm not sure how that could occur anyway, so maybe a NULL
open_filehere ought to be aBUG()?After that, we drop the reference to the
open_fileand then we re-get one prior to each WriteAndX call. This helps ensure that the filehandle isn't held open any longer than necessary and that open files are reclaimed prior to each write call.Signed-off-by Jeff Layton <[email protected]>---
fs/cifs/file.c | 70 ++++++++++++++++++++++++++++++-------------------------1 files changed, 38 insertions(+), 32 deletions(-)fs/cifs/file.c +38
-33@@ -1353,6 +1353,15 @@ static int cifs_writepages(struct address_space *mapping, int scanned = 0; int xid, long_op; + /* + * BB: Is this meaningful for a non-block-device file system? + * If it is, we should test it again after we do I/O + */ + if (wbc->nonblocking && bdi_write_congested(bdi)) { + wbc->encountered_congestion = 1; + return 0; + } + cifs_sb = CIFS_SB(mapping->host->i_sb); /*@@ -1362,26 +1371,28 @@ static int cifs_writepages(struct address_space *mapping, if (cifs_sb->wsize < PAGE_CACHE_SIZE) return generic_writepages(mapping, wbc); - if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server)) - if (cifs_sb->tcon->ses->server->secMode & - (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) - if (!experimEnabled) - return generic_writepages(mapping, wbc); - iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL); if (iov == NULL) return generic_writepages(mapping, wbc); - /* - * BB: Is this meaningful for a non-block-device file system? - * If it is, we should test it again after we do I/O + * if there's no open file, then this is likely to fail too, + * but it'll at least handle the return. Maybe it should be + * a BUG() instead? */ - if (wbc->nonblocking && bdi_write_congested(bdi)) { - wbc->encountered_congestion = 1; + open_file = find_writable_file(CIFS_I(mapping->host)); + if (!open_file) { kfree(iov); - return 0; + return generic_writepages(mapping, wbc); + } + + tcon = open_file->tcon; + if (!experimEnabled && tcon->ses->server->secMode & + (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) { + cifsFileInfo_put(open_file); + return generic_writepages(mapping, wbc); } + cifsFileInfo_put(open_file); xid = GetXid();@@ -1486,38 +1497,33 @@ retry: break; } if (n_iov) { - /* Search for a writable handle every time we call - * CIFSSMBWrite2. We can't rely on the last handle - * we used to still be valid - */ open_file = find_writable_file(CIFS_I(mapping->host)); if (!open_file) { cERROR(1, "No writable handles for inode"); rc = -EBADF; } else { - tcon = open_file->tcon; long_op = cifs_write_timeout(cifsi, offset); - rc = CIFSSMBWrite2(xid, tcon, - open_file->netfid, + rc = CIFSSMBWrite2(xid, tcon, open_file->netfid, bytes_to_write, offset, &bytes_written, iov, n_iov, long_op); cifsFileInfo_put(open_file); - cifs_update_eof(cifsi, offset, bytes_written); + } - if (rc || bytes_written < bytes_to_write) { - cERROR(1, "Write2 ret %d, wrote %d", - rc, bytes_written); - /* BB what if continued retry is - requested via mount flags? */ - if (rc == -ENOSPC) - set_bit(AS_ENOSPC, &mapping->flags); - else - set_bit(AS_EIO, &mapping->flags); - } else { - cifs_stats_bytes_written(tcon, bytes_written); - } + if (rc || bytes_written < bytes_to_write) { + cERROR(1, "Write2 ret %d, wrote %d", + rc, bytes_written); + /* BB what if continued retry is + requested via mount flags? */ + if (rc == -ENOSPC) + set_bit(AS_ENOSPC, &mapping->flags); + else + set_bit(AS_EIO, &mapping->flags); + } else { + cifs_update_eof(cifsi, offset, bytes_written); + cifs_stats_bytes_written(tcon, bytes_written); } + for (i = 0; i < n_iov; i++) { page = pvec.pages[first + i]; /* Should we also set page error on -- 1.7.3
Signature · 3 lines
Thanks, Steve
A message that is not in the archive (sent off-list or not yet received): AANLkTi=U25njqKFiGW2vO_uWMqQ52Xz9m+wigyW=AHez-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 7 lines · On Wed, 22 Sep 2010 17:29:12 -0500 Steve French…
The patch does seem to change one behavior.
If CIFSSMBWrite2 writes data, but less than requested (bytes written <
bytes_to_write) this may change the file size but the (local copy of eof?) eof won't be updated in that case (as it was before)
Good catch. I'll fix, retest and resend...
Signature · 1 line
Jeff Layton <[email protected]>