smb: client: add support for O_TMPFILE
Subsystems (3)
Paulo Alcantara
maintainerRFC PATCH 1/1 smb: client: add support for O_TMPFILE
Implement O_TMPFILE support for SMB2+ in the CIFS client.
---
Al, Christian,
Could you please have a look at cifs_d_mark_tmpfile() in this patch? This is just an open-coded version of d_mark_tmpfile() but with the ability of setting ->d_shortname with CIFS generated tmpfile names instead.
I do know we're not supposed to change ->d_shortname or ->__dname directly outside fs/dcache.c, so I've added cifs_d_mark_tmpfile() just to show you how simple it would be supporting O_TMPFILE in CIFS by having a function like that.
CIFS relies heavily on
page = alloc_dentry_path(); full_path = build_path_from_dentry(dentry, page); ...
in order to build full paths all the way from current dentry up to the root; including share name and prefix paths, in some cases.
Most of the CIFS functions are path-based, hence not instantiating O_TMPFILE dentries with CIFS-specific tmpfile names will require a lot of changes on how we build those full paths. In the worst case having to increase inode by storing those tmpfile names, having to kmalloc() and then append the names with ->d_parent when CIFS_INO_TMPFILE bit is set, etc.
Would it be OK to have a helper or extend d_mark_tmpfile() in a way that we can _optionally_ pass down @name and @namelen (<= DNAME_INLINE_LEN - 1) and then set it in O_TMPFILE dentry?
If there is another way to solve that, then please let me know.
Thanks.
fs/smb/client/cifsfs.c | 4 + fs/smb/client/cifsfs.h | 19 ++ fs/smb/client/cifsglob.h | 23 ++- fs/smb/client/cifsproto.h | 3 +- fs/smb/client/dir.c | 232 ++++++++++++++++++++-- fs/smb/client/file.c | 23 ++- fs/smb/client/inode.c | 3 +- fs/smb/client/link.c | 1 + fs/smb/client/smb2inode.c | 403 ++++++++++++++------------------------ 9 files changed, 426 insertions(+), 285 deletions(-)
fs/smb/client/cifsfs.c +4 -0
@@ -124,6 +124,9 @@ MODULE_PARM_DESC(dir_cache_timeout, "Number of seconds to cache directory conten/* Module-wide total cached dirents (in bytes) across all tcons */ atomic64_t cifs_dircache_bytes_used =ATOMIC64_INIT (0 ); +atomic_t cifs_sillycounter; +atomic_t cifs_tmpcounter; +/* * Write-only module parameter to drop all cached directory entries across * all CIFS mounts. Echo a non-zero value to trigger.
@@ -1199,6 +1202,7 @@ MODULE_ALIAS("smb3");const struct inode_operations cifs_dir_inode_ops = { .create = cifs_create, .atomic_open = cifs_atomic_open, + .tmpfile = cifs_tmpfile, .lookup = cifs_lookup, .getattr = cifs_getattr, .unlink = cifs_unlink,
fs/smb/client/cifsfs.h +19 -0
@@ -13,6 +13,9 @@#define ROOT_I 2 +extern atomic_t cifs_sillycounter; +extern atomic_t cifs_tmpcounter; +/* * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down * so that it will fit. We use hash_64 to convert the value to 31 bits, and
@@ -53,6 +56,8 @@ int cifs_create(struct mnt_idmap *idmap, struct inode *inode, struct dentry *direntry, umode_t mode, bool excl);int cifs_atomic_open (struct inode *inode,struct dentry *direntry,struct file *file,unsigned int oflags,umode_t mode); +int cifs_tmpfile (struct mnt_idmap *idmap,struct inode *dir, +struct file *file,umode_t mode);struct dentry *cifs_lookup (struct inode *parent_dir_inode,struct dentry *direntry,unsigned int flags);int cifs_unlink (struct inode *dir,struct dentry *dentry);
@@ -142,6 +147,20 @@ struct smb3_fs_context;struct dentry *cifs_smb3_do_mount (struct file_system_type *fs_type,int flags,struct smb3_fs_context *old_ctx); +char *cifs_silly_fullpath (struct dentry *dentry); + +#define CIFS_TMPNAME_PREFIX ".__smbfile_tmp" +#define CIFS_TMPNAME_PREFIX_LEN ((int)sizeof(CIFS_TMPNAME_PREFIX) - 1) +#define CIFS_TMPNAME_COUNTER_LEN ((int)sizeof(cifs_tmpcounter) * 2) +#define CIFS_TMPNAME_LEN \ + (CIFS_TMPNAME_PREFIX_LEN + CIFS_TMPNAME_COUNTER_LEN) + +#define CIFS_SILLYNAME_PREFIX ".__smbfile_silly" +#define CIFS_SILLYNAME_PREFIX_LEN ((int)sizeof(CIFS_SILLYNAME_PREFIX) - 1) +#define CIFS_SILLYNAME_COUNTER_LEN ((int)sizeof(cifs_sillycounter) * 2) +#define CIFS_SILLYNAME_LEN \ + (CIFS_SILLYNAME_PREFIX_LEN + CIFS_SILLYNAME_COUNTER_LEN) +#ifdef CONFIG_CIFS_NFSD_EXPORT extern const struct export_operations cifs_export_ops;#endif /* CONFIG_CIFS_NFSD_EXPORT */
fs/smb/client/cifsglob.h +13 -10
@@ -1534,9 +1534,16 @@ int cifs_file_set_size(const unsigned int xid, struct dentry *dentry,#define CIFS_CACHE_RW_FLG (CIFS_CACHE_READ_FLG | CIFS_CACHE_WRITE_FLG)#define CIFS_CACHE_RHW_FLG (CIFS_CACHE_RW_FLG | CIFS_CACHE_HANDLE_FLG) -/* -* One of these for each file inode -*/ +enum cifs_inode_flags { +CIFS_INODE_PENDING_OPLOCK_BREAK ,/* oplock break in progress */ +CIFS_INODE_PENDING_WRITERS ,/* Writes in progress */ +CIFS_INODE_FLAG_UNUSED ,/* Unused flag */ +CIFS_INO_DELETE_PENDING ,/* delete pending on server */ +CIFS_INO_INVALID_MAPPING ,/* pagecache is invalid */ +CIFS_INO_LOCK ,/* lock bit for synchronization */ +CIFS_INO_CLOSE_ON_LOCK ,/* Not to defer the close when lock is set */ +CIFS_INO_TMPFILE ,/* for O_TMPFILE inodes */ +};struct cifsInodeInfo {struct netfs_inode netfs;/* Netfslib context and vfs inode */
@@ -1554,13 +1561,6 @@ struct cifsInodeInfo {__u32 cifsAttrs;/* e.g. DOS archive bit, sparse, compressed, system */ unsigned int oplock;/* oplock/lease level we have */ __u16 epoch;/* used to track lease state changes */ -#define CIFS_INODE_PENDING_OPLOCK_BREAK (0)/* oplock break in progress */ -#define CIFS_INODE_PENDING_WRITERS (1)/* Writes in progress */ -#define CIFS_INODE_FLAG_UNUSED (2)/* Unused flag */ -#define CIFS_INO_DELETE_PENDING (3)/* delete pending on server */ -#define CIFS_INO_INVALID_MAPPING (4)/* pagecache is invalid */ -#define CIFS_INO_LOCK (5)/* lock bit for synchronization */ -#define CIFS_INO_CLOSE_ON_LOCK (7)/* Not to defer the close when lock is set */ unsigned long flags;spinlock_t writers_lock;unsigned int writers;/* Number of writers on this inode */
@@ -2259,6 +2259,7 @@ struct smb2_compound_vars {struct kvec qi_iov;struct kvec io_iov[SMB2_IOCTL_IOV_SIZE ];struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE ]; +struct kvec hl_iov[SMB2_SET_INFO_IOV_SIZE ];struct kvec unlink_iov[SMB2_SET_INFO_IOV_SIZE ];struct kvec rename_iov[SMB2_SET_INFO_IOV_SIZE ];struct kvec close_iov;
@@ -2383,6 +2384,8 @@ static inline int cifs_open_create_options(unsigned int oflags, int opts) opts |=CREATE_WRITE_THROUGH ;if (oflags &O_DIRECT ) opts |=CREATE_NO_BUFFER ; +if (oflags &O_TMPFILE ) + opts |=CREATE_DELETE_ON_CLOSE ;return opts; }
fs/smb/client/cifsproto.h +2 -1
@@ -141,7 +141,8 @@ struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,int __cifs_get_writable_file (struct cifsInodeInfo *cifs_inode,unsigned int find_flags,unsigned int open_flags,struct cifsFileInfo **ret_file); -int cifs_get_writable_path (struct cifs_tcon *tcon,const char *name,int flags, +int cifs_get_writable_path (struct cifs_tcon *tcon,const char *name, +struct inode *inode,int flags,struct cifsFileInfo **ret_file); struct cifsFileInfo *__find_readable_file(struct cifsInodeInfo *cifs_inode, unsigned int find_flags,
fs/smb/client/dir.c +219 -13
256 lines not on this page · raw message
fs/smb/client/file.c +14 -9
@@ -696,6 +696,7 @@ struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, cfile->f_flags = file->f_flags; cfile->invalidHandle = false; cfile->deferred_close_scheduled = false; + cfile->status_file_deleted = file->f_flags &O_TMPFILE ; cfile->tlink =cifs_get_tlink (tlink);INIT_WORK (&cfile->oplock_break, cifs_oplock_break);INIT_WORK (&cfile->put, cifsFileInfo_put_work);
@@ -727,6 +728,8 @@ struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,/* if readable file instance put first in list*/ spin_lock (&cinode->open_file_lock); +if (file->f_flags &O_TMPFILE ) +set_bit (CIFS_INO_TMPFILE , &cinode->flags); fid->purge_cache = false; server->ops->set_fid (cfile, fid, oplock);
@@ -2578,13 +2581,12 @@ int __cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, struct cifsFileInfo **ret_file) {struct cifsFileInfo *open_file, *inv_file =NULL ; +bool fsuid_only, with_delete;struct cifs_sb_info *cifs_sb;bool any_available = false; -int rc = -EBADF ;unsigned int refind =0 ; -bool fsuid_only = find_flags &FIND_FSUID_ONLY ; -bool with_delete = find_flags &FIND_WITH_DELETE ; *ret_file =NULL ; +int rc = -EBADF ;/* * Having a null inode here (because mapping->host was set to zero by
@@ -2600,11 +2602,15 @@ int __cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, cifs_sb =CIFS_SB (cifs_inode); +spin_lock (&cifs_inode->open_file_lock); +if (test_bit (CIFS_INO_TMPFILE , &cifs_inode->flags)) + find_flags =FIND_ANY ; + + with_delete = find_flags &FIND_WITH_DELETE ; + fsuid_only = find_flags &FIND_FSUID_ONLY ;/* only filter by fsuid on multiuser mounts */ if (!(cifs_sb_flags (cifs_sb) &CIFS_MOUNT_MULTIUSER )) fsuid_only = false; - -spin_lock (&cifs_inode->open_file_lock); refind_writable:if (refind >MAX_REOPEN_ATT ) {spin_unlock (&cifs_inode->open_file_lock);
@@ -2683,10 +2689,9 @@ find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)return cfile; } -int -cifs_get_writable_path (struct cifs_tcon *tcon,const char *name, -int flags, -struct cifsFileInfo **ret_file) +int cifs_get_writable_path (struct cifs_tcon *tcon,const char *name, +struct inode *inode,int flags, +struct cifsFileInfo **ret_file) {struct cifsFileInfo *cfile;void *page =alloc_dentry_path ();
fs/smb/client/inode.c +2 -1
@@ -2690,7 +2690,8 @@ cifs_dentry_needs_reval(struct dentry *dentry)struct cifs_tcon *tcon =cifs_sb_master_tcon (cifs_sb);struct cached_fid *cfid =NULL ; -if (test_bit (CIFS_INO_DELETE_PENDING , &cifs_i->flags)) +if (test_bit (CIFS_INO_DELETE_PENDING , &cifs_i->flags) || +test_bit (CIFS_INO_TMPFILE , &cifs_i->flags))return false;if (cifs_i->time ==0 )return true;
fs/smb/client/link.c +1 -0
@@ -503,6 +503,7 @@ cifs_hardlink(struct dentry *old_file, struct inode *inode,if (d_really_is_positive (old_file)) { cifsInode =CIFS_I (d_inode (old_file));if (rc ==0 ) { +clear_bit (CIFS_INO_TMPFILE , &cifsInode->flags);spin_lock (&d_inode (old_file)->i_lock);inc_nlink (d_inode (old_file));spin_unlock (&d_inode (old_file)->i_lock);
fs/smb/client/smb2inode.c +152 -251
591 lines not on this page · raw message
Signature · 1 line
2.53.0
Al Viro
maintainerfs/smb/client/dir.c static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned · line 458
Quoted 11 lines · Paulo Alcantara
fs/smb/client/dir.c +8
-2d_drop (direntry); -d_add (direntry, newinode); - +inode_set_mtime_to_ts (inode,inode_set_ctime_current (inode)); +if (oflags &O_TMPFILE ) { +set_nlink (newinode,0 ); +mark_inode_dirty (newinode); +d_instantiate (direntry, newinode); + }else { +d_add (direntry, newinode); + }
What d_unhashed(dentry) is going to be when we arrive to that thing with O_TMPFILE?
Quoted 4 lines
fs/smb/client/dir.c +4
-0+static void cifs_d_mark_tmpfile (struct file *file, +const unsigned char *name, +size_t namelen) +{
fs/smb/client/dir.c static int cifs_ci_compare(const struct dentry *dentry, · line 1010
Quoted 10 lines
fs/smb/client/dir.c +10
-0+struct dentry *dentry = file->f_path.dentry; + +BUG_ON (dentry->d_name.name != dentry->d_shortname.string || + !hlist_unhashed (&dentry->d_u.d_alias) || + !d_unlinked (dentry) || + namelen >DNAME_INLINE_LEN -1 ); +spin_lock (&dentry->d_parent->d_lock); +spin_lock_nested (&dentry->d_lock,DENTRY_D_LOCK_NESTED ); + dentry->__d_name.len =sprintf (dentry->d_shortname.string,"%.*s" , + (int )namelen, name);
That's one hell of an odd way to spell that... What's wrong with using union shortname_store for an argument? Just memchr() for '\0' to verify it's there and use the result to calculate the length and plain assignment to d_shortname for copying...
And in any case, that !hlist_unhashed(....) in there is d_really_is_positive().
Quoted 7 lines
fs/smb/client/dir.c +7
-0+static int set_tmpfile_name (struct file *file) +{ +struct dentry *dentry = file->f_path.dentry; +unsigned char name[CIFS_TMPNAME_LEN +1 ]; +struct dentry *sdentry =NULL ; + + do {
fs/smb/client/dir.c static int cifs_ci_compare(const struct dentry *dentry, · line 1031
Quoted 10 lines
fs/smb/client/dir.c +10
-0+dput (sdentry); +scnprintf (name,sizeof (name), +CIFS_TMPNAME_PREFIX "%0*x" , +CIFS_TMPNAME_COUNTER_LEN , +atomic_inc_return (&cifs_tmpcounter)); + sdentry =lookup_noperm_unlocked (&QSTR (name), dentry->d_parent); +if (IS_ERR (sdentry)) +return -EBUSY ; + }while (!d_is_negative (sdentry)); +dput (sdentry);
That looks racy. Checking it doesn't exist at the moment is fine, but what if it's created right after that lookup? You are not holding any locks, so even the same-client race (with plain create()) is possible...
Quoted 3 lines
fs/smb/client/dir.c +3
-0+cifs_d_mark_tmpfile (file, name,sizeof (name) -1 ); +return 0 ; +}
Paulo Alcantara
maintainerQuoted 16 lines · Al Viro
fs/smb/client/dir.c static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned · line 458
Quoted 11 lines · Paulo Alcantara
fs/smb/client/dir.c +8
-2d_drop (direntry); -d_add (direntry, newinode); - +inode_set_mtime_to_ts (inode,inode_set_ctime_current (inode)); +if (oflags &O_TMPFILE ) { +set_nlink (newinode,0 ); +mark_inode_dirty (newinode); +d_instantiate (direntry, newinode); + }else { +d_add (direntry, newinode); + }What
d_unhashed(dentry)is going to be when we arrive to that thing withO_TMPFILE?
It's called with an unhashed negative dentry. Do you see any problems with it?
Quoted 20 lines
Quoted 4 lines
fs/smb/client/dir.c +4
-0+static void cifs_d_mark_tmpfile (struct file *file, +const unsigned char *name, +size_t namelen) +{fs/smb/client/dir.c static int cifs_ci_compare(const struct dentry *dentry, · line 1010
Quoted 10 lines
fs/smb/client/dir.c +10
-0+struct dentry *dentry = file->f_path.dentry; + +BUG_ON (dentry->d_name.name != dentry->d_shortname.string || + !hlist_unhashed (&dentry->d_u.d_alias) || + !d_unlinked (dentry) || + namelen >DNAME_INLINE_LEN -1 ); +spin_lock (&dentry->d_parent->d_lock); +spin_lock_nested (&dentry->d_lock,DENTRY_D_LOCK_NESTED ); + dentry->__d_name.len =sprintf (dentry->d_shortname.string,"%.*s" , + (int )namelen, name);That's one hell of an odd way to spell that... What's wrong with using union
shortname_storefor an argument? Justmemchr()for '\0' to verify it's there and use the result to calculate the length and plain assignment tod_shortnamefor copying...
Quoted 1 line
And in any case, that !hlist_unhashed(....) in there is
d_really_is_positive().
I was thinking more along these lines (e.g. add a new VFS helper and then call it in CIFS, as you suggested earlier)
diff --git a/fs/dcache.c b/fs/dcache.c
index 7ba1801d8132..c20a9c9e921c 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -3196,6 +3196,25 @@ void d_mark_tmpfile(struct file *file, struct inode *inode)
}
EXPORT_SYMBOL(d_mark_tmpfile);
+void d_mark_tmpfile_name(struct file *file, const struct qstr *name)
+{
+ struct dentry *dentry = file->f_path.dentry;
+ char *dname = dentry->d_shortname.string;
+
+ BUG_ON(dname_external(dentry) ||
+ d_really_is_positive(dentry) ||
+ !d_unlinked(dentry) ||
+ name->len > DNAME_INLINE_LEN - 1);
+ spin_lock(&dentry->d_parent->d_lock);
+ spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
+ dentry->__d_name.len = name->len;
+ memcpy(dname, name->name, name->len);
+ dname[name->len] = '\0';
+ spin_unlock(&dentry->d_lock);
+ spin_unlock(&dentry->d_parent->d_lock);
+}
+EXPORT_SYMBOL(d_mark_tmpfile_name);
+
void d_tmpfile(struct file *file, struct inode *inode)
{
struct dentry *dentry = file->f_path.dentry;
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index 898c60d21c92..f60819dcfebd 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -264,6 +264,7 @@ extern void d_invalidate(struct dentry *);
extern struct dentry * d_make_root(struct inode *);
extern void d_mark_tmpfile(struct file *, struct inode *);
+void d_mark_tmpfile_name(struct file *file, const struct qstr *name);
extern void d_tmpfile(struct file *, struct inode *);
extern struct dentry *d_find_alias(struct inode *);Looks good?
Quoted 13 lines
Quoted 13 lines
fs/smb/client/dir.c +13
-0+static int set_tmpfile_name (struct file *file) +{ +struct dentry *dentry = file->f_path.dentry; +unsigned char name[CIFS_TMPNAME_LEN +1 ]; +struct dentry *sdentry =NULL ; + + do { + dput(sdentry); + scnprintf(name, sizeof(name), + CIFS_TMPNAME_PREFIX "%0*x", + CIFS_TMPNAME_COUNTER_LEN, + atomic_inc_return(&cifs_tmpcounter)); + sdentry = lookup_noperm_unlocked(&QSTR(name), dentry->d_parent);
Quoted 8 lines
fs/smb/client/dir.c static int cifs_ci_compare(const struct dentry *dentry, · line 1031
Quoted 4 lines
fs/smb/client/dir.c +4
-0+if (IS_ERR (sdentry)) +return -EBUSY ; + }while (!d_is_negative (sdentry)); +dput (sdentry);That looks racy. Checking it doesn't exist at the moment is fine, but what if it's created right after that lookup? You are not holding any locks, so even the same-client race (with plain
create()) is possible...
Yeah, that's definitely a TOCTOU race. I should've looped over cifs_do_create() instead to make sure that the tmpfile is created in the server, by using a sane retry counter.