xref: /freebsd/sys/fs/cd9660/cd9660_node.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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 	} */ *ap;
67 {
68 	struct vnode *vp = ap->a_vp;
69 	struct iso_node *ip = VTOI(vp);
70 	int error = 0;
71 
72 	/*
73 	 * If we are done with the inode, reclaim it
74 	 * so that it can be reused immediately.
75 	 */
76 	if (ip->inode.iso_mode == 0)
77 		vrecycle(vp);
78 	return error;
79 }
80 
81 /*
82  * Reclaim an inode so that it can be used for other purposes.
83  */
84 int
85 cd9660_reclaim(ap)
86 	struct vop_reclaim_args /* {
87 		struct vnode *a_vp;
88 	} */ *ap;
89 {
90 	struct vnode *vp = ap->a_vp;
91 
92 	/*
93 	 * Remove the inode from its hash chain.
94 	 */
95 	vfs_hash_remove(vp);
96 
97 	/*
98 	 * Purge old data structures associated with the inode.
99 	 */
100 	free(vp->v_data, M_ISOFSNODE);
101 	vp->v_data = NULL;
102 	return (0);
103 }
104 
105 /*
106  * File attributes
107  */
108 void
109 cd9660_defattr(isodir, inop, bp, ftype)
110 	struct iso_directory_record *isodir;
111 	struct iso_node *inop;
112 	struct buf *bp;
113 	enum ISO_FTYPE ftype;
114 {
115 	struct buf *bp2 = NULL;
116 	struct iso_mnt *imp;
117 	struct iso_extended_attributes *ap = NULL;
118 	int off;
119 
120 	/* high sierra does not have timezone data, flag is one byte ahead */
121 	if (isonum_711(ftype == ISO_FTYPE_HIGH_SIERRA?
122 		       &isodir->date[6]: isodir->flags)&2) {
123 		inop->inode.iso_mode = S_IFDIR;
124 		/*
125 		 * If we return 2, fts() will assume there are no subdirectories
126 		 * (just links for the path and .), so instead we return 1.
127 		 */
128 		inop->inode.iso_links = 1;
129 	} else {
130 		inop->inode.iso_mode = S_IFREG;
131 		inop->inode.iso_links = 1;
132 	}
133 	if (!bp
134 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
135 	    && (off = isonum_711(isodir->ext_attr_length))) {
136 		cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
137 			     &bp2);
138 		bp = bp2;
139 	}
140 	if (bp) {
141 		ap = (struct iso_extended_attributes *)bp->b_data;
142 
143 		if (isonum_711(ap->version) == 1) {
144 			if (!(ap->perm[0]&0x40))
145 				inop->inode.iso_mode |= S_IXOTH;
146 			if (!(ap->perm[0]&0x10))
147 				inop->inode.iso_mode |= S_IROTH;
148 			if (!(ap->perm[0]&4))
149 				inop->inode.iso_mode |= S_IXGRP;
150 			if (!(ap->perm[0]&1))
151 				inop->inode.iso_mode |= S_IRGRP;
152 			if (!(ap->perm[1]&0x40))
153 				inop->inode.iso_mode |= S_IXUSR;
154 			if (!(ap->perm[1]&0x10))
155 				inop->inode.iso_mode |= S_IRUSR;
156 			inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
157 			inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
158 		} else
159 			ap = NULL;
160 	}
161 	if (!ap) {
162 		inop->inode.iso_mode |= S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
163 		inop->inode.iso_uid = (uid_t)0;
164 		inop->inode.iso_gid = (gid_t)0;
165 	}
166 	if (bp2)
167 		brelse(bp2);
168 }
169 
170 /*
171  * Time stamps
172  */
173 void
174 cd9660_deftstamp(isodir,inop,bp,ftype)
175 	struct iso_directory_record *isodir;
176 	struct iso_node *inop;
177 	struct buf *bp;
178 	enum ISO_FTYPE ftype;
179 {
180 	struct buf *bp2 = NULL;
181 	struct iso_mnt *imp;
182 	struct iso_extended_attributes *ap = NULL;
183 	int off;
184 
185 	if (!bp
186 	    && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
187 	    && (off = isonum_711(isodir->ext_attr_length))) {
188 		cd9660_blkatoff(ITOV(inop), (off_t)-(off << imp->im_bshift), NULL,
189 			     &bp2);
190 		bp = bp2;
191 	}
192 	if (bp) {
193 		ap = (struct iso_extended_attributes *)bp->b_data;
194 
195 		if (ftype != ISO_FTYPE_HIGH_SIERRA
196 		    && isonum_711(ap->version) == 1) {
197 			if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
198 				cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
199 			if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
200 				inop->inode.iso_ctime = inop->inode.iso_atime;
201 			if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
202 				inop->inode.iso_mtime = inop->inode.iso_ctime;
203 		} else
204 			ap = NULL;
205 	}
206 	if (!ap) {
207 		cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime,ftype);
208 		inop->inode.iso_atime = inop->inode.iso_ctime;
209 		inop->inode.iso_mtime = inop->inode.iso_ctime;
210 	}
211 	if (bp2)
212 		brelse(bp2);
213 }
214 
215 int
216 cd9660_tstamp_conv7(pi,pu,ftype)
217 	u_char *pi;
218 	struct timespec *pu;
219 	enum ISO_FTYPE ftype;
220 {
221 	int crtime, days;
222 	int y, m, d, hour, minute, second, tz;
223 
224 	y = pi[0] + 1900;
225 	m = pi[1];
226 	d = pi[2];
227 	hour = pi[3];
228 	minute = pi[4];
229 	second = pi[5];
230 	if(ftype != ISO_FTYPE_HIGH_SIERRA)
231 		tz = ((signed char *)pi)[6]; /* Timezone value is signed. */
232 	else
233 		/* original high sierra misses timezone data */
234 		tz = 0;
235 
236 	if (y < 1970) {
237 		pu->tv_sec  = 0;
238 		pu->tv_nsec = 0;
239 		return 0;
240 	} else {
241 #ifdef	ORIGINAL
242 		/* computes day number relative to Sept. 19th,1989 */
243 		/* don't even *THINK* about changing formula. It works! */
244 		days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
245 #else
246 		/*
247 		 * Changed :-) to make it relative to Jan. 1st, 1970
248 		 * and to disambiguate negative division
249 		 */
250 		days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
251 #endif
252 		crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
253 
254 		/* timezone offset is unreliable on some disks */
255 		if (-48 <= tz && tz <= 52)
256 			crtime -= tz * 15 * 60;
257 	}
258 	pu->tv_sec  = crtime;
259 	pu->tv_nsec = 0;
260 	return 1;
261 }
262 
263 static u_int
264 cd9660_chars2ui(begin,len)
265 	u_char *begin;
266 	int len;
267 {
268 	u_int rc;
269 
270 	for (rc = 0; --len >= 0;) {
271 		rc *= 10;
272 		rc += *begin++ - '0';
273 	}
274 	return rc;
275 }
276 
277 int
278 cd9660_tstamp_conv17(pi,pu)
279 	u_char *pi;
280 	struct timespec *pu;
281 {
282 	u_char buf[7];
283 
284 	/* year:"0001"-"9999" -> -1900  */
285 	buf[0] = cd9660_chars2ui(pi,4) - 1900;
286 
287 	/* month: " 1"-"12"   -> 1 - 12 */
288 	buf[1] = cd9660_chars2ui(pi + 4,2);
289 
290 	/* day:	  " 1"-"31"   -> 1 - 31 */
291 	buf[2] = cd9660_chars2ui(pi + 6,2);
292 
293 	/* hour:  " 0"-"23"   -> 0 - 23 */
294 	buf[3] = cd9660_chars2ui(pi + 8,2);
295 
296 	/* minute:" 0"-"59"   -> 0 - 59 */
297 	buf[4] = cd9660_chars2ui(pi + 10,2);
298 
299 	/* second:" 0"-"59"   -> 0 - 59 */
300 	buf[5] = cd9660_chars2ui(pi + 12,2);
301 
302 	/* difference of GMT */
303 	buf[6] = pi[16];
304 
305 	return cd9660_tstamp_conv7(buf, pu, ISO_FTYPE_DEFAULT);
306 }
307 
308 cd_ino_t
309 isodirino(isodir, imp)
310 	struct iso_directory_record *isodir;
311 	struct iso_mnt *imp;
312 {
313 	cd_ino_t ino;
314 
315 	/*
316 	 * Note there is an inverse calculation in
317 	 * cd9660_vfsops.c:cd9660_vget_internal():
318 	 *   ip->iso_start = ino >> imp->im_bshift;
319 	 * and also a calculation of the isodir pointer
320 	 * from an inode in cd9660_vnops.c:cd9660_readlink()
321 	 */
322 	ino = ((cd_ino_t)isonum_733(isodir->extent) +
323 		isonum_711(isodir->ext_attr_length)) << imp->im_bshift;
324 	return ino;
325 }
326