1df8bae1dSRodney W. Grimes /*- 2996c772fSJohn Dyson * Copyright (c) 1982, 1986, 1989, 1994, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley 6df8bae1dSRodney W. Grimes * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 7df8bae1dSRodney W. Grimes * Support code is derived from software contributed to Berkeley 8df8bae1dSRodney W. Grimes * by Atsushi Murai (amurai@spec.co.jp). 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)cd9660_node.c 8.2 (Berkeley) 1/23/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 378c9bbf48SDavid E. O'Brien #include <sys/cdefs.h> 388c9bbf48SDavid E. O'Brien __FBSDID("$FreeBSD$"); 398c9bbf48SDavid E. O'Brien 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42df8bae1dSRodney W. Grimes #include <sys/mount.h> 439626b608SPoul-Henning Kamp #include <sys/bio.h> 44df8bae1dSRodney W. Grimes #include <sys/buf.h> 45df8bae1dSRodney W. Grimes #include <sys/vnode.h> 46df8bae1dSRodney W. Grimes #include <sys/malloc.h> 47df8bae1dSRodney W. Grimes #include <sys/stat.h> 481b367556SJason Evans #include <sys/mutex.h> 49a18b1f1dSJason Evans 50df8bae1dSRodney W. Grimes #include <isofs/cd9660/iso.h> 51df8bae1dSRodney W. Grimes #include <isofs/cd9660/cd9660_node.h> 52996c772fSJohn Dyson #include <isofs/cd9660/cd9660_mount.h> 53df8bae1dSRodney W. Grimes 5489c9a483SAlfred Perlstein static unsigned cd9660_chars2ui(unsigned char *begin, int len); 5510dd32cdSBruce Evans 56df8bae1dSRodney W. Grimes /* 57df8bae1dSRodney W. Grimes * Last reference to an inode, write the inode out and if necessary, 58df8bae1dSRodney W. Grimes * truncate and deallocate the file. 59df8bae1dSRodney W. Grimes */ 60df8bae1dSRodney W. Grimes int 61df8bae1dSRodney W. Grimes cd9660_inactive(ap) 62df8bae1dSRodney W. Grimes struct vop_inactive_args /* { 63df8bae1dSRodney W. Grimes struct vnode *a_vp; 64b40ce416SJulian Elischer struct thread *a_td; 65df8bae1dSRodney W. Grimes } */ *ap; 66df8bae1dSRodney W. Grimes { 67df8bae1dSRodney W. Grimes struct vnode *vp = ap->a_vp; 68b40ce416SJulian Elischer struct thread *td = ap->a_td; 69bffd1b7aSPoul-Henning Kamp struct iso_node *ip = VTOI(vp); 701295d82eSGary Palmer int error = 0; 71df8bae1dSRodney W. Grimes 7237ab0e0dSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 73df8bae1dSRodney W. Grimes vprint("cd9660_inactive: pushing active", vp); 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes ip->i_flag = 0; 76df8bae1dSRodney W. Grimes /* 77df8bae1dSRodney W. Grimes * If we are done with the inode, reclaim it 78df8bae1dSRodney W. Grimes * so that it can be reused immediately. 79df8bae1dSRodney W. Grimes */ 80996c772fSJohn Dyson if (ip->inode.iso_mode == 0) 81d4eb29baSPoul-Henning Kamp vrecycle(vp, td); 82df8bae1dSRodney W. Grimes return error; 83df8bae1dSRodney W. Grimes } 84df8bae1dSRodney W. Grimes 85df8bae1dSRodney W. Grimes /* 86df8bae1dSRodney W. Grimes * Reclaim an inode so that it can be used for other purposes. 87df8bae1dSRodney W. Grimes */ 88df8bae1dSRodney W. Grimes int 89df8bae1dSRodney W. Grimes cd9660_reclaim(ap) 90df8bae1dSRodney W. Grimes struct vop_reclaim_args /* { 91df8bae1dSRodney W. Grimes struct vnode *a_vp; 92b40ce416SJulian Elischer struct thread *a_td; 93df8bae1dSRodney W. Grimes } */ *ap; 94df8bae1dSRodney W. Grimes { 95bffd1b7aSPoul-Henning Kamp struct vnode *vp = ap->a_vp; 96bffd1b7aSPoul-Henning Kamp struct iso_node *ip = VTOI(vp); 97df8bae1dSRodney W. Grimes 9837ab0e0dSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 99df8bae1dSRodney W. Grimes vprint("cd9660_reclaim: pushing active", vp); 100df8bae1dSRodney W. Grimes /* 101df8bae1dSRodney W. Grimes * Remove the inode from its hash chain. 102df8bae1dSRodney W. Grimes */ 103dfb9f846SPoul-Henning Kamp vfs_hash_remove(vp); 104dfb9f846SPoul-Henning Kamp 105df8bae1dSRodney W. Grimes /* 106df8bae1dSRodney W. Grimes * Purge old data structures associated with the inode. 107df8bae1dSRodney W. Grimes */ 108bf7e2ae1SPoul-Henning Kamp if (ip->i_mnt->im_devvp) 109bf7e2ae1SPoul-Henning Kamp vrele(ip->i_mnt->im_devvp); 110df8bae1dSRodney W. Grimes FREE(vp->v_data, M_ISOFSNODE); 111df8bae1dSRodney W. Grimes vp->v_data = NULL; 112a369f34dSPoul-Henning Kamp vnode_destroy_vobject(vp); 11326f9a767SRodney W. Grimes return (0); 114df8bae1dSRodney W. Grimes } 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes /* 117df8bae1dSRodney W. Grimes * File attributes 118df8bae1dSRodney W. Grimes */ 119df8bae1dSRodney W. Grimes void 120988fa8efSJoerg Wunsch cd9660_defattr(isodir, inop, bp, ftype) 121df8bae1dSRodney W. Grimes struct iso_directory_record *isodir; 122df8bae1dSRodney W. Grimes struct iso_node *inop; 123df8bae1dSRodney W. Grimes struct buf *bp; 124988fa8efSJoerg Wunsch enum ISO_FTYPE ftype; 125df8bae1dSRodney W. Grimes { 126df8bae1dSRodney W. Grimes struct buf *bp2 = NULL; 127df8bae1dSRodney W. Grimes struct iso_mnt *imp; 128df8bae1dSRodney W. Grimes struct iso_extended_attributes *ap = NULL; 129df8bae1dSRodney W. Grimes int off; 130df8bae1dSRodney W. Grimes 131988fa8efSJoerg Wunsch /* high sierra does not have timezone data, flag is one byte ahead */ 132988fa8efSJoerg Wunsch if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA? 133988fa8efSJoerg Wunsch &isodir->date[6]: isodir->flags)&2) { 134df8bae1dSRodney W. Grimes inop->inode.iso_mode = S_IFDIR; 135df8bae1dSRodney W. Grimes /* 136df8bae1dSRodney W. Grimes * If we return 2, fts() will assume there are no subdirectories 137df8bae1dSRodney W. Grimes * (just links for the path and .), so instead we return 1. 138df8bae1dSRodney W. Grimes */ 139df8bae1dSRodney W. Grimes inop->inode.iso_links = 1; 140df8bae1dSRodney W. Grimes } else { 141df8bae1dSRodney W. Grimes inop->inode.iso_mode = S_IFREG; 142df8bae1dSRodney W. Grimes inop->inode.iso_links = 1; 143df8bae1dSRodney W. Grimes } 144df8bae1dSRodney W. Grimes if (!bp 145df8bae1dSRodney W. Grimes && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 146df8bae1dSRodney W. Grimes && (off = isonum_711(isodir->ext_attr_length))) { 147cec0f20cSPoul-Henning Kamp cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 148996c772fSJohn Dyson &bp2); 149df8bae1dSRodney W. Grimes bp = bp2; 150df8bae1dSRodney W. Grimes } 151df8bae1dSRodney W. Grimes if (bp) { 152996c772fSJohn Dyson ap = (struct iso_extended_attributes *)bp->b_data; 153df8bae1dSRodney W. Grimes 154df8bae1dSRodney W. Grimes if (isonum_711(ap->version) == 1) { 155df8bae1dSRodney W. Grimes if (!(ap->perm[0]&0x40)) 156df8bae1dSRodney W. Grimes inop->inode.iso_mode |= VEXEC >> 6; 157df8bae1dSRodney W. Grimes if (!(ap->perm[0]&0x10)) 158df8bae1dSRodney W. Grimes inop->inode.iso_mode |= VREAD >> 6; 159df8bae1dSRodney W. Grimes if (!(ap->perm[0]&4)) 160df8bae1dSRodney W. Grimes inop->inode.iso_mode |= VEXEC >> 3; 161df8bae1dSRodney W. Grimes if (!(ap->perm[0]&1)) 162df8bae1dSRodney W. Grimes inop->inode.iso_mode |= VREAD >> 3; 163df8bae1dSRodney W. Grimes if (!(ap->perm[1]&0x40)) 164df8bae1dSRodney W. Grimes inop->inode.iso_mode |= VEXEC; 165df8bae1dSRodney W. Grimes if (!(ap->perm[1]&0x10)) 166df8bae1dSRodney W. Grimes inop->inode.iso_mode |= VREAD; 167df8bae1dSRodney W. Grimes inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 168df8bae1dSRodney W. Grimes inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 169df8bae1dSRodney W. Grimes } else 170df8bae1dSRodney W. Grimes ap = NULL; 171df8bae1dSRodney W. Grimes } 172df8bae1dSRodney W. Grimes if (!ap) { 173df8bae1dSRodney W. Grimes inop->inode.iso_mode |= VREAD|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6; 174df8bae1dSRodney W. Grimes inop->inode.iso_uid = (uid_t)0; 175df8bae1dSRodney W. Grimes inop->inode.iso_gid = (gid_t)0; 176df8bae1dSRodney W. Grimes } 177df8bae1dSRodney W. Grimes if (bp2) 178df8bae1dSRodney W. Grimes brelse(bp2); 179df8bae1dSRodney W. Grimes } 180df8bae1dSRodney W. Grimes 181df8bae1dSRodney W. Grimes /* 182df8bae1dSRodney W. Grimes * Time stamps 183df8bae1dSRodney W. Grimes */ 184df8bae1dSRodney W. Grimes void 185988fa8efSJoerg Wunsch cd9660_deftstamp(isodir,inop,bp,ftype) 186df8bae1dSRodney W. Grimes struct iso_directory_record *isodir; 187df8bae1dSRodney W. Grimes struct iso_node *inop; 188df8bae1dSRodney W. Grimes struct buf *bp; 189988fa8efSJoerg Wunsch enum ISO_FTYPE ftype; 190df8bae1dSRodney W. Grimes { 191df8bae1dSRodney W. Grimes struct buf *bp2 = NULL; 192df8bae1dSRodney W. Grimes struct iso_mnt *imp; 193df8bae1dSRodney W. Grimes struct iso_extended_attributes *ap = NULL; 194df8bae1dSRodney W. Grimes int off; 195df8bae1dSRodney W. Grimes 196df8bae1dSRodney W. Grimes if (!bp 197df8bae1dSRodney W. Grimes && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 198df8bae1dSRodney W. Grimes && (off = isonum_711(isodir->ext_attr_length))) { 199cec0f20cSPoul-Henning Kamp cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 200996c772fSJohn Dyson &bp2); 201df8bae1dSRodney W. Grimes bp = bp2; 202df8bae1dSRodney W. Grimes } 203df8bae1dSRodney W. Grimes if (bp) { 204996c772fSJohn Dyson ap = (struct iso_extended_attributes *)bp->b_data; 205df8bae1dSRodney W. Grimes 206596d40b9SBruce Evans if (ftype != ISO_FTYPE_HIGH_SIERRA 207596d40b9SBruce Evans && isonum_711(ap->version) == 1) { 208df8bae1dSRodney W. Grimes if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 209df8bae1dSRodney W. Grimes cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 210df8bae1dSRodney W. Grimes if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 211df8bae1dSRodney W. Grimes inop->inode.iso_ctime = inop->inode.iso_atime; 212df8bae1dSRodney W. Grimes if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 213df8bae1dSRodney W. Grimes inop->inode.iso_mtime = inop->inode.iso_ctime; 214df8bae1dSRodney W. Grimes } else 215df8bae1dSRodney W. Grimes ap = NULL; 216df8bae1dSRodney W. Grimes } 217df8bae1dSRodney W. Grimes if (!ap) { 218988fa8efSJoerg Wunsch cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime,ftype); 219df8bae1dSRodney W. Grimes inop->inode.iso_atime = inop->inode.iso_ctime; 220df8bae1dSRodney W. Grimes inop->inode.iso_mtime = inop->inode.iso_ctime; 221df8bae1dSRodney W. Grimes } 222df8bae1dSRodney W. Grimes if (bp2) 223df8bae1dSRodney W. Grimes brelse(bp2); 224df8bae1dSRodney W. Grimes } 225df8bae1dSRodney W. Grimes 226df8bae1dSRodney W. Grimes int 227988fa8efSJoerg Wunsch cd9660_tstamp_conv7(pi,pu,ftype) 228996c772fSJohn Dyson u_char *pi; 2291dbaf90cSBruce Evans struct timespec *pu; 230988fa8efSJoerg Wunsch enum ISO_FTYPE ftype; 231df8bae1dSRodney W. Grimes { 232df8bae1dSRodney W. Grimes int crtime, days; 233df8bae1dSRodney W. Grimes int y, m, d, hour, minute, second, tz; 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes y = pi[0] + 1900; 236df8bae1dSRodney W. Grimes m = pi[1]; 237df8bae1dSRodney W. Grimes d = pi[2]; 238df8bae1dSRodney W. Grimes hour = pi[3]; 239df8bae1dSRodney W. Grimes minute = pi[4]; 240df8bae1dSRodney W. Grimes second = pi[5]; 241988fa8efSJoerg Wunsch if(ftype != ISO_FTYPE_HIGH_SIERRA) 242df8bae1dSRodney W. Grimes tz = pi[6]; 243988fa8efSJoerg Wunsch else 244988fa8efSJoerg Wunsch /* original high sierra misses timezone data */ 245988fa8efSJoerg Wunsch tz = 0; 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes if (y < 1970) { 24895a1574eSNate Williams pu->tv_sec = 0; 24995a1574eSNate Williams pu->tv_nsec = 0; 250df8bae1dSRodney W. Grimes return 0; 251df8bae1dSRodney W. Grimes } else { 252df8bae1dSRodney W. Grimes #ifdef ORIGINAL 253df8bae1dSRodney W. Grimes /* computes day number relative to Sept. 19th,1989 */ 254df8bae1dSRodney W. Grimes /* don't even *THINK* about changing formula. It works! */ 255df8bae1dSRodney W. Grimes days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100; 256df8bae1dSRodney W. Grimes #else 257df8bae1dSRodney W. Grimes /* 258df8bae1dSRodney W. Grimes * Changed :-) to make it relative to Jan. 1st, 1970 259df8bae1dSRodney W. Grimes * and to disambiguate negative division 260df8bae1dSRodney W. Grimes */ 261df8bae1dSRodney W. Grimes days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239; 262df8bae1dSRodney W. Grimes #endif 263df8bae1dSRodney W. Grimes crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second; 264df8bae1dSRodney W. Grimes 265df8bae1dSRodney W. Grimes /* timezone offset is unreliable on some disks */ 266df8bae1dSRodney W. Grimes if (-48 <= tz && tz <= 52) 2672d091ecfSBruce Evans crtime -= tz * 15 * 60; 268df8bae1dSRodney W. Grimes } 26995a1574eSNate Williams pu->tv_sec = crtime; 27095a1574eSNate Williams pu->tv_nsec = 0; 271df8bae1dSRodney W. Grimes return 1; 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes 274996c772fSJohn Dyson static u_int 275df8bae1dSRodney W. Grimes cd9660_chars2ui(begin,len) 276996c772fSJohn Dyson u_char *begin; 277df8bae1dSRodney W. Grimes int len; 278df8bae1dSRodney W. Grimes { 279996c772fSJohn Dyson u_int rc; 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes for (rc = 0; --len >= 0;) { 282df8bae1dSRodney W. Grimes rc *= 10; 283df8bae1dSRodney W. Grimes rc += *begin++ - '0'; 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes return rc; 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes int 289df8bae1dSRodney W. Grimes cd9660_tstamp_conv17(pi,pu) 290996c772fSJohn Dyson u_char *pi; 2911dbaf90cSBruce Evans struct timespec *pu; 292df8bae1dSRodney W. Grimes { 293996c772fSJohn Dyson u_char buf[7]; 294df8bae1dSRodney W. Grimes 295df8bae1dSRodney W. Grimes /* year:"0001"-"9999" -> -1900 */ 296df8bae1dSRodney W. Grimes buf[0] = cd9660_chars2ui(pi,4) - 1900; 297df8bae1dSRodney W. Grimes 298df8bae1dSRodney W. Grimes /* month: " 1"-"12" -> 1 - 12 */ 299df8bae1dSRodney W. Grimes buf[1] = cd9660_chars2ui(pi + 4,2); 300df8bae1dSRodney W. Grimes 301df8bae1dSRodney W. Grimes /* day: " 1"-"31" -> 1 - 31 */ 302df8bae1dSRodney W. Grimes buf[2] = cd9660_chars2ui(pi + 6,2); 303df8bae1dSRodney W. Grimes 304df8bae1dSRodney W. Grimes /* hour: " 0"-"23" -> 0 - 23 */ 305df8bae1dSRodney W. Grimes buf[3] = cd9660_chars2ui(pi + 8,2); 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes /* minute:" 0"-"59" -> 0 - 59 */ 308df8bae1dSRodney W. Grimes buf[4] = cd9660_chars2ui(pi + 10,2); 309df8bae1dSRodney W. Grimes 310df8bae1dSRodney W. Grimes /* second:" 0"-"59" -> 0 - 59 */ 311df8bae1dSRodney W. Grimes buf[5] = cd9660_chars2ui(pi + 12,2); 312df8bae1dSRodney W. Grimes 313df8bae1dSRodney W. Grimes /* difference of GMT */ 314df8bae1dSRodney W. Grimes buf[6] = pi[16]; 315df8bae1dSRodney W. Grimes 316988fa8efSJoerg Wunsch return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT); 317df8bae1dSRodney W. Grimes } 318df8bae1dSRodney W. Grimes 319996c772fSJohn Dyson ino_t 320996c772fSJohn Dyson isodirino(isodir, imp) 321df8bae1dSRodney W. Grimes struct iso_directory_record *isodir; 322df8bae1dSRodney W. Grimes struct iso_mnt *imp; 323df8bae1dSRodney W. Grimes { 324996c772fSJohn Dyson ino_t ino; 325996c772fSJohn Dyson 326996c772fSJohn Dyson ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length)) 327996c772fSJohn Dyson << imp->im_bshift; 328996c772fSJohn Dyson return (ino); 329df8bae1dSRodney W. Grimes } 330