1 /* 2 * Copyright (c) 2007-2009 Google Inc. and Amit Singh 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Copyright (C) 2005 Csaba Henk. 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 43 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53 * SUCH DAMAGE. 54 * 55 * $FreeBSD$ 56 */ 57 58 #ifndef _FUSE_INTERNAL_H_ 59 #define _FUSE_INTERNAL_H_ 60 61 #include <sys/types.h> 62 #include <sys/uio.h> 63 #include <sys/stat.h> 64 #include <sys/vnode.h> 65 66 #include "fuse_ipc.h" 67 #include "fuse_node.h" 68 69 static __inline int 70 vfs_isrdonly(struct mount *mp) 71 { 72 return ((mp->mnt_flag & MNT_RDONLY) != 0 ? 1 : 0); 73 } 74 75 static __inline struct mount * 76 vnode_mount(struct vnode *vp) 77 { 78 return (vp->v_mount); 79 } 80 81 static __inline int 82 vnode_mountedhere(struct vnode *vp) 83 { 84 return (vp->v_mountedhere != NULL ? 1 : 0); 85 } 86 87 static __inline enum vtype 88 vnode_vtype(struct vnode *vp) 89 { 90 return (vp->v_type); 91 } 92 93 static __inline int 94 vnode_isvroot(struct vnode *vp) 95 { 96 return ((vp->v_vflag & VV_ROOT) != 0 ? 1 : 0); 97 } 98 99 static __inline int 100 vnode_isreg(struct vnode *vp) 101 { 102 return (vp->v_type == VREG ? 1 : 0); 103 } 104 105 static __inline int 106 vnode_isdir(struct vnode *vp) 107 { 108 return (vp->v_type == VDIR ? 1 : 0); 109 } 110 111 static __inline int 112 vnode_islnk(struct vnode *vp) 113 { 114 return (vp->v_type == VLNK ? 1 : 0); 115 } 116 117 static __inline ssize_t 118 uio_resid(struct uio *uio) 119 { 120 return (uio->uio_resid); 121 } 122 123 static __inline off_t 124 uio_offset(struct uio *uio) 125 { 126 return (uio->uio_offset); 127 } 128 129 static __inline void 130 uio_setoffset(struct uio *uio, off_t offset) 131 { 132 uio->uio_offset = offset; 133 } 134 135 static __inline void 136 uio_setresid(struct uio *uio, ssize_t resid) 137 { 138 uio->uio_resid = resid; 139 } 140 141 /* time */ 142 143 #define fuse_timespec_add(vvp, uvp) \ 144 do { \ 145 (vvp)->tv_sec += (uvp)->tv_sec; \ 146 (vvp)->tv_nsec += (uvp)->tv_nsec; \ 147 if ((vvp)->tv_nsec >= 1000000000) { \ 148 (vvp)->tv_sec++; \ 149 (vvp)->tv_nsec -= 1000000000; \ 150 } \ 151 } while (0) 152 153 #define fuse_timespec_cmp(tvp, uvp, cmp) \ 154 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 155 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ 156 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 157 158 /* miscellaneous */ 159 160 static __inline__ 161 int 162 fuse_isdeadfs(struct vnode *vp) 163 { 164 struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp)); 165 166 return (data->dataflags & FSESS_DEAD); 167 } 168 169 static __inline__ 170 int 171 fuse_iosize(struct vnode *vp) 172 { 173 return vp->v_mount->mnt_stat.f_iosize; 174 } 175 176 /* access */ 177 178 #define FVP_ACCESS_NOOP 0x01 179 180 #define FACCESS_VA_VALID 0x01 181 #define FACCESS_DO_ACCESS 0x02 182 #define FACCESS_STICKY 0x04 183 #define FACCESS_CHOWN 0x08 184 #define FACCESS_NOCHECKSPY 0x10 185 #define FACCESS_SETGID 0x12 186 187 #define FACCESS_XQUERIES FACCESS_STICKY | FACCESS_CHOWN | FACCESS_SETGID 188 189 struct fuse_access_param { 190 uid_t xuid; 191 gid_t xgid; 192 uint32_t facc_flags; 193 }; 194 195 static __inline int 196 fuse_match_cred(struct ucred *basecred, struct ucred *usercred) 197 { 198 if (basecred->cr_uid == usercred->cr_uid && 199 basecred->cr_uid == usercred->cr_ruid && 200 basecred->cr_uid == usercred->cr_svuid && 201 basecred->cr_groups[0] == usercred->cr_groups[0] && 202 basecred->cr_groups[0] == usercred->cr_rgid && 203 basecred->cr_groups[0] == usercred->cr_svgid) 204 return 0; 205 206 return EPERM; 207 } 208 209 int 210 fuse_internal_access(struct vnode *vp, 211 mode_t mode, 212 struct fuse_access_param *facp, 213 struct thread *td, 214 struct ucred *cred); 215 216 /* attributes */ 217 218 static __inline 219 void 220 fuse_internal_attr_fat2vat(struct mount *mp, 221 struct fuse_attr *fat, 222 struct vattr *vap) 223 { 224 DEBUGX(FUSE_DEBUG_INTERNAL, 225 "node #%ju, mode 0%o\n", (uintmax_t)fat->ino, fat->mode); 226 227 vattr_null(vap); 228 229 vap->va_fsid = mp->mnt_stat.f_fsid.val[0]; 230 vap->va_fileid = fat->ino; /* XXX cast from 64 bits to 32 */ 231 vap->va_mode = fat->mode & ~S_IFMT; 232 vap->va_nlink = fat->nlink; 233 vap->va_uid = fat->uid; 234 vap->va_gid = fat->gid; 235 vap->va_rdev = fat->rdev; 236 vap->va_size = fat->size; 237 vap->va_atime.tv_sec = fat->atime; /* XXX on some platforms cast from 64 bits to 32 */ 238 vap->va_atime.tv_nsec = fat->atimensec; 239 vap->va_mtime.tv_sec = fat->mtime; 240 vap->va_mtime.tv_nsec = fat->mtimensec; 241 vap->va_ctime.tv_sec = fat->ctime; 242 vap->va_ctime.tv_nsec = fat->ctimensec; 243 vap->va_blocksize = PAGE_SIZE; 244 vap->va_type = IFTOVT(fat->mode); 245 246 #if (S_BLKSIZE == 512) 247 /* Optimize this case */ 248 vap->va_bytes = fat->blocks << 9; 249 #else 250 vap->va_bytes = fat->blocks * S_BLKSIZE; 251 #endif 252 253 vap->va_flags = 0; 254 } 255 256 257 #define cache_attrs(vp, fuse_out) do { \ 258 struct timespec uptsp_ ## __func__; \ 259 \ 260 VTOFUD(vp)->cached_attrs_valid.tv_sec = (fuse_out)->attr_valid; \ 261 VTOFUD(vp)->cached_attrs_valid.tv_nsec = (fuse_out)->attr_valid_nsec; \ 262 nanouptime(&uptsp_ ## __func__); \ 263 \ 264 fuse_timespec_add(&VTOFUD(vp)->cached_attrs_valid, &uptsp_ ## __func__); \ 265 \ 266 fuse_internal_attr_fat2vat(vnode_mount(vp), &(fuse_out)->attr, VTOVA(vp)); \ 267 } while (0) 268 269 /* fsync */ 270 271 int 272 fuse_internal_fsync(struct vnode *vp, 273 struct thread *td, 274 struct ucred *cred, 275 struct fuse_filehandle *fufh); 276 277 int 278 fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio); 279 280 /* readdir */ 281 282 struct pseudo_dirent { 283 uint32_t d_namlen; 284 }; 285 286 int 287 fuse_internal_readdir(struct vnode *vp, 288 struct uio *uio, 289 struct fuse_filehandle *fufh, 290 struct fuse_iov *cookediov); 291 292 int 293 fuse_internal_readdir_processdata(struct uio *uio, 294 size_t reqsize, 295 void *buf, 296 size_t bufsize, 297 void *param); 298 299 /* remove */ 300 301 int 302 fuse_internal_remove(struct vnode *dvp, 303 struct vnode *vp, 304 struct componentname *cnp, 305 enum fuse_opcode op); 306 307 /* rename */ 308 309 int 310 fuse_internal_rename(struct vnode *fdvp, 311 struct componentname *fcnp, 312 struct vnode *tdvp, 313 struct componentname *tcnp); 314 /* revoke */ 315 316 void 317 fuse_internal_vnode_disappear(struct vnode *vp); 318 319 /* strategy */ 320 321 /* entity creation */ 322 323 static __inline 324 int 325 fuse_internal_checkentry(struct fuse_entry_out *feo, enum vtype vtyp) 326 { 327 DEBUGX(FUSE_DEBUG_INTERNAL, 328 "feo=%p, vtype=%d\n", feo, vtyp); 329 330 if (vtyp != IFTOVT(feo->attr.mode)) { 331 DEBUGX(FUSE_DEBUG_INTERNAL, 332 "EINVAL -- %x != %x\n", vtyp, IFTOVT(feo->attr.mode)); 333 return EINVAL; 334 } 335 336 if (feo->nodeid == FUSE_NULL_ID) { 337 DEBUGX(FUSE_DEBUG_INTERNAL, 338 "EINVAL -- feo->nodeid is NULL\n"); 339 return EINVAL; 340 } 341 342 if (feo->nodeid == FUSE_ROOT_ID) { 343 DEBUGX(FUSE_DEBUG_INTERNAL, 344 "EINVAL -- feo->nodeid is FUSE_ROOT_ID\n"); 345 return EINVAL; 346 } 347 348 return 0; 349 } 350 351 int 352 fuse_internal_newentry(struct vnode *dvp, 353 struct vnode **vpp, 354 struct componentname *cnp, 355 enum fuse_opcode op, 356 void *buf, 357 size_t bufsize, 358 enum vtype vtyp); 359 360 void 361 fuse_internal_newentry_makerequest(struct mount *mp, 362 uint64_t dnid, 363 struct componentname *cnp, 364 enum fuse_opcode op, 365 void *buf, 366 size_t bufsize, 367 struct fuse_dispatcher *fdip); 368 369 int 370 fuse_internal_newentry_core(struct vnode *dvp, 371 struct vnode **vpp, 372 struct componentname *cnp, 373 enum vtype vtyp, 374 struct fuse_dispatcher *fdip); 375 376 /* entity destruction */ 377 378 int 379 fuse_internal_forget_callback(struct fuse_ticket *tick, struct uio *uio); 380 381 void 382 fuse_internal_forget_send(struct mount *mp, 383 struct thread *td, 384 struct ucred *cred, 385 uint64_t nodeid, 386 uint64_t nlookup); 387 388 /* fuse start/stop */ 389 390 int fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio); 391 void fuse_internal_send_init(struct fuse_data *data, struct thread *td); 392 393 #endif /* _FUSE_INTERNAL_H_ */ 394