darwin-xnu/bsd/isofs/cd9660/cd9660_node.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
   3 *
   4 * @APPLE_LICENSE_HEADER_START@
   5 * 
   6 * The contents of this file constitute Original Code as defined in and
   7 * are subject to the Apple Public Source License Version 1.1 (the
   8 * "License").  You may not use this file except in compliance with the
   9 * License.  Please obtain a copy of the License at
  10 * http://www.apple.com/publicsource and read it before using this file.
  11 * 
  12 * This Original Code and all software distributed under the License are
  13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
  17 * License for the specific language governing rights and limitations
  18 * under the License.
  19 * 
  20 * @APPLE_LICENSE_HEADER_END@
  21 */
  22/*      $NetBSD: cd9660_node.c,v 1.13 1994/12/24 15:30:07 cgd Exp $     */
  23
  24/*-
  25 * Copyright (c) 1982, 1986, 1989, 1994
  26 *      The Regents of the University of California.  All rights reserved.
  27 *
  28 * This code is derived from software contributed to Berkeley
  29 * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
  30 * Support code is derived from software contributed to Berkeley
  31 * by Atsushi Murai (amurai@spec.co.jp).
  32 *
  33 * Redistribution and use in source and binary forms, with or without
  34 * modification, are permitted provided that the following conditions
  35 * are met:
  36 * 1. Redistributions of source code must retain the above copyright
  37 *    notice, this list of conditions and the following disclaimer.
  38 * 2. Redistributions in binary form must reproduce the above copyright
  39 *    notice, this list of conditions and the following disclaimer in the
  40 *    documentation and/or other materials provided with the distribution.
  41 * 3. All advertising materials mentioning features or use of this software
  42 *    must display the following acknowledgement:
  43 *      This product includes software developed by the University of
  44 *      California, Berkeley and its contributors.
  45 * 4. Neither the name of the University nor the names of its contributors
  46 *    may be used to endorse or promote products derived from this software
  47 *    without specific prior written permission.
  48 *
  49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59 * SUCH DAMAGE.
  60 *
  61 *      @(#)cd9660_node.c       8.5 (Berkeley) 12/5/94
  62
  63
  64
  65 * HISTORY
  66 * 22-Jan-98    radar 1669467 - ISO 9660 CD support - jwc
  67 * 17-Feb-98    radar 1669467 - changed lock protocols to use the lock manager - chw
  68
  69 */
  70
  71#include <sys/param.h>
  72#include <sys/systm.h>
  73#include <sys/mount.h>
  74#include <sys/proc.h>
  75#include <sys/file.h>
  76#include <sys/buf.h>
  77#include <sys/vnode.h>
  78#include <sys/kernel.h>
  79#include <sys/malloc.h>
  80#include <sys/stat.h>
  81#include <sys/lock.h>
  82#include <sys/namei.h>
  83
  84#include <isofs/cd9660/iso.h>
  85#include <isofs/cd9660/cd9660_node.h>
  86#include <isofs/cd9660/iso_rrip.h>
  87#include <isofs/cd9660/cd9660_mount.h>
  88
  89/*
  90 * Structures associated with iso_node caching.
  91 */
  92struct iso_node **isohashtbl;
  93u_long isohash;
  94#define INOHASH(device, inum)   (((device) + ((inum)>>12)) & isohash)
  95
  96#ifdef ISODEVMAP
  97struct iso_node **idvhashtbl;
  98u_long idvhash;
  99#define DNOHASH(device, inum)   (((device) + ((inum)>>12)) & idvhash)
 100#endif
 101
 102/* defined in bsd/vfs/vfs_subr.c */
 103extern int prtactive;   /* 1 => print out reclaim of active vnodes */
 104
 105extern u_char isonullname[];
 106/*
 107 * Initialize hash links for inodes and dnodes.
 108 */
 109int
 110cd9660_init(__unused struct vfsconf *cp)
 111{
 112
 113        isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, &isohash);
 114#ifdef ISODEVMAP
 115        idvhashtbl = hashinit(desiredvnodes / 8, M_ISOFSMNT, &idvhash);
 116#endif
 117    return 0;
 118}
 119
 120#ifdef ISODEVMAP
 121/*
 122 * Enter a new node into the device hash list
 123 */
 124struct iso_dnode *
 125iso_dmap(dev_t device, ino_t inum, int create)
 126{
 127        register struct iso_dnode **dpp, *dp, *dq;
 128
 129        dpp = &idvhashtbl[DNOHASH(device, inum)];
 130        for (dp = *dpp;; dp = dp->d_next) {
 131                if (dp == NULL)
 132                        return (NULL);
 133                if (inum == dp->i_number && device == dp->i_dev)
 134                        return (dp);
 135
 136        if (!create)
 137                return (NULL);
 138
 139        MALLOC(dp, struct iso_dnode *, sizeof(struct iso_dnode), M_CACHE,
 140               M_WAITOK);
 141        dp->i_dev = dev;
 142        dp->i_number = ino;
 143
 144        if (dq = *dpp)
 145                dq->d_prev = dp->d_next;
 146        dp->d_next = dq;
 147        dp->d_prev = dpp;
 148        *dpp = dp;
 149
 150        return (dp);
 151}
 152
 153void
 154iso_dunmap(dev_t device)
 155{
 156        struct iso_dnode **dpp, *dp, *dq;
 157        
 158        for (dpp = idvhashtbl; dpp <= idvhashtbl + idvhash; dpp++) {
 159                for (dp = *dpp; dp != NULL; dp = dq)
 160                        dq = dp->d_next;
 161                        if (device == dp->i_dev) {
 162                                if (dq)
 163                                        dq->d_prev = dp->d_prev;
 164                                *dp->d_prev = dq;
 165                                FREE(dp, M_CACHE);
 166                        }
 167                }
 168        }
 169}
 170#endif
 171
 172/*
 173 * Use the device/inum pair to find the incore inode, and return a pointer
 174 * to it. If it is in core, but locked, wait for it.
 175 */
 176struct vnode *
 177cd9660_ihashget(dev_t device, ino_t inum, struct proc *p)
 178{
 179        register struct iso_node *ip;
 180        struct vnode *vp;
 181        uint32_t vid;
 182
 183retry:
 184        for (ip = isohashtbl[INOHASH(device, inum)]; ip; ip = ip->i_next) {
 185                if (inum == ip->i_number && device == ip->i_dev) {
 186                          
 187                        if (ISSET(ip->i_flag, ISO_INALLOC)) {
 188                                /*
 189                                 * inode is being created... wait for it
 190                                 * to be ready for consumption
 191                                 */
 192                                SET(ip->i_flag, ISO_INWALLOC);
 193                                tsleep((caddr_t)ip, PINOD, "cd9960_ihashget", 0);
 194                                goto retry;
 195                        }
 196                        vp = ITOV(ip);
 197                        /*
 198                         * the vid needs to be grabbed before we drop
 199                         * lock protecting the hash
 200                         */
 201                        vid = vnode_vid(vp);
 202
 203                        /*
 204                         * we currently depend on running under the FS funnel
 205                         * when we do proper locking and advertise ourselves
 206                         * as thread safe, we'll need a lock to protect the
 207                         * hash lookup... this is where we would drop it
 208                         */
 209                        if (vnode_getwithvid(vp, vid)) {
 210                                /*
 211                                 * If vnode is being reclaimed, or has
 212                                 * already changed identity, no need to wait
 213                                 */
 214                                return (NULL);
 215                        }       
 216                        return (vp);
 217                }
 218        }
 219        return (NULL);
 220}
 221
 222/*
 223 * Insert the inode into the hash table, and return it locked.
 224 */
 225void
 226cd9660_ihashins(struct iso_node *ip)
 227{
 228        struct iso_node **ipp, *iq;
 229
 230        /* lock the inode, then put it on the appropriate hash list */
 231
 232        ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
 233        if ((iq = *ipp))
 234                iq->i_prev = &ip->i_next;
 235        ip->i_next = iq;
 236        ip->i_prev = ipp;
 237        *ipp = ip;
 238}
 239
 240/*
 241 * Remove the inode from the hash table.
 242 */
 243void
 244cd9660_ihashrem(register struct iso_node *ip)
 245{
 246        register struct iso_node *iq;
 247
 248        if ((iq = ip->i_next))
 249                iq->i_prev = ip->i_prev;
 250        *ip->i_prev = iq;
 251#if 1 /* was ifdef DIAGNOSTIC */
 252        ip->i_next = NULL;
 253        ip->i_prev = NULL;
 254#endif
 255}
 256
 257/*
 258 * Last reference to an inode... if we're done with
 259 * it, go ahead and recycle it for other use
 260 */
 261int
 262cd9660_inactive(struct vnop_inactive_args *ap)
 263{
 264        vnode_t vp = ap->a_vp;
 265        struct iso_node *ip = VTOI(vp);
 266        
 267        /*
 268         * If we are done with the inode, reclaim it
 269         * so that it can be reused immediately.
 270         */
 271        if (ip->inode.iso_mode == 0)
 272                vnode_recycle(vp);
 273
 274        return 0;
 275}
 276
 277/*
 278 * Reclaim an inode so that it can be used for other purposes.
 279 */
 280int
 281cd9660_reclaim(struct vnop_reclaim_args *ap)
 282{
 283        vnode_t vp = ap->a_vp;
 284        struct iso_node *ip = VTOI(vp);
 285        
 286        vnode_removefsref(vp);
 287        /*
 288         * Remove the inode from its hash chain.
 289         */
 290        cd9660_ihashrem(ip);
 291
 292        if (ip->i_devvp) {
 293                vnode_t devvp = ip->i_devvp;
 294                ip->i_devvp = NULL;
 295                vnode_rele(devvp);
 296        }
 297        vnode_clearfsnode(vp);
 298
 299        if (ip->i_namep != isonullname)
 300                FREE(ip->i_namep, M_TEMP);
 301        if (ip->i_riff != NULL)
 302                FREE(ip->i_riff, M_TEMP);
 303        FREE_ZONE(ip, sizeof(struct iso_node), M_ISOFSNODE);
 304
 305        return (0);
 306}
 307
 308/*
 309 * File attributes
 310 */
 311void
 312cd9660_defattr(struct iso_directory_record *isodir, struct iso_node *inop,
 313                struct buf *bp)
 314{
 315        struct buf *bp2 = NULL;
 316        struct iso_mnt *imp;
 317        struct iso_extended_attributes *ap = NULL;
 318        int off;
 319        
 320        if ( isonum_711(isodir->flags) & directoryBit ) {
 321                inop->inode.iso_mode = S_IFDIR;
 322                /*
 323                 * If we return 2, fts() will assume there are no subdirectories
 324                 * (just links for the path and .), so instead we return 1.
 325                 */
 326                inop->inode.iso_links = 1;
 327        } else {
 328                inop->inode.iso_mode = S_IFREG;
 329                inop->inode.iso_links = 1;
 330        }
 331        if (!bp
 332            && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
 333            && (off = isonum_711(isodir->ext_attr_length))) {
 334                cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, &bp2);
 335                bp = bp2;
 336        }
 337        if (bp) {
 338                ap = (struct iso_extended_attributes *)buf_dataptr(bp);
 339                
 340                if (isonum_711(ap->version) == 1) {
 341                        if (!(ap->perm[0]&0x40))
 342                                inop->inode.iso_mode |= VEXEC >> 6;
 343                        if (!(ap->perm[0]&0x10))
 344                                inop->inode.iso_mode |= VREAD >> 6;
 345                        if (!(ap->perm[0]&4))
 346                                inop->inode.iso_mode |= VEXEC >> 3;
 347                        if (!(ap->perm[0]&1))
 348                                inop->inode.iso_mode |= VREAD >> 3;
 349                        if (!(ap->perm[1]&0x40))
 350                                inop->inode.iso_mode |= VEXEC;
 351                        if (!(ap->perm[1]&0x10))
 352                                inop->inode.iso_mode |= VREAD;
 353                        inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
 354                        inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
 355                } else
 356                        ap = NULL;
 357        }
 358        if (!ap) {
 359                inop->inode.iso_mode |= VREAD|VWRITE|VEXEC|(VREAD|VEXEC)>>3|(VREAD|VEXEC)>>6;
 360                inop->inode.iso_uid = ISO_UNKNOWNUID;
 361                inop->inode.iso_gid = ISO_UNKNOWNGID;
 362        }
 363        if (bp2)
 364                buf_brelse(bp2);
 365}
 366
 367/*
 368 * Time stamps
 369 */
 370void
 371cd9660_deftstamp(struct iso_directory_record *isodir, struct iso_node *inop,
 372                struct buf *bp)
 373{
 374        struct buf *bp2 = NULL;
 375        struct iso_mnt *imp;
 376        struct iso_extended_attributes *ap = NULL;
 377        int off;
 378        
 379        if (!bp
 380            && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
 381            && (off = isonum_711(isodir->ext_attr_length))) 
 382        {
 383                cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL, &bp2);
 384                bp = bp2;
 385        }
 386        if (bp) {
 387                ap = (struct iso_extended_attributes *)buf_dataptr(bp);
 388                
 389                if (isonum_711(ap->version) == 1) {
 390                        if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
 391                                cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
 392                        if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
 393                                inop->inode.iso_ctime = inop->inode.iso_atime;
 394                        if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
 395                                inop->inode.iso_mtime = inop->inode.iso_ctime;
 396                } else
 397                        ap = NULL;
 398        }
 399        if (!ap) {
 400                cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
 401                inop->inode.iso_atime = inop->inode.iso_ctime;
 402                inop->inode.iso_mtime = inop->inode.iso_ctime;
 403        }
 404        if (bp2)
 405                buf_brelse(bp2);
 406}
 407
 408int
 409cd9660_tstamp_conv7(u_char *pi, struct timespec *pu)
 410{
 411        int crtime, days;
 412        int y, m, d, hour, minute, second, mytz;
 413        
 414        y = pi[0] + 1900;
 415        m = pi[1];
 416        d = pi[2];
 417        hour = pi[3];
 418        minute = pi[4];
 419        second = pi[5];
 420        mytz = pi[6];
 421        
 422        if (y < 1970) {
 423                pu->tv_sec  = 0;
 424                pu->tv_nsec = 0;
 425                return 0;
 426        } else {
 427#ifdef  ORIGINAL
 428                /* computes day number relative to Sept. 19th,1989 */
 429                /* don't even *THINK* about changing formula. It works! */
 430                days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
 431#else
 432                /*
 433                 * Changed :-) to make it relative to Jan. 1st, 1970
 434                 * and to disambiguate negative division
 435                 */
 436                days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
 437#endif
 438                crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
 439                
 440                /* timezone offset is unreliable on some disks */
 441                if (-48 <= mytz && mytz <= 52)
 442                        crtime -= mytz * 15 * 60;
 443        }
 444        pu->tv_sec  = crtime;
 445        pu->tv_nsec = 0;
 446        return 1;
 447}
 448
 449static u_int
 450cd9660_chars2ui(u_char *begin, int len)
 451{
 452        u_int rc;
 453        
 454        for (rc = 0; --len >= 0;) {
 455                rc *= 10;
 456                rc += *begin++ - '0';
 457        }
 458        return rc;
 459}
 460
 461int
 462cd9660_tstamp_conv17(u_char *pi, struct timespec *pu)
 463{
 464        u_char buf[7];
 465        
 466        /* year:"0001"-"9999" -> -1900  */
 467        buf[0] = cd9660_chars2ui(pi,4) - 1900;
 468        
 469        /* month: " 1"-"12"      -> 1 - 12 */
 470        buf[1] = cd9660_chars2ui(pi + 4,2);
 471        
 472        /* day:   " 1"-"31"      -> 1 - 31 */
 473        buf[2] = cd9660_chars2ui(pi + 6,2);
 474        
 475        /* hour:  " 0"-"23"      -> 0 - 23 */
 476        buf[3] = cd9660_chars2ui(pi + 8,2);
 477        
 478        /* minute:" 0"-"59"      -> 0 - 59 */
 479        buf[4] = cd9660_chars2ui(pi + 10,2);
 480        
 481        /* second:" 0"-"59"      -> 0 - 59 */
 482        buf[5] = cd9660_chars2ui(pi + 12,2);
 483        
 484        /* difference of GMT */
 485        buf[6] = pi[16];
 486        
 487        return cd9660_tstamp_conv7(buf,pu);
 488}
 489
 490ino_t
 491isodirino(struct iso_directory_record *isodir, struct iso_mnt *imp)
 492{
 493        ino_t ino;
 494
 495        ino = (isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length))
 496              << imp->im_bshift;
 497        return (ino);
 498}
 499
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.