+void
+vn_setunionwait(vnode_t vp)
+{
+ vnode_lock_spin(vp);
+ vp->v_flag |= VISUNION;
+ vnode_unlock(vp);
+}
+
+
+void
+vn_checkunionwait(vnode_t vp)
+{
+ vnode_lock(vp);
+ while ((vp->v_flag & VISUNION) == VISUNION)
+ msleep((caddr_t)&vp->v_flag, &vp->v_lock, 0, 0, 0);
+ vnode_unlock(vp);
+}
+
+void
+vn_clearunionwait(vnode_t vp, int locked)
+{
+ if (!locked)
+ vnode_lock(vp);
+ if((vp->v_flag & VISUNION) == VISUNION) {
+ vp->v_flag &= ~VISUNION;
+ wakeup((caddr_t)&vp->v_flag);
+ }
+ if (!locked)
+ vnode_unlock(vp);
+}
+
+/*
+ * XXX - get "don't trigger mounts" flag for thread; used by autofs.
+ */
+extern int thread_notrigger(void);
+
+int
+thread_notrigger(void)
+{
+ struct uthread *uth = (struct uthread *)get_bsdthread_info(current_thread());
+ return (uth->uu_notrigger);
+}
+
+/*
+ * Removes orphaned apple double files during a rmdir
+ * Works by:
+ * 1. vnode_suspend().
+ * 2. Call VNOP_READDIR() till the end of directory is reached.
+ * 3. Check if the directory entries returned are regular files with name starting with "._". If not, return ENOTEMPTY.
+ * 4. Continue (2) and (3) till end of directory is reached.
+ * 5. If all the entries in the directory were files with "._" name, delete all the files.
+ * 6. vnode_resume()
+ * 7. If deletion of all files succeeded, call VNOP_RMDIR() again.
+ */
+
+errno_t rmdir_remove_orphaned_appleDouble(vnode_t vp , vfs_context_t ctx, int * restart_flag)
+{
+
+#define UIO_BUFF_SIZE 2048
+ uio_t auio = NULL;
+ int eofflag, siz = UIO_BUFF_SIZE, nentries = 0;
+ int open_flag = 0, full_erase_flag = 0;
+ char uio_buf[ UIO_SIZEOF(1) ];
+ char *rbuf = NULL, *cpos, *cend;
+ struct nameidata nd_temp;
+ struct dirent *dp;
+ errno_t error;
+
+ error = vnode_suspend(vp);
+
+ /*
+ * restart_flag is set so that the calling rmdir sleeps and resets
+ */
+ if (error == EBUSY)
+ *restart_flag = 1;
+ if (error != 0)
+ goto outsc;
+
+ /*
+ * set up UIO
+ */
+ MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
+ if (rbuf)
+ auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
+ &uio_buf[0], sizeof(uio_buf));
+ if (!rbuf || !auio) {
+ error = ENOMEM;
+ goto outsc;
+ }
+
+ uio_setoffset(auio,0);
+
+ eofflag = 0;
+
+ if ((error = VNOP_OPEN(vp, FREAD, ctx)))
+ goto outsc;
+ else
+ open_flag = 1;
+
+ /*
+ * First pass checks if all files are appleDouble files.
+ */
+
+ do {
+ siz = UIO_BUFF_SIZE;
+ uio_reset(auio, uio_offset(auio), UIO_SYSSPACE, UIO_READ);
+ uio_addiov(auio, CAST_USER_ADDR_T(rbuf), UIO_BUFF_SIZE);
+
+ if((error = VNOP_READDIR(vp, auio, 0, &eofflag, &nentries, ctx)))
+ goto outsc;
+
+ if (uio_resid(auio) != 0)
+ siz -= uio_resid(auio);
+
+ /*
+ * Iterate through directory
+ */
+ cpos = rbuf;
+ cend = rbuf + siz;
+ dp = (struct dirent*) cpos;
+
+ if (cpos == cend)
+ eofflag = 1;
+
+ while ((cpos < cend)) {
+ /*
+ * Check for . and .. as well as directories
+ */
+ if (dp->d_ino != 0 &&
+ !((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
+ (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.'))) {
+ /*
+ * Check for irregular files and ._ files
+ * If there is a ._._ file abort the op
+ */
+ if ( dp->d_namlen < 2 ||
+ strncmp(dp->d_name,"._",2) ||
+ (dp->d_namlen >= 4 && !strncmp(&(dp->d_name[2]), "._",2))) {
+ error = ENOTEMPTY;
+ goto outsc;
+ }
+ }
+ cpos += dp->d_reclen;
+ dp = (struct dirent*)cpos;
+ }
+
+ /*
+ * workaround for HFS/NFS setting eofflag before end of file
+ */
+ if (vp->v_tag == VT_HFS && nentries > 2)
+ eofflag=0;
+
+ if (vp->v_tag == VT_NFS) {
+ if (eofflag && !full_erase_flag) {
+ full_erase_flag = 1;
+ eofflag = 0;
+ uio_reset(auio, 0, UIO_SYSSPACE, UIO_READ);
+ }
+ else if (!eofflag && full_erase_flag)
+ full_erase_flag = 0;
+ }
+
+ } while (!eofflag);
+ /*
+ * If we've made it here all the files in the dir are AppleDouble
+ * We can delete the files even though the node is suspended
+ * because we are the owner of the file.
+ */
+
+ uio_reset(auio, 0, UIO_SYSSPACE, UIO_READ);
+ eofflag = 0;
+ full_erase_flag = 0;
+
+ do {
+ siz = UIO_BUFF_SIZE;
+ uio_reset(auio, uio_offset(auio), UIO_SYSSPACE, UIO_READ);
+ uio_addiov(auio, CAST_USER_ADDR_T(rbuf), UIO_BUFF_SIZE);
+
+ error = VNOP_READDIR(vp, auio, 0, &eofflag, &nentries, ctx);
+
+ if (error != 0)
+ goto outsc;
+
+ if (uio_resid(auio) != 0)
+ siz -= uio_resid(auio);
+
+ /*
+ * Iterate through directory
+ */
+ cpos = rbuf;
+ cend = rbuf + siz;
+ dp = (struct dirent*) cpos;
+
+ if (cpos == cend)
+ eofflag = 1;
+
+ while ((cpos < cend)) {
+ /*
+ * Check for . and .. as well as directories
+ */
+ if (dp->d_ino != 0 &&
+ !((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
+ (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.'))
+ ) {
+ NDINIT(&nd_temp, DELETE, USEDVP, UIO_SYSSPACE, CAST_USER_ADDR_T(dp->d_name), ctx);
+ nd_temp.ni_dvp = vp;
+ error = unlink1(ctx, &nd_temp, 0);
+ if(error && error != ENOENT)
+ goto outsc;
+ }
+ cpos += dp->d_reclen;
+ dp = (struct dirent*)cpos;
+ }
+
+ /*
+ * workaround for HFS/NFS setting eofflag before end of file
+ */
+ if (vp->v_tag == VT_HFS && nentries > 2)
+ eofflag=0;
+
+ if (vp->v_tag == VT_NFS) {
+ if (eofflag && !full_erase_flag) {
+ full_erase_flag = 1;
+ eofflag = 0;
+ uio_reset(auio, 0, UIO_SYSSPACE, UIO_READ);
+ }
+ else if (!eofflag && full_erase_flag)
+ full_erase_flag = 0;
+ }
+
+ } while (!eofflag);
+
+
+ error = 0;
+
+outsc:
+ if (open_flag)
+ VNOP_CLOSE(vp, FREAD, ctx);
+
+ uio_free(auio);
+ FREE(rbuf, M_TEMP);
+
+ vnode_resume(vp);
+
+
+ return(error);
+
+}
+