ANDROID: fuse-bpf: Simplify and fix setting bpf program

Fix case when an existing bpf prog is being removed
Tidy up code

Bug: 279363668
Bug: 286939538
Test: Boots, can copy file to /sdcardfs/Android/data, fuse_test passes
Change-Id: If0e682f43cbeb62764a7a2be543b90cb974b0aa0
Signed-off-by: Paul Lawrence <[email protected]>
diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c
index f544286..e292c55 100644
--- a/fs/fuse/backing.c
+++ b/fs/fuse/backing.c
@@ -1235,39 +1235,36 @@ int fuse_handle_backing(struct fuse_entry_bpf *feb, struct inode **backing_inode
 int fuse_handle_bpf_prog(struct fuse_entry_bpf *feb, struct inode *parent,
 			 struct bpf_prog **bpf)
 {
-	struct bpf_prog *new_bpf;
-
-	/* Parent isn't presented, but we want to keep
-	 * Don't touch bpf program at all in this case
-	 */
-	if (feb->out.bpf_action == FUSE_ACTION_KEEP && !parent)
-		return 0;
+	struct bpf_prog *new_bpf = NULL;
 
 	switch (feb->out.bpf_action) {
 	case FUSE_ACTION_KEEP: {
-		struct fuse_inode *pi = get_fuse_inode(parent);
+		/* Parent isn't presented, but we want to keep
+		 * Don't touch bpf program at all in this case
+		 */
+		if (!parent)
+			return 0;
 
-		new_bpf = pi->bpf;
+		new_bpf = get_fuse_inode(parent)->bpf;
 		if (new_bpf)
 			bpf_prog_inc(new_bpf);
 		break;
 	}
 
 	case FUSE_ACTION_REMOVE:
-		new_bpf = NULL;
 		break;
 
 	case FUSE_ACTION_REPLACE: {
 		struct file *bpf_file = feb->bpf_file;
-		struct bpf_prog *bpf_prog = ERR_PTR(-EINVAL);
 
-		if (bpf_file && !IS_ERR(bpf_file))
-			bpf_prog = fuse_get_bpf_prog(bpf_file);
+		if (!bpf_file)
+			return -EINVAL;
+		if (IS_ERR(bpf_file))
+			return PTR_ERR(bpf_file);
 
-		if (IS_ERR(bpf_prog))
-			return PTR_ERR(bpf_prog);
-
-		new_bpf = bpf_prog;
+		new_bpf = fuse_get_bpf_prog(bpf_file);
+		if (IS_ERR(new_bpf))
+			return PTR_ERR(new_bpf);
 		break;
 	}
 
@@ -1276,11 +1273,14 @@ int fuse_handle_bpf_prog(struct fuse_entry_bpf *feb, struct inode *parent,
 	}
 
 	/* Cannot change existing program */
-	if (*bpf) {
+	if (*bpf && new_bpf) {
 		bpf_prog_put(new_bpf);
 		return new_bpf == *bpf ? 0 : -EINVAL;
 	}
 
+	if (*bpf)
+		bpf_prog_put(*bpf);
+
 	*bpf = new_bpf;
 	return 0;
 }