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
69static __inline int
70vfs_isrdonly(struct mount *mp)
71{
72    return ((mp->mnt_flag & MNT_RDONLY) != 0 ? 1 : 0);
73}
74
75static __inline struct mount *
76vnode_mount(struct vnode *vp)
77{
78	return (vp->v_mount);
79}
80
81static __inline int
82vnode_mountedhere(struct vnode *vp)
83{
84	return (vp->v_mountedhere != NULL ? 1 : 0);
85}
86
87static __inline enum vtype
88vnode_vtype(struct vnode *vp)
89{
90    return (vp->v_type);
91}
92
93static __inline int
94vnode_isvroot(struct vnode *vp)
95{
96    return ((vp->v_vflag & VV_ROOT) != 0 ? 1 : 0);
97}
98
99static __inline int
100vnode_isreg(struct vnode *vp)
101{
102    return (vp->v_type == VREG ? 1 : 0);
103}
104
105static __inline int
106vnode_isdir(struct vnode *vp)
107{
108    return (vp->v_type == VDIR ? 1 : 0);
109}
110
111static __inline int
112vnode_islnk(struct vnode *vp)
113{
114    return (vp->v_type == VLNK ? 1 : 0);
115}
116
117static __inline ssize_t
118uio_resid(struct uio *uio)
119{
120    return (uio->uio_resid);
121}
122
123static __inline off_t
124uio_offset(struct uio *uio)
125{
126    return (uio->uio_offset);
127}
128
129static __inline void
130uio_setoffset(struct uio *uio, off_t offset)
131{
132    uio->uio_offset = offset;
133}
134
135static __inline void
136uio_setresid(struct uio *uio, ssize_t resid)
137{
138    uio->uio_resid = resid;
139}
140
141/* miscellaneous */
142
143static __inline__
144int
145fuse_isdeadfs(struct vnode *vp)
146{
147    struct fuse_data *data = fuse_get_mpdata(vnode_mount(vp));
148
149    return (data->dataflags & FSESS_DEAD);
150}
151
152static __inline__
153int
154fuse_iosize(struct vnode *vp)
155{
156    return vp->v_mount->mnt_stat.f_iosize;
157}
158
159/* access */
160
161#define FVP_ACCESS_NOOP   0x01
162
163#define FACCESS_VA_VALID   0x01
164#define FACCESS_DO_ACCESS  0x02
165#define FACCESS_STICKY     0x04
166#define FACCESS_CHOWN      0x08
167#define FACCESS_NOCHECKSPY 0x10
168#define FACCESS_SETGID     0x12
169
170#define FACCESS_XQUERIES FACCESS_STICKY | FACCESS_CHOWN | FACCESS_SETGID
171
172struct fuse_access_param {
173    uid_t    xuid;
174    gid_t    xgid;
175    uint32_t facc_flags;
176};
177
178static __inline int
179fuse_match_cred(struct ucred *basecred, struct ucred *usercred)
180{
181	if (basecred->cr_uid == usercred->cr_uid             &&
182	    basecred->cr_uid == usercred->cr_ruid            &&
183	    basecred->cr_uid == usercred->cr_svuid           &&
184	    basecred->cr_groups[0] == usercred->cr_groups[0] &&
185	    basecred->cr_groups[0] == usercred->cr_rgid      &&
186	    basecred->cr_groups[0] == usercred->cr_svgid)
187		return 0;
188
189	return EPERM;
190}
191
192int
193fuse_internal_access(struct vnode *vp,
194                     mode_t mode,
195                     struct fuse_access_param *facp,
196                     struct thread *td,
197                     struct ucred *cred);
198
199/* attributes */
200
201static __inline
202void
203fuse_internal_attr_fat2vat(struct mount *mp,
204                           struct fuse_attr *fat,
205                           struct vattr *vap)
206{
207    DEBUGX(FUSE_DEBUG_INTERNAL,
208        "node #%ju, mode 0%o\n", (uintmax_t)fat->ino, fat->mode);
209
210    vattr_null(vap);
211
212    vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
213    vap->va_fileid = fat->ino; /* XXX cast from 64 bits to 32 */
214    vap->va_mode = fat->mode & ~S_IFMT;
215    vap->va_nlink     = fat->nlink;
216    vap->va_uid       = fat->uid;
217    vap->va_gid       = fat->gid;
218    vap->va_rdev      = fat->rdev;
219    vap->va_size      = fat->size;
220    vap->va_atime.tv_sec  = fat->atime; /* XXX on some platforms cast from 64 bits to 32 */
221    vap->va_atime.tv_nsec = fat->atimensec;
222    vap->va_mtime.tv_sec  = fat->mtime;
223    vap->va_mtime.tv_nsec = fat->mtimensec;
224    vap->va_ctime.tv_sec  = fat->ctime;
225    vap->va_ctime.tv_nsec = fat->ctimensec;
226    vap->va_blocksize = PAGE_SIZE;
227    vap->va_type = IFTOVT(fat->mode);
228
229#if (S_BLKSIZE == 512)
230    /* Optimize this case */
231    vap->va_bytes = fat->blocks << 9;
232#else
233    vap->va_bytes = fat->blocks * S_BLKSIZE;
234#endif
235
236    vap->va_flags = 0;
237}
238
239
240#define	cache_attrs(vp, fuse_out)					\
241	fuse_internal_attr_fat2vat(vnode_mount(vp), &(fuse_out)->attr,	\
242	    VTOVA(vp));
243
244/* fsync */
245
246int
247fuse_internal_fsync(struct vnode           *vp,
248                    struct thread          *td,
249                    struct ucred           *cred,
250                    struct fuse_filehandle *fufh);
251
252int
253fuse_internal_fsync_callback(struct fuse_ticket *tick, struct uio *uio);
254
255/* readdir */
256
257struct pseudo_dirent {
258    uint32_t d_namlen;
259};
260
261int
262fuse_internal_readdir(struct vnode           *vp,
263                      struct uio             *uio,
264                      struct fuse_filehandle *fufh,
265                      struct fuse_iov        *cookediov);
266
267int
268fuse_internal_readdir_processdata(struct uio *uio,
269                                  size_t reqsize,
270                                  void *buf,
271                                  size_t bufsize,
272                                  void *param);
273
274/* remove */
275
276int
277fuse_internal_remove(struct vnode *dvp,
278                     struct vnode *vp,
279                     struct componentname *cnp,
280                     enum fuse_opcode op);
281
282/* rename */
283
284int
285fuse_internal_rename(struct vnode *fdvp,
286                     struct componentname *fcnp,
287                     struct vnode *tdvp,
288                     struct componentname *tcnp);
289/* revoke */
290
291void
292fuse_internal_vnode_disappear(struct vnode *vp);
293
294/* strategy */
295
296/* entity creation */
297
298static __inline
299int
300fuse_internal_checkentry(struct fuse_entry_out *feo, enum vtype vtyp)
301{
302    DEBUGX(FUSE_DEBUG_INTERNAL,
303        "feo=%p, vtype=%d\n", feo, vtyp);
304
305    if (vtyp != IFTOVT(feo->attr.mode)) {
306        DEBUGX(FUSE_DEBUG_INTERNAL,
307            "EINVAL -- %x != %x\n", vtyp, IFTOVT(feo->attr.mode));
308        return EINVAL;
309    }
310
311    if (feo->nodeid == FUSE_NULL_ID) {
312        DEBUGX(FUSE_DEBUG_INTERNAL,
313            "EINVAL -- feo->nodeid is NULL\n");
314        return EINVAL;
315    }
316
317    if (feo->nodeid == FUSE_ROOT_ID) {
318        DEBUGX(FUSE_DEBUG_INTERNAL,
319            "EINVAL -- feo->nodeid is FUSE_ROOT_ID\n");
320        return EINVAL;
321    }
322
323    return 0;
324}
325
326int
327fuse_internal_newentry(struct vnode *dvp,
328                       struct vnode **vpp,
329                       struct componentname *cnp,
330                       enum fuse_opcode op,
331                       void *buf,
332                       size_t bufsize,
333                       enum vtype vtyp);
334
335void
336fuse_internal_newentry_makerequest(struct mount *mp,
337                                   uint64_t dnid,
338                                   struct componentname *cnp,
339                                   enum fuse_opcode op,
340                                   void *buf,
341                                   size_t bufsize,
342                                   struct fuse_dispatcher *fdip);
343
344int
345fuse_internal_newentry_core(struct vnode *dvp,
346                            struct vnode **vpp,
347                            struct componentname   *cnp,
348                            enum vtype vtyp,
349                            struct fuse_dispatcher *fdip);
350
351/* entity destruction */
352
353int
354fuse_internal_forget_callback(struct fuse_ticket *tick, struct uio *uio);
355
356void
357fuse_internal_forget_send(struct mount *mp,
358                          struct thread *td,
359                          struct ucred *cred,
360                          uint64_t nodeid,
361                          uint64_t nlookup);
362
363/* fuse start/stop */
364
365int fuse_internal_init_callback(struct fuse_ticket *tick, struct uio *uio);
366void fuse_internal_send_init(struct fuse_data *data, struct thread *td);
367
368#endif /* _FUSE_INTERNAL_H_ */
369