xref: /freebsd/sys/fs/fuse/fuse_node.h (revision 95ee2897)
151369649SPedro F. Giffuni /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
45fe58019SAttilio Rao  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
55fe58019SAttilio Rao  * All rights reserved.
65fe58019SAttilio Rao  *
75fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
85fe58019SAttilio Rao  * modification, are permitted provided that the following conditions are
95fe58019SAttilio Rao  * met:
105fe58019SAttilio Rao  *
115fe58019SAttilio Rao  * * Redistributions of source code must retain the above copyright
125fe58019SAttilio Rao  *   notice, this list of conditions and the following disclaimer.
135fe58019SAttilio Rao  * * Redistributions in binary form must reproduce the above
145fe58019SAttilio Rao  *   copyright notice, this list of conditions and the following disclaimer
155fe58019SAttilio Rao  *   in the documentation and/or other materials provided with the
165fe58019SAttilio Rao  *   distribution.
175fe58019SAttilio Rao  * * Neither the name of Google Inc. nor the names of its
185fe58019SAttilio Rao  *   contributors may be used to endorse or promote products derived from
195fe58019SAttilio Rao  *   this software without specific prior written permission.
205fe58019SAttilio Rao  *
215fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225fe58019SAttilio Rao  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235fe58019SAttilio Rao  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245fe58019SAttilio Rao  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255fe58019SAttilio Rao  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265fe58019SAttilio Rao  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275fe58019SAttilio Rao  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285fe58019SAttilio Rao  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295fe58019SAttilio Rao  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305fe58019SAttilio Rao  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315fe58019SAttilio Rao  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325fe58019SAttilio Rao  *
335fe58019SAttilio Rao  * Copyright (C) 2005 Csaba Henk.
345fe58019SAttilio Rao  * All rights reserved.
355fe58019SAttilio Rao  *
368aafc8c3SAlan Somers  * Copyright (c) 2019 The FreeBSD Foundation
378aafc8c3SAlan Somers  *
388aafc8c3SAlan Somers  * Portions of this software were developed by BFF Storage Systems, LLC under
398aafc8c3SAlan Somers  * sponsorship from the FreeBSD Foundation.
408aafc8c3SAlan Somers  *
415fe58019SAttilio Rao  * Redistribution and use in source and binary forms, with or without
425fe58019SAttilio Rao  * modification, are permitted provided that the following conditions
435fe58019SAttilio Rao  * are met:
445fe58019SAttilio Rao  * 1. Redistributions of source code must retain the above copyright
455fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer.
465fe58019SAttilio Rao  * 2. Redistributions in binary form must reproduce the above copyright
475fe58019SAttilio Rao  *    notice, this list of conditions and the following disclaimer in the
485fe58019SAttilio Rao  *    documentation and/or other materials provided with the distribution.
495fe58019SAttilio Rao  *
505fe58019SAttilio Rao  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
515fe58019SAttilio Rao  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
525fe58019SAttilio Rao  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535fe58019SAttilio Rao  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
545fe58019SAttilio Rao  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
555fe58019SAttilio Rao  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
565fe58019SAttilio Rao  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
575fe58019SAttilio Rao  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
585fe58019SAttilio Rao  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
595fe58019SAttilio Rao  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
605fe58019SAttilio Rao  * SUCH DAMAGE.
615fe58019SAttilio Rao  */
625fe58019SAttilio Rao 
635fe58019SAttilio Rao #ifndef _FUSE_NODE_H_
645fe58019SAttilio Rao #define _FUSE_NODE_H_
655fe58019SAttilio Rao 
66e5b50fe7SAlan Somers #include <sys/fnv_hash.h>
675fe58019SAttilio Rao #include <sys/types.h>
685fe58019SAttilio Rao #include <sys/mutex.h>
692bfd8992SKonstantin Belousov #include <sys/buf.h>
705fe58019SAttilio Rao 
715fe58019SAttilio Rao #include "fuse_file.h"
725fe58019SAttilio Rao 
735fe58019SAttilio Rao #define	FN_REVOKED		0x00000020
745fe58019SAttilio Rao #define	FN_FLUSHINPROG		0x00000040
755fe58019SAttilio Rao #define	FN_FLUSHWANT		0x00000080
763d15b234SAlan Somers /*
773d15b234SAlan Somers  * Indicates that the file's size is dirty; the kernel has changed it but not
783d15b234SAlan Somers  * yet send the change to the daemon.  When this bit is set, the
79788af953SAlan Somers  * cache_attrs.va_size field does not time out.
803d15b234SAlan Somers  */
815fe58019SAttilio Rao #define	FN_SIZECHANGE		0x00000100
82ead063e0SEdward Tomasz Napierala #define	FN_DIRECTIO		0x00000200
83e5b50fe7SAlan Somers /* Indicates that parent_nid is valid */
84e5b50fe7SAlan Somers #define	FN_PARENT_NID		0x00000400
855fe58019SAttilio Rao 
86788af953SAlan Somers /*
87788af953SAlan Somers  * Indicates that the file's cached timestamps are dirty.  They will be flushed
88788af953SAlan Somers  * during the next SETATTR or WRITE.  Until then, the cached fields will not
89788af953SAlan Somers  * time out.
90788af953SAlan Somers  */
91788af953SAlan Somers #define	FN_MTIMECHANGE		0x00000800
92788af953SAlan Somers #define	FN_CTIMECHANGE		0x00001000
9391972cfcSAlan Somers #define	FN_ATIMECHANGE		0x00002000
94788af953SAlan Somers 
955fe58019SAttilio Rao struct fuse_vnode_data {
965fe58019SAttilio Rao 	/** self **/
975fe58019SAttilio Rao 	uint64_t	nid;
98e5b50fe7SAlan Somers 	uint64_t	generation;
995fe58019SAttilio Rao 
1005fe58019SAttilio Rao 	/** parent **/
1015fe58019SAttilio Rao 	uint64_t	parent_nid;
1025fe58019SAttilio Rao 
1035fe58019SAttilio Rao 	/** I/O **/
104cad67791SAlan Somers 	/* List of file handles for all of the vnode's open file descriptors */
1051cedd6dfSAlan Somers 	LIST_HEAD(, fuse_filehandle)	handles;
1065fe58019SAttilio Rao 
1075fe58019SAttilio Rao 	/** flags **/
1085fe58019SAttilio Rao 	uint32_t	flag;
1095fe58019SAttilio Rao 
1105fe58019SAttilio Rao 	/** meta **/
1113f2c630cSAlan Somers 	/* The monotonic time after which the attr cache is invalid */
1123f2c630cSAlan Somers 	struct bintime	attr_cache_timeout;
1130d2bf489SAlan Somers 	/*
1140d2bf489SAlan Somers 	 * Monotonic time after which the entry is invalid.  Used for lookups
1150d2bf489SAlan Somers 	 * by nodeid instead of pathname.
1160d2bf489SAlan Somers 	 */
1170d2bf489SAlan Somers 	struct bintime	entry_cache_timeout;
11813d593a5SAlan Somers 	/*
11913d593a5SAlan Somers 	 * Monotonic time of the last FUSE operation that modified the file
12013d593a5SAlan Somers 	 * size.  Used to avoid races between mutator ops like VOP_SETATTR and
12113d593a5SAlan Somers 	 * unlocked accessor ops like VOP_LOOKUP.
12213d593a5SAlan Somers 	 */
12313d593a5SAlan Somers 	struct timespec	last_local_modify;
1245fe58019SAttilio Rao 	struct vattr	cached_attrs;
1255fe58019SAttilio Rao 	uint64_t	nlookup;
126ba8cc6d7SMateusz Guzik 	__enum_uint8(vtype)	vtype;
1272bfd8992SKonstantin Belousov 	struct vn_clusterw clusterw;
1285fe58019SAttilio Rao };
1295fe58019SAttilio Rao 
130e5b50fe7SAlan Somers /*
131e5b50fe7SAlan Somers  * This overlays the fid structure (see mount.h). Mostly the same as the types
132e5b50fe7SAlan Somers  * used by UFS and ext2.
133e5b50fe7SAlan Somers  */
134e5b50fe7SAlan Somers struct fuse_fid {
135e5b50fe7SAlan Somers 	uint16_t	len;	/* Length of structure. */
136e5b50fe7SAlan Somers 	uint16_t	pad;	/* Force 32-bit alignment. */
137e5b50fe7SAlan Somers 	uint32_t	gen;	/* Generation number. */
138e5b50fe7SAlan Somers 	uint64_t	nid;	/* FUSE node id. */
139e5b50fe7SAlan Somers };
140e5b50fe7SAlan Somers 
1415fe58019SAttilio Rao #define VTOFUD(vp) \
1425fe58019SAttilio Rao 	((struct fuse_vnode_data *)((vp)->v_data))
1435fe58019SAttilio Rao #define VTOI(vp)    (VTOFUD(vp)->nid)
144b0ecfb42SAlan Somers static inline bool
fuse_vnode_attr_cache_valid(struct vnode * vp)145b0ecfb42SAlan Somers fuse_vnode_attr_cache_valid(struct vnode *vp)
1463f2c630cSAlan Somers {
1473f2c630cSAlan Somers 	struct bintime now;
1483f2c630cSAlan Somers 
1493f2c630cSAlan Somers 	getbinuptime(&now);
150b0ecfb42SAlan Somers 	return (bintime_cmp(&(VTOFUD(vp)->attr_cache_timeout), &now, >));
151b0ecfb42SAlan Somers }
152b0ecfb42SAlan Somers 
153b0ecfb42SAlan Somers static inline struct vattr*
VTOVA(struct vnode * vp)154b0ecfb42SAlan Somers VTOVA(struct vnode *vp)
155b0ecfb42SAlan Somers {
156b0ecfb42SAlan Somers 	if (fuse_vnode_attr_cache_valid(vp))
1573f2c630cSAlan Somers 		return &(VTOFUD(vp)->cached_attrs);
1583f2c630cSAlan Somers 	else
1593f2c630cSAlan Somers 		return NULL;
1603f2c630cSAlan Somers }
1613f2c630cSAlan Somers 
162002e54b0SAlan Somers static inline void
fuse_vnode_clear_attr_cache(struct vnode * vp)163002e54b0SAlan Somers fuse_vnode_clear_attr_cache(struct vnode *vp)
164002e54b0SAlan Somers {
165002e54b0SAlan Somers 	bintime_clear(&VTOFUD(vp)->attr_cache_timeout);
166002e54b0SAlan Somers }
167002e54b0SAlan Somers 
168e5b50fe7SAlan Somers static uint32_t inline
fuse_vnode_hash(uint64_t id)169e5b50fe7SAlan Somers fuse_vnode_hash(uint64_t id)
170e5b50fe7SAlan Somers {
171e5b50fe7SAlan Somers 	return (fnv_32_buf(&id, sizeof(id), FNV1_32_INIT));
172e5b50fe7SAlan Somers }
173e5b50fe7SAlan Somers 
1745fe58019SAttilio Rao #define VTOILLU(vp) ((uint64_t)(VTOFUD(vp) ? VTOI(vp) : 0))
1755fe58019SAttilio Rao 
1765fe58019SAttilio Rao #define FUSE_NULL_ID 0
1775fe58019SAttilio Rao 
178f9b0e30bSAlan Somers extern struct vop_vector fuse_fifoops;
1795fe58019SAttilio Rao extern struct vop_vector fuse_vnops;
1805fe58019SAttilio Rao 
181e5b50fe7SAlan Somers int fuse_vnode_cmp(struct vnode *vp, void *nidp);
182e5b50fe7SAlan Somers 
18302295cafSConrad Meyer static inline void
fuse_vnode_setparent(struct vnode * vp,struct vnode * dvp)1845fe58019SAttilio Rao fuse_vnode_setparent(struct vnode *vp, struct vnode *dvp)
1855fe58019SAttilio Rao {
1865fe58019SAttilio Rao 	if (dvp != NULL && vp->v_type == VDIR) {
1875fe58019SAttilio Rao 		MPASS(dvp->v_type == VDIR);
1885fe58019SAttilio Rao 		VTOFUD(vp)->parent_nid = VTOI(dvp);
189e5b50fe7SAlan Somers 		VTOFUD(vp)->flag |= FN_PARENT_NID;
19042767f76SAlan Somers 	} else {
19142767f76SAlan Somers 		VTOFUD(vp)->flag &= ~FN_PARENT_NID;
1925fe58019SAttilio Rao 	}
1935fe58019SAttilio Rao }
1945fe58019SAttilio Rao 
1953d15b234SAlan Somers int fuse_vnode_size(struct vnode *vp, off_t *filesize, struct ucred *cred,
1963d15b234SAlan Somers 	struct thread *td);
1973d15b234SAlan Somers 
1985fe58019SAttilio Rao void fuse_vnode_destroy(struct vnode *vp);
1995fe58019SAttilio Rao 
20002295cafSConrad Meyer int fuse_vnode_get(struct mount *mp, struct fuse_entry_out *feo,
20102295cafSConrad Meyer     uint64_t nodeid, struct vnode *dvp, struct vnode **vpp,
202ba8cc6d7SMateusz Guzik     struct componentname *cnp, __enum_uint8(vtype) vtyp);
2035fe58019SAttilio Rao 
20402295cafSConrad Meyer void fuse_vnode_open(struct vnode *vp, int32_t fuse_open_flags,
2055fe58019SAttilio Rao     struct thread *td);
2065fe58019SAttilio Rao 
207f8d4af10SAlan Somers int fuse_vnode_savesize(struct vnode *vp, struct ucred *cred, pid_t pid);
2085fe58019SAttilio Rao 
2095d94aaacSAlan Somers int fuse_vnode_setsize(struct vnode *vp, off_t newsize, bool from_server);
2105fe58019SAttilio Rao 
21191972cfcSAlan Somers void fuse_vnode_undirty_cached_timestamps(struct vnode *vp, bool atime);
212788af953SAlan Somers 
213788af953SAlan Somers void fuse_vnode_update(struct vnode *vp, int flags);
214560a55d0SAlan Somers 
215560a55d0SAlan Somers void fuse_node_init(void);
216560a55d0SAlan Somers void fuse_node_destroy(void);
2175fe58019SAttilio Rao #endif /* _FUSE_NODE_H_ */
218