1/* $OpenBSD: fuse_file.c,v 1.9 2016/08/30 16:45:54 natano Exp $ */
2/*
3 * Copyright (c) 2012-2013 Sylvestre Gallon <ccna.syl@gmail.com>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/systm.h>
20#include <sys/stat.h>
21#include <sys/statvfs.h>
22#include <sys/vnode.h>
23#include <sys/fusebuf.h>
24
25#include "fusefs_node.h"
26#include "fusefs.h"
27
28int
29fusefs_file_open(struct fusefs_mnt *fmp, struct fusefs_node *ip,
30    enum fufh_type fufh_type, int flags, int isdir, struct proc *p)
31{
32	struct fusebuf *fbuf;
33	int error = 0;
34
35	if (!fmp->sess_init)
36		return (0);
37
38	fbuf = fb_setup(0, ip->ufs_ino.i_number,
39	    ((isdir) ? FBT_OPENDIR : FBT_OPEN), p);
40	fbuf->fb_io_flags = flags;
41
42	error = fb_queue(fmp->dev, fbuf);
43	if (error) {
44		fb_delete(fbuf);
45		return (error);
46	}
47
48	ip->fufh[fufh_type].fh_id = fbuf->fb_io_fd;
49	ip->fufh[fufh_type].fh_type = fufh_type;
50
51	fb_delete(fbuf);
52	return (0);
53}
54
55int
56fusefs_file_close(struct fusefs_mnt *fmp, struct fusefs_node * ip,
57    enum fufh_type fufh_type, int flags, int isdir, struct proc *p)
58{
59	struct fusebuf *fbuf;
60	int error = 0;
61
62	if (fmp->sess_init) {
63		fbuf = fb_setup(0, ip->ufs_ino.i_number,
64		    ((isdir) ? FBT_RELEASEDIR : FBT_RELEASE), p);
65		fbuf->fb_io_fd  = ip->fufh[fufh_type].fh_id;
66		fbuf->fb_io_flags = flags;
67
68		error = fb_queue(fmp->dev, fbuf);
69		if (error && (error != ENOSYS))
70			printf("fusefs: file error %d\n", error);
71
72		fb_delete(fbuf);
73	}
74
75	ip->fufh[fufh_type].fh_id = (uint64_t)-1;
76	ip->fufh[fufh_type].fh_type = FUFH_INVALID;
77
78	return (error);
79}
80
81uint64_t
82fusefs_fd_get(struct fusefs_node *ip, enum fufh_type type)
83{
84	if (ip->fufh[type].fh_type == FUFH_INVALID)
85		type = FUFH_RDWR;
86
87	return (ip->fufh[type].fh_id);
88}
89