Skip to contentSkip to conversation

vfs: introduce d_mark_tmpfile_name()

PATCH Superseded Paulo Alcantara 4 participants 9 messages Apr 5 – Apr 8

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Paulo Alcantara

author maintainer
#0 lore raw

PATCH 1/2 vfs: introduce d_mark_tmpfile_name()

CIFS requires O_TMPFILE dentries to have names of newly created delete-on-close files in the server so it can build full pathnames from the root of the share when performing operations on them.

---
 fs/dcache.c            | 19 +++++++++++++++++++
 include/linux/dcache.h |  1 +
 2 files changed, 20 insertions(+)
fs/dcache.c +19 -0
index 7ba1801d8132..c20a9c9e921c 100644
@@ -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;
include/linux/dcache.h +1 -0
index 898c60d21c92..f60819dcfebd 100644
@@ -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 *);
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 2/2 smb: client: add support for O_TMPFILE

Implement O_TMPFILE support for SMB2+ in the CIFS client.

---
 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       | 294 ++++++++++++++++++++++-----
 fs/smb/client/file.c      |  56 ++++--
 fs/smb/client/inode.c     |   3 +-
 fs/smb/client/link.c      |   1 +
 fs/smb/client/smb2inode.c | 403 ++++++++++++++------------------------
 9 files changed, 477 insertions(+), 329 deletions(-)
fs/smb/client/cifsfs.c +4 -0
index 32d0305a1239..f4bed8d4a072 100644
@@ -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
index e320d39b01f5..64c7a4c6ac83 100644
@@ -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
index 709e96e07791..ccfde157d3be 100644
@@ -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_TMPFILE,			/* for O_TMPFILE inodes */
+	CIFS_INO_CLOSE_ON_LOCK,			/* Not to defer the close when lock is set */
+};
 
 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
index 884bfa1cf0b4..c24c50d732e6 100644
@@ -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 +248 -46

400 lines not on this page · raw message

fs/smb/client/file.c +36 -20

110 lines not on this page · raw message

fs/smb/client/inode.c +2 -1
index 888f9e35f14b..24040909d184 100644
@@ -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
index 434e8fe74080..dd127917a340 100644
@@ -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
maintainer
#2 lore raw

Re: [PATCH 2/2] smb: client: add support for O_TMPFILE

Quoted 22 lines · Paulo Alcantara
fs/smb/client/dir.c +9 -6
@@ -436,17 +457,13 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned
 		goto out_err;
 	}
 
-	if (newinode)
-		if (S_ISDIR(newinode->i_mode)) {
-			rc = -EISDIR;
-			goto out_err;
-		}
+	if (newinode && S_ISDIR(newinode->i_mode)) {
+		rc = -EISDIR;
+		goto out_err;
+	}
 
 	d_drop(direntry);
-	d_add(direntry, newinode);
 
+		rc = __cifs_do_create(dir, direntry, full_path, xid,
+				      tlink, oflags, mode, oplock,
+				      fid, buf, &inode);
+		if (!rc)
+			d_add(direntry, inode);

fs/smb/client/dir.c static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned

Quoted 10 lines
fs/smb/client/dir.c +8 -0
 
 
+		rc = __cifs_do_create(dir, dentry, path, xid, tlink,
+				      file->f_flags, mode, &oplock,
+				      &fid, NULL, &inode);
+		if (!rc) {
+			set_nlink(inode, 0);
+			mark_inode_dirty(inode);
+			d_mark_tmpfile_name(file, &QSTR_LEN(name, size - 1));
+			d_instantiate(dentry, inode);

I really don't like this "not sure what the state is, d_drop() to get it unhashed" pattern, _especially_ when d_drop() and d_add() or d_instantiate() get separated.

Note, BTW, this d_add() call site is one of the two in the entire kernel that are neither d_splice_alias() in disguise nor pass NULL as inode. The other one is nfs_link(), where we also have this kind of "d_drop() first, then use d_add()" pattern.

Folks, what state can dentry be in cifs_do_create()? I'd rather see that sorted out, not obfuscated even more.

FWIW, that's a major headache for Neil's stuff around directory locking changes - any place where we play with unhash-and-rehash needs separate analysis. d_drop() is not something to be used lightly; it obfuscates the dentry state and we'll need to translate those to the new locking scheme.

#3 lore raw

Re: [PATCH 2/2] smb: client: add support for O_TMPFILE

Quoted 39 lines · Al Viro
Quoted 22 lines · Paulo Alcantara
fs/smb/client/dir.c +9 -6
@@ -436,17 +457,13 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned
 		goto out_err;
 	}
 
-	if (newinode)
-		if (S_ISDIR(newinode->i_mode)) {
-			rc = -EISDIR;
-			goto out_err;
-		}
+	if (newinode && S_ISDIR(newinode->i_mode)) {
+		rc = -EISDIR;
+		goto out_err;
+	}
 
 	d_drop(direntry);
-	d_add(direntry, newinode);
 
+		rc = __cifs_do_create(dir, direntry, full_path, xid,
+				      tlink, oflags, mode, oplock,
+				      fid, buf, &inode);
+		if (!rc)
+			d_add(direntry, inode);

fs/smb/client/dir.c static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned

Quoted 10 lines
fs/smb/client/dir.c +8 -0
 
 
+		rc = __cifs_do_create(dir, dentry, path, xid, tlink,
+				      file->f_flags, mode, &oplock,
+				      &fid, NULL, &inode);
+		if (!rc) {
+			set_nlink(inode, 0);
+			mark_inode_dirty(inode);
+			d_mark_tmpfile_name(file, &QSTR_LEN(name, size - 1));
+			d_instantiate(dentry, inode);

I really don't like this "not sure what the state is, d_drop() to get it unhashed" pattern, _especially_ when d_drop() and d_add() or d_instantiate() get separated.

Quoted 7 lines

Note, BTW, this d_add() call site is one of the two in the entire kernel that are neither d_splice_alias() in disguise nor pass NULL as inode. The other one is nfs_link(), where we also have this kind of "d_drop() first, then use d_add()" pattern.

Folks, what state can dentry be in cifs_do_create()? I'd rather see that sorted out, not obfuscated even more.

cifs_do_create() gets call from cifs_atomic_open() which is ->atomic_open(), so dentry can be in-lookup or negative-hashed.

If is also called from cifs_create() (->create()) and as cifs_lookup() never skips the lookup due to intent (like NFS does) the dentry will always be hashed negative.

So dentry can be in-lookup or hashed-negative. With current APIs we can avoid d_drop() with

if (d_in_lookup(dentry))

    d_add(dentry, inode);
 else
    d_instantiate(dentry, inode);

though I would prefer to provide an interface which handled both.

NeilBrown

Quoted 6 lines

FWIW, that's a major headache for Neil's stuff around directory locking changes - any place where we play with unhash-and-rehash needs separate analysis. d_drop() is not something to be used lightly; it obfuscates the dentry state and we'll need to translate those to the new locking scheme.

author maintainer
#4 lore raw

Re: [PATCH 2/2] smb: client: add support for O_TMPFILE

Quoted 48 lines · NeilBrown
Quoted 39 lines · Al Viro
Quoted 22 lines · Paulo Alcantara
fs/smb/client/dir.c +9 -6
@@ -436,17 +457,13 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned
 		goto out_err;
 	}
 
-	if (newinode)
-		if (S_ISDIR(newinode->i_mode)) {
-			rc = -EISDIR;
-			goto out_err;
-		}
+	if (newinode && S_ISDIR(newinode->i_mode)) {
+		rc = -EISDIR;
+		goto out_err;
+	}
 
 	d_drop(direntry);
-	d_add(direntry, newinode);
 
+		rc = __cifs_do_create(dir, direntry, full_path, xid,
+				      tlink, oflags, mode, oplock,
+				      fid, buf, &inode);
+		if (!rc)
+			d_add(direntry, inode);

fs/smb/client/dir.c static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned

Quoted 10 lines
fs/smb/client/dir.c +8 -0
 
 
+		rc = __cifs_do_create(dir, dentry, path, xid, tlink,
+				      file->f_flags, mode, &oplock,
+				      &fid, NULL, &inode);
+		if (!rc) {
+			set_nlink(inode, 0);
+			mark_inode_dirty(inode);
+			d_mark_tmpfile_name(file, &QSTR_LEN(name, size - 1));
+			d_instantiate(dentry, inode);

I really don't like this "not sure what the state is, d_drop() to get it unhashed" pattern, _especially_ when d_drop() and d_add() or d_instantiate() get separated.

Quoted 7 lines

Note, BTW, this d_add() call site is one of the two in the entire kernel that are neither d_splice_alias() in disguise nor pass NULL as inode. The other one is nfs_link(), where we also have this kind of "d_drop() first, then use d_add()" pattern.

Folks, what state can dentry be in cifs_do_create()? I'd rather see that sorted out, not obfuscated even more.

Quoted 6 lines

cifs_do_create() gets call from cifs_atomic_open() which is ->atomic_open(), so dentry can be in-lookup or negative-hashed.

If is also called from cifs_create() (->create()) and as cifs_lookup() never skips the lookup due to intent (like NFS does) the dentry will always be hashed negative.

Thanks Neil for the details!

IIUC, this is what we currently have

* cifs_atomic_open()

init state: in-lookup or hashed-negative
[d_drop() + d_add()]
end state: hashed-positive

* cifs_tmpfile()

init state: unhashed-negative
[d_drop() + d_instantiate()]
end state: unhashed-positive

* cifs_create()

init state: hashed-negative
[d_drop() + d_add()]
end state: hashed-positive

maintainer
#5 lore raw

Re: [PATCH 2/2] smb: client: add support for O_TMPFILE

Quoted 14 lines · Paulo Alcantara

IIUC, this is what we currently have

* cifs_atomic_open()

init state: in-lookup or hashed-negative
[d_drop() + d_add()]
end state: hashed-positive

* cifs_tmpfile()

init state: unhashed-negative
[d_drop() + d_instantiate()]
end state: unhashed-positive

Quoted 5 lines

* cifs_create()

init state: hashed-negative
[d_drop() + d_add()]
end state: hashed-positive

IDGI. Why shouldn't cifs_create() simply do d_instantiate() instead of that dance, ditto for cifs_tmpfile()?

The only case where you want to change the hashed status is in-lookup O_CREAT ->atomic_open(). And there it's not d_drop()+d_add() - it's d_splice_alias().

I'd lift that d_drop() out of cifs_do_create() into the callers and get rid of it, along with d_add()...

author maintainer
#6 lore raw

Re: [PATCH 2/2] smb: client: add support for O_TMPFILE

Quoted 16 lines · Al Viro
Quoted 14 lines · Paulo Alcantara

IIUC, this is what we currently have

* cifs_atomic_open()

init state: in-lookup or hashed-negative
[d_drop() + d_add()]
end state: hashed-positive

* cifs_tmpfile()

init state: unhashed-negative
[d_drop() + d_instantiate()]
end state: unhashed-positive

Quoted 8 lines
Quoted 5 lines

* cifs_create()

init state: hashed-negative
[d_drop() + d_add()]
end state: hashed-positive

IDGI. Why shouldn't cifs_create() simply do d_instantiate() instead of that dance, ditto for cifs_tmpfile()?

Yes, makes sense. Especially d_drop() for cifs_tmpfile() which is simply a no-op.

Quoted 3 lines

The only case where you want to change the hashed status is in-lookup O_CREAT ->atomic_open(). And there it's not d_drop()+d_add() - it's d_splice_alias().

ACK.

Quoted 2 lines

I'd lift that d_drop() out of cifs_do_create() into the callers and get rid of it, along with d_add()...

Sounds good. Let me play with it a little bit and, if tests survive, will send a follow-up patch fixing all that mess.

#7 lore raw
In reply to Paulo Alcantara

fs/dcache.c void d_mark_tmpfile(struct file *file, struct inode *inode) · line 3207

Quoted 9 lines · Paulo Alcantara
fs/dcache.c +9 -0
+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);

We tend to prefer each of these to be written out separately. ie:

	BUG_ON(dname_external(dentry));
	BUG_ON(d_really_is_positive(dentry));

...

That way if one triggers, we know which condition is violated.

author maintainer
#8 lore raw
Quoted 13 lines · Matthew Wilcox

fs/dcache.c void d_mark_tmpfile(struct file *file, struct inode *inode) · line 3207

Quoted 9 lines · Paulo Alcantara
fs/dcache.c +9 -0
+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);

We tend to prefer each of these to be written out separately. ie:

Quoted 5 lines
	BUG_ON(dname_external(dentry));
	BUG_ON(d_really_is_positive(dentry));

...

That way if one triggers, we know which condition is violated.

Makes sense, thanks. Will fix it in v2.