1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/isa_defs.h>
37 #include <sys/types.h>
38 #include <sys/sysmacros.h>
39 #include <sys/cred.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/pathname.h>
43 #include <sys/vnode.h>
44 #include <sys/vfs.h>
45 #include <sys/file.h>
46 #include <sys/uio.h>
47 #include <sys/debug.h>
48 #include <fs/fs_subr.h>
49 
50 /*
51  * Common code for pathconf(), fpathconf() system calls
52  */
53 static long
cpathconf(register vnode_t * vp,int cmd,struct cred * cr)54 cpathconf(register vnode_t *vp, int cmd, struct cred *cr)
55 {
56 	struct statvfs64 sb;
57 	int error;
58 	ulong_t val;
59 
60 	switch (cmd) {
61 	case _PC_2_SYMLINKS:
62 		if (error = VOP_PATHCONF(vp, _PC_SYMLINK_MAX, &val, cr, NULL))
63 			return ((long)set_errno(error));
64 		return ((long)(val > 0));
65 
66 	case _PC_ALLOC_SIZE_MIN:
67 	case _PC_REC_INCR_XFER_SIZE:
68 	case _PC_REC_MAX_XFER_SIZE:
69 	case _PC_REC_MIN_XFER_SIZE:
70 	case _PC_REC_XFER_ALIGN:
71 		if ((error = VFS_STATVFS(vp->v_vfsp, &sb)) != 0)
72 			return ((long)set_errno(error));
73 
74 		/*
75 		 * There is generally no harm in doing larger transfers, but
76 		 * there's a point of diminishing returns.  With 1MB transfers,
77 		 * even if they're random, you get very close to platter speed.
78 		 * Se we return 1MB as the maximum transfer size.
79 		 */
80 		if (cmd == _PC_REC_MAX_XFER_SIZE)
81 			return ((long)MAX(sb.f_bsize, 1UL << 20));
82 
83 		/*
84 		 * By definition, f_frsize is the smallest filesystem block.
85 		 * However, _PC_ALLOC_SIZE_MIN is intended to define the
86 		 * threshold for direct I/O.  This implies two requirements:
87 		 * the VM and I/O subsystems must be able to create mappings
88 		 * for DMA, which requires at least page alignment; and the
89 		 * filesystem must avoid read/modify/write, which generally
90 		 * requires multiples of its 'preferred' blocksize, f_bsize.
91 		 *
92 		 * PAGESIZE alignment is sufficient for DMA and block copy.
93 		 * Rounding up to the filesystem 'preferred' blocksize
94 		 * works just as well.
95 		 *
96 		 * All together, this means that the remaining parameters
97 		 * map into the same value.
98 		 */
99 		return ((long)MAX(sb.f_bsize, PAGESIZE));
100 
101 	case _PC_ASYNC_IO:
102 		return (1l);
103 
104 	case _PC_PRIO_IO:
105 		return ((long)set_errno(EINVAL));
106 
107 	case _PC_SYNC_IO:
108 		if (!(error = VOP_FSYNC(vp, FSYNC, cr, NULL)))
109 			return (1l);
110 		return ((long)set_errno(error));
111 
112 	case _PC_XATTR_ENABLED:
113 		return ((vp->v_vfsp->vfs_flag & VFS_XATTR) ? 1 : 0);
114 
115 	default:
116 		if (error = VOP_PATHCONF(vp, cmd, &val, cr, NULL))
117 			return ((long)set_errno(error));
118 		return (val);
119 	}
120 	/* NOTREACHED */
121 }
122 
123 /* fpathconf/pathconf interfaces */
124 
125 long
fpathconf(int fdes,int name)126 fpathconf(int fdes, int name)
127 {
128 	file_t *fp;
129 	long retval;
130 
131 	if ((fp = getf(fdes)) == NULL)
132 		return (set_errno(EBADF));
133 	retval = cpathconf(fp->f_vnode, name, fp->f_cred);
134 	releasef(fdes);
135 	return (retval);
136 }
137 
138 long
pathconf(char * fname,int name)139 pathconf(char *fname, int name)
140 {
141 	vnode_t *vp;
142 	long	retval;
143 	int	error;
144 	int 	estale_retry = 0;
145 
146 lookup:
147 	if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
148 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
149 			goto lookup;
150 		return ((long)set_errno(error));
151 	}
152 
153 	retval = cpathconf(vp, name, CRED());
154 	VN_RELE(vp);
155 	return (retval);
156 }
157