1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/darwin/darwin-fs.c
38 * Darwin porting library
39 * Make things easy to port
41 * Author: Phil Schwan <phil@clusterfs.com>
44 #include <mach/mach_types.h>
47 #include <sys/malloc.h>
49 #include <sys/mount.h>
51 #include <sys/filedesc.h>
52 #include <sys/namei.h>
54 #define DEBUG_SUBSYSTEM S_LNET
56 #include <libcfs/libcfs.h>
59 * Kernel APIs for file system in xnu
65 #include <sys/vnode.h>
67 extern int vn_rdwr(enum uio_rw, vnode_t, caddr_t, int, off_t, enum uio_seg, int, kauth_cred_t, int *, proc_t);
69 /* vnode_size() is not exported */
71 vnode_size(vnode_t vp, off_t *sizep, vfs_context_t ctx)
77 VATTR_WANTED(&va, va_data_size);
78 error = vnode_getattr(vp, &va, ctx);
80 *sizep = va.va_data_size;
87 * kern_file_*() are not safe for multi-threads now,
88 * however, we need them only for tracefiled, so it's
89 * not so important to implement for MT.
92 kern_file_size(struct cfs_kern_file *fp, off_t *psize)
97 error = vnode_size(fp->f_vp, &size, fp->f_ctxt);
106 struct cfs_kern_file *
107 kern_file_open(const char * filename, int uflags, int mode, int *err)
109 struct cfs_kern_file *fp;
113 fp = (struct cfs_kern_file *)_MALLOC(sizeof(struct cfs_kern_file), M_TEMP, M_WAITOK);
119 fp->f_flags = FFLAGS(uflags);
120 fp->f_ctxt = vfs_context_create(NULL);
122 if ((error = vnode_open(filename, fp->f_flags,
123 mode, 0, &vp, fp->f_ctxt))){
137 kern_file_close(struct cfs_kern_file *fp)
139 vnode_close(fp->f_vp, fp->f_flags, fp->f_ctxt);
140 vfs_context_rele(fp->f_ctxt);
147 kern_file_read(struct cfs_kern_file *fp, void *buf, size_t nbytes, loff_t *pos)
149 struct proc *p = current_proc();
154 assert(fp != NULL && fp->f_vp != NULL);
156 error = vn_rdwr(UIO_READ, fp->f_vp, buf, nbytes, *pos,
157 UIO_SYSSPACE32, 0, vfs_context_ucred(fp->f_ctxt), &resid, p);
158 if ((error) || (nbytes == resid)) {
163 *pos += nbytes - resid;
165 return (int)(nbytes - resid);
169 kern_file_write(struct cfs_kern_file *fp, void *buf, size_t nbytes, loff_t *pos)
171 struct proc *p = current_proc();
176 assert(fp != NULL && fp->f_vp != NULL);
178 error = vn_rdwr(UIO_WRITE, fp->f_vp, buf, nbytes, *pos,
179 UIO_SYSSPACE32, 0, vfs_context_ucred(fp->f_ctxt), &resid, p);
180 if ((error) || (nbytes == resid)) {
185 *pos += nbytes - resid;
187 return (int)(nbytes - resid);
192 kern_file_sync (struct cfs_kern_file *fp)
194 return VNOP_FSYNC(fp->f_vp, MNT_WAIT, fp->f_ctxt);
197 #else /* !__DARWIN8__ */
200 kern_file_size(struct file *fp, off_t *size)
202 struct vnode *vp = (struct vnode *)fp->f_data;
206 rc = vn_stat(vp, &sb, current_proc());
216 kern_file_open(const char * filename, int flags, int mode, int *err)
220 register struct vnode *vp;
222 extern struct fileops vnops;
228 MALLOC_ZONE(fp, cfs_file_t *, sizeof(cfs_file_t), M_FILE, M_WAITOK|M_ZERO);
229 bzero(fp, sizeof(cfs_file_t));
231 LIST_CIRCLE(fp, f_list);
232 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, (char *)filename, current_proc());
233 if ((rc = vn_open(&nd, flags, mode)) != 0){
234 printf("filp_open failed at (%d)\n", rc);
237 FREE_ZONE(fp, sizeof *fp, M_FILE);
242 fp->f_flag = flags & FMASK;
243 fp->f_type = DTYPE_VNODE;
245 fp->f_data = (caddr_t)vp;
246 fp->f_cred = current_proc()->p_ucred;
248 * Hold cred to increase reference
252 * vnode is locked inside vn_open for lookup,
253 * we should release the lock before return
255 VOP_UNLOCK(vp, 0, current_proc());
262 frele_internal(cfs_file_t *fp)
264 if (fp->f_count == (short)0xffff)
265 panic("frele of lustre: stale");
266 if (--fp->f_count < 0)
267 panic("frele of lustre: count < 0");
268 return ((int)fp->f_count);
272 kern_file_close (cfs_file_t *fp)
281 if (frele_internal(fp) > 0)
283 vp = (struct vnode *)fp->f_data;
284 (void )vn_close(vp, fp->f_flag, fp->f_cred, current_proc());
287 * Dont use ffree to release fp!!!!
288 * ffree will call LIST_REMOVE(fp),
289 * but fp is not in any list, this will
294 if (cred != NOCRED) {
300 memset(fp, 0xff, sizeof *fp);
301 fp->f_count = (short)0xffff;
302 FREE_ZONE(fp, sizeof *fp, M_FILE);
308 extern void bwillwrite(void);
311 * Write buffer to filp inside kernel
314 kern_file_write (cfs_file_t *fp, void *buf, size_t nbyte, loff_t *pos)
318 struct proc *p = current_proc();
323 aiov.iov_base = (void *)(uintptr_t)buf;
324 aiov.iov_len = nbyte;
325 auio.uio_iov = &aiov;
328 auio.uio_offset = *pos;
330 * Liang: If don't set FOF_OFFSET, vn_write()
331 * will use fp->f_offset as the the real offset.
336 auio.uio_offset = (off_t)-1;
339 auio.uio_resid = nbyte;
340 auio.uio_rw = UIO_WRITE;
341 auio.uio_segflg = UIO_SYSSPACE;
346 if (fp->f_type == DTYPE_VNODE)
347 bwillwrite(); /* empty stuff now */
348 if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
349 if (auio.uio_resid != cnt && (error == ERESTART ||\
350 error == EINTR || error == EWOULDBLOCK))
352 /* The socket layer handles SIGPIPE */
353 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
360 cnt -= auio.uio_resid;
367 * Read from filp inside kernel
370 kern_file_read (cfs_file_t *fp, void *buf, size_t nbyte, loff_t *pos)
374 struct proc *p = current_proc();
379 aiov.iov_base = (caddr_t)buf;
380 aiov.iov_len = nbyte;
381 auio.uio_iov = &aiov;
384 auio.uio_offset = *pos;
387 auio.uio_offset = (off_t)-1;
390 auio.uio_resid = nbyte;
391 auio.uio_rw = UIO_READ;
392 auio.uio_segflg = UIO_SYSSPACE;
397 if ((error = fo_read(fp, &auio, fp->f_cred, flags, p)) != 0) {
398 if (auio.uio_resid != cnt && (error == ERESTART ||
399 error == EINTR || error == EWOULDBLOCK))
406 cnt -= auio.uio_resid;
414 kern_file_sync (cfs_file_t *fp)
416 struct vnode *vp = (struct vnode *)fp->f_data;
417 struct proc *p = current_proc();
422 if (fref(fp) == -1) {
426 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
427 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
428 VOP_UNLOCK(vp, 0, p);
435 #endif /* !__DARWIN8__ */
437 struct posix_acl *posix_acl_alloc(int count, int flags)
439 static struct posix_acl acl;
444 * XXX Liang: I've not converted all of them,
447 int cfs_oflags2univ(int flags)
451 f = flags & O_ACCMODE;
452 f |= (flags & O_CREAT) ? CFS_O_CREAT: 0;
453 f |= (flags & O_TRUNC) ? CFS_O_TRUNC: 0;
454 f |= (flags & O_EXCL) ? CFS_O_EXCL: 0;
455 f |= (flags & O_NONBLOCK) ? CFS_O_NONBLOCK: 0;
456 f |= (flags & O_APPEND) ? CFS_O_APPEND: 0;
457 f |= (flags & O_NOFOLLOW) ? CFS_O_NOFOLLOW: 0;
458 f |= (flags & O_SYNC)? CFS_O_SYNC: 0;
463 * XXX Liang: we don't need it in OSX.
464 * But it should be implemented anyway.
466 int cfs_univ2oflags(int flags)