/*1* Copyright 2016 Jakub Klama <[email protected]>2* All rights reserved3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted providing that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED15* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,21* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING22* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE23* POSSIBILITY OF SUCH DAMAGE.24*25*/2627#ifndef LIB9P_FID_H28#define LIB9P_FID_H2930#include <stdbool.h>3132/*33* Data structure for a fid. All active fids in one session34* are stored in a hash table; the hash table provides the35* iterator to process them. (See also l9p_connection in lib9p.h.)36*37* The back-end code has additional data per fid, found via38* lo_aux. Currently this is allocated with a separate calloc().39*40* Most fids represent a file or directory, but a few are special41* purpose, including the auth fid from Tauth+Tattach, and the42* fids used for extended attributes. We have our own set of43* flags here in lo_flags.44*45* Note that all new fids start as potentially-valid (reserving46* their 32-bit fid value), but not actually-valid. If another47* (threaded) op is invoked on a not-yet-valid fid, the fid cannot48* be used. A fid can also be locked against other threads, in49* which case they must wait for it: this happens during create50* and open, which on success result in the fid changing from a51* directory to a file. (At least, all this applies in principle52* -- we're currently single-threaded per connection so the locks53* are nop-ed out and the valid bit is mainly just for debug.)54*55* Fids that are "open" (the underlying file or directory is open)56* are marked as well.57*58* Locking is managed by the front end (request.c); validation59* and type-marking can be done by either side as needed.60*61* Fid types and validity are manipulated by set* and unset*62* functions, and tested by is* ops. Note that we only63* distinguish between "directory" and "not directory" at this64* level, i.e., symlinks and devices are just "not a directory65* fid". Also, fids cannot be unset as auth or xattr fids,66* nor can an open fid become closed, except by being clunked.67* While files should not normally become directories, it IS normal68* for directory fids to become file fids due to Twalk operations.69*70* (These accessor functions are just to leave wiggle room for71* different future implementations.)72*/73struct l9p_fid {74void *lo_aux;75uint32_t lo_fid;76uint32_t lo_flags; /* volatile atomic_t when threaded? */77};7879enum l9p_lo_flags {80L9P_LO_ISAUTH = 0x01,81L9P_LO_ISDIR = 0x02,82L9P_LO_ISOPEN = 0x04,83L9P_LO_ISVALID = 0x08,84L9P_LO_ISXATTR = 0x10,85};8687static inline bool88l9p_fid_isauth(struct l9p_fid *fid)89{90return ((fid->lo_flags & L9P_LO_ISAUTH) != 0);91}9293static inline void94l9p_fid_setauth(struct l9p_fid *fid)95{96fid->lo_flags |= L9P_LO_ISAUTH;97}9899static inline bool100l9p_fid_isdir(struct l9p_fid *fid)101{102return ((fid->lo_flags & L9P_LO_ISDIR) != 0);103}104105static inline void106l9p_fid_setdir(struct l9p_fid *fid)107{108fid->lo_flags |= L9P_LO_ISDIR;109}110111static inline void112l9p_fid_unsetdir(struct l9p_fid *fid)113{114fid->lo_flags &= ~(uint32_t)L9P_LO_ISDIR;115}116117static inline bool118l9p_fid_isopen(struct l9p_fid *fid)119{120return ((fid->lo_flags & L9P_LO_ISOPEN) != 0);121}122123static inline void124l9p_fid_setopen(struct l9p_fid *fid)125{126fid->lo_flags |= L9P_LO_ISOPEN;127}128129static inline bool130l9p_fid_isvalid(struct l9p_fid *fid)131{132return ((fid->lo_flags & L9P_LO_ISVALID) != 0);133}134135static inline void136l9p_fid_setvalid(struct l9p_fid *fid)137{138fid->lo_flags |= L9P_LO_ISVALID;139}140141static inline void142l9p_fid_unsetvalid(struct l9p_fid *fid)143{144fid->lo_flags &= ~(uint32_t)L9P_LO_ISVALID;145}146147static inline bool148l9p_fid_isxattr(struct l9p_fid *fid)149{150return ((fid->lo_flags & L9P_LO_ISXATTR) != 0);151}152153static inline void154l9p_fid_setxattr(struct l9p_fid *fid)155{156fid->lo_flags |= L9P_LO_ISXATTR;157}158159#endif /* LIB9P_FID_H */160161162