1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Darwin porting library
23 * Make things easy to port
25 #include <mach/mach_types.h>
28 #include <sys/malloc.h>
30 #include <sys/mount.h>
32 #include <sys/filedesc.h>
33 #include <sys/namei.h>
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <libcfs/libcfs.h>
40 * Kernel APIs for file system in xnu
46 #include <sys/vnode.h>
48 extern int vn_rdwr(enum uio_rw, vnode_t, caddr_t, int, off_t, enum uio_seg, int, kauth_cred_t, int *, proc_t);
50 /* vnode_size() is not exported */
52 vnode_size(vnode_t vp, off_t *sizep, vfs_context_t ctx)
58 VATTR_WANTED(&va, va_data_size);
59 error = vnode_getattr(vp, &va, ctx);
61 *sizep = va.va_data_size;
68 * kern_file_*() are not safe for multi-threads now,
69 * however, we need them only for tracefiled, so it's
70 * not so important to implement for MT.
73 kern_file_size(struct cfs_kern_file *fp, off_t *psize)
78 error = vnode_size(fp->f_vp, &size, fp->f_ctxt);
87 struct cfs_kern_file *
88 kern_file_open(const char * filename, int uflags, int mode, int *err)
90 struct cfs_kern_file *fp;
94 fp = (struct cfs_kern_file *)_MALLOC(sizeof(struct cfs_kern_file), M_TEMP, M_WAITOK);
100 fp->f_flags = FFLAGS(uflags);
101 fp->f_ctxt = vfs_context_create(NULL);
103 if ((error = vnode_open(filename, fp->f_flags,
104 mode, 0, &vp, fp->f_ctxt))){
118 kern_file_close(struct cfs_kern_file *fp)
120 vnode_close(fp->f_vp, fp->f_flags, fp->f_ctxt);
121 vfs_context_rele(fp->f_ctxt);
128 kern_file_read(struct cfs_kern_file *fp, void *buf, size_t nbytes, loff_t *pos)
130 struct proc *p = current_proc();
135 assert(fp != NULL && fp->f_vp != NULL);
137 error = vn_rdwr(UIO_READ, fp->f_vp, buf, nbytes, *pos,
138 UIO_SYSSPACE32, 0, vfs_context_ucred(fp->f_ctxt), &resid, p);
139 if ((error) || (nbytes == resid)) {
144 *pos += nbytes - resid;
146 return (int)(nbytes - resid);
150 kern_file_write(struct cfs_kern_file *fp, void *buf, size_t nbytes, loff_t *pos)
152 struct proc *p = current_proc();
157 assert(fp != NULL && fp->f_vp != NULL);
159 error = vn_rdwr(UIO_WRITE, fp->f_vp, buf, nbytes, *pos,
160 UIO_SYSSPACE32, 0, vfs_context_ucred(fp->f_ctxt), &resid, p);
161 if ((error) || (nbytes == resid)) {
166 *pos += nbytes - resid;
168 return (int)(nbytes - resid);
173 kern_file_sync (struct cfs_kern_file *fp)
175 return VNOP_FSYNC(fp->f_vp, MNT_WAIT, fp->f_ctxt);
178 #else /* !__DARWIN8__ */
181 kern_file_size(struct file *fp, off_t *size)
183 struct vnode *vp = (struct vnode *)fp->f_data;
187 rc = vn_stat(vp, &sb, current_proc());
197 kern_file_open(const char * filename, int flags, int mode, int *err)
201 register struct vnode *vp;
203 extern struct fileops vnops;
209 MALLOC_ZONE(fp, cfs_file_t *, sizeof(cfs_file_t), M_FILE, M_WAITOK|M_ZERO);
210 bzero(fp, sizeof(cfs_file_t));
212 LIST_CIRCLE(fp, f_list);
213 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, (char *)filename, current_proc());
214 if ((rc = vn_open(&nd, flags, mode)) != 0){
215 printf("filp_open failed at (%d)\n", rc);
218 FREE_ZONE(fp, sizeof *fp, M_FILE);
223 fp->f_flag = flags & FMASK;
224 fp->f_type = DTYPE_VNODE;
226 fp->f_data = (caddr_t)vp;
227 fp->f_cred = current_proc()->p_ucred;
229 * Hold cred to increase reference
233 * vnode is locked inside vn_open for lookup,
234 * we should release the lock before return
236 VOP_UNLOCK(vp, 0, current_proc());
243 frele_internal(cfs_file_t *fp)
245 if (fp->f_count == (short)0xffff)
246 panic("frele of lustre: stale");
247 if (--fp->f_count < 0)
248 panic("frele of lustre: count < 0");
249 return ((int)fp->f_count);
253 kern_file_close (cfs_file_t *fp)
262 if (frele_internal(fp) > 0)
264 vp = (struct vnode *)fp->f_data;
265 (void )vn_close(vp, fp->f_flag, fp->f_cred, current_proc());
268 * Dont use ffree to release fp!!!!
269 * ffree will call LIST_REMOVE(fp),
270 * but fp is not in any list, this will
275 if (cred != NOCRED) {
281 memset(fp, 0xff, sizeof *fp);
282 fp->f_count = (short)0xffff;
283 FREE_ZONE(fp, sizeof *fp, M_FILE);
289 extern void bwillwrite(void);
292 * Write buffer to filp inside kernel
295 kern_file_write (cfs_file_t *fp, void *buf, size_t nbyte, loff_t *pos)
299 struct proc *p = current_proc();
304 aiov.iov_base = (void *)(uintptr_t)buf;
305 aiov.iov_len = nbyte;
306 auio.uio_iov = &aiov;
309 auio.uio_offset = *pos;
311 * Liang: If don't set FOF_OFFSET, vn_write()
312 * will use fp->f_offset as the the real offset.
317 auio.uio_offset = (off_t)-1;
320 auio.uio_resid = nbyte;
321 auio.uio_rw = UIO_WRITE;
322 auio.uio_segflg = UIO_SYSSPACE;
327 if (fp->f_type == DTYPE_VNODE)
328 bwillwrite(); /* empty stuff now */
329 if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
330 if (auio.uio_resid != cnt && (error == ERESTART ||\
331 error == EINTR || error == EWOULDBLOCK))
333 /* The socket layer handles SIGPIPE */
334 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
341 cnt -= auio.uio_resid;
348 * Read from filp inside kernel
351 kern_file_read (cfs_file_t *fp, void *buf, size_t nbyte, loff_t *pos)
355 struct proc *p = current_proc();
360 aiov.iov_base = (caddr_t)buf;
361 aiov.iov_len = nbyte;
362 auio.uio_iov = &aiov;
365 auio.uio_offset = *pos;
368 auio.uio_offset = (off_t)-1;
371 auio.uio_resid = nbyte;
372 auio.uio_rw = UIO_READ;
373 auio.uio_segflg = UIO_SYSSPACE;
378 if ((error = fo_read(fp, &auio, fp->f_cred, flags, p)) != 0) {
379 if (auio.uio_resid != cnt && (error == ERESTART ||
380 error == EINTR || error == EWOULDBLOCK))
387 cnt -= auio.uio_resid;
395 kern_file_sync (cfs_file_t *fp)
397 struct vnode *vp = (struct vnode *)fp->f_data;
398 struct proc *p = current_proc();
403 if (fref(fp) == -1) {
407 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
408 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
409 VOP_UNLOCK(vp, 0, p);
416 #endif /* !__DARWIN8__ */
418 struct posix_acl *posix_acl_alloc(int count, int flags)
420 static struct posix_acl acl;
425 * XXX Liang: I've not converted all of them,
428 int cfs_oflags2univ(int flags)
432 f = flags & O_ACCMODE;
433 f |= (flags & O_CREAT) ? CFS_O_CREAT: 0;
434 f |= (flags & O_TRUNC) ? CFS_O_TRUNC: 0;
435 f |= (flags & O_EXCL) ? CFS_O_EXCL: 0;
436 f |= (flags & O_NONBLOCK) ? CFS_O_NONBLOCK: 0;
437 f |= (flags & O_APPEND) ? CFS_O_APPEND: 0;
438 f |= (flags & O_NOFOLLOW) ? CFS_O_NOFOLLOW: 0;
439 f |= (flags & O_SYNC)? CFS_O_SYNC: 0;
444 * XXX Liang: we don't need it in OSX.
445 * But it should be implemented anyway.
447 int cfs_univ2oflags(int flags)