1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1994, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley 8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 9 * Support code is derived from software contributed to Berkeley 10 * by Atsushi Murai (amurai@spec.co.jp). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)cd9660_node.c 8.2 (Berkeley) 1/23/94 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/mount.h> 45 #include <sys/bio.h> 46 #include <sys/buf.h> 47 #include <sys/vnode.h> 48 #include <sys/malloc.h> 49 #include <sys/stat.h> 50 #include <sys/mutex.h> 51 52 #include <fs/cd9660/iso.h> 53 #include <fs/cd9660/cd9660_node.h> 54 #include <fs/cd9660/cd9660_mount.h> 55 56 static unsigned cd9660_chars2ui(unsigned char *begin, int len); 57 58 /* 59 * Last reference to an inode, write the inode out and if necessary, 60 * truncate and deallocate the file. 61 */ 62 int 63 cd9660_inactive(ap) 64 struct vop_inactive_args /* { 65 struct vnode *a_vp; 66 struct thread *a_td; 67 } */ *ap; 68 { 69 struct vnode *vp = ap->a_vp; 70 struct iso_node *ip = VTOI(vp); 71 int error = 0; 72 73 /* 74 * If we are done with the inode, reclaim it 75 * so that it can be reused immediately. 76 */ 77 if (ip->inode.iso_mode == 0) 78 vrecycle(vp); 79 return error; 80 } 81 82 /* 83 * Reclaim an inode so that it can be used for other purposes. 84 */ 85 int 86 cd9660_reclaim(ap) 87 struct vop_reclaim_args /* { 88 struct vnode *a_vp; 89 struct thread *a_td; 90 } */ *ap; 91 { 92 struct vnode *vp = ap->a_vp; 93 94 /* 95 * Remove the inode from its hash chain. 96 */ 97 vfs_hash_remove(vp); 98 99 /* 100 * Purge old data structures associated with the inode. 101 */ 102 free(vp->v_data, M_ISOFSNODE); 103 vp->v_data = NULL; 104 return (0); 105 } 106 107 /* 108 * File attributes 109 */ 110 void 111 cd9660_defattr(isodir, inop, bp, ftype) 112 struct iso_directory_record *isodir; 113 struct iso_node *inop; 114 struct buf *bp; 115 enum ISO_FTYPE ftype; 116 { 117 struct buf *bp2 = NULL; 118 struct iso_mnt *imp; 119 struct iso_extended_attributes *ap = NULL; 120 int off; 121 122 /* high sierra does not have timezone data, flag is one byte ahead */ 123 if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA? 124 &isodir->date[6]: isodir->flags)&2) { 125 inop->inode.iso_mode = S_IFDIR; 126 /* 127 * If we return 2, fts() will assume there are no subdirectories 128 * (just links for the path and .), so instead we return 1. 129 */ 130 inop->inode.iso_links = 1; 131 } else { 132 inop->inode.iso_mode = S_IFREG; 133 inop->inode.iso_links = 1; 134 } 135 if (!bp 136 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 137 && (off = isonum_711(isodir->ext_attr_length))) { 138 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 139 &bp2); 140 bp = bp2; 141 } 142 if (bp) { 143 ap = (struct iso_extended_attributes *)bp->b_data; 144 145 if (isonum_711(ap->version) == 1) { 146 if (!(ap->perm[0]&0x40)) 147 inop->inode.iso_mode |= S_IXOTH; 148 if (!(ap->perm[0]&0x10)) 149 inop->inode.iso_mode |= S_IROTH; 150 if (!(ap->perm[0]&4)) 151 inop->inode.iso_mode |= S_IXGRP; 152 if (!(ap->perm[0]&1)) 153 inop->inode.iso_mode |= S_IRGRP; 154 if (!(ap->perm[1]&0x40)) 155 inop->inode.iso_mode |= S_IXUSR; 156 if (!(ap->perm[1]&0x10)) 157 inop->inode.iso_mode |= S_IRUSR; 158 inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */ 159 inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */ 160 } else 161 ap = NULL; 162 } 163 if (!ap) { 164 inop->inode.iso_mode |= S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 165 inop->inode.iso_uid = (uid_t)0; 166 inop->inode.iso_gid = (gid_t)0; 167 } 168 if (bp2) 169 brelse(bp2); 170 } 171 172 /* 173 * Time stamps 174 */ 175 void 176 cd9660_deftstamp(isodir,inop,bp,ftype) 177 struct iso_directory_record *isodir; 178 struct iso_node *inop; 179 struct buf *bp; 180 enum ISO_FTYPE ftype; 181 { 182 struct buf *bp2 = NULL; 183 struct iso_mnt *imp; 184 struct iso_extended_attributes *ap = NULL; 185 int off; 186 187 if (!bp 188 && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT) 189 && (off = isonum_711(isodir->ext_attr_length))) { 190 cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, 191 &bp2); 192 bp = bp2; 193 } 194 if (bp) { 195 ap = (struct iso_extended_attributes *)bp->b_data; 196 197 if (ftype != ISO_FTYPE_HIGH_SIERRA 198 && isonum_711(ap->version) == 1) { 199 if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime)) 200 cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime); 201 if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime)) 202 inop->inode.iso_ctime = inop->inode.iso_atime; 203 if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime)) 204 inop->inode.iso_mtime = inop->inode.iso_ctime; 205 } else 206 ap = NULL; 207 } 208 if (!ap) { 209 cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime,ftype); 210 inop->inode.iso_atime = inop->inode.iso_ctime; 211 inop->inode.iso_mtime = inop->inode.iso_ctime; 212 } 213 if (bp2) 214 brelse(bp2); 215 } 216 217 int 218 cd9660_tstamp_conv7(pi,pu,ftype) 219 u_char *pi; 220 struct timespec *pu; 221 enum ISO_FTYPE ftype; 222 { 223 int crtime, days; 224 int y, m, d, hour, minute, second, tz; 225 226 y = pi[0] + 1900; 227 m = pi[1]; 228 d = pi[2]; 229 hour = pi[3]; 230 minute = pi[4]; 231 second = pi[5]; 232 if(ftype != ISO_FTYPE_HIGH_SIERRA) 233 tz = ((signed char *)pi)[6]; /* Timezone value is signed. */ 234 else 235 /* original high sierra misses timezone data */ 236 tz = 0; 237 238 if (y < 1970) { 239 pu->tv_sec = 0; 240 pu->tv_nsec = 0; 241 return 0; 242 } else { 243 #ifdef ORIGINAL 244 /* computes day number relative to Sept. 19th,1989 */ 245 /* don't even *THINK* about changing formula. It works! */ 246 days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100; 247 #else 248 /* 249 * Changed :-) to make it relative to Jan. 1st, 1970 250 * and to disambiguate negative division 251 */ 252 days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239; 253 #endif 254 crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second; 255 256 /* timezone offset is unreliable on some disks */ 257 if (-48 <= tz && tz <= 52) 258 crtime -= tz * 15 * 60; 259 } 260 pu->tv_sec = crtime; 261 pu->tv_nsec = 0; 262 return 1; 263 } 264 265 static u_int 266 cd9660_chars2ui(begin,len) 267 u_char *begin; 268 int len; 269 { 270 u_int rc; 271 272 for (rc = 0; --len >= 0;) { 273 rc *= 10; 274 rc += *begin++ - '0'; 275 } 276 return rc; 277 } 278 279 int 280 cd9660_tstamp_conv17(pi,pu) 281 u_char *pi; 282 struct timespec *pu; 283 { 284 u_char buf[7]; 285 286 /* year:"0001"-"9999" -> -1900 */ 287 buf[0] = cd9660_chars2ui(pi,4) - 1900; 288 289 /* month: " 1"-"12" -> 1 - 12 */ 290 buf[1] = cd9660_chars2ui(pi + 4,2); 291 292 /* day: " 1"-"31" -> 1 - 31 */ 293 buf[2] = cd9660_chars2ui(pi + 6,2); 294 295 /* hour: " 0"-"23" -> 0 - 23 */ 296 buf[3] = cd9660_chars2ui(pi + 8,2); 297 298 /* minute:" 0"-"59" -> 0 - 59 */ 299 buf[4] = cd9660_chars2ui(pi + 10,2); 300 301 /* second:" 0"-"59" -> 0 - 59 */ 302 buf[5] = cd9660_chars2ui(pi + 12,2); 303 304 /* difference of GMT */ 305 buf[6] = pi[16]; 306 307 return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT); 308 } 309 310 cd_ino_t 311 isodirino(isodir, imp) 312 struct iso_directory_record *isodir; 313 struct iso_mnt *imp; 314 { 315 cd_ino_t ino; 316 317 /* 318 * Note there is an inverse calculation in 319 * cd9660_vfsops.c:cd9660_vget_internal(): 320 * ip->iso_start = ino >> imp->im_bshift; 321 * and also a calculation of the isodir pointer 322 * from an inode in cd9660_vnops.c:cd9660_readlink() 323 */ 324 ino = ((cd_ino_t)isonum_733(isodir->extent) + 325 isonum_711(isodir->ext_attr_length)) << imp->im_bshift; 326 return ino; 327 } 328