4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/libcfs/darwin/darwin-fs.c
36 * Darwin porting library
37 * Make things easy to port
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #include <mach/mach_types.h>
45 #include <sys/malloc.h>
47 #include <sys/mount.h>
49 #include <sys/filedesc.h>
50 #include <sys/namei.h>
52 #define DEBUG_SUBSYSTEM S_LNET
54 #include <libcfs/libcfs.h>
57 * Kernel APIs for file system in xnu
63 #include <sys/vnode.h>
65 extern int vn_rdwr(enum uio_rw, vnode_t, caddr_t, int, off_t, enum uio_seg, int, kauth_cred_t, int *, proc_t);
67 /* vnode_size() is not exported */
69 vnode_size(vnode_t vp, off_t *sizep, vfs_context_t ctx)
75 VATTR_WANTED(&va, va_data_size);
76 error = vnode_getattr(vp, &va, ctx);
78 *sizep = va.va_data_size;
85 * kern_file_*() are not safe for multi-threads now,
86 * however, we need them only for tracefiled, so it's
87 * not so important to implement for MT.
90 kern_file_size(struct cfs_kern_file *fp, off_t *psize)
95 error = vnode_size(fp->f_vp, &size, fp->f_ctxt);
104 struct cfs_kern_file *
105 kern_file_open(const char *filename, int uflags, int mode)
107 struct cfs_kern_file *fp;
111 fp = (struct cfs_kern_file *)_MALLOC(sizeof(struct cfs_kern_file),
114 return ERR_PTR(-ENOMEM);
116 fp->f_flags = FFLAGS(uflags);
117 fp->f_ctxt = vfs_context_create(NULL);
119 error = vnode_open(filename, fp->f_flags, mode, 0, &vp, fp->f_ctxt);
122 return ERR_PTR(-error);
131 kern_file_close(struct cfs_kern_file *fp)
133 vnode_close(fp->f_vp, fp->f_flags, fp->f_ctxt);
134 vfs_context_rele(fp->f_ctxt);
141 kern_file_read(struct cfs_kern_file *fp, void *buf, size_t nbytes, loff_t *pos)
143 struct proc *p = current_proc();
148 assert(fp != NULL && fp->f_vp != NULL);
150 error = vn_rdwr(UIO_READ, fp->f_vp, buf, nbytes, *pos,
151 UIO_SYSSPACE32, 0, vfs_context_ucred(fp->f_ctxt), &resid, p);
152 if ((error) || (nbytes == resid)) {
157 *pos += nbytes - resid;
159 return (int)(nbytes - resid);
163 kern_file_write(struct cfs_kern_file *fp, void *buf, size_t nbytes, loff_t *pos)
165 struct proc *p = current_proc();
170 assert(fp != NULL && fp->f_vp != NULL);
172 error = vn_rdwr(UIO_WRITE, fp->f_vp, buf, nbytes, *pos,
173 UIO_SYSSPACE32, 0, vfs_context_ucred(fp->f_ctxt), &resid, p);
174 if ((error) || (nbytes == resid)) {
179 *pos += nbytes - resid;
181 return (int)(nbytes - resid);
186 kern_file_sync (struct cfs_kern_file *fp)
188 return VNOP_FSYNC(fp->f_vp, MNT_WAIT, fp->f_ctxt);
191 #else /* !__DARWIN8__ */
194 kern_file_size(struct file *fp, off_t *size)
196 struct vnode *vp = (struct vnode *)fp->f_data;
200 rc = vn_stat(vp, &sb, current_proc());
210 kern_file_open(const char *filename, int flags, int mode)
214 register struct vnode *vp;
216 extern struct fileops vnops;
222 MALLOC_ZONE(fp, struct file *, sizeof(file_t), M_FILE, M_WAITOK|M_ZERO);
223 bzero(fp, sizeof(*fp));
225 LIST_CIRCLE(fp, f_list);
226 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, (char *)filename, current_proc());
227 if ((rc = vn_open(&nd, flags, mode)) != 0){
228 printf("filp_open failed at (%d)\n", rc);
229 FREE_ZONE(fp, sizeof(*fp), M_FILE);
234 fp->f_flag = flags & FMASK;
235 fp->f_type = DTYPE_VNODE;
237 fp->f_data = (caddr_t)vp;
238 fp->f_cred = current_proc()->p_ucred;
240 * Hold cred to increase reference
244 * vnode is locked inside vn_open for lookup,
245 * we should release the lock before return
247 VOP_UNLOCK(vp, 0, current_proc());
254 frele_internal(struct file *fp)
256 if (fp->f_count == (short)0xffff)
257 panic("frele of lustre: stale");
258 if (--fp->f_count < 0)
259 panic("frele of lustre: count < 0");
260 return ((int)fp->f_count);
264 kern_file_close(struct file *fp)
273 if (frele_internal(fp) > 0)
275 vp = (struct vnode *)fp->f_data;
276 (void )vn_close(vp, fp->f_flag, fp->f_cred, current_proc());
279 * Dont use ffree to release fp!!!!
280 * ffree will call LIST_REMOVE(fp),
281 * but fp is not in any list, this will
286 if (cred != NOCRED) {
292 memset(fp, 0xff, sizeof *fp);
293 fp->f_count = (short)0xffff;
294 FREE_ZONE(fp, sizeof *fp, M_FILE);
300 extern void bwillwrite(void);
303 * Write buffer to filp inside kernel
306 kern_file_write(struct file *fp, void *buf, size_t nbyte, loff_t *pos)
310 struct proc *p = current_proc();
315 aiov.iov_base = (void *)(uintptr_t)buf;
316 aiov.iov_len = nbyte;
317 auio.uio_iov = &aiov;
320 auio.uio_offset = *pos;
322 * Liang: If don't set FOF_OFFSET, vn_write()
323 * will use fp->f_offset as the the real offset.
328 auio.uio_offset = (off_t)-1;
331 auio.uio_resid = nbyte;
332 auio.uio_rw = UIO_WRITE;
333 auio.uio_segflg = UIO_SYSSPACE;
338 if (fp->f_type == DTYPE_VNODE)
339 bwillwrite(); /* empty stuff now */
340 if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
341 if (auio.uio_resid != cnt && (error == ERESTART ||\
342 error == EINTR || error == EWOULDBLOCK))
344 /* The socket layer handles SIGPIPE */
345 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
352 cnt -= auio.uio_resid;
359 * Read from filp inside kernel
362 kern_file_read(struct file *fp, void *buf, size_t nbyte, loff_t *pos)
366 struct proc *p = current_proc();
371 aiov.iov_base = (caddr_t)buf;
372 aiov.iov_len = nbyte;
373 auio.uio_iov = &aiov;
376 auio.uio_offset = *pos;
379 auio.uio_offset = (off_t)-1;
382 auio.uio_resid = nbyte;
383 auio.uio_rw = UIO_READ;
384 auio.uio_segflg = UIO_SYSSPACE;
389 if ((error = fo_read(fp, &auio, fp->f_cred, flags, p)) != 0) {
390 if (auio.uio_resid != cnt && (error == ERESTART ||
391 error == EINTR || error == EWOULDBLOCK))
398 cnt -= auio.uio_resid;
406 kern_file_sync(struct file *fp)
408 struct vnode *vp = (struct vnode *)fp->f_data;
409 struct proc *p = current_proc();
414 if (fref(fp) == -1) {
418 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
419 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
420 VOP_UNLOCK(vp, 0, p);
427 #endif /* !__DARWIN8__ */
429 struct posix_acl *posix_acl_alloc(int count, int flags)
431 static struct posix_acl acl;
436 * XXX Liang: I've not converted all of them,
439 int cfs_oflags2univ(int flags)
443 f = flags & O_NOACCESS;
444 f |= (flags & O_CREAT) ? CFS_O_CREAT: 0;
445 f |= (flags & O_TRUNC) ? CFS_O_TRUNC: 0;
446 f |= (flags & O_EXCL) ? CFS_O_EXCL: 0;
447 f |= (flags & O_NONBLOCK) ? CFS_O_NONBLOCK: 0;
448 f |= (flags & O_APPEND) ? CFS_O_APPEND: 0;
449 f |= (flags & O_NOFOLLOW) ? CFS_O_NOFOLLOW: 0;
450 f |= (flags & O_SYNC)? CFS_O_SYNC: 0;
455 * XXX Liang: we don't need it in OSX.
456 * But it should be implemented anyway.
458 int cfs_univ2oflags(int flags)