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 50a8d36d0dSCraig Rodrigues #include <fs/cd9660/iso.h> 51a8d36d0dSCraig Rodrigues #include <fs/cd9660/cd9660_node.h> 52a8d36d0dSCraig Rodrigues #include <fs/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; 68bffd1b7aSPoul-Henning Kamp struct iso_node *ip = VTOI(vp); 691295d82eSGary Palmer int error = 0; 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72df8bae1dSRodney W. Grimes * If we are done with the inode, reclaim it 73df8bae1dSRodney W. Grimes * so that it can be reused immediately. 74df8bae1dSRodney W. Grimes */ 75996c772fSJohn Dyson if (ip->inode.iso_mode == 0) 76*af6e6b87SEdward Tomasz Napierala vrecycle(vp); 77df8bae1dSRodney W. Grimes return error; 78df8bae1dSRodney W. Grimes } 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes /* 81df8bae1dSRodney W. Grimes * Reclaim an inode so that it can be used for other purposes. 82df8bae1dSRodney W. Grimes */ 83df8bae1dSRodney W. Grimes int 84df8bae1dSRodney W. Grimes cd9660_reclaim(ap) 85df8bae1dSRodney W. Grimes struct vop_reclaim_args /* { 86df8bae1dSRodney W. Grimes struct vnode *a_vp; 87b40ce416SJulian Elischer struct thread *a_td; 88df8bae1dSRodney W. Grimes } */ *ap; 89df8bae1dSRodney W. Grimes { 90bffd1b7aSPoul-Henning Kamp struct vnode *vp = ap->a_vp; 91df8bae1dSRodney W. Grimes 92df8bae1dSRodney W. Grimes /* 9392e73f57SAlfred Perlstein * Destroy the vm object and flush associated pages. 9492e73f57SAlfred Perlstein */ 9592e73f57SAlfred Perlstein vnode_destroy_vobject(vp); 9692e73f57SAlfred Perlstein /* 97df8bae1dSRodney W. Grimes * Remove the inode from its hash chain. 98df8bae1dSRodney W. Grimes */ 99dfb9f846SPoul-Henning Kamp vfs_hash_remove(vp); 100dfb9f846SPoul-Henning Kamp 101df8bae1dSRodney W. Grimes /* 102df8bae1dSRodney W. Grimes * Purge old data structures associated with the inode. 103df8bae1dSRodney W. Grimes */ 1041ede983cSDag-Erling Smørgrav free(vp->v_data, M_ISOFSNODE); 105df8bae1dSRodney W. Grimes vp->v_data = NULL; 10626f9a767SRodney W. Grimes return (0); 107df8bae1dSRodney W. Grimes } 108df8bae1dSRodney W. Grimes 109df8bae1dSRodney W. Grimes /* 110df8bae1dSRodney W. Grimes * File attributes 111df8bae1dSRodney W. Grimes */ 112df8bae1dSRodney W. Grimes void 113988fa8efSJoerg Wunsch cd9660_defattr(isodir, inop, bp, ftype) 114df8bae1dSRodney W. Grimes struct iso_directory_record *isodir; 115df8bae1dSRodney W. Grimes struct iso_node *inop; 116df8bae1dSRodney W. Grimes struct buf *bp; 117988fa8efSJoerg Wunsch enum ISO_FTYPE ftype; 118df8bae1dSRodney W. Grimes { 119df8bae1dSRodney W. Grimes struct buf *bp2 = NULL; 120df8bae1dSRodney W. Grimes struct iso_mnt *imp; 121df8bae1dSRodney W. Grimes struct iso_extended_attributes *ap = NULL; 122df8bae1dSRodney W. Grimes int off; 123df8bae1dSRodney W. Grimes 124988fa8efSJoerg Wunsch /* high sierra does not have timezone data, flag is one byte ahead */ 125988fa8efSJoerg Wunsch if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA? 126988fa8efSJoerg Wunsch &isodir->date[6]: isodir->flags)&2) { 127df8bae1dSRodney W. Grimes inop->inode.iso_mode = S_IFDIR; 128df8bae1dSRodney W. Grimes /* 129df8bae1dSRodney W. Grimes * If we return 2, fts() will assume there are no subdirectories 130df8bae1dSRodney W. Grimes * (just links for the path and .), so instead we return 1. 131df8bae1dSRodney W. Grimes */ 132df8bae1dSRodney W. Grimes inop->inode.iso_links = 1; 133df8bae1dSRodney W. Grimes } else { 134df8bae1dSRodney W. Grimes inop->inode.iso_mode = S_IFREG; 135df8bae1dSRodney W. Grimes inop->inode.iso_links = 1; 136df8bae1dSRodney W. Grimes } 137df8bae1dSRodney W. Grimes if (!bp 138df8bae1dSRodney W. Grimes && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 139df8bae1dSRodney W. Grimes && (off = isonum_711(isodir->ext_attr_length))) { 140cec0f20cSPoul-Henning Kamp cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 141996c772fSJohn Dyson &bp2); 142df8bae1dSRodney W. Grimes bp = bp2; 143df8bae1dSRodney W. Grimes } 144df8bae1dSRodney W. Grimes if (bp) { 145996c772fSJohn Dyson ap = (struct iso_extended_attributes *)bp->b_data; 146df8bae1dSRodney W. Grimes 147df8bae1dSRodney W. Grimes if (isonum_711(ap->version) == 1) { 148df8bae1dSRodney W. Grimes if (!(ap->perm[0]&0x40)) 149464119c4SEdward Tomasz Napierala inop->inode.iso_mode |= S_IXOTH; 150df8bae1dSRodney W. Grimes if (!(ap->perm[0]&0x10)) 151464119c4SEdward Tomasz Napierala inop->inode.iso_mode |= S_IROTH; 152df8bae1dSRodney W. Grimes if (!(ap->perm[0]&4)) 153464119c4SEdward Tomasz Napierala inop->inode.iso_mode |= S_IXGRP; 154df8bae1dSRodney W. Grimes if (!(ap->perm[0]&1)) 155464119c4SEdward Tomasz Napierala inop->inode.iso_mode |= S_IRGRP; 156df8bae1dSRodney W. Grimes if (!(ap->perm[1]&0x40)) 157464119c4SEdward Tomasz Napierala inop->inode.iso_mode |= S_IXUSR; 158df8bae1dSRodney W. Grimes if (!(ap->perm[1]&0x10)) 159464119c4SEdward Tomasz Napierala inop->inode.iso_mode |= S_IRUSR; 160df8bae1dSRodney W. Grimes inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 161df8bae1dSRodney W. Grimes inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 162df8bae1dSRodney W. Grimes } else 163df8bae1dSRodney W. Grimes ap = NULL; 164df8bae1dSRodney W. Grimes } 165df8bae1dSRodney W. Grimes if (!ap) { 166464119c4SEdward Tomasz Napierala inop->inode.iso_mode |= S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 167df8bae1dSRodney W. Grimes inop->inode.iso_uid = (uid_t)0; 168df8bae1dSRodney W. Grimes inop->inode.iso_gid = (gid_t)0; 169df8bae1dSRodney W. Grimes } 170df8bae1dSRodney W. Grimes if (bp2) 171df8bae1dSRodney W. Grimes brelse(bp2); 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes 174df8bae1dSRodney W. Grimes /* 175df8bae1dSRodney W. Grimes * Time stamps 176df8bae1dSRodney W. Grimes */ 177df8bae1dSRodney W. Grimes void 178988fa8efSJoerg Wunsch cd9660_deftstamp(isodir,inop,bp,ftype) 179df8bae1dSRodney W. Grimes struct iso_directory_record *isodir; 180df8bae1dSRodney W. Grimes struct iso_node *inop; 181df8bae1dSRodney W. Grimes struct buf *bp; 182988fa8efSJoerg Wunsch enum ISO_FTYPE ftype; 183df8bae1dSRodney W. Grimes { 184df8bae1dSRodney W. Grimes struct buf *bp2 = NULL; 185df8bae1dSRodney W. Grimes struct iso_mnt *imp; 186df8bae1dSRodney W. Grimes struct iso_extended_attributes *ap = NULL; 187df8bae1dSRodney W. Grimes int off; 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes if (!bp 190df8bae1dSRodney W. Grimes && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 191df8bae1dSRodney W. Grimes && (off = isonum_711(isodir->ext_attr_length))) { 192cec0f20cSPoul-Henning Kamp cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 193996c772fSJohn Dyson &bp2); 194df8bae1dSRodney W. Grimes bp = bp2; 195df8bae1dSRodney W. Grimes } 196df8bae1dSRodney W. Grimes if (bp) { 197996c772fSJohn Dyson ap = (struct iso_extended_attributes *)bp->b_data; 198df8bae1dSRodney W. Grimes 199596d40b9SBruce Evans if (ftype != ISO_FTYPE_HIGH_SIERRA 200596d40b9SBruce Evans && isonum_711(ap->version) == 1) { 201df8bae1dSRodney W. Grimes if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 202df8bae1dSRodney W. Grimes cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 203df8bae1dSRodney W. Grimes if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 204df8bae1dSRodney W. Grimes inop->inode.iso_ctime = inop->inode.iso_atime; 205df8bae1dSRodney W. Grimes if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 206df8bae1dSRodney W. Grimes inop->inode.iso_mtime = inop->inode.iso_ctime; 207df8bae1dSRodney W. Grimes } else 208df8bae1dSRodney W. Grimes ap = NULL; 209df8bae1dSRodney W. Grimes } 210df8bae1dSRodney W. Grimes if (!ap) { 211988fa8efSJoerg Wunsch cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime,ftype); 212df8bae1dSRodney W. Grimes inop->inode.iso_atime = inop->inode.iso_ctime; 213df8bae1dSRodney W. Grimes inop->inode.iso_mtime = inop->inode.iso_ctime; 214df8bae1dSRodney W. Grimes } 215df8bae1dSRodney W. Grimes if (bp2) 216df8bae1dSRodney W. Grimes brelse(bp2); 217df8bae1dSRodney W. Grimes } 218df8bae1dSRodney W. Grimes 219df8bae1dSRodney W. Grimes int 220988fa8efSJoerg Wunsch cd9660_tstamp_conv7(pi,pu,ftype) 221996c772fSJohn Dyson u_char *pi; 2221dbaf90cSBruce Evans struct timespec *pu; 223988fa8efSJoerg Wunsch enum ISO_FTYPE ftype; 224df8bae1dSRodney W. Grimes { 225df8bae1dSRodney W. Grimes int crtime, days; 226df8bae1dSRodney W. Grimes int y, m, d, hour, minute, second, tz; 227df8bae1dSRodney W. Grimes 228df8bae1dSRodney W. Grimes y = pi[0] + 1900; 229df8bae1dSRodney W. Grimes m = pi[1]; 230df8bae1dSRodney W. Grimes d = pi[2]; 231df8bae1dSRodney W. Grimes hour = pi[3]; 232df8bae1dSRodney W. Grimes minute = pi[4]; 233df8bae1dSRodney W. Grimes second = pi[5]; 234988fa8efSJoerg Wunsch if(ftype != ISO_FTYPE_HIGH_SIERRA) 2355c423e06STim Kientzle tz = ((signed char *)pi)[6]; /* Timezone value is signed. */ 236988fa8efSJoerg Wunsch else 237988fa8efSJoerg Wunsch /* original high sierra misses timezone data */ 238988fa8efSJoerg Wunsch tz = 0; 239df8bae1dSRodney W. Grimes 240df8bae1dSRodney W. Grimes if (y < 1970) { 24195a1574eSNate Williams pu->tv_sec = 0; 24295a1574eSNate Williams pu->tv_nsec = 0; 243df8bae1dSRodney W. Grimes return 0; 244df8bae1dSRodney W. Grimes } else { 245df8bae1dSRodney W. Grimes #ifdef ORIGINAL 246df8bae1dSRodney W. Grimes /* computes day number relative to Sept. 19th,1989 */ 247df8bae1dSRodney W. Grimes /* don't even *THINK* about changing formula. It works! */ 248df8bae1dSRodney 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; 249df8bae1dSRodney W. Grimes #else 250df8bae1dSRodney W. Grimes /* 251df8bae1dSRodney W. Grimes * Changed :-) to make it relative to Jan. 1st, 1970 252df8bae1dSRodney W. Grimes * and to disambiguate negative division 253df8bae1dSRodney W. Grimes */ 254df8bae1dSRodney 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; 255df8bae1dSRodney W. Grimes #endif 256df8bae1dSRodney W. Grimes crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second; 257df8bae1dSRodney W. Grimes 258df8bae1dSRodney W. Grimes /* timezone offset is unreliable on some disks */ 259df8bae1dSRodney W. Grimes if (-48 <= tz && tz <= 52) 2602d091ecfSBruce Evans crtime -= tz * 15 * 60; 261df8bae1dSRodney W. Grimes } 26295a1574eSNate Williams pu->tv_sec = crtime; 26395a1574eSNate Williams pu->tv_nsec = 0; 264df8bae1dSRodney W. Grimes return 1; 265df8bae1dSRodney W. Grimes } 266df8bae1dSRodney W. Grimes 267996c772fSJohn Dyson static u_int 268df8bae1dSRodney W. Grimes cd9660_chars2ui(begin,len) 269996c772fSJohn Dyson u_char *begin; 270df8bae1dSRodney W. Grimes int len; 271df8bae1dSRodney W. Grimes { 272996c772fSJohn Dyson u_int rc; 273df8bae1dSRodney W. Grimes 274df8bae1dSRodney W. Grimes for (rc = 0; --len >= 0;) { 275df8bae1dSRodney W. Grimes rc *= 10; 276df8bae1dSRodney W. Grimes rc += *begin++ - '0'; 277df8bae1dSRodney W. Grimes } 278df8bae1dSRodney W. Grimes return rc; 279df8bae1dSRodney W. Grimes } 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes int 282df8bae1dSRodney W. Grimes cd9660_tstamp_conv17(pi,pu) 283996c772fSJohn Dyson u_char *pi; 2841dbaf90cSBruce Evans struct timespec *pu; 285df8bae1dSRodney W. Grimes { 286996c772fSJohn Dyson u_char buf[7]; 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes /* year:"0001"-"9999" -> -1900 */ 289df8bae1dSRodney W. Grimes buf[0] = cd9660_chars2ui(pi,4) - 1900; 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes /* month: " 1"-"12" -> 1 - 12 */ 292df8bae1dSRodney W. Grimes buf[1] = cd9660_chars2ui(pi + 4,2); 293df8bae1dSRodney W. Grimes 294df8bae1dSRodney W. Grimes /* day: " 1"-"31" -> 1 - 31 */ 295df8bae1dSRodney W. Grimes buf[2] = cd9660_chars2ui(pi + 6,2); 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes /* hour: " 0"-"23" -> 0 - 23 */ 298df8bae1dSRodney W. Grimes buf[3] = cd9660_chars2ui(pi + 8,2); 299df8bae1dSRodney W. Grimes 300df8bae1dSRodney W. Grimes /* minute:" 0"-"59" -> 0 - 59 */ 301df8bae1dSRodney W. Grimes buf[4] = cd9660_chars2ui(pi + 10,2); 302df8bae1dSRodney W. Grimes 303df8bae1dSRodney W. Grimes /* second:" 0"-"59" -> 0 - 59 */ 304df8bae1dSRodney W. Grimes buf[5] = cd9660_chars2ui(pi + 12,2); 305df8bae1dSRodney W. Grimes 306df8bae1dSRodney W. Grimes /* difference of GMT */ 307df8bae1dSRodney W. Grimes buf[6] = pi[16]; 308df8bae1dSRodney W. Grimes 309988fa8efSJoerg Wunsch return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT); 310df8bae1dSRodney W. Grimes } 311df8bae1dSRodney W. Grimes 312996c772fSJohn Dyson ino_t 313996c772fSJohn Dyson isodirino(isodir, imp) 314df8bae1dSRodney W. Grimes struct iso_directory_record *isodir; 315df8bae1dSRodney W. Grimes struct iso_mnt *imp; 316df8bae1dSRodney W. Grimes { 317996c772fSJohn Dyson ino_t ino; 318996c772fSJohn Dyson 319996c772fSJohn Dyson ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length)) 320996c772fSJohn Dyson << imp->im_bshift; 321996c772fSJohn Dyson return (ino); 322df8bae1dSRodney W. Grimes } 323