cd9660_node.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* $OpenBSD: cd9660_node.c,v 1.29 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: cd9660_node.c,v 1.17 1997/05/05 07:13:57 mycroft Exp $ */
  3. /*-
  4. * Copyright (c) 1982, 1986, 1989, 1994
  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.5 (Berkeley) 12/5/94
  37. */
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/mount.h>
  41. #include <sys/file.h>
  42. #include <sys/buf.h>
  43. #include <sys/vnode.h>
  44. #include <sys/lock.h>
  45. #include <sys/namei.h>
  46. #include <sys/kernel.h>
  47. #include <sys/malloc.h>
  48. #include <sys/stat.h>
  49. #include <crypto/siphash.h>
  50. #include <isofs/cd9660/iso.h>
  51. #include <isofs/cd9660/cd9660_extern.h>
  52. #include <isofs/cd9660/cd9660_node.h>
  53. /*
  54. * Structures associated with iso_node caching.
  55. */
  56. u_int cd9660_isohash(dev_t, cdino_t);
  57. struct iso_node **isohashtbl;
  58. u_long isohash;
  59. SIPHASH_KEY isohashkey;
  60. #define INOHASH(device, inum) cd9660_isohash((device), (inum))
  61. extern int prtactive; /* 1 => print out reclaim of active vnodes */
  62. static u_int cd9660_chars2ui(u_char *, int);
  63. /*
  64. * Initialize hash links for inodes and dnodes.
  65. */
  66. int
  67. cd9660_init(vfsp)
  68. struct vfsconf *vfsp;
  69. {
  70. isohashtbl = hashinit(initialvnodes, M_ISOFSMNT, M_WAITOK, &isohash);
  71. arc4random_buf(&isohashkey, sizeof(isohashkey));
  72. return (0);
  73. }
  74. u_int
  75. cd9660_isohash(dev_t device, cdino_t inum)
  76. {
  77. SIPHASH_CTX ctx;
  78. SipHash24_Init(&ctx, &isohashkey);
  79. SipHash24_Update(&ctx, &device, sizeof(device));
  80. SipHash24_Update(&ctx, &inum, sizeof(inum));
  81. return (SipHash24_End(&ctx) & isohash);
  82. }
  83. /*
  84. * Use the device/inum pair to find the incore inode, and return a pointer
  85. * to it. If it is in core, but locked, wait for it.
  86. */
  87. struct vnode *
  88. cd9660_ihashget(dev, inum)
  89. dev_t dev;
  90. cdino_t inum;
  91. {
  92. struct proc *p = curproc; /* XXX */
  93. struct iso_node *ip;
  94. struct vnode *vp;
  95. loop:
  96. /* XXX locking lock hash list? */
  97. for (ip = isohashtbl[INOHASH(dev, inum)]; ip; ip = ip->i_next) {
  98. if (inum == ip->i_number && dev == ip->i_dev) {
  99. vp = ITOV(ip);
  100. /* XXX locking unlock hash list? */
  101. if (vget(vp, LK_EXCLUSIVE, p))
  102. goto loop;
  103. return (vp);
  104. }
  105. }
  106. /* XXX locking unlock hash list? */
  107. return (NULL);
  108. }
  109. /*
  110. * Insert the inode into the hash table, and return it locked.
  111. */
  112. int
  113. cd9660_ihashins(ip)
  114. struct iso_node *ip;
  115. {
  116. struct iso_node **ipp, *iq;
  117. /* XXX locking lock hash list? */
  118. ipp = &isohashtbl[INOHASH(ip->i_dev, ip->i_number)];
  119. for (iq = *ipp; iq; iq = iq->i_next) {
  120. if (iq->i_dev == ip->i_dev &&
  121. iq->i_number == ip->i_number)
  122. return (EEXIST);
  123. }
  124. if ((iq = *ipp) != NULL)
  125. iq->i_prev = &ip->i_next;
  126. ip->i_next = iq;
  127. ip->i_prev = ipp;
  128. *ipp = ip;
  129. /* XXX locking unlock hash list? */
  130. lockmgr(&ip->i_lock, LK_EXCLUSIVE, NULL);
  131. return (0);
  132. }
  133. /*
  134. * Remove the inode from the hash table.
  135. */
  136. void
  137. cd9660_ihashrem(ip)
  138. register struct iso_node *ip;
  139. {
  140. register struct iso_node *iq;
  141. if (ip->i_prev == NULL)
  142. return;
  143. /* XXX locking lock hash list? */
  144. if ((iq = ip->i_next) != NULL)
  145. iq->i_prev = ip->i_prev;
  146. *ip->i_prev = iq;
  147. #ifdef DIAGNOSTIC
  148. ip->i_next = NULL;
  149. ip->i_prev = NULL;
  150. #endif
  151. /* XXX locking unlock hash list? */
  152. }
  153. /*
  154. * Last reference to an inode, write the inode out and if necessary,
  155. * truncate and deallocate the file.
  156. */
  157. int
  158. cd9660_inactive(v)
  159. void *v;
  160. {
  161. struct vop_inactive_args *ap = v;
  162. struct vnode *vp = ap->a_vp;
  163. struct proc *p = ap->a_p;
  164. register struct iso_node *ip = VTOI(vp);
  165. int error = 0;
  166. #ifdef DIAGNOSTIC
  167. if (prtactive && vp->v_usecount != 0)
  168. vprint("cd9660_inactive: pushing active", vp);
  169. #endif
  170. ip->i_flag = 0;
  171. VOP_UNLOCK(vp, 0, p);
  172. /*
  173. * If we are done with the inode, reclaim it
  174. * so that it can be reused immediately.
  175. */
  176. if (ip->inode.iso_mode == 0)
  177. vrecycle(vp, p);
  178. return (error);
  179. }
  180. /*
  181. * Reclaim an inode so that it can be used for other purposes.
  182. */
  183. int
  184. cd9660_reclaim(v)
  185. void *v;
  186. {
  187. struct vop_reclaim_args *ap = v;
  188. register struct vnode *vp = ap->a_vp;
  189. register struct iso_node *ip = VTOI(vp);
  190. #ifdef DIAGNOSTIC
  191. if (prtactive && vp->v_usecount != 0)
  192. vprint("cd9660_reclaim: pushing active", vp);
  193. #endif
  194. /*
  195. * Remove the inode from its hash chain.
  196. */
  197. cd9660_ihashrem(ip);
  198. /*
  199. * Purge old data structures associated with the inode.
  200. */
  201. cache_purge(vp);
  202. if (ip->i_devvp) {
  203. vrele(ip->i_devvp);
  204. ip->i_devvp = 0;
  205. }
  206. free(vp->v_data, M_ISOFSNODE, 0);
  207. vp->v_data = NULL;
  208. return (0);
  209. }
  210. /*
  211. * File attributes
  212. */
  213. void
  214. cd9660_defattr(isodir, inop, bp)
  215. struct iso_directory_record *isodir;
  216. struct iso_node *inop;
  217. struct buf *bp;
  218. {
  219. struct buf *bp2 = NULL;
  220. struct iso_mnt *imp;
  221. struct iso_extended_attributes *ap = NULL;
  222. int off;
  223. if (isonum_711(isodir->flags)&2) {
  224. inop->inode.iso_mode = S_IFDIR;
  225. /*
  226. * If we return 2, fts() will assume there are no subdirectories
  227. * (just links for the path and .), so instead we return 1.
  228. */
  229. inop->inode.iso_links = 1;
  230. } else {
  231. inop->inode.iso_mode = S_IFREG;
  232. inop->inode.iso_links = 1;
  233. }
  234. if (!bp
  235. && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
  236. && (off = isonum_711(isodir->ext_attr_length))) {
  237. cd9660_bufatoff(inop, (off_t)-(off << imp->im_bshift), NULL,
  238. &bp2);
  239. bp = bp2;
  240. }
  241. if (bp) {
  242. ap = (struct iso_extended_attributes *)bp->b_data;
  243. if (isonum_711(ap->version) == 1) {
  244. if (!(ap->perm[1]&0x10))
  245. inop->inode.iso_mode |= S_IRUSR;
  246. if (!(ap->perm[1]&0x40))
  247. inop->inode.iso_mode |= S_IXUSR;
  248. if (!(ap->perm[0]&0x01))
  249. inop->inode.iso_mode |= S_IRGRP;
  250. if (!(ap->perm[0]&0x04))
  251. inop->inode.iso_mode |= S_IXGRP;
  252. if (!(ap->perm[0]&0x10))
  253. inop->inode.iso_mode |= S_IROTH;
  254. if (!(ap->perm[0]&0x40))
  255. inop->inode.iso_mode |= S_IXOTH;
  256. inop->inode.iso_uid = isonum_723(ap->owner); /* what about 0? */
  257. inop->inode.iso_gid = isonum_723(ap->group); /* what about 0? */
  258. } else
  259. ap = NULL;
  260. }
  261. if (!ap) {
  262. inop->inode.iso_mode |=
  263. S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
  264. inop->inode.iso_uid = (uid_t)0;
  265. inop->inode.iso_gid = (gid_t)0;
  266. }
  267. if (bp2)
  268. brelse(bp2);
  269. }
  270. /*
  271. * Time stamps
  272. */
  273. void
  274. cd9660_deftstamp(isodir,inop,bp)
  275. struct iso_directory_record *isodir;
  276. struct iso_node *inop;
  277. struct buf *bp;
  278. {
  279. struct buf *bp2 = NULL;
  280. struct iso_mnt *imp;
  281. struct iso_extended_attributes *ap = NULL;
  282. int off;
  283. if (!bp
  284. && ((imp = inop->i_mnt)->im_flags & ISOFSMNT_EXTATT)
  285. && (off = isonum_711(isodir->ext_attr_length))) {
  286. cd9660_bufatoff(inop, (off_t)-(off << imp->im_bshift), NULL,
  287. &bp2);
  288. bp = bp2;
  289. }
  290. if (bp) {
  291. ap = (struct iso_extended_attributes *)bp->b_data;
  292. if (isonum_711(ap->version) == 1) {
  293. if (!cd9660_tstamp_conv17(ap->ftime,&inop->inode.iso_atime))
  294. cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_atime);
  295. if (!cd9660_tstamp_conv17(ap->ctime,&inop->inode.iso_ctime))
  296. inop->inode.iso_ctime = inop->inode.iso_atime;
  297. if (!cd9660_tstamp_conv17(ap->mtime,&inop->inode.iso_mtime))
  298. inop->inode.iso_mtime = inop->inode.iso_ctime;
  299. } else
  300. ap = NULL;
  301. }
  302. if (!ap) {
  303. cd9660_tstamp_conv7(isodir->date,&inop->inode.iso_ctime);
  304. inop->inode.iso_atime = inop->inode.iso_ctime;
  305. inop->inode.iso_mtime = inop->inode.iso_ctime;
  306. }
  307. if (bp2)
  308. brelse(bp2);
  309. }
  310. int
  311. cd9660_tstamp_conv7(pi,pu)
  312. u_char *pi;
  313. struct timespec *pu;
  314. {
  315. int crtime, days;
  316. int y, m, d, hour, minute, second;
  317. signed char tz;
  318. y = pi[0] + 1900;
  319. m = pi[1];
  320. d = pi[2];
  321. hour = pi[3];
  322. minute = pi[4];
  323. second = pi[5];
  324. tz = (signed char) pi[6];
  325. if (y < 1970) {
  326. pu->tv_sec = 0;
  327. pu->tv_nsec = 0;
  328. return (0);
  329. } else {
  330. #ifdef ORIGINAL
  331. /* computes day number relative to Sept. 19th,1989 */
  332. /* don't even *THINK* about changing formula. It works! */
  333. days = 367*(y-1980)-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d-100;
  334. #else
  335. /*
  336. * Changed :-) to make it relative to Jan. 1st, 1970
  337. * and to disambiguate negative division
  338. */
  339. days = 367*(y-1960)-7*(y+(m+9)/12)/4-3*((y+(m+9)/12-1)/100+1)/4+275*m/9+d-239;
  340. #endif
  341. crtime = ((((days * 24) + hour) * 60 + minute) * 60) + second;
  342. /* timezone offset is unreliable on some disks */
  343. if (-48 <= tz && tz <= 52)
  344. crtime -= tz * 15 * 60;
  345. }
  346. pu->tv_sec = crtime;
  347. pu->tv_nsec = 0;
  348. return (1);
  349. }
  350. static u_int
  351. cd9660_chars2ui(begin,len)
  352. u_char *begin;
  353. int len;
  354. {
  355. u_int rc;
  356. for (rc = 0; --len >= 0;) {
  357. rc *= 10;
  358. rc += *begin++ - '0';
  359. }
  360. return (rc);
  361. }
  362. int
  363. cd9660_tstamp_conv17(pi,pu)
  364. u_char *pi;
  365. struct timespec *pu;
  366. {
  367. u_char buf[7];
  368. /* year:"0001"-"9999" -> -1900 */
  369. buf[0] = cd9660_chars2ui(pi,4) - 1900;
  370. /* month: " 1"-"12" -> 1 - 12 */
  371. buf[1] = cd9660_chars2ui(pi + 4,2);
  372. /* day: " 1"-"31" -> 1 - 31 */
  373. buf[2] = cd9660_chars2ui(pi + 6,2);
  374. /* hour: " 0"-"23" -> 0 - 23 */
  375. buf[3] = cd9660_chars2ui(pi + 8,2);
  376. /* minute:" 0"-"59" -> 0 - 59 */
  377. buf[4] = cd9660_chars2ui(pi + 10,2);
  378. /* second:" 0"-"59" -> 0 - 59 */
  379. buf[5] = cd9660_chars2ui(pi + 12,2);
  380. /* difference of GMT */
  381. buf[6] = pi[16];
  382. return (cd9660_tstamp_conv7(buf,pu));
  383. }
  384. cdino_t
  385. isodirino(isodir, imp)
  386. struct iso_directory_record *isodir;
  387. struct iso_mnt *imp;
  388. {
  389. cdino_t ino;
  390. ino = (isonum_733(isodir->extent) +
  391. isonum_711(isodir->ext_attr_length)) << imp->im_bshift;
  392. return (ino);
  393. }