xref: /freebsd/contrib/lib9p/fid.h (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
1 /*
2  * Copyright 2016 Jakub Klama <jceel@FreeBSD.org>
3  * All rights reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted providing that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef LIB9P_FID_H
29 #define LIB9P_FID_H
30 
31 #include <stdbool.h>
32 
33 /*
34  * Data structure for a fid.  All active fids in one session
35  * are stored in a hash table; the hash table provides the
36  * iterator to process them.  (See also l9p_connection in lib9p.h.)
37  *
38  * The back-end code has additional data per fid, found via
39  * lo_aux.  Currently this is allocated with a separate calloc().
40  *
41  * Most fids represent a file or directory, but a few are special
42  * purpose, including the auth fid from Tauth+Tattach, and the
43  * fids used for extended attributes.  We have our own set of
44  * flags here in lo_flags.
45  *
46  * Note that all new fids start as potentially-valid (reserving
47  * their 32-bit fid value), but not actually-valid.  If another
48  * (threaded) op is invoked on a not-yet-valid fid, the fid cannot
49  * be used.  A fid can also be locked against other threads, in
50  * which case they must wait for it: this happens during create
51  * and open, which on success result in the fid changing from a
52  * directory to a file.  (At least, all this applies in principle
53  * -- we're currently single-threaded per connection so the locks
54  * are nop-ed out and the valid bit is mainly just for debug.)
55  *
56  * Fids that are "open" (the underlying file or directory is open)
57  * are marked as well.
58  *
59  * Locking is managed by the front end (request.c); validation
60  * and type-marking can be done by either side as needed.
61  *
62  * Fid types and validity are manipulated by set* and unset*
63  * functions, and tested by is* ops.  Note that we only
64  * distinguish between "directory" and "not directory" at this
65  * level, i.e., symlinks and devices are just "not a directory
66  * fid".  Also, fids cannot be unset as auth or xattr fids,
67  * nor can an open fid become closed, except by being clunked.
68  * While files should not normally become directories, it IS normal
69  * for directory fids to become file fids due to Twalk operations.
70  *
71  * (These accessor functions are just to leave wiggle room for
72  * different future implementations.)
73  */
74 struct l9p_fid {
75 	void	*lo_aux;
76 	uint32_t lo_fid;
77 	uint32_t lo_flags;	/* volatile atomic_t when threaded? */
78 };
79 
80 enum l9p_lo_flags {
81 	L9P_LO_ISAUTH = 0x01,
82 	L9P_LO_ISDIR = 0x02,
83 	L9P_LO_ISOPEN = 0x04,
84 	L9P_LO_ISVALID = 0x08,
85 	L9P_LO_ISXATTR = 0x10,
86 };
87 
88 static inline bool
89 l9p_fid_isauth(struct l9p_fid *fid)
90 {
91 	return ((fid->lo_flags & L9P_LO_ISAUTH) != 0);
92 }
93 
94 static inline void
95 l9p_fid_setauth(struct l9p_fid *fid)
96 {
97 	fid->lo_flags |= L9P_LO_ISAUTH;
98 }
99 
100 static inline bool
101 l9p_fid_isdir(struct l9p_fid *fid)
102 {
103 	return ((fid->lo_flags & L9P_LO_ISDIR) != 0);
104 }
105 
106 static inline void
107 l9p_fid_setdir(struct l9p_fid *fid)
108 {
109 	fid->lo_flags |= L9P_LO_ISDIR;
110 }
111 
112 static inline void
113 l9p_fid_unsetdir(struct l9p_fid *fid)
114 {
115 	fid->lo_flags &= ~(uint32_t)L9P_LO_ISDIR;
116 }
117 
118 static inline bool
119 l9p_fid_isopen(struct l9p_fid *fid)
120 {
121 	return ((fid->lo_flags & L9P_LO_ISOPEN) != 0);
122 }
123 
124 static inline void
125 l9p_fid_setopen(struct l9p_fid *fid)
126 {
127 	fid->lo_flags |= L9P_LO_ISOPEN;
128 }
129 
130 static inline bool
131 l9p_fid_isvalid(struct l9p_fid *fid)
132 {
133 	return ((fid->lo_flags & L9P_LO_ISVALID) != 0);
134 }
135 
136 static inline void
137 l9p_fid_setvalid(struct l9p_fid *fid)
138 {
139 	fid->lo_flags |= L9P_LO_ISVALID;
140 }
141 
142 static inline void
143 l9p_fid_unsetvalid(struct l9p_fid *fid)
144 {
145 	fid->lo_flags &= ~(uint32_t)L9P_LO_ISVALID;
146 }
147 
148 static inline bool
149 l9p_fid_isxattr(struct l9p_fid *fid)
150 {
151 	return ((fid->lo_flags & L9P_LO_ISXATTR) != 0);
152 }
153 
154 static inline void
155 l9p_fid_setxattr(struct l9p_fid *fid)
156 {
157 	fid->lo_flags |= L9P_LO_ISXATTR;
158 }
159 
160 #endif  /* LIB9P_FID_H */
161