PageRenderTime 117ms CodeModel.GetById 36ms RepoModel.GetById 2ms app.codeStats 1ms

/sys/ufs/ffs/ffs_vfsops.c

https://bitbucket.org/gthummalapalle/minix
C | 2144 lines | 1567 code | 184 blank | 393 comment | 401 complexity | 83ac6bcbf6505dbe547ad438974d1365 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, WTFPL, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /* $NetBSD: ffs_vfsops.c,v 1.271 2011/11/14 18:35:14 hannken Exp $ */
  2. /*-
  3. * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Wasabi Systems, Inc, and by Andrew Doran.
  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. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * Copyright (c) 1989, 1991, 1993, 1994
  32. * The Regents of the University of California. All rights reserved.
  33. *
  34. * Redistribution and use in source and binary forms, with or without
  35. * modification, are permitted provided that the following conditions
  36. * are met:
  37. * 1. Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. * 2. Redistributions in binary form must reproduce the above copyright
  40. * notice, this list of conditions and the following disclaimer in the
  41. * documentation and/or other materials provided with the distribution.
  42. * 3. Neither the name of the University nor the names of its contributors
  43. * may be used to endorse or promote products derived from this software
  44. * without specific prior written permission.
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  47. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  49. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  50. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  52. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  54. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  56. * SUCH DAMAGE.
  57. *
  58. * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95
  59. */
  60. #include <sys/cdefs.h>
  61. __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.271 2011/11/14 18:35:14 hannken Exp $");
  62. #if defined(_KERNEL_OPT)
  63. #include "opt_ffs.h"
  64. #include "opt_quota.h"
  65. #include "opt_wapbl.h"
  66. #endif
  67. #include <sys/param.h>
  68. #include <sys/systm.h>
  69. #include <sys/namei.h>
  70. #include <sys/proc.h>
  71. #include <sys/kernel.h>
  72. #include <sys/vnode.h>
  73. #include <sys/socket.h>
  74. #include <sys/mount.h>
  75. #include <sys/buf.h>
  76. #include <sys/device.h>
  77. #include <sys/disk.h>
  78. #include <sys/mbuf.h>
  79. #include <sys/file.h>
  80. #include <sys/disklabel.h>
  81. #include <sys/ioctl.h>
  82. #include <sys/errno.h>
  83. #include <sys/malloc.h>
  84. #include <sys/pool.h>
  85. #include <sys/lock.h>
  86. #include <sys/sysctl.h>
  87. #include <sys/conf.h>
  88. #include <sys/kauth.h>
  89. #include <sys/wapbl.h>
  90. #include <sys/fstrans.h>
  91. #include <sys/module.h>
  92. #include <miscfs/genfs/genfs.h>
  93. #include <miscfs/specfs/specdev.h>
  94. #include <ufs/ufs/quota.h>
  95. #include <ufs/ufs/ufsmount.h>
  96. #include <ufs/ufs/inode.h>
  97. #include <ufs/ufs/dir.h>
  98. #include <ufs/ufs/ufs_extern.h>
  99. #include <ufs/ufs/ufs_bswap.h>
  100. #include <ufs/ufs/ufs_wapbl.h>
  101. #include <ufs/ffs/fs.h>
  102. #include <ufs/ffs/ffs_extern.h>
  103. MODULE(MODULE_CLASS_VFS, ffs, NULL);
  104. static int ffs_vfs_fsync(vnode_t *, int);
  105. static struct sysctllog *ffs_sysctl_log;
  106. /* how many times ffs_init() was called */
  107. int ffs_initcount = 0;
  108. extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
  109. extern const struct vnodeopv_desc ffs_specop_opv_desc;
  110. extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
  111. const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
  112. &ffs_vnodeop_opv_desc,
  113. &ffs_specop_opv_desc,
  114. &ffs_fifoop_opv_desc,
  115. NULL,
  116. };
  117. struct vfsops ffs_vfsops = {
  118. MOUNT_FFS,
  119. sizeof (struct ufs_args),
  120. ffs_mount,
  121. ufs_start,
  122. ffs_unmount,
  123. ufs_root,
  124. ufs_quotactl,
  125. ffs_statvfs,
  126. ffs_sync,
  127. ffs_vget,
  128. ffs_fhtovp,
  129. ffs_vptofh,
  130. ffs_init,
  131. ffs_reinit,
  132. ffs_done,
  133. ffs_mountroot,
  134. ffs_snapshot,
  135. ffs_extattrctl,
  136. ffs_suspendctl,
  137. genfs_renamelock_enter,
  138. genfs_renamelock_exit,
  139. ffs_vfs_fsync,
  140. ffs_vnodeopv_descs,
  141. 0,
  142. { NULL, NULL },
  143. };
  144. static const struct genfs_ops ffs_genfsops = {
  145. .gop_size = ffs_gop_size,
  146. .gop_alloc = ufs_gop_alloc,
  147. .gop_write = genfs_gop_write,
  148. .gop_markupdate = ufs_gop_markupdate,
  149. };
  150. static const struct ufs_ops ffs_ufsops = {
  151. .uo_itimes = ffs_itimes,
  152. .uo_update = ffs_update,
  153. .uo_truncate = ffs_truncate,
  154. .uo_valloc = ffs_valloc,
  155. .uo_vfree = ffs_vfree,
  156. .uo_balloc = ffs_balloc,
  157. .uo_unmark_vnode = (void (*)(vnode_t *))nullop,
  158. };
  159. static int
  160. ffs_modcmd(modcmd_t cmd, void *arg)
  161. {
  162. int error;
  163. #if 0
  164. extern int doasyncfree;
  165. #endif
  166. #ifdef UFS_EXTATTR
  167. extern int ufs_extattr_autocreate;
  168. #endif
  169. extern int ffs_log_changeopt;
  170. switch (cmd) {
  171. case MODULE_CMD_INIT:
  172. error = vfs_attach(&ffs_vfsops);
  173. if (error != 0)
  174. break;
  175. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  176. CTLFLAG_PERMANENT,
  177. CTLTYPE_NODE, "vfs", NULL,
  178. NULL, 0, NULL, 0,
  179. CTL_VFS, CTL_EOL);
  180. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  181. CTLFLAG_PERMANENT,
  182. CTLTYPE_NODE, "ffs",
  183. SYSCTL_DESCR("Berkeley Fast File System"),
  184. NULL, 0, NULL, 0,
  185. CTL_VFS, 1, CTL_EOL);
  186. /*
  187. * @@@ should we even bother with these first three?
  188. */
  189. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  190. CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  191. CTLTYPE_INT, "doclusterread", NULL,
  192. sysctl_notavail, 0, NULL, 0,
  193. CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
  194. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  195. CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  196. CTLTYPE_INT, "doclusterwrite", NULL,
  197. sysctl_notavail, 0, NULL, 0,
  198. CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
  199. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  200. CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  201. CTLTYPE_INT, "doreallocblks", NULL,
  202. sysctl_notavail, 0, NULL, 0,
  203. CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
  204. #if 0
  205. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  206. CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  207. CTLTYPE_INT, "doasyncfree",
  208. SYSCTL_DESCR("Release dirty blocks asynchronously"),
  209. NULL, 0, &doasyncfree, 0,
  210. CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
  211. #endif
  212. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  213. CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  214. CTLTYPE_INT, "log_changeopt",
  215. SYSCTL_DESCR("Log changes in optimization strategy"),
  216. NULL, 0, &ffs_log_changeopt, 0,
  217. CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
  218. #ifdef UFS_EXTATTR
  219. sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
  220. CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
  221. CTLTYPE_INT, "extattr_autocreate",
  222. SYSCTL_DESCR("Size of attribute for "
  223. "backing file autocreation"),
  224. NULL, 0, &ufs_extattr_autocreate, 0,
  225. CTL_VFS, 1, FFS_EXTATTR_AUTOCREATE, CTL_EOL);
  226. #endif /* UFS_EXTATTR */
  227. break;
  228. case MODULE_CMD_FINI:
  229. error = vfs_detach(&ffs_vfsops);
  230. if (error != 0)
  231. break;
  232. sysctl_teardown(&ffs_sysctl_log);
  233. break;
  234. default:
  235. error = ENOTTY;
  236. break;
  237. }
  238. return (error);
  239. }
  240. pool_cache_t ffs_inode_cache;
  241. pool_cache_t ffs_dinode1_cache;
  242. pool_cache_t ffs_dinode2_cache;
  243. static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
  244. static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
  245. /*
  246. * Called by main() when ffs is going to be mounted as root.
  247. */
  248. int
  249. ffs_mountroot(void)
  250. {
  251. struct fs *fs;
  252. struct mount *mp;
  253. struct lwp *l = curlwp; /* XXX */
  254. struct ufsmount *ump;
  255. int error;
  256. if (device_class(root_device) != DV_DISK)
  257. return (ENODEV);
  258. if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
  259. vrele(rootvp);
  260. return (error);
  261. }
  262. /*
  263. * We always need to be able to mount the root file system.
  264. */
  265. mp->mnt_flag |= MNT_FORCE;
  266. if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
  267. vfs_unbusy(mp, false, NULL);
  268. vfs_destroy(mp);
  269. return (error);
  270. }
  271. mp->mnt_flag &= ~MNT_FORCE;
  272. mutex_enter(&mountlist_lock);
  273. CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
  274. mutex_exit(&mountlist_lock);
  275. ump = VFSTOUFS(mp);
  276. fs = ump->um_fs;
  277. memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
  278. (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
  279. (void)ffs_statvfs(mp, &mp->mnt_stat);
  280. vfs_unbusy(mp, false, NULL);
  281. setrootfstime((time_t)fs->fs_time);
  282. return (0);
  283. }
  284. /*
  285. * VFS Operations.
  286. *
  287. * mount system call
  288. */
  289. int
  290. ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
  291. {
  292. struct lwp *l = curlwp;
  293. struct vnode *devvp = NULL;
  294. struct ufs_args *args = data;
  295. struct ufsmount *ump = NULL;
  296. struct fs *fs;
  297. int error = 0, flags, update;
  298. mode_t accessmode;
  299. if (*data_len < sizeof *args)
  300. return EINVAL;
  301. if (mp->mnt_flag & MNT_GETARGS) {
  302. ump = VFSTOUFS(mp);
  303. if (ump == NULL)
  304. return EIO;
  305. args->fspec = NULL;
  306. *data_len = sizeof *args;
  307. return 0;
  308. }
  309. update = mp->mnt_flag & MNT_UPDATE;
  310. /* Check arguments */
  311. if (args->fspec != NULL) {
  312. /*
  313. * Look up the name and verify that it's sane.
  314. */
  315. error = namei_simple_user(args->fspec,
  316. NSM_FOLLOW_NOEMULROOT, &devvp);
  317. if (error != 0)
  318. return (error);
  319. if (!update) {
  320. /*
  321. * Be sure this is a valid block device
  322. */
  323. if (devvp->v_type != VBLK)
  324. error = ENOTBLK;
  325. else if (bdevsw_lookup(devvp->v_rdev) == NULL)
  326. error = ENXIO;
  327. } else {
  328. /*
  329. * Be sure we're still naming the same device
  330. * used for our initial mount
  331. */
  332. ump = VFSTOUFS(mp);
  333. if (devvp != ump->um_devvp) {
  334. if (devvp->v_rdev != ump->um_devvp->v_rdev)
  335. error = EINVAL;
  336. else {
  337. vrele(devvp);
  338. devvp = ump->um_devvp;
  339. vref(devvp);
  340. }
  341. }
  342. }
  343. } else {
  344. if (!update) {
  345. /* New mounts must have a filename for the device */
  346. return (EINVAL);
  347. } else {
  348. /* Use the extant mount */
  349. ump = VFSTOUFS(mp);
  350. devvp = ump->um_devvp;
  351. vref(devvp);
  352. }
  353. }
  354. /*
  355. * If mount by non-root, then verify that user has necessary
  356. * permissions on the device.
  357. *
  358. * Permission to update a mount is checked higher, so here we presume
  359. * updating the mount is okay (for example, as far as securelevel goes)
  360. * which leaves us with the normal check.
  361. */
  362. if (error == 0) {
  363. accessmode = VREAD;
  364. if (update ?
  365. (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
  366. (mp->mnt_flag & MNT_RDONLY) == 0)
  367. accessmode |= VWRITE;
  368. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
  369. error = genfs_can_mount(devvp, accessmode, l->l_cred);
  370. VOP_UNLOCK(devvp);
  371. }
  372. if (error) {
  373. vrele(devvp);
  374. return (error);
  375. }
  376. #ifdef WAPBL
  377. /* WAPBL can only be enabled on a r/w mount. */
  378. if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
  379. mp->mnt_flag &= ~MNT_LOG;
  380. }
  381. #else /* !WAPBL */
  382. mp->mnt_flag &= ~MNT_LOG;
  383. #endif /* !WAPBL */
  384. if (!update) {
  385. int xflags;
  386. if (mp->mnt_flag & MNT_RDONLY)
  387. xflags = FREAD;
  388. else
  389. xflags = FREAD | FWRITE;
  390. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
  391. error = VOP_OPEN(devvp, xflags, FSCRED);
  392. VOP_UNLOCK(devvp);
  393. if (error)
  394. goto fail;
  395. error = ffs_mountfs(devvp, mp, l);
  396. if (error) {
  397. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
  398. (void)VOP_CLOSE(devvp, xflags, NOCRED);
  399. VOP_UNLOCK(devvp);
  400. goto fail;
  401. }
  402. ump = VFSTOUFS(mp);
  403. fs = ump->um_fs;
  404. } else {
  405. /*
  406. * Update the mount.
  407. */
  408. /*
  409. * The initial mount got a reference on this
  410. * device, so drop the one obtained via
  411. * namei(), above.
  412. */
  413. vrele(devvp);
  414. ump = VFSTOUFS(mp);
  415. fs = ump->um_fs;
  416. if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
  417. /*
  418. * Changing from r/w to r/o
  419. */
  420. flags = WRITECLOSE;
  421. if (mp->mnt_flag & MNT_FORCE)
  422. flags |= FORCECLOSE;
  423. error = ffs_flushfiles(mp, flags, l);
  424. if (error == 0)
  425. error = UFS_WAPBL_BEGIN(mp);
  426. if (error == 0 &&
  427. ffs_cgupdate(ump, MNT_WAIT) == 0 &&
  428. fs->fs_clean & FS_WASCLEAN) {
  429. if (mp->mnt_flag & MNT_SOFTDEP)
  430. fs->fs_flags &= ~FS_DOSOFTDEP;
  431. fs->fs_clean = FS_ISCLEAN;
  432. (void) ffs_sbupdate(ump, MNT_WAIT);
  433. }
  434. if (error == 0)
  435. UFS_WAPBL_END(mp);
  436. if (error)
  437. return (error);
  438. }
  439. #ifdef WAPBL
  440. if ((mp->mnt_flag & MNT_LOG) == 0) {
  441. error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
  442. if (error)
  443. return error;
  444. }
  445. #endif /* WAPBL */
  446. if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
  447. /*
  448. * Finish change from r/w to r/o
  449. */
  450. fs->fs_ronly = 1;
  451. fs->fs_fmod = 0;
  452. }
  453. if (mp->mnt_flag & MNT_RELOAD) {
  454. error = ffs_reload(mp, l->l_cred, l);
  455. if (error)
  456. return (error);
  457. }
  458. if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
  459. /*
  460. * Changing from read-only to read/write
  461. */
  462. #ifndef QUOTA2
  463. if (fs->fs_flags & FS_DOQUOTA2) {
  464. ump->um_flags |= UFS_QUOTA2;
  465. uprintf("%s: options QUOTA2 not enabled%s\n",
  466. mp->mnt_stat.f_mntonname,
  467. (mp->mnt_flag & MNT_FORCE) ? "" :
  468. ", not mounting");
  469. return EINVAL;
  470. }
  471. #endif
  472. fs->fs_ronly = 0;
  473. fs->fs_clean <<= 1;
  474. fs->fs_fmod = 1;
  475. #ifdef WAPBL
  476. if (fs->fs_flags & FS_DOWAPBL) {
  477. printf("%s: replaying log to disk\n",
  478. fs->fs_fsmnt);
  479. KDASSERT(mp->mnt_wapbl_replay);
  480. error = wapbl_replay_write(mp->mnt_wapbl_replay,
  481. devvp);
  482. if (error) {
  483. return error;
  484. }
  485. wapbl_replay_stop(mp->mnt_wapbl_replay);
  486. fs->fs_clean = FS_WASCLEAN;
  487. }
  488. #endif /* WAPBL */
  489. if (fs->fs_snapinum[0] != 0)
  490. ffs_snapshot_mount(mp);
  491. }
  492. #ifdef WAPBL
  493. error = ffs_wapbl_start(mp);
  494. if (error)
  495. return error;
  496. #endif /* WAPBL */
  497. #ifdef QUOTA2
  498. if (!fs->fs_ronly) {
  499. error = ffs_quota2_mount(mp);
  500. if (error) {
  501. return error;
  502. }
  503. }
  504. #endif
  505. if (args->fspec == NULL)
  506. return 0;
  507. }
  508. error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
  509. UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
  510. if (error == 0)
  511. (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
  512. sizeof(fs->fs_fsmnt));
  513. fs->fs_flags &= ~FS_DOSOFTDEP;
  514. if (fs->fs_fmod != 0) { /* XXX */
  515. int err;
  516. fs->fs_fmod = 0;
  517. if (fs->fs_clean & FS_WASCLEAN)
  518. fs->fs_time = time_second;
  519. else {
  520. printf("%s: file system not clean (fs_clean=%#x); "
  521. "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
  522. fs->fs_clean);
  523. printf("%s: lost blocks %" PRId64 " files %d\n",
  524. mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
  525. fs->fs_pendinginodes);
  526. }
  527. err = UFS_WAPBL_BEGIN(mp);
  528. if (err == 0) {
  529. (void) ffs_cgupdate(ump, MNT_WAIT);
  530. UFS_WAPBL_END(mp);
  531. }
  532. }
  533. if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
  534. printf("%s: `-o softdep' is no longer supported, "
  535. "consider `-o log'\n", mp->mnt_stat.f_mntfromname);
  536. mp->mnt_flag &= ~MNT_SOFTDEP;
  537. }
  538. return (error);
  539. fail:
  540. vrele(devvp);
  541. return (error);
  542. }
  543. /*
  544. * Reload all incore data for a filesystem (used after running fsck on
  545. * the root filesystem and finding things to fix). The filesystem must
  546. * be mounted read-only.
  547. *
  548. * Things to do to update the mount:
  549. * 1) invalidate all cached meta-data.
  550. * 2) re-read superblock from disk.
  551. * 3) re-read summary information from disk.
  552. * 4) invalidate all inactive vnodes.
  553. * 5) invalidate all cached file data.
  554. * 6) re-read inode data for all active vnodes.
  555. */
  556. int
  557. ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
  558. {
  559. struct vnode *vp, *mvp, *devvp;
  560. struct inode *ip;
  561. void *space;
  562. struct buf *bp;
  563. struct fs *fs, *newfs;
  564. struct dkwedge_info dkw;
  565. int i, bsize, blks, error;
  566. int32_t *lp;
  567. struct ufsmount *ump;
  568. daddr_t sblockloc;
  569. if ((mp->mnt_flag & MNT_RDONLY) == 0)
  570. return (EINVAL);
  571. ump = VFSTOUFS(mp);
  572. /*
  573. * Step 1: invalidate all cached meta-data.
  574. */
  575. devvp = ump->um_devvp;
  576. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
  577. error = vinvalbuf(devvp, 0, cred, l, 0, 0);
  578. VOP_UNLOCK(devvp);
  579. if (error)
  580. panic("ffs_reload: dirty1");
  581. /*
  582. * Step 2: re-read superblock from disk.
  583. */
  584. fs = ump->um_fs;
  585. /* XXX we don't handle possibility that superblock moved. */
  586. error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, fs->fs_sbsize,
  587. NOCRED, 0, &bp);
  588. if (error) {
  589. brelse(bp, 0);
  590. return (error);
  591. }
  592. newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
  593. memcpy(newfs, bp->b_data, fs->fs_sbsize);
  594. #ifdef FFS_EI
  595. if (ump->um_flags & UFS_NEEDSWAP) {
  596. ffs_sb_swap((struct fs*)bp->b_data, newfs);
  597. fs->fs_flags |= FS_SWAPPED;
  598. } else
  599. #endif
  600. fs->fs_flags &= ~FS_SWAPPED;
  601. if ((newfs->fs_magic != FS_UFS1_MAGIC &&
  602. newfs->fs_magic != FS_UFS2_MAGIC)||
  603. newfs->fs_bsize > MAXBSIZE ||
  604. newfs->fs_bsize < sizeof(struct fs)) {
  605. brelse(bp, 0);
  606. free(newfs, M_UFSMNT);
  607. return (EIO); /* XXX needs translation */
  608. }
  609. /* Store off old fs_sblockloc for fs_oldfscompat_read. */
  610. sblockloc = fs->fs_sblockloc;
  611. /*
  612. * Copy pointer fields back into superblock before copying in XXX
  613. * new superblock. These should really be in the ufsmount. XXX
  614. * Note that important parameters (eg fs_ncg) are unchanged.
  615. */
  616. newfs->fs_csp = fs->fs_csp;
  617. newfs->fs_maxcluster = fs->fs_maxcluster;
  618. newfs->fs_contigdirs = fs->fs_contigdirs;
  619. newfs->fs_ronly = fs->fs_ronly;
  620. newfs->fs_active = fs->fs_active;
  621. memcpy(fs, newfs, (u_int)fs->fs_sbsize);
  622. brelse(bp, 0);
  623. free(newfs, M_UFSMNT);
  624. /* Recheck for apple UFS filesystem */
  625. ump->um_flags &= ~UFS_ISAPPLEUFS;
  626. /* First check to see if this is tagged as an Apple UFS filesystem
  627. * in the disklabel
  628. */
  629. if (getdiskinfo(devvp, &dkw) == 0 &&
  630. strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
  631. ump->um_flags |= UFS_ISAPPLEUFS;
  632. #ifdef APPLE_UFS
  633. else {
  634. /* Manually look for an apple ufs label, and if a valid one
  635. * is found, then treat it like an Apple UFS filesystem anyway
  636. *
  637. * EINVAL is most probably a blocksize or alignment problem,
  638. * it is unlikely that this is an Apple UFS filesystem then.
  639. */
  640. error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
  641. APPLEUFS_LABEL_SIZE, cred, 0, &bp);
  642. if (error && error != EINVAL) {
  643. brelse(bp, 0);
  644. return (error);
  645. }
  646. if (error == 0) {
  647. error = ffs_appleufs_validate(fs->fs_fsmnt,
  648. (struct appleufslabel *)bp->b_data, NULL);
  649. if (error == 0)
  650. ump->um_flags |= UFS_ISAPPLEUFS;
  651. }
  652. brelse(bp, 0);
  653. bp = NULL;
  654. }
  655. #else
  656. if (ump->um_flags & UFS_ISAPPLEUFS)
  657. return (EIO);
  658. #endif
  659. if (UFS_MPISAPPLEUFS(ump)) {
  660. /* see comment about NeXT below */
  661. ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
  662. ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
  663. mp->mnt_iflag |= IMNT_DTYPE;
  664. } else {
  665. ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
  666. ump->um_dirblksiz = DIRBLKSIZ;
  667. if (ump->um_maxsymlinklen > 0)
  668. mp->mnt_iflag |= IMNT_DTYPE;
  669. else
  670. mp->mnt_iflag &= ~IMNT_DTYPE;
  671. }
  672. ffs_oldfscompat_read(fs, ump, sblockloc);
  673. mutex_enter(&ump->um_lock);
  674. ump->um_maxfilesize = fs->fs_maxfilesize;
  675. if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
  676. uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
  677. mp->mnt_stat.f_mntonname, fs->fs_flags,
  678. (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
  679. if ((mp->mnt_flag & MNT_FORCE) == 0) {
  680. mutex_exit(&ump->um_lock);
  681. return (EINVAL);
  682. }
  683. }
  684. if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
  685. fs->fs_pendingblocks = 0;
  686. fs->fs_pendinginodes = 0;
  687. }
  688. mutex_exit(&ump->um_lock);
  689. ffs_statvfs(mp, &mp->mnt_stat);
  690. /*
  691. * Step 3: re-read summary information from disk.
  692. */
  693. blks = howmany(fs->fs_cssize, fs->fs_fsize);
  694. space = fs->fs_csp;
  695. for (i = 0; i < blks; i += fs->fs_frag) {
  696. bsize = fs->fs_bsize;
  697. if (i + fs->fs_frag > blks)
  698. bsize = (blks - i) * fs->fs_fsize;
  699. error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), bsize,
  700. NOCRED, 0, &bp);
  701. if (error) {
  702. brelse(bp, 0);
  703. return (error);
  704. }
  705. #ifdef FFS_EI
  706. if (UFS_FSNEEDSWAP(fs))
  707. ffs_csum_swap((struct csum *)bp->b_data,
  708. (struct csum *)space, bsize);
  709. else
  710. #endif
  711. memcpy(space, bp->b_data, (size_t)bsize);
  712. space = (char *)space + bsize;
  713. brelse(bp, 0);
  714. }
  715. if (fs->fs_snapinum[0] != 0)
  716. ffs_snapshot_mount(mp);
  717. /*
  718. * We no longer know anything about clusters per cylinder group.
  719. */
  720. if (fs->fs_contigsumsize > 0) {
  721. lp = fs->fs_maxcluster;
  722. for (i = 0; i < fs->fs_ncg; i++)
  723. *lp++ = fs->fs_contigsumsize;
  724. }
  725. /* Allocate a marker vnode. */
  726. mvp = vnalloc(mp);
  727. /*
  728. * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
  729. * and vclean() can be called indirectly
  730. */
  731. mutex_enter(&mntvnode_lock);
  732. loop:
  733. for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
  734. vmark(mvp, vp);
  735. if (vp->v_mount != mp || vismarker(vp))
  736. continue;
  737. /*
  738. * Step 4: invalidate all inactive vnodes.
  739. */
  740. if (vrecycle(vp, &mntvnode_lock, l)) {
  741. mutex_enter(&mntvnode_lock);
  742. (void)vunmark(mvp);
  743. goto loop;
  744. }
  745. /*
  746. * Step 5: invalidate all cached file data.
  747. */
  748. mutex_enter(vp->v_interlock);
  749. mutex_exit(&mntvnode_lock);
  750. if (vget(vp, LK_EXCLUSIVE)) {
  751. (void)vunmark(mvp);
  752. goto loop;
  753. }
  754. if (vinvalbuf(vp, 0, cred, l, 0, 0))
  755. panic("ffs_reload: dirty2");
  756. /*
  757. * Step 6: re-read inode data for all active vnodes.
  758. */
  759. ip = VTOI(vp);
  760. error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
  761. (int)fs->fs_bsize, NOCRED, 0, &bp);
  762. if (error) {
  763. brelse(bp, 0);
  764. vput(vp);
  765. (void)vunmark(mvp);
  766. break;
  767. }
  768. ffs_load_inode(bp, ip, fs, ip->i_number);
  769. brelse(bp, 0);
  770. vput(vp);
  771. mutex_enter(&mntvnode_lock);
  772. }
  773. mutex_exit(&mntvnode_lock);
  774. vnfree(mvp);
  775. return (error);
  776. }
  777. /*
  778. * Possible superblock locations ordered from most to least likely.
  779. */
  780. static const int sblock_try[] = SBLOCKSEARCH;
  781. /*
  782. * Common code for mount and mountroot
  783. */
  784. int
  785. ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
  786. {
  787. struct ufsmount *ump;
  788. struct buf *bp;
  789. struct fs *fs;
  790. dev_t dev;
  791. struct dkwedge_info dkw;
  792. void *space;
  793. daddr_t sblockloc, fsblockloc;
  794. int blks, fstype;
  795. int error, i, bsize, ronly, bset = 0;
  796. #ifdef FFS_EI
  797. int needswap = 0; /* keep gcc happy */
  798. #endif
  799. int32_t *lp;
  800. kauth_cred_t cred;
  801. u_int32_t sbsize = 8192; /* keep gcc happy*/
  802. int32_t fsbsize;
  803. dev = devvp->v_rdev;
  804. cred = l ? l->l_cred : NOCRED;
  805. /* Flush out any old buffers remaining from a previous use. */
  806. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
  807. error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
  808. VOP_UNLOCK(devvp);
  809. if (error)
  810. return (error);
  811. ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
  812. bp = NULL;
  813. ump = NULL;
  814. fs = NULL;
  815. sblockloc = 0;
  816. fstype = 0;
  817. error = fstrans_mount(mp);
  818. if (error)
  819. return error;
  820. ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
  821. memset(ump, 0, sizeof *ump);
  822. mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
  823. error = ffs_snapshot_init(ump);
  824. if (error)
  825. goto out;
  826. ump->um_ops = &ffs_ufsops;
  827. #ifdef WAPBL
  828. sbagain:
  829. #endif
  830. /*
  831. * Try reading the superblock in each of its possible locations.
  832. */
  833. for (i = 0; ; i++) {
  834. if (bp != NULL) {
  835. brelse(bp, BC_NOCACHE);
  836. bp = NULL;
  837. }
  838. if (sblock_try[i] == -1) {
  839. error = EINVAL;
  840. fs = NULL;
  841. goto out;
  842. }
  843. error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, cred,
  844. 0, &bp);
  845. if (error) {
  846. fs = NULL;
  847. goto out;
  848. }
  849. fs = (struct fs*)bp->b_data;
  850. fsblockloc = sblockloc = sblock_try[i];
  851. if (fs->fs_magic == FS_UFS1_MAGIC) {
  852. sbsize = fs->fs_sbsize;
  853. fstype = UFS1;
  854. fsbsize = fs->fs_bsize;
  855. #ifdef FFS_EI
  856. needswap = 0;
  857. } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
  858. sbsize = bswap32(fs->fs_sbsize);
  859. fstype = UFS1;
  860. fsbsize = bswap32(fs->fs_bsize);
  861. needswap = 1;
  862. #endif
  863. } else if (fs->fs_magic == FS_UFS2_MAGIC) {
  864. sbsize = fs->fs_sbsize;
  865. fstype = UFS2;
  866. fsbsize = fs->fs_bsize;
  867. #ifdef FFS_EI
  868. needswap = 0;
  869. } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
  870. sbsize = bswap32(fs->fs_sbsize);
  871. fstype = UFS2;
  872. fsbsize = bswap32(fs->fs_bsize);
  873. needswap = 1;
  874. #endif
  875. } else
  876. continue;
  877. /* fs->fs_sblockloc isn't defined for old filesystems */
  878. if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
  879. if (sblockloc == SBLOCK_UFS2)
  880. /*
  881. * This is likely to be the first alternate
  882. * in a filesystem with 64k blocks.
  883. * Don't use it.
  884. */
  885. continue;
  886. fsblockloc = sblockloc;
  887. } else {
  888. fsblockloc = fs->fs_sblockloc;
  889. #ifdef FFS_EI
  890. if (needswap)
  891. fsblockloc = bswap64(fsblockloc);
  892. #endif
  893. }
  894. /* Check we haven't found an alternate superblock */
  895. if (fsblockloc != sblockloc)
  896. continue;
  897. /* Validate size of superblock */
  898. if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
  899. continue;
  900. /* Check that we can handle the file system blocksize */
  901. if (fsbsize > MAXBSIZE) {
  902. printf("ffs_mountfs: block size (%d) > MAXBSIZE (%d)\n",
  903. fsbsize, MAXBSIZE);
  904. continue;
  905. }
  906. /* Ok seems to be a good superblock */
  907. break;
  908. }
  909. fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
  910. memcpy(fs, bp->b_data, sbsize);
  911. ump->um_fs = fs;
  912. #ifdef FFS_EI
  913. if (needswap) {
  914. ffs_sb_swap((struct fs*)bp->b_data, fs);
  915. fs->fs_flags |= FS_SWAPPED;
  916. } else
  917. #endif
  918. fs->fs_flags &= ~FS_SWAPPED;
  919. #ifdef WAPBL
  920. if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
  921. error = ffs_wapbl_replay_start(mp, fs, devvp);
  922. if (error && (mp->mnt_flag & MNT_FORCE) == 0)
  923. goto out;
  924. if (!error) {
  925. if (!ronly) {
  926. /* XXX fsmnt may be stale. */
  927. printf("%s: replaying log to disk\n",
  928. fs->fs_fsmnt);
  929. error = wapbl_replay_write(mp->mnt_wapbl_replay,
  930. devvp);
  931. if (error)
  932. goto out;
  933. wapbl_replay_stop(mp->mnt_wapbl_replay);
  934. fs->fs_clean = FS_WASCLEAN;
  935. } else {
  936. /* XXX fsmnt may be stale */
  937. printf("%s: replaying log to memory\n",
  938. fs->fs_fsmnt);
  939. }
  940. /* Force a re-read of the superblock */
  941. brelse(bp, BC_INVAL);
  942. bp = NULL;
  943. free(fs, M_UFSMNT);
  944. fs = NULL;
  945. goto sbagain;
  946. }
  947. }
  948. #else /* !WAPBL */
  949. if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
  950. error = EPERM;
  951. goto out;
  952. }
  953. #endif /* !WAPBL */
  954. ffs_oldfscompat_read(fs, ump, sblockloc);
  955. ump->um_maxfilesize = fs->fs_maxfilesize;
  956. if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
  957. uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
  958. mp->mnt_stat.f_mntonname, fs->fs_flags,
  959. (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
  960. if ((mp->mnt_flag & MNT_FORCE) == 0) {
  961. error = EINVAL;
  962. goto out;
  963. }
  964. }
  965. if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
  966. fs->fs_pendingblocks = 0;
  967. fs->fs_pendinginodes = 0;
  968. }
  969. ump->um_fstype = fstype;
  970. if (fs->fs_sbsize < SBLOCKSIZE)
  971. brelse(bp, BC_INVAL);
  972. else
  973. brelse(bp, 0);
  974. bp = NULL;
  975. /* First check to see if this is tagged as an Apple UFS filesystem
  976. * in the disklabel
  977. */
  978. if (getdiskinfo(devvp, &dkw) == 0 &&
  979. strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
  980. ump->um_flags |= UFS_ISAPPLEUFS;
  981. #ifdef APPLE_UFS
  982. else {
  983. /* Manually look for an apple ufs label, and if a valid one
  984. * is found, then treat it like an Apple UFS filesystem anyway
  985. */
  986. error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
  987. APPLEUFS_LABEL_SIZE, cred, 0, &bp);
  988. if (error)
  989. goto out;
  990. error = ffs_appleufs_validate(fs->fs_fsmnt,
  991. (struct appleufslabel *)bp->b_data, NULL);
  992. if (error == 0) {
  993. ump->um_flags |= UFS_ISAPPLEUFS;
  994. }
  995. brelse(bp, 0);
  996. bp = NULL;
  997. }
  998. #else
  999. if (ump->um_flags & UFS_ISAPPLEUFS) {
  1000. error = EINVAL;
  1001. goto out;
  1002. }
  1003. #endif
  1004. #if 0
  1005. /*
  1006. * XXX This code changes the behaviour of mounting dirty filesystems, to
  1007. * XXX require "mount -f ..." to mount them. This doesn't match what
  1008. * XXX mount(8) describes and is disabled for now.
  1009. */
  1010. /*
  1011. * If the file system is not clean, don't allow it to be mounted
  1012. * unless MNT_FORCE is specified. (Note: MNT_FORCE is always set
  1013. * for the root file system.)
  1014. */
  1015. if (fs->fs_flags & FS_DOWAPBL) {
  1016. /*
  1017. * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
  1018. * bit is set, although there's a window in unmount where it
  1019. * could be FS_ISCLEAN
  1020. */
  1021. if ((mp->mnt_flag & MNT_FORCE) == 0 &&
  1022. (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
  1023. error = EPERM;
  1024. goto out;
  1025. }
  1026. } else
  1027. if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
  1028. (mp->mnt_flag & MNT_FORCE) == 0) {
  1029. error = EPERM;
  1030. goto out;
  1031. }
  1032. #endif
  1033. /*
  1034. * verify that we can access the last block in the fs
  1035. * if we're mounting read/write.
  1036. */
  1037. if (!ronly) {
  1038. error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
  1039. cred, 0, &bp);
  1040. if (bp->b_bcount != fs->fs_fsize)
  1041. error = EINVAL;
  1042. if (error) {
  1043. bset = BC_INVAL;
  1044. goto out;
  1045. }
  1046. brelse(bp, BC_INVAL);
  1047. bp = NULL;
  1048. }
  1049. fs->fs_ronly = ronly;
  1050. /* Don't bump fs_clean if we're replaying journal */
  1051. if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
  1052. if (ronly == 0) {
  1053. fs->fs_clean <<= 1;
  1054. fs->fs_fmod = 1;
  1055. }
  1056. bsize = fs->fs_cssize;
  1057. blks = howmany(bsize, fs->fs_fsize);
  1058. if (fs->fs_contigsumsize > 0)
  1059. bsize += fs->fs_ncg * sizeof(int32_t);
  1060. bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
  1061. space = malloc((u_long)bsize, M_UFSMNT, M_WAITOK);
  1062. fs->fs_csp = space;
  1063. for (i = 0; i < blks; i += fs->fs_frag) {
  1064. bsize = fs->fs_bsize;
  1065. if (i + fs->fs_frag > blks)
  1066. bsize = (blks - i) * fs->fs_fsize;
  1067. error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), bsize,
  1068. cred, 0, &bp);
  1069. if (error) {
  1070. free(fs->fs_csp, M_UFSMNT);
  1071. goto out;
  1072. }
  1073. #ifdef FFS_EI
  1074. if (needswap)
  1075. ffs_csum_swap((struct csum *)bp->b_data,
  1076. (struct csum *)space, bsize);
  1077. else
  1078. #endif
  1079. memcpy(space, bp->b_data, (u_int)bsize);
  1080. space = (char *)space + bsize;
  1081. brelse(bp, 0);
  1082. bp = NULL;
  1083. }
  1084. if (fs->fs_contigsumsize > 0) {
  1085. fs->fs_maxcluster = lp = space;
  1086. for (i = 0; i < fs->fs_ncg; i++)
  1087. *lp++ = fs->fs_contigsumsize;
  1088. space = lp;
  1089. }
  1090. bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
  1091. fs->fs_contigdirs = space;
  1092. space = (char *)space + bsize;
  1093. memset(fs->fs_contigdirs, 0, bsize);
  1094. /* Compatibility for old filesystems - XXX */
  1095. if (fs->fs_avgfilesize <= 0)
  1096. fs->fs_avgfilesize = AVFILESIZ;
  1097. if (fs->fs_avgfpdir <= 0)
  1098. fs->fs_avgfpdir = AFPDIR;
  1099. fs->fs_active = NULL;
  1100. mp->mnt_data = ump;
  1101. mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
  1102. mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
  1103. mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
  1104. mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
  1105. if (UFS_MPISAPPLEUFS(ump)) {
  1106. /* NeXT used to keep short symlinks in the inode even
  1107. * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen
  1108. * is probably -1, but we still need to be able to identify
  1109. * short symlinks.
  1110. */
  1111. ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
  1112. ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
  1113. mp->mnt_iflag |= IMNT_DTYPE;
  1114. } else {
  1115. ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
  1116. ump->um_dirblksiz = DIRBLKSIZ;
  1117. if (ump->um_maxsymlinklen > 0)
  1118. mp->mnt_iflag |= IMNT_DTYPE;
  1119. else
  1120. mp->mnt_iflag &= ~IMNT_DTYPE;
  1121. }
  1122. mp->mnt_fs_bshift = fs->fs_bshift;
  1123. mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
  1124. mp->mnt_flag |= MNT_LOCAL;
  1125. mp->mnt_iflag |= IMNT_MPSAFE;
  1126. #ifdef FFS_EI
  1127. if (needswap)
  1128. ump->um_flags |= UFS_NEEDSWAP;
  1129. #endif
  1130. ump->um_mountp = mp;
  1131. ump->um_dev = dev;
  1132. ump->um_devvp = devvp;
  1133. ump->um_nindir = fs->fs_nindir;
  1134. ump->um_lognindir = ffs(fs->fs_nindir) - 1;
  1135. ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
  1136. ump->um_seqinc = fs->fs_frag;
  1137. for (i = 0; i < MAXQUOTAS; i++)
  1138. ump->um_quotas[i] = NULLVP;
  1139. devvp->v_specmountpoint = mp;
  1140. if (ronly == 0 && fs->fs_snapinum[0] != 0)
  1141. ffs_snapshot_mount(mp);
  1142. #ifdef WAPBL
  1143. if (!ronly) {
  1144. KDASSERT(fs->fs_ronly == 0);
  1145. /*
  1146. * ffs_wapbl_start() needs mp->mnt_stat initialised if it
  1147. * needs to create a new log file in-filesystem.
  1148. */
  1149. ffs_statvfs(mp, &mp->mnt_stat);
  1150. error = ffs_wapbl_start(mp);
  1151. if (error) {
  1152. free(fs->fs_csp, M_UFSMNT);
  1153. goto out;
  1154. }
  1155. }
  1156. #endif /* WAPBL */
  1157. if (ronly == 0) {
  1158. #ifdef QUOTA2
  1159. error = ffs_quota2_mount(mp);
  1160. if (error) {
  1161. free(fs->fs_csp, M_UFSMNT);
  1162. goto out;
  1163. }
  1164. #else
  1165. if (fs->fs_flags & FS_DOQUOTA2) {
  1166. ump->um_flags |= UFS_QUOTA2;
  1167. uprintf("%s: options QUOTA2 not enabled%s\n",
  1168. mp->mnt_stat.f_mntonname,
  1169. (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
  1170. if ((mp->mnt_flag & MNT_FORCE) == 0) {
  1171. error = EINVAL;
  1172. free(fs->fs_csp, M_UFSMNT);
  1173. goto out;
  1174. }
  1175. }
  1176. #endif
  1177. }
  1178. #ifdef UFS_EXTATTR
  1179. /*
  1180. * Initialize file-backed extended attributes on UFS1 file
  1181. * systems.
  1182. */
  1183. if (ump->um_fstype == UFS1)
  1184. ufs_extattr_uepm_init(&ump->um_extattr);
  1185. #endif /* UFS_EXTATTR */
  1186. return (0);
  1187. out:
  1188. #ifdef WAPBL
  1189. if (mp->mnt_wapbl_replay) {
  1190. wapbl_replay_stop(mp->mnt_wapbl_replay);
  1191. wapbl_replay_free(mp->mnt_wapbl_replay);
  1192. mp->mnt_wapbl_replay = 0;
  1193. }
  1194. #endif
  1195. fstrans_unmount(mp);
  1196. if (fs)
  1197. free(fs, M_UFSMNT);
  1198. devvp->v_specmountpoint = NULL;
  1199. if (bp)
  1200. brelse(bp, bset);
  1201. if (ump) {
  1202. if (ump->um_oldfscompat)
  1203. free(ump->um_oldfscompat, M_UFSMNT);
  1204. mutex_destroy(&ump->um_lock);
  1205. free(ump, M_UFSMNT);
  1206. mp->mnt_data = NULL;
  1207. }
  1208. return (error);
  1209. }
  1210. /*
  1211. * Sanity checks for loading old filesystem superblocks.
  1212. * See ffs_oldfscompat_write below for unwound actions.
  1213. *
  1214. * XXX - Parts get retired eventually.
  1215. * Unfortunately new bits get added.
  1216. */
  1217. static void
  1218. ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
  1219. {
  1220. off_t maxfilesize;
  1221. int32_t *extrasave;
  1222. if ((fs->fs_magic != FS_UFS1_MAGIC) ||
  1223. (fs->fs_old_flags & FS_FLAGS_UPDATED))
  1224. return;
  1225. if (!ump->um_oldfscompat)
  1226. ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
  1227. M_UFSMNT, M_WAITOK);
  1228. memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
  1229. extrasave = ump->um_oldfscompat;
  1230. extrasave += 512/sizeof(int32_t);
  1231. extrasave[0] = fs->fs_old_npsect;
  1232. extrasave[1] = fs->fs_old_interleave;
  1233. extrasave[2] = fs->fs_old_trackskew;
  1234. /* These fields will be overwritten by their
  1235. * original values in fs_oldfscompat_write, so it is harmless
  1236. * to modify them here.
  1237. */
  1238. fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
  1239. fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
  1240. fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
  1241. fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
  1242. fs->fs_maxbsize = fs->fs_bsize;
  1243. fs->fs_time = fs->fs_old_time;
  1244. fs->fs_size = fs->fs_old_size;
  1245. fs->fs_dsize = fs->fs_old_dsize;
  1246. fs->fs_csaddr = fs->fs_old_csaddr;
  1247. fs->fs_sblockloc = sblockloc;
  1248. fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
  1249. if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
  1250. fs->fs_old_nrpos = 8;
  1251. fs->fs_old_npsect = fs->fs_old_nsect;
  1252. fs->fs_old_interleave = 1;
  1253. fs->fs_old_trackskew = 0;
  1254. }
  1255. if (fs->fs_old_inodefmt < FS_44INODEFMT) {
  1256. fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
  1257. fs->fs_qbmask = ~fs->fs_bmask;
  1258. fs->fs_qfmask = ~fs->fs_fmask;
  1259. }
  1260. maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
  1261. if (fs->fs_maxfilesize > maxfilesize)
  1262. fs->fs_maxfilesize = maxfilesize;
  1263. /* Compatibility for old filesystems */
  1264. if (fs->fs_avgfilesize <= 0)
  1265. fs->fs_avgfilesize = AVFILESIZ;
  1266. if (fs->fs_avgfpdir <= 0)
  1267. fs->fs_avgfpdir = AFPDIR;
  1268. #if 0
  1269. if (bigcgs) {
  1270. fs->fs_save_cgsize = fs->fs_cgsize;
  1271. fs->fs_cgsize = fs->fs_bsize;
  1272. }
  1273. #endif
  1274. }
  1275. /*
  1276. * Unwinding superblock updates for old filesystems.
  1277. * See ffs_oldfscompat_read above for details.
  1278. *
  1279. * XXX - Parts get retired eventually.
  1280. * Unfortunately new bits get added.
  1281. */
  1282. static void
  1283. ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
  1284. {
  1285. int32_t *extrasave;
  1286. if ((fs->fs_magic != FS_UFS1_MAGIC) ||
  1287. (fs->fs_old_flags & FS_FLAGS_UPDATED))
  1288. return;
  1289. fs->fs_old_time = fs->fs_time;
  1290. fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
  1291. fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
  1292. fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
  1293. fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
  1294. fs->fs_old_flags = fs->fs_flags;
  1295. #if 0
  1296. if (bigcgs) {
  1297. fs->fs_cgsize = fs->fs_save_cgsize;
  1298. }
  1299. #endif
  1300. memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
  1301. extrasave = ump->um_oldfscompat;
  1302. extrasave += 512/sizeof(int32_t);
  1303. fs->fs_old_npsect = extrasave[0];
  1304. fs->fs_old_interleave = extrasave[1];
  1305. fs->fs_old_trackskew = extrasave[2];
  1306. }
  1307. /*
  1308. * unmount vfs operation
  1309. */
  1310. int
  1311. ffs_unmount(struct mount *mp, int mntflags)
  1312. {
  1313. struct lwp *l = curlwp;
  1314. struct ufsmount *ump = VFSTOUFS(mp);
  1315. struct fs *fs = ump->um_fs;
  1316. int error, flags;
  1317. #ifdef WAPBL
  1318. extern int doforce;
  1319. #endif
  1320. flags = 0;
  1321. if (mntflags & MNT_FORCE)
  1322. flags |= FORCECLOSE;
  1323. if ((error = ffs_flushfiles(mp, flags, l)) != 0)
  1324. return (error);
  1325. error = UFS_WAPBL_BEGIN(mp);
  1326. if (error == 0)
  1327. if (fs->fs_ronly == 0 &&
  1328. ffs_cgupdate(ump, MNT_WAIT) == 0 &&
  1329. fs->fs_clean & FS_WASCLEAN) {
  1330. fs->fs_clean = FS_ISCLEAN;
  1331. fs->fs_fmod = 0;
  1332. (void) ffs_sbupdate(ump, MNT_WAIT);
  1333. }
  1334. if (error == 0)
  1335. UFS_WAPBL_END(mp);
  1336. #ifdef WAPBL
  1337. KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
  1338. if (mp->mnt_wapbl_replay) {
  1339. KDASSERT(fs->fs_ronly);
  1340. wapbl_replay_stop(mp->mnt_wapbl_replay);
  1341. wapbl_replay_free(mp->mnt_wapbl_replay);
  1342. mp->mnt_wapbl_replay = 0;
  1343. }
  1344. error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
  1345. if (error) {
  1346. return error;
  1347. }
  1348. #endif /* WAPBL */
  1349. #ifdef UFS_EXTATTR
  1350. if (ump->um_fstype == UFS1) {
  1351. ufs_extattr_stop(mp, l);
  1352. ufs_extattr_uepm_destroy(&ump->um_extattr);
  1353. }
  1354. #endif /* UFS_EXTATTR */
  1355. if (ump->um_devvp->v_type != VBAD)
  1356. ump->um_devvp->v_specmountpoint = NULL;
  1357. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
  1358. (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
  1359. NOCRED);
  1360. vput(ump->um_devvp);
  1361. free(fs->fs_csp, M_UFSMNT);
  1362. free(fs, M_UFSMNT);
  1363. if (ump->um_oldfscompat != NULL)
  1364. free(ump->um_oldfscompat, M_UFSMNT);
  1365. mutex_destroy(&ump->um_lock);
  1366. ffs_snapshot_fini(ump);
  1367. free(ump, M_UFSMNT);
  1368. mp->mnt_data = NULL;
  1369. mp->mnt_flag &= ~MNT_LOCAL;
  1370. fstrans_unmount(mp);
  1371. return (0);
  1372. }
  1373. /*
  1374. * Flush out all the files in a filesystem.
  1375. */
  1376. int
  1377. ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
  1378. {
  1379. extern int doforce;
  1380. struct ufsmount *ump;
  1381. int error;
  1382. if (!doforce)
  1383. flags &= ~FORCECLOSE;
  1384. ump = VFSTOUFS(mp);
  1385. #ifdef QUOTA
  1386. if ((error = quota1_umount(mp, flags)) != 0)
  1387. return (error);
  1388. #endif
  1389. #ifdef QUOTA2
  1390. if ((error = quota2_umount(mp, flags)) != 0)
  1391. return (error);
  1392. #endif
  1393. if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
  1394. return (error);
  1395. ffs_snapshot_unmount(mp);
  1396. /*
  1397. * Flush all the files.
  1398. */
  1399. error = vflush(mp, NULLVP, flags);
  1400. if (error)
  1401. return (error);
  1402. /*
  1403. * Flush filesystem metadata.
  1404. */
  1405. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
  1406. error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
  1407. VOP_UNLOCK(ump->um_devvp);
  1408. if (flags & FORCECLOSE) /* XXXDBJ */
  1409. error = 0;
  1410. #ifdef WAPBL
  1411. if (error)
  1412. return error;
  1413. if (mp->mnt_wapbl) {
  1414. error = wapbl_flush(mp->mnt_wapbl, 1);
  1415. if (flags & FORCECLOSE)
  1416. error = 0;
  1417. }
  1418. #endif
  1419. return (error);
  1420. }
  1421. /*
  1422. * Get file system statistics.
  1423. */
  1424. int
  1425. ffs_statvfs(struct mount *mp, struct statvfs *sbp)
  1426. {
  1427. struct ufsmount *ump;
  1428. struct fs *fs;
  1429. ump = VFSTOUFS(mp);
  1430. fs = ump->um_fs;
  1431. mutex_enter(&ump->um_lock);
  1432. sbp->f_bsize = fs->fs_bsize;
  1433. sbp->f_frsize = fs->fs_fsize;
  1434. sbp->f_iosize = fs->fs_bsize;
  1435. sbp->f_blocks = fs->fs_dsize;
  1436. sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
  1437. fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
  1438. sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
  1439. fs->fs_minfree) / (u_int64_t) 100;
  1440. if (sbp->f_bfree > sbp->f_bresvd)
  1441. sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
  1442. else
  1443. sbp->f_bavail = 0;
  1444. sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
  1445. sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
  1446. sbp->f_favail = sbp->f_ffree;
  1447. sbp->f_fresvd = 0;
  1448. mutex_exit(&ump->um_lock);
  1449. copy_statvfs_info(sbp, mp);
  1450. return (0);
  1451. }
  1452. /*
  1453. * Go through the disk queues to initiate sandbagged IO;
  1454. * go through the inodes to write those that have been modified;
  1455. * initiate the writing of the super block if it has been modified.
  1456. *
  1457. * Note: we are always called with the filesystem marked `MPBUSY'.
  1458. */
  1459. int
  1460. ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
  1461. {
  1462. struct vnode *vp, *mvp, *nvp;
  1463. struct inode *ip;
  1464. struct ufsmount *ump = VFSTOUFS(mp);
  1465. struct fs *fs;
  1466. int error, allerror = 0;
  1467. bool is_suspending;
  1468. fs = ump->um_fs;
  1469. if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
  1470. printf("fs = %s\n", fs->fs_fsmnt);
  1471. panic("update: rofs mod");
  1472. }
  1473. /* Allocate a marker vnode. */
  1474. mvp = vnalloc(mp);
  1475. fstrans_start(mp, FSTRANS_SHARED);
  1476. is_suspending = (fstrans_getstate(mp) == FSTRANS_SUSPENDING);
  1477. /*
  1478. * Write back each (modified) inode.
  1479. */
  1480. mutex_enter(&mntvnode_lock);
  1481. loop:
  1482. /*
  1483. * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
  1484. * and vclean() can be called indirectly
  1485. */
  1486. for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
  1487. nvp = TAILQ_NEXT(vp, v_mntvnodes);
  1488. /*
  1489. * If the vnode that we are about to sync is no longer
  1490. * associated with this mount point, start over.
  1491. */
  1492. if (vp->v_mount != mp)
  1493. goto loop;
  1494. /*
  1495. * Don't interfere with concurrent scans of this FS.
  1496. */
  1497. if (vismarker(vp))
  1498. continue;
  1499. mutex_enter(vp->v_interlock);
  1500. ip = VTOI(vp);
  1501. /*
  1502. * Skip the vnode/inode if inaccessible.
  1503. */
  1504. if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
  1505. vp->v_type == VNON) {
  1506. mutex_exit(vp->v_interlock);
  1507. continue;
  1508. }
  1509. /*
  1510. * We deliberately update inode times here. This will
  1511. * prevent a massive queue of updates accumulating, only
  1512. * to be handled by a call to unmount.
  1513. *
  1514. * XXX It would be better to have the syncer trickle these
  1515. * out. Adjustment needed to allow registering vnodes for
  1516. * sync when the vnode is clean, but the inode dirty. Or
  1517. * have ufs itself trickle out inode updates.
  1518. *
  1519. * If doing a lazy sync, we don't care about metadata or
  1520. * data updates, because they are handled by each vnode's
  1521. * synclist entry. In this case we are only interested in
  1522. * writing back modified inodes.
  1523. */
  1524. if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
  1525. IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
  1526. (waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
  1527. UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
  1528. mutex_exit(vp->v_interlock);
  1529. continue;
  1530. }
  1531. if (vp->v_type == VBLK && is_suspending) {
  1532. mutex_exit(vp->v_interlock);
  1533. continue;
  1534. }
  1535. vmark(mvp, vp);
  1536. mutex_exit(&mntvnode_lock);
  1537. error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT);
  1538. if (error) {
  1539. mutex_enter(&mntvnode_lock);
  1540. nvp = vunmark(mvp);
  1541. if (error == ENOENT) {
  1542. goto loop;
  1543. }
  1544. continue;
  1545. }
  1546. if (waitfor == MNT_LAZY) {
  1547. error = UFS_WAPBL_BEGIN(vp->v_mount);
  1548. if (!error) {
  1549. error = ffs_update(vp, NULL, NULL,
  1550. UPDATE_CLOSE);
  1551. UFS_WAPBL_END(vp->v_mount);
  1552. }
  1553. } else {
  1554. error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
  1555. (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
  1556. }
  1557. if (error)
  1558. allerror = error;
  1559. vput(vp);
  1560. mutex_enter(&mntvnode_lock);
  1561. nvp = vunmark(mvp);
  1562. }
  1563. mutex_exit(&mntvnode_lock);
  1564. /*
  1565. * Force stale file system control information to be flushed.
  1566. */
  1567. if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
  1568. !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
  1569. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
  1570. if ((error = VOP_FSYNC(ump->um_devvp, cred,
  1571. (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
  1572. 0, 0)) != 0)
  1573. allerror = error;
  1574. VOP_UNLOCK(ump->um_devvp);
  1575. if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
  1576. mutex_enter(&mntvnode_lock);
  1577. goto loop;
  1578. }
  1579. }
  1580. #if defined(QUOTA) || defined(QUOTA2)
  1581. qsync(mp);
  1582. #endif
  1583. /*
  1584. * Write back modified superblock.
  1585. */
  1586. if (fs->fs_fmod != 0) {
  1587. fs->fs_fmod = 0;
  1588. fs->fs_time = time_second;
  1589. error = UFS_WAPBL_BEGIN(mp);
  1590. if (error)
  1591. allerror = error;
  1592. else {
  1593. if ((error = ffs_cgupdate(ump, waitfor)))
  1594. allerror = error;
  1595. UFS_WAPBL_END(mp);
  1596. }
  1597. }
  1598. #ifdef WAPBL
  1599. if (mp->mnt_wapbl) {
  1600. error = wapbl_flush(mp->mnt_wapbl, 0);
  1601. if (error)
  1602. allerror = error;
  1603. }
  1604. #endif
  1605. fstrans_done(mp);
  1606. vnfree(mvp);
  1607. return (allerror);
  1608. }
  1609. /*
  1610. * Look up a FFS dinode number to find its incore vnode, otherwise read it
  1611. * in from disk. If it is in core, wait for the lock bit to clear, then
  1612. * return the inode locked. Detection and handling of mount points must be
  1613. * done by the calling routine.
  1614. */
  1615. int
  1616. ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
  1617. {
  1618. struct fs *fs;
  1619. struct inode *ip;
  1620. struct ufsmount *ump;
  1621. struct buf *bp;
  1622. struct vnode *vp;
  1623. dev_t dev;
  1624. int error;
  1625. ump = VFSTOUFS(mp);
  1626. dev = ump->um_dev;
  1627. retry:
  1628. if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
  1629. return (0);
  1630. /* Allocate a new vnode/inode. */
  1631. error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, NULL, &vp);
  1632. if (error) {
  1633. *vpp = NULL;
  1634. return (error);
  1635. }
  1636. ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
  1637. /*
  1638. * If someone beat us to it, put back the freshly allocated
  1639. * vnode/inode pair and retry.
  1640. */
  1641. mutex_enter(&ufs_hashlock);
  1642. if (ufs_ihashget(dev, ino, 0) != NULL) {
  1643. mutex_exit(&ufs_hashlock);
  1644. ungetnewvnode(vp);
  1645. pool_cache_put(ffs_inode_cache, ip);
  1646. goto retry;
  1647. }
  1648. vp->v_vflag |= VV_LOCKSWORK;
  1649. /*
  1650. * XXX MFS ends up here, too, to allocate an inode. Should we
  1651. * XXX create another pool for MFS inodes?
  1652. */
  1653. memset(ip, 0, sizeof(struct inode));
  1654. vp->v_data = ip;
  1655. ip->i_vnode = vp;
  1656. ip->i_ump = ump;
  1657. ip->i_fs = fs = ump->um_fs;
  1658. ip->i_dev = dev;
  1659. ip->i_number = ino;
  1660. #if defined(QUOTA) || defined(QUOTA2)
  1661. ufsquota_init(ip);
  1662. #endif
  1663. /*
  1664. * Initialize genfs node, we might proceed to destroy it in
  1665. * error branches.
  1666. */
  1667. genfs_node_init(vp, &ffs_genfsops);
  1668. /*
  1669. * Put it onto its hash chain and lock it so that other requests for
  1670. * this inode will block if they arrive while we are sleeping waiting
  1671. * for old data structures to be purged or for the contents of the
  1672. * disk portion of this inode to be read.
  1673. */
  1674. ufs_ihashins(ip);
  1675. mutex_exit(&ufs_hashlock);
  1676. /* Read in the disk contents for the inode, copy into the inode. */
  1677. error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
  1678. (int)fs->fs_bsize, NOCRED, 0, &bp);
  1679. if (error) {
  1680. /*
  1681. * The inode does not contain anything useful, so it would
  1682. * be misleading to leave it on its hash chain. With mode
  1683. * still zero, it will be unlinked and returned to the free
  1684. * list by vput().
  1685. */
  1686. vput(vp);
  1687. brelse(bp, 0);
  1688. *vpp = NULL;
  1689. return (error);
  1690. }
  1691. if (ip->i_ump->um_fstype == UFS1)
  1692. ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
  1693. PR_WAITOK);
  1694. else
  1695. ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
  1696. PR_WAITOK);
  1697. ffs_load_inode(bp, ip, fs, ino);
  1698. brelse(bp, 0);
  1699. /*
  1700. * Initialize the vnode from the inode, check for aliases.
  1701. * Note that the underlying vnode may have changed.
  1702. */
  1703. ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
  1704. /*
  1705. * Finish inode initialization now that aliasing has been resolved.
  1706. */
  1707. ip->i_devvp = ump->um_devvp;
  1708. vref(ip->i_devvp);
  1709. /*
  1710. * Ensure that uid and gid are correct. This is a temporary
  1711. * fix until fsck has been changed to do the update.
  1712. */
  1713. if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
  1714. ip->i_uid = ip->i_ffs1_ouid; /* XXX */
  1715. ip->i_gid = ip->i_ffs1_ogid; /* XXX */
  1716. } /* XXX */
  1717. uvm_vnp_setsize(vp, ip->i_size);
  1718. *vpp = vp;
  1719. return (0);
  1720. }
  1721. /*
  1722. * File handle to vnode
  1723. *
  1724. * Have to be really careful about stale file handles:
  1725. * - check that the inode number is valid
  1726. * - call ffs_vget() to get the locked inode
  1727. * - check for an unallocated inode (i_mode == 0)
  1728. * - check that the given client host has export rights and return
  1729. * those rights via. exflagsp and credanonp
  1730. */
  1731. int
  1732. ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
  1733. {
  1734. struct ufid ufh;
  1735. struct fs *fs;
  1736. if (fhp->fid_len != sizeof(struct ufid))
  1737. return EINVAL;
  1738. memcpy(&ufh, fhp, sizeof(ufh));
  1739. fs = VFSTOUFS(mp)->um_fs;
  1740. if (ufh.ufid_ino < ROOTINO ||
  1741. ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
  1742. return (ESTALE);
  1743. return (ufs_fhtovp(mp, &ufh, vpp));
  1744. }
  1745. /*
  1746. * Vnode pointer to File handle
  1747. */
  1748. /* ARGSUSED */
  1749. int
  1750. ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
  1751. {
  1752. struct inode *ip;
  1753. struct ufid ufh;
  1754. if (*fh_size < sizeof(struct ufid)) {
  1755. *fh_size = sizeof(struct ufid);
  1756. return E2BIG;
  1757. }
  1758. ip = VTOI(vp);
  1759. *fh_size = sizeof(struct ufid);
  1760. memset(&ufh, 0, sizeof(ufh));
  1761. ufh.ufid_len = sizeof(struct ufid);
  1762. ufh.ufid_ino = ip->i_number;
  1763. ufh.ufid_gen = ip->i_gen;
  1764. memcpy(fhp, &ufh, sizeof(ufh));
  1765. return (0);
  1766. }
  1767. void
  1768. ffs_init(void)
  1769. {
  1770. if (ffs_initcount++ > 0)
  1771. return;
  1772. ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
  1773. "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
  1774. ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
  1775. "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
  1776. ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
  1777. "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
  1778. ufs_init();
  1779. }
  1780. void
  1781. ffs_reinit(void)
  1782. {
  1783. ufs_reinit();
  1784. }
  1785. void
  1786. ffs_done(void)
  1787. {
  1788. if (--ffs_initcount > 0)
  1789. return;
  1790. ufs_done();
  1791. pool_cache_destroy(ffs_dinode2_cache);
  1792. pool_cache_destroy(ffs_dinode1_cache);
  1793. pool_cache_destroy(ffs_inode_cache);
  1794. }
  1795. /*
  1796. * Write a superblock and associated information back to disk.
  1797. */
  1798. int
  1799. ffs_sbupdate(struct ufsmou

Large files files are truncated, but you can click here to view the full file