FreeBSD-10.3-autofs.patch 9.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
#
# Backported patch from FreeBSD 11 to make autofs handle long directory
# entry names. Useful if you use autofs rather than amd to mount user/proj
# directories on bss.
#
#   cd /usr/src
#   sudo patch -p0 < patchfile
#

diff -Nur sys.orig/fs/autofs/autofs.h sys/fs/autofs/autofs.h
--- sys.orig/fs/autofs/autofs.h	2017-03-28 17:07:41.499848000 -0600
+++ sys/fs/autofs/autofs.h	2016-05-27 10:02:16.000000000 -0600
@@ -120,13 +120,6 @@
 	int				sc_last_request_id;
 };
 
-/*
- * Limits and constants
- */
-#define AUTOFS_NAMELEN		24
-#define AUTOFS_FSNAMELEN	16	/* equal to MFSNAMELEN */
-#define AUTOFS_DELEN		(8 + AUTOFS_NAMELEN)
-
 int	autofs_init(struct vfsconf *vfsp);
 int	autofs_uninit(struct vfsconf *vfsp);
 int	autofs_trigger(struct autofs_node *anp, const char *component,
diff -Nur sys.orig/fs/autofs/autofs_vfsops.c sys/fs/autofs/autofs_vfsops.c
--- sys.orig/fs/autofs/autofs_vfsops.c	2017-03-28 17:07:41.500245000 -0600
+++ sys/fs/autofs/autofs_vfsops.c	2016-05-27 10:47:24.000000000 -0600
@@ -39,6 +39,7 @@
 #include <sys/kernel.h>
 #include <sys/module.h>
 #include <sys/mount.h>
+#include <sys/stat.h>
 #include <sys/sx.h>
 #include <sys/taskqueue.h>
 #include <sys/vnode.h>
@@ -192,7 +193,7 @@
 autofs_statfs(struct mount *mp, struct statfs *sbp)
 {
 
-	sbp->f_bsize = 512;
+	sbp->f_bsize = S_BLKSIZE;
 	sbp->f_iosize = 0;
 	sbp->f_blocks = 0;
 	sbp->f_bfree = 0;
diff -Nur sys.orig/fs/autofs/autofs_vnops.c sys/fs/autofs/autofs_vnops.c
--- sys.orig/fs/autofs/autofs_vnops.c	2017-03-28 17:07:41.501063000 -0600
+++ sys/fs/autofs/autofs_vnops.c	2016-05-27 10:29:50.000000000 -0600
@@ -41,6 +41,7 @@
 #include <sys/mutex.h>
 #include <sys/namei.h>
 #include <sys/signalvar.h>
+#include <sys/stat.h>
 #include <sys/systm.h>
 #include <sys/taskqueue.h>
 #include <sys/vnode.h>
@@ -110,8 +111,8 @@
 	vap->va_rdev = NODEV;
 	vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
 	vap->va_fileid = anp->an_fileno;
-	vap->va_size = 512; /* XXX */
-	vap->va_blocksize = 512;
+	vap->va_size = S_BLKSIZE;
+	vap->va_blocksize = S_BLKSIZE;
 	vap->va_mtime = anp->an_ctime;
 	vap->va_atime = anp->an_ctime;
 	vap->va_ctime = anp->an_ctime;
@@ -119,7 +120,7 @@
 	vap->va_gen = 0;
 	vap->va_flags = 0;
 	vap->va_rdev = 0;
-	vap->va_bytes = 512; /* XXX */
+	vap->va_bytes = S_BLKSIZE;
 	vap->va_filerev = 0;
 	vap->va_spare = 0;
 
@@ -214,7 +215,7 @@
 	struct autofs_mount *amp;
 	struct autofs_node *anp, *child;
 	struct componentname *cnp;
-	int error, lock_flags;
+	int error;
 
 	dvp = ap->a_dvp;
 	vpp = ap->a_vpp;
@@ -257,23 +258,13 @@
 			return (error);
 
 		if (newvp != NULL) {
-			error = VOP_LOOKUP(newvp, ap->a_vpp, ap->a_cnp);
-
 			/*
-			 * Instead of figuring out whether our vnode should
-			 * be locked or not given the error and cnp flags,
-			 * just "copy" the lock status from vnode returned
-			 * by mounted filesystem's VOP_LOOKUP().  Get rid
-			 * of that new vnode afterwards.
+			 * The target filesystem got automounted.
+			 * Let the lookup(9) go around with the same
+			 * path component.
 			 */
-			lock_flags = VOP_ISLOCKED(newvp);
-			if (lock_flags == 0) {
-				VOP_UNLOCK(dvp, 0);
-				vrele(newvp);
-			} else {
-				vput(newvp);
-			}
-			return (error);
+			vput(newvp);
+			return (ERELOOKUP);
 		}
 	}
 
@@ -339,26 +330,52 @@
 	return (error);
 }
 
+/*
+ * Write out a single 'struct dirent', based on 'name' and 'fileno' arguments.
+ */
 static int
-autofs_readdir_one(struct uio *uio, const char *name, int fileno)
+autofs_readdir_one(struct uio *uio, const char *name, int fileno,
+    size_t *reclenp)
 {
 	struct dirent dirent;
-	int error, i;
+	size_t namlen, padded_namlen, reclen;
+	int error;
+
+	namlen = strlen(name);
+	padded_namlen = roundup2(namlen + 1, __alignof(struct dirent));
+	KASSERT(padded_namlen <= MAXNAMLEN, ("%zd > MAXNAMLEN", padded_namlen));
+	reclen = offsetof(struct dirent, d_name) + padded_namlen;
+
+	if (reclenp != NULL)
+		*reclenp = reclen;
+
+	if (uio == NULL)
+		return (0);
+
+	if (uio->uio_resid < reclen)
+		return (EINVAL);
 
-	memset(&dirent, 0, sizeof(dirent));
-	dirent.d_type = DT_DIR;
-	dirent.d_reclen = AUTOFS_DELEN;
 	dirent.d_fileno = fileno;
-	/* PFS_DELEN was picked to fit PFS_NAMLEN */
-	for (i = 0; i < AUTOFS_NAMELEN - 1 && name[i] != '\0'; ++i)
-		dirent.d_name[i] = name[i];
-	dirent.d_name[i] = 0;
-	dirent.d_namlen = i;
+	dirent.d_reclen = reclen;
+	dirent.d_type = DT_DIR;
+	dirent.d_namlen = namlen;
+	memcpy(dirent.d_name, name, namlen);
+	memset(dirent.d_name + namlen, 0, padded_namlen - namlen);
+	error = uiomove(&dirent, reclen, uio);
 
-	error = uiomove(&dirent, AUTOFS_DELEN, uio);
 	return (error);
 }
 
+static size_t
+autofs_dirent_reclen(const char *name)
+{
+	size_t reclen;
+
+	autofs_readdir_one(NULL, name, -1, &reclen);
+
+	return (reclen);
+}
+
 static int
 autofs_readdir(struct vop_readdir_args *ap)
 {
@@ -366,13 +383,15 @@
 	struct autofs_mount *amp;
 	struct autofs_node *anp, *child;
 	struct uio *uio;
-	off_t offset;
-	int error, i, resid;
+	size_t reclen, reclens;
+	ssize_t initial_resid;
+	int error;
 
 	vp = ap->a_vp;
 	amp = VFSTOAUTOFS(vp->v_mount);
 	anp = vp->v_data;
 	uio = ap->a_uio;
+	initial_resid = ap->a_uio->uio_resid;
 
 	KASSERT(vp->v_type == VDIR, ("!VDIR"));
 
@@ -390,70 +409,94 @@
 		}
 	}
 
-	/* only allow reading entire entries */
-	offset = uio->uio_offset;
-	resid = uio->uio_resid;
-	if (offset < 0 || offset % AUTOFS_DELEN != 0 ||
-	    (resid && resid < AUTOFS_DELEN))
+	if (uio->uio_offset < 0)
 		return (EINVAL);
-	if (resid == 0)
-		return (0);
 
 	if (ap->a_eofflag != NULL)
-		*ap->a_eofflag = TRUE;
+		*ap->a_eofflag = FALSE;
 
-	if (offset == 0 && resid >= AUTOFS_DELEN) {
-		error = autofs_readdir_one(uio, ".", anp->an_fileno);
+	/*
+	 * Write out the directory entry for ".".  This is conditional
+	 * on the current offset into the directory; same applies to the
+	 * other two cases below.
+	 */
+	if (uio->uio_offset == 0) {
+		error = autofs_readdir_one(uio, ".", anp->an_fileno, &reclen);
 		if (error != 0)
-			return (error);
-		offset += AUTOFS_DELEN;
-		resid -= AUTOFS_DELEN;
+			goto out;
 	}
+	reclens = autofs_dirent_reclen(".");
 
-	if (offset == AUTOFS_DELEN && resid >= AUTOFS_DELEN) {
+	/*
+	 * Write out the directory entry for "..".
+	 */
+	if (uio->uio_offset <= reclens) {
+		if (uio->uio_offset != reclens)
+			return (EINVAL);
 		if (anp->an_parent == NULL) {
-			/*
-			 * XXX: Right?
-			 */
-			error = autofs_readdir_one(uio, "..", anp->an_fileno);
+			error = autofs_readdir_one(uio, "..",
+			    anp->an_fileno, &reclen);
 		} else {
 			error = autofs_readdir_one(uio, "..",
-			    anp->an_parent->an_fileno);
+			    anp->an_parent->an_fileno, &reclen);
 		}
 		if (error != 0)
-			return (error);
-		offset += AUTOFS_DELEN;
-		resid -= AUTOFS_DELEN;
+			goto out;
 	}
 
-	i = 2; /* Account for "." and "..". */
+	reclens += autofs_dirent_reclen("..");
+
+	/*
+	 * Write out the directory entries for subdirectories.
+	 */
 	AUTOFS_SLOCK(amp);
 	TAILQ_FOREACH(child, &anp->an_children, an_next) {
-		if (resid < AUTOFS_DELEN) {
-			if (ap->a_eofflag != NULL)
-				*ap->a_eofflag = 0;
-			break;
+		/*
+		 * Check the offset to skip entries returned by previous
+		 * calls to getdents().
+		 */
+		if (uio->uio_offset > reclens) {
+			reclens += autofs_dirent_reclen(child->an_name);
+			continue;
 		}
 
 		/*
-		 * Skip entries returned by previous call to getdents().
+		 * Prevent seeking into the middle of dirent.
 		 */
-		i++;
-		if (i * AUTOFS_DELEN <= offset)
-			continue;
+		if (uio->uio_offset != reclens) {
+			AUTOFS_SUNLOCK(amp);
+			return (EINVAL);
+		}
 
 		error = autofs_readdir_one(uio, child->an_name,
-		    child->an_fileno);
+		    child->an_fileno, &reclen);
+		reclens += reclen;
 		if (error != 0) {
 			AUTOFS_SUNLOCK(amp);
-			return (error);
+			goto out;
 		}
-		offset += AUTOFS_DELEN;
-		resid -= AUTOFS_DELEN;
 	}
-
 	AUTOFS_SUNLOCK(amp);
+
+	if (ap->a_eofflag != NULL)
+		*ap->a_eofflag = TRUE;
+
 	return (0);
+
+out:
+	/*
+	 * Return error if the initial buffer was too small to do anything.
+	 */
+	if (uio->uio_resid == initial_resid)
+		return (error);
+
+	/*
+	 * Don't return an error if we managed to copy out some entries.
+	 */
+	if (uio->uio_resid < reclen)
+		return (0);
+
+	return (error);
 }
 
 static int
diff -Nur sys.orig/kern/vfs_lookup.c sys/kern/vfs_lookup.c
--- sys.orig/kern/vfs_lookup.c	2017-03-28 17:06:55.070726000 -0600
+++ sys/kern/vfs_lookup.c	2016-05-27 10:32:01.000000000 -0600
@@ -475,6 +475,7 @@
 	int rdonly;			/* lookup read-only flag bit */
 	int error = 0;
 	int dpunlocked = 0;		/* dp has already been unlocked */
+	int relookup = 0;		/* do not consume the path component */
 	struct componentname *cnp = &ndp->ni_cnd;
 	int lkflags_save;
 	int ni_dvp_unlocked;
@@ -725,6 +726,14 @@
 			goto unionlookup;
 		}
 
+		if (error == ERELOOKUP) {
+			vref(dp);
+			ndp->ni_vp = dp;
+			error = 0;
+			relookup = 1;
+			goto good;
+		}
+
 		if (error != EJUSTRETURN)
 			goto bad;
 		/*
@@ -757,6 +766,7 @@
 		goto success;
 	} else
 		cnp->cn_lkflags = lkflags_save;
+good:
 #ifdef NAMEI_DIAGNOSTIC
 	printf("found\n");
 #endif
@@ -836,6 +846,14 @@
 	 */
 	KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
 	    ("lookup: invalid path state."));
+	if (relookup) {
+		relookup = 0;
+		if (ndp->ni_dvp != dp)
+			vput(ndp->ni_dvp);
+		else
+			vrele(ndp->ni_dvp);
+		goto dirloop;
+	}
 	if (*ndp->ni_next == '/') {
 		cnp->cn_nameptr = ndp->ni_next;
 		while (*cnp->cn_nameptr == '/') {
diff -Nur sys.orig/sys/errno.h sys/sys/errno.h
--- sys.orig/sys/errno.h	2017-03-28 17:07:30.588321000 -0600
+++ sys/sys/errno.h	2016-05-27 10:34:04.000000000 -0600
@@ -190,6 +190,7 @@
 #define	EJUSTRETURN	(-2)		/* don't modify regs, just return */
 #define	ENOIOCTL	(-3)		/* ioctl not handled by this layer */
 #define	EDIRIOCTL	(-4)		/* do direct ioctl in GEOM */
+#define	ERELOOKUP	(-5)		/* retry the directory lookup */
 #endif
 
 #endif