Annotation of src/sys/compat/linux/common/linux_file64.c, Revision 1.28.2.9
1.28.2.9! yamt 1: /* $NetBSD: linux_file64.c,v 1.28.2.8 2008/02/04 09:23:05 yamt Exp $ */
1.1 jdolecek 2:
3: /*-
1.28.2.9! yamt 4: * Copyright (c) 1995, 1998, 2000, 2008 The NetBSD Foundation, Inc.
1.1 jdolecek 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Frank van der Linden and Eric Haszlakiewicz.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the NetBSD
21: * Foundation, Inc. and its contributors.
22: * 4. Neither the name of The NetBSD Foundation nor the names of its
23: * contributors may be used to endorse or promote products derived
24: * from this software without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: * POSSIBILITY OF SUCH DAMAGE.
37: */
38:
39: /*
40: * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
41: */
1.6 lukem 42:
43: #include <sys/cdefs.h>
1.28.2.9! yamt 44: __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.28.2.8 2008/02/04 09:23:05 yamt Exp $");
1.1 jdolecek 45:
46: #include <sys/param.h>
47: #include <sys/systm.h>
48: #include <sys/namei.h>
49: #include <sys/proc.h>
1.11 tron 50: #include <sys/dirent.h>
1.1 jdolecek 51: #include <sys/file.h>
52: #include <sys/stat.h>
53: #include <sys/filedesc.h>
54: #include <sys/ioctl.h>
55: #include <sys/kernel.h>
56: #include <sys/mount.h>
57: #include <sys/malloc.h>
1.28.2.4 yamt 58: #include <sys/namei.h>
59: #include <sys/vfs_syscalls.h>
1.1 jdolecek 60: #include <sys/vnode.h>
61: #include <sys/tty.h>
62: #include <sys/conf.h>
63:
64: #include <sys/syscallargs.h>
65:
66: #include <compat/linux/common/linux_types.h>
67: #include <compat/linux/common/linux_signal.h>
68: #include <compat/linux/common/linux_fcntl.h>
69: #include <compat/linux/common/linux_util.h>
70: #include <compat/linux/common/linux_machdep.h>
1.11 tron 71: #include <compat/linux/common/linux_dirent.h>
1.28.2.5 yamt 72: #include <compat/linux/common/linux_ipc.h>
73: #include <compat/linux/common/linux_sem.h>
1.1 jdolecek 74:
75: #include <compat/linux/linux_syscallargs.h>
76:
1.15 matt 77: #ifndef alpha
78:
1.28.2.6 yamt 79: static void bsd_to_linux_stat(struct stat *, struct linux_stat64 *);
1.1 jdolecek 80:
81: /*
82: * Convert a NetBSD stat structure to a Linux stat structure.
83: * Only the order of the fields and the padding in the structure
84: * is different. linux_fakedev is a machine-dependent function
85: * which optionally converts device driver major/minor numbers
86: * (XXX horrible, but what can you do against code that compares
87: * things against constant major device numbers? sigh)
88: */
89: static void
1.28.2.7 yamt 90: bsd_to_linux_stat(struct stat *bsp, struct linux_stat64 *lsp)
1.1 jdolecek 91: {
1.7 christos 92: lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
1.1 jdolecek 93: lsp->lst_ino = bsp->st_ino;
94: lsp->lst_mode = (linux_mode_t)bsp->st_mode;
95: if (bsp->st_nlink >= (1 << 15))
96: lsp->lst_nlink = (1 << 15) - 1;
97: else
98: lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
99: lsp->lst_uid = bsp->st_uid;
100: lsp->lst_gid = bsp->st_gid;
1.7 christos 101: lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
1.1 jdolecek 102: lsp->lst_size = bsp->st_size;
103: lsp->lst_blksize = bsp->st_blksize;
104: lsp->lst_blocks = bsp->st_blocks;
105: lsp->lst_atime = bsp->st_atime;
106: lsp->lst_mtime = bsp->st_mtime;
107: lsp->lst_ctime = bsp->st_ctime;
1.28.2.1 yamt 108: # ifdef LINUX_STAT64_HAS_NSEC
1.25 christos 109: lsp->lst_atime_nsec = bsp->st_atimensec;
110: lsp->lst_mtime_nsec = bsp->st_mtimensec;
111: lsp->lst_ctime_nsec = bsp->st_ctimensec;
1.28.2.1 yamt 112: # endif
113: # if LINUX_STAT64_HAS_BROKEN_ST_INO
1.16 jdolecek 114: lsp->__lst_ino = (linux_ino_t) bsp->st_ino;
1.28.2.1 yamt 115: # endif
1.1 jdolecek 116: }
117:
118: /*
119: * The stat functions below are plain sailing. stat and lstat are handled
120: * by one function to avoid code duplication.
121: */
122: int
1.28.2.7 yamt 123: linux_sys_fstat64(struct lwp *l, const struct linux_sys_fstat64_args *uap, register_t *retval)
1.1 jdolecek 124: {
1.28.2.7 yamt 125: /* {
1.1 jdolecek 126: syscallarg(int) fd;
1.5 manu 127: syscallarg(struct linux_stat64 *) sp;
1.28.2.7 yamt 128: } */
1.1 jdolecek 129: struct linux_stat64 tmplst;
1.28.2.4 yamt 130: struct stat tmpst;
1.1 jdolecek 131: int error;
132:
1.28.2.9! yamt 133: error = do_sys_fstat(SCARG(uap, fd), &tmpst);
1.28.2.4 yamt 134: if (error != 0)
1.1 jdolecek 135: return error;
136:
137: bsd_to_linux_stat(&tmpst, &tmplst);
138:
1.28.2.4 yamt 139: return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
1.1 jdolecek 140: }
141:
142: static int
1.28.2.7 yamt 143: linux_do_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval, int flags)
1.1 jdolecek 144: {
145: struct linux_stat64 tmplst;
1.28.2.4 yamt 146: struct stat tmpst;
1.1 jdolecek 147: int error;
148:
1.28.2.9! yamt 149: error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
1.28.2.4 yamt 150: if (error != 0)
1.1 jdolecek 151: return error;
152:
153: bsd_to_linux_stat(&tmpst, &tmplst);
154:
1.28.2.4 yamt 155: return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
1.1 jdolecek 156: }
157:
158: int
1.28.2.7 yamt 159: linux_sys_stat64(struct lwp *l, const struct linux_sys_stat64_args *uap, register_t *retval)
1.1 jdolecek 160: {
1.28.2.7 yamt 161: /* {
1.1 jdolecek 162: syscallarg(const char *) path;
163: syscallarg(struct linux_stat64 *) sp;
1.28.2.7 yamt 164: } */
1.1 jdolecek 165:
1.28.2.4 yamt 166: return linux_do_stat64(l, uap, retval, FOLLOW);
1.1 jdolecek 167: }
168:
169: int
1.28.2.7 yamt 170: linux_sys_lstat64(struct lwp *l, const struct linux_sys_lstat64_args *uap, register_t *retval)
1.1 jdolecek 171: {
1.28.2.7 yamt 172: /* {
1.1 jdolecek 173: syscallarg(const char *) path;
174: syscallarg(struct linux_stat64 *) sp;
1.28.2.7 yamt 175: } */
1.1 jdolecek 176:
1.28.2.7 yamt 177: return linux_do_stat64(l, (const void *)uap, retval, NOFOLLOW);
1.2 jdolecek 178: }
179:
180: int
1.28.2.7 yamt 181: linux_sys_truncate64(struct lwp *l, const struct linux_sys_truncate64_args *uap, register_t *retval)
1.2 jdolecek 182: {
1.28.2.7 yamt 183: /* {
1.2 jdolecek 184: syscallarg(const char *) path;
185: syscallarg(off_t) length;
1.28.2.7 yamt 186: } */
1.22 jdolecek 187: struct sys_truncate_args ta;
1.2 jdolecek 188:
1.22 jdolecek 189: /* Linux doesn't have the 'pad' pseudo-parameter */
190: SCARG(&ta, path) = SCARG(uap, path);
191: SCARG(&ta, pad) = 0;
192: SCARG(&ta, length) = SCARG(uap, length);
193:
194: return sys_truncate(l, &ta, retval);
195: }
196:
197: int
1.28.2.7 yamt 198: linux_sys_ftruncate64(struct lwp *l, const struct linux_sys_ftruncate64_args *uap, register_t *retval)
1.22 jdolecek 199: {
1.28.2.7 yamt 200: /* {
1.22 jdolecek 201: syscallarg(unsigned int) fd;
202: syscallarg(off_t) length;
1.28.2.7 yamt 203: } */
1.22 jdolecek 204: struct sys_ftruncate_args ta;
205:
206: /* Linux doesn't have the 'pad' pseudo-parameter */
207: SCARG(&ta, fd) = SCARG(uap, fd);
208: SCARG(&ta, pad) = 0;
209: SCARG(&ta, length) = SCARG(uap, length);
210:
211: return sys_ftruncate(l, &ta, retval);
1.1 jdolecek 212: }
1.15 matt 213: #endif /* !alpha */
1.11 tron 214:
215: /*
216: * Linux 'readdir' call. This code is mostly taken from the
217: * SunOS getdents call (see compat/sunos/sunos_misc.c), though
1.18 jdolecek 218: * an attempt has been made to keep it a little cleaner.
1.11 tron 219: *
1.18 jdolecek 220: * The d_off field contains the offset of the next valid entry,
221: * unless the older Linux getdents(2), which used to have it set
222: * to the offset of the entry itself. This function also doesn't
223: * need to deal with the old count == 1 glibc problem.
1.11 tron 224: *
225: * Read in BSD-style entries, convert them, and copy them out.
226: *
227: * Note that this doesn't handle union-mounted filesystems.
228: */
229: int
1.28.2.7 yamt 230: linux_sys_getdents64(struct lwp *l, const struct linux_sys_getdents64_args *uap, register_t *retval)
1.11 tron 231: {
1.28.2.7 yamt 232: /* {
1.11 tron 233: syscallarg(int) fd;
234: syscallarg(struct linux_dirent64 *) dent;
235: syscallarg(unsigned int) count;
1.28.2.7 yamt 236: } */
1.11 tron 237: struct dirent *bdp;
238: struct vnode *vp;
1.28.2.4 yamt 239: char *inp, *tbuf; /* BSD-format */
1.11 tron 240: int len, reclen; /* BSD-format */
1.28.2.4 yamt 241: char *outp; /* Linux-format */
1.11 tron 242: int resid, linux_reclen = 0; /* Linux-format */
1.28.2.9! yamt 243: file_t *fp;
1.11 tron 244: struct uio auio;
245: struct iovec aiov;
246: struct linux_dirent64 idb;
247: off_t off; /* true file offset */
1.18 jdolecek 248: int buflen, error, eofflag, nbytes;
1.11 tron 249: struct vattr va;
250: off_t *cookiebuf = NULL, *cookie;
251: int ncookies;
252:
253: /* getvnode() will use the descriptor for us */
1.28.2.9! yamt 254: if ((error = getvnode(SCARG(uap, fd), &fp)) != 0)
1.11 tron 255: return (error);
256:
257: if ((fp->f_flag & FREAD) == 0) {
258: error = EBADF;
259: goto out1;
260: }
261:
262: vp = (struct vnode *)fp->f_data;
263: if (vp->v_type != VDIR) {
264: error = EINVAL;
265: goto out1;
266: }
267:
1.28.2.6 yamt 268: if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
1.11 tron 269: goto out1;
270:
271: nbytes = SCARG(uap, count);
1.18 jdolecek 272: buflen = min(MAXBSIZE, nbytes);
273: if (buflen < va.va_blocksize)
274: buflen = va.va_blocksize;
1.28 christos 275: tbuf = malloc(buflen, M_TEMP, M_WAITOK);
1.11 tron 276:
277: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
278: off = fp->f_offset;
279: again:
1.28 christos 280: aiov.iov_base = tbuf;
1.11 tron 281: aiov.iov_len = buflen;
282: auio.uio_iov = &aiov;
283: auio.uio_iovcnt = 1;
284: auio.uio_rw = UIO_READ;
285: auio.uio_resid = buflen;
286: auio.uio_offset = off;
1.28.2.1 yamt 287: UIO_SETUP_SYSSPACE(&auio);
1.11 tron 288: /*
289: * First we read into the malloc'ed buffer, then
290: * we massage it into user space, one record at a time.
291: */
292: error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
293: &ncookies);
294: if (error)
295: goto out;
296:
1.28 christos 297: inp = tbuf;
1.28.2.4 yamt 298: outp = (void *)SCARG(uap, dent);
1.11 tron 299: resid = nbytes;
300: if ((len = buflen - auio.uio_resid) == 0)
301: goto eof;
302:
303: for (cookie = cookiebuf; len > 0; len -= reclen) {
304: bdp = (struct dirent *)inp;
305: reclen = bdp->d_reclen;
306: if (reclen & 3)
307: panic("linux_readdir");
308: if (bdp->d_fileno == 0) {
309: inp += reclen; /* it is a hole; squish it out */
1.26 christos 310: if (cookie)
311: off = *cookie++;
312: else
313: off += reclen;
1.11 tron 314: continue;
315: }
316: linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
317: if (reclen > len || resid < linux_reclen) {
318: /* entry too big for buffer, so just stop */
319: outp++;
320: break;
321: }
1.26 christos 322: if (cookie)
323: off = *cookie++; /* each entry points to next */
324: else
325: off += reclen;
1.11 tron 326: /*
327: * Massage in place to make a Linux-shaped dirent (otherwise
328: * we have to worry about touching user memory outside of
329: * the copyout() call).
330: */
331: idb.d_ino = bdp->d_fileno;
332: idb.d_type = bdp->d_type;
1.18 jdolecek 333: idb.d_off = off;
334: idb.d_reclen = (u_short)linux_reclen;
1.11 tron 335: strcpy(idb.d_name, bdp->d_name);
1.28.2.4 yamt 336: if ((error = copyout((void *)&idb, outp, linux_reclen)))
1.11 tron 337: goto out;
338: /* advance past this real entry */
339: inp += reclen;
340: /* advance output past Linux-shaped entry */
341: outp += linux_reclen;
342: resid -= linux_reclen;
343: }
344:
345: /* if we squished out the whole block, try again */
1.28.2.4 yamt 346: if (outp == (void *)SCARG(uap, dent))
1.11 tron 347: goto again;
348: fp->f_offset = off; /* update the vnode offset */
349:
350: eof:
351: *retval = nbytes - resid;
352: out:
353: VOP_UNLOCK(vp, 0);
354: if (cookiebuf)
355: free(cookiebuf, M_TEMP);
1.28 christos 356: free(tbuf, M_TEMP);
1.11 tron 357: out1:
1.28.2.9! yamt 358: fd_putfile(SCARG(uap, fd));
1.3 manu 359: return error;
360: }
CVSweb <webmaster@jp.NetBSD.org>