| 1 |
/* |
| 2 |
* Copyright (C) 2006 by Latchesar Ionkov <[email protected]> |
| 3 |
* Copyright (C) 2010-2014 by Lawrence Livermore National Security, LLC. |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
typedef struct Npcfid Npcfid; |
| 26 |
typedef struct Npcfsys Npcfsys; |
| 27 |
|
| 28 |
struct Npcfsys; |
| 29 |
struct stat; |
| 30 |
|
| 31 |
struct Npcfid { |
| 32 |
u32 iounit; |
| 33 |
Npcfsys* fsys; |
| 34 |
u32 fid; |
| 35 |
u64 offset; |
| 36 |
char *buf; /* packed p9_dirents or gets buffer */ |
| 37 |
int buf_size; /* size of 'buf' buffer */ |
| 38 |
int buf_len; /* length of 'buf' filled (readdir_r) */ |
| 39 |
int buf_used; /* amount of 'buf' used (readdir_r)*/ |
| 40 |
}; |
| 41 |
|
| 42 |
typedef int (*AuthFun)(Npcfid *afid, u32 uid); |
| 43 |
|
| 44 |
enum { |
| 45 |
NPC_MULTI_RPC=1, /* use 'mtfsys'c' multi-threaded rpc engine */ |
| 46 |
NPC_SHORTREAD_EOF=2, /* npc_aget, npc_get treat short read as eof */ |
| 47 |
}; |
| 48 |
|
| 49 |
struct utimbuf; |
| 50 |
struct dirent; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
** Basic functions |
| 55 |
**/ |
| 56 |
|
| 57 |
/* Given a server already connected on rfd,wfd, send a VERSION request |
| 58 |
* to negotiate 9P2000.L and an msize <= the one provided. |
| 59 |
* Return fsys structure or NULL on error (retrieve with np_rerror ()) |
| 60 |
*/ |
| 61 |
Npcfsys* npc_start (int rfd, int wfd, int msize, int flags); |
| 62 |
|
| 63 |
/* Close fd and deallocate file system structure. |
| 64 |
*/ |
| 65 |
void npc_finish (Npcfsys *fs); |
| 66 |
|
| 67 |
/* Obtain an afid from the server using an AUTH request, then |
| 68 |
* call the 'auth' function on the afid to establish it as a credential. |
| 69 |
* Return afid or NULL on error (retrieve with np_rerror ()). |
| 70 |
* Note: server indicates "auth not required" with NULL and np_rerror () == 0. |
| 71 |
*/ |
| 72 |
Npcfid* npc_auth (Npcfsys *fs, char *aname, u32 uid, AuthFun auth); |
| 73 |
|
| 74 |
/* Obtain a fid from the server for the specified aname with an ATTACH request. |
| 75 |
* Optionally present 'afid' as an authentication credential or NULL. |
| 76 |
* Returns fid or NULL on error (retrieve with np_rerror ()). |
| 77 |
*/ |
| 78 |
Npcfid *npc_attach (Npcfsys *fs, Npcfid *afid, char *aname, uid_t uid); |
| 79 |
|
| 80 |
/* Tell the server to forget about 'fid' with a CLUNK request. |
| 81 |
* Return 0 on success, -1 on error (retrieve with np_rerror ()). |
| 82 |
*/ |
| 83 |
int npc_clunk (Npcfid *fid); |
| 84 |
|
| 85 |
/* Send LOPEN request to prepare 'fid' for I/O. |
| 86 |
* 'flags' uses the same bits as open (2), e.g. O_RDONLY, O_WRONLY, O_RDWR. |
| 87 |
* Returns 0 on success, -1 on error (retrieve with np_rerror ()). |
| 88 |
*/ |
| 89 |
int npc_open (Npcfid *fid, u32 flags); |
| 90 |
|
| 91 |
/* Send LCREATE request to create a file in directory 'fid' |
| 92 |
* with specified 'name', 'flags', and 'mode'. |
| 93 |
* Afterward, 'fid' will represent the new file, which can be used for I/O. |
| 94 |
* Returns 0 on success, -1 on error (retrieve with np_rerror ()). |
| 95 |
*/ |
| 96 |
int npc_create (Npcfid *fid, char *name, u32 flags, u32 mode, gid_t gid); |
| 97 |
|
| 98 |
/* Read 'count' bytes to 'buf' at 'offset' using READ requests. |
| 99 |
* Less than 'count' may be read if it exceeds the maximum request size |
| 100 |
* or the next call will return EOF. |
| 101 |
* Returns bytes read, 0 on EOF, or -1 on error (retrieve with np_rerror ()). |
| 102 |
*/ |
| 103 |
int npc_pread (Npcfid *fid, void *buf, u32 count, u64 offset); |
| 104 |
|
| 105 |
/* Write 'count' bytes from 'buf' at 'offset' using WRITE request. |
| 106 |
* Less than 'count' may be written if it exceeds the maximum request size. |
| 107 |
* Returns bytes written or -1 on error (retrieve with np_rerror ()). |
| 108 |
*/ |
| 109 |
int npc_pwrite (Npcfid *fid, void *buf, u32 count, u64 offset); |
| 110 |
|
| 111 |
/* Descend a directory represnted by 'fid' by walking successive path |
| 112 |
* elements in 'path'. Multiple WALK requests will be sent depending on |
| 113 |
* the number of path elements. If 'path' is NULL, call npc_clone(). |
| 114 |
* Returns a new fid representing path, or NULL on error (retrieve with |
| 115 |
* np_rerror ()). |
| 116 |
*/ |
| 117 |
Npcfid *npc_walk (Npcfid *fid, char *path); |
| 118 |
|
| 119 |
/* Clone a fid. Returns a new fid representing the same path as 'fid', |
| 120 |
* or NULL on error (retrieve with np_rerror ()). |
| 121 |
*/ |
| 122 |
Npcfid *npc_clone(Npcfid *fid); |
| 123 |
|
| 124 |
/* Send a MKDIR request to create 'name' in parent directory 'fid', |
| 125 |
* with 'mode' bits as in open (2). |
| 126 |
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). |
| 127 |
*/ |
| 128 |
int npc_mkdir (Npcfid *fid, char *name, u32 mode); |
| 129 |
|
| 130 |
/* Send a GETATTR request to get information on 'fid'. |
| 131 |
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). |
| 132 |
*/ |
| 133 |
int npc_getattr (Npcfid *fid, u64 request_mask, u64 *valid, struct p9_qid *qid, |
| 134 |
u32 *mode, u32 *uid, u32 *gid, u64 *nlink, u64 *rdev, |
| 135 |
u64 *size, u64 *blksize, u64 *blocks, u64 *atime_sec, |
| 136 |
u64 *atime_nsec, u64 *mtime_sec, u64 *mtime_nsec, |
| 137 |
u64 *ctime_sec, u64 *ctime_nsec, u64 *btime_sec, |
| 138 |
u64 *btime_nsec, u64 *gen, u64 *data_version); |
| 139 |
|
| 140 |
/* Send a SETATTR request to set information about 'fid'. |
| 141 |
*/ |
| 142 |
int npc_setattr (Npcfid *fid, u32 valid, u32 mode, u32 uid, u32 gid, u64 size, |
| 143 |
u64 atime_sec, u64 atime_nsec, u64 mtime_sec, u64 mtime_nsec); |
| 144 |
|
| 145 |
/* Send REMOVE request to unlink file/dir associated with 'fid', and clunk fid. |
| 146 |
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). |
| 147 |
*/ |
| 148 |
int npc_remove (Npcfid *fid); |
| 149 |
|
| 150 |
/* Send READDIR request to list contents of directory 'fid'. |
| 151 |
* 'data' will contain up to 'count' bytes of packed variable length |
| 152 |
* directory structures of the form: qid[13] offset[8] type[1] name[s] |
| 153 |
* The 'offset' returned in the last entry can be passed to a subsequent |
| 154 |
* npc_readdir call in the offset field. The only legal values for |
| 155 |
* the offset argument are 0 or the last offset returned. |
| 156 |
*/ |
| 157 |
int npc_readdir (Npcfid *fid, u64 offset, char *data, u32 count); |
| 158 |
|
| 159 |
/* Xattr functions |
| 160 |
*/ |
| 161 |
ssize_t npc_xattrwalk (Npcfid *fid, Npcfid *attrfid, char *name); |
| 162 |
int npc_xattrcreate (Npcfid *fid, char *name, u64 attr_size, u32 flags); |
| 163 |
|
| 164 |
|
| 165 |
/* TODO: |
| 166 |
* npc_statfs () |
| 167 |
* npc_symlink () |
| 168 |
* npc_rename () |
| 169 |
* npc_readlink () |
| 170 |
* npc_fsync () |
| 171 |
* npc_lock () |
| 172 |
* npc_getlock () |
| 173 |
* npc_link () |
| 174 |
*/ |
| 175 |
|
| 176 |
/** |
| 177 |
** Convenience wrappers. |
| 178 |
**/ |
| 179 |
|
| 180 |
/* Shorthand for start/auth/attach using the caller's effective uid. |
| 181 |
* Employs simple fsys implementation that only allows one outstanding RPC. |
| 182 |
* Returns a fid for the attach or NULL on error (retrieve with np_rerror ()). |
| 183 |
*/ |
| 184 |
Npcfid *npc_mount (int rfd, int wfd, int msize, char *aname, AuthFun auth); |
| 185 |
|
| 186 |
/* Shorthand for clunk/finish. Always succeeds. |
| 187 |
*/ |
| 188 |
void npc_umount (Npcfid *fid); |
| 189 |
|
| 190 |
/* Shorthand for walk/open. |
| 191 |
* Returns fid for file, or NULL on error (retrieve with np_rerror ()). |
| 192 |
*/ |
| 193 |
Npcfid* npc_open_bypath (Npcfid *root, char *path, u32 mode); |
| 194 |
|
| 195 |
/* Shorthand for walk/create. |
| 196 |
* Returns fid for new file, or NULL on error (retrieve with np_rerror ()). |
| 197 |
*/ |
| 198 |
Npcfid *npc_create_bypath (Npcfid *root, char *path, u32 flags, u32 mode, |
| 199 |
gid_t gid); |
| 200 |
|
| 201 |
/* Like read (2). Just a npc_pread() using offset stored in fid. |
| 202 |
* Returns bytes read, 0 on EOF, or -1 on error (retrieve with np_rerror ()). |
| 203 |
*/ |
| 204 |
int npc_read(Npcfid *fid, void *buf, u32 count); |
| 205 |
|
| 206 |
/* Shorthand for walk/open/read[count]/close, i.e. read the whole file. |
| 207 |
* Returns bytes read, or -1 on error (retrieve with np_rerror ()). |
| 208 |
*/ |
| 209 |
int npc_get(Npcfid *root, char *path, void *buf, u32 count); |
| 210 |
|
| 211 |
/* Same as npc_get but result is a null terminated string that the caller |
| 212 |
* must free. Returns NULL on error (retrieve with np_rerror ()). |
| 213 |
*/ |
| 214 |
char *npc_aget(Npcfid *root, char *path); |
| 215 |
|
| 216 |
/* npc_read_all() up to and including the next '\n' character, or until buffer |
| 217 |
* is exhausted, whichever comes first. |
| 218 |
* Returns bytes read, 0 on EOF, or -1 on error (retrieve with np_rerror ()). |
| 219 |
*/ |
| 220 |
char *npc_gets(Npcfid *fid, char *buf, u32 count); |
| 221 |
|
| 222 |
/* Like write (2). Just a npc_pwrite () using offset stored in fid. |
| 223 |
* Returns bytes written or -1 on error (retrieve with np_rerror ()). |
| 224 |
*/ |
| 225 |
int npc_write(Npcfid *fid, void *buf, u32 count); |
| 226 |
|
| 227 |
/* Shorthand for walk/open/write[count]/close, i.e. overwrite entire |
| 228 |
* existing file with buffer. |
| 229 |
* Returns bytes written or -1 on error (retrieve with np_rerror ()). |
| 230 |
*/ |
| 231 |
int npc_put(Npcfid *root, char *path, void *buf, u32 count); |
| 232 |
|
| 233 |
/* Like npc_write(fid,buf,strlen(buf)) except issue multiple writes as needed. |
| 234 |
* Returns bytes written or -1 on error (retrieve with np_rerror ()). |
| 235 |
*/ |
| 236 |
int npc_puts(Npcfid *fid, char *buf); |
| 237 |
|
| 238 |
/* Change the file offset associated with 'fid'. This is like lseek (2). |
| 239 |
* The state is local, kept within the fid. N.B. SEEK_END doesn't work yet. |
| 240 |
*/ |
| 241 |
u64 npc_lseek(Npcfid *fid, u64 offset, int whence); |
| 242 |
|
| 243 |
/* Like mkdir (2). Shorthand for walk/mkdir/clunk. |
| 244 |
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). |
| 245 |
*/ |
| 246 |
int npc_mkdir_bypath (Npcfid *root, char *path, u32 mode); |
| 247 |
|
| 248 |
/* Like fstat (2). Maps directly to a getattr, but we use struct stat. |
| 249 |
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). |
| 250 |
*/ |
| 251 |
int npc_fstat (Npcfid *fid, struct stat *sb); |
| 252 |
|
| 253 |
/* Like stat (2). Shorthand for walk/getattr/clunk. |
| 254 |
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). |
| 255 |
*/ |
| 256 |
int npc_stat (Npcfid *root, char *path, struct stat *sb); |
| 257 |
|
| 258 |
/* Like unlink/rmdir (2). Shorthand for walk/remove. |
| 259 |
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). |
| 260 |
*/ |
| 261 |
int npc_remove_bypath (Npcfid *root, char *path); |
| 262 |
|
| 263 |
/* Shorthand for walk/open, and initializes internally for npc_readdir_r() |
| 264 |
* Returns fid for file, or NULL on error (retrieve with np_rerror ()). |
| 265 |
*/ |
| 266 |
Npcfid *npc_opendir (Npcfid *root, char *path); |
| 267 |
|
| 268 |
/* Read a directory entry from directory 'fid' that was opened with |
| 269 |
* npc_opendir(). Entry points to dirent storage allocated by the caller. |
| 270 |
* Like readdir_r, returns 0 on success (>0) errno on failure. |
| 271 |
* On EOF set result to NULL, otherwise set to entry. |
| 272 |
*/ |
| 273 |
int npc_readdir_r (Npcfid *fid, struct dirent *entry, struct dirent **result); |
| 274 |
void npc_seekdir (Npcfid *fid, long offset); |
| 275 |
long npc_telldir (Npcfid *fid); |
| 276 |
|
| 277 |
/* Wrappers for setattr. |
| 278 |
*/ |
| 279 |
int npc_fchmod (Npcfid *fid, mode_t mode); |
| 280 |
int npc_chmod (Npcfid *root, char *path, mode_t mode); |
| 281 |
int npc_fchown (Npcfid *fid, uid_t owner, gid_t group); |
| 282 |
int npc_chown (Npcfid *root, char *path, uid_t owner, gid_t group); |
| 283 |
int npc_ftruncate (Npcfid *fid, off_t length); |
| 284 |
int npc_truncate (Npcfid *root, char *path, off_t length); |
| 285 |
int npc_futime (Npcfid *fid, const struct utimbuf *times); |
| 286 |
int npc_utime(Npcfid *root, char *path, const struct utimbuf *times); |
| 287 |
|
| 288 |
/* Wrappers for xattrwalk/xattrcreate. |
| 289 |
*/ |
| 290 |
ssize_t npc_listxattr (Npcfid *root, char *path, char *buf, size_t size); |
| 291 |
ssize_t npc_getxattr (Npcfid *root, char *path, char *attr, |
| 292 |
char *buf, size_t size); |
| 293 |
int npc_setxattr (Npcfid *root, char *path, char *name, char *val, size_t size, |
| 294 |
int flags); |