xref: /freebsd/sys/fs/cd9660/cd9660_node.c (revision 96e69c8e3167a57b8b37317796eba3068d12a771)
1df8bae1dSRodney W. Grimes /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4996c772fSJohn Dyson  * Copyright (c) 1982, 1986, 1989, 1994, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley
8df8bae1dSRodney W. Grimes  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9df8bae1dSRodney W. Grimes  * Support code is derived from software contributed to Berkeley
10df8bae1dSRodney W. Grimes  * by Atsushi Murai (amurai@spec.co.jp).
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/mount.h>
409626b608SPoul-Henning Kamp #include <sys/bio.h>
41df8bae1dSRodney W. Grimes #include <sys/buf.h>
42df8bae1dSRodney W. Grimes #include <sys/vnode.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/stat.h>
451b367556SJason Evans #include <sys/mutex.h>
46a18b1f1dSJason Evans 
47a8d36d0dSCraig Rodrigues #include <fs/cd9660/iso.h>
48a8d36d0dSCraig Rodrigues #include <fs/cd9660/cd9660_node.h>
49a8d36d0dSCraig Rodrigues #include <fs/cd9660/cd9660_mount.h>
50df8bae1dSRodney W. Grimes 
5189c9a483SAlfred Perlstein static unsigned	cd9660_chars2ui(unsigned char *begin, int len);
5210dd32cdSBruce Evans 
53df8bae1dSRodney W. Grimes /*
54df8bae1dSRodney W. Grimes  * Last reference to an inode, write the inode out and if necessary,
55df8bae1dSRodney W. Grimes  * truncate and deallocate the file.
56df8bae1dSRodney W. Grimes  */
57df8bae1dSRodney W. Grimes int
cd9660_inactive(struct vop_inactive_args * ap)58a5f59e85SEd Maste cd9660_inactive(struct vop_inactive_args *ap)
59df8bae1dSRodney W. Grimes {
60df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
61bffd1b7aSPoul-Henning Kamp 	struct iso_node *ip = VTOI(vp);
621295d82eSGary Palmer 	int error = 0;
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes 	/*
65df8bae1dSRodney W. Grimes 	 * If we are done with the inode, reclaim it
66df8bae1dSRodney W. Grimes 	 * so that it can be reused immediately.
67df8bae1dSRodney W. Grimes 	 */
68996c772fSJohn Dyson 	if (ip->inode.iso_mode == 0)
69af6e6b87SEdward Tomasz Napierala 		vrecycle(vp);
70df8bae1dSRodney W. Grimes 	return error;
71df8bae1dSRodney W. Grimes }
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  * Reclaim an inode so that it can be used for other purposes.
75df8bae1dSRodney W. Grimes  */
76df8bae1dSRodney W. Grimes int
cd9660_reclaim(struct vop_reclaim_args * ap)77a5f59e85SEd Maste cd9660_reclaim(struct vop_reclaim_args *ap)
78df8bae1dSRodney W. Grimes {
79bffd1b7aSPoul-Henning Kamp 	struct vnode *vp = ap->a_vp;
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes 	/*
82df8bae1dSRodney W. Grimes 	 * Remove the inode from its hash chain.
83df8bae1dSRodney W. Grimes 	 */
84dfb9f846SPoul-Henning Kamp 	vfs_hash_remove(vp);
85dfb9f846SPoul-Henning Kamp 
86df8bae1dSRodney W. Grimes 	/*
87df8bae1dSRodney W. Grimes 	 * Purge old data structures associated with the inode.
88df8bae1dSRodney W. Grimes 	 */
891ede983cSDag-Erling Smørgrav 	free(vp->v_data, M_ISOFSNODE);
90df8bae1dSRodney W. Grimes 	vp->v_data = NULL;
9126f9a767SRodney W. Grimes 	return (0);
92df8bae1dSRodney W. Grimes }
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes /*
95df8bae1dSRodney W. Grimes  * File attributes
96df8bae1dSRodney W. Grimes  */
97df8bae1dSRodney W. Grimes void
cd9660_defattr(struct iso_directory_record * isodir,struct iso_node * inop,struct buf * bp,enum ISO_FTYPE ftype)98a5f59e85SEd Maste cd9660_defattr(struct iso_directory_record *isodir, struct iso_node *inop,
99a5f59e85SEd Maste     struct buf *bp, enum ISO_FTYPE ftype)
100df8bae1dSRodney W. Grimes {
101df8bae1dSRodney W. Grimes 	struct buf *bp2 = NULL;
102df8bae1dSRodney W. Grimes 	struct iso_mnt *imp;
103df8bae1dSRodney W. Grimes 	struct iso_extended_attributes *ap = NULL;
104df8bae1dSRodney W. Grimes 	int off;
105df8bae1dSRodney W. Grimes 
106988fa8efSJoerg Wunsch 	/* high sierra does not have timezone data, flag is one byte ahead */
107988fa8efSJoerg Wunsch 	if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA?
108988fa8efSJoerg Wunsch 		       &isodir->date[6]: isodir->flags)&2) {
109df8bae1dSRodney W. Grimes 		inop->inode.iso_mode = S_IFDIR;
110df8bae1dSRodney W. Grimes 		/*
111df8bae1dSRodney W. Grimes 		 * If we return 2, fts() will assume there are no subdirectories
112df8bae1dSRodney W. Grimes 		 * (just links for the path and .), so instead we return 1.
113df8bae1dSRodney W. Grimes 		 */
114df8bae1dSRodney W. Grimes 		inop->inode.iso_links = 1;
115df8bae1dSRodney W. Grimes 	} else {
116df8bae1dSRodney W. Grimes 		inop->inode.iso_mode = S_IFREG;
117df8bae1dSRodney W. Grimes 		inop->inode.iso_links = 1;
118df8bae1dSRodney W. Grimes 	}
119df8bae1dSRodney W. Grimes 	if (!bp
120df8bae1dSRodney W. Grimes 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
121df8bae1dSRodney W. Grimes 	    && (off = isonum_711(isodir->ext_attr_length))) {
122cec0f20cSPoul-Henning Kamp 		cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
123996c772fSJohn Dyson 			     &bp2);
124df8bae1dSRodney W. Grimes 		bp = bp2;
125df8bae1dSRodney W. Grimes 	}
126df8bae1dSRodney W. Grimes 	if (bp) {
127996c772fSJohn Dyson 		ap = (struct iso_extended_attributes *)bp->b_data;
128df8bae1dSRodney W. Grimes 
129df8bae1dSRodney W. Grimes 		if (isonum_711(ap->version) == 1) {
130df8bae1dSRodney W. Grimes 			if (!(ap->perm[0]&0x40))
131464119c4SEdward Tomasz Napierala 				inop->inode.iso_mode |= S_IXOTH;
132df8bae1dSRodney W. Grimes 			if (!(ap->perm[0]&0x10))
133464119c4SEdward Tomasz Napierala 				inop->inode.iso_mode |= S_IROTH;
134df8bae1dSRodney W. Grimes 			if (!(ap->perm[0]&4))
135464119c4SEdward Tomasz Napierala 				inop->inode.iso_mode |= S_IXGRP;
136df8bae1dSRodney W. Grimes 			if (!(ap->perm[0]&1))
137464119c4SEdward Tomasz Napierala 				inop->inode.iso_mode |= S_IRGRP;
138df8bae1dSRodney W. Grimes 			if (!(ap->perm[1]&0x40))
139464119c4SEdward Tomasz Napierala 				inop->inode.iso_mode |= S_IXUSR;
140df8bae1dSRodney W. Grimes 			if (!(ap->perm[1]&0x10))
141464119c4SEdward Tomasz Napierala 				inop->inode.iso_mode |= S_IRUSR;
142df8bae1dSRodney W. Grimes 			inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
143df8bae1dSRodney W. Grimes 			inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
144df8bae1dSRodney W. Grimes 		} else
145df8bae1dSRodney W. Grimes 			ap = NULL;
146df8bae1dSRodney W. Grimes 	}
147df8bae1dSRodney W. Grimes 	if (!ap) {
148464119c4SEdward Tomasz Napierala 		inop->inode.iso_mode |= S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
149df8bae1dSRodney W. Grimes 		inop->inode.iso_uid = (uid_t)0;
150df8bae1dSRodney W. Grimes 		inop->inode.iso_gid = (gid_t)0;
151df8bae1dSRodney W. Grimes 	}
152df8bae1dSRodney W. Grimes 	if (bp2)
153df8bae1dSRodney W. Grimes 		brelse(bp2);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes /*
157df8bae1dSRodney W. Grimes  * Time stamps
158df8bae1dSRodney W. Grimes  */
159df8bae1dSRodney W. Grimes void
cd9660_deftstamp(struct iso_directory_record * isodir,struct iso_node * inop,struct buf * bp,enum ISO_FTYPE ftype)160a5f59e85SEd Maste cd9660_deftstamp(struct iso_directory_record *isodir, struct iso_node *inop,
161a5f59e85SEd Maste     struct buf *bp, enum ISO_FTYPE ftype)
162df8bae1dSRodney W. Grimes {
163df8bae1dSRodney W. Grimes 	struct buf *bp2 = NULL;
164df8bae1dSRodney W. Grimes 	struct iso_mnt *imp;
165df8bae1dSRodney W. Grimes 	struct iso_extended_attributes *ap = NULL;
166df8bae1dSRodney W. Grimes 	int off;
167df8bae1dSRodney W. Grimes 
168df8bae1dSRodney W. Grimes 	if (!bp
169df8bae1dSRodney W. Grimes 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
170df8bae1dSRodney W. Grimes 	    && (off = isonum_711(isodir->ext_attr_length))) {
171cec0f20cSPoul-Henning Kamp 		cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
172996c772fSJohn Dyson 			     &bp2);
173df8bae1dSRodney W. Grimes 		bp = bp2;
174df8bae1dSRodney W. Grimes 	}
175df8bae1dSRodney W. Grimes 	if (bp) {
176996c772fSJohn Dyson 		ap = (struct iso_extended_attributes *)bp->b_data;
177df8bae1dSRodney W. Grimes 
178596d40b9SBruce Evans 		if (ftype != ISO_FTYPE_HIGH_SIERRA
179596d40b9SBruce Evans 		    && isonum_711(ap->version) == 1) {
180df8bae1dSRodney W. Grimes 			if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
181df8bae1dSRodney W. Grimes 				cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
182df8bae1dSRodney W. Grimes 			if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
183df8bae1dSRodney W. Grimes 				inop->inode.iso_ctime = inop->inode.iso_atime;
184df8bae1dSRodney W. Grimes 			if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
185df8bae1dSRodney W. Grimes 				inop->inode.iso_mtime = inop->inode.iso_ctime;
186df8bae1dSRodney W. Grimes 		} else
187df8bae1dSRodney W. Grimes 			ap = NULL;
188df8bae1dSRodney W. Grimes 	}
189df8bae1dSRodney W. Grimes 	if (!ap) {
190988fa8efSJoerg Wunsch 		cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime,ftype);
191df8bae1dSRodney W. Grimes 		inop->inode.iso_atime = inop->inode.iso_ctime;
192df8bae1dSRodney W. Grimes 		inop->inode.iso_mtime = inop->inode.iso_ctime;
193df8bae1dSRodney W. Grimes 	}
194df8bae1dSRodney W. Grimes 	if (bp2)
195df8bae1dSRodney W. Grimes 		brelse(bp2);
196df8bae1dSRodney W. Grimes }
197df8bae1dSRodney W. Grimes 
198df8bae1dSRodney W. Grimes int
cd9660_tstamp_conv7(u_char * pi,struct timespec * pu,enum ISO_FTYPE ftype)199a5f59e85SEd Maste cd9660_tstamp_conv7(u_char *pi, struct timespec *pu, enum ISO_FTYPE ftype)
200df8bae1dSRodney W. Grimes {
201df8bae1dSRodney W. Grimes 	int crtime, days;
202df8bae1dSRodney W. Grimes 	int y, m, d, hour, minute, second, tz;
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes 	y = pi[0] + 1900;
205df8bae1dSRodney W. Grimes 	m = pi[1];
206df8bae1dSRodney W. Grimes 	d = pi[2];
207df8bae1dSRodney W. Grimes 	hour = pi[3];
208df8bae1dSRodney W. Grimes 	minute = pi[4];
209df8bae1dSRodney W. Grimes 	second = pi[5];
210988fa8efSJoerg Wunsch 	if(ftype != ISO_FTYPE_HIGH_SIERRA)
2115c423e06STim Kientzle 		tz = ((signed char *)pi)[6]; /* Timezone value is signed. */
212988fa8efSJoerg Wunsch 	else
213988fa8efSJoerg Wunsch 		/* original high sierra misses timezone data */
214988fa8efSJoerg Wunsch 		tz = 0;
215df8bae1dSRodney W. Grimes 
216df8bae1dSRodney W. Grimes 	if (y < 1970) {
21795a1574eSNate Williams 		pu->tv_sec  = 0;
21895a1574eSNate Williams 		pu->tv_nsec = 0;
219df8bae1dSRodney W. Grimes 		return 0;
220df8bae1dSRodney W. Grimes 	} else {
221df8bae1dSRodney W. Grimes #ifdef	ORIGINAL
222df8bae1dSRodney W. Grimes 		/* computes day number relative to Sept. 19th,1989 */
223df8bae1dSRodney W. Grimes 		/* don't even *THINK* about changing formula. It works! */
224df8bae1dSRodney 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;
225df8bae1dSRodney W. Grimes #else
226df8bae1dSRodney W. Grimes 		/*
227df8bae1dSRodney W. Grimes 		 * Changed :-) to make it relative to Jan. 1st, 1970
228df8bae1dSRodney W. Grimes 		 * and to disambiguate negative division
229df8bae1dSRodney W. Grimes 		 */
230df8bae1dSRodney 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;
231df8bae1dSRodney W. Grimes #endif
232df8bae1dSRodney W. Grimes 		crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
233df8bae1dSRodney W. Grimes 
234df8bae1dSRodney W. Grimes 		/* timezone offset is unreliable on some disks */
235df8bae1dSRodney W. Grimes 		if (-48 <= tz && tz <= 52)
2362d091ecfSBruce Evans 			crtime -= tz * 15 * 60;
237df8bae1dSRodney W. Grimes 	}
23895a1574eSNate Williams 	pu->tv_sec  = crtime;
23995a1574eSNate Williams 	pu->tv_nsec = 0;
240df8bae1dSRodney W. Grimes 	return 1;
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243996c772fSJohn Dyson static u_int
cd9660_chars2ui(u_char * begin,int len)244a5f59e85SEd Maste cd9660_chars2ui(u_char *begin, int len)
245df8bae1dSRodney W. Grimes {
246996c772fSJohn Dyson 	u_int rc;
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes 	for (rc = 0; --len >= 0;) {
249df8bae1dSRodney W. Grimes 		rc *= 10;
250df8bae1dSRodney W. Grimes 		rc += *begin++ - '0';
251df8bae1dSRodney W. Grimes 	}
252df8bae1dSRodney W. Grimes 	return rc;
253df8bae1dSRodney W. Grimes }
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes int
cd9660_tstamp_conv17(u_char * pi,struct timespec * pu)256a5f59e85SEd Maste cd9660_tstamp_conv17(u_char *pi, struct timespec *pu)
257df8bae1dSRodney W. Grimes {
258996c772fSJohn Dyson 	u_char buf[7];
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes 	/* year:"0001"-"9999" -> -1900  */
261df8bae1dSRodney W. Grimes 	buf[0] = cd9660_chars2ui(pi,4) - 1900;
262df8bae1dSRodney W. Grimes 
263df8bae1dSRodney W. Grimes 	/* month: " 1"-"12"   -> 1 - 12 */
264df8bae1dSRodney W. Grimes 	buf[1] = cd9660_chars2ui(pi + 4,2);
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes 	/* day:	  " 1"-"31"   -> 1 - 31 */
267df8bae1dSRodney W. Grimes 	buf[2] = cd9660_chars2ui(pi + 6,2);
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes 	/* hour:  " 0"-"23"   -> 0 - 23 */
270df8bae1dSRodney W. Grimes 	buf[3] = cd9660_chars2ui(pi + 8,2);
271df8bae1dSRodney W. Grimes 
272df8bae1dSRodney W. Grimes 	/* minute:" 0"-"59"   -> 0 - 59 */
273df8bae1dSRodney W. Grimes 	buf[4] = cd9660_chars2ui(pi + 10,2);
274df8bae1dSRodney W. Grimes 
275df8bae1dSRodney W. Grimes 	/* second:" 0"-"59"   -> 0 - 59 */
276df8bae1dSRodney W. Grimes 	buf[5] = cd9660_chars2ui(pi + 12,2);
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes 	/* difference of GMT */
279df8bae1dSRodney W. Grimes 	buf[6] = pi[16];
280df8bae1dSRodney W. Grimes 
281988fa8efSJoerg Wunsch 	return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT);
282df8bae1dSRodney W. Grimes }
283df8bae1dSRodney W. Grimes 
284*96e69c8eSMark Johnston ino_t
isodirino(struct iso_directory_record * isodir,struct iso_mnt * imp)285a5f59e85SEd Maste isodirino(struct iso_directory_record *isodir, struct iso_mnt *imp)
286df8bae1dSRodney W. Grimes {
287*96e69c8eSMark Johnston 	ino_t ino;
288996c772fSJohn Dyson 
289c8227de2SConrad Meyer 	/*
290c8227de2SConrad Meyer 	 * Note there is an inverse calculation in
291c8227de2SConrad Meyer 	 * cd9660_vfsops.c:cd9660_vget_internal():
292c8227de2SConrad Meyer 	 *   ip->iso_start = ino >> imp->im_bshift;
293c8227de2SConrad Meyer 	 * and also a calculation of the isodir pointer
294c8227de2SConrad Meyer 	 * from an inode in cd9660_vnops.c:cd9660_readlink()
295c8227de2SConrad Meyer 	 */
296*96e69c8eSMark Johnston 	ino = ((ino_t)isonum_733(isodir->extent) +
297c8227de2SConrad Meyer 		isonum_711(isodir->ext_attr_length)) << imp->im_bshift;
298c8227de2SConrad Meyer 	return ino;
299df8bae1dSRodney W. Grimes }
300