ffs_vfsops.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. /* $OpenBSD: ffs_vfsops.c,v 1.149 2015/03/14 03:38:52 jsg Exp $ */
  2. /* $NetBSD: ffs_vfsops.c,v 1.19 1996/02/09 22:22:26 christos Exp $ */
  3. /*
  4. * Copyright (c) 1989, 1991, 1993, 1994
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
  32. */
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/namei.h>
  36. #include <sys/proc.h>
  37. #include <sys/kernel.h>
  38. #include <sys/vnode.h>
  39. #include <sys/socket.h>
  40. #include <sys/mount.h>
  41. #include <sys/buf.h>
  42. #include <sys/mbuf.h>
  43. #include <sys/file.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/errno.h>
  46. #include <sys/malloc.h>
  47. #include <sys/sysctl.h>
  48. #include <sys/pool.h>
  49. #include <sys/dkio.h>
  50. #include <sys/disk.h>
  51. #include <sys/specdev.h>
  52. #include <ufs/ufs/quota.h>
  53. #include <ufs/ufs/ufsmount.h>
  54. #include <ufs/ufs/inode.h>
  55. #include <ufs/ufs/dir.h>
  56. #include <ufs/ufs/ufs_extern.h>
  57. #include <ufs/ufs/dirhash.h>
  58. #include <ufs/ffs/fs.h>
  59. #include <ufs/ffs/ffs_extern.h>
  60. #include <uvm/uvm_extern.h>
  61. int ffs_sbupdate(struct ufsmount *, int);
  62. int ffs_reload_vnode(struct vnode *, void *);
  63. int ffs_sync_vnode(struct vnode *, void *);
  64. int ffs_validate(struct fs *);
  65. void ffs1_compat_read(struct fs *, struct ufsmount *, daddr_t);
  66. void ffs1_compat_write(struct fs *, struct ufsmount *);
  67. const struct vfsops ffs_vfsops = {
  68. ffs_mount,
  69. ufs_start,
  70. ffs_unmount,
  71. ufs_root,
  72. ufs_quotactl,
  73. ffs_statfs,
  74. ffs_sync,
  75. ffs_vget,
  76. ffs_fhtovp,
  77. ffs_vptofh,
  78. ffs_init,
  79. ffs_sysctl,
  80. ufs_check_export
  81. };
  82. struct inode_vtbl ffs_vtbl = {
  83. ffs_truncate,
  84. ffs_update,
  85. ffs_inode_alloc,
  86. ffs_inode_free,
  87. ffs_balloc,
  88. ffs_bufatoff
  89. };
  90. /*
  91. * Called by main() when ufs is going to be mounted as root.
  92. */
  93. struct pool ffs_ino_pool;
  94. struct pool ffs_dinode1_pool;
  95. #ifdef FFS2
  96. struct pool ffs_dinode2_pool;
  97. #endif
  98. int
  99. ffs_mountroot(void)
  100. {
  101. struct fs *fs;
  102. struct mount *mp;
  103. struct proc *p = curproc; /* XXX */
  104. struct ufsmount *ump;
  105. int error;
  106. /*
  107. * Get vnodes for swapdev and rootdev.
  108. */
  109. swapdev_vp = NULL;
  110. if ((error = bdevvp(swapdev, &swapdev_vp)) ||
  111. (error = bdevvp(rootdev, &rootvp))) {
  112. printf("ffs_mountroot: can't setup bdevvp's\n");
  113. if (swapdev_vp)
  114. vrele(swapdev_vp);
  115. return (error);
  116. }
  117. if ((error = vfs_rootmountalloc("ffs", "root_device", &mp)) != 0) {
  118. vrele(swapdev_vp);
  119. vrele(rootvp);
  120. return (error);
  121. }
  122. if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
  123. mp->mnt_vfc->vfc_refcount--;
  124. vfs_unbusy(mp);
  125. free(mp, M_MOUNT, sizeof(*mp));
  126. vrele(swapdev_vp);
  127. vrele(rootvp);
  128. return (error);
  129. }
  130. TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
  131. ump = VFSTOUFS(mp);
  132. fs = ump->um_fs;
  133. strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, sizeof(fs->fs_fsmnt));
  134. (void)ffs_statfs(mp, &mp->mnt_stat, p);
  135. vfs_unbusy(mp);
  136. inittodr(fs->fs_time);
  137. return (0);
  138. }
  139. /*
  140. * VFS Operations.
  141. *
  142. * mount system call
  143. */
  144. int
  145. ffs_mount(struct mount *mp, const char *path, void *data,
  146. struct nameidata *ndp, struct proc *p)
  147. {
  148. struct vnode *devvp;
  149. struct ufs_args args;
  150. struct ufsmount *ump = NULL;
  151. struct fs *fs;
  152. char fname[MNAMELEN];
  153. char fspec[MNAMELEN];
  154. int error = 0, flags;
  155. int ronly;
  156. mode_t accessmode;
  157. error = copyin(data, &args, sizeof(struct ufs_args));
  158. if (error)
  159. return (error);
  160. #ifndef FFS_SOFTUPDATES
  161. if (mp->mnt_flag & MNT_SOFTDEP) {
  162. printf("WARNING: soft updates isn't compiled in\n");
  163. mp->mnt_flag &= ~MNT_SOFTDEP;
  164. }
  165. #endif
  166. /*
  167. * Soft updates is incompatible with "async",
  168. * so if we are doing softupdates stop the user
  169. * from setting the async flag.
  170. */
  171. if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
  172. (MNT_SOFTDEP | MNT_ASYNC)) {
  173. return (EINVAL);
  174. }
  175. /*
  176. * If updating, check whether changing from read-only to
  177. * read/write; if there is no device name, that's all we do.
  178. */
  179. if (mp->mnt_flag & MNT_UPDATE) {
  180. ump = VFSTOUFS(mp);
  181. fs = ump->um_fs;
  182. devvp = ump->um_devvp;
  183. error = 0;
  184. ronly = fs->fs_ronly;
  185. if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
  186. /* Flush any dirty data */
  187. mp->mnt_flag &= ~MNT_RDONLY;
  188. VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p);
  189. mp->mnt_flag |= MNT_RDONLY;
  190. /*
  191. * Get rid of files open for writing.
  192. */
  193. flags = WRITECLOSE;
  194. if (mp->mnt_flag & MNT_FORCE)
  195. flags |= FORCECLOSE;
  196. if (fs->fs_flags & FS_DOSOFTDEP) {
  197. error = softdep_flushfiles(mp, flags, p);
  198. mp->mnt_flag &= ~MNT_SOFTDEP;
  199. } else
  200. error = ffs_flushfiles(mp, flags, p);
  201. ronly = 1;
  202. }
  203. /*
  204. * Flush soft dependencies if disabling it via an update
  205. * mount. This may leave some items to be processed,
  206. * so don't do this yet XXX.
  207. */
  208. if ((fs->fs_flags & FS_DOSOFTDEP) &&
  209. !(mp->mnt_flag & MNT_SOFTDEP) &&
  210. !(mp->mnt_flag & MNT_RDONLY) && fs->fs_ronly == 0) {
  211. #if 0
  212. flags = WRITECLOSE;
  213. if (mp->mnt_flag & MNT_FORCE)
  214. flags |= FORCECLOSE;
  215. error = softdep_flushfiles(mp, flags, p);
  216. #elif FFS_SOFTUPDATES
  217. mp->mnt_flag |= MNT_SOFTDEP;
  218. #endif
  219. }
  220. /*
  221. * When upgrading to a softdep mount, we must first flush
  222. * all vnodes. (not done yet -- see above)
  223. */
  224. if (!(fs->fs_flags & FS_DOSOFTDEP) &&
  225. (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
  226. #if 0
  227. flags = WRITECLOSE;
  228. if (mp->mnt_flag & MNT_FORCE)
  229. flags |= FORCECLOSE;
  230. error = ffs_flushfiles(mp, flags, p);
  231. #else
  232. mp->mnt_flag &= ~MNT_SOFTDEP;
  233. #endif
  234. }
  235. if (!error && (mp->mnt_flag & MNT_RELOAD))
  236. error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
  237. if (error)
  238. goto error_1;
  239. if (ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
  240. /*
  241. * If upgrade to read-write by non-root, then verify
  242. * that user has necessary permissions on the device.
  243. */
  244. if (suser(p, 0)) {
  245. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  246. error = VOP_ACCESS(devvp, VREAD | VWRITE,
  247. p->p_ucred, p);
  248. VOP_UNLOCK(devvp, 0, p);
  249. if (error)
  250. goto error_1;
  251. }
  252. if (fs->fs_clean == 0) {
  253. #if 0
  254. /*
  255. * It is safe to mount an unclean file system
  256. * if it was previously mounted with softdep
  257. * but we may lose space and must
  258. * sometimes run fsck manually.
  259. */
  260. if (fs->fs_flags & FS_DOSOFTDEP)
  261. printf(
  262. "WARNING: %s was not properly unmounted\n",
  263. fs->fs_fsmnt);
  264. else
  265. #endif
  266. if (mp->mnt_flag & MNT_FORCE) {
  267. printf(
  268. "WARNING: %s was not properly unmounted\n",
  269. fs->fs_fsmnt);
  270. } else {
  271. printf(
  272. "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
  273. fs->fs_fsmnt);
  274. error = EROFS;
  275. goto error_1;
  276. }
  277. }
  278. if ((fs->fs_flags & FS_DOSOFTDEP)) {
  279. error = softdep_mount(devvp, mp, fs,
  280. p->p_ucred);
  281. if (error)
  282. goto error_1;
  283. }
  284. fs->fs_contigdirs = malloc((u_long)fs->fs_ncg,
  285. M_UFSMNT, M_WAITOK|M_ZERO);
  286. ronly = 0;
  287. }
  288. if (args.fspec == NULL) {
  289. /*
  290. * Process export requests.
  291. */
  292. error = vfs_export(mp, &ump->um_export,
  293. &args.export_info);
  294. if (error)
  295. goto error_1;
  296. else
  297. goto success;
  298. }
  299. }
  300. /*
  301. * Not an update, or updating the name: look up the name
  302. * and verify that it refers to a sensible block device.
  303. */
  304. error = copyinstr(args.fspec, fspec, sizeof(fspec), NULL);
  305. if (error)
  306. goto error_1;
  307. if (disk_map(fspec, fname, MNAMELEN, DM_OPENBLCK) == -1)
  308. memcpy(fname, fspec, sizeof(fname));
  309. NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, p);
  310. if ((error = namei(ndp)) != 0)
  311. goto error_1;
  312. devvp = ndp->ni_vp;
  313. if (devvp->v_type != VBLK) {
  314. error = ENOTBLK;
  315. goto error_2;
  316. }
  317. if (major(devvp->v_rdev) >= nblkdev) {
  318. error = ENXIO;
  319. goto error_2;
  320. }
  321. /*
  322. * If mount by non-root, then verify that user has necessary
  323. * permissions on the device.
  324. */
  325. if (suser(p, 0)) {
  326. accessmode = VREAD;
  327. if ((mp->mnt_flag & MNT_RDONLY) == 0)
  328. accessmode |= VWRITE;
  329. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  330. error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
  331. VOP_UNLOCK(devvp, 0, p);
  332. if (error)
  333. goto error_2;
  334. }
  335. if (mp->mnt_flag & MNT_UPDATE) {
  336. /*
  337. * UPDATE
  338. * If it's not the same vnode, or at least the same device
  339. * then it's not correct.
  340. */
  341. if (devvp != ump->um_devvp) {
  342. if (devvp->v_rdev == ump->um_devvp->v_rdev) {
  343. vrele(devvp);
  344. } else {
  345. error = EINVAL; /* needs translation */
  346. }
  347. } else
  348. vrele(devvp);
  349. /*
  350. * Update device name only on success
  351. */
  352. if (!error) {
  353. /*
  354. * Save "mounted from" info for mount point (NULL pad)
  355. */
  356. memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
  357. strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
  358. memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
  359. strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
  360. }
  361. } else {
  362. /*
  363. * Since this is a new mount, we want the names for
  364. * the device and the mount point copied in. If an
  365. * error occurs, the mountpoint is discarded by the
  366. * upper level code.
  367. */
  368. memset(mp->mnt_stat.f_mntonname, 0, MNAMELEN);
  369. strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN);
  370. memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
  371. strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
  372. memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
  373. strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
  374. error = ffs_mountfs(devvp, mp, p);
  375. }
  376. if (error)
  377. goto error_2;
  378. /*
  379. * Initialize FS stat information in mount struct; uses both
  380. * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
  381. *
  382. * This code is common to root and non-root mounts
  383. */
  384. memcpy(&mp->mnt_stat.mount_info.ufs_args, &args, sizeof(args));
  385. VFS_STATFS(mp, &mp->mnt_stat, p);
  386. success:
  387. if (path && (mp->mnt_flag & MNT_UPDATE)) {
  388. /* Update clean flag after changing read-onlyness. */
  389. fs = ump->um_fs;
  390. if (ronly != fs->fs_ronly) {
  391. fs->fs_ronly = ronly;
  392. fs->fs_clean = ronly &&
  393. (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
  394. if (ronly)
  395. free(fs->fs_contigdirs, M_UFSMNT, 0);
  396. }
  397. if (!ronly) {
  398. if (mp->mnt_flag & MNT_SOFTDEP)
  399. fs->fs_flags |= FS_DOSOFTDEP;
  400. else
  401. fs->fs_flags &= ~FS_DOSOFTDEP;
  402. }
  403. ffs_sbupdate(ump, MNT_WAIT);
  404. }
  405. return (0);
  406. error_2: /* error with devvp held */
  407. vrele (devvp);
  408. error_1: /* no state to back out */
  409. return (error);
  410. }
  411. struct ffs_reload_args {
  412. struct fs *fs;
  413. struct proc *p;
  414. struct ucred *cred;
  415. struct vnode *devvp;
  416. };
  417. int
  418. ffs_reload_vnode(struct vnode *vp, void *args)
  419. {
  420. struct ffs_reload_args *fra = args;
  421. struct inode *ip;
  422. struct buf *bp;
  423. int error;
  424. /*
  425. * Step 4: invalidate all inactive vnodes.
  426. */
  427. if (vp->v_usecount == 0) {
  428. vgonel(vp, fra->p);
  429. return (0);
  430. }
  431. /*
  432. * Step 5: invalidate all cached file data.
  433. */
  434. if (vget(vp, LK_EXCLUSIVE, fra->p))
  435. return (0);
  436. if (vinvalbuf(vp, 0, fra->cred, fra->p, 0, 0))
  437. panic("ffs_reload: dirty2");
  438. /*
  439. * Step 6: re-read inode data for all active vnodes.
  440. */
  441. ip = VTOI(vp);
  442. error = bread(fra->devvp,
  443. fsbtodb(fra->fs, ino_to_fsba(fra->fs, ip->i_number)),
  444. (int)fra->fs->fs_bsize, &bp);
  445. if (error) {
  446. brelse(bp);
  447. vput(vp);
  448. return (error);
  449. }
  450. *ip->i_din1 = *((struct ufs1_dinode *)bp->b_data +
  451. ino_to_fsbo(fra->fs, ip->i_number));
  452. ip->i_effnlink = DIP(ip, nlink);
  453. brelse(bp);
  454. vput(vp);
  455. return (0);
  456. }
  457. /*
  458. * Reload all incore data for a filesystem (used after running fsck on
  459. * the root filesystem and finding things to fix). The filesystem must
  460. * be mounted read-only.
  461. *
  462. * Things to do to update the mount:
  463. * 1) invalidate all cached meta-data.
  464. * 2) re-read superblock from disk.
  465. * 3) re-read summary information from disk.
  466. * 4) invalidate all inactive vnodes.
  467. * 5) invalidate all cached file data.
  468. * 6) re-read inode data for all active vnodes.
  469. */
  470. int
  471. ffs_reload(struct mount *mountp, struct ucred *cred, struct proc *p)
  472. {
  473. struct vnode *devvp;
  474. caddr_t space;
  475. struct fs *fs, *newfs;
  476. int i, blks, size, error;
  477. int32_t *lp;
  478. struct buf *bp = NULL;
  479. struct ffs_reload_args fra;
  480. if ((mountp->mnt_flag & MNT_RDONLY) == 0)
  481. return (EINVAL);
  482. /*
  483. * Step 1: invalidate all cached meta-data.
  484. */
  485. devvp = VFSTOUFS(mountp)->um_devvp;
  486. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  487. error = vinvalbuf(devvp, 0, cred, p, 0, 0);
  488. VOP_UNLOCK(devvp, 0, p);
  489. if (error)
  490. panic("ffs_reload: dirty1");
  491. /*
  492. * Step 2: re-read superblock from disk.
  493. */
  494. fs = VFSTOUFS(mountp)->um_fs;
  495. error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, SBSIZE, &bp);
  496. if (error) {
  497. brelse(bp);
  498. return (error);
  499. }
  500. newfs = (struct fs *)bp->b_data;
  501. if (ffs_validate(newfs) == 0) {
  502. brelse(bp);
  503. return (EINVAL);
  504. }
  505. /*
  506. * Copy pointer fields back into superblock before copying in XXX
  507. * new superblock. These should really be in the ufsmount. XXX
  508. * Note that important parameters (eg fs_ncg) are unchanged.
  509. */
  510. newfs->fs_csp = fs->fs_csp;
  511. newfs->fs_maxcluster = fs->fs_maxcluster;
  512. newfs->fs_ronly = fs->fs_ronly;
  513. memcpy(fs, newfs, fs->fs_sbsize);
  514. if (fs->fs_sbsize < SBSIZE)
  515. bp->b_flags |= B_INVAL;
  516. brelse(bp);
  517. mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
  518. ffs1_compat_read(fs, VFSTOUFS(mountp), fs->fs_sblockloc);
  519. ffs_oldfscompat(fs);
  520. (void)ffs_statfs(mountp, &mountp->mnt_stat, p);
  521. /*
  522. * Step 3: re-read summary information from disk.
  523. */
  524. blks = howmany(fs->fs_cssize, fs->fs_fsize);
  525. space = (caddr_t)fs->fs_csp;
  526. for (i = 0; i < blks; i += fs->fs_frag) {
  527. size = fs->fs_bsize;
  528. if (i + fs->fs_frag > blks)
  529. size = (blks - i) * fs->fs_fsize;
  530. error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
  531. if (error) {
  532. brelse(bp);
  533. return (error);
  534. }
  535. memcpy(space, bp->b_data, size);
  536. space += size;
  537. brelse(bp);
  538. }
  539. if ((fs->fs_flags & FS_DOSOFTDEP))
  540. (void) softdep_mount(devvp, mountp, fs, cred);
  541. /*
  542. * We no longer know anything about clusters per cylinder group.
  543. */
  544. if (fs->fs_contigsumsize > 0) {
  545. lp = fs->fs_maxcluster;
  546. for (i = 0; i < fs->fs_ncg; i++)
  547. *lp++ = fs->fs_contigsumsize;
  548. }
  549. fra.p = p;
  550. fra.cred = cred;
  551. fra.fs = fs;
  552. fra.devvp = devvp;
  553. error = vfs_mount_foreach_vnode(mountp, ffs_reload_vnode, &fra);
  554. return (error);
  555. }
  556. /*
  557. * Checks if a super block is sane enough to be mounted.
  558. */
  559. int
  560. ffs_validate(struct fs *fsp)
  561. {
  562. #ifdef FFS2
  563. if (fsp->fs_magic != FS_UFS2_MAGIC && fsp->fs_magic != FS_UFS1_MAGIC)
  564. return (0); /* Invalid magic */
  565. #else
  566. if (fsp->fs_magic != FS_UFS1_MAGIC)
  567. return (0); /* Invalid magic */
  568. #endif /* FFS2 */
  569. if ((u_int)fsp->fs_bsize > MAXBSIZE)
  570. return (0); /* Invalid block size */
  571. if ((u_int)fsp->fs_bsize < sizeof(struct fs))
  572. return (0); /* Invalid block size */
  573. if ((u_int)fsp->fs_sbsize > SBSIZE)
  574. return (0); /* Invalid super block size */
  575. if ((u_int)fsp->fs_frag > MAXFRAG || fragtbl[fsp->fs_frag] == NULL)
  576. return (0); /* Invalid number of fragments */
  577. return (1); /* Super block is okay */
  578. }
  579. /*
  580. * Possible locations for the super-block.
  581. */
  582. const int sbtry[] = SBLOCKSEARCH;
  583. /*
  584. * Common code for mount and mountroot
  585. */
  586. int
  587. ffs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
  588. {
  589. struct ufsmount *ump;
  590. struct buf *bp;
  591. struct fs *fs;
  592. dev_t dev;
  593. caddr_t space;
  594. daddr_t sbloc;
  595. int error, i, blks, size, ronly;
  596. int32_t *lp;
  597. struct ucred *cred;
  598. u_int64_t maxfilesize; /* XXX */
  599. dev = devvp->v_rdev;
  600. cred = p ? p->p_ucred : NOCRED;
  601. /*
  602. * Disallow multiple mounts of the same device.
  603. * Disallow mounting of a device that is currently in use
  604. * (except for root, which might share swap device for miniroot).
  605. * Flush out any old buffers remaining from a previous use.
  606. */
  607. if ((error = vfs_mountedon(devvp)) != 0)
  608. return (error);
  609. if (vcount(devvp) > 1 && devvp != rootvp)
  610. return (EBUSY);
  611. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  612. error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
  613. VOP_UNLOCK(devvp, 0, p);
  614. if (error)
  615. return (error);
  616. ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
  617. error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
  618. if (error)
  619. return (error);
  620. bp = NULL;
  621. ump = NULL;
  622. /*
  623. * Try reading the super-block in each of its possible locations.
  624. */
  625. for (i = 0; sbtry[i] != -1; i++) {
  626. if (bp != NULL) {
  627. bp->b_flags |= B_NOCACHE;
  628. brelse(bp);
  629. bp = NULL;
  630. }
  631. error = bread(devvp, sbtry[i] / DEV_BSIZE, SBSIZE, &bp);
  632. if (error)
  633. goto out;
  634. fs = (struct fs *) bp->b_data;
  635. sbloc = sbtry[i];
  636. #if 0
  637. if (fs->fs_magic == FS_UFS2_MAGIC) {
  638. printf("ffs_mountfs(): Sorry, no UFS2 support (yet)\n");
  639. error = EFTYPE;
  640. goto out;
  641. }
  642. #endif
  643. /*
  644. * Do not look for an FFS1 file system at SBLOCK_UFS2. Doing so
  645. * will find the wrong super-block for file systems with 64k
  646. * block size.
  647. */
  648. if (fs->fs_magic == FS_UFS1_MAGIC && sbloc == SBLOCK_UFS2)
  649. continue;
  650. if (ffs_validate(fs))
  651. break; /* Super block validated */
  652. }
  653. if (sbtry[i] == -1) {
  654. error = EINVAL;
  655. goto out;
  656. }
  657. fs->fs_fmod = 0;
  658. fs->fs_flags &= ~FS_UNCLEAN;
  659. if (fs->fs_clean == 0) {
  660. #if 0
  661. /*
  662. * It is safe to mount an unclean file system
  663. * if it was previously mounted with softdep
  664. * but we may lose space and must
  665. * sometimes run fsck manually.
  666. */
  667. if (fs->fs_flags & FS_DOSOFTDEP)
  668. printf(
  669. "WARNING: %s was not properly unmounted\n",
  670. fs->fs_fsmnt);
  671. else
  672. #endif
  673. if (ronly || (mp->mnt_flag & MNT_FORCE)) {
  674. printf(
  675. "WARNING: %s was not properly unmounted\n",
  676. fs->fs_fsmnt);
  677. } else {
  678. printf(
  679. "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
  680. fs->fs_fsmnt);
  681. error = EROFS;
  682. goto out;
  683. }
  684. }
  685. if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
  686. #ifndef SMALL_KERNEL
  687. printf("ffs_mountfs(): obsolete rotational table format, "
  688. "please use fsck_ffs(8) -c 1\n");
  689. #endif
  690. error = EFTYPE;
  691. goto out;
  692. }
  693. ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK|M_ZERO);
  694. ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
  695. M_WAITOK);
  696. if (fs->fs_magic == FS_UFS1_MAGIC)
  697. ump->um_fstype = UM_UFS1;
  698. #ifdef FFS2
  699. else
  700. ump->um_fstype = UM_UFS2;
  701. #endif
  702. memcpy(ump->um_fs, bp->b_data, fs->fs_sbsize);
  703. if (fs->fs_sbsize < SBSIZE)
  704. bp->b_flags |= B_INVAL;
  705. brelse(bp);
  706. bp = NULL;
  707. fs = ump->um_fs;
  708. ffs1_compat_read(fs, ump, sbloc);
  709. if (fs->fs_clean == 0)
  710. fs->fs_flags |= FS_UNCLEAN;
  711. fs->fs_ronly = ronly;
  712. size = fs->fs_cssize;
  713. blks = howmany(size, fs->fs_fsize);
  714. if (fs->fs_contigsumsize > 0)
  715. size += fs->fs_ncg * sizeof(int32_t);
  716. space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
  717. fs->fs_csp = (struct csum *)space;
  718. for (i = 0; i < blks; i += fs->fs_frag) {
  719. size = fs->fs_bsize;
  720. if (i + fs->fs_frag > blks)
  721. size = (blks - i) * fs->fs_fsize;
  722. error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size, &bp);
  723. if (error) {
  724. free(fs->fs_csp, M_UFSMNT, 0);
  725. goto out;
  726. }
  727. memcpy(space, bp->b_data, size);
  728. space += size;
  729. brelse(bp);
  730. bp = NULL;
  731. }
  732. if (fs->fs_contigsumsize > 0) {
  733. fs->fs_maxcluster = lp = (int32_t *)space;
  734. for (i = 0; i < fs->fs_ncg; i++)
  735. *lp++ = fs->fs_contigsumsize;
  736. }
  737. mp->mnt_data = (qaddr_t)ump;
  738. mp->mnt_stat.f_fsid.val[0] = (long)dev;
  739. /* Use on-disk fsid if it exists, else fake it */
  740. if (fs->fs_id[0] != 0 && fs->fs_id[1] != 0)
  741. mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
  742. else
  743. mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
  744. mp->mnt_stat.f_namemax = MAXNAMLEN;
  745. mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
  746. mp->mnt_flag |= MNT_LOCAL;
  747. ump->um_mountp = mp;
  748. ump->um_dev = dev;
  749. ump->um_devvp = devvp;
  750. ump->um_nindir = fs->fs_nindir;
  751. ump->um_bptrtodb = fs->fs_fsbtodb;
  752. ump->um_seqinc = fs->fs_frag;
  753. for (i = 0; i < MAXQUOTAS; i++)
  754. ump->um_quotas[i] = NULLVP;
  755. devvp->v_specmountpoint = mp;
  756. ffs_oldfscompat(fs);
  757. if (ronly)
  758. fs->fs_contigdirs = NULL;
  759. else {
  760. fs->fs_contigdirs = malloc((u_long)fs->fs_ncg,
  761. M_UFSMNT, M_WAITOK|M_ZERO);
  762. }
  763. /*
  764. * Set FS local "last mounted on" information (NULL pad)
  765. */
  766. memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
  767. strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, sizeof(fs->fs_fsmnt));
  768. #if 0
  769. if( mp->mnt_flag & MNT_ROOTFS) {
  770. /*
  771. * Root mount; update timestamp in mount structure.
  772. * this will be used by the common root mount code
  773. * to update the system clock.
  774. */
  775. mp->mnt_time = fs->fs_time;
  776. }
  777. #endif
  778. /*
  779. * XXX
  780. * Limit max file size. Even though ffs can handle files up to 16TB,
  781. * we do limit the max file to 2^31 pages to prevent overflow of
  782. * a 32-bit unsigned int. The buffer cache has its own checks but
  783. * a little added paranoia never hurts.
  784. */
  785. ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
  786. maxfilesize = FS_KERNMAXFILESIZE(PAGE_SIZE, fs);
  787. if (fs->fs_maxfilesize > maxfilesize) /* XXX */
  788. fs->fs_maxfilesize = maxfilesize; /* XXX */
  789. if (ronly == 0) {
  790. if ((fs->fs_flags & FS_DOSOFTDEP) &&
  791. (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
  792. free(fs->fs_csp, M_UFSMNT, 0);
  793. free(fs->fs_contigdirs, M_UFSMNT, 0);
  794. goto out;
  795. }
  796. fs->fs_fmod = 1;
  797. fs->fs_clean = 0;
  798. if (mp->mnt_flag & MNT_SOFTDEP)
  799. fs->fs_flags |= FS_DOSOFTDEP;
  800. else
  801. fs->fs_flags &= ~FS_DOSOFTDEP;
  802. error = ffs_sbupdate(ump, MNT_WAIT);
  803. if (error == EROFS)
  804. goto out;
  805. }
  806. return (0);
  807. out:
  808. devvp->v_specmountpoint = NULL;
  809. if (bp)
  810. brelse(bp);
  811. vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY, p);
  812. (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
  813. VOP_UNLOCK(devvp, 0, p);
  814. if (ump) {
  815. free(ump->um_fs, M_UFSMNT, ump->um_fs->fs_sbsize);
  816. free(ump, M_UFSMNT, sizeof(*ump));
  817. mp->mnt_data = NULL;
  818. }
  819. return (error);
  820. }
  821. /*
  822. * Sanity checks for old file systems.
  823. */
  824. int
  825. ffs_oldfscompat(struct fs *fs)
  826. {
  827. int i;
  828. fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
  829. fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
  830. if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
  831. fs->fs_nrpos = 8; /* XXX */
  832. if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
  833. u_int64_t sizepb = fs->fs_bsize; /* XXX */
  834. /* XXX */
  835. fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
  836. for (i = 0; i < NIADDR; i++) { /* XXX */
  837. sizepb *= NINDIR(fs); /* XXX */
  838. fs->fs_maxfilesize += sizepb; /* XXX */
  839. } /* XXX */
  840. fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
  841. fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
  842. } /* XXX */
  843. if (fs->fs_avgfilesize <= 0) /* XXX */
  844. fs->fs_avgfilesize = AVFILESIZ; /* XXX */
  845. if (fs->fs_avgfpdir <= 0) /* XXX */
  846. fs->fs_avgfpdir = AFPDIR; /* XXX */
  847. return (0);
  848. }
  849. /*
  850. * Auxiliary function for reading FFS1 super blocks.
  851. */
  852. void
  853. ffs1_compat_read(struct fs *fs, struct ufsmount *ump, daddr_t sbloc)
  854. {
  855. if (fs->fs_magic == FS_UFS2_MAGIC)
  856. return; /* UFS2 */
  857. #if 0
  858. if (fs->fs_ffs1_flags & FS_FLAGS_UPDATED)
  859. return; /* Already updated */
  860. #endif
  861. fs->fs_flags = fs->fs_ffs1_flags;
  862. fs->fs_sblockloc = sbloc;
  863. fs->fs_maxbsize = fs->fs_bsize;
  864. fs->fs_time = fs->fs_ffs1_time;
  865. fs->fs_size = fs->fs_ffs1_size;
  866. fs->fs_dsize = fs->fs_ffs1_dsize;
  867. fs->fs_csaddr = fs->fs_ffs1_csaddr;
  868. fs->fs_cstotal.cs_ndir = fs->fs_ffs1_cstotal.cs_ndir;
  869. fs->fs_cstotal.cs_nbfree = fs->fs_ffs1_cstotal.cs_nbfree;
  870. fs->fs_cstotal.cs_nifree = fs->fs_ffs1_cstotal.cs_nifree;
  871. fs->fs_cstotal.cs_nffree = fs->fs_ffs1_cstotal.cs_nffree;
  872. fs->fs_ffs1_flags |= FS_FLAGS_UPDATED;
  873. }
  874. /*
  875. * Auxiliary function for writing FFS1 super blocks.
  876. */
  877. void
  878. ffs1_compat_write(struct fs *fs, struct ufsmount *ump)
  879. {
  880. if (fs->fs_magic != FS_UFS1_MAGIC)
  881. return; /* UFS2 */
  882. fs->fs_ffs1_time = fs->fs_time;
  883. fs->fs_ffs1_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
  884. fs->fs_ffs1_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
  885. fs->fs_ffs1_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
  886. fs->fs_ffs1_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
  887. }
  888. /*
  889. * unmount system call
  890. */
  891. int
  892. ffs_unmount(struct mount *mp, int mntflags, struct proc *p)
  893. {
  894. struct ufsmount *ump;
  895. struct fs *fs;
  896. int error, flags;
  897. flags = 0;
  898. if (mntflags & MNT_FORCE)
  899. flags |= FORCECLOSE;
  900. ump = VFSTOUFS(mp);
  901. fs = ump->um_fs;
  902. if (mp->mnt_flag & MNT_SOFTDEP)
  903. error = softdep_flushfiles(mp, flags, p);
  904. else
  905. error = ffs_flushfiles(mp, flags, p);
  906. if (error != 0)
  907. return (error);
  908. if (fs->fs_ronly == 0) {
  909. fs->fs_clean = (fs->fs_flags & FS_UNCLEAN) ? 0 : 1;
  910. error = ffs_sbupdate(ump, MNT_WAIT);
  911. /* ignore write errors if mounted RW on read-only device */
  912. if (error && error != EROFS) {
  913. fs->fs_clean = 0;
  914. return (error);
  915. }
  916. free(fs->fs_contigdirs, M_UFSMNT, 0);
  917. }
  918. ump->um_devvp->v_specmountpoint = NULL;
  919. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
  920. vinvalbuf(ump->um_devvp, V_SAVE, NOCRED, p, 0, 0);
  921. error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
  922. NOCRED, p);
  923. vput(ump->um_devvp);
  924. free(fs->fs_csp, M_UFSMNT, 0);
  925. free(fs, M_UFSMNT, fs->fs_sbsize);
  926. free(ump, M_UFSMNT, sizeof(*ump));
  927. mp->mnt_data = (qaddr_t)0;
  928. mp->mnt_flag &= ~MNT_LOCAL;
  929. return (error);
  930. }
  931. /*
  932. * Flush out all the files in a filesystem.
  933. */
  934. int
  935. ffs_flushfiles(struct mount *mp, int flags, struct proc *p)
  936. {
  937. struct ufsmount *ump;
  938. int error;
  939. ump = VFSTOUFS(mp);
  940. if (mp->mnt_flag & MNT_QUOTA) {
  941. int i;
  942. if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
  943. return (error);
  944. for (i = 0; i < MAXQUOTAS; i++) {
  945. if (ump->um_quotas[i] == NULLVP)
  946. continue;
  947. quotaoff(p, mp, i);
  948. }
  949. /*
  950. * Here we fall through to vflush again to ensure
  951. * that we have gotten rid of all the system vnodes.
  952. */
  953. }
  954. /*
  955. * Flush all the files.
  956. */
  957. if ((error = vflush(mp, NULL, flags)) != 0)
  958. return (error);
  959. /*
  960. * Flush filesystem metadata.
  961. */
  962. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
  963. error = VOP_FSYNC(ump->um_devvp, p->p_ucred, MNT_WAIT, p);
  964. VOP_UNLOCK(ump->um_devvp, 0, p);
  965. return (error);
  966. }
  967. /*
  968. * Get file system statistics.
  969. */
  970. int
  971. ffs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
  972. {
  973. struct ufsmount *ump;
  974. struct fs *fs;
  975. ump = VFSTOUFS(mp);
  976. fs = ump->um_fs;
  977. #ifdef FFS2
  978. if (fs->fs_magic != FS_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
  979. panic("ffs_statfs");
  980. #else
  981. if (fs->fs_magic != FS_MAGIC)
  982. panic("ffs_statfs");
  983. #endif /* FFS2 */
  984. sbp->f_bsize = fs->fs_fsize;
  985. sbp->f_iosize = fs->fs_bsize;
  986. sbp->f_blocks = fs->fs_dsize;
  987. sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
  988. fs->fs_cstotal.cs_nffree;
  989. sbp->f_bavail = sbp->f_bfree -
  990. ((int64_t)fs->fs_dsize * fs->fs_minfree / 100);
  991. sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
  992. sbp->f_ffree = fs->fs_cstotal.cs_nifree;
  993. sbp->f_favail = sbp->f_ffree;
  994. copy_statfs_info(sbp, mp);
  995. return (0);
  996. }
  997. struct ffs_sync_args {
  998. int allerror;
  999. struct proc *p;
  1000. int waitfor;
  1001. struct ucred *cred;
  1002. };
  1003. int
  1004. ffs_sync_vnode(struct vnode *vp, void *arg) {
  1005. struct ffs_sync_args *fsa = arg;
  1006. struct inode *ip;
  1007. int error;
  1008. ip = VTOI(vp);
  1009. if (vp->v_type == VNON ||
  1010. ((ip->i_flag &
  1011. (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
  1012. LIST_EMPTY(&vp->v_dirtyblkhd)) ) {
  1013. return (0);
  1014. }
  1015. if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT, fsa->p))
  1016. return (0);
  1017. if ((error = VOP_FSYNC(vp, fsa->cred, fsa->waitfor, fsa->p)))
  1018. fsa->allerror = error;
  1019. VOP_UNLOCK(vp, 0, fsa->p);
  1020. vrele(vp);
  1021. return (0);
  1022. }
  1023. /*
  1024. * Go through the disk queues to initiate sandbagged IO;
  1025. * go through the inodes to write those that have been modified;
  1026. * initiate the writing of the super block if it has been modified.
  1027. *
  1028. * Should always be called with the mount point locked.
  1029. */
  1030. int
  1031. ffs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
  1032. {
  1033. struct ufsmount *ump = VFSTOUFS(mp);
  1034. struct fs *fs;
  1035. int error, allerror = 0, count;
  1036. struct ffs_sync_args fsa;
  1037. fs = ump->um_fs;
  1038. /*
  1039. * Write back modified superblock.
  1040. * Consistency check that the superblock
  1041. * is still in the buffer cache.
  1042. */
  1043. if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {
  1044. printf("fs = %s\n", fs->fs_fsmnt);
  1045. panic("update: rofs mod");
  1046. }
  1047. loop:
  1048. /*
  1049. * Write back each (modified) inode.
  1050. */
  1051. fsa.allerror = 0;
  1052. fsa.p = p;
  1053. fsa.cred = cred;
  1054. fsa.waitfor = waitfor;
  1055. /*
  1056. * Don't traverse the vnode list if we want to skip all of them.
  1057. */
  1058. if (waitfor != MNT_LAZY) {
  1059. vfs_mount_foreach_vnode(mp, ffs_sync_vnode, &fsa);
  1060. allerror = fsa.allerror;
  1061. }
  1062. /*
  1063. * Force stale file system control information to be flushed.
  1064. */
  1065. if ((ump->um_mountp->mnt_flag & MNT_SOFTDEP) && waitfor == MNT_WAIT) {
  1066. if ((error = softdep_flushworklist(ump->um_mountp, &count, p)))
  1067. allerror = error;
  1068. /* Flushed work items may create new vnodes to clean */
  1069. if (count)
  1070. goto loop;
  1071. }
  1072. if (waitfor != MNT_LAZY) {
  1073. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
  1074. if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
  1075. allerror = error;
  1076. VOP_UNLOCK(ump->um_devvp, 0, p);
  1077. }
  1078. qsync(mp);
  1079. /*
  1080. * Write back modified superblock.
  1081. */
  1082. if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
  1083. allerror = error;
  1084. return (allerror);
  1085. }
  1086. /*
  1087. * Look up a FFS dinode number to find its incore vnode, otherwise read it
  1088. * in from disk. If it is in core, wait for the lock bit to clear, then
  1089. * return the inode locked. Detection and handling of mount points must be
  1090. * done by the calling routine.
  1091. */
  1092. int
  1093. ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
  1094. {
  1095. struct fs *fs;
  1096. struct inode *ip;
  1097. struct ufs1_dinode *dp1;
  1098. #ifdef FFS2
  1099. struct ufs2_dinode *dp2;
  1100. #endif
  1101. struct ufsmount *ump;
  1102. struct buf *bp;
  1103. struct vnode *vp;
  1104. dev_t dev;
  1105. int error;
  1106. if (ino > (ufsino_t)-1)
  1107. panic("ffs_vget: alien ino_t %llu", (unsigned long long)ino);
  1108. ump = VFSTOUFS(mp);
  1109. dev = ump->um_dev;
  1110. retry:
  1111. if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
  1112. return (0);
  1113. /* Allocate a new vnode/inode. */
  1114. if ((error = getnewvnode(VT_UFS, mp, &ffs_vops, &vp)) != 0) {
  1115. *vpp = NULL;
  1116. return (error);
  1117. }
  1118. #ifdef VFSLCKDEBUG
  1119. vp->v_flag |= VLOCKSWORK;
  1120. #endif
  1121. ip = pool_get(&ffs_ino_pool, PR_WAITOK|PR_ZERO);
  1122. lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
  1123. ip->i_ump = ump;
  1124. vref(ip->i_devvp);
  1125. vp->v_data = ip;
  1126. ip->i_vnode = vp;
  1127. ip->i_fs = fs = ump->um_fs;
  1128. ip->i_dev = dev;
  1129. ip->i_number = ino;
  1130. ip->i_vtbl = &ffs_vtbl;
  1131. /*
  1132. * Put it onto its hash chain and lock it so that other requests for
  1133. * this inode will block if they arrive while we are sleeping waiting
  1134. * for old data structures to be purged or for the contents of the
  1135. * disk portion of this inode to be read.
  1136. */
  1137. error = ufs_ihashins(ip);
  1138. if (error) {
  1139. /*
  1140. * VOP_INACTIVE will treat this as a stale file
  1141. * and recycle it quickly
  1142. */
  1143. vrele(vp);
  1144. if (error == EEXIST)
  1145. goto retry;
  1146. return (error);
  1147. }
  1148. /* Read in the disk contents for the inode, copy into the inode. */
  1149. error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
  1150. (int)fs->fs_bsize, &bp);
  1151. if (error) {
  1152. /*
  1153. * The inode does not contain anything useful, so it would
  1154. * be misleading to leave it on its hash chain. With mode
  1155. * still zero, it will be unlinked and returned to the free
  1156. * list by vput().
  1157. */
  1158. vput(vp);
  1159. brelse(bp);
  1160. *vpp = NULL;
  1161. return (error);
  1162. }
  1163. #ifdef FFS2
  1164. if (ip->i_ump->um_fstype == UM_UFS2) {
  1165. ip->i_din2 = pool_get(&ffs_dinode2_pool, PR_WAITOK);
  1166. dp2 = (struct ufs2_dinode *) bp->b_data + ino_to_fsbo(fs, ino);
  1167. *ip->i_din2 = *dp2;
  1168. } else
  1169. #endif
  1170. {
  1171. ip->i_din1 = pool_get(&ffs_dinode1_pool, PR_WAITOK);
  1172. dp1 = (struct ufs1_dinode *) bp->b_data + ino_to_fsbo(fs, ino);
  1173. *ip->i_din1 = *dp1;
  1174. }
  1175. brelse(bp);
  1176. if (DOINGSOFTDEP(vp))
  1177. softdep_load_inodeblock(ip);
  1178. else
  1179. ip->i_effnlink = DIP(ip, nlink);
  1180. /*
  1181. * Initialize the vnode from the inode, check for aliases.
  1182. * Note that the underlying vnode may have changed.
  1183. */
  1184. error = ufs_vinit(mp, &ffs_specvops, FFS_FIFOOPS, &vp);
  1185. if (error) {
  1186. vput(vp);
  1187. *vpp = NULL;
  1188. return (error);
  1189. }
  1190. /*
  1191. * Set up a generation number for this inode if it does not
  1192. * already have one. This should only happen on old filesystems.
  1193. */
  1194. if (DIP(ip, gen) == 0) {
  1195. DIP_ASSIGN(ip, gen, arc4random() & INT_MAX);
  1196. if (DIP(ip, gen) == 0 || DIP(ip, gen) == -1)
  1197. DIP_ASSIGN(ip, gen, 1); /* Shouldn't happen */
  1198. if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
  1199. ip->i_flag |= IN_MODIFIED;
  1200. }
  1201. /*
  1202. * Ensure that uid and gid are correct. This is a temporary
  1203. * fix until fsck has been changed to do the update.
  1204. */
  1205. if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_inodefmt < FS_44INODEFMT) {
  1206. ip->i_ffs1_uid = ip->i_din1->di_ouid;
  1207. ip->i_ffs1_gid = ip->i_din1->di_ogid;
  1208. }
  1209. *vpp = vp;
  1210. return (0);
  1211. }
  1212. /*
  1213. * File handle to vnode
  1214. *
  1215. * Have to be really careful about stale file handles:
  1216. * - check that the inode number is valid
  1217. * - call ffs_vget() to get the locked inode
  1218. * - check for an unallocated inode (i_mode == 0)
  1219. */
  1220. int
  1221. ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
  1222. {
  1223. struct ufid *ufhp;
  1224. struct fs *fs;
  1225. ufhp = (struct ufid *)fhp;
  1226. fs = VFSTOUFS(mp)->um_fs;
  1227. if (ufhp->ufid_len != sizeof(*ufhp) ||
  1228. ufhp->ufid_ino < ROOTINO ||
  1229. ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
  1230. return (ESTALE);
  1231. return (ufs_fhtovp(mp, ufhp, vpp));
  1232. }
  1233. /*
  1234. * Vnode pointer to File handle
  1235. */
  1236. int
  1237. ffs_vptofh(struct vnode *vp, struct fid *fhp)
  1238. {
  1239. struct inode *ip;
  1240. struct ufid *ufhp;
  1241. ip = VTOI(vp);
  1242. ufhp = (struct ufid *)fhp;
  1243. ufhp->ufid_len = sizeof(struct ufid);
  1244. ufhp->ufid_ino = ip->i_number;
  1245. ufhp->ufid_gen = DIP(ip, gen);
  1246. return (0);
  1247. }
  1248. /*
  1249. * Write a superblock and associated information back to disk.
  1250. */
  1251. int
  1252. ffs_sbupdate(struct ufsmount *mp, int waitfor)
  1253. {
  1254. struct fs *dfs, *fs = mp->um_fs;
  1255. struct buf *bp;
  1256. int blks;
  1257. caddr_t space;
  1258. int i, size, error, allerror = 0;
  1259. /*
  1260. * First write back the summary information.
  1261. */
  1262. blks = howmany(fs->fs_cssize, fs->fs_fsize);
  1263. space = (caddr_t)fs->fs_csp;
  1264. for (i = 0; i < blks; i += fs->fs_frag) {
  1265. size = fs->fs_bsize;
  1266. if (i + fs->fs_frag > blks)
  1267. size = (blks - i) * fs->fs_fsize;
  1268. bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
  1269. size, 0, 0);
  1270. memcpy(bp->b_data, space, size);
  1271. space += size;
  1272. if (waitfor != MNT_WAIT)
  1273. bawrite(bp);
  1274. else if ((error = bwrite(bp)))
  1275. allerror = error;
  1276. }
  1277. /*
  1278. * Now write back the superblock itself. If any errors occurred
  1279. * up to this point, then fail so that the superblock avoids
  1280. * being written out as clean.
  1281. */
  1282. if (allerror) {
  1283. return (allerror);
  1284. }
  1285. bp = getblk(mp->um_devvp,
  1286. fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
  1287. (int)fs->fs_sbsize, 0, 0);
  1288. fs->fs_fmod = 0;
  1289. fs->fs_time = time_second;
  1290. memcpy(bp->b_data, fs, fs->fs_sbsize);
  1291. /* Restore compatibility to old file systems. XXX */
  1292. dfs = (struct fs *)bp->b_data; /* XXX */
  1293. if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
  1294. dfs->fs_nrpos = -1; /* XXX */
  1295. if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
  1296. int32_t *lp, tmp; /* XXX */
  1297. /* XXX */
  1298. lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
  1299. tmp = lp[4]; /* XXX */
  1300. for (i = 4; i > 0; i--) /* XXX */
  1301. lp[i] = lp[i-1]; /* XXX */
  1302. lp[0] = tmp; /* XXX */
  1303. } /* XXX */
  1304. dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
  1305. ffs1_compat_write(dfs, mp);
  1306. if (waitfor != MNT_WAIT)
  1307. bawrite(bp);
  1308. else if ((error = bwrite(bp)))
  1309. allerror = error;
  1310. return (allerror);
  1311. }
  1312. int
  1313. ffs_init(struct vfsconf *vfsp)
  1314. {
  1315. static int done;
  1316. if (done)
  1317. return (0);
  1318. done = 1;
  1319. pool_init(&ffs_ino_pool, sizeof(struct inode), 0, 0, PR_WAITOK,
  1320. "ffsino", NULL);
  1321. pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0,
  1322. PR_WAITOK, "dino1pl", NULL);
  1323. #ifdef FFS2
  1324. pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0,
  1325. PR_WAITOK, "dino2pl", NULL);
  1326. #endif
  1327. softdep_initialize();
  1328. return (ufs_init(vfsp));
  1329. }
  1330. /*
  1331. * fast filesystem related variables.
  1332. */
  1333. int
  1334. ffs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
  1335. size_t newlen, struct proc *p)
  1336. {
  1337. extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
  1338. #ifdef FFS_SOFTUPDATES
  1339. extern int max_softdeps, tickdelay, stat_worklist_push;
  1340. extern int stat_blk_limit_push, stat_ino_limit_push, stat_blk_limit_hit;
  1341. extern int stat_ino_limit_hit, stat_sync_limit_hit, stat_indir_blk_ptrs;
  1342. extern int stat_inode_bitmap, stat_direct_blk_ptrs, stat_dir_entry;
  1343. #endif
  1344. /* all sysctl names at this level are terminal */
  1345. if (namelen != 1)
  1346. return (ENOTDIR); /* overloaded */
  1347. switch (name[0]) {
  1348. case FFS_CLUSTERREAD:
  1349. return (sysctl_int(oldp, oldlenp, newp, newlen,
  1350. &doclusterread));
  1351. case FFS_CLUSTERWRITE:
  1352. return (sysctl_int(oldp, oldlenp, newp, newlen,
  1353. &doclusterwrite));
  1354. case FFS_REALLOCBLKS:
  1355. return (sysctl_int(oldp, oldlenp, newp, newlen,
  1356. &doreallocblks));
  1357. case FFS_ASYNCFREE:
  1358. return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
  1359. #ifdef FFS_SOFTUPDATES
  1360. case FFS_MAX_SOFTDEPS:
  1361. return (sysctl_int(oldp, oldlenp, newp, newlen, &max_softdeps));
  1362. case FFS_SD_TICKDELAY:
  1363. return (sysctl_int(oldp, oldlenp, newp, newlen, &tickdelay));
  1364. case FFS_SD_WORKLIST_PUSH:
  1365. return (sysctl_rdint(oldp, oldlenp, newp, stat_worklist_push));
  1366. case FFS_SD_BLK_LIMIT_PUSH:
  1367. return (sysctl_rdint(oldp, oldlenp, newp, stat_blk_limit_push));
  1368. case FFS_SD_INO_LIMIT_PUSH:
  1369. return (sysctl_rdint(oldp, oldlenp, newp, stat_ino_limit_push));
  1370. case FFS_SD_BLK_LIMIT_HIT:
  1371. return (sysctl_rdint(oldp, oldlenp, newp, stat_blk_limit_hit));
  1372. case FFS_SD_INO_LIMIT_HIT:
  1373. return (sysctl_rdint(oldp, oldlenp, newp, stat_ino_limit_hit));
  1374. case FFS_SD_SYNC_LIMIT_HIT:
  1375. return (sysctl_rdint(oldp, oldlenp, newp, stat_sync_limit_hit));
  1376. case FFS_SD_INDIR_BLK_PTRS:
  1377. return (sysctl_rdint(oldp, oldlenp, newp, stat_indir_blk_ptrs));
  1378. case FFS_SD_INODE_BITMAP:
  1379. return (sysctl_rdint(oldp, oldlenp, newp, stat_inode_bitmap));
  1380. case FFS_SD_DIRECT_BLK_PTRS:
  1381. return (sysctl_rdint(oldp, oldlenp, newp, stat_direct_blk_ptrs));
  1382. case FFS_SD_DIR_ENTRY:
  1383. return (sysctl_rdint(oldp, oldlenp, newp, stat_dir_entry));
  1384. #endif
  1385. #ifdef UFS_DIRHASH
  1386. case FFS_DIRHASH_DIRSIZE:
  1387. return (sysctl_int(oldp, oldlenp, newp, newlen,
  1388. &ufs_mindirhashsize));
  1389. case FFS_DIRHASH_MAXMEM:
  1390. return (sysctl_int(oldp, oldlenp, newp, newlen,
  1391. &ufs_dirhashmaxmem));
  1392. case FFS_DIRHASH_MEM:
  1393. return (sysctl_rdint(oldp, oldlenp, newp, ufs_dirhashmem));
  1394. #endif
  1395. default:
  1396. return (EOPNOTSUPP);
  1397. }
  1398. /* NOTREACHED */
  1399. }