msdosfs_vfsops.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /* $OpenBSD: msdosfs_vfsops.c,v 1.71 2015/02/13 13:35:03 millert Exp $ */
  2. /* $NetBSD: msdosfs_vfsops.c,v 1.48 1997/10/18 02:54:57 briggs Exp $ */
  3. /*-
  4. * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
  5. * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
  6. * All rights reserved.
  7. * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by TooLs GmbH.
  20. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  24. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  29. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  31. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  32. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Written by Paul Popelka (paulp@uts.amdahl.com)
  36. *
  37. * You can do anything you want with this software, just don't say you wrote
  38. * it, and don't remove this notice.
  39. *
  40. * This software is provided "as is".
  41. *
  42. * The author supplies this software to be publicly redistributed on the
  43. * understanding that the author is not responsible for the correct
  44. * functioning of this software in any circumstances and is not liable for
  45. * any damages caused by this software.
  46. *
  47. * October 1992
  48. */
  49. #include <sys/param.h>
  50. #include <sys/systm.h>
  51. #include <sys/namei.h>
  52. #include <sys/proc.h>
  53. #include <sys/kernel.h>
  54. #include <sys/vnode.h>
  55. #include <sys/lock.h>
  56. #include <sys/specdev.h> /* XXX */ /* defines v_rdev */
  57. #include <sys/mount.h>
  58. #include <sys/buf.h>
  59. #include <sys/file.h>
  60. #include <sys/disklabel.h>
  61. #include <sys/ioctl.h>
  62. #include <sys/malloc.h>
  63. #include <sys/dirent.h>
  64. #include <sys/disk.h>
  65. #include <sys/stdint.h>
  66. #include <msdosfs/bpb.h>
  67. #include <msdosfs/bootsect.h>
  68. #include <msdosfs/direntry.h>
  69. #include <msdosfs/denode.h>
  70. #include <msdosfs/msdosfsmount.h>
  71. #include <msdosfs/fat.h>
  72. int msdosfs_mount(struct mount *, const char *, void *, struct nameidata *,
  73. struct proc *);
  74. int msdosfs_start(struct mount *, int, struct proc *);
  75. int msdosfs_unmount(struct mount *, int, struct proc *);
  76. int msdosfs_root(struct mount *, struct vnode **);
  77. int msdosfs_statfs(struct mount *, struct statfs *, struct proc *);
  78. int msdosfs_sync(struct mount *, int, struct ucred *, struct proc *);
  79. int msdosfs_fhtovp(struct mount *, struct fid *, struct vnode **);
  80. int msdosfs_vptofh(struct vnode *, struct fid *);
  81. int msdosfs_check_export(struct mount *mp, struct mbuf *nam,
  82. int *extflagsp, struct ucred **credanonp);
  83. int msdosfs_mountfs(struct vnode *, struct mount *, struct proc *,
  84. struct msdosfs_args *);
  85. int msdosfs_sync_vnode(struct vnode *, void *);
  86. /*
  87. * mp - path - addr in user space of mount point (ie /usr or whatever)
  88. * data - addr in user space of mount params including the name of the block
  89. * special file to treat as a filesystem.
  90. */
  91. int
  92. msdosfs_mount(struct mount *mp, const char *path, void *data,
  93. struct nameidata *ndp, struct proc *p)
  94. {
  95. struct vnode *devvp; /* vnode for blk device to mount */
  96. struct msdosfs_args args; /* will hold data from mount request */
  97. /* msdosfs specific mount control block */
  98. struct msdosfsmount *pmp = NULL;
  99. char fname[MNAMELEN];
  100. char fspec[MNAMELEN];
  101. int error, flags;
  102. mode_t accessmode;
  103. error = copyin(data, &args, sizeof(struct msdosfs_args));
  104. if (error)
  105. return (error);
  106. /*
  107. * If updating, check whether changing from read-only to
  108. * read/write; if there is no device name, that's all we do.
  109. */
  110. if (mp->mnt_flag & MNT_UPDATE) {
  111. pmp = VFSTOMSDOSFS(mp);
  112. error = 0;
  113. if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
  114. (mp->mnt_flag & MNT_RDONLY)) {
  115. mp->mnt_flag &= ~MNT_RDONLY;
  116. VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p);
  117. mp->mnt_flag |= MNT_RDONLY;
  118. flags = WRITECLOSE;
  119. if (mp->mnt_flag & MNT_FORCE)
  120. flags |= FORCECLOSE;
  121. error = vflush(mp, NULLVP, flags);
  122. if (!error)
  123. pmp->pm_flags |= MSDOSFSMNT_RONLY;
  124. }
  125. if (!error && (mp->mnt_flag & MNT_RELOAD))
  126. /* not yet implemented */
  127. error = EOPNOTSUPP;
  128. if (error)
  129. return (error);
  130. if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
  131. (mp->mnt_flag & MNT_WANTRDWR)) {
  132. /*
  133. * If upgrade to read-write by non-root, then verify
  134. * that user has necessary permissions on the device.
  135. */
  136. if (suser(p, 0) != 0) {
  137. devvp = pmp->pm_devvp;
  138. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  139. error = VOP_ACCESS(devvp, VREAD | VWRITE,
  140. p->p_ucred, p);
  141. VOP_UNLOCK(devvp, 0, p);
  142. if (error)
  143. return (error);
  144. }
  145. pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
  146. }
  147. if (args.fspec == NULL) {
  148. #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
  149. if (args.flags & MSDOSFSMNT_MNTOPT) {
  150. pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
  151. pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
  152. if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
  153. pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
  154. }
  155. #endif
  156. /*
  157. * Process export requests.
  158. */
  159. return (vfs_export(mp, &pmp->pm_export,
  160. &args.export_info));
  161. }
  162. }
  163. /*
  164. * Not an update, or updating the name: look up the name
  165. * and verify that it refers to a sensible block device.
  166. */
  167. error = copyinstr(args.fspec, fspec, sizeof(fspec), NULL);
  168. if (error)
  169. goto error;
  170. if (disk_map(fspec, fname, sizeof(fname), DM_OPENBLCK) == -1)
  171. bcopy(fspec, fname, sizeof(fname));
  172. NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, p);
  173. if ((error = namei(ndp)) != 0)
  174. goto error;
  175. devvp = ndp->ni_vp;
  176. if (devvp->v_type != VBLK) {
  177. error = ENOTBLK;
  178. goto error_devvp;
  179. }
  180. if (major(devvp->v_rdev) >= nblkdev) {
  181. error = ENXIO;
  182. goto error_devvp;
  183. }
  184. /*
  185. * If mount by non-root, then verify that user has necessary
  186. * permissions on the device.
  187. */
  188. if (suser(p, 0) != 0) {
  189. accessmode = VREAD;
  190. if ((mp->mnt_flag & MNT_RDONLY) == 0)
  191. accessmode |= VWRITE;
  192. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  193. error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
  194. VOP_UNLOCK(devvp, 0, p);
  195. if (error)
  196. goto error_devvp;
  197. }
  198. if ((mp->mnt_flag & MNT_UPDATE) == 0)
  199. error = msdosfs_mountfs(devvp, mp, p, &args);
  200. else {
  201. if (devvp != pmp->pm_devvp)
  202. error = EINVAL; /* XXX needs translation */
  203. else
  204. vrele(devvp);
  205. }
  206. if (error)
  207. goto error_devvp;
  208. pmp = VFSTOMSDOSFS(mp);
  209. pmp->pm_gid = args.gid;
  210. pmp->pm_uid = args.uid;
  211. pmp->pm_mask = args.mask;
  212. pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
  213. if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
  214. pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
  215. else if (!(pmp->pm_flags &
  216. (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
  217. struct vnode *rvp;
  218. /*
  219. * Try to divine whether to support Win'95 long filenames
  220. */
  221. if (FAT32(pmp))
  222. pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
  223. else {
  224. if ((error = msdosfs_root(mp, &rvp)) != 0) {
  225. msdosfs_unmount(mp, MNT_FORCE, p);
  226. goto error;
  227. }
  228. pmp->pm_flags |= findwin95(VTODE(rvp))
  229. ? MSDOSFSMNT_LONGNAME
  230. : MSDOSFSMNT_SHORTNAME;
  231. vput(rvp);
  232. }
  233. }
  234. bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
  235. strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN);
  236. bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
  237. strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
  238. bzero(mp->mnt_stat.f_mntfromspec, MNAMELEN);
  239. strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
  240. bcopy(&args, &mp->mnt_stat.mount_info.msdosfs_args, sizeof(args));
  241. #ifdef MSDOSFS_DEBUG
  242. printf("msdosfs_mount(): mp %x, pmp %x, inusemap %x\n", mp,
  243. pmp, pmp->pm_inusemap);
  244. #endif
  245. return (0);
  246. error_devvp:
  247. vrele(devvp);
  248. error:
  249. return (error);
  250. }
  251. int
  252. msdosfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p,
  253. struct msdosfs_args *argp)
  254. {
  255. struct msdosfsmount *pmp;
  256. struct buf *bp;
  257. dev_t dev = devvp->v_rdev;
  258. union bootsector *bsp;
  259. struct byte_bpb33 *b33;
  260. struct byte_bpb50 *b50;
  261. struct byte_bpb710 *b710;
  262. extern struct vnode *rootvp;
  263. u_int8_t SecPerClust;
  264. int ronly, error, bmapsiz;
  265. uint32_t fat_max_clusters;
  266. /*
  267. * Disallow multiple mounts of the same device.
  268. * Disallow mounting of a device that is currently in use
  269. * (except for root, which might share swap device for miniroot).
  270. * Flush out any old buffers remaining from a previous use.
  271. */
  272. if ((error = vfs_mountedon(devvp)) != 0)
  273. return (error);
  274. if (vcount(devvp) > 1 && devvp != rootvp)
  275. return (EBUSY);
  276. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  277. error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
  278. VOP_UNLOCK(devvp, 0, p);
  279. if (error)
  280. return (error);
  281. ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
  282. error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
  283. if (error)
  284. return (error);
  285. bp = NULL; /* both used in error_exit */
  286. pmp = NULL;
  287. /*
  288. * Read the boot sector of the filesystem, and then check the
  289. * boot signature. If not a dos boot sector then error out.
  290. */
  291. if ((error = bread(devvp, 0, 4096, &bp)) != 0)
  292. goto error_exit;
  293. bsp = (union bootsector *)bp->b_data;
  294. b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
  295. b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
  296. b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
  297. pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
  298. pmp->pm_mountp = mp;
  299. /*
  300. * Compute several useful quantities from the bpb in the
  301. * bootsector. Copy in the dos 5 variant of the bpb then fix up
  302. * the fields that are different between dos 5 and dos 3.3.
  303. */
  304. SecPerClust = b50->bpbSecPerClust;
  305. pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
  306. pmp->pm_ResSectors = getushort(b50->bpbResSectors);
  307. pmp->pm_FATs = b50->bpbFATs;
  308. pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
  309. pmp->pm_Sectors = getushort(b50->bpbSectors);
  310. pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
  311. pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
  312. pmp->pm_Heads = getushort(b50->bpbHeads);
  313. pmp->pm_Media = b50->bpbMedia;
  314. /* Determine the number of DEV_BSIZE blocks in a MSDOSFS sector */
  315. pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
  316. if (!pmp->pm_BytesPerSec || !SecPerClust || pmp->pm_SecPerTrack > 64) {
  317. error = EFTYPE;
  318. goto error_exit;
  319. }
  320. if (pmp->pm_Sectors == 0) {
  321. pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
  322. pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
  323. } else {
  324. pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
  325. pmp->pm_HugeSectors = pmp->pm_Sectors;
  326. }
  327. if (pmp->pm_RootDirEnts == 0) {
  328. if (pmp->pm_Sectors || pmp->pm_FATsecs ||
  329. getushort(b710->bpbFSVers)) {
  330. error = EINVAL;
  331. goto error_exit;
  332. }
  333. pmp->pm_fatmask = FAT32_MASK;
  334. pmp->pm_fatmult = 4;
  335. pmp->pm_fatdiv = 1;
  336. pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
  337. if (getushort(b710->bpbExtFlags) & FATMIRROR)
  338. pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
  339. else
  340. pmp->pm_flags |= MSDOSFS_FATMIRROR;
  341. } else
  342. pmp->pm_flags |= MSDOSFS_FATMIRROR;
  343. /*
  344. * More sanity checks:
  345. * MSDOSFS sectors per cluster: >0 && power of 2
  346. * MSDOSFS sector size: >= DEV_BSIZE && power of 2
  347. * HUGE sector count: >0
  348. * FAT sectors: >0
  349. */
  350. if ((SecPerClust == 0) || (SecPerClust & (SecPerClust - 1)) ||
  351. (pmp->pm_BytesPerSec < DEV_BSIZE) ||
  352. (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) ||
  353. (pmp->pm_HugeSectors == 0) || (pmp->pm_FATsecs == 0) ||
  354. (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)) {
  355. error = EINVAL;
  356. goto error_exit;
  357. }
  358. pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
  359. pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;
  360. pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
  361. pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
  362. SecPerClust *= pmp->pm_BlkPerSec;
  363. if (FAT32(pmp)) {
  364. pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
  365. pmp->pm_firstcluster = pmp->pm_fatblk
  366. + (pmp->pm_FATs * pmp->pm_FATsecs);
  367. pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
  368. } else {
  369. pmp->pm_rootdirblk = pmp->pm_fatblk +
  370. (pmp->pm_FATs * pmp->pm_FATsecs);
  371. pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
  372. + DEV_BSIZE - 1) / DEV_BSIZE;
  373. pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
  374. }
  375. pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
  376. SecPerClust;
  377. pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
  378. pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;
  379. if (pmp->pm_fatmask == 0) {
  380. if (pmp->pm_maxcluster
  381. <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
  382. /*
  383. * This will usually be a floppy disk. This size makes
  384. * sure that one fat entry will not be split across
  385. * multiple blocks.
  386. */
  387. pmp->pm_fatmask = FAT12_MASK;
  388. pmp->pm_fatmult = 3;
  389. pmp->pm_fatdiv = 2;
  390. } else {
  391. pmp->pm_fatmask = FAT16_MASK;
  392. pmp->pm_fatmult = 2;
  393. pmp->pm_fatdiv = 1;
  394. }
  395. }
  396. if (FAT12(pmp))
  397. pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
  398. else
  399. pmp->pm_fatblocksize = MAXBSIZE;
  400. /*
  401. * We now have the number of sectors in each FAT, so can work
  402. * out how many clusters can be represented in a FAT. Let's
  403. * make sure the file system doesn't claim to have more clusters
  404. * than this.
  405. *
  406. * We perform the calculation like we do to avoid integer overflow.
  407. *
  408. * This will give us a count of clusters. They are numbered
  409. * from 0, so the max cluster value is one less than the value
  410. * we end up with.
  411. */
  412. fat_max_clusters = pmp->pm_fatsize / pmp->pm_fatmult;
  413. fat_max_clusters *= pmp->pm_fatdiv;
  414. if (pmp->pm_maxcluster >= fat_max_clusters) {
  415. #ifndef SMALL_KERNEL
  416. printf("msdosfs: reducing max cluster to %d from %d "
  417. "due to FAT size\n", fat_max_clusters - 1,
  418. pmp->pm_maxcluster);
  419. #endif
  420. pmp->pm_maxcluster = fat_max_clusters - 1;
  421. }
  422. pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
  423. pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
  424. /*
  425. * Compute mask and shift value for isolating cluster relative byte
  426. * offsets and cluster numbers from a file offset.
  427. */
  428. pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
  429. pmp->pm_crbomask = pmp->pm_bpcluster - 1;
  430. pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
  431. /*
  432. * Check for valid cluster size
  433. * must be a power of 2
  434. */
  435. if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
  436. error = EFTYPE;
  437. goto error_exit;
  438. }
  439. /*
  440. * Release the bootsector buffer.
  441. */
  442. brelse(bp);
  443. bp = NULL;
  444. /*
  445. * Check FSInfo
  446. */
  447. if (pmp->pm_fsinfo) {
  448. struct fsinfo *fp;
  449. if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
  450. &bp)) != 0)
  451. goto error_exit;
  452. fp = (struct fsinfo *)bp->b_data;
  453. if (!bcmp(fp->fsisig1, "RRaA", 4)
  454. && !bcmp(fp->fsisig2, "rrAa", 4)
  455. && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
  456. && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
  457. /* Valid FSInfo. */
  458. ;
  459. else
  460. pmp->pm_fsinfo = 0;
  461. /* XXX make sure this tiny buf doesn't come back in fillinusemap! */
  462. SET(bp->b_flags, B_INVAL);
  463. brelse(bp);
  464. bp = NULL;
  465. }
  466. /*
  467. * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
  468. */
  469. /*
  470. * Allocate memory for the bitmap of allocated clusters, and then
  471. * fill it in.
  472. */
  473. bmapsiz = howmany(pmp->pm_maxcluster + 1, N_INUSEBITS);
  474. if (bmapsiz == 0 || SIZE_MAX / bmapsiz < sizeof(*pmp->pm_inusemap)) {
  475. /* detect multiplicative integer overflow */
  476. error = EINVAL;
  477. goto error_exit;
  478. }
  479. pmp->pm_inusemap = mallocarray(bmapsiz, sizeof(*pmp->pm_inusemap),
  480. M_MSDOSFSFAT, M_WAITOK | M_CANFAIL);
  481. if (pmp->pm_inusemap == NULL) {
  482. error = EINVAL;
  483. goto error_exit;
  484. }
  485. /*
  486. * fillinusemap() needs pm_devvp.
  487. */
  488. pmp->pm_dev = dev;
  489. pmp->pm_devvp = devvp;
  490. /*
  491. * Have the inuse map filled in.
  492. */
  493. if ((error = fillinusemap(pmp)) != 0)
  494. goto error_exit;
  495. /*
  496. * If they want fat updates to be synchronous then let them suffer
  497. * the performance degradation in exchange for the on disk copy of
  498. * the fat being correct just about all the time. I suppose this
  499. * would be a good thing to turn on if the kernel is still flakey.
  500. */
  501. if (mp->mnt_flag & MNT_SYNCHRONOUS)
  502. pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
  503. /*
  504. * Finish up.
  505. */
  506. if (ronly)
  507. pmp->pm_flags |= MSDOSFSMNT_RONLY;
  508. else
  509. pmp->pm_fmod = 1;
  510. mp->mnt_data = (qaddr_t)pmp;
  511. mp->mnt_stat.f_fsid.val[0] = (long)dev;
  512. mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
  513. #ifdef QUOTA
  514. /*
  515. * If we ever do quotas for DOS filesystems this would be a place
  516. * to fill in the info in the msdosfsmount structure. You dolt,
  517. * quotas on dos filesystems make no sense because files have no
  518. * owners on dos filesystems. of course there is some empty space
  519. * in the directory entry where we could put uid's and gid's.
  520. */
  521. #endif
  522. devvp->v_specmountpoint = mp;
  523. return (0);
  524. error_exit:
  525. devvp->v_specmountpoint = NULL;
  526. if (bp)
  527. brelse(bp);
  528. vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY, p);
  529. (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
  530. VOP_UNLOCK(devvp, 0, p);
  531. if (pmp) {
  532. if (pmp->pm_inusemap)
  533. free(pmp->pm_inusemap, M_MSDOSFSFAT, 0);
  534. free(pmp, M_MSDOSFSMNT, 0);
  535. mp->mnt_data = (qaddr_t)0;
  536. }
  537. return (error);
  538. }
  539. int
  540. msdosfs_start(struct mount *mp, int flags, struct proc *p)
  541. {
  542. return (0);
  543. }
  544. /*
  545. * Unmount the filesystem described by mp.
  546. */
  547. int
  548. msdosfs_unmount(struct mount *mp, int mntflags,struct proc *p)
  549. {
  550. struct msdosfsmount *pmp;
  551. int error, flags;
  552. struct vnode *vp;
  553. flags = 0;
  554. if (mntflags & MNT_FORCE)
  555. flags |= FORCECLOSE;
  556. #ifdef QUOTA
  557. #endif
  558. if ((error = vflush(mp, NULLVP, flags)) != 0)
  559. return (error);
  560. pmp = VFSTOMSDOSFS(mp);
  561. pmp->pm_devvp->v_specmountpoint = NULL;
  562. vp = pmp->pm_devvp;
  563. #ifdef MSDOSFS_DEBUG
  564. vprint("msdosfs_umount(): just before calling VOP_CLOSE()\n", vp);
  565. #endif
  566. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
  567. error = VOP_CLOSE(vp,
  568. pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
  569. vput(vp);
  570. free(pmp->pm_inusemap, M_MSDOSFSFAT, 0);
  571. free(pmp, M_MSDOSFSMNT, 0);
  572. mp->mnt_data = (qaddr_t)0;
  573. mp->mnt_flag &= ~MNT_LOCAL;
  574. return (error);
  575. }
  576. int
  577. msdosfs_root(struct mount *mp, struct vnode **vpp)
  578. {
  579. struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  580. struct denode *ndep;
  581. int error;
  582. if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
  583. return (error);
  584. #ifdef MSDOSFS_DEBUG
  585. printf("msdosfs_root(); mp %08x, pmp %08x, ndep %08x, vp %08x\n",
  586. mp, pmp, ndep, DETOV(ndep));
  587. #endif
  588. *vpp = DETOV(ndep);
  589. return (0);
  590. }
  591. int
  592. msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
  593. {
  594. struct msdosfsmount *pmp;
  595. pmp = VFSTOMSDOSFS(mp);
  596. sbp->f_bsize = pmp->pm_bpcluster;
  597. sbp->f_iosize = pmp->pm_bpcluster;
  598. sbp->f_blocks = pmp->pm_nmbrofclusters;
  599. sbp->f_bfree = pmp->pm_freeclustercount;
  600. sbp->f_bavail = pmp->pm_freeclustercount;
  601. sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
  602. sbp->f_ffree = 0; /* what to put in here? */
  603. if (sbp != &mp->mnt_stat) {
  604. bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
  605. bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
  606. bcopy(&mp->mnt_stat.mount_info.msdosfs_args,
  607. &sbp->mount_info.msdosfs_args, sizeof(struct msdosfs_args));
  608. }
  609. strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
  610. return (0);
  611. }
  612. struct msdosfs_sync_arg {
  613. struct proc *p;
  614. struct ucred *cred;
  615. int allerror;
  616. int waitfor;
  617. };
  618. int
  619. msdosfs_sync_vnode(struct vnode *vp, void *arg)
  620. {
  621. struct msdosfs_sync_arg *msa = arg;
  622. int error;
  623. struct denode *dep;
  624. dep = VTODE(vp);
  625. if (vp->v_type == VNON ||
  626. ((dep->de_flag & (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0
  627. && LIST_EMPTY(&vp->v_dirtyblkhd)) ||
  628. msa->waitfor == MNT_LAZY) {
  629. return (0);
  630. }
  631. if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT, msa->p))
  632. return (0);
  633. if ((error = VOP_FSYNC(vp, msa->cred, msa->waitfor, msa->p)) != 0)
  634. msa->allerror = error;
  635. VOP_UNLOCK(vp, 0, msa->p);
  636. vrele(vp);
  637. return (0);
  638. }
  639. int
  640. msdosfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
  641. {
  642. struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  643. struct msdosfs_sync_arg msa;
  644. int error;
  645. msa.allerror = 0;
  646. msa.p = p;
  647. msa.cred = cred;
  648. msa.waitfor = waitfor;
  649. /*
  650. * If we ever switch to not updating all of the fats all the time,
  651. * this would be the place to update them from the first one.
  652. */
  653. if (pmp->pm_fmod != 0) {
  654. if (pmp->pm_flags & MSDOSFSMNT_RONLY)
  655. panic("msdosfs_sync: rofs mod");
  656. else {
  657. /* update fats here */
  658. }
  659. }
  660. /*
  661. * Write back each (modified) denode.
  662. */
  663. vfs_mount_foreach_vnode(mp, msdosfs_sync_vnode, &msa);
  664. /*
  665. * Force stale file system control information to be flushed.
  666. */
  667. if (waitfor != MNT_LAZY) {
  668. vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
  669. if ((error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p)) != 0)
  670. msa.allerror = error;
  671. VOP_UNLOCK(pmp->pm_devvp, 0, p);
  672. }
  673. return (msa.allerror);
  674. }
  675. int
  676. msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
  677. {
  678. struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  679. struct defid *defhp = (struct defid *) fhp;
  680. struct denode *dep;
  681. int error;
  682. error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
  683. if (error) {
  684. *vpp = NULLVP;
  685. return (error);
  686. }
  687. *vpp = DETOV(dep);
  688. return (0);
  689. }
  690. int
  691. msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
  692. {
  693. struct denode *dep;
  694. struct defid *defhp;
  695. dep = VTODE(vp);
  696. defhp = (struct defid *)fhp;
  697. defhp->defid_len = sizeof(struct defid);
  698. defhp->defid_dirclust = dep->de_dirclust;
  699. defhp->defid_dirofs = dep->de_diroffset;
  700. /* defhp->defid_gen = dep->de_gen; */
  701. return (0);
  702. }
  703. int
  704. msdosfs_check_export(struct mount *mp, struct mbuf *nam, int *exflagsp,
  705. struct ucred **credanonp)
  706. {
  707. struct netcred *np;
  708. struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  709. /*
  710. * Get the export permission structure for this <mp, client> tuple.
  711. */
  712. np = vfs_export_lookup(mp, &pmp->pm_export, nam);
  713. if (np == NULL)
  714. return (EACCES);
  715. *exflagsp = np->netc_exflags;
  716. *credanonp = &np->netc_anon;
  717. return (0);
  718. }
  719. #define msdosfs_vget ((int (*)(struct mount *, ino_t, struct vnode **)) \
  720. eopnotsupp)
  721. #define msdosfs_quotactl ((int (*)(struct mount *, int, uid_t, caddr_t, \
  722. struct proc *))eopnotsupp)
  723. #define msdosfs_sysctl ((int (*)(int *, u_int, void *, size_t *, void *, \
  724. size_t, struct proc *))eopnotsupp)
  725. const struct vfsops msdosfs_vfsops = {
  726. msdosfs_mount,
  727. msdosfs_start,
  728. msdosfs_unmount,
  729. msdosfs_root,
  730. msdosfs_quotactl,
  731. msdosfs_statfs,
  732. msdosfs_sync,
  733. msdosfs_vget,
  734. msdosfs_fhtovp,
  735. msdosfs_vptofh,
  736. msdosfs_init,
  737. msdosfs_sysctl,
  738. msdosfs_check_export
  739. };