PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/core/10.5/fusefs/fuse_file.h

http://macfuse.googlecode.com/
C++ Header | 174 lines | 130 code | 28 blank | 16 comment | 24 complexity | 5756de9c66474cba86f6700ee3ff2a48 MD5 | raw file
  1/*
  2 * Copyright (C) 2006-2008 Google. All Rights Reserved.
  3 * Amit Singh <singh@>
  4 */
  5
  6#ifndef _FUSE_FILE_H_
  7#define _FUSE_FILE_H_
  8
  9#include <sys/fcntl.h>
 10#include <sys/kauth.h>
 11#include <sys/stat.h>
 12#include <sys/mman.h>
 13#include <sys/mount.h>
 14#include <sys/types.h>
 15#include <sys/vnode.h>
 16
 17typedef enum fufh_type {
 18    FUFH_INVALID = -1,
 19    FUFH_RDONLY  = 0,
 20    FUFH_WRONLY  = 1,
 21    FUFH_RDWR    = 2,
 22    FUFH_MAXTYPE = 3,
 23} fufh_type_t;
 24
 25struct fuse_filehandle {
 26    uint64_t fh_id;
 27    int32_t  open_count;
 28    int32_t  open_flags;
 29    int32_t  fuse_open_flags;
 30    int32_t  aux_count;
 31};
 32typedef struct fuse_filehandle * fuse_filehandle_t;
 33
 34#define FUFH_IS_VALID(f)  ((f)->open_count > 0)
 35#define FUFH_USE_INC(f)   ((f)->open_count++)
 36#define FUFH_USE_DEC(f)   ((f)->open_count--)
 37#define FUFH_USE_RESET(f) ((f)->open_count = 0)
 38#define FUFH_AUX_INC(f)   ((f)->aux_count++)
 39
 40static __inline__
 41fufh_type_t
 42fuse_filehandle_xlate_from_mmap(int fflags)
 43{
 44    if (fflags & PROT_WRITE) {
 45        if (fflags & (PROT_READ | PROT_EXEC)) {
 46            return FUFH_RDWR;
 47        } else {
 48            return FUFH_WRONLY;
 49        }
 50    } else if (fflags & (PROT_READ | PROT_EXEC)) {
 51        return FUFH_RDONLY;
 52    } else {
 53        IOLog("MacFUSE: mmap being attempted with no region accessibility\n");
 54        return FUFH_INVALID;
 55    }
 56}
 57
 58static __inline__
 59fufh_type_t
 60fuse_filehandle_xlate_from_fflags(int fflags)
 61{
 62    if ((fflags & FREAD) && (fflags & FWRITE)) {
 63        return FUFH_RDWR;
 64    } else if (fflags & (FWRITE)) {
 65        return FUFH_WRONLY;
 66    } else if (fflags & (FREAD)) {
 67        return FUFH_RDONLY;
 68    } else {
 69        /*
 70         * Looks like there might be a code path in Apple's
 71         * IOHDIXController/AppleDiskImagesFileBackingStore
 72         * that calls vnode_open() with a 0 fmode argument.
 73         * Translate 0 to FREAD, which is most likely what
 74         * that kext intends to do anyway. Lets hope the
 75         * calls to VNOP_OPEN and VNOP_CLOSE do match up
 76         * even with this fudging.
 77         */
 78        if (fflags == 0) {
 79            return FUFH_RDONLY;
 80        } else {
 81            panic("MacFUSE: What kind of a flag is this (%x)?", fflags);
 82        }
 83    }
 84
 85    return FUFH_INVALID;
 86}
 87
 88static __inline__
 89int
 90fuse_filehandle_xlate_to_oflags(fufh_type_t type)
 91{
 92    int oflags = -1;
 93
 94    switch (type) {
 95
 96    case FUFH_RDONLY:
 97        oflags = O_RDONLY;
 98        break;
 99
100    case FUFH_WRONLY:
101        oflags = O_WRONLY;
102        break;
103
104    case FUFH_RDWR:
105        oflags = O_RDWR;
106        break;
107
108    default:
109        break;
110    }
111
112    return oflags;
113}
114
115/*
116 * 0 return => can proceed
117 */
118static __inline__
119int
120fuse_filehandle_preflight_status(vnode_t vp, vnode_t dvp, vfs_context_t context,
121                                 fufh_type_t fufh_type)
122{
123    vfs_context_t icontext = context;
124    kauth_action_t action  = 0;
125    mount_t mp = vnode_mount(vp);
126    int err = 0;
127
128    if (vfs_authopaque(mp) || !vfs_issynchronous(mp) || !vnode_isreg(vp)) {
129        goto out;
130    }
131
132#if M_MACFUSE_ENABLE_UNSUPPORTED
133    if (!icontext) {
134        icontext = vfs_context_current();
135    }
136#endif /* M_MACFUSE_ENABLE_UNSUPPORTED */
137
138    if (!icontext) {
139        goto out;
140    }
141
142    switch (fufh_type) {
143    case FUFH_RDONLY:
144        action |= KAUTH_VNODE_READ_DATA;
145        break;
146
147    case FUFH_WRONLY:
148        action |= KAUTH_VNODE_WRITE_DATA;
149        break;
150
151    case FUFH_RDWR:
152        action |= (KAUTH_VNODE_READ_DATA | KAUTH_VNODE_WRITE_DATA);
153        break;
154
155    default: 
156        err = EINVAL;
157        break;
158    }
159
160    if (!err) {
161        err = vnode_authorize(vp, dvp, action, icontext);
162    }
163
164out:
165    return err;
166}
167
168int fuse_filehandle_get(vnode_t vp, vfs_context_t context,
169                        fufh_type_t fufh_type, int mode);
170
171int fuse_filehandle_put(vnode_t vp, vfs_context_t context,
172                        fufh_type_t fufh_type, fuse_op_waitfor_t waitfor);
173
174#endif /* _FUSE_FILE_H_ */